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