nsproxy.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 = kmemdup(orig, sizeof(struct nsproxy), GFP_KERNEL);
  42. if (ns)
  43. atomic_set(&ns->count, 1);
  44. return ns;
  45. }
  46. /*
  47. * copies the nsproxy, setting refcount to 1, and grabbing a
  48. * reference to all contained namespaces. Called from
  49. * sys_unshare()
  50. */
  51. struct nsproxy *dup_namespaces(struct nsproxy *orig)
  52. {
  53. struct nsproxy *ns = clone_namespaces(orig);
  54. if (ns) {
  55. if (ns->namespace)
  56. get_namespace(ns->namespace);
  57. if (ns->uts_ns)
  58. get_uts_ns(ns->uts_ns);
  59. if (ns->ipc_ns)
  60. get_ipc_ns(ns->ipc_ns);
  61. }
  62. return ns;
  63. }
  64. /*
  65. * called from clone. This now handles copy for nsproxy and all
  66. * namespaces therein.
  67. */
  68. int copy_namespaces(int flags, struct task_struct *tsk)
  69. {
  70. struct nsproxy *old_ns = tsk->nsproxy;
  71. struct nsproxy *new_ns;
  72. int err = 0;
  73. if (!old_ns)
  74. return 0;
  75. get_nsproxy(old_ns);
  76. if (!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC)))
  77. return 0;
  78. new_ns = clone_namespaces(old_ns);
  79. if (!new_ns) {
  80. err = -ENOMEM;
  81. goto out;
  82. }
  83. tsk->nsproxy = new_ns;
  84. err = copy_namespace(flags, tsk);
  85. if (err)
  86. goto out_ns;
  87. err = copy_utsname(flags, tsk);
  88. if (err)
  89. goto out_uts;
  90. err = copy_ipcs(flags, tsk);
  91. if (err)
  92. goto out_ipc;
  93. out:
  94. put_nsproxy(old_ns);
  95. return err;
  96. out_ipc:
  97. if (new_ns->uts_ns)
  98. put_uts_ns(new_ns->uts_ns);
  99. out_uts:
  100. if (new_ns->namespace)
  101. put_namespace(new_ns->namespace);
  102. out_ns:
  103. tsk->nsproxy = old_ns;
  104. kfree(new_ns);
  105. goto out;
  106. }
  107. void free_nsproxy(struct nsproxy *ns)
  108. {
  109. if (ns->namespace)
  110. put_namespace(ns->namespace);
  111. if (ns->uts_ns)
  112. put_uts_ns(ns->uts_ns);
  113. if (ns->ipc_ns)
  114. put_ipc_ns(ns->ipc_ns);
  115. kfree(ns);
  116. }