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.5 2003/06/26 05:55:19 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>
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 thread *td = curthread; /* XXX */
233 struct proc *p = td->td_proc;
236 caddr_t inp, buf; /* BSD-format */
237 int len, reclen; /* BSD-format */
238 caddr_t outp; /* SVR4-format */
239 int resid, svr4reclen=0; /* SVR4-format */
245 struct svr4_dirent64 svr4_dirent;
246 int buflen, error, eofflag, nbytes, justone;
247 u_long *cookies = NULL, *cookiep;
252 DPRINTF(("svr4_sys_getdents64(%d, *, %d)\n",
253 p->p_pid, SCARG(uap, fd), SCARG(uap, nbytes)));
254 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0) {
258 if ((fp->f_flag & FREAD) == 0)
261 vp = (struct vnode *) fp->f_data;
263 if (vp->v_type != VDIR)
266 if ((error = VOP_GETATTR(vp, &va, td))) {
270 nbytes = SCARG(uap, nbytes);
272 nbytes = sizeof (struct svr4_dirent64);
279 #define DIRBLKSIZ 512 /* XXX we used to use ufs's DIRBLKSIZ */
280 buflen = max(DIRBLKSIZ, nbytes);
281 buflen = min(buflen, MAXBSIZE);
282 buf = malloc(buflen, M_TEMP, M_WAITOK);
283 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
286 aiov.iov_len = buflen;
287 auio.uio_iov = &aiov;
289 auio.uio_rw = UIO_READ;
290 auio.uio_segflg = UIO_SYSSPACE;
292 auio.uio_resid = buflen;
293 auio.uio_offset = off;
296 free(cookies, M_TEMP);
300 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
301 &ncookies, &cookies);
307 outp = (caddr_t) SCARG(uap, dp);
309 if ((len = buflen - auio.uio_resid) <= 0) {
317 * When using cookies, the vfs has the option of reading from
318 * a different offset than that supplied (UFS truncates the
319 * offset to a block boundary to make sure that it never reads
320 * partway through a directory entry, even if the directory
321 * has been compacted).
323 while (len > 0 && ncookies > 0 && *cookiep <= off) {
324 bdp = (struct dirent *) inp;
325 len -= bdp->d_reclen;
326 inp += bdp->d_reclen;
333 if (cookiep && ncookies == 0)
335 bdp = (struct dirent *) inp;
336 reclen = bdp->d_reclen;
338 DPRINTF(("svr4_readdir: reclen=%d\n", reclen));
343 if (bdp->d_fileno == 0) {
353 svr4reclen = SVR4_RECLEN(&svr4_dirent, bdp->d_namlen);
354 if (reclen > len || resid < svr4reclen) {
358 svr4_dirent.d_ino = (long) bdp->d_fileno;
361 * old svr4-style readdir usage.
363 svr4_dirent.d_off = (svr4_off_t) svr4reclen;
364 svr4_dirent.d_reclen = (u_short) bdp->d_namlen;
366 svr4_dirent.d_off = (svr4_off_t)(off + reclen);
367 svr4_dirent.d_reclen = (u_short) svr4reclen;
369 strcpy(svr4_dirent.d_name, bdp->d_name);
370 if ((error = copyout((caddr_t)&svr4_dirent, outp, svr4reclen)))
385 if (outp == (caddr_t) SCARG(uap, dp))
390 nbytes = resid + svr4reclen;
393 p->p_retval[0] = nbytes - resid;
396 free(cookies, M_TEMP);
397 VOP_UNLOCK(vp, 0, td);
404 svr4_sys_getdents(struct svr4_sys_getdents_args *uap)
406 struct thread *td = curthread;
407 struct proc *p = td->td_proc;
410 caddr_t inp, buf; /* BSD-format */
411 int len, reclen; /* BSD-format */
412 caddr_t outp; /* SVR4-format */
413 int resid, svr4_reclen; /* SVR4-format */
417 struct svr4_dirent idb;
418 off_t off; /* true file offset */
419 int buflen, error, eofflag;
420 u_long *cookiebuf = NULL, *cookie;
421 int ncookies = 0, *retval = p->p_retval;
425 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
428 if ((fp->f_flag & FREAD) == 0)
431 vp = (struct vnode *)fp->f_data;
432 if (vp->v_type != VDIR)
435 buflen = min(MAXBSIZE, SCARG(uap, nbytes));
436 buf = malloc(buflen, M_TEMP, M_WAITOK);
437 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
441 aiov.iov_len = buflen;
442 auio.uio_iov = &aiov;
444 auio.uio_rw = UIO_READ;
445 auio.uio_segflg = UIO_SYSSPACE;
447 auio.uio_resid = buflen;
448 auio.uio_offset = off;
450 * First we read into the malloc'ed buffer, then
451 * we massage it into user space, one record at a time.
453 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies,
459 outp = SCARG(uap, buf);
460 resid = SCARG(uap, nbytes);
461 if ((len = buflen - auio.uio_resid) == 0)
464 for (cookie = cookiebuf; len > 0; len -= reclen) {
465 bdp = (struct dirent *)inp;
466 reclen = bdp->d_reclen;
468 panic("svr4_sys_getdents64: bad reclen");
469 off = *cookie++; /* each entry points to the next */
470 if ((off >> 32) != 0) {
471 uprintf("svr4_sys_getdents64: dir offset too large for emulated program");
475 if (bdp->d_fileno == 0) {
476 inp += reclen; /* it is a hole; squish it out */
479 svr4_reclen = SVR4_RECLEN(&idb, bdp->d_namlen);
480 if (reclen > len || resid < svr4_reclen) {
481 /* entry too big for buffer, so just stop */
486 * Massage in place to make a SVR4-shaped dirent (otherwise
487 * we have to worry about touching user memory outside of
488 * the copyout() call).
490 idb.d_ino = (svr4_ino_t)bdp->d_fileno;
491 idb.d_off = (svr4_off_t)off;
492 idb.d_reclen = (u_short)svr4_reclen;
493 strcpy(idb.d_name, bdp->d_name);
494 if ((error = copyout((caddr_t)&idb, outp, svr4_reclen)))
496 /* advance past this real entry */
498 /* advance output past SVR4-shaped entry */
500 resid -= svr4_reclen;
503 /* if we squished out the whole block, try again */
504 if (outp == SCARG(uap, buf))
506 fp->f_offset = off; /* update the vnode offset */
509 *retval = SCARG(uap, nbytes) - resid;
511 VOP_UNLOCK(vp, 0, td);
513 free(cookiebuf, M_TEMP);
520 svr4_sys_mmap(struct svr4_sys_mmap_args *uap)
522 struct proc *p = curproc;
526 retval = p->p_retval;
527 #define _MAP_NEW 0x80000000
529 * Verify the arguments.
531 if (SCARG(uap, prot) & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
532 return EINVAL; /* XXX still needed? */
534 if (SCARG(uap, len) == 0)
537 SCARG(&mm, prot) = SCARG(uap, prot);
538 SCARG(&mm, len) = SCARG(uap, len);
539 SCARG(&mm, flags) = SCARG(uap, flags) & ~_MAP_NEW;
540 SCARG(&mm, fd) = SCARG(uap, fd);
541 SCARG(&mm, addr) = SCARG(uap, addr);
542 SCARG(&mm, pos) = SCARG(uap, pos);
548 svr4_sys_mmap64(struct svr4_sys_mmap64_args *uap)
550 struct proc *p = curproc;
554 #define _MAP_NEW 0x80000000
556 * Verify the arguments.
558 if (SCARG(uap, prot) & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
559 return EINVAL; /* XXX still needed? */
561 if (SCARG(uap, len) == 0)
564 SCARG(&mm, prot) = SCARG(uap, prot);
565 SCARG(&mm, len) = SCARG(uap, len);
566 SCARG(&mm, flags) = SCARG(uap, flags) & ~_MAP_NEW;
567 SCARG(&mm, fd) = SCARG(uap, fd);
568 SCARG(&mm, addr) = SCARG(uap, addr);
569 SCARG(&mm, pos) = SCARG(uap, pos);
571 rp = (void *) round_page((vm_offset_t)(p->p_vmspace->vm_daddr + maxdsiz));
572 if ((SCARG(&mm, flags) & MAP_FIXED) == 0 &&
573 SCARG(&mm, addr) != 0 && (void *)SCARG(&mm, addr) < rp)
574 SCARG(&mm, addr) = rp;
581 svr4_sys_fchroot(struct svr4_sys_fchroot_args *uap)
583 struct thread *td = curthread; /* XXX */
584 struct proc *p = td->td_proc;
586 struct filedesc *fdp;
595 if ((error = suser(td)) != 0)
597 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
599 vp = (struct vnode *) fp->f_data;
600 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
601 if (vp->v_type != VDIR)
604 error = VOP_ACCESS(vp, VEXEC, cred, td);
605 VOP_UNLOCK(vp, 0, td);
609 if (fdp->fd_rdir != NULL)
617 svr4_mknod(retval, path, mode, dev)
623 caddr_t sg = stackgap_init();
625 CHECKALTEXIST(&sg, path);
627 if (S_ISFIFO(mode)) {
628 struct mkfifo_args ap;
629 SCARG(&ap, path) = path;
630 SCARG(&ap, mode) = mode;
633 struct mknod_args ap;
634 SCARG(&ap, path) = path;
635 SCARG(&ap, mode) = mode;
636 SCARG(&ap, dev) = dev;
643 svr4_sys_mknod(struct svr4_sys_mknod_args *uap)
645 struct thread *td = curthread; /* XXX */
646 struct proc *p = td->td_proc;
650 retval = p->p_retval;
652 return svr4_mknod(retval, SCARG(uap, path), SCARG(uap, mode),
653 (svr4_dev_t)svr4_to_bsd_odev_t(SCARG(uap, dev)));
658 svr4_sys_xmknod(struct svr4_sys_xmknod_args *uap)
660 struct thread *td = curthread; /* XXX */
661 struct proc *p = td->td_proc;
665 retval = p->p_retval;
667 return svr4_mknod(retval, SCARG(uap, path), SCARG(uap, mode),
668 (svr4_dev_t)svr4_to_bsd_dev_t(SCARG(uap, dev)));
673 svr4_sys_vhangup(struct svr4_sys_vhangup_args *uap)
680 svr4_sys_sysconfig(struct svr4_sys_sysconfig_args *uap)
682 struct thread *td = curthread; /* XXX */
683 struct proc *p = td->td_proc;
687 retval = &(p->p_retval[0]);
689 switch (SCARG(uap, name)) {
690 case SVR4_CONFIG_UNUSED:
693 case SVR4_CONFIG_NGROUPS:
694 *retval = NGROUPS_MAX;
696 case SVR4_CONFIG_CHILD_MAX:
699 case SVR4_CONFIG_OPEN_FILES:
702 case SVR4_CONFIG_POSIX_VER:
705 case SVR4_CONFIG_PAGESIZE:
708 case SVR4_CONFIG_CLK_TCK:
709 *retval = 60; /* should this be `hz', ie. 100? */
711 case SVR4_CONFIG_XOPEN_VER:
712 *retval = 2; /* XXX: What should that be? */
714 case SVR4_CONFIG_PROF_TCK:
715 *retval = 60; /* XXX: What should that be? */
717 case SVR4_CONFIG_NPROC_CONF:
718 *retval = 1; /* Only one processor for now */
720 case SVR4_CONFIG_NPROC_ONLN:
721 *retval = 1; /* And it better be online */
723 case SVR4_CONFIG_AIO_LISTIO_MAX:
724 case SVR4_CONFIG_AIO_MAX:
725 case SVR4_CONFIG_AIO_PRIO_DELTA_MAX:
726 *retval = 0; /* No aio support */
728 case SVR4_CONFIG_DELAYTIMER_MAX:
729 *retval = 0; /* No delaytimer support */
731 case SVR4_CONFIG_MQ_OPEN_MAX:
732 *retval = msginfo.msgmni;
734 case SVR4_CONFIG_MQ_PRIO_MAX:
735 *retval = 0; /* XXX: Don't know */
737 case SVR4_CONFIG_RTSIG_MAX:
740 case SVR4_CONFIG_SEM_NSEMS_MAX:
741 *retval = seminfo.semmni;
743 case SVR4_CONFIG_SEM_VALUE_MAX:
744 *retval = seminfo.semvmx;
746 case SVR4_CONFIG_SIGQUEUE_MAX:
747 *retval = 0; /* XXX: Don't know */
749 case SVR4_CONFIG_SIGRT_MIN:
750 case SVR4_CONFIG_SIGRT_MAX:
751 *retval = 0; /* No real time signals */
753 case SVR4_CONFIG_TIMER_MAX:
754 *retval = 3; /* XXX: real, virtual, profiling */
757 case SVR4_CONFIG_PHYS_PAGES:
759 *retval = uvmexp.free; /* XXX: free instead of total */
761 *retval = cnt.v_free_count; /* XXX: free instead of total */
764 case SVR4_CONFIG_AVPHYS_PAGES:
766 *retval = uvmexp.active; /* XXX: active instead of avg */
768 *retval = cnt.v_active_count; /* XXX: active instead of avg */
779 extern int swap_pager_full;
783 svr4_sys_break(struct svr4_sys_break_args *uap)
785 struct thread *td = curthread; /* XXX */
786 struct proc *p = td->td_proc;
788 vm_offset_t new, old, base, ns;
794 base = round_page((vm_offset_t) vm->vm_daddr);
795 ns = (vm_offset_t)SCARG(uap, nsize);
796 new = round_page(ns);
798 if ((new - base) > (unsigned) p->p_rlimit[RLIMIT_DATA].rlim_cur) {
801 if (new >= VM_MAXUSER_ADDRESS) {
804 } else if (new < base) {
806 * This is simply an invalid value. If someone wants to
807 * do fancy address space manipulations, mmap and munmap
808 * can do most of what the user would want.
813 old = base + ctob(vm->vm_dsize);
818 if (vm->vm_map.size + diff > p->p_rlimit[RLIMIT_VMEM].rlim_cur)
820 rv = vm_map_find(&vm->vm_map, NULL, 0, &old, diff, FALSE,
821 VM_PROT_ALL, VM_PROT_ALL, 0);
822 if (rv != KERN_SUCCESS) {
825 vm->vm_dsize += btoc(diff);
826 } else if (new < old) {
827 rv = vm_map_remove(&vm->vm_map, new, old);
828 if (rv != KERN_SUCCESS) {
831 vm->vm_dsize -= btoc(old - new);
837 static __inline clock_t
838 timeval_to_clock_t(tv)
841 return tv->tv_sec * hz + tv->tv_usec / (1000000 / hz);
846 svr4_sys_times(struct svr4_sys_times_args *uap)
848 struct proc *p = curproc;
849 int error, *retval = p->p_retval;
854 struct getrusage_args ga;
856 caddr_t sg = stackgap_init();
857 ru = stackgap_alloc(&sg, sizeof(struct rusage));
859 SCARG(&ga, who) = RUSAGE_SELF;
860 SCARG(&ga, rusage) = ru;
862 error = getrusage(&ga);
866 if ((error = copyin(ru, &r, sizeof r)) != 0)
869 tms.tms_utime = timeval_to_clock_t(&r.ru_utime);
870 tms.tms_stime = timeval_to_clock_t(&r.ru_stime);
872 SCARG(&ga, who) = RUSAGE_CHILDREN;
873 error = getrusage(&ga);
877 if ((error = copyin(ru, &r, sizeof r)) != 0)
880 tms.tms_cutime = timeval_to_clock_t(&r.ru_utime);
881 tms.tms_cstime = timeval_to_clock_t(&r.ru_stime);
884 *retval = timeval_to_clock_t(&t);
886 return copyout(&tms, SCARG(uap, tp), sizeof(tms));
891 svr4_sys_ulimit(struct svr4_sys_ulimit_args *uap)
893 struct proc *p = curproc;
894 int *retval = p->p_retval;
896 switch (SCARG(uap, cmd)) {
898 *retval = p->p_rlimit[RLIMIT_FSIZE].rlim_cur / 512;
900 *retval = 0x7fffffff;
906 struct __setrlimit_args srl;
908 caddr_t sg = stackgap_init();
909 struct rlimit *url = (struct rlimit *)
910 stackgap_alloc(&sg, sizeof *url);
912 krl.rlim_cur = SCARG(uap, newlimit) * 512;
913 krl.rlim_max = p->p_rlimit[RLIMIT_FSIZE].rlim_max;
915 error = copyout(&krl, url, sizeof(*url));
919 SCARG(&srl, which) = RLIMIT_FSIZE;
920 SCARG(&srl, rlp) = (struct rlimit *)url;
922 error = setrlimit(&srl);
926 *retval = p->p_rlimit[RLIMIT_FSIZE].rlim_cur;
928 *retval = 0x7fffffff;
934 struct vmspace *vm = p->p_vmspace;
935 register_t r = p->p_rlimit[RLIMIT_DATA].rlim_cur;
939 r += (long) vm->vm_daddr;
947 *retval = p->p_rlimit[RLIMIT_NOFILE].rlim_cur;
949 *retval = 0x7fffffff;
963 /* look in the live processes */
964 if ((p = pfind(pid)) != NULL)
967 /* look in the zombies */
968 for (p = zombproc.lh_first; p != 0; p = p->p_list.le_next)
977 svr4_sys_pgrpsys(struct svr4_sys_pgrpsys_args *uap)
979 struct proc *p = curproc;
980 int *retval = p->p_retval;
982 switch (SCARG(uap, cmd)) {
983 case 1: /* setpgrp() */
985 * SVR4 setpgrp() (which takes no arguments) has the
986 * semantics that the session ID is also created anew, so
987 * in almost every sense, setpgrp() is identical to
988 * setsid() for SVR4. (Under BSD, the difference is that
989 * a setpgid(0,0) will not create a new session.)
994 case 0: /* getpgrp() */
995 *retval = p->p_pgrp->pg_id;
998 case 2: /* getsid(pid) */
999 if (SCARG(uap, pid) != 0 &&
1000 (p = svr4_pfind(SCARG(uap, pid))) == NULL)
1003 * This has already been initialized to the pid of
1004 * the session leader.
1006 *retval = (register_t) p->p_session->s_leader->p_pid;
1009 case 3: /* setsid() */
1010 return setsid(NULL);
1012 case 4: /* getpgid(pid) */
1014 if (SCARG(uap, pid) != 0 &&
1015 (p = svr4_pfind(SCARG(uap, pid))) == NULL)
1018 *retval = (int) p->p_pgrp->pg_id;
1021 case 5: /* setpgid(pid, pgid); */
1023 struct setpgid_args sa;
1025 SCARG(&sa, pid) = SCARG(uap, pid);
1026 SCARG(&sa, pgid) = SCARG(uap, pgid);
1027 return setpgid(&sa);
1035 #define syscallarg(x) union { x datum; register_t pad; }
1037 struct svr4_hrtcntl_args {
1041 svr4_hrt_interval_t * iv;
1042 svr4_hrt_time_t * ti;
1047 svr4_hrtcntl(struct svr4_hrtcntl_args *uap, register_t *retval)
1049 switch (SCARG(uap, fun)) {
1050 case SVR4_HRT_CNTL_RES:
1051 DPRINTF(("htrcntl(RES)\n"));
1052 *retval = SVR4_HRT_USEC;
1055 case SVR4_HRT_CNTL_TOFD:
1056 DPRINTF(("htrcntl(TOFD)\n"));
1060 if (SCARG(uap, clk) != SVR4_HRT_CLK_STD) {
1061 DPRINTF(("clk == %d\n", SCARG(uap, clk)));
1064 if (SCARG(uap, ti) == NULL) {
1065 DPRINTF(("ti NULL\n"));
1069 t.h_sec = tv.tv_sec;
1070 t.h_rem = tv.tv_usec;
1071 t.h_res = SVR4_HRT_USEC;
1072 return copyout(&t, SCARG(uap, ti), sizeof(t));
1075 case SVR4_HRT_CNTL_START:
1076 DPRINTF(("htrcntl(START)\n"));
1079 case SVR4_HRT_CNTL_GET:
1080 DPRINTF(("htrcntl(GET)\n"));
1083 DPRINTF(("Bad htrcntl command %d\n", SCARG(uap, fun)));
1090 svr4_sys_hrtsys(struct svr4_sys_hrtsys_args *uap)
1092 struct proc *p = curproc;
1093 int *retval = p->p_retval;
1095 switch (SCARG(uap, cmd)) {
1097 return svr4_hrtcntl((struct svr4_hrtcntl_args *) uap, retval);
1100 DPRINTF(("hrtalarm\n"));
1104 DPRINTF(("hrtsleep\n"));
1108 DPRINTF(("hrtcancel\n"));
1112 DPRINTF(("Bad hrtsys command %d\n", SCARG(uap, cmd)));
1119 svr4_setinfo(p, st, s)
1127 memset(&i, 0, sizeof(i));
1129 i.si_signo = SVR4_SIGCHLD;
1130 i.si_errno = 0; /* XXX? */
1133 i.si_pid = p->p_pid;
1134 if (p->p_stat == SZOMB) {
1135 i.si_stime = p->p_ru->ru_stime.tv_sec;
1136 i.si_utime = p->p_ru->ru_utime.tv_sec;
1139 i.si_stime = p->p_stats->p_ru.ru_stime.tv_sec;
1140 i.si_utime = p->p_stats->p_ru.ru_utime.tv_sec;
1144 if (WIFEXITED(st)) {
1145 i.si_status = WEXITSTATUS(st);
1146 i.si_code = SVR4_CLD_EXITED;
1147 } else if (WIFSTOPPED(st)) {
1149 if (sig >= 0 && sig < NSIG)
1150 i.si_status = SVR4_BSD2SVR4_SIG(sig);
1152 if (i.si_status == SVR4_SIGCONT)
1153 i.si_code = SVR4_CLD_CONTINUED;
1155 i.si_code = SVR4_CLD_STOPPED;
1158 if (sig >= 0 && sig < NSIG)
1159 i.si_status = SVR4_BSD2SVR4_SIG(sig);
1162 i.si_code = SVR4_CLD_DUMPED;
1164 i.si_code = SVR4_CLD_KILLED;
1167 DPRINTF(("siginfo [pid %ld signo %d code %d errno %d status %d]\n",
1168 i.si_pid, i.si_signo, i.si_code, i.si_errno, i.si_status));
1170 return copyout(&i, s, sizeof(i));
1175 svr4_sys_waitsys(struct svr4_sys_waitsys_args *uap)
1177 struct proc *p = curproc;
1179 int error, *retval = p->p_retval;
1183 switch (SCARG(uap, grp)) {
1188 SCARG(uap, id) = -p->p_pgid;
1192 SCARG(uap, id) = WAIT_ANY;
1199 DPRINTF(("waitsys(%d, %d, %p, %x)\n",
1200 SCARG(uap, grp), SCARG(uap, id),
1201 SCARG(uap, info), SCARG(uap, options)));
1205 for (q = p->p_children.lh_first; q != 0; q = q->p_sibling.le_next) {
1206 if (SCARG(uap, id) != WAIT_ANY &&
1207 q->p_pid != SCARG(uap, id) &&
1208 q->p_pgid != -SCARG(uap, id)) {
1209 DPRINTF(("pid %d pgid %d != %d\n", q->p_pid,
1210 q->p_pgid, SCARG(uap, id)));
1214 if (q->p_stat == SZOMB &&
1215 ((SCARG(uap, options) & (SVR4_WEXITED|SVR4_WTRAPPED)))) {
1217 DPRINTF(("found %d\n", q->p_pid));
1218 if ((error = svr4_setinfo(q, q->p_xstat,
1219 SCARG(uap, info))) != 0)
1223 if ((SCARG(uap, options) & SVR4_WNOWAIT)) {
1224 DPRINTF(("Don't wait\n"));
1229 * If we got the child via ptrace(2) or procfs, and
1230 * the parent is different (meaning the process was
1231 * attached, rather than run as a child), then we need
1232 * to give it back to the ol dparent, and send the
1233 * parent a SIGCHLD. The rest of the cleanup will be
1234 * done when the old parent waits on the child.
1236 if ((q->p_flag & P_TRACED) &&
1237 q->p_oppid != q->p_pptr->p_pid) {
1238 t = pfind(q->p_oppid);
1239 proc_reparent(q, t ? t : initproc);
1241 q->p_flag &= ~(P_TRACED | P_WAITED);
1242 wakeup((caddr_t)q->p_pptr);
1246 ruadd(&p->p_stats->p_cru, q->p_ru);
1248 FREE(q->p_ru, M_ZOMBIE);
1251 * Finally finished with old proc entry.
1252 * Unlink it from its process group and free it.
1256 LIST_REMOVE(q, p_list); /* off zombproc */
1258 LIST_REMOVE(q, p_sibling);
1261 * Decrement the count of procs running with this uid.
1263 (void)chgproccnt(q->p_ucred->cr_uidinfo, -1, 0);
1266 * Free up credentials.
1272 * Release reference to text vnode
1278 * Give machine-dependent layer a chance
1279 * to free anything that cpu_exit couldn't
1280 * release while still running in process context.
1283 /* XXX what about process 'q' itself? zfree? */
1284 #if defined(__NetBSD__)
1285 pool_put(&proc_pool, q);
1290 if (q->p_stat == SSTOP && (q->p_flag & P_WAITED) == 0 &&
1291 (q->p_flag & P_TRACED ||
1292 (SCARG(uap, options) & (SVR4_WSTOPPED|SVR4_WCONTINUED)))) {
1293 DPRINTF(("jobcontrol %d\n", q->p_pid));
1294 if (((SCARG(uap, options) & SVR4_WNOWAIT)) == 0)
1295 q->p_flag |= P_WAITED;
1297 return svr4_setinfo(q, W_STOPCODE(q->p_xstat),
1305 if (SCARG(uap, options) & SVR4_WNOHANG) {
1307 if ((error = svr4_setinfo(NULL, 0, SCARG(uap, info))) != 0)
1312 if ((error = tsleep((caddr_t)p, PWAIT | PCATCH, "svr4_wait", 0)) != 0)
1319 bsd_statfs_to_svr4_statvfs(bfs, sfs)
1320 const struct statfs *bfs;
1321 struct svr4_statvfs *sfs;
1323 sfs->f_bsize = bfs->f_iosize; /* XXX */
1324 sfs->f_frsize = bfs->f_bsize;
1325 sfs->f_blocks = bfs->f_blocks;
1326 sfs->f_bfree = bfs->f_bfree;
1327 sfs->f_bavail = bfs->f_bavail;
1328 sfs->f_files = bfs->f_files;
1329 sfs->f_ffree = bfs->f_ffree;
1330 sfs->f_favail = bfs->f_ffree;
1331 sfs->f_fsid = bfs->f_fsid.val[0];
1332 memcpy(sfs->f_basetype, bfs->f_fstypename, sizeof(sfs->f_basetype));
1334 if (bfs->f_flags & MNT_RDONLY)
1335 sfs->f_flag |= SVR4_ST_RDONLY;
1336 if (bfs->f_flags & MNT_NOSUID)
1337 sfs->f_flag |= SVR4_ST_NOSUID;
1338 sfs->f_namemax = MAXNAMLEN;
1339 memcpy(sfs->f_fstr, bfs->f_fstypename, sizeof(sfs->f_fstr)); /* XXX */
1340 memset(sfs->f_filler, 0, sizeof(sfs->f_filler));
1345 bsd_statfs_to_svr4_statvfs64(bfs, sfs)
1346 const struct statfs *bfs;
1347 struct svr4_statvfs64 *sfs;
1349 sfs->f_bsize = bfs->f_iosize; /* XXX */
1350 sfs->f_frsize = bfs->f_bsize;
1351 sfs->f_blocks = bfs->f_blocks;
1352 sfs->f_bfree = bfs->f_bfree;
1353 sfs->f_bavail = bfs->f_bavail;
1354 sfs->f_files = bfs->f_files;
1355 sfs->f_ffree = bfs->f_ffree;
1356 sfs->f_favail = bfs->f_ffree;
1357 sfs->f_fsid = bfs->f_fsid.val[0];
1358 memcpy(sfs->f_basetype, bfs->f_fstypename, sizeof(sfs->f_basetype));
1360 if (bfs->f_flags & MNT_RDONLY)
1361 sfs->f_flag |= SVR4_ST_RDONLY;
1362 if (bfs->f_flags & MNT_NOSUID)
1363 sfs->f_flag |= SVR4_ST_NOSUID;
1364 sfs->f_namemax = MAXNAMLEN;
1365 memcpy(sfs->f_fstr, bfs->f_fstypename, sizeof(sfs->f_fstr)); /* XXX */
1366 memset(sfs->f_filler, 0, sizeof(sfs->f_filler));
1371 svr4_sys_statvfs(struct svr4_sys_statvfs_args *uap)
1373 struct statfs_args fs_args;
1374 caddr_t sg = stackgap_init();
1375 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1377 struct svr4_statvfs sfs;
1380 CHECKALTEXIST(&sg, SCARG(uap, path));
1381 SCARG(&fs_args, path) = SCARG(uap, path);
1382 SCARG(&fs_args, buf) = fs;
1384 if ((error = statfs(&fs_args)) != 0)
1387 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1390 bsd_statfs_to_svr4_statvfs(&bfs, &sfs);
1392 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1397 svr4_sys_fstatvfs(struct svr4_sys_fstatvfs_args *uap)
1399 struct fstatfs_args fs_args;
1400 caddr_t sg = stackgap_init();
1401 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1403 struct svr4_statvfs sfs;
1406 SCARG(&fs_args, fd) = SCARG(uap, fd);
1407 SCARG(&fs_args, buf) = fs;
1409 if ((error = fstatfs(&fs_args)) != 0)
1412 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1415 bsd_statfs_to_svr4_statvfs(&bfs, &sfs);
1417 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1422 svr4_sys_statvfs64(struct svr4_sys_statvfs64_args *uap)
1424 struct statfs_args fs_args;
1425 caddr_t sg = stackgap_init();
1426 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1428 struct svr4_statvfs64 sfs;
1431 CHECKALTEXIST(&sg, SCARG(uap, path));
1432 SCARG(&fs_args, path) = SCARG(uap, path);
1433 SCARG(&fs_args, buf) = fs;
1435 if ((error = statfs(&fs_args)) != 0)
1438 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1441 bsd_statfs_to_svr4_statvfs64(&bfs, &sfs);
1443 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1448 svr4_sys_fstatvfs64(struct svr4_sys_fstatvfs64_args *uap)
1450 struct fstatfs_args fs_args;
1451 caddr_t sg = stackgap_init();
1452 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1454 struct svr4_statvfs64 sfs;
1457 SCARG(&fs_args, fd) = SCARG(uap, fd);
1458 SCARG(&fs_args, buf) = fs;
1460 if ((error = fstatfs(&fs_args)) != 0)
1463 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1466 bsd_statfs_to_svr4_statvfs64(&bfs, &sfs);
1468 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1472 svr4_sys_alarm(struct svr4_sys_alarm_args *uap)
1474 struct proc *p = curproc;
1476 struct itimerval *itp, *oitp;
1477 struct setitimer_args sa;
1478 caddr_t sg = stackgap_init();
1480 itp = stackgap_alloc(&sg, sizeof(*itp));
1481 oitp = stackgap_alloc(&sg, sizeof(*oitp));
1482 timevalclear(&itp->it_interval);
1483 itp->it_value.tv_sec = SCARG(uap, sec);
1484 itp->it_value.tv_usec = 0;
1486 SCARG(&sa, which) = ITIMER_REAL;
1487 SCARG(&sa, itv) = itp;
1488 SCARG(&sa, oitv) = oitp;
1489 error = setitimer(&sa);
1492 if (oitp->it_value.tv_usec)
1493 oitp->it_value.tv_sec++;
1494 p->p_retval[0] = oitp->it_value.tv_sec;
1500 svr4_sys_gettimeofday(struct svr4_sys_gettimeofday_args *uap)
1502 if (SCARG(uap, tp)) {
1506 return copyout(&atv, SCARG(uap, tp), sizeof (atv));
1513 svr4_sys_facl(struct svr4_sys_facl_args *uap)
1515 struct proc *p = curproc;
1518 retval = p->p_retval;
1521 switch (SCARG(uap, cmd)) {
1522 case SVR4_SYS_SETACL:
1523 /* We don't support acls on any filesystem */
1526 case SVR4_SYS_GETACL:
1527 return copyout(retval, &SCARG(uap, num),
1528 sizeof(SCARG(uap, num)));
1530 case SVR4_SYS_GETACLCNT:
1540 svr4_sys_acl(struct svr4_sys_acl_args *uap)
1542 /* XXX: for now the same */
1543 return svr4_sys_facl((struct svr4_sys_facl_args *)uap);
1547 svr4_sys_auditsys(struct svr4_sys_auditsys_args *uap)
1550 * XXX: Big brother is *not* watching.
1556 svr4_sys_memcntl(struct svr4_sys_memcntl_args *uap)
1558 switch (SCARG(uap, cmd)) {
1561 struct msync_args msa;
1563 SCARG(&msa, addr) = SCARG(uap, addr);
1564 SCARG(&msa, len) = SCARG(uap, len);
1565 SCARG(&msa, flags) = (int)SCARG(uap, arg);
1569 case SVR4_MC_ADVISE:
1571 struct madvise_args maa;
1573 SCARG(&maa, addr) = SCARG(uap, addr);
1574 SCARG(&maa, len) = SCARG(uap, len);
1575 SCARG(&maa, behav) = (int)SCARG(uap, arg);
1577 return madvise(&maa);
1580 case SVR4_MC_UNLOCK:
1581 case SVR4_MC_LOCKAS:
1582 case SVR4_MC_UNLOCKAS:
1591 svr4_sys_nice(struct svr4_sys_nice_args *uap)
1593 struct setpriority_args ap;
1596 SCARG(&ap, which) = PRIO_PROCESS;
1597 SCARG(&ap, who) = 0;
1598 SCARG(&ap, prio) = SCARG(uap, prio);
1600 if ((error = setpriority(&ap)) != 0)
1603 /* the cast is stupid, but the structures are the same */
1604 if ((error = getpriority((struct getpriority_args *)&ap)) != 0)
1611 svr4_sys_resolvepath(struct svr4_sys_resolvepath_args *uap)
1613 struct thread *td = curthread; /* XXX */
1614 struct proc *p = td->td_proc;
1615 struct nameidata nd;
1620 retval = p->p_retval;
1622 NDINIT(&nd, LOOKUP, NOFOLLOW | SAVENAME, UIO_USERSPACE,
1623 SCARG(uap, path), td);
1625 if ((error = namei(&nd)) != 0)
1628 if ((error = copyout(nd.ni_cnd.cn_pnbuf, SCARG(uap, buf),
1629 SCARG(uap, bufsiz))) != 0)
1632 *retval = strlen(nd.ni_cnd.cn_pnbuf) < SCARG(uap, bufsiz) ?
1633 strlen(nd.ni_cnd.cn_pnbuf) + 1 : SCARG(uap, bufsiz);
1635 NDFREE(&nd, NDF_ONLY_PNBUF);