utsname.c 1.5 KB

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