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.18 2003/09/29 18:52:11 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 (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",
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)
664 retval = &uap->sysmsg_result;
666 return svr4_mknod(retval, SCARG(uap, path), SCARG(uap, mode),
667 (svr4_dev_t)svr4_to_bsd_odev_t(SCARG(uap, dev)));
672 svr4_sys_xmknod(struct svr4_sys_xmknod_args *uap)
676 retval = &uap->sysmsg_result;
678 return svr4_mknod(retval, SCARG(uap, path), SCARG(uap, mode),
679 (svr4_dev_t)svr4_to_bsd_dev_t(SCARG(uap, dev)));
684 svr4_sys_vhangup(struct svr4_sys_vhangup_args *uap)
691 svr4_sys_sysconfig(struct svr4_sys_sysconfig_args *uap)
695 retval = &uap->sysmsg_result;
697 switch (SCARG(uap, name)) {
698 case SVR4_CONFIG_UNUSED:
701 case SVR4_CONFIG_NGROUPS:
702 *retval = NGROUPS_MAX;
704 case SVR4_CONFIG_CHILD_MAX:
707 case SVR4_CONFIG_OPEN_FILES:
710 case SVR4_CONFIG_POSIX_VER:
713 case SVR4_CONFIG_PAGESIZE:
716 case SVR4_CONFIG_CLK_TCK:
717 *retval = 60; /* should this be `hz', ie. 100? */
719 case SVR4_CONFIG_XOPEN_VER:
720 *retval = 2; /* XXX: What should that be? */
722 case SVR4_CONFIG_PROF_TCK:
723 *retval = 60; /* XXX: What should that be? */
725 case SVR4_CONFIG_NPROC_CONF:
726 *retval = 1; /* Only one processor for now */
728 case SVR4_CONFIG_NPROC_ONLN:
729 *retval = 1; /* And it better be online */
731 case SVR4_CONFIG_AIO_LISTIO_MAX:
732 case SVR4_CONFIG_AIO_MAX:
733 case SVR4_CONFIG_AIO_PRIO_DELTA_MAX:
734 *retval = 0; /* No aio support */
736 case SVR4_CONFIG_DELAYTIMER_MAX:
737 *retval = 0; /* No delaytimer support */
739 case SVR4_CONFIG_MQ_OPEN_MAX:
740 *retval = msginfo.msgmni;
742 case SVR4_CONFIG_MQ_PRIO_MAX:
743 *retval = 0; /* XXX: Don't know */
745 case SVR4_CONFIG_RTSIG_MAX:
748 case SVR4_CONFIG_SEM_NSEMS_MAX:
749 *retval = seminfo.semmni;
751 case SVR4_CONFIG_SEM_VALUE_MAX:
752 *retval = seminfo.semvmx;
754 case SVR4_CONFIG_SIGQUEUE_MAX:
755 *retval = 0; /* XXX: Don't know */
757 case SVR4_CONFIG_SIGRT_MIN:
758 case SVR4_CONFIG_SIGRT_MAX:
759 *retval = 0; /* No real time signals */
761 case SVR4_CONFIG_TIMER_MAX:
762 *retval = 3; /* XXX: real, virtual, profiling */
765 case SVR4_CONFIG_PHYS_PAGES:
767 *retval = uvmexp.free; /* XXX: free instead of total */
769 *retval = cnt.v_free_count; /* XXX: free instead of total */
772 case SVR4_CONFIG_AVPHYS_PAGES:
774 *retval = uvmexp.active; /* XXX: active instead of avg */
776 *retval = cnt.v_active_count; /* XXX: active instead of avg */
787 extern int swap_pager_full;
791 svr4_sys_break(struct svr4_sys_break_args *uap)
793 struct thread *td = curthread; /* XXX */
794 struct proc *p = td->td_proc;
796 vm_offset_t new, old, base, ns;
802 base = round_page((vm_offset_t) vm->vm_daddr);
803 ns = (vm_offset_t)SCARG(uap, nsize);
804 new = round_page(ns);
806 if ((new - base) > (unsigned) p->p_rlimit[RLIMIT_DATA].rlim_cur) {
809 if (new >= VM_MAXUSER_ADDRESS) {
812 } else if (new < base) {
814 * This is simply an invalid value. If someone wants to
815 * do fancy address space manipulations, mmap and munmap
816 * can do most of what the user would want.
821 old = base + ctob(vm->vm_dsize);
826 if (vm->vm_map.size + diff > p->p_rlimit[RLIMIT_VMEM].rlim_cur)
828 rv = vm_map_find(&vm->vm_map, NULL, 0, &old, diff, FALSE,
829 VM_PROT_ALL, VM_PROT_ALL, 0);
830 if (rv != KERN_SUCCESS) {
833 vm->vm_dsize += btoc(diff);
834 } else if (new < old) {
835 rv = vm_map_remove(&vm->vm_map, new, old);
836 if (rv != KERN_SUCCESS) {
839 vm->vm_dsize -= btoc(old - new);
845 static __inline clock_t
846 timeval_to_clock_t(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;
970 /* look in the live processes */
971 if ((p = pfind(pid)) != NULL)
974 /* look in the zombies */
975 for (p = zombproc.lh_first; p != 0; p = p->p_list.le_next)
984 svr4_sys_pgrpsys(struct svr4_sys_pgrpsys_args *uap)
986 struct proc *p = curproc;
987 int *retval = &uap->sysmsg_result;
989 switch (SCARG(uap, cmd)) {
990 case 1: /* setpgrp() */
992 * SVR4 setpgrp() (which takes no arguments) has the
993 * semantics that the session ID is also created anew, so
994 * in almost every sense, setpgrp() is identical to
995 * setsid() for SVR4. (Under BSD, the difference is that
996 * a setpgid(0,0) will not create a new session.)
999 struct setsid_args sida;
1001 sida.sysmsg_result = 0;
1003 /* ignore result? */
1007 case 0: /* getpgrp() */
1008 *retval = p->p_pgrp->pg_id;
1011 case 2: /* getsid(pid) */
1012 if (SCARG(uap, pid) != 0 &&
1013 (p = svr4_pfind(SCARG(uap, pid))) == NULL)
1016 * This has already been initialized to the pid of
1017 * the session leader.
1019 *retval = (register_t) p->p_session->s_leader->p_pid;
1022 case 3: /* setsid() */
1025 struct setsid_args sida;
1028 sida.sysmsg_result = 0;
1029 error = setsid(&sida);
1030 uap->sysmsg_result = sida.sysmsg_result;
1033 case 4: /* getpgid(pid) */
1035 if (SCARG(uap, pid) != 0 &&
1036 (p = svr4_pfind(SCARG(uap, pid))) == NULL)
1039 *retval = (int) p->p_pgrp->pg_id;
1042 case 5: /* setpgid(pid, pgid); */
1044 struct setpgid_args sa;
1047 SCARG(&sa, pid) = SCARG(uap, pid);
1048 SCARG(&sa, pgid) = SCARG(uap, pgid);
1049 sa.sysmsg_result = 0;
1050 error = setpgid(&sa);
1051 uap->sysmsg_result = sa.sysmsg_result;
1059 #define syscallarg(x) union { x datum; register_t pad; }
1061 struct svr4_hrtcntl_args {
1065 svr4_hrt_interval_t * iv;
1066 svr4_hrt_time_t * ti;
1071 svr4_hrtcntl(struct svr4_hrtcntl_args *uap, register_t *retval)
1073 switch (SCARG(uap, fun)) {
1074 case SVR4_HRT_CNTL_RES:
1075 DPRINTF(("htrcntl(RES)\n"));
1076 *retval = SVR4_HRT_USEC;
1079 case SVR4_HRT_CNTL_TOFD:
1080 DPRINTF(("htrcntl(TOFD)\n"));
1084 if (SCARG(uap, clk) != SVR4_HRT_CLK_STD) {
1085 DPRINTF(("clk == %d\n", SCARG(uap, clk)));
1088 if (SCARG(uap, ti) == NULL) {
1089 DPRINTF(("ti NULL\n"));
1093 t.h_sec = tv.tv_sec;
1094 t.h_rem = tv.tv_usec;
1095 t.h_res = SVR4_HRT_USEC;
1096 return copyout(&t, SCARG(uap, ti), sizeof(t));
1099 case SVR4_HRT_CNTL_START:
1100 DPRINTF(("htrcntl(START)\n"));
1103 case SVR4_HRT_CNTL_GET:
1104 DPRINTF(("htrcntl(GET)\n"));
1107 DPRINTF(("Bad htrcntl command %d\n", SCARG(uap, fun)));
1114 svr4_sys_hrtsys(struct svr4_sys_hrtsys_args *uap)
1116 int *retval = &uap->sysmsg_result;
1118 switch (SCARG(uap, cmd)) {
1120 return svr4_hrtcntl((struct svr4_hrtcntl_args *)(&uap->sysmsg + 1), retval);
1123 DPRINTF(("hrtalarm\n"));
1127 DPRINTF(("hrtsleep\n"));
1131 DPRINTF(("hrtcancel\n"));
1135 DPRINTF(("Bad hrtsys command %d\n", SCARG(uap, cmd)));
1142 svr4_setinfo(p, st, s)
1150 memset(&i, 0, sizeof(i));
1152 i.si_signo = SVR4_SIGCHLD;
1153 i.si_errno = 0; /* XXX? */
1156 i.si_pid = p->p_pid;
1157 if (p->p_stat == SZOMB) {
1158 i.si_stime = p->p_ru->ru_stime.tv_sec;
1159 i.si_utime = p->p_ru->ru_utime.tv_sec;
1162 i.si_stime = p->p_stats->p_ru.ru_stime.tv_sec;
1163 i.si_utime = p->p_stats->p_ru.ru_utime.tv_sec;
1167 if (WIFEXITED(st)) {
1168 i.si_status = WEXITSTATUS(st);
1169 i.si_code = SVR4_CLD_EXITED;
1170 } else if (WIFSTOPPED(st)) {
1172 if (sig >= 0 && sig < NSIG)
1173 i.si_status = SVR4_BSD2SVR4_SIG(sig);
1175 if (i.si_status == SVR4_SIGCONT)
1176 i.si_code = SVR4_CLD_CONTINUED;
1178 i.si_code = SVR4_CLD_STOPPED;
1181 if (sig >= 0 && sig < NSIG)
1182 i.si_status = SVR4_BSD2SVR4_SIG(sig);
1185 i.si_code = SVR4_CLD_DUMPED;
1187 i.si_code = SVR4_CLD_KILLED;
1190 DPRINTF(("siginfo [pid %ld signo %d code %d errno %d status %d]\n",
1191 i.si_pid, i.si_signo, i.si_code, i.si_errno, i.si_status));
1193 return copyout(&i, s, sizeof(i));
1198 svr4_sys_waitsys(struct svr4_sys_waitsys_args *uap)
1200 struct proc *p = curproc;
1202 int error, *retval = &uap->sysmsg_result;
1206 switch (SCARG(uap, grp)) {
1211 SCARG(uap, id) = -p->p_pgid;
1215 SCARG(uap, id) = WAIT_ANY;
1222 DPRINTF(("waitsys(%d, %d, %p, %x)\n",
1223 SCARG(uap, grp), SCARG(uap, id),
1224 SCARG(uap, info), SCARG(uap, options)));
1228 for (q = p->p_children.lh_first; q != 0; q = q->p_sibling.le_next) {
1229 if (SCARG(uap, id) != WAIT_ANY &&
1230 q->p_pid != SCARG(uap, id) &&
1231 q->p_pgid != -SCARG(uap, id)) {
1232 DPRINTF(("pid %d pgid %d != %d\n", q->p_pid,
1233 q->p_pgid, SCARG(uap, id)));
1237 if (q->p_stat == SZOMB &&
1238 ((SCARG(uap, options) & (SVR4_WEXITED|SVR4_WTRAPPED)))) {
1240 DPRINTF(("found %d\n", q->p_pid));
1241 if ((error = svr4_setinfo(q, q->p_xstat,
1242 SCARG(uap, info))) != 0)
1246 if ((SCARG(uap, options) & SVR4_WNOWAIT)) {
1247 DPRINTF(("Don't wait\n"));
1252 * If we got the child via ptrace(2) or procfs, and
1253 * the parent is different (meaning the process was
1254 * attached, rather than run as a child), then we need
1255 * to give it back to the ol dparent, and send the
1256 * parent a SIGCHLD. The rest of the cleanup will be
1257 * done when the old parent waits on the child.
1259 if ((q->p_flag & P_TRACED) &&
1260 q->p_oppid != q->p_pptr->p_pid) {
1261 t = pfind(q->p_oppid);
1262 proc_reparent(q, t ? t : initproc);
1264 q->p_flag &= ~(P_TRACED | P_WAITED);
1265 wakeup((caddr_t)q->p_pptr);
1269 ruadd(&p->p_stats->p_cru, q->p_ru);
1271 FREE(q->p_ru, M_ZOMBIE);
1274 * Finally finished with old proc entry.
1275 * Unlink it from its process group and free it.
1279 LIST_REMOVE(q, p_list); /* off zombproc */
1281 LIST_REMOVE(q, p_sibling);
1284 * Decrement the count of procs running with this uid.
1286 (void)chgproccnt(q->p_ucred->cr_uidinfo, -1, 0);
1289 * Free up credentials.
1295 * Release reference to text vnode
1301 * Give machine-dependent layer a chance
1302 * to free anything that cpu_exit couldn't
1303 * release while still running in process context.
1306 /* XXX what about process 'q' itself? zfree? */
1307 #if defined(__NetBSD__)
1308 pool_put(&proc_pool, q);
1313 if (q->p_stat == SSTOP && (q->p_flag & P_WAITED) == 0 &&
1314 (q->p_flag & P_TRACED ||
1315 (SCARG(uap, options) & (SVR4_WSTOPPED|SVR4_WCONTINUED)))) {
1316 DPRINTF(("jobcontrol %d\n", q->p_pid));
1317 if (((SCARG(uap, options) & SVR4_WNOWAIT)) == 0)
1318 q->p_flag |= P_WAITED;
1320 return svr4_setinfo(q, W_STOPCODE(q->p_xstat),
1328 if (SCARG(uap, options) & SVR4_WNOHANG) {
1330 if ((error = svr4_setinfo(NULL, 0, SCARG(uap, info))) != 0)
1335 if ((error = tsleep((caddr_t)p, PCATCH, "svr4_wait", 0)) != 0)
1342 bsd_statfs_to_svr4_statvfs(bfs, sfs)
1343 const struct statfs *bfs;
1344 struct svr4_statvfs *sfs;
1346 sfs->f_bsize = bfs->f_iosize; /* XXX */
1347 sfs->f_frsize = bfs->f_bsize;
1348 sfs->f_blocks = bfs->f_blocks;
1349 sfs->f_bfree = bfs->f_bfree;
1350 sfs->f_bavail = bfs->f_bavail;
1351 sfs->f_files = bfs->f_files;
1352 sfs->f_ffree = bfs->f_ffree;
1353 sfs->f_favail = bfs->f_ffree;
1354 sfs->f_fsid = bfs->f_fsid.val[0];
1355 memcpy(sfs->f_basetype, bfs->f_fstypename, sizeof(sfs->f_basetype));
1357 if (bfs->f_flags & MNT_RDONLY)
1358 sfs->f_flag |= SVR4_ST_RDONLY;
1359 if (bfs->f_flags & MNT_NOSUID)
1360 sfs->f_flag |= SVR4_ST_NOSUID;
1361 sfs->f_namemax = MAXNAMLEN;
1362 memcpy(sfs->f_fstr, bfs->f_fstypename, sizeof(sfs->f_fstr)); /* XXX */
1363 memset(sfs->f_filler, 0, sizeof(sfs->f_filler));
1368 bsd_statfs_to_svr4_statvfs64(bfs, sfs)
1369 const struct statfs *bfs;
1370 struct svr4_statvfs64 *sfs;
1372 sfs->f_bsize = bfs->f_iosize; /* XXX */
1373 sfs->f_frsize = bfs->f_bsize;
1374 sfs->f_blocks = bfs->f_blocks;
1375 sfs->f_bfree = bfs->f_bfree;
1376 sfs->f_bavail = bfs->f_bavail;
1377 sfs->f_files = bfs->f_files;
1378 sfs->f_ffree = bfs->f_ffree;
1379 sfs->f_favail = bfs->f_ffree;
1380 sfs->f_fsid = bfs->f_fsid.val[0];
1381 memcpy(sfs->f_basetype, bfs->f_fstypename, sizeof(sfs->f_basetype));
1383 if (bfs->f_flags & MNT_RDONLY)
1384 sfs->f_flag |= SVR4_ST_RDONLY;
1385 if (bfs->f_flags & MNT_NOSUID)
1386 sfs->f_flag |= SVR4_ST_NOSUID;
1387 sfs->f_namemax = MAXNAMLEN;
1388 memcpy(sfs->f_fstr, bfs->f_fstypename, sizeof(sfs->f_fstr)); /* XXX */
1389 memset(sfs->f_filler, 0, sizeof(sfs->f_filler));
1394 svr4_sys_statvfs(struct svr4_sys_statvfs_args *uap)
1396 struct statfs_args fs_args;
1397 caddr_t sg = stackgap_init();
1398 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1400 struct svr4_statvfs sfs;
1403 CHECKALTEXIST(&sg, SCARG(uap, path));
1404 SCARG(&fs_args, path) = SCARG(uap, path);
1405 SCARG(&fs_args, buf) = fs;
1407 if ((error = statfs(&fs_args)) != 0)
1410 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1413 uap->sysmsg_result = fs_args.sysmsg_result;
1415 bsd_statfs_to_svr4_statvfs(&bfs, &sfs);
1417 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1422 svr4_sys_fstatvfs(struct svr4_sys_fstatvfs_args *uap)
1424 struct fstatfs_args fs_args;
1425 caddr_t sg = stackgap_init();
1426 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1428 struct svr4_statvfs sfs;
1431 SCARG(&fs_args, fd) = SCARG(uap, fd);
1432 SCARG(&fs_args, buf) = fs;
1434 if ((error = fstatfs(&fs_args)) != 0)
1437 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1440 uap->sysmsg_result = fs_args.sysmsg_result;
1442 bsd_statfs_to_svr4_statvfs(&bfs, &sfs);
1444 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1449 svr4_sys_statvfs64(struct svr4_sys_statvfs64_args *uap)
1451 struct statfs_args fs_args;
1452 caddr_t sg = stackgap_init();
1453 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1455 struct svr4_statvfs64 sfs;
1458 CHECKALTEXIST(&sg, SCARG(uap, path));
1459 SCARG(&fs_args, path) = SCARG(uap, path);
1460 SCARG(&fs_args, buf) = fs;
1462 if ((error = statfs(&fs_args)) != 0)
1465 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1468 uap->sysmsg_result = fs_args.sysmsg_result;
1470 bsd_statfs_to_svr4_statvfs64(&bfs, &sfs);
1472 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1477 svr4_sys_fstatvfs64(struct svr4_sys_fstatvfs64_args *uap)
1479 struct fstatfs_args fs_args;
1480 caddr_t sg = stackgap_init();
1481 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1483 struct svr4_statvfs64 sfs;
1486 SCARG(&fs_args, fd) = SCARG(uap, fd);
1487 SCARG(&fs_args, buf) = fs;
1489 if ((error = fstatfs(&fs_args)) != 0)
1492 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1495 uap->sysmsg_result = fs_args.sysmsg_result;
1497 bsd_statfs_to_svr4_statvfs64(&bfs, &sfs);
1499 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1503 svr4_sys_alarm(struct svr4_sys_alarm_args *uap)
1506 struct itimerval *itp, *oitp;
1507 struct setitimer_args sa;
1508 caddr_t sg = stackgap_init();
1510 itp = stackgap_alloc(&sg, sizeof(*itp));
1511 oitp = stackgap_alloc(&sg, sizeof(*oitp));
1512 timevalclear(&itp->it_interval);
1513 itp->it_value.tv_sec = SCARG(uap, sec);
1514 itp->it_value.tv_usec = 0;
1516 SCARG(&sa, which) = ITIMER_REAL;
1517 SCARG(&sa, itv) = itp;
1518 SCARG(&sa, oitv) = oitp;
1519 error = setitimer(&sa);
1522 if (oitp->it_value.tv_usec)
1523 oitp->it_value.tv_sec++;
1524 uap->sysmsg_result = oitp->it_value.tv_sec;
1530 svr4_sys_gettimeofday(struct svr4_sys_gettimeofday_args *uap)
1532 if (SCARG(uap, tp)) {
1536 return copyout(&atv, SCARG(uap, tp), sizeof (atv));
1543 svr4_sys_facl(struct svr4_sys_facl_args *uap)
1547 retval = &uap->sysmsg_result;
1550 switch (SCARG(uap, cmd)) {
1551 case SVR4_SYS_SETACL:
1552 /* We don't support acls on any filesystem */
1555 case SVR4_SYS_GETACL:
1556 return copyout(retval, &SCARG(uap, num),
1557 sizeof(SCARG(uap, num)));
1559 case SVR4_SYS_GETACLCNT:
1569 svr4_sys_acl(struct svr4_sys_acl_args *uap)
1571 /* XXX: for now the same */
1572 return svr4_sys_facl((struct svr4_sys_facl_args *)uap);
1576 svr4_sys_auditsys(struct svr4_sys_auditsys_args *uap)
1579 * XXX: Big brother is *not* watching.
1585 svr4_sys_memcntl(struct svr4_sys_memcntl_args *uap)
1587 switch (SCARG(uap, cmd)) {
1590 struct msync_args msa;
1593 SCARG(&msa, addr) = SCARG(uap, addr);
1594 SCARG(&msa, len) = SCARG(uap, len);
1595 SCARG(&msa, flags) = (int)SCARG(uap, arg);
1597 msa.sysmsg_result = 0;
1598 error = msync(&msa);
1599 uap->sysmsg_result = msa.sysmsg_result;
1602 case SVR4_MC_ADVISE:
1604 struct madvise_args maa;
1607 SCARG(&maa, addr) = SCARG(uap, addr);
1608 SCARG(&maa, len) = SCARG(uap, len);
1609 SCARG(&maa, behav) = (int)SCARG(uap, arg);
1611 maa.sysmsg_result = 0;
1612 error = madvise(&maa);
1613 uap->sysmsg_result = maa.sysmsg_result;
1617 case SVR4_MC_UNLOCK:
1618 case SVR4_MC_LOCKAS:
1619 case SVR4_MC_UNLOCKAS:
1628 svr4_sys_nice(struct svr4_sys_nice_args *uap)
1630 struct setpriority_args ap;
1633 SCARG(&ap, which) = PRIO_PROCESS;
1634 SCARG(&ap, who) = 0;
1635 SCARG(&ap, prio) = SCARG(uap, prio);
1637 if ((error = setpriority(&ap)) != 0)
1640 /* the cast is stupid, but the structures are the same */
1641 if ((error = getpriority((struct getpriority_args *)&ap)) != 0)
1644 uap->sysmsg_result = ap.sysmsg_result;
1650 svr4_sys_resolvepath(struct svr4_sys_resolvepath_args *uap)
1652 struct thread *td = curthread; /* XXX */
1653 struct nameidata nd;
1657 retval = &uap->sysmsg_result;
1659 NDINIT(&nd, NAMEI_LOOKUP, CNP_SAVENAME, UIO_USERSPACE,
1660 SCARG(uap, path), td);
1662 if ((error = namei(&nd)) != 0)
1665 if ((error = copyout(nd.ni_cnd.cn_pnbuf, SCARG(uap, buf),
1666 SCARG(uap, bufsiz))) != 0)
1669 *retval = strlen(nd.ni_cnd.cn_pnbuf) < SCARG(uap, bufsiz) ?
1670 strlen(nd.ni_cnd.cn_pnbuf) + 1 : SCARG(uap, bufsiz);
1672 NDFREE(&nd, NDF_ONLY_PNBUF);