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.39 2007/06/26 19:31:03 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>
46 #include <sys/nlookup.h>
47 #include <sys/blist.h>
48 #include <sys/reboot.h>
49 #include <sys/resourcevar.h>
50 #include <sys/signalvar.h>
51 #include <sys/signal2.h>
53 #include <sys/sysctl.h>
54 #include <sys/sysproto.h>
56 #include <sys/unistd.h>
57 #include <sys/vmmeter.h>
58 #include <sys/vnode.h>
60 #include <sys/thread2.h>
64 #include <vm/vm_kern.h>
65 #include <vm/vm_map.h>
66 #include <vm/vm_extern.h>
67 #include <vm/vm_object.h>
68 #include <vm/vm_zone.h>
69 #include <vm/swap_pager.h>
71 #include <machine/frame.h>
72 #include <machine/limits.h>
73 #include <machine/psl.h>
74 #include <machine/sysarch.h>
76 #include <machine/segments.h>
79 #include <sys/sched.h>
81 #include <emulation/linux/linux_sysproto.h>
82 #include <arch_linux/linux.h>
83 #include <arch_linux/linux_proto.h>
84 #include "linux_mib.h"
85 #include "linux_util.h"
87 #define BSD_TO_LINUX_SIGNAL(sig) \
88 (((sig) <= LINUX_SIGTBLSZ) ? bsd_to_linux_signal[_SIG_IDX(sig)] : sig)
90 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
91 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
92 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
97 l_long uptime; /* Seconds since boot */
98 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */
99 l_ulong totalram; /* Total usable main memory size */
100 l_ulong freeram; /* Available memory size */
101 l_ulong sharedram; /* Amount of shared memory */
102 l_ulong bufferram; /* Memory used by buffers */
103 l_ulong totalswap; /* Total swap space size */
104 l_ulong freeswap; /* swap space still available */
105 l_ushort procs; /* Number of current processes */
106 char _f[22]; /* Pads structure to 64 bytes */
110 sys_linux_sysinfo(struct linux_sysinfo_args *args)
112 struct l_sysinfo sysinfo;
117 /* Uptime is copied out of print_uptime() in kern_shutdown.c */
120 if (ts.tv_sec >= 86400) {
124 if (i || ts.tv_sec >= 3600) {
128 if (i || ts.tv_sec >= 60) {
132 sysinfo.uptime=ts.tv_sec;
134 /* Use the information from the mib to get our load averages */
135 for (i = 0; i < 3; i++)
136 sysinfo.loads[i] = averunnable.ldavg[i];
138 sysinfo.totalram = Maxmem * PAGE_SIZE;
139 sysinfo.freeram = sysinfo.totalram - vmstats.v_wire_count * PAGE_SIZE;
141 sysinfo.sharedram = 0;
142 for (object = TAILQ_FIRST(&vm_object_list); object != NULL;
143 object = TAILQ_NEXT(object, object_list))
144 if (object->shadow_count > 1)
145 sysinfo.sharedram += object->resident_page_count;
147 sysinfo.sharedram *= PAGE_SIZE;
148 sysinfo.bufferram = 0;
150 if (swapblist == NULL) {
151 sysinfo.totalswap= 0;
152 sysinfo.freeswap = 0;
154 sysinfo.totalswap = swapblist->bl_blocks * 1024;
155 sysinfo.freeswap = swapblist->bl_root->u.bmu_avail * PAGE_SIZE;
158 sysinfo.procs = 20; /* Hack */
160 return copyout(&sysinfo, (caddr_t)args->info, sizeof(sysinfo));
164 sys_linux_alarm(struct linux_alarm_args *args)
166 struct thread *td = curthread;
167 struct proc *p = td->td_proc;
168 struct itimerval it, old_it;
175 kprintf(ARGS(alarm, "%u"), args->secs);
178 if (args->secs > 100000000)
181 it.it_value.tv_sec = (long)args->secs;
182 it.it_value.tv_usec = 0;
183 it.it_interval.tv_sec = 0;
184 it.it_interval.tv_usec = 0;
186 old_it = p->p_realtimer;
188 if (timevalisset(&old_it.it_value))
189 callout_stop(&p->p_ithandle);
190 if (it.it_value.tv_sec != 0) {
191 callout_reset(&p->p_ithandle, tvtohz_high(&it.it_value),
193 timevaladd(&it.it_value, &tv);
197 if (timevalcmp(&old_it.it_value, &tv, >)) {
198 timevalsub(&old_it.it_value, &tv);
199 if (old_it.it_value.tv_usec != 0)
200 old_it.it_value.tv_sec++;
201 args->sysmsg_result = old_it.it_value.tv_sec;
207 sys_linux_brk(struct linux_brk_args *args)
209 struct thread *td = curthread;
210 struct proc *p = td->td_proc;
212 vm_offset_t new, old;
213 struct obreak_args bsd_args;
219 kprintf(ARGS(brk, "%p"), (void *)args->dsend);
221 old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
222 new = (vm_offset_t)args->dsend;
223 bsd_args.sysmsg_result = 0;
224 bsd_args.nsize = (char *) new;
225 bsd_args.sysmsg_result = 0;
226 if (((caddr_t)new > vm->vm_daddr) && !sys_obreak(&bsd_args))
227 args->sysmsg_result = (long)new;
229 args->sysmsg_result = (long)old;
235 sys_linux_uselib(struct linux_uselib_args *args)
237 struct thread *td = curthread;
239 struct nlookupdata nd;
244 unsigned long file_offset;
246 unsigned long bss_size;
251 KKASSERT(td->td_proc);
254 error = linux_copyin_path(args->library, &path, LINUX_PATH_EXISTS);
259 kprintf(ARGS(uselib, "%s"), path);
266 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
268 error = nlookup(&nd);
270 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
274 * From here on down, we have a locked vnode that must be unlocked.
279 if (vp->v_writecount) {
285 error = VOP_GETATTR(vp, &attr);
289 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
290 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
296 if (attr.va_size == 0) {
301 /* Can we access it? */
302 error = VOP_ACCESS(vp, VEXEC, p->p_ucred);
306 error = VOP_OPEN(vp, FREAD, p->p_ucred, NULL);
311 * Lock no longer needed
316 /* Pull in executable header into kernel_map */
317 error = vm_mmap(&kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE,
318 VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp, 0);
322 /* Is it a Linux binary ? */
323 if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
329 * While we are here, we should REALLY do some more checks
332 /* Set file/virtual offset based on a.out variant. */
333 switch ((int)(a_out->a_magic & 0xffff)) {
334 case 0413: /* ZMAGIC */
337 case 0314: /* QMAGIC */
345 bss_size = round_page(a_out->a_bss);
347 /* Check various fields in header for validity/bounds. */
348 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
353 /* text + data can't exceed file size */
354 if (a_out->a_data + a_out->a_text > attr.va_size) {
360 * text/data/bss must not exceed limits
361 * XXX - this is not complete. it should check current usage PLUS
362 * the resources needed by this library.
364 if (a_out->a_text > maxtsiz ||
365 a_out->a_data + bss_size > p->p_rlimit[RLIMIT_DATA].rlim_cur) {
370 /* prevent more writers */
374 * Check if file_offset page aligned. Currently we cannot handle
375 * misalinged file offsets, and so we read in the entire image
378 if (file_offset & PAGE_MASK) {
380 kprintf("uselib: Non page aligned binary %lu\n", file_offset);
382 /* Map text+data read/write/execute */
384 /* a_entry is the load address and is page aligned */
385 vmaddr = trunc_page(a_out->a_entry);
387 /* get anon user mapping, read+write+execute */
388 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0,
389 &vmaddr, a_out->a_text + a_out->a_data,
392 VM_PROT_ALL, VM_PROT_ALL,
397 /* map file into kernel_map */
398 error = vm_mmap(&kernel_map, &buffer,
399 round_page(a_out->a_text + a_out->a_data + file_offset),
400 VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp,
401 trunc_page(file_offset));
405 /* copy from kernel VM space to user space */
406 error = copyout((caddr_t)(uintptr_t)(buffer + file_offset),
407 (caddr_t)vmaddr, a_out->a_text + a_out->a_data);
409 /* release temporary kernel space */
410 vm_map_remove(&kernel_map, buffer, buffer +
411 round_page(a_out->a_text + a_out->a_data + file_offset));
417 kprintf("uselib: Page aligned binary %lu\n", file_offset);
420 * for QMAGIC, a_entry is 20 bytes beyond the load address
421 * to skip the executable header
423 vmaddr = trunc_page(a_out->a_entry);
426 * Map it all into the process's space as a single
427 * copy-on-write "data" segment.
429 error = vm_mmap(&p->p_vmspace->vm_map, &vmaddr,
430 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
431 MAP_PRIVATE | MAP_FIXED, (caddr_t)vp, file_offset);
436 kprintf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long*)vmaddr)[0],
440 /* Calculate BSS start address */
441 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
444 /* allocate some 'anon' space */
445 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0,
449 VM_PROT_ALL, VM_PROT_ALL,
456 /* Unlock/release vnode */
462 /* Release the kernel mapping. */
464 vm_map_remove(&kernel_map, (vm_offset_t)a_out,
465 (vm_offset_t)a_out + PAGE_SIZE);
468 linux_free_path(&path);
473 sys_linux_select(struct linux_select_args *args)
475 struct select_args bsa;
476 struct timeval tv0, tv1, utv, *tvp;
482 kprintf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
483 (void *)args->readfds, (void *)args->writefds,
484 (void *)args->exceptfds, (void *)args->timeout);
488 bsa.sysmsg_result = 0;
490 bsa.in = args->readfds;
491 bsa.ou = args->writefds;
492 bsa.ex = args->exceptfds;
493 bsa.tv = (struct timeval *)args->timeout;
496 * Store current time for computation of the amount of
500 if ((error = copyin((caddr_t)args->timeout, &utv,
505 kprintf(LMSG("incoming timeout (%ld/%ld)"),
506 utv.tv_sec, utv.tv_usec);
509 if (itimerfix(&utv)) {
511 * The timeval was invalid. Convert it to something
512 * valid that will act as it does under Linux.
514 sg = stackgap_init();
515 tvp = stackgap_alloc(&sg, sizeof(utv));
516 utv.tv_sec += utv.tv_usec / 1000000;
517 utv.tv_usec %= 1000000;
518 if (utv.tv_usec < 0) {
520 utv.tv_usec += 1000000;
524 if ((error = copyout(&utv, tvp, sizeof(utv))))
531 error = sys_select(&bsa);
532 args->sysmsg_result = bsa.sysmsg_result;
535 kprintf(LMSG("real select returns %d"), error);
539 * See fs/select.c in the Linux kernel. Without this,
540 * Maelstrom doesn't work.
542 if (error == ERESTART)
548 if (args->sysmsg_result) {
550 * Compute how much time was left of the timeout,
551 * by subtracting the current time and the time
552 * before we started the call, and subtracting
553 * that result from the user-supplied value.
556 timevalsub(&tv1, &tv0);
557 timevalsub(&utv, &tv1);
564 kprintf(LMSG("outgoing timeout (%ld/%ld)"),
565 utv.tv_sec, utv.tv_usec);
567 if ((error = copyout(&utv, (caddr_t)args->timeout,
575 kprintf(LMSG("select_out -> %d"), error);
581 sys_linux_mremap(struct linux_mremap_args *args)
583 struct munmap_args bsd_args;
588 kprintf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
590 (unsigned long)args->old_len,
591 (unsigned long)args->new_len,
592 (unsigned long)args->flags);
594 args->new_len = round_page(args->new_len);
595 args->old_len = round_page(args->old_len);
597 if (args->new_len > args->old_len) {
598 args->sysmsg_result = 0;
602 if (args->new_len < args->old_len) {
603 bsd_args.sysmsg_result = 0;
604 bsd_args.addr = (caddr_t)(args->addr + args->new_len);
605 bsd_args.len = args->old_len - args->new_len;
606 error = sys_munmap(&bsd_args);
609 args->sysmsg_resultp = error ? NULL : (void *)args->addr;
613 #define LINUX_MS_ASYNC 0x0001
614 #define LINUX_MS_INVALIDATE 0x0002
615 #define LINUX_MS_SYNC 0x0004
618 sys_linux_msync(struct linux_msync_args *args)
620 struct msync_args bsd_args;
623 bsd_args.addr = (caddr_t)args->addr;
624 bsd_args.len = args->len;
625 bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
626 bsd_args.sysmsg_result = 0;
628 error = sys_msync(&bsd_args);
629 args->sysmsg_result = bsd_args.sysmsg_result;
634 sys_linux_time(struct linux_time_args *args)
642 kprintf(ARGS(time, "*"));
647 if (args->tm && (error = copyout(&tm, (caddr_t)args->tm, sizeof(tm))))
649 args->sysmsg_lresult = tm;
653 struct l_times_argv {
660 #define CLK_TCK 100 /* Linux uses 100 */
662 #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
665 sys_linux_times(struct linux_times_args *args)
667 struct thread *td = curthread;
668 struct proc *p = td->td_proc;
670 struct l_times_argv tms;
677 kprintf(ARGS(times, "*"));
682 tms.tms_utime = CONVTCK(ru.ru_utime);
683 tms.tms_stime = CONVTCK(ru.ru_stime);
685 tms.tms_cutime = CONVTCK(p->p_cru.ru_utime);
686 tms.tms_cstime = CONVTCK(p->p_cru.ru_stime);
688 if ((error = copyout(&tms, (caddr_t)args->buf, sizeof(tms))))
692 args->sysmsg_result = (int)CONVTCK(tv);
697 sys_linux_newuname(struct linux_newuname_args *args)
699 struct thread *td = curthread;
700 struct l_new_utsname utsname;
701 char *osrelease, *osname;
704 if (ldebug(newuname))
705 kprintf(ARGS(newuname, "*"));
708 osname = linux_get_osname(td);
709 osrelease = linux_get_osrelease(td);
711 bzero(&utsname, sizeof(utsname));
712 strncpy(utsname.sysname, osname, LINUX_MAX_UTSNAME-1);
713 strncpy(utsname.nodename, hostname, LINUX_MAX_UTSNAME-1);
714 strncpy(utsname.release, osrelease, LINUX_MAX_UTSNAME-1);
715 strncpy(utsname.version, version, LINUX_MAX_UTSNAME-1);
716 strncpy(utsname.machine, machine, LINUX_MAX_UTSNAME-1);
717 strncpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME-1);
719 return (copyout(&utsname, (caddr_t)args->buf, sizeof(utsname)));
722 #if defined(__i386__)
729 sys_linux_utime(struct linux_utime_args *args)
731 struct timeval tv[2];
732 struct l_utimbuf lut;
733 struct nlookupdata nd;
737 error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
742 kprintf(ARGS(utime, "%s, *"), path);
746 error = copyin(args->times, &lut, sizeof(lut));
749 tv[0].tv_sec = lut.l_actime;
751 tv[1].tv_sec = lut.l_modtime;
754 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
756 error = kern_utimes(&nd, args->times ? tv : NULL);
759 linux_free_path(&path);
762 #endif /* __i386__ */
764 #define __WCLONE 0x80000000
767 sys_linux_waitpid(struct linux_waitpid_args *args)
769 int error, options, status;
773 kprintf(ARGS(waitpid, "%d, %p, %d"),
774 args->pid, (void *)args->status, args->options);
776 options = args->options & (WNOHANG | WUNTRACED);
777 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
778 if (args->options & __WCLONE)
779 options |= WLINUXCLONE;
781 error = kern_wait(args->pid, args->status ? &status : NULL, options,
782 NULL, &args->sysmsg_result);
784 if (error == 0 && args->status) {
786 if (WIFSIGNALED(status))
787 status = (status & 0xffffff80) |
788 BSD_TO_LINUX_SIGNAL(WTERMSIG(status));
789 else if (WIFSTOPPED(status))
790 status = (status & 0xffff00ff) |
791 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(status)) << 8);
792 error = copyout(&status, args->status, sizeof(status));
799 sys_linux_wait4(struct linux_wait4_args *args)
801 struct thread *td = curthread;
802 struct lwp *lp = td->td_lwp;
803 struct rusage rusage;
804 int error, options, status;
810 kprintf(ARGS(wait4, "%d, %p, %d, %p"),
811 args->pid, (void *)args->status, args->options,
812 (void *)args->rusage);
814 options = args->options & (WNOHANG | WUNTRACED);
815 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
816 if (args->options & __WCLONE)
817 options |= WLINUXCLONE;
819 error = kern_wait(args->pid, args->status ? &status : NULL, options,
820 args->rusage ? &rusage : NULL, &args->sysmsg_result);
823 lwp_delsig(lp, SIGCHLD);
825 if (error == 0 && args->status) {
827 if (WIFSIGNALED(status))
828 status = (status & 0xffffff80) |
829 BSD_TO_LINUX_SIGNAL(WTERMSIG(status));
830 else if (WIFSTOPPED(status))
831 status = (status & 0xffff00ff) |
832 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(status)) << 8);
833 error = copyout(&status, args->status, sizeof(status));
835 if (error == 0 && args->rusage)
836 error = copyout(&rusage, args->rusage, sizeof(rusage));
842 sys_linux_mknod(struct linux_mknod_args *args)
844 struct nlookupdata nd;
848 error = linux_copyin_path(args->path, &path, LINUX_PATH_CREATE);
853 kprintf(ARGS(mknod, "%s, %d, %d"),
854 path, args->mode, args->dev);
856 error = nlookup_init(&nd, path, UIO_SYSSPACE, 0);
858 if (args->mode & S_IFIFO) {
859 error = kern_mkfifo(&nd, args->mode);
861 error = kern_mknod(&nd, args->mode,
868 linux_free_path(&path);
873 * UGH! This is just about the dumbest idea I've ever heard!!
876 sys_linux_personality(struct linux_personality_args *args)
879 if (ldebug(personality))
880 kprintf(ARGS(personality, "%d"), args->per);
885 /* Yes Jim, it's still a Linux... */
886 args->sysmsg_result = 0;
891 * Wrappers for get/setitimer for debugging..
894 sys_linux_setitimer(struct linux_setitimer_args *args)
896 struct setitimer_args bsa;
897 struct itimerval foo;
901 if (ldebug(setitimer))
902 kprintf(ARGS(setitimer, "%p, %p"),
903 (void *)args->itv, (void *)args->oitv);
905 bsa.which = args->which;
906 bsa.itv = (struct itimerval *)args->itv;
907 bsa.oitv = (struct itimerval *)args->oitv;
908 bsa.sysmsg_result = 0;
910 if ((error = copyin((caddr_t)args->itv, &foo, sizeof(foo))))
913 if (ldebug(setitimer)) {
914 kprintf("setitimer: value: sec: %ld, usec: %ld\n",
915 foo.it_value.tv_sec, foo.it_value.tv_usec);
916 kprintf("setitimer: interval: sec: %ld, usec: %ld\n",
917 foo.it_interval.tv_sec, foo.it_interval.tv_usec);
921 error = sys_setitimer(&bsa);
922 args->sysmsg_result = bsa.sysmsg_result;
927 sys_linux_getitimer(struct linux_getitimer_args *args)
929 struct getitimer_args bsa;
932 if (ldebug(getitimer))
933 kprintf(ARGS(getitimer, "%p"), (void *)args->itv);
935 bsa.which = args->which;
936 bsa.itv = (struct itimerval *)args->itv;
937 bsa.sysmsg_result = 0;
938 error = sys_getitimer(&bsa);
939 args->sysmsg_result = bsa.sysmsg_result;
944 sys_linux_nice(struct linux_nice_args *args)
946 struct setpriority_args bsd_args;
949 bsd_args.which = PRIO_PROCESS;
950 bsd_args.who = 0; /* current process */
951 bsd_args.prio = args->inc;
952 bsd_args.sysmsg_result = 0;
953 error = sys_setpriority(&bsd_args);
954 args->sysmsg_result = bsd_args.sysmsg_result;
959 sys_linux_setgroups(struct linux_setgroups_args *args)
961 struct thread *td = curthread;
962 struct proc *p = td->td_proc;
963 struct ucred *newcred, *oldcred;
964 l_gid_t linux_gidset[NGROUPS];
970 ngrp = args->gidsetsize;
971 oldcred = p->p_ucred;
974 * cr_groups[0] holds egid. Setting the whole set from
975 * the supplied set will cause egid to be changed too.
976 * Keep cr_groups[0] unchanged to prevent that.
979 if ((error = priv_check_cred(oldcred, PRIV_ROOT, PRISON_ROOT)) != 0)
985 newcred = crdup(oldcred);
987 error = copyin((caddr_t)args->grouplist, linux_gidset,
988 ngrp * sizeof(l_gid_t));
992 newcred->cr_ngroups = ngrp + 1;
994 bsd_gidset = newcred->cr_groups;
997 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1001 newcred->cr_ngroups = 1;
1005 p->p_ucred = newcred;
1011 sys_linux_getgroups(struct linux_getgroups_args *args)
1013 struct thread *td = curthread;
1014 struct proc *p = td->td_proc;
1016 l_gid_t linux_gidset[NGROUPS];
1018 int bsd_gidsetsz, ngrp, error;
1023 bsd_gidset = cred->cr_groups;
1024 bsd_gidsetsz = cred->cr_ngroups - 1;
1027 * cr_groups[0] holds egid. Returning the whole set
1028 * here will cause a duplicate. Exclude cr_groups[0]
1032 if ((ngrp = args->gidsetsize) == 0) {
1033 args->sysmsg_result = bsd_gidsetsz;
1037 if (ngrp < bsd_gidsetsz)
1041 while (ngrp < bsd_gidsetsz) {
1042 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1046 if ((error = copyout(linux_gidset, (caddr_t)args->grouplist,
1047 ngrp * sizeof(l_gid_t))))
1050 args->sysmsg_result = ngrp;
1055 sys_linux_setrlimit(struct linux_setrlimit_args *args)
1057 struct l_rlimit linux_rlim;
1063 if (ldebug(setrlimit))
1064 kprintf(ARGS(setrlimit, "%d, %p"),
1065 args->resource, (void *)args->rlim);
1067 if (args->resource >= LINUX_RLIM_NLIMITS)
1069 which = linux_to_bsd_resource[args->resource];
1073 error = copyin(args->rlim, &linux_rlim, sizeof(linux_rlim));
1076 rlim.rlim_cur = (rlim_t)linux_rlim.rlim_cur;
1077 rlim.rlim_max = (rlim_t)linux_rlim.rlim_max;
1079 error = kern_setrlimit(which, &rlim);
1085 sys_linux_old_getrlimit(struct linux_old_getrlimit_args *args)
1087 struct l_rlimit linux_rlim;
1093 if (ldebug(old_getrlimit))
1094 kprintf(ARGS(old_getrlimit, "%d, %p"),
1095 args->resource, (void *)args->rlim);
1097 if (args->resource >= LINUX_RLIM_NLIMITS)
1099 which = linux_to_bsd_resource[args->resource];
1103 error = kern_getrlimit(which, &rlim);
1106 linux_rlim.rlim_cur = (l_ulong)rlim.rlim_cur;
1107 if (linux_rlim.rlim_cur == ULONG_MAX)
1108 linux_rlim.rlim_cur = LONG_MAX;
1109 linux_rlim.rlim_max = (l_ulong)rlim.rlim_max;
1110 if (linux_rlim.rlim_max == ULONG_MAX)
1111 linux_rlim.rlim_max = LONG_MAX;
1112 error = copyout(&linux_rlim, args->rlim, sizeof(linux_rlim));
1118 sys_linux_getrlimit(struct linux_getrlimit_args *args)
1120 struct l_rlimit linux_rlim;
1126 if (ldebug(getrlimit))
1127 kprintf(ARGS(getrlimit, "%d, %p"),
1128 args->resource, (void *)args->rlim);
1130 if (args->resource >= LINUX_RLIM_NLIMITS)
1132 which = linux_to_bsd_resource[args->resource];
1136 error = kern_getrlimit(which, &rlim);
1139 linux_rlim.rlim_cur = (l_ulong)rlim.rlim_cur;
1140 linux_rlim.rlim_max = (l_ulong)rlim.rlim_max;
1141 error = copyout(&linux_rlim, args->rlim, sizeof(linux_rlim));
1147 sys_linux_sched_setscheduler(struct linux_sched_setscheduler_args *args)
1149 struct sched_setscheduler_args bsd;
1153 if (ldebug(sched_setscheduler))
1154 kprintf(ARGS(sched_setscheduler, "%d, %d, %p"),
1155 args->pid, args->policy, (const void *)args->param);
1158 switch (args->policy) {
1159 case LINUX_SCHED_OTHER:
1160 bsd.policy = SCHED_OTHER;
1162 case LINUX_SCHED_FIFO:
1163 bsd.policy = SCHED_FIFO;
1165 case LINUX_SCHED_RR:
1166 bsd.policy = SCHED_RR;
1172 bsd.pid = args->pid;
1173 bsd.param = (struct sched_param *)args->param;
1174 bsd.sysmsg_result = 0;
1176 error = sys_sched_setscheduler(&bsd);
1177 args->sysmsg_result = bsd.sysmsg_result;
1182 sys_linux_sched_getscheduler(struct linux_sched_getscheduler_args *args)
1184 struct sched_getscheduler_args bsd;
1188 if (ldebug(sched_getscheduler))
1189 kprintf(ARGS(sched_getscheduler, "%d"), args->pid);
1192 bsd.sysmsg_result = 0;
1193 bsd.pid = args->pid;
1194 error = sys_sched_getscheduler(&bsd);
1195 args->sysmsg_result = bsd.sysmsg_result;
1197 switch (args->sysmsg_result) {
1199 args->sysmsg_result = LINUX_SCHED_OTHER;
1202 args->sysmsg_result = LINUX_SCHED_FIFO;
1205 args->sysmsg_result = LINUX_SCHED_RR;
1212 sys_linux_sched_get_priority_max(struct linux_sched_get_priority_max_args *args)
1214 struct sched_get_priority_max_args bsd;
1218 if (ldebug(sched_get_priority_max))
1219 kprintf(ARGS(sched_get_priority_max, "%d"), args->policy);
1222 switch (args->policy) {
1223 case LINUX_SCHED_OTHER:
1224 bsd.policy = SCHED_OTHER;
1226 case LINUX_SCHED_FIFO:
1227 bsd.policy = SCHED_FIFO;
1229 case LINUX_SCHED_RR:
1230 bsd.policy = SCHED_RR;
1235 bsd.sysmsg_result = 0;
1237 error = sys_sched_get_priority_max(&bsd);
1238 args->sysmsg_result = bsd.sysmsg_result;
1243 sys_linux_sched_get_priority_min(struct linux_sched_get_priority_min_args *args)
1245 struct sched_get_priority_min_args bsd;
1249 if (ldebug(sched_get_priority_min))
1250 kprintf(ARGS(sched_get_priority_min, "%d"), args->policy);
1253 switch (args->policy) {
1254 case LINUX_SCHED_OTHER:
1255 bsd.policy = SCHED_OTHER;
1257 case LINUX_SCHED_FIFO:
1258 bsd.policy = SCHED_FIFO;
1260 case LINUX_SCHED_RR:
1261 bsd.policy = SCHED_RR;
1266 bsd.sysmsg_result = 0;
1268 error = sys_sched_get_priority_min(&bsd);
1269 args->sysmsg_result = bsd.sysmsg_result;
1273 #define REBOOT_CAD_ON 0x89abcdef
1274 #define REBOOT_CAD_OFF 0
1275 #define REBOOT_HALT 0xcdef0123
1278 sys_linux_reboot(struct linux_reboot_args *args)
1280 struct reboot_args bsd_args;
1285 kprintf(ARGS(reboot, "0x%x"), args->cmd);
1287 if (args->cmd == REBOOT_CAD_ON || args->cmd == REBOOT_CAD_OFF)
1289 bsd_args.opt = (args->cmd == REBOOT_HALT) ? RB_HALT : 0;
1290 bsd_args.sysmsg_result = 0;
1292 error = sys_reboot(&bsd_args);
1293 args->sysmsg_result = bsd_args.sysmsg_result;
1298 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1299 * p->p_retval[1] when COMPAT_43 or COMPAT_SUNOS is defined. This
1300 * globbers registers that are assumed to be preserved. The following
1301 * lightweight syscalls fixes this. See also linux_getgid16() and
1302 * linux_getuid16() in linux_uid16.c.
1304 * linux_getpid() - MP SAFE
1305 * linux_getgid() - MP SAFE
1306 * linux_getuid() - MP SAFE
1310 sys_linux_getpid(struct linux_getpid_args *args)
1312 struct thread *td = curthread;
1313 struct proc *p = td->td_proc;
1317 args->sysmsg_result = p->p_pid;
1322 sys_linux_getgid(struct linux_getgid_args *args)
1324 struct thread *td = curthread;
1325 struct proc *p = td->td_proc;
1329 args->sysmsg_result = p->p_ucred->cr_rgid;
1334 sys_linux_getuid(struct linux_getuid_args *args)
1336 struct thread *td = curthread;
1337 struct proc *p = td->td_proc;
1341 args->sysmsg_result = p->p_ucred->cr_ruid;
1346 sys_linux_getsid(struct linux_getsid_args *args)
1348 struct getsid_args bsd;
1351 bsd.sysmsg_result = 0;
1352 bsd.pid = args->pid;
1353 error = sys_getsid(&bsd);
1354 args->sysmsg_result = bsd.sysmsg_result;
1359 linux_nosys(struct nosys_args *args)