Serge E. Hallyn | ab51601 | 2006-10-02 02:18:06 -0700 | [diff] [blame] | 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. |
Kirill Korotaev | 25b21cb | 2006-10-02 02:18:19 -0700 | [diff] [blame] | 10 | * |
| 11 | * Jun 2006 - namespaces support |
| 12 | * OpenVZ, SWsoft Inc. |
| 13 | * Pavel Emelianov <xemul@openvz.org> |
Serge E. Hallyn | ab51601 | 2006-10-02 02:18:06 -0700 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/version.h> |
| 18 | #include <linux/nsproxy.h> |
Serge E. Hallyn | 0437eb5 | 2006-10-02 02:18:07 -0700 | [diff] [blame] | 19 | #include <linux/init_task.h> |
Serge E. Hallyn | 1651e14 | 2006-10-02 02:18:08 -0700 | [diff] [blame] | 20 | #include <linux/namespace.h> |
Serge E. Hallyn | 4865ecf | 2006-10-02 02:18:14 -0700 | [diff] [blame] | 21 | #include <linux/utsname.h> |
Serge E. Hallyn | 0437eb5 | 2006-10-02 02:18:07 -0700 | [diff] [blame] | 22 | |
| 23 | struct nsproxy init_nsproxy = INIT_NSPROXY(init_nsproxy); |
Serge E. Hallyn | ab51601 | 2006-10-02 02:18:06 -0700 | [diff] [blame] | 24 | |
| 25 | static inline void get_nsproxy(struct nsproxy *ns) |
| 26 | { |
| 27 | atomic_inc(&ns->count); |
| 28 | } |
| 29 | |
| 30 | void 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 | */ |
| 43 | static inline struct nsproxy *clone_namespaces(struct nsproxy *orig) |
| 44 | { |
| 45 | struct nsproxy *ns; |
| 46 | |
Alexey Dobriyan | e05d722 | 2006-10-19 23:29:12 -0700 | [diff] [blame] | 47 | ns = kmemdup(orig, sizeof(struct nsproxy), GFP_KERNEL); |
| 48 | if (ns) |
Serge E. Hallyn | ab51601 | 2006-10-02 02:18:06 -0700 | [diff] [blame] | 49 | atomic_set(&ns->count, 1); |
Serge E. Hallyn | ab51601 | 2006-10-02 02:18:06 -0700 | [diff] [blame] | 50 | return ns; |
| 51 | } |
| 52 | |
| 53 | /* |
| 54 | * copies the nsproxy, setting refcount to 1, and grabbing a |
| 55 | * reference to all contained namespaces. Called from |
| 56 | * sys_unshare() |
| 57 | */ |
| 58 | struct nsproxy *dup_namespaces(struct nsproxy *orig) |
| 59 | { |
| 60 | struct nsproxy *ns = clone_namespaces(orig); |
| 61 | |
Serge E. Hallyn | 1651e14 | 2006-10-02 02:18:08 -0700 | [diff] [blame] | 62 | if (ns) { |
| 63 | if (ns->namespace) |
| 64 | get_namespace(ns->namespace); |
Serge E. Hallyn | 4865ecf | 2006-10-02 02:18:14 -0700 | [diff] [blame] | 65 | if (ns->uts_ns) |
| 66 | get_uts_ns(ns->uts_ns); |
Kirill Korotaev | 25b21cb | 2006-10-02 02:18:19 -0700 | [diff] [blame] | 67 | if (ns->ipc_ns) |
| 68 | get_ipc_ns(ns->ipc_ns); |
Serge E. Hallyn | 1651e14 | 2006-10-02 02:18:08 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Serge E. Hallyn | ab51601 | 2006-10-02 02:18:06 -0700 | [diff] [blame] | 71 | return ns; |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | * called from clone. This now handles copy for nsproxy and all |
| 76 | * namespaces therein. |
| 77 | */ |
| 78 | int copy_namespaces(int flags, struct task_struct *tsk) |
| 79 | { |
| 80 | struct nsproxy *old_ns = tsk->nsproxy; |
Serge E. Hallyn | 1651e14 | 2006-10-02 02:18:08 -0700 | [diff] [blame] | 81 | struct nsproxy *new_ns; |
| 82 | int err = 0; |
Serge E. Hallyn | ab51601 | 2006-10-02 02:18:06 -0700 | [diff] [blame] | 83 | |
| 84 | if (!old_ns) |
| 85 | return 0; |
| 86 | |
| 87 | get_nsproxy(old_ns); |
| 88 | |
Kirill Korotaev | 25b21cb | 2006-10-02 02:18:19 -0700 | [diff] [blame] | 89 | if (!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC))) |
Serge E. Hallyn | 1651e14 | 2006-10-02 02:18:08 -0700 | [diff] [blame] | 90 | return 0; |
| 91 | |
| 92 | new_ns = clone_namespaces(old_ns); |
| 93 | if (!new_ns) { |
| 94 | err = -ENOMEM; |
| 95 | goto out; |
| 96 | } |
| 97 | |
| 98 | tsk->nsproxy = new_ns; |
| 99 | |
| 100 | err = copy_namespace(flags, tsk); |
Kirill Korotaev | 25b21cb | 2006-10-02 02:18:19 -0700 | [diff] [blame] | 101 | if (err) |
| 102 | goto out_ns; |
Serge E. Hallyn | 1651e14 | 2006-10-02 02:18:08 -0700 | [diff] [blame] | 103 | |
Serge E. Hallyn | 4865ecf | 2006-10-02 02:18:14 -0700 | [diff] [blame] | 104 | err = copy_utsname(flags, tsk); |
Kirill Korotaev | 25b21cb | 2006-10-02 02:18:19 -0700 | [diff] [blame] | 105 | if (err) |
| 106 | goto out_uts; |
| 107 | |
| 108 | err = copy_ipcs(flags, tsk); |
| 109 | if (err) |
| 110 | goto out_ipc; |
Serge E. Hallyn | 4865ecf | 2006-10-02 02:18:14 -0700 | [diff] [blame] | 111 | |
Serge E. Hallyn | 1651e14 | 2006-10-02 02:18:08 -0700 | [diff] [blame] | 112 | out: |
| 113 | put_nsproxy(old_ns); |
| 114 | return err; |
Kirill Korotaev | 25b21cb | 2006-10-02 02:18:19 -0700 | [diff] [blame] | 115 | |
| 116 | out_ipc: |
| 117 | if (new_ns->uts_ns) |
| 118 | put_uts_ns(new_ns->uts_ns); |
| 119 | out_uts: |
| 120 | if (new_ns->namespace) |
| 121 | put_namespace(new_ns->namespace); |
| 122 | out_ns: |
| 123 | tsk->nsproxy = old_ns; |
Pavel | 5d124e9 | 2006-10-02 02:18:24 -0700 | [diff] [blame] | 124 | kfree(new_ns); |
Kirill Korotaev | 25b21cb | 2006-10-02 02:18:19 -0700 | [diff] [blame] | 125 | goto out; |
Serge E. Hallyn | ab51601 | 2006-10-02 02:18:06 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void free_nsproxy(struct nsproxy *ns) |
| 129 | { |
Serge E. Hallyn | 1651e14 | 2006-10-02 02:18:08 -0700 | [diff] [blame] | 130 | if (ns->namespace) |
| 131 | put_namespace(ns->namespace); |
Serge E. Hallyn | 4865ecf | 2006-10-02 02:18:14 -0700 | [diff] [blame] | 132 | if (ns->uts_ns) |
| 133 | put_uts_ns(ns->uts_ns); |
Kirill Korotaev | 25b21cb | 2006-10-02 02:18:19 -0700 | [diff] [blame] | 134 | if (ns->ipc_ns) |
| 135 | put_ipc_ns(ns->ipc_ns); |
Serge E. Hallyn | ab51601 | 2006-10-02 02:18:06 -0700 | [diff] [blame] | 136 | kfree(ns); |
| 137 | } |