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