xref: /netbsd-src/sys/compat/linux/common/linux_sysctl.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: linux_sysctl.c,v 1.32 2007/10/19 18:52:12 njoly Exp $	*/
2 
3 /*-
4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Andrew Brown.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the NetBSD
21  *      Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * sysctl system call.
41  */
42 
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: linux_sysctl.c,v 1.32 2007/10/19 18:52:12 njoly Exp $");
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/errno.h>
49 #include <sys/proc.h>
50 #include <sys/mount.h>
51 #include <sys/sysctl.h>
52 #include <sys/syscallargs.h>
53 #include <sys/ktrace.h>
54 
55 #include <compat/linux/common/linux_types.h>
56 #include <compat/linux/common/linux_signal.h>
57 #include <compat/linux/common/linux_ipc.h>
58 #include <compat/linux/common/linux_sem.h>
59 
60 #include <compat/linux/linux_syscallargs.h>
61 #include <compat/linux/common/linux_sysctl.h>
62 #include <compat/linux/common/linux_exec.h>
63 #include <compat/linux/common/linux_machdep.h>
64 
65 char linux_sysname[128] = "Linux";
66 #if defined(__amd64__) || defined(__i386__) || defined(__powerpc__)
67 char linux_release[128] = "2.4.18";
68 char linux_version[128] = "#0 Wed Feb 20 20:00:02 CET 2002";
69 #else
70 char linux_release[128] = "2.0.38";
71 char linux_version[128] = "#0 Sun Nov 11 11:11:11 MET 2000";
72 #endif
73 
74 #ifndef _LKM
75 static
76 #endif
77 struct sysctlnode linux_sysctl_root = {
78 	.sysctl_flags = SYSCTL_VERSION|
79 	    CTLFLAG_ROOT|CTLTYPE_NODE|CTLFLAG_READWRITE,
80 	.sysctl_num = 0,
81 	.sysctl_name = "(linux_root)",
82 	sysc_init_field(_sysctl_size, sizeof(struct sysctlnode)),
83 };
84 
85 /*
86  * setup for small sysctl tree used by emulation
87  */
88 SYSCTL_SETUP(linux_sysctl_setup, "linux emulated sysctl subtree setup")
89 {
90 	const struct sysctlnode *node = &linux_sysctl_root;
91 
92 	sysctl_createv(clog, 0, &node, &node,
93 		       CTLFLAG_PERMANENT,
94 		       CTLTYPE_NODE, "kern", NULL,
95 		       NULL, 0, NULL, 0,
96 		       LINUX_CTL_KERN, CTL_EOL);
97 
98 	sysctl_createv(clog, 0, &node, NULL,
99 		       CTLFLAG_PERMANENT,
100 		       CTLTYPE_STRING, "ostype", NULL,
101 		       NULL, 0, linux_sysname, sizeof(linux_sysname),
102 		       LINUX_KERN_OSTYPE, CTL_EOL);
103 	sysctl_createv(clog, 0, &node, NULL,
104 		       CTLFLAG_PERMANENT,
105 		       CTLTYPE_STRING, "osrelease", NULL,
106 		       NULL, 0, linux_release, sizeof(linux_release),
107 		       LINUX_KERN_OSRELEASE, CTL_EOL);
108 	sysctl_createv(clog, 0, &node, NULL,
109 		       CTLFLAG_PERMANENT,
110 		       CTLTYPE_STRING, "version", NULL,
111 		       NULL, 0, linux_version, sizeof(linux_version),
112 		       LINUX_KERN_VERSION, CTL_EOL);
113 
114 	linux_sysctl_root.sysctl_flags &= ~CTLFLAG_READWRITE;
115 }
116 
117 /*
118  * linux sysctl system call
119  */
120 int
121 linux_sys___sysctl(struct lwp *l, void *v, register_t *retval)
122 {
123 	struct linux_sys___sysctl_args *uap = v;
124 	struct linux___sysctl ls;
125 	int error, nerror, name[CTL_MAXNAME];
126 	size_t savelen = 0, oldlen = 0;
127 
128 	/*
129 	 * get linux args structure
130 	 */
131 	if ((error = copyin(SCARG(uap, lsp), &ls, sizeof(ls))))
132 		return error;
133 
134 	/*
135 	 * get oldlen
136 	 */
137 	oldlen = 0;
138 	if (ls.oldlenp != NULL) {
139 		error = copyin(ls.oldlenp, &oldlen, sizeof(oldlen));
140 		if (error)
141 			return (error);
142 	}
143 	savelen = oldlen;
144 
145 	/*
146 	 * top-level sysctl names may or may not be non-terminal, but
147 	 * we don't care
148 	 */
149 	if (ls.nlen > CTL_MAXNAME || ls.nlen < 1)
150 		return (EINVAL);
151 	error = copyin(ls.name, &name, ls.nlen * sizeof(int));
152 	if (error)
153 		return (error);
154 
155        ktrmib(name, ls.nlen);
156 
157 	/*
158 	 * wire old so that copyout() is less likely to fail?
159 	 */
160 	error = sysctl_lock(l, ls.oldval, savelen);
161 	if (error)
162 		return (error);
163 
164 	/*
165 	 * dispatch request into linux sysctl tree
166 	 */
167 	error = sysctl_dispatch(&name[0], ls.nlen,
168 				ls.oldval, &oldlen,
169 				ls.newval, ls.newlen,
170 				&name[0], l, &linux_sysctl_root);
171 
172 	/*
173 	 * release the sysctl lock
174 	 */
175 	sysctl_unlock(l);
176 
177 	/*
178 	 * reset caller's oldlen, even if we got an error
179 	 */
180 	if (ls.oldlenp) {
181 		nerror = copyout(&oldlen, ls.oldlenp, sizeof(oldlen));
182 		if (error == 0)
183 			error = nerror;
184 	}
185 
186 	/*
187 	 * if the only problem is that we weren't given enough space,
188 	 * that's an ENOMEM error
189 	 */
190 	if (error == 0 && ls.oldval != NULL && savelen < oldlen)
191 		error = ENOMEM;
192 
193 	return (error);
194 }
195 
196 /*
197  * kernel related system variables under emul.linux in the main sysctl
198  * tree
199  */
200 SYSCTL_SETUP(sysctl_emul_linux_setup, "sysctl emul.linux subtree setup")
201 {
202 
203 	sysctl_createv(clog, 0, NULL, NULL,
204 		       CTLFLAG_PERMANENT,
205 		       CTLTYPE_NODE, "emul", NULL,
206 		       NULL, 0, NULL, 0,
207 		       CTL_EMUL, CTL_EOL);
208 	sysctl_createv(clog, 0, NULL, NULL,
209 		       CTLFLAG_PERMANENT,
210 		       CTLTYPE_NODE, "linux",
211 		       SYSCTL_DESCR("Linux emulation settings"),
212 		       NULL, 0, NULL, 0,
213 		       CTL_EMUL, EMUL_LINUX, CTL_EOL);
214 	sysctl_createv(clog, 0, NULL, NULL,
215 		       CTLFLAG_PERMANENT,
216 		       CTLTYPE_NODE, "kern",
217 		       SYSCTL_DESCR("Linux kernel emulation settings"),
218 		       NULL, 0, NULL, 0,
219 		       CTL_EMUL, EMUL_LINUX, EMUL_LINUX_KERN, CTL_EOL);
220 
221 	sysctl_createv(clog, 0, NULL, NULL,
222 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
223 		       CTLTYPE_STRING, "ostype",
224 		       SYSCTL_DESCR("Linux operating system type"),
225 		       NULL, 0, linux_sysname, sizeof(linux_sysname),
226 		       CTL_EMUL, EMUL_LINUX, EMUL_LINUX_KERN,
227 		       EMUL_LINUX_KERN_OSTYPE, CTL_EOL);
228 	sysctl_createv(clog, 0, NULL, NULL,
229 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
230 		       CTLTYPE_STRING, "osrelease",
231 		       SYSCTL_DESCR("Linux operating system release"),
232 		       NULL, 0, linux_release, sizeof(linux_release),
233 		       CTL_EMUL, EMUL_LINUX, EMUL_LINUX_KERN,
234 		       EMUL_LINUX_KERN_OSRELEASE, CTL_EOL);
235 	sysctl_createv(clog, 0, NULL, NULL,
236 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
237 		       CTLTYPE_STRING, "osversion",
238 		       SYSCTL_DESCR("Linux operating system revision"),
239 		       NULL, 0, linux_version, sizeof(linux_version),
240 		       CTL_EMUL, EMUL_LINUX, EMUL_LINUX_KERN,
241 		       EMUL_LINUX_KERN_VERSION, CTL_EOL);
242 }
243