blob: 3ae43936bd88fbd250e4a2951e5ded19c34c74b3 [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>
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070017
18/*
Serge E. Hallyn071df102006-10-02 02:18:17 -070019 * 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 */
23static 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);
Cedric Le Goater467e9f42007-07-15 23:41:06 -070028 if (!ns)
29 return ERR_PTR(-ENOMEM);
30
31 memcpy(&ns->name, &old_ns->name, sizeof(ns->name));
32 kref_init(&ns->kref);
Serge E. Hallyn071df102006-10-02 02:18:17 -070033 return ns;
34}
35
36/*
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070037 * Copy task tsk's utsname namespace, or clone it if flags
38 * specifies CLONE_NEWUTS. In latter case, changes to the
39 * utsname of this process won't be seen by parent, and vice
40 * versa.
41 */
Badari Pulavartye3222c42007-05-08 00:25:21 -070042struct uts_namespace *copy_utsname(int flags, struct uts_namespace *old_ns)
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070043{
Serge E. Hallyn071df102006-10-02 02:18:17 -070044 struct uts_namespace *new_ns;
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070045
Badari Pulavartye3222c42007-05-08 00:25:21 -070046 BUG_ON(!old_ns);
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070047 get_uts_ns(old_ns);
48
Serge E. Hallyn071df102006-10-02 02:18:17 -070049 if (!(flags & CLONE_NEWUTS))
Badari Pulavartye3222c42007-05-08 00:25:21 -070050 return old_ns;
Serge E. Hallyn071df102006-10-02 02:18:17 -070051
52 new_ns = clone_uts_ns(old_ns);
Serge E. Hallyn071df102006-10-02 02:18:17 -070053
Serge E. Hallyn071df102006-10-02 02:18:17 -070054 put_uts_ns(old_ns);
Badari Pulavartye3222c42007-05-08 00:25:21 -070055 return new_ns;
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070056}
57
58void free_uts_ns(struct kref *kref)
59{
60 struct uts_namespace *ns;
61
62 ns = container_of(kref, struct uts_namespace, kref);
63 kfree(ns);
64}