2 * Copyright (c) 1998 Mark Newton
3 * Copyright (c) 1994 Christos Zoulas
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * $FreeBSD: src/sys/svr4/svr4_misc.c,v 1.13.2.7 2003/01/14 21:33:58 dillon Exp $
29 * $DragonFly: src/sys/emulation/svr4/Attic/svr4_misc.c,v 1.30 2005/11/14 18:50:01 dillon Exp $
33 * SVR4 compatibility module.
35 * SVR4 system calls that are implemented differently in BSD are
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/dirent.h>
42 #include <sys/kern_syscall.h>
43 #include <sys/malloc.h>
45 #include <sys/nlookup.h>
50 #include <sys/filedesc.h>
51 #include <sys/kernel.h>
53 #include <sys/mount.h>
54 #include <sys/nlookup.h>
55 #include <sys/resource.h>
56 #include <sys/resourcevar.h>
57 #include <sys/vnode.h>
60 #include <sys/times.h>
61 #include <sys/fcntl.h>
64 #include <sys/ptrace.h>
65 #include <vm/vm_zone.h>
67 #include <sys/sysproto.h>
70 #include "svr4_types.h"
71 #include "svr4_signal.h"
72 #include "svr4_proto.h"
73 #include "svr4_util.h"
74 #include "svr4_sysconfig.h"
75 #include "svr4_dirent.h"
77 #include "svr4_ulimit.h"
78 #include "svr4_statvfs.h"
80 #include "svr4_mman.h"
81 #include "svr4_wait.h"
83 #include <machine/vmparam.h>
85 #include <vm/vm_param.h>
86 #include <vm/vm_map.h>
88 #if defined(__DragonFly__) || defined(__FreeBSD__)
89 #include <vm/vm_extern.h>
94 # include <uvm/uvm_extern.h>
98 #define BSD_DIRENT(cp) ((struct dirent *)(cp))
100 static int svr4_mknod (register_t *, char *, svr4_mode_t, svr4_dev_t);
102 static __inline clock_t timeval_to_clock_t (struct timeval *);
103 static int svr4_setinfo (struct proc *, int, svr4_siginfo_t *);
105 struct svr4_hrtcntl_args;
106 static int svr4_hrtcntl (struct svr4_hrtcntl_args *, register_t *);
107 static void bsd_statfs_to_svr4_statvfs (const struct statfs *,
108 struct svr4_statvfs *, int);
109 static void bsd_statfs_to_svr4_statvfs64 (const struct statfs *,
110 struct svr4_statvfs64 *, int);
111 static struct proc *svr4_pfind (pid_t pid);
116 svr4_sys_setitimer(struct svr4_sys_setitimer_args *uap)
118 uap->sysmsg_result = 0;
124 svr4_sys_wait(struct svr4_sys_wait_args *uap)
127 int error, *retval = &uap->sysmsg_result, st, sig;
128 size_t sz = sizeof(*SCARG(&w4, status));
130 SCARG(&w4, rusage) = NULL;
131 SCARG(&w4, options) = 0;
133 if (SCARG(uap, status) == NULL) {
134 caddr_t sg = stackgap_init();
136 SCARG(&w4, status) = stackgap_alloc(&sg, sz);
139 SCARG(&w4, status) = SCARG(uap, status);
141 SCARG(&w4, pid) = WAIT_ANY;
142 w4.sysmsg_result = 0;
143 if ((error = wait4(&w4)) != 0)
145 *retval = w4.sysmsg_result;
147 if ((error = copyin(SCARG(&w4, status), &st, sizeof(st))) != 0)
150 if (WIFSIGNALED(st)) {
152 if (sig >= 0 && sig < NSIG)
153 st = (st & ~0177) | SVR4_BSD2SVR4_SIG(sig);
154 } else if (WIFSTOPPED(st)) {
156 if (sig >= 0 && sig < NSIG)
157 st = (st & ~0xff00) | (SVR4_BSD2SVR4_SIG(sig) << 8);
161 * It looks like wait(2) on svr4/solaris/2.4 returns
162 * the status in retval[1], and the pid on retval[0].
166 if (SCARG(uap, status))
167 if ((error = copyout(&st, SCARG(uap, status), sizeof(st))) != 0)
174 svr4_sys_execv(struct svr4_sys_execv_args *uap)
176 struct execve_args ap;
180 sg = stackgap_init();
181 CHECKALTEXIST(&sg, uap->path);
183 /* note: parts of result64 may be maintained or cleared by execve */
184 ap.sysmsg_result64 = uap->sysmsg_result64;
185 ap.fname = uap->path;
189 uap->sysmsg_result64 = ap.sysmsg_result64;
194 svr4_sys_execve(struct svr4_sys_execve_args *uap)
196 struct execve_args ap;
200 sg = stackgap_init();
201 CHECKALTEXIST(&sg, uap->path);
203 SCARG(&ap, fname) = SCARG(uap, path);
204 SCARG(&ap, argv) = SCARG(uap, argp);
205 SCARG(&ap, envv) = SCARG(uap, envp);
207 ap.sysmsg_result = 0;
209 uap->sysmsg_result = ap.sysmsg_result;
214 svr4_sys_time(struct svr4_sys_time_args *v)
216 struct svr4_sys_time_args *uap = v;
222 error = copyout(&tv.tv_sec, SCARG(uap, t),
223 sizeof(*(SCARG(uap, t))));
224 v->sysmsg_result = (int) tv.tv_sec;
231 * Read SVR4-style directory entries. We suck them into kernel space so
232 * that they can be massaged before being copied out to user code.
234 * This code is ported from the Linux emulator: Changes to the VFS interface
235 * between FreeBSD and NetBSD have made it simpler to port it from there than
236 * to adapt the NetBSD version.
239 svr4_sys_getdents64(struct svr4_sys_getdents64_args *uap)
241 struct thread *td = curthread; /* XXX */
242 struct proc *p = td->td_proc;
245 caddr_t inp, buf; /* BSD-format */
246 int len, reclen; /* BSD-format */
247 caddr_t outp; /* SVR4-format */
248 int resid, svr4reclen=0; /* SVR4-format */
254 struct svr4_dirent64 svr4_dirent;
255 int buflen, error, eofflag, nbytes, justone;
256 u_long *cookies = NULL, *cookiep;
261 DPRINTF(("svr4_sys_getdents64(%d, *, %d)\n", uap->fd, uap->nbytes));
262 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0) {
266 if ((fp->f_flag & FREAD) == 0)
269 vp = (struct vnode *) fp->f_data;
271 if (vp->v_type != VDIR)
274 if ((error = VOP_GETATTR(vp, &va, td))) {
278 nbytes = SCARG(uap, nbytes);
280 nbytes = sizeof (struct svr4_dirent64);
287 #define DIRBLKSIZ 512 /* XXX we used to use ufs's DIRBLKSIZ */
288 buflen = max(DIRBLKSIZ, nbytes);
289 buflen = min(buflen, MAXBSIZE);
290 buf = malloc(buflen, M_TEMP, M_WAITOK);
291 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
294 aiov.iov_len = buflen;
295 auio.uio_iov = &aiov;
297 auio.uio_rw = UIO_READ;
298 auio.uio_segflg = UIO_SYSSPACE;
300 auio.uio_resid = buflen;
301 auio.uio_offset = off;
304 free(cookies, M_TEMP);
308 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
309 &ncookies, &cookies);
315 outp = (caddr_t) SCARG(uap, dp);
317 if ((len = buflen - auio.uio_resid) <= 0) {
325 * When using cookies, the vfs has the option of reading from
326 * a different offset than that supplied (UFS truncates the
327 * offset to a block boundary to make sure that it never reads
328 * partway through a directory entry, even if the directory
329 * has been compacted).
331 while (len > 0 && ncookies > 0 && *cookiep <= off) {
332 bdp = (struct dirent *) inp;
333 len -= _DIRENT_DIRSIZ(bdp);
334 inp += _DIRENT_DIRSIZ(bdp);
341 if (cookiep && ncookies == 0)
343 bdp = (struct dirent *) inp;
344 reclen = _DIRENT_DIRSIZ(bdp);
346 DPRINTF(("svr4_readdir: reclen=%d\n", reclen));
351 if (bdp->d_ino == 0) {
361 svr4reclen = SVR4_RECLEN(&svr4_dirent, bdp->d_namlen);
362 if (reclen > len || resid < svr4reclen) {
366 svr4_dirent.d_ino = (long) bdp->d_ino;
369 * old svr4-style readdir usage.
371 svr4_dirent.d_off = (svr4_off_t) svr4reclen;
372 svr4_dirent.d_reclen = (u_short) bdp->d_namlen;
374 svr4_dirent.d_off = (svr4_off_t)(off + reclen);
375 svr4_dirent.d_reclen = (u_short) svr4reclen;
377 strcpy(svr4_dirent.d_name, bdp->d_name);
378 if ((error = copyout((caddr_t)&svr4_dirent, outp, svr4reclen)))
393 if (outp == (caddr_t) SCARG(uap, dp))
398 nbytes = resid + svr4reclen;
401 uap->sysmsg_result = nbytes - resid;
404 free(cookies, M_TEMP);
405 VOP_UNLOCK(vp, 0, td);
412 svr4_sys_getdents(struct svr4_sys_getdents_args *uap)
414 struct thread *td = curthread;
415 struct proc *p = td->td_proc;
418 caddr_t inp, buf; /* BSD-format */
419 int len, reclen; /* BSD-format */
420 caddr_t outp; /* SVR4-format */
421 int resid, svr4_reclen; /* SVR4-format */
425 struct svr4_dirent idb;
426 off_t off; /* true file offset */
427 int buflen, error, eofflag;
428 u_long *cookiebuf = NULL, *cookie;
429 int ncookies = 0, *retval = &uap->sysmsg_result;
433 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
436 if ((fp->f_flag & FREAD) == 0)
439 vp = (struct vnode *)fp->f_data;
440 if (vp->v_type != VDIR)
443 buflen = min(MAXBSIZE, SCARG(uap, nbytes));
444 buf = malloc(buflen, M_TEMP, M_WAITOK);
445 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
449 aiov.iov_len = buflen;
450 auio.uio_iov = &aiov;
452 auio.uio_rw = UIO_READ;
453 auio.uio_segflg = UIO_SYSSPACE;
455 auio.uio_resid = buflen;
456 auio.uio_offset = off;
458 * First we read into the malloc'ed buffer, then
459 * we massage it into user space, one record at a time.
461 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies,
467 outp = SCARG(uap, buf);
468 resid = SCARG(uap, nbytes);
469 if ((len = buflen - auio.uio_resid) == 0)
472 for (cookie = cookiebuf; len > 0; len -= reclen) {
473 bdp = (struct dirent *)inp;
474 reclen = _DIRENT_DIRSIZ(bdp);
476 panic("svr4_sys_getdents64: bad reclen");
477 off = *cookie++; /* each entry points to the next */
478 if ((off >> 32) != 0) {
479 uprintf("svr4_sys_getdents64: dir offset too large for emulated program");
483 if (bdp->d_ino == 0) {
484 inp += reclen; /* it is a hole; squish it out */
487 svr4_reclen = SVR4_RECLEN(&idb, bdp->d_namlen);
488 if (reclen > len || resid < svr4_reclen) {
489 /* entry too big for buffer, so just stop */
494 * Massage in place to make a SVR4-shaped dirent (otherwise
495 * we have to worry about touching user memory outside of
496 * the copyout() call).
498 idb.d_ino = (svr4_ino_t)bdp->d_ino;
499 idb.d_off = (svr4_off_t)off;
500 idb.d_reclen = (u_short)svr4_reclen;
501 strcpy(idb.d_name, bdp->d_name);
502 if ((error = copyout((caddr_t)&idb, outp, svr4_reclen)))
504 /* advance past this real entry */
506 /* advance output past SVR4-shaped entry */
508 resid -= svr4_reclen;
511 /* if we squished out the whole block, try again */
512 if (outp == SCARG(uap, buf))
514 fp->f_offset = off; /* update the vnode offset */
517 *retval = SCARG(uap, nbytes) - resid;
519 VOP_UNLOCK(vp, 0, td);
521 free(cookiebuf, M_TEMP);
528 svr4_sys_mmap(struct svr4_sys_mmap_args *uap)
534 retval = &uap->sysmsg_result;
535 #define _MAP_NEW 0x80000000
537 * Verify the arguments.
539 if (SCARG(uap, prot) & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
540 return EINVAL; /* XXX still needed? */
542 if (SCARG(uap, len) == 0)
545 SCARG(&mm, prot) = SCARG(uap, prot);
546 SCARG(&mm, len) = SCARG(uap, len);
547 SCARG(&mm, flags) = SCARG(uap, flags) & ~_MAP_NEW;
548 SCARG(&mm, fd) = SCARG(uap, fd);
549 SCARG(&mm, addr) = SCARG(uap, addr);
550 SCARG(&mm, pos) = SCARG(uap, pos);
551 mm.sysmsg_resultp = NULL;
553 uap->sysmsg_resultp = mm.sysmsg_resultp;
558 svr4_sys_mmap64(struct svr4_sys_mmap64_args *uap)
560 struct proc *p = curproc;
565 #define _MAP_NEW 0x80000000
567 * Verify the arguments.
569 if (SCARG(uap, prot) & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
570 return EINVAL; /* XXX still needed? */
572 if (SCARG(uap, len) == 0)
575 SCARG(&mm, prot) = SCARG(uap, prot);
576 SCARG(&mm, len) = SCARG(uap, len);
577 SCARG(&mm, flags) = SCARG(uap, flags) & ~_MAP_NEW;
578 SCARG(&mm, fd) = SCARG(uap, fd);
579 SCARG(&mm, addr) = SCARG(uap, addr);
580 SCARG(&mm, pos) = SCARG(uap, pos);
582 rp = (void *) round_page((vm_offset_t)(p->p_vmspace->vm_daddr + maxdsiz));
583 if ((SCARG(&mm, flags) & MAP_FIXED) == 0 &&
584 SCARG(&mm, addr) != 0 && (void *)SCARG(&mm, addr) < rp)
585 SCARG(&mm, addr) = rp;
587 mm.sysmsg_resultp = 0;
589 uap->sysmsg_resultp = mm.sysmsg_resultp;
595 svr4_sys_fchroot(struct svr4_sys_fchroot_args *uap)
597 struct thread *td = curthread; /* XXX */
598 struct proc *p = td->td_proc;
600 struct filedesc *fdp;
609 if ((error = suser(td)) != 0)
611 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
613 vp = (struct vnode *) fp->f_data;
614 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
615 if (vp->v_type != VDIR || fp->f_ncp == NULL)
618 error = VOP_ACCESS(vp, VEXEC, cred, td);
619 VOP_UNLOCK(vp, 0, td);
623 if (fdp->fd_rdir != NULL) {
625 cache_drop(fdp->fd_nrdir);
628 fdp->fd_nrdir = cache_hold(fp->f_ncp); /* stopgap */
634 svr4_mknod(retval, path, mode, dev)
640 caddr_t sg = stackgap_init();
643 CHECKALTEXIST(&sg, path);
645 if (S_ISFIFO(mode)) {
646 struct mkfifo_args ap;
647 SCARG(&ap, path) = path;
648 SCARG(&ap, mode) = mode;
650 *retval = ap.sysmsg_result;
652 struct mknod_args ap;
653 SCARG(&ap, path) = path;
654 SCARG(&ap, mode) = mode;
655 SCARG(&ap, dev) = dev;
657 *retval = ap.sysmsg_result;
664 svr4_sys_mknod(struct svr4_sys_mknod_args *uap)
668 retval = &uap->sysmsg_result;
670 return svr4_mknod(retval, SCARG(uap, path), SCARG(uap, mode),
671 (svr4_dev_t)svr4_to_bsd_odev_t(SCARG(uap, dev)));
676 svr4_sys_xmknod(struct svr4_sys_xmknod_args *uap)
680 retval = &uap->sysmsg_result;
682 return svr4_mknod(retval, SCARG(uap, path), SCARG(uap, mode),
683 (svr4_dev_t)svr4_to_bsd_dev_t(SCARG(uap, dev)));
688 svr4_sys_vhangup(struct svr4_sys_vhangup_args *uap)
695 svr4_sys_sysconfig(struct svr4_sys_sysconfig_args *uap)
699 retval = &uap->sysmsg_result;
701 switch (SCARG(uap, name)) {
702 case SVR4_CONFIG_UNUSED:
705 case SVR4_CONFIG_NGROUPS:
706 *retval = NGROUPS_MAX;
708 case SVR4_CONFIG_CHILD_MAX:
711 case SVR4_CONFIG_OPEN_FILES:
714 case SVR4_CONFIG_POSIX_VER:
717 case SVR4_CONFIG_PAGESIZE:
720 case SVR4_CONFIG_CLK_TCK:
721 *retval = 60; /* should this be `hz', ie. 100? */
723 case SVR4_CONFIG_XOPEN_VER:
724 *retval = 2; /* XXX: What should that be? */
726 case SVR4_CONFIG_PROF_TCK:
727 *retval = 60; /* XXX: What should that be? */
729 case SVR4_CONFIG_NPROC_CONF:
730 *retval = 1; /* Only one processor for now */
732 case SVR4_CONFIG_NPROC_ONLN:
733 *retval = 1; /* And it better be online */
735 case SVR4_CONFIG_AIO_LISTIO_MAX:
736 case SVR4_CONFIG_AIO_MAX:
737 case SVR4_CONFIG_AIO_PRIO_DELTA_MAX:
738 *retval = 0; /* No aio support */
740 case SVR4_CONFIG_DELAYTIMER_MAX:
741 *retval = 0; /* No delaytimer support */
743 case SVR4_CONFIG_MQ_OPEN_MAX:
744 *retval = msginfo.msgmni;
746 case SVR4_CONFIG_MQ_PRIO_MAX:
747 *retval = 0; /* XXX: Don't know */
749 case SVR4_CONFIG_RTSIG_MAX:
752 case SVR4_CONFIG_SEM_NSEMS_MAX:
753 *retval = seminfo.semmni;
755 case SVR4_CONFIG_SEM_VALUE_MAX:
756 *retval = seminfo.semvmx;
758 case SVR4_CONFIG_SIGQUEUE_MAX:
759 *retval = 0; /* XXX: Don't know */
761 case SVR4_CONFIG_SIGRT_MIN:
762 case SVR4_CONFIG_SIGRT_MAX:
763 *retval = 0; /* No real time signals */
765 case SVR4_CONFIG_TIMER_MAX:
766 *retval = 3; /* XXX: real, virtual, profiling */
769 case SVR4_CONFIG_PHYS_PAGES:
771 *retval = uvmexp.free; /* XXX: free instead of total */
773 *retval = cnt.v_free_count; /* XXX: free instead of total */
776 case SVR4_CONFIG_AVPHYS_PAGES:
778 *retval = uvmexp.active; /* XXX: active instead of avg */
780 *retval = cnt.v_active_count; /* XXX: active instead of avg */
791 extern int swap_pager_full;
795 svr4_sys_break(struct svr4_sys_break_args *uap)
797 struct thread *td = curthread; /* XXX */
798 struct proc *p = td->td_proc;
800 vm_offset_t new, old, base, ns;
806 base = round_page((vm_offset_t) vm->vm_daddr);
807 ns = (vm_offset_t)SCARG(uap, nsize);
808 new = round_page(ns);
810 if ((new - base) > (unsigned) p->p_rlimit[RLIMIT_DATA].rlim_cur) {
813 if (new >= VM_MAXUSER_ADDRESS) {
816 } else if (new < base) {
818 * This is simply an invalid value. If someone wants to
819 * do fancy address space manipulations, mmap and munmap
820 * can do most of what the user would want.
825 old = base + ctob(vm->vm_dsize);
830 if (vm->vm_map.size + diff > p->p_rlimit[RLIMIT_VMEM].rlim_cur)
832 rv = vm_map_find(&vm->vm_map, NULL, 0, &old, diff, FALSE,
833 VM_PROT_ALL, VM_PROT_ALL, 0);
834 if (rv != KERN_SUCCESS) {
837 vm->vm_dsize += btoc(diff);
838 } else if (new < old) {
839 rv = vm_map_remove(&vm->vm_map, new, old);
840 if (rv != KERN_SUCCESS) {
843 vm->vm_dsize -= btoc(old - new);
849 static __inline clock_t
850 timeval_to_clock_t(tv)
853 return tv->tv_sec * hz + tv->tv_usec / (1000000 / hz);
858 svr4_sys_times(struct svr4_sys_times_args *uap)
860 int error, *retval = &uap->sysmsg_result;
865 struct getrusage_args ga;
867 caddr_t sg = stackgap_init();
868 ru = stackgap_alloc(&sg, sizeof(struct rusage));
870 SCARG(&ga, who) = RUSAGE_SELF;
871 SCARG(&ga, rusage) = ru;
873 error = getrusage(&ga);
877 if ((error = copyin(ru, &r, sizeof r)) != 0)
880 tms.tms_utime = timeval_to_clock_t(&r.ru_utime);
881 tms.tms_stime = timeval_to_clock_t(&r.ru_stime);
883 SCARG(&ga, who) = RUSAGE_CHILDREN;
884 error = getrusage(&ga);
888 if ((error = copyin(ru, &r, sizeof r)) != 0)
891 tms.tms_cutime = timeval_to_clock_t(&r.ru_utime);
892 tms.tms_cstime = timeval_to_clock_t(&r.ru_stime);
895 *retval = timeval_to_clock_t(&t);
897 return copyout(&tms, SCARG(uap, tp), sizeof(tms));
902 svr4_sys_ulimit(struct svr4_sys_ulimit_args *uap)
904 struct proc *p = curproc;
905 int *retval = &uap->sysmsg_result;
907 switch (SCARG(uap, cmd)) {
909 *retval = p->p_rlimit[RLIMIT_FSIZE].rlim_cur / 512;
911 *retval = 0x7fffffff;
917 struct __setrlimit_args srl;
919 caddr_t sg = stackgap_init();
920 struct rlimit *url = (struct rlimit *)
921 stackgap_alloc(&sg, sizeof *url);
923 krl.rlim_cur = SCARG(uap, newlimit) * 512;
924 krl.rlim_max = p->p_rlimit[RLIMIT_FSIZE].rlim_max;
926 error = copyout(&krl, url, sizeof(*url));
930 SCARG(&srl, which) = RLIMIT_FSIZE;
931 SCARG(&srl, rlp) = (struct rlimit *)url;
933 error = setrlimit(&srl);
937 *retval = p->p_rlimit[RLIMIT_FSIZE].rlim_cur;
939 *retval = 0x7fffffff;
945 struct vmspace *vm = p->p_vmspace;
946 register_t r = p->p_rlimit[RLIMIT_DATA].rlim_cur;
950 r += (long) vm->vm_daddr;
958 *retval = p->p_rlimit[RLIMIT_NOFILE].rlim_cur;
960 *retval = 0x7fffffff;
974 /* look in the live processes */
975 if ((p = pfind(pid)) != NULL)
978 /* look in the zombies */
979 for (p = zombproc.lh_first; p != 0; p = p->p_list.le_next)
988 svr4_sys_pgrpsys(struct svr4_sys_pgrpsys_args *uap)
990 struct proc *p = curproc;
991 int *retval = &uap->sysmsg_result;
993 switch (SCARG(uap, cmd)) {
994 case 1: /* setpgrp() */
996 * SVR4 setpgrp() (which takes no arguments) has the
997 * semantics that the session ID is also created anew, so
998 * in almost every sense, setpgrp() is identical to
999 * setsid() for SVR4. (Under BSD, the difference is that
1000 * a setpgid(0,0) will not create a new session.)
1003 struct setsid_args sida;
1005 sida.sysmsg_result = 0;
1007 /* ignore result? */
1011 case 0: /* getpgrp() */
1012 *retval = p->p_pgrp->pg_id;
1015 case 2: /* getsid(pid) */
1016 if (SCARG(uap, pid) != 0 &&
1017 (p = svr4_pfind(SCARG(uap, pid))) == NULL)
1020 * This has already been initialized to the pid of
1021 * the session leader.
1023 *retval = (register_t) p->p_session->s_leader->p_pid;
1026 case 3: /* setsid() */
1029 struct setsid_args sida;
1032 sida.sysmsg_result = 0;
1033 error = setsid(&sida);
1034 uap->sysmsg_result = sida.sysmsg_result;
1037 case 4: /* getpgid(pid) */
1039 if (SCARG(uap, pid) != 0 &&
1040 (p = svr4_pfind(SCARG(uap, pid))) == NULL)
1043 *retval = (int) p->p_pgrp->pg_id;
1046 case 5: /* setpgid(pid, pgid); */
1048 struct setpgid_args sa;
1051 SCARG(&sa, pid) = SCARG(uap, pid);
1052 SCARG(&sa, pgid) = SCARG(uap, pgid);
1053 sa.sysmsg_result = 0;
1054 error = setpgid(&sa);
1055 uap->sysmsg_result = sa.sysmsg_result;
1063 #define syscallarg(x) union { x datum; register_t pad; }
1065 struct svr4_hrtcntl_args {
1069 svr4_hrt_interval_t * iv;
1070 svr4_hrt_time_t * ti;
1075 svr4_hrtcntl(struct svr4_hrtcntl_args *uap, register_t *retval)
1077 switch (SCARG(uap, fun)) {
1078 case SVR4_HRT_CNTL_RES:
1079 DPRINTF(("htrcntl(RES)\n"));
1080 *retval = SVR4_HRT_USEC;
1083 case SVR4_HRT_CNTL_TOFD:
1084 DPRINTF(("htrcntl(TOFD)\n"));
1088 if (SCARG(uap, clk) != SVR4_HRT_CLK_STD) {
1089 DPRINTF(("clk == %d\n", SCARG(uap, clk)));
1092 if (SCARG(uap, ti) == NULL) {
1093 DPRINTF(("ti NULL\n"));
1097 t.h_sec = tv.tv_sec;
1098 t.h_rem = tv.tv_usec;
1099 t.h_res = SVR4_HRT_USEC;
1100 return copyout(&t, SCARG(uap, ti), sizeof(t));
1103 case SVR4_HRT_CNTL_START:
1104 DPRINTF(("htrcntl(START)\n"));
1107 case SVR4_HRT_CNTL_GET:
1108 DPRINTF(("htrcntl(GET)\n"));
1111 DPRINTF(("Bad htrcntl command %d\n", SCARG(uap, fun)));
1118 svr4_sys_hrtsys(struct svr4_sys_hrtsys_args *uap)
1120 int *retval = &uap->sysmsg_result;
1122 switch (SCARG(uap, cmd)) {
1124 return svr4_hrtcntl((struct svr4_hrtcntl_args *)(&uap->sysmsg + 1), retval);
1127 DPRINTF(("hrtalarm\n"));
1131 DPRINTF(("hrtsleep\n"));
1135 DPRINTF(("hrtcancel\n"));
1139 DPRINTF(("Bad hrtsys command %d\n", SCARG(uap, cmd)));
1146 svr4_setinfo(p, st, s)
1154 memset(&i, 0, sizeof(i));
1156 i.si_signo = SVR4_SIGCHLD;
1157 i.si_errno = 0; /* XXX? */
1160 i.si_pid = p->p_pid;
1161 if (p->p_stat == SZOMB) {
1162 i.si_stime = p->p_ru->ru_stime.tv_sec;
1163 i.si_utime = p->p_ru->ru_utime.tv_sec;
1166 i.si_stime = p->p_stats->p_ru.ru_stime.tv_sec;
1167 i.si_utime = p->p_stats->p_ru.ru_utime.tv_sec;
1171 if (WIFEXITED(st)) {
1172 i.si_status = WEXITSTATUS(st);
1173 i.si_code = SVR4_CLD_EXITED;
1174 } else if (WIFSTOPPED(st)) {
1176 if (sig >= 0 && sig < NSIG)
1177 i.si_status = SVR4_BSD2SVR4_SIG(sig);
1179 if (i.si_status == SVR4_SIGCONT)
1180 i.si_code = SVR4_CLD_CONTINUED;
1182 i.si_code = SVR4_CLD_STOPPED;
1185 if (sig >= 0 && sig < NSIG)
1186 i.si_status = SVR4_BSD2SVR4_SIG(sig);
1189 i.si_code = SVR4_CLD_DUMPED;
1191 i.si_code = SVR4_CLD_KILLED;
1194 DPRINTF(("siginfo [pid %ld signo %d code %d errno %d status %d]\n",
1195 i.si_pid, i.si_signo, i.si_code, i.si_errno, i.si_status));
1197 return copyout(&i, s, sizeof(i));
1202 svr4_sys_waitsys(struct svr4_sys_waitsys_args *uap)
1204 struct proc *p = curproc;
1206 int error, *retval = &uap->sysmsg_result;
1210 switch (SCARG(uap, grp)) {
1215 SCARG(uap, id) = -p->p_pgid;
1219 SCARG(uap, id) = WAIT_ANY;
1226 DPRINTF(("waitsys(%d, %d, %p, %x)\n",
1227 SCARG(uap, grp), SCARG(uap, id),
1228 SCARG(uap, info), SCARG(uap, options)));
1232 for (q = p->p_children.lh_first; q != 0; q = q->p_sibling.le_next) {
1233 if (SCARG(uap, id) != WAIT_ANY &&
1234 q->p_pid != SCARG(uap, id) &&
1235 q->p_pgid != -SCARG(uap, id)) {
1236 DPRINTF(("pid %d pgid %d != %d\n", q->p_pid,
1237 q->p_pgid, SCARG(uap, id)));
1241 if (q->p_stat == SZOMB &&
1242 ((SCARG(uap, options) & (SVR4_WEXITED|SVR4_WTRAPPED)))) {
1244 DPRINTF(("found %d\n", q->p_pid));
1245 if ((error = svr4_setinfo(q, q->p_xstat,
1246 SCARG(uap, info))) != 0)
1250 if ((SCARG(uap, options) & SVR4_WNOWAIT)) {
1251 DPRINTF(("Don't wait\n"));
1256 * If we got the child via ptrace(2) or procfs, and
1257 * the parent is different (meaning the process was
1258 * attached, rather than run as a child), then we need
1259 * to give it back to the ol dparent, and send the
1260 * parent a SIGCHLD. The rest of the cleanup will be
1261 * done when the old parent waits on the child.
1263 if ((q->p_flag & P_TRACED) &&
1264 q->p_oppid != q->p_pptr->p_pid) {
1265 t = pfind(q->p_oppid);
1266 proc_reparent(q, t ? t : initproc);
1268 q->p_flag &= ~(P_TRACED | P_WAITED);
1269 wakeup((caddr_t)q->p_pptr);
1273 ruadd(&p->p_stats->p_cru, q->p_ru);
1275 FREE(q->p_ru, M_ZOMBIE);
1278 * Finally finished with old proc entry.
1279 * Unlink it from its process group and free it.
1283 LIST_REMOVE(q, p_list); /* off zombproc */
1285 LIST_REMOVE(q, p_sibling);
1288 * Decrement the count of procs running with this uid.
1290 (void)chgproccnt(q->p_ucred->cr_uidinfo, -1, 0);
1293 * Free up credentials.
1299 * Release reference to text vnode
1305 * Give machine-dependent layer a chance
1306 * to free anything that cpu_exit couldn't
1307 * release while still running in process context.
1310 /* XXX what about process 'q' itself? zfree? */
1311 #if defined(__NetBSD__)
1312 pool_put(&proc_pool, q);
1317 if ((q->p_flag & P_STOPPED) && (q->p_flag & P_WAITED) == 0 &&
1318 (q->p_flag & P_TRACED ||
1319 (SCARG(uap, options) & (SVR4_WSTOPPED|SVR4_WCONTINUED)))) {
1320 DPRINTF(("jobcontrol %d\n", q->p_pid));
1321 if (((SCARG(uap, options) & SVR4_WNOWAIT)) == 0)
1322 q->p_flag |= P_WAITED;
1324 return svr4_setinfo(q, W_STOPCODE(q->p_xstat),
1332 if (SCARG(uap, options) & SVR4_WNOHANG) {
1334 if ((error = svr4_setinfo(NULL, 0, SCARG(uap, info))) != 0)
1339 if ((error = tsleep((caddr_t)p, PCATCH, "svr4_wait", 0)) != 0)
1346 bsd_statfs_to_svr4_statvfs(const struct statfs *bfs, struct svr4_statvfs *sfs,
1349 sfs->f_bsize = bfs->f_iosize; /* XXX */
1350 sfs->f_frsize = bfs->f_bsize;
1351 sfs->f_blocks = bfs->f_blocks;
1352 sfs->f_bfree = bfs->f_bfree;
1353 sfs->f_bavail = bfs->f_bavail;
1354 sfs->f_files = bfs->f_files;
1355 sfs->f_ffree = bfs->f_ffree;
1356 sfs->f_favail = bfs->f_ffree;
1357 sfs->f_fsid = bfs->f_fsid.val[0];
1358 memcpy(sfs->f_basetype, bfs->f_fstypename, sizeof(sfs->f_basetype));
1360 if (bfs->f_flags & MNT_RDONLY)
1361 sfs->f_flag |= SVR4_ST_RDONLY;
1362 if (bfs->f_flags & MNT_NOSUID)
1363 sfs->f_flag |= SVR4_ST_NOSUID;
1364 sfs->f_namemax = namelen;
1365 memcpy(sfs->f_fstr, bfs->f_fstypename, sizeof(sfs->f_fstr)); /* XXX */
1366 memset(sfs->f_filler, 0, sizeof(sfs->f_filler));
1371 bsd_statfs_to_svr4_statvfs64(const struct statfs *bfs,
1372 struct svr4_statvfs64 *sfs, int namelen)
1374 sfs->f_bsize = bfs->f_iosize; /* XXX */
1375 sfs->f_frsize = bfs->f_bsize;
1376 sfs->f_blocks = bfs->f_blocks;
1377 sfs->f_bfree = bfs->f_bfree;
1378 sfs->f_bavail = bfs->f_bavail;
1379 sfs->f_files = bfs->f_files;
1380 sfs->f_ffree = bfs->f_ffree;
1381 sfs->f_favail = bfs->f_ffree;
1382 sfs->f_fsid = bfs->f_fsid.val[0];
1383 memcpy(sfs->f_basetype, bfs->f_fstypename, sizeof(sfs->f_basetype));
1385 if (bfs->f_flags & MNT_RDONLY)
1386 sfs->f_flag |= SVR4_ST_RDONLY;
1387 if (bfs->f_flags & MNT_NOSUID)
1388 sfs->f_flag |= SVR4_ST_NOSUID;
1389 sfs->f_namemax = namelen;
1390 memcpy(sfs->f_fstr, bfs->f_fstypename, sizeof(sfs->f_fstr)); /* XXX */
1391 memset(sfs->f_filler, 0, sizeof(sfs->f_filler));
1396 svr4_sys_statvfs(struct svr4_sys_statvfs_args *uap)
1398 struct nlookupdata nd;
1399 struct svr4_statvfs sfs;
1403 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1405 error = kern_statfs(&nd, &bfs);
1407 if (nd.nl_ncp->nc_vp != NULL)
1408 error = vn_get_namelen(nd.nl_ncp->nc_vp, &namelen);
1414 bsd_statfs_to_svr4_statvfs(&bfs, &sfs, namelen);
1415 error = copyout(&sfs, uap->fs, sizeof(*uap->fs));
1423 svr4_sys_fstatvfs(struct svr4_sys_fstatvfs_args *uap)
1425 struct svr4_statvfs sfs;
1427 struct proc *p = curthread->td_proc;
1431 error = kern_fstatfs(uap->fd, &bfs);
1434 error = getvnode(p->p_fd, uap->fd, &fp);
1436 error = vn_get_namelen((struct vnode *)fp->f_data, &namelen);
1438 bsd_statfs_to_svr4_statvfs(&bfs, &sfs, namelen);
1439 error = copyout(&bfs, uap->fs, sizeof(*uap->fs));
1447 svr4_sys_statvfs64(struct svr4_sys_statvfs64_args *uap)
1449 struct nlookupdata nd;
1450 struct svr4_statvfs64 sfs;
1454 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1456 error = kern_statfs(&nd, &bfs);
1458 if (nd.nl_ncp->nc_vp != NULL)
1459 error = vn_get_namelen(nd.nl_ncp->nc_vp, &namelen);
1465 bsd_statfs_to_svr4_statvfs64(&bfs, &sfs, namelen);
1466 error = copyout(&sfs, uap->fs, sizeof(*uap->fs));
1474 svr4_sys_fstatvfs64(struct svr4_sys_fstatvfs64_args *uap)
1476 struct svr4_statvfs64 sfs;
1478 struct proc *p = curthread->td_proc;
1482 error = kern_fstatfs(uap->fd, &bfs);
1485 error = getvnode(p->p_fd, uap->fd, &fp);
1487 error = vn_get_namelen((struct vnode *)fp->f_data, &namelen);
1489 bsd_statfs_to_svr4_statvfs64(&bfs, &sfs, namelen);
1490 error = copyout(&bfs, uap->fs, sizeof(*uap->fs));
1497 svr4_sys_alarm(struct svr4_sys_alarm_args *uap)
1500 struct itimerval *itp, *oitp;
1501 struct setitimer_args sa;
1502 caddr_t sg = stackgap_init();
1504 itp = stackgap_alloc(&sg, sizeof(*itp));
1505 oitp = stackgap_alloc(&sg, sizeof(*oitp));
1506 timevalclear(&itp->it_interval);
1507 itp->it_value.tv_sec = SCARG(uap, sec);
1508 itp->it_value.tv_usec = 0;
1510 SCARG(&sa, which) = ITIMER_REAL;
1511 SCARG(&sa, itv) = itp;
1512 SCARG(&sa, oitv) = oitp;
1513 error = setitimer(&sa);
1516 if (oitp->it_value.tv_usec)
1517 oitp->it_value.tv_sec++;
1518 uap->sysmsg_result = oitp->it_value.tv_sec;
1524 svr4_sys_gettimeofday(struct svr4_sys_gettimeofday_args *uap)
1526 if (SCARG(uap, tp)) {
1530 return copyout(&atv, SCARG(uap, tp), sizeof (atv));
1537 svr4_sys_facl(struct svr4_sys_facl_args *uap)
1541 retval = &uap->sysmsg_result;
1544 switch (SCARG(uap, cmd)) {
1545 case SVR4_SYS_SETACL:
1546 /* We don't support acls on any filesystem */
1549 case SVR4_SYS_GETACL:
1550 return copyout(retval, &SCARG(uap, num),
1551 sizeof(SCARG(uap, num)));
1553 case SVR4_SYS_GETACLCNT:
1563 svr4_sys_acl(struct svr4_sys_acl_args *uap)
1565 /* XXX: for now the same */
1566 return svr4_sys_facl((struct svr4_sys_facl_args *)uap);
1570 svr4_sys_auditsys(struct svr4_sys_auditsys_args *uap)
1573 * XXX: Big brother is *not* watching.
1579 svr4_sys_memcntl(struct svr4_sys_memcntl_args *uap)
1581 switch (SCARG(uap, cmd)) {
1584 struct msync_args msa;
1587 SCARG(&msa, addr) = SCARG(uap, addr);
1588 SCARG(&msa, len) = SCARG(uap, len);
1589 SCARG(&msa, flags) = (int)SCARG(uap, arg);
1591 msa.sysmsg_result = 0;
1592 error = msync(&msa);
1593 uap->sysmsg_result = msa.sysmsg_result;
1596 case SVR4_MC_ADVISE:
1598 struct madvise_args maa;
1601 SCARG(&maa, addr) = SCARG(uap, addr);
1602 SCARG(&maa, len) = SCARG(uap, len);
1603 SCARG(&maa, behav) = (int)SCARG(uap, arg);
1605 maa.sysmsg_result = 0;
1606 error = madvise(&maa);
1607 uap->sysmsg_result = maa.sysmsg_result;
1611 case SVR4_MC_UNLOCK:
1612 case SVR4_MC_LOCKAS:
1613 case SVR4_MC_UNLOCKAS:
1622 svr4_sys_nice(struct svr4_sys_nice_args *uap)
1624 struct setpriority_args ap;
1627 SCARG(&ap, which) = PRIO_PROCESS;
1628 SCARG(&ap, who) = 0;
1629 SCARG(&ap, prio) = SCARG(uap, prio);
1631 if ((error = setpriority(&ap)) != 0)
1634 /* the cast is stupid, but the structures are the same */
1635 if ((error = getpriority((struct getpriority_args *)&ap)) != 0)
1638 uap->sysmsg_result = ap.sysmsg_result;
1644 svr4_sys_resolvepath(struct svr4_sys_resolvepath_args *uap)
1646 struct nlookupdata nd;
1650 retval = &uap->sysmsg_result;
1652 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, 0);
1654 error = nlookup(&nd);
1658 if ((error = copyout(nd.nl_path, SCARG(uap, buf),
1659 SCARG(uap, bufsiz))) != 0) {
1663 *retval = strlen(nd.nl_path) < SCARG(uap, bufsiz) ?
1664 strlen(nd.nl_path) + 1 : SCARG(uap, bufsiz);