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.
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>
52 #include <sys/mount.h>
53 #include <sys/resource.h>
54 #include <sys/resourcevar.h>
55 #include <sys/socket.h>
56 #include <sys/vnode.h>
59 #include <sys/utsname.h>
60 #include <sys/unistd.h>
61 #include <sys/times.h>
62 #include <sys/fcntl.h>
65 #include <sys/ptrace.h>
66 #include <sys/signalvar.h>
67 #include <ufs/ufs/dir.h>
68 #include <vm/vm_zone.h>
70 #include <sys/sysproto.h>
72 #include <netinet/in.h>
74 #include <svr4/svr4.h>
75 #include <svr4/svr4_types.h>
76 #include <svr4/svr4_signal.h>
77 #include <svr4/svr4_proto.h>
78 #include <svr4/svr4_util.h>
79 #include <svr4/svr4_sysconfig.h>
80 #include <svr4/svr4_dirent.h>
81 #include <svr4/svr4_acl.h>
82 #include <svr4/svr4_time.h>
83 #include <svr4/svr4_ulimit.h>
84 #include <svr4/svr4_statvfs.h>
85 #include <svr4/svr4_hrt.h>
86 #include <svr4/svr4_mman.h>
87 #include <svr4/svr4_wait.h>
89 #include <machine/vmparam.h>
91 #include <vm/vm_param.h>
92 #include <vm/vm_map.h>
93 #include <vm/vm_prot.h>
97 # include <uvm/uvm_extern.h>
101 #define BSD_DIRENT(cp) ((struct dirent *)(cp))
103 extern int bsd_to_svr4_sig[];
104 static int svr4_mknod __P((struct proc *, register_t *, char *,
105 svr4_mode_t, svr4_dev_t));
107 static __inline clock_t timeval_to_clock_t __P((struct timeval *));
108 static int svr4_setinfo __P((struct proc *, int, svr4_siginfo_t *));
110 struct svr4_hrtcntl_args;
111 static int svr4_hrtcntl __P((struct proc *, struct svr4_hrtcntl_args *,
113 static void bsd_statfs_to_svr4_statvfs __P((const struct statfs *,
114 struct svr4_statvfs *));
115 static void bsd_statfs_to_svr4_statvfs64 __P((const struct statfs *,
116 struct svr4_statvfs64 *));
117 static struct proc *svr4_pfind __P((pid_t pid));
122 svr4_sys_setitimer(p, uap)
123 register struct proc *p;
124 struct svr4_sys_setitimer_args *uap;
132 svr4_sys_wait(p, uap)
134 struct svr4_sys_wait_args *uap;
137 int error, *retval = p->p_retval, st, sig;
138 size_t sz = sizeof(*SCARG(&w4, status));
140 SCARG(&w4, rusage) = NULL;
141 SCARG(&w4, options) = 0;
143 if (SCARG(uap, status) == NULL) {
144 caddr_t sg = stackgap_init();
146 SCARG(&w4, status) = stackgap_alloc(&sg, sz);
149 SCARG(&w4, status) = SCARG(uap, status);
151 SCARG(&w4, pid) = WAIT_ANY;
153 if ((error = wait4(p, &w4)) != 0)
156 if ((error = copyin(SCARG(&w4, status), &st, sizeof(st))) != 0)
159 if (WIFSIGNALED(st)) {
161 if (sig >= 0 && sig < NSIG)
162 st = (st & ~0177) | bsd_to_svr4_sig[sig];
163 } else if (WIFSTOPPED(st)) {
165 if (sig >= 0 && sig < NSIG)
166 st = (st & ~0xff00) | (bsd_to_svr4_sig[sig] << 8);
170 * It looks like wait(2) on svr4/solaris/2.4 returns
171 * the status in retval[1], and the pid on retval[0].
175 if (SCARG(uap, status))
176 if ((error = copyout(&st, SCARG(uap, status), sizeof(st))) != 0)
183 svr4_sys_execv(p, uap)
185 struct svr4_sys_execv_args *uap;
187 struct execve_args ap;
190 sg = stackgap_init();
191 CHECKALTEXIST(p, &sg, SCARG(uap, path));
193 SCARG(&ap, fname) = SCARG(uap, path);
194 SCARG(&ap, argv) = SCARG(uap, argp);
195 SCARG(&ap, envv) = NULL;
197 return execve(p, &ap);
201 svr4_sys_execve(p, uap)
203 struct svr4_sys_execve_args *uap;
205 struct execve_args ap;
208 sg = stackgap_init();
209 CHECKALTEXIST(p, &sg, uap->path);
211 SCARG(&ap, fname) = SCARG(uap, path);
212 SCARG(&ap, argv) = SCARG(uap, argp);
213 SCARG(&ap, envv) = SCARG(uap, envp);
215 return execve(p, &ap);
221 struct svr4_sys_time_args *v;
223 struct svr4_sys_time_args *uap = v;
229 error = copyout(&tv.tv_sec, SCARG(uap, t),
230 sizeof(*(SCARG(uap, t))));
231 p->p_retval[0] = (int) tv.tv_sec;
238 * Read SVR4-style directory entries. We suck them into kernel space so
239 * that they can be massaged before being copied out to user code. Like
240 * SunOS, we squish out `empty' entries.
242 * This is quite ugly, but what do you expect from compatibility code?
246 svr4_sys_getdents64(p, uap)
248 struct svr4_sys_getdents64_args *uap;
252 caddr_t inp, buf; /* BSD-format */
253 int len, reclen; /* BSD-format */
254 caddr_t outp; /* SVR4-format */
255 int resid, svr4_reclen; /* SVR4-format */
259 struct svr4_dirent64 idb;
260 off_t off; /* true file offset */
261 int buflen, error, eofflag;
262 u_long *cookiebuf = NULL, *cookie;
263 int ncookies = 0, retval = 0, offcnt = 0;
265 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
268 if ((fp->f_flag & FREAD) == 0)
271 vp = (struct vnode *)fp->f_data;
272 if (vp->v_type != VDIR)
275 buflen = min(MAXBSIZE, SCARG(uap, nbytes));
276 DPRINTF(("buflen = %d, spec = %d\n", buflen, SCARG(uap, nbytes)));
277 buf = malloc(buflen, M_TEMP, M_WAITOK);
278 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
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;
290 DPRINTF((">>> off = %d\n", off));
293 * First we read into the malloc'ed buffer, then
294 * we massage it into user space, one record at a time.
296 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies,
302 outp = (caddr_t)SCARG(uap, dp);
303 resid = SCARG(uap, nbytes);
304 if ((len = buflen - auio.uio_resid) == 0)
307 for (cookie = cookiebuf; len > 0; len -= reclen) {
308 bdp = (struct dirent *)inp;
309 reclen = bdp->d_reclen;
311 panic("svr4_getdents64: bad reclen");
312 if (bdp->d_fileno == 0) {
313 inp += reclen; /* it is a hole; squish it out */
319 DPRINTF(("+++ off = %d\n", off));
322 svr4_reclen = SVR4_RECLEN(&idb, (bdp->d_namlen));
324 if (reclen > len || resid < svr4_reclen) {
325 /* entry too big for buffer, so just stop */
327 DPRINTF(("+++ off = %d\n", off));
331 off = *cookie++; /* each entry points to the next */
335 DPRINTF(("+++ off = %d\n", off));
338 * Massage in place to make a SVR4-shaped dirent (otherwise
339 * we have to worry about touching user memory outside of
340 * the copyout() call).
342 idb.d_ino = (svr4_ino64_t)bdp->d_fileno;
343 idb.d_off = (svr4_off64_t)off;
344 idb.d_reclen = (u_short)svr4_reclen;
345 strcpy(idb.d_name, bdp->d_name);
346 if ((error = copyout((caddr_t)&idb, outp, svr4_reclen)))
348 DPRINTF(("d_ino = %d\nd_off = %d\nd_reclen = %d\n",
349 idb.d_ino, idb.d_off, (u_short)idb.d_reclen));
350 DPRINTF(("d_name = %s\n", idb.d_name));
351 DPRINTF(("(bdp->d_type = %d, reclen = %d, bdp->d_namelen = %d)\n", bdp->d_type, svr4_reclen, bdp->d_namlen));
352 /* advance past this real entry */
354 /* advance output past SVR4-shaped entry */
356 resid -= svr4_reclen;
357 offcnt += svr4_reclen;
359 DPRINTF(("block finished\n"));
360 /* if we squished out the whole block, try again */
361 fp->f_offset = off; /* update the vnode offset */
362 if (outp == (caddr_t)SCARG(uap, dp))
366 retval = offcnt; /* SCARG(uap, nbytes) - resid;*/
368 VOP_UNLOCK(vp, 0, p);
370 free(cookiebuf, M_TEMP);
372 DPRINTF(("\t\treturning %d\n", retval));
373 p->p_retval[0] = retval;
379 svr4_sys_getdents(p, uap)
381 struct svr4_sys_getdents_args *uap;
385 caddr_t inp, buf; /* BSD-format */
386 int len, reclen; /* BSD-format */
387 caddr_t outp; /* SVR4-format */
388 int resid, svr4_reclen; /* SVR4-format */
392 struct svr4_dirent idb;
393 off_t off; /* true file offset */
394 int buflen, error, eofflag;
395 u_long *cookiebuf = NULL, *cookie;
396 int ncookies = 0, *retval = p->p_retval;
398 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
401 if ((fp->f_flag & FREAD) == 0)
404 vp = (struct vnode *)fp->f_data;
405 if (vp->v_type != VDIR)
408 buflen = min(MAXBSIZE, SCARG(uap, nbytes));
409 buf = malloc(buflen, M_TEMP, M_WAITOK);
410 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
414 aiov.iov_len = buflen;
415 auio.uio_iov = &aiov;
417 auio.uio_rw = UIO_READ;
418 auio.uio_segflg = UIO_SYSSPACE;
420 auio.uio_resid = buflen;
421 auio.uio_offset = off;
423 * First we read into the malloc'ed buffer, then
424 * we massage it into user space, one record at a time.
426 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies,
432 outp = SCARG(uap, buf);
433 resid = SCARG(uap, nbytes);
434 if ((len = buflen - auio.uio_resid) == 0)
437 for (cookie = cookiebuf; len > 0; len -= reclen) {
438 bdp = (struct dirent *)inp;
439 reclen = bdp->d_reclen;
441 panic("svr4_getdents: bad reclen");
442 off = *cookie++; /* each entry points to the next */
443 if ((off >> 32) != 0) {
444 uprintf("svr4_getdents: dir offset too large for emulated program");
448 if (bdp->d_fileno == 0) {
449 inp += reclen; /* it is a hole; squish it out */
452 svr4_reclen = SVR4_RECLEN(&idb, bdp->d_namlen);
453 if (reclen > len || resid < svr4_reclen) {
454 /* entry too big for buffer, so just stop */
459 * Massage in place to make a SVR4-shaped dirent (otherwise
460 * we have to worry about touching user memory outside of
461 * the copyout() call).
463 idb.d_ino = (svr4_ino_t)bdp->d_fileno;
464 idb.d_off = (svr4_off_t)off;
465 idb.d_reclen = (u_short)svr4_reclen;
466 strcpy(idb.d_name, bdp->d_name);
467 if ((error = copyout((caddr_t)&idb, outp, svr4_reclen)))
469 /* advance past this real entry */
471 /* advance output past SVR4-shaped entry */
473 resid -= svr4_reclen;
476 /* if we squished out the whole block, try again */
477 if (outp == SCARG(uap, buf))
479 fp->f_offset = off; /* update the vnode offset */
482 *retval = SCARG(uap, nbytes) - resid;
484 VOP_UNLOCK(vp, 0, p);
486 free(cookiebuf, M_TEMP);
493 svr4_sys_mmap(p, uap)
495 struct svr4_sys_mmap_args *uap;
500 retval = p->p_retval;
501 #define _MAP_NEW 0x80000000
503 * Verify the arguments.
505 if (SCARG(uap, prot) & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
506 return EINVAL; /* XXX still needed? */
508 if (SCARG(uap, len) == 0)
511 SCARG(&mm, prot) = SCARG(uap, prot);
512 SCARG(&mm, len) = SCARG(uap, len);
513 SCARG(&mm, flags) = SCARG(uap, flags) & ~_MAP_NEW;
514 SCARG(&mm, fd) = SCARG(uap, fd);
515 SCARG(&mm, addr) = SCARG(uap, addr);
516 SCARG(&mm, pos) = SCARG(uap, pos);
522 svr4_sys_mmap64(p, uap)
524 struct svr4_sys_mmap64_args *uap;
529 #define _MAP_NEW 0x80000000
531 * Verify the arguments.
533 if (SCARG(uap, prot) & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
534 return EINVAL; /* XXX still needed? */
536 if (SCARG(uap, len) == 0)
539 SCARG(&mm, prot) = SCARG(uap, prot);
540 SCARG(&mm, len) = SCARG(uap, len);
541 SCARG(&mm, flags) = SCARG(uap, flags) & ~_MAP_NEW;
542 SCARG(&mm, fd) = SCARG(uap, fd);
543 SCARG(&mm, addr) = SCARG(uap, addr);
544 SCARG(&mm, pos) = SCARG(uap, pos);
546 rp = (void *) round_page((vm_offset_t)(p->p_vmspace->vm_daddr + MAXDSIZ));
547 if ((SCARG(&mm, flags) & MAP_FIXED) == 0 &&
548 SCARG(&mm, addr) != 0 && (void *)SCARG(&mm, addr) < rp)
549 SCARG(&mm, addr) = rp;
556 svr4_sys_fchroot(p, uap)
558 struct svr4_sys_fchroot_args *uap;
560 struct filedesc *fdp = p->p_fd;
565 if ((error = suser(p)) != 0)
567 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
569 vp = (struct vnode *) fp->f_data;
570 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
571 if (vp->v_type != VDIR)
574 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
575 VOP_UNLOCK(vp, 0, p);
579 if (fdp->fd_rdir != NULL)
587 svr4_mknod(p, retval, path, mode, dev)
594 caddr_t sg = stackgap_init();
596 CHECKALTEXIST(p, &sg, path);
598 if (S_ISFIFO(mode)) {
599 struct mkfifo_args ap;
600 SCARG(&ap, path) = path;
601 SCARG(&ap, mode) = mode;
602 return mkfifo(p, &ap);
604 struct mknod_args ap;
605 SCARG(&ap, path) = path;
606 SCARG(&ap, mode) = mode;
607 SCARG(&ap, dev) = dev;
608 return mknod(p, &ap);
614 svr4_sys_mknod(p, uap)
615 register struct proc *p;
616 struct svr4_sys_mknod_args *uap;
618 int *retval = p->p_retval;
619 return svr4_mknod(p, retval,
620 SCARG(uap, path), SCARG(uap, mode),
621 (svr4_dev_t)svr4_to_bsd_odev_t(SCARG(uap, dev)));
626 svr4_sys_xmknod(p, uap)
628 struct svr4_sys_xmknod_args *uap;
630 int *retval = p->p_retval;
631 return svr4_mknod(p, retval,
632 SCARG(uap, path), SCARG(uap, mode),
633 (svr4_dev_t)svr4_to_bsd_dev_t(SCARG(uap, dev)));
638 svr4_sys_vhangup(p, uap)
640 struct svr4_sys_vhangup_args *uap;
647 svr4_sys_sysconfig(p, uap)
649 struct svr4_sys_sysconfig_args *uap;
653 retval = &(p->p_retval[0]);
655 switch (SCARG(uap, name)) {
656 case SVR4_CONFIG_UNUSED:
659 case SVR4_CONFIG_NGROUPS:
660 *retval = NGROUPS_MAX;
662 case SVR4_CONFIG_CHILD_MAX:
665 case SVR4_CONFIG_OPEN_FILES:
668 case SVR4_CONFIG_POSIX_VER:
671 case SVR4_CONFIG_PAGESIZE:
674 case SVR4_CONFIG_CLK_TCK:
675 *retval = 60; /* should this be `hz', ie. 100? */
677 case SVR4_CONFIG_XOPEN_VER:
678 *retval = 2; /* XXX: What should that be? */
680 case SVR4_CONFIG_PROF_TCK:
681 *retval = 60; /* XXX: What should that be? */
683 case SVR4_CONFIG_NPROC_CONF:
684 *retval = 1; /* Only one processor for now */
686 case SVR4_CONFIG_NPROC_ONLN:
687 *retval = 1; /* And it better be online */
689 case SVR4_CONFIG_AIO_LISTIO_MAX:
690 case SVR4_CONFIG_AIO_MAX:
691 case SVR4_CONFIG_AIO_PRIO_DELTA_MAX:
692 *retval = 0; /* No aio support */
694 case SVR4_CONFIG_DELAYTIMER_MAX:
695 *retval = 0; /* No delaytimer support */
697 case SVR4_CONFIG_MQ_OPEN_MAX:
698 *retval = msginfo.msgmni;
700 case SVR4_CONFIG_MQ_PRIO_MAX:
701 *retval = 0; /* XXX: Don't know */
703 case SVR4_CONFIG_RTSIG_MAX:
706 case SVR4_CONFIG_SEM_NSEMS_MAX:
707 *retval = seminfo.semmni;
709 case SVR4_CONFIG_SEM_VALUE_MAX:
710 *retval = seminfo.semvmx;
712 case SVR4_CONFIG_SIGQUEUE_MAX:
713 *retval = 0; /* XXX: Don't know */
715 case SVR4_CONFIG_SIGRT_MIN:
716 case SVR4_CONFIG_SIGRT_MAX:
717 *retval = 0; /* No real time signals */
719 case SVR4_CONFIG_TIMER_MAX:
720 *retval = 3; /* XXX: real, virtual, profiling */
723 case SVR4_CONFIG_PHYS_PAGES:
725 *retval = uvmexp.free; /* XXX: free instead of total */
727 *retval = cnt.v_free_count; /* XXX: free instead of total */
730 case SVR4_CONFIG_AVPHYS_PAGES:
732 *retval = uvmexp.active; /* XXX: active instead of avg */
734 *retval = cnt.v_active_count; /* XXX: active instead of avg */
745 extern int swap_pager_full;
749 svr4_sys_break(p, uap)
751 struct svr4_sys_break_args *uap;
753 struct vmspace *vm = p->p_vmspace;
754 vm_offset_t new, old, base, ns;
757 base = round_page((vm_offset_t) vm->vm_daddr);
758 ns = (vm_offset_t)SCARG(uap, nsize);
759 new = round_page(ns);
761 if ((new - base) > (unsigned) p->p_rlimit[RLIMIT_DATA].rlim_cur) {
764 if (new >= VM_MAXUSER_ADDRESS) {
767 } else if (new < base) {
769 * This is simply an invalid value. If someone wants to
770 * do fancy address space manipulations, mmap and munmap
771 * can do most of what the user would want.
776 old = base + ctob(vm->vm_dsize);
780 if (swap_pager_full) {
784 rv = vm_map_find(&vm->vm_map, NULL, 0, &old, diff, FALSE,
785 VM_PROT_ALL, VM_PROT_ALL, 0);
786 if (rv != KERN_SUCCESS) {
789 vm->vm_dsize += btoc(diff);
790 } else if (new < old) {
791 rv = vm_map_remove(&vm->vm_map, new, old);
792 if (rv != KERN_SUCCESS) {
795 vm->vm_dsize -= btoc(old - new);
801 static __inline clock_t
802 timeval_to_clock_t(tv)
805 return tv->tv_sec * hz + tv->tv_usec / (1000000 / hz);
810 svr4_sys_times(p, uap)
812 struct svr4_sys_times_args *uap;
814 int error, *retval = p->p_retval;
819 struct getrusage_args ga;
821 caddr_t sg = stackgap_init();
822 ru = stackgap_alloc(&sg, sizeof(struct rusage));
824 SCARG(&ga, who) = RUSAGE_SELF;
825 SCARG(&ga, rusage) = ru;
827 error = getrusage(p, &ga);
831 if ((error = copyin(ru, &r, sizeof r)) != 0)
834 tms.tms_utime = timeval_to_clock_t(&r.ru_utime);
835 tms.tms_stime = timeval_to_clock_t(&r.ru_stime);
837 SCARG(&ga, who) = RUSAGE_CHILDREN;
838 error = getrusage(p, &ga);
842 if ((error = copyin(ru, &r, sizeof r)) != 0)
845 tms.tms_cutime = timeval_to_clock_t(&r.ru_utime);
846 tms.tms_cstime = timeval_to_clock_t(&r.ru_stime);
849 *retval = timeval_to_clock_t(&t);
851 return copyout(&tms, SCARG(uap, tp), sizeof(tms));
856 svr4_sys_ulimit(p, uap)
858 struct svr4_sys_ulimit_args *uap;
860 int *retval = p->p_retval;
862 switch (SCARG(uap, cmd)) {
864 *retval = p->p_rlimit[RLIMIT_FSIZE].rlim_cur / 512;
866 *retval = 0x7fffffff;
872 struct __setrlimit_args srl;
874 caddr_t sg = stackgap_init();
875 struct rlimit *url = (struct rlimit *)
876 stackgap_alloc(&sg, sizeof *url);
878 krl.rlim_cur = SCARG(uap, newlimit) * 512;
879 krl.rlim_max = p->p_rlimit[RLIMIT_FSIZE].rlim_max;
881 error = copyout(&krl, url, sizeof(*url));
885 SCARG(&srl, which) = RLIMIT_FSIZE;
886 SCARG(&srl, rlp) = (struct orlimit *)url;
888 error = setrlimit(p, &srl);
892 *retval = p->p_rlimit[RLIMIT_FSIZE].rlim_cur;
894 *retval = 0x7fffffff;
900 struct vmspace *vm = p->p_vmspace;
901 register_t r = p->p_rlimit[RLIMIT_DATA].rlim_cur;
905 r += (long) vm->vm_daddr;
913 *retval = p->p_rlimit[RLIMIT_NOFILE].rlim_cur;
915 *retval = 0x7fffffff;
929 /* look in the live processes */
930 if ((p = pfind(pid)) != NULL)
933 /* look in the zombies */
934 for (p = zombproc.lh_first; p != 0; p = p->p_list.le_next)
943 svr4_sys_pgrpsys(p, uap)
945 struct svr4_sys_pgrpsys_args *uap;
947 int *retval = p->p_retval;
949 switch (SCARG(uap, cmd)) {
950 case 1: /* setpgrp() */
952 * SVR4 setpgrp() (which takes no arguments) has the
953 * semantics that the session ID is also created anew, so
954 * in almost every sense, setpgrp() is identical to
955 * setsid() for SVR4. (Under BSD, the difference is that
956 * a setpgid(0,0) will not create a new session.)
961 case 0: /* getpgrp() */
962 *retval = p->p_pgrp->pg_id;
965 case 2: /* getsid(pid) */
966 if (SCARG(uap, pid) != 0 &&
967 (p = svr4_pfind(SCARG(uap, pid))) == NULL)
970 * This has already been initialized to the pid of
971 * the session leader.
973 *retval = (register_t) p->p_session->s_leader->p_pid;
976 case 3: /* setsid() */
977 return setsid(p, NULL);
979 case 4: /* getpgid(pid) */
981 if (SCARG(uap, pid) != 0 &&
982 (p = svr4_pfind(SCARG(uap, pid))) == NULL)
985 *retval = (int) p->p_pgrp->pg_id;
988 case 5: /* setpgid(pid, pgid); */
990 struct setpgid_args sa;
992 SCARG(&sa, pid) = SCARG(uap, pid);
993 SCARG(&sa, pgid) = SCARG(uap, pgid);
994 return setpgid(p, &sa);
1002 #define syscallarg(x) union { x datum; register_t pad; }
1004 struct svr4_hrtcntl_args {
1008 svr4_hrt_interval_t * iv;
1009 svr4_hrt_time_t * ti;
1014 svr4_hrtcntl(p, uap, retval)
1016 struct svr4_hrtcntl_args *uap;
1019 switch (SCARG(uap, fun)) {
1020 case SVR4_HRT_CNTL_RES:
1021 DPRINTF(("htrcntl(RES)\n"));
1022 *retval = SVR4_HRT_USEC;
1025 case SVR4_HRT_CNTL_TOFD:
1026 DPRINTF(("htrcntl(TOFD)\n"));
1030 if (SCARG(uap, clk) != SVR4_HRT_CLK_STD) {
1031 DPRINTF(("clk == %d\n", SCARG(uap, clk)));
1034 if (SCARG(uap, ti) == NULL) {
1035 DPRINTF(("ti NULL\n"));
1039 t.h_sec = tv.tv_sec;
1040 t.h_rem = tv.tv_usec;
1041 t.h_res = SVR4_HRT_USEC;
1042 return copyout(&t, SCARG(uap, ti), sizeof(t));
1045 case SVR4_HRT_CNTL_START:
1046 DPRINTF(("htrcntl(START)\n"));
1049 case SVR4_HRT_CNTL_GET:
1050 DPRINTF(("htrcntl(GET)\n"));
1053 DPRINTF(("Bad htrcntl command %d\n", SCARG(uap, fun)));
1060 svr4_sys_hrtsys(p, uap)
1062 struct svr4_sys_hrtsys_args *uap;
1064 int *retval = p->p_retval;
1066 switch (SCARG(uap, cmd)) {
1068 return svr4_hrtcntl(p, (struct svr4_hrtcntl_args *) uap,
1072 DPRINTF(("hrtalarm\n"));
1076 DPRINTF(("hrtsleep\n"));
1080 DPRINTF(("hrtcancel\n"));
1084 DPRINTF(("Bad hrtsys command %d\n", SCARG(uap, cmd)));
1091 svr4_setinfo(p, st, s)
1099 memset(&i, 0, sizeof(i));
1101 i.si_signo = SVR4_SIGCHLD;
1102 i.si_errno = 0; /* XXX? */
1105 i.si_pid = p->p_pid;
1106 if (p->p_stat == SZOMB) {
1107 i.si_stime = p->p_ru->ru_stime.tv_sec;
1108 i.si_utime = p->p_ru->ru_utime.tv_sec;
1111 i.si_stime = p->p_stats->p_ru.ru_stime.tv_sec;
1112 i.si_utime = p->p_stats->p_ru.ru_utime.tv_sec;
1116 if (WIFEXITED(st)) {
1117 i.si_status = WEXITSTATUS(st);
1118 i.si_code = SVR4_CLD_EXITED;
1119 } else if (WIFSTOPPED(st)) {
1121 if (sig >= 0 && sig < NSIG)
1122 i.si_status = bsd_to_svr4_sig[sig];
1124 if (i.si_status == SVR4_SIGCONT)
1125 i.si_code = SVR4_CLD_CONTINUED;
1127 i.si_code = SVR4_CLD_STOPPED;
1130 if (sig >= 0 && sig < NSIG)
1131 i.si_status = bsd_to_svr4_sig[sig];
1134 i.si_code = SVR4_CLD_DUMPED;
1136 i.si_code = SVR4_CLD_KILLED;
1139 DPRINTF(("siginfo [pid %ld signo %d code %d errno %d status %d]\n",
1140 i.si_pid, i.si_signo, i.si_code, i.si_errno, i.si_status));
1142 return copyout(&i, s, sizeof(i));
1147 svr4_sys_waitsys(p, uap)
1149 struct svr4_sys_waitsys_args *uap;
1152 int error, *retval = p->p_retval;
1156 switch (SCARG(uap, grp)) {
1161 SCARG(uap, id) = -p->p_pgid;
1165 SCARG(uap, id) = WAIT_ANY;
1172 DPRINTF(("waitsys(%d, %d, %p, %x)\n",
1173 SCARG(uap, grp), SCARG(uap, id),
1174 SCARG(uap, info), SCARG(uap, options)));
1178 for (q = p->p_children.lh_first; q != 0; q = q->p_sibling.le_next) {
1179 if (SCARG(uap, id) != WAIT_ANY &&
1180 q->p_pid != SCARG(uap, id) &&
1181 q->p_pgid != -SCARG(uap, id)) {
1182 DPRINTF(("pid %d pgid %d != %d\n", q->p_pid,
1183 q->p_pgid, SCARG(uap, id)));
1187 if (q->p_stat == SZOMB &&
1188 ((SCARG(uap, options) & (SVR4_WEXITED|SVR4_WTRAPPED)))) {
1190 DPRINTF(("found %d\n", q->p_pid));
1191 if ((error = svr4_setinfo(q, q->p_xstat,
1192 SCARG(uap, info))) != 0)
1196 if ((SCARG(uap, options) & SVR4_WNOWAIT)) {
1197 DPRINTF(("Don't wait\n"));
1202 * If we got the child via ptrace(2) or procfs, and
1203 * the parent is different (meaning the process was
1204 * attached, rather than run as a child), then we need
1205 * to give it back to the ol dparent, and send the
1206 * parent a SIGCHLD. The rest of the cleanup will be
1207 * done when the old parent waits on the child.
1209 if ((q->p_flag & P_TRACED) &&
1210 q->p_oppid != q->p_pptr->p_pid) {
1211 t = pfind(q->p_oppid);
1212 proc_reparent(q, t ? t : initproc);
1214 q->p_flag &= ~(P_TRACED | P_WAITED);
1215 wakeup((caddr_t)q->p_pptr);
1219 ruadd(&p->p_stats->p_cru, q->p_ru);
1221 FREE(q->p_ru, M_ZOMBIE);
1224 * Finally finished with old proc entry.
1225 * Unlink it from its process group and free it.
1229 LIST_REMOVE(q, p_list); /* off zombproc */
1231 LIST_REMOVE(q, p_sibling);
1234 * Decrement the count of procs running with this uid.
1236 (void)chgproccnt(q->p_cred->p_ruid, -1);
1239 * Free up credentials.
1241 if (--q->p_cred->p_refcnt == 0) {
1242 crfree(q->p_cred->pc_ucred);
1243 FREE(q->p_cred, M_SUBPROC);
1247 * Release reference to text vnode
1253 * Give machine-dependent layer a chance
1254 * to free anything that cpu_exit couldn't
1255 * release while still running in process context.
1258 #if defined(__NetBSD__)
1259 pool_put(&proc_pool, q);
1264 if (q->p_stat == SSTOP && (q->p_flag & P_WAITED) == 0 &&
1265 (q->p_flag & P_TRACED ||
1266 (SCARG(uap, options) & (SVR4_WSTOPPED|SVR4_WCONTINUED)))) {
1267 DPRINTF(("jobcontrol %d\n", q->p_pid));
1268 if (((SCARG(uap, options) & SVR4_WNOWAIT)) == 0)
1269 q->p_flag |= P_WAITED;
1271 return svr4_setinfo(q, W_STOPCODE(q->p_xstat),
1279 if (SCARG(uap, options) & SVR4_WNOHANG) {
1281 if ((error = svr4_setinfo(NULL, 0, SCARG(uap, info))) != 0)
1286 if ((error = tsleep((caddr_t)p, PWAIT | PCATCH, "svr4_wait", 0)) != 0)
1293 bsd_statfs_to_svr4_statvfs(bfs, sfs)
1294 const struct statfs *bfs;
1295 struct svr4_statvfs *sfs;
1297 sfs->f_bsize = bfs->f_iosize; /* XXX */
1298 sfs->f_frsize = bfs->f_bsize;
1299 sfs->f_blocks = bfs->f_blocks;
1300 sfs->f_bfree = bfs->f_bfree;
1301 sfs->f_bavail = bfs->f_bavail;
1302 sfs->f_files = bfs->f_files;
1303 sfs->f_ffree = bfs->f_ffree;
1304 sfs->f_favail = bfs->f_ffree;
1305 sfs->f_fsid = bfs->f_fsid.val[0];
1306 memcpy(sfs->f_basetype, bfs->f_fstypename, sizeof(sfs->f_basetype));
1308 if (bfs->f_flags & MNT_RDONLY)
1309 sfs->f_flag |= SVR4_ST_RDONLY;
1310 if (bfs->f_flags & MNT_NOSUID)
1311 sfs->f_flag |= SVR4_ST_NOSUID;
1312 sfs->f_namemax = MAXNAMLEN;
1313 memcpy(sfs->f_fstr, bfs->f_fstypename, sizeof(sfs->f_fstr)); /* XXX */
1314 memset(sfs->f_filler, 0, sizeof(sfs->f_filler));
1319 bsd_statfs_to_svr4_statvfs64(bfs, sfs)
1320 const struct statfs *bfs;
1321 struct svr4_statvfs64 *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 svr4_sys_statvfs(p, uap)
1347 struct svr4_sys_statvfs_args *uap;
1349 struct statfs_args fs_args;
1350 caddr_t sg = stackgap_init();
1351 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1353 struct svr4_statvfs sfs;
1356 CHECKALTEXIST(p, &sg, SCARG(uap, path));
1357 SCARG(&fs_args, path) = SCARG(uap, path);
1358 SCARG(&fs_args, buf) = fs;
1360 if ((error = statfs(p, &fs_args)) != 0)
1363 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1366 bsd_statfs_to_svr4_statvfs(&bfs, &sfs);
1368 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1373 svr4_sys_fstatvfs(p, uap)
1375 struct svr4_sys_fstatvfs_args *uap;
1377 struct fstatfs_args fs_args;
1378 caddr_t sg = stackgap_init();
1379 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1381 struct svr4_statvfs sfs;
1384 SCARG(&fs_args, fd) = SCARG(uap, fd);
1385 SCARG(&fs_args, buf) = fs;
1387 if ((error = fstatfs(p, &fs_args)) != 0)
1390 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1393 bsd_statfs_to_svr4_statvfs(&bfs, &sfs);
1395 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1400 svr4_sys_statvfs64(p, uap)
1402 struct svr4_sys_statvfs64_args *uap;
1404 struct statfs_args fs_args;
1405 caddr_t sg = stackgap_init();
1406 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1408 struct svr4_statvfs64 sfs;
1411 CHECKALTEXIST(p, &sg, SCARG(uap, path));
1412 SCARG(&fs_args, path) = SCARG(uap, path);
1413 SCARG(&fs_args, buf) = fs;
1415 if ((error = statfs(p, &fs_args)) != 0)
1418 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1421 bsd_statfs_to_svr4_statvfs64(&bfs, &sfs);
1423 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1428 svr4_sys_fstatvfs64(p, uap)
1430 struct svr4_sys_fstatvfs64_args *uap;
1432 struct fstatfs_args fs_args;
1433 caddr_t sg = stackgap_init();
1434 struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1436 struct svr4_statvfs64 sfs;
1439 SCARG(&fs_args, fd) = SCARG(uap, fd);
1440 SCARG(&fs_args, buf) = fs;
1442 if ((error = fstatfs(p, &fs_args)) != 0)
1445 if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1448 bsd_statfs_to_svr4_statvfs64(&bfs, &sfs);
1450 return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1454 svr4_sys_alarm(p, uap)
1456 struct svr4_sys_alarm_args *uap;
1459 struct itimerval *itp, *oitp;
1460 struct setitimer_args sa;
1461 caddr_t sg = stackgap_init();
1463 itp = stackgap_alloc(&sg, sizeof(*itp));
1464 oitp = stackgap_alloc(&sg, sizeof(*oitp));
1465 timevalclear(&itp->it_interval);
1466 itp->it_value.tv_sec = SCARG(uap, sec);
1467 itp->it_value.tv_usec = 0;
1469 SCARG(&sa, which) = ITIMER_REAL;
1470 SCARG(&sa, itv) = itp;
1471 SCARG(&sa, oitv) = oitp;
1472 error = setitimer(p, &sa);
1475 if (oitp->it_value.tv_usec)
1476 oitp->it_value.tv_sec++;
1477 p->p_retval[0] = oitp->it_value.tv_sec;
1483 svr4_sys_gettimeofday(p, uap)
1485 struct svr4_sys_gettimeofday_args *uap;
1487 if (SCARG(uap, tp)) {
1491 return copyout(&atv, SCARG(uap, tp), sizeof (atv));
1498 svr4_sys_facl(p, uap)
1500 struct svr4_sys_facl_args *uap;
1504 retval = p->p_retval;
1507 switch (SCARG(uap, cmd)) {
1508 case SVR4_SYS_SETACL:
1509 /* We don't support acls on any filesystem */
1512 case SVR4_SYS_GETACL:
1513 return copyout(retval, &SCARG(uap, num),
1514 sizeof(SCARG(uap, num)));
1516 case SVR4_SYS_GETACLCNT:
1526 svr4_sys_acl(p, uap)
1528 struct svr4_sys_acl_args *uap;
1530 /* XXX: for now the same */
1531 return svr4_sys_facl(p, (struct svr4_sys_facl_args *)uap);
1535 svr4_sys_auditsys(p, uap)
1537 struct svr4_sys_auditsys_args *uap;
1540 * XXX: Big brother is *not* watching.
1546 svr4_sys_memcntl(p, uap)
1548 struct svr4_sys_memcntl_args *uap;
1550 switch (SCARG(uap, cmd)) {
1553 struct msync_args msa;
1555 SCARG(&msa, addr) = SCARG(uap, addr);
1556 SCARG(&msa, len) = SCARG(uap, len);
1557 SCARG(&msa, flags) = (int)SCARG(uap, arg);
1559 return msync(p, &msa);
1561 case SVR4_MC_ADVISE:
1563 struct madvise_args maa;
1565 SCARG(&maa, addr) = SCARG(uap, addr);
1566 SCARG(&maa, len) = SCARG(uap, len);
1567 SCARG(&maa, behav) = (int)SCARG(uap, arg);
1569 return madvise(p, &maa);
1572 case SVR4_MC_UNLOCK:
1573 case SVR4_MC_LOCKAS:
1574 case SVR4_MC_UNLOCKAS:
1583 svr4_sys_nice(p, uap)
1585 struct svr4_sys_nice_args *uap;
1587 struct setpriority_args ap;
1590 SCARG(&ap, which) = PRIO_PROCESS;
1591 SCARG(&ap, who) = 0;
1592 SCARG(&ap, prio) = SCARG(uap, prio);
1594 if ((error = setpriority(p, &ap)) != 0)
1597 /* the cast is stupid, but the structures are the same */
1598 if ((error = getpriority(p, (struct getpriority_args *)&ap)) != 0)
1605 svr4_sys_resolvepath(p, uap)
1607 struct svr4_sys_resolvepath_args *uap;
1609 struct nameidata nd;
1610 int error, *retval = p->p_retval;
1612 NDINIT(&nd, LOOKUP, NOFOLLOW | SAVENAME, UIO_USERSPACE,
1613 SCARG(uap, path), p);
1615 if ((error = namei(&nd)) != 0)
1618 if ((error = copyout(nd.ni_cnd.cn_pnbuf, SCARG(uap, buf),
1619 SCARG(uap, bufsiz))) != 0)
1622 *retval = strlen(nd.ni_cnd.cn_pnbuf) < SCARG(uap, bufsiz) ?
1623 strlen(nd.ni_cnd.cn_pnbuf) + 1 : SCARG(uap, bufsiz);
1626 zfree(namei_zone, nd.ni_cnd.cn_pnbuf);