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.22 2004/04/24 04:32:03 drhodus 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(__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, NULL, 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, NULL, 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, NULL, 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, NULL, 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, NULL, LK_EXCLUSIVE | LK_RETRY, td);
613 if (vp->v_type != VDIR)
616 error = VOP_ACCESS(vp, VEXEC, cred, td);
617 VOP_UNLOCK(vp, NULL, 0, td);
621 if (fdp->fd_rdir != NULL)
629 svr4_mknod(retval, path, mode, dev)
635 caddr_t sg = stackgap_init();
638 CHECKALTEXIST(&sg, path);
640 if (S_ISFIFO(mode)) {
641 struct mkfifo_args ap;
642 SCARG(&ap, path) = path;
643 SCARG(&ap, mode) = mode;
645 *retval = ap.sysmsg_result;
647 struct mknod_args ap;
648 SCARG(&ap, path) = path;
649 SCARG(&ap, mode) = mode;
650 SCARG(&ap, dev) = dev;
652 *retval = ap.sysmsg_result;
659 svr4_sys_mknod(struct svr4_sys_mknod_args *uap)
663 retval = &uap->sysmsg_result;
665 return svr4_mknod(retval, SCARG(uap, path), SCARG(uap, mode),
666 (svr4_dev_t)svr4_to_bsd_odev_t(SCARG(uap, dev)));
671 svr4_sys_xmknod(struct svr4_sys_xmknod_args *uap)
675 retval = &uap->sysmsg_result;
677 return svr4_mknod(retval, SCARG(uap, path), SCARG(uap, mode),
678 (svr4_dev_t)svr4_to_bsd_dev_t(SCARG(uap, dev)));
683 svr4_sys_vhangup(struct svr4_sys_vhangup_args *uap)
690 svr4_sys_sysconfig(struct svr4_sys_sysconfig_args *uap)
694 retval = &uap->sysmsg_result;
696 switch (SCARG(uap, name)) {
697 case SVR4_CONFIG_UNUSED:
700 case SVR4_CONFIG_NGROUPS:
701 *retval = NGROUPS_MAX;
703 case SVR4_CONFIG_CHILD_MAX:
706 case SVR4_CONFIG_OPEN_FILES:
709 case SVR4_CONFIG_POSIX_VER:
712 case SVR4_CONFIG_PAGESIZE:
715 case SVR4_CONFIG_CLK_TCK:
716 *retval = 60; /* should this be `hz', ie. 100? */
718 case SVR4_CONFIG_XOPEN_VER:
719 *retval = 2; /* XXX: What should that be? */
721 case SVR4_CONFIG_PROF_TCK:
722 *retval = 60; /* XXX: What should that be? */
724 case SVR4_CONFIG_NPROC_CONF:
725 *retval = 1; /* Only one processor for now */
727 case SVR4_CONFIG_NPROC_ONLN:
728 *retval = 1; /* And it better be online */
730 case SVR4_CONFIG_AIO_LISTIO_MAX:
731 case SVR4_CONFIG_AIO_MAX:
732 case SVR4_CONFIG_AIO_PRIO_DELTA_MAX:
733 *retval = 0; /* No aio support */
735 case SVR4_CONFIG_DELAYTIMER_MAX:
736 *retval = 0; /* No delaytimer support */
738 case SVR4_CONFIG_MQ_OPEN_MAX:
739 *retval = msginfo.msgmni;
741 case SVR4_CONFIG_MQ_PRIO_MAX:
742 *retval = 0; /* XXX: Don't know */
744 case SVR4_CONFIG_RTSIG_MAX:
747 case SVR4_CONFIG_SEM_NSEMS_MAX:
748 *retval = seminfo.semmni;
750 case SVR4_CONFIG_SEM_VALUE_MAX:
751 *retval = seminfo.semvmx;
753 case SVR4_CONFIG_SIGQUEUE_MAX:
754 *retval = 0; /* XXX: Don't know */
756 case SVR4_CONFIG_SIGRT_MIN:
757 case SVR4_CONFIG_SIGRT_MAX:
758 *retval = 0; /* No real time signals */
760 case SVR4_CONFIG_TIMER_MAX:
761 *retval = 3; /* XXX: real, virtual, profiling */
764 case SVR4_CONFIG_PHYS_PAGES:
766 *retval = uvmexp.free; /* XXX: free instead of total */
768 *retval = cnt.v_free_count; /* XXX: free instead of total */
771 case SVR4_CONFIG_AVPHYS_PAGES:
773 *retval = uvmexp.active; /* XXX: active instead of avg */
775 *retval = cnt.v_active_count; /* XXX: active instead of avg */
786 extern int swap_pager_full;
790 svr4_sys_break(struct svr4_sys_break_args *uap)
792 struct thread *td = curthread; /* XXX */
793 struct proc *p = td->td_proc;
795 vm_offset_t new, old, base, ns;
801 base = round_page((vm_offset_t) vm->vm_daddr);
802 ns = (vm_offset_t)SCARG(uap, nsize);
803 new = round_page(ns);
805 if ((new - base) > (unsigned) p->p_rlimit[RLIMIT_DATA].rlim_cur) {
808 if (new >= VM_MAXUSER_ADDRESS) {
811 } else if (new < base) {
813 * This is simply an invalid value. If someone wants to
814 * do fancy address space manipulations, mmap and munmap
815 * can do most of what the user would want.
820 old = base + ctob(vm->vm_dsize);
825 if (vm->vm_map.size + diff > p->p_rlimit[RLIMIT_VMEM].rlim_cur)
827 rv = vm_map_find(&vm->vm_map, NULL, 0, &old, diff, FALSE,
828 VM_PROT_ALL, VM_PROT_ALL, 0);
829 if (rv != KERN_SUCCESS) {
832 vm->vm_dsize += btoc(diff);
833 } else if (new < old) {
834 rv = vm_map_remove(&vm->vm_map, new, old);
835 if (rv != KERN_SUCCESS) {
838 vm->vm_dsize -= btoc(old - new);
844 static __inline clock_t
845 timeval_to_clock_t(tv)
848 return tv->tv_sec * hz + tv->tv_usec / (1000000 / hz);
853 svr4_sys_times(struct svr4_sys_times_args *uap)
855 int error, *retval = &uap->sysmsg_result;
860 struct getrusage_args ga;
862 caddr_t sg = stackgap_init();
863 ru = stackgap_alloc(&sg, sizeof(struct rusage));
865 SCARG(&ga, who) = RUSAGE_SELF;
866 SCARG(&ga, rusage) = ru;
868 error = getrusage(&ga);
872 if ((error = copyin(ru, &r, sizeof r)) != 0)
875 tms.tms_utime = timeval_to_clock_t(&r.ru_utime);
876 tms.tms_stime = timeval_to_clock_t(&r.ru_stime);
878 SCARG(&ga, who) = RUSAGE_CHILDREN;
879 error = getrusage(&ga);
883 if ((error = copyin(ru, &r, sizeof r)) != 0)
886 tms.tms_cutime = timeval_to_clock_t(&r.ru_utime);
887 tms.tms_cstime = timeval_to_clock_t(&r.ru_stime);
890 *retval = timeval_to_clock_t(&t);
892 return copyout(&tms, SCARG(uap, tp), sizeof(tms));
897 svr4_sys_ulimit(struct svr4_sys_ulimit_args *uap)
899 struct proc *p = curproc;
900 int *retval = &uap->sysmsg_result;
902 switch (SCARG(uap, cmd)) {
904 *retval = p->p_rlimit[RLIMIT_FSIZE].rlim_cur / 512;
906 *retval = 0x7fffffff;
912 struct __setrlimit_args srl;
914 caddr_t sg = stackgap_init();
915 struct rlimit *url = (struct rlimit *)
916 stackgap_alloc(&sg, sizeof *url);
918 krl.rlim_cur = SCARG(uap, newlimit) * 512;
919 krl.rlim_max = p->p_rlimit[RLIMIT_FSIZE].rlim_max;
921 error = copyout(&krl, url, sizeof(*url));
925 SCARG(&srl, which) = RLIMIT_FSIZE;
926 SCARG(&srl, rlp) = (struct rlimit *)url;
928 error = setrlimit(&srl);
932 *retval = p->p_rlimit[RLIMIT_FSIZE].rlim_cur;
934 *retval = 0x7fffffff;
940 struct vmspace *vm = p->p_vmspace;
941 register_t r = p->p_rlimit[RLIMIT_DATA].rlim_cur;
945 r += (long) vm->vm_daddr;
953 *retval = p->p_rlimit[RLIMIT_NOFILE].rlim_cur;
955 *retval = 0x7fffffff;
969 /* look in the live processes */
970 if ((p = pfind(pid)) != NULL)
973 /* look in the zombies */
974 for (p = zombproc.lh_first; p != 0; p = p->p_list.le_next)
983 svr4_sys_pgrpsys(struct svr4_sys_pgrpsys_args *uap)
985 struct proc *p = curproc;
986 int *retval = &uap->sysmsg_result;
988 switch (SCARG(uap, cmd)) {
989 case 1: /* setpgrp() */
991 * SVR4 setpgrp() (which takes no arguments) has the
992 * semantics that the session ID is also created anew, so
993 * in almost every sense, setpgrp() is identical to
994 * setsid() for SVR4. (Under BSD, the difference is that
995 * a setpgid(0,0) will not create a new session.)
998 struct setsid_args sida;
1000 sida.sysmsg_result = 0;
1002 /* ignore result? */
1006 case 0: /* getpgrp() */
1007 *retval = p->p_pgrp->pg_id;
1010 case 2: /* getsid(pid) */
1011 if (SCARG(uap, pid) != 0 &&
1012 (p = svr4_pfind(SCARG(uap, pid))) == NULL)
1015 * This has already been initialized to the pid of
1016 * the session leader.
1018 *retval = (register_t) p->p_session->s_leader->p_pid;
1021 case 3: /* setsid() */
1024 struct setsid_args sida;
1027 sida.sysmsg_result = 0;
1028 error = setsid(&sida);
1029 uap->sysmsg_result = sida.sysmsg_result;
1032 case 4: /* getpgid(pid) */
1034 if (SCARG(uap, pid) != 0 &&
1035 (p = svr4_pfind(SCARG(uap, pid))) == NULL)
1038 *retval = (int) p->p_pgrp->pg_id;
1041 case 5: /* setpgid(pid, pgid); */
1043 struct setpgid_args sa;
1046 SCARG(&sa, pid) = SCARG(uap, pid);
1047 SCARG(&sa, pgid) = SCARG(uap, pgid);
1048 sa.sysmsg_result = 0;
1049 error = setpgid(&sa);
1050 uap->sysmsg_result = sa.sysmsg_result;
1058 #define syscallarg(x) union { x datum; register_t pad; }
1060 struct svr4_hrtcntl_args {
1064 svr4_hrt_interval_t * iv;
1065 svr4_hrt_time_t * ti;
1070 svr4_hrtcntl(struct svr4_hrtcntl_args *uap, register_t *retval)
1072 switch (SCARG(uap, fun)) {
1073 case SVR4_HRT_CNTL_RES:
1074 DPRINTF(("htrcntl(RES)\n"));
1075 *retval = SVR4_HRT_USEC;
1078 case SVR4_HRT_CNTL_TOFD:
1079 DPRINTF(("htrcntl(TOFD)\n"));
1083 if (SCARG(uap, clk) != SVR4_HRT_CLK_STD) {
1084 DPRINTF(("clk == %d\n", SCARG(uap, clk)));
1087 if (SCARG(uap, ti) == NULL) {
1088 DPRINTF(("ti NULL\n"));
1092 t.h_sec = tv.tv_sec;
1093 t.h_rem = tv.tv_usec;
1094 t.h_res = SVR4_HRT_USEC;
1095 return copyout(&t, SCARG(uap, ti), sizeof(t));
1098 case SVR4_HRT_CNTL_START:
1099 DPRINTF(("htrcntl(START)\n"));
1102 case SVR4_HRT_CNTL_GET:
1103 DPRINTF(("htrcntl(GET)\n"));
1106 DPRINTF(("Bad htrcntl command %d\n", SCARG(uap, fun)));
1113 svr4_sys_hrtsys(struct svr4_sys_hrtsys_args *uap)
1115 int *retval = &uap->sysmsg_result;
1117 switch (SCARG(uap, cmd)) {
1119 return svr4_hrtcntl((struct svr4_hrtcntl_args *)(&uap->sysmsg + 1), retval);
1122 DPRINTF(("hrtalarm\n"));
1126 DPRINTF(("hrtsleep\n"));
1130 DPRINTF(("hrtcancel\n"));
1134 DPRINTF(("Bad hrtsys command %d\n", SCARG(uap, cmd)));
1141 svr4_setinfo(p, st, s)
1149 memset(&i, 0, sizeof(i));
1151 i.si_signo = SVR4_SIGCHLD;
1152 i.si_errno = 0; /* XXX? */
1155 i.si_pid = p->p_pid;
1156 if (p->p_stat == SZOMB) {
1157 i.si_stime = p->p_ru->ru_stime.tv_sec;
1158 i.si_utime = p->p_ru->ru_utime.tv_sec;
1161 i.si_stime = p->p_stats->p_ru.ru_stime.tv_sec;
1162 i.si_utime = p->p_stats->p_ru.ru_utime.tv_sec;
1166 if (WIFEXITED(st)) {
1167 i.si_status = WEXITSTATUS(st);
1168 i.si_code = SVR4_CLD_EXITED;
1169 } else if (WIFSTOPPED(st)) {
1171 if (sig >= 0 && sig < NSIG)
1172 i.si_status = SVR4_BSD2SVR4_SIG(sig);
1174 if (i.si_status == SVR4_SIGCONT)
1175 i.si_code = SVR4_CLD_CONTINUED;
1177 i.si_code = SVR4_CLD_STOPPED;
1180 if (sig >= 0 && sig < NSIG)
1181 i.si_status = SVR4_BSD2SVR4_SIG(sig);
1184 i.si_code = SVR4_CLD_DUMPED;
1186 i.si_code = SVR4_CLD_KILLED;
1189 DPRINTF(("siginfo [pid %ld signo %d code %d errno %d status %d]\n",
1190 i.si_pid, i.si_signo, i.si_code, i.si_errno, i.si_status));
1192 return copyout(&i, s, sizeof(i));
1197 svr4_sys_waitsys(struct svr4_sys_waitsys_args *uap)
1199 struct proc *p = curproc;
1201 int error, *retval = &uap->sysmsg_result;
1205 switch (SCARG(uap, grp)) {
1210 SCARG(uap, id) = -p->p_pgid;
1214 SCARG(uap, id) = WAIT_ANY;
1221 DPRINTF(("waitsys(%d, %d, %p, %x)\n",
1222 SCARG(uap, grp), SCARG(uap, id),
1223 SCARG(uap, info), SCARG(uap, options)));
1227 for (q = p->p_children.lh_first; q != 0; q = q->p_sibling.le_next) {
1228 if (SCARG(uap, id) != WAIT_ANY &&
1229 q->p_pid != SCARG(uap, id) &&
1230 q->p_pgid != -SCARG(uap, id)) {
1231 DPRINTF(("pid %d pgid %d != %d\n", q->p_pid,
1232 q->p_pgid, SCARG(uap, id)));
1236 if (q->p_stat == SZOMB &&
1237 ((SCARG(uap, options) & (SVR4_WEXITED|SVR4_WTRAPPED)))) {
1239 DPRINTF(("found %d\n", q->p_pid));
1240 if ((error = svr4_setinfo(q, q->p_xstat,
1241 SCARG(uap, info))) != 0)
1245 if ((SCARG(uap, options) & SVR4_WNOWAIT)) {
1246 DPRINTF(("Don't wait\n"));
1251 * If we got the child via ptrace(2) or procfs, and
1252 * the parent is different (meaning the process was
1253 * attached, rather than run as a child), then we need
1254 * to give it back to the ol dparent, and send the
1255 * parent a SIGCHLD. The rest of the cleanup will be
1256 * done when the old parent waits on the child.
1258 if ((q->p_flag & P_TRACED) &&
1259 q->p_oppid != q->p_pptr->p_pid) {
1260 t = pfind(q->p_oppid);
1261 proc_reparent(q, t ? t : initproc);
1263 q->p_flag &= ~(P_TRACED | P_WAITED);
1264 wakeup((caddr_t)q->p_pptr);
1268 ruadd(&p->p_stats->p_cru, q->p_ru);
1270 FREE(q->p_ru, M_ZOMBIE);
1273 * Finally finished with old proc entry.
1274 * Unlink it from its process group and free it.
1278 LIST_REMOVE(q, p_list); /* off zombproc */
1280 LIST_REMOVE(q, p_sibling);
1283 * Decrement the count of procs running with this uid.
1285 (void)chgproccnt(q->p_ucred->cr_uidinfo, -1, 0);
1288 * Free up credentials.
1294 * Release reference to text vnode
1300 * Give machine-dependent layer a chance
1301 * to free anything that cpu_exit couldn't
1302 * release while still running in process context.
1305 /* XXX what about process 'q' itself? zfree? */
1306 #if defined(__NetBSD__)
1307 pool_put(&proc_pool, q);
1312 if (q->p_stat == SSTOP && (q->p_flag & P_WAITED) == 0 &&
1313 (q->p_flag & P_TRACED ||
1314 (SCARG(uap, options) & (SVR4_WSTOPPED|SVR4_WCONTINUED)))) {
1315 DPRINTF(("jobcontrol %d\n", q->p_pid));
1316 if (((SCARG(uap, options) & SVR4_WNOWAIT)) == 0)
1317 q->p_flag |= P_WAITED;
1319 return svr4_setinfo(q, W_STOPCODE(q->p_xstat),
1327 if (SCARG(uap, options) & SVR4_WNOHANG) {
1329 if ((error = svr4_setinfo(NULL, 0, SCARG(uap, info))) != 0)
1334 if ((error = tsleep((caddr_t)p, PCATCH, "svr4_wait", 0)) != 0)
1341 bsd_statfs_to_svr4_statvfs(bfs, sfs)
1342 const struct statfs *bfs;
1343 struct svr4_statvfs *sfs;
1345 sfs->f_bsize = bfs->f_iosize; /* XXX */
1346 sfs->f_frsize = bfs->f_bsize;
1347 sfs->f_blocks = bfs->f_blocks;
1348 sfs->f_bfree = bfs->f_bfree;
1349 sfs->f_bavail = bfs->f_bavail;
1350 sfs->f_files = bfs->f_files;
1351 sfs->f_ffree = bfs->f_ffree;
1352 sfs->f_favail = bfs->f_ffree;
1353 sfs->f_fsid = bfs->f_fsid.val[0];
1354 memcpy(sfs->f_basetype, bfs->f_fstypename, sizeof(sfs->f_basetype));
1356 if (bfs->f_flags & MNT_RDONLY)
1357 sfs->f_flag |= SVR4_ST_RDONLY;
1358 if (bfs->f_flags & MNT_NOSUID)
1359 sfs->f_flag |= SVR4_ST_NOSUID;
1360 sfs->f_namemax = MAXNAMLEN;
1361 memcpy(sfs->f_fstr, bfs->f_fstypename, sizeof(sfs->f_fstr)); /* XXX */
1362 memset(sfs->f_filler, 0, sizeof(sfs->f_filler));
1367 bsd_statfs_to_svr4_statvfs64(bfs, sfs)
1368 const struct statfs *bfs;
1369 struct svr4_statvfs64 *sfs;
1371 sfs->f_bsize = bfs->f_iosize; /* XXX */
1372 sfs->f_frsize = bfs->f_bsize;
1373 sfs->f_blocks = bfs->f_blocks;
1374 sfs->f_bfree = bfs->f_bfree;
1375 sfs->f_bavail = bfs->f_bavail;
1376 sfs->f_files = bfs->f_files;
1377 sfs->f_ffree = bfs->f_ffree;
1378 sfs->f_favail = bfs->f_ffree;
1379 sfs->f_fsid = bfs->f_fsid.val[0];
1380 memcpy(sfs->f_basetype, bfs->f_fstypename, sizeof(sfs->f_basetype));
1382 if (bfs->f_flags & MNT_RDONLY)
1383 sfs->f_flag |= SVR4_ST_RDONLY;
1384 if (bfs->f_flags & MNT_NOSUID)
1385 sfs->f_flag |= SVR4_ST_NOSUID;
1386 sfs->f_namemax = MAXNAMLEN;
1387 memcpy(sfs->f_fstr, bfs->f_fstypename, sizeof(sfs->f_fstr)); /* XXX */
1388 memset(sfs->f_filler, 0, sizeof(sfs->f_filler));
1393 svr4_sys_statvfs(struct svr4_sys_statvfs_args *uap)
1395 struct statfs_args fs_args;
1396 caddr_t sg = stackgap_init();
1397 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1399 struct svr4_statvfs sfs;
1402 CHECKALTEXIST(&sg, SCARG(uap, path));
1403 SCARG(&fs_args, path) = SCARG(uap, path);
1404 SCARG(&fs_args, buf) = fs;
1406 if ((error = statfs(&fs_args)) != 0)
1409 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1412 uap->sysmsg_result = fs_args.sysmsg_result;
1414 bsd_statfs_to_svr4_statvfs(&bfs, &sfs);
1416 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1421 svr4_sys_fstatvfs(struct svr4_sys_fstatvfs_args *uap)
1423 struct fstatfs_args fs_args;
1424 caddr_t sg = stackgap_init();
1425 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1427 struct svr4_statvfs sfs;
1430 SCARG(&fs_args, fd) = SCARG(uap, fd);
1431 SCARG(&fs_args, buf) = fs;
1433 if ((error = fstatfs(&fs_args)) != 0)
1436 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1439 uap->sysmsg_result = fs_args.sysmsg_result;
1441 bsd_statfs_to_svr4_statvfs(&bfs, &sfs);
1443 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1448 svr4_sys_statvfs64(struct svr4_sys_statvfs64_args *uap)
1450 struct statfs_args fs_args;
1451 caddr_t sg = stackgap_init();
1452 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1454 struct svr4_statvfs64 sfs;
1457 CHECKALTEXIST(&sg, SCARG(uap, path));
1458 SCARG(&fs_args, path) = SCARG(uap, path);
1459 SCARG(&fs_args, buf) = fs;
1461 if ((error = statfs(&fs_args)) != 0)
1464 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1467 uap->sysmsg_result = fs_args.sysmsg_result;
1469 bsd_statfs_to_svr4_statvfs64(&bfs, &sfs);
1471 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1476 svr4_sys_fstatvfs64(struct svr4_sys_fstatvfs64_args *uap)
1478 struct fstatfs_args fs_args;
1479 caddr_t sg = stackgap_init();
1480 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1482 struct svr4_statvfs64 sfs;
1485 SCARG(&fs_args, fd) = SCARG(uap, fd);
1486 SCARG(&fs_args, buf) = fs;
1488 if ((error = fstatfs(&fs_args)) != 0)
1491 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1494 uap->sysmsg_result = fs_args.sysmsg_result;
1496 bsd_statfs_to_svr4_statvfs64(&bfs, &sfs);
1498 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1502 svr4_sys_alarm(struct svr4_sys_alarm_args *uap)
1505 struct itimerval *itp, *oitp;
1506 struct setitimer_args sa;
1507 caddr_t sg = stackgap_init();
1509 itp = stackgap_alloc(&sg, sizeof(*itp));
1510 oitp = stackgap_alloc(&sg, sizeof(*oitp));
1511 timevalclear(&itp->it_interval);
1512 itp->it_value.tv_sec = SCARG(uap, sec);
1513 itp->it_value.tv_usec = 0;
1515 SCARG(&sa, which) = ITIMER_REAL;
1516 SCARG(&sa, itv) = itp;
1517 SCARG(&sa, oitv) = oitp;
1518 error = setitimer(&sa);
1521 if (oitp->it_value.tv_usec)
1522 oitp->it_value.tv_sec++;
1523 uap->sysmsg_result = oitp->it_value.tv_sec;
1529 svr4_sys_gettimeofday(struct svr4_sys_gettimeofday_args *uap)
1531 if (SCARG(uap, tp)) {
1535 return copyout(&atv, SCARG(uap, tp), sizeof (atv));
1542 svr4_sys_facl(struct svr4_sys_facl_args *uap)
1546 retval = &uap->sysmsg_result;
1549 switch (SCARG(uap, cmd)) {
1550 case SVR4_SYS_SETACL:
1551 /* We don't support acls on any filesystem */
1554 case SVR4_SYS_GETACL:
1555 return copyout(retval, &SCARG(uap, num),
1556 sizeof(SCARG(uap, num)));
1558 case SVR4_SYS_GETACLCNT:
1568 svr4_sys_acl(struct svr4_sys_acl_args *uap)
1570 /* XXX: for now the same */
1571 return svr4_sys_facl((struct svr4_sys_facl_args *)uap);
1575 svr4_sys_auditsys(struct svr4_sys_auditsys_args *uap)
1578 * XXX: Big brother is *not* watching.
1584 svr4_sys_memcntl(struct svr4_sys_memcntl_args *uap)
1586 switch (SCARG(uap, cmd)) {
1589 struct msync_args msa;
1592 SCARG(&msa, addr) = SCARG(uap, addr);
1593 SCARG(&msa, len) = SCARG(uap, len);
1594 SCARG(&msa, flags) = (int)SCARG(uap, arg);
1596 msa.sysmsg_result = 0;
1597 error = msync(&msa);
1598 uap->sysmsg_result = msa.sysmsg_result;
1601 case SVR4_MC_ADVISE:
1603 struct madvise_args maa;
1606 SCARG(&maa, addr) = SCARG(uap, addr);
1607 SCARG(&maa, len) = SCARG(uap, len);
1608 SCARG(&maa, behav) = (int)SCARG(uap, arg);
1610 maa.sysmsg_result = 0;
1611 error = madvise(&maa);
1612 uap->sysmsg_result = maa.sysmsg_result;
1616 case SVR4_MC_UNLOCK:
1617 case SVR4_MC_LOCKAS:
1618 case SVR4_MC_UNLOCKAS:
1627 svr4_sys_nice(struct svr4_sys_nice_args *uap)
1629 struct setpriority_args ap;
1632 SCARG(&ap, which) = PRIO_PROCESS;
1633 SCARG(&ap, who) = 0;
1634 SCARG(&ap, prio) = SCARG(uap, prio);
1636 if ((error = setpriority(&ap)) != 0)
1639 /* the cast is stupid, but the structures are the same */
1640 if ((error = getpriority((struct getpriority_args *)&ap)) != 0)
1643 uap->sysmsg_result = ap.sysmsg_result;
1649 svr4_sys_resolvepath(struct svr4_sys_resolvepath_args *uap)
1651 struct thread *td = curthread; /* XXX */
1652 struct nameidata nd;
1656 retval = &uap->sysmsg_result;
1658 NDINIT(&nd, NAMEI_LOOKUP, CNP_SAVENAME, UIO_USERSPACE,
1659 SCARG(uap, path), td);
1661 if ((error = namei(&nd)) != 0)
1664 if ((error = copyout(nd.ni_cnd.cn_pnbuf, SCARG(uap, buf),
1665 SCARG(uap, bufsiz))) != 0)
1668 *retval = strlen(nd.ni_cnd.cn_pnbuf) < SCARG(uap, bufsiz) ?
1669 strlen(nd.ni_cnd.cn_pnbuf) + 1 : SCARG(uap, bufsiz);
1671 NDFREE(&nd, NDF_ONLY_PNBUF);