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