nsproxy.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (C) 2006 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. * Jun 2006 - namespaces support
  12. * OpenVZ, SWsoft Inc.
  13. * Pavel Emelianov <xemul@openvz.org>
  14. */
  15. #include <linux/module.h>
  16. #include <linux/version.h>
  17. #include <linux/nsproxy.h>
  18. #include <linux/init_task.h>
  19. #include <linux/namespace.h>
  20. #include <linux/utsname.h>
  21. struct nsproxy init_nsproxy = INIT_NSPROXY(init_nsproxy);
  22. static inline void get_nsproxy(struct nsproxy *ns)
  23. {
  24. atomic_inc(&ns->count);
  25. }
  26. void get_task_namespaces(struct task_struct *tsk)
  27. {
  28. struct nsproxy *ns = tsk->nsproxy;
  29. if (ns) {
  30. get_nsproxy(ns);
  31. }
  32. }
  33. /*
  34. * creates a copy of "orig" with refcount 1.
  35. * This does not grab references to the contained namespaces,
  36. * so that needs to be done by dup_namespaces.
  37. */
  38. static inline struct nsproxy *clone_namespaces(struct nsproxy *orig)
  39. {
  40. struct nsproxy *ns;
  41. ns = kmalloc(sizeof(struct nsproxy), GFP_KERNEL);
  42. if (ns) {
  43. memcpy(ns, orig, sizeof(struct nsproxy));
  44. atomic_set(&ns->count, 1);
  45. }
  46. return ns;
  47. }
  48. /*
  49. * copies the nsproxy, setting refcount to 1, and grabbing a
  50. * reference to all contained namespaces. Called from
  51. * sys_unshare()
  52. */
  53. struct nsproxy *dup_namespaces(struct nsproxy *orig)
  54. {
  55. struct nsproxy *ns = clone_namespaces(orig);
  56. if (ns) {
  57. if (ns->namespace)
  58. get_namespace(ns->namespace);
  59. if (ns->uts_ns)
  60. get_uts_ns(ns->uts_ns);
  61. if (ns->ipc_ns)
  62. get_ipc_ns(ns->ipc_ns);
  63. }
  64. return ns;
  65. }
  66. /*
  67. * called from clone. This now handles copy for nsproxy and all
  68. * namespaces therein.
  69. */
  70. int copy_namespaces(int flags, struct task_struct *tsk)
  71. {
  72. struct nsproxy *old_ns = tsk->nsproxy;
  73. struct nsproxy *new_ns;
  74. int err = 0;
  75. if (!old_ns)
  76. return 0;
  77. get_nsproxy(old_ns);
  78. if (!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC)))
  79. return 0;
  80. new_ns = clone_namespaces(old_ns);
  81. if (!new_ns) {
  82. err = -ENOMEM;
  83. goto out;
  84. }
  85. tsk->nsproxy = new_ns;
  86. err = copy_namespace(flags, tsk);
  87. if (err)
  88. goto out_ns;
  89. err = copy_utsname(flags, tsk);
  90. if (err)
  91. goto out_uts;
  92. err = copy_ipcs(flags, tsk);
  93. if (err)
  94. goto out_ipc;
  95. out:
  96. put_nsproxy(old_ns);
  97. return err;
  98. out_ipc:
  99. if (new_ns->uts_ns)
  100. put_uts_ns(new_ns->uts_ns);
  101. out_uts:
  102. if (new_ns->namespace)
  103. put_namespace(new_ns->namespace);
  104. out_ns:
  105. tsk->nsproxy = old_ns;
  106. kfree(new_ns);
  107. goto out;
  108. }
  109. void free_nsproxy(struct nsproxy *ns)
  110. {
  111. if (ns->namespace)
  112. put_namespace(ns->namespace);
  113. if (ns->uts_ns)
  114. put_uts_ns(ns->uts_ns);
  115. if (ns->ipc_ns)
  116. put_ipc_ns(ns->ipc_ns);
  117. kfree(ns);
  118. }