utsname.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. * unshare the current process' utsname namespace.
  32. * called only in sys_unshare()
  33. */
  34. int unshare_utsname(unsigned long unshare_flags, struct uts_namespace **new_uts)
  35. {
  36. if (unshare_flags & CLONE_NEWUTS) {
  37. if (!capable(CAP_SYS_ADMIN))
  38. return -EPERM;
  39. *new_uts = clone_uts_ns(current->nsproxy->uts_ns);
  40. if (!*new_uts)
  41. return -ENOMEM;
  42. }
  43. return 0;
  44. }
  45. /*
  46. * Copy task tsk's utsname namespace, or clone it if flags
  47. * specifies CLONE_NEWUTS. In latter case, changes to the
  48. * utsname of this process won't be seen by parent, and vice
  49. * versa.
  50. */
  51. int copy_utsname(int flags, struct task_struct *tsk)
  52. {
  53. struct uts_namespace *old_ns = tsk->nsproxy->uts_ns;
  54. struct uts_namespace *new_ns;
  55. int err = 0;
  56. if (!old_ns)
  57. return 0;
  58. get_uts_ns(old_ns);
  59. if (!(flags & CLONE_NEWUTS))
  60. return 0;
  61. if (!capable(CAP_SYS_ADMIN)) {
  62. err = -EPERM;
  63. goto out;
  64. }
  65. new_ns = clone_uts_ns(old_ns);
  66. if (!new_ns) {
  67. err = -ENOMEM;
  68. goto out;
  69. }
  70. tsk->nsproxy->uts_ns = new_ns;
  71. out:
  72. put_uts_ns(old_ns);
  73. return err;
  74. }
  75. void free_uts_ns(struct kref *kref)
  76. {
  77. struct uts_namespace *ns;
  78. ns = container_of(kref, struct uts_namespace, kref);
  79. kfree(ns);
  80. }