utsname.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #include <linux/proc_fs.h>
  18. static struct uts_namespace *create_uts_ns(void)
  19. {
  20. struct uts_namespace *uts_ns;
  21. uts_ns = kmalloc(sizeof(struct uts_namespace), GFP_KERNEL);
  22. if (uts_ns)
  23. kref_init(&uts_ns->kref);
  24. return uts_ns;
  25. }
  26. /*
  27. * Clone a new ns copying an original utsname, setting refcount to 1
  28. * @old_ns: namespace to clone
  29. * Return NULL on error (failure to kmalloc), new ns otherwise
  30. */
  31. static struct uts_namespace *clone_uts_ns(struct task_struct *tsk,
  32. struct uts_namespace *old_ns)
  33. {
  34. struct uts_namespace *ns;
  35. ns = create_uts_ns();
  36. if (!ns)
  37. return ERR_PTR(-ENOMEM);
  38. down_read(&uts_sem);
  39. memcpy(&ns->name, &old_ns->name, sizeof(ns->name));
  40. ns->user_ns = get_user_ns(task_cred_xxx(tsk, user)->user_ns);
  41. up_read(&uts_sem);
  42. return ns;
  43. }
  44. /*
  45. * Copy task tsk's utsname namespace, or clone it if flags
  46. * specifies CLONE_NEWUTS. In latter case, changes to the
  47. * utsname of this process won't be seen by parent, and vice
  48. * versa.
  49. */
  50. struct uts_namespace *copy_utsname(unsigned long flags,
  51. struct task_struct *tsk)
  52. {
  53. struct uts_namespace *old_ns = tsk->nsproxy->uts_ns;
  54. struct uts_namespace *new_ns;
  55. BUG_ON(!old_ns);
  56. get_uts_ns(old_ns);
  57. if (!(flags & CLONE_NEWUTS))
  58. return old_ns;
  59. new_ns = clone_uts_ns(tsk, old_ns);
  60. put_uts_ns(old_ns);
  61. return new_ns;
  62. }
  63. void free_uts_ns(struct kref *kref)
  64. {
  65. struct uts_namespace *ns;
  66. ns = container_of(kref, struct uts_namespace, kref);
  67. put_user_ns(ns->user_ns);
  68. kfree(ns);
  69. }
  70. static void *utsns_get(struct task_struct *task)
  71. {
  72. struct uts_namespace *ns = NULL;
  73. struct nsproxy *nsproxy;
  74. rcu_read_lock();
  75. nsproxy = task_nsproxy(task);
  76. if (nsproxy) {
  77. ns = nsproxy->uts_ns;
  78. get_uts_ns(ns);
  79. }
  80. rcu_read_unlock();
  81. return ns;
  82. }
  83. static void utsns_put(void *ns)
  84. {
  85. put_uts_ns(ns);
  86. }
  87. static int utsns_install(struct nsproxy *nsproxy, void *ns)
  88. {
  89. get_uts_ns(ns);
  90. put_uts_ns(nsproxy->uts_ns);
  91. nsproxy->uts_ns = ns;
  92. return 0;
  93. }
  94. const struct proc_ns_operations utsns_operations = {
  95. .name = "uts",
  96. .type = CLONE_NEWUTS,
  97. .get = utsns_get,
  98. .put = utsns_put,
  99. .install = utsns_install,
  100. };