xref: /netbsd-src/sys/compat/linux32/common/linux32_sysctl.c (revision b7ae68fde0d8ef1c03714e8bbb1ee7c6118ea93b)
1 /*	$NetBSD: linux32_sysctl.c,v 1.3 2006/09/23 22:12:00 manu Exp $ */
2 
3 /*-
4  * Copyright (c) 2006 Emmanuel Dreyfus, all rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Emmanuel Dreyfus
17  * 4. The name of the author may not be used to endorse or promote
18  *    products derived from this software without specific prior written
19  *    permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS''
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: linux32_sysctl.c,v 1.3 2006/09/23 22:12:00 manu Exp $");
35 
36 #include "opt_ktrace.h"
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/errno.h>
41 #include <sys/proc.h>
42 #include <sys/mount.h>
43 #include <sys/sysctl.h>
44 #include <sys/sa.h>
45 #include <sys/syscallargs.h>
46 #ifdef KTRACE
47 #include <sys/ktrace.h>
48 #endif
49 
50 #include <compat/netbsd32/netbsd32.h>
51 
52 #include <compat/linux/common/linux_types.h>
53 #include <compat/linux/common/linux_signal.h>
54 #include <compat/linux/common/linux_sysctl.h>
55 
56 #include <compat/linux/linux_syscallargs.h>
57 
58 #include <compat/linux32/common/linux32_types.h>
59 #include <compat/linux32/common/linux32_signal.h>
60 #include <compat/linux32/common/linux32_sysctl.h>
61 
62 #include <compat/linux32/linux32_syscallargs.h>
63 
64 char linux32_sysname[128] = "Linux";
65 char linux32_release[128] = "2.4.18";
66 char linux32_version[128] = "#0 Wed Feb 20 20:00:02 CET 2002";
67 
68 
69 SYSCTL_SETUP(sysctl_emul_linux32_setup, "sysctl emul.linux32 subtree setup")
70 {
71 
72 	sysctl_createv(clog, 0, NULL, NULL,
73 		       CTLFLAG_PERMANENT,
74 		       CTLTYPE_NODE, "emul", NULL,
75 		       NULL, 0, NULL, 0,
76 		       CTL_EMUL, CTL_EOL);
77 	sysctl_createv(clog, 0, NULL, NULL,
78 		       CTLFLAG_PERMANENT,
79 		       CTLTYPE_NODE, "linux32",
80 		       SYSCTL_DESCR("Linux 32 bit emulation settings"),
81 		       NULL, 0, NULL, 0,
82 		       CTL_EMUL, EMUL_LINUX32, CTL_EOL);
83 	sysctl_createv(clog, 0, NULL, NULL,
84 		       CTLFLAG_PERMANENT,
85 		       CTLTYPE_NODE, "kern",
86 		       SYSCTL_DESCR("Linux 32 bit kernel emulation settings"),
87 		       NULL, 0, NULL, 0,
88 		       CTL_EMUL, EMUL_LINUX32, EMUL_LINUX32_KERN, CTL_EOL);
89 
90 	sysctl_createv(clog, 0, NULL, NULL,
91 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
92 		       CTLTYPE_STRING, "ostype",
93 		       SYSCTL_DESCR("Linux 32 bit operating system type"),
94 		       NULL, 0, linux32_sysname, sizeof(linux32_sysname),
95 		       CTL_EMUL, EMUL_LINUX32, EMUL_LINUX32_KERN,
96 		       EMUL_LINUX32_KERN_OSTYPE, CTL_EOL);
97 	sysctl_createv(clog, 0, NULL, NULL,
98 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
99 		       CTLTYPE_STRING, "osrelease",
100 		       SYSCTL_DESCR("Linux 32 bit operating system release"),
101 		       NULL, 0, linux32_release, sizeof(linux32_release),
102 		       CTL_EMUL, EMUL_LINUX32, EMUL_LINUX32_KERN,
103 		       EMUL_LINUX32_KERN_OSRELEASE, CTL_EOL);
104 	sysctl_createv(clog, 0, NULL, NULL,
105 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
106 		       CTLTYPE_STRING, "osversion",
107 		       SYSCTL_DESCR("Linux 32 bit operating system revision"),
108 		       NULL, 0, linux32_version, sizeof(linux32_version),
109 		       CTL_EMUL, EMUL_LINUX32, EMUL_LINUX32_KERN,
110 		       EMUL_LINUX32_KERN_VERSION, CTL_EOL);
111 }
112 
113 #ifndef _LKM
114 static
115 #endif
116 struct sysctlnode linux32_sysctl_root = {
117 	.sysctl_flags = SYSCTL_VERSION|
118 	    CTLFLAG_ROOT|CTLTYPE_NODE|CTLFLAG_READWRITE,
119 	.sysctl_num = 0,
120 	.sysctl_name = "(linux32_root)",
121 	sysc_init_field(_sysctl_size, sizeof(struct sysctlnode)),
122 };
123 
124 SYSCTL_SETUP(linux32_sysctl_setup, "linux32 emulated sysctl subtree setup")
125 {
126 	const struct sysctlnode *node = &linux32_sysctl_root;
127 
128 	sysctl_createv(clog, 0, &node, &node,
129 		       CTLFLAG_PERMANENT,
130 		       CTLTYPE_NODE, "kern", NULL,
131 		       NULL, 0, NULL, 0,
132 		       LINUX_CTL_KERN, CTL_EOL);
133 
134 	sysctl_createv(clog, 0, &node, NULL,
135 		       CTLFLAG_PERMANENT,
136 		       CTLTYPE_STRING, "ostype", NULL,
137 		       NULL, 0, linux32_sysname, sizeof(linux32_sysname),
138 		       LINUX_KERN_OSTYPE, CTL_EOL);
139 	sysctl_createv(clog, 0, &node, NULL,
140 		       CTLFLAG_PERMANENT,
141 		       CTLTYPE_STRING, "osrelease", NULL,
142 		       NULL, 0, linux32_release, sizeof(linux32_release),
143 		       LINUX_KERN_OSRELEASE, CTL_EOL);
144 	sysctl_createv(clog, 0, &node, NULL,
145 		       CTLFLAG_PERMANENT,
146 		       CTLTYPE_STRING, "version", NULL,
147 		       NULL, 0, linux32_version, sizeof(linux32_version),
148 		       LINUX_KERN_VERSION, CTL_EOL);
149 
150 	linux32_sysctl_root.sysctl_flags &= ~CTLFLAG_READWRITE;
151 }
152 
153 int
154 linux32_sys___sysctl(l, v, retval)
155 	struct lwp *l;
156 	void *v;
157 	register_t *retval;
158 {
159 	struct linux32_sys___sysctl_args /* {
160 		syscallarg(linux32___sysctlp_t) lsp;
161 	} */ *uap = v;
162 	struct linux32_sysctl ls32;
163 	int name[CTL_MAXNAME];
164 	size_t savelen;
165 	netbsd32_size_t oldlen32;
166 	size_t oldlen;
167 	int error;
168 
169 	/*
170 	 * Read sysctl arguments
171 	 */
172 	if ((error = copyin(NETBSD32PTR64(SCARG(uap, lsp)),
173 	    &ls32, sizeof(ls32))) != 0)
174 		return error;
175 
176 	/*
177 	 * Read oldlen
178 	 */
179 	if (NETBSD32PTR64(ls32.oldlenp) != NULL) {
180 		if ((error = copyin(NETBSD32PTR64(ls32.oldlenp),
181 		    &oldlen32, sizeof(oldlen32))) != 0)
182 			return error;
183 	} else {
184 		oldlen32 = 0;
185 	}
186 
187 	savelen = (size_t)oldlen32;
188 
189 	/*
190 	 * Sanity check nlen
191 	 */
192 	if ((ls32.nlen > CTL_MAXNAME) || (ls32.nlen < 1))
193 		return EINVAL;
194 
195 	/*
196 	 * Read the sysctl name
197 	 */
198 	if ((error = copyin(NETBSD32PTR64(ls32.name), &name,
199 	   ls32.nlen * sizeof(int))) != 0)
200 		return error;
201 
202 #ifdef KTRACE
203 	if (KTRPOINT(l->l_proc, KTR_MIB))
204 		ktrmib(l, name, ls32.nlen);
205 #endif
206 
207 	if ((error = sysctl_lock(l,
208 	    NETBSD32PTR64(ls32.oldval), savelen)) != 0)
209 		return error;
210 
211 	/*
212 	 * First try linux32 tree, then linux tree
213 	 */
214 	oldlen = (size_t)oldlen32;
215 	error = sysctl_dispatch(name, ls32.nlen,
216 				NETBSD32PTR64(ls32.oldval), &oldlen,
217 				NETBSD32PTR64(ls32.newval), ls32.newlen,
218 				name, l, &linux32_sysctl_root);
219 	oldlen32 = (netbsd32_size_t)oldlen;
220 
221 	sysctl_unlock(l);
222 
223 	/*
224 	 * Check for oldlen overflow (not likely, but who knows...)
225 	 */
226 	if (oldlen != oldlen32) {
227 #ifdef DEBUG_LINUX
228 		printf("%s: oldlen32 = %d, oldlen = %ld\n",
229 		    __func__, oldlen32, oldlen);
230 #endif
231 		return EINVAL;
232 	}
233 
234 	/*
235 	 * set caller's oldlen, even if we got an error
236 	 */
237 	if (NETBSD32PTR64(ls32.oldlenp)) {
238 		int nerror;
239 
240 		nerror = copyout(&oldlen32,
241 		    NETBSD32PTR64(ls32.oldlenp), sizeof(oldlen32));
242 
243 		if (error == 0)
244 			error = nerror;
245 	}
246 
247 	/*
248 	 * oldlen was too short
249 	 */
250 	if ((error == 0) &&
251 	    (NETBSD32PTR64(ls32.oldval) != NULL) &&
252 	    (savelen < oldlen32))
253 		error = ENOMEM;
254 
255 	return error;
256 }
257