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