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