| Serge E. Hallyn | 4865ecf | 2006-10-02 02:18:14 -0700 | [diff] [blame] | 1 | /* | 
 | 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 Goater | 467e9f4 | 2007-07-15 23:41:06 -0700 | [diff] [blame] | 15 | #include <linux/err.h> | 
| Robert P. J. Day | 1aeb272 | 2008-04-29 00:59:25 -0700 | [diff] [blame] | 16 | #include <linux/slab.h> | 
| Serge E. Hallyn | 4865ecf | 2006-10-02 02:18:14 -0700 | [diff] [blame] | 17 |  | 
 | 18 | /* | 
| Serge E. Hallyn | 071df10 | 2006-10-02 02:18:17 -0700 | [diff] [blame] | 19 |  * 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 |  */ | 
 | 23 | static 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 Goater | 467e9f4 | 2007-07-15 23:41:06 -0700 | [diff] [blame] | 28 | 	if (!ns) | 
 | 29 | 		return ERR_PTR(-ENOMEM); | 
 | 30 |  | 
| Alexey Dobriyan | efc63c4 | 2007-09-18 22:46:27 -0700 | [diff] [blame] | 31 | 	down_read(&uts_sem); | 
| Cedric Le Goater | 467e9f4 | 2007-07-15 23:41:06 -0700 | [diff] [blame] | 32 | 	memcpy(&ns->name, &old_ns->name, sizeof(ns->name)); | 
| Alexey Dobriyan | efc63c4 | 2007-09-18 22:46:27 -0700 | [diff] [blame] | 33 | 	up_read(&uts_sem); | 
| Cedric Le Goater | 467e9f4 | 2007-07-15 23:41:06 -0700 | [diff] [blame] | 34 | 	kref_init(&ns->kref); | 
| Serge E. Hallyn | 071df10 | 2006-10-02 02:18:17 -0700 | [diff] [blame] | 35 | 	return ns; | 
 | 36 | } | 
 | 37 |  | 
 | 38 | /* | 
| Serge E. Hallyn | 4865ecf | 2006-10-02 02:18:14 -0700 | [diff] [blame] | 39 |  * Copy task tsk's utsname namespace, or clone it if flags | 
 | 40 |  * specifies CLONE_NEWUTS.  In latter case, changes to the | 
 | 41 |  * utsname of this process won't be seen by parent, and vice | 
 | 42 |  * versa. | 
 | 43 |  */ | 
| Eric W. Biederman | 213dd26 | 2007-07-15 23:41:15 -0700 | [diff] [blame] | 44 | struct uts_namespace *copy_utsname(unsigned long flags, struct uts_namespace *old_ns) | 
| Serge E. Hallyn | 4865ecf | 2006-10-02 02:18:14 -0700 | [diff] [blame] | 45 | { | 
| Serge E. Hallyn | 071df10 | 2006-10-02 02:18:17 -0700 | [diff] [blame] | 46 | 	struct uts_namespace *new_ns; | 
| Serge E. Hallyn | 4865ecf | 2006-10-02 02:18:14 -0700 | [diff] [blame] | 47 |  | 
| Badari Pulavarty | e3222c4 | 2007-05-08 00:25:21 -0700 | [diff] [blame] | 48 | 	BUG_ON(!old_ns); | 
| Serge E. Hallyn | 4865ecf | 2006-10-02 02:18:14 -0700 | [diff] [blame] | 49 | 	get_uts_ns(old_ns); | 
 | 50 |  | 
| Serge E. Hallyn | 071df10 | 2006-10-02 02:18:17 -0700 | [diff] [blame] | 51 | 	if (!(flags & CLONE_NEWUTS)) | 
| Badari Pulavarty | e3222c4 | 2007-05-08 00:25:21 -0700 | [diff] [blame] | 52 | 		return old_ns; | 
| Serge E. Hallyn | 071df10 | 2006-10-02 02:18:17 -0700 | [diff] [blame] | 53 |  | 
 | 54 | 	new_ns = clone_uts_ns(old_ns); | 
| Serge E. Hallyn | 071df10 | 2006-10-02 02:18:17 -0700 | [diff] [blame] | 55 |  | 
| Serge E. Hallyn | 071df10 | 2006-10-02 02:18:17 -0700 | [diff] [blame] | 56 | 	put_uts_ns(old_ns); | 
| Badari Pulavarty | e3222c4 | 2007-05-08 00:25:21 -0700 | [diff] [blame] | 57 | 	return new_ns; | 
| Serge E. Hallyn | 4865ecf | 2006-10-02 02:18:14 -0700 | [diff] [blame] | 58 | } | 
 | 59 |  | 
 | 60 | void free_uts_ns(struct kref *kref) | 
 | 61 | { | 
 | 62 | 	struct uts_namespace *ns; | 
 | 63 |  | 
 | 64 | 	ns = container_of(kref, struct uts_namespace, kref); | 
 | 65 | 	kfree(ns); | 
 | 66 | } |