Pavel Emelyanov | ae5e1b2 | 2008-02-08 04:18:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * linux/ipc/namespace.c |
| 3 | * Copyright (C) 2006 Pavel Emelyanov <xemul@openvz.org> OpenVZ, SWsoft Inc. |
| 4 | */ |
| 5 | |
| 6 | #include <linux/ipc.h> |
| 7 | #include <linux/msg.h> |
| 8 | #include <linux/ipc_namespace.h> |
| 9 | #include <linux/rcupdate.h> |
| 10 | #include <linux/nsproxy.h> |
| 11 | #include <linux/slab.h> |
| 12 | |
| 13 | #include "util.h" |
| 14 | |
| 15 | static struct ipc_namespace *clone_ipc_ns(struct ipc_namespace *old_ns) |
| 16 | { |
Pavel Emelyanov | ae5e1b2 | 2008-02-08 04:18:22 -0800 | [diff] [blame] | 17 | struct ipc_namespace *ns; |
| 18 | |
Pavel Emelyanov | ae5e1b2 | 2008-02-08 04:18:22 -0800 | [diff] [blame] | 19 | ns = kmalloc(sizeof(struct ipc_namespace), GFP_KERNEL); |
| 20 | if (ns == NULL) |
Pierre Peiffer | ed2ddbf | 2008-02-08 04:18:57 -0800 | [diff] [blame] | 21 | return ERR_PTR(-ENOMEM); |
Pavel Emelyanov | ae5e1b2 | 2008-02-08 04:18:22 -0800 | [diff] [blame] | 22 | |
Nadia Derbey | 4d89dc6 | 2008-04-29 01:00:40 -0700 | [diff] [blame] | 23 | atomic_inc(&nr_ipc_ns); |
| 24 | |
Pierre Peiffer | ed2ddbf | 2008-02-08 04:18:57 -0800 | [diff] [blame] | 25 | sem_init_ns(ns); |
| 26 | msg_init_ns(ns); |
| 27 | shm_init_ns(ns); |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame^] | 28 | mq_init_ns(ns); |
Pavel Emelyanov | ae5e1b2 | 2008-02-08 04:18:22 -0800 | [diff] [blame] | 29 | |
Nadia Derbey | e2c284d | 2008-04-29 01:00:44 -0700 | [diff] [blame] | 30 | /* |
| 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. |
| 34 | */ |
| 35 | ipcns_notify(IPCNS_CREATED); |
Nadia Derbey | b6b337a | 2008-04-29 01:00:42 -0700 | [diff] [blame] | 36 | register_ipcns_notifier(ns); |
| 37 | |
Pavel Emelyanov | ae5e1b2 | 2008-02-08 04:18:22 -0800 | [diff] [blame] | 38 | kref_init(&ns->kref); |
| 39 | return ns; |
Pavel Emelyanov | ae5e1b2 | 2008-02-08 04:18:22 -0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | struct ipc_namespace *copy_ipcs(unsigned long flags, struct ipc_namespace *ns) |
| 43 | { |
| 44 | struct ipc_namespace *new_ns; |
| 45 | |
| 46 | BUG_ON(!ns); |
| 47 | get_ipc_ns(ns); |
| 48 | |
| 49 | if (!(flags & CLONE_NEWIPC)) |
| 50 | return ns; |
| 51 | |
| 52 | new_ns = clone_ipc_ns(ns); |
| 53 | |
| 54 | put_ipc_ns(ns); |
| 55 | return new_ns; |
| 56 | } |
| 57 | |
Pierre Peiffer | 01b8b07 | 2008-02-08 04:18:57 -0800 | [diff] [blame] | 58 | /* |
| 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 |
| 63 | * |
| 64 | * Called for each kind of ipc when an ipc_namespace exits. |
| 65 | */ |
| 66 | void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids, |
| 67 | void (*free)(struct ipc_namespace *, struct kern_ipc_perm *)) |
| 68 | { |
| 69 | struct kern_ipc_perm *perm; |
| 70 | int next_id; |
| 71 | int total, in_use; |
| 72 | |
| 73 | down_write(&ids->rw_mutex); |
| 74 | |
| 75 | in_use = ids->in_use; |
| 76 | |
| 77 | for (total = 0, next_id = 0; total < in_use; next_id++) { |
| 78 | perm = idr_find(&ids->ipcs_idr, next_id); |
| 79 | if (perm == NULL) |
| 80 | continue; |
| 81 | ipc_lock_by_ptr(perm); |
| 82 | free(ns, perm); |
| 83 | total++; |
| 84 | } |
| 85 | up_write(&ids->rw_mutex); |
| 86 | } |
| 87 | |
Pavel Emelyanov | ae5e1b2 | 2008-02-08 04:18:22 -0800 | [diff] [blame] | 88 | void free_ipc_ns(struct kref *kref) |
| 89 | { |
| 90 | struct ipc_namespace *ns; |
| 91 | |
| 92 | ns = container_of(kref, struct ipc_namespace, kref); |
Nadia Derbey | b6b337a | 2008-04-29 01:00:42 -0700 | [diff] [blame] | 93 | /* |
| 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. |
| 100 | */ |
| 101 | unregister_ipcns_notifier(ns); |
Pavel Emelyanov | ae5e1b2 | 2008-02-08 04:18:22 -0800 | [diff] [blame] | 102 | sem_exit_ns(ns); |
| 103 | msg_exit_ns(ns); |
| 104 | shm_exit_ns(ns); |
Serge E. Hallyn | 614b84c | 2009-04-06 19:01:08 -0700 | [diff] [blame^] | 105 | mq_exit_ns(ns); |
Pavel Emelyanov | ae5e1b2 | 2008-02-08 04:18:22 -0800 | [diff] [blame] | 106 | kfree(ns); |
Nadia Derbey | 4d89dc6 | 2008-04-29 01:00:40 -0700 | [diff] [blame] | 107 | atomic_dec(&nr_ipc_ns); |
Nadia Derbey | e2c284d | 2008-04-29 01:00:44 -0700 | [diff] [blame] | 108 | |
| 109 | /* |
| 110 | * Do the ipcns removal notification after decrementing nr_ipc_ns in |
| 111 | * order to have a correct value when recomputing msgmni. |
| 112 | */ |
| 113 | ipcns_notify(IPCNS_REMOVED); |
Pavel Emelyanov | ae5e1b2 | 2008-02-08 04:18:22 -0800 | [diff] [blame] | 114 | } |