utsname.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright (C) 2004 IBM Corporation
  3. *
  4. * Author: Serge Hallyn <serue@us.ibm.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, version 2 of the
  9. * License.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/uts.h>
  13. #include <linux/utsname.h>
  14. #include <linux/err.h>
  15. #include <linux/slab.h>
  16. static struct uts_namespace *create_uts_ns(void)
  17. {
  18. struct uts_namespace *uts_ns;
  19. uts_ns = kmalloc(sizeof(struct uts_namespace), GFP_KERNEL);
  20. if (uts_ns)
  21. kref_init(&uts_ns->kref);
  22. return uts_ns;
  23. }
  24. /*
  25. * Clone a new ns copying an original utsname, setting refcount to 1
  26. * @old_ns: namespace to clone
  27. * Return NULL on error (failure to kmalloc), new ns otherwise
  28. */
  29. static struct uts_namespace *clone_uts_ns(struct uts_namespace *old_ns)
  30. {
  31. struct uts_namespace *ns;
  32. ns = create_uts_ns();
  33. if (!ns)
  34. return ERR_PTR(-ENOMEM);
  35. down_read(&uts_sem);
  36. memcpy(&ns->name, &old_ns->name, sizeof(ns->name));
  37. up_read(&uts_sem);
  38. return ns;
  39. }
  40. /*
  41. * Copy task tsk's utsname namespace, or clone it if flags
  42. * specifies CLONE_NEWUTS. In latter case, changes to the
  43. * utsname of this process won't be seen by parent, and vice
  44. * versa.
  45. */
  46. struct uts_namespace *copy_utsname(unsigned long flags, struct uts_namespace *old_ns)
  47. {
  48. struct uts_namespace *new_ns;
  49. BUG_ON(!old_ns);
  50. get_uts_ns(old_ns);
  51. if (!(flags & CLONE_NEWUTS))
  52. return old_ns;
  53. new_ns = clone_uts_ns(old_ns);
  54. put_uts_ns(old_ns);
  55. return new_ns;
  56. }
  57. void free_uts_ns(struct kref *kref)
  58. {
  59. struct uts_namespace *ns;
  60. ns = container_of(kref, struct uts_namespace, kref);
  61. kfree(ns);
  62. }