utsname.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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/version.h>
  15. /*
  16. * Clone a new ns copying an original utsname, setting refcount to 1
  17. * @old_ns: namespace to clone
  18. * Return NULL on error (failure to kmalloc), new ns otherwise
  19. */
  20. static struct uts_namespace *clone_uts_ns(struct uts_namespace *old_ns)
  21. {
  22. struct uts_namespace *ns;
  23. ns = kmalloc(sizeof(struct uts_namespace), GFP_KERNEL);
  24. if (ns) {
  25. memcpy(&ns->name, &old_ns->name, sizeof(ns->name));
  26. kref_init(&ns->kref);
  27. }
  28. return ns;
  29. }
  30. /*
  31. * Copy task tsk's utsname namespace, or clone it if flags
  32. * specifies CLONE_NEWUTS. In latter case, changes to the
  33. * utsname of this process won't be seen by parent, and vice
  34. * versa.
  35. */
  36. struct uts_namespace *copy_utsname(int flags, struct uts_namespace *old_ns)
  37. {
  38. struct uts_namespace *new_ns;
  39. BUG_ON(!old_ns);
  40. get_uts_ns(old_ns);
  41. if (!(flags & CLONE_NEWUTS))
  42. return old_ns;
  43. new_ns = clone_uts_ns(old_ns);
  44. put_uts_ns(old_ns);
  45. return new_ns;
  46. }
  47. void free_uts_ns(struct kref *kref)
  48. {
  49. struct uts_namespace *ns;
  50. ns = container_of(kref, struct uts_namespace, kref);
  51. kfree(ns);
  52. }