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.13 2003/08/25 18:48:14 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/namei.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(__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 __P((register_t *, char *, svr4_mode_t, svr4_dev_t));
100 static __inline clock_t timeval_to_clock_t __P((struct timeval *));
101 static int svr4_setinfo __P((struct proc *, int, svr4_siginfo_t *));
103 struct svr4_hrtcntl_args;
104 static int svr4_hrtcntl __P((struct svr4_hrtcntl_args *, register_t *));
105 static void bsd_statfs_to_svr4_statvfs __P((const struct statfs *,
106 struct svr4_statvfs *));
107 static void bsd_statfs_to_svr4_statvfs64 __P((const struct statfs *,
108 struct svr4_statvfs64 *));
109 static struct proc *svr4_pfind __P((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",
260 p->p_pid, SCARG(uap, fd), SCARG(uap, nbytes)));
261 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0) {
265 if ((fp->f_flag & FREAD) == 0)
268 vp = (struct vnode *) fp->f_data;
270 if (vp->v_type != VDIR)
273 if ((error = VOP_GETATTR(vp, &va, td))) {
277 nbytes = SCARG(uap, nbytes);
279 nbytes = sizeof (struct svr4_dirent64);
286 #define DIRBLKSIZ 512 /* XXX we used to use ufs's DIRBLKSIZ */
287 buflen = max(DIRBLKSIZ, nbytes);
288 buflen = min(buflen, MAXBSIZE);
289 buf = malloc(buflen, M_TEMP, M_WAITOK);
290 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
293 aiov.iov_len = buflen;
294 auio.uio_iov = &aiov;
296 auio.uio_rw = UIO_READ;
297 auio.uio_segflg = UIO_SYSSPACE;
299 auio.uio_resid = buflen;
300 auio.uio_offset = off;
303 free(cookies, M_TEMP);
307 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
308 &ncookies, &cookies);
314 outp = (caddr_t) SCARG(uap, dp);
316 if ((len = buflen - auio.uio_resid) <= 0) {
324 * When using cookies, the vfs has the option of reading from
325 * a different offset than that supplied (UFS truncates the
326 * offset to a block boundary to make sure that it never reads
327 * partway through a directory entry, even if the directory
328 * has been compacted).
330 while (len > 0 && ncookies > 0 && *cookiep <= off) {
331 bdp = (struct dirent *) inp;
332 len -= bdp->d_reclen;
333 inp += bdp->d_reclen;
340 if (cookiep && ncookies == 0)
342 bdp = (struct dirent *) inp;
343 reclen = bdp->d_reclen;
345 DPRINTF(("svr4_readdir: reclen=%d\n", reclen));
350 if (bdp->d_fileno == 0) {
360 svr4reclen = SVR4_RECLEN(&svr4_dirent, bdp->d_namlen);
361 if (reclen > len || resid < svr4reclen) {
365 svr4_dirent.d_ino = (long) bdp->d_fileno;
368 * old svr4-style readdir usage.
370 svr4_dirent.d_off = (svr4_off_t) svr4reclen;
371 svr4_dirent.d_reclen = (u_short) bdp->d_namlen;
373 svr4_dirent.d_off = (svr4_off_t)(off + reclen);
374 svr4_dirent.d_reclen = (u_short) svr4reclen;
376 strcpy(svr4_dirent.d_name, bdp->d_name);
377 if ((error = copyout((caddr_t)&svr4_dirent, outp, svr4reclen)))
392 if (outp == (caddr_t) SCARG(uap, dp))
397 nbytes = resid + svr4reclen;
400 uap->sysmsg_result = nbytes - resid;
403 free(cookies, M_TEMP);
404 VOP_UNLOCK(vp, 0, td);
411 svr4_sys_getdents(struct svr4_sys_getdents_args *uap)
413 struct thread *td = curthread;
414 struct proc *p = td->td_proc;
417 caddr_t inp, buf; /* BSD-format */
418 int len, reclen; /* BSD-format */
419 caddr_t outp; /* SVR4-format */
420 int resid, svr4_reclen; /* SVR4-format */
424 struct svr4_dirent idb;
425 off_t off; /* true file offset */
426 int buflen, error, eofflag;
427 u_long *cookiebuf = NULL, *cookie;
428 int ncookies = 0, *retval = &uap->sysmsg_result;
432 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
435 if ((fp->f_flag & FREAD) == 0)
438 vp = (struct vnode *)fp->f_data;
439 if (vp->v_type != VDIR)
442 buflen = min(MAXBSIZE, SCARG(uap, nbytes));
443 buf = malloc(buflen, M_TEMP, M_WAITOK);
444 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
448 aiov.iov_len = buflen;
449 auio.uio_iov = &aiov;
451 auio.uio_rw = UIO_READ;
452 auio.uio_segflg = UIO_SYSSPACE;
454 auio.uio_resid = buflen;
455 auio.uio_offset = off;
457 * First we read into the malloc'ed buffer, then
458 * we massage it into user space, one record at a time.
460 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies,
466 outp = SCARG(uap, buf);
467 resid = SCARG(uap, nbytes);
468 if ((len = buflen - auio.uio_resid) == 0)
471 for (cookie = cookiebuf; len > 0; len -= reclen) {
472 bdp = (struct dirent *)inp;
473 reclen = bdp->d_reclen;
475 panic("svr4_sys_getdents64: bad reclen");
476 off = *cookie++; /* each entry points to the next */
477 if ((off >> 32) != 0) {
478 uprintf("svr4_sys_getdents64: dir offset too large for emulated program");
482 if (bdp->d_fileno == 0) {
483 inp += reclen; /* it is a hole; squish it out */
486 svr4_reclen = SVR4_RECLEN(&idb, bdp->d_namlen);
487 if (reclen > len || resid < svr4_reclen) {
488 /* entry too big for buffer, so just stop */
493 * Massage in place to make a SVR4-shaped dirent (otherwise
494 * we have to worry about touching user memory outside of
495 * the copyout() call).
497 idb.d_ino = (svr4_ino_t)bdp->d_fileno;
498 idb.d_off = (svr4_off_t)off;
499 idb.d_reclen = (u_short)svr4_reclen;
500 strcpy(idb.d_name, bdp->d_name);
501 if ((error = copyout((caddr_t)&idb, outp, svr4_reclen)))
503 /* advance past this real entry */
505 /* advance output past SVR4-shaped entry */
507 resid -= svr4_reclen;
510 /* if we squished out the whole block, try again */
511 if (outp == SCARG(uap, buf))
513 fp->f_offset = off; /* update the vnode offset */
516 *retval = SCARG(uap, nbytes) - resid;
518 VOP_UNLOCK(vp, 0, td);
520 free(cookiebuf, M_TEMP);
527 svr4_sys_mmap(struct svr4_sys_mmap_args *uap)
533 retval = &uap->sysmsg_result;
534 #define _MAP_NEW 0x80000000
536 * Verify the arguments.
538 if (SCARG(uap, prot) & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
539 return EINVAL; /* XXX still needed? */
541 if (SCARG(uap, len) == 0)
544 SCARG(&mm, prot) = SCARG(uap, prot);
545 SCARG(&mm, len) = SCARG(uap, len);
546 SCARG(&mm, flags) = SCARG(uap, flags) & ~_MAP_NEW;
547 SCARG(&mm, fd) = SCARG(uap, fd);
548 SCARG(&mm, addr) = SCARG(uap, addr);
549 SCARG(&mm, pos) = SCARG(uap, pos);
550 mm.sysmsg_resultp = NULL;
552 uap->sysmsg_resultp = mm.sysmsg_resultp;
557 svr4_sys_mmap64(struct svr4_sys_mmap64_args *uap)
559 struct proc *p = curproc;
564 #define _MAP_NEW 0x80000000
566 * Verify the arguments.
568 if (SCARG(uap, prot) & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
569 return EINVAL; /* XXX still needed? */
571 if (SCARG(uap, len) == 0)
574 SCARG(&mm, prot) = SCARG(uap, prot);
575 SCARG(&mm, len) = SCARG(uap, len);
576 SCARG(&mm, flags) = SCARG(uap, flags) & ~_MAP_NEW;
577 SCARG(&mm, fd) = SCARG(uap, fd);
578 SCARG(&mm, addr) = SCARG(uap, addr);
579 SCARG(&mm, pos) = SCARG(uap, pos);
581 rp = (void *) round_page((vm_offset_t)(p->p_vmspace->vm_daddr + maxdsiz));
582 if ((SCARG(&mm, flags) & MAP_FIXED) == 0 &&
583 SCARG(&mm, addr) != 0 && (void *)SCARG(&mm, addr) < rp)
584 SCARG(&mm, addr) = rp;
586 mm.sysmsg_resultp = 0;
588 uap->sysmsg_resultp = mm.sysmsg_resultp;
594 svr4_sys_fchroot(struct svr4_sys_fchroot_args *uap)
596 struct thread *td = curthread; /* XXX */
597 struct proc *p = td->td_proc;
599 struct filedesc *fdp;
608 if ((error = suser(td)) != 0)
610 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
612 vp = (struct vnode *) fp->f_data;
613 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
614 if (vp->v_type != VDIR)
617 error = VOP_ACCESS(vp, VEXEC, cred, td);
618 VOP_UNLOCK(vp, 0, td);
622 if (fdp->fd_rdir != NULL)
630 svr4_mknod(retval, path, mode, dev)
636 caddr_t sg = stackgap_init();
639 CHECKALTEXIST(&sg, path);
641 if (S_ISFIFO(mode)) {
642 struct mkfifo_args ap;
643 SCARG(&ap, path) = path;
644 SCARG(&ap, mode) = mode;
646 *retval = ap.sysmsg_result;
648 struct mknod_args ap;
649 SCARG(&ap, path) = path;
650 SCARG(&ap, mode) = mode;
651 SCARG(&ap, dev) = dev;
653 *retval = ap.sysmsg_result;
660 svr4_sys_mknod(struct svr4_sys_mknod_args *uap)
662 struct thread *td = curthread; /* XXX */
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)
676 struct thread *td = curthread; /* XXX */
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)
697 struct thread *td = curthread; /* XXX */
701 retval = &uap->sysmsg_result;
703 switch (SCARG(uap, name)) {
704 case SVR4_CONFIG_UNUSED:
707 case SVR4_CONFIG_NGROUPS:
708 *retval = NGROUPS_MAX;
710 case SVR4_CONFIG_CHILD_MAX:
713 case SVR4_CONFIG_OPEN_FILES:
716 case SVR4_CONFIG_POSIX_VER:
719 case SVR4_CONFIG_PAGESIZE:
722 case SVR4_CONFIG_CLK_TCK:
723 *retval = 60; /* should this be `hz', ie. 100? */
725 case SVR4_CONFIG_XOPEN_VER:
726 *retval = 2; /* XXX: What should that be? */
728 case SVR4_CONFIG_PROF_TCK:
729 *retval = 60; /* XXX: What should that be? */
731 case SVR4_CONFIG_NPROC_CONF:
732 *retval = 1; /* Only one processor for now */
734 case SVR4_CONFIG_NPROC_ONLN:
735 *retval = 1; /* And it better be online */
737 case SVR4_CONFIG_AIO_LISTIO_MAX:
738 case SVR4_CONFIG_AIO_MAX:
739 case SVR4_CONFIG_AIO_PRIO_DELTA_MAX:
740 *retval = 0; /* No aio support */
742 case SVR4_CONFIG_DELAYTIMER_MAX:
743 *retval = 0; /* No delaytimer support */
745 case SVR4_CONFIG_MQ_OPEN_MAX:
746 *retval = msginfo.msgmni;
748 case SVR4_CONFIG_MQ_PRIO_MAX:
749 *retval = 0; /* XXX: Don't know */
751 case SVR4_CONFIG_RTSIG_MAX:
754 case SVR4_CONFIG_SEM_NSEMS_MAX:
755 *retval = seminfo.semmni;
757 case SVR4_CONFIG_SEM_VALUE_MAX:
758 *retval = seminfo.semvmx;
760 case SVR4_CONFIG_SIGQUEUE_MAX:
761 *retval = 0; /* XXX: Don't know */
763 case SVR4_CONFIG_SIGRT_MIN:
764 case SVR4_CONFIG_SIGRT_MAX:
765 *retval = 0; /* No real time signals */
767 case SVR4_CONFIG_TIMER_MAX:
768 *retval = 3; /* XXX: real, virtual, profiling */
771 case SVR4_CONFIG_PHYS_PAGES:
773 *retval = uvmexp.free; /* XXX: free instead of total */
775 *retval = cnt.v_free_count; /* XXX: free instead of total */
778 case SVR4_CONFIG_AVPHYS_PAGES:
780 *retval = uvmexp.active; /* XXX: active instead of avg */
782 *retval = cnt.v_active_count; /* XXX: active instead of avg */
793 extern int swap_pager_full;
797 svr4_sys_break(struct svr4_sys_break_args *uap)
799 struct thread *td = curthread; /* XXX */
800 struct proc *p = td->td_proc;
802 vm_offset_t new, old, base, ns;
808 base = round_page((vm_offset_t) vm->vm_daddr);
809 ns = (vm_offset_t)SCARG(uap, nsize);
810 new = round_page(ns);
812 if ((new - base) > (unsigned) p->p_rlimit[RLIMIT_DATA].rlim_cur) {
815 if (new >= VM_MAXUSER_ADDRESS) {
818 } else if (new < base) {
820 * This is simply an invalid value. If someone wants to
821 * do fancy address space manipulations, mmap and munmap
822 * can do most of what the user would want.
827 old = base + ctob(vm->vm_dsize);
832 if (vm->vm_map.size + diff > p->p_rlimit[RLIMIT_VMEM].rlim_cur)
834 rv = vm_map_find(&vm->vm_map, NULL, 0, &old, diff, FALSE,
835 VM_PROT_ALL, VM_PROT_ALL, 0);
836 if (rv != KERN_SUCCESS) {
839 vm->vm_dsize += btoc(diff);
840 } else if (new < old) {
841 rv = vm_map_remove(&vm->vm_map, new, old);
842 if (rv != KERN_SUCCESS) {
845 vm->vm_dsize -= btoc(old - new);
851 static __inline clock_t
852 timeval_to_clock_t(tv)
855 return tv->tv_sec * hz + tv->tv_usec / (1000000 / hz);
860 svr4_sys_times(struct svr4_sys_times_args *uap)
862 int error, *retval = &uap->sysmsg_result;
867 struct getrusage_args ga;
869 caddr_t sg = stackgap_init();
870 ru = stackgap_alloc(&sg, sizeof(struct rusage));
872 SCARG(&ga, who) = RUSAGE_SELF;
873 SCARG(&ga, rusage) = ru;
875 error = getrusage(&ga);
879 if ((error = copyin(ru, &r, sizeof r)) != 0)
882 tms.tms_utime = timeval_to_clock_t(&r.ru_utime);
883 tms.tms_stime = timeval_to_clock_t(&r.ru_stime);
885 SCARG(&ga, who) = RUSAGE_CHILDREN;
886 error = getrusage(&ga);
890 if ((error = copyin(ru, &r, sizeof r)) != 0)
893 tms.tms_cutime = timeval_to_clock_t(&r.ru_utime);
894 tms.tms_cstime = timeval_to_clock_t(&r.ru_stime);
897 *retval = timeval_to_clock_t(&t);
899 return copyout(&tms, SCARG(uap, tp), sizeof(tms));
904 svr4_sys_ulimit(struct svr4_sys_ulimit_args *uap)
906 struct proc *p = curproc;
907 int *retval = &uap->sysmsg_result;
909 switch (SCARG(uap, cmd)) {
911 *retval = p->p_rlimit[RLIMIT_FSIZE].rlim_cur / 512;
913 *retval = 0x7fffffff;
919 struct __setrlimit_args srl;
921 caddr_t sg = stackgap_init();
922 struct rlimit *url = (struct rlimit *)
923 stackgap_alloc(&sg, sizeof *url);
925 krl.rlim_cur = SCARG(uap, newlimit) * 512;
926 krl.rlim_max = p->p_rlimit[RLIMIT_FSIZE].rlim_max;
928 error = copyout(&krl, url, sizeof(*url));
932 SCARG(&srl, which) = RLIMIT_FSIZE;
933 SCARG(&srl, rlp) = (struct rlimit *)url;
935 error = setrlimit(&srl);
939 *retval = p->p_rlimit[RLIMIT_FSIZE].rlim_cur;
941 *retval = 0x7fffffff;
947 struct vmspace *vm = p->p_vmspace;
948 register_t r = p->p_rlimit[RLIMIT_DATA].rlim_cur;
952 r += (long) vm->vm_daddr;
960 *retval = p->p_rlimit[RLIMIT_NOFILE].rlim_cur;
962 *retval = 0x7fffffff;
976 /* look in the live processes */
977 if ((p = pfind(pid)) != NULL)
980 /* look in the zombies */
981 for (p = zombproc.lh_first; p != 0; p = p->p_list.le_next)
990 svr4_sys_pgrpsys(struct svr4_sys_pgrpsys_args *uap)
992 struct proc *p = curproc;
993 int *retval = &uap->sysmsg_result;
995 switch (SCARG(uap, cmd)) {
996 case 1: /* setpgrp() */
998 * SVR4 setpgrp() (which takes no arguments) has the
999 * semantics that the session ID is also created anew, so
1000 * in almost every sense, setpgrp() is identical to
1001 * setsid() for SVR4. (Under BSD, the difference is that
1002 * a setpgid(0,0) will not create a new session.)
1005 struct setsid_args sida;
1007 sida.sysmsg_result = 0;
1009 /* ignore result? */
1013 case 0: /* getpgrp() */
1014 *retval = p->p_pgrp->pg_id;
1017 case 2: /* getsid(pid) */
1018 if (SCARG(uap, pid) != 0 &&
1019 (p = svr4_pfind(SCARG(uap, pid))) == NULL)
1022 * This has already been initialized to the pid of
1023 * the session leader.
1025 *retval = (register_t) p->p_session->s_leader->p_pid;
1028 case 3: /* setsid() */
1031 struct setsid_args sida;
1034 sida.sysmsg_result = 0;
1035 error = setsid(&sida);
1036 uap->sysmsg_result = sida.sysmsg_result;
1039 case 4: /* getpgid(pid) */
1041 if (SCARG(uap, pid) != 0 &&
1042 (p = svr4_pfind(SCARG(uap, pid))) == NULL)
1045 *retval = (int) p->p_pgrp->pg_id;
1048 case 5: /* setpgid(pid, pgid); */
1050 struct setpgid_args sa;
1053 SCARG(&sa, pid) = SCARG(uap, pid);
1054 SCARG(&sa, pgid) = SCARG(uap, pgid);
1055 sa.sysmsg_result = 0;
1056 error = setpgid(&sa);
1057 uap->sysmsg_result = sa.sysmsg_result;
1065 #define syscallarg(x) union { x datum; register_t pad; }
1067 struct svr4_hrtcntl_args {
1071 svr4_hrt_interval_t * iv;
1072 svr4_hrt_time_t * ti;
1077 svr4_hrtcntl(struct svr4_hrtcntl_args *uap, register_t *retval)
1079 switch (SCARG(uap, fun)) {
1080 case SVR4_HRT_CNTL_RES:
1081 DPRINTF(("htrcntl(RES)\n"));
1082 *retval = SVR4_HRT_USEC;
1085 case SVR4_HRT_CNTL_TOFD:
1086 DPRINTF(("htrcntl(TOFD)\n"));
1090 if (SCARG(uap, clk) != SVR4_HRT_CLK_STD) {
1091 DPRINTF(("clk == %d\n", SCARG(uap, clk)));
1094 if (SCARG(uap, ti) == NULL) {
1095 DPRINTF(("ti NULL\n"));
1099 t.h_sec = tv.tv_sec;
1100 t.h_rem = tv.tv_usec;
1101 t.h_res = SVR4_HRT_USEC;
1102 return copyout(&t, SCARG(uap, ti), sizeof(t));
1105 case SVR4_HRT_CNTL_START:
1106 DPRINTF(("htrcntl(START)\n"));
1109 case SVR4_HRT_CNTL_GET:
1110 DPRINTF(("htrcntl(GET)\n"));
1113 DPRINTF(("Bad htrcntl command %d\n", SCARG(uap, fun)));
1120 svr4_sys_hrtsys(struct svr4_sys_hrtsys_args *uap)
1122 int *retval = &uap->sysmsg_result;
1124 switch (SCARG(uap, cmd)) {
1126 return svr4_hrtcntl((struct svr4_hrtcntl_args *)(&uap->sysmsg + 1), retval);
1129 DPRINTF(("hrtalarm\n"));
1133 DPRINTF(("hrtsleep\n"));
1137 DPRINTF(("hrtcancel\n"));
1141 DPRINTF(("Bad hrtsys command %d\n", SCARG(uap, cmd)));
1148 svr4_setinfo(p, st, s)
1156 memset(&i, 0, sizeof(i));
1158 i.si_signo = SVR4_SIGCHLD;
1159 i.si_errno = 0; /* XXX? */
1162 i.si_pid = p->p_pid;
1163 if (p->p_stat == SZOMB) {
1164 i.si_stime = p->p_ru->ru_stime.tv_sec;
1165 i.si_utime = p->p_ru->ru_utime.tv_sec;
1168 i.si_stime = p->p_stats->p_ru.ru_stime.tv_sec;
1169 i.si_utime = p->p_stats->p_ru.ru_utime.tv_sec;
1173 if (WIFEXITED(st)) {
1174 i.si_status = WEXITSTATUS(st);
1175 i.si_code = SVR4_CLD_EXITED;
1176 } else if (WIFSTOPPED(st)) {
1178 if (sig >= 0 && sig < NSIG)
1179 i.si_status = SVR4_BSD2SVR4_SIG(sig);
1181 if (i.si_status == SVR4_SIGCONT)
1182 i.si_code = SVR4_CLD_CONTINUED;
1184 i.si_code = SVR4_CLD_STOPPED;
1187 if (sig >= 0 && sig < NSIG)
1188 i.si_status = SVR4_BSD2SVR4_SIG(sig);
1191 i.si_code = SVR4_CLD_DUMPED;
1193 i.si_code = SVR4_CLD_KILLED;
1196 DPRINTF(("siginfo [pid %ld signo %d code %d errno %d status %d]\n",
1197 i.si_pid, i.si_signo, i.si_code, i.si_errno, i.si_status));
1199 return copyout(&i, s, sizeof(i));
1204 svr4_sys_waitsys(struct svr4_sys_waitsys_args *uap)
1206 struct proc *p = curproc;
1208 int error, *retval = &uap->sysmsg_result;
1212 switch (SCARG(uap, grp)) {
1217 SCARG(uap, id) = -p->p_pgid;
1221 SCARG(uap, id) = WAIT_ANY;
1228 DPRINTF(("waitsys(%d, %d, %p, %x)\n",
1229 SCARG(uap, grp), SCARG(uap, id),
1230 SCARG(uap, info), SCARG(uap, options)));
1234 for (q = p->p_children.lh_first; q != 0; q = q->p_sibling.le_next) {
1235 if (SCARG(uap, id) != WAIT_ANY &&
1236 q->p_pid != SCARG(uap, id) &&
1237 q->p_pgid != -SCARG(uap, id)) {
1238 DPRINTF(("pid %d pgid %d != %d\n", q->p_pid,
1239 q->p_pgid, SCARG(uap, id)));
1243 if (q->p_stat == SZOMB &&
1244 ((SCARG(uap, options) & (SVR4_WEXITED|SVR4_WTRAPPED)))) {
1246 DPRINTF(("found %d\n", q->p_pid));
1247 if ((error = svr4_setinfo(q, q->p_xstat,
1248 SCARG(uap, info))) != 0)
1252 if ((SCARG(uap, options) & SVR4_WNOWAIT)) {
1253 DPRINTF(("Don't wait\n"));
1258 * If we got the child via ptrace(2) or procfs, and
1259 * the parent is different (meaning the process was
1260 * attached, rather than run as a child), then we need
1261 * to give it back to the ol dparent, and send the
1262 * parent a SIGCHLD. The rest of the cleanup will be
1263 * done when the old parent waits on the child.
1265 if ((q->p_flag & P_TRACED) &&
1266 q->p_oppid != q->p_pptr->p_pid) {
1267 t = pfind(q->p_oppid);
1268 proc_reparent(q, t ? t : initproc);
1270 q->p_flag &= ~(P_TRACED | P_WAITED);
1271 wakeup((caddr_t)q->p_pptr);
1275 ruadd(&p->p_stats->p_cru, q->p_ru);
1277 FREE(q->p_ru, M_ZOMBIE);
1280 * Finally finished with old proc entry.
1281 * Unlink it from its process group and free it.
1285 LIST_REMOVE(q, p_list); /* off zombproc */
1287 LIST_REMOVE(q, p_sibling);
1290 * Decrement the count of procs running with this uid.
1292 (void)chgproccnt(q->p_ucred->cr_uidinfo, -1, 0);
1295 * Free up credentials.
1301 * Release reference to text vnode
1307 * Give machine-dependent layer a chance
1308 * to free anything that cpu_exit couldn't
1309 * release while still running in process context.
1312 /* XXX what about process 'q' itself? zfree? */
1313 #if defined(__NetBSD__)
1314 pool_put(&proc_pool, q);
1319 if (q->p_stat == SSTOP && (q->p_flag & P_WAITED) == 0 &&
1320 (q->p_flag & P_TRACED ||
1321 (SCARG(uap, options) & (SVR4_WSTOPPED|SVR4_WCONTINUED)))) {
1322 DPRINTF(("jobcontrol %d\n", q->p_pid));
1323 if (((SCARG(uap, options) & SVR4_WNOWAIT)) == 0)
1324 q->p_flag |= P_WAITED;
1326 return svr4_setinfo(q, W_STOPCODE(q->p_xstat),
1334 if (SCARG(uap, options) & SVR4_WNOHANG) {
1336 if ((error = svr4_setinfo(NULL, 0, SCARG(uap, info))) != 0)
1341 if ((error = tsleep((caddr_t)p, PCATCH, "svr4_wait", 0)) != 0)
1348 bsd_statfs_to_svr4_statvfs(bfs, sfs)
1349 const struct statfs *bfs;
1350 struct svr4_statvfs *sfs;
1352 sfs->f_bsize = bfs->f_iosize; /* XXX */
1353 sfs->f_frsize = bfs->f_bsize;
1354 sfs->f_blocks = bfs->f_blocks;
1355 sfs->f_bfree = bfs->f_bfree;
1356 sfs->f_bavail = bfs->f_bavail;
1357 sfs->f_files = bfs->f_files;
1358 sfs->f_ffree = bfs->f_ffree;
1359 sfs->f_favail = bfs->f_ffree;
1360 sfs->f_fsid = bfs->f_fsid.val[0];
1361 memcpy(sfs->f_basetype, bfs->f_fstypename, sizeof(sfs->f_basetype));
1363 if (bfs->f_flags & MNT_RDONLY)
1364 sfs->f_flag |= SVR4_ST_RDONLY;
1365 if (bfs->f_flags & MNT_NOSUID)
1366 sfs->f_flag |= SVR4_ST_NOSUID;
1367 sfs->f_namemax = MAXNAMLEN;
1368 memcpy(sfs->f_fstr, bfs->f_fstypename, sizeof(sfs->f_fstr)); /* XXX */
1369 memset(sfs->f_filler, 0, sizeof(sfs->f_filler));
1374 bsd_statfs_to_svr4_statvfs64(bfs, sfs)
1375 const struct statfs *bfs;
1376 struct svr4_statvfs64 *sfs;
1378 sfs->f_bsize = bfs->f_iosize; /* XXX */
1379 sfs->f_frsize = bfs->f_bsize;
1380 sfs->f_blocks = bfs->f_blocks;
1381 sfs->f_bfree = bfs->f_bfree;
1382 sfs->f_bavail = bfs->f_bavail;
1383 sfs->f_files = bfs->f_files;
1384 sfs->f_ffree = bfs->f_ffree;
1385 sfs->f_favail = bfs->f_ffree;
1386 sfs->f_fsid = bfs->f_fsid.val[0];
1387 memcpy(sfs->f_basetype, bfs->f_fstypename, sizeof(sfs->f_basetype));
1389 if (bfs->f_flags & MNT_RDONLY)
1390 sfs->f_flag |= SVR4_ST_RDONLY;
1391 if (bfs->f_flags & MNT_NOSUID)
1392 sfs->f_flag |= SVR4_ST_NOSUID;
1393 sfs->f_namemax = MAXNAMLEN;
1394 memcpy(sfs->f_fstr, bfs->f_fstypename, sizeof(sfs->f_fstr)); /* XXX */
1395 memset(sfs->f_filler, 0, sizeof(sfs->f_filler));
1400 svr4_sys_statvfs(struct svr4_sys_statvfs_args *uap)
1402 struct statfs_args fs_args;
1403 caddr_t sg = stackgap_init();
1404 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1406 struct svr4_statvfs sfs;
1409 CHECKALTEXIST(&sg, SCARG(uap, path));
1410 SCARG(&fs_args, path) = SCARG(uap, path);
1411 SCARG(&fs_args, buf) = fs;
1413 if ((error = statfs(&fs_args)) != 0)
1416 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1419 uap->sysmsg_result = fs_args.sysmsg_result;
1421 bsd_statfs_to_svr4_statvfs(&bfs, &sfs);
1423 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1428 svr4_sys_fstatvfs(struct svr4_sys_fstatvfs_args *uap)
1430 struct fstatfs_args fs_args;
1431 caddr_t sg = stackgap_init();
1432 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1434 struct svr4_statvfs sfs;
1437 SCARG(&fs_args, fd) = SCARG(uap, fd);
1438 SCARG(&fs_args, buf) = fs;
1440 if ((error = fstatfs(&fs_args)) != 0)
1443 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1446 uap->sysmsg_result = fs_args.sysmsg_result;
1448 bsd_statfs_to_svr4_statvfs(&bfs, &sfs);
1450 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1455 svr4_sys_statvfs64(struct svr4_sys_statvfs64_args *uap)
1457 struct statfs_args fs_args;
1458 caddr_t sg = stackgap_init();
1459 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1461 struct svr4_statvfs64 sfs;
1464 CHECKALTEXIST(&sg, SCARG(uap, path));
1465 SCARG(&fs_args, path) = SCARG(uap, path);
1466 SCARG(&fs_args, buf) = fs;
1468 if ((error = statfs(&fs_args)) != 0)
1471 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1474 uap->sysmsg_result = fs_args.sysmsg_result;
1476 bsd_statfs_to_svr4_statvfs64(&bfs, &sfs);
1478 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1483 svr4_sys_fstatvfs64(struct svr4_sys_fstatvfs64_args *uap)
1485 struct fstatfs_args fs_args;
1486 caddr_t sg = stackgap_init();
1487 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1489 struct svr4_statvfs64 sfs;
1492 SCARG(&fs_args, fd) = SCARG(uap, fd);
1493 SCARG(&fs_args, buf) = fs;
1495 if ((error = fstatfs(&fs_args)) != 0)
1498 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1501 uap->sysmsg_result = fs_args.sysmsg_result;
1503 bsd_statfs_to_svr4_statvfs64(&bfs, &sfs);
1505 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1509 svr4_sys_alarm(struct svr4_sys_alarm_args *uap)
1512 struct itimerval *itp, *oitp;
1513 struct setitimer_args sa;
1514 caddr_t sg = stackgap_init();
1516 itp = stackgap_alloc(&sg, sizeof(*itp));
1517 oitp = stackgap_alloc(&sg, sizeof(*oitp));
1518 timevalclear(&itp->it_interval);
1519 itp->it_value.tv_sec = SCARG(uap, sec);
1520 itp->it_value.tv_usec = 0;
1522 SCARG(&sa, which) = ITIMER_REAL;
1523 SCARG(&sa, itv) = itp;
1524 SCARG(&sa, oitv) = oitp;
1525 error = setitimer(&sa);
1528 if (oitp->it_value.tv_usec)
1529 oitp->it_value.tv_sec++;
1530 uap->sysmsg_result = oitp->it_value.tv_sec;
1536 svr4_sys_gettimeofday(struct svr4_sys_gettimeofday_args *uap)
1538 if (SCARG(uap, tp)) {
1542 return copyout(&atv, SCARG(uap, tp), sizeof (atv));
1549 svr4_sys_facl(struct svr4_sys_facl_args *uap)
1553 retval = &uap->sysmsg_result;
1556 switch (SCARG(uap, cmd)) {
1557 case SVR4_SYS_SETACL:
1558 /* We don't support acls on any filesystem */
1561 case SVR4_SYS_GETACL:
1562 return copyout(retval, &SCARG(uap, num),
1563 sizeof(SCARG(uap, num)));
1565 case SVR4_SYS_GETACLCNT:
1575 svr4_sys_acl(struct svr4_sys_acl_args *uap)
1577 /* XXX: for now the same */
1578 return svr4_sys_facl((struct svr4_sys_facl_args *)uap);
1582 svr4_sys_auditsys(struct svr4_sys_auditsys_args *uap)
1585 * XXX: Big brother is *not* watching.
1591 svr4_sys_memcntl(struct svr4_sys_memcntl_args *uap)
1593 switch (SCARG(uap, cmd)) {
1596 struct msync_args msa;
1599 SCARG(&msa, addr) = SCARG(uap, addr);
1600 SCARG(&msa, len) = SCARG(uap, len);
1601 SCARG(&msa, flags) = (int)SCARG(uap, arg);
1603 msa.sysmsg_result = 0;
1604 error = msync(&msa);
1605 uap->sysmsg_result = msa.sysmsg_result;
1608 case SVR4_MC_ADVISE:
1610 struct madvise_args maa;
1613 SCARG(&maa, addr) = SCARG(uap, addr);
1614 SCARG(&maa, len) = SCARG(uap, len);
1615 SCARG(&maa, behav) = (int)SCARG(uap, arg);
1617 maa.sysmsg_result = 0;
1618 error = madvise(&maa);
1619 uap->sysmsg_result = maa.sysmsg_result;
1623 case SVR4_MC_UNLOCK:
1624 case SVR4_MC_LOCKAS:
1625 case SVR4_MC_UNLOCKAS:
1634 svr4_sys_nice(struct svr4_sys_nice_args *uap)
1636 struct setpriority_args ap;
1639 SCARG(&ap, which) = PRIO_PROCESS;
1640 SCARG(&ap, who) = 0;
1641 SCARG(&ap, prio) = SCARG(uap, prio);
1643 if ((error = setpriority(&ap)) != 0)
1646 /* the cast is stupid, but the structures are the same */
1647 if ((error = getpriority((struct getpriority_args *)&ap)) != 0)
1650 uap->sysmsg_result = ap.sysmsg_result;
1656 svr4_sys_resolvepath(struct svr4_sys_resolvepath_args *uap)
1658 struct thread *td = curthread; /* XXX */
1659 struct nameidata nd;
1664 retval = &uap->sysmsg_result;
1666 NDINIT(&nd, LOOKUP, NOFOLLOW | SAVENAME, UIO_USERSPACE,
1667 SCARG(uap, path), td);
1669 if ((error = namei(&nd)) != 0)
1672 if ((error = copyout(nd.ni_cnd.cn_pnbuf, SCARG(uap, buf),
1673 SCARG(uap, bufsiz))) != 0)
1676 *retval = strlen(nd.ni_cnd.cn_pnbuf) < SCARG(uap, bufsiz) ?
1677 strlen(nd.ni_cnd.cn_pnbuf) + 1 : SCARG(uap, bufsiz);
1679 NDFREE(&nd, NDF_ONLY_PNBUF);