blob: 6ebdb82a0ce43a5211e3862008959cb5091febcd [file] [log] [blame]
Serge E. Hallynab516012006-10-02 02:18:06 -07001/*
2 * Copyright (C) 2006 IBM Corporation
3 *
4 * Author: Serge Hallyn <serue@us.ibm.com>
5 *
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
9 * License.
Kirill Korotaev25b21cb2006-10-02 02:18:19 -070010 *
11 * Jun 2006 - namespaces support
12 * OpenVZ, SWsoft Inc.
13 * Pavel Emelianov <xemul@openvz.org>
Serge E. Hallynab516012006-10-02 02:18:06 -070014 */
15
16#include <linux/module.h>
17#include <linux/version.h>
18#include <linux/nsproxy.h>
Serge E. Hallyn0437eb52006-10-02 02:18:07 -070019#include <linux/init_task.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070020#include <linux/namespace.h>
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070021#include <linux/utsname.h>
Serge E. Hallyn0437eb52006-10-02 02:18:07 -070022
23struct nsproxy init_nsproxy = INIT_NSPROXY(init_nsproxy);
Serge E. Hallynab516012006-10-02 02:18:06 -070024
25static inline void get_nsproxy(struct nsproxy *ns)
26{
27 atomic_inc(&ns->count);
28}
29
30void get_task_namespaces(struct task_struct *tsk)
31{
32 struct nsproxy *ns = tsk->nsproxy;
33 if (ns) {
34 get_nsproxy(ns);
35 }
36}
37
38/*
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.
42 */
43static inline struct nsproxy *clone_namespaces(struct nsproxy *orig)
44{
45 struct nsproxy *ns;
46
47 ns = kmalloc(sizeof(struct nsproxy), GFP_KERNEL);
48 if (ns) {
49 memcpy(ns, orig, sizeof(struct nsproxy));
50 atomic_set(&ns->count, 1);
51 }
52 return ns;
53}
54
55/*
56 * copies the nsproxy, setting refcount to 1, and grabbing a
57 * reference to all contained namespaces. Called from
58 * sys_unshare()
59 */
60struct nsproxy *dup_namespaces(struct nsproxy *orig)
61{
62 struct nsproxy *ns = clone_namespaces(orig);
63
Serge E. Hallyn1651e142006-10-02 02:18:08 -070064 if (ns) {
65 if (ns->namespace)
66 get_namespace(ns->namespace);
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070067 if (ns->uts_ns)
68 get_uts_ns(ns->uts_ns);
Kirill Korotaev25b21cb2006-10-02 02:18:19 -070069 if (ns->ipc_ns)
70 get_ipc_ns(ns->ipc_ns);
Serge E. Hallyn1651e142006-10-02 02:18:08 -070071 }
72
Serge E. Hallynab516012006-10-02 02:18:06 -070073 return ns;
74}
75
76/*
77 * called from clone. This now handles copy for nsproxy and all
78 * namespaces therein.
79 */
80int copy_namespaces(int flags, struct task_struct *tsk)
81{
82 struct nsproxy *old_ns = tsk->nsproxy;
Serge E. Hallyn1651e142006-10-02 02:18:08 -070083 struct nsproxy *new_ns;
84 int err = 0;
Serge E. Hallynab516012006-10-02 02:18:06 -070085
86 if (!old_ns)
87 return 0;
88
89 get_nsproxy(old_ns);
90
Kirill Korotaev25b21cb2006-10-02 02:18:19 -070091 if (!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC)))
Serge E. Hallyn1651e142006-10-02 02:18:08 -070092 return 0;
93
94 new_ns = clone_namespaces(old_ns);
95 if (!new_ns) {
96 err = -ENOMEM;
97 goto out;
98 }
99
100 tsk->nsproxy = new_ns;
101
102 err = copy_namespace(flags, tsk);
Kirill Korotaev25b21cb2006-10-02 02:18:19 -0700103 if (err)
104 goto out_ns;
Serge E. Hallyn1651e142006-10-02 02:18:08 -0700105
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -0700106 err = copy_utsname(flags, tsk);
Kirill Korotaev25b21cb2006-10-02 02:18:19 -0700107 if (err)
108 goto out_uts;
109
110 err = copy_ipcs(flags, tsk);
111 if (err)
112 goto out_ipc;
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -0700113
Serge E. Hallyn1651e142006-10-02 02:18:08 -0700114out:
115 put_nsproxy(old_ns);
116 return err;
Kirill Korotaev25b21cb2006-10-02 02:18:19 -0700117
118out_ipc:
119 if (new_ns->uts_ns)
120 put_uts_ns(new_ns->uts_ns);
121out_uts:
122 if (new_ns->namespace)
123 put_namespace(new_ns->namespace);
124out_ns:
125 tsk->nsproxy = old_ns;
Pavel5d124e92006-10-02 02:18:24 -0700126 kfree(new_ns);
Kirill Korotaev25b21cb2006-10-02 02:18:19 -0700127 goto out;
Serge E. Hallynab516012006-10-02 02:18:06 -0700128}
129
130void free_nsproxy(struct nsproxy *ns)
131{
Serge E. Hallyn1651e142006-10-02 02:18:08 -0700132 if (ns->namespace)
133 put_namespace(ns->namespace);
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -0700134 if (ns->uts_ns)
135 put_uts_ns(ns->uts_ns);
Kirill Korotaev25b21cb2006-10-02 02:18:19 -0700136 if (ns->ipc_ns)
137 put_ipc_ns(ns->ipc_ns);
Serge E. Hallynab516012006-10-02 02:18:06 -0700138 kfree(ns);
139}