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 | { |
| 17 | int err; |
| 18 | struct ipc_namespace *ns; |
| 19 | |
| 20 | err = -ENOMEM; |
| 21 | ns = kmalloc(sizeof(struct ipc_namespace), GFP_KERNEL); |
| 22 | if (ns == NULL) |
| 23 | goto err_mem; |
| 24 | |
| 25 | err = sem_init_ns(ns); |
| 26 | if (err) |
| 27 | goto err_sem; |
| 28 | err = msg_init_ns(ns); |
| 29 | if (err) |
| 30 | goto err_msg; |
| 31 | err = shm_init_ns(ns); |
| 32 | if (err) |
| 33 | goto err_shm; |
| 34 | |
| 35 | kref_init(&ns->kref); |
| 36 | return ns; |
| 37 | |
| 38 | err_shm: |
| 39 | msg_exit_ns(ns); |
| 40 | err_msg: |
| 41 | sem_exit_ns(ns); |
| 42 | err_sem: |
| 43 | kfree(ns); |
| 44 | err_mem: |
| 45 | return ERR_PTR(err); |
| 46 | } |
| 47 | |
| 48 | struct ipc_namespace *copy_ipcs(unsigned long flags, struct ipc_namespace *ns) |
| 49 | { |
| 50 | struct ipc_namespace *new_ns; |
| 51 | |
| 52 | BUG_ON(!ns); |
| 53 | get_ipc_ns(ns); |
| 54 | |
| 55 | if (!(flags & CLONE_NEWIPC)) |
| 56 | return ns; |
| 57 | |
| 58 | new_ns = clone_ipc_ns(ns); |
| 59 | |
| 60 | put_ipc_ns(ns); |
| 61 | return new_ns; |
| 62 | } |
| 63 | |
| 64 | void free_ipc_ns(struct kref *kref) |
| 65 | { |
| 66 | struct ipc_namespace *ns; |
| 67 | |
| 68 | ns = container_of(kref, struct ipc_namespace, kref); |
| 69 | sem_exit_ns(ns); |
| 70 | msg_exit_ns(ns); |
| 71 | shm_exit_ns(ns); |
| 72 | kfree(ns); |
| 73 | } |