xref: /openbmc/linux/kernel/utsname_sysctl.c (revision 22246614)
1 /*
2  *  Copyright (C) 2007
3  *
4  *  Author: Eric Biederman <ebiederm@xmision.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 #include <linux/sysctl.h>
17 
18 static void *get_uts(ctl_table *table, int write)
19 {
20 	char *which = table->data;
21 	struct uts_namespace *uts_ns;
22 
23 	uts_ns = current->nsproxy->uts_ns;
24 	which = (which - (char *)&init_uts_ns) + (char *)uts_ns;
25 
26 	if (!write)
27 		down_read(&uts_sem);
28 	else
29 		down_write(&uts_sem);
30 	return which;
31 }
32 
33 static void put_uts(ctl_table *table, int write, void *which)
34 {
35 	if (!write)
36 		up_read(&uts_sem);
37 	else
38 		up_write(&uts_sem);
39 }
40 
41 #ifdef CONFIG_PROC_FS
42 /*
43  *	Special case of dostring for the UTS structure. This has locks
44  *	to observe. Should this be in kernel/sys.c ????
45  */
46 static int proc_do_uts_string(ctl_table *table, int write, struct file *filp,
47 		  void __user *buffer, size_t *lenp, loff_t *ppos)
48 {
49 	struct ctl_table uts_table;
50 	int r;
51 	memcpy(&uts_table, table, sizeof(uts_table));
52 	uts_table.data = get_uts(table, write);
53 	r = proc_dostring(&uts_table,write,filp,buffer,lenp, ppos);
54 	put_uts(table, write, uts_table.data);
55 	return r;
56 }
57 #else
58 #define proc_do_uts_string NULL
59 #endif
60 
61 
62 #ifdef CONFIG_SYSCTL_SYSCALL
63 /* The generic string strategy routine: */
64 static int sysctl_uts_string(ctl_table *table, int __user *name, int nlen,
65 		  void __user *oldval, size_t __user *oldlenp,
66 		  void __user *newval, size_t newlen)
67 {
68 	struct ctl_table uts_table;
69 	int r, write;
70 	write = newval && newlen;
71 	memcpy(&uts_table, table, sizeof(uts_table));
72 	uts_table.data = get_uts(table, write);
73 	r = sysctl_string(&uts_table, name, nlen,
74 		oldval, oldlenp, newval, newlen);
75 	put_uts(table, write, uts_table.data);
76 	return r;
77 }
78 #else
79 #define sysctl_uts_string NULL
80 #endif
81 
82 static struct ctl_table uts_kern_table[] = {
83 	{
84 		.ctl_name	= KERN_OSTYPE,
85 		.procname	= "ostype",
86 		.data		= init_uts_ns.name.sysname,
87 		.maxlen		= sizeof(init_uts_ns.name.sysname),
88 		.mode		= 0444,
89 		.proc_handler	= proc_do_uts_string,
90 		.strategy	= sysctl_uts_string,
91 	},
92 	{
93 		.ctl_name	= KERN_OSRELEASE,
94 		.procname	= "osrelease",
95 		.data		= init_uts_ns.name.release,
96 		.maxlen		= sizeof(init_uts_ns.name.release),
97 		.mode		= 0444,
98 		.proc_handler	= proc_do_uts_string,
99 		.strategy	= sysctl_uts_string,
100 	},
101 	{
102 		.ctl_name	= KERN_VERSION,
103 		.procname	= "version",
104 		.data		= init_uts_ns.name.version,
105 		.maxlen		= sizeof(init_uts_ns.name.version),
106 		.mode		= 0444,
107 		.proc_handler	= proc_do_uts_string,
108 		.strategy	= sysctl_uts_string,
109 	},
110 	{
111 		.ctl_name	= KERN_NODENAME,
112 		.procname	= "hostname",
113 		.data		= init_uts_ns.name.nodename,
114 		.maxlen		= sizeof(init_uts_ns.name.nodename),
115 		.mode		= 0644,
116 		.proc_handler	= proc_do_uts_string,
117 		.strategy	= sysctl_uts_string,
118 	},
119 	{
120 		.ctl_name	= KERN_DOMAINNAME,
121 		.procname	= "domainname",
122 		.data		= init_uts_ns.name.domainname,
123 		.maxlen		= sizeof(init_uts_ns.name.domainname),
124 		.mode		= 0644,
125 		.proc_handler	= proc_do_uts_string,
126 		.strategy	= sysctl_uts_string,
127 	},
128 	{}
129 };
130 
131 static struct ctl_table uts_root_table[] = {
132 	{
133 		.ctl_name	= CTL_KERN,
134 		.procname	= "kernel",
135 		.mode		= 0555,
136 		.child		= uts_kern_table,
137 	},
138 	{}
139 };
140 
141 static int __init utsname_sysctl_init(void)
142 {
143 	register_sysctl_table(uts_root_table);
144 	return 0;
145 }
146 
147 __initcall(utsname_sysctl_init);
148