This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / kernel / utsname.c
1 /*
2  *  Copyright (C) 2004 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/uts.h>
14 #include <linux/utsname.h>
15 #include <linux/version.h>
16 #include <linux/vserver/global.h>
17
18 /*
19  * Clone a new ns copying an original utsname, setting refcount to 1
20  * @old_ns: namespace to clone
21  * Return NULL on error (failure to kmalloc), new ns otherwise
22  */
23 static struct uts_namespace *clone_uts_ns(struct uts_namespace *old_ns)
24 {
25         struct uts_namespace *ns;
26
27         ns = kmalloc(sizeof(struct uts_namespace), GFP_KERNEL);
28         if (ns) {
29                 memcpy(&ns->name, &old_ns->name, sizeof(ns->name));
30                 kref_init(&ns->kref);
31                 atomic_inc(&vs_global_uts_ns);
32         }
33         return ns;
34 }
35
36 /*
37  * unshare the current process' utsname namespace.
38  * called only in sys_unshare()
39  */
40 int unshare_utsname(unsigned long unshare_flags, struct uts_namespace **new_uts)
41 {
42         if (unshare_flags & CLONE_NEWUTS) {
43                 if (!capable(CAP_SYS_ADMIN))
44                         return -EPERM;
45
46                 *new_uts = clone_uts_ns(current->nsproxy->uts_ns);
47                 if (!*new_uts)
48                         return -ENOMEM;
49         }
50
51         return 0;
52 }
53
54 /*
55  * Copy task tsk's utsname namespace, or clone it if flags
56  * specifies CLONE_NEWUTS.  In latter case, changes to the
57  * utsname of this process won't be seen by parent, and vice
58  * versa.
59  */
60 int copy_utsname(int flags, struct task_struct *tsk)
61 {
62         struct uts_namespace *old_ns = tsk->nsproxy->uts_ns;
63         struct uts_namespace *new_ns;
64         int err = 0;
65
66         if (!old_ns)
67                 return 0;
68
69         get_uts_ns(old_ns);
70
71         if (!(flags & CLONE_NEWUTS))
72                 return 0;
73
74         if (!capable(CAP_SYS_ADMIN)) {
75                 err = -EPERM;
76                 goto out;
77         }
78
79         new_ns = clone_uts_ns(old_ns);
80         if (!new_ns) {
81                 err = -ENOMEM;
82                 goto out;
83         }
84         tsk->nsproxy->uts_ns = new_ns;
85
86 out:
87         put_uts_ns(old_ns);
88         return err;
89 }
90
91 void free_uts_ns(struct kref *kref)
92 {
93         struct uts_namespace *ns;
94
95         ns = container_of(kref, struct uts_namespace, kref);
96         atomic_dec(&vs_global_uts_ns);
97         kfree(ns);
98 }