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