blob: a7b3a8d1ad24cebcd69982aa9d924542d02cb53a [file] [log] [blame]
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -07001/*
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>
Cedric Le Goater467e9f42007-07-15 23:41:06 -070015#include <linux/err.h>
Robert P. J. Day1aeb2722008-04-29 00:59:25 -070016#include <linux/slab.h>
Serge E. Hallyn59607db2011-03-23 16:43:16 -070017#include <linux/user_namespace.h>
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070018
Alexey Dobriyan4c2a7e72009-06-17 16:27:54 -070019static 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
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070029/*
Serge E. Hallyn071df102006-10-02 02:18:17 -070030 * 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
Alexey Dobriyan4c2a7e72009-06-17 16:27:54 -070038 ns = create_uts_ns();
Cedric Le Goater467e9f42007-07-15 23:41:06 -070039 if (!ns)
40 return ERR_PTR(-ENOMEM);
41
Alexey Dobriyanefc63c42007-09-18 22:46:27 -070042 down_read(&uts_sem);
Cedric Le Goater467e9f42007-07-15 23:41:06 -070043 memcpy(&ns->name, &old_ns->name, sizeof(ns->name));
Serge E. Hallyn59607db2011-03-23 16:43:16 -070044 ns->user_ns = old_ns->user_ns;
45 get_user_ns(ns->user_ns);
Alexey Dobriyanefc63c42007-09-18 22:46:27 -070046 up_read(&uts_sem);
Serge E. Hallyn071df102006-10-02 02:18:17 -070047 return ns;
48}
49
50/*
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070051 * 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 */
Eric W. Biederman213dd262007-07-15 23:41:15 -070056struct uts_namespace *copy_utsname(unsigned long flags, struct uts_namespace *old_ns)
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070057{
Serge E. Hallyn071df102006-10-02 02:18:17 -070058 struct uts_namespace *new_ns;
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070059
Badari Pulavartye3222c42007-05-08 00:25:21 -070060 BUG_ON(!old_ns);
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070061 get_uts_ns(old_ns);
62
Serge E. Hallyn071df102006-10-02 02:18:17 -070063 if (!(flags & CLONE_NEWUTS))
Badari Pulavartye3222c42007-05-08 00:25:21 -070064 return old_ns;
Serge E. Hallyn071df102006-10-02 02:18:17 -070065
66 new_ns = clone_uts_ns(old_ns);
Serge E. Hallyn071df102006-10-02 02:18:17 -070067
Serge E. Hallyn071df102006-10-02 02:18:17 -070068 put_uts_ns(old_ns);
Badari Pulavartye3222c42007-05-08 00:25:21 -070069 return new_ns;
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070070}
71
72void free_uts_ns(struct kref *kref)
73{
74 struct uts_namespace *ns;
75
76 ns = container_of(kref, struct uts_namespace, kref);
Serge E. Hallyn59607db2011-03-23 16:43:16 -070077 put_user_ns(ns->user_ns);
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070078 kfree(ns);
79}