nsproxy.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #include <linux/module.h>
  12. #include <linux/version.h>
  13. #include <linux/nsproxy.h>
  14. #include <linux/init_task.h>
  15. #include <linux/namespace.h>
  16. struct nsproxy init_nsproxy = INIT_NSPROXY(init_nsproxy);
  17. static inline void get_nsproxy(struct nsproxy *ns)
  18. {
  19. atomic_inc(&ns->count);
  20. }
  21. void get_task_namespaces(struct task_struct *tsk)
  22. {
  23. struct nsproxy *ns = tsk->nsproxy;
  24. if (ns) {
  25. get_nsproxy(ns);
  26. }
  27. }
  28. /*
  29. * creates a copy of "orig" with refcount 1.
  30. * This does not grab references to the contained namespaces,
  31. * so that needs to be done by dup_namespaces.
  32. */
  33. static inline struct nsproxy *clone_namespaces(struct nsproxy *orig)
  34. {
  35. struct nsproxy *ns;
  36. ns = kmalloc(sizeof(struct nsproxy), GFP_KERNEL);
  37. if (ns) {
  38. memcpy(ns, orig, sizeof(struct nsproxy));
  39. atomic_set(&ns->count, 1);
  40. }
  41. return ns;
  42. }
  43. /*
  44. * copies the nsproxy, setting refcount to 1, and grabbing a
  45. * reference to all contained namespaces. Called from
  46. * sys_unshare()
  47. */
  48. struct nsproxy *dup_namespaces(struct nsproxy *orig)
  49. {
  50. struct nsproxy *ns = clone_namespaces(orig);
  51. if (ns) {
  52. if (ns->namespace)
  53. get_namespace(ns->namespace);
  54. }
  55. return ns;
  56. }
  57. /*
  58. * called from clone. This now handles copy for nsproxy and all
  59. * namespaces therein.
  60. */
  61. int copy_namespaces(int flags, struct task_struct *tsk)
  62. {
  63. struct nsproxy *old_ns = tsk->nsproxy;
  64. struct nsproxy *new_ns;
  65. int err = 0;
  66. if (!old_ns)
  67. return 0;
  68. get_nsproxy(old_ns);
  69. if (!(flags & CLONE_NEWNS))
  70. return 0;
  71. new_ns = clone_namespaces(old_ns);
  72. if (!new_ns) {
  73. err = -ENOMEM;
  74. goto out;
  75. }
  76. tsk->nsproxy = new_ns;
  77. err = copy_namespace(flags, tsk);
  78. if (err) {
  79. tsk->nsproxy = old_ns;
  80. put_nsproxy(new_ns);
  81. goto out;
  82. }
  83. out:
  84. put_nsproxy(old_ns);
  85. return err;
  86. }
  87. void free_nsproxy(struct nsproxy *ns)
  88. {
  89. if (ns->namespace)
  90. put_namespace(ns->namespace);
  91. kfree(ns);
  92. }