2 * linux/ipc/namespace.c
3 * Copyright (C) 2006 Pavel Emelyanov <xemul@openvz.org> OpenVZ, SWsoft Inc.
8 #include <linux/ipc_namespace.h>
9 #include <linux/rcupdate.h>
10 #include <linux/nsproxy.h>
11 #include <linux/slab.h>
15 static struct ipc_namespace
*clone_ipc_ns(struct ipc_namespace
*old_ns
)
17 struct ipc_namespace
*ns
;
19 ns
= kmalloc(sizeof(struct ipc_namespace
), GFP_KERNEL
);
21 return ERR_PTR(-ENOMEM
);
23 atomic_inc(&nr_ipc_ns
);
31 * msgmni has already been computed for the new ipc ns.
32 * Thus, do the ipcns creation notification before registering that
33 * new ipcns in the chain.
35 ipcns_notify(IPCNS_CREATED
);
36 register_ipcns_notifier(ns
);
42 struct ipc_namespace
*copy_ipcs(unsigned long flags
, struct ipc_namespace
*ns
)
44 struct ipc_namespace
*new_ns
;
49 if (!(flags
& CLONE_NEWIPC
))
52 new_ns
= clone_ipc_ns(ns
);
59 * free_ipcs - free all ipcs of one type
60 * @ns: the namespace to remove the ipcs from
61 * @ids: the table of ipcs to free
62 * @free: the function called to free each individual ipc
64 * Called for each kind of ipc when an ipc_namespace exits.
66 void free_ipcs(struct ipc_namespace
*ns
, struct ipc_ids
*ids
,
67 void (*free
)(struct ipc_namespace
*, struct kern_ipc_perm
*))
69 struct kern_ipc_perm
*perm
;
73 down_write(&ids
->rw_mutex
);
77 for (total
= 0, next_id
= 0; total
< in_use
; next_id
++) {
78 perm
= idr_find(&ids
->ipcs_idr
, next_id
);
81 ipc_lock_by_ptr(perm
);
85 up_write(&ids
->rw_mutex
);
88 void free_ipc_ns(struct kref
*kref
)
90 struct ipc_namespace
*ns
;
92 ns
= container_of(kref
, struct ipc_namespace
, kref
);
94 * Unregistering the hotplug notifier at the beginning guarantees
95 * that the ipc namespace won't be freed while we are inside the
96 * callback routine. Since the blocking_notifier_chain_XXX routines
97 * hold a rw lock on the notifier list, unregister_ipcns_notifier()
98 * won't take the rw lock before blocking_notifier_call_chain() has
99 * released the rd lock.
101 unregister_ipcns_notifier(ns
);
107 atomic_dec(&nr_ipc_ns
);
110 * Do the ipcns removal notification after decrementing nr_ipc_ns in
111 * order to have a correct value when recomputing msgmni.
113 ipcns_notify(IPCNS_REMOVED
);