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 | |
Pierre Peiffer | ed2ddbf | 2008-02-08 04:18:57 -0800 | [diff] [blame] | 23 | sem_init_ns(ns); |
| 24 | msg_init_ns(ns); |
| 25 | shm_init_ns(ns); |
Pavel Emelyanov | ae5e1b2 | 2008-02-08 04:18:22 -0800 | [diff] [blame] | 26 | |
| 27 | kref_init(&ns->kref); |
| 28 | return ns; |
Pavel Emelyanov | ae5e1b2 | 2008-02-08 04:18:22 -0800 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | struct ipc_namespace *copy_ipcs(unsigned long flags, struct ipc_namespace *ns) |
| 32 | { |
| 33 | struct ipc_namespace *new_ns; |
| 34 | |
| 35 | BUG_ON(!ns); |
| 36 | get_ipc_ns(ns); |
| 37 | |
| 38 | if (!(flags & CLONE_NEWIPC)) |
| 39 | return ns; |
| 40 | |
| 41 | new_ns = clone_ipc_ns(ns); |
| 42 | |
| 43 | put_ipc_ns(ns); |
| 44 | return new_ns; |
| 45 | } |
| 46 | |
Pierre Peiffer | 01b8b07 | 2008-02-08 04:18:57 -0800 | [diff] [blame] | 47 | /* |
| 48 | * free_ipcs - free all ipcs of one type |
| 49 | * @ns: the namespace to remove the ipcs from |
| 50 | * @ids: the table of ipcs to free |
| 51 | * @free: the function called to free each individual ipc |
| 52 | * |
| 53 | * Called for each kind of ipc when an ipc_namespace exits. |
| 54 | */ |
| 55 | void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids, |
| 56 | void (*free)(struct ipc_namespace *, struct kern_ipc_perm *)) |
| 57 | { |
| 58 | struct kern_ipc_perm *perm; |
| 59 | int next_id; |
| 60 | int total, in_use; |
| 61 | |
| 62 | down_write(&ids->rw_mutex); |
| 63 | |
| 64 | in_use = ids->in_use; |
| 65 | |
| 66 | for (total = 0, next_id = 0; total < in_use; next_id++) { |
| 67 | perm = idr_find(&ids->ipcs_idr, next_id); |
| 68 | if (perm == NULL) |
| 69 | continue; |
| 70 | ipc_lock_by_ptr(perm); |
| 71 | free(ns, perm); |
| 72 | total++; |
| 73 | } |
| 74 | up_write(&ids->rw_mutex); |
| 75 | } |
| 76 | |
Pavel Emelyanov | ae5e1b2 | 2008-02-08 04:18:22 -0800 | [diff] [blame] | 77 | void free_ipc_ns(struct kref *kref) |
| 78 | { |
| 79 | struct ipc_namespace *ns; |
| 80 | |
| 81 | ns = container_of(kref, struct ipc_namespace, kref); |
| 82 | sem_exit_ns(ns); |
| 83 | msg_exit_ns(ns); |
| 84 | shm_exit_ns(ns); |
| 85 | kfree(ns); |
| 86 | } |