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