namespace.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. atomic_inc(&nr_ipc_ns);
  19. sem_init_ns(ns);
  20. msg_init_ns(ns);
  21. shm_init_ns(ns);
  22. /*
  23. * msgmni has already been computed for the new ipc ns.
  24. * Thus, do the ipcns creation notification before registering that
  25. * new ipcns in the chain.
  26. */
  27. ipcns_notify(IPCNS_CREATED);
  28. register_ipcns_notifier(ns);
  29. kref_init(&ns->kref);
  30. return ns;
  31. }
  32. struct ipc_namespace *copy_ipcs(unsigned long flags, struct ipc_namespace *ns)
  33. {
  34. struct ipc_namespace *new_ns;
  35. BUG_ON(!ns);
  36. get_ipc_ns(ns);
  37. if (!(flags & CLONE_NEWIPC))
  38. return ns;
  39. new_ns = clone_ipc_ns(ns);
  40. put_ipc_ns(ns);
  41. return new_ns;
  42. }
  43. /*
  44. * free_ipcs - free all ipcs of one type
  45. * @ns: the namespace to remove the ipcs from
  46. * @ids: the table of ipcs to free
  47. * @free: the function called to free each individual ipc
  48. *
  49. * Called for each kind of ipc when an ipc_namespace exits.
  50. */
  51. void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids,
  52. void (*free)(struct ipc_namespace *, struct kern_ipc_perm *))
  53. {
  54. struct kern_ipc_perm *perm;
  55. int next_id;
  56. int total, in_use;
  57. down_write(&ids->rw_mutex);
  58. in_use = ids->in_use;
  59. for (total = 0, next_id = 0; total < in_use; next_id++) {
  60. perm = idr_find(&ids->ipcs_idr, next_id);
  61. if (perm == NULL)
  62. continue;
  63. ipc_lock_by_ptr(perm);
  64. free(ns, perm);
  65. total++;
  66. }
  67. up_write(&ids->rw_mutex);
  68. }
  69. void free_ipc_ns(struct kref *kref)
  70. {
  71. struct ipc_namespace *ns;
  72. ns = container_of(kref, struct ipc_namespace, kref);
  73. /*
  74. * Unregistering the hotplug notifier at the beginning guarantees
  75. * that the ipc namespace won't be freed while we are inside the
  76. * callback routine. Since the blocking_notifier_chain_XXX routines
  77. * hold a rw lock on the notifier list, unregister_ipcns_notifier()
  78. * won't take the rw lock before blocking_notifier_call_chain() has
  79. * released the rd lock.
  80. */
  81. unregister_ipcns_notifier(ns);
  82. sem_exit_ns(ns);
  83. msg_exit_ns(ns);
  84. shm_exit_ns(ns);
  85. kfree(ns);
  86. atomic_dec(&nr_ipc_ns);
  87. /*
  88. * Do the ipcns removal notification after decrementing nr_ipc_ns in
  89. * order to have a correct value when recomputing msgmni.
  90. */
  91. ipcns_notify(IPCNS_REMOVED);
  92. }