nsproxy.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. struct nsproxy init_nsproxy = INIT_NSPROXY(init_nsproxy);
  16. static inline void get_nsproxy(struct nsproxy *ns)
  17. {
  18. atomic_inc(&ns->count);
  19. }
  20. void get_task_namespaces(struct task_struct *tsk)
  21. {
  22. struct nsproxy *ns = tsk->nsproxy;
  23. if (ns) {
  24. get_nsproxy(ns);
  25. }
  26. }
  27. /*
  28. * creates a copy of "orig" with refcount 1.
  29. * This does not grab references to the contained namespaces,
  30. * so that needs to be done by dup_namespaces.
  31. */
  32. static inline struct nsproxy *clone_namespaces(struct nsproxy *orig)
  33. {
  34. struct nsproxy *ns;
  35. ns = kmalloc(sizeof(struct nsproxy), GFP_KERNEL);
  36. if (ns) {
  37. memcpy(ns, orig, sizeof(struct nsproxy));
  38. atomic_set(&ns->count, 1);
  39. }
  40. return ns;
  41. }
  42. /*
  43. * copies the nsproxy, setting refcount to 1, and grabbing a
  44. * reference to all contained namespaces. Called from
  45. * sys_unshare()
  46. */
  47. struct nsproxy *dup_namespaces(struct nsproxy *orig)
  48. {
  49. struct nsproxy *ns = clone_namespaces(orig);
  50. return ns;
  51. }
  52. /*
  53. * called from clone. This now handles copy for nsproxy and all
  54. * namespaces therein.
  55. */
  56. int copy_namespaces(int flags, struct task_struct *tsk)
  57. {
  58. struct nsproxy *old_ns = tsk->nsproxy;
  59. if (!old_ns)
  60. return 0;
  61. get_nsproxy(old_ns);
  62. return 0;
  63. }
  64. void free_nsproxy(struct nsproxy *ns)
  65. {
  66. kfree(ns);
  67. }