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