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