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/namespace.h>
21 #include <linux/utsname.h>
23 struct nsproxy init_nsproxy = INIT_NSPROXY(init_nsproxy);
25 static inline void get_nsproxy(struct nsproxy *ns)
27 atomic_inc(&ns->count);
30 void get_task_namespaces(struct task_struct *tsk)
32 struct nsproxy *ns = tsk->nsproxy;
39 * creates a copy of "orig" with refcount 1.
40 * This does not grab references to the contained namespaces,
41 * so that needs to be done by dup_namespaces.
43 static inline struct nsproxy *clone_namespaces(struct nsproxy *orig)
47 ns = kmalloc(sizeof(struct nsproxy), GFP_KERNEL);
49 memcpy(ns, orig, sizeof(struct nsproxy));
50 atomic_set(&ns->count, 1);
56 * copies the nsproxy, setting refcount to 1, and grabbing a
57 * reference to all contained namespaces. Called from
60 struct nsproxy *dup_namespaces(struct nsproxy *orig)
62 struct nsproxy *ns = clone_namespaces(orig);
66 get_namespace(ns->namespace);
68 get_uts_ns(ns->uts_ns);
70 get_ipc_ns(ns->ipc_ns);
77 * called from clone. This now handles copy for nsproxy and all
80 int copy_namespaces(int flags, struct task_struct *tsk)
82 struct nsproxy *old_ns = tsk->nsproxy;
83 struct nsproxy *new_ns;
91 if (!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC)))
94 new_ns = clone_namespaces(old_ns);
100 tsk->nsproxy = new_ns;
102 err = copy_namespace(flags, tsk);
106 err = copy_utsname(flags, tsk);
110 err = copy_ipcs(flags, tsk);
120 put_uts_ns(new_ns->uts_ns);
122 if (new_ns->namespace)
123 put_namespace(new_ns->namespace);
125 tsk->nsproxy = old_ns;
130 void free_nsproxy(struct nsproxy *ns)
133 put_namespace(ns->namespace);
135 put_uts_ns(ns->uts_ns);
137 put_ipc_ns(ns->ipc_ns);