2 * Copyright (c) 1994-1995 Søren Schmidt
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 withough 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.
28 * $FreeBSD: src/sys/compat/linux/linux_misc.c,v 1.85.2.9 2002/09/24 08:11:41 mdodd Exp $
29 * $DragonFly: src/sys/emulation/linux/linux_misc.c,v 1.29 2006/08/12 00:26:19 dillon Exp $
32 #include "opt_compat.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/fcntl.h>
37 #include <sys/imgact_aout.h>
38 #include <sys/kernel.h>
39 #include <sys/kern_syscall.h>
42 #include <sys/mount.h>
45 #include <sys/nlookup.h>
46 #include <sys/blist.h>
47 #include <sys/reboot.h>
48 #include <sys/resourcevar.h>
49 #include <sys/signalvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/sysproto.h>
54 #include <sys/unistd.h>
55 #include <sys/vmmeter.h>
56 #include <sys/vnode.h>
58 #include <sys/thread2.h>
62 #include <vm/vm_kern.h>
63 #include <vm/vm_map.h>
64 #include <vm/vm_extern.h>
65 #include <vm/vm_object.h>
66 #include <vm/vm_zone.h>
67 #include <vm/swap_pager.h>
69 #include <machine/frame.h>
70 #include <machine/limits.h>
71 #include <machine/psl.h>
72 #include <machine/sysarch.h>
74 #include <machine/segments.h>
77 #include <emulation/posix4/sched.h>
79 #include <emulation/linux/linux_sysproto.h>
80 #include <arch_linux/linux.h>
81 #include <arch_linux/linux_proto.h>
82 #include "linux_mib.h"
83 #include "linux_util.h"
85 #define BSD_TO_LINUX_SIGNAL(sig) \
86 (((sig) <= LINUX_SIGTBLSZ) ? bsd_to_linux_signal[_SIG_IDX(sig)] : sig)
88 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
89 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
90 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
95 l_long uptime; /* Seconds since boot */
96 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */
97 l_ulong totalram; /* Total usable main memory size */
98 l_ulong freeram; /* Available memory size */
99 l_ulong sharedram; /* Amount of shared memory */
100 l_ulong bufferram; /* Memory used by buffers */
101 l_ulong totalswap; /* Total swap space size */
102 l_ulong freeswap; /* swap space still available */
103 l_ushort procs; /* Number of current processes */
104 char _f[22]; /* Pads structure to 64 bytes */
108 sys_linux_sysinfo(struct linux_sysinfo_args *args)
110 struct l_sysinfo sysinfo;
115 /* Uptime is copied out of print_uptime() in kern_shutdown.c */
118 if (ts.tv_sec >= 86400) {
122 if (i || ts.tv_sec >= 3600) {
126 if (i || ts.tv_sec >= 60) {
130 sysinfo.uptime=ts.tv_sec;
132 /* Use the information from the mib to get our load averages */
133 for (i = 0; i < 3; i++)
134 sysinfo.loads[i] = averunnable.ldavg[i];
136 sysinfo.totalram = physmem * PAGE_SIZE;
137 sysinfo.freeram = sysinfo.totalram - vmstats.v_wire_count * PAGE_SIZE;
139 sysinfo.sharedram = 0;
140 for (object = TAILQ_FIRST(&vm_object_list); object != NULL;
141 object = TAILQ_NEXT(object, object_list))
142 if (object->shadow_count > 1)
143 sysinfo.sharedram += object->resident_page_count;
145 sysinfo.sharedram *= PAGE_SIZE;
146 sysinfo.bufferram = 0;
148 if (swapblist == NULL) {
149 sysinfo.totalswap= 0;
150 sysinfo.freeswap = 0;
152 sysinfo.totalswap = swapblist->bl_blocks * 1024;
153 sysinfo.freeswap = swapblist->bl_root->u.bmu_avail * PAGE_SIZE;
156 sysinfo.procs = 20; /* Hack */
158 return copyout(&sysinfo, (caddr_t)args->info, sizeof(sysinfo));
162 sys_linux_alarm(struct linux_alarm_args *args)
164 struct thread *td = curthread;
165 struct proc *p = td->td_proc;
166 struct itimerval it, old_it;
173 printf(ARGS(alarm, "%u"), args->secs);
176 if (args->secs > 100000000)
179 it.it_value.tv_sec = (long)args->secs;
180 it.it_value.tv_usec = 0;
181 it.it_interval.tv_sec = 0;
182 it.it_interval.tv_usec = 0;
184 old_it = p->p_realtimer;
186 if (timevalisset(&old_it.it_value))
187 callout_stop(&p->p_ithandle);
188 if (it.it_value.tv_sec != 0) {
189 callout_reset(&p->p_ithandle, tvtohz_high(&it.it_value),
191 timevaladd(&it.it_value, &tv);
195 if (timevalcmp(&old_it.it_value, &tv, >)) {
196 timevalsub(&old_it.it_value, &tv);
197 if (old_it.it_value.tv_usec != 0)
198 old_it.it_value.tv_sec++;
199 args->sysmsg_result = old_it.it_value.tv_sec;
205 sys_linux_brk(struct linux_brk_args *args)
207 struct thread *td = curthread;
208 struct proc *p = td->td_proc;
210 vm_offset_t new, old;
211 struct obreak_args bsd_args;
217 printf(ARGS(brk, "%p"), (void *)args->dsend);
219 old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
220 new = (vm_offset_t)args->dsend;
221 bsd_args.sysmsg_result = 0;
222 bsd_args.nsize = (char *) new;
223 bsd_args.sysmsg_result = 0;
224 if (((caddr_t)new > vm->vm_daddr) && !sys_obreak(&bsd_args))
225 args->sysmsg_result = (long)new;
227 args->sysmsg_result = (long)old;
233 sys_linux_uselib(struct linux_uselib_args *args)
235 struct thread *td = curthread;
237 struct nlookupdata nd;
242 unsigned long file_offset;
244 unsigned long bss_size;
249 KKASSERT(td->td_proc);
252 error = linux_copyin_path(args->library, &path, LINUX_PATH_EXISTS);
257 printf(ARGS(uselib, "%s"), path);
264 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
266 error = nlookup(&nd);
268 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
272 * From here on down, we have a locked vnode that must be unlocked.
277 if (vp->v_writecount) {
283 error = VOP_GETATTR(vp, &attr);
287 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
288 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
294 if (attr.va_size == 0) {
299 /* Can we access it? */
300 error = VOP_ACCESS(vp, VEXEC, p->p_ucred);
304 error = VOP_OPEN(vp, FREAD, p->p_ucred, NULL);
309 * Lock no longer needed
314 /* Pull in executable header into kernel_map */
315 error = vm_mmap(kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE,
316 VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp, 0);
320 /* Is it a Linux binary ? */
321 if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
327 * While we are here, we should REALLY do some more checks
330 /* Set file/virtual offset based on a.out variant. */
331 switch ((int)(a_out->a_magic & 0xffff)) {
332 case 0413: /* ZMAGIC */
335 case 0314: /* QMAGIC */
343 bss_size = round_page(a_out->a_bss);
345 /* Check various fields in header for validity/bounds. */
346 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
351 /* text + data can't exceed file size */
352 if (a_out->a_data + a_out->a_text > attr.va_size) {
358 * text/data/bss must not exceed limits
359 * XXX - this is not complete. it should check current usage PLUS
360 * the resources needed by this library.
362 if (a_out->a_text > maxtsiz ||
363 a_out->a_data + bss_size > p->p_rlimit[RLIMIT_DATA].rlim_cur) {
368 /* prevent more writers */
372 * Check if file_offset page aligned. Currently we cannot handle
373 * misalinged file offsets, and so we read in the entire image
376 if (file_offset & PAGE_MASK) {
378 printf("uselib: Non page aligned binary %lu\n", file_offset);
380 /* Map text+data read/write/execute */
382 /* a_entry is the load address and is page aligned */
383 vmaddr = trunc_page(a_out->a_entry);
385 /* get anon user mapping, read+write+execute */
386 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0,
387 &vmaddr, a_out->a_text + a_out->a_data, FALSE, VM_PROT_ALL,
392 /* map file into kernel_map */
393 error = vm_mmap(kernel_map, &buffer,
394 round_page(a_out->a_text + a_out->a_data + file_offset),
395 VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp,
396 trunc_page(file_offset));
400 /* copy from kernel VM space to user space */
401 error = copyout((caddr_t)(uintptr_t)(buffer + file_offset),
402 (caddr_t)vmaddr, a_out->a_text + a_out->a_data);
404 /* release temporary kernel space */
405 vm_map_remove(kernel_map, buffer, buffer +
406 round_page(a_out->a_text + a_out->a_data + file_offset));
412 printf("uselib: Page aligned binary %lu\n", file_offset);
415 * for QMAGIC, a_entry is 20 bytes beyond the load address
416 * to skip the executable header
418 vmaddr = trunc_page(a_out->a_entry);
421 * Map it all into the process's space as a single
422 * copy-on-write "data" segment.
424 error = vm_mmap(&p->p_vmspace->vm_map, &vmaddr,
425 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
426 MAP_PRIVATE | MAP_FIXED, (caddr_t)vp, file_offset);
431 printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long*)vmaddr)[0],
435 /* Calculate BSS start address */
436 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
439 /* allocate some 'anon' space */
440 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0,
441 &vmaddr, bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
447 /* Unlock/release vnode */
453 /* Release the kernel mapping. */
455 vm_map_remove(kernel_map, (vm_offset_t)a_out,
456 (vm_offset_t)a_out + PAGE_SIZE);
459 linux_free_path(&path);
464 sys_linux_select(struct linux_select_args *args)
466 struct select_args bsa;
467 struct timeval tv0, tv1, utv, *tvp;
473 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
474 (void *)args->readfds, (void *)args->writefds,
475 (void *)args->exceptfds, (void *)args->timeout);
479 bsa.sysmsg_result = 0;
481 bsa.in = args->readfds;
482 bsa.ou = args->writefds;
483 bsa.ex = args->exceptfds;
484 bsa.tv = (struct timeval *)args->timeout;
487 * Store current time for computation of the amount of
491 if ((error = copyin((caddr_t)args->timeout, &utv,
496 printf(LMSG("incoming timeout (%ld/%ld)"),
497 utv.tv_sec, utv.tv_usec);
500 if (itimerfix(&utv)) {
502 * The timeval was invalid. Convert it to something
503 * valid that will act as it does under Linux.
505 sg = stackgap_init();
506 tvp = stackgap_alloc(&sg, sizeof(utv));
507 utv.tv_sec += utv.tv_usec / 1000000;
508 utv.tv_usec %= 1000000;
509 if (utv.tv_usec < 0) {
511 utv.tv_usec += 1000000;
515 if ((error = copyout(&utv, tvp, sizeof(utv))))
522 error = sys_select(&bsa);
523 args->sysmsg_result = bsa.sysmsg_result;
526 printf(LMSG("real select returns %d"), error);
530 * See fs/select.c in the Linux kernel. Without this,
531 * Maelstrom doesn't work.
533 if (error == ERESTART)
539 if (args->sysmsg_result) {
541 * Compute how much time was left of the timeout,
542 * by subtracting the current time and the time
543 * before we started the call, and subtracting
544 * that result from the user-supplied value.
547 timevalsub(&tv1, &tv0);
548 timevalsub(&utv, &tv1);
555 printf(LMSG("outgoing timeout (%ld/%ld)"),
556 utv.tv_sec, utv.tv_usec);
558 if ((error = copyout(&utv, (caddr_t)args->timeout,
566 printf(LMSG("select_out -> %d"), error);
572 sys_linux_mremap(struct linux_mremap_args *args)
574 struct munmap_args bsd_args;
579 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
581 (unsigned long)args->old_len,
582 (unsigned long)args->new_len,
583 (unsigned long)args->flags);
585 args->new_len = round_page(args->new_len);
586 args->old_len = round_page(args->old_len);
588 if (args->new_len > args->old_len) {
589 args->sysmsg_result = 0;
593 if (args->new_len < args->old_len) {
594 bsd_args.sysmsg_result = 0;
595 bsd_args.addr = (caddr_t)(args->addr + args->new_len);
596 bsd_args.len = args->old_len - args->new_len;
597 error = sys_munmap(&bsd_args);
600 args->sysmsg_resultp = error ? NULL : (void *)args->addr;
604 #define LINUX_MS_ASYNC 0x0001
605 #define LINUX_MS_INVALIDATE 0x0002
606 #define LINUX_MS_SYNC 0x0004
609 sys_linux_msync(struct linux_msync_args *args)
611 struct msync_args bsd_args;
614 bsd_args.addr = (caddr_t)args->addr;
615 bsd_args.len = args->len;
616 bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
617 bsd_args.sysmsg_result = 0;
619 error = sys_msync(&bsd_args);
620 args->sysmsg_result = bsd_args.sysmsg_result;
625 sys_linux_time(struct linux_time_args *args)
633 printf(ARGS(time, "*"));
638 if (args->tm && (error = copyout(&tm, (caddr_t)args->tm, sizeof(tm))))
640 args->sysmsg_lresult = tm;
644 struct l_times_argv {
651 #define CLK_TCK 100 /* Linux uses 100 */
653 #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
656 sys_linux_times(struct linux_times_args *args)
658 struct thread *td = curthread;
659 struct proc *p = td->td_proc;
661 struct l_times_argv tms;
668 printf(ARGS(times, "*"));
671 calcru(p, &ru.ru_utime, &ru.ru_stime, NULL);
673 tms.tms_utime = CONVTCK(ru.ru_utime);
674 tms.tms_stime = CONVTCK(ru.ru_stime);
676 tms.tms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
677 tms.tms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
679 if ((error = copyout(&tms, (caddr_t)args->buf, sizeof(tms))))
683 args->sysmsg_result = (int)CONVTCK(tv);
688 sys_linux_newuname(struct linux_newuname_args *args)
690 struct thread *td = curthread;
691 struct l_new_utsname utsname;
692 char *osrelease, *osname;
695 if (ldebug(newuname))
696 printf(ARGS(newuname, "*"));
699 osname = linux_get_osname(td);
700 osrelease = linux_get_osrelease(td);
702 bzero(&utsname, sizeof(utsname));
703 strncpy(utsname.sysname, osname, LINUX_MAX_UTSNAME-1);
704 strncpy(utsname.nodename, hostname, LINUX_MAX_UTSNAME-1);
705 strncpy(utsname.release, osrelease, LINUX_MAX_UTSNAME-1);
706 strncpy(utsname.version, version, LINUX_MAX_UTSNAME-1);
707 strncpy(utsname.machine, machine, LINUX_MAX_UTSNAME-1);
708 strncpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME-1);
710 return (copyout(&utsname, (caddr_t)args->buf, sizeof(utsname)));
713 #if defined(__i386__)
720 sys_linux_utime(struct linux_utime_args *args)
722 struct timeval tv[2];
723 struct l_utimbuf lut;
724 struct nlookupdata nd;
728 error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
733 printf(ARGS(utime, "%s, *"), path);
737 error = copyin(args->times, &lut, sizeof(lut));
740 tv[0].tv_sec = lut.l_actime;
742 tv[1].tv_sec = lut.l_modtime;
745 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
747 error = kern_utimes(&nd, args->times ? tv : NULL);
750 linux_free_path(&path);
753 #endif /* __i386__ */
755 #define __WCLONE 0x80000000
758 sys_linux_waitpid(struct linux_waitpid_args *args)
760 int error, options, status;
764 printf(ARGS(waitpid, "%d, %p, %d"),
765 args->pid, (void *)args->status, args->options);
767 options = args->options & (WNOHANG | WUNTRACED);
768 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
769 if (args->options & __WCLONE)
770 options |= WLINUXCLONE;
772 error = kern_wait(args->pid, args->status ? &status : NULL, options,
773 NULL, &args->sysmsg_result);
775 if (error == 0 && args->status) {
777 if (WIFSIGNALED(status))
778 status = (status & 0xffffff80) |
779 BSD_TO_LINUX_SIGNAL(WTERMSIG(status));
780 else if (WIFSTOPPED(status))
781 status = (status & 0xffff00ff) |
782 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(status)) << 8);
783 error = copyout(&status, args->status, sizeof(status));
790 sys_linux_wait4(struct linux_wait4_args *args)
792 struct thread *td = curthread;
793 struct proc *p = td->td_proc;
794 struct rusage rusage;
795 int error, options, status;
801 printf(ARGS(wait4, "%d, %p, %d, %p"),
802 args->pid, (void *)args->status, args->options,
803 (void *)args->rusage);
805 options = args->options & (WNOHANG | WUNTRACED);
806 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
807 if (args->options & __WCLONE)
808 options |= WLINUXCLONE;
810 error = kern_wait(args->pid, args->status ? &status : NULL, options,
811 args->rusage ? &rusage : NULL, &args->sysmsg_result);
814 SIGDELSET(p->p_siglist, SIGCHLD);
816 if (error == 0 && args->status) {
818 if (WIFSIGNALED(status))
819 status = (status & 0xffffff80) |
820 BSD_TO_LINUX_SIGNAL(WTERMSIG(status));
821 else if (WIFSTOPPED(status))
822 status = (status & 0xffff00ff) |
823 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(status)) << 8);
824 error = copyout(&status, args->status, sizeof(status));
826 if (error == 0 && args->rusage)
827 error = copyout(&rusage, args->rusage, sizeof(rusage));
833 sys_linux_mknod(struct linux_mknod_args *args)
835 struct nlookupdata nd;
839 error = linux_copyin_path(args->path, &path, LINUX_PATH_CREATE);
844 printf(ARGS(mknod, "%s, %d, %d"),
845 path, args->mode, args->dev);
847 error = nlookup_init(&nd, path, UIO_SYSSPACE, 0);
849 if (args->mode & S_IFIFO) {
850 error = kern_mkfifo(&nd, args->mode);
852 error = kern_mknod(&nd, args->mode, args->dev);
857 linux_free_path(&path);
862 * UGH! This is just about the dumbest idea I've ever heard!!
865 sys_linux_personality(struct linux_personality_args *args)
868 if (ldebug(personality))
869 printf(ARGS(personality, "%d"), args->per);
874 /* Yes Jim, it's still a Linux... */
875 args->sysmsg_result = 0;
880 * Wrappers for get/setitimer for debugging..
883 sys_linux_setitimer(struct linux_setitimer_args *args)
885 struct setitimer_args bsa;
886 struct itimerval foo;
890 if (ldebug(setitimer))
891 printf(ARGS(setitimer, "%p, %p"),
892 (void *)args->itv, (void *)args->oitv);
894 bsa.which = args->which;
895 bsa.itv = (struct itimerval *)args->itv;
896 bsa.oitv = (struct itimerval *)args->oitv;
897 bsa.sysmsg_result = 0;
899 if ((error = copyin((caddr_t)args->itv, &foo, sizeof(foo))))
902 if (ldebug(setitimer)) {
903 printf("setitimer: value: sec: %ld, usec: %ld\n",
904 foo.it_value.tv_sec, foo.it_value.tv_usec);
905 printf("setitimer: interval: sec: %ld, usec: %ld\n",
906 foo.it_interval.tv_sec, foo.it_interval.tv_usec);
910 error = sys_setitimer(&bsa);
911 args->sysmsg_result = bsa.sysmsg_result;
916 sys_linux_getitimer(struct linux_getitimer_args *args)
918 struct getitimer_args bsa;
921 if (ldebug(getitimer))
922 printf(ARGS(getitimer, "%p"), (void *)args->itv);
924 bsa.which = args->which;
925 bsa.itv = (struct itimerval *)args->itv;
926 bsa.sysmsg_result = 0;
927 error = sys_getitimer(&bsa);
928 args->sysmsg_result = bsa.sysmsg_result;
933 sys_linux_nice(struct linux_nice_args *args)
935 struct setpriority_args bsd_args;
938 bsd_args.which = PRIO_PROCESS;
939 bsd_args.who = 0; /* current process */
940 bsd_args.prio = args->inc;
941 bsd_args.sysmsg_result = 0;
942 error = sys_setpriority(&bsd_args);
943 args->sysmsg_result = bsd_args.sysmsg_result;
948 sys_linux_setgroups(struct linux_setgroups_args *args)
950 struct thread *td = curthread;
951 struct proc *p = td->td_proc;
952 struct ucred *newcred, *oldcred;
953 l_gid_t linux_gidset[NGROUPS];
959 ngrp = args->gidsetsize;
960 oldcred = p->p_ucred;
963 * cr_groups[0] holds egid. Setting the whole set from
964 * the supplied set will cause egid to be changed too.
965 * Keep cr_groups[0] unchanged to prevent that.
968 if ((error = suser_cred(oldcred, PRISON_ROOT)) != 0)
974 newcred = crdup(oldcred);
976 error = copyin((caddr_t)args->grouplist, linux_gidset,
977 ngrp * sizeof(l_gid_t));
981 newcred->cr_ngroups = ngrp + 1;
983 bsd_gidset = newcred->cr_groups;
986 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
990 newcred->cr_ngroups = 1;
994 p->p_ucred = newcred;
1000 sys_linux_getgroups(struct linux_getgroups_args *args)
1002 struct thread *td = curthread;
1003 struct proc *p = td->td_proc;
1005 l_gid_t linux_gidset[NGROUPS];
1007 int bsd_gidsetsz, ngrp, error;
1012 bsd_gidset = cred->cr_groups;
1013 bsd_gidsetsz = cred->cr_ngroups - 1;
1016 * cr_groups[0] holds egid. Returning the whole set
1017 * here will cause a duplicate. Exclude cr_groups[0]
1021 if ((ngrp = args->gidsetsize) == 0) {
1022 args->sysmsg_result = bsd_gidsetsz;
1026 if (ngrp < bsd_gidsetsz)
1030 while (ngrp < bsd_gidsetsz) {
1031 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1035 if ((error = copyout(linux_gidset, (caddr_t)args->grouplist,
1036 ngrp * sizeof(l_gid_t))))
1039 args->sysmsg_result = ngrp;
1044 sys_linux_setrlimit(struct linux_setrlimit_args *args)
1046 struct l_rlimit linux_rlim;
1052 if (ldebug(setrlimit))
1053 printf(ARGS(setrlimit, "%d, %p"),
1054 args->resource, (void *)args->rlim);
1056 if (args->resource >= LINUX_RLIM_NLIMITS)
1058 which = linux_to_bsd_resource[args->resource];
1062 error = copyin(args->rlim, &linux_rlim, sizeof(linux_rlim));
1065 rlim.rlim_cur = (rlim_t)linux_rlim.rlim_cur;
1066 rlim.rlim_max = (rlim_t)linux_rlim.rlim_max;
1068 error = kern_setrlimit(which, &rlim);
1074 sys_linux_old_getrlimit(struct linux_old_getrlimit_args *args)
1076 struct l_rlimit linux_rlim;
1082 if (ldebug(old_getrlimit))
1083 printf(ARGS(old_getrlimit, "%d, %p"),
1084 args->resource, (void *)args->rlim);
1086 if (args->resource >= LINUX_RLIM_NLIMITS)
1088 which = linux_to_bsd_resource[args->resource];
1092 error = kern_getrlimit(which, &rlim);
1095 linux_rlim.rlim_cur = (l_ulong)rlim.rlim_cur;
1096 if (linux_rlim.rlim_cur == ULONG_MAX)
1097 linux_rlim.rlim_cur = LONG_MAX;
1098 linux_rlim.rlim_max = (l_ulong)rlim.rlim_max;
1099 if (linux_rlim.rlim_max == ULONG_MAX)
1100 linux_rlim.rlim_max = LONG_MAX;
1101 error = copyout(&linux_rlim, args->rlim, sizeof(linux_rlim));
1107 sys_linux_getrlimit(struct linux_getrlimit_args *args)
1109 struct l_rlimit linux_rlim;
1115 if (ldebug(getrlimit))
1116 printf(ARGS(getrlimit, "%d, %p"),
1117 args->resource, (void *)args->rlim);
1119 if (args->resource >= LINUX_RLIM_NLIMITS)
1121 which = linux_to_bsd_resource[args->resource];
1125 error = kern_getrlimit(which, &rlim);
1128 linux_rlim.rlim_cur = (l_ulong)rlim.rlim_cur;
1129 linux_rlim.rlim_max = (l_ulong)rlim.rlim_max;
1130 error = copyout(&linux_rlim, args->rlim, sizeof(linux_rlim));
1136 sys_linux_sched_setscheduler(struct linux_sched_setscheduler_args *args)
1138 struct sched_setscheduler_args bsd;
1142 if (ldebug(sched_setscheduler))
1143 printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1144 args->pid, args->policy, (const void *)args->param);
1147 switch (args->policy) {
1148 case LINUX_SCHED_OTHER:
1149 bsd.policy = SCHED_OTHER;
1151 case LINUX_SCHED_FIFO:
1152 bsd.policy = SCHED_FIFO;
1154 case LINUX_SCHED_RR:
1155 bsd.policy = SCHED_RR;
1161 bsd.pid = args->pid;
1162 bsd.param = (struct sched_param *)args->param;
1163 bsd.sysmsg_result = 0;
1165 error = sys_sched_setscheduler(&bsd);
1166 args->sysmsg_result = bsd.sysmsg_result;
1171 sys_linux_sched_getscheduler(struct linux_sched_getscheduler_args *args)
1173 struct sched_getscheduler_args bsd;
1177 if (ldebug(sched_getscheduler))
1178 printf(ARGS(sched_getscheduler, "%d"), args->pid);
1181 bsd.sysmsg_result = 0;
1182 bsd.pid = args->pid;
1183 error = sys_sched_getscheduler(&bsd);
1184 args->sysmsg_result = bsd.sysmsg_result;
1186 switch (args->sysmsg_result) {
1188 args->sysmsg_result = LINUX_SCHED_OTHER;
1191 args->sysmsg_result = LINUX_SCHED_FIFO;
1194 args->sysmsg_result = LINUX_SCHED_RR;
1201 sys_linux_sched_get_priority_max(struct linux_sched_get_priority_max_args *args)
1203 struct sched_get_priority_max_args bsd;
1207 if (ldebug(sched_get_priority_max))
1208 printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1211 switch (args->policy) {
1212 case LINUX_SCHED_OTHER:
1213 bsd.policy = SCHED_OTHER;
1215 case LINUX_SCHED_FIFO:
1216 bsd.policy = SCHED_FIFO;
1218 case LINUX_SCHED_RR:
1219 bsd.policy = SCHED_RR;
1224 bsd.sysmsg_result = 0;
1226 error = sys_sched_get_priority_max(&bsd);
1227 args->sysmsg_result = bsd.sysmsg_result;
1232 sys_linux_sched_get_priority_min(struct linux_sched_get_priority_min_args *args)
1234 struct sched_get_priority_min_args bsd;
1238 if (ldebug(sched_get_priority_min))
1239 printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1242 switch (args->policy) {
1243 case LINUX_SCHED_OTHER:
1244 bsd.policy = SCHED_OTHER;
1246 case LINUX_SCHED_FIFO:
1247 bsd.policy = SCHED_FIFO;
1249 case LINUX_SCHED_RR:
1250 bsd.policy = SCHED_RR;
1255 bsd.sysmsg_result = 0;
1257 error = sys_sched_get_priority_min(&bsd);
1258 args->sysmsg_result = bsd.sysmsg_result;
1262 #define REBOOT_CAD_ON 0x89abcdef
1263 #define REBOOT_CAD_OFF 0
1264 #define REBOOT_HALT 0xcdef0123
1267 sys_linux_reboot(struct linux_reboot_args *args)
1269 struct reboot_args bsd_args;
1274 printf(ARGS(reboot, "0x%x"), args->cmd);
1276 if (args->cmd == REBOOT_CAD_ON || args->cmd == REBOOT_CAD_OFF)
1278 bsd_args.opt = (args->cmd == REBOOT_HALT) ? RB_HALT : 0;
1279 bsd_args.sysmsg_result = 0;
1281 error = sys_reboot(&bsd_args);
1282 args->sysmsg_result = bsd_args.sysmsg_result;
1287 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1288 * p->p_retval[1] when COMPAT_43 or COMPAT_SUNOS is defined. This
1289 * globbers registers that are assumed to be preserved. The following
1290 * lightweight syscalls fixes this. See also linux_getgid16() and
1291 * linux_getuid16() in linux_uid16.c.
1293 * linux_getpid() - MP SAFE
1294 * linux_getgid() - MP SAFE
1295 * linux_getuid() - MP SAFE
1299 sys_linux_getpid(struct linux_getpid_args *args)
1301 struct thread *td = curthread;
1302 struct proc *p = td->td_proc;
1306 args->sysmsg_result = p->p_pid;
1311 sys_linux_getgid(struct linux_getgid_args *args)
1313 struct thread *td = curthread;
1314 struct proc *p = td->td_proc;
1318 args->sysmsg_result = p->p_ucred->cr_rgid;
1323 sys_linux_getuid(struct linux_getuid_args *args)
1325 struct thread *td = curthread;
1326 struct proc *p = td->td_proc;
1330 args->sysmsg_result = p->p_ucred->cr_ruid;
1335 sys_linux_getsid(struct linux_getsid_args *args)
1337 struct getsid_args bsd;
1340 bsd.sysmsg_result = 0;
1341 bsd.pid = args->pid;
1342 error = sys_getsid(&bsd);
1343 args->sysmsg_result = bsd.sysmsg_result;
1348 linux_nosys(struct nosys_args *args)