blob: 8a82b4b8ea52cb3690e25bf039eaf32ff5c8c079 [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. Hallyn4865ecf2006-10-02 02:18:14 -070017
Alexey Dobriyan4c2a7e72009-06-17 16:27:54 -070018static struct uts_namespace *create_uts_ns(void)
19{
20 struct uts_namespace *uts_ns;
21
22 uts_ns = kmalloc(sizeof(struct uts_namespace), GFP_KERNEL);
23 if (uts_ns)
24 kref_init(&uts_ns->kref);
25 return uts_ns;
26}
27
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070028/*
Serge E. Hallyn071df102006-10-02 02:18:17 -070029 * Clone a new ns copying an original utsname, setting refcount to 1
30 * @old_ns: namespace to clone
31 * Return NULL on error (failure to kmalloc), new ns otherwise
32 */
33static struct uts_namespace *clone_uts_ns(struct uts_namespace *old_ns)
34{
35 struct uts_namespace *ns;
36
Alexey Dobriyan4c2a7e72009-06-17 16:27:54 -070037 ns = create_uts_ns();
Cedric Le Goater467e9f42007-07-15 23:41:06 -070038 if (!ns)
39 return ERR_PTR(-ENOMEM);
40
Alexey Dobriyanefc63c42007-09-18 22:46:27 -070041 down_read(&uts_sem);
Cedric Le Goater467e9f42007-07-15 23:41:06 -070042 memcpy(&ns->name, &old_ns->name, sizeof(ns->name));
Alexey Dobriyanefc63c42007-09-18 22:46:27 -070043 up_read(&uts_sem);
Serge E. Hallyn071df102006-10-02 02:18:17 -070044 return ns;
45}
46
47/*
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070048 * Copy task tsk's utsname namespace, or clone it if flags
49 * specifies CLONE_NEWUTS. In latter case, changes to the
50 * utsname of this process won't be seen by parent, and vice
51 * versa.
52 */
Eric W. Biederman213dd262007-07-15 23:41:15 -070053struct uts_namespace *copy_utsname(unsigned long flags, struct uts_namespace *old_ns)
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070054{
Serge E. Hallyn071df102006-10-02 02:18:17 -070055 struct uts_namespace *new_ns;
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070056
Badari Pulavartye3222c42007-05-08 00:25:21 -070057 BUG_ON(!old_ns);
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070058 get_uts_ns(old_ns);
59
Serge E. Hallyn071df102006-10-02 02:18:17 -070060 if (!(flags & CLONE_NEWUTS))
Badari Pulavartye3222c42007-05-08 00:25:21 -070061 return old_ns;
Serge E. Hallyn071df102006-10-02 02:18:17 -070062
63 new_ns = clone_uts_ns(old_ns);
Serge E. Hallyn071df102006-10-02 02:18:17 -070064
Serge E. Hallyn071df102006-10-02 02:18:17 -070065 put_uts_ns(old_ns);
Badari Pulavartye3222c42007-05-08 00:25:21 -070066 return new_ns;
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070067}
68
69void free_uts_ns(struct kref *kref)
70{
71 struct uts_namespace *ns;
72
73 ns = container_of(kref, struct uts_namespace, kref);
74 kfree(ns);
75}