xref: /freebsd/sys/compat/linux/linux_mib.c (revision a346b0961c0939e5df18cda6627d380b57459743)
1 /*-
2  * Copyright (c) 1999 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
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. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/systm.h>
35 #include <sys/sysctl.h>
36 #include <sys/proc.h>
37 #include <sys/malloc.h>
38 #include <sys/jail.h>
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 
42 #include "opt_compat.h"
43 
44 #ifdef COMPAT_LINUX32
45 #include <machine/../linux32/linux.h>
46 #else
47 #include <machine/../linux/linux.h>
48 #endif
49 #include <compat/linux/linux_mib.h>
50 
51 struct linux_prison {
52 	char	pr_osname[LINUX_MAX_UTSNAME];
53 	char	pr_osrelease[LINUX_MAX_UTSNAME];
54 	int	pr_oss_version;
55 	int	pr_use_linux26;	/* flag to determine whether to use 2.6 emulation */
56 };
57 
58 SYSCTL_NODE(_compat, OID_AUTO, linux, CTLFLAG_RW, 0,
59 	    "Linux mode");
60 
61 static struct mtx osname_lock;
62 MTX_SYSINIT(linux_osname, &osname_lock, "linux osname", MTX_DEF);
63 
64 static char	linux_osname[LINUX_MAX_UTSNAME] = "Linux";
65 
66 static int
67 linux_sysctl_osname(SYSCTL_HANDLER_ARGS)
68 {
69 	char osname[LINUX_MAX_UTSNAME];
70 	int error;
71 
72 	linux_get_osname(req->td, osname);
73 	error = sysctl_handle_string(oidp, osname, LINUX_MAX_UTSNAME, req);
74 	if (error || req->newptr == NULL)
75 		return (error);
76 	error = linux_set_osname(req->td, osname);
77 	return (error);
78 }
79 
80 SYSCTL_PROC(_compat_linux, OID_AUTO, osname,
81 	    CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_PRISON,
82 	    0, 0, linux_sysctl_osname, "A",
83 	    "Linux kernel OS name");
84 
85 static char	linux_osrelease[LINUX_MAX_UTSNAME] = "2.4.2";
86 static int	linux_use_linux26 = 0;
87 
88 static int
89 linux_sysctl_osrelease(SYSCTL_HANDLER_ARGS)
90 {
91 	char osrelease[LINUX_MAX_UTSNAME];
92 	int error;
93 
94 	linux_get_osrelease(req->td, osrelease);
95 	error = sysctl_handle_string(oidp, osrelease, LINUX_MAX_UTSNAME, req);
96 	if (error || req->newptr == NULL)
97 		return (error);
98 	error = linux_set_osrelease(req->td, osrelease);
99 	return (error);
100 }
101 
102 SYSCTL_PROC(_compat_linux, OID_AUTO, osrelease,
103 	    CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_PRISON,
104 	    0, 0, linux_sysctl_osrelease, "A",
105 	    "Linux kernel OS release");
106 
107 static int	linux_oss_version = 0x030600;
108 
109 static int
110 linux_sysctl_oss_version(SYSCTL_HANDLER_ARGS)
111 {
112 	int oss_version;
113 	int error;
114 
115 	oss_version = linux_get_oss_version(req->td);
116 	error = sysctl_handle_int(oidp, &oss_version, 0, req);
117 	if (error || req->newptr == NULL)
118 		return (error);
119 	error = linux_set_oss_version(req->td, oss_version);
120 	return (error);
121 }
122 
123 SYSCTL_PROC(_compat_linux, OID_AUTO, oss_version,
124 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON,
125 	    0, 0, linux_sysctl_oss_version, "I",
126 	    "Linux OSS version");
127 
128 /*
129  * Returns holding the prison mutex if return non-NULL.
130  */
131 static struct prison *
132 linux_get_prison(struct thread *td)
133 {
134 	register struct prison *pr;
135 	register struct linux_prison *lpr;
136 
137 	KASSERT(td == curthread, ("linux_get_prison() called on !curthread"));
138 	if (!jailed(td->td_ucred))
139 		return (NULL);
140 	pr = td->td_ucred->cr_prison;
141 	mtx_lock(&pr->pr_mtx);
142 	if (pr->pr_linux == NULL) {
143 		/*
144 		 * If we don't have a linux prison structure yet, allocate
145 		 * one.  We have to handle the race where another thread
146 		 * could be adding a linux prison to this process already.
147 		 */
148 		mtx_unlock(&pr->pr_mtx);
149 		lpr = malloc(sizeof(struct linux_prison), M_PRISON,
150 		    M_WAITOK | M_ZERO);
151 		mtx_lock(&pr->pr_mtx);
152 		if (pr->pr_linux == NULL)
153 			pr->pr_linux = lpr;
154 		else
155 			free(lpr, M_PRISON);
156 	}
157 	return (pr);
158 }
159 
160 void
161 linux_get_osname(struct thread *td, char *dst)
162 {
163 	register struct prison *pr;
164 	register struct linux_prison *lpr;
165 
166 	pr = td->td_ucred->cr_prison;
167 	if (pr != NULL) {
168 		mtx_lock(&pr->pr_mtx);
169 		if (pr->pr_linux != NULL) {
170 			lpr = (struct linux_prison *)pr->pr_linux;
171 			if (lpr->pr_osname[0]) {
172 				bcopy(lpr->pr_osname, dst, LINUX_MAX_UTSNAME);
173 				mtx_unlock(&pr->pr_mtx);
174 				return;
175 			}
176 		}
177 		mtx_unlock(&pr->pr_mtx);
178 	}
179 
180 	mtx_lock(&osname_lock);
181 	bcopy(linux_osname, dst, LINUX_MAX_UTSNAME);
182 	mtx_unlock(&osname_lock);
183 }
184 
185 int
186 linux_set_osname(struct thread *td, char *osname)
187 {
188 	struct prison *pr;
189 	struct linux_prison *lpr;
190 
191 	pr = linux_get_prison(td);
192 	if (pr != NULL) {
193 		lpr = (struct linux_prison *)pr->pr_linux;
194 		strcpy(lpr->pr_osname, osname);
195 		mtx_unlock(&pr->pr_mtx);
196 	} else {
197 		mtx_lock(&osname_lock);
198 		strcpy(linux_osname, osname);
199 		mtx_unlock(&osname_lock);
200 	}
201 
202 	return (0);
203 }
204 
205 void
206 linux_get_osrelease(struct thread *td, char *dst)
207 {
208 	register struct prison *pr;
209 	struct linux_prison *lpr;
210 
211 	pr = td->td_ucred->cr_prison;
212 	if (pr != NULL) {
213 		mtx_lock(&pr->pr_mtx);
214 		if (pr->pr_linux != NULL) {
215 			lpr = (struct linux_prison *)pr->pr_linux;
216 			if (lpr->pr_osrelease[0]) {
217 				bcopy(lpr->pr_osrelease, dst,
218 				    LINUX_MAX_UTSNAME);
219 				mtx_unlock(&pr->pr_mtx);
220 				return;
221 			}
222 		}
223 		mtx_unlock(&pr->pr_mtx);
224 	}
225 
226 	mtx_lock(&osname_lock);
227 	bcopy(linux_osrelease, dst, LINUX_MAX_UTSNAME);
228 	mtx_unlock(&osname_lock);
229 }
230 
231 int
232 linux_use26(struct thread *td)
233 {
234 	struct prison *pr;
235 	struct linux_prison *lpr;
236 	int use26 = 0;		/* defaults to off */
237 
238 	pr = td->td_ucred->cr_prison;
239 	if (pr != NULL) {
240 		if (pr->pr_linux != NULL) {
241 			lpr = (struct linux_prison *)pr->pr_linux;
242 			use26 = lpr->pr_use_linux26;
243 		}
244 	} else
245 		use26 = linux_use_linux26;
246 
247 	return (use26);
248 }
249 
250 int
251 linux_set_osrelease(struct thread *td, char *osrelease)
252 {
253 	struct prison *pr;
254 	struct linux_prison *lpr;
255 	int use26;
256 
257 	use26 = (strlen(osrelease) >= 3 && osrelease[2] == '6');
258 
259 	pr = linux_get_prison(td);
260 	if (pr != NULL) {
261 		lpr = (struct linux_prison *)pr->pr_linux;
262 		strcpy(lpr->pr_osrelease, osrelease);
263 		lpr->pr_use_linux26 = use26;
264 		mtx_unlock(&pr->pr_mtx);
265 	} else {
266 		mtx_lock(&osname_lock);
267 		strcpy(linux_osrelease, osrelease);
268 		linux_use_linux26 = use26;
269 		mtx_unlock(&osname_lock);
270 	}
271 
272 	return (0);
273 }
274 
275 int
276 linux_get_oss_version(struct thread *td)
277 {
278 	register struct prison *pr;
279 	register struct linux_prison *lpr;
280 	int version;
281 
282 	pr = td->td_ucred->cr_prison;
283 	if (pr != NULL) {
284 		mtx_lock(&pr->pr_mtx);
285 		if (pr->pr_linux != NULL) {
286 			lpr = (struct linux_prison *)pr->pr_linux;
287 			if (lpr->pr_oss_version) {
288 				version = lpr->pr_oss_version;
289 				mtx_unlock(&pr->pr_mtx);
290 				return (version);
291 			}
292 		}
293 		mtx_unlock(&pr->pr_mtx);
294 	}
295 
296 	mtx_lock(&osname_lock);
297 	version = linux_oss_version;
298 	mtx_unlock(&osname_lock);
299 	return (version);
300 }
301 
302 int
303 linux_set_oss_version(struct thread *td, int oss_version)
304 {
305 	struct prison *pr;
306 	struct linux_prison *lpr;
307 
308 	pr = linux_get_prison(td);
309 	if (pr != NULL) {
310 		lpr = (struct linux_prison *)pr->pr_linux;
311 		lpr->pr_oss_version = oss_version;
312 		mtx_unlock(&pr->pr_mtx);
313 	} else {
314 		mtx_lock(&osname_lock);
315 		linux_oss_version = oss_version;
316 		mtx_unlock(&osname_lock);
317 	}
318 
319 	return (0);
320 }
321 
322 #ifdef DEBUG
323 
324 u_char linux_debug_map[howmany(LINUX_SYS_MAXSYSCALL, sizeof(u_char))];
325 
326 static int
327 linux_debug(int syscall, int toggle, int global)
328 {
329 
330 	if (global) {
331 		char c = toggle ? 0 : 0xff;
332 
333 		memset(linux_debug_map, c, sizeof(linux_debug_map));
334 		return (0);
335 	}
336 	if (syscall < 0 || syscall >= LINUX_SYS_MAXSYSCALL)
337 		return (EINVAL);
338 	if (toggle)
339 		clrbit(linux_debug_map, syscall);
340 	else
341 		setbit(linux_debug_map, syscall);
342 	return (0);
343 }
344 
345 /*
346  * Usage: sysctl linux.debug=<syscall_nr>.<0/1>
347  *
348  *    E.g.: sysctl linux.debug=21.0
349  *
350  * As a special case, syscall "all" will apply to all syscalls globally.
351  */
352 #define LINUX_MAX_DEBUGSTR	16
353 static int
354 linux_sysctl_debug(SYSCTL_HANDLER_ARGS)
355 {
356 	char value[LINUX_MAX_DEBUGSTR], *p;
357 	int error, sysc, toggle;
358 	int global = 0;
359 
360 	value[0] = '\0';
361 	error = sysctl_handle_string(oidp, value, LINUX_MAX_DEBUGSTR, req);
362 	if (error || req->newptr == NULL)
363 		return (error);
364 	for (p = value; *p != '\0' && *p != '.'; p++);
365 	if (*p == '\0')
366 		return (EINVAL);
367 	*p++ = '\0';
368 	sysc = strtol(value, NULL, 0);
369 	toggle = strtol(p, NULL, 0);
370 	if (strcmp(value, "all") == 0)
371 		global = 1;
372 	error = linux_debug(sysc, toggle, global);
373 	return (error);
374 }
375 
376 SYSCTL_PROC(_compat_linux, OID_AUTO, debug,
377             CTLTYPE_STRING | CTLFLAG_RW,
378             0, 0, linux_sysctl_debug, "A",
379             "Linux debugging control");
380 
381 #endif /* DEBUG */
382