Commit | Line | Data |
---|---|---|
ab516013 SH |
1 | /* |
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. | |
10 | */ | |
11 | ||
12 | #include <linux/module.h> | |
13 | #include <linux/version.h> | |
14 | #include <linux/nsproxy.h> | |
0437eb59 | 15 | #include <linux/init_task.h> |
1651e14e | 16 | #include <linux/namespace.h> |
0437eb59 SH |
17 | |
18 | struct nsproxy init_nsproxy = INIT_NSPROXY(init_nsproxy); | |
ab516013 SH |
19 | |
20 | static inline void get_nsproxy(struct nsproxy *ns) | |
21 | { | |
22 | atomic_inc(&ns->count); | |
23 | } | |
24 | ||
25 | void get_task_namespaces(struct task_struct *tsk) | |
26 | { | |
27 | struct nsproxy *ns = tsk->nsproxy; | |
28 | if (ns) { | |
29 | get_nsproxy(ns); | |
30 | } | |
31 | } | |
32 | ||
33 | /* | |
34 | * creates a copy of "orig" with refcount 1. | |
35 | * This does not grab references to the contained namespaces, | |
36 | * so that needs to be done by dup_namespaces. | |
37 | */ | |
38 | static inline struct nsproxy *clone_namespaces(struct nsproxy *orig) | |
39 | { | |
40 | struct nsproxy *ns; | |
41 | ||
42 | ns = kmalloc(sizeof(struct nsproxy), GFP_KERNEL); | |
43 | if (ns) { | |
44 | memcpy(ns, orig, sizeof(struct nsproxy)); | |
45 | atomic_set(&ns->count, 1); | |
46 | } | |
47 | return ns; | |
48 | } | |
49 | ||
50 | /* | |
51 | * copies the nsproxy, setting refcount to 1, and grabbing a | |
52 | * reference to all contained namespaces. Called from | |
53 | * sys_unshare() | |
54 | */ | |
55 | struct nsproxy *dup_namespaces(struct nsproxy *orig) | |
56 | { | |
57 | struct nsproxy *ns = clone_namespaces(orig); | |
58 | ||
1651e14e SH |
59 | if (ns) { |
60 | if (ns->namespace) | |
61 | get_namespace(ns->namespace); | |
62 | } | |
63 | ||
ab516013 SH |
64 | return ns; |
65 | } | |
66 | ||
67 | /* | |
68 | * called from clone. This now handles copy for nsproxy and all | |
69 | * namespaces therein. | |
70 | */ | |
71 | int copy_namespaces(int flags, struct task_struct *tsk) | |
72 | { | |
73 | struct nsproxy *old_ns = tsk->nsproxy; | |
1651e14e SH |
74 | struct nsproxy *new_ns; |
75 | int err = 0; | |
ab516013 SH |
76 | |
77 | if (!old_ns) | |
78 | return 0; | |
79 | ||
80 | get_nsproxy(old_ns); | |
81 | ||
1651e14e SH |
82 | if (!(flags & CLONE_NEWNS)) |
83 | return 0; | |
84 | ||
85 | new_ns = clone_namespaces(old_ns); | |
86 | if (!new_ns) { | |
87 | err = -ENOMEM; | |
88 | goto out; | |
89 | } | |
90 | ||
91 | tsk->nsproxy = new_ns; | |
92 | ||
93 | err = copy_namespace(flags, tsk); | |
94 | if (err) { | |
95 | tsk->nsproxy = old_ns; | |
96 | put_nsproxy(new_ns); | |
97 | goto out; | |
98 | } | |
99 | ||
100 | out: | |
101 | put_nsproxy(old_ns); | |
102 | return err; | |
ab516013 SH |
103 | } |
104 | ||
105 | void free_nsproxy(struct nsproxy *ns) | |
106 | { | |
1651e14e SH |
107 | if (ns->namespace) |
108 | put_namespace(ns->namespace); | |
ab516013 SH |
109 | kfree(ns); |
110 | } |