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