blob: fe3c97aa99dc191f69c9febf058fc2b876204523 [file] [log] [blame]
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -08001/*
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
15static struct ipc_namespace *clone_ipc_ns(struct ipc_namespace *old_ns)
16{
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080017 struct ipc_namespace *ns;
18
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080019 ns = kmalloc(sizeof(struct ipc_namespace), GFP_KERNEL);
20 if (ns == NULL)
Pierre Peiffered2ddbf2008-02-08 04:18:57 -080021 return ERR_PTR(-ENOMEM);
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080022
Nadia Derbey4d89dc62008-04-29 01:00:40 -070023 atomic_inc(&nr_ipc_ns);
24
Pierre Peiffered2ddbf2008-02-08 04:18:57 -080025 sem_init_ns(ns);
26 msg_init_ns(ns);
27 shm_init_ns(ns);
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080028
29 kref_init(&ns->kref);
30 return ns;
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080031}
32
33struct ipc_namespace *copy_ipcs(unsigned long flags, struct ipc_namespace *ns)
34{
35 struct ipc_namespace *new_ns;
36
37 BUG_ON(!ns);
38 get_ipc_ns(ns);
39
40 if (!(flags & CLONE_NEWIPC))
41 return ns;
42
43 new_ns = clone_ipc_ns(ns);
44
45 put_ipc_ns(ns);
46 return new_ns;
47}
48
Pierre Peiffer01b8b072008-02-08 04:18:57 -080049/*
50 * free_ipcs - free all ipcs of one type
51 * @ns: the namespace to remove the ipcs from
52 * @ids: the table of ipcs to free
53 * @free: the function called to free each individual ipc
54 *
55 * Called for each kind of ipc when an ipc_namespace exits.
56 */
57void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids,
58 void (*free)(struct ipc_namespace *, struct kern_ipc_perm *))
59{
60 struct kern_ipc_perm *perm;
61 int next_id;
62 int total, in_use;
63
64 down_write(&ids->rw_mutex);
65
66 in_use = ids->in_use;
67
68 for (total = 0, next_id = 0; total < in_use; next_id++) {
69 perm = idr_find(&ids->ipcs_idr, next_id);
70 if (perm == NULL)
71 continue;
72 ipc_lock_by_ptr(perm);
73 free(ns, perm);
74 total++;
75 }
76 up_write(&ids->rw_mutex);
77}
78
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080079void free_ipc_ns(struct kref *kref)
80{
81 struct ipc_namespace *ns;
82
83 ns = container_of(kref, struct ipc_namespace, kref);
84 sem_exit_ns(ns);
85 msg_exit_ns(ns);
86 shm_exit_ns(ns);
87 kfree(ns);
Nadia Derbey4d89dc62008-04-29 01:00:40 -070088 atomic_dec(&nr_ipc_ns);
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080089}