blob: 64d398f1244436cbdc4ede981dd927e39ee708b5 [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>
15#include <linux/version.h>
Cedric Le Goater467e9f42007-07-15 23:41:06 -070016#include <linux/err.h>
Robert P. J. Day1aeb2722008-04-29 00:59:25 -070017#include <linux/slab.h>
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070018
19/*
Serge E. Hallyn071df102006-10-02 02:18:17 -070020 * Clone a new ns copying an original utsname, setting refcount to 1
21 * @old_ns: namespace to clone
22 * Return NULL on error (failure to kmalloc), new ns otherwise
23 */
24static struct uts_namespace *clone_uts_ns(struct uts_namespace *old_ns)
25{
26 struct uts_namespace *ns;
27
28 ns = kmalloc(sizeof(struct uts_namespace), GFP_KERNEL);
Cedric Le Goater467e9f42007-07-15 23:41:06 -070029 if (!ns)
30 return ERR_PTR(-ENOMEM);
31
Alexey Dobriyanefc63c42007-09-18 22:46:27 -070032 down_read(&uts_sem);
Cedric Le Goater467e9f42007-07-15 23:41:06 -070033 memcpy(&ns->name, &old_ns->name, sizeof(ns->name));
Alexey Dobriyanefc63c42007-09-18 22:46:27 -070034 up_read(&uts_sem);
Cedric Le Goater467e9f42007-07-15 23:41:06 -070035 kref_init(&ns->kref);
Serge E. Hallyn071df102006-10-02 02:18:17 -070036 return ns;
37}
38
39/*
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070040 * Copy task tsk's utsname namespace, or clone it if flags
41 * specifies CLONE_NEWUTS. In latter case, changes to the
42 * utsname of this process won't be seen by parent, and vice
43 * versa.
44 */
Eric W. Biederman213dd262007-07-15 23:41:15 -070045struct uts_namespace *copy_utsname(unsigned long flags, struct uts_namespace *old_ns)
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070046{
Serge E. Hallyn071df102006-10-02 02:18:17 -070047 struct uts_namespace *new_ns;
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070048
Badari Pulavartye3222c42007-05-08 00:25:21 -070049 BUG_ON(!old_ns);
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070050 get_uts_ns(old_ns);
51
Serge E. Hallyn071df102006-10-02 02:18:17 -070052 if (!(flags & CLONE_NEWUTS))
Badari Pulavartye3222c42007-05-08 00:25:21 -070053 return old_ns;
Serge E. Hallyn071df102006-10-02 02:18:17 -070054
55 new_ns = clone_uts_ns(old_ns);
Serge E. Hallyn071df102006-10-02 02:18:17 -070056
Serge E. Hallyn071df102006-10-02 02:18:17 -070057 put_uts_ns(old_ns);
Badari Pulavartye3222c42007-05-08 00:25:21 -070058 return new_ns;
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070059}
60
61void free_uts_ns(struct kref *kref)
62{
63 struct uts_namespace *ns;
64
65 ns = container_of(kref, struct uts_namespace, kref);
66 kfree(ns);
67}