nsproxy.c 1.5 KB

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