2 * Copyright (c) 1999 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/systm.h>
35 #include <sys/sysctl.h>
37 #include <sys/malloc.h>
38 #include <sys/mount.h>
41 #include <sys/mutex.h>
44 #include "opt_compat.h"
47 #include <machine/../linux32/linux.h>
49 #include <machine/../linux/linux.h>
51 #include <compat/linux/linux_mib.h>
54 char pr_osname[LINUX_MAX_UTSNAME];
55 char pr_osrelease[LINUX_MAX_UTSNAME];
60 static struct linux_prison lprison0 = {
62 .pr_osrelease = "2.6.16",
63 .pr_oss_version = 0x030600,
67 static unsigned linux_osd_jail_slot;
69 SYSCTL_NODE(_compat, OID_AUTO, linux, CTLFLAG_RW, 0,
73 linux_sysctl_osname(SYSCTL_HANDLER_ARGS)
75 char osname[LINUX_MAX_UTSNAME];
78 linux_get_osname(req->td, osname);
79 error = sysctl_handle_string(oidp, osname, LINUX_MAX_UTSNAME, req);
80 if (error || req->newptr == NULL)
82 error = linux_set_osname(req->td, osname);
86 SYSCTL_PROC(_compat_linux, OID_AUTO, osname,
87 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_MPSAFE,
88 0, 0, linux_sysctl_osname, "A",
89 "Linux kernel OS name");
92 linux_sysctl_osrelease(SYSCTL_HANDLER_ARGS)
94 char osrelease[LINUX_MAX_UTSNAME];
97 linux_get_osrelease(req->td, osrelease);
98 error = sysctl_handle_string(oidp, osrelease, LINUX_MAX_UTSNAME, req);
99 if (error || req->newptr == NULL)
101 error = linux_set_osrelease(req->td, osrelease);
105 SYSCTL_PROC(_compat_linux, OID_AUTO, osrelease,
106 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_MPSAFE,
107 0, 0, linux_sysctl_osrelease, "A",
108 "Linux kernel OS release");
111 linux_sysctl_oss_version(SYSCTL_HANDLER_ARGS)
116 oss_version = linux_get_oss_version(req->td);
117 error = sysctl_handle_int(oidp, &oss_version, 0, req);
118 if (error || req->newptr == NULL)
120 error = linux_set_oss_version(req->td, oss_version);
124 SYSCTL_PROC(_compat_linux, OID_AUTO, oss_version,
125 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_MPSAFE,
126 0, 0, linux_sysctl_oss_version, "I",
127 "Linux OSS version");
130 * Map the osrelease into integer
133 linux_map_osrel(char *osrelease, int *osrel)
135 char *sep, *eosrelease;
136 int len, v0, v1, v2, v;
138 len = strlen(osrelease);
139 eosrelease = osrelease + len;
140 v0 = strtol(osrelease, &sep, 10);
141 if (osrelease == sep || sep + 1 >= eosrelease || *sep != '.')
144 v1 = strtol(osrelease, &sep, 10);
145 if (osrelease == sep || sep + 1 >= eosrelease || *sep != '.')
148 v2 = strtol(osrelease, &sep, 10);
149 if (osrelease == sep || sep != eosrelease)
152 v = v0 * 1000000 + v1 * 1000 + v2;
161 * Find a prison with Linux info.
162 * Return the Linux info and the (locked) prison.
164 static struct linux_prison *
165 linux_find_prison(struct prison *spr, struct prison **prp)
168 struct linux_prison *lpr;
170 if (!linux_osd_jail_slot)
171 /* In case osd_register failed. */
173 for (pr = spr;; pr = pr->pr_parent) {
174 mtx_lock(&pr->pr_mtx);
175 lpr = (pr == &prison0)
177 : osd_jail_get(pr, linux_osd_jail_slot);
180 mtx_unlock(&pr->pr_mtx);
187 * Ensure a prison has its own Linux info. If lprp is non-null, point it to
188 * the Linux info and lock the prison.
191 linux_alloc_prison(struct prison *pr, struct linux_prison **lprp)
194 struct linux_prison *lpr, *nlpr;
197 /* If this prison already has Linux info, return that. */
199 lpr = linux_find_prison(pr, &ppr);
203 * Allocate a new info record. Then check again, in case something
204 * changed during the allocation.
206 mtx_unlock(&ppr->pr_mtx);
207 nlpr = malloc(sizeof(struct linux_prison), M_PRISON, M_WAITOK);
208 lpr = linux_find_prison(pr, &ppr);
210 free(nlpr, M_PRISON);
213 /* Inherit the initial values from the ancestor. */
214 mtx_lock(&pr->pr_mtx);
215 error = osd_jail_set(pr, linux_osd_jail_slot, nlpr);
217 bcopy(lpr, nlpr, sizeof(*lpr));
220 free(nlpr, M_PRISON);
223 mtx_unlock(&ppr->pr_mtx);
228 mtx_unlock(&pr->pr_mtx);
233 * Jail OSD methods for Linux prison data.
236 linux_prison_create(void *obj, void *data)
238 struct prison *pr = obj;
239 struct vfsoptlist *opts = data;
242 if (vfs_copyopt(opts, "linux", &jsys, sizeof(jsys)) == 0 &&
243 jsys == JAIL_SYS_INHERIT)
246 * Inherit a prison's initial values from its parent
247 * (different from JAIL_SYS_INHERIT which also inherits changes).
249 return linux_alloc_prison(pr, NULL);
253 linux_prison_check(void *obj __unused, void *data)
255 struct vfsoptlist *opts = data;
256 char *osname, *osrelease;
257 int error, jsys, len, osrel, oss_version;
259 /* Check that the parameters are correct. */
260 error = vfs_copyopt(opts, "linux", &jsys, sizeof(jsys));
261 if (error != ENOENT) {
264 if (jsys != JAIL_SYS_NEW && jsys != JAIL_SYS_INHERIT)
267 error = vfs_getopt(opts, "linux.osname", (void **)&osname, &len);
268 if (error != ENOENT) {
271 if (len == 0 || osname[len - 1] != '\0')
273 if (len > LINUX_MAX_UTSNAME) {
274 vfs_opterror(opts, "linux.osname too long");
275 return (ENAMETOOLONG);
278 error = vfs_getopt(opts, "linux.osrelease", (void **)&osrelease, &len);
279 if (error != ENOENT) {
282 if (len == 0 || osrelease[len - 1] != '\0')
284 if (len > LINUX_MAX_UTSNAME) {
285 vfs_opterror(opts, "linux.osrelease too long");
286 return (ENAMETOOLONG);
288 error = linux_map_osrel(osrelease, &osrel);
290 vfs_opterror(opts, "linux.osrelease format error");
294 error = vfs_copyopt(opts, "linux.oss_version", &oss_version,
295 sizeof(oss_version));
296 return (error == ENOENT ? 0 : error);
300 linux_prison_set(void *obj, void *data)
302 struct linux_prison *lpr;
303 struct prison *pr = obj;
304 struct vfsoptlist *opts = data;
305 char *osname, *osrelease;
306 int error, gotversion, jsys, len, oss_version;
308 /* Set the parameters, which should be correct. */
309 error = vfs_copyopt(opts, "linux", &jsys, sizeof(jsys));
312 error = vfs_getopt(opts, "linux.osname", (void **)&osname, &len);
317 error = vfs_getopt(opts, "linux.osrelease", (void **)&osrelease, &len);
322 error = vfs_copyopt(opts, "linux.oss_version", &oss_version,
323 sizeof(oss_version));
331 case JAIL_SYS_INHERIT:
332 /* "linux=inherit": inherit the parent's Linux info. */
333 mtx_lock(&pr->pr_mtx);
334 osd_jail_del(pr, linux_osd_jail_slot);
335 mtx_unlock(&pr->pr_mtx);
339 * "linux=new" or "linux.*":
340 * the prison gets its own Linux info.
342 error = linux_alloc_prison(pr, &lpr);
344 mtx_unlock(&pr->pr_mtx);
348 error = linux_map_osrel(osrelease, &lpr->pr_osrel);
350 mtx_unlock(&pr->pr_mtx);
353 strlcpy(lpr->pr_osrelease, osrelease,
357 strlcpy(lpr->pr_osname, osname, LINUX_MAX_UTSNAME);
359 lpr->pr_oss_version = oss_version;
360 mtx_unlock(&pr->pr_mtx);
365 SYSCTL_JAIL_PARAM_SYS_NODE(linux, CTLFLAG_RW, "Jail Linux parameters");
366 SYSCTL_JAIL_PARAM_STRING(_linux, osname, CTLFLAG_RW, LINUX_MAX_UTSNAME,
367 "Jail Linux kernel OS name");
368 SYSCTL_JAIL_PARAM_STRING(_linux, osrelease, CTLFLAG_RW, LINUX_MAX_UTSNAME,
369 "Jail Linux kernel OS release");
370 SYSCTL_JAIL_PARAM(_linux, oss_version, CTLTYPE_INT | CTLFLAG_RW,
371 "I", "Jail Linux OSS version");
374 linux_prison_get(void *obj, void *data)
376 struct linux_prison *lpr;
378 struct prison *pr = obj;
379 struct vfsoptlist *opts = data;
384 /* See if this prison is the one with the Linux info. */
385 lpr = linux_find_prison(pr, &ppr);
386 i = (ppr == pr) ? JAIL_SYS_NEW : JAIL_SYS_INHERIT;
387 error = vfs_setopt(opts, "linux", &i, sizeof(i));
388 if (error != 0 && error != ENOENT)
391 error = vfs_setopts(opts, "linux.osname", lpr->pr_osname);
392 if (error != 0 && error != ENOENT)
394 error = vfs_setopts(opts, "linux.osrelease", lpr->pr_osrelease);
395 if (error != 0 && error != ENOENT)
397 error = vfs_setopt(opts, "linux.oss_version",
398 &lpr->pr_oss_version, sizeof(lpr->pr_oss_version));
399 if (error != 0 && error != ENOENT)
403 * If this prison is inheriting its Linux info, report
404 * empty/zero parameters.
406 error = vfs_setopts(opts, "linux.osname", "");
407 if (error != 0 && error != ENOENT)
409 error = vfs_setopts(opts, "linux.osrelease", "");
410 if (error != 0 && error != ENOENT)
412 error = vfs_setopt(opts, "linux.oss_version", &version0,
413 sizeof(lpr->pr_oss_version));
414 if (error != 0 && error != ENOENT)
420 mtx_unlock(&ppr->pr_mtx);
425 linux_prison_destructor(void *data)
428 free(data, M_PRISON);
432 linux_osd_jail_register(void)
435 osd_method_t methods[PR_MAXMETHOD] = {
436 [PR_METHOD_CREATE] = linux_prison_create,
437 [PR_METHOD_GET] = linux_prison_get,
438 [PR_METHOD_SET] = linux_prison_set,
439 [PR_METHOD_CHECK] = linux_prison_check
442 linux_osd_jail_slot =
443 osd_jail_register(linux_prison_destructor, methods);
444 if (linux_osd_jail_slot > 0) {
445 /* Copy the system linux info to any current prisons. */
446 sx_xlock(&allprison_lock);
447 TAILQ_FOREACH(pr, &allprison, pr_list)
448 (void)linux_alloc_prison(pr, NULL);
449 sx_xunlock(&allprison_lock);
454 linux_osd_jail_deregister(void)
457 if (linux_osd_jail_slot)
458 osd_jail_deregister(linux_osd_jail_slot);
462 linux_get_osname(struct thread *td, char *dst)
465 struct linux_prison *lpr;
467 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
468 bcopy(lpr->pr_osname, dst, LINUX_MAX_UTSNAME);
469 mtx_unlock(&pr->pr_mtx);
473 linux_set_osname(struct thread *td, char *osname)
476 struct linux_prison *lpr;
478 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
479 strlcpy(lpr->pr_osname, osname, LINUX_MAX_UTSNAME);
480 mtx_unlock(&pr->pr_mtx);
485 linux_get_osrelease(struct thread *td, char *dst)
488 struct linux_prison *lpr;
490 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
491 bcopy(lpr->pr_osrelease, dst, LINUX_MAX_UTSNAME);
492 mtx_unlock(&pr->pr_mtx);
496 linux_kernver(struct thread *td)
499 struct linux_prison *lpr;
502 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
503 osrel = lpr->pr_osrel;
504 mtx_unlock(&pr->pr_mtx);
509 linux_set_osrelease(struct thread *td, char *osrelease)
512 struct linux_prison *lpr;
515 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
516 error = linux_map_osrel(osrelease, &lpr->pr_osrel);
518 strlcpy(lpr->pr_osrelease, osrelease, LINUX_MAX_UTSNAME);
519 mtx_unlock(&pr->pr_mtx);
524 linux_get_oss_version(struct thread *td)
527 struct linux_prison *lpr;
530 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
531 version = lpr->pr_oss_version;
532 mtx_unlock(&pr->pr_mtx);
537 linux_set_oss_version(struct thread *td, int oss_version)
540 struct linux_prison *lpr;
542 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
543 lpr->pr_oss_version = oss_version;
544 mtx_unlock(&pr->pr_mtx);
548 #if defined(DEBUG) || defined(KTR)
550 u_char linux_debug_map[howmany(LINUX_SYS_MAXSYSCALL, sizeof(u_char))];
553 linux_debug(int syscall, int toggle, int global)
557 char c = toggle ? 0 : 0xff;
559 memset(linux_debug_map, c, sizeof(linux_debug_map));
562 if (syscall < 0 || syscall >= LINUX_SYS_MAXSYSCALL)
565 clrbit(linux_debug_map, syscall);
567 setbit(linux_debug_map, syscall);
572 * Usage: sysctl linux.debug=<syscall_nr>.<0/1>
574 * E.g.: sysctl linux.debug=21.0
576 * As a special case, syscall "all" will apply to all syscalls globally.
578 #define LINUX_MAX_DEBUGSTR 16
580 linux_sysctl_debug(SYSCTL_HANDLER_ARGS)
582 char value[LINUX_MAX_DEBUGSTR], *p;
583 int error, sysc, toggle;
587 error = sysctl_handle_string(oidp, value, LINUX_MAX_DEBUGSTR, req);
588 if (error || req->newptr == NULL)
590 for (p = value; *p != '\0' && *p != '.'; p++);
594 sysc = strtol(value, NULL, 0);
595 toggle = strtol(p, NULL, 0);
596 if (strcmp(value, "all") == 0)
598 error = linux_debug(sysc, toggle, global);
602 SYSCTL_PROC(_compat_linux, OID_AUTO, debug,
603 CTLTYPE_STRING | CTLFLAG_RW,
604 0, 0, linux_sysctl_debug, "A",
605 "Linux debugging control");
607 #endif /* DEBUG || KTR */