blob: e2ce748e96afaca08cf0e5b2821d4cf6e8cab823 [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>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080020#include <linux/mnt_namespace.h>
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070021#include <linux/utsname.h>
Cedric Le Goater9a575a92006-12-08 02:37:59 -080022#include <linux/pid_namespace.h>
Serge E. Hallyn0437eb52006-10-02 02:18:07 -070023
24struct nsproxy init_nsproxy = INIT_NSPROXY(init_nsproxy);
Serge E. Hallynab516012006-10-02 02:18:06 -070025
26static inline void get_nsproxy(struct nsproxy *ns)
27{
28 atomic_inc(&ns->count);
29}
30
31void get_task_namespaces(struct task_struct *tsk)
32{
33 struct nsproxy *ns = tsk->nsproxy;
34 if (ns) {
35 get_nsproxy(ns);
36 }
37}
38
39/*
40 * creates a copy of "orig" with refcount 1.
41 * This does not grab references to the contained namespaces,
42 * so that needs to be done by dup_namespaces.
43 */
44static inline struct nsproxy *clone_namespaces(struct nsproxy *orig)
45{
46 struct nsproxy *ns;
47
Alexey Dobriyane05d7222006-10-19 23:29:12 -070048 ns = kmemdup(orig, sizeof(struct nsproxy), GFP_KERNEL);
Cedric Le Goater373beb32006-12-08 02:37:57 -080049 if (ns) {
Serge E. Hallynab516012006-10-02 02:18:06 -070050 atomic_set(&ns->count, 1);
Cedric Le Goater373beb32006-12-08 02:37:57 -080051 ns->id = -1;
52 }
Serge E. Hallynab516012006-10-02 02:18:06 -070053 return ns;
54}
55
56/*
57 * copies the nsproxy, setting refcount to 1, and grabbing a
58 * reference to all contained namespaces. Called from
59 * sys_unshare()
60 */
61struct nsproxy *dup_namespaces(struct nsproxy *orig)
62{
63 struct nsproxy *ns = clone_namespaces(orig);
64
Serge E. Hallyn1651e142006-10-02 02:18:08 -070065 if (ns) {
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080066 if (ns->mnt_ns)
67 get_mnt_ns(ns->mnt_ns);
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070068 if (ns->uts_ns)
69 get_uts_ns(ns->uts_ns);
Kirill Korotaev25b21cb2006-10-02 02:18:19 -070070 if (ns->ipc_ns)
71 get_ipc_ns(ns->ipc_ns);
Cedric Le Goater9a575a92006-12-08 02:37:59 -080072 if (ns->pid_ns)
73 get_pid_ns(ns->pid_ns);
Serge E. Hallyn1651e142006-10-02 02:18:08 -070074 }
75
Serge E. Hallynab516012006-10-02 02:18:06 -070076 return ns;
77}
78
79/*
80 * called from clone. This now handles copy for nsproxy and all
81 * namespaces therein.
82 */
83int copy_namespaces(int flags, struct task_struct *tsk)
84{
85 struct nsproxy *old_ns = tsk->nsproxy;
Serge E. Hallyn1651e142006-10-02 02:18:08 -070086 struct nsproxy *new_ns;
87 int err = 0;
Serge E. Hallynab516012006-10-02 02:18:06 -070088
89 if (!old_ns)
90 return 0;
91
92 get_nsproxy(old_ns);
93
Kirill Korotaev25b21cb2006-10-02 02:18:19 -070094 if (!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC)))
Serge E. Hallyn1651e142006-10-02 02:18:08 -070095 return 0;
96
97 new_ns = clone_namespaces(old_ns);
98 if (!new_ns) {
99 err = -ENOMEM;
100 goto out;
101 }
102
103 tsk->nsproxy = new_ns;
104
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800105 err = copy_mnt_ns(flags, tsk);
Kirill Korotaev25b21cb2006-10-02 02:18:19 -0700106 if (err)
107 goto out_ns;
Serge E. Hallyn1651e142006-10-02 02:18:08 -0700108
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -0700109 err = copy_utsname(flags, tsk);
Kirill Korotaev25b21cb2006-10-02 02:18:19 -0700110 if (err)
111 goto out_uts;
112
113 err = copy_ipcs(flags, tsk);
114 if (err)
115 goto out_ipc;
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -0700116
Cedric Le Goater9a575a92006-12-08 02:37:59 -0800117 err = copy_pid_ns(flags, tsk);
118 if (err)
119 goto out_pid;
120
Serge E. Hallyn1651e142006-10-02 02:18:08 -0700121out:
122 put_nsproxy(old_ns);
123 return err;
Kirill Korotaev25b21cb2006-10-02 02:18:19 -0700124
Cedric Le Goater9a575a92006-12-08 02:37:59 -0800125out_pid:
126 if (new_ns->ipc_ns)
127 put_ipc_ns(new_ns->ipc_ns);
Kirill Korotaev25b21cb2006-10-02 02:18:19 -0700128out_ipc:
129 if (new_ns->uts_ns)
130 put_uts_ns(new_ns->uts_ns);
131out_uts:
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800132 if (new_ns->mnt_ns)
133 put_mnt_ns(new_ns->mnt_ns);
Kirill Korotaev25b21cb2006-10-02 02:18:19 -0700134out_ns:
135 tsk->nsproxy = old_ns;
Pavel5d124e92006-10-02 02:18:24 -0700136 kfree(new_ns);
Kirill Korotaev25b21cb2006-10-02 02:18:19 -0700137 goto out;
Serge E. Hallynab516012006-10-02 02:18:06 -0700138}
139
140void free_nsproxy(struct nsproxy *ns)
141{
Cedric Le Goater9a575a92006-12-08 02:37:59 -0800142 if (ns->mnt_ns)
143 put_mnt_ns(ns->mnt_ns);
144 if (ns->uts_ns)
145 put_uts_ns(ns->uts_ns);
146 if (ns->ipc_ns)
147 put_ipc_ns(ns->ipc_ns);
148 if (ns->pid_ns)
149 put_pid_ns(ns->pid_ns);
150 kfree(ns);
Serge E. Hallynab516012006-10-02 02:18:06 -0700151}