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.2 2003/06/17 04:28:57 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/namei.h>
42 #include <sys/dirent.h>
43 #include <sys/malloc.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>
67 #include <svr4/svr4.h>
68 #include <svr4/svr4_types.h>
69 #include <svr4/svr4_signal.h>
70 #include <svr4/svr4_proto.h>
71 #include <svr4/svr4_util.h>
72 #include <svr4/svr4_sysconfig.h>
73 #include <svr4/svr4_dirent.h>
74 #include <svr4/svr4_acl.h>
75 #include <svr4/svr4_ulimit.h>
76 #include <svr4/svr4_statvfs.h>
77 #include <svr4/svr4_hrt.h>
78 #include <svr4/svr4_mman.h>
79 #include <svr4/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((struct proc *, register_t *, char *,
99 svr4_mode_t, svr4_dev_t));
101 static __inline clock_t timeval_to_clock_t __P((struct timeval *));
102 static int svr4_setinfo __P((struct proc *, int, svr4_siginfo_t *));
104 struct svr4_hrtcntl_args;
105 static int svr4_hrtcntl __P((struct proc *, struct svr4_hrtcntl_args *,
107 static void bsd_statfs_to_svr4_statvfs __P((const struct statfs *,
108 struct svr4_statvfs *));
109 static void bsd_statfs_to_svr4_statvfs64 __P((const struct statfs *,
110 struct svr4_statvfs64 *));
111 static struct proc *svr4_pfind __P((pid_t pid));
116 svr4_sys_setitimer(p, uap)
117 register struct proc *p;
118 struct svr4_sys_setitimer_args *uap;
126 svr4_sys_wait(p, uap)
128 struct svr4_sys_wait_args *uap;
131 int error, *retval = p->p_retval, st, sig;
132 size_t sz = sizeof(*SCARG(&w4, status));
134 SCARG(&w4, rusage) = NULL;
135 SCARG(&w4, options) = 0;
137 if (SCARG(uap, status) == NULL) {
138 caddr_t sg = stackgap_init();
140 SCARG(&w4, status) = stackgap_alloc(&sg, sz);
143 SCARG(&w4, status) = SCARG(uap, status);
145 SCARG(&w4, pid) = WAIT_ANY;
147 if ((error = wait4(p, &w4)) != 0)
150 if ((error = copyin(SCARG(&w4, status), &st, sizeof(st))) != 0)
153 if (WIFSIGNALED(st)) {
155 if (sig >= 0 && sig < NSIG)
156 st = (st & ~0177) | SVR4_BSD2SVR4_SIG(sig);
157 } else if (WIFSTOPPED(st)) {
159 if (sig >= 0 && sig < NSIG)
160 st = (st & ~0xff00) | (SVR4_BSD2SVR4_SIG(sig) << 8);
164 * It looks like wait(2) on svr4/solaris/2.4 returns
165 * the status in retval[1], and the pid on retval[0].
169 if (SCARG(uap, status))
170 if ((error = copyout(&st, SCARG(uap, status), sizeof(st))) != 0)
177 svr4_sys_execv(p, uap)
179 struct svr4_sys_execv_args *uap;
181 struct execve_args ap;
184 sg = stackgap_init();
185 CHECKALTEXIST(p, &sg, SCARG(uap, path));
187 SCARG(&ap, fname) = SCARG(uap, path);
188 SCARG(&ap, argv) = SCARG(uap, argp);
189 SCARG(&ap, envv) = NULL;
191 return execve(p, &ap);
195 svr4_sys_execve(p, uap)
197 struct svr4_sys_execve_args *uap;
199 struct execve_args ap;
202 sg = stackgap_init();
203 CHECKALTEXIST(p, &sg, uap->path);
205 SCARG(&ap, fname) = SCARG(uap, path);
206 SCARG(&ap, argv) = SCARG(uap, argp);
207 SCARG(&ap, envv) = SCARG(uap, envp);
209 return execve(p, &ap);
215 struct svr4_sys_time_args *v;
217 struct svr4_sys_time_args *uap = v;
223 error = copyout(&tv.tv_sec, SCARG(uap, t),
224 sizeof(*(SCARG(uap, t))));
225 p->p_retval[0] = (int) tv.tv_sec;
232 * Read SVR4-style directory entries. We suck them into kernel space so
233 * that they can be massaged before being copied out to user code.
235 * This code is ported from the Linux emulator: Changes to the VFS interface
236 * between FreeBSD and NetBSD have made it simpler to port it from there than
237 * to adapt the NetBSD version.
240 svr4_sys_getdents64(p, uap)
242 struct svr4_sys_getdents64_args *uap;
244 register struct dirent *bdp;
246 caddr_t inp, buf; /* BSD-format */
247 int len, reclen; /* BSD-format */
248 caddr_t outp; /* SVR4-format */
249 int resid, svr4reclen=0; /* SVR4-format */
255 struct svr4_dirent64 svr4_dirent;
256 int buflen, error, eofflag, nbytes, justone;
257 u_long *cookies = NULL, *cookiep;
260 DPRINTF(("svr4_sys_getdents64(%d, *, %d)\n",
261 p->p_pid, SCARG(uap, fd), SCARG(uap, nbytes)));
262 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0) {
266 if ((fp->f_flag & FREAD) == 0)
269 vp = (struct vnode *) fp->f_data;
271 if (vp->v_type != VDIR)
274 if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p))) {
278 nbytes = SCARG(uap, nbytes);
280 nbytes = sizeof (struct svr4_dirent64);
287 #define DIRBLKSIZ 512 /* XXX we used to use ufs's DIRBLKSIZ */
288 buflen = max(DIRBLKSIZ, nbytes);
289 buflen = min(buflen, MAXBSIZE);
290 buf = malloc(buflen, M_TEMP, M_WAITOK);
291 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
294 aiov.iov_len = buflen;
295 auio.uio_iov = &aiov;
297 auio.uio_rw = UIO_READ;
298 auio.uio_segflg = UIO_SYSSPACE;
300 auio.uio_resid = buflen;
301 auio.uio_offset = off;
304 free(cookies, M_TEMP);
308 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
309 &ncookies, &cookies);
315 outp = (caddr_t) SCARG(uap, dp);
317 if ((len = buflen - auio.uio_resid) <= 0) {
325 * When using cookies, the vfs has the option of reading from
326 * a different offset than that supplied (UFS truncates the
327 * offset to a block boundary to make sure that it never reads
328 * partway through a directory entry, even if the directory
329 * has been compacted).
331 while (len > 0 && ncookies > 0 && *cookiep <= off) {
332 bdp = (struct dirent *) inp;
333 len -= bdp->d_reclen;
334 inp += bdp->d_reclen;
341 if (cookiep && ncookies == 0)
343 bdp = (struct dirent *) inp;
344 reclen = bdp->d_reclen;
346 DPRINTF(("svr4_readdir: reclen=%d\n", reclen));
351 if (bdp->d_fileno == 0) {
361 svr4reclen = SVR4_RECLEN(&svr4_dirent, bdp->d_namlen);
362 if (reclen > len || resid < svr4reclen) {
366 svr4_dirent.d_ino = (long) bdp->d_fileno;
369 * old svr4-style readdir usage.
371 svr4_dirent.d_off = (svr4_off_t) svr4reclen;
372 svr4_dirent.d_reclen = (u_short) bdp->d_namlen;
374 svr4_dirent.d_off = (svr4_off_t)(off + reclen);
375 svr4_dirent.d_reclen = (u_short) svr4reclen;
377 strcpy(svr4_dirent.d_name, bdp->d_name);
378 if ((error = copyout((caddr_t)&svr4_dirent, outp, svr4reclen)))
393 if (outp == (caddr_t) SCARG(uap, dp))
398 nbytes = resid + svr4reclen;
401 p->p_retval[0] = nbytes - resid;
404 free(cookies, M_TEMP);
405 VOP_UNLOCK(vp, 0, p);
412 svr4_sys_getdents(p, uap)
414 struct svr4_sys_getdents_args *uap;
418 caddr_t inp, buf; /* BSD-format */
419 int len, reclen; /* BSD-format */
420 caddr_t outp; /* SVR4-format */
421 int resid, svr4_reclen; /* SVR4-format */
425 struct svr4_dirent idb;
426 off_t off; /* true file offset */
427 int buflen, error, eofflag;
428 u_long *cookiebuf = NULL, *cookie;
429 int ncookies = 0, *retval = p->p_retval;
431 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
434 if ((fp->f_flag & FREAD) == 0)
437 vp = (struct vnode *)fp->f_data;
438 if (vp->v_type != VDIR)
441 buflen = min(MAXBSIZE, SCARG(uap, nbytes));
442 buf = malloc(buflen, M_TEMP, M_WAITOK);
443 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
447 aiov.iov_len = buflen;
448 auio.uio_iov = &aiov;
450 auio.uio_rw = UIO_READ;
451 auio.uio_segflg = UIO_SYSSPACE;
453 auio.uio_resid = buflen;
454 auio.uio_offset = off;
456 * First we read into the malloc'ed buffer, then
457 * we massage it into user space, one record at a time.
459 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies,
465 outp = SCARG(uap, buf);
466 resid = SCARG(uap, nbytes);
467 if ((len = buflen - auio.uio_resid) == 0)
470 for (cookie = cookiebuf; len > 0; len -= reclen) {
471 bdp = (struct dirent *)inp;
472 reclen = bdp->d_reclen;
474 panic("svr4_sys_getdents64: bad reclen");
475 off = *cookie++; /* each entry points to the next */
476 if ((off >> 32) != 0) {
477 uprintf("svr4_sys_getdents64: dir offset too large for emulated program");
481 if (bdp->d_fileno == 0) {
482 inp += reclen; /* it is a hole; squish it out */
485 svr4_reclen = SVR4_RECLEN(&idb, bdp->d_namlen);
486 if (reclen > len || resid < svr4_reclen) {
487 /* entry too big for buffer, so just stop */
492 * Massage in place to make a SVR4-shaped dirent (otherwise
493 * we have to worry about touching user memory outside of
494 * the copyout() call).
496 idb.d_ino = (svr4_ino_t)bdp->d_fileno;
497 idb.d_off = (svr4_off_t)off;
498 idb.d_reclen = (u_short)svr4_reclen;
499 strcpy(idb.d_name, bdp->d_name);
500 if ((error = copyout((caddr_t)&idb, outp, svr4_reclen)))
502 /* advance past this real entry */
504 /* advance output past SVR4-shaped entry */
506 resid -= svr4_reclen;
509 /* if we squished out the whole block, try again */
510 if (outp == SCARG(uap, buf))
512 fp->f_offset = off; /* update the vnode offset */
515 *retval = SCARG(uap, nbytes) - resid;
517 VOP_UNLOCK(vp, 0, p);
519 free(cookiebuf, M_TEMP);
526 svr4_sys_mmap(p, uap)
528 struct svr4_sys_mmap_args *uap;
533 retval = p->p_retval;
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);
555 svr4_sys_mmap64(p, uap)
557 struct svr4_sys_mmap64_args *uap;
562 #define _MAP_NEW 0x80000000
564 * Verify the arguments.
566 if (SCARG(uap, prot) & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
567 return EINVAL; /* XXX still needed? */
569 if (SCARG(uap, len) == 0)
572 SCARG(&mm, prot) = SCARG(uap, prot);
573 SCARG(&mm, len) = SCARG(uap, len);
574 SCARG(&mm, flags) = SCARG(uap, flags) & ~_MAP_NEW;
575 SCARG(&mm, fd) = SCARG(uap, fd);
576 SCARG(&mm, addr) = SCARG(uap, addr);
577 SCARG(&mm, pos) = SCARG(uap, pos);
579 rp = (void *) round_page((vm_offset_t)(p->p_vmspace->vm_daddr + maxdsiz));
580 if ((SCARG(&mm, flags) & MAP_FIXED) == 0 &&
581 SCARG(&mm, addr) != 0 && (void *)SCARG(&mm, addr) < rp)
582 SCARG(&mm, addr) = rp;
589 svr4_sys_fchroot(p, uap)
591 struct svr4_sys_fchroot_args *uap;
593 struct filedesc *fdp = p->p_fd;
598 if ((error = suser(p)) != 0)
600 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
602 vp = (struct vnode *) fp->f_data;
603 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
604 if (vp->v_type != VDIR)
607 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
608 VOP_UNLOCK(vp, 0, p);
612 if (fdp->fd_rdir != NULL)
620 svr4_mknod(p, retval, path, mode, dev)
627 caddr_t sg = stackgap_init();
629 CHECKALTEXIST(p, &sg, path);
631 if (S_ISFIFO(mode)) {
632 struct mkfifo_args ap;
633 SCARG(&ap, path) = path;
634 SCARG(&ap, mode) = mode;
635 return mkfifo(p, &ap);
637 struct mknod_args ap;
638 SCARG(&ap, path) = path;
639 SCARG(&ap, mode) = mode;
640 SCARG(&ap, dev) = dev;
641 return mknod(p, &ap);
647 svr4_sys_mknod(p, uap)
648 register struct proc *p;
649 struct svr4_sys_mknod_args *uap;
651 int *retval = p->p_retval;
652 return svr4_mknod(p, retval,
653 SCARG(uap, path), SCARG(uap, mode),
654 (svr4_dev_t)svr4_to_bsd_odev_t(SCARG(uap, dev)));
659 svr4_sys_xmknod(p, uap)
661 struct svr4_sys_xmknod_args *uap;
663 int *retval = p->p_retval;
664 return svr4_mknod(p, retval,
665 SCARG(uap, path), SCARG(uap, mode),
666 (svr4_dev_t)svr4_to_bsd_dev_t(SCARG(uap, dev)));
671 svr4_sys_vhangup(p, uap)
673 struct svr4_sys_vhangup_args *uap;
680 svr4_sys_sysconfig(p, uap)
682 struct svr4_sys_sysconfig_args *uap;
686 retval = &(p->p_retval[0]);
688 switch (SCARG(uap, name)) {
689 case SVR4_CONFIG_UNUSED:
692 case SVR4_CONFIG_NGROUPS:
693 *retval = NGROUPS_MAX;
695 case SVR4_CONFIG_CHILD_MAX:
698 case SVR4_CONFIG_OPEN_FILES:
701 case SVR4_CONFIG_POSIX_VER:
704 case SVR4_CONFIG_PAGESIZE:
707 case SVR4_CONFIG_CLK_TCK:
708 *retval = 60; /* should this be `hz', ie. 100? */
710 case SVR4_CONFIG_XOPEN_VER:
711 *retval = 2; /* XXX: What should that be? */
713 case SVR4_CONFIG_PROF_TCK:
714 *retval = 60; /* XXX: What should that be? */
716 case SVR4_CONFIG_NPROC_CONF:
717 *retval = 1; /* Only one processor for now */
719 case SVR4_CONFIG_NPROC_ONLN:
720 *retval = 1; /* And it better be online */
722 case SVR4_CONFIG_AIO_LISTIO_MAX:
723 case SVR4_CONFIG_AIO_MAX:
724 case SVR4_CONFIG_AIO_PRIO_DELTA_MAX:
725 *retval = 0; /* No aio support */
727 case SVR4_CONFIG_DELAYTIMER_MAX:
728 *retval = 0; /* No delaytimer support */
730 case SVR4_CONFIG_MQ_OPEN_MAX:
731 *retval = msginfo.msgmni;
733 case SVR4_CONFIG_MQ_PRIO_MAX:
734 *retval = 0; /* XXX: Don't know */
736 case SVR4_CONFIG_RTSIG_MAX:
739 case SVR4_CONFIG_SEM_NSEMS_MAX:
740 *retval = seminfo.semmni;
742 case SVR4_CONFIG_SEM_VALUE_MAX:
743 *retval = seminfo.semvmx;
745 case SVR4_CONFIG_SIGQUEUE_MAX:
746 *retval = 0; /* XXX: Don't know */
748 case SVR4_CONFIG_SIGRT_MIN:
749 case SVR4_CONFIG_SIGRT_MAX:
750 *retval = 0; /* No real time signals */
752 case SVR4_CONFIG_TIMER_MAX:
753 *retval = 3; /* XXX: real, virtual, profiling */
756 case SVR4_CONFIG_PHYS_PAGES:
758 *retval = uvmexp.free; /* XXX: free instead of total */
760 *retval = cnt.v_free_count; /* XXX: free instead of total */
763 case SVR4_CONFIG_AVPHYS_PAGES:
765 *retval = uvmexp.active; /* XXX: active instead of avg */
767 *retval = cnt.v_active_count; /* XXX: active instead of avg */
778 extern int swap_pager_full;
782 svr4_sys_break(p, uap)
784 struct svr4_sys_break_args *uap;
786 struct vmspace *vm = p->p_vmspace;
787 vm_offset_t new, old, base, ns;
790 base = round_page((vm_offset_t) vm->vm_daddr);
791 ns = (vm_offset_t)SCARG(uap, nsize);
792 new = round_page(ns);
794 if ((new - base) > (unsigned) p->p_rlimit[RLIMIT_DATA].rlim_cur) {
797 if (new >= VM_MAXUSER_ADDRESS) {
800 } else if (new < base) {
802 * This is simply an invalid value. If someone wants to
803 * do fancy address space manipulations, mmap and munmap
804 * can do most of what the user would want.
809 old = base + ctob(vm->vm_dsize);
814 if (vm->vm_map.size + diff > p->p_rlimit[RLIMIT_VMEM].rlim_cur)
816 rv = vm_map_find(&vm->vm_map, NULL, 0, &old, diff, FALSE,
817 VM_PROT_ALL, VM_PROT_ALL, 0);
818 if (rv != KERN_SUCCESS) {
821 vm->vm_dsize += btoc(diff);
822 } else if (new < old) {
823 rv = vm_map_remove(&vm->vm_map, new, old);
824 if (rv != KERN_SUCCESS) {
827 vm->vm_dsize -= btoc(old - new);
833 static __inline clock_t
834 timeval_to_clock_t(tv)
837 return tv->tv_sec * hz + tv->tv_usec / (1000000 / hz);
842 svr4_sys_times(p, uap)
844 struct svr4_sys_times_args *uap;
846 int error, *retval = p->p_retval;
851 struct getrusage_args ga;
853 caddr_t sg = stackgap_init();
854 ru = stackgap_alloc(&sg, sizeof(struct rusage));
856 SCARG(&ga, who) = RUSAGE_SELF;
857 SCARG(&ga, rusage) = ru;
859 error = getrusage(p, &ga);
863 if ((error = copyin(ru, &r, sizeof r)) != 0)
866 tms.tms_utime = timeval_to_clock_t(&r.ru_utime);
867 tms.tms_stime = timeval_to_clock_t(&r.ru_stime);
869 SCARG(&ga, who) = RUSAGE_CHILDREN;
870 error = getrusage(p, &ga);
874 if ((error = copyin(ru, &r, sizeof r)) != 0)
877 tms.tms_cutime = timeval_to_clock_t(&r.ru_utime);
878 tms.tms_cstime = timeval_to_clock_t(&r.ru_stime);
881 *retval = timeval_to_clock_t(&t);
883 return copyout(&tms, SCARG(uap, tp), sizeof(tms));
888 svr4_sys_ulimit(p, uap)
890 struct svr4_sys_ulimit_args *uap;
892 int *retval = p->p_retval;
894 switch (SCARG(uap, cmd)) {
896 *retval = p->p_rlimit[RLIMIT_FSIZE].rlim_cur / 512;
898 *retval = 0x7fffffff;
904 struct __setrlimit_args srl;
906 caddr_t sg = stackgap_init();
907 struct rlimit *url = (struct rlimit *)
908 stackgap_alloc(&sg, sizeof *url);
910 krl.rlim_cur = SCARG(uap, newlimit) * 512;
911 krl.rlim_max = p->p_rlimit[RLIMIT_FSIZE].rlim_max;
913 error = copyout(&krl, url, sizeof(*url));
917 SCARG(&srl, which) = RLIMIT_FSIZE;
918 SCARG(&srl, rlp) = (struct orlimit *)url;
920 error = setrlimit(p, &srl);
924 *retval = p->p_rlimit[RLIMIT_FSIZE].rlim_cur;
926 *retval = 0x7fffffff;
932 struct vmspace *vm = p->p_vmspace;
933 register_t r = p->p_rlimit[RLIMIT_DATA].rlim_cur;
937 r += (long) vm->vm_daddr;
945 *retval = p->p_rlimit[RLIMIT_NOFILE].rlim_cur;
947 *retval = 0x7fffffff;
961 /* look in the live processes */
962 if ((p = pfind(pid)) != NULL)
965 /* look in the zombies */
966 for (p = zombproc.lh_first; p != 0; p = p->p_list.le_next)
975 svr4_sys_pgrpsys(p, uap)
977 struct svr4_sys_pgrpsys_args *uap;
979 int *retval = p->p_retval;
981 switch (SCARG(uap, cmd)) {
982 case 1: /* setpgrp() */
984 * SVR4 setpgrp() (which takes no arguments) has the
985 * semantics that the session ID is also created anew, so
986 * in almost every sense, setpgrp() is identical to
987 * setsid() for SVR4. (Under BSD, the difference is that
988 * a setpgid(0,0) will not create a new session.)
993 case 0: /* getpgrp() */
994 *retval = p->p_pgrp->pg_id;
997 case 2: /* getsid(pid) */
998 if (SCARG(uap, pid) != 0 &&
999 (p = svr4_pfind(SCARG(uap, pid))) == NULL)
1002 * This has already been initialized to the pid of
1003 * the session leader.
1005 *retval = (register_t) p->p_session->s_leader->p_pid;
1008 case 3: /* setsid() */
1009 return setsid(p, NULL);
1011 case 4: /* getpgid(pid) */
1013 if (SCARG(uap, pid) != 0 &&
1014 (p = svr4_pfind(SCARG(uap, pid))) == NULL)
1017 *retval = (int) p->p_pgrp->pg_id;
1020 case 5: /* setpgid(pid, pgid); */
1022 struct setpgid_args sa;
1024 SCARG(&sa, pid) = SCARG(uap, pid);
1025 SCARG(&sa, pgid) = SCARG(uap, pgid);
1026 return setpgid(p, &sa);
1034 #define syscallarg(x) union { x datum; register_t pad; }
1036 struct svr4_hrtcntl_args {
1040 svr4_hrt_interval_t * iv;
1041 svr4_hrt_time_t * ti;
1046 svr4_hrtcntl(p, uap, retval)
1048 struct svr4_hrtcntl_args *uap;
1051 switch (SCARG(uap, fun)) {
1052 case SVR4_HRT_CNTL_RES:
1053 DPRINTF(("htrcntl(RES)\n"));
1054 *retval = SVR4_HRT_USEC;
1057 case SVR4_HRT_CNTL_TOFD:
1058 DPRINTF(("htrcntl(TOFD)\n"));
1062 if (SCARG(uap, clk) != SVR4_HRT_CLK_STD) {
1063 DPRINTF(("clk == %d\n", SCARG(uap, clk)));
1066 if (SCARG(uap, ti) == NULL) {
1067 DPRINTF(("ti NULL\n"));
1071 t.h_sec = tv.tv_sec;
1072 t.h_rem = tv.tv_usec;
1073 t.h_res = SVR4_HRT_USEC;
1074 return copyout(&t, SCARG(uap, ti), sizeof(t));
1077 case SVR4_HRT_CNTL_START:
1078 DPRINTF(("htrcntl(START)\n"));
1081 case SVR4_HRT_CNTL_GET:
1082 DPRINTF(("htrcntl(GET)\n"));
1085 DPRINTF(("Bad htrcntl command %d\n", SCARG(uap, fun)));
1092 svr4_sys_hrtsys(p, uap)
1094 struct svr4_sys_hrtsys_args *uap;
1096 int *retval = p->p_retval;
1098 switch (SCARG(uap, cmd)) {
1100 return svr4_hrtcntl(p, (struct svr4_hrtcntl_args *) uap,
1104 DPRINTF(("hrtalarm\n"));
1108 DPRINTF(("hrtsleep\n"));
1112 DPRINTF(("hrtcancel\n"));
1116 DPRINTF(("Bad hrtsys command %d\n", SCARG(uap, cmd)));
1123 svr4_setinfo(p, st, s)
1131 memset(&i, 0, sizeof(i));
1133 i.si_signo = SVR4_SIGCHLD;
1134 i.si_errno = 0; /* XXX? */
1137 i.si_pid = p->p_pid;
1138 if (p->p_stat == SZOMB) {
1139 i.si_stime = p->p_ru->ru_stime.tv_sec;
1140 i.si_utime = p->p_ru->ru_utime.tv_sec;
1143 i.si_stime = p->p_stats->p_ru.ru_stime.tv_sec;
1144 i.si_utime = p->p_stats->p_ru.ru_utime.tv_sec;
1148 if (WIFEXITED(st)) {
1149 i.si_status = WEXITSTATUS(st);
1150 i.si_code = SVR4_CLD_EXITED;
1151 } else if (WIFSTOPPED(st)) {
1153 if (sig >= 0 && sig < NSIG)
1154 i.si_status = SVR4_BSD2SVR4_SIG(sig);
1156 if (i.si_status == SVR4_SIGCONT)
1157 i.si_code = SVR4_CLD_CONTINUED;
1159 i.si_code = SVR4_CLD_STOPPED;
1162 if (sig >= 0 && sig < NSIG)
1163 i.si_status = SVR4_BSD2SVR4_SIG(sig);
1166 i.si_code = SVR4_CLD_DUMPED;
1168 i.si_code = SVR4_CLD_KILLED;
1171 DPRINTF(("siginfo [pid %ld signo %d code %d errno %d status %d]\n",
1172 i.si_pid, i.si_signo, i.si_code, i.si_errno, i.si_status));
1174 return copyout(&i, s, sizeof(i));
1179 svr4_sys_waitsys(p, uap)
1181 struct svr4_sys_waitsys_args *uap;
1184 int error, *retval = p->p_retval;
1188 switch (SCARG(uap, grp)) {
1193 SCARG(uap, id) = -p->p_pgid;
1197 SCARG(uap, id) = WAIT_ANY;
1204 DPRINTF(("waitsys(%d, %d, %p, %x)\n",
1205 SCARG(uap, grp), SCARG(uap, id),
1206 SCARG(uap, info), SCARG(uap, options)));
1210 for (q = p->p_children.lh_first; q != 0; q = q->p_sibling.le_next) {
1211 if (SCARG(uap, id) != WAIT_ANY &&
1212 q->p_pid != SCARG(uap, id) &&
1213 q->p_pgid != -SCARG(uap, id)) {
1214 DPRINTF(("pid %d pgid %d != %d\n", q->p_pid,
1215 q->p_pgid, SCARG(uap, id)));
1219 if (q->p_stat == SZOMB &&
1220 ((SCARG(uap, options) & (SVR4_WEXITED|SVR4_WTRAPPED)))) {
1222 DPRINTF(("found %d\n", q->p_pid));
1223 if ((error = svr4_setinfo(q, q->p_xstat,
1224 SCARG(uap, info))) != 0)
1228 if ((SCARG(uap, options) & SVR4_WNOWAIT)) {
1229 DPRINTF(("Don't wait\n"));
1234 * If we got the child via ptrace(2) or procfs, and
1235 * the parent is different (meaning the process was
1236 * attached, rather than run as a child), then we need
1237 * to give it back to the ol dparent, and send the
1238 * parent a SIGCHLD. The rest of the cleanup will be
1239 * done when the old parent waits on the child.
1241 if ((q->p_flag & P_TRACED) &&
1242 q->p_oppid != q->p_pptr->p_pid) {
1243 t = pfind(q->p_oppid);
1244 proc_reparent(q, t ? t : initproc);
1246 q->p_flag &= ~(P_TRACED | P_WAITED);
1247 wakeup((caddr_t)q->p_pptr);
1251 ruadd(&p->p_stats->p_cru, q->p_ru);
1253 FREE(q->p_ru, M_ZOMBIE);
1256 * Finally finished with old proc entry.
1257 * Unlink it from its process group and free it.
1261 LIST_REMOVE(q, p_list); /* off zombproc */
1263 LIST_REMOVE(q, p_sibling);
1266 * Decrement the count of procs running with this uid.
1268 (void)chgproccnt(q->p_ucred->cr_uidinfo, -1, 0);
1271 * Free up credentials.
1273 if (--q->p_cred->p_refcnt == 0) {
1275 FREE(q->p_cred, M_SUBPROC);
1279 * Release reference to text vnode
1285 * Give machine-dependent layer a chance
1286 * to free anything that cpu_exit couldn't
1287 * release while still running in process context.
1290 /* XXX what about process 'q' itself? zfree? */
1291 #if defined(__NetBSD__)
1292 pool_put(&proc_pool, q);
1297 if (q->p_stat == SSTOP && (q->p_flag & P_WAITED) == 0 &&
1298 (q->p_flag & P_TRACED ||
1299 (SCARG(uap, options) & (SVR4_WSTOPPED|SVR4_WCONTINUED)))) {
1300 DPRINTF(("jobcontrol %d\n", q->p_pid));
1301 if (((SCARG(uap, options) & SVR4_WNOWAIT)) == 0)
1302 q->p_flag |= P_WAITED;
1304 return svr4_setinfo(q, W_STOPCODE(q->p_xstat),
1312 if (SCARG(uap, options) & SVR4_WNOHANG) {
1314 if ((error = svr4_setinfo(NULL, 0, SCARG(uap, info))) != 0)
1319 if ((error = tsleep((caddr_t)p, PWAIT | PCATCH, "svr4_wait", 0)) != 0)
1326 bsd_statfs_to_svr4_statvfs(bfs, sfs)
1327 const struct statfs *bfs;
1328 struct svr4_statvfs *sfs;
1330 sfs->f_bsize = bfs->f_iosize; /* XXX */
1331 sfs->f_frsize = bfs->f_bsize;
1332 sfs->f_blocks = bfs->f_blocks;
1333 sfs->f_bfree = bfs->f_bfree;
1334 sfs->f_bavail = bfs->f_bavail;
1335 sfs->f_files = bfs->f_files;
1336 sfs->f_ffree = bfs->f_ffree;
1337 sfs->f_favail = bfs->f_ffree;
1338 sfs->f_fsid = bfs->f_fsid.val[0];
1339 memcpy(sfs->f_basetype, bfs->f_fstypename, sizeof(sfs->f_basetype));
1341 if (bfs->f_flags & MNT_RDONLY)
1342 sfs->f_flag |= SVR4_ST_RDONLY;
1343 if (bfs->f_flags & MNT_NOSUID)
1344 sfs->f_flag |= SVR4_ST_NOSUID;
1345 sfs->f_namemax = MAXNAMLEN;
1346 memcpy(sfs->f_fstr, bfs->f_fstypename, sizeof(sfs->f_fstr)); /* XXX */
1347 memset(sfs->f_filler, 0, sizeof(sfs->f_filler));
1352 bsd_statfs_to_svr4_statvfs64(bfs, sfs)
1353 const struct statfs *bfs;
1354 struct svr4_statvfs64 *sfs;
1356 sfs->f_bsize = bfs->f_iosize; /* XXX */
1357 sfs->f_frsize = bfs->f_bsize;
1358 sfs->f_blocks = bfs->f_blocks;
1359 sfs->f_bfree = bfs->f_bfree;
1360 sfs->f_bavail = bfs->f_bavail;
1361 sfs->f_files = bfs->f_files;
1362 sfs->f_ffree = bfs->f_ffree;
1363 sfs->f_favail = bfs->f_ffree;
1364 sfs->f_fsid = bfs->f_fsid.val[0];
1365 memcpy(sfs->f_basetype, bfs->f_fstypename, sizeof(sfs->f_basetype));
1367 if (bfs->f_flags & MNT_RDONLY)
1368 sfs->f_flag |= SVR4_ST_RDONLY;
1369 if (bfs->f_flags & MNT_NOSUID)
1370 sfs->f_flag |= SVR4_ST_NOSUID;
1371 sfs->f_namemax = MAXNAMLEN;
1372 memcpy(sfs->f_fstr, bfs->f_fstypename, sizeof(sfs->f_fstr)); /* XXX */
1373 memset(sfs->f_filler, 0, sizeof(sfs->f_filler));
1378 svr4_sys_statvfs(p, uap)
1380 struct svr4_sys_statvfs_args *uap;
1382 struct statfs_args fs_args;
1383 caddr_t sg = stackgap_init();
1384 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1386 struct svr4_statvfs sfs;
1389 CHECKALTEXIST(p, &sg, SCARG(uap, path));
1390 SCARG(&fs_args, path) = SCARG(uap, path);
1391 SCARG(&fs_args, buf) = fs;
1393 if ((error = statfs(p, &fs_args)) != 0)
1396 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1399 bsd_statfs_to_svr4_statvfs(&bfs, &sfs);
1401 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1406 svr4_sys_fstatvfs(p, uap)
1408 struct svr4_sys_fstatvfs_args *uap;
1410 struct fstatfs_args fs_args;
1411 caddr_t sg = stackgap_init();
1412 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1414 struct svr4_statvfs sfs;
1417 SCARG(&fs_args, fd) = SCARG(uap, fd);
1418 SCARG(&fs_args, buf) = fs;
1420 if ((error = fstatfs(p, &fs_args)) != 0)
1423 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1426 bsd_statfs_to_svr4_statvfs(&bfs, &sfs);
1428 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1433 svr4_sys_statvfs64(p, uap)
1435 struct svr4_sys_statvfs64_args *uap;
1437 struct statfs_args fs_args;
1438 caddr_t sg = stackgap_init();
1439 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1441 struct svr4_statvfs64 sfs;
1444 CHECKALTEXIST(p, &sg, SCARG(uap, path));
1445 SCARG(&fs_args, path) = SCARG(uap, path);
1446 SCARG(&fs_args, buf) = fs;
1448 if ((error = statfs(p, &fs_args)) != 0)
1451 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1454 bsd_statfs_to_svr4_statvfs64(&bfs, &sfs);
1456 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1461 svr4_sys_fstatvfs64(p, uap)
1463 struct svr4_sys_fstatvfs64_args *uap;
1465 struct fstatfs_args fs_args;
1466 caddr_t sg = stackgap_init();
1467 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1469 struct svr4_statvfs64 sfs;
1472 SCARG(&fs_args, fd) = SCARG(uap, fd);
1473 SCARG(&fs_args, buf) = fs;
1475 if ((error = fstatfs(p, &fs_args)) != 0)
1478 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1481 bsd_statfs_to_svr4_statvfs64(&bfs, &sfs);
1483 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1487 svr4_sys_alarm(p, uap)
1489 struct svr4_sys_alarm_args *uap;
1492 struct itimerval *itp, *oitp;
1493 struct setitimer_args sa;
1494 caddr_t sg = stackgap_init();
1496 itp = stackgap_alloc(&sg, sizeof(*itp));
1497 oitp = stackgap_alloc(&sg, sizeof(*oitp));
1498 timevalclear(&itp->it_interval);
1499 itp->it_value.tv_sec = SCARG(uap, sec);
1500 itp->it_value.tv_usec = 0;
1502 SCARG(&sa, which) = ITIMER_REAL;
1503 SCARG(&sa, itv) = itp;
1504 SCARG(&sa, oitv) = oitp;
1505 error = setitimer(p, &sa);
1508 if (oitp->it_value.tv_usec)
1509 oitp->it_value.tv_sec++;
1510 p->p_retval[0] = oitp->it_value.tv_sec;
1516 svr4_sys_gettimeofday(p, uap)
1518 struct svr4_sys_gettimeofday_args *uap;
1520 if (SCARG(uap, tp)) {
1524 return copyout(&atv, SCARG(uap, tp), sizeof (atv));
1531 svr4_sys_facl(p, uap)
1533 struct svr4_sys_facl_args *uap;
1537 retval = p->p_retval;
1540 switch (SCARG(uap, cmd)) {
1541 case SVR4_SYS_SETACL:
1542 /* We don't support acls on any filesystem */
1545 case SVR4_SYS_GETACL:
1546 return copyout(retval, &SCARG(uap, num),
1547 sizeof(SCARG(uap, num)));
1549 case SVR4_SYS_GETACLCNT:
1559 svr4_sys_acl(p, uap)
1561 struct svr4_sys_acl_args *uap;
1563 /* XXX: for now the same */
1564 return svr4_sys_facl(p, (struct svr4_sys_facl_args *)uap);
1568 svr4_sys_auditsys(p, uap)
1570 struct svr4_sys_auditsys_args *uap;
1573 * XXX: Big brother is *not* watching.
1579 svr4_sys_memcntl(p, uap)
1581 struct svr4_sys_memcntl_args *uap;
1583 switch (SCARG(uap, cmd)) {
1586 struct msync_args msa;
1588 SCARG(&msa, addr) = SCARG(uap, addr);
1589 SCARG(&msa, len) = SCARG(uap, len);
1590 SCARG(&msa, flags) = (int)SCARG(uap, arg);
1592 return msync(p, &msa);
1594 case SVR4_MC_ADVISE:
1596 struct madvise_args maa;
1598 SCARG(&maa, addr) = SCARG(uap, addr);
1599 SCARG(&maa, len) = SCARG(uap, len);
1600 SCARG(&maa, behav) = (int)SCARG(uap, arg);
1602 return madvise(p, &maa);
1605 case SVR4_MC_UNLOCK:
1606 case SVR4_MC_LOCKAS:
1607 case SVR4_MC_UNLOCKAS:
1616 svr4_sys_nice(p, uap)
1618 struct svr4_sys_nice_args *uap;
1620 struct setpriority_args ap;
1623 SCARG(&ap, which) = PRIO_PROCESS;
1624 SCARG(&ap, who) = 0;
1625 SCARG(&ap, prio) = SCARG(uap, prio);
1627 if ((error = setpriority(p, &ap)) != 0)
1630 /* the cast is stupid, but the structures are the same */
1631 if ((error = getpriority(p, (struct getpriority_args *)&ap)) != 0)
1638 svr4_sys_resolvepath(p, uap)
1640 struct svr4_sys_resolvepath_args *uap;
1642 struct nameidata nd;
1643 int error, *retval = p->p_retval;
1645 NDINIT(&nd, LOOKUP, NOFOLLOW | SAVENAME, UIO_USERSPACE,
1646 SCARG(uap, path), p);
1648 if ((error = namei(&nd)) != 0)
1651 if ((error = copyout(nd.ni_cnd.cn_pnbuf, SCARG(uap, buf),
1652 SCARG(uap, bufsiz))) != 0)
1655 *retval = strlen(nd.ni_cnd.cn_pnbuf) < SCARG(uap, bufsiz) ?
1656 strlen(nd.ni_cnd.cn_pnbuf) + 1 : SCARG(uap, bufsiz);
1658 NDFREE(&nd, NDF_ONLY_PNBUF);