blob: 160c8c5136bd6a6607ccfae5cfc2e285fe5791c6 [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>
16
17/*
Serge E. Hallyn071df102006-10-02 02:18:17 -070018 * Clone a new ns copying an original utsname, setting refcount to 1
19 * @old_ns: namespace to clone
20 * Return NULL on error (failure to kmalloc), new ns otherwise
21 */
22static struct uts_namespace *clone_uts_ns(struct uts_namespace *old_ns)
23{
24 struct uts_namespace *ns;
25
26 ns = kmalloc(sizeof(struct uts_namespace), GFP_KERNEL);
27 if (ns) {
28 memcpy(&ns->name, &old_ns->name, sizeof(ns->name));
29 kref_init(&ns->kref);
30 }
31 return ns;
32}
33
34/*
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070035 * Copy task tsk's utsname namespace, or clone it if flags
36 * specifies CLONE_NEWUTS. In latter case, changes to the
37 * utsname of this process won't be seen by parent, and vice
38 * versa.
39 */
Badari Pulavartye3222c42007-05-08 00:25:21 -070040struct uts_namespace *copy_utsname(int flags, struct uts_namespace *old_ns)
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070041{
Serge E. Hallyn071df102006-10-02 02:18:17 -070042 struct uts_namespace *new_ns;
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070043
Badari Pulavartye3222c42007-05-08 00:25:21 -070044 BUG_ON(!old_ns);
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070045 get_uts_ns(old_ns);
46
Serge E. Hallyn071df102006-10-02 02:18:17 -070047 if (!(flags & CLONE_NEWUTS))
Badari Pulavartye3222c42007-05-08 00:25:21 -070048 return old_ns;
Serge E. Hallyn071df102006-10-02 02:18:17 -070049
50 new_ns = clone_uts_ns(old_ns);
Serge E. Hallyn071df102006-10-02 02:18:17 -070051
Serge E. Hallyn071df102006-10-02 02:18:17 -070052 put_uts_ns(old_ns);
Badari Pulavartye3222c42007-05-08 00:25:21 -070053 return new_ns;
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070054}
55
56void free_uts_ns(struct kref *kref)
57{
58 struct uts_namespace *ns;
59
60 ns = container_of(kref, struct uts_namespace, kref);
61 kfree(ns);
62}