namespace.c 2.6 KB

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