2 * Copyright (C) 2006 IBM Corporation
4 * Author: Serge Hallyn <serue@us.ibm.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, version 2 of the
11 * Jun 2006 - namespaces support
13 * Pavel Emelianov <xemul@openvz.org>
16 #include <linux/module.h>
17 #include <linux/version.h>
18 #include <linux/nsproxy.h>
19 #include <linux/init_task.h>
20 #include <linux/mnt_namespace.h>
21 #include <linux/utsname.h>
22 #include <linux/pid_namespace.h>
23 #include <linux/vserver/global.h>
25 struct nsproxy init_nsproxy = INIT_NSPROXY(init_nsproxy);
27 void get_task_namespaces(struct task_struct *tsk)
29 struct nsproxy *ns = tsk->nsproxy;
36 * creates a copy of "orig" with refcount 1.
37 * This does not grab references to the contained namespaces,
38 * so that needs to be done by dup_namespaces.
40 static inline struct nsproxy *clone_namespaces(struct nsproxy *orig)
44 ns = kmemdup(orig, sizeof(struct nsproxy), GFP_KERNEL);
46 atomic_set(&ns->count, 1);
47 atomic_inc(&vs_global_nsproxy);
52 * copies the nsproxy, setting refcount to 1, and grabbing a
53 * reference to all contained namespaces. Called from
56 struct nsproxy *dup_namespaces(struct nsproxy *orig)
58 struct nsproxy *ns = clone_namespaces(orig);
62 get_mnt_ns(ns->mnt_ns);
64 get_uts_ns(ns->uts_ns);
66 get_ipc_ns(ns->ipc_ns);
68 get_pid_ns(ns->pid_ns);
75 * called from clone. This now handles copy for nsproxy and all
78 int copy_namespaces(int flags, struct task_struct *tsk)
80 struct nsproxy *old_ns = tsk->nsproxy;
81 struct nsproxy *new_ns;
89 if (!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC)))
92 new_ns = clone_namespaces(old_ns);
98 tsk->nsproxy = new_ns;
100 err = copy_mnt_ns(flags, tsk);
104 err = copy_utsname(flags, tsk);
108 err = copy_ipcs(flags, tsk);
112 err = copy_pid_ns(flags, tsk);
122 put_ipc_ns(new_ns->ipc_ns);
125 put_uts_ns(new_ns->uts_ns);
128 put_mnt_ns(new_ns->mnt_ns);
130 tsk->nsproxy = old_ns;
135 void free_nsproxy(struct nsproxy *ns)
138 put_mnt_ns(ns->mnt_ns);
140 put_uts_ns(ns->uts_ns);
142 put_ipc_ns(ns->ipc_ns);
144 put_pid_ns(ns->pid_ns);
145 atomic_dec(&vs_global_nsproxy);