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.26 2004/11/12 00:09:22 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/malloc.h>
44 #include <sys/nlookup.h>
49 #include <sys/filedesc.h>
50 #include <sys/kernel.h>
52 #include <sys/mount.h>
53 #include <sys/resource.h>
54 #include <sys/resourcevar.h>
55 #include <sys/vnode.h>
58 #include <sys/times.h>
59 #include <sys/fcntl.h>
62 #include <sys/ptrace.h>
63 #include <vm/vm_zone.h>
65 #include <sys/sysproto.h>
68 #include "svr4_types.h"
69 #include "svr4_signal.h"
70 #include "svr4_proto.h"
71 #include "svr4_util.h"
72 #include "svr4_sysconfig.h"
73 #include "svr4_dirent.h"
75 #include "svr4_ulimit.h"
76 #include "svr4_statvfs.h"
78 #include "svr4_mman.h"
79 #include "svr4_wait.h"
81 #include <machine/vmparam.h>
83 #include <vm/vm_param.h>
84 #include <vm/vm_map.h>
86 #if defined(__DragonFly__) || defined(__FreeBSD__)
87 #include <vm/vm_extern.h>
92 # include <uvm/uvm_extern.h>
96 #define BSD_DIRENT(cp) ((struct dirent *)(cp))
98 static int svr4_mknod (register_t *, char *, svr4_mode_t, svr4_dev_t);
100 static __inline clock_t timeval_to_clock_t (struct timeval *);
101 static int svr4_setinfo (struct proc *, int, svr4_siginfo_t *);
103 struct svr4_hrtcntl_args;
104 static int svr4_hrtcntl (struct svr4_hrtcntl_args *, register_t *);
105 static void bsd_statfs_to_svr4_statvfs (const struct statfs *,
106 struct svr4_statvfs *);
107 static void bsd_statfs_to_svr4_statvfs64 (const struct statfs *,
108 struct svr4_statvfs64 *);
109 static struct proc *svr4_pfind (pid_t pid);
114 svr4_sys_setitimer(struct svr4_sys_setitimer_args *uap)
116 uap->sysmsg_result = 0;
122 svr4_sys_wait(struct svr4_sys_wait_args *uap)
125 int error, *retval = &uap->sysmsg_result, st, sig;
126 size_t sz = sizeof(*SCARG(&w4, status));
128 SCARG(&w4, rusage) = NULL;
129 SCARG(&w4, options) = 0;
131 if (SCARG(uap, status) == NULL) {
132 caddr_t sg = stackgap_init();
134 SCARG(&w4, status) = stackgap_alloc(&sg, sz);
137 SCARG(&w4, status) = SCARG(uap, status);
139 SCARG(&w4, pid) = WAIT_ANY;
140 w4.sysmsg_result = 0;
141 if ((error = wait4(&w4)) != 0)
143 *retval = w4.sysmsg_result;
145 if ((error = copyin(SCARG(&w4, status), &st, sizeof(st))) != 0)
148 if (WIFSIGNALED(st)) {
150 if (sig >= 0 && sig < NSIG)
151 st = (st & ~0177) | SVR4_BSD2SVR4_SIG(sig);
152 } else if (WIFSTOPPED(st)) {
154 if (sig >= 0 && sig < NSIG)
155 st = (st & ~0xff00) | (SVR4_BSD2SVR4_SIG(sig) << 8);
159 * It looks like wait(2) on svr4/solaris/2.4 returns
160 * the status in retval[1], and the pid on retval[0].
164 if (SCARG(uap, status))
165 if ((error = copyout(&st, SCARG(uap, status), sizeof(st))) != 0)
172 svr4_sys_execv(struct svr4_sys_execv_args *uap)
174 struct execve_args ap;
178 sg = stackgap_init();
179 CHECKALTEXIST(&sg, uap->path);
181 /* note: parts of result64 may be maintained or cleared by execve */
182 ap.sysmsg_result64 = uap->sysmsg_result64;
183 ap.fname = uap->path;
187 uap->sysmsg_result64 = ap.sysmsg_result64;
192 svr4_sys_execve(struct svr4_sys_execve_args *uap)
194 struct execve_args ap;
198 sg = stackgap_init();
199 CHECKALTEXIST(&sg, uap->path);
201 SCARG(&ap, fname) = SCARG(uap, path);
202 SCARG(&ap, argv) = SCARG(uap, argp);
203 SCARG(&ap, envv) = SCARG(uap, envp);
205 ap.sysmsg_result = 0;
207 uap->sysmsg_result = ap.sysmsg_result;
212 svr4_sys_time(struct svr4_sys_time_args *v)
214 struct svr4_sys_time_args *uap = v;
220 error = copyout(&tv.tv_sec, SCARG(uap, t),
221 sizeof(*(SCARG(uap, t))));
222 v->sysmsg_result = (int) tv.tv_sec;
229 * Read SVR4-style directory entries. We suck them into kernel space so
230 * that they can be massaged before being copied out to user code.
232 * This code is ported from the Linux emulator: Changes to the VFS interface
233 * between FreeBSD and NetBSD have made it simpler to port it from there than
234 * to adapt the NetBSD version.
237 svr4_sys_getdents64(struct svr4_sys_getdents64_args *uap)
239 struct thread *td = curthread; /* XXX */
240 struct proc *p = td->td_proc;
243 caddr_t inp, buf; /* BSD-format */
244 int len, reclen; /* BSD-format */
245 caddr_t outp; /* SVR4-format */
246 int resid, svr4reclen=0; /* SVR4-format */
252 struct svr4_dirent64 svr4_dirent;
253 int buflen, error, eofflag, nbytes, justone;
254 u_long *cookies = NULL, *cookiep;
259 DPRINTF(("svr4_sys_getdents64(%d, *, %d)\n", uap->fd, uap->nbytes));
260 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0) {
264 if ((fp->f_flag & FREAD) == 0)
267 vp = (struct vnode *) fp->f_data;
269 if (vp->v_type != VDIR)
272 if ((error = VOP_GETATTR(vp, &va, td))) {
276 nbytes = SCARG(uap, nbytes);
278 nbytes = sizeof (struct svr4_dirent64);
285 #define DIRBLKSIZ 512 /* XXX we used to use ufs's DIRBLKSIZ */
286 buflen = max(DIRBLKSIZ, nbytes);
287 buflen = min(buflen, MAXBSIZE);
288 buf = malloc(buflen, M_TEMP, M_WAITOK);
289 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
292 aiov.iov_len = buflen;
293 auio.uio_iov = &aiov;
295 auio.uio_rw = UIO_READ;
296 auio.uio_segflg = UIO_SYSSPACE;
298 auio.uio_resid = buflen;
299 auio.uio_offset = off;
302 free(cookies, M_TEMP);
306 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
307 &ncookies, &cookies);
313 outp = (caddr_t) SCARG(uap, dp);
315 if ((len = buflen - auio.uio_resid) <= 0) {
323 * When using cookies, the vfs has the option of reading from
324 * a different offset than that supplied (UFS truncates the
325 * offset to a block boundary to make sure that it never reads
326 * partway through a directory entry, even if the directory
327 * has been compacted).
329 while (len > 0 && ncookies > 0 && *cookiep <= off) {
330 bdp = (struct dirent *) inp;
331 len -= bdp->d_reclen;
332 inp += bdp->d_reclen;
339 if (cookiep && ncookies == 0)
341 bdp = (struct dirent *) inp;
342 reclen = bdp->d_reclen;
344 DPRINTF(("svr4_readdir: reclen=%d\n", reclen));
349 if (bdp->d_fileno == 0) {
359 svr4reclen = SVR4_RECLEN(&svr4_dirent, bdp->d_namlen);
360 if (reclen > len || resid < svr4reclen) {
364 svr4_dirent.d_ino = (long) bdp->d_fileno;
367 * old svr4-style readdir usage.
369 svr4_dirent.d_off = (svr4_off_t) svr4reclen;
370 svr4_dirent.d_reclen = (u_short) bdp->d_namlen;
372 svr4_dirent.d_off = (svr4_off_t)(off + reclen);
373 svr4_dirent.d_reclen = (u_short) svr4reclen;
375 strcpy(svr4_dirent.d_name, bdp->d_name);
376 if ((error = copyout((caddr_t)&svr4_dirent, outp, svr4reclen)))
391 if (outp == (caddr_t) SCARG(uap, dp))
396 nbytes = resid + svr4reclen;
399 uap->sysmsg_result = nbytes - resid;
402 free(cookies, M_TEMP);
403 VOP_UNLOCK(vp, 0, td);
410 svr4_sys_getdents(struct svr4_sys_getdents_args *uap)
412 struct thread *td = curthread;
413 struct proc *p = td->td_proc;
416 caddr_t inp, buf; /* BSD-format */
417 int len, reclen; /* BSD-format */
418 caddr_t outp; /* SVR4-format */
419 int resid, svr4_reclen; /* SVR4-format */
423 struct svr4_dirent idb;
424 off_t off; /* true file offset */
425 int buflen, error, eofflag;
426 u_long *cookiebuf = NULL, *cookie;
427 int ncookies = 0, *retval = &uap->sysmsg_result;
431 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
434 if ((fp->f_flag & FREAD) == 0)
437 vp = (struct vnode *)fp->f_data;
438 if (vp->v_type != VDIR)
441 buflen = min(MAXBSIZE, SCARG(uap, nbytes));
442 buf = malloc(buflen, M_TEMP, M_WAITOK);
443 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
447 aiov.iov_len = buflen;
448 auio.uio_iov = &aiov;
450 auio.uio_rw = UIO_READ;
451 auio.uio_segflg = UIO_SYSSPACE;
453 auio.uio_resid = buflen;
454 auio.uio_offset = off;
456 * First we read into the malloc'ed buffer, then
457 * we massage it into user space, one record at a time.
459 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies,
465 outp = SCARG(uap, buf);
466 resid = SCARG(uap, nbytes);
467 if ((len = buflen - auio.uio_resid) == 0)
470 for (cookie = cookiebuf; len > 0; len -= reclen) {
471 bdp = (struct dirent *)inp;
472 reclen = bdp->d_reclen;
474 panic("svr4_sys_getdents64: bad reclen");
475 off = *cookie++; /* each entry points to the next */
476 if ((off >> 32) != 0) {
477 uprintf("svr4_sys_getdents64: dir offset too large for emulated program");
481 if (bdp->d_fileno == 0) {
482 inp += reclen; /* it is a hole; squish it out */
485 svr4_reclen = SVR4_RECLEN(&idb, bdp->d_namlen);
486 if (reclen > len || resid < svr4_reclen) {
487 /* entry too big for buffer, so just stop */
492 * Massage in place to make a SVR4-shaped dirent (otherwise
493 * we have to worry about touching user memory outside of
494 * the copyout() call).
496 idb.d_ino = (svr4_ino_t)bdp->d_fileno;
497 idb.d_off = (svr4_off_t)off;
498 idb.d_reclen = (u_short)svr4_reclen;
499 strcpy(idb.d_name, bdp->d_name);
500 if ((error = copyout((caddr_t)&idb, outp, svr4_reclen)))
502 /* advance past this real entry */
504 /* advance output past SVR4-shaped entry */
506 resid -= svr4_reclen;
509 /* if we squished out the whole block, try again */
510 if (outp == SCARG(uap, buf))
512 fp->f_offset = off; /* update the vnode offset */
515 *retval = SCARG(uap, nbytes) - resid;
517 VOP_UNLOCK(vp, 0, td);
519 free(cookiebuf, M_TEMP);
526 svr4_sys_mmap(struct svr4_sys_mmap_args *uap)
532 retval = &uap->sysmsg_result;
533 #define _MAP_NEW 0x80000000
535 * Verify the arguments.
537 if (SCARG(uap, prot) & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
538 return EINVAL; /* XXX still needed? */
540 if (SCARG(uap, len) == 0)
543 SCARG(&mm, prot) = SCARG(uap, prot);
544 SCARG(&mm, len) = SCARG(uap, len);
545 SCARG(&mm, flags) = SCARG(uap, flags) & ~_MAP_NEW;
546 SCARG(&mm, fd) = SCARG(uap, fd);
547 SCARG(&mm, addr) = SCARG(uap, addr);
548 SCARG(&mm, pos) = SCARG(uap, pos);
549 mm.sysmsg_resultp = NULL;
551 uap->sysmsg_resultp = mm.sysmsg_resultp;
556 svr4_sys_mmap64(struct svr4_sys_mmap64_args *uap)
558 struct proc *p = curproc;
563 #define _MAP_NEW 0x80000000
565 * Verify the arguments.
567 if (SCARG(uap, prot) & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
568 return EINVAL; /* XXX still needed? */
570 if (SCARG(uap, len) == 0)
573 SCARG(&mm, prot) = SCARG(uap, prot);
574 SCARG(&mm, len) = SCARG(uap, len);
575 SCARG(&mm, flags) = SCARG(uap, flags) & ~_MAP_NEW;
576 SCARG(&mm, fd) = SCARG(uap, fd);
577 SCARG(&mm, addr) = SCARG(uap, addr);
578 SCARG(&mm, pos) = SCARG(uap, pos);
580 rp = (void *) round_page((vm_offset_t)(p->p_vmspace->vm_daddr + maxdsiz));
581 if ((SCARG(&mm, flags) & MAP_FIXED) == 0 &&
582 SCARG(&mm, addr) != 0 && (void *)SCARG(&mm, addr) < rp)
583 SCARG(&mm, addr) = rp;
585 mm.sysmsg_resultp = 0;
587 uap->sysmsg_resultp = mm.sysmsg_resultp;
593 svr4_sys_fchroot(struct svr4_sys_fchroot_args *uap)
595 struct thread *td = curthread; /* XXX */
596 struct proc *p = td->td_proc;
598 struct filedesc *fdp;
607 if ((error = suser(td)) != 0)
609 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
611 vp = (struct vnode *) fp->f_data;
612 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
613 if (vp->v_type != VDIR || fp->f_ncp == NULL)
616 error = VOP_ACCESS(vp, VEXEC, cred, td);
617 VOP_UNLOCK(vp, 0, td);
621 if (fdp->fd_rdir != NULL) {
623 cache_drop(fdp->fd_nrdir);
626 fdp->fd_nrdir = cache_hold(fp->f_ncp); /* stopgap */
632 svr4_mknod(retval, path, mode, dev)
638 caddr_t sg = stackgap_init();
641 CHECKALTEXIST(&sg, path);
643 if (S_ISFIFO(mode)) {
644 struct mkfifo_args ap;
645 SCARG(&ap, path) = path;
646 SCARG(&ap, mode) = mode;
648 *retval = ap.sysmsg_result;
650 struct mknod_args ap;
651 SCARG(&ap, path) = path;
652 SCARG(&ap, mode) = mode;
653 SCARG(&ap, dev) = dev;
655 *retval = ap.sysmsg_result;
662 svr4_sys_mknod(struct svr4_sys_mknod_args *uap)
666 retval = &uap->sysmsg_result;
668 return svr4_mknod(retval, SCARG(uap, path), SCARG(uap, mode),
669 (svr4_dev_t)svr4_to_bsd_odev_t(SCARG(uap, dev)));
674 svr4_sys_xmknod(struct svr4_sys_xmknod_args *uap)
678 retval = &uap->sysmsg_result;
680 return svr4_mknod(retval, SCARG(uap, path), SCARG(uap, mode),
681 (svr4_dev_t)svr4_to_bsd_dev_t(SCARG(uap, dev)));
686 svr4_sys_vhangup(struct svr4_sys_vhangup_args *uap)
693 svr4_sys_sysconfig(struct svr4_sys_sysconfig_args *uap)
697 retval = &uap->sysmsg_result;
699 switch (SCARG(uap, name)) {
700 case SVR4_CONFIG_UNUSED:
703 case SVR4_CONFIG_NGROUPS:
704 *retval = NGROUPS_MAX;
706 case SVR4_CONFIG_CHILD_MAX:
709 case SVR4_CONFIG_OPEN_FILES:
712 case SVR4_CONFIG_POSIX_VER:
715 case SVR4_CONFIG_PAGESIZE:
718 case SVR4_CONFIG_CLK_TCK:
719 *retval = 60; /* should this be `hz', ie. 100? */
721 case SVR4_CONFIG_XOPEN_VER:
722 *retval = 2; /* XXX: What should that be? */
724 case SVR4_CONFIG_PROF_TCK:
725 *retval = 60; /* XXX: What should that be? */
727 case SVR4_CONFIG_NPROC_CONF:
728 *retval = 1; /* Only one processor for now */
730 case SVR4_CONFIG_NPROC_ONLN:
731 *retval = 1; /* And it better be online */
733 case SVR4_CONFIG_AIO_LISTIO_MAX:
734 case SVR4_CONFIG_AIO_MAX:
735 case SVR4_CONFIG_AIO_PRIO_DELTA_MAX:
736 *retval = 0; /* No aio support */
738 case SVR4_CONFIG_DELAYTIMER_MAX:
739 *retval = 0; /* No delaytimer support */
741 case SVR4_CONFIG_MQ_OPEN_MAX:
742 *retval = msginfo.msgmni;
744 case SVR4_CONFIG_MQ_PRIO_MAX:
745 *retval = 0; /* XXX: Don't know */
747 case SVR4_CONFIG_RTSIG_MAX:
750 case SVR4_CONFIG_SEM_NSEMS_MAX:
751 *retval = seminfo.semmni;
753 case SVR4_CONFIG_SEM_VALUE_MAX:
754 *retval = seminfo.semvmx;
756 case SVR4_CONFIG_SIGQUEUE_MAX:
757 *retval = 0; /* XXX: Don't know */
759 case SVR4_CONFIG_SIGRT_MIN:
760 case SVR4_CONFIG_SIGRT_MAX:
761 *retval = 0; /* No real time signals */
763 case SVR4_CONFIG_TIMER_MAX:
764 *retval = 3; /* XXX: real, virtual, profiling */
767 case SVR4_CONFIG_PHYS_PAGES:
769 *retval = uvmexp.free; /* XXX: free instead of total */
771 *retval = cnt.v_free_count; /* XXX: free instead of total */
774 case SVR4_CONFIG_AVPHYS_PAGES:
776 *retval = uvmexp.active; /* XXX: active instead of avg */
778 *retval = cnt.v_active_count; /* XXX: active instead of avg */
789 extern int swap_pager_full;
793 svr4_sys_break(struct svr4_sys_break_args *uap)
795 struct thread *td = curthread; /* XXX */
796 struct proc *p = td->td_proc;
798 vm_offset_t new, old, base, ns;
804 base = round_page((vm_offset_t) vm->vm_daddr);
805 ns = (vm_offset_t)SCARG(uap, nsize);
806 new = round_page(ns);
808 if ((new - base) > (unsigned) p->p_rlimit[RLIMIT_DATA].rlim_cur) {
811 if (new >= VM_MAXUSER_ADDRESS) {
814 } else if (new < base) {
816 * This is simply an invalid value. If someone wants to
817 * do fancy address space manipulations, mmap and munmap
818 * can do most of what the user would want.
823 old = base + ctob(vm->vm_dsize);
828 if (vm->vm_map.size + diff > p->p_rlimit[RLIMIT_VMEM].rlim_cur)
830 rv = vm_map_find(&vm->vm_map, NULL, 0, &old, diff, FALSE,
831 VM_PROT_ALL, VM_PROT_ALL, 0);
832 if (rv != KERN_SUCCESS) {
835 vm->vm_dsize += btoc(diff);
836 } else if (new < old) {
837 rv = vm_map_remove(&vm->vm_map, new, old);
838 if (rv != KERN_SUCCESS) {
841 vm->vm_dsize -= btoc(old - new);
847 static __inline clock_t
848 timeval_to_clock_t(tv)
851 return tv->tv_sec * hz + tv->tv_usec / (1000000 / hz);
856 svr4_sys_times(struct svr4_sys_times_args *uap)
858 int error, *retval = &uap->sysmsg_result;
863 struct getrusage_args ga;
865 caddr_t sg = stackgap_init();
866 ru = stackgap_alloc(&sg, sizeof(struct rusage));
868 SCARG(&ga, who) = RUSAGE_SELF;
869 SCARG(&ga, rusage) = ru;
871 error = getrusage(&ga);
875 if ((error = copyin(ru, &r, sizeof r)) != 0)
878 tms.tms_utime = timeval_to_clock_t(&r.ru_utime);
879 tms.tms_stime = timeval_to_clock_t(&r.ru_stime);
881 SCARG(&ga, who) = RUSAGE_CHILDREN;
882 error = getrusage(&ga);
886 if ((error = copyin(ru, &r, sizeof r)) != 0)
889 tms.tms_cutime = timeval_to_clock_t(&r.ru_utime);
890 tms.tms_cstime = timeval_to_clock_t(&r.ru_stime);
893 *retval = timeval_to_clock_t(&t);
895 return copyout(&tms, SCARG(uap, tp), sizeof(tms));
900 svr4_sys_ulimit(struct svr4_sys_ulimit_args *uap)
902 struct proc *p = curproc;
903 int *retval = &uap->sysmsg_result;
905 switch (SCARG(uap, cmd)) {
907 *retval = p->p_rlimit[RLIMIT_FSIZE].rlim_cur / 512;
909 *retval = 0x7fffffff;
915 struct __setrlimit_args srl;
917 caddr_t sg = stackgap_init();
918 struct rlimit *url = (struct rlimit *)
919 stackgap_alloc(&sg, sizeof *url);
921 krl.rlim_cur = SCARG(uap, newlimit) * 512;
922 krl.rlim_max = p->p_rlimit[RLIMIT_FSIZE].rlim_max;
924 error = copyout(&krl, url, sizeof(*url));
928 SCARG(&srl, which) = RLIMIT_FSIZE;
929 SCARG(&srl, rlp) = (struct rlimit *)url;
931 error = setrlimit(&srl);
935 *retval = p->p_rlimit[RLIMIT_FSIZE].rlim_cur;
937 *retval = 0x7fffffff;
943 struct vmspace *vm = p->p_vmspace;
944 register_t r = p->p_rlimit[RLIMIT_DATA].rlim_cur;
948 r += (long) vm->vm_daddr;
956 *retval = p->p_rlimit[RLIMIT_NOFILE].rlim_cur;
958 *retval = 0x7fffffff;
972 /* look in the live processes */
973 if ((p = pfind(pid)) != NULL)
976 /* look in the zombies */
977 for (p = zombproc.lh_first; p != 0; p = p->p_list.le_next)
986 svr4_sys_pgrpsys(struct svr4_sys_pgrpsys_args *uap)
988 struct proc *p = curproc;
989 int *retval = &uap->sysmsg_result;
991 switch (SCARG(uap, cmd)) {
992 case 1: /* setpgrp() */
994 * SVR4 setpgrp() (which takes no arguments) has the
995 * semantics that the session ID is also created anew, so
996 * in almost every sense, setpgrp() is identical to
997 * setsid() for SVR4. (Under BSD, the difference is that
998 * a setpgid(0,0) will not create a new session.)
1001 struct setsid_args sida;
1003 sida.sysmsg_result = 0;
1005 /* ignore result? */
1009 case 0: /* getpgrp() */
1010 *retval = p->p_pgrp->pg_id;
1013 case 2: /* getsid(pid) */
1014 if (SCARG(uap, pid) != 0 &&
1015 (p = svr4_pfind(SCARG(uap, pid))) == NULL)
1018 * This has already been initialized to the pid of
1019 * the session leader.
1021 *retval = (register_t) p->p_session->s_leader->p_pid;
1024 case 3: /* setsid() */
1027 struct setsid_args sida;
1030 sida.sysmsg_result = 0;
1031 error = setsid(&sida);
1032 uap->sysmsg_result = sida.sysmsg_result;
1035 case 4: /* getpgid(pid) */
1037 if (SCARG(uap, pid) != 0 &&
1038 (p = svr4_pfind(SCARG(uap, pid))) == NULL)
1041 *retval = (int) p->p_pgrp->pg_id;
1044 case 5: /* setpgid(pid, pgid); */
1046 struct setpgid_args sa;
1049 SCARG(&sa, pid) = SCARG(uap, pid);
1050 SCARG(&sa, pgid) = SCARG(uap, pgid);
1051 sa.sysmsg_result = 0;
1052 error = setpgid(&sa);
1053 uap->sysmsg_result = sa.sysmsg_result;
1061 #define syscallarg(x) union { x datum; register_t pad; }
1063 struct svr4_hrtcntl_args {
1067 svr4_hrt_interval_t * iv;
1068 svr4_hrt_time_t * ti;
1073 svr4_hrtcntl(struct svr4_hrtcntl_args *uap, register_t *retval)
1075 switch (SCARG(uap, fun)) {
1076 case SVR4_HRT_CNTL_RES:
1077 DPRINTF(("htrcntl(RES)\n"));
1078 *retval = SVR4_HRT_USEC;
1081 case SVR4_HRT_CNTL_TOFD:
1082 DPRINTF(("htrcntl(TOFD)\n"));
1086 if (SCARG(uap, clk) != SVR4_HRT_CLK_STD) {
1087 DPRINTF(("clk == %d\n", SCARG(uap, clk)));
1090 if (SCARG(uap, ti) == NULL) {
1091 DPRINTF(("ti NULL\n"));
1095 t.h_sec = tv.tv_sec;
1096 t.h_rem = tv.tv_usec;
1097 t.h_res = SVR4_HRT_USEC;
1098 return copyout(&t, SCARG(uap, ti), sizeof(t));
1101 case SVR4_HRT_CNTL_START:
1102 DPRINTF(("htrcntl(START)\n"));
1105 case SVR4_HRT_CNTL_GET:
1106 DPRINTF(("htrcntl(GET)\n"));
1109 DPRINTF(("Bad htrcntl command %d\n", SCARG(uap, fun)));
1116 svr4_sys_hrtsys(struct svr4_sys_hrtsys_args *uap)
1118 int *retval = &uap->sysmsg_result;
1120 switch (SCARG(uap, cmd)) {
1122 return svr4_hrtcntl((struct svr4_hrtcntl_args *)(&uap->sysmsg + 1), retval);
1125 DPRINTF(("hrtalarm\n"));
1129 DPRINTF(("hrtsleep\n"));
1133 DPRINTF(("hrtcancel\n"));
1137 DPRINTF(("Bad hrtsys command %d\n", SCARG(uap, cmd)));
1144 svr4_setinfo(p, st, s)
1152 memset(&i, 0, sizeof(i));
1154 i.si_signo = SVR4_SIGCHLD;
1155 i.si_errno = 0; /* XXX? */
1158 i.si_pid = p->p_pid;
1159 if (p->p_stat == SZOMB) {
1160 i.si_stime = p->p_ru->ru_stime.tv_sec;
1161 i.si_utime = p->p_ru->ru_utime.tv_sec;
1164 i.si_stime = p->p_stats->p_ru.ru_stime.tv_sec;
1165 i.si_utime = p->p_stats->p_ru.ru_utime.tv_sec;
1169 if (WIFEXITED(st)) {
1170 i.si_status = WEXITSTATUS(st);
1171 i.si_code = SVR4_CLD_EXITED;
1172 } else if (WIFSTOPPED(st)) {
1174 if (sig >= 0 && sig < NSIG)
1175 i.si_status = SVR4_BSD2SVR4_SIG(sig);
1177 if (i.si_status == SVR4_SIGCONT)
1178 i.si_code = SVR4_CLD_CONTINUED;
1180 i.si_code = SVR4_CLD_STOPPED;
1183 if (sig >= 0 && sig < NSIG)
1184 i.si_status = SVR4_BSD2SVR4_SIG(sig);
1187 i.si_code = SVR4_CLD_DUMPED;
1189 i.si_code = SVR4_CLD_KILLED;
1192 DPRINTF(("siginfo [pid %ld signo %d code %d errno %d status %d]\n",
1193 i.si_pid, i.si_signo, i.si_code, i.si_errno, i.si_status));
1195 return copyout(&i, s, sizeof(i));
1200 svr4_sys_waitsys(struct svr4_sys_waitsys_args *uap)
1202 struct proc *p = curproc;
1204 int error, *retval = &uap->sysmsg_result;
1208 switch (SCARG(uap, grp)) {
1213 SCARG(uap, id) = -p->p_pgid;
1217 SCARG(uap, id) = WAIT_ANY;
1224 DPRINTF(("waitsys(%d, %d, %p, %x)\n",
1225 SCARG(uap, grp), SCARG(uap, id),
1226 SCARG(uap, info), SCARG(uap, options)));
1230 for (q = p->p_children.lh_first; q != 0; q = q->p_sibling.le_next) {
1231 if (SCARG(uap, id) != WAIT_ANY &&
1232 q->p_pid != SCARG(uap, id) &&
1233 q->p_pgid != -SCARG(uap, id)) {
1234 DPRINTF(("pid %d pgid %d != %d\n", q->p_pid,
1235 q->p_pgid, SCARG(uap, id)));
1239 if (q->p_stat == SZOMB &&
1240 ((SCARG(uap, options) & (SVR4_WEXITED|SVR4_WTRAPPED)))) {
1242 DPRINTF(("found %d\n", q->p_pid));
1243 if ((error = svr4_setinfo(q, q->p_xstat,
1244 SCARG(uap, info))) != 0)
1248 if ((SCARG(uap, options) & SVR4_WNOWAIT)) {
1249 DPRINTF(("Don't wait\n"));
1254 * If we got the child via ptrace(2) or procfs, and
1255 * the parent is different (meaning the process was
1256 * attached, rather than run as a child), then we need
1257 * to give it back to the ol dparent, and send the
1258 * parent a SIGCHLD. The rest of the cleanup will be
1259 * done when the old parent waits on the child.
1261 if ((q->p_flag & P_TRACED) &&
1262 q->p_oppid != q->p_pptr->p_pid) {
1263 t = pfind(q->p_oppid);
1264 proc_reparent(q, t ? t : initproc);
1266 q->p_flag &= ~(P_TRACED | P_WAITED);
1267 wakeup((caddr_t)q->p_pptr);
1271 ruadd(&p->p_stats->p_cru, q->p_ru);
1273 FREE(q->p_ru, M_ZOMBIE);
1276 * Finally finished with old proc entry.
1277 * Unlink it from its process group and free it.
1281 LIST_REMOVE(q, p_list); /* off zombproc */
1283 LIST_REMOVE(q, p_sibling);
1286 * Decrement the count of procs running with this uid.
1288 (void)chgproccnt(q->p_ucred->cr_uidinfo, -1, 0);
1291 * Free up credentials.
1297 * Release reference to text vnode
1303 * Give machine-dependent layer a chance
1304 * to free anything that cpu_exit couldn't
1305 * release while still running in process context.
1308 /* XXX what about process 'q' itself? zfree? */
1309 #if defined(__NetBSD__)
1310 pool_put(&proc_pool, q);
1315 if (q->p_stat == SSTOP && (q->p_flag & P_WAITED) == 0 &&
1316 (q->p_flag & P_TRACED ||
1317 (SCARG(uap, options) & (SVR4_WSTOPPED|SVR4_WCONTINUED)))) {
1318 DPRINTF(("jobcontrol %d\n", q->p_pid));
1319 if (((SCARG(uap, options) & SVR4_WNOWAIT)) == 0)
1320 q->p_flag |= P_WAITED;
1322 return svr4_setinfo(q, W_STOPCODE(q->p_xstat),
1330 if (SCARG(uap, options) & SVR4_WNOHANG) {
1332 if ((error = svr4_setinfo(NULL, 0, SCARG(uap, info))) != 0)
1337 if ((error = tsleep((caddr_t)p, PCATCH, "svr4_wait", 0)) != 0)
1344 bsd_statfs_to_svr4_statvfs(bfs, sfs)
1345 const struct statfs *bfs;
1346 struct svr4_statvfs *sfs;
1348 sfs->f_bsize = bfs->f_iosize; /* XXX */
1349 sfs->f_frsize = bfs->f_bsize;
1350 sfs->f_blocks = bfs->f_blocks;
1351 sfs->f_bfree = bfs->f_bfree;
1352 sfs->f_bavail = bfs->f_bavail;
1353 sfs->f_files = bfs->f_files;
1354 sfs->f_ffree = bfs->f_ffree;
1355 sfs->f_favail = bfs->f_ffree;
1356 sfs->f_fsid = bfs->f_fsid.val[0];
1357 memcpy(sfs->f_basetype, bfs->f_fstypename, sizeof(sfs->f_basetype));
1359 if (bfs->f_flags & MNT_RDONLY)
1360 sfs->f_flag |= SVR4_ST_RDONLY;
1361 if (bfs->f_flags & MNT_NOSUID)
1362 sfs->f_flag |= SVR4_ST_NOSUID;
1363 sfs->f_namemax = MAXNAMLEN;
1364 memcpy(sfs->f_fstr, bfs->f_fstypename, sizeof(sfs->f_fstr)); /* XXX */
1365 memset(sfs->f_filler, 0, sizeof(sfs->f_filler));
1370 bsd_statfs_to_svr4_statvfs64(bfs, sfs)
1371 const struct statfs *bfs;
1372 struct svr4_statvfs64 *sfs;
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 = MAXNAMLEN;
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 statfs_args fs_args;
1399 caddr_t sg = stackgap_init();
1400 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1402 struct svr4_statvfs sfs;
1405 CHECKALTEXIST(&sg, SCARG(uap, path));
1406 SCARG(&fs_args, path) = SCARG(uap, path);
1407 SCARG(&fs_args, buf) = fs;
1409 if ((error = statfs(&fs_args)) != 0)
1412 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1415 uap->sysmsg_result = fs_args.sysmsg_result;
1417 bsd_statfs_to_svr4_statvfs(&bfs, &sfs);
1419 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1424 svr4_sys_fstatvfs(struct svr4_sys_fstatvfs_args *uap)
1426 struct fstatfs_args fs_args;
1427 caddr_t sg = stackgap_init();
1428 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1430 struct svr4_statvfs sfs;
1433 SCARG(&fs_args, fd) = SCARG(uap, fd);
1434 SCARG(&fs_args, buf) = fs;
1436 if ((error = fstatfs(&fs_args)) != 0)
1439 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1442 uap->sysmsg_result = fs_args.sysmsg_result;
1444 bsd_statfs_to_svr4_statvfs(&bfs, &sfs);
1446 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1451 svr4_sys_statvfs64(struct svr4_sys_statvfs64_args *uap)
1453 struct statfs_args fs_args;
1454 caddr_t sg = stackgap_init();
1455 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1457 struct svr4_statvfs64 sfs;
1460 CHECKALTEXIST(&sg, SCARG(uap, path));
1461 SCARG(&fs_args, path) = SCARG(uap, path);
1462 SCARG(&fs_args, buf) = fs;
1464 if ((error = statfs(&fs_args)) != 0)
1467 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1470 uap->sysmsg_result = fs_args.sysmsg_result;
1472 bsd_statfs_to_svr4_statvfs64(&bfs, &sfs);
1474 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1479 svr4_sys_fstatvfs64(struct svr4_sys_fstatvfs64_args *uap)
1481 struct fstatfs_args fs_args;
1482 caddr_t sg = stackgap_init();
1483 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1485 struct svr4_statvfs64 sfs;
1488 SCARG(&fs_args, fd) = SCARG(uap, fd);
1489 SCARG(&fs_args, buf) = fs;
1491 if ((error = fstatfs(&fs_args)) != 0)
1494 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1497 uap->sysmsg_result = fs_args.sysmsg_result;
1499 bsd_statfs_to_svr4_statvfs64(&bfs, &sfs);
1501 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1505 svr4_sys_alarm(struct svr4_sys_alarm_args *uap)
1508 struct itimerval *itp, *oitp;
1509 struct setitimer_args sa;
1510 caddr_t sg = stackgap_init();
1512 itp = stackgap_alloc(&sg, sizeof(*itp));
1513 oitp = stackgap_alloc(&sg, sizeof(*oitp));
1514 timevalclear(&itp->it_interval);
1515 itp->it_value.tv_sec = SCARG(uap, sec);
1516 itp->it_value.tv_usec = 0;
1518 SCARG(&sa, which) = ITIMER_REAL;
1519 SCARG(&sa, itv) = itp;
1520 SCARG(&sa, oitv) = oitp;
1521 error = setitimer(&sa);
1524 if (oitp->it_value.tv_usec)
1525 oitp->it_value.tv_sec++;
1526 uap->sysmsg_result = oitp->it_value.tv_sec;
1532 svr4_sys_gettimeofday(struct svr4_sys_gettimeofday_args *uap)
1534 if (SCARG(uap, tp)) {
1538 return copyout(&atv, SCARG(uap, tp), sizeof (atv));
1545 svr4_sys_facl(struct svr4_sys_facl_args *uap)
1549 retval = &uap->sysmsg_result;
1552 switch (SCARG(uap, cmd)) {
1553 case SVR4_SYS_SETACL:
1554 /* We don't support acls on any filesystem */
1557 case SVR4_SYS_GETACL:
1558 return copyout(retval, &SCARG(uap, num),
1559 sizeof(SCARG(uap, num)));
1561 case SVR4_SYS_GETACLCNT:
1571 svr4_sys_acl(struct svr4_sys_acl_args *uap)
1573 /* XXX: for now the same */
1574 return svr4_sys_facl((struct svr4_sys_facl_args *)uap);
1578 svr4_sys_auditsys(struct svr4_sys_auditsys_args *uap)
1581 * XXX: Big brother is *not* watching.
1587 svr4_sys_memcntl(struct svr4_sys_memcntl_args *uap)
1589 switch (SCARG(uap, cmd)) {
1592 struct msync_args msa;
1595 SCARG(&msa, addr) = SCARG(uap, addr);
1596 SCARG(&msa, len) = SCARG(uap, len);
1597 SCARG(&msa, flags) = (int)SCARG(uap, arg);
1599 msa.sysmsg_result = 0;
1600 error = msync(&msa);
1601 uap->sysmsg_result = msa.sysmsg_result;
1604 case SVR4_MC_ADVISE:
1606 struct madvise_args maa;
1609 SCARG(&maa, addr) = SCARG(uap, addr);
1610 SCARG(&maa, len) = SCARG(uap, len);
1611 SCARG(&maa, behav) = (int)SCARG(uap, arg);
1613 maa.sysmsg_result = 0;
1614 error = madvise(&maa);
1615 uap->sysmsg_result = maa.sysmsg_result;
1619 case SVR4_MC_UNLOCK:
1620 case SVR4_MC_LOCKAS:
1621 case SVR4_MC_UNLOCKAS:
1630 svr4_sys_nice(struct svr4_sys_nice_args *uap)
1632 struct setpriority_args ap;
1635 SCARG(&ap, which) = PRIO_PROCESS;
1636 SCARG(&ap, who) = 0;
1637 SCARG(&ap, prio) = SCARG(uap, prio);
1639 if ((error = setpriority(&ap)) != 0)
1642 /* the cast is stupid, but the structures are the same */
1643 if ((error = getpriority((struct getpriority_args *)&ap)) != 0)
1646 uap->sysmsg_result = ap.sysmsg_result;
1652 svr4_sys_resolvepath(struct svr4_sys_resolvepath_args *uap)
1654 struct nlookupdata nd;
1658 retval = &uap->sysmsg_result;
1660 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, 0);
1662 error = nlookup(&nd);
1666 if ((error = copyout(nd.nl_path, SCARG(uap, buf),
1667 SCARG(uap, bufsiz))) != 0) {
1671 *retval = strlen(nd.nl_path) < SCARG(uap, bufsiz) ?
1672 strlen(nd.nl_path) + 1 : SCARG(uap, bufsiz);