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