xref: /freebsd/sys/compat/linux/linux_mib.c (revision 729362425c09cf6b362366aabc6fb547eee8035a)
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  * $FreeBSD$
29  */
30 
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/systm.h>
34 #include <sys/sysctl.h>
35 #include <sys/proc.h>
36 #include <sys/malloc.h>
37 #include <sys/jail.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 
41 #include <machine/../linux/linux.h>
42 #include <compat/linux/linux_mib.h>
43 
44 struct linux_prison {
45 	char	pr_osname[LINUX_MAX_UTSNAME];
46 	char	pr_osrelease[LINUX_MAX_UTSNAME];
47 	int	pr_oss_version;
48 };
49 
50 SYSCTL_NODE(_compat, OID_AUTO, linux, CTLFLAG_RW, 0,
51 	    "Linux mode");
52 
53 static struct mtx osname_lock;
54 MTX_SYSINIT(linux_osname, &osname_lock, "linux osname", MTX_DEF);
55 
56 static char	linux_osname[LINUX_MAX_UTSNAME] = "Linux";
57 
58 static int
59 linux_sysctl_osname(SYSCTL_HANDLER_ARGS)
60 {
61 	char osname[LINUX_MAX_UTSNAME];
62 	int error;
63 
64 	linux_get_osname(req->td, osname);
65 	error = sysctl_handle_string(oidp, osname, LINUX_MAX_UTSNAME, req);
66 	if (error || req->newptr == NULL)
67 		return (error);
68 	error = linux_set_osname(req->td, osname);
69 	return (error);
70 }
71 
72 SYSCTL_PROC(_compat_linux, OID_AUTO, osname,
73 	    CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_PRISON,
74 	    0, 0, linux_sysctl_osname, "A",
75 	    "Linux kernel OS name");
76 
77 static char	linux_osrelease[LINUX_MAX_UTSNAME] = "2.4.2";
78 
79 static int
80 linux_sysctl_osrelease(SYSCTL_HANDLER_ARGS)
81 {
82 	char osrelease[LINUX_MAX_UTSNAME];
83 	int error;
84 
85 	linux_get_osrelease(req->td, osrelease);
86 	error = sysctl_handle_string(oidp, osrelease, LINUX_MAX_UTSNAME, req);
87 	if (error || req->newptr == NULL)
88 		return (error);
89 	error = linux_set_osrelease(req->td, osrelease);
90 	return (error);
91 }
92 
93 SYSCTL_PROC(_compat_linux, OID_AUTO, osrelease,
94 	    CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_PRISON,
95 	    0, 0, linux_sysctl_osrelease, "A",
96 	    "Linux kernel OS release");
97 
98 static int	linux_oss_version = 0x030600;
99 
100 static int
101 linux_sysctl_oss_version(SYSCTL_HANDLER_ARGS)
102 {
103 	int oss_version;
104 	int error;
105 
106 	oss_version = linux_get_oss_version(req->td);
107 	error = sysctl_handle_int(oidp, &oss_version, 0, req);
108 	if (error || req->newptr == NULL)
109 		return (error);
110 	error = linux_set_oss_version(req->td, oss_version);
111 	return (error);
112 }
113 
114 SYSCTL_PROC(_compat_linux, OID_AUTO, oss_version,
115 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON,
116 	    0, 0, linux_sysctl_oss_version, "I",
117 	    "Linux OSS version");
118 
119 /*
120  * Returns holding the prison mutex if return non-NULL.
121  */
122 static struct prison *
123 linux_get_prison(struct thread *td)
124 {
125 	register struct prison *pr;
126 	register struct linux_prison *lpr;
127 
128 	KASSERT(td == curthread, ("linux_get_prison() called on !curthread"));
129 	if (!jailed(td->td_ucred))
130 		return (NULL);
131 	pr = td->td_ucred->cr_prison;
132 	mtx_lock(&pr->pr_mtx);
133 	if (pr->pr_linux == NULL) {
134 		/*
135 		 * If we don't have a linux prison structure yet, allocate
136 		 * one.  We have to handle the race where another thread
137 		 * could be adding a linux prison to this process already.
138 		 */
139 		mtx_unlock(&pr->pr_mtx);
140 		lpr = malloc(sizeof(struct linux_prison), M_PRISON,
141 		    M_WAITOK | M_ZERO);
142 		mtx_lock(&pr->pr_mtx);
143 		if (pr->pr_linux == NULL)
144 			pr->pr_linux = lpr;
145 		else
146 			free(lpr, M_PRISON);
147 	}
148 	return (pr);
149 }
150 
151 void
152 linux_mib_destroy(void)
153 {
154 
155 	mtx_destroy(&osname_lock);
156 }
157 
158 void
159 linux_get_osname(struct thread *td, char *dst)
160 {
161 	register struct prison *pr;
162 	register struct linux_prison *lpr;
163 
164 	pr = td->td_ucred->cr_prison;
165 	if (pr != NULL) {
166 		mtx_lock(&pr->pr_mtx);
167 		if (pr->pr_linux != NULL) {
168 			lpr = (struct linux_prison *)pr->pr_linux;
169 			if (lpr->pr_osname[0]) {
170 				bcopy(lpr->pr_osname, dst, LINUX_MAX_UTSNAME);
171 				mtx_unlock(&pr->pr_mtx);
172 				return;
173 			}
174 		}
175 		mtx_unlock(&pr->pr_mtx);
176 	}
177 
178 	mtx_lock(&osname_lock);
179 	bcopy(linux_osname, dst, LINUX_MAX_UTSNAME);
180 	mtx_unlock(&osname_lock);
181 }
182 
183 int
184 linux_set_osname(struct thread *td, char *osname)
185 {
186 	struct prison *pr;
187 	struct linux_prison *lpr;
188 
189 	pr = linux_get_prison(td);
190 	if (pr != NULL) {
191 		lpr = (struct linux_prison *)pr->pr_linux;
192 		strcpy(lpr->pr_osname, osname);
193 		mtx_unlock(&pr->pr_mtx);
194 	} else {
195 		mtx_lock(&osname_lock);
196 		strcpy(linux_osname, osname);
197 		mtx_unlock(&osname_lock);
198 	}
199 
200 	return (0);
201 }
202 
203 void
204 linux_get_osrelease(struct thread *td, char *dst)
205 {
206 	register struct prison *pr;
207 	struct linux_prison *lpr;
208 
209 	pr = td->td_ucred->cr_prison;
210 	if (pr != NULL) {
211 		mtx_lock(&pr->pr_mtx);
212 		if (pr->pr_linux != NULL) {
213 			lpr = (struct linux_prison *)pr->pr_linux;
214 			if (lpr->pr_osrelease[0]) {
215 				bcopy(lpr->pr_osrelease, dst,
216 				    LINUX_MAX_UTSNAME);
217 				mtx_unlock(&pr->pr_mtx);
218 				return;
219 			}
220 		}
221 		mtx_unlock(&pr->pr_mtx);
222 	}
223 
224 	mtx_lock(&osname_lock);
225 	bcopy(linux_osrelease, dst, LINUX_MAX_UTSNAME);
226 	mtx_unlock(&osname_lock);
227 }
228 
229 int
230 linux_set_osrelease(struct thread *td, char *osrelease)
231 {
232 	struct prison *pr;
233 	struct linux_prison *lpr;
234 
235 	pr = linux_get_prison(td);
236 	if (pr != NULL) {
237 		lpr = (struct linux_prison *)pr->pr_linux;
238 		strcpy(lpr->pr_osrelease, osrelease);
239 		mtx_unlock(&pr->pr_mtx);
240 	} else {
241 		mtx_lock(&osname_lock);
242 		strcpy(linux_osrelease, osrelease);
243 		mtx_unlock(&osname_lock);
244 	}
245 
246 	return (0);
247 }
248 
249 int
250 linux_get_oss_version(struct thread *td)
251 {
252 	register struct prison *pr;
253 	register struct linux_prison *lpr;
254 	int version;
255 
256 	pr = td->td_ucred->cr_prison;
257 	if (pr != NULL) {
258 		mtx_lock(&pr->pr_mtx);
259 		if (pr->pr_linux != NULL) {
260 			lpr = (struct linux_prison *)pr->pr_linux;
261 			if (lpr->pr_oss_version) {
262 				version = lpr->pr_oss_version;
263 				mtx_unlock(&pr->pr_mtx);
264 				return (version);
265 			}
266 		}
267 		mtx_unlock(&pr->pr_mtx);
268 	}
269 
270 	mtx_lock(&osname_lock);
271 	version = linux_oss_version;
272 	mtx_unlock(&osname_lock);
273 	return (version);
274 }
275 
276 int
277 linux_set_oss_version(struct thread *td, int oss_version)
278 {
279 	struct prison *pr;
280 	struct linux_prison *lpr;
281 
282 	pr = linux_get_prison(td);
283 	if (pr != NULL) {
284 		lpr = (struct linux_prison *)pr->pr_linux;
285 		lpr->pr_oss_version = oss_version;
286 		mtx_unlock(&pr->pr_mtx);
287 	} else {
288 		mtx_lock(&osname_lock);
289 		linux_oss_version = oss_version;
290 		mtx_unlock(&osname_lock);
291 	}
292 
293 	return (0);
294 }
295 
296 #ifdef DEBUG
297 
298 u_char linux_debug_map[howmany(LINUX_SYS_MAXSYSCALL, sizeof(u_char))];
299 
300 static int
301 linux_debug(int syscall, int toggle, int global)
302 {
303 
304 	if (global) {
305 		char c = toggle ? 0 : 0xff;
306 
307 		memset(linux_debug_map, c, sizeof(linux_debug_map));
308 		return (0);
309 	}
310 	if (syscall < 0 || syscall >= LINUX_SYS_MAXSYSCALL)
311 		return (EINVAL);
312 	if (toggle)
313 		clrbit(linux_debug_map, syscall);
314 	else
315 		setbit(linux_debug_map, syscall);
316 	return (0);
317 }
318 
319 /*
320  * Usage: sysctl linux.debug=<syscall_nr>.<0/1>
321  *
322  *    E.g.: sysctl linux.debug=21.0
323  *
324  * As a special case, syscall "all" will apply to all syscalls globally.
325  */
326 #define LINUX_MAX_DEBUGSTR	16
327 static int
328 linux_sysctl_debug(SYSCTL_HANDLER_ARGS)
329 {
330 	char value[LINUX_MAX_DEBUGSTR], *p;
331 	int error, sysc, toggle;
332 	int global = 0;
333 
334 	value[0] = '\0';
335 	error = sysctl_handle_string(oidp, value, LINUX_MAX_DEBUGSTR, req);
336 	if (error || req->newptr == NULL)
337 		return (error);
338 	for (p = value; *p != '\0' && *p != '.'; p++);
339 	if (*p == '\0')
340 		return (EINVAL);
341 	*p++ = '\0';
342 	sysc = strtol(value, NULL, 0);
343 	toggle = strtol(p, NULL, 0);
344 	if (strcmp(value, "all") == 0)
345 		global = 1;
346 	error = linux_debug(sysc, toggle, global);
347 	return (error);
348 }
349 
350 SYSCTL_PROC(_compat_linux, OID_AUTO, debug,
351             CTLTYPE_STRING | CTLFLAG_RW,
352             0, 0, linux_sysctl_debug, "A",
353             "Linux debugging control");
354 
355 #endif /* DEBUG */
356