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,
72 static int linux_set_osname(struct thread *td, char *osname);
73 static int linux_set_osrelease(struct thread *td, char *osrelease);
74 static int linux_set_oss_version(struct thread *td, int oss_version);
77 linux_sysctl_osname(SYSCTL_HANDLER_ARGS)
79 char osname[LINUX_MAX_UTSNAME];
82 linux_get_osname(req->td, osname);
83 error = sysctl_handle_string(oidp, osname, LINUX_MAX_UTSNAME, req);
84 if (error || req->newptr == NULL)
86 error = linux_set_osname(req->td, osname);
90 SYSCTL_PROC(_compat_linux, OID_AUTO, osname,
91 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_MPSAFE,
92 0, 0, linux_sysctl_osname, "A",
93 "Linux kernel OS name");
96 linux_sysctl_osrelease(SYSCTL_HANDLER_ARGS)
98 char osrelease[LINUX_MAX_UTSNAME];
101 linux_get_osrelease(req->td, osrelease);
102 error = sysctl_handle_string(oidp, osrelease, LINUX_MAX_UTSNAME, req);
103 if (error || req->newptr == NULL)
105 error = linux_set_osrelease(req->td, osrelease);
109 SYSCTL_PROC(_compat_linux, OID_AUTO, osrelease,
110 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_MPSAFE,
111 0, 0, linux_sysctl_osrelease, "A",
112 "Linux kernel OS release");
115 linux_sysctl_oss_version(SYSCTL_HANDLER_ARGS)
120 oss_version = linux_get_oss_version(req->td);
121 error = sysctl_handle_int(oidp, &oss_version, 0, req);
122 if (error || req->newptr == NULL)
124 error = linux_set_oss_version(req->td, oss_version);
128 SYSCTL_PROC(_compat_linux, OID_AUTO, oss_version,
129 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_MPSAFE,
130 0, 0, linux_sysctl_oss_version, "I",
131 "Linux OSS version");
134 * Map the osrelease into integer
137 linux_map_osrel(char *osrelease, int *osrel)
139 char *sep, *eosrelease;
140 int len, v0, v1, v2, v;
142 len = strlen(osrelease);
143 eosrelease = osrelease + len;
144 v0 = strtol(osrelease, &sep, 10);
145 if (osrelease == sep || sep + 1 >= eosrelease || *sep != '.')
148 v1 = strtol(osrelease, &sep, 10);
149 if (osrelease == sep || sep + 1 >= eosrelease || *sep != '.')
152 v2 = strtol(osrelease, &sep, 10);
153 if (osrelease == sep || sep != eosrelease)
156 v = v0 * 1000000 + v1 * 1000 + v2;
165 * Find a prison with Linux info.
166 * Return the Linux info and the (locked) prison.
168 static struct linux_prison *
169 linux_find_prison(struct prison *spr, struct prison **prp)
172 struct linux_prison *lpr;
174 if (!linux_osd_jail_slot)
175 /* In case osd_register failed. */
177 for (pr = spr;; pr = pr->pr_parent) {
178 mtx_lock(&pr->pr_mtx);
179 lpr = (pr == &prison0)
181 : osd_jail_get(pr, linux_osd_jail_slot);
184 mtx_unlock(&pr->pr_mtx);
191 * Ensure a prison has its own Linux info. If lprp is non-null, point it to
192 * the Linux info and lock the prison.
195 linux_alloc_prison(struct prison *pr, struct linux_prison **lprp)
198 struct linux_prison *lpr, *nlpr;
201 /* If this prison already has Linux info, return that. */
203 lpr = linux_find_prison(pr, &ppr);
207 * Allocate a new info record. Then check again, in case something
208 * changed during the allocation.
210 mtx_unlock(&ppr->pr_mtx);
211 nlpr = malloc(sizeof(struct linux_prison), M_PRISON, M_WAITOK);
212 lpr = linux_find_prison(pr, &ppr);
214 free(nlpr, M_PRISON);
217 /* Inherit the initial values from the ancestor. */
218 mtx_lock(&pr->pr_mtx);
219 error = osd_jail_set(pr, linux_osd_jail_slot, nlpr);
221 bcopy(lpr, nlpr, sizeof(*lpr));
224 free(nlpr, M_PRISON);
227 mtx_unlock(&ppr->pr_mtx);
232 mtx_unlock(&pr->pr_mtx);
237 * Jail OSD methods for Linux prison data.
240 linux_prison_create(void *obj, void *data)
242 struct prison *pr = obj;
243 struct vfsoptlist *opts = data;
246 if (vfs_copyopt(opts, "linux", &jsys, sizeof(jsys)) == 0 &&
247 jsys == JAIL_SYS_INHERIT)
250 * Inherit a prison's initial values from its parent
251 * (different from JAIL_SYS_INHERIT which also inherits changes).
253 return linux_alloc_prison(pr, NULL);
257 linux_prison_check(void *obj __unused, void *data)
259 struct vfsoptlist *opts = data;
260 char *osname, *osrelease;
261 int error, jsys, len, osrel, oss_version;
263 /* Check that the parameters are correct. */
264 error = vfs_copyopt(opts, "linux", &jsys, sizeof(jsys));
265 if (error != ENOENT) {
268 if (jsys != JAIL_SYS_NEW && jsys != JAIL_SYS_INHERIT)
271 error = vfs_getopt(opts, "linux.osname", (void **)&osname, &len);
272 if (error != ENOENT) {
275 if (len == 0 || osname[len - 1] != '\0')
277 if (len > LINUX_MAX_UTSNAME) {
278 vfs_opterror(opts, "linux.osname too long");
279 return (ENAMETOOLONG);
282 error = vfs_getopt(opts, "linux.osrelease", (void **)&osrelease, &len);
283 if (error != ENOENT) {
286 if (len == 0 || osrelease[len - 1] != '\0')
288 if (len > LINUX_MAX_UTSNAME) {
289 vfs_opterror(opts, "linux.osrelease too long");
290 return (ENAMETOOLONG);
292 error = linux_map_osrel(osrelease, &osrel);
294 vfs_opterror(opts, "linux.osrelease format error");
298 error = vfs_copyopt(opts, "linux.oss_version", &oss_version,
299 sizeof(oss_version));
300 return (error == ENOENT ? 0 : error);
304 linux_prison_set(void *obj, void *data)
306 struct linux_prison *lpr;
307 struct prison *pr = obj;
308 struct vfsoptlist *opts = data;
309 char *osname, *osrelease;
310 int error, gotversion, jsys, len, oss_version;
312 /* Set the parameters, which should be correct. */
313 error = vfs_copyopt(opts, "linux", &jsys, sizeof(jsys));
316 error = vfs_getopt(opts, "linux.osname", (void **)&osname, &len);
321 error = vfs_getopt(opts, "linux.osrelease", (void **)&osrelease, &len);
326 error = vfs_copyopt(opts, "linux.oss_version", &oss_version,
327 sizeof(oss_version));
335 case JAIL_SYS_INHERIT:
336 /* "linux=inherit": inherit the parent's Linux info. */
337 mtx_lock(&pr->pr_mtx);
338 osd_jail_del(pr, linux_osd_jail_slot);
339 mtx_unlock(&pr->pr_mtx);
343 * "linux=new" or "linux.*":
344 * the prison gets its own Linux info.
346 error = linux_alloc_prison(pr, &lpr);
348 mtx_unlock(&pr->pr_mtx);
352 error = linux_map_osrel(osrelease, &lpr->pr_osrel);
354 mtx_unlock(&pr->pr_mtx);
357 strlcpy(lpr->pr_osrelease, osrelease,
361 strlcpy(lpr->pr_osname, osname, LINUX_MAX_UTSNAME);
363 lpr->pr_oss_version = oss_version;
364 mtx_unlock(&pr->pr_mtx);
369 SYSCTL_JAIL_PARAM_SYS_NODE(linux, CTLFLAG_RW, "Jail Linux parameters");
370 SYSCTL_JAIL_PARAM_STRING(_linux, osname, CTLFLAG_RW, LINUX_MAX_UTSNAME,
371 "Jail Linux kernel OS name");
372 SYSCTL_JAIL_PARAM_STRING(_linux, osrelease, CTLFLAG_RW, LINUX_MAX_UTSNAME,
373 "Jail Linux kernel OS release");
374 SYSCTL_JAIL_PARAM(_linux, oss_version, CTLTYPE_INT | CTLFLAG_RW,
375 "I", "Jail Linux OSS version");
378 linux_prison_get(void *obj, void *data)
380 struct linux_prison *lpr;
382 struct prison *pr = obj;
383 struct vfsoptlist *opts = data;
388 /* See if this prison is the one with the Linux info. */
389 lpr = linux_find_prison(pr, &ppr);
390 i = (ppr == pr) ? JAIL_SYS_NEW : JAIL_SYS_INHERIT;
391 error = vfs_setopt(opts, "linux", &i, sizeof(i));
392 if (error != 0 && error != ENOENT)
395 error = vfs_setopts(opts, "linux.osname", lpr->pr_osname);
396 if (error != 0 && error != ENOENT)
398 error = vfs_setopts(opts, "linux.osrelease", lpr->pr_osrelease);
399 if (error != 0 && error != ENOENT)
401 error = vfs_setopt(opts, "linux.oss_version",
402 &lpr->pr_oss_version, sizeof(lpr->pr_oss_version));
403 if (error != 0 && error != ENOENT)
407 * If this prison is inheriting its Linux info, report
408 * empty/zero parameters.
410 error = vfs_setopts(opts, "linux.osname", "");
411 if (error != 0 && error != ENOENT)
413 error = vfs_setopts(opts, "linux.osrelease", "");
414 if (error != 0 && error != ENOENT)
416 error = vfs_setopt(opts, "linux.oss_version", &version0,
417 sizeof(lpr->pr_oss_version));
418 if (error != 0 && error != ENOENT)
424 mtx_unlock(&ppr->pr_mtx);
429 linux_prison_destructor(void *data)
432 free(data, M_PRISON);
436 linux_osd_jail_register(void)
439 osd_method_t methods[PR_MAXMETHOD] = {
440 [PR_METHOD_CREATE] = linux_prison_create,
441 [PR_METHOD_GET] = linux_prison_get,
442 [PR_METHOD_SET] = linux_prison_set,
443 [PR_METHOD_CHECK] = linux_prison_check
446 linux_osd_jail_slot =
447 osd_jail_register(linux_prison_destructor, methods);
448 if (linux_osd_jail_slot > 0) {
449 /* Copy the system linux info to any current prisons. */
450 sx_xlock(&allprison_lock);
451 TAILQ_FOREACH(pr, &allprison, pr_list)
452 (void)linux_alloc_prison(pr, NULL);
453 sx_xunlock(&allprison_lock);
458 linux_osd_jail_deregister(void)
461 if (linux_osd_jail_slot)
462 osd_jail_deregister(linux_osd_jail_slot);
466 linux_get_osname(struct thread *td, char *dst)
469 struct linux_prison *lpr;
471 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
472 bcopy(lpr->pr_osname, dst, LINUX_MAX_UTSNAME);
473 mtx_unlock(&pr->pr_mtx);
477 linux_set_osname(struct thread *td, char *osname)
480 struct linux_prison *lpr;
482 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
483 strlcpy(lpr->pr_osname, osname, LINUX_MAX_UTSNAME);
484 mtx_unlock(&pr->pr_mtx);
489 linux_get_osrelease(struct thread *td, char *dst)
492 struct linux_prison *lpr;
494 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
495 bcopy(lpr->pr_osrelease, dst, LINUX_MAX_UTSNAME);
496 mtx_unlock(&pr->pr_mtx);
500 linux_kernver(struct thread *td)
503 struct linux_prison *lpr;
506 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
507 osrel = lpr->pr_osrel;
508 mtx_unlock(&pr->pr_mtx);
513 linux_set_osrelease(struct thread *td, char *osrelease)
516 struct linux_prison *lpr;
519 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
520 error = linux_map_osrel(osrelease, &lpr->pr_osrel);
522 strlcpy(lpr->pr_osrelease, osrelease, LINUX_MAX_UTSNAME);
523 mtx_unlock(&pr->pr_mtx);
528 linux_get_oss_version(struct thread *td)
531 struct linux_prison *lpr;
534 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
535 version = lpr->pr_oss_version;
536 mtx_unlock(&pr->pr_mtx);
541 linux_set_oss_version(struct thread *td, int oss_version)
544 struct linux_prison *lpr;
546 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
547 lpr->pr_oss_version = oss_version;
548 mtx_unlock(&pr->pr_mtx);
552 #if defined(DEBUG) || defined(KTR)
554 u_char linux_debug_map[howmany(LINUX_SYS_MAXSYSCALL, sizeof(u_char))];
557 linux_debug(int syscall, int toggle, int global)
561 char c = toggle ? 0 : 0xff;
563 memset(linux_debug_map, c, sizeof(linux_debug_map));
566 if (syscall < 0 || syscall >= LINUX_SYS_MAXSYSCALL)
569 clrbit(linux_debug_map, syscall);
571 setbit(linux_debug_map, syscall);
576 * Usage: sysctl linux.debug=<syscall_nr>.<0/1>
578 * E.g.: sysctl linux.debug=21.0
580 * As a special case, syscall "all" will apply to all syscalls globally.
582 #define LINUX_MAX_DEBUGSTR 16
584 linux_sysctl_debug(SYSCTL_HANDLER_ARGS)
586 char value[LINUX_MAX_DEBUGSTR], *p;
587 int error, sysc, toggle;
591 error = sysctl_handle_string(oidp, value, LINUX_MAX_DEBUGSTR, req);
592 if (error || req->newptr == NULL)
594 for (p = value; *p != '\0' && *p != '.'; p++);
598 sysc = strtol(value, NULL, 0);
599 toggle = strtol(p, NULL, 0);
600 if (strcmp(value, "all") == 0)
602 error = linux_debug(sysc, toggle, global);
606 SYSCTL_PROC(_compat_linux, OID_AUTO, debug,
607 CTLTYPE_STRING | CTLFLAG_RW,
608 0, 0, linux_sysctl_debug, "A",
609 "Linux debugging control");
611 #endif /* DEBUG || KTR */