utsname.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #include <linux/user_namespace.h>
  17. static struct uts_namespace *create_uts_ns(void)
  18. {
  19. struct uts_namespace *uts_ns;
  20. uts_ns = kmalloc(sizeof(struct uts_namespace), GFP_KERNEL);
  21. if (uts_ns)
  22. kref_init(&uts_ns->kref);
  23. return uts_ns;
  24. }
  25. /*
  26. * Clone a new ns copying an original utsname, setting refcount to 1
  27. * @old_ns: namespace to clone
  28. * Return NULL on error (failure to kmalloc), new ns otherwise
  29. */
  30. static struct uts_namespace *clone_uts_ns(struct uts_namespace *old_ns)
  31. {
  32. struct uts_namespace *ns;
  33. ns = create_uts_ns();
  34. if (!ns)
  35. return ERR_PTR(-ENOMEM);
  36. down_read(&uts_sem);
  37. memcpy(&ns->name, &old_ns->name, sizeof(ns->name));
  38. ns->user_ns = old_ns->user_ns;
  39. get_user_ns(ns->user_ns);
  40. up_read(&uts_sem);
  41. return ns;
  42. }
  43. /*
  44. * Copy task tsk's utsname namespace, or clone it if flags
  45. * specifies CLONE_NEWUTS. In latter case, changes to the
  46. * utsname of this process won't be seen by parent, and vice
  47. * versa.
  48. */
  49. struct uts_namespace *copy_utsname(unsigned long flags, struct uts_namespace *old_ns)
  50. {
  51. struct uts_namespace *new_ns;
  52. BUG_ON(!old_ns);
  53. get_uts_ns(old_ns);
  54. if (!(flags & CLONE_NEWUTS))
  55. return old_ns;
  56. new_ns = clone_uts_ns(old_ns);
  57. put_uts_ns(old_ns);
  58. return new_ns;
  59. }
  60. void free_uts_ns(struct kref *kref)
  61. {
  62. struct uts_namespace *ns;
  63. ns = container_of(kref, struct uts_namespace, kref);
  64. put_user_ns(ns->user_ns);
  65. kfree(ns);
  66. }