namespace.c 2.2 KB

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