namespace.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * linux/ipc/namespace.c
  3. * Copyright (C) 2006 Pavel Emelyanov <xemul@openvz.org> OpenVZ, SWsoft Inc.
  4. */
  5. #include <linux/ipc.h>
  6. #include <linux/msg.h>
  7. #include <linux/ipc_namespace.h>
  8. #include <linux/rcupdate.h>
  9. #include <linux/nsproxy.h>
  10. #include <linux/slab.h>
  11. #include "util.h"
  12. static struct ipc_namespace *clone_ipc_ns(struct ipc_namespace *old_ns)
  13. {
  14. struct ipc_namespace *ns;
  15. ns = kmalloc(sizeof(struct ipc_namespace), GFP_KERNEL);
  16. if (ns == NULL)
  17. return ERR_PTR(-ENOMEM);
  18. sem_init_ns(ns);
  19. msg_init_ns(ns);
  20. shm_init_ns(ns);
  21. kref_init(&ns->kref);
  22. return ns;
  23. }
  24. struct ipc_namespace *copy_ipcs(unsigned long flags, struct ipc_namespace *ns)
  25. {
  26. struct ipc_namespace *new_ns;
  27. BUG_ON(!ns);
  28. get_ipc_ns(ns);
  29. if (!(flags & CLONE_NEWIPC))
  30. return ns;
  31. new_ns = clone_ipc_ns(ns);
  32. put_ipc_ns(ns);
  33. return new_ns;
  34. }
  35. /*
  36. * free_ipcs - free all ipcs of one type
  37. * @ns: the namespace to remove the ipcs from
  38. * @ids: the table of ipcs to free
  39. * @free: the function called to free each individual ipc
  40. *
  41. * Called for each kind of ipc when an ipc_namespace exits.
  42. */
  43. void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids,
  44. void (*free)(struct ipc_namespace *, struct kern_ipc_perm *))
  45. {
  46. struct kern_ipc_perm *perm;
  47. int next_id;
  48. int total, in_use;
  49. down_write(&ids->rw_mutex);
  50. in_use = ids->in_use;
  51. for (total = 0, next_id = 0; total < in_use; next_id++) {
  52. perm = idr_find(&ids->ipcs_idr, next_id);
  53. if (perm == NULL)
  54. continue;
  55. ipc_lock_by_ptr(perm);
  56. free(ns, perm);
  57. total++;
  58. }
  59. up_write(&ids->rw_mutex);
  60. }
  61. void free_ipc_ns(struct kref *kref)
  62. {
  63. struct ipc_namespace *ns;
  64. ns = container_of(kref, struct ipc_namespace, kref);
  65. sem_exit_ns(ns);
  66. msg_exit_ns(ns);
  67. shm_exit_ns(ns);
  68. kfree(ns);
  69. }