userns: add a user_namespace as creator/owner of uts_namespace
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / kernel / utsname.c
CommitLineData
4865ecf1
SH
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>
467e9f4b 15#include <linux/err.h>
1aeb272c 16#include <linux/slab.h>
59607db3 17#include <linux/user_namespace.h>
4865ecf1 18
4c2a7e72
AD
19static struct uts_namespace *create_uts_ns(void)
20{
21 struct uts_namespace *uts_ns;
22
23 uts_ns = kmalloc(sizeof(struct uts_namespace), GFP_KERNEL);
24 if (uts_ns)
25 kref_init(&uts_ns->kref);
26 return uts_ns;
27}
28
071df104
SH
29/*
30 * Clone a new ns copying an original utsname, setting refcount to 1
31 * @old_ns: namespace to clone
32 * Return NULL on error (failure to kmalloc), new ns otherwise
33 */
34static struct uts_namespace *clone_uts_ns(struct uts_namespace *old_ns)
35{
36 struct uts_namespace *ns;
37
4c2a7e72 38 ns = create_uts_ns();
467e9f4b
CLG
39 if (!ns)
40 return ERR_PTR(-ENOMEM);
41
efc63c4f 42 down_read(&uts_sem);
467e9f4b 43 memcpy(&ns->name, &old_ns->name, sizeof(ns->name));
59607db3
SH
44 ns->user_ns = old_ns->user_ns;
45 get_user_ns(ns->user_ns);
efc63c4f 46 up_read(&uts_sem);
071df104
SH
47 return ns;
48}
49
4865ecf1
SH
50/*
51 * Copy task tsk's utsname namespace, or clone it if flags
52 * specifies CLONE_NEWUTS. In latter case, changes to the
53 * utsname of this process won't be seen by parent, and vice
54 * versa.
55 */
213dd266 56struct uts_namespace *copy_utsname(unsigned long flags, struct uts_namespace *old_ns)
4865ecf1 57{
071df104 58 struct uts_namespace *new_ns;
4865ecf1 59
e3222c4e 60 BUG_ON(!old_ns);
4865ecf1
SH
61 get_uts_ns(old_ns);
62
071df104 63 if (!(flags & CLONE_NEWUTS))
e3222c4e 64 return old_ns;
071df104
SH
65
66 new_ns = clone_uts_ns(old_ns);
071df104 67
071df104 68 put_uts_ns(old_ns);
e3222c4e 69 return new_ns;
4865ecf1
SH
70}
71
72void free_uts_ns(struct kref *kref)
73{
74 struct uts_namespace *ns;
75
76 ns = container_of(kref, struct uts_namespace, kref);
59607db3 77 put_user_ns(ns->user_ns);
4865ecf1
SH
78 kfree(ns);
79}