Ansify function definitions.
[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.32 2005/12/10 16:06:20 swildner 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(register_t *retval, char *path, svr4_mode_t mode,
635            svr4_dev_t dev)
636 {
637         caddr_t sg = stackgap_init();
638         int error;
639
640         CHECKALTEXIST(&sg, path);
641
642         if (S_ISFIFO(mode)) {
643                 struct mkfifo_args ap;
644                 SCARG(&ap, path) = path;
645                 SCARG(&ap, mode) = mode;
646                 error = mkfifo(&ap);
647                 *retval = ap.sysmsg_result;
648         } else {
649                 struct mknod_args ap;
650                 SCARG(&ap, path) = path;
651                 SCARG(&ap, mode) = mode;
652                 SCARG(&ap, dev) = dev;
653                 error = mknod(&ap);
654                 *retval = ap.sysmsg_result;
655         }
656         return(error);
657 }
658
659
660 int
661 svr4_sys_mknod(struct svr4_sys_mknod_args *uap)
662 {
663         int *retval;
664
665         retval = &uap->sysmsg_result;
666
667         return svr4_mknod(retval, SCARG(uap, path), SCARG(uap, mode),
668                           (svr4_dev_t)svr4_to_bsd_odev_t(SCARG(uap, dev)));
669 }
670
671
672 int
673 svr4_sys_xmknod(struct svr4_sys_xmknod_args *uap)
674 {
675         int *retval;
676
677         retval = &uap->sysmsg_result;
678
679         return svr4_mknod(retval, SCARG(uap, path), SCARG(uap, mode),
680                           (svr4_dev_t)svr4_to_bsd_dev_t(SCARG(uap, dev)));
681 }
682
683
684 int
685 svr4_sys_vhangup(struct svr4_sys_vhangup_args *uap)
686 {
687         return 0;
688 }
689
690
691 int
692 svr4_sys_sysconfig(struct svr4_sys_sysconfig_args *uap)
693 {
694         int *retval;
695
696         retval = &uap->sysmsg_result;
697
698         switch (SCARG(uap, name)) {
699         case SVR4_CONFIG_UNUSED:
700                 *retval = 0;
701                 break;
702         case SVR4_CONFIG_NGROUPS:
703                 *retval = NGROUPS_MAX;
704                 break;
705         case SVR4_CONFIG_CHILD_MAX:
706                 *retval = maxproc;
707                 break;
708         case SVR4_CONFIG_OPEN_FILES:
709                 *retval = maxfiles;
710                 break;
711         case SVR4_CONFIG_POSIX_VER:
712                 *retval = 198808;
713                 break;
714         case SVR4_CONFIG_PAGESIZE:
715                 *retval = PAGE_SIZE;
716                 break;
717         case SVR4_CONFIG_CLK_TCK:
718                 *retval = 60;   /* should this be `hz', ie. 100? */
719                 break;
720         case SVR4_CONFIG_XOPEN_VER:
721                 *retval = 2;    /* XXX: What should that be? */
722                 break;
723         case SVR4_CONFIG_PROF_TCK:
724                 *retval = 60;   /* XXX: What should that be? */
725                 break;
726         case SVR4_CONFIG_NPROC_CONF:
727                 *retval = 1;    /* Only one processor for now */
728                 break;
729         case SVR4_CONFIG_NPROC_ONLN:
730                 *retval = 1;    /* And it better be online */
731                 break;
732         case SVR4_CONFIG_AIO_LISTIO_MAX:
733         case SVR4_CONFIG_AIO_MAX:
734         case SVR4_CONFIG_AIO_PRIO_DELTA_MAX:
735                 *retval = 0;    /* No aio support */
736                 break;
737         case SVR4_CONFIG_DELAYTIMER_MAX:
738                 *retval = 0;    /* No delaytimer support */
739                 break;
740         case SVR4_CONFIG_MQ_OPEN_MAX:
741                 *retval = msginfo.msgmni;
742                 break;
743         case SVR4_CONFIG_MQ_PRIO_MAX:
744                 *retval = 0;    /* XXX: Don't know */
745                 break;
746         case SVR4_CONFIG_RTSIG_MAX:
747                 *retval = 0;
748                 break;
749         case SVR4_CONFIG_SEM_NSEMS_MAX:
750                 *retval = seminfo.semmni;
751                 break;
752         case SVR4_CONFIG_SEM_VALUE_MAX:
753                 *retval = seminfo.semvmx;
754                 break;
755         case SVR4_CONFIG_SIGQUEUE_MAX:
756                 *retval = 0;    /* XXX: Don't know */
757                 break;
758         case SVR4_CONFIG_SIGRT_MIN:
759         case SVR4_CONFIG_SIGRT_MAX:
760                 *retval = 0;    /* No real time signals */
761                 break;
762         case SVR4_CONFIG_TIMER_MAX:
763                 *retval = 3;    /* XXX: real, virtual, profiling */
764                 break;
765 #if defined(NOTYET)
766         case SVR4_CONFIG_PHYS_PAGES:
767 #if defined(UVM)
768                 *retval = uvmexp.free;  /* XXX: free instead of total */
769 #else
770                 *retval = cnt.v_free_count;     /* XXX: free instead of total */
771 #endif
772                 break;
773         case SVR4_CONFIG_AVPHYS_PAGES:
774 #if defined(UVM)
775                 *retval = uvmexp.active;        /* XXX: active instead of avg */
776 #else
777                 *retval = cnt.v_active_count;   /* XXX: active instead of avg */
778 #endif
779                 break;
780 #endif /* NOTYET */
781
782         default:
783                 return EINVAL;
784         }
785         return 0;
786 }
787
788 extern int swap_pager_full;
789
790 /* ARGSUSED */
791 int
792 svr4_sys_break(struct svr4_sys_break_args *uap)
793 {
794         struct thread *td = curthread;  /* XXX */
795         struct proc *p = td->td_proc;
796         struct vmspace *vm;
797         vm_offset_t new, old, base, ns;
798         int rv;
799
800         KKASSERT(p);
801         vm = p->p_vmspace;
802
803         base = round_page((vm_offset_t) vm->vm_daddr);
804         ns = (vm_offset_t)SCARG(uap, nsize);
805         new = round_page(ns);
806         if (new > base) {
807           if ((new - base) > (unsigned) p->p_rlimit[RLIMIT_DATA].rlim_cur) {
808                         return ENOMEM;
809           }
810           if (new >= VM_MAXUSER_ADDRESS) {
811             return (ENOMEM);
812           }
813         } else if (new < base) {
814                 /*
815                  * This is simply an invalid value.  If someone wants to
816                  * do fancy address space manipulations, mmap and munmap
817                  * can do most of what the user would want.
818                  */
819                 return EINVAL;
820         }
821
822         old = base + ctob(vm->vm_dsize);
823
824         if (new > old) {
825                 vm_size_t diff;
826                 diff = new - old;
827                 if (vm->vm_map.size + diff > p->p_rlimit[RLIMIT_VMEM].rlim_cur)
828                         return(ENOMEM);
829                 rv = vm_map_find(&vm->vm_map, NULL, 0, &old, diff, FALSE,
830                         VM_PROT_ALL, VM_PROT_ALL, 0);
831                 if (rv != KERN_SUCCESS) {
832                         return (ENOMEM);
833                 }
834                 vm->vm_dsize += btoc(diff);
835         } else if (new < old) {
836                 rv = vm_map_remove(&vm->vm_map, new, old);
837                 if (rv != KERN_SUCCESS) {
838                         return (ENOMEM);
839                 }
840                 vm->vm_dsize -= btoc(old - new);
841         }
842
843         return (0);
844 }
845
846 static __inline clock_t
847 timeval_to_clock_t(struct timeval *tv)
848 {
849         return tv->tv_sec * hz + tv->tv_usec / (1000000 / hz);
850 }
851
852
853 int
854 svr4_sys_times(struct svr4_sys_times_args *uap)
855 {
856         int                      error, *retval = &uap->sysmsg_result;
857         struct tms               tms;
858         struct timeval           t;
859         struct rusage           *ru;
860         struct rusage            r;
861         struct getrusage_args    ga;
862
863         caddr_t sg = stackgap_init();
864         ru = stackgap_alloc(&sg, sizeof(struct rusage));
865
866         SCARG(&ga, who) = RUSAGE_SELF;
867         SCARG(&ga, rusage) = ru;
868
869         error = getrusage(&ga);
870         if (error)
871                 return error;
872
873         if ((error = copyin(ru, &r, sizeof r)) != 0)
874                 return error;
875
876         tms.tms_utime = timeval_to_clock_t(&r.ru_utime);
877         tms.tms_stime = timeval_to_clock_t(&r.ru_stime);
878
879         SCARG(&ga, who) = RUSAGE_CHILDREN;
880         error = getrusage(&ga);
881         if (error)
882                 return error;
883
884         if ((error = copyin(ru, &r, sizeof r)) != 0)
885                 return error;
886
887         tms.tms_cutime = timeval_to_clock_t(&r.ru_utime);
888         tms.tms_cstime = timeval_to_clock_t(&r.ru_stime);
889
890         microtime(&t);
891         *retval = timeval_to_clock_t(&t);
892
893         return copyout(&tms, SCARG(uap, tp), sizeof(tms));
894 }
895
896
897 int
898 svr4_sys_ulimit(struct svr4_sys_ulimit_args *uap)
899 {
900         struct proc *p = curproc;
901         int *retval = &uap->sysmsg_result;
902
903         switch (SCARG(uap, cmd)) {
904         case SVR4_GFILLIM:
905                 *retval = p->p_rlimit[RLIMIT_FSIZE].rlim_cur / 512;
906                 if (*retval == -1)
907                         *retval = 0x7fffffff;
908                 return 0;
909
910         case SVR4_SFILLIM:
911                 {
912                         int error;
913                         struct __setrlimit_args srl;
914                         struct rlimit krl;
915                         caddr_t sg = stackgap_init();
916                         struct rlimit *url = (struct rlimit *) 
917                                 stackgap_alloc(&sg, sizeof *url);
918
919                         krl.rlim_cur = SCARG(uap, newlimit) * 512;
920                         krl.rlim_max = p->p_rlimit[RLIMIT_FSIZE].rlim_max;
921
922                         error = copyout(&krl, url, sizeof(*url));
923                         if (error)
924                                 return error;
925
926                         SCARG(&srl, which) = RLIMIT_FSIZE;
927                         SCARG(&srl, rlp) = (struct rlimit *)url;
928
929                         error = setrlimit(&srl);
930                         if (error)
931                                 return error;
932
933                         *retval = p->p_rlimit[RLIMIT_FSIZE].rlim_cur;
934                         if (*retval == -1)
935                                 *retval = 0x7fffffff;
936                         return 0;
937                 }
938
939         case SVR4_GMEMLIM:
940                 {
941                         struct vmspace *vm = p->p_vmspace;
942                         register_t r = p->p_rlimit[RLIMIT_DATA].rlim_cur;
943
944                         if (r == -1)
945                                 r = 0x7fffffff;
946                         r += (long) vm->vm_daddr;
947                         if (r < 0)
948                                 r = 0x7fffffff;
949                         *retval = r;
950                         return 0;
951                 }
952
953         case SVR4_GDESLIM:
954                 *retval = p->p_rlimit[RLIMIT_NOFILE].rlim_cur;
955                 if (*retval == -1)
956                         *retval = 0x7fffffff;
957                 return 0;
958
959         default:
960                 return EINVAL;
961         }
962 }
963
964 static struct proc *
965 svr4_pfind(pid_t pid)
966 {
967         struct proc *p;
968
969         /* look in the live processes */
970         if ((p = pfind(pid)) != NULL)
971                 return p;
972
973         /* look in the zombies */
974         for (p = zombproc.lh_first; p != 0; p = p->p_list.le_next)
975                 if (p->p_pid == pid)
976                         return p;
977
978         return NULL;
979 }
980
981
982 int
983 svr4_sys_pgrpsys(struct svr4_sys_pgrpsys_args *uap)
984 {
985         struct proc *p = curproc;
986         int *retval = &uap->sysmsg_result;
987
988         switch (SCARG(uap, cmd)) {
989         case 1:                 /* setpgrp() */
990                 /*
991                  * SVR4 setpgrp() (which takes no arguments) has the
992                  * semantics that the session ID is also created anew, so
993                  * in almost every sense, setpgrp() is identical to
994                  * setsid() for SVR4.  (Under BSD, the difference is that
995                  * a setpgid(0,0) will not create a new session.)
996                  */
997                 {
998                         struct setsid_args sida;
999
1000                         sida.sysmsg_result = 0;
1001                         setsid(&sida);
1002                         /* ignore result? */
1003                 }
1004                 /*FALLTHROUGH*/
1005
1006         case 0:                 /* getpgrp() */
1007                 *retval = p->p_pgrp->pg_id;
1008                 return 0;
1009
1010         case 2:                 /* getsid(pid) */
1011                 if (SCARG(uap, pid) != 0 &&
1012                     (p = svr4_pfind(SCARG(uap, pid))) == NULL)
1013                         return ESRCH;
1014                 /*
1015                  * This has already been initialized to the pid of
1016                  * the session leader.
1017                  */
1018                 *retval = (register_t) p->p_session->s_leader->p_pid;
1019                 return 0;
1020
1021         case 3:                 /* setsid() */
1022                 {
1023                         
1024                         struct setsid_args sida;
1025                         int error;
1026
1027                         sida.sysmsg_result = 0;
1028                         error = setsid(&sida);
1029                         uap->sysmsg_result = sida.sysmsg_result;
1030                         return(error);
1031                 }
1032         case 4:                 /* getpgid(pid) */
1033
1034                 if (SCARG(uap, pid) != 0 &&
1035                     (p = svr4_pfind(SCARG(uap, pid))) == NULL)
1036                         return ESRCH;
1037
1038                 *retval = (int) p->p_pgrp->pg_id;
1039                 return 0;
1040
1041         case 5:                 /* setpgid(pid, pgid); */
1042                 {
1043                         struct setpgid_args sa;
1044                         int error;
1045
1046                         SCARG(&sa, pid) = SCARG(uap, pid);
1047                         SCARG(&sa, pgid) = SCARG(uap, pgid);
1048                         sa.sysmsg_result = 0;
1049                         error = setpgid(&sa);
1050                         uap->sysmsg_result = sa.sysmsg_result;
1051                 }
1052
1053         default:
1054                 return EINVAL;
1055         }
1056 }
1057
1058 #define syscallarg(x)   union { x datum; register_t pad; }
1059
1060 struct svr4_hrtcntl_args {
1061         int                     cmd;
1062         int                     fun;
1063         int                     clk;
1064         svr4_hrt_interval_t *   iv;
1065         svr4_hrt_time_t *       ti;
1066 };
1067
1068
1069 static int
1070 svr4_hrtcntl(struct svr4_hrtcntl_args *uap, register_t *retval)
1071 {
1072         switch (SCARG(uap, fun)) {
1073         case SVR4_HRT_CNTL_RES:
1074                 DPRINTF(("htrcntl(RES)\n"));
1075                 *retval = SVR4_HRT_USEC;
1076                 return 0;
1077
1078         case SVR4_HRT_CNTL_TOFD:
1079                 DPRINTF(("htrcntl(TOFD)\n"));
1080                 {
1081                         struct timeval tv;
1082                         svr4_hrt_time_t t;
1083                         if (SCARG(uap, clk) != SVR4_HRT_CLK_STD) {
1084                                 DPRINTF(("clk == %d\n", SCARG(uap, clk)));
1085                                 return EINVAL;
1086                         }
1087                         if (SCARG(uap, ti) == NULL) {
1088                                 DPRINTF(("ti NULL\n"));
1089                                 return EINVAL;
1090                         }
1091                         microtime(&tv);
1092                         t.h_sec = tv.tv_sec;
1093                         t.h_rem = tv.tv_usec;
1094                         t.h_res = SVR4_HRT_USEC;
1095                         return copyout(&t, SCARG(uap, ti), sizeof(t));
1096                 }
1097
1098         case SVR4_HRT_CNTL_START:
1099                 DPRINTF(("htrcntl(START)\n"));
1100                 return ENOSYS;
1101
1102         case SVR4_HRT_CNTL_GET:
1103                 DPRINTF(("htrcntl(GET)\n"));
1104                 return ENOSYS;
1105         default:
1106                 DPRINTF(("Bad htrcntl command %d\n", SCARG(uap, fun)));
1107                 return ENOSYS;
1108         }
1109 }
1110
1111
1112 int
1113 svr4_sys_hrtsys(struct svr4_sys_hrtsys_args *uap)
1114 {
1115         int *retval = &uap->sysmsg_result;
1116
1117         switch (SCARG(uap, cmd)) {
1118         case SVR4_HRT_CNTL:
1119                 return svr4_hrtcntl((struct svr4_hrtcntl_args *)(&uap->sysmsg + 1), retval);
1120
1121         case SVR4_HRT_ALRM:
1122                 DPRINTF(("hrtalarm\n"));
1123                 return ENOSYS;
1124
1125         case SVR4_HRT_SLP:
1126                 DPRINTF(("hrtsleep\n"));
1127                 return ENOSYS;
1128
1129         case SVR4_HRT_CAN:
1130                 DPRINTF(("hrtcancel\n"));
1131                 return ENOSYS;
1132
1133         default:
1134                 DPRINTF(("Bad hrtsys command %d\n", SCARG(uap, cmd)));
1135                 return EINVAL;
1136         }
1137 }
1138
1139
1140 static int
1141 svr4_setinfo(struct proc *p, int st, svr4_siginfo_t *s)
1142 {
1143         svr4_siginfo_t i;
1144         int sig;
1145
1146         memset(&i, 0, sizeof(i));
1147
1148         i.si_signo = SVR4_SIGCHLD;
1149         i.si_errno = 0; /* XXX? */
1150
1151         if (p) {
1152                 i.si_pid = p->p_pid;
1153                 if (p->p_flag & P_ZOMBIE) {
1154                         i.si_stime = p->p_ru->ru_stime.tv_sec;
1155                         i.si_utime = p->p_ru->ru_utime.tv_sec;
1156                 } else {
1157                         i.si_stime = p->p_stats->p_ru.ru_stime.tv_sec;
1158                         i.si_utime = p->p_stats->p_ru.ru_utime.tv_sec;
1159                 }
1160         }
1161
1162         if (WIFEXITED(st)) {
1163                 i.si_status = WEXITSTATUS(st);
1164                 i.si_code = SVR4_CLD_EXITED;
1165         } else if (WIFSTOPPED(st)) {
1166                 sig = WSTOPSIG(st);
1167                 if (sig >= 0 && sig < NSIG)
1168                         i.si_status = SVR4_BSD2SVR4_SIG(sig);
1169
1170                 if (i.si_status == SVR4_SIGCONT)
1171                         i.si_code = SVR4_CLD_CONTINUED;
1172                 else
1173                         i.si_code = SVR4_CLD_STOPPED;
1174         } else {
1175                 sig = WTERMSIG(st);
1176                 if (sig >= 0 && sig < NSIG)
1177                         i.si_status = SVR4_BSD2SVR4_SIG(sig);
1178
1179                 if (WCOREDUMP(st))
1180                         i.si_code = SVR4_CLD_DUMPED;
1181                 else
1182                         i.si_code = SVR4_CLD_KILLED;
1183         }
1184
1185         DPRINTF(("siginfo [pid %ld signo %d code %d errno %d status %d]\n",
1186                  i.si_pid, i.si_signo, i.si_code, i.si_errno, i.si_status));
1187
1188         return copyout(&i, s, sizeof(i));
1189 }
1190
1191
1192 int
1193 svr4_sys_waitsys(struct svr4_sys_waitsys_args *uap)
1194 {
1195         struct proc *p = curproc;
1196         int nfound;
1197         int error, *retval = &uap->sysmsg_result;
1198         struct proc *q, *t;
1199
1200
1201         switch (SCARG(uap, grp)) {
1202         case SVR4_P_PID:        
1203                 break;
1204
1205         case SVR4_P_PGID:
1206                 SCARG(uap, id) = -p->p_pgid;
1207                 break;
1208
1209         case SVR4_P_ALL:
1210                 SCARG(uap, id) = WAIT_ANY;
1211                 break;
1212
1213         default:
1214                 return EINVAL;
1215         }
1216
1217         DPRINTF(("waitsys(%d, %d, %p, %x)\n", 
1218                  SCARG(uap, grp), SCARG(uap, id),
1219                  SCARG(uap, info), SCARG(uap, options)));
1220
1221 loop:
1222         nfound = 0;
1223         for (q = p->p_children.lh_first; q != 0; q = q->p_sibling.le_next) {
1224                 if (SCARG(uap, id) != WAIT_ANY &&
1225                     q->p_pid != SCARG(uap, id) &&
1226                     q->p_pgid != -SCARG(uap, id)) {
1227                         DPRINTF(("pid %d pgid %d != %d\n", q->p_pid,
1228                                  q->p_pgid, SCARG(uap, id)));
1229                         continue;
1230                 }
1231                 nfound++;
1232                 if ((q->p_flag & P_ZOMBIE) && 
1233                     ((SCARG(uap, options) & (SVR4_WEXITED|SVR4_WTRAPPED)))) {
1234                         *retval = 0;
1235                         DPRINTF(("found %d\n", q->p_pid));
1236                         if ((error = svr4_setinfo(q, q->p_xstat,
1237                                                   SCARG(uap, info))) != 0)
1238                                 return error;
1239
1240
1241                         if ((SCARG(uap, options) & SVR4_WNOWAIT)) {
1242                                 DPRINTF(("Don't wait\n"));
1243                                 return 0;
1244                         }
1245
1246                         /*
1247                          * If we got the child via ptrace(2) or procfs, and
1248                          * the parent is different (meaning the process was
1249                          * attached, rather than run as a child), then we need
1250                          * to give it back to the ol dparent, and send the
1251                          * parent a SIGCHLD.  The rest of the cleanup will be
1252                          * done when the old parent waits on the child.
1253                          */
1254                         if ((q->p_flag & P_TRACED) &&
1255                             q->p_oppid != q->p_pptr->p_pid) {
1256                                 t = pfind(q->p_oppid);
1257                                 proc_reparent(q, t ? t : initproc);
1258                                 q->p_oppid = 0;
1259                                 q->p_flag &= ~(P_TRACED | P_WAITED);
1260                                 wakeup((caddr_t)q->p_pptr);
1261                                 return 0;
1262                         }
1263                         q->p_xstat = 0;
1264                         ruadd(&p->p_stats->p_cru, q->p_ru);
1265
1266                         FREE(q->p_ru, M_ZOMBIE);
1267
1268                         /*
1269                          * Finally finished with old proc entry.
1270                          * Unlink it from its process group and free it.
1271                          */
1272                         leavepgrp(q);
1273
1274                         LIST_REMOVE(q, p_list); /* off zombproc */
1275
1276                         LIST_REMOVE(q, p_sibling);
1277
1278                         /*
1279                          * Decrement the count of procs running with this uid.
1280                          */
1281                         (void)chgproccnt(q->p_ucred->cr_uidinfo, -1, 0);
1282
1283                         /*
1284                          * Free up credentials.
1285                          */
1286                         crfree(q->p_ucred);
1287                         q->p_ucred = NULL;
1288
1289                         /*
1290                          * Release reference to text vnode
1291                          */
1292                         if (q->p_textvp)
1293                                 vrele(q->p_textvp);
1294
1295                         /*
1296                          * Give machine-dependent layer a chance
1297                          * to free anything that cpu_exit couldn't
1298                          * release while still running in process context.
1299                          */
1300                         vm_waitproc(q);
1301                         /* XXX what about process 'q' itself?  zfree? */
1302 #if defined(__NetBSD__)
1303                         pool_put(&proc_pool, q);
1304 #endif
1305                         nprocs--;
1306                         return 0;
1307                 }
1308                 if ((q->p_flag & P_STOPPED) && (q->p_flag & P_WAITED) == 0 &&
1309                     (q->p_flag & P_TRACED ||
1310                      (SCARG(uap, options) & (SVR4_WSTOPPED|SVR4_WCONTINUED)))) {
1311                         DPRINTF(("jobcontrol %d\n", q->p_pid));
1312                         if (((SCARG(uap, options) & SVR4_WNOWAIT)) == 0)
1313                                 q->p_flag |= P_WAITED;
1314                         *retval = 0;
1315                         return svr4_setinfo(q, W_STOPCODE(q->p_xstat),
1316                                             SCARG(uap, info));
1317                 }
1318         }
1319
1320         if (nfound == 0)
1321                 return ECHILD;
1322
1323         if (SCARG(uap, options) & SVR4_WNOHANG) {
1324                 *retval = 0;
1325                 if ((error = svr4_setinfo(NULL, 0, SCARG(uap, info))) != 0)
1326                         return error;
1327                 return 0;
1328         }
1329
1330         if ((error = tsleep((caddr_t)p, PCATCH, "svr4_wait", 0)) != 0)
1331                 return error;
1332         goto loop;
1333 }
1334
1335
1336 static void
1337 bsd_statfs_to_svr4_statvfs(const struct statfs *bfs, struct svr4_statvfs *sfs,
1338     int namelen)
1339 {
1340         sfs->f_bsize = bfs->f_iosize; /* XXX */
1341         sfs->f_frsize = bfs->f_bsize;
1342         sfs->f_blocks = bfs->f_blocks;
1343         sfs->f_bfree = bfs->f_bfree;
1344         sfs->f_bavail = bfs->f_bavail;
1345         sfs->f_files = bfs->f_files;
1346         sfs->f_ffree = bfs->f_ffree;
1347         sfs->f_favail = bfs->f_ffree;
1348         sfs->f_fsid = bfs->f_fsid.val[0];
1349         memcpy(sfs->f_basetype, bfs->f_fstypename, sizeof(sfs->f_basetype));
1350         sfs->f_flag = 0;
1351         if (bfs->f_flags & MNT_RDONLY)
1352                 sfs->f_flag |= SVR4_ST_RDONLY;
1353         if (bfs->f_flags & MNT_NOSUID)
1354                 sfs->f_flag |= SVR4_ST_NOSUID;
1355         sfs->f_namemax = namelen;
1356         memcpy(sfs->f_fstr, bfs->f_fstypename, sizeof(sfs->f_fstr)); /* XXX */
1357         memset(sfs->f_filler, 0, sizeof(sfs->f_filler));
1358 }
1359
1360
1361 static void
1362 bsd_statfs_to_svr4_statvfs64(const struct statfs *bfs,
1363     struct svr4_statvfs64 *sfs, int namelen)
1364 {
1365         sfs->f_bsize = bfs->f_iosize; /* XXX */
1366         sfs->f_frsize = bfs->f_bsize;
1367         sfs->f_blocks = bfs->f_blocks;
1368         sfs->f_bfree = bfs->f_bfree;
1369         sfs->f_bavail = bfs->f_bavail;
1370         sfs->f_files = bfs->f_files;
1371         sfs->f_ffree = bfs->f_ffree;
1372         sfs->f_favail = bfs->f_ffree;
1373         sfs->f_fsid = bfs->f_fsid.val[0];
1374         memcpy(sfs->f_basetype, bfs->f_fstypename, sizeof(sfs->f_basetype));
1375         sfs->f_flag = 0;
1376         if (bfs->f_flags & MNT_RDONLY)
1377                 sfs->f_flag |= SVR4_ST_RDONLY;
1378         if (bfs->f_flags & MNT_NOSUID)
1379                 sfs->f_flag |= SVR4_ST_NOSUID;
1380         sfs->f_namemax = namelen;
1381         memcpy(sfs->f_fstr, bfs->f_fstypename, sizeof(sfs->f_fstr)); /* XXX */
1382         memset(sfs->f_filler, 0, sizeof(sfs->f_filler));
1383 }
1384
1385
1386 int
1387 svr4_sys_statvfs(struct svr4_sys_statvfs_args *uap)
1388 {
1389         struct nlookupdata nd;
1390         struct svr4_statvfs sfs;
1391         struct statfs bfs;
1392         int error, namelen;
1393
1394         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1395         if (error == 0)
1396                 error = kern_statfs(&nd, &bfs);
1397         if (error == 0) {
1398                 if (nd.nl_ncp->nc_vp != NULL)
1399                         error = vn_get_namelen(nd.nl_ncp->nc_vp, &namelen);
1400                 else
1401                         error = EINVAL;
1402         }
1403         nlookup_done(&nd);
1404         if (error == 0) {
1405                 bsd_statfs_to_svr4_statvfs(&bfs, &sfs, namelen);
1406                 error = copyout(&sfs, uap->fs, sizeof(*uap->fs));
1407         }
1408
1409         return (error);
1410 }
1411
1412
1413 int
1414 svr4_sys_fstatvfs(struct svr4_sys_fstatvfs_args *uap)
1415 {
1416         struct svr4_statvfs sfs;
1417         struct statfs bfs;
1418         struct proc *p = curthread->td_proc;
1419         struct file *fp;
1420         int error, namelen;
1421
1422         error = kern_fstatfs(uap->fd, &bfs);
1423
1424         if (error == 0)
1425                 error = getvnode(p->p_fd, uap->fd, &fp);
1426         if (error == 0)
1427                 error = vn_get_namelen((struct vnode *)fp->f_data, &namelen);
1428         if (error == 0) {
1429                 bsd_statfs_to_svr4_statvfs(&bfs, &sfs, namelen);
1430                 error = copyout(&bfs, uap->fs, sizeof(*uap->fs));
1431         }
1432
1433         return (error);
1434 }
1435
1436
1437 int
1438 svr4_sys_statvfs64(struct svr4_sys_statvfs64_args *uap)
1439 {
1440         struct nlookupdata nd;
1441         struct svr4_statvfs64 sfs;
1442         struct statfs bfs;
1443         int error, namelen;
1444
1445         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1446         if (error == 0)
1447                 error = kern_statfs(&nd, &bfs);
1448         if (error == 0) {
1449                 if (nd.nl_ncp->nc_vp != NULL)
1450                         error = vn_get_namelen(nd.nl_ncp->nc_vp, &namelen);
1451                 else
1452                         error = EINVAL;
1453         }
1454         nlookup_done(&nd);
1455         if (error == 0) {
1456                 bsd_statfs_to_svr4_statvfs64(&bfs, &sfs, namelen);
1457                 error = copyout(&sfs, uap->fs, sizeof(*uap->fs));
1458         }
1459
1460         return (error);
1461 }
1462
1463
1464 int
1465 svr4_sys_fstatvfs64(struct svr4_sys_fstatvfs64_args *uap)
1466 {
1467         struct svr4_statvfs64 sfs;
1468         struct statfs bfs;
1469         struct proc *p = curthread->td_proc;
1470         struct file *fp;
1471         int error, namelen;
1472
1473         error = kern_fstatfs(uap->fd, &bfs);
1474
1475         if (error == 0)
1476                 error = getvnode(p->p_fd, uap->fd, &fp);
1477         if (error == 0)
1478                 error = vn_get_namelen((struct vnode *)fp->f_data, &namelen);
1479         if (error == 0) {
1480                 bsd_statfs_to_svr4_statvfs64(&bfs, &sfs, namelen);
1481                 error = copyout(&bfs, uap->fs, sizeof(*uap->fs));
1482         }
1483
1484         return (error);
1485 }
1486
1487 int
1488 svr4_sys_alarm(struct svr4_sys_alarm_args *uap)
1489 {
1490         int error;
1491         struct itimerval *itp, *oitp;
1492         struct setitimer_args sa;
1493         caddr_t sg = stackgap_init();
1494
1495         itp = stackgap_alloc(&sg, sizeof(*itp));
1496         oitp = stackgap_alloc(&sg, sizeof(*oitp));
1497         timevalclear(&itp->it_interval);
1498         itp->it_value.tv_sec = SCARG(uap, sec);
1499         itp->it_value.tv_usec = 0;
1500
1501         SCARG(&sa, which) = ITIMER_REAL;
1502         SCARG(&sa, itv) = itp;
1503         SCARG(&sa, oitv) = oitp;
1504         error = setitimer(&sa);
1505         if (error)
1506                 return error;
1507         if (oitp->it_value.tv_usec)
1508                 oitp->it_value.tv_sec++;
1509         uap->sysmsg_result = oitp->it_value.tv_sec;
1510         return 0;
1511
1512 }
1513
1514 int
1515 svr4_sys_gettimeofday(struct svr4_sys_gettimeofday_args *uap)
1516 {
1517         if (SCARG(uap, tp)) {
1518                 struct timeval atv;
1519
1520                 microtime(&atv);
1521                 return copyout(&atv, SCARG(uap, tp), sizeof (atv));
1522         }
1523
1524         return 0;
1525 }
1526
1527 int
1528 svr4_sys_facl(struct svr4_sys_facl_args *uap)
1529 {
1530         int *retval;
1531
1532         retval = &uap->sysmsg_result;
1533         *retval = 0;
1534
1535         switch (SCARG(uap, cmd)) {
1536         case SVR4_SYS_SETACL:
1537                 /* We don't support acls on any filesystem */
1538                 return ENOSYS;
1539
1540         case SVR4_SYS_GETACL:
1541                 return copyout(retval, &SCARG(uap, num),
1542                     sizeof(SCARG(uap, num)));
1543
1544         case SVR4_SYS_GETACLCNT:
1545                 return 0;
1546
1547         default:
1548                 return EINVAL;
1549         }
1550 }
1551
1552
1553 int
1554 svr4_sys_acl(struct svr4_sys_acl_args *uap)
1555 {
1556         /* XXX: for now the same */
1557         return svr4_sys_facl((struct svr4_sys_facl_args *)uap);
1558 }
1559
1560 int
1561 svr4_sys_auditsys(struct svr4_sys_auditsys_args *uap)
1562 {
1563         /*
1564          * XXX: Big brother is *not* watching.
1565          */
1566         return 0;
1567 }
1568
1569 int
1570 svr4_sys_memcntl(struct svr4_sys_memcntl_args *uap)
1571 {
1572         switch (SCARG(uap, cmd)) {
1573         case SVR4_MC_SYNC:
1574                 {
1575                         struct msync_args msa;
1576                         int error;
1577
1578                         SCARG(&msa, addr) = SCARG(uap, addr);
1579                         SCARG(&msa, len) = SCARG(uap, len);
1580                         SCARG(&msa, flags) = (int)SCARG(uap, arg);
1581
1582                         msa.sysmsg_result = 0;
1583                         error = msync(&msa);
1584                         uap->sysmsg_result = msa.sysmsg_result;
1585                         return(error);
1586                 }
1587         case SVR4_MC_ADVISE:
1588                 {
1589                         struct madvise_args maa;
1590                         int error;
1591
1592                         SCARG(&maa, addr) = SCARG(uap, addr);
1593                         SCARG(&maa, len) = SCARG(uap, len);
1594                         SCARG(&maa, behav) = (int)SCARG(uap, arg);
1595
1596                         maa.sysmsg_result = 0;
1597                         error = madvise(&maa);
1598                         uap->sysmsg_result = maa.sysmsg_result;
1599                         return(error);
1600                 }
1601         case SVR4_MC_LOCK:
1602         case SVR4_MC_UNLOCK:
1603         case SVR4_MC_LOCKAS:
1604         case SVR4_MC_UNLOCKAS:
1605                 return EOPNOTSUPP;
1606         default:
1607                 return ENOSYS;
1608         }
1609 }
1610
1611
1612 int
1613 svr4_sys_nice(struct svr4_sys_nice_args *uap)
1614 {
1615         struct setpriority_args ap;
1616         int error;
1617
1618         SCARG(&ap, which) = PRIO_PROCESS;
1619         SCARG(&ap, who) = 0;
1620         SCARG(&ap, prio) = SCARG(uap, prio);
1621
1622         if ((error = setpriority(&ap)) != 0)
1623                 return error;
1624
1625         /* the cast is stupid, but the structures are the same */
1626         if ((error = getpriority((struct getpriority_args *)&ap)) != 0)
1627                 return error;
1628
1629         uap->sysmsg_result = ap.sysmsg_result;
1630
1631         return 0;
1632 }
1633
1634 int
1635 svr4_sys_resolvepath(struct svr4_sys_resolvepath_args *uap)
1636 {
1637         struct nlookupdata nd;
1638         int error;
1639         int *retval;
1640
1641         retval = &uap->sysmsg_result;
1642
1643         error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, 0);
1644         if (error == 0)
1645                 error = nlookup(&nd);
1646         if (error)
1647                 goto bad;
1648
1649         if ((error = copyout(nd.nl_path, SCARG(uap, buf),
1650                                 SCARG(uap, bufsiz))) != 0) {
1651                 goto bad;
1652         }
1653
1654         *retval = strlen(nd.nl_path) < SCARG(uap, bufsiz) ? 
1655                   strlen(nd.nl_path) + 1 : SCARG(uap, bufsiz);
1656 bad:
1657         nlookup_done(&nd);
1658         return (error);
1659 }