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.12 2003/08/20 07:13:30 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/dirent.h>
42 #include <sys/malloc.h>
44 #include <sys/namei.h>
49 #include <sys/filedesc.h>
50 #include <sys/kernel.h>
52 #include <sys/mount.h>
53 #include <sys/resource.h>
54 #include <sys/resourcevar.h>
55 #include <sys/vnode.h>
58 #include <sys/times.h>
59 #include <sys/fcntl.h>
62 #include <sys/ptrace.h>
63 #include <vm/vm_zone.h>
65 #include <sys/sysproto.h>
68 #include "svr4_types.h"
69 #include "svr4_signal.h"
70 #include "svr4_proto.h"
71 #include "svr4_util.h"
72 #include "svr4_sysconfig.h"
73 #include "svr4_dirent.h"
75 #include "svr4_ulimit.h"
76 #include "svr4_statvfs.h"
78 #include "svr4_mman.h"
79 #include "svr4_wait.h"
81 #include <machine/vmparam.h>
83 #include <vm/vm_param.h>
84 #include <vm/vm_map.h>
86 #if defined(__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)
116 uap->sysmsg_result = 0;
122 svr4_sys_wait(struct svr4_sys_wait_args *uap)
125 int error, *retval = &uap->sysmsg_result, st, sig;
126 size_t sz = sizeof(*SCARG(&w4, status));
128 SCARG(&w4, rusage) = NULL;
129 SCARG(&w4, options) = 0;
131 if (SCARG(uap, status) == NULL) {
132 caddr_t sg = stackgap_init();
134 SCARG(&w4, status) = stackgap_alloc(&sg, sz);
137 SCARG(&w4, status) = SCARG(uap, status);
139 SCARG(&w4, pid) = WAIT_ANY;
140 w4.sysmsg_result = 0;
141 if ((error = wait4(&w4)) != 0)
143 *retval = w4.sysmsg_result;
145 if ((error = copyin(SCARG(&w4, status), &st, sizeof(st))) != 0)
148 if (WIFSIGNALED(st)) {
150 if (sig >= 0 && sig < NSIG)
151 st = (st & ~0177) | SVR4_BSD2SVR4_SIG(sig);
152 } else if (WIFSTOPPED(st)) {
154 if (sig >= 0 && sig < NSIG)
155 st = (st & ~0xff00) | (SVR4_BSD2SVR4_SIG(sig) << 8);
159 * It looks like wait(2) on svr4/solaris/2.4 returns
160 * the status in retval[1], and the pid on retval[0].
164 if (SCARG(uap, status))
165 if ((error = copyout(&st, SCARG(uap, status), sizeof(st))) != 0)
172 svr4_sys_execv(struct svr4_sys_execv_args *uap)
174 struct execve_args ap;
178 sg = stackgap_init();
179 CHECKALTEXIST(&sg, uap->path);
181 /* note: parts of result64 may be maintained or cleared by execve */
182 ap.sysmsg_result64 = uap->sysmsg_result64;
183 ap.fname = uap->path;
187 uap->sysmsg_result64 = ap.sysmsg_result64;
192 svr4_sys_execve(struct svr4_sys_execve_args *uap)
194 struct execve_args ap;
198 sg = stackgap_init();
199 CHECKALTEXIST(&sg, uap->path);
201 SCARG(&ap, fname) = SCARG(uap, path);
202 SCARG(&ap, argv) = SCARG(uap, argp);
203 SCARG(&ap, envv) = SCARG(uap, envp);
205 ap.sysmsg_result = 0;
207 uap->sysmsg_result = ap.sysmsg_result;
212 svr4_sys_time(struct svr4_sys_time_args *v)
214 struct svr4_sys_time_args *uap = v;
220 error = copyout(&tv.tv_sec, SCARG(uap, t),
221 sizeof(*(SCARG(uap, t))));
222 v->sysmsg_result = (int) tv.tv_sec;
229 * Read SVR4-style directory entries. We suck them into kernel space so
230 * that they can be massaged before being copied out to user code.
232 * This code is ported from the Linux emulator: Changes to the VFS interface
233 * between FreeBSD and NetBSD have made it simpler to port it from there than
234 * to adapt the NetBSD version.
237 svr4_sys_getdents64(struct svr4_sys_getdents64_args *uap)
239 struct thread *td = curthread; /* XXX */
240 struct proc *p = td->td_proc;
243 caddr_t inp, buf; /* BSD-format */
244 int len, reclen; /* BSD-format */
245 caddr_t outp; /* SVR4-format */
246 int resid, svr4reclen=0; /* SVR4-format */
252 struct svr4_dirent64 svr4_dirent;
253 int buflen, error, eofflag, nbytes, justone;
254 u_long *cookies = NULL, *cookiep;
259 DPRINTF(("svr4_sys_getdents64(%d, *, %d)\n",
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, td))) {
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, td);
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 uap->sysmsg_result = nbytes - resid;
403 free(cookies, M_TEMP);
404 VOP_UNLOCK(vp, 0, td);
411 svr4_sys_getdents(struct svr4_sys_getdents_args *uap)
413 struct thread *td = curthread;
414 struct proc *p = td->td_proc;
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 = &uap->sysmsg_result;
432 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
435 if ((fp->f_flag & FREAD) == 0)
438 vp = (struct vnode *)fp->f_data;
439 if (vp->v_type != VDIR)
442 buflen = min(MAXBSIZE, SCARG(uap, nbytes));
443 buf = malloc(buflen, M_TEMP, M_WAITOK);
444 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
448 aiov.iov_len = buflen;
449 auio.uio_iov = &aiov;
451 auio.uio_rw = UIO_READ;
452 auio.uio_segflg = UIO_SYSSPACE;
454 auio.uio_resid = buflen;
455 auio.uio_offset = off;
457 * First we read into the malloc'ed buffer, then
458 * we massage it into user space, one record at a time.
460 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies,
466 outp = SCARG(uap, buf);
467 resid = SCARG(uap, nbytes);
468 if ((len = buflen - auio.uio_resid) == 0)
471 for (cookie = cookiebuf; len > 0; len -= reclen) {
472 bdp = (struct dirent *)inp;
473 reclen = bdp->d_reclen;
475 panic("svr4_sys_getdents64: bad reclen");
476 off = *cookie++; /* each entry points to the next */
477 if ((off >> 32) != 0) {
478 uprintf("svr4_sys_getdents64: dir offset too large for emulated program");
482 if (bdp->d_fileno == 0) {
483 inp += reclen; /* it is a hole; squish it out */
486 svr4_reclen = SVR4_RECLEN(&idb, bdp->d_namlen);
487 if (reclen > len || resid < svr4_reclen) {
488 /* entry too big for buffer, so just stop */
493 * Massage in place to make a SVR4-shaped dirent (otherwise
494 * we have to worry about touching user memory outside of
495 * the copyout() call).
497 idb.d_ino = (svr4_ino_t)bdp->d_fileno;
498 idb.d_off = (svr4_off_t)off;
499 idb.d_reclen = (u_short)svr4_reclen;
500 strcpy(idb.d_name, bdp->d_name);
501 if ((error = copyout((caddr_t)&idb, outp, svr4_reclen)))
503 /* advance past this real entry */
505 /* advance output past SVR4-shaped entry */
507 resid -= svr4_reclen;
510 /* if we squished out the whole block, try again */
511 if (outp == SCARG(uap, buf))
513 fp->f_offset = off; /* update the vnode offset */
516 *retval = SCARG(uap, nbytes) - resid;
518 VOP_UNLOCK(vp, 0, td);
520 free(cookiebuf, M_TEMP);
527 svr4_sys_mmap(struct svr4_sys_mmap_args *uap)
533 retval = &uap->sysmsg_result;
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);
550 mm.sysmsg_resultp = NULL;
552 uap->sysmsg_resultp = mm.sysmsg_resultp;
557 svr4_sys_mmap64(struct svr4_sys_mmap64_args *uap)
559 struct proc *p = curproc;
564 #define _MAP_NEW 0x80000000
566 * Verify the arguments.
568 if (SCARG(uap, prot) & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
569 return EINVAL; /* XXX still needed? */
571 if (SCARG(uap, len) == 0)
574 SCARG(&mm, prot) = SCARG(uap, prot);
575 SCARG(&mm, len) = SCARG(uap, len);
576 SCARG(&mm, flags) = SCARG(uap, flags) & ~_MAP_NEW;
577 SCARG(&mm, fd) = SCARG(uap, fd);
578 SCARG(&mm, addr) = SCARG(uap, addr);
579 SCARG(&mm, pos) = SCARG(uap, pos);
581 rp = (void *) round_page((vm_offset_t)(p->p_vmspace->vm_daddr + maxdsiz));
582 if ((SCARG(&mm, flags) & MAP_FIXED) == 0 &&
583 SCARG(&mm, addr) != 0 && (void *)SCARG(&mm, addr) < rp)
584 SCARG(&mm, addr) = rp;
586 mm.sysmsg_resultp = 0;
588 uap->sysmsg_resultp = mm.sysmsg_resultp;
594 svr4_sys_fchroot(struct svr4_sys_fchroot_args *uap)
596 struct thread *td = curthread; /* XXX */
597 struct proc *p = td->td_proc;
599 struct filedesc *fdp;
608 if ((error = suser(td)) != 0)
610 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
612 vp = (struct vnode *) fp->f_data;
613 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
614 if (vp->v_type != VDIR)
617 error = VOP_ACCESS(vp, VEXEC, cred, td);
618 VOP_UNLOCK(vp, 0, td);
622 if (fdp->fd_rdir != NULL)
630 svr4_mknod(retval, path, mode, dev)
636 caddr_t sg = stackgap_init();
639 CHECKALTEXIST(&sg, path);
641 if (S_ISFIFO(mode)) {
642 struct mkfifo_args ap;
643 SCARG(&ap, path) = path;
644 SCARG(&ap, mode) = mode;
646 *retval = ap.sysmsg_result;
648 struct mknod_args ap;
649 SCARG(&ap, path) = path;
650 SCARG(&ap, mode) = mode;
651 SCARG(&ap, dev) = dev;
653 *retval = ap.sysmsg_result;
660 svr4_sys_mknod(struct svr4_sys_mknod_args *uap)
662 struct thread *td = curthread; /* XXX */
663 struct proc *p = td->td_proc;
667 retval = &uap->sysmsg_result;
669 return svr4_mknod(retval, SCARG(uap, path), SCARG(uap, mode),
670 (svr4_dev_t)svr4_to_bsd_odev_t(SCARG(uap, dev)));
675 svr4_sys_xmknod(struct svr4_sys_xmknod_args *uap)
677 struct thread *td = curthread; /* XXX */
678 struct proc *p = td->td_proc;
682 retval = &uap->sysmsg_result;
684 return svr4_mknod(retval, SCARG(uap, path), SCARG(uap, mode),
685 (svr4_dev_t)svr4_to_bsd_dev_t(SCARG(uap, dev)));
690 svr4_sys_vhangup(struct svr4_sys_vhangup_args *uap)
697 svr4_sys_sysconfig(struct svr4_sys_sysconfig_args *uap)
699 struct thread *td = curthread; /* XXX */
700 struct proc *p = td->td_proc;
704 retval = &uap->sysmsg_result;
706 switch (SCARG(uap, name)) {
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 */
796 extern int swap_pager_full;
800 svr4_sys_break(struct svr4_sys_break_args *uap)
802 struct thread *td = curthread; /* XXX */
803 struct proc *p = td->td_proc;
805 vm_offset_t new, old, base, ns;
811 base = round_page((vm_offset_t) vm->vm_daddr);
812 ns = (vm_offset_t)SCARG(uap, nsize);
813 new = round_page(ns);
815 if ((new - base) > (unsigned) p->p_rlimit[RLIMIT_DATA].rlim_cur) {
818 if (new >= VM_MAXUSER_ADDRESS) {
821 } else if (new < base) {
823 * This is simply an invalid value. If someone wants to
824 * do fancy address space manipulations, mmap and munmap
825 * can do most of what the user would want.
830 old = base + ctob(vm->vm_dsize);
835 if (vm->vm_map.size + diff > p->p_rlimit[RLIMIT_VMEM].rlim_cur)
837 rv = vm_map_find(&vm->vm_map, NULL, 0, &old, diff, FALSE,
838 VM_PROT_ALL, VM_PROT_ALL, 0);
839 if (rv != KERN_SUCCESS) {
842 vm->vm_dsize += btoc(diff);
843 } else if (new < old) {
844 rv = vm_map_remove(&vm->vm_map, new, old);
845 if (rv != KERN_SUCCESS) {
848 vm->vm_dsize -= btoc(old - new);
854 static __inline clock_t
855 timeval_to_clock_t(tv)
858 return tv->tv_sec * hz + tv->tv_usec / (1000000 / hz);
863 svr4_sys_times(struct svr4_sys_times_args *uap)
865 int error, *retval = &uap->sysmsg_result;
870 struct getrusage_args ga;
872 caddr_t sg = stackgap_init();
873 ru = stackgap_alloc(&sg, sizeof(struct rusage));
875 SCARG(&ga, who) = RUSAGE_SELF;
876 SCARG(&ga, rusage) = ru;
878 error = getrusage(&ga);
882 if ((error = copyin(ru, &r, sizeof r)) != 0)
885 tms.tms_utime = timeval_to_clock_t(&r.ru_utime);
886 tms.tms_stime = timeval_to_clock_t(&r.ru_stime);
888 SCARG(&ga, who) = RUSAGE_CHILDREN;
889 error = getrusage(&ga);
893 if ((error = copyin(ru, &r, sizeof r)) != 0)
896 tms.tms_cutime = timeval_to_clock_t(&r.ru_utime);
897 tms.tms_cstime = timeval_to_clock_t(&r.ru_stime);
900 *retval = timeval_to_clock_t(&t);
902 return copyout(&tms, SCARG(uap, tp), sizeof(tms));
907 svr4_sys_ulimit(struct svr4_sys_ulimit_args *uap)
909 struct proc *p = curproc;
910 int *retval = &uap->sysmsg_result;
912 switch (SCARG(uap, cmd)) {
914 *retval = p->p_rlimit[RLIMIT_FSIZE].rlim_cur / 512;
916 *retval = 0x7fffffff;
922 struct __setrlimit_args srl;
924 caddr_t sg = stackgap_init();
925 struct rlimit *url = (struct rlimit *)
926 stackgap_alloc(&sg, sizeof *url);
928 krl.rlim_cur = SCARG(uap, newlimit) * 512;
929 krl.rlim_max = p->p_rlimit[RLIMIT_FSIZE].rlim_max;
931 error = copyout(&krl, url, sizeof(*url));
935 SCARG(&srl, which) = RLIMIT_FSIZE;
936 SCARG(&srl, rlp) = (struct rlimit *)url;
938 error = setrlimit(&srl);
942 *retval = p->p_rlimit[RLIMIT_FSIZE].rlim_cur;
944 *retval = 0x7fffffff;
950 struct vmspace *vm = p->p_vmspace;
951 register_t r = p->p_rlimit[RLIMIT_DATA].rlim_cur;
955 r += (long) vm->vm_daddr;
963 *retval = p->p_rlimit[RLIMIT_NOFILE].rlim_cur;
965 *retval = 0x7fffffff;
979 /* look in the live processes */
980 if ((p = pfind(pid)) != NULL)
983 /* look in the zombies */
984 for (p = zombproc.lh_first; p != 0; p = p->p_list.le_next)
993 svr4_sys_pgrpsys(struct svr4_sys_pgrpsys_args *uap)
995 struct proc *p = curproc;
996 int *retval = &uap->sysmsg_result;
998 switch (SCARG(uap, cmd)) {
999 case 1: /* setpgrp() */
1001 * SVR4 setpgrp() (which takes no arguments) has the
1002 * semantics that the session ID is also created anew, so
1003 * in almost every sense, setpgrp() is identical to
1004 * setsid() for SVR4. (Under BSD, the difference is that
1005 * a setpgid(0,0) will not create a new session.)
1008 struct setsid_args sida;
1010 sida.sysmsg_result = 0;
1012 /* ignore result? */
1016 case 0: /* getpgrp() */
1017 *retval = p->p_pgrp->pg_id;
1020 case 2: /* getsid(pid) */
1021 if (SCARG(uap, pid) != 0 &&
1022 (p = svr4_pfind(SCARG(uap, pid))) == NULL)
1025 * This has already been initialized to the pid of
1026 * the session leader.
1028 *retval = (register_t) p->p_session->s_leader->p_pid;
1031 case 3: /* setsid() */
1034 struct setsid_args sida;
1037 sida.sysmsg_result = 0;
1038 error = setsid(&sida);
1039 uap->sysmsg_result = sida.sysmsg_result;
1042 case 4: /* getpgid(pid) */
1044 if (SCARG(uap, pid) != 0 &&
1045 (p = svr4_pfind(SCARG(uap, pid))) == NULL)
1048 *retval = (int) p->p_pgrp->pg_id;
1051 case 5: /* setpgid(pid, pgid); */
1053 struct setpgid_args sa;
1056 SCARG(&sa, pid) = SCARG(uap, pid);
1057 SCARG(&sa, pgid) = SCARG(uap, pgid);
1058 sa.sysmsg_result = 0;
1059 error = setpgid(&sa);
1060 uap->sysmsg_result = sa.sysmsg_result;
1068 #define syscallarg(x) union { x datum; register_t pad; }
1070 struct svr4_hrtcntl_args {
1074 svr4_hrt_interval_t * iv;
1075 svr4_hrt_time_t * ti;
1080 svr4_hrtcntl(struct svr4_hrtcntl_args *uap, register_t *retval)
1082 switch (SCARG(uap, fun)) {
1083 case SVR4_HRT_CNTL_RES:
1084 DPRINTF(("htrcntl(RES)\n"));
1085 *retval = SVR4_HRT_USEC;
1088 case SVR4_HRT_CNTL_TOFD:
1089 DPRINTF(("htrcntl(TOFD)\n"));
1093 if (SCARG(uap, clk) != SVR4_HRT_CLK_STD) {
1094 DPRINTF(("clk == %d\n", SCARG(uap, clk)));
1097 if (SCARG(uap, ti) == NULL) {
1098 DPRINTF(("ti NULL\n"));
1102 t.h_sec = tv.tv_sec;
1103 t.h_rem = tv.tv_usec;
1104 t.h_res = SVR4_HRT_USEC;
1105 return copyout(&t, SCARG(uap, ti), sizeof(t));
1108 case SVR4_HRT_CNTL_START:
1109 DPRINTF(("htrcntl(START)\n"));
1112 case SVR4_HRT_CNTL_GET:
1113 DPRINTF(("htrcntl(GET)\n"));
1116 DPRINTF(("Bad htrcntl command %d\n", SCARG(uap, fun)));
1123 svr4_sys_hrtsys(struct svr4_sys_hrtsys_args *uap)
1125 int *retval = &uap->sysmsg_result;
1127 switch (SCARG(uap, cmd)) {
1129 return svr4_hrtcntl((struct svr4_hrtcntl_args *)(&uap->sysmsg + 1), retval);
1132 DPRINTF(("hrtalarm\n"));
1136 DPRINTF(("hrtsleep\n"));
1140 DPRINTF(("hrtcancel\n"));
1144 DPRINTF(("Bad hrtsys command %d\n", SCARG(uap, cmd)));
1151 svr4_setinfo(p, st, s)
1159 memset(&i, 0, sizeof(i));
1161 i.si_signo = SVR4_SIGCHLD;
1162 i.si_errno = 0; /* XXX? */
1165 i.si_pid = p->p_pid;
1166 if (p->p_stat == SZOMB) {
1167 i.si_stime = p->p_ru->ru_stime.tv_sec;
1168 i.si_utime = p->p_ru->ru_utime.tv_sec;
1171 i.si_stime = p->p_stats->p_ru.ru_stime.tv_sec;
1172 i.si_utime = p->p_stats->p_ru.ru_utime.tv_sec;
1176 if (WIFEXITED(st)) {
1177 i.si_status = WEXITSTATUS(st);
1178 i.si_code = SVR4_CLD_EXITED;
1179 } else if (WIFSTOPPED(st)) {
1181 if (sig >= 0 && sig < NSIG)
1182 i.si_status = SVR4_BSD2SVR4_SIG(sig);
1184 if (i.si_status == SVR4_SIGCONT)
1185 i.si_code = SVR4_CLD_CONTINUED;
1187 i.si_code = SVR4_CLD_STOPPED;
1190 if (sig >= 0 && sig < NSIG)
1191 i.si_status = SVR4_BSD2SVR4_SIG(sig);
1194 i.si_code = SVR4_CLD_DUMPED;
1196 i.si_code = SVR4_CLD_KILLED;
1199 DPRINTF(("siginfo [pid %ld signo %d code %d errno %d status %d]\n",
1200 i.si_pid, i.si_signo, i.si_code, i.si_errno, i.si_status));
1202 return copyout(&i, s, sizeof(i));
1207 svr4_sys_waitsys(struct svr4_sys_waitsys_args *uap)
1209 struct proc *p = curproc;
1211 int error, *retval = &uap->sysmsg_result;
1215 switch (SCARG(uap, grp)) {
1220 SCARG(uap, id) = -p->p_pgid;
1224 SCARG(uap, id) = WAIT_ANY;
1231 DPRINTF(("waitsys(%d, %d, %p, %x)\n",
1232 SCARG(uap, grp), SCARG(uap, id),
1233 SCARG(uap, info), SCARG(uap, options)));
1237 for (q = p->p_children.lh_first; q != 0; q = q->p_sibling.le_next) {
1238 if (SCARG(uap, id) != WAIT_ANY &&
1239 q->p_pid != SCARG(uap, id) &&
1240 q->p_pgid != -SCARG(uap, id)) {
1241 DPRINTF(("pid %d pgid %d != %d\n", q->p_pid,
1242 q->p_pgid, SCARG(uap, id)));
1246 if (q->p_stat == SZOMB &&
1247 ((SCARG(uap, options) & (SVR4_WEXITED|SVR4_WTRAPPED)))) {
1249 DPRINTF(("found %d\n", q->p_pid));
1250 if ((error = svr4_setinfo(q, q->p_xstat,
1251 SCARG(uap, info))) != 0)
1255 if ((SCARG(uap, options) & SVR4_WNOWAIT)) {
1256 DPRINTF(("Don't wait\n"));
1261 * If we got the child via ptrace(2) or procfs, and
1262 * the parent is different (meaning the process was
1263 * attached, rather than run as a child), then we need
1264 * to give it back to the ol dparent, and send the
1265 * parent a SIGCHLD. The rest of the cleanup will be
1266 * done when the old parent waits on the child.
1268 if ((q->p_flag & P_TRACED) &&
1269 q->p_oppid != q->p_pptr->p_pid) {
1270 t = pfind(q->p_oppid);
1271 proc_reparent(q, t ? t : initproc);
1273 q->p_flag &= ~(P_TRACED | P_WAITED);
1274 wakeup((caddr_t)q->p_pptr);
1278 ruadd(&p->p_stats->p_cru, q->p_ru);
1280 FREE(q->p_ru, M_ZOMBIE);
1283 * Finally finished with old proc entry.
1284 * Unlink it from its process group and free it.
1288 LIST_REMOVE(q, p_list); /* off zombproc */
1290 LIST_REMOVE(q, p_sibling);
1293 * Decrement the count of procs running with this uid.
1295 (void)chgproccnt(q->p_ucred->cr_uidinfo, -1, 0);
1298 * Free up credentials.
1304 * Release reference to text vnode
1310 * Give machine-dependent layer a chance
1311 * to free anything that cpu_exit couldn't
1312 * release while still running in process context.
1315 /* XXX what about process 'q' itself? zfree? */
1316 #if defined(__NetBSD__)
1317 pool_put(&proc_pool, q);
1322 if (q->p_stat == SSTOP && (q->p_flag & P_WAITED) == 0 &&
1323 (q->p_flag & P_TRACED ||
1324 (SCARG(uap, options) & (SVR4_WSTOPPED|SVR4_WCONTINUED)))) {
1325 DPRINTF(("jobcontrol %d\n", q->p_pid));
1326 if (((SCARG(uap, options) & SVR4_WNOWAIT)) == 0)
1327 q->p_flag |= P_WAITED;
1329 return svr4_setinfo(q, W_STOPCODE(q->p_xstat),
1337 if (SCARG(uap, options) & SVR4_WNOHANG) {
1339 if ((error = svr4_setinfo(NULL, 0, SCARG(uap, info))) != 0)
1344 if ((error = tsleep((caddr_t)p, PCATCH, "svr4_wait", 0)) != 0)
1351 bsd_statfs_to_svr4_statvfs(bfs, sfs)
1352 const struct statfs *bfs;
1353 struct svr4_statvfs *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 bsd_statfs_to_svr4_statvfs64(bfs, sfs)
1378 const struct statfs *bfs;
1379 struct svr4_statvfs64 *sfs;
1381 sfs->f_bsize = bfs->f_iosize; /* XXX */
1382 sfs->f_frsize = bfs->f_bsize;
1383 sfs->f_blocks = bfs->f_blocks;
1384 sfs->f_bfree = bfs->f_bfree;
1385 sfs->f_bavail = bfs->f_bavail;
1386 sfs->f_files = bfs->f_files;
1387 sfs->f_ffree = bfs->f_ffree;
1388 sfs->f_favail = bfs->f_ffree;
1389 sfs->f_fsid = bfs->f_fsid.val[0];
1390 memcpy(sfs->f_basetype, bfs->f_fstypename, sizeof(sfs->f_basetype));
1392 if (bfs->f_flags & MNT_RDONLY)
1393 sfs->f_flag |= SVR4_ST_RDONLY;
1394 if (bfs->f_flags & MNT_NOSUID)
1395 sfs->f_flag |= SVR4_ST_NOSUID;
1396 sfs->f_namemax = MAXNAMLEN;
1397 memcpy(sfs->f_fstr, bfs->f_fstypename, sizeof(sfs->f_fstr)); /* XXX */
1398 memset(sfs->f_filler, 0, sizeof(sfs->f_filler));
1403 svr4_sys_statvfs(struct svr4_sys_statvfs_args *uap)
1405 struct statfs_args fs_args;
1406 caddr_t sg = stackgap_init();
1407 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1409 struct svr4_statvfs sfs;
1412 CHECKALTEXIST(&sg, SCARG(uap, path));
1413 SCARG(&fs_args, path) = SCARG(uap, path);
1414 SCARG(&fs_args, buf) = fs;
1416 if ((error = statfs(&fs_args)) != 0)
1419 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1422 uap->sysmsg_result = fs_args.sysmsg_result;
1424 bsd_statfs_to_svr4_statvfs(&bfs, &sfs);
1426 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1431 svr4_sys_fstatvfs(struct svr4_sys_fstatvfs_args *uap)
1433 struct fstatfs_args fs_args;
1434 caddr_t sg = stackgap_init();
1435 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1437 struct svr4_statvfs sfs;
1440 SCARG(&fs_args, fd) = SCARG(uap, fd);
1441 SCARG(&fs_args, buf) = fs;
1443 if ((error = fstatfs(&fs_args)) != 0)
1446 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1449 uap->sysmsg_result = fs_args.sysmsg_result;
1451 bsd_statfs_to_svr4_statvfs(&bfs, &sfs);
1453 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1458 svr4_sys_statvfs64(struct svr4_sys_statvfs64_args *uap)
1460 struct statfs_args fs_args;
1461 caddr_t sg = stackgap_init();
1462 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1464 struct svr4_statvfs64 sfs;
1467 CHECKALTEXIST(&sg, SCARG(uap, path));
1468 SCARG(&fs_args, path) = SCARG(uap, path);
1469 SCARG(&fs_args, buf) = fs;
1471 if ((error = statfs(&fs_args)) != 0)
1474 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1477 uap->sysmsg_result = fs_args.sysmsg_result;
1479 bsd_statfs_to_svr4_statvfs64(&bfs, &sfs);
1481 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1486 svr4_sys_fstatvfs64(struct svr4_sys_fstatvfs64_args *uap)
1488 struct fstatfs_args fs_args;
1489 caddr_t sg = stackgap_init();
1490 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1492 struct svr4_statvfs64 sfs;
1495 SCARG(&fs_args, fd) = SCARG(uap, fd);
1496 SCARG(&fs_args, buf) = fs;
1498 if ((error = fstatfs(&fs_args)) != 0)
1501 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1504 uap->sysmsg_result = fs_args.sysmsg_result;
1506 bsd_statfs_to_svr4_statvfs64(&bfs, &sfs);
1508 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1512 svr4_sys_alarm(struct svr4_sys_alarm_args *uap)
1515 struct itimerval *itp, *oitp;
1516 struct setitimer_args sa;
1517 caddr_t sg = stackgap_init();
1519 itp = stackgap_alloc(&sg, sizeof(*itp));
1520 oitp = stackgap_alloc(&sg, sizeof(*oitp));
1521 timevalclear(&itp->it_interval);
1522 itp->it_value.tv_sec = SCARG(uap, sec);
1523 itp->it_value.tv_usec = 0;
1525 SCARG(&sa, which) = ITIMER_REAL;
1526 SCARG(&sa, itv) = itp;
1527 SCARG(&sa, oitv) = oitp;
1528 error = setitimer(&sa);
1531 if (oitp->it_value.tv_usec)
1532 oitp->it_value.tv_sec++;
1533 uap->sysmsg_result = oitp->it_value.tv_sec;
1539 svr4_sys_gettimeofday(struct svr4_sys_gettimeofday_args *uap)
1541 if (SCARG(uap, tp)) {
1545 return copyout(&atv, SCARG(uap, tp), sizeof (atv));
1552 svr4_sys_facl(struct svr4_sys_facl_args *uap)
1556 retval = &uap->sysmsg_result;
1559 switch (SCARG(uap, cmd)) {
1560 case SVR4_SYS_SETACL:
1561 /* We don't support acls on any filesystem */
1564 case SVR4_SYS_GETACL:
1565 return copyout(retval, &SCARG(uap, num),
1566 sizeof(SCARG(uap, num)));
1568 case SVR4_SYS_GETACLCNT:
1578 svr4_sys_acl(struct svr4_sys_acl_args *uap)
1580 /* XXX: for now the same */
1581 return svr4_sys_facl((struct svr4_sys_facl_args *)uap);
1585 svr4_sys_auditsys(struct svr4_sys_auditsys_args *uap)
1588 * XXX: Big brother is *not* watching.
1594 svr4_sys_memcntl(struct svr4_sys_memcntl_args *uap)
1596 switch (SCARG(uap, cmd)) {
1599 struct msync_args msa;
1602 SCARG(&msa, addr) = SCARG(uap, addr);
1603 SCARG(&msa, len) = SCARG(uap, len);
1604 SCARG(&msa, flags) = (int)SCARG(uap, arg);
1606 msa.sysmsg_result = 0;
1607 error = msync(&msa);
1608 uap->sysmsg_result = msa.sysmsg_result;
1611 case SVR4_MC_ADVISE:
1613 struct madvise_args maa;
1616 SCARG(&maa, addr) = SCARG(uap, addr);
1617 SCARG(&maa, len) = SCARG(uap, len);
1618 SCARG(&maa, behav) = (int)SCARG(uap, arg);
1620 maa.sysmsg_result = 0;
1621 error = madvise(&maa);
1622 uap->sysmsg_result = maa.sysmsg_result;
1626 case SVR4_MC_UNLOCK:
1627 case SVR4_MC_LOCKAS:
1628 case SVR4_MC_UNLOCKAS:
1637 svr4_sys_nice(struct svr4_sys_nice_args *uap)
1639 struct setpriority_args ap;
1642 SCARG(&ap, which) = PRIO_PROCESS;
1643 SCARG(&ap, who) = 0;
1644 SCARG(&ap, prio) = SCARG(uap, prio);
1646 if ((error = setpriority(&ap)) != 0)
1649 /* the cast is stupid, but the structures are the same */
1650 if ((error = getpriority((struct getpriority_args *)&ap)) != 0)
1653 uap->sysmsg_result = ap.sysmsg_result;
1659 svr4_sys_resolvepath(struct svr4_sys_resolvepath_args *uap)
1661 struct thread *td = curthread; /* XXX */
1662 struct proc *p = td->td_proc;
1663 struct nameidata nd;
1668 retval = &uap->sysmsg_result;
1670 NDINIT(&nd, LOOKUP, NOFOLLOW | SAVENAME, UIO_USERSPACE,
1671 SCARG(uap, path), td);
1673 if ((error = namei(&nd)) != 0)
1676 if ((error = copyout(nd.ni_cnd.cn_pnbuf, SCARG(uap, buf),
1677 SCARG(uap, bufsiz))) != 0)
1680 *retval = strlen(nd.ni_cnd.cn_pnbuf) < SCARG(uap, bufsiz) ?
1681 strlen(nd.ni_cnd.cn_pnbuf) + 1 : SCARG(uap, bufsiz);
1683 NDFREE(&nd, NDF_ONLY_PNBUF);