xref: /netbsd-src/sys/compat/linux/common/linux_sysctl.c (revision 0920b4f20b78ab1ccd9f2312fbe10deaf000cbf3)
1 /*	$NetBSD: linux_sysctl.c,v 1.31 2007/08/15 12:07:30 ad 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.31 2007/08/15 12:07:30 ad 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 
58 #include <compat/linux/linux_syscallargs.h>
59 #include <compat/linux/common/linux_sysctl.h>
60 #include <compat/linux/common/linux_exec.h>
61 #include <compat/linux/common/linux_machdep.h>
62 
63 char linux_sysname[128] = "Linux";
64 #if defined(__amd64__) || defined(__i386__) || defined(__powerpc__)
65 char linux_release[128] = "2.4.18";
66 char linux_version[128] = "#0 Wed Feb 20 20:00:02 CET 2002";
67 #else
68 char linux_release[128] = "2.0.38";
69 char linux_version[128] = "#0 Sun Nov 11 11:11:11 MET 2000";
70 #endif
71 
72 #ifndef _LKM
73 static
74 #endif
75 struct sysctlnode linux_sysctl_root = {
76 	.sysctl_flags = SYSCTL_VERSION|
77 	    CTLFLAG_ROOT|CTLTYPE_NODE|CTLFLAG_READWRITE,
78 	.sysctl_num = 0,
79 	.sysctl_name = "(linux_root)",
80 	sysc_init_field(_sysctl_size, sizeof(struct sysctlnode)),
81 };
82 
83 /*
84  * setup for small sysctl tree used by emulation
85  */
86 SYSCTL_SETUP(linux_sysctl_setup, "linux emulated sysctl subtree setup")
87 {
88 	const struct sysctlnode *node = &linux_sysctl_root;
89 
90 	sysctl_createv(clog, 0, &node, &node,
91 		       CTLFLAG_PERMANENT,
92 		       CTLTYPE_NODE, "kern", NULL,
93 		       NULL, 0, NULL, 0,
94 		       LINUX_CTL_KERN, CTL_EOL);
95 
96 	sysctl_createv(clog, 0, &node, NULL,
97 		       CTLFLAG_PERMANENT,
98 		       CTLTYPE_STRING, "ostype", NULL,
99 		       NULL, 0, linux_sysname, sizeof(linux_sysname),
100 		       LINUX_KERN_OSTYPE, CTL_EOL);
101 	sysctl_createv(clog, 0, &node, NULL,
102 		       CTLFLAG_PERMANENT,
103 		       CTLTYPE_STRING, "osrelease", NULL,
104 		       NULL, 0, linux_release, sizeof(linux_release),
105 		       LINUX_KERN_OSRELEASE, CTL_EOL);
106 	sysctl_createv(clog, 0, &node, NULL,
107 		       CTLFLAG_PERMANENT,
108 		       CTLTYPE_STRING, "version", NULL,
109 		       NULL, 0, linux_version, sizeof(linux_version),
110 		       LINUX_KERN_VERSION, CTL_EOL);
111 
112 	linux_sysctl_root.sysctl_flags &= ~CTLFLAG_READWRITE;
113 }
114 
115 /*
116  * linux sysctl system call
117  */
118 int
119 linux_sys___sysctl(struct lwp *l, void *v, register_t *retval)
120 {
121 	struct linux_sys___sysctl_args *uap = v;
122 	struct linux___sysctl ls;
123 	int error, nerror, name[CTL_MAXNAME];
124 	size_t savelen = 0, oldlen = 0;
125 
126 	/*
127 	 * get linux args structure
128 	 */
129 	if ((error = copyin(SCARG(uap, lsp), &ls, sizeof(ls))))
130 		return error;
131 
132 	/*
133 	 * get oldlen
134 	 */
135 	oldlen = 0;
136 	if (ls.oldlenp != NULL) {
137 		error = copyin(ls.oldlenp, &oldlen, sizeof(oldlen));
138 		if (error)
139 			return (error);
140 	}
141 	savelen = oldlen;
142 
143 	/*
144 	 * top-level sysctl names may or may not be non-terminal, but
145 	 * we don't care
146 	 */
147 	if (ls.nlen > CTL_MAXNAME || ls.nlen < 1)
148 		return (EINVAL);
149 	error = copyin(ls.name, &name, ls.nlen * sizeof(int));
150 	if (error)
151 		return (error);
152 
153        ktrmib(name, ls.nlen);
154 
155 	/*
156 	 * wire old so that copyout() is less likely to fail?
157 	 */
158 	error = sysctl_lock(l, ls.oldval, savelen);
159 	if (error)
160 		return (error);
161 
162 	/*
163 	 * dispatch request into linux sysctl tree
164 	 */
165 	error = sysctl_dispatch(&name[0], ls.nlen,
166 				ls.oldval, &oldlen,
167 				ls.newval, ls.newlen,
168 				&name[0], l, &linux_sysctl_root);
169 
170 	/*
171 	 * release the sysctl lock
172 	 */
173 	sysctl_unlock(l);
174 
175 	/*
176 	 * reset caller's oldlen, even if we got an error
177 	 */
178 	if (ls.oldlenp) {
179 		nerror = copyout(&oldlen, ls.oldlenp, sizeof(oldlen));
180 		if (error == 0)
181 			error = nerror;
182 	}
183 
184 	/*
185 	 * if the only problem is that we weren't given enough space,
186 	 * that's an ENOMEM error
187 	 */
188 	if (error == 0 && ls.oldval != NULL && savelen < oldlen)
189 		error = ENOMEM;
190 
191 	return (error);
192 }
193 
194 /*
195  * kernel related system variables under emul.linux in the main sysctl
196  * tree
197  */
198 SYSCTL_SETUP(sysctl_emul_linux_setup, "sysctl emul.linux subtree setup")
199 {
200 
201 	sysctl_createv(clog, 0, NULL, NULL,
202 		       CTLFLAG_PERMANENT,
203 		       CTLTYPE_NODE, "emul", NULL,
204 		       NULL, 0, NULL, 0,
205 		       CTL_EMUL, CTL_EOL);
206 	sysctl_createv(clog, 0, NULL, NULL,
207 		       CTLFLAG_PERMANENT,
208 		       CTLTYPE_NODE, "linux",
209 		       SYSCTL_DESCR("Linux emulation settings"),
210 		       NULL, 0, NULL, 0,
211 		       CTL_EMUL, EMUL_LINUX, CTL_EOL);
212 	sysctl_createv(clog, 0, NULL, NULL,
213 		       CTLFLAG_PERMANENT,
214 		       CTLTYPE_NODE, "kern",
215 		       SYSCTL_DESCR("Linux kernel emulation settings"),
216 		       NULL, 0, NULL, 0,
217 		       CTL_EMUL, EMUL_LINUX, EMUL_LINUX_KERN, CTL_EOL);
218 
219 	sysctl_createv(clog, 0, NULL, NULL,
220 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
221 		       CTLTYPE_STRING, "ostype",
222 		       SYSCTL_DESCR("Linux operating system type"),
223 		       NULL, 0, linux_sysname, sizeof(linux_sysname),
224 		       CTL_EMUL, EMUL_LINUX, EMUL_LINUX_KERN,
225 		       EMUL_LINUX_KERN_OSTYPE, CTL_EOL);
226 	sysctl_createv(clog, 0, NULL, NULL,
227 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
228 		       CTLTYPE_STRING, "osrelease",
229 		       SYSCTL_DESCR("Linux operating system release"),
230 		       NULL, 0, linux_release, sizeof(linux_release),
231 		       CTL_EMUL, EMUL_LINUX, EMUL_LINUX_KERN,
232 		       EMUL_LINUX_KERN_OSRELEASE, CTL_EOL);
233 	sysctl_createv(clog, 0, NULL, NULL,
234 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
235 		       CTLTYPE_STRING, "osversion",
236 		       SYSCTL_DESCR("Linux operating system revision"),
237 		       NULL, 0, linux_version, sizeof(linux_version),
238 		       CTL_EMUL, EMUL_LINUX, EMUL_LINUX_KERN,
239 		       EMUL_LINUX_KERN_VERSION, CTL_EOL);
240 }
241