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.24 2005/06/10 23:27:01 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 <arch_linux/linux.h>
80 #include <arch_linux/linux_proto.h>
81 #include "linux_mib.h"
82 #include "linux_util.h"
84 #define BSD_TO_LINUX_SIGNAL(sig) \
85 (((sig) <= LINUX_SIGTBLSZ) ? bsd_to_linux_signal[_SIG_IDX(sig)] : sig)
87 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
88 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
89 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
94 l_long uptime; /* Seconds since boot */
95 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */
96 l_ulong totalram; /* Total usable main memory size */
97 l_ulong freeram; /* Available memory size */
98 l_ulong sharedram; /* Amount of shared memory */
99 l_ulong bufferram; /* Memory used by buffers */
100 l_ulong totalswap; /* Total swap space size */
101 l_ulong freeswap; /* swap space still available */
102 l_ushort procs; /* Number of current processes */
103 char _f[22]; /* Pads structure to 64 bytes */
107 linux_sysinfo(struct linux_sysinfo_args *args)
109 struct l_sysinfo sysinfo;
114 /* Uptime is copied out of print_uptime() in kern_shutdown.c */
117 if (ts.tv_sec >= 86400) {
121 if (i || ts.tv_sec >= 3600) {
125 if (i || ts.tv_sec >= 60) {
129 sysinfo.uptime=ts.tv_sec;
131 /* Use the information from the mib to get our load averages */
132 for (i = 0; i < 3; i++)
133 sysinfo.loads[i] = averunnable.ldavg[i];
135 sysinfo.totalram = physmem * PAGE_SIZE;
136 sysinfo.freeram = sysinfo.totalram - vmstats.v_wire_count * PAGE_SIZE;
138 sysinfo.sharedram = 0;
139 for (object = TAILQ_FIRST(&vm_object_list); object != NULL;
140 object = TAILQ_NEXT(object, object_list))
141 if (object->shadow_count > 1)
142 sysinfo.sharedram += object->resident_page_count;
144 sysinfo.sharedram *= PAGE_SIZE;
145 sysinfo.bufferram = 0;
147 if (swapblist == NULL) {
148 sysinfo.totalswap= 0;
149 sysinfo.freeswap = 0;
151 sysinfo.totalswap = swapblist->bl_blocks * 1024;
152 sysinfo.freeswap = swapblist->bl_root->u.bmu_avail * PAGE_SIZE;
155 sysinfo.procs = 20; /* Hack */
157 return copyout(&sysinfo, (caddr_t)args->info, sizeof(sysinfo));
161 linux_alarm(struct linux_alarm_args *args)
163 struct thread *td = curthread;
164 struct proc *p = td->td_proc;
165 struct itimerval it, old_it;
172 printf(ARGS(alarm, "%u"), args->secs);
175 if (args->secs > 100000000)
178 it.it_value.tv_sec = (long)args->secs;
179 it.it_value.tv_usec = 0;
180 it.it_interval.tv_sec = 0;
181 it.it_interval.tv_usec = 0;
183 old_it = p->p_realtimer;
185 if (timevalisset(&old_it.it_value))
186 callout_stop(&p->p_ithandle);
187 if (it.it_value.tv_sec != 0) {
188 callout_reset(&p->p_ithandle, tvtohz_high(&it.it_value),
190 timevaladd(&it.it_value, &tv);
194 if (timevalcmp(&old_it.it_value, &tv, >)) {
195 timevalsub(&old_it.it_value, &tv);
196 if (old_it.it_value.tv_usec != 0)
197 old_it.it_value.tv_sec++;
198 args->sysmsg_result = old_it.it_value.tv_sec;
204 linux_brk(struct linux_brk_args *args)
206 struct thread *td = curthread;
207 struct proc *p = td->td_proc;
209 vm_offset_t new, old;
210 struct obreak_args bsd_args;
216 printf(ARGS(brk, "%p"), (void *)args->dsend);
218 old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
219 new = (vm_offset_t)args->dsend;
220 bsd_args.sysmsg_result = 0;
221 bsd_args.nsize = (char *) new;
222 bsd_args.sysmsg_result = 0;
223 if (((caddr_t)new > vm->vm_daddr) && !obreak(&bsd_args))
224 args->sysmsg_result = (long)new;
226 args->sysmsg_result = (long)old;
232 linux_uselib(struct linux_uselib_args *args)
234 struct thread *td = curthread;
236 struct nlookupdata nd;
241 unsigned long file_offset;
243 unsigned long bss_size;
248 KKASSERT(td->td_proc);
251 error = linux_copyin_path(args->library, &path, LINUX_PATH_EXISTS);
256 printf(ARGS(uselib, "%s"), path);
263 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
265 error = nlookup(&nd);
267 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
271 * From here on down, we have a locked vnode that must be unlocked.
276 if (vp->v_writecount) {
282 error = VOP_GETATTR(vp, &attr, td);
286 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
287 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
293 if (attr.va_size == 0) {
298 /* Can we access it? */
299 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, td);
303 error = VOP_OPEN(vp, FREAD, p->p_ucred, NULL, td);
308 * Lock no longer needed
310 VOP_UNLOCK(vp, 0, td);
313 /* Pull in executable header into kernel_map */
314 error = vm_mmap(kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE,
315 VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp, 0);
319 /* Is it a Linux binary ? */
320 if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
326 * While we are here, we should REALLY do some more checks
329 /* Set file/virtual offset based on a.out variant. */
330 switch ((int)(a_out->a_magic & 0xffff)) {
331 case 0413: /* ZMAGIC */
334 case 0314: /* QMAGIC */
342 bss_size = round_page(a_out->a_bss);
344 /* Check various fields in header for validity/bounds. */
345 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
350 /* text + data can't exceed file size */
351 if (a_out->a_data + a_out->a_text > attr.va_size) {
357 * text/data/bss must not exceed limits
358 * XXX - this is not complete. it should check current usage PLUS
359 * the resources needed by this library.
361 if (a_out->a_text > maxtsiz ||
362 a_out->a_data + bss_size > p->p_rlimit[RLIMIT_DATA].rlim_cur) {
367 /* prevent more writers */
371 * Check if file_offset page aligned. Currently we cannot handle
372 * misalinged file offsets, and so we read in the entire image
375 if (file_offset & PAGE_MASK) {
377 printf("uselib: Non page aligned binary %lu\n", file_offset);
379 /* Map text+data read/write/execute */
381 /* a_entry is the load address and is page aligned */
382 vmaddr = trunc_page(a_out->a_entry);
384 /* get anon user mapping, read+write+execute */
385 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0,
386 &vmaddr, a_out->a_text + a_out->a_data, FALSE, VM_PROT_ALL,
391 /* map file into kernel_map */
392 error = vm_mmap(kernel_map, &buffer,
393 round_page(a_out->a_text + a_out->a_data + file_offset),
394 VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp,
395 trunc_page(file_offset));
399 /* copy from kernel VM space to user space */
400 error = copyout((caddr_t)(uintptr_t)(buffer + file_offset),
401 (caddr_t)vmaddr, a_out->a_text + a_out->a_data);
403 /* release temporary kernel space */
404 vm_map_remove(kernel_map, buffer, buffer +
405 round_page(a_out->a_text + a_out->a_data + file_offset));
411 printf("uselib: Page aligned binary %lu\n", file_offset);
414 * for QMAGIC, a_entry is 20 bytes beyond the load address
415 * to skip the executable header
417 vmaddr = trunc_page(a_out->a_entry);
420 * Map it all into the process's space as a single
421 * copy-on-write "data" segment.
423 error = vm_mmap(&p->p_vmspace->vm_map, &vmaddr,
424 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
425 MAP_PRIVATE | MAP_FIXED, (caddr_t)vp, file_offset);
430 printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long*)vmaddr)[0],
434 /* Calculate BSS start address */
435 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
438 /* allocate some 'anon' space */
439 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0,
440 &vmaddr, bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
446 /* Unlock/release vnode */
449 VOP_UNLOCK(vp, 0, td);
452 /* Release the kernel mapping. */
454 vm_map_remove(kernel_map, (vm_offset_t)a_out,
455 (vm_offset_t)a_out + PAGE_SIZE);
458 linux_free_path(&path);
463 linux_select(struct linux_select_args *args)
465 struct select_args bsa;
466 struct timeval tv0, tv1, utv, *tvp;
472 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
473 (void *)args->readfds, (void *)args->writefds,
474 (void *)args->exceptfds, (void *)args->timeout);
478 bsa.sysmsg_result = 0;
480 bsa.in = args->readfds;
481 bsa.ou = args->writefds;
482 bsa.ex = args->exceptfds;
483 bsa.tv = (struct timeval *)args->timeout;
486 * Store current time for computation of the amount of
490 if ((error = copyin((caddr_t)args->timeout, &utv,
495 printf(LMSG("incoming timeout (%ld/%ld)"),
496 utv.tv_sec, utv.tv_usec);
499 if (itimerfix(&utv)) {
501 * The timeval was invalid. Convert it to something
502 * valid that will act as it does under Linux.
504 sg = stackgap_init();
505 tvp = stackgap_alloc(&sg, sizeof(utv));
506 utv.tv_sec += utv.tv_usec / 1000000;
507 utv.tv_usec %= 1000000;
508 if (utv.tv_usec < 0) {
510 utv.tv_usec += 1000000;
514 if ((error = copyout(&utv, tvp, sizeof(utv))))
521 error = select(&bsa);
522 args->sysmsg_result = bsa.sysmsg_result;
525 printf(LMSG("real select returns %d"), error);
529 * See fs/select.c in the Linux kernel. Without this,
530 * Maelstrom doesn't work.
532 if (error == ERESTART)
538 if (args->sysmsg_result) {
540 * Compute how much time was left of the timeout,
541 * by subtracting the current time and the time
542 * before we started the call, and subtracting
543 * that result from the user-supplied value.
546 timevalsub(&tv1, &tv0);
547 timevalsub(&utv, &tv1);
554 printf(LMSG("outgoing timeout (%ld/%ld)"),
555 utv.tv_sec, utv.tv_usec);
557 if ((error = copyout(&utv, (caddr_t)args->timeout,
565 printf(LMSG("select_out -> %d"), error);
571 linux_mremap(struct linux_mremap_args *args)
573 struct munmap_args bsd_args;
578 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
580 (unsigned long)args->old_len,
581 (unsigned long)args->new_len,
582 (unsigned long)args->flags);
584 args->new_len = round_page(args->new_len);
585 args->old_len = round_page(args->old_len);
587 if (args->new_len > args->old_len) {
588 args->sysmsg_result = 0;
592 if (args->new_len < args->old_len) {
593 bsd_args.sysmsg_result = 0;
594 bsd_args.addr = (caddr_t)(args->addr + args->new_len);
595 bsd_args.len = args->old_len - args->new_len;
596 error = munmap(&bsd_args);
599 args->sysmsg_resultp = error ? NULL : (void *)args->addr;
603 #define LINUX_MS_ASYNC 0x0001
604 #define LINUX_MS_INVALIDATE 0x0002
605 #define LINUX_MS_SYNC 0x0004
608 linux_msync(struct linux_msync_args *args)
610 struct msync_args bsd_args;
613 bsd_args.addr = (caddr_t)args->addr;
614 bsd_args.len = args->len;
615 bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
616 bsd_args.sysmsg_result = 0;
618 error = msync(&bsd_args);
619 args->sysmsg_result = bsd_args.sysmsg_result;
624 linux_time(struct linux_time_args *args)
632 printf(ARGS(time, "*"));
637 if (args->tm && (error = copyout(&tm, (caddr_t)args->tm, sizeof(tm))))
639 args->sysmsg_lresult = tm;
643 struct l_times_argv {
650 #define CLK_TCK 100 /* Linux uses 100 */
652 #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
655 linux_times(struct linux_times_args *args)
657 struct thread *td = curthread;
658 struct proc *p = td->td_proc;
660 struct l_times_argv tms;
667 printf(ARGS(times, "*"));
670 calcru(p, &ru.ru_utime, &ru.ru_stime, NULL);
672 tms.tms_utime = CONVTCK(ru.ru_utime);
673 tms.tms_stime = CONVTCK(ru.ru_stime);
675 tms.tms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
676 tms.tms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
678 if ((error = copyout(&tms, (caddr_t)args->buf, sizeof(tms))))
682 args->sysmsg_result = (int)CONVTCK(tv);
687 linux_newuname(struct linux_newuname_args *args)
689 struct thread *td = curthread;
690 struct l_new_utsname utsname;
691 char *osrelease, *osname;
694 if (ldebug(newuname))
695 printf(ARGS(newuname, "*"));
698 osname = linux_get_osname(td);
699 osrelease = linux_get_osrelease(td);
701 bzero(&utsname, sizeof(utsname));
702 strncpy(utsname.sysname, osname, LINUX_MAX_UTSNAME-1);
703 strncpy(utsname.nodename, hostname, LINUX_MAX_UTSNAME-1);
704 strncpy(utsname.release, osrelease, LINUX_MAX_UTSNAME-1);
705 strncpy(utsname.version, version, LINUX_MAX_UTSNAME-1);
706 strncpy(utsname.machine, machine, LINUX_MAX_UTSNAME-1);
707 strncpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME-1);
709 return (copyout(&utsname, (caddr_t)args->buf, sizeof(utsname)));
712 #if defined(__i386__)
719 linux_utime(struct linux_utime_args *args)
721 struct timeval tv[2];
722 struct l_utimbuf lut;
723 struct nlookupdata nd;
727 error = linux_copyin_path(args->fname, &path, LINUX_PATH_EXISTS);
732 printf(ARGS(utime, "%s, *"), path);
736 error = copyin(args->times, &lut, sizeof(lut));
739 tv[0].tv_sec = lut.l_actime;
741 tv[1].tv_sec = lut.l_modtime;
744 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
746 error = kern_utimes(&nd, args->times ? tv : NULL);
749 linux_free_path(&path);
752 #endif /* __i386__ */
754 #define __WCLONE 0x80000000
757 linux_waitpid(struct linux_waitpid_args *args)
759 int error, options, status;
763 printf(ARGS(waitpid, "%d, %p, %d"),
764 args->pid, (void *)args->status, args->options);
766 options = args->options & (WNOHANG | WUNTRACED);
767 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
768 if (args->options & __WCLONE)
769 options |= WLINUXCLONE;
771 error = kern_wait(args->pid, args->status ? &status : NULL, options,
772 NULL, &args->sysmsg_result);
774 if (error == 0 && args->status) {
776 if (WIFSIGNALED(status))
777 status = (status & 0xffffff80) |
778 BSD_TO_LINUX_SIGNAL(WTERMSIG(status));
779 else if (WIFSTOPPED(status))
780 status = (status & 0xffff00ff) |
781 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(status)) << 8);
782 error = copyout(&status, args->status, sizeof(status));
789 linux_wait4(struct linux_wait4_args *args)
791 struct thread *td = curthread;
792 struct proc *p = td->td_proc;
793 struct rusage rusage;
794 int error, options, status;
800 printf(ARGS(wait4, "%d, %p, %d, %p"),
801 args->pid, (void *)args->status, args->options,
802 (void *)args->rusage);
804 options = args->options & (WNOHANG | WUNTRACED);
805 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
806 if (args->options & __WCLONE)
807 options |= WLINUXCLONE;
809 error = kern_wait(args->pid, args->status ? &status : NULL, options,
810 args->rusage ? &rusage : NULL, &args->sysmsg_result);
813 SIGDELSET(p->p_siglist, SIGCHLD);
815 if (error == 0 && args->status) {
817 if (WIFSIGNALED(status))
818 status = (status & 0xffffff80) |
819 BSD_TO_LINUX_SIGNAL(WTERMSIG(status));
820 else if (WIFSTOPPED(status))
821 status = (status & 0xffff00ff) |
822 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(status)) << 8);
823 error = copyout(&status, args->status, sizeof(status));
825 if (error == 0 && args->rusage)
826 error = copyout(&rusage, args->rusage, sizeof(rusage));
832 linux_mknod(struct linux_mknod_args *args)
834 struct nlookupdata nd;
838 error = linux_copyin_path(args->path, &path, LINUX_PATH_CREATE);
843 printf(ARGS(mknod, "%s, %d, %d"),
844 path, args->mode, args->dev);
846 error = nlookup_init(&nd, path, UIO_SYSSPACE, 0);
848 if (args->mode & S_IFIFO) {
849 error = kern_mkfifo(&nd, args->mode);
851 error = kern_mknod(&nd, args->mode, args->dev);
856 linux_free_path(&path);
861 * UGH! This is just about the dumbest idea I've ever heard!!
864 linux_personality(struct linux_personality_args *args)
867 if (ldebug(personality))
868 printf(ARGS(personality, "%d"), args->per);
873 /* Yes Jim, it's still a Linux... */
874 args->sysmsg_result = 0;
879 * Wrappers for get/setitimer for debugging..
882 linux_setitimer(struct linux_setitimer_args *args)
884 struct setitimer_args bsa;
885 struct itimerval foo;
889 if (ldebug(setitimer))
890 printf(ARGS(setitimer, "%p, %p"),
891 (void *)args->itv, (void *)args->oitv);
893 bsa.which = args->which;
894 bsa.itv = (struct itimerval *)args->itv;
895 bsa.oitv = (struct itimerval *)args->oitv;
896 bsa.sysmsg_result = 0;
898 if ((error = copyin((caddr_t)args->itv, &foo, sizeof(foo))))
901 if (ldebug(setitimer)) {
902 printf("setitimer: value: sec: %ld, usec: %ld\n",
903 foo.it_value.tv_sec, foo.it_value.tv_usec);
904 printf("setitimer: interval: sec: %ld, usec: %ld\n",
905 foo.it_interval.tv_sec, foo.it_interval.tv_usec);
909 error = setitimer(&bsa);
910 args->sysmsg_result = bsa.sysmsg_result;
915 linux_getitimer(struct linux_getitimer_args *args)
917 struct getitimer_args bsa;
920 if (ldebug(getitimer))
921 printf(ARGS(getitimer, "%p"), (void *)args->itv);
923 bsa.which = args->which;
924 bsa.itv = (struct itimerval *)args->itv;
925 bsa.sysmsg_result = 0;
926 error = getitimer(&bsa);
927 args->sysmsg_result = bsa.sysmsg_result;
932 linux_nice(struct linux_nice_args *args)
934 struct setpriority_args bsd_args;
937 bsd_args.which = PRIO_PROCESS;
938 bsd_args.who = 0; /* current process */
939 bsd_args.prio = args->inc;
940 bsd_args.sysmsg_result = 0;
941 error = setpriority(&bsd_args);
942 args->sysmsg_result = bsd_args.sysmsg_result;
947 linux_setgroups(struct linux_setgroups_args *args)
949 struct thread *td = curthread;
950 struct proc *p = td->td_proc;
951 struct ucred *newcred, *oldcred;
952 l_gid_t linux_gidset[NGROUPS];
958 ngrp = args->gidsetsize;
959 oldcred = p->p_ucred;
962 * cr_groups[0] holds egid. Setting the whole set from
963 * the supplied set will cause egid to be changed too.
964 * Keep cr_groups[0] unchanged to prevent that.
967 if ((error = suser_cred(oldcred, PRISON_ROOT)) != 0)
973 newcred = crdup(oldcred);
975 error = copyin((caddr_t)args->grouplist, linux_gidset,
976 ngrp * sizeof(l_gid_t));
980 newcred->cr_ngroups = ngrp + 1;
982 bsd_gidset = newcred->cr_groups;
985 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
989 newcred->cr_ngroups = 1;
993 p->p_ucred = newcred;
999 linux_getgroups(struct linux_getgroups_args *args)
1001 struct thread *td = curthread;
1002 struct proc *p = td->td_proc;
1004 l_gid_t linux_gidset[NGROUPS];
1006 int bsd_gidsetsz, ngrp, error;
1011 bsd_gidset = cred->cr_groups;
1012 bsd_gidsetsz = cred->cr_ngroups - 1;
1015 * cr_groups[0] holds egid. Returning the whole set
1016 * here will cause a duplicate. Exclude cr_groups[0]
1020 if ((ngrp = args->gidsetsize) == 0) {
1021 args->sysmsg_result = bsd_gidsetsz;
1025 if (ngrp < bsd_gidsetsz)
1029 while (ngrp < bsd_gidsetsz) {
1030 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1034 if ((error = copyout(linux_gidset, (caddr_t)args->grouplist,
1035 ngrp * sizeof(l_gid_t))))
1038 args->sysmsg_result = ngrp;
1043 linux_setrlimit(struct linux_setrlimit_args *args)
1045 struct l_rlimit linux_rlim;
1051 if (ldebug(setrlimit))
1052 printf(ARGS(setrlimit, "%d, %p"),
1053 args->resource, (void *)args->rlim);
1055 if (args->resource >= LINUX_RLIM_NLIMITS)
1057 which = linux_to_bsd_resource[args->resource];
1061 error = copyin(args->rlim, &linux_rlim, sizeof(linux_rlim));
1064 rlim.rlim_cur = (rlim_t)linux_rlim.rlim_cur;
1065 rlim.rlim_max = (rlim_t)linux_rlim.rlim_max;
1067 error = kern_setrlimit(which, &rlim);
1073 linux_old_getrlimit(struct linux_old_getrlimit_args *args)
1075 struct l_rlimit linux_rlim;
1081 if (ldebug(old_getrlimit))
1082 printf(ARGS(old_getrlimit, "%d, %p"),
1083 args->resource, (void *)args->rlim);
1085 if (args->resource >= LINUX_RLIM_NLIMITS)
1087 which = linux_to_bsd_resource[args->resource];
1091 error = kern_getrlimit(which, &rlim);
1094 linux_rlim.rlim_cur = (l_ulong)rlim.rlim_cur;
1095 if (linux_rlim.rlim_cur == ULONG_MAX)
1096 linux_rlim.rlim_cur = LONG_MAX;
1097 linux_rlim.rlim_max = (l_ulong)rlim.rlim_max;
1098 if (linux_rlim.rlim_max == ULONG_MAX)
1099 linux_rlim.rlim_max = LONG_MAX;
1100 error = copyout(&linux_rlim, args->rlim, sizeof(linux_rlim));
1106 linux_getrlimit(struct linux_getrlimit_args *args)
1108 struct l_rlimit linux_rlim;
1114 if (ldebug(getrlimit))
1115 printf(ARGS(getrlimit, "%d, %p"),
1116 args->resource, (void *)args->rlim);
1118 if (args->resource >= LINUX_RLIM_NLIMITS)
1120 which = linux_to_bsd_resource[args->resource];
1124 error = kern_getrlimit(which, &rlim);
1127 linux_rlim.rlim_cur = (l_ulong)rlim.rlim_cur;
1128 linux_rlim.rlim_max = (l_ulong)rlim.rlim_max;
1129 error = copyout(&linux_rlim, args->rlim, sizeof(linux_rlim));
1135 linux_sched_setscheduler(struct linux_sched_setscheduler_args *args)
1137 struct sched_setscheduler_args bsd;
1141 if (ldebug(sched_setscheduler))
1142 printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1143 args->pid, args->policy, (const void *)args->param);
1146 switch (args->policy) {
1147 case LINUX_SCHED_OTHER:
1148 bsd.policy = SCHED_OTHER;
1150 case LINUX_SCHED_FIFO:
1151 bsd.policy = SCHED_FIFO;
1153 case LINUX_SCHED_RR:
1154 bsd.policy = SCHED_RR;
1160 bsd.pid = args->pid;
1161 bsd.param = (struct sched_param *)args->param;
1162 bsd.sysmsg_result = 0;
1164 error = sched_setscheduler(&bsd);
1165 args->sysmsg_result = bsd.sysmsg_result;
1170 linux_sched_getscheduler(struct linux_sched_getscheduler_args *args)
1172 struct sched_getscheduler_args bsd;
1176 if (ldebug(sched_getscheduler))
1177 printf(ARGS(sched_getscheduler, "%d"), args->pid);
1180 bsd.sysmsg_result = 0;
1181 bsd.pid = args->pid;
1182 error = sched_getscheduler(&bsd);
1183 args->sysmsg_result = bsd.sysmsg_result;
1185 switch (args->sysmsg_result) {
1187 args->sysmsg_result = LINUX_SCHED_OTHER;
1190 args->sysmsg_result = LINUX_SCHED_FIFO;
1193 args->sysmsg_result = LINUX_SCHED_RR;
1200 linux_sched_get_priority_max(struct linux_sched_get_priority_max_args *args)
1202 struct sched_get_priority_max_args bsd;
1206 if (ldebug(sched_get_priority_max))
1207 printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1210 switch (args->policy) {
1211 case LINUX_SCHED_OTHER:
1212 bsd.policy = SCHED_OTHER;
1214 case LINUX_SCHED_FIFO:
1215 bsd.policy = SCHED_FIFO;
1217 case LINUX_SCHED_RR:
1218 bsd.policy = SCHED_RR;
1223 bsd.sysmsg_result = 0;
1225 error = sched_get_priority_max(&bsd);
1226 args->sysmsg_result = bsd.sysmsg_result;
1231 linux_sched_get_priority_min(struct linux_sched_get_priority_min_args *args)
1233 struct sched_get_priority_min_args bsd;
1237 if (ldebug(sched_get_priority_min))
1238 printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1241 switch (args->policy) {
1242 case LINUX_SCHED_OTHER:
1243 bsd.policy = SCHED_OTHER;
1245 case LINUX_SCHED_FIFO:
1246 bsd.policy = SCHED_FIFO;
1248 case LINUX_SCHED_RR:
1249 bsd.policy = SCHED_RR;
1254 bsd.sysmsg_result = 0;
1256 error = sched_get_priority_min(&bsd);
1257 args->sysmsg_result = bsd.sysmsg_result;
1261 #define REBOOT_CAD_ON 0x89abcdef
1262 #define REBOOT_CAD_OFF 0
1263 #define REBOOT_HALT 0xcdef0123
1266 linux_reboot(struct linux_reboot_args *args)
1268 struct reboot_args bsd_args;
1273 printf(ARGS(reboot, "0x%x"), args->cmd);
1275 if (args->cmd == REBOOT_CAD_ON || args->cmd == REBOOT_CAD_OFF)
1277 bsd_args.opt = (args->cmd == REBOOT_HALT) ? RB_HALT : 0;
1278 bsd_args.sysmsg_result = 0;
1280 error = reboot(&bsd_args);
1281 args->sysmsg_result = bsd_args.sysmsg_result;
1286 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1287 * p->p_retval[1] when COMPAT_43 or COMPAT_SUNOS is defined. This
1288 * globbers registers that are assumed to be preserved. The following
1289 * lightweight syscalls fixes this. See also linux_getgid16() and
1290 * linux_getuid16() in linux_uid16.c.
1292 * linux_getpid() - MP SAFE
1293 * linux_getgid() - MP SAFE
1294 * linux_getuid() - MP SAFE
1298 linux_getpid(struct linux_getpid_args *args)
1300 struct thread *td = curthread;
1301 struct proc *p = td->td_proc;
1305 args->sysmsg_result = p->p_pid;
1310 linux_getgid(struct linux_getgid_args *args)
1312 struct thread *td = curthread;
1313 struct proc *p = td->td_proc;
1317 args->sysmsg_result = p->p_ucred->cr_rgid;
1322 linux_getuid(struct linux_getuid_args *args)
1324 struct thread *td = curthread;
1325 struct proc *p = td->td_proc;
1329 args->sysmsg_result = p->p_ucred->cr_ruid;
1334 linux_getsid(struct linux_getsid_args *args)
1336 struct getsid_args bsd;
1339 bsd.sysmsg_result = 0;
1340 bsd.pid = args->pid;
1341 error = getsid(&bsd);
1342 args->sysmsg_result = bsd.sysmsg_result;