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 $
32 * SVR4 compatibility module.
34 * SVR4 system calls that are implemented differently in BSD are
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/namei.h>
41 #include <sys/dirent.h>
42 #include <sys/malloc.h>
48 #include <sys/filedesc.h>
49 #include <sys/kernel.h>
51 #include <sys/mount.h>
52 #include <sys/resource.h>
53 #include <sys/resourcevar.h>
54 #include <sys/vnode.h>
57 #include <sys/times.h>
58 #include <sys/fcntl.h>
61 #include <sys/ptrace.h>
62 #include <vm/vm_zone.h>
64 #include <sys/sysproto.h>
66 #include <svr4/svr4.h>
67 #include <svr4/svr4_types.h>
68 #include <svr4/svr4_signal.h>
69 #include <svr4/svr4_proto.h>
70 #include <svr4/svr4_util.h>
71 #include <svr4/svr4_sysconfig.h>
72 #include <svr4/svr4_dirent.h>
73 #include <svr4/svr4_acl.h>
74 #include <svr4/svr4_ulimit.h>
75 #include <svr4/svr4_statvfs.h>
76 #include <svr4/svr4_hrt.h>
77 #include <svr4/svr4_mman.h>
78 #include <svr4/svr4_wait.h>
80 #include <machine/vmparam.h>
82 #include <vm/vm_param.h>
83 #include <vm/vm_map.h>
85 #if defined(__FreeBSD__)
86 #include <vm/vm_extern.h>
91 # include <uvm/uvm_extern.h>
95 #define BSD_DIRENT(cp) ((struct dirent *)(cp))
97 static int svr4_mknod __P((struct proc *, register_t *, char *,
98 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 proc *, struct svr4_hrtcntl_args *,
106 static void bsd_statfs_to_svr4_statvfs __P((const struct statfs *,
107 struct svr4_statvfs *));
108 static void bsd_statfs_to_svr4_statvfs64 __P((const struct statfs *,
109 struct svr4_statvfs64 *));
110 static struct proc *svr4_pfind __P((pid_t pid));
115 svr4_sys_setitimer(p, uap)
116 register struct proc *p;
117 struct svr4_sys_setitimer_args *uap;
125 svr4_sys_wait(p, uap)
127 struct svr4_sys_wait_args *uap;
130 int error, *retval = p->p_retval, st, sig;
131 size_t sz = sizeof(*SCARG(&w4, status));
133 SCARG(&w4, rusage) = NULL;
134 SCARG(&w4, options) = 0;
136 if (SCARG(uap, status) == NULL) {
137 caddr_t sg = stackgap_init();
139 SCARG(&w4, status) = stackgap_alloc(&sg, sz);
142 SCARG(&w4, status) = SCARG(uap, status);
144 SCARG(&w4, pid) = WAIT_ANY;
146 if ((error = wait4(p, &w4)) != 0)
149 if ((error = copyin(SCARG(&w4, status), &st, sizeof(st))) != 0)
152 if (WIFSIGNALED(st)) {
154 if (sig >= 0 && sig < NSIG)
155 st = (st & ~0177) | SVR4_BSD2SVR4_SIG(sig);
156 } else if (WIFSTOPPED(st)) {
158 if (sig >= 0 && sig < NSIG)
159 st = (st & ~0xff00) | (SVR4_BSD2SVR4_SIG(sig) << 8);
163 * It looks like wait(2) on svr4/solaris/2.4 returns
164 * the status in retval[1], and the pid on retval[0].
168 if (SCARG(uap, status))
169 if ((error = copyout(&st, SCARG(uap, status), sizeof(st))) != 0)
176 svr4_sys_execv(p, uap)
178 struct svr4_sys_execv_args *uap;
180 struct execve_args ap;
183 sg = stackgap_init();
184 CHECKALTEXIST(p, &sg, SCARG(uap, path));
186 SCARG(&ap, fname) = SCARG(uap, path);
187 SCARG(&ap, argv) = SCARG(uap, argp);
188 SCARG(&ap, envv) = NULL;
190 return execve(p, &ap);
194 svr4_sys_execve(p, uap)
196 struct svr4_sys_execve_args *uap;
198 struct execve_args ap;
201 sg = stackgap_init();
202 CHECKALTEXIST(p, &sg, uap->path);
204 SCARG(&ap, fname) = SCARG(uap, path);
205 SCARG(&ap, argv) = SCARG(uap, argp);
206 SCARG(&ap, envv) = SCARG(uap, envp);
208 return execve(p, &ap);
214 struct svr4_sys_time_args *v;
216 struct svr4_sys_time_args *uap = v;
222 error = copyout(&tv.tv_sec, SCARG(uap, t),
223 sizeof(*(SCARG(uap, t))));
224 p->p_retval[0] = (int) tv.tv_sec;
231 * Read SVR4-style directory entries. We suck them into kernel space so
232 * that they can be massaged before being copied out to user code.
234 * This code is ported from the Linux emulator: Changes to the VFS interface
235 * between FreeBSD and NetBSD have made it simpler to port it from there than
236 * to adapt the NetBSD version.
239 svr4_sys_getdents64(p, uap)
241 struct svr4_sys_getdents64_args *uap;
243 register struct dirent *bdp;
245 caddr_t inp, buf; /* BSD-format */
246 int len, reclen; /* BSD-format */
247 caddr_t outp; /* SVR4-format */
248 int resid, svr4reclen=0; /* SVR4-format */
254 struct svr4_dirent64 svr4_dirent;
255 int buflen, error, eofflag, nbytes, justone;
256 u_long *cookies = NULL, *cookiep;
259 DPRINTF(("svr4_sys_getdents64(%d, *, %d)\n",
260 p->p_pid, SCARG(uap, fd), SCARG(uap, nbytes)));
261 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0) {
265 if ((fp->f_flag & FREAD) == 0)
268 vp = (struct vnode *) fp->f_data;
270 if (vp->v_type != VDIR)
273 if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p))) {
277 nbytes = SCARG(uap, nbytes);
279 nbytes = sizeof (struct svr4_dirent64);
286 #define DIRBLKSIZ 512 /* XXX we used to use ufs's DIRBLKSIZ */
287 buflen = max(DIRBLKSIZ, nbytes);
288 buflen = min(buflen, MAXBSIZE);
289 buf = malloc(buflen, M_TEMP, M_WAITOK);
290 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
293 aiov.iov_len = buflen;
294 auio.uio_iov = &aiov;
296 auio.uio_rw = UIO_READ;
297 auio.uio_segflg = UIO_SYSSPACE;
299 auio.uio_resid = buflen;
300 auio.uio_offset = off;
303 free(cookies, M_TEMP);
307 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
308 &ncookies, &cookies);
314 outp = (caddr_t) SCARG(uap, dp);
316 if ((len = buflen - auio.uio_resid) <= 0) {
324 * When using cookies, the vfs has the option of reading from
325 * a different offset than that supplied (UFS truncates the
326 * offset to a block boundary to make sure that it never reads
327 * partway through a directory entry, even if the directory
328 * has been compacted).
330 while (len > 0 && ncookies > 0 && *cookiep <= off) {
331 bdp = (struct dirent *) inp;
332 len -= bdp->d_reclen;
333 inp += bdp->d_reclen;
340 if (cookiep && ncookies == 0)
342 bdp = (struct dirent *) inp;
343 reclen = bdp->d_reclen;
345 DPRINTF(("svr4_readdir: reclen=%d\n", reclen));
350 if (bdp->d_fileno == 0) {
360 svr4reclen = SVR4_RECLEN(&svr4_dirent, bdp->d_namlen);
361 if (reclen > len || resid < svr4reclen) {
365 svr4_dirent.d_ino = (long) bdp->d_fileno;
368 * old svr4-style readdir usage.
370 svr4_dirent.d_off = (svr4_off_t) svr4reclen;
371 svr4_dirent.d_reclen = (u_short) bdp->d_namlen;
373 svr4_dirent.d_off = (svr4_off_t)(off + reclen);
374 svr4_dirent.d_reclen = (u_short) svr4reclen;
376 strcpy(svr4_dirent.d_name, bdp->d_name);
377 if ((error = copyout((caddr_t)&svr4_dirent, outp, svr4reclen)))
392 if (outp == (caddr_t) SCARG(uap, dp))
397 nbytes = resid + svr4reclen;
400 p->p_retval[0] = nbytes - resid;
403 free(cookies, M_TEMP);
404 VOP_UNLOCK(vp, 0, p);
411 svr4_sys_getdents(p, uap)
413 struct svr4_sys_getdents_args *uap;
417 caddr_t inp, buf; /* BSD-format */
418 int len, reclen; /* BSD-format */
419 caddr_t outp; /* SVR4-format */
420 int resid, svr4_reclen; /* SVR4-format */
424 struct svr4_dirent idb;
425 off_t off; /* true file offset */
426 int buflen, error, eofflag;
427 u_long *cookiebuf = NULL, *cookie;
428 int ncookies = 0, *retval = p->p_retval;
430 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
433 if ((fp->f_flag & FREAD) == 0)
436 vp = (struct vnode *)fp->f_data;
437 if (vp->v_type != VDIR)
440 buflen = min(MAXBSIZE, SCARG(uap, nbytes));
441 buf = malloc(buflen, M_TEMP, M_WAITOK);
442 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
446 aiov.iov_len = buflen;
447 auio.uio_iov = &aiov;
449 auio.uio_rw = UIO_READ;
450 auio.uio_segflg = UIO_SYSSPACE;
452 auio.uio_resid = buflen;
453 auio.uio_offset = off;
455 * First we read into the malloc'ed buffer, then
456 * we massage it into user space, one record at a time.
458 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies,
464 outp = SCARG(uap, buf);
465 resid = SCARG(uap, nbytes);
466 if ((len = buflen - auio.uio_resid) == 0)
469 for (cookie = cookiebuf; len > 0; len -= reclen) {
470 bdp = (struct dirent *)inp;
471 reclen = bdp->d_reclen;
473 panic("svr4_sys_getdents64: bad reclen");
474 off = *cookie++; /* each entry points to the next */
475 if ((off >> 32) != 0) {
476 uprintf("svr4_sys_getdents64: dir offset too large for emulated program");
480 if (bdp->d_fileno == 0) {
481 inp += reclen; /* it is a hole; squish it out */
484 svr4_reclen = SVR4_RECLEN(&idb, bdp->d_namlen);
485 if (reclen > len || resid < svr4_reclen) {
486 /* entry too big for buffer, so just stop */
491 * Massage in place to make a SVR4-shaped dirent (otherwise
492 * we have to worry about touching user memory outside of
493 * the copyout() call).
495 idb.d_ino = (svr4_ino_t)bdp->d_fileno;
496 idb.d_off = (svr4_off_t)off;
497 idb.d_reclen = (u_short)svr4_reclen;
498 strcpy(idb.d_name, bdp->d_name);
499 if ((error = copyout((caddr_t)&idb, outp, svr4_reclen)))
501 /* advance past this real entry */
503 /* advance output past SVR4-shaped entry */
505 resid -= svr4_reclen;
508 /* if we squished out the whole block, try again */
509 if (outp == SCARG(uap, buf))
511 fp->f_offset = off; /* update the vnode offset */
514 *retval = SCARG(uap, nbytes) - resid;
516 VOP_UNLOCK(vp, 0, p);
518 free(cookiebuf, M_TEMP);
525 svr4_sys_mmap(p, uap)
527 struct svr4_sys_mmap_args *uap;
532 retval = p->p_retval;
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);
554 svr4_sys_mmap64(p, uap)
556 struct svr4_sys_mmap64_args *uap;
561 #define _MAP_NEW 0x80000000
563 * Verify the arguments.
565 if (SCARG(uap, prot) & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
566 return EINVAL; /* XXX still needed? */
568 if (SCARG(uap, len) == 0)
571 SCARG(&mm, prot) = SCARG(uap, prot);
572 SCARG(&mm, len) = SCARG(uap, len);
573 SCARG(&mm, flags) = SCARG(uap, flags) & ~_MAP_NEW;
574 SCARG(&mm, fd) = SCARG(uap, fd);
575 SCARG(&mm, addr) = SCARG(uap, addr);
576 SCARG(&mm, pos) = SCARG(uap, pos);
578 rp = (void *) round_page((vm_offset_t)(p->p_vmspace->vm_daddr + maxdsiz));
579 if ((SCARG(&mm, flags) & MAP_FIXED) == 0 &&
580 SCARG(&mm, addr) != 0 && (void *)SCARG(&mm, addr) < rp)
581 SCARG(&mm, addr) = rp;
588 svr4_sys_fchroot(p, uap)
590 struct svr4_sys_fchroot_args *uap;
592 struct filedesc *fdp = p->p_fd;
597 if ((error = suser(p)) != 0)
599 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
601 vp = (struct vnode *) fp->f_data;
602 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
603 if (vp->v_type != VDIR)
606 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
607 VOP_UNLOCK(vp, 0, p);
611 if (fdp->fd_rdir != NULL)
619 svr4_mknod(p, retval, path, mode, dev)
626 caddr_t sg = stackgap_init();
628 CHECKALTEXIST(p, &sg, path);
630 if (S_ISFIFO(mode)) {
631 struct mkfifo_args ap;
632 SCARG(&ap, path) = path;
633 SCARG(&ap, mode) = mode;
634 return mkfifo(p, &ap);
636 struct mknod_args ap;
637 SCARG(&ap, path) = path;
638 SCARG(&ap, mode) = mode;
639 SCARG(&ap, dev) = dev;
640 return mknod(p, &ap);
646 svr4_sys_mknod(p, uap)
647 register struct proc *p;
648 struct svr4_sys_mknod_args *uap;
650 int *retval = p->p_retval;
651 return svr4_mknod(p, retval,
652 SCARG(uap, path), SCARG(uap, mode),
653 (svr4_dev_t)svr4_to_bsd_odev_t(SCARG(uap, dev)));
658 svr4_sys_xmknod(p, uap)
660 struct svr4_sys_xmknod_args *uap;
662 int *retval = p->p_retval;
663 return svr4_mknod(p, retval,
664 SCARG(uap, path), SCARG(uap, mode),
665 (svr4_dev_t)svr4_to_bsd_dev_t(SCARG(uap, dev)));
670 svr4_sys_vhangup(p, uap)
672 struct svr4_sys_vhangup_args *uap;
679 svr4_sys_sysconfig(p, uap)
681 struct svr4_sys_sysconfig_args *uap;
685 retval = &(p->p_retval[0]);
687 switch (SCARG(uap, name)) {
688 case SVR4_CONFIG_UNUSED:
691 case SVR4_CONFIG_NGROUPS:
692 *retval = NGROUPS_MAX;
694 case SVR4_CONFIG_CHILD_MAX:
697 case SVR4_CONFIG_OPEN_FILES:
700 case SVR4_CONFIG_POSIX_VER:
703 case SVR4_CONFIG_PAGESIZE:
706 case SVR4_CONFIG_CLK_TCK:
707 *retval = 60; /* should this be `hz', ie. 100? */
709 case SVR4_CONFIG_XOPEN_VER:
710 *retval = 2; /* XXX: What should that be? */
712 case SVR4_CONFIG_PROF_TCK:
713 *retval = 60; /* XXX: What should that be? */
715 case SVR4_CONFIG_NPROC_CONF:
716 *retval = 1; /* Only one processor for now */
718 case SVR4_CONFIG_NPROC_ONLN:
719 *retval = 1; /* And it better be online */
721 case SVR4_CONFIG_AIO_LISTIO_MAX:
722 case SVR4_CONFIG_AIO_MAX:
723 case SVR4_CONFIG_AIO_PRIO_DELTA_MAX:
724 *retval = 0; /* No aio support */
726 case SVR4_CONFIG_DELAYTIMER_MAX:
727 *retval = 0; /* No delaytimer support */
729 case SVR4_CONFIG_MQ_OPEN_MAX:
730 *retval = msginfo.msgmni;
732 case SVR4_CONFIG_MQ_PRIO_MAX:
733 *retval = 0; /* XXX: Don't know */
735 case SVR4_CONFIG_RTSIG_MAX:
738 case SVR4_CONFIG_SEM_NSEMS_MAX:
739 *retval = seminfo.semmni;
741 case SVR4_CONFIG_SEM_VALUE_MAX:
742 *retval = seminfo.semvmx;
744 case SVR4_CONFIG_SIGQUEUE_MAX:
745 *retval = 0; /* XXX: Don't know */
747 case SVR4_CONFIG_SIGRT_MIN:
748 case SVR4_CONFIG_SIGRT_MAX:
749 *retval = 0; /* No real time signals */
751 case SVR4_CONFIG_TIMER_MAX:
752 *retval = 3; /* XXX: real, virtual, profiling */
755 case SVR4_CONFIG_PHYS_PAGES:
757 *retval = uvmexp.free; /* XXX: free instead of total */
759 *retval = cnt.v_free_count; /* XXX: free instead of total */
762 case SVR4_CONFIG_AVPHYS_PAGES:
764 *retval = uvmexp.active; /* XXX: active instead of avg */
766 *retval = cnt.v_active_count; /* XXX: active instead of avg */
777 extern int swap_pager_full;
781 svr4_sys_break(p, uap)
783 struct svr4_sys_break_args *uap;
785 struct vmspace *vm = p->p_vmspace;
786 vm_offset_t new, old, base, ns;
789 base = round_page((vm_offset_t) vm->vm_daddr);
790 ns = (vm_offset_t)SCARG(uap, nsize);
791 new = round_page(ns);
793 if ((new - base) > (unsigned) p->p_rlimit[RLIMIT_DATA].rlim_cur) {
796 if (new >= VM_MAXUSER_ADDRESS) {
799 } else if (new < base) {
801 * This is simply an invalid value. If someone wants to
802 * do fancy address space manipulations, mmap and munmap
803 * can do most of what the user would want.
808 old = base + ctob(vm->vm_dsize);
813 if (vm->vm_map.size + diff > p->p_rlimit[RLIMIT_VMEM].rlim_cur)
815 rv = vm_map_find(&vm->vm_map, NULL, 0, &old, diff, FALSE,
816 VM_PROT_ALL, VM_PROT_ALL, 0);
817 if (rv != KERN_SUCCESS) {
820 vm->vm_dsize += btoc(diff);
821 } else if (new < old) {
822 rv = vm_map_remove(&vm->vm_map, new, old);
823 if (rv != KERN_SUCCESS) {
826 vm->vm_dsize -= btoc(old - new);
832 static __inline clock_t
833 timeval_to_clock_t(tv)
836 return tv->tv_sec * hz + tv->tv_usec / (1000000 / hz);
841 svr4_sys_times(p, uap)
843 struct svr4_sys_times_args *uap;
845 int error, *retval = p->p_retval;
850 struct getrusage_args ga;
852 caddr_t sg = stackgap_init();
853 ru = stackgap_alloc(&sg, sizeof(struct rusage));
855 SCARG(&ga, who) = RUSAGE_SELF;
856 SCARG(&ga, rusage) = ru;
858 error = getrusage(p, &ga);
862 if ((error = copyin(ru, &r, sizeof r)) != 0)
865 tms.tms_utime = timeval_to_clock_t(&r.ru_utime);
866 tms.tms_stime = timeval_to_clock_t(&r.ru_stime);
868 SCARG(&ga, who) = RUSAGE_CHILDREN;
869 error = getrusage(p, &ga);
873 if ((error = copyin(ru, &r, sizeof r)) != 0)
876 tms.tms_cutime = timeval_to_clock_t(&r.ru_utime);
877 tms.tms_cstime = timeval_to_clock_t(&r.ru_stime);
880 *retval = timeval_to_clock_t(&t);
882 return copyout(&tms, SCARG(uap, tp), sizeof(tms));
887 svr4_sys_ulimit(p, uap)
889 struct svr4_sys_ulimit_args *uap;
891 int *retval = p->p_retval;
893 switch (SCARG(uap, cmd)) {
895 *retval = p->p_rlimit[RLIMIT_FSIZE].rlim_cur / 512;
897 *retval = 0x7fffffff;
903 struct __setrlimit_args srl;
905 caddr_t sg = stackgap_init();
906 struct rlimit *url = (struct rlimit *)
907 stackgap_alloc(&sg, sizeof *url);
909 krl.rlim_cur = SCARG(uap, newlimit) * 512;
910 krl.rlim_max = p->p_rlimit[RLIMIT_FSIZE].rlim_max;
912 error = copyout(&krl, url, sizeof(*url));
916 SCARG(&srl, which) = RLIMIT_FSIZE;
917 SCARG(&srl, rlp) = (struct orlimit *)url;
919 error = setrlimit(p, &srl);
923 *retval = p->p_rlimit[RLIMIT_FSIZE].rlim_cur;
925 *retval = 0x7fffffff;
931 struct vmspace *vm = p->p_vmspace;
932 register_t r = p->p_rlimit[RLIMIT_DATA].rlim_cur;
936 r += (long) vm->vm_daddr;
944 *retval = p->p_rlimit[RLIMIT_NOFILE].rlim_cur;
946 *retval = 0x7fffffff;
960 /* look in the live processes */
961 if ((p = pfind(pid)) != NULL)
964 /* look in the zombies */
965 for (p = zombproc.lh_first; p != 0; p = p->p_list.le_next)
974 svr4_sys_pgrpsys(p, uap)
976 struct svr4_sys_pgrpsys_args *uap;
978 int *retval = p->p_retval;
980 switch (SCARG(uap, cmd)) {
981 case 1: /* setpgrp() */
983 * SVR4 setpgrp() (which takes no arguments) has the
984 * semantics that the session ID is also created anew, so
985 * in almost every sense, setpgrp() is identical to
986 * setsid() for SVR4. (Under BSD, the difference is that
987 * a setpgid(0,0) will not create a new session.)
992 case 0: /* getpgrp() */
993 *retval = p->p_pgrp->pg_id;
996 case 2: /* getsid(pid) */
997 if (SCARG(uap, pid) != 0 &&
998 (p = svr4_pfind(SCARG(uap, pid))) == NULL)
1001 * This has already been initialized to the pid of
1002 * the session leader.
1004 *retval = (register_t) p->p_session->s_leader->p_pid;
1007 case 3: /* setsid() */
1008 return setsid(p, NULL);
1010 case 4: /* getpgid(pid) */
1012 if (SCARG(uap, pid) != 0 &&
1013 (p = svr4_pfind(SCARG(uap, pid))) == NULL)
1016 *retval = (int) p->p_pgrp->pg_id;
1019 case 5: /* setpgid(pid, pgid); */
1021 struct setpgid_args sa;
1023 SCARG(&sa, pid) = SCARG(uap, pid);
1024 SCARG(&sa, pgid) = SCARG(uap, pgid);
1025 return setpgid(p, &sa);
1033 #define syscallarg(x) union { x datum; register_t pad; }
1035 struct svr4_hrtcntl_args {
1039 svr4_hrt_interval_t * iv;
1040 svr4_hrt_time_t * ti;
1045 svr4_hrtcntl(p, uap, retval)
1047 struct svr4_hrtcntl_args *uap;
1050 switch (SCARG(uap, fun)) {
1051 case SVR4_HRT_CNTL_RES:
1052 DPRINTF(("htrcntl(RES)\n"));
1053 *retval = SVR4_HRT_USEC;
1056 case SVR4_HRT_CNTL_TOFD:
1057 DPRINTF(("htrcntl(TOFD)\n"));
1061 if (SCARG(uap, clk) != SVR4_HRT_CLK_STD) {
1062 DPRINTF(("clk == %d\n", SCARG(uap, clk)));
1065 if (SCARG(uap, ti) == NULL) {
1066 DPRINTF(("ti NULL\n"));
1070 t.h_sec = tv.tv_sec;
1071 t.h_rem = tv.tv_usec;
1072 t.h_res = SVR4_HRT_USEC;
1073 return copyout(&t, SCARG(uap, ti), sizeof(t));
1076 case SVR4_HRT_CNTL_START:
1077 DPRINTF(("htrcntl(START)\n"));
1080 case SVR4_HRT_CNTL_GET:
1081 DPRINTF(("htrcntl(GET)\n"));
1084 DPRINTF(("Bad htrcntl command %d\n", SCARG(uap, fun)));
1091 svr4_sys_hrtsys(p, uap)
1093 struct svr4_sys_hrtsys_args *uap;
1095 int *retval = p->p_retval;
1097 switch (SCARG(uap, cmd)) {
1099 return svr4_hrtcntl(p, (struct svr4_hrtcntl_args *) uap,
1103 DPRINTF(("hrtalarm\n"));
1107 DPRINTF(("hrtsleep\n"));
1111 DPRINTF(("hrtcancel\n"));
1115 DPRINTF(("Bad hrtsys command %d\n", SCARG(uap, cmd)));
1122 svr4_setinfo(p, st, s)
1130 memset(&i, 0, sizeof(i));
1132 i.si_signo = SVR4_SIGCHLD;
1133 i.si_errno = 0; /* XXX? */
1136 i.si_pid = p->p_pid;
1137 if (p->p_stat == SZOMB) {
1138 i.si_stime = p->p_ru->ru_stime.tv_sec;
1139 i.si_utime = p->p_ru->ru_utime.tv_sec;
1142 i.si_stime = p->p_stats->p_ru.ru_stime.tv_sec;
1143 i.si_utime = p->p_stats->p_ru.ru_utime.tv_sec;
1147 if (WIFEXITED(st)) {
1148 i.si_status = WEXITSTATUS(st);
1149 i.si_code = SVR4_CLD_EXITED;
1150 } else if (WIFSTOPPED(st)) {
1152 if (sig >= 0 && sig < NSIG)
1153 i.si_status = SVR4_BSD2SVR4_SIG(sig);
1155 if (i.si_status == SVR4_SIGCONT)
1156 i.si_code = SVR4_CLD_CONTINUED;
1158 i.si_code = SVR4_CLD_STOPPED;
1161 if (sig >= 0 && sig < NSIG)
1162 i.si_status = SVR4_BSD2SVR4_SIG(sig);
1165 i.si_code = SVR4_CLD_DUMPED;
1167 i.si_code = SVR4_CLD_KILLED;
1170 DPRINTF(("siginfo [pid %ld signo %d code %d errno %d status %d]\n",
1171 i.si_pid, i.si_signo, i.si_code, i.si_errno, i.si_status));
1173 return copyout(&i, s, sizeof(i));
1178 svr4_sys_waitsys(p, uap)
1180 struct svr4_sys_waitsys_args *uap;
1183 int error, *retval = p->p_retval;
1187 switch (SCARG(uap, grp)) {
1192 SCARG(uap, id) = -p->p_pgid;
1196 SCARG(uap, id) = WAIT_ANY;
1203 DPRINTF(("waitsys(%d, %d, %p, %x)\n",
1204 SCARG(uap, grp), SCARG(uap, id),
1205 SCARG(uap, info), SCARG(uap, options)));
1209 for (q = p->p_children.lh_first; q != 0; q = q->p_sibling.le_next) {
1210 if (SCARG(uap, id) != WAIT_ANY &&
1211 q->p_pid != SCARG(uap, id) &&
1212 q->p_pgid != -SCARG(uap, id)) {
1213 DPRINTF(("pid %d pgid %d != %d\n", q->p_pid,
1214 q->p_pgid, SCARG(uap, id)));
1218 if (q->p_stat == SZOMB &&
1219 ((SCARG(uap, options) & (SVR4_WEXITED|SVR4_WTRAPPED)))) {
1221 DPRINTF(("found %d\n", q->p_pid));
1222 if ((error = svr4_setinfo(q, q->p_xstat,
1223 SCARG(uap, info))) != 0)
1227 if ((SCARG(uap, options) & SVR4_WNOWAIT)) {
1228 DPRINTF(("Don't wait\n"));
1233 * If we got the child via ptrace(2) or procfs, and
1234 * the parent is different (meaning the process was
1235 * attached, rather than run as a child), then we need
1236 * to give it back to the ol dparent, and send the
1237 * parent a SIGCHLD. The rest of the cleanup will be
1238 * done when the old parent waits on the child.
1240 if ((q->p_flag & P_TRACED) &&
1241 q->p_oppid != q->p_pptr->p_pid) {
1242 t = pfind(q->p_oppid);
1243 proc_reparent(q, t ? t : initproc);
1245 q->p_flag &= ~(P_TRACED | P_WAITED);
1246 wakeup((caddr_t)q->p_pptr);
1250 ruadd(&p->p_stats->p_cru, q->p_ru);
1252 FREE(q->p_ru, M_ZOMBIE);
1255 * Finally finished with old proc entry.
1256 * Unlink it from its process group and free it.
1260 LIST_REMOVE(q, p_list); /* off zombproc */
1262 LIST_REMOVE(q, p_sibling);
1265 * Decrement the count of procs running with this uid.
1267 (void)chgproccnt(q->p_ucred->cr_uidinfo, -1, 0);
1270 * Free up credentials.
1272 if (--q->p_cred->p_refcnt == 0) {
1274 FREE(q->p_cred, M_SUBPROC);
1278 * Release reference to text vnode
1284 * Give machine-dependent layer a chance
1285 * to free anything that cpu_exit couldn't
1286 * release while still running in process context.
1289 /* XXX what about process 'q' itself? zfree? */
1290 #if defined(__NetBSD__)
1291 pool_put(&proc_pool, q);
1296 if (q->p_stat == SSTOP && (q->p_flag & P_WAITED) == 0 &&
1297 (q->p_flag & P_TRACED ||
1298 (SCARG(uap, options) & (SVR4_WSTOPPED|SVR4_WCONTINUED)))) {
1299 DPRINTF(("jobcontrol %d\n", q->p_pid));
1300 if (((SCARG(uap, options) & SVR4_WNOWAIT)) == 0)
1301 q->p_flag |= P_WAITED;
1303 return svr4_setinfo(q, W_STOPCODE(q->p_xstat),
1311 if (SCARG(uap, options) & SVR4_WNOHANG) {
1313 if ((error = svr4_setinfo(NULL, 0, SCARG(uap, info))) != 0)
1318 if ((error = tsleep((caddr_t)p, PWAIT | PCATCH, "svr4_wait", 0)) != 0)
1325 bsd_statfs_to_svr4_statvfs(bfs, sfs)
1326 const struct statfs *bfs;
1327 struct svr4_statvfs *sfs;
1329 sfs->f_bsize = bfs->f_iosize; /* XXX */
1330 sfs->f_frsize = bfs->f_bsize;
1331 sfs->f_blocks = bfs->f_blocks;
1332 sfs->f_bfree = bfs->f_bfree;
1333 sfs->f_bavail = bfs->f_bavail;
1334 sfs->f_files = bfs->f_files;
1335 sfs->f_ffree = bfs->f_ffree;
1336 sfs->f_favail = bfs->f_ffree;
1337 sfs->f_fsid = bfs->f_fsid.val[0];
1338 memcpy(sfs->f_basetype, bfs->f_fstypename, sizeof(sfs->f_basetype));
1340 if (bfs->f_flags & MNT_RDONLY)
1341 sfs->f_flag |= SVR4_ST_RDONLY;
1342 if (bfs->f_flags & MNT_NOSUID)
1343 sfs->f_flag |= SVR4_ST_NOSUID;
1344 sfs->f_namemax = MAXNAMLEN;
1345 memcpy(sfs->f_fstr, bfs->f_fstypename, sizeof(sfs->f_fstr)); /* XXX */
1346 memset(sfs->f_filler, 0, sizeof(sfs->f_filler));
1351 bsd_statfs_to_svr4_statvfs64(bfs, sfs)
1352 const struct statfs *bfs;
1353 struct svr4_statvfs64 *sfs;
1355 sfs->f_bsize = bfs->f_iosize; /* XXX */
1356 sfs->f_frsize = bfs->f_bsize;
1357 sfs->f_blocks = bfs->f_blocks;
1358 sfs->f_bfree = bfs->f_bfree;
1359 sfs->f_bavail = bfs->f_bavail;
1360 sfs->f_files = bfs->f_files;
1361 sfs->f_ffree = bfs->f_ffree;
1362 sfs->f_favail = bfs->f_ffree;
1363 sfs->f_fsid = bfs->f_fsid.val[0];
1364 memcpy(sfs->f_basetype, bfs->f_fstypename, sizeof(sfs->f_basetype));
1366 if (bfs->f_flags & MNT_RDONLY)
1367 sfs->f_flag |= SVR4_ST_RDONLY;
1368 if (bfs->f_flags & MNT_NOSUID)
1369 sfs->f_flag |= SVR4_ST_NOSUID;
1370 sfs->f_namemax = MAXNAMLEN;
1371 memcpy(sfs->f_fstr, bfs->f_fstypename, sizeof(sfs->f_fstr)); /* XXX */
1372 memset(sfs->f_filler, 0, sizeof(sfs->f_filler));
1377 svr4_sys_statvfs(p, uap)
1379 struct svr4_sys_statvfs_args *uap;
1381 struct statfs_args fs_args;
1382 caddr_t sg = stackgap_init();
1383 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1385 struct svr4_statvfs sfs;
1388 CHECKALTEXIST(p, &sg, SCARG(uap, path));
1389 SCARG(&fs_args, path) = SCARG(uap, path);
1390 SCARG(&fs_args, buf) = fs;
1392 if ((error = statfs(p, &fs_args)) != 0)
1395 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1398 bsd_statfs_to_svr4_statvfs(&bfs, &sfs);
1400 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1405 svr4_sys_fstatvfs(p, uap)
1407 struct svr4_sys_fstatvfs_args *uap;
1409 struct fstatfs_args fs_args;
1410 caddr_t sg = stackgap_init();
1411 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1413 struct svr4_statvfs sfs;
1416 SCARG(&fs_args, fd) = SCARG(uap, fd);
1417 SCARG(&fs_args, buf) = fs;
1419 if ((error = fstatfs(p, &fs_args)) != 0)
1422 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1425 bsd_statfs_to_svr4_statvfs(&bfs, &sfs);
1427 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1432 svr4_sys_statvfs64(p, uap)
1434 struct svr4_sys_statvfs64_args *uap;
1436 struct statfs_args fs_args;
1437 caddr_t sg = stackgap_init();
1438 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1440 struct svr4_statvfs64 sfs;
1443 CHECKALTEXIST(p, &sg, SCARG(uap, path));
1444 SCARG(&fs_args, path) = SCARG(uap, path);
1445 SCARG(&fs_args, buf) = fs;
1447 if ((error = statfs(p, &fs_args)) != 0)
1450 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1453 bsd_statfs_to_svr4_statvfs64(&bfs, &sfs);
1455 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1460 svr4_sys_fstatvfs64(p, uap)
1462 struct svr4_sys_fstatvfs64_args *uap;
1464 struct fstatfs_args fs_args;
1465 caddr_t sg = stackgap_init();
1466 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1468 struct svr4_statvfs64 sfs;
1471 SCARG(&fs_args, fd) = SCARG(uap, fd);
1472 SCARG(&fs_args, buf) = fs;
1474 if ((error = fstatfs(p, &fs_args)) != 0)
1477 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1480 bsd_statfs_to_svr4_statvfs64(&bfs, &sfs);
1482 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1486 svr4_sys_alarm(p, uap)
1488 struct svr4_sys_alarm_args *uap;
1491 struct itimerval *itp, *oitp;
1492 struct setitimer_args sa;
1493 caddr_t sg = stackgap_init();
1495 itp = stackgap_alloc(&sg, sizeof(*itp));
1496 oitp = stackgap_alloc(&sg, sizeof(*oitp));
1497 timevalclear(&itp->it_interval);
1498 itp->it_value.tv_sec = SCARG(uap, sec);
1499 itp->it_value.tv_usec = 0;
1501 SCARG(&sa, which) = ITIMER_REAL;
1502 SCARG(&sa, itv) = itp;
1503 SCARG(&sa, oitv) = oitp;
1504 error = setitimer(p, &sa);
1507 if (oitp->it_value.tv_usec)
1508 oitp->it_value.tv_sec++;
1509 p->p_retval[0] = oitp->it_value.tv_sec;
1515 svr4_sys_gettimeofday(p, uap)
1517 struct svr4_sys_gettimeofday_args *uap;
1519 if (SCARG(uap, tp)) {
1523 return copyout(&atv, SCARG(uap, tp), sizeof (atv));
1530 svr4_sys_facl(p, uap)
1532 struct svr4_sys_facl_args *uap;
1536 retval = p->p_retval;
1539 switch (SCARG(uap, cmd)) {
1540 case SVR4_SYS_SETACL:
1541 /* We don't support acls on any filesystem */
1544 case SVR4_SYS_GETACL:
1545 return copyout(retval, &SCARG(uap, num),
1546 sizeof(SCARG(uap, num)));
1548 case SVR4_SYS_GETACLCNT:
1558 svr4_sys_acl(p, uap)
1560 struct svr4_sys_acl_args *uap;
1562 /* XXX: for now the same */
1563 return svr4_sys_facl(p, (struct svr4_sys_facl_args *)uap);
1567 svr4_sys_auditsys(p, uap)
1569 struct svr4_sys_auditsys_args *uap;
1572 * XXX: Big brother is *not* watching.
1578 svr4_sys_memcntl(p, uap)
1580 struct svr4_sys_memcntl_args *uap;
1582 switch (SCARG(uap, cmd)) {
1585 struct msync_args msa;
1587 SCARG(&msa, addr) = SCARG(uap, addr);
1588 SCARG(&msa, len) = SCARG(uap, len);
1589 SCARG(&msa, flags) = (int)SCARG(uap, arg);
1591 return msync(p, &msa);
1593 case SVR4_MC_ADVISE:
1595 struct madvise_args maa;
1597 SCARG(&maa, addr) = SCARG(uap, addr);
1598 SCARG(&maa, len) = SCARG(uap, len);
1599 SCARG(&maa, behav) = (int)SCARG(uap, arg);
1601 return madvise(p, &maa);
1604 case SVR4_MC_UNLOCK:
1605 case SVR4_MC_LOCKAS:
1606 case SVR4_MC_UNLOCKAS:
1615 svr4_sys_nice(p, uap)
1617 struct svr4_sys_nice_args *uap;
1619 struct setpriority_args ap;
1622 SCARG(&ap, which) = PRIO_PROCESS;
1623 SCARG(&ap, who) = 0;
1624 SCARG(&ap, prio) = SCARG(uap, prio);
1626 if ((error = setpriority(p, &ap)) != 0)
1629 /* the cast is stupid, but the structures are the same */
1630 if ((error = getpriority(p, (struct getpriority_args *)&ap)) != 0)
1637 svr4_sys_resolvepath(p, uap)
1639 struct svr4_sys_resolvepath_args *uap;
1641 struct nameidata nd;
1642 int error, *retval = p->p_retval;
1644 NDINIT(&nd, LOOKUP, NOFOLLOW | SAVENAME, UIO_USERSPACE,
1645 SCARG(uap, path), p);
1647 if ((error = namei(&nd)) != 0)
1650 if ((error = copyout(nd.ni_cnd.cn_pnbuf, SCARG(uap, buf),
1651 SCARG(uap, bufsiz))) != 0)
1654 *retval = strlen(nd.ni_cnd.cn_pnbuf) < SCARG(uap, bufsiz) ?
1655 strlen(nd.ni_cnd.cn_pnbuf) + 1 : SCARG(uap, bufsiz);
1657 NDFREE(&nd, NDF_ONLY_PNBUF);