proc->thread stage 3.5: Add an IO_CORE flag so coda doesn't have to dig
[dragonfly.git] / sys / emulation / svr4 / svr4_misc.c
1 /*
2  * Copyright (c) 1998 Mark Newton
3  * Copyright (c) 1994 Christos Zoulas
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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
16  *
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.
27  * 
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.3 2003/06/23 17:55:49 dillon Exp $
30  */
31
32 /*
33  * SVR4 compatibility module.
34  *
35  * SVR4 system calls that are implemented differently in BSD are
36  * handled here.
37  */
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/namei.h>
42 #include <sys/dirent.h>
43 #include <sys/malloc.h>
44 #include <sys/proc.h>
45 #include <sys/file.h>
46 #include <sys/stat.h>
47 #include <sys/time.h>
48 #include <sys/file.h>
49 #include <sys/filedesc.h>
50 #include <sys/kernel.h>
51 #include <sys/mman.h>
52 #include <sys/mount.h>
53 #include <sys/resource.h>
54 #include <sys/resourcevar.h>
55 #include <sys/vnode.h>
56 #include <sys/uio.h>
57 #include <sys/wait.h>
58 #include <sys/times.h>
59 #include <sys/fcntl.h>
60 #include <sys/sem.h>
61 #include <sys/msg.h>
62 #include <sys/ptrace.h>
63 #include <vm/vm_zone.h>
64
65 #include <sys/sysproto.h>
66
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>
80
81 #include <machine/vmparam.h>
82 #include <vm/vm.h>
83 #include <vm/vm_param.h>
84 #include <vm/vm_map.h>
85
86 #if defined(__FreeBSD__)
87 #include <vm/vm_extern.h>
88 #endif
89
90 #if defined(NetBSD)
91 # if defined(UVM)
92 #  include <uvm/uvm_extern.h>
93 # endif
94 #endif
95
96 #define BSD_DIRENT(cp)          ((struct dirent *)(cp))
97
98 static int svr4_mknod __P((register_t *, char *, svr4_mode_t, svr4_dev_t));
99
100 static __inline clock_t timeval_to_clock_t __P((struct timeval *));
101 static int svr4_setinfo __P((struct proc *, int, svr4_siginfo_t *));
102
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));
110
111 /* BOGUS noop */
112 #if defined(BOGUS)
113 int
114 svr4_sys_setitimer(struct svr4_sys_setitimer_args *uap)
115 {
116         p->p_retval[0] = 0;
117         return 0;
118 }
119 #endif
120
121 int
122 svr4_sys_wait(struct svr4_sys_wait_args *uap)
123 {
124         struct proc *p = curproc;
125         struct wait_args w4;
126         int error, *retval = p->p_retval, st, sig;
127         size_t sz = sizeof(*SCARG(&w4, status));
128
129         SCARG(&w4, rusage) = NULL;
130         SCARG(&w4, options) = 0;
131
132         if (SCARG(uap, status) == NULL) {
133                 caddr_t sg = stackgap_init();
134
135                 SCARG(&w4, status) = stackgap_alloc(&sg, sz);
136         }
137         else
138                 SCARG(&w4, status) = SCARG(uap, status);
139
140         SCARG(&w4, pid) = WAIT_ANY;
141
142         if ((error = wait4(&w4)) != 0)
143                 return error;
144       
145         if ((error = copyin(SCARG(&w4, status), &st, sizeof(st))) != 0)
146                 return error;
147
148         if (WIFSIGNALED(st)) {
149                 sig = WTERMSIG(st);
150                 if (sig >= 0 && sig < NSIG)
151                         st = (st & ~0177) | SVR4_BSD2SVR4_SIG(sig);
152         } else if (WIFSTOPPED(st)) {
153                 sig = WSTOPSIG(st);
154                 if (sig >= 0 && sig < NSIG)
155                         st = (st & ~0xff00) | (SVR4_BSD2SVR4_SIG(sig) << 8);
156         }
157
158         /*
159          * It looks like wait(2) on svr4/solaris/2.4 returns
160          * the status in retval[1], and the pid on retval[0].
161          */
162         retval[1] = st;
163
164         if (SCARG(uap, status))
165                 if ((error = copyout(&st, SCARG(uap, status), sizeof(st))) != 0)
166                         return error;
167
168         return 0;
169 }
170
171 int
172 svr4_sys_execv(struct svr4_sys_execv_args *uap)
173 {
174         struct execve_args ap;
175         caddr_t sg;
176
177         sg = stackgap_init();
178         CHECKALTEXIST(&sg, SCARG(uap, path));
179
180         SCARG(&ap, fname) = SCARG(uap, path);
181         SCARG(&ap, argv) = SCARG(uap, argp);
182         SCARG(&ap, envv) = NULL;
183
184         return execve(&ap);
185 }
186
187 int
188 svr4_sys_execve(struct svr4_sys_execve_args *uap)
189 {
190         struct execve_args ap;
191         caddr_t sg;
192
193         sg = stackgap_init();
194         CHECKALTEXIST(&sg, uap->path);
195
196         SCARG(&ap, fname) = SCARG(uap, path);
197         SCARG(&ap, argv) = SCARG(uap, argp);
198         SCARG(&ap, envv) = SCARG(uap, envp);
199
200         return execve(&ap);
201 }
202
203 int
204 svr4_sys_time(struct svr4_sys_time_args *v)
205 {
206         struct proc *p = curproc;
207         struct svr4_sys_time_args *uap = v;
208         int error = 0;
209         struct timeval tv;
210
211         microtime(&tv);
212         if (SCARG(uap, t))
213                 error = copyout(&tv.tv_sec, SCARG(uap, t),
214                                 sizeof(*(SCARG(uap, t))));
215         p->p_retval[0] = (int) tv.tv_sec;
216
217         return error;
218 }
219
220
221 /*
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.  
224  *
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.
228  */
229 int
230 svr4_sys_getdents64(struct svr4_sys_getdents64_args *uap)
231 {
232         struct proc *p = curproc;
233         struct dirent *bdp;
234         struct vnode *vp;
235         caddr_t inp, buf;               /* BSD-format */
236         int len, reclen;                /* BSD-format */
237         caddr_t outp;                   /* SVR4-format */
238         int resid, svr4reclen=0;        /* SVR4-format */
239         struct file *fp;
240         struct uio auio;
241         struct iovec aiov;
242         struct vattr va;
243         off_t off;
244         struct svr4_dirent64 svr4_dirent;
245         int buflen, error, eofflag, nbytes, justone;
246         u_long *cookies = NULL, *cookiep;
247         int ncookies;
248
249         DPRINTF(("svr4_sys_getdents64(%d, *, %d)\n",
250                 p->p_pid, SCARG(uap, fd), SCARG(uap, nbytes)));
251         if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0) {
252                 return (error);
253         }
254
255         if ((fp->f_flag & FREAD) == 0)
256                 return (EBADF);
257
258         vp = (struct vnode *) fp->f_data;
259
260         if (vp->v_type != VDIR)
261                 return (EINVAL);
262
263         if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p))) {
264                 return error;
265         }
266
267         nbytes = SCARG(uap, nbytes);
268         if (nbytes == 1) {
269                 nbytes = sizeof (struct svr4_dirent64);
270                 justone = 1;
271         }
272         else
273                 justone = 0;
274
275         off = fp->f_offset;
276 #define DIRBLKSIZ       512             /* XXX we used to use ufs's DIRBLKSIZ */
277         buflen = max(DIRBLKSIZ, nbytes);
278         buflen = min(buflen, MAXBSIZE);
279         buf = malloc(buflen, M_TEMP, M_WAITOK);
280         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
281 again:
282         aiov.iov_base = buf;
283         aiov.iov_len = buflen;
284         auio.uio_iov = &aiov;
285         auio.uio_iovcnt = 1;
286         auio.uio_rw = UIO_READ;
287         auio.uio_segflg = UIO_SYSSPACE;
288         auio.uio_procp = p;
289         auio.uio_resid = buflen;
290         auio.uio_offset = off;
291
292         if (cookies) {
293                 free(cookies, M_TEMP);
294                 cookies = NULL;
295         }
296
297         error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
298                                                 &ncookies, &cookies);
299         if (error) {
300                 goto out;
301         }
302
303         inp = buf;
304         outp = (caddr_t) SCARG(uap, dp);
305         resid = nbytes;
306         if ((len = buflen - auio.uio_resid) <= 0) {
307                 goto eof;
308         }
309
310         cookiep = cookies;
311
312         if (cookies) {
313                 /*
314                  * When using cookies, the vfs has the option of reading from
315                  * a different offset than that supplied (UFS truncates the
316                  * offset to a block boundary to make sure that it never reads
317                  * partway through a directory entry, even if the directory
318                  * has been compacted).
319                  */
320                 while (len > 0 && ncookies > 0 && *cookiep <= off) {
321                         bdp = (struct dirent *) inp;
322                         len -= bdp->d_reclen;
323                         inp += bdp->d_reclen;
324                         cookiep++;
325                         ncookies--;
326                 }
327         }
328
329         while (len > 0) {
330                 if (cookiep && ncookies == 0)
331                         break;
332                 bdp = (struct dirent *) inp;
333                 reclen = bdp->d_reclen;
334                 if (reclen & 3) {
335                         DPRINTF(("svr4_readdir: reclen=%d\n", reclen));
336                         error = EFAULT;
337                         goto out;
338                 }
339   
340                 if (bdp->d_fileno == 0) {
341                         inp += reclen;
342                         if (cookiep) {
343                                 off = *cookiep++;
344                                 ncookies--;
345                         } else
346                                 off += reclen;
347                         len -= reclen;
348                         continue;
349                 }
350                 svr4reclen = SVR4_RECLEN(&svr4_dirent, bdp->d_namlen);
351                 if (reclen > len || resid < svr4reclen) {
352                         outp++;
353                         break;
354                 }
355                 svr4_dirent.d_ino = (long) bdp->d_fileno;
356                 if (justone) {
357                         /*
358                          * old svr4-style readdir usage.
359                          */
360                         svr4_dirent.d_off = (svr4_off_t) svr4reclen;
361                         svr4_dirent.d_reclen = (u_short) bdp->d_namlen;
362                 } else {
363                         svr4_dirent.d_off = (svr4_off_t)(off + reclen);
364                         svr4_dirent.d_reclen = (u_short) svr4reclen;
365                 }
366                 strcpy(svr4_dirent.d_name, bdp->d_name);
367                 if ((error = copyout((caddr_t)&svr4_dirent, outp, svr4reclen)))
368                         goto out;
369                 inp += reclen;
370                 if (cookiep) {
371                         off = *cookiep++;
372                         ncookies--;
373                 } else
374                         off += reclen;
375                 outp += svr4reclen;
376                 resid -= svr4reclen;
377                 len -= reclen;
378                 if (justone)
379                         break;
380         }
381
382         if (outp == (caddr_t) SCARG(uap, dp))
383                 goto again;
384         fp->f_offset = off;
385
386         if (justone)
387                 nbytes = resid + svr4reclen;
388
389 eof:
390         p->p_retval[0] = nbytes - resid;
391 out:
392         if (cookies)
393                 free(cookies, M_TEMP);
394         VOP_UNLOCK(vp, 0, p);
395         free(buf, M_TEMP);
396         return error;
397 }
398
399
400 int
401 svr4_sys_getdents(struct svr4_sys_getdents_args *uap)
402 {
403         struct proc *p = curproc;
404         struct dirent *bdp;
405         struct vnode *vp;
406         caddr_t inp, buf;       /* BSD-format */
407         int len, reclen;        /* BSD-format */
408         caddr_t outp;           /* SVR4-format */
409         int resid, svr4_reclen; /* SVR4-format */
410         struct file *fp;
411         struct uio auio;
412         struct iovec aiov;
413         struct svr4_dirent idb;
414         off_t off;              /* true file offset */
415         int buflen, error, eofflag;
416         u_long *cookiebuf = NULL, *cookie;
417         int ncookies = 0, *retval = p->p_retval;
418
419         if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
420                 return (error);
421
422         if ((fp->f_flag & FREAD) == 0)
423                 return (EBADF);
424
425         vp = (struct vnode *)fp->f_data;
426         if (vp->v_type != VDIR)
427                 return (EINVAL);
428
429         buflen = min(MAXBSIZE, SCARG(uap, nbytes));
430         buf = malloc(buflen, M_TEMP, M_WAITOK);
431         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
432         off = fp->f_offset;
433 again:
434         aiov.iov_base = buf;
435         aiov.iov_len = buflen;
436         auio.uio_iov = &aiov;
437         auio.uio_iovcnt = 1;
438         auio.uio_rw = UIO_READ;
439         auio.uio_segflg = UIO_SYSSPACE;
440         auio.uio_procp = p;
441         auio.uio_resid = buflen;
442         auio.uio_offset = off;
443         /*
444          * First we read into the malloc'ed buffer, then
445          * we massage it into user space, one record at a time.
446          */
447         error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies,
448             &cookiebuf);
449         if (error)
450                 goto out;
451
452         inp = buf;
453         outp = SCARG(uap, buf);
454         resid = SCARG(uap, nbytes);
455         if ((len = buflen - auio.uio_resid) == 0)
456                 goto eof;
457
458         for (cookie = cookiebuf; len > 0; len -= reclen) {
459                 bdp = (struct dirent *)inp;
460                 reclen = bdp->d_reclen;
461                 if (reclen & 3)
462                         panic("svr4_sys_getdents64: bad reclen");
463                 off = *cookie++;        /* each entry points to the next */
464                 if ((off >> 32) != 0) {
465                         uprintf("svr4_sys_getdents64: dir offset too large for emulated program");
466                         error = EINVAL;
467                         goto out;
468                 }
469                 if (bdp->d_fileno == 0) {
470                         inp += reclen;  /* it is a hole; squish it out */
471                         continue;
472                 }
473                 svr4_reclen = SVR4_RECLEN(&idb, bdp->d_namlen);
474                 if (reclen > len || resid < svr4_reclen) {
475                         /* entry too big for buffer, so just stop */
476                         outp++;
477                         break;
478                 }
479                 /*
480                  * Massage in place to make a SVR4-shaped dirent (otherwise
481                  * we have to worry about touching user memory outside of
482                  * the copyout() call).
483                  */
484                 idb.d_ino = (svr4_ino_t)bdp->d_fileno;
485                 idb.d_off = (svr4_off_t)off;
486                 idb.d_reclen = (u_short)svr4_reclen;
487                 strcpy(idb.d_name, bdp->d_name);
488                 if ((error = copyout((caddr_t)&idb, outp, svr4_reclen)))
489                         goto out;
490                 /* advance past this real entry */
491                 inp += reclen;
492                 /* advance output past SVR4-shaped entry */
493                 outp += svr4_reclen;
494                 resid -= svr4_reclen;
495         }
496
497         /* if we squished out the whole block, try again */
498         if (outp == SCARG(uap, buf))
499                 goto again;
500         fp->f_offset = off;     /* update the vnode offset */
501
502 eof:
503         *retval = SCARG(uap, nbytes) - resid;
504 out:
505         VOP_UNLOCK(vp, 0, p);
506         if (cookiebuf)
507                 free(cookiebuf, M_TEMP);
508         free(buf, M_TEMP);
509         return error;
510 }
511
512
513 int
514 svr4_sys_mmap(struct svr4_sys_mmap_args *uap)
515 {
516         struct proc *p = curproc;
517         struct mmap_args         mm;
518         int             *retval;
519
520         retval = p->p_retval;
521 #define _MAP_NEW        0x80000000
522         /*
523          * Verify the arguments.
524          */
525         if (SCARG(uap, prot) & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
526                 return EINVAL;  /* XXX still needed? */
527
528         if (SCARG(uap, len) == 0)
529                 return EINVAL;
530
531         SCARG(&mm, prot) = SCARG(uap, prot);
532         SCARG(&mm, len) = SCARG(uap, len);
533         SCARG(&mm, flags) = SCARG(uap, flags) & ~_MAP_NEW;
534         SCARG(&mm, fd) = SCARG(uap, fd);
535         SCARG(&mm, addr) = SCARG(uap, addr);
536         SCARG(&mm, pos) = SCARG(uap, pos);
537
538         return mmap(&mm);
539 }
540
541 int
542 svr4_sys_mmap64(struct svr4_sys_mmap64_args *uap)
543 {
544         struct proc *p = curproc;
545         struct mmap_args         mm;
546         void            *rp;
547
548 #define _MAP_NEW        0x80000000
549         /*
550          * Verify the arguments.
551          */
552         if (SCARG(uap, prot) & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
553                 return EINVAL;  /* XXX still needed? */
554
555         if (SCARG(uap, len) == 0)
556                 return EINVAL;
557
558         SCARG(&mm, prot) = SCARG(uap, prot);
559         SCARG(&mm, len) = SCARG(uap, len);
560         SCARG(&mm, flags) = SCARG(uap, flags) & ~_MAP_NEW;
561         SCARG(&mm, fd) = SCARG(uap, fd);
562         SCARG(&mm, addr) = SCARG(uap, addr);
563         SCARG(&mm, pos) = SCARG(uap, pos);
564
565         rp = (void *) round_page((vm_offset_t)(p->p_vmspace->vm_daddr + maxdsiz));
566         if ((SCARG(&mm, flags) & MAP_FIXED) == 0 &&
567             SCARG(&mm, addr) != 0 && (void *)SCARG(&mm, addr) < rp)
568                 SCARG(&mm, addr) = rp;
569
570         return mmap(&mm);
571 }
572
573
574 int
575 svr4_sys_fchroot(struct svr4_sys_fchroot_args *uap)
576 {
577         struct proc *p = curproc;
578         struct filedesc *fdp = p->p_fd;
579         struct vnode    *vp;
580         struct file     *fp;
581         int              error;
582
583         if ((error = suser_xxx(p->p_ucred, 0)) != 0)
584                 return error;
585         if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
586                 return error;
587         vp = (struct vnode *) fp->f_data;
588         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
589         if (vp->v_type != VDIR)
590                 error = ENOTDIR;
591         else
592                 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
593         VOP_UNLOCK(vp, 0, p);
594         if (error)
595                 return error;
596         VREF(vp);
597         if (fdp->fd_rdir != NULL)
598                 vrele(fdp->fd_rdir);
599         fdp->fd_rdir = vp;
600         return 0;
601 }
602
603
604 static int
605 svr4_mknod(retval, path, mode, dev)
606         register_t *retval;
607         char *path;
608         svr4_mode_t mode;
609         svr4_dev_t dev;
610 {
611         caddr_t sg = stackgap_init();
612
613         CHECKALTEXIST(&sg, path);
614
615         if (S_ISFIFO(mode)) {
616                 struct mkfifo_args ap;
617                 SCARG(&ap, path) = path;
618                 SCARG(&ap, mode) = mode;
619                 return mkfifo(&ap);
620         } else {
621                 struct mknod_args ap;
622                 SCARG(&ap, path) = path;
623                 SCARG(&ap, mode) = mode;
624                 SCARG(&ap, dev) = dev;
625                 return mknod(&ap);
626         }
627 }
628
629
630 int
631 svr4_sys_mknod(struct svr4_sys_mknod_args *uap)
632 {
633         struct proc *p = curproc;
634
635         int *retval = p->p_retval;
636         return svr4_mknod(retval, SCARG(uap, path), SCARG(uap, mode),
637                           (svr4_dev_t)svr4_to_bsd_odev_t(SCARG(uap, dev)));
638 }
639
640
641 int
642 svr4_sys_xmknod(struct svr4_sys_xmknod_args *uap)
643 {
644         struct proc *p = curproc;
645         int *retval = p->p_retval;
646         return svr4_mknod(retval, SCARG(uap, path), SCARG(uap, mode),
647                           (svr4_dev_t)svr4_to_bsd_dev_t(SCARG(uap, dev)));
648 }
649
650
651 int
652 svr4_sys_vhangup(struct svr4_sys_vhangup_args *uap)
653 {
654         return 0;
655 }
656
657
658 int
659 svr4_sys_sysconfig(struct svr4_sys_sysconfig_args *uap)
660 {
661         struct proc *p = curproc;
662         int *retval;
663
664         retval = &(p->p_retval[0]);
665
666         switch (SCARG(uap, name)) {
667         case SVR4_CONFIG_UNUSED:
668                 *retval = 0;
669                 break;
670         case SVR4_CONFIG_NGROUPS:
671                 *retval = NGROUPS_MAX;
672                 break;
673         case SVR4_CONFIG_CHILD_MAX:
674                 *retval = maxproc;
675                 break;
676         case SVR4_CONFIG_OPEN_FILES:
677                 *retval = maxfiles;
678                 break;
679         case SVR4_CONFIG_POSIX_VER:
680                 *retval = 198808;
681                 break;
682         case SVR4_CONFIG_PAGESIZE:
683                 *retval = PAGE_SIZE;
684                 break;
685         case SVR4_CONFIG_CLK_TCK:
686                 *retval = 60;   /* should this be `hz', ie. 100? */
687                 break;
688         case SVR4_CONFIG_XOPEN_VER:
689                 *retval = 2;    /* XXX: What should that be? */
690                 break;
691         case SVR4_CONFIG_PROF_TCK:
692                 *retval = 60;   /* XXX: What should that be? */
693                 break;
694         case SVR4_CONFIG_NPROC_CONF:
695                 *retval = 1;    /* Only one processor for now */
696                 break;
697         case SVR4_CONFIG_NPROC_ONLN:
698                 *retval = 1;    /* And it better be online */
699                 break;
700         case SVR4_CONFIG_AIO_LISTIO_MAX:
701         case SVR4_CONFIG_AIO_MAX:
702         case SVR4_CONFIG_AIO_PRIO_DELTA_MAX:
703                 *retval = 0;    /* No aio support */
704                 break;
705         case SVR4_CONFIG_DELAYTIMER_MAX:
706                 *retval = 0;    /* No delaytimer support */
707                 break;
708         case SVR4_CONFIG_MQ_OPEN_MAX:
709                 *retval = msginfo.msgmni;
710                 break;
711         case SVR4_CONFIG_MQ_PRIO_MAX:
712                 *retval = 0;    /* XXX: Don't know */
713                 break;
714         case SVR4_CONFIG_RTSIG_MAX:
715                 *retval = 0;
716                 break;
717         case SVR4_CONFIG_SEM_NSEMS_MAX:
718                 *retval = seminfo.semmni;
719                 break;
720         case SVR4_CONFIG_SEM_VALUE_MAX:
721                 *retval = seminfo.semvmx;
722                 break;
723         case SVR4_CONFIG_SIGQUEUE_MAX:
724                 *retval = 0;    /* XXX: Don't know */
725                 break;
726         case SVR4_CONFIG_SIGRT_MIN:
727         case SVR4_CONFIG_SIGRT_MAX:
728                 *retval = 0;    /* No real time signals */
729                 break;
730         case SVR4_CONFIG_TIMER_MAX:
731                 *retval = 3;    /* XXX: real, virtual, profiling */
732                 break;
733 #if defined(NOTYET)
734         case SVR4_CONFIG_PHYS_PAGES:
735 #if defined(UVM)
736                 *retval = uvmexp.free;  /* XXX: free instead of total */
737 #else
738                 *retval = cnt.v_free_count;     /* XXX: free instead of total */
739 #endif
740                 break;
741         case SVR4_CONFIG_AVPHYS_PAGES:
742 #if defined(UVM)
743                 *retval = uvmexp.active;        /* XXX: active instead of avg */
744 #else
745                 *retval = cnt.v_active_count;   /* XXX: active instead of avg */
746 #endif
747                 break;
748 #endif /* NOTYET */
749
750         default:
751                 return EINVAL;
752         }
753         return 0;
754 }
755
756 extern int swap_pager_full;
757
758 /* ARGSUSED */
759 int
760 svr4_sys_break(struct svr4_sys_break_args *uap)
761 {
762         struct proc *p = curproc;
763         struct vmspace *vm = p->p_vmspace;
764         vm_offset_t new, old, base, ns;
765         int rv;
766
767         base = round_page((vm_offset_t) vm->vm_daddr);
768         ns = (vm_offset_t)SCARG(uap, nsize);
769         new = round_page(ns);
770         if (new > base) {
771           if ((new - base) > (unsigned) p->p_rlimit[RLIMIT_DATA].rlim_cur) {
772                         return ENOMEM;
773           }
774           if (new >= VM_MAXUSER_ADDRESS) {
775             return (ENOMEM);
776           }
777         } else if (new < base) {
778                 /*
779                  * This is simply an invalid value.  If someone wants to
780                  * do fancy address space manipulations, mmap and munmap
781                  * can do most of what the user would want.
782                  */
783                 return EINVAL;
784         }
785
786         old = base + ctob(vm->vm_dsize);
787
788         if (new > old) {
789                 vm_size_t diff;
790                 diff = new - old;
791                 if (vm->vm_map.size + diff > p->p_rlimit[RLIMIT_VMEM].rlim_cur)
792                         return(ENOMEM);
793                 rv = vm_map_find(&vm->vm_map, NULL, 0, &old, diff, FALSE,
794                         VM_PROT_ALL, VM_PROT_ALL, 0);
795                 if (rv != KERN_SUCCESS) {
796                         return (ENOMEM);
797                 }
798                 vm->vm_dsize += btoc(diff);
799         } else if (new < old) {
800                 rv = vm_map_remove(&vm->vm_map, new, old);
801                 if (rv != KERN_SUCCESS) {
802                         return (ENOMEM);
803                 }
804                 vm->vm_dsize -= btoc(old - new);
805         }
806
807         return (0);
808 }
809
810 static __inline clock_t
811 timeval_to_clock_t(tv)
812         struct timeval *tv;
813 {
814         return tv->tv_sec * hz + tv->tv_usec / (1000000 / hz);
815 }
816
817
818 int
819 svr4_sys_times(struct svr4_sys_times_args *uap)
820 {
821         struct proc *p = curproc;
822         int                      error, *retval = p->p_retval;
823         struct tms               tms;
824         struct timeval           t;
825         struct rusage           *ru;
826         struct rusage            r;
827         struct getrusage_args    ga;
828
829         caddr_t sg = stackgap_init();
830         ru = stackgap_alloc(&sg, sizeof(struct rusage));
831
832         SCARG(&ga, who) = RUSAGE_SELF;
833         SCARG(&ga, rusage) = ru;
834
835         error = getrusage(&ga);
836         if (error)
837                 return error;
838
839         if ((error = copyin(ru, &r, sizeof r)) != 0)
840                 return error;
841
842         tms.tms_utime = timeval_to_clock_t(&r.ru_utime);
843         tms.tms_stime = timeval_to_clock_t(&r.ru_stime);
844
845         SCARG(&ga, who) = RUSAGE_CHILDREN;
846         error = getrusage(&ga);
847         if (error)
848                 return error;
849
850         if ((error = copyin(ru, &r, sizeof r)) != 0)
851                 return error;
852
853         tms.tms_cutime = timeval_to_clock_t(&r.ru_utime);
854         tms.tms_cstime = timeval_to_clock_t(&r.ru_stime);
855
856         microtime(&t);
857         *retval = timeval_to_clock_t(&t);
858
859         return copyout(&tms, SCARG(uap, tp), sizeof(tms));
860 }
861
862
863 int
864 svr4_sys_ulimit(struct svr4_sys_ulimit_args *uap)
865 {
866         struct proc *p = curproc;
867         int *retval = p->p_retval;
868
869         switch (SCARG(uap, cmd)) {
870         case SVR4_GFILLIM:
871                 *retval = p->p_rlimit[RLIMIT_FSIZE].rlim_cur / 512;
872                 if (*retval == -1)
873                         *retval = 0x7fffffff;
874                 return 0;
875
876         case SVR4_SFILLIM:
877                 {
878                         int error;
879                         struct __setrlimit_args srl;
880                         struct rlimit krl;
881                         caddr_t sg = stackgap_init();
882                         struct rlimit *url = (struct rlimit *) 
883                                 stackgap_alloc(&sg, sizeof *url);
884
885                         krl.rlim_cur = SCARG(uap, newlimit) * 512;
886                         krl.rlim_max = p->p_rlimit[RLIMIT_FSIZE].rlim_max;
887
888                         error = copyout(&krl, url, sizeof(*url));
889                         if (error)
890                                 return error;
891
892                         SCARG(&srl, which) = RLIMIT_FSIZE;
893                         SCARG(&srl, rlp) = (struct orlimit *)url;
894
895                         error = setrlimit(&srl);
896                         if (error)
897                                 return error;
898
899                         *retval = p->p_rlimit[RLIMIT_FSIZE].rlim_cur;
900                         if (*retval == -1)
901                                 *retval = 0x7fffffff;
902                         return 0;
903                 }
904
905         case SVR4_GMEMLIM:
906                 {
907                         struct vmspace *vm = p->p_vmspace;
908                         register_t r = p->p_rlimit[RLIMIT_DATA].rlim_cur;
909
910                         if (r == -1)
911                                 r = 0x7fffffff;
912                         r += (long) vm->vm_daddr;
913                         if (r < 0)
914                                 r = 0x7fffffff;
915                         *retval = r;
916                         return 0;
917                 }
918
919         case SVR4_GDESLIM:
920                 *retval = p->p_rlimit[RLIMIT_NOFILE].rlim_cur;
921                 if (*retval == -1)
922                         *retval = 0x7fffffff;
923                 return 0;
924
925         default:
926                 return EINVAL;
927         }
928 }
929
930 static struct proc *
931 svr4_pfind(pid)
932         pid_t pid;
933 {
934         struct proc *p;
935
936         /* look in the live processes */
937         if ((p = pfind(pid)) != NULL)
938                 return p;
939
940         /* look in the zombies */
941         for (p = zombproc.lh_first; p != 0; p = p->p_list.le_next)
942                 if (p->p_pid == pid)
943                         return p;
944
945         return NULL;
946 }
947
948
949 int
950 svr4_sys_pgrpsys(struct svr4_sys_pgrpsys_args *uap)
951 {
952         struct proc *p = curproc;
953         int *retval = p->p_retval;
954
955         switch (SCARG(uap, cmd)) {
956         case 1:                 /* setpgrp() */
957                 /*
958                  * SVR4 setpgrp() (which takes no arguments) has the
959                  * semantics that the session ID is also created anew, so
960                  * in almost every sense, setpgrp() is identical to
961                  * setsid() for SVR4.  (Under BSD, the difference is that
962                  * a setpgid(0,0) will not create a new session.)
963                  */
964                 setsid(NULL);
965                 /*FALLTHROUGH*/
966
967         case 0:                 /* getpgrp() */
968                 *retval = p->p_pgrp->pg_id;
969                 return 0;
970
971         case 2:                 /* getsid(pid) */
972                 if (SCARG(uap, pid) != 0 &&
973                     (p = svr4_pfind(SCARG(uap, pid))) == NULL)
974                         return ESRCH;
975                 /*
976                  * This has already been initialized to the pid of
977                  * the session leader.
978                  */
979                 *retval = (register_t) p->p_session->s_leader->p_pid;
980                 return 0;
981
982         case 3:                 /* setsid() */
983                 return setsid(NULL);
984
985         case 4:                 /* getpgid(pid) */
986
987                 if (SCARG(uap, pid) != 0 &&
988                     (p = svr4_pfind(SCARG(uap, pid))) == NULL)
989                         return ESRCH;
990
991                 *retval = (int) p->p_pgrp->pg_id;
992                 return 0;
993
994         case 5:                 /* setpgid(pid, pgid); */
995                 {
996                         struct setpgid_args sa;
997
998                         SCARG(&sa, pid) = SCARG(uap, pid);
999                         SCARG(&sa, pgid) = SCARG(uap, pgid);
1000                         return setpgid(&sa);
1001                 }
1002
1003         default:
1004                 return EINVAL;
1005         }
1006 }
1007
1008 #define syscallarg(x)   union { x datum; register_t pad; }
1009
1010 struct svr4_hrtcntl_args {
1011         int                     cmd;
1012         int                     fun;
1013         int                     clk;
1014         svr4_hrt_interval_t *   iv;
1015         svr4_hrt_time_t *       ti;
1016 };
1017
1018
1019 static int
1020 svr4_hrtcntl(struct svr4_hrtcntl_args *uap, register_t *retval)
1021 {
1022         switch (SCARG(uap, fun)) {
1023         case SVR4_HRT_CNTL_RES:
1024                 DPRINTF(("htrcntl(RES)\n"));
1025                 *retval = SVR4_HRT_USEC;
1026                 return 0;
1027
1028         case SVR4_HRT_CNTL_TOFD:
1029                 DPRINTF(("htrcntl(TOFD)\n"));
1030                 {
1031                         struct timeval tv;
1032                         svr4_hrt_time_t t;
1033                         if (SCARG(uap, clk) != SVR4_HRT_CLK_STD) {
1034                                 DPRINTF(("clk == %d\n", SCARG(uap, clk)));
1035                                 return EINVAL;
1036                         }
1037                         if (SCARG(uap, ti) == NULL) {
1038                                 DPRINTF(("ti NULL\n"));
1039                                 return EINVAL;
1040                         }
1041                         microtime(&tv);
1042                         t.h_sec = tv.tv_sec;
1043                         t.h_rem = tv.tv_usec;
1044                         t.h_res = SVR4_HRT_USEC;
1045                         return copyout(&t, SCARG(uap, ti), sizeof(t));
1046                 }
1047
1048         case SVR4_HRT_CNTL_START:
1049                 DPRINTF(("htrcntl(START)\n"));
1050                 return ENOSYS;
1051
1052         case SVR4_HRT_CNTL_GET:
1053                 DPRINTF(("htrcntl(GET)\n"));
1054                 return ENOSYS;
1055         default:
1056                 DPRINTF(("Bad htrcntl command %d\n", SCARG(uap, fun)));
1057                 return ENOSYS;
1058         }
1059 }
1060
1061
1062 int
1063 svr4_sys_hrtsys(struct svr4_sys_hrtsys_args *uap)
1064 {
1065         struct proc *p = curproc;
1066         int *retval = p->p_retval;
1067
1068         switch (SCARG(uap, cmd)) {
1069         case SVR4_HRT_CNTL:
1070                 return svr4_hrtcntl((struct svr4_hrtcntl_args *) uap, retval);
1071
1072         case SVR4_HRT_ALRM:
1073                 DPRINTF(("hrtalarm\n"));
1074                 return ENOSYS;
1075
1076         case SVR4_HRT_SLP:
1077                 DPRINTF(("hrtsleep\n"));
1078                 return ENOSYS;
1079
1080         case SVR4_HRT_CAN:
1081                 DPRINTF(("hrtcancel\n"));
1082                 return ENOSYS;
1083
1084         default:
1085                 DPRINTF(("Bad hrtsys command %d\n", SCARG(uap, cmd)));
1086                 return EINVAL;
1087         }
1088 }
1089
1090
1091 static int
1092 svr4_setinfo(p, st, s)
1093         struct proc *p;
1094         int st;
1095         svr4_siginfo_t *s;
1096 {
1097         svr4_siginfo_t i;
1098         int sig;
1099
1100         memset(&i, 0, sizeof(i));
1101
1102         i.si_signo = SVR4_SIGCHLD;
1103         i.si_errno = 0; /* XXX? */
1104
1105         if (p) {
1106                 i.si_pid = p->p_pid;
1107                 if (p->p_stat == SZOMB) {
1108                         i.si_stime = p->p_ru->ru_stime.tv_sec;
1109                         i.si_utime = p->p_ru->ru_utime.tv_sec;
1110                 }
1111                 else {
1112                         i.si_stime = p->p_stats->p_ru.ru_stime.tv_sec;
1113                         i.si_utime = p->p_stats->p_ru.ru_utime.tv_sec;
1114                 }
1115         }
1116
1117         if (WIFEXITED(st)) {
1118                 i.si_status = WEXITSTATUS(st);
1119                 i.si_code = SVR4_CLD_EXITED;
1120         } else if (WIFSTOPPED(st)) {
1121                 sig = WSTOPSIG(st);
1122                 if (sig >= 0 && sig < NSIG)
1123                         i.si_status = SVR4_BSD2SVR4_SIG(sig);
1124
1125                 if (i.si_status == SVR4_SIGCONT)
1126                         i.si_code = SVR4_CLD_CONTINUED;
1127                 else
1128                         i.si_code = SVR4_CLD_STOPPED;
1129         } else {
1130                 sig = WTERMSIG(st);
1131                 if (sig >= 0 && sig < NSIG)
1132                         i.si_status = SVR4_BSD2SVR4_SIG(sig);
1133
1134                 if (WCOREDUMP(st))
1135                         i.si_code = SVR4_CLD_DUMPED;
1136                 else
1137                         i.si_code = SVR4_CLD_KILLED;
1138         }
1139
1140         DPRINTF(("siginfo [pid %ld signo %d code %d errno %d status %d]\n",
1141                  i.si_pid, i.si_signo, i.si_code, i.si_errno, i.si_status));
1142
1143         return copyout(&i, s, sizeof(i));
1144 }
1145
1146
1147 int
1148 svr4_sys_waitsys(struct svr4_sys_waitsys_args *uap)
1149 {
1150         struct proc *p = curproc;
1151         int nfound;
1152         int error, *retval = p->p_retval;
1153         struct proc *q, *t;
1154
1155
1156         switch (SCARG(uap, grp)) {
1157         case SVR4_P_PID:        
1158                 break;
1159
1160         case SVR4_P_PGID:
1161                 SCARG(uap, id) = -p->p_pgid;
1162                 break;
1163
1164         case SVR4_P_ALL:
1165                 SCARG(uap, id) = WAIT_ANY;
1166                 break;
1167
1168         default:
1169                 return EINVAL;
1170         }
1171
1172         DPRINTF(("waitsys(%d, %d, %p, %x)\n", 
1173                  SCARG(uap, grp), SCARG(uap, id),
1174                  SCARG(uap, info), SCARG(uap, options)));
1175
1176 loop:
1177         nfound = 0;
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)));
1184                         continue;
1185                 }
1186                 nfound++;
1187                 if (q->p_stat == SZOMB && 
1188                     ((SCARG(uap, options) & (SVR4_WEXITED|SVR4_WTRAPPED)))) {
1189                         *retval = 0;
1190                         DPRINTF(("found %d\n", q->p_pid));
1191                         if ((error = svr4_setinfo(q, q->p_xstat,
1192                                                   SCARG(uap, info))) != 0)
1193                                 return error;
1194
1195
1196                         if ((SCARG(uap, options) & SVR4_WNOWAIT)) {
1197                                 DPRINTF(("Don't wait\n"));
1198                                 return 0;
1199                         }
1200
1201                         /*
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.
1208                          */
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);
1213                                 q->p_oppid = 0;
1214                                 q->p_flag &= ~(P_TRACED | P_WAITED);
1215                                 wakeup((caddr_t)q->p_pptr);
1216                                 return 0;
1217                         }
1218                         q->p_xstat = 0;
1219                         ruadd(&p->p_stats->p_cru, q->p_ru);
1220
1221                         FREE(q->p_ru, M_ZOMBIE);
1222
1223                         /*
1224                          * Finally finished with old proc entry.
1225                          * Unlink it from its process group and free it.
1226                          */
1227                         leavepgrp(q);
1228
1229                         LIST_REMOVE(q, p_list); /* off zombproc */
1230
1231                         LIST_REMOVE(q, p_sibling);
1232
1233                         /*
1234                          * Decrement the count of procs running with this uid.
1235                          */
1236                         (void)chgproccnt(q->p_ucred->cr_uidinfo, -1, 0);
1237
1238                         /*
1239                          * Free up credentials.
1240                          */
1241                         crfree(q->p_ucred);
1242                         q->p_ucred = NULL;
1243
1244                         /*
1245                          * Release reference to text vnode
1246                          */
1247                         if (q->p_textvp)
1248                                 vrele(q->p_textvp);
1249
1250                         /*
1251                          * Give machine-dependent layer a chance
1252                          * to free anything that cpu_exit couldn't
1253                          * release while still running in process context.
1254                          */
1255                         vm_waitproc(q);
1256                         /* XXX what about process 'q' itself?  zfree? */
1257 #if defined(__NetBSD__)
1258                         pool_put(&proc_pool, q);
1259 #endif
1260                         nprocs--;
1261                         return 0;
1262                 }
1263                 if (q->p_stat == SSTOP && (q->p_flag & P_WAITED) == 0 &&
1264                     (q->p_flag & P_TRACED ||
1265                      (SCARG(uap, options) & (SVR4_WSTOPPED|SVR4_WCONTINUED)))) {
1266                         DPRINTF(("jobcontrol %d\n", q->p_pid));
1267                         if (((SCARG(uap, options) & SVR4_WNOWAIT)) == 0)
1268                                 q->p_flag |= P_WAITED;
1269                         *retval = 0;
1270                         return svr4_setinfo(q, W_STOPCODE(q->p_xstat),
1271                                             SCARG(uap, info));
1272                 }
1273         }
1274
1275         if (nfound == 0)
1276                 return ECHILD;
1277
1278         if (SCARG(uap, options) & SVR4_WNOHANG) {
1279                 *retval = 0;
1280                 if ((error = svr4_setinfo(NULL, 0, SCARG(uap, info))) != 0)
1281                         return error;
1282                 return 0;
1283         }
1284
1285         if ((error = tsleep((caddr_t)p, PWAIT | PCATCH, "svr4_wait", 0)) != 0)
1286                 return error;
1287         goto loop;
1288 }
1289
1290
1291 static void
1292 bsd_statfs_to_svr4_statvfs(bfs, sfs)
1293         const struct statfs *bfs;
1294         struct svr4_statvfs *sfs;
1295 {
1296         sfs->f_bsize = bfs->f_iosize; /* XXX */
1297         sfs->f_frsize = bfs->f_bsize;
1298         sfs->f_blocks = bfs->f_blocks;
1299         sfs->f_bfree = bfs->f_bfree;
1300         sfs->f_bavail = bfs->f_bavail;
1301         sfs->f_files = bfs->f_files;
1302         sfs->f_ffree = bfs->f_ffree;
1303         sfs->f_favail = bfs->f_ffree;
1304         sfs->f_fsid = bfs->f_fsid.val[0];
1305         memcpy(sfs->f_basetype, bfs->f_fstypename, sizeof(sfs->f_basetype));
1306         sfs->f_flag = 0;
1307         if (bfs->f_flags & MNT_RDONLY)
1308                 sfs->f_flag |= SVR4_ST_RDONLY;
1309         if (bfs->f_flags & MNT_NOSUID)
1310                 sfs->f_flag |= SVR4_ST_NOSUID;
1311         sfs->f_namemax = MAXNAMLEN;
1312         memcpy(sfs->f_fstr, bfs->f_fstypename, sizeof(sfs->f_fstr)); /* XXX */
1313         memset(sfs->f_filler, 0, sizeof(sfs->f_filler));
1314 }
1315
1316
1317 static void
1318 bsd_statfs_to_svr4_statvfs64(bfs, sfs)
1319         const struct statfs *bfs;
1320         struct svr4_statvfs64 *sfs;
1321 {
1322         sfs->f_bsize = bfs->f_iosize; /* XXX */
1323         sfs->f_frsize = bfs->f_bsize;
1324         sfs->f_blocks = bfs->f_blocks;
1325         sfs->f_bfree = bfs->f_bfree;
1326         sfs->f_bavail = bfs->f_bavail;
1327         sfs->f_files = bfs->f_files;
1328         sfs->f_ffree = bfs->f_ffree;
1329         sfs->f_favail = bfs->f_ffree;
1330         sfs->f_fsid = bfs->f_fsid.val[0];
1331         memcpy(sfs->f_basetype, bfs->f_fstypename, sizeof(sfs->f_basetype));
1332         sfs->f_flag = 0;
1333         if (bfs->f_flags & MNT_RDONLY)
1334                 sfs->f_flag |= SVR4_ST_RDONLY;
1335         if (bfs->f_flags & MNT_NOSUID)
1336                 sfs->f_flag |= SVR4_ST_NOSUID;
1337         sfs->f_namemax = MAXNAMLEN;
1338         memcpy(sfs->f_fstr, bfs->f_fstypename, sizeof(sfs->f_fstr)); /* XXX */
1339         memset(sfs->f_filler, 0, sizeof(sfs->f_filler));
1340 }
1341
1342
1343 int
1344 svr4_sys_statvfs(struct svr4_sys_statvfs_args *uap)
1345 {
1346         struct statfs_args      fs_args;
1347         caddr_t sg = stackgap_init();
1348         struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1349         struct statfs bfs;
1350         struct svr4_statvfs sfs;
1351         int error;
1352
1353         CHECKALTEXIST(&sg, SCARG(uap, path));
1354         SCARG(&fs_args, path) = SCARG(uap, path);
1355         SCARG(&fs_args, buf) = fs;
1356
1357         if ((error = statfs(&fs_args)) != 0)
1358                 return error;
1359
1360         if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1361                 return error;
1362
1363         bsd_statfs_to_svr4_statvfs(&bfs, &sfs);
1364
1365         return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1366 }
1367
1368
1369 int
1370 svr4_sys_fstatvfs(struct svr4_sys_fstatvfs_args *uap)
1371 {
1372         struct fstatfs_args     fs_args;
1373         caddr_t sg = stackgap_init();
1374         struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1375         struct statfs bfs;
1376         struct svr4_statvfs sfs;
1377         int error;
1378
1379         SCARG(&fs_args, fd) = SCARG(uap, fd);
1380         SCARG(&fs_args, buf) = fs;
1381
1382         if ((error = fstatfs(&fs_args)) != 0)
1383                 return error;
1384
1385         if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1386                 return error;
1387
1388         bsd_statfs_to_svr4_statvfs(&bfs, &sfs);
1389
1390         return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1391 }
1392
1393
1394 int
1395 svr4_sys_statvfs64(struct svr4_sys_statvfs64_args *uap)
1396 {
1397         struct statfs_args      fs_args;
1398         caddr_t sg = stackgap_init();
1399         struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1400         struct statfs bfs;
1401         struct svr4_statvfs64 sfs;
1402         int error;
1403
1404         CHECKALTEXIST(&sg, SCARG(uap, path));
1405         SCARG(&fs_args, path) = SCARG(uap, path);
1406         SCARG(&fs_args, buf) = fs;
1407
1408         if ((error = statfs(&fs_args)) != 0)
1409                 return error;
1410
1411         if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1412                 return error;
1413
1414         bsd_statfs_to_svr4_statvfs64(&bfs, &sfs);
1415
1416         return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1417 }
1418
1419
1420 int
1421 svr4_sys_fstatvfs64(struct svr4_sys_fstatvfs64_args *uap)
1422 {
1423         struct fstatfs_args     fs_args;
1424         caddr_t sg = stackgap_init();
1425         struct statfs *fs = stackgap_alloc(&sg, sizeof(struct statfs));
1426         struct statfs bfs;
1427         struct svr4_statvfs64 sfs;
1428         int error;
1429
1430         SCARG(&fs_args, fd) = SCARG(uap, fd);
1431         SCARG(&fs_args, buf) = fs;
1432
1433         if ((error = fstatfs(&fs_args)) != 0)
1434                 return error;
1435
1436         if ((error = copyin(fs, &bfs, sizeof(bfs))) != 0)
1437                 return error;
1438
1439         bsd_statfs_to_svr4_statvfs64(&bfs, &sfs);
1440
1441         return copyout(&sfs, SCARG(uap, fs), sizeof(sfs));
1442 }
1443
1444 int
1445 svr4_sys_alarm(struct svr4_sys_alarm_args *uap)
1446 {
1447         struct proc *p = curproc;
1448         int error;
1449         struct itimerval *itp, *oitp;
1450         struct setitimer_args sa;
1451         caddr_t sg = stackgap_init();
1452
1453         itp = stackgap_alloc(&sg, sizeof(*itp));
1454         oitp = stackgap_alloc(&sg, sizeof(*oitp));
1455         timevalclear(&itp->it_interval);
1456         itp->it_value.tv_sec = SCARG(uap, sec);
1457         itp->it_value.tv_usec = 0;
1458
1459         SCARG(&sa, which) = ITIMER_REAL;
1460         SCARG(&sa, itv) = itp;
1461         SCARG(&sa, oitv) = oitp;
1462         error = setitimer(&sa);
1463         if (error)
1464                 return error;
1465         if (oitp->it_value.tv_usec)
1466                 oitp->it_value.tv_sec++;
1467         p->p_retval[0] = oitp->it_value.tv_sec;
1468         return 0;
1469
1470 }
1471
1472 int
1473 svr4_sys_gettimeofday(struct svr4_sys_gettimeofday_args *uap)
1474 {
1475         if (SCARG(uap, tp)) {
1476                 struct timeval atv;
1477
1478                 microtime(&atv);
1479                 return copyout(&atv, SCARG(uap, tp), sizeof (atv));
1480         }
1481
1482         return 0;
1483 }
1484
1485 int
1486 svr4_sys_facl(struct svr4_sys_facl_args *uap)
1487 {
1488         struct proc *p = curproc;
1489         int *retval;
1490
1491         retval = p->p_retval;
1492         *retval = 0;
1493
1494         switch (SCARG(uap, cmd)) {
1495         case SVR4_SYS_SETACL:
1496                 /* We don't support acls on any filesystem */
1497                 return ENOSYS;
1498
1499         case SVR4_SYS_GETACL:
1500                 return copyout(retval, &SCARG(uap, num),
1501                     sizeof(SCARG(uap, num)));
1502
1503         case SVR4_SYS_GETACLCNT:
1504                 return 0;
1505
1506         default:
1507                 return EINVAL;
1508         }
1509 }
1510
1511
1512 int
1513 svr4_sys_acl(struct svr4_sys_acl_args *uap)
1514 {
1515         /* XXX: for now the same */
1516         return svr4_sys_facl((struct svr4_sys_facl_args *)uap);
1517 }
1518
1519 int
1520 svr4_sys_auditsys(struct svr4_sys_auditsys_args *uap)
1521 {
1522         /*
1523          * XXX: Big brother is *not* watching.
1524          */
1525         return 0;
1526 }
1527
1528 int
1529 svr4_sys_memcntl(struct svr4_sys_memcntl_args *uap)
1530 {
1531         switch (SCARG(uap, cmd)) {
1532         case SVR4_MC_SYNC:
1533                 {
1534                         struct msync_args msa;
1535
1536                         SCARG(&msa, addr) = SCARG(uap, addr);
1537                         SCARG(&msa, len) = SCARG(uap, len);
1538                         SCARG(&msa, flags) = (int)SCARG(uap, arg);
1539
1540                         return msync(&msa);
1541                 }
1542         case SVR4_MC_ADVISE:
1543                 {
1544                         struct madvise_args maa;
1545
1546                         SCARG(&maa, addr) = SCARG(uap, addr);
1547                         SCARG(&maa, len) = SCARG(uap, len);
1548                         SCARG(&maa, behav) = (int)SCARG(uap, arg);
1549
1550                         return madvise(&maa);
1551                 }
1552         case SVR4_MC_LOCK:
1553         case SVR4_MC_UNLOCK:
1554         case SVR4_MC_LOCKAS:
1555         case SVR4_MC_UNLOCKAS:
1556                 return EOPNOTSUPP;
1557         default:
1558                 return ENOSYS;
1559         }
1560 }
1561
1562
1563 int
1564 svr4_sys_nice(struct svr4_sys_nice_args *uap)
1565 {
1566         struct setpriority_args ap;
1567         int error;
1568
1569         SCARG(&ap, which) = PRIO_PROCESS;
1570         SCARG(&ap, who) = 0;
1571         SCARG(&ap, prio) = SCARG(uap, prio);
1572
1573         if ((error = setpriority(&ap)) != 0)
1574                 return error;
1575
1576         /* the cast is stupid, but the structures are the same */
1577         if ((error = getpriority((struct getpriority_args *)&ap)) != 0)
1578                 return error;
1579
1580         return 0;
1581 }
1582
1583 int
1584 svr4_sys_resolvepath(struct svr4_sys_resolvepath_args *uap)
1585 {
1586         struct proc *p = curproc;
1587         struct nameidata nd;
1588         int error, *retval = p->p_retval;
1589
1590         NDINIT(&nd, LOOKUP, NOFOLLOW | SAVENAME, UIO_USERSPACE,
1591             SCARG(uap, path), p);
1592
1593         if ((error = namei(&nd)) != 0)
1594                 return error;
1595
1596         if ((error = copyout(nd.ni_cnd.cn_pnbuf, SCARG(uap, buf),
1597             SCARG(uap, bufsiz))) != 0)
1598                 goto bad;
1599
1600         *retval = strlen(nd.ni_cnd.cn_pnbuf) < SCARG(uap, bufsiz) ? 
1601           strlen(nd.ni_cnd.cn_pnbuf) + 1 : SCARG(uap, bufsiz);
1602 bad:
1603         NDFREE(&nd, NDF_ONLY_PNBUF);
1604         vput(nd.ni_vp);
1605         return error;
1606 }