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 */
113 sys_linux_sysinfo(struct linux_sysinfo_args *args)
115 struct l_sysinfo sysinfo;
121 /* Uptime is copied out of print_uptime() in kern_shutdown.c */
124 if (ts.tv_sec >= 86400) {
128 if (i || ts.tv_sec >= 3600) {
132 if (i || ts.tv_sec >= 60) {
136 sysinfo.uptime=ts.tv_sec;
138 /* Use the information from the mib to get our load averages */
139 for (i = 0; i < 3; i++)
140 sysinfo.loads[i] = averunnable.ldavg[i];
142 sysinfo.totalram = Maxmem * PAGE_SIZE;
143 sysinfo.freeram = sysinfo.totalram - vmstats.v_wire_count * PAGE_SIZE;
146 sysinfo.sharedram = 0;
147 for (object = TAILQ_FIRST(&vm_object_list); object != NULL;
148 object = TAILQ_NEXT(object, object_list))
149 if (object->shadow_count > 1)
150 sysinfo.sharedram += object->resident_page_count;
152 sysinfo.sharedram *= PAGE_SIZE;
153 sysinfo.bufferram = 0;
155 if (swapblist == NULL) {
156 sysinfo.totalswap= 0;
157 sysinfo.freeswap = 0;
159 sysinfo.totalswap = swapblist->bl_blocks * 1024;
160 sysinfo.freeswap = swapblist->bl_root->u.bmu_avail * PAGE_SIZE;
164 sysinfo.procs = 20; /* Hack */
166 error = copyout(&sysinfo, (caddr_t)args->info, sizeof(sysinfo));
174 sys_linux_alarm(struct linux_alarm_args *args)
176 struct thread *td = curthread;
177 struct proc *p = td->td_proc;
178 struct itimerval it, old_it;
183 kprintf(ARGS(alarm, "%u"), args->secs);
186 if (args->secs > 100000000)
189 it.it_value.tv_sec = (long)args->secs;
190 it.it_value.tv_usec = 0;
191 it.it_interval.tv_sec = 0;
192 it.it_interval.tv_usec = 0;
195 old_it = p->p_realtimer;
197 if (timevalisset(&old_it.it_value))
198 callout_stop(&p->p_ithandle);
199 if (it.it_value.tv_sec != 0) {
200 callout_reset(&p->p_ithandle, tvtohz_high(&it.it_value),
202 timevaladd(&it.it_value, &tv);
207 if (timevalcmp(&old_it.it_value, &tv, >)) {
208 timevalsub(&old_it.it_value, &tv);
209 if (old_it.it_value.tv_usec != 0)
210 old_it.it_value.tv_sec++;
211 args->sysmsg_result = old_it.it_value.tv_sec;
220 sys_linux_brk(struct linux_brk_args *args)
222 struct thread *td = curthread;
223 struct proc *p = td->td_proc;
225 vm_offset_t new, old;
226 struct obreak_args bsd_args;
232 kprintf(ARGS(brk, "%p"), (void *)args->dsend);
234 old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
235 new = (vm_offset_t)args->dsend;
236 bsd_args.sysmsg_result = 0;
237 bsd_args.nsize = (char *) new;
238 bsd_args.sysmsg_result = 0;
239 if (((caddr_t)new > vm->vm_daddr) && !sys_obreak(&bsd_args))
240 args->sysmsg_result = (long)new;
242 args->sysmsg_result = (long)old;
252 sys_linux_uselib(struct linux_uselib_args *args)
254 struct thread *td = curthread;
256 struct nlookupdata nd;
261 unsigned long file_offset;
263 unsigned long bss_size;
270 error = linux_copyin_path(args->library, &path, LINUX_PATH_EXISTS);
275 kprintf(ARGS(uselib, "%s"), path);
283 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
284 nd.nl_flags |= NLC_EXEC;
286 error = nlookup(&nd);
288 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
292 * From here on down, we have a locked vnode that must be unlocked.
297 if (vp->v_writecount) {
303 error = VOP_GETATTR(vp, &attr);
307 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
308 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
314 if (attr.va_size == 0) {
319 error = VOP_OPEN(vp, FREAD, p->p_ucred, NULL);
324 * Lock no longer needed
329 /* Pull in executable header into kernel_map */
330 error = vm_mmap(&kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE,
331 VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp, 0);
335 /* Is it a Linux binary ? */
336 if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
342 * While we are here, we should REALLY do some more checks
345 /* Set file/virtual offset based on a.out variant. */
346 switch ((int)(a_out->a_magic & 0xffff)) {
347 case 0413: /* ZMAGIC */
350 case 0314: /* QMAGIC */
358 bss_size = round_page(a_out->a_bss);
360 /* Check various fields in header for validity/bounds. */
361 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
366 /* text + data can't exceed file size */
367 if (a_out->a_data + a_out->a_text > attr.va_size) {
373 * text/data/bss must not exceed limits
374 * XXX - this is not complete. it should check current usage PLUS
375 * the resources needed by this library.
377 if (a_out->a_text > maxtsiz ||
378 a_out->a_data + bss_size > p->p_rlimit[RLIMIT_DATA].rlim_cur) {
383 /* prevent more writers */
387 * Check if file_offset page aligned. Currently we cannot handle
388 * misalinged file offsets, and so we read in the entire image
391 if (file_offset & PAGE_MASK) {
393 kprintf("uselib: Non page aligned binary %lu\n", file_offset);
395 /* Map text+data read/write/execute */
397 /* a_entry is the load address and is page aligned */
398 vmaddr = trunc_page(a_out->a_entry);
400 /* get anon user mapping, read+write+execute */
401 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0,
402 &vmaddr, a_out->a_text + a_out->a_data,
405 VM_PROT_ALL, VM_PROT_ALL,
410 /* map file into kernel_map */
411 error = vm_mmap(&kernel_map, &buffer,
412 round_page(a_out->a_text + a_out->a_data + file_offset),
413 VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp,
414 trunc_page(file_offset));
418 /* copy from kernel VM space to user space */
419 error = copyout((caddr_t)(uintptr_t)(buffer + file_offset),
420 (caddr_t)vmaddr, a_out->a_text + a_out->a_data);
422 /* release temporary kernel space */
423 vm_map_remove(&kernel_map, buffer, buffer +
424 round_page(a_out->a_text + a_out->a_data + file_offset));
430 kprintf("uselib: Page aligned binary %lu\n", file_offset);
433 * for QMAGIC, a_entry is 20 bytes beyond the load address
434 * to skip the executable header
436 vmaddr = trunc_page(a_out->a_entry);
439 * Map it all into the process's space as a single
440 * copy-on-write "data" segment.
442 error = vm_mmap(&p->p_vmspace->vm_map, &vmaddr,
443 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
444 MAP_PRIVATE | MAP_FIXED, (caddr_t)vp, file_offset);
449 kprintf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long*)vmaddr)[0],
453 /* Calculate BSS start address */
454 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
457 /* allocate some 'anon' space */
458 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0,
462 VM_PROT_ALL, VM_PROT_ALL,
469 /* Unlock/release vnode */
475 /* Release the kernel mapping. */
477 vm_map_remove(&kernel_map, (vm_offset_t)a_out,
478 (vm_offset_t)a_out + PAGE_SIZE);
482 linux_free_path(&path);
490 sys_linux_select(struct linux_select_args *args)
492 struct select_args bsa;
493 struct timeval tv0, tv1, utv, *tvp;
499 kprintf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
500 (void *)args->readfds, (void *)args->writefds,
501 (void *)args->exceptfds, (void *)args->timeout);
505 bsa.sysmsg_result = 0;
507 bsa.in = args->readfds;
508 bsa.ou = args->writefds;
509 bsa.ex = args->exceptfds;
510 bsa.tv = (struct timeval *)args->timeout;
513 * Store current time for computation of the amount of
517 if ((error = copyin((caddr_t)args->timeout, &utv,
522 kprintf(LMSG("incoming timeout (%ld/%ld)"),
523 utv.tv_sec, utv.tv_usec);
526 if (itimerfix(&utv)) {
528 * The timeval was invalid. Convert it to something
529 * valid that will act as it does under Linux.
531 sg = stackgap_init();
532 tvp = stackgap_alloc(&sg, sizeof(utv));
533 utv.tv_sec += utv.tv_usec / 1000000;
534 utv.tv_usec %= 1000000;
535 if (utv.tv_usec < 0) {
537 utv.tv_usec += 1000000;
541 if ((error = copyout(&utv, tvp, sizeof(utv))))
548 error = sys_select(&bsa);
549 args->sysmsg_result = bsa.sysmsg_result;
552 kprintf(LMSG("real select returns %d"), error);
556 * See fs/select.c in the Linux kernel. Without this,
557 * Maelstrom doesn't work.
559 if (error == ERESTART)
565 if (args->sysmsg_result) {
567 * Compute how much time was left of the timeout,
568 * by subtracting the current time and the time
569 * before we started the call, and subtracting
570 * that result from the user-supplied value.
573 timevalsub(&tv1, &tv0);
574 timevalsub(&utv, &tv1);
581 kprintf(LMSG("outgoing timeout (%ld/%ld)"),
582 utv.tv_sec, utv.tv_usec);
584 if ((error = copyout(&utv, (caddr_t)args->timeout,
592 kprintf(LMSG("select_out -> %d"), error);
601 sys_linux_mremap(struct linux_mremap_args *args)
603 struct munmap_args bsd_args;
608 kprintf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
610 (unsigned long)args->old_len,
611 (unsigned long)args->new_len,
612 (unsigned long)args->flags);
614 args->new_len = round_page(args->new_len);
615 args->old_len = round_page(args->old_len);
617 if (args->new_len > args->old_len) {
618 args->sysmsg_result = 0;
622 if (args->new_len < args->old_len) {
623 bsd_args.sysmsg_result = 0;
624 bsd_args.addr = (caddr_t)(args->addr + args->new_len);
625 bsd_args.len = args->old_len - args->new_len;
626 error = sys_munmap(&bsd_args);
629 args->sysmsg_resultp = error ? NULL : (void *)args->addr;
633 #define LINUX_MS_ASYNC 0x0001
634 #define LINUX_MS_INVALIDATE 0x0002
635 #define LINUX_MS_SYNC 0x0004
641 sys_linux_msync(struct linux_msync_args *args)
643 struct msync_args bsd_args;
646 bsd_args.addr = (caddr_t)args->addr;
647 bsd_args.len = args->len;
648 bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
649 bsd_args.sysmsg_result = 0;
651 error = sys_msync(&bsd_args);
652 args->sysmsg_result = bsd_args.sysmsg_result;
660 sys_linux_time(struct linux_time_args *args)
668 kprintf(ARGS(time, "*"));
673 if (args->tm && (error = copyout(&tm, (caddr_t)args->tm, sizeof(tm))))
675 args->sysmsg_lresult = tm;
679 struct l_times_argv {
686 #define CLK_TCK 100 /* Linux uses 100 */
688 #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
694 sys_linux_times(struct linux_times_args *args)
696 struct thread *td = curthread;
697 struct proc *p = td->td_proc;
699 struct l_times_argv tms;
705 kprintf(ARGS(times, "*"));
712 tms.tms_utime = CONVTCK(ru.ru_utime);
713 tms.tms_stime = CONVTCK(ru.ru_stime);
715 tms.tms_cutime = CONVTCK(p->p_cru.ru_utime);
716 tms.tms_cstime = CONVTCK(p->p_cru.ru_stime);
718 if ((error = copyout(&tms, (caddr_t)args->buf, sizeof(tms))))
722 args->sysmsg_result = (int)CONVTCK(tv);
730 sys_linux_newuname(struct linux_newuname_args *args)
732 struct thread *td = curthread;
733 struct l_new_utsname utsname;
734 char *osrelease, *osname;
737 if (ldebug(newuname))
738 kprintf(ARGS(newuname, "*"));
742 osname = linux_get_osname(td);
743 osrelease = linux_get_osrelease(td);
745 bzero(&utsname, sizeof(utsname));
746 strncpy(utsname.sysname, osname, LINUX_MAX_UTSNAME-1);
747 strncpy(utsname.nodename, hostname, LINUX_MAX_UTSNAME-1);
748 strncpy(utsname.release, osrelease, LINUX_MAX_UTSNAME-1);
749 strncpy(utsname.version, version, LINUX_MAX_UTSNAME-1);
750 strncpy(utsname.machine, machine, LINUX_MAX_UTSNAME-1);
751 strncpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME-1);
754 return (copyout(&utsname, (caddr_t)args->buf, sizeof(utsname)));
757 #if defined(__i386__)
767 sys_linux_utime(struct linux_utime_args *args)
769 struct timeval tv[2];
770 struct l_utimbuf lut;
771 struct nlookupdata nd;
775 error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
780 kprintf(ARGS(utime, "%s, *"), path);
784 error = copyin(args->times, &lut, sizeof(lut));
787 tv[0].tv_sec = lut.l_actime;
789 tv[1].tv_sec = lut.l_modtime;
793 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
795 error = kern_utimes(&nd, args->times ? tv : NULL);
799 linux_free_path(&path);
802 #endif /* __i386__ */
804 #define __WCLONE 0x80000000
810 sys_linux_waitpid(struct linux_waitpid_args *args)
812 int error, options, status;
816 kprintf(ARGS(waitpid, "%d, %p, %d"),
817 args->pid, (void *)args->status, args->options);
819 options = args->options & (WNOHANG | WUNTRACED);
820 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
821 if (args->options & __WCLONE)
822 options |= WLINUXCLONE;
824 error = kern_wait(args->pid, args->status ? &status : NULL, options,
825 NULL, &args->sysmsg_result);
827 if (error == 0 && args->status) {
829 if (WIFSIGNALED(status))
830 status = (status & 0xffffff80) |
831 BSD_TO_LINUX_SIGNAL(WTERMSIG(status));
832 else if (WIFSTOPPED(status))
833 status = (status & 0xffff00ff) |
834 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(status)) << 8);
835 error = copyout(&status, args->status, sizeof(status));
845 sys_linux_wait4(struct linux_wait4_args *args)
847 struct thread *td = curthread;
848 struct lwp *lp = td->td_lwp;
849 struct rusage rusage;
850 int error, options, status;
854 kprintf(ARGS(wait4, "%d, %p, %d, %p"),
855 args->pid, (void *)args->status, args->options,
856 (void *)args->rusage);
858 options = args->options & (WNOHANG | WUNTRACED);
859 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
860 if (args->options & __WCLONE)
861 options |= WLINUXCLONE;
863 error = kern_wait(args->pid, args->status ? &status : NULL, options,
864 args->rusage ? &rusage : NULL, &args->sysmsg_result);
867 lwp_delsig(lp, SIGCHLD);
869 if (error == 0 && args->status) {
871 if (WIFSIGNALED(status))
872 status = (status & 0xffffff80) |
873 BSD_TO_LINUX_SIGNAL(WTERMSIG(status));
874 else if (WIFSTOPPED(status))
875 status = (status & 0xffff00ff) |
876 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(status)) << 8);
877 error = copyout(&status, args->status, sizeof(status));
879 if (error == 0 && args->rusage)
880 error = copyout(&rusage, args->rusage, sizeof(rusage));
889 sys_linux_mknod(struct linux_mknod_args *args)
891 struct nlookupdata nd;
895 error = linux_copyin_path(args->path, &path, LINUX_PATH_CREATE);
900 kprintf(ARGS(mknod, "%s, %d, %d"),
901 path, args->mode, args->dev);
904 error = nlookup_init(&nd, path, UIO_SYSSPACE, 0);
906 if (args->mode & S_IFIFO) {
907 error = kern_mkfifo(&nd, args->mode);
909 error = kern_mknod(&nd, args->mode,
917 linux_free_path(&path);
922 * UGH! This is just about the dumbest idea I've ever heard!!
927 sys_linux_personality(struct linux_personality_args *args)
930 if (ldebug(personality))
931 kprintf(ARGS(personality, "%d"), args->per);
936 /* Yes Jim, it's still a Linux... */
937 args->sysmsg_result = 0;
942 * Wrappers for get/setitimer for debugging..
947 sys_linux_setitimer(struct linux_setitimer_args *args)
949 struct setitimer_args bsa;
950 struct itimerval foo;
954 if (ldebug(setitimer))
955 kprintf(ARGS(setitimer, "%p, %p"),
956 (void *)args->itv, (void *)args->oitv);
958 bsa.which = args->which;
959 bsa.itv = (struct itimerval *)args->itv;
960 bsa.oitv = (struct itimerval *)args->oitv;
961 bsa.sysmsg_result = 0;
963 if ((error = copyin((caddr_t)args->itv, &foo, sizeof(foo))))
966 if (ldebug(setitimer)) {
967 kprintf("setitimer: value: sec: %ld, usec: %ld\n",
968 foo.it_value.tv_sec, foo.it_value.tv_usec);
969 kprintf("setitimer: interval: sec: %ld, usec: %ld\n",
970 foo.it_interval.tv_sec, foo.it_interval.tv_usec);
974 error = sys_setitimer(&bsa);
975 args->sysmsg_result = bsa.sysmsg_result;
983 sys_linux_getitimer(struct linux_getitimer_args *args)
985 struct getitimer_args bsa;
988 if (ldebug(getitimer))
989 kprintf(ARGS(getitimer, "%p"), (void *)args->itv);
991 bsa.which = args->which;
992 bsa.itv = (struct itimerval *)args->itv;
993 bsa.sysmsg_result = 0;
994 error = sys_getitimer(&bsa);
995 args->sysmsg_result = bsa.sysmsg_result;
1003 sys_linux_nice(struct linux_nice_args *args)
1005 struct setpriority_args bsd_args;
1008 bsd_args.which = PRIO_PROCESS;
1009 bsd_args.who = 0; /* current process */
1010 bsd_args.prio = args->inc;
1011 bsd_args.sysmsg_result = 0;
1012 error = sys_setpriority(&bsd_args);
1013 args->sysmsg_result = bsd_args.sysmsg_result;
1021 sys_linux_setgroups(struct linux_setgroups_args *args)
1023 struct thread *td = curthread;
1024 struct proc *p = td->td_proc;
1025 struct ucred *newcred, *oldcred;
1026 l_gid_t linux_gidset[NGROUPS];
1030 ngrp = args->gidsetsize;
1031 oldcred = p->p_ucred;
1034 * cr_groups[0] holds egid. Setting the whole set from
1035 * the supplied set will cause egid to be changed too.
1036 * Keep cr_groups[0] unchanged to prevent that.
1039 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0)
1042 if ((u_int)ngrp >= NGROUPS)
1046 newcred = crdup(oldcred);
1048 error = copyin((caddr_t)args->grouplist, linux_gidset,
1049 ngrp * sizeof(l_gid_t));
1056 newcred->cr_ngroups = ngrp + 1;
1058 bsd_gidset = newcred->cr_groups;
1061 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1065 newcred->cr_ngroups = 1;
1069 p->p_ucred = newcred;
1079 sys_linux_getgroups(struct linux_getgroups_args *args)
1081 struct thread *td = curthread;
1082 struct proc *p = td->td_proc;
1084 l_gid_t linux_gidset[NGROUPS];
1086 int bsd_gidsetsz, ngrp, error;
1089 bsd_gidset = cred->cr_groups;
1090 bsd_gidsetsz = cred->cr_ngroups - 1;
1093 * cr_groups[0] holds egid. Returning the whole set
1094 * here will cause a duplicate. Exclude cr_groups[0]
1098 if ((ngrp = args->gidsetsize) == 0) {
1099 args->sysmsg_result = bsd_gidsetsz;
1103 if ((u_int)ngrp < bsd_gidsetsz)
1107 while (ngrp < bsd_gidsetsz) {
1108 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1112 if ((error = copyout(linux_gidset, args->grouplist,
1113 ngrp * sizeof(l_gid_t)))) {
1117 args->sysmsg_result = ngrp;
1125 sys_linux_setrlimit(struct linux_setrlimit_args *args)
1127 struct l_rlimit linux_rlim;
1133 if (ldebug(setrlimit))
1134 kprintf(ARGS(setrlimit, "%d, %p"),
1135 args->resource, (void *)args->rlim);
1137 if (args->resource >= LINUX_RLIM_NLIMITS)
1139 which = linux_to_bsd_resource[args->resource];
1143 error = copyin(args->rlim, &linux_rlim, sizeof(linux_rlim));
1146 rlim.rlim_cur = (rlim_t)linux_rlim.rlim_cur;
1147 rlim.rlim_max = (rlim_t)linux_rlim.rlim_max;
1149 error = kern_setrlimit(which, &rlim);
1158 sys_linux_old_getrlimit(struct linux_old_getrlimit_args *args)
1160 struct l_rlimit linux_rlim;
1166 if (ldebug(old_getrlimit))
1167 kprintf(ARGS(old_getrlimit, "%d, %p"),
1168 args->resource, (void *)args->rlim);
1170 if (args->resource >= LINUX_RLIM_NLIMITS)
1172 which = linux_to_bsd_resource[args->resource];
1176 error = kern_getrlimit(which, &rlim);
1179 linux_rlim.rlim_cur = (l_ulong)rlim.rlim_cur;
1180 if (linux_rlim.rlim_cur == ULONG_MAX)
1181 linux_rlim.rlim_cur = LONG_MAX;
1182 linux_rlim.rlim_max = (l_ulong)rlim.rlim_max;
1183 if (linux_rlim.rlim_max == ULONG_MAX)
1184 linux_rlim.rlim_max = LONG_MAX;
1185 error = copyout(&linux_rlim, args->rlim, sizeof(linux_rlim));
1194 sys_linux_getrlimit(struct linux_getrlimit_args *args)
1196 struct l_rlimit linux_rlim;
1202 if (ldebug(getrlimit))
1203 kprintf(ARGS(getrlimit, "%d, %p"),
1204 args->resource, (void *)args->rlim);
1206 if (args->resource >= LINUX_RLIM_NLIMITS)
1208 which = linux_to_bsd_resource[args->resource];
1212 error = kern_getrlimit(which, &rlim);
1215 linux_rlim.rlim_cur = (l_ulong)rlim.rlim_cur;
1216 linux_rlim.rlim_max = (l_ulong)rlim.rlim_max;
1217 error = copyout(&linux_rlim, args->rlim, sizeof(linux_rlim));
1226 sys_linux_sched_setscheduler(struct linux_sched_setscheduler_args *args)
1228 struct sched_setscheduler_args bsd;
1232 if (ldebug(sched_setscheduler))
1233 kprintf(ARGS(sched_setscheduler, "%d, %d, %p"),
1234 args->pid, args->policy, (const void *)args->param);
1237 switch (args->policy) {
1238 case LINUX_SCHED_OTHER:
1239 bsd.policy = SCHED_OTHER;
1241 case LINUX_SCHED_FIFO:
1242 bsd.policy = SCHED_FIFO;
1244 case LINUX_SCHED_RR:
1245 bsd.policy = SCHED_RR;
1251 bsd.pid = args->pid;
1252 bsd.param = (struct sched_param *)args->param;
1253 bsd.sysmsg_result = 0;
1255 error = sys_sched_setscheduler(&bsd);
1256 args->sysmsg_result = bsd.sysmsg_result;
1264 sys_linux_sched_getscheduler(struct linux_sched_getscheduler_args *args)
1266 struct sched_getscheduler_args bsd;
1270 if (ldebug(sched_getscheduler))
1271 kprintf(ARGS(sched_getscheduler, "%d"), args->pid);
1274 bsd.sysmsg_result = 0;
1275 bsd.pid = args->pid;
1276 error = sys_sched_getscheduler(&bsd);
1277 args->sysmsg_result = bsd.sysmsg_result;
1279 switch (args->sysmsg_result) {
1281 args->sysmsg_result = LINUX_SCHED_OTHER;
1284 args->sysmsg_result = LINUX_SCHED_FIFO;
1287 args->sysmsg_result = LINUX_SCHED_RR;
1297 sys_linux_sched_get_priority_max(struct linux_sched_get_priority_max_args *args)
1299 struct sched_get_priority_max_args bsd;
1303 if (ldebug(sched_get_priority_max))
1304 kprintf(ARGS(sched_get_priority_max, "%d"), args->policy);
1307 switch (args->policy) {
1308 case LINUX_SCHED_OTHER:
1309 bsd.policy = SCHED_OTHER;
1311 case LINUX_SCHED_FIFO:
1312 bsd.policy = SCHED_FIFO;
1314 case LINUX_SCHED_RR:
1315 bsd.policy = SCHED_RR;
1320 bsd.sysmsg_result = 0;
1322 error = sys_sched_get_priority_max(&bsd);
1323 args->sysmsg_result = bsd.sysmsg_result;
1331 sys_linux_sched_get_priority_min(struct linux_sched_get_priority_min_args *args)
1333 struct sched_get_priority_min_args bsd;
1337 if (ldebug(sched_get_priority_min))
1338 kprintf(ARGS(sched_get_priority_min, "%d"), args->policy);
1341 switch (args->policy) {
1342 case LINUX_SCHED_OTHER:
1343 bsd.policy = SCHED_OTHER;
1345 case LINUX_SCHED_FIFO:
1346 bsd.policy = SCHED_FIFO;
1348 case LINUX_SCHED_RR:
1349 bsd.policy = SCHED_RR;
1354 bsd.sysmsg_result = 0;
1356 error = sys_sched_get_priority_min(&bsd);
1357 args->sysmsg_result = bsd.sysmsg_result;
1361 #define REBOOT_CAD_ON 0x89abcdef
1362 #define REBOOT_CAD_OFF 0
1363 #define REBOOT_HALT 0xcdef0123
1369 sys_linux_reboot(struct linux_reboot_args *args)
1371 struct reboot_args bsd_args;
1376 kprintf(ARGS(reboot, "0x%x"), args->cmd);
1378 if (args->cmd == REBOOT_CAD_ON || args->cmd == REBOOT_CAD_OFF)
1380 bsd_args.opt = (args->cmd == REBOOT_HALT) ? RB_HALT : 0;
1381 bsd_args.sysmsg_result = 0;
1383 error = sys_reboot(&bsd_args);
1384 args->sysmsg_result = bsd_args.sysmsg_result;
1389 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1390 * p->p_retval[1] when COMPAT_43 or COMPAT_SUNOS is defined. This
1391 * globbers registers that are assumed to be preserved. The following
1392 * lightweight syscalls fixes this. See also linux_getgid16() and
1393 * linux_getuid16() in linux_uid16.c.
1395 * linux_getpid() - MP SAFE
1396 * linux_getgid() - MP SAFE
1397 * linux_getuid() - MP SAFE
1404 sys_linux_getpid(struct linux_getpid_args *args)
1406 struct thread *td = curthread;
1407 struct proc *p = td->td_proc;
1409 args->sysmsg_result = p->p_pid;
1417 sys_linux_getgid(struct linux_getgid_args *args)
1419 struct thread *td = curthread;
1420 struct proc *p = td->td_proc;
1422 args->sysmsg_result = p->p_ucred->cr_rgid;
1430 sys_linux_getuid(struct linux_getuid_args *args)
1432 struct thread *td = curthread;
1433 struct proc *p = td->td_proc;
1435 args->sysmsg_result = p->p_ucred->cr_ruid;
1443 sys_linux_getsid(struct linux_getsid_args *args)
1445 struct getsid_args bsd;
1448 bsd.sysmsg_result = 0;
1449 bsd.pid = args->pid;
1450 error = sys_getsid(&bsd);
1451 args->sysmsg_result = bsd.sysmsg_result;
1459 linux_nosys(struct nosys_args *args)