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.
29 * SVR4 compatibility module.
31 * SVR4 system calls that are implemented differently in BSD are
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/dirent.h>
43 #include <sys/fcntl.h>
44 #include <sys/filedesc.h>
45 #include <sys/imgact.h>
46 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/file.h> /* Must come after sys/malloc.h */
52 #include <sys/mount.h>
54 #include <sys/mutex.h>
55 #include <sys/namei.h>
57 #include <sys/ptrace.h>
58 #include <sys/resource.h>
59 #include <sys/resourcevar.h>
61 #include <sys/signalvar.h>
64 #include <sys/syscallsubr.h>
65 #include <sys/sysproto.h>
67 #include <sys/times.h>
69 #include <sys/vnode.h>
72 #include <compat/svr4/svr4.h>
73 #include <compat/svr4/svr4_types.h>
74 #include <compat/svr4/svr4_signal.h>
75 #include <compat/svr4/svr4_proto.h>
76 #include <compat/svr4/svr4_util.h>
77 #include <compat/svr4/svr4_sysconfig.h>
78 #include <compat/svr4/svr4_dirent.h>
79 #include <compat/svr4/svr4_acl.h>
80 #include <compat/svr4/svr4_ulimit.h>
81 #include <compat/svr4/svr4_statvfs.h>
82 #include <compat/svr4/svr4_hrt.h>
83 #include <compat/svr4/svr4_mman.h>
84 #include <compat/svr4/svr4_wait.h>
86 #include <machine/vmparam.h>
88 #include <vm/vm_param.h>
89 #include <vm/vm_map.h>
90 #if defined(__FreeBSD__)
92 #include <vm/vm_extern.h>
97 # include <uvm/uvm_extern.h>
101 #define BSD_DIRENT(cp) ((struct dirent *)(cp))
103 static int svr4_mknod(struct thread *, register_t *, char *,
104 svr4_mode_t, svr4_dev_t);
106 static __inline clock_t timeval_to_clock_t(struct timeval *);
107 static int svr4_setinfo (pid_t , struct rusage *, int, svr4_siginfo_t *);
109 struct svr4_hrtcntl_args;
110 static int svr4_hrtcntl (struct thread *, struct svr4_hrtcntl_args *,
112 static void bsd_statfs_to_svr4_statvfs(const struct statfs *,
113 struct svr4_statvfs *);
114 static void bsd_statfs_to_svr4_statvfs64(const struct statfs *,
115 struct svr4_statvfs64 *);
116 static struct proc *svr4_pfind(pid_t pid);
121 svr4_sys_setitimer(td, uap)
122 register struct thread *td;
123 struct svr4_sys_setitimer_args *uap;
125 td->td_retval[0] = 0;
131 svr4_sys_wait(td, uap)
133 struct svr4_sys_wait_args *uap;
137 error = kern_wait(td, WAIT_ANY, &st, 0, NULL);
141 if (WIFSIGNALED(st)) {
143 if (sig >= 0 && sig < NSIG)
144 st = (st & ~0177) | SVR4_BSD2SVR4_SIG(sig);
145 } else if (WIFSTOPPED(st)) {
147 if (sig >= 0 && sig < NSIG)
148 st = (st & ~0xff00) | (SVR4_BSD2SVR4_SIG(sig) << 8);
152 * It looks like wait(2) on svr4/solaris/2.4 returns
153 * the status in retval[1], and the pid on retval[0].
155 td->td_retval[1] = st;
158 error = copyout(&st, uap->status, sizeof(st));
164 svr4_sys_execv(td, uap)
166 struct svr4_sys_execv_args *uap;
168 struct image_args eargs;
172 CHECKALTEXIST(td, uap->path, &path);
174 error = exec_copyin_args(&eargs, path, UIO_SYSSPACE, uap->argp, NULL);
177 error = kern_execve(td, &eargs, NULL);
182 svr4_sys_execve(td, uap)
184 struct svr4_sys_execve_args *uap;
186 struct image_args eargs;
190 CHECKALTEXIST(td, uap->path, &path);
192 error = exec_copyin_args(&eargs, path, UIO_SYSSPACE, uap->argp,
196 error = kern_execve(td, &eargs, NULL);
203 struct svr4_sys_time_args *v;
205 struct svr4_sys_time_args *uap = v;
211 error = copyout(&tv.tv_sec, uap->t,
213 td->td_retval[0] = (int) tv.tv_sec;
220 * Read SVR4-style directory entries. We suck them into kernel space so
221 * that they can be massaged before being copied out to user code.
223 * This code is ported from the Linux emulator: Changes to the VFS interface
224 * between FreeBSD and NetBSD have made it simpler to port it from there than
225 * to adapt the NetBSD version.
228 svr4_sys_getdents64(td, uap)
230 struct svr4_sys_getdents64_args *uap;
232 register struct dirent *bdp;
234 caddr_t inp, buf; /* BSD-format */
235 int len, reclen; /* BSD-format */
236 caddr_t outp; /* SVR4-format */
237 int resid, svr4reclen=0; /* SVR4-format */
242 struct svr4_dirent64 svr4_dirent;
243 int buflen, error, eofflag, nbytes, justone, vfslocked;
244 u_long *cookies = NULL, *cookiep;
247 DPRINTF(("svr4_sys_getdents64(%d, *, %d)\n",
248 uap->fd, uap->nbytes));
249 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0) {
253 if ((fp->f_flag & FREAD) == 0) {
259 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
260 if (vp->v_type != VDIR) {
261 VFS_UNLOCK_GIANT(vfslocked);
266 nbytes = uap->nbytes;
268 nbytes = sizeof (struct svr4_dirent64);
275 #define DIRBLKSIZ 512 /* XXX we used to use ufs's DIRBLKSIZ */
276 buflen = max(DIRBLKSIZ, nbytes);
277 buflen = min(buflen, MAXBSIZE);
278 buf = malloc(buflen, M_TEMP, M_WAITOK);
279 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
282 aiov.iov_len = buflen;
283 auio.uio_iov = &aiov;
285 auio.uio_rw = UIO_READ;
286 auio.uio_segflg = UIO_SYSSPACE;
288 auio.uio_resid = buflen;
289 auio.uio_offset = off;
292 free(cookies, M_TEMP);
297 error = mac_check_vnode_readdir(td->td_ucred, vp);
302 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
303 &ncookies, &cookies);
309 outp = (caddr_t) uap->dp;
311 if ((len = buflen - auio.uio_resid) <= 0) {
319 * When using cookies, the vfs has the option of reading from
320 * a different offset than that supplied (UFS truncates the
321 * offset to a block boundary to make sure that it never reads
322 * partway through a directory entry, even if the directory
323 * has been compacted).
325 while (len > 0 && ncookies > 0 && *cookiep <= off) {
326 bdp = (struct dirent *) inp;
327 len -= bdp->d_reclen;
328 inp += bdp->d_reclen;
335 if (cookiep && ncookies == 0)
337 bdp = (struct dirent *) inp;
338 reclen = bdp->d_reclen;
340 DPRINTF(("svr4_readdir: reclen=%d\n", reclen));
345 if (bdp->d_fileno == 0) {
355 svr4reclen = SVR4_RECLEN(&svr4_dirent, bdp->d_namlen);
356 if (reclen > len || resid < svr4reclen) {
360 svr4_dirent.d_ino = (long) bdp->d_fileno;
363 * old svr4-style readdir usage.
365 svr4_dirent.d_off = (svr4_off_t) svr4reclen;
366 svr4_dirent.d_reclen = (u_short) bdp->d_namlen;
368 svr4_dirent.d_off = (svr4_off_t)(off + reclen);
369 svr4_dirent.d_reclen = (u_short) svr4reclen;
371 strcpy(svr4_dirent.d_name, bdp->d_name);
372 if ((error = copyout((caddr_t)&svr4_dirent, outp, svr4reclen)))
387 if (outp == (caddr_t) uap->dp)
392 nbytes = resid + svr4reclen;
395 td->td_retval[0] = nbytes - resid;
397 VOP_UNLOCK(vp, 0, td);
398 VFS_UNLOCK_GIANT(vfslocked);
401 free(cookies, M_TEMP);
408 svr4_sys_getdents(td, uap)
410 struct svr4_sys_getdents_args *uap;
414 caddr_t inp, buf; /* BSD-format */
415 int len, reclen; /* BSD-format */
416 caddr_t outp; /* SVR4-format */
417 int resid, svr4_reclen; /* SVR4-format */
421 struct svr4_dirent idb;
422 off_t off; /* true file offset */
423 int buflen, error, eofflag, vfslocked;
424 u_long *cookiebuf = NULL, *cookie;
425 int ncookies = 0, *retval = td->td_retval;
430 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
433 if ((fp->f_flag & FREAD) == 0) {
439 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
440 if (vp->v_type != VDIR) {
441 VFS_UNLOCK_GIANT(vfslocked);
446 buflen = min(MAXBSIZE, uap->nbytes);
447 buf = malloc(buflen, M_TEMP, M_WAITOK);
448 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
452 aiov.iov_len = buflen;
453 auio.uio_iov = &aiov;
455 auio.uio_rw = UIO_READ;
456 auio.uio_segflg = UIO_SYSSPACE;
458 auio.uio_resid = buflen;
459 auio.uio_offset = off;
462 error = mac_check_vnode_readdir(td->td_ucred, vp);
468 * First we read into the malloc'ed buffer, then
469 * we massage it into user space, one record at a time.
471 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies,
480 if ((len = buflen - auio.uio_resid) == 0)
483 for (cookie = cookiebuf; len > 0; len -= reclen) {
484 bdp = (struct dirent *)inp;
485 reclen = bdp->d_reclen;
487 panic("svr4_sys_getdents64: bad reclen");
488 off = *cookie++; /* each entry points to the next */
489 if ((off >> 32) != 0) {
490 uprintf("svr4_sys_getdents64: dir offset too large for emulated program");
494 if (bdp->d_fileno == 0) {
495 inp += reclen; /* it is a hole; squish it out */
498 svr4_reclen = SVR4_RECLEN(&idb, bdp->d_namlen);
499 if (reclen > len || resid < svr4_reclen) {
500 /* entry too big for buffer, so just stop */
505 * Massage in place to make a SVR4-shaped dirent (otherwise
506 * we have to worry about touching user memory outside of
507 * the copyout() call).
509 idb.d_ino = (svr4_ino_t)bdp->d_fileno;
510 idb.d_off = (svr4_off_t)off;
511 idb.d_reclen = (u_short)svr4_reclen;
512 strcpy(idb.d_name, bdp->d_name);
513 if ((error = copyout((caddr_t)&idb, outp, svr4_reclen)))
515 /* advance past this real entry */
517 /* advance output past SVR4-shaped entry */
519 resid -= svr4_reclen;
522 /* if we squished out the whole block, try again */
523 if (outp == uap->buf)
525 fp->f_offset = off; /* update the vnode offset */
528 *retval = uap->nbytes - resid;
530 VOP_UNLOCK(vp, 0, td);
531 VFS_UNLOCK_GIANT(vfslocked);
534 free(cookiebuf, M_TEMP);
541 svr4_sys_mmap(td, uap)
543 struct svr4_sys_mmap_args *uap;
548 retval = td->td_retval;
549 #define _MAP_NEW 0x80000000
551 * Verify the arguments.
553 if (uap->prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
554 return EINVAL; /* XXX still needed? */
561 mm.flags = uap->flags & ~_MAP_NEW;
566 return mmap(td, &mm);
570 svr4_sys_mmap64(td, uap)
572 struct svr4_sys_mmap64_args *uap;
577 #define _MAP_NEW 0x80000000
579 * Verify the arguments.
581 if (uap->prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
582 return EINVAL; /* XXX still needed? */
589 mm.flags = uap->flags & ~_MAP_NEW;
594 rp = (void *) round_page((vm_offset_t)(td->td_proc->p_vmspace->vm_daddr + maxdsiz));
595 if ((mm.flags & MAP_FIXED) == 0 &&
596 mm.addr != 0 && (void *)mm.addr < rp)
599 return mmap(td, &mm);
604 svr4_sys_fchroot(td, uap)
606 struct svr4_sys_fchroot_args *uap;
608 struct filedesc *fdp = td->td_proc->p_fd;
611 int error, vfslocked;
613 if ((error = suser(td)) != 0)
615 if ((error = getvnode(fdp, uap->fd, &fp)) != 0)
620 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
621 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
622 error = change_dir(vp, td);
626 error = mac_check_vnode_chroot(td->td_ucred, vp);
630 VOP_UNLOCK(vp, 0, td);
631 error = change_root(vp, td);
633 VFS_UNLOCK_GIANT(vfslocked);
637 VFS_UNLOCK_GIANT(vfslocked);
643 svr4_mknod(td, retval, path, mode, dev)
653 CHECKALTEXIST(td, path, &newpath);
656 error = kern_mkfifo(td, newpath, UIO_SYSSPACE, mode);
658 error = kern_mknod(td, newpath, UIO_SYSSPACE, mode, dev);
659 free(newpath, M_TEMP);
665 svr4_sys_mknod(td, uap)
666 register struct thread *td;
667 struct svr4_sys_mknod_args *uap;
669 int *retval = td->td_retval;
670 return svr4_mknod(td, retval,
671 uap->path, uap->mode,
672 (svr4_dev_t)svr4_to_bsd_odev_t(uap->dev));
677 svr4_sys_xmknod(td, uap)
679 struct svr4_sys_xmknod_args *uap;
681 int *retval = td->td_retval;
682 return svr4_mknod(td, retval,
683 uap->path, uap->mode,
684 (svr4_dev_t)svr4_to_bsd_dev_t(uap->dev));
689 svr4_sys_vhangup(td, uap)
691 struct svr4_sys_vhangup_args *uap;
698 svr4_sys_sysconfig(td, uap)
700 struct svr4_sys_sysconfig_args *uap;
704 retval = &(td->td_retval[0]);
707 case SVR4_CONFIG_UNUSED:
710 case SVR4_CONFIG_NGROUPS:
711 *retval = NGROUPS_MAX;
713 case SVR4_CONFIG_CHILD_MAX:
716 case SVR4_CONFIG_OPEN_FILES:
719 case SVR4_CONFIG_POSIX_VER:
722 case SVR4_CONFIG_PAGESIZE:
725 case SVR4_CONFIG_CLK_TCK:
726 *retval = 60; /* should this be `hz', ie. 100? */
728 case SVR4_CONFIG_XOPEN_VER:
729 *retval = 2; /* XXX: What should that be? */
731 case SVR4_CONFIG_PROF_TCK:
732 *retval = 60; /* XXX: What should that be? */
734 case SVR4_CONFIG_NPROC_CONF:
735 *retval = 1; /* Only one processor for now */
737 case SVR4_CONFIG_NPROC_ONLN:
738 *retval = 1; /* And it better be online */
740 case SVR4_CONFIG_AIO_LISTIO_MAX:
741 case SVR4_CONFIG_AIO_MAX:
742 case SVR4_CONFIG_AIO_PRIO_DELTA_MAX:
743 *retval = 0; /* No aio support */
745 case SVR4_CONFIG_DELAYTIMER_MAX:
746 *retval = 0; /* No delaytimer support */
748 case SVR4_CONFIG_MQ_OPEN_MAX:
749 *retval = msginfo.msgmni;
751 case SVR4_CONFIG_MQ_PRIO_MAX:
752 *retval = 0; /* XXX: Don't know */
754 case SVR4_CONFIG_RTSIG_MAX:
757 case SVR4_CONFIG_SEM_NSEMS_MAX:
758 *retval = seminfo.semmni;
760 case SVR4_CONFIG_SEM_VALUE_MAX:
761 *retval = seminfo.semvmx;
763 case SVR4_CONFIG_SIGQUEUE_MAX:
764 *retval = 0; /* XXX: Don't know */
766 case SVR4_CONFIG_SIGRT_MIN:
767 case SVR4_CONFIG_SIGRT_MAX:
768 *retval = 0; /* No real time signals */
770 case SVR4_CONFIG_TIMER_MAX:
771 *retval = 3; /* XXX: real, virtual, profiling */
774 case SVR4_CONFIG_PHYS_PAGES:
776 *retval = uvmexp.free; /* XXX: free instead of total */
778 *retval = cnt.v_free_count; /* XXX: free instead of total */
781 case SVR4_CONFIG_AVPHYS_PAGES:
783 *retval = uvmexp.active; /* XXX: active instead of avg */
785 *retval = cnt.v_active_count; /* XXX: active instead of avg */
798 svr4_sys_break(td, uap)
800 struct svr4_sys_break_args *uap;
802 struct obreak_args ap;
804 ap.nsize = uap->nsize;
805 return (obreak(td, &ap));
808 static __inline clock_t
809 timeval_to_clock_t(tv)
812 return tv->tv_sec * hz + tv->tv_usec / (1000000 / hz);
817 svr4_sys_times(td, uap)
819 struct svr4_sys_times_args *uap;
821 struct timeval tv, utime, stime, cutime, cstime;
828 calcru(p, &utime, &stime);
829 calccru(p, &cutime, &cstime);
832 tms.tms_utime = timeval_to_clock_t(&utime);
833 tms.tms_stime = timeval_to_clock_t(&stime);
835 tms.tms_cutime = timeval_to_clock_t(&cutime);
836 tms.tms_cstime = timeval_to_clock_t(&cstime);
838 error = copyout(&tms, uap->tp, sizeof(tms));
843 td->td_retval[0] = (int)timeval_to_clock_t(&tv);
849 svr4_sys_ulimit(td, uap)
851 struct svr4_sys_ulimit_args *uap;
853 int *retval = td->td_retval;
858 PROC_LOCK(td->td_proc);
859 *retval = lim_cur(td->td_proc, RLIMIT_FSIZE) / 512;
860 PROC_UNLOCK(td->td_proc);
862 *retval = 0x7fffffff;
869 krl.rlim_cur = uap->newlimit * 512;
870 PROC_LOCK(td->td_proc);
871 krl.rlim_max = lim_max(td->td_proc, RLIMIT_FSIZE);
872 PROC_UNLOCK(td->td_proc);
874 error = kern_setrlimit(td, RLIMIT_FSIZE, &krl);
878 PROC_LOCK(td->td_proc);
879 *retval = lim_cur(td->td_proc, RLIMIT_FSIZE);
880 PROC_UNLOCK(td->td_proc);
882 *retval = 0x7fffffff;
888 struct vmspace *vm = td->td_proc->p_vmspace;
891 PROC_LOCK(td->td_proc);
892 r = lim_cur(td->td_proc, RLIMIT_DATA);
893 PROC_UNLOCK(td->td_proc);
897 mtx_lock(&Giant); /* XXX */
898 r += (long) vm->vm_daddr;
907 PROC_LOCK(td->td_proc);
908 *retval = lim_cur(td->td_proc, RLIMIT_NOFILE);
909 PROC_UNLOCK(td->td_proc);
911 *retval = 0x7fffffff;
925 /* look in the live processes */
926 if ((p = pfind(pid)) == NULL)
927 /* look in the zombies */
935 svr4_sys_pgrpsys(td, uap)
937 struct svr4_sys_pgrpsys_args *uap;
939 int *retval = td->td_retval;
940 struct proc *p = td->td_proc;
943 case 1: /* setpgrp() */
945 * SVR4 setpgrp() (which takes no arguments) has the
946 * semantics that the session ID is also created anew, so
947 * in almost every sense, setpgrp() is identical to
948 * setsid() for SVR4. (Under BSD, the difference is that
949 * a setpgid(0,0) will not create a new session.)
954 case 0: /* getpgrp() */
956 *retval = p->p_pgrp->pg_id;
960 case 2: /* getsid(pid) */
963 else if ((p = svr4_pfind(uap->pid)) == NULL)
966 * This has already been initialized to the pid of
967 * the session leader.
969 *retval = (register_t) p->p_session->s_sid;
973 case 3: /* setsid() */
974 return setsid(td, NULL);
976 case 4: /* getpgid(pid) */
980 else if ((p = svr4_pfind(uap->pid)) == NULL)
983 *retval = (int) p->p_pgrp->pg_id;
987 case 5: /* setpgid(pid, pgid); */
989 struct setpgid_args sa;
993 return setpgid(td, &sa);
1001 struct svr4_hrtcntl_args {
1005 svr4_hrt_interval_t * iv;
1006 svr4_hrt_time_t * ti;
1011 svr4_hrtcntl(td, uap, retval)
1013 struct svr4_hrtcntl_args *uap;
1017 case SVR4_HRT_CNTL_RES:
1018 DPRINTF(("htrcntl(RES)\n"));
1019 *retval = SVR4_HRT_USEC;
1022 case SVR4_HRT_CNTL_TOFD:
1023 DPRINTF(("htrcntl(TOFD)\n"));
1027 if (uap->clk != SVR4_HRT_CLK_STD) {
1028 DPRINTF(("clk == %d\n", uap->clk));
1031 if (uap->ti == NULL) {
1032 DPRINTF(("ti NULL\n"));
1036 t.h_sec = tv.tv_sec;
1037 t.h_rem = tv.tv_usec;
1038 t.h_res = SVR4_HRT_USEC;
1039 return copyout(&t, uap->ti, sizeof(t));
1042 case SVR4_HRT_CNTL_START:
1043 DPRINTF(("htrcntl(START)\n"));
1046 case SVR4_HRT_CNTL_GET:
1047 DPRINTF(("htrcntl(GET)\n"));
1050 DPRINTF(("Bad htrcntl command %d\n", uap->fun));
1057 svr4_sys_hrtsys(td, uap)
1059 struct svr4_sys_hrtsys_args *uap;
1061 int *retval = td->td_retval;
1065 return svr4_hrtcntl(td, (struct svr4_hrtcntl_args *) uap,
1069 DPRINTF(("hrtalarm\n"));
1073 DPRINTF(("hrtsleep\n"));
1077 DPRINTF(("hrtcancel\n"));
1081 DPRINTF(("Bad hrtsys command %d\n", uap->cmd));
1088 svr4_setinfo(pid, ru, st, s)
1097 memset(&i, 0, sizeof(i));
1099 i.svr4_si_signo = SVR4_SIGCHLD;
1100 i.svr4_si_errno = 0; /* XXX? */
1102 i.svr4_si_pid = pid;
1104 i.svr4_si_stime = ru->ru_stime.tv_sec;
1105 i.svr4_si_utime = ru->ru_utime.tv_sec;
1108 if (WIFEXITED(st)) {
1109 i.svr4_si_status = WEXITSTATUS(st);
1110 i.svr4_si_code = SVR4_CLD_EXITED;
1111 } else if (WIFSTOPPED(st)) {
1113 if (sig >= 0 && sig < NSIG)
1114 i.svr4_si_status = SVR4_BSD2SVR4_SIG(sig);
1116 if (i.svr4_si_status == SVR4_SIGCONT)
1117 i.svr4_si_code = SVR4_CLD_CONTINUED;
1119 i.svr4_si_code = SVR4_CLD_STOPPED;
1122 if (sig >= 0 && sig < NSIG)
1123 i.svr4_si_status = SVR4_BSD2SVR4_SIG(sig);
1126 i.svr4_si_code = SVR4_CLD_DUMPED;
1128 i.svr4_si_code = SVR4_CLD_KILLED;
1131 DPRINTF(("siginfo [pid %ld signo %d code %d errno %d status %d]\n",
1132 i.svr4_si_pid, i.svr4_si_signo, i.svr4_si_code, i.svr4_si_errno,
1135 return copyout(&i, s, sizeof(i));
1140 svr4_sys_waitsys(td, uap)
1142 struct svr4_sys_waitsys_args *uap;
1147 int error, *retval = td->td_retval;
1150 DPRINTF(("waitsys(%d, %d, %p, %x)\n",
1152 uap->info, uap->options));
1174 /* Hand off the easy cases to kern_wait(). */
1175 if (!(uap->options & (SVR4_WNOWAIT)) &&
1176 (uap->options & (SVR4_WEXITED | SVR4_WTRAPPED))) {
1180 if (uap->options & SVR4_WSTOPPED)
1181 options |= WUNTRACED;
1182 if (uap->options & SVR4_WCONTINUED)
1183 options |= WCONTINUED;
1184 if (uap->options & SVR4_WNOHANG)
1187 error = kern_wait(td, pid, &status, options, &ru);
1190 if (uap->options & SVR4_WNOHANG && *retval == 0)
1191 error = svr4_setinfo(*retval, NULL, 0, uap->info);
1193 error = svr4_setinfo(*retval, &ru, status, uap->info);
1199 * Ok, handle the weird cases. Either WNOWAIT is set (meaning we
1200 * just want to see if there is a process to harvest, we dont'
1201 * want to actually harvest it), or WEXIT and WTRAPPED are clear
1202 * meaning we want to ignore zombies. Either way, we don't have
1203 * to handle harvesting zombies here. We do have to duplicate the
1204 * other portions of kern_wait() though, especially for the
1205 * WCONTINUED and WSTOPPED.
1209 sx_slock(&proctree_lock);
1210 LIST_FOREACH(p, &q->p_children, p_sibling) {
1212 if (pid != WAIT_ANY &&
1213 p->p_pid != pid && p->p_pgid != -pid) {
1215 DPRINTF(("pid %d pgid %d != %d\n", p->p_pid,
1219 if (p_canwait(td, p)) {
1227 * See if we have a zombie. If so, WNOWAIT should be set,
1228 * as otherwise we should have called kern_wait() up above.
1230 if ((p->p_state == PRS_ZOMBIE) &&
1231 ((uap->options & (SVR4_WEXITED|SVR4_WTRAPPED)))) {
1232 KASSERT(uap->options & SVR4_WNOWAIT,
1233 ("WNOWAIT is clear"));
1235 /* Found a zombie, so cache info in local variables. */
1237 status = p->p_xstat;
1239 calcru(p, &ru.ru_utime, &ru.ru_stime);
1241 sx_sunlock(&proctree_lock);
1243 /* Copy the info out to userland. */
1245 DPRINTF(("found %d\n", pid));
1246 return (svr4_setinfo(pid, &ru, status, uap->info));
1250 * See if we have a stopped or continued process.
1251 * XXX: This duplicates the same code in kern_wait().
1253 mtx_lock_spin(&sched_lock);
1254 if ((p->p_flag & P_STOPPED_SIG) &&
1255 (p->p_suspcount == p->p_numthreads) &&
1256 (p->p_flag & P_WAITED) == 0 &&
1257 (p->p_flag & P_TRACED || uap->options & SVR4_WSTOPPED)) {
1258 mtx_unlock_spin(&sched_lock);
1259 if (((uap->options & SVR4_WNOWAIT)) == 0)
1260 p->p_flag |= P_WAITED;
1261 sx_sunlock(&proctree_lock);
1263 status = W_STOPCODE(p->p_xstat);
1265 calcru(p, &ru.ru_utime, &ru.ru_stime);
1268 if (((uap->options & SVR4_WNOWAIT)) == 0) {
1270 sigqueue_take(p->p_ksi);
1275 DPRINTF(("jobcontrol %d\n", pid));
1276 return (svr4_setinfo(pid, &ru, status, uap->info));
1278 mtx_unlock_spin(&sched_lock);
1279 if (uap->options & SVR4_WCONTINUED &&
1280 (p->p_flag & P_CONTINUED)) {
1281 sx_sunlock(&proctree_lock);
1282 if (((uap->options & SVR4_WNOWAIT)) == 0)
1283 p->p_flag &= ~P_CONTINUED;
1287 calcru(p, &ru.ru_utime, &ru.ru_stime);
1290 if (((uap->options & SVR4_WNOWAIT)) == 0) {
1292 sigqueue_take(p->p_ksi);
1297 DPRINTF(("jobcontrol %d\n", pid));
1298 return (svr4_setinfo(pid, &ru, status, uap->info));
1304 sx_sunlock(&proctree_lock);
1308 if (uap->options & SVR4_WNOHANG) {
1309 sx_sunlock(&proctree_lock);
1311 return (svr4_setinfo(0, NULL, 0, uap->info));
1315 sx_sunlock(&proctree_lock);
1316 if (q->p_flag & P_STATCHILD) {
1317 q->p_flag &= ~P_STATCHILD;
1320 error = msleep(q, &q->p_mtx, PWAIT | PCATCH, "svr4_wait", 0);
1329 bsd_statfs_to_svr4_statvfs(bfs, sfs)
1330 const struct statfs *bfs;
1331 struct svr4_statvfs *sfs;
1333 sfs->f_bsize = bfs->f_iosize; /* XXX */
1334 sfs->f_frsize = bfs->f_bsize;
1335 sfs->f_blocks = bfs->f_blocks;
1336 sfs->f_bfree = bfs->f_bfree;
1337 sfs->f_bavail = bfs->f_bavail;
1338 sfs->f_files = bfs->f_files;
1339 sfs->f_ffree = bfs->f_ffree;
1340 sfs->f_favail = bfs->f_ffree;
1341 sfs->f_fsid = bfs->f_fsid.val[0];
1342 memcpy(sfs->f_basetype, bfs->f_fstypename, sizeof(sfs->f_basetype));
1344 if (bfs->f_flags & MNT_RDONLY)
1345 sfs->f_flag |= SVR4_ST_RDONLY;
1346 if (bfs->f_flags & MNT_NOSUID)
1347 sfs->f_flag |= SVR4_ST_NOSUID;
1348 sfs->f_namemax = MAXNAMLEN;
1349 memcpy(sfs->f_fstr, bfs->f_fstypename, sizeof(sfs->f_fstr)); /* XXX */
1350 memset(sfs->f_filler, 0, sizeof(sfs->f_filler));
1355 bsd_statfs_to_svr4_statvfs64(bfs, sfs)
1356 const struct statfs *bfs;
1357 struct svr4_statvfs64 *sfs;
1359 sfs->f_bsize = bfs->f_iosize; /* XXX */
1360 sfs->f_frsize = bfs->f_bsize;
1361 sfs->f_blocks = bfs->f_blocks;
1362 sfs->f_bfree = bfs->f_bfree;
1363 sfs->f_bavail = bfs->f_bavail;
1364 sfs->f_files = bfs->f_files;
1365 sfs->f_ffree = bfs->f_ffree;
1366 sfs->f_favail = bfs->f_ffree;
1367 sfs->f_fsid = bfs->f_fsid.val[0];
1368 memcpy(sfs->f_basetype, bfs->f_fstypename, sizeof(sfs->f_basetype));
1370 if (bfs->f_flags & MNT_RDONLY)
1371 sfs->f_flag |= SVR4_ST_RDONLY;
1372 if (bfs->f_flags & MNT_NOSUID)
1373 sfs->f_flag |= SVR4_ST_NOSUID;
1374 sfs->f_namemax = MAXNAMLEN;
1375 memcpy(sfs->f_fstr, bfs->f_fstypename, sizeof(sfs->f_fstr)); /* XXX */
1376 memset(sfs->f_filler, 0, sizeof(sfs->f_filler));
1381 svr4_sys_statvfs(td, uap)
1383 struct svr4_sys_statvfs_args *uap;
1385 struct svr4_statvfs sfs;
1390 CHECKALTEXIST(td, uap->path, &path);
1392 error = kern_statfs(td, path, UIO_SYSSPACE, &bfs);
1396 bsd_statfs_to_svr4_statvfs(&bfs, &sfs);
1397 return copyout(&sfs, uap->fs, sizeof(sfs));
1402 svr4_sys_fstatvfs(td, uap)
1404 struct svr4_sys_fstatvfs_args *uap;
1406 struct svr4_statvfs sfs;
1410 error = kern_fstatfs(td, uap->fd, &bfs);
1413 bsd_statfs_to_svr4_statvfs(&bfs, &sfs);
1414 return copyout(&sfs, uap->fs, sizeof(sfs));
1419 svr4_sys_statvfs64(td, uap)
1421 struct svr4_sys_statvfs64_args *uap;
1423 struct svr4_statvfs64 sfs;
1428 CHECKALTEXIST(td, uap->path, &path);
1430 error = kern_statfs(td, path, UIO_SYSSPACE, &bfs);
1434 bsd_statfs_to_svr4_statvfs64(&bfs, &sfs);
1435 return copyout(&sfs, uap->fs, sizeof(sfs));
1440 svr4_sys_fstatvfs64(td, uap)
1442 struct svr4_sys_fstatvfs64_args *uap;
1444 struct svr4_statvfs64 sfs;
1448 error = kern_fstatfs(td, uap->fd, &bfs);
1451 bsd_statfs_to_svr4_statvfs64(&bfs, &sfs);
1452 return copyout(&sfs, uap->fs, sizeof(sfs));
1456 svr4_sys_alarm(td, uap)
1458 struct svr4_sys_alarm_args *uap;
1460 struct itimerval itv, oitv;
1463 timevalclear(&itv.it_interval);
1464 itv.it_value.tv_sec = uap->sec;
1465 itv.it_value.tv_usec = 0;
1466 error = kern_setitimer(td, ITIMER_REAL, &itv, &oitv);
1469 if (oitv.it_value.tv_usec != 0)
1470 oitv.it_value.tv_sec++;
1471 td->td_retval[0] = oitv.it_value.tv_sec;
1476 svr4_sys_gettimeofday(td, uap)
1478 struct svr4_sys_gettimeofday_args *uap;
1484 return copyout(&atv, uap->tp, sizeof (atv));
1491 svr4_sys_facl(td, uap)
1493 struct svr4_sys_facl_args *uap;
1497 retval = td->td_retval;
1501 case SVR4_SYS_SETACL:
1502 /* We don't support acls on any filesystem */
1505 case SVR4_SYS_GETACL:
1506 return copyout(retval, &uap->num,
1509 case SVR4_SYS_GETACLCNT:
1519 svr4_sys_acl(td, uap)
1521 struct svr4_sys_acl_args *uap;
1523 /* XXX: for now the same */
1524 return svr4_sys_facl(td, (struct svr4_sys_facl_args *)uap);
1528 svr4_sys_auditsys(td, uap)
1530 struct svr4_sys_auditsys_args *uap;
1533 * XXX: Big brother is *not* watching.
1539 svr4_sys_memcntl(td, uap)
1541 struct svr4_sys_memcntl_args *uap;
1546 struct msync_args msa;
1548 msa.addr = uap->addr;
1550 msa.flags = (int)uap->arg;
1552 return msync(td, &msa);
1554 case SVR4_MC_ADVISE:
1556 struct madvise_args maa;
1558 maa.addr = uap->addr;
1560 maa.behav = (int)uap->arg;
1562 return madvise(td, &maa);
1565 case SVR4_MC_UNLOCK:
1566 case SVR4_MC_LOCKAS:
1567 case SVR4_MC_UNLOCKAS:
1576 svr4_sys_nice(td, uap)
1578 struct svr4_sys_nice_args *uap;
1580 struct setpriority_args ap;
1583 ap.which = PRIO_PROCESS;
1585 ap.prio = uap->prio;
1587 if ((error = setpriority(td, &ap)) != 0)
1590 /* the cast is stupid, but the structures are the same */
1591 if ((error = getpriority(td, (struct getpriority_args *)&ap)) != 0)
1598 svr4_sys_resolvepath(td, uap)
1600 struct svr4_sys_resolvepath_args *uap;
1602 struct nameidata nd;
1603 int error, *retval = td->td_retval;
1607 NDINIT(&nd, LOOKUP, NOFOLLOW | SAVENAME | MPSAFE, UIO_USERSPACE,
1610 if ((error = namei(&nd)) != 0)
1612 vfslocked = NDHASGIANT(&nd);
1614 ncopy = min(uap->bufsiz, strlen(nd.ni_cnd.cn_pnbuf) + 1);
1615 if ((error = copyout(nd.ni_cnd.cn_pnbuf, uap->buf, ncopy)) != 0)
1620 NDFREE(&nd, NDF_ONLY_PNBUF);
1622 VFS_UNLOCK_GIANT(vfslocked);