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.3 2003/06/23 17:55:49 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((register_t *, char *, svr4_mode_t, svr4_dev_t));
100 static __inline clock_t timeval_to_clock_t __P((struct timeval *));
101 static int svr4_setinfo __P((struct proc *, int, svr4_siginfo_t *));
103 struct svr4_hrtcntl_args;
104 static int svr4_hrtcntl __P((struct svr4_hrtcntl_args *, register_t *));
105 static void bsd_statfs_to_svr4_statvfs __P((const struct statfs *,
106 struct svr4_statvfs *));
107 static void bsd_statfs_to_svr4_statvfs64 __P((const struct statfs *,
108 struct svr4_statvfs64 *));
109 static struct proc *svr4_pfind __P((pid_t pid));
114 svr4_sys_setitimer(struct svr4_sys_setitimer_args *uap)
122 svr4_sys_wait(struct svr4_sys_wait_args *uap)
124 struct proc *p = curproc;
126 int error, *retval = p->p_retval, st, sig;
127 size_t sz = sizeof(*SCARG(&w4, status));
129 SCARG(&w4, rusage) = NULL;
130 SCARG(&w4, options) = 0;
132 if (SCARG(uap, status) == NULL) {
133 caddr_t sg = stackgap_init();
135 SCARG(&w4, status) = stackgap_alloc(&sg, sz);
138 SCARG(&w4, status) = SCARG(uap, status);
140 SCARG(&w4, pid) = WAIT_ANY;
142 if ((error = wait4(&w4)) != 0)
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;
177 sg = stackgap_init();
178 CHECKALTEXIST(&sg, SCARG(uap, path));
180 SCARG(&ap, fname) = SCARG(uap, path);
181 SCARG(&ap, argv) = SCARG(uap, argp);
182 SCARG(&ap, envv) = NULL;
188 svr4_sys_execve(struct svr4_sys_execve_args *uap)
190 struct execve_args ap;
193 sg = stackgap_init();
194 CHECKALTEXIST(&sg, uap->path);
196 SCARG(&ap, fname) = SCARG(uap, path);
197 SCARG(&ap, argv) = SCARG(uap, argp);
198 SCARG(&ap, envv) = SCARG(uap, envp);
204 svr4_sys_time(struct svr4_sys_time_args *v)
206 struct proc *p = curproc;
207 struct svr4_sys_time_args *uap = v;
213 error = copyout(&tv.tv_sec, SCARG(uap, t),
214 sizeof(*(SCARG(uap, t))));
215 p->p_retval[0] = (int) tv.tv_sec;
222 * Read SVR4-style directory entries. We suck them into kernel space so
223 * that they can be massaged before being copied out to user code.
225 * This code is ported from the Linux emulator: Changes to the VFS interface
226 * between FreeBSD and NetBSD have made it simpler to port it from there than
227 * to adapt the NetBSD version.
230 svr4_sys_getdents64(struct svr4_sys_getdents64_args *uap)
232 struct proc *p = curproc;
235 caddr_t inp, buf; /* BSD-format */
236 int len, reclen; /* BSD-format */
237 caddr_t outp; /* SVR4-format */
238 int resid, svr4reclen=0; /* SVR4-format */
244 struct svr4_dirent64 svr4_dirent;
245 int buflen, error, eofflag, nbytes, justone;
246 u_long *cookies = NULL, *cookiep;
249 DPRINTF(("svr4_sys_getdents64(%d, *, %d)\n",
250 p->p_pid, SCARG(uap, fd), SCARG(uap, nbytes)));
251 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0) {
255 if ((fp->f_flag & FREAD) == 0)
258 vp = (struct vnode *) fp->f_data;
260 if (vp->v_type != VDIR)
263 if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p))) {
267 nbytes = SCARG(uap, nbytes);
269 nbytes = sizeof (struct svr4_dirent64);
276 #define DIRBLKSIZ 512 /* XXX we used to use ufs's DIRBLKSIZ */
277 buflen = max(DIRBLKSIZ, nbytes);
278 buflen = min(buflen, MAXBSIZE);
279 buf = malloc(buflen, M_TEMP, M_WAITOK);
280 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
283 aiov.iov_len = buflen;
284 auio.uio_iov = &aiov;
286 auio.uio_rw = UIO_READ;
287 auio.uio_segflg = UIO_SYSSPACE;
289 auio.uio_resid = buflen;
290 auio.uio_offset = off;
293 free(cookies, M_TEMP);
297 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
298 &ncookies, &cookies);
304 outp = (caddr_t) SCARG(uap, dp);
306 if ((len = buflen - auio.uio_resid) <= 0) {
314 * When using cookies, the vfs has the option of reading from
315 * a different offset than that supplied (UFS truncates the
316 * offset to a block boundary to make sure that it never reads
317 * partway through a directory entry, even if the directory
318 * has been compacted).
320 while (len > 0 && ncookies > 0 && *cookiep <= off) {
321 bdp = (struct dirent *) inp;
322 len -= bdp->d_reclen;
323 inp += bdp->d_reclen;
330 if (cookiep && ncookies == 0)
332 bdp = (struct dirent *) inp;
333 reclen = bdp->d_reclen;
335 DPRINTF(("svr4_readdir: reclen=%d\n", reclen));
340 if (bdp->d_fileno == 0) {
350 svr4reclen = SVR4_RECLEN(&svr4_dirent, bdp->d_namlen);
351 if (reclen > len || resid < svr4reclen) {
355 svr4_dirent.d_ino = (long) bdp->d_fileno;
358 * old svr4-style readdir usage.
360 svr4_dirent.d_off = (svr4_off_t) svr4reclen;
361 svr4_dirent.d_reclen = (u_short) bdp->d_namlen;
363 svr4_dirent.d_off = (svr4_off_t)(off + reclen);
364 svr4_dirent.d_reclen = (u_short) svr4reclen;
366 strcpy(svr4_dirent.d_name, bdp->d_name);
367 if ((error = copyout((caddr_t)&svr4_dirent, outp, svr4reclen)))
382 if (outp == (caddr_t) SCARG(uap, dp))
387 nbytes = resid + svr4reclen;
390 p->p_retval[0] = nbytes - resid;
393 free(cookies, M_TEMP);
394 VOP_UNLOCK(vp, 0, p);
401 svr4_sys_getdents(struct svr4_sys_getdents_args *uap)
403 struct proc *p = curproc;
406 caddr_t inp, buf; /* BSD-format */
407 int len, reclen; /* BSD-format */
408 caddr_t outp; /* SVR4-format */
409 int resid, svr4_reclen; /* SVR4-format */
413 struct svr4_dirent idb;
414 off_t off; /* true file offset */
415 int buflen, error, eofflag;
416 u_long *cookiebuf = NULL, *cookie;
417 int ncookies = 0, *retval = p->p_retval;
419 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
422 if ((fp->f_flag & FREAD) == 0)
425 vp = (struct vnode *)fp->f_data;
426 if (vp->v_type != VDIR)
429 buflen = min(MAXBSIZE, SCARG(uap, nbytes));
430 buf = malloc(buflen, M_TEMP, M_WAITOK);
431 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
435 aiov.iov_len = buflen;
436 auio.uio_iov = &aiov;
438 auio.uio_rw = UIO_READ;
439 auio.uio_segflg = UIO_SYSSPACE;
441 auio.uio_resid = buflen;
442 auio.uio_offset = off;
444 * First we read into the malloc'ed buffer, then
445 * we massage it into user space, one record at a time.
447 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies,
453 outp = SCARG(uap, buf);
454 resid = SCARG(uap, nbytes);
455 if ((len = buflen - auio.uio_resid) == 0)
458 for (cookie = cookiebuf; len > 0; len -= reclen) {
459 bdp = (struct dirent *)inp;
460 reclen = bdp->d_reclen;
462 panic("svr4_sys_getdents64: bad reclen");
463 off = *cookie++; /* each entry points to the next */
464 if ((off >> 32) != 0) {
465 uprintf("svr4_sys_getdents64: dir offset too large for emulated program");
469 if (bdp->d_fileno == 0) {
470 inp += reclen; /* it is a hole; squish it out */
473 svr4_reclen = SVR4_RECLEN(&idb, bdp->d_namlen);
474 if (reclen > len || resid < svr4_reclen) {
475 /* entry too big for buffer, so just stop */
480 * Massage in place to make a SVR4-shaped dirent (otherwise
481 * we have to worry about touching user memory outside of
482 * the copyout() call).
484 idb.d_ino = (svr4_ino_t)bdp->d_fileno;
485 idb.d_off = (svr4_off_t)off;
486 idb.d_reclen = (u_short)svr4_reclen;
487 strcpy(idb.d_name, bdp->d_name);
488 if ((error = copyout((caddr_t)&idb, outp, svr4_reclen)))
490 /* advance past this real entry */
492 /* advance output past SVR4-shaped entry */
494 resid -= svr4_reclen;
497 /* if we squished out the whole block, try again */
498 if (outp == SCARG(uap, buf))
500 fp->f_offset = off; /* update the vnode offset */
503 *retval = SCARG(uap, nbytes) - resid;
505 VOP_UNLOCK(vp, 0, p);
507 free(cookiebuf, M_TEMP);
514 svr4_sys_mmap(struct svr4_sys_mmap_args *uap)
516 struct proc *p = curproc;
520 retval = p->p_retval;
521 #define _MAP_NEW 0x80000000
523 * Verify the arguments.
525 if (SCARG(uap, prot) & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
526 return EINVAL; /* XXX still needed? */
528 if (SCARG(uap, len) == 0)
531 SCARG(&mm, prot) = SCARG(uap, prot);
532 SCARG(&mm, len) = SCARG(uap, len);
533 SCARG(&mm, flags) = SCARG(uap, flags) & ~_MAP_NEW;
534 SCARG(&mm, fd) = SCARG(uap, fd);
535 SCARG(&mm, addr) = SCARG(uap, addr);
536 SCARG(&mm, pos) = SCARG(uap, pos);
542 svr4_sys_mmap64(struct svr4_sys_mmap64_args *uap)
544 struct proc *p = curproc;
548 #define _MAP_NEW 0x80000000
550 * Verify the arguments.
552 if (SCARG(uap, prot) & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
553 return EINVAL; /* XXX still needed? */
555 if (SCARG(uap, len) == 0)
558 SCARG(&mm, prot) = SCARG(uap, prot);
559 SCARG(&mm, len) = SCARG(uap, len);
560 SCARG(&mm, flags) = SCARG(uap, flags) & ~_MAP_NEW;
561 SCARG(&mm, fd) = SCARG(uap, fd);
562 SCARG(&mm, addr) = SCARG(uap, addr);
563 SCARG(&mm, pos) = SCARG(uap, pos);
565 rp = (void *) round_page((vm_offset_t)(p->p_vmspace->vm_daddr + maxdsiz));
566 if ((SCARG(&mm, flags) & MAP_FIXED) == 0 &&
567 SCARG(&mm, addr) != 0 && (void *)SCARG(&mm, addr) < rp)
568 SCARG(&mm, addr) = rp;
575 svr4_sys_fchroot(struct svr4_sys_fchroot_args *uap)
577 struct proc *p = curproc;
578 struct filedesc *fdp = p->p_fd;
583 if ((error = suser_xxx(p->p_ucred, 0)) != 0)
585 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
587 vp = (struct vnode *) fp->f_data;
588 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
589 if (vp->v_type != VDIR)
592 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
593 VOP_UNLOCK(vp, 0, p);
597 if (fdp->fd_rdir != NULL)
605 svr4_mknod(retval, path, mode, dev)
611 caddr_t sg = stackgap_init();
613 CHECKALTEXIST(&sg, path);
615 if (S_ISFIFO(mode)) {
616 struct mkfifo_args ap;
617 SCARG(&ap, path) = path;
618 SCARG(&ap, mode) = mode;
621 struct mknod_args ap;
622 SCARG(&ap, path) = path;
623 SCARG(&ap, mode) = mode;
624 SCARG(&ap, dev) = dev;
631 svr4_sys_mknod(struct svr4_sys_mknod_args *uap)
633 struct proc *p = curproc;
635 int *retval = p->p_retval;
636 return svr4_mknod(retval, SCARG(uap, path), SCARG(uap, mode),
637 (svr4_dev_t)svr4_to_bsd_odev_t(SCARG(uap, dev)));
642 svr4_sys_xmknod(struct svr4_sys_xmknod_args *uap)
644 struct proc *p = curproc;
645 int *retval = p->p_retval;
646 return svr4_mknod(retval, SCARG(uap, path), SCARG(uap, mode),
647 (svr4_dev_t)svr4_to_bsd_dev_t(SCARG(uap, dev)));
652 svr4_sys_vhangup(struct svr4_sys_vhangup_args *uap)
659 svr4_sys_sysconfig(struct svr4_sys_sysconfig_args *uap)
661 struct proc *p = curproc;
664 retval = &(p->p_retval[0]);
666 switch (SCARG(uap, name)) {
667 case SVR4_CONFIG_UNUSED:
670 case SVR4_CONFIG_NGROUPS:
671 *retval = NGROUPS_MAX;
673 case SVR4_CONFIG_CHILD_MAX:
676 case SVR4_CONFIG_OPEN_FILES:
679 case SVR4_CONFIG_POSIX_VER:
682 case SVR4_CONFIG_PAGESIZE:
685 case SVR4_CONFIG_CLK_TCK:
686 *retval = 60; /* should this be `hz', ie. 100? */
688 case SVR4_CONFIG_XOPEN_VER:
689 *retval = 2; /* XXX: What should that be? */
691 case SVR4_CONFIG_PROF_TCK:
692 *retval = 60; /* XXX: What should that be? */
694 case SVR4_CONFIG_NPROC_CONF:
695 *retval = 1; /* Only one processor for now */
697 case SVR4_CONFIG_NPROC_ONLN:
698 *retval = 1; /* And it better be online */
700 case SVR4_CONFIG_AIO_LISTIO_MAX:
701 case SVR4_CONFIG_AIO_MAX:
702 case SVR4_CONFIG_AIO_PRIO_DELTA_MAX:
703 *retval = 0; /* No aio support */
705 case SVR4_CONFIG_DELAYTIMER_MAX:
706 *retval = 0; /* No delaytimer support */
708 case SVR4_CONFIG_MQ_OPEN_MAX:
709 *retval = msginfo.msgmni;
711 case SVR4_CONFIG_MQ_PRIO_MAX:
712 *retval = 0; /* XXX: Don't know */
714 case SVR4_CONFIG_RTSIG_MAX:
717 case SVR4_CONFIG_SEM_NSEMS_MAX:
718 *retval = seminfo.semmni;
720 case SVR4_CONFIG_SEM_VALUE_MAX:
721 *retval = seminfo.semvmx;
723 case SVR4_CONFIG_SIGQUEUE_MAX:
724 *retval = 0; /* XXX: Don't know */
726 case SVR4_CONFIG_SIGRT_MIN:
727 case SVR4_CONFIG_SIGRT_MAX:
728 *retval = 0; /* No real time signals */
730 case SVR4_CONFIG_TIMER_MAX:
731 *retval = 3; /* XXX: real, virtual, profiling */
734 case SVR4_CONFIG_PHYS_PAGES:
736 *retval = uvmexp.free; /* XXX: free instead of total */
738 *retval = cnt.v_free_count; /* XXX: free instead of total */
741 case SVR4_CONFIG_AVPHYS_PAGES:
743 *retval = uvmexp.active; /* XXX: active instead of avg */
745 *retval = cnt.v_active_count; /* XXX: active instead of avg */
756 extern int swap_pager_full;
760 svr4_sys_break(struct svr4_sys_break_args *uap)
762 struct proc *p = curproc;
763 struct vmspace *vm = p->p_vmspace;
764 vm_offset_t new, old, base, ns;
767 base = round_page((vm_offset_t) vm->vm_daddr);
768 ns = (vm_offset_t)SCARG(uap, nsize);
769 new = round_page(ns);
771 if ((new - base) > (unsigned) p->p_rlimit[RLIMIT_DATA].rlim_cur) {
774 if (new >= VM_MAXUSER_ADDRESS) {
777 } else if (new < base) {
779 * This is simply an invalid value. If someone wants to
780 * do fancy address space manipulations, mmap and munmap
781 * can do most of what the user would want.
786 old = base + ctob(vm->vm_dsize);
791 if (vm->vm_map.size + diff > p->p_rlimit[RLIMIT_VMEM].rlim_cur)
793 rv = vm_map_find(&vm->vm_map, NULL, 0, &old, diff, FALSE,
794 VM_PROT_ALL, VM_PROT_ALL, 0);
795 if (rv != KERN_SUCCESS) {
798 vm->vm_dsize += btoc(diff);
799 } else if (new < old) {
800 rv = vm_map_remove(&vm->vm_map, new, old);
801 if (rv != KERN_SUCCESS) {
804 vm->vm_dsize -= btoc(old - new);
810 static __inline clock_t
811 timeval_to_clock_t(tv)
814 return tv->tv_sec * hz + tv->tv_usec / (1000000 / hz);
819 svr4_sys_times(struct svr4_sys_times_args *uap)
821 struct proc *p = curproc;
822 int error, *retval = p->p_retval;
827 struct getrusage_args ga;
829 caddr_t sg = stackgap_init();
830 ru = stackgap_alloc(&sg, sizeof(struct rusage));
832 SCARG(&ga, who) = RUSAGE_SELF;
833 SCARG(&ga, rusage) = ru;
835 error = getrusage(&ga);
839 if ((error = copyin(ru, &r, sizeof r)) != 0)
842 tms.tms_utime = timeval_to_clock_t(&r.ru_utime);
843 tms.tms_stime = timeval_to_clock_t(&r.ru_stime);
845 SCARG(&ga, who) = RUSAGE_CHILDREN;
846 error = getrusage(&ga);
850 if ((error = copyin(ru, &r, sizeof r)) != 0)
853 tms.tms_cutime = timeval_to_clock_t(&r.ru_utime);
854 tms.tms_cstime = timeval_to_clock_t(&r.ru_stime);
857 *retval = timeval_to_clock_t(&t);
859 return copyout(&tms, SCARG(uap, tp), sizeof(tms));
864 svr4_sys_ulimit(struct svr4_sys_ulimit_args *uap)
866 struct proc *p = curproc;
867 int *retval = p->p_retval;
869 switch (SCARG(uap, cmd)) {
871 *retval = p->p_rlimit[RLIMIT_FSIZE].rlim_cur / 512;
873 *retval = 0x7fffffff;
879 struct __setrlimit_args srl;
881 caddr_t sg = stackgap_init();
882 struct rlimit *url = (struct rlimit *)
883 stackgap_alloc(&sg, sizeof *url);
885 krl.rlim_cur = SCARG(uap, newlimit) * 512;
886 krl.rlim_max = p->p_rlimit[RLIMIT_FSIZE].rlim_max;
888 error = copyout(&krl, url, sizeof(*url));
892 SCARG(&srl, which) = RLIMIT_FSIZE;
893 SCARG(&srl, rlp) = (struct orlimit *)url;
895 error = setrlimit(&srl);
899 *retval = p->p_rlimit[RLIMIT_FSIZE].rlim_cur;
901 *retval = 0x7fffffff;
907 struct vmspace *vm = p->p_vmspace;
908 register_t r = p->p_rlimit[RLIMIT_DATA].rlim_cur;
912 r += (long) vm->vm_daddr;
920 *retval = p->p_rlimit[RLIMIT_NOFILE].rlim_cur;
922 *retval = 0x7fffffff;
936 /* look in the live processes */
937 if ((p = pfind(pid)) != NULL)
940 /* look in the zombies */
941 for (p = zombproc.lh_first; p != 0; p = p->p_list.le_next)
950 svr4_sys_pgrpsys(struct svr4_sys_pgrpsys_args *uap)
952 struct proc *p = curproc;
953 int *retval = p->p_retval;
955 switch (SCARG(uap, cmd)) {
956 case 1: /* setpgrp() */
958 * SVR4 setpgrp() (which takes no arguments) has the
959 * semantics that the session ID is also created anew, so
960 * in almost every sense, setpgrp() is identical to
961 * setsid() for SVR4. (Under BSD, the difference is that
962 * a setpgid(0,0) will not create a new session.)
967 case 0: /* getpgrp() */
968 *retval = p->p_pgrp->pg_id;
971 case 2: /* getsid(pid) */
972 if (SCARG(uap, pid) != 0 &&
973 (p = svr4_pfind(SCARG(uap, pid))) == NULL)
976 * This has already been initialized to the pid of
977 * the session leader.
979 *retval = (register_t) p->p_session->s_leader->p_pid;
982 case 3: /* setsid() */
985 case 4: /* getpgid(pid) */
987 if (SCARG(uap, pid) != 0 &&
988 (p = svr4_pfind(SCARG(uap, pid))) == NULL)
991 *retval = (int) p->p_pgrp->pg_id;
994 case 5: /* setpgid(pid, pgid); */
996 struct setpgid_args sa;
998 SCARG(&sa, pid) = SCARG(uap, pid);
999 SCARG(&sa, pgid) = SCARG(uap, pgid);
1000 return setpgid(&sa);
1008 #define syscallarg(x) union { x datum; register_t pad; }
1010 struct svr4_hrtcntl_args {
1014 svr4_hrt_interval_t * iv;
1015 svr4_hrt_time_t * ti;
1020 svr4_hrtcntl(struct svr4_hrtcntl_args *uap, register_t *retval)
1022 switch (SCARG(uap, fun)) {
1023 case SVR4_HRT_CNTL_RES:
1024 DPRINTF(("htrcntl(RES)\n"));
1025 *retval = SVR4_HRT_USEC;
1028 case SVR4_HRT_CNTL_TOFD:
1029 DPRINTF(("htrcntl(TOFD)\n"));
1033 if (SCARG(uap, clk) != SVR4_HRT_CLK_STD) {
1034 DPRINTF(("clk == %d\n", SCARG(uap, clk)));
1037 if (SCARG(uap, ti) == NULL) {
1038 DPRINTF(("ti NULL\n"));
1042 t.h_sec = tv.tv_sec;
1043 t.h_rem = tv.tv_usec;
1044 t.h_res = SVR4_HRT_USEC;
1045 return copyout(&t, SCARG(uap, ti), sizeof(t));
1048 case SVR4_HRT_CNTL_START:
1049 DPRINTF(("htrcntl(START)\n"));
1052 case SVR4_HRT_CNTL_GET:
1053 DPRINTF(("htrcntl(GET)\n"));
1056 DPRINTF(("Bad htrcntl command %d\n", SCARG(uap, fun)));
1063 svr4_sys_hrtsys(struct svr4_sys_hrtsys_args *uap)
1065 struct proc *p = curproc;
1066 int *retval = p->p_retval;
1068 switch (SCARG(uap, cmd)) {
1070 return svr4_hrtcntl((struct svr4_hrtcntl_args *) uap, retval);
1073 DPRINTF(("hrtalarm\n"));
1077 DPRINTF(("hrtsleep\n"));
1081 DPRINTF(("hrtcancel\n"));
1085 DPRINTF(("Bad hrtsys command %d\n", SCARG(uap, cmd)));
1092 svr4_setinfo(p, st, s)
1100 memset(&i, 0, sizeof(i));
1102 i.si_signo = SVR4_SIGCHLD;
1103 i.si_errno = 0; /* XXX? */
1106 i.si_pid = p->p_pid;
1107 if (p->p_stat == SZOMB) {
1108 i.si_stime = p->p_ru->ru_stime.tv_sec;
1109 i.si_utime = p->p_ru->ru_utime.tv_sec;
1112 i.si_stime = p->p_stats->p_ru.ru_stime.tv_sec;
1113 i.si_utime = p->p_stats->p_ru.ru_utime.tv_sec;
1117 if (WIFEXITED(st)) {
1118 i.si_status = WEXITSTATUS(st);
1119 i.si_code = SVR4_CLD_EXITED;
1120 } else if (WIFSTOPPED(st)) {
1122 if (sig >= 0 && sig < NSIG)
1123 i.si_status = SVR4_BSD2SVR4_SIG(sig);
1125 if (i.si_status == SVR4_SIGCONT)
1126 i.si_code = SVR4_CLD_CONTINUED;
1128 i.si_code = SVR4_CLD_STOPPED;
1131 if (sig >= 0 && sig < NSIG)
1132 i.si_status = SVR4_BSD2SVR4_SIG(sig);
1135 i.si_code = SVR4_CLD_DUMPED;
1137 i.si_code = SVR4_CLD_KILLED;
1140 DPRINTF(("siginfo [pid %ld signo %d code %d errno %d status %d]\n",
1141 i.si_pid, i.si_signo, i.si_code, i.si_errno, i.si_status));
1143 return copyout(&i, s, sizeof(i));
1148 svr4_sys_waitsys(struct svr4_sys_waitsys_args *uap)
1150 struct proc *p = curproc;
1152 int error, *retval = p->p_retval;
1156 switch (SCARG(uap, grp)) {
1161 SCARG(uap, id) = -p->p_pgid;
1165 SCARG(uap, id) = WAIT_ANY;
1172 DPRINTF(("waitsys(%d, %d, %p, %x)\n",
1173 SCARG(uap, grp), SCARG(uap, id),
1174 SCARG(uap, info), SCARG(uap, options)));
1178 for (q = p->p_children.lh_first; q != 0; q = q->p_sibling.le_next) {
1179 if (SCARG(uap, id) != WAIT_ANY &&
1180 q->p_pid != SCARG(uap, id) &&
1181 q->p_pgid != -SCARG(uap, id)) {
1182 DPRINTF(("pid %d pgid %d != %d\n", q->p_pid,
1183 q->p_pgid, SCARG(uap, id)));
1187 if (q->p_stat == SZOMB &&
1188 ((SCARG(uap, options) & (SVR4_WEXITED|SVR4_WTRAPPED)))) {
1190 DPRINTF(("found %d\n", q->p_pid));
1191 if ((error = svr4_setinfo(q, q->p_xstat,
1192 SCARG(uap, info))) != 0)
1196 if ((SCARG(uap, options) & SVR4_WNOWAIT)) {
1197 DPRINTF(("Don't wait\n"));
1202 * If we got the child via ptrace(2) or procfs, and
1203 * the parent is different (meaning the process was
1204 * attached, rather than run as a child), then we need
1205 * to give it back to the ol dparent, and send the
1206 * parent a SIGCHLD. The rest of the cleanup will be
1207 * done when the old parent waits on the child.
1209 if ((q->p_flag & P_TRACED) &&
1210 q->p_oppid != q->p_pptr->p_pid) {
1211 t = pfind(q->p_oppid);
1212 proc_reparent(q, t ? t : initproc);
1214 q->p_flag &= ~(P_TRACED | P_WAITED);
1215 wakeup((caddr_t)q->p_pptr);
1219 ruadd(&p->p_stats->p_cru, q->p_ru);
1221 FREE(q->p_ru, M_ZOMBIE);
1224 * Finally finished with old proc entry.
1225 * Unlink it from its process group and free it.
1229 LIST_REMOVE(q, p_list); /* off zombproc */
1231 LIST_REMOVE(q, p_sibling);
1234 * Decrement the count of procs running with this uid.
1236 (void)chgproccnt(q->p_ucred->cr_uidinfo, -1, 0);
1239 * Free up credentials.
1245 * Release reference to text vnode
1251 * Give machine-dependent layer a chance
1252 * to free anything that cpu_exit couldn't
1253 * release while still running in process context.
1256 /* XXX what about process 'q' itself? zfree? */
1257 #if defined(__NetBSD__)
1258 pool_put(&proc_pool, q);
1263 if (q->p_stat == SSTOP && (q->p_flag & P_WAITED) == 0 &&
1264 (q->p_flag & P_TRACED ||
1265 (SCARG(uap, options) & (SVR4_WSTOPPED|SVR4_WCONTINUED)))) {
1266 DPRINTF(("jobcontrol %d\n", q->p_pid));
1267 if (((SCARG(uap, options) & SVR4_WNOWAIT)) == 0)
1268 q->p_flag |= P_WAITED;
1270 return svr4_setinfo(q, W_STOPCODE(q->p_xstat),
1278 if (SCARG(uap, options) & SVR4_WNOHANG) {
1280 if ((error = svr4_setinfo(NULL, 0, SCARG(uap, info))) != 0)
1285 if ((error = tsleep((caddr_t)p, PWAIT | PCATCH, "svr4_wait", 0)) != 0)
1292 bsd_statfs_to_svr4_statvfs(bfs, sfs)
1293 const struct statfs *bfs;
1294 struct svr4_statvfs *sfs;
1296 sfs->f_bsize = bfs->f_iosize; /* XXX */
1297 sfs->f_frsize = bfs->f_bsize;
1298 sfs->f_blocks = bfs->f_blocks;
1299 sfs->f_bfree = bfs->f_bfree;
1300 sfs->f_bavail = bfs->f_bavail;
1301 sfs->f_files = bfs->f_files;
1302 sfs->f_ffree = bfs->f_ffree;
1303 sfs->f_favail = bfs->f_ffree;
1304 sfs->f_fsid = bfs->f_fsid.val[0];
1305 memcpy(sfs->f_basetype, bfs->f_fstypename, sizeof(sfs->f_basetype));
1307 if (bfs->f_flags & MNT_RDONLY)
1308 sfs->f_flag |= SVR4_ST_RDONLY;
1309 if (bfs->f_flags & MNT_NOSUID)
1310 sfs->f_flag |= SVR4_ST_NOSUID;
1311 sfs->f_namemax = MAXNAMLEN;
1312 memcpy(sfs->f_fstr, bfs->f_fstypename, sizeof(sfs->f_fstr)); /* XXX */
1313 memset(sfs->f_filler, 0, sizeof(sfs->f_filler));
1318 bsd_statfs_to_svr4_statvfs64(bfs, sfs)
1319 const struct statfs *bfs;
1320 struct svr4_statvfs64 *sfs;
1322 sfs->f_bsize = bfs->f_iosize; /* XXX */
1323 sfs->f_frsize = bfs->f_bsize;
1324 sfs->f_blocks = bfs->f_blocks;
1325 sfs->f_bfree = bfs->f_bfree;
1326 sfs->f_bavail = bfs->f_bavail;
1327 sfs->f_files = bfs->f_files;
1328 sfs->f_ffree = bfs->f_ffree;
1329 sfs->f_favail = bfs->f_ffree;
1330 sfs->f_fsid = bfs->f_fsid.val[0];
1331 memcpy(sfs->f_basetype, bfs->f_fstypename, sizeof(sfs->f_basetype));
1333 if (bfs->f_flags & MNT_RDONLY)
1334 sfs->f_flag |= SVR4_ST_RDONLY;
1335 if (bfs->f_flags & MNT_NOSUID)
1336 sfs->f_flag |= SVR4_ST_NOSUID;
1337 sfs->f_namemax = MAXNAMLEN;
1338 memcpy(sfs->f_fstr, bfs->f_fstypename, sizeof(sfs->f_fstr)); /* XXX */
1339 memset(sfs->f_filler, 0, sizeof(sfs->f_filler));
1344 svr4_sys_statvfs(struct svr4_sys_statvfs_args *uap)
1346 struct statfs_args fs_args;
1347 caddr_t sg = stackgap_init();
1348 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1350 struct svr4_statvfs sfs;
1353 CHECKALTEXIST(&sg, SCARG(uap, path));
1354 SCARG(&fs_args, path) = SCARG(uap, path);
1355 SCARG(&fs_args, buf) = fs;
1357 if ((error = statfs(&fs_args)) != 0)
1360 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1363 bsd_statfs_to_svr4_statvfs(&bfs, &sfs);
1365 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1370 svr4_sys_fstatvfs(struct svr4_sys_fstatvfs_args *uap)
1372 struct fstatfs_args fs_args;
1373 caddr_t sg = stackgap_init();
1374 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1376 struct svr4_statvfs sfs;
1379 SCARG(&fs_args, fd) = SCARG(uap, fd);
1380 SCARG(&fs_args, buf) = fs;
1382 if ((error = fstatfs(&fs_args)) != 0)
1385 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1388 bsd_statfs_to_svr4_statvfs(&bfs, &sfs);
1390 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1395 svr4_sys_statvfs64(struct svr4_sys_statvfs64_args *uap)
1397 struct statfs_args fs_args;
1398 caddr_t sg = stackgap_init();
1399 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1401 struct svr4_statvfs64 sfs;
1404 CHECKALTEXIST(&sg, SCARG(uap, path));
1405 SCARG(&fs_args, path) = SCARG(uap, path);
1406 SCARG(&fs_args, buf) = fs;
1408 if ((error = statfs(&fs_args)) != 0)
1411 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1414 bsd_statfs_to_svr4_statvfs64(&bfs, &sfs);
1416 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1421 svr4_sys_fstatvfs64(struct svr4_sys_fstatvfs64_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_statvfs64 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 bsd_statfs_to_svr4_statvfs64(&bfs, &sfs);
1441 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1445 svr4_sys_alarm(struct svr4_sys_alarm_args *uap)
1447 struct proc *p = curproc;
1449 struct itimerval *itp, *oitp;
1450 struct setitimer_args sa;
1451 caddr_t sg = stackgap_init();
1453 itp = stackgap_alloc(&sg, sizeof(*itp));
1454 oitp = stackgap_alloc(&sg, sizeof(*oitp));
1455 timevalclear(&itp->it_interval);
1456 itp->it_value.tv_sec = SCARG(uap, sec);
1457 itp->it_value.tv_usec = 0;
1459 SCARG(&sa, which) = ITIMER_REAL;
1460 SCARG(&sa, itv) = itp;
1461 SCARG(&sa, oitv) = oitp;
1462 error = setitimer(&sa);
1465 if (oitp->it_value.tv_usec)
1466 oitp->it_value.tv_sec++;
1467 p->p_retval[0] = oitp->it_value.tv_sec;
1473 svr4_sys_gettimeofday(struct svr4_sys_gettimeofday_args *uap)
1475 if (SCARG(uap, tp)) {
1479 return copyout(&atv, SCARG(uap, tp), sizeof (atv));
1486 svr4_sys_facl(struct svr4_sys_facl_args *uap)
1488 struct proc *p = curproc;
1491 retval = p->p_retval;
1494 switch (SCARG(uap, cmd)) {
1495 case SVR4_SYS_SETACL:
1496 /* We don't support acls on any filesystem */
1499 case SVR4_SYS_GETACL:
1500 return copyout(retval, &SCARG(uap, num),
1501 sizeof(SCARG(uap, num)));
1503 case SVR4_SYS_GETACLCNT:
1513 svr4_sys_acl(struct svr4_sys_acl_args *uap)
1515 /* XXX: for now the same */
1516 return svr4_sys_facl((struct svr4_sys_facl_args *)uap);
1520 svr4_sys_auditsys(struct svr4_sys_auditsys_args *uap)
1523 * XXX: Big brother is *not* watching.
1529 svr4_sys_memcntl(struct svr4_sys_memcntl_args *uap)
1531 switch (SCARG(uap, cmd)) {
1534 struct msync_args msa;
1536 SCARG(&msa, addr) = SCARG(uap, addr);
1537 SCARG(&msa, len) = SCARG(uap, len);
1538 SCARG(&msa, flags) = (int)SCARG(uap, arg);
1542 case SVR4_MC_ADVISE:
1544 struct madvise_args maa;
1546 SCARG(&maa, addr) = SCARG(uap, addr);
1547 SCARG(&maa, len) = SCARG(uap, len);
1548 SCARG(&maa, behav) = (int)SCARG(uap, arg);
1550 return madvise(&maa);
1553 case SVR4_MC_UNLOCK:
1554 case SVR4_MC_LOCKAS:
1555 case SVR4_MC_UNLOCKAS:
1564 svr4_sys_nice(struct svr4_sys_nice_args *uap)
1566 struct setpriority_args ap;
1569 SCARG(&ap, which) = PRIO_PROCESS;
1570 SCARG(&ap, who) = 0;
1571 SCARG(&ap, prio) = SCARG(uap, prio);
1573 if ((error = setpriority(&ap)) != 0)
1576 /* the cast is stupid, but the structures are the same */
1577 if ((error = getpriority((struct getpriority_args *)&ap)) != 0)
1584 svr4_sys_resolvepath(struct svr4_sys_resolvepath_args *uap)
1586 struct proc *p = curproc;
1587 struct nameidata nd;
1588 int error, *retval = p->p_retval;
1590 NDINIT(&nd, LOOKUP, NOFOLLOW | SAVENAME, UIO_USERSPACE,
1591 SCARG(uap, path), p);
1593 if ((error = namei(&nd)) != 0)
1596 if ((error = copyout(nd.ni_cnd.cn_pnbuf, SCARG(uap, buf),
1597 SCARG(uap, bufsiz))) != 0)
1600 *retval = strlen(nd.ni_cnd.cn_pnbuf) < SCARG(uap, bufsiz) ?
1601 strlen(nd.ni_cnd.cn_pnbuf) + 1 : SCARG(uap, bufsiz);
1603 NDFREE(&nd, NDF_ONLY_PNBUF);