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.32 2005/12/10 16:06:20 swildner 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(register_t *retval, char *path, svr4_mode_t mode,
637 caddr_t sg = stackgap_init();
640 CHECKALTEXIST(&sg, path);
642 if (S_ISFIFO(mode)) {
643 struct mkfifo_args ap;
644 SCARG(&ap, path) = path;
645 SCARG(&ap, mode) = mode;
647 *retval = ap.sysmsg_result;
649 struct mknod_args ap;
650 SCARG(&ap, path) = path;
651 SCARG(&ap, mode) = mode;
652 SCARG(&ap, dev) = dev;
654 *retval = ap.sysmsg_result;
661 svr4_sys_mknod(struct svr4_sys_mknod_args *uap)
665 retval = &uap->sysmsg_result;
667 return svr4_mknod(retval, SCARG(uap, path), SCARG(uap, mode),
668 (svr4_dev_t)svr4_to_bsd_odev_t(SCARG(uap, dev)));
673 svr4_sys_xmknod(struct svr4_sys_xmknod_args *uap)
677 retval = &uap->sysmsg_result;
679 return svr4_mknod(retval, SCARG(uap, path), SCARG(uap, mode),
680 (svr4_dev_t)svr4_to_bsd_dev_t(SCARG(uap, dev)));
685 svr4_sys_vhangup(struct svr4_sys_vhangup_args *uap)
692 svr4_sys_sysconfig(struct svr4_sys_sysconfig_args *uap)
696 retval = &uap->sysmsg_result;
698 switch (SCARG(uap, name)) {
699 case SVR4_CONFIG_UNUSED:
702 case SVR4_CONFIG_NGROUPS:
703 *retval = NGROUPS_MAX;
705 case SVR4_CONFIG_CHILD_MAX:
708 case SVR4_CONFIG_OPEN_FILES:
711 case SVR4_CONFIG_POSIX_VER:
714 case SVR4_CONFIG_PAGESIZE:
717 case SVR4_CONFIG_CLK_TCK:
718 *retval = 60; /* should this be `hz', ie. 100? */
720 case SVR4_CONFIG_XOPEN_VER:
721 *retval = 2; /* XXX: What should that be? */
723 case SVR4_CONFIG_PROF_TCK:
724 *retval = 60; /* XXX: What should that be? */
726 case SVR4_CONFIG_NPROC_CONF:
727 *retval = 1; /* Only one processor for now */
729 case SVR4_CONFIG_NPROC_ONLN:
730 *retval = 1; /* And it better be online */
732 case SVR4_CONFIG_AIO_LISTIO_MAX:
733 case SVR4_CONFIG_AIO_MAX:
734 case SVR4_CONFIG_AIO_PRIO_DELTA_MAX:
735 *retval = 0; /* No aio support */
737 case SVR4_CONFIG_DELAYTIMER_MAX:
738 *retval = 0; /* No delaytimer support */
740 case SVR4_CONFIG_MQ_OPEN_MAX:
741 *retval = msginfo.msgmni;
743 case SVR4_CONFIG_MQ_PRIO_MAX:
744 *retval = 0; /* XXX: Don't know */
746 case SVR4_CONFIG_RTSIG_MAX:
749 case SVR4_CONFIG_SEM_NSEMS_MAX:
750 *retval = seminfo.semmni;
752 case SVR4_CONFIG_SEM_VALUE_MAX:
753 *retval = seminfo.semvmx;
755 case SVR4_CONFIG_SIGQUEUE_MAX:
756 *retval = 0; /* XXX: Don't know */
758 case SVR4_CONFIG_SIGRT_MIN:
759 case SVR4_CONFIG_SIGRT_MAX:
760 *retval = 0; /* No real time signals */
762 case SVR4_CONFIG_TIMER_MAX:
763 *retval = 3; /* XXX: real, virtual, profiling */
766 case SVR4_CONFIG_PHYS_PAGES:
768 *retval = uvmexp.free; /* XXX: free instead of total */
770 *retval = cnt.v_free_count; /* XXX: free instead of total */
773 case SVR4_CONFIG_AVPHYS_PAGES:
775 *retval = uvmexp.active; /* XXX: active instead of avg */
777 *retval = cnt.v_active_count; /* XXX: active instead of avg */
788 extern int swap_pager_full;
792 svr4_sys_break(struct svr4_sys_break_args *uap)
794 struct thread *td = curthread; /* XXX */
795 struct proc *p = td->td_proc;
797 vm_offset_t new, old, base, ns;
803 base = round_page((vm_offset_t) vm->vm_daddr);
804 ns = (vm_offset_t)SCARG(uap, nsize);
805 new = round_page(ns);
807 if ((new - base) > (unsigned) p->p_rlimit[RLIMIT_DATA].rlim_cur) {
810 if (new >= VM_MAXUSER_ADDRESS) {
813 } else if (new < base) {
815 * This is simply an invalid value. If someone wants to
816 * do fancy address space manipulations, mmap and munmap
817 * can do most of what the user would want.
822 old = base + ctob(vm->vm_dsize);
827 if (vm->vm_map.size + diff > p->p_rlimit[RLIMIT_VMEM].rlim_cur)
829 rv = vm_map_find(&vm->vm_map, NULL, 0, &old, diff, FALSE,
830 VM_PROT_ALL, VM_PROT_ALL, 0);
831 if (rv != KERN_SUCCESS) {
834 vm->vm_dsize += btoc(diff);
835 } else if (new < old) {
836 rv = vm_map_remove(&vm->vm_map, new, old);
837 if (rv != KERN_SUCCESS) {
840 vm->vm_dsize -= btoc(old - new);
846 static __inline clock_t
847 timeval_to_clock_t(struct timeval *tv)
849 return tv->tv_sec * hz + tv->tv_usec / (1000000 / hz);
854 svr4_sys_times(struct svr4_sys_times_args *uap)
856 int error, *retval = &uap->sysmsg_result;
861 struct getrusage_args ga;
863 caddr_t sg = stackgap_init();
864 ru = stackgap_alloc(&sg, sizeof(struct rusage));
866 SCARG(&ga, who) = RUSAGE_SELF;
867 SCARG(&ga, rusage) = ru;
869 error = getrusage(&ga);
873 if ((error = copyin(ru, &r, sizeof r)) != 0)
876 tms.tms_utime = timeval_to_clock_t(&r.ru_utime);
877 tms.tms_stime = timeval_to_clock_t(&r.ru_stime);
879 SCARG(&ga, who) = RUSAGE_CHILDREN;
880 error = getrusage(&ga);
884 if ((error = copyin(ru, &r, sizeof r)) != 0)
887 tms.tms_cutime = timeval_to_clock_t(&r.ru_utime);
888 tms.tms_cstime = timeval_to_clock_t(&r.ru_stime);
891 *retval = timeval_to_clock_t(&t);
893 return copyout(&tms, SCARG(uap, tp), sizeof(tms));
898 svr4_sys_ulimit(struct svr4_sys_ulimit_args *uap)
900 struct proc *p = curproc;
901 int *retval = &uap->sysmsg_result;
903 switch (SCARG(uap, cmd)) {
905 *retval = p->p_rlimit[RLIMIT_FSIZE].rlim_cur / 512;
907 *retval = 0x7fffffff;
913 struct __setrlimit_args srl;
915 caddr_t sg = stackgap_init();
916 struct rlimit *url = (struct rlimit *)
917 stackgap_alloc(&sg, sizeof *url);
919 krl.rlim_cur = SCARG(uap, newlimit) * 512;
920 krl.rlim_max = p->p_rlimit[RLIMIT_FSIZE].rlim_max;
922 error = copyout(&krl, url, sizeof(*url));
926 SCARG(&srl, which) = RLIMIT_FSIZE;
927 SCARG(&srl, rlp) = (struct rlimit *)url;
929 error = setrlimit(&srl);
933 *retval = p->p_rlimit[RLIMIT_FSIZE].rlim_cur;
935 *retval = 0x7fffffff;
941 struct vmspace *vm = p->p_vmspace;
942 register_t r = p->p_rlimit[RLIMIT_DATA].rlim_cur;
946 r += (long) vm->vm_daddr;
954 *retval = p->p_rlimit[RLIMIT_NOFILE].rlim_cur;
956 *retval = 0x7fffffff;
965 svr4_pfind(pid_t pid)
969 /* look in the live processes */
970 if ((p = pfind(pid)) != NULL)
973 /* look in the zombies */
974 for (p = zombproc.lh_first; p != 0; p = p->p_list.le_next)
983 svr4_sys_pgrpsys(struct svr4_sys_pgrpsys_args *uap)
985 struct proc *p = curproc;
986 int *retval = &uap->sysmsg_result;
988 switch (SCARG(uap, cmd)) {
989 case 1: /* setpgrp() */
991 * SVR4 setpgrp() (which takes no arguments) has the
992 * semantics that the session ID is also created anew, so
993 * in almost every sense, setpgrp() is identical to
994 * setsid() for SVR4. (Under BSD, the difference is that
995 * a setpgid(0,0) will not create a new session.)
998 struct setsid_args sida;
1000 sida.sysmsg_result = 0;
1002 /* ignore result? */
1006 case 0: /* getpgrp() */
1007 *retval = p->p_pgrp->pg_id;
1010 case 2: /* getsid(pid) */
1011 if (SCARG(uap, pid) != 0 &&
1012 (p = svr4_pfind(SCARG(uap, pid))) == NULL)
1015 * This has already been initialized to the pid of
1016 * the session leader.
1018 *retval = (register_t) p->p_session->s_leader->p_pid;
1021 case 3: /* setsid() */
1024 struct setsid_args sida;
1027 sida.sysmsg_result = 0;
1028 error = setsid(&sida);
1029 uap->sysmsg_result = sida.sysmsg_result;
1032 case 4: /* getpgid(pid) */
1034 if (SCARG(uap, pid) != 0 &&
1035 (p = svr4_pfind(SCARG(uap, pid))) == NULL)
1038 *retval = (int) p->p_pgrp->pg_id;
1041 case 5: /* setpgid(pid, pgid); */
1043 struct setpgid_args sa;
1046 SCARG(&sa, pid) = SCARG(uap, pid);
1047 SCARG(&sa, pgid) = SCARG(uap, pgid);
1048 sa.sysmsg_result = 0;
1049 error = setpgid(&sa);
1050 uap->sysmsg_result = sa.sysmsg_result;
1058 #define syscallarg(x) union { x datum; register_t pad; }
1060 struct svr4_hrtcntl_args {
1064 svr4_hrt_interval_t * iv;
1065 svr4_hrt_time_t * ti;
1070 svr4_hrtcntl(struct svr4_hrtcntl_args *uap, register_t *retval)
1072 switch (SCARG(uap, fun)) {
1073 case SVR4_HRT_CNTL_RES:
1074 DPRINTF(("htrcntl(RES)\n"));
1075 *retval = SVR4_HRT_USEC;
1078 case SVR4_HRT_CNTL_TOFD:
1079 DPRINTF(("htrcntl(TOFD)\n"));
1083 if (SCARG(uap, clk) != SVR4_HRT_CLK_STD) {
1084 DPRINTF(("clk == %d\n", SCARG(uap, clk)));
1087 if (SCARG(uap, ti) == NULL) {
1088 DPRINTF(("ti NULL\n"));
1092 t.h_sec = tv.tv_sec;
1093 t.h_rem = tv.tv_usec;
1094 t.h_res = SVR4_HRT_USEC;
1095 return copyout(&t, SCARG(uap, ti), sizeof(t));
1098 case SVR4_HRT_CNTL_START:
1099 DPRINTF(("htrcntl(START)\n"));
1102 case SVR4_HRT_CNTL_GET:
1103 DPRINTF(("htrcntl(GET)\n"));
1106 DPRINTF(("Bad htrcntl command %d\n", SCARG(uap, fun)));
1113 svr4_sys_hrtsys(struct svr4_sys_hrtsys_args *uap)
1115 int *retval = &uap->sysmsg_result;
1117 switch (SCARG(uap, cmd)) {
1119 return svr4_hrtcntl((struct svr4_hrtcntl_args *)(&uap->sysmsg + 1), retval);
1122 DPRINTF(("hrtalarm\n"));
1126 DPRINTF(("hrtsleep\n"));
1130 DPRINTF(("hrtcancel\n"));
1134 DPRINTF(("Bad hrtsys command %d\n", SCARG(uap, cmd)));
1141 svr4_setinfo(struct proc *p, int st, svr4_siginfo_t *s)
1146 memset(&i, 0, sizeof(i));
1148 i.si_signo = SVR4_SIGCHLD;
1149 i.si_errno = 0; /* XXX? */
1152 i.si_pid = p->p_pid;
1153 if (p->p_flag & P_ZOMBIE) {
1154 i.si_stime = p->p_ru->ru_stime.tv_sec;
1155 i.si_utime = p->p_ru->ru_utime.tv_sec;
1157 i.si_stime = p->p_stats->p_ru.ru_stime.tv_sec;
1158 i.si_utime = p->p_stats->p_ru.ru_utime.tv_sec;
1162 if (WIFEXITED(st)) {
1163 i.si_status = WEXITSTATUS(st);
1164 i.si_code = SVR4_CLD_EXITED;
1165 } else if (WIFSTOPPED(st)) {
1167 if (sig >= 0 && sig < NSIG)
1168 i.si_status = SVR4_BSD2SVR4_SIG(sig);
1170 if (i.si_status == SVR4_SIGCONT)
1171 i.si_code = SVR4_CLD_CONTINUED;
1173 i.si_code = SVR4_CLD_STOPPED;
1176 if (sig >= 0 && sig < NSIG)
1177 i.si_status = SVR4_BSD2SVR4_SIG(sig);
1180 i.si_code = SVR4_CLD_DUMPED;
1182 i.si_code = SVR4_CLD_KILLED;
1185 DPRINTF(("siginfo [pid %ld signo %d code %d errno %d status %d]\n",
1186 i.si_pid, i.si_signo, i.si_code, i.si_errno, i.si_status));
1188 return copyout(&i, s, sizeof(i));
1193 svr4_sys_waitsys(struct svr4_sys_waitsys_args *uap)
1195 struct proc *p = curproc;
1197 int error, *retval = &uap->sysmsg_result;
1201 switch (SCARG(uap, grp)) {
1206 SCARG(uap, id) = -p->p_pgid;
1210 SCARG(uap, id) = WAIT_ANY;
1217 DPRINTF(("waitsys(%d, %d, %p, %x)\n",
1218 SCARG(uap, grp), SCARG(uap, id),
1219 SCARG(uap, info), SCARG(uap, options)));
1223 for (q = p->p_children.lh_first; q != 0; q = q->p_sibling.le_next) {
1224 if (SCARG(uap, id) != WAIT_ANY &&
1225 q->p_pid != SCARG(uap, id) &&
1226 q->p_pgid != -SCARG(uap, id)) {
1227 DPRINTF(("pid %d pgid %d != %d\n", q->p_pid,
1228 q->p_pgid, SCARG(uap, id)));
1232 if ((q->p_flag & P_ZOMBIE) &&
1233 ((SCARG(uap, options) & (SVR4_WEXITED|SVR4_WTRAPPED)))) {
1235 DPRINTF(("found %d\n", q->p_pid));
1236 if ((error = svr4_setinfo(q, q->p_xstat,
1237 SCARG(uap, info))) != 0)
1241 if ((SCARG(uap, options) & SVR4_WNOWAIT)) {
1242 DPRINTF(("Don't wait\n"));
1247 * If we got the child via ptrace(2) or procfs, and
1248 * the parent is different (meaning the process was
1249 * attached, rather than run as a child), then we need
1250 * to give it back to the ol dparent, and send the
1251 * parent a SIGCHLD. The rest of the cleanup will be
1252 * done when the old parent waits on the child.
1254 if ((q->p_flag & P_TRACED) &&
1255 q->p_oppid != q->p_pptr->p_pid) {
1256 t = pfind(q->p_oppid);
1257 proc_reparent(q, t ? t : initproc);
1259 q->p_flag &= ~(P_TRACED | P_WAITED);
1260 wakeup((caddr_t)q->p_pptr);
1264 ruadd(&p->p_stats->p_cru, q->p_ru);
1266 FREE(q->p_ru, M_ZOMBIE);
1269 * Finally finished with old proc entry.
1270 * Unlink it from its process group and free it.
1274 LIST_REMOVE(q, p_list); /* off zombproc */
1276 LIST_REMOVE(q, p_sibling);
1279 * Decrement the count of procs running with this uid.
1281 (void)chgproccnt(q->p_ucred->cr_uidinfo, -1, 0);
1284 * Free up credentials.
1290 * Release reference to text vnode
1296 * Give machine-dependent layer a chance
1297 * to free anything that cpu_exit couldn't
1298 * release while still running in process context.
1301 /* XXX what about process 'q' itself? zfree? */
1302 #if defined(__NetBSD__)
1303 pool_put(&proc_pool, q);
1308 if ((q->p_flag & P_STOPPED) && (q->p_flag & P_WAITED) == 0 &&
1309 (q->p_flag & P_TRACED ||
1310 (SCARG(uap, options) & (SVR4_WSTOPPED|SVR4_WCONTINUED)))) {
1311 DPRINTF(("jobcontrol %d\n", q->p_pid));
1312 if (((SCARG(uap, options) & SVR4_WNOWAIT)) == 0)
1313 q->p_flag |= P_WAITED;
1315 return svr4_setinfo(q, W_STOPCODE(q->p_xstat),
1323 if (SCARG(uap, options) & SVR4_WNOHANG) {
1325 if ((error = svr4_setinfo(NULL, 0, SCARG(uap, info))) != 0)
1330 if ((error = tsleep((caddr_t)p, PCATCH, "svr4_wait", 0)) != 0)
1337 bsd_statfs_to_svr4_statvfs(const struct statfs *bfs, struct svr4_statvfs *sfs,
1340 sfs->f_bsize = bfs->f_iosize; /* XXX */
1341 sfs->f_frsize = bfs->f_bsize;
1342 sfs->f_blocks = bfs->f_blocks;
1343 sfs->f_bfree = bfs->f_bfree;
1344 sfs->f_bavail = bfs->f_bavail;
1345 sfs->f_files = bfs->f_files;
1346 sfs->f_ffree = bfs->f_ffree;
1347 sfs->f_favail = bfs->f_ffree;
1348 sfs->f_fsid = bfs->f_fsid.val[0];
1349 memcpy(sfs->f_basetype, bfs->f_fstypename, sizeof(sfs->f_basetype));
1351 if (bfs->f_flags & MNT_RDONLY)
1352 sfs->f_flag |= SVR4_ST_RDONLY;
1353 if (bfs->f_flags & MNT_NOSUID)
1354 sfs->f_flag |= SVR4_ST_NOSUID;
1355 sfs->f_namemax = namelen;
1356 memcpy(sfs->f_fstr, bfs->f_fstypename, sizeof(sfs->f_fstr)); /* XXX */
1357 memset(sfs->f_filler, 0, sizeof(sfs->f_filler));
1362 bsd_statfs_to_svr4_statvfs64(const struct statfs *bfs,
1363 struct svr4_statvfs64 *sfs, int namelen)
1365 sfs->f_bsize = bfs->f_iosize; /* XXX */
1366 sfs->f_frsize = bfs->f_bsize;
1367 sfs->f_blocks = bfs->f_blocks;
1368 sfs->f_bfree = bfs->f_bfree;
1369 sfs->f_bavail = bfs->f_bavail;
1370 sfs->f_files = bfs->f_files;
1371 sfs->f_ffree = bfs->f_ffree;
1372 sfs->f_favail = bfs->f_ffree;
1373 sfs->f_fsid = bfs->f_fsid.val[0];
1374 memcpy(sfs->f_basetype, bfs->f_fstypename, sizeof(sfs->f_basetype));
1376 if (bfs->f_flags & MNT_RDONLY)
1377 sfs->f_flag |= SVR4_ST_RDONLY;
1378 if (bfs->f_flags & MNT_NOSUID)
1379 sfs->f_flag |= SVR4_ST_NOSUID;
1380 sfs->f_namemax = namelen;
1381 memcpy(sfs->f_fstr, bfs->f_fstypename, sizeof(sfs->f_fstr)); /* XXX */
1382 memset(sfs->f_filler, 0, sizeof(sfs->f_filler));
1387 svr4_sys_statvfs(struct svr4_sys_statvfs_args *uap)
1389 struct nlookupdata nd;
1390 struct svr4_statvfs sfs;
1394 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1396 error = kern_statfs(&nd, &bfs);
1398 if (nd.nl_ncp->nc_vp != NULL)
1399 error = vn_get_namelen(nd.nl_ncp->nc_vp, &namelen);
1405 bsd_statfs_to_svr4_statvfs(&bfs, &sfs, namelen);
1406 error = copyout(&sfs, uap->fs, sizeof(*uap->fs));
1414 svr4_sys_fstatvfs(struct svr4_sys_fstatvfs_args *uap)
1416 struct svr4_statvfs sfs;
1418 struct proc *p = curthread->td_proc;
1422 error = kern_fstatfs(uap->fd, &bfs);
1425 error = getvnode(p->p_fd, uap->fd, &fp);
1427 error = vn_get_namelen((struct vnode *)fp->f_data, &namelen);
1429 bsd_statfs_to_svr4_statvfs(&bfs, &sfs, namelen);
1430 error = copyout(&bfs, uap->fs, sizeof(*uap->fs));
1438 svr4_sys_statvfs64(struct svr4_sys_statvfs64_args *uap)
1440 struct nlookupdata nd;
1441 struct svr4_statvfs64 sfs;
1445 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1447 error = kern_statfs(&nd, &bfs);
1449 if (nd.nl_ncp->nc_vp != NULL)
1450 error = vn_get_namelen(nd.nl_ncp->nc_vp, &namelen);
1456 bsd_statfs_to_svr4_statvfs64(&bfs, &sfs, namelen);
1457 error = copyout(&sfs, uap->fs, sizeof(*uap->fs));
1465 svr4_sys_fstatvfs64(struct svr4_sys_fstatvfs64_args *uap)
1467 struct svr4_statvfs64 sfs;
1469 struct proc *p = curthread->td_proc;
1473 error = kern_fstatfs(uap->fd, &bfs);
1476 error = getvnode(p->p_fd, uap->fd, &fp);
1478 error = vn_get_namelen((struct vnode *)fp->f_data, &namelen);
1480 bsd_statfs_to_svr4_statvfs64(&bfs, &sfs, namelen);
1481 error = copyout(&bfs, uap->fs, sizeof(*uap->fs));
1488 svr4_sys_alarm(struct svr4_sys_alarm_args *uap)
1491 struct itimerval *itp, *oitp;
1492 struct setitimer_args sa;
1493 caddr_t sg = stackgap_init();
1495 itp = stackgap_alloc(&sg, sizeof(*itp));
1496 oitp = stackgap_alloc(&sg, sizeof(*oitp));
1497 timevalclear(&itp->it_interval);
1498 itp->it_value.tv_sec = SCARG(uap, sec);
1499 itp->it_value.tv_usec = 0;
1501 SCARG(&sa, which) = ITIMER_REAL;
1502 SCARG(&sa, itv) = itp;
1503 SCARG(&sa, oitv) = oitp;
1504 error = setitimer(&sa);
1507 if (oitp->it_value.tv_usec)
1508 oitp->it_value.tv_sec++;
1509 uap->sysmsg_result = oitp->it_value.tv_sec;
1515 svr4_sys_gettimeofday(struct svr4_sys_gettimeofday_args *uap)
1517 if (SCARG(uap, tp)) {
1521 return copyout(&atv, SCARG(uap, tp), sizeof (atv));
1528 svr4_sys_facl(struct svr4_sys_facl_args *uap)
1532 retval = &uap->sysmsg_result;
1535 switch (SCARG(uap, cmd)) {
1536 case SVR4_SYS_SETACL:
1537 /* We don't support acls on any filesystem */
1540 case SVR4_SYS_GETACL:
1541 return copyout(retval, &SCARG(uap, num),
1542 sizeof(SCARG(uap, num)));
1544 case SVR4_SYS_GETACLCNT:
1554 svr4_sys_acl(struct svr4_sys_acl_args *uap)
1556 /* XXX: for now the same */
1557 return svr4_sys_facl((struct svr4_sys_facl_args *)uap);
1561 svr4_sys_auditsys(struct svr4_sys_auditsys_args *uap)
1564 * XXX: Big brother is *not* watching.
1570 svr4_sys_memcntl(struct svr4_sys_memcntl_args *uap)
1572 switch (SCARG(uap, cmd)) {
1575 struct msync_args msa;
1578 SCARG(&msa, addr) = SCARG(uap, addr);
1579 SCARG(&msa, len) = SCARG(uap, len);
1580 SCARG(&msa, flags) = (int)SCARG(uap, arg);
1582 msa.sysmsg_result = 0;
1583 error = msync(&msa);
1584 uap->sysmsg_result = msa.sysmsg_result;
1587 case SVR4_MC_ADVISE:
1589 struct madvise_args maa;
1592 SCARG(&maa, addr) = SCARG(uap, addr);
1593 SCARG(&maa, len) = SCARG(uap, len);
1594 SCARG(&maa, behav) = (int)SCARG(uap, arg);
1596 maa.sysmsg_result = 0;
1597 error = madvise(&maa);
1598 uap->sysmsg_result = maa.sysmsg_result;
1602 case SVR4_MC_UNLOCK:
1603 case SVR4_MC_LOCKAS:
1604 case SVR4_MC_UNLOCKAS:
1613 svr4_sys_nice(struct svr4_sys_nice_args *uap)
1615 struct setpriority_args ap;
1618 SCARG(&ap, which) = PRIO_PROCESS;
1619 SCARG(&ap, who) = 0;
1620 SCARG(&ap, prio) = SCARG(uap, prio);
1622 if ((error = setpriority(&ap)) != 0)
1625 /* the cast is stupid, but the structures are the same */
1626 if ((error = getpriority((struct getpriority_args *)&ap)) != 0)
1629 uap->sysmsg_result = ap.sysmsg_result;
1635 svr4_sys_resolvepath(struct svr4_sys_resolvepath_args *uap)
1637 struct nlookupdata nd;
1641 retval = &uap->sysmsg_result;
1643 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, 0);
1645 error = nlookup(&nd);
1649 if ((error = copyout(nd.nl_path, SCARG(uap, buf),
1650 SCARG(uap, bufsiz))) != 0) {
1654 *retval = strlen(nd.nl_path) < SCARG(uap, bufsiz) ?
1655 strlen(nd.nl_path) + 1 : SCARG(uap, bufsiz);