]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/svr4/svr4_misc.c
This commit was generated by cvs2svn to compensate for changes in r161537,
[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 (pid_t , struct rusage *, 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, vfslocked;
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         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
260         if (vp->v_type != VDIR) {
261                 VFS_UNLOCK_GIANT(vfslocked);
262                 fdrop(fp, td);
263                 return (EINVAL);
264         }
265
266         nbytes = uap->nbytes;
267         if (nbytes == 1) {
268                 nbytes = sizeof (struct svr4_dirent64);
269                 justone = 1;
270         }
271         else
272                 justone = 0;
273
274         off = fp->f_offset;
275 #define DIRBLKSIZ       512             /* XXX we used to use ufs's DIRBLKSIZ */
276         buflen = max(DIRBLKSIZ, nbytes);
277         buflen = min(buflen, MAXBSIZE);
278         buf = malloc(buflen, M_TEMP, M_WAITOK);
279         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
280 again:
281         aiov.iov_base = buf;
282         aiov.iov_len = buflen;
283         auio.uio_iov = &aiov;
284         auio.uio_iovcnt = 1;
285         auio.uio_rw = UIO_READ;
286         auio.uio_segflg = UIO_SYSSPACE;
287         auio.uio_td = td;
288         auio.uio_resid = buflen;
289         auio.uio_offset = off;
290
291         if (cookies) {
292                 free(cookies, M_TEMP);
293                 cookies = NULL;
294         }
295
296 #ifdef MAC
297         error = mac_check_vnode_readdir(td->td_ucred, vp);
298         if (error)
299                 goto out;
300 #endif
301
302         error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
303                                                 &ncookies, &cookies);
304         if (error) {
305                 goto out;
306         }
307
308         inp = buf;
309         outp = (caddr_t) uap->dp;
310         resid = nbytes;
311         if ((len = buflen - auio.uio_resid) <= 0) {
312                 goto eof;
313         }
314
315         cookiep = cookies;
316
317         if (cookies) {
318                 /*
319                  * When using cookies, the vfs has the option of reading from
320                  * a different offset than that supplied (UFS truncates the
321                  * offset to a block boundary to make sure that it never reads
322                  * partway through a directory entry, even if the directory
323                  * has been compacted).
324                  */
325                 while (len > 0 && ncookies > 0 && *cookiep <= off) {
326                         bdp = (struct dirent *) inp;
327                         len -= bdp->d_reclen;
328                         inp += bdp->d_reclen;
329                         cookiep++;
330                         ncookies--;
331                 }
332         }
333
334         while (len > 0) {
335                 if (cookiep && ncookies == 0)
336                         break;
337                 bdp = (struct dirent *) inp;
338                 reclen = bdp->d_reclen;
339                 if (reclen & 3) {
340                         DPRINTF(("svr4_readdir: reclen=%d\n", reclen));
341                         error = EFAULT;
342                         goto out;
343                 }
344   
345                 if (bdp->d_fileno == 0) {
346                         inp += reclen;
347                         if (cookiep) {
348                                 off = *cookiep++;
349                                 ncookies--;
350                         } else
351                                 off += reclen;
352                         len -= reclen;
353                         continue;
354                 }
355                 svr4reclen = SVR4_RECLEN(&svr4_dirent, bdp->d_namlen);
356                 if (reclen > len || resid < svr4reclen) {
357                         outp++;
358                         break;
359                 }
360                 svr4_dirent.d_ino = (long) bdp->d_fileno;
361                 if (justone) {
362                         /*
363                          * old svr4-style readdir usage.
364                          */
365                         svr4_dirent.d_off = (svr4_off_t) svr4reclen;
366                         svr4_dirent.d_reclen = (u_short) bdp->d_namlen;
367                 } else {
368                         svr4_dirent.d_off = (svr4_off_t)(off + reclen);
369                         svr4_dirent.d_reclen = (u_short) svr4reclen;
370                 }
371                 strcpy(svr4_dirent.d_name, bdp->d_name);
372                 if ((error = copyout((caddr_t)&svr4_dirent, outp, svr4reclen)))
373                         goto out;
374                 inp += reclen;
375                 if (cookiep) {
376                         off = *cookiep++;
377                         ncookies--;
378                 } else
379                         off += reclen;
380                 outp += svr4reclen;
381                 resid -= svr4reclen;
382                 len -= reclen;
383                 if (justone)
384                         break;
385         }
386
387         if (outp == (caddr_t) uap->dp)
388                 goto again;
389         fp->f_offset = off;
390
391         if (justone)
392                 nbytes = resid + svr4reclen;
393
394 eof:
395         td->td_retval[0] = nbytes - resid;
396 out:
397         VOP_UNLOCK(vp, 0, td);
398         VFS_UNLOCK_GIANT(vfslocked);
399         fdrop(fp, td);
400         if (cookies)
401                 free(cookies, M_TEMP);
402         free(buf, M_TEMP);
403         return error;
404 }
405
406
407 int
408 svr4_sys_getdents(td, uap)
409         struct thread *td;
410         struct svr4_sys_getdents_args *uap;
411 {
412         struct dirent *bdp;
413         struct vnode *vp;
414         caddr_t inp, buf;       /* BSD-format */
415         int len, reclen;        /* BSD-format */
416         caddr_t outp;           /* SVR4-format */
417         int resid, svr4_reclen; /* SVR4-format */
418         struct file *fp;
419         struct uio auio;
420         struct iovec aiov;
421         struct svr4_dirent idb;
422         off_t off;              /* true file offset */
423         int buflen, error, eofflag, vfslocked;
424         u_long *cookiebuf = NULL, *cookie;
425         int ncookies = 0, *retval = td->td_retval;
426
427         if (uap->nbytes < 0)
428                 return (EINVAL);
429
430         if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
431                 return (error);
432
433         if ((fp->f_flag & FREAD) == 0) {
434                 fdrop(fp, td);
435                 return (EBADF);
436         }
437
438         vp = fp->f_vnode;
439         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
440         if (vp->v_type != VDIR) {
441                 VFS_UNLOCK_GIANT(vfslocked);
442                 fdrop(fp, td);
443                 return (EINVAL);
444         }
445
446         buflen = min(MAXBSIZE, uap->nbytes);
447         buf = malloc(buflen, M_TEMP, M_WAITOK);
448         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
449         off = fp->f_offset;
450 again:
451         aiov.iov_base = buf;
452         aiov.iov_len = buflen;
453         auio.uio_iov = &aiov;
454         auio.uio_iovcnt = 1;
455         auio.uio_rw = UIO_READ;
456         auio.uio_segflg = UIO_SYSSPACE;
457         auio.uio_td = td;
458         auio.uio_resid = buflen;
459         auio.uio_offset = off;
460
461 #ifdef MAC
462         error = mac_check_vnode_readdir(td->td_ucred, vp);
463         if (error)
464                 goto out;
465 #endif
466
467         /*
468          * First we read into the malloc'ed buffer, then
469          * we massage it into user space, one record at a time.
470          */
471         error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies,
472             &cookiebuf);
473         if (error) {
474                 goto out;
475         }
476
477         inp = buf;
478         outp = uap->buf;
479         resid = uap->nbytes;
480         if ((len = buflen - auio.uio_resid) == 0)
481                 goto eof;
482
483         for (cookie = cookiebuf; len > 0; len -= reclen) {
484                 bdp = (struct dirent *)inp;
485                 reclen = bdp->d_reclen;
486                 if (reclen & 3)
487                         panic("svr4_sys_getdents64: bad reclen");
488                 off = *cookie++;        /* each entry points to the next */
489                 if ((off >> 32) != 0) {
490                         uprintf("svr4_sys_getdents64: dir offset too large for emulated program");
491                         error = EINVAL;
492                         goto out;
493                 }
494                 if (bdp->d_fileno == 0) {
495                         inp += reclen;  /* it is a hole; squish it out */
496                         continue;
497                 }
498                 svr4_reclen = SVR4_RECLEN(&idb, bdp->d_namlen);
499                 if (reclen > len || resid < svr4_reclen) {
500                         /* entry too big for buffer, so just stop */
501                         outp++;
502                         break;
503                 }
504                 /*
505                  * Massage in place to make a SVR4-shaped dirent (otherwise
506                  * we have to worry about touching user memory outside of
507                  * the copyout() call).
508                  */
509                 idb.d_ino = (svr4_ino_t)bdp->d_fileno;
510                 idb.d_off = (svr4_off_t)off;
511                 idb.d_reclen = (u_short)svr4_reclen;
512                 strcpy(idb.d_name, bdp->d_name);
513                 if ((error = copyout((caddr_t)&idb, outp, svr4_reclen)))
514                         goto out;
515                 /* advance past this real entry */
516                 inp += reclen;
517                 /* advance output past SVR4-shaped entry */
518                 outp += svr4_reclen;
519                 resid -= svr4_reclen;
520         }
521
522         /* if we squished out the whole block, try again */
523         if (outp == uap->buf)
524                 goto again;
525         fp->f_offset = off;     /* update the vnode offset */
526
527 eof:
528         *retval = uap->nbytes - resid;
529 out:
530         VOP_UNLOCK(vp, 0, td);
531         VFS_UNLOCK_GIANT(vfslocked);
532         fdrop(fp, td);
533         if (cookiebuf)
534                 free(cookiebuf, M_TEMP);
535         free(buf, M_TEMP);
536         return error;
537 }
538
539
540 int
541 svr4_sys_mmap(td, uap)
542         struct thread *td;
543         struct svr4_sys_mmap_args *uap;
544 {
545         struct mmap_args         mm;
546         int             *retval;
547
548         retval = td->td_retval;
549 #define _MAP_NEW        0x80000000
550         /*
551          * Verify the arguments.
552          */
553         if (uap->prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
554                 return EINVAL;  /* XXX still needed? */
555
556         if (uap->len == 0)
557                 return EINVAL;
558
559         mm.prot = uap->prot;
560         mm.len = uap->len;
561         mm.flags = uap->flags & ~_MAP_NEW;
562         mm.fd = uap->fd;
563         mm.addr = uap->addr;
564         mm.pos = uap->pos;
565
566         return mmap(td, &mm);
567 }
568
569 int
570 svr4_sys_mmap64(td, uap)
571         struct thread *td;
572         struct svr4_sys_mmap64_args *uap;
573 {
574         struct mmap_args         mm;
575         void            *rp;
576
577 #define _MAP_NEW        0x80000000
578         /*
579          * Verify the arguments.
580          */
581         if (uap->prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
582                 return EINVAL;  /* XXX still needed? */
583
584         if (uap->len == 0)
585                 return EINVAL;
586
587         mm.prot = uap->prot;
588         mm.len = uap->len;
589         mm.flags = uap->flags & ~_MAP_NEW;
590         mm.fd = uap->fd;
591         mm.addr = uap->addr;
592         mm.pos = uap->pos;
593
594         rp = (void *) round_page((vm_offset_t)(td->td_proc->p_vmspace->vm_daddr + maxdsiz));
595         if ((mm.flags & MAP_FIXED) == 0 &&
596             mm.addr != 0 && (void *)mm.addr < rp)
597                 mm.addr = rp;
598
599         return mmap(td, &mm);
600 }
601
602
603 int
604 svr4_sys_fchroot(td, uap)
605         struct thread *td;
606         struct svr4_sys_fchroot_args *uap;
607 {
608         struct filedesc *fdp = td->td_proc->p_fd;
609         struct vnode    *vp;
610         struct file     *fp;
611         int              error, vfslocked;
612
613         if ((error = suser(td)) != 0)
614                 return error;
615         if ((error = getvnode(fdp, uap->fd, &fp)) != 0)
616                 return error;
617         vp = fp->f_vnode;
618         VREF(vp);
619         fdrop(fp, td);
620         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
621         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
622         error = change_dir(vp, td);
623         if (error)
624                 goto fail;
625 #ifdef MAC
626         error = mac_check_vnode_chroot(td->td_ucred, vp);
627         if (error)
628                 goto fail;
629 #endif
630         VOP_UNLOCK(vp, 0, td);
631         error = change_root(vp, td);
632         vrele(vp);
633         VFS_UNLOCK_GIANT(vfslocked);
634         return (error);
635 fail:
636         vput(vp);
637         VFS_UNLOCK_GIANT(vfslocked);
638         return (error);
639 }
640
641
642 static int
643 svr4_mknod(td, retval, path, mode, dev)
644         struct thread *td;
645         register_t *retval;
646         char *path;
647         svr4_mode_t mode;
648         svr4_dev_t dev;
649 {
650         char *newpath;
651         int error;
652
653         CHECKALTEXIST(td, path, &newpath);
654
655         if (S_ISFIFO(mode))
656                 error = kern_mkfifo(td, newpath, UIO_SYSSPACE, mode);
657         else
658                 error = kern_mknod(td, newpath, UIO_SYSSPACE, mode, dev);
659         free(newpath, M_TEMP);
660         return (error);
661 }
662
663
664 int
665 svr4_sys_mknod(td, uap)
666         register struct thread *td;
667         struct svr4_sys_mknod_args *uap;
668 {
669         int *retval = td->td_retval;
670         return svr4_mknod(td, retval,
671                           uap->path, uap->mode,
672                           (svr4_dev_t)svr4_to_bsd_odev_t(uap->dev));
673 }
674
675
676 int
677 svr4_sys_xmknod(td, uap)
678         struct thread *td;
679         struct svr4_sys_xmknod_args *uap;
680 {
681         int *retval = td->td_retval;
682         return svr4_mknod(td, retval,
683                           uap->path, uap->mode,
684                           (svr4_dev_t)svr4_to_bsd_dev_t(uap->dev));
685 }
686
687
688 int
689 svr4_sys_vhangup(td, uap)
690         struct thread *td;
691         struct svr4_sys_vhangup_args *uap;
692 {
693         return 0;
694 }
695
696
697 int
698 svr4_sys_sysconfig(td, uap)
699         struct thread *td;
700         struct svr4_sys_sysconfig_args *uap;
701 {
702         int *retval;
703
704         retval = &(td->td_retval[0]);
705
706         switch (uap->name) {
707         case SVR4_CONFIG_UNUSED:
708                 *retval = 0;
709                 break;
710         case SVR4_CONFIG_NGROUPS:
711                 *retval = NGROUPS_MAX;
712                 break;
713         case SVR4_CONFIG_CHILD_MAX:
714                 *retval = maxproc;
715                 break;
716         case SVR4_CONFIG_OPEN_FILES:
717                 *retval = maxfiles;
718                 break;
719         case SVR4_CONFIG_POSIX_VER:
720                 *retval = 198808;
721                 break;
722         case SVR4_CONFIG_PAGESIZE:
723                 *retval = PAGE_SIZE;
724                 break;
725         case SVR4_CONFIG_CLK_TCK:
726                 *retval = 60;   /* should this be `hz', ie. 100? */
727                 break;
728         case SVR4_CONFIG_XOPEN_VER:
729                 *retval = 2;    /* XXX: What should that be? */
730                 break;
731         case SVR4_CONFIG_PROF_TCK:
732                 *retval = 60;   /* XXX: What should that be? */
733                 break;
734         case SVR4_CONFIG_NPROC_CONF:
735                 *retval = 1;    /* Only one processor for now */
736                 break;
737         case SVR4_CONFIG_NPROC_ONLN:
738                 *retval = 1;    /* And it better be online */
739                 break;
740         case SVR4_CONFIG_AIO_LISTIO_MAX:
741         case SVR4_CONFIG_AIO_MAX:
742         case SVR4_CONFIG_AIO_PRIO_DELTA_MAX:
743                 *retval = 0;    /* No aio support */
744                 break;
745         case SVR4_CONFIG_DELAYTIMER_MAX:
746                 *retval = 0;    /* No delaytimer support */
747                 break;
748         case SVR4_CONFIG_MQ_OPEN_MAX:
749                 *retval = msginfo.msgmni;
750                 break;
751         case SVR4_CONFIG_MQ_PRIO_MAX:
752                 *retval = 0;    /* XXX: Don't know */
753                 break;
754         case SVR4_CONFIG_RTSIG_MAX:
755                 *retval = 0;
756                 break;
757         case SVR4_CONFIG_SEM_NSEMS_MAX:
758                 *retval = seminfo.semmni;
759                 break;
760         case SVR4_CONFIG_SEM_VALUE_MAX:
761                 *retval = seminfo.semvmx;
762                 break;
763         case SVR4_CONFIG_SIGQUEUE_MAX:
764                 *retval = 0;    /* XXX: Don't know */
765                 break;
766         case SVR4_CONFIG_SIGRT_MIN:
767         case SVR4_CONFIG_SIGRT_MAX:
768                 *retval = 0;    /* No real time signals */
769                 break;
770         case SVR4_CONFIG_TIMER_MAX:
771                 *retval = 3;    /* XXX: real, virtual, profiling */
772                 break;
773 #if defined(NOTYET)
774         case SVR4_CONFIG_PHYS_PAGES:
775 #if defined(UVM)
776                 *retval = uvmexp.free;  /* XXX: free instead of total */
777 #else
778                 *retval = cnt.v_free_count;     /* XXX: free instead of total */
779 #endif
780                 break;
781         case SVR4_CONFIG_AVPHYS_PAGES:
782 #if defined(UVM)
783                 *retval = uvmexp.active;        /* XXX: active instead of avg */
784 #else
785                 *retval = cnt.v_active_count;   /* XXX: active instead of avg */
786 #endif
787                 break;
788 #endif /* NOTYET */
789
790         default:
791                 return EINVAL;
792         }
793         return 0;
794 }
795
796 /* ARGSUSED */
797 int
798 svr4_sys_break(td, uap)
799         struct thread *td;
800         struct svr4_sys_break_args *uap;
801 {
802         struct obreak_args ap;
803
804         ap.nsize = uap->nsize;
805         return (obreak(td, &ap));
806 }
807
808 static __inline clock_t
809 timeval_to_clock_t(tv)
810         struct timeval *tv;
811 {
812         return tv->tv_sec * hz + tv->tv_usec / (1000000 / hz);
813 }
814
815
816 int
817 svr4_sys_times(td, uap)
818         struct thread *td;
819         struct svr4_sys_times_args *uap;
820 {
821         struct timeval tv, utime, stime, cutime, cstime;
822         struct tms tms;
823         struct proc *p;
824         int error;
825
826         p = td->td_proc;
827         PROC_LOCK(p);
828         calcru(p, &utime, &stime);
829         calccru(p, &cutime, &cstime);
830         PROC_UNLOCK(p);
831
832         tms.tms_utime = timeval_to_clock_t(&utime);
833         tms.tms_stime = timeval_to_clock_t(&stime);
834
835         tms.tms_cutime = timeval_to_clock_t(&cutime);
836         tms.tms_cstime = timeval_to_clock_t(&cstime);
837
838         error = copyout(&tms, uap->tp, sizeof(tms));
839         if (error)
840                 return (error);
841
842         microtime(&tv);
843         td->td_retval[0] = (int)timeval_to_clock_t(&tv);
844         return (0);
845 }
846
847
848 int
849 svr4_sys_ulimit(td, uap)
850         struct thread *td;
851         struct svr4_sys_ulimit_args *uap;
852 {
853         int *retval = td->td_retval;
854         int error;
855
856         switch (uap->cmd) {
857         case SVR4_GFILLIM:
858                 PROC_LOCK(td->td_proc);
859                 *retval = lim_cur(td->td_proc, RLIMIT_FSIZE) / 512;
860                 PROC_UNLOCK(td->td_proc);
861                 if (*retval == -1)
862                         *retval = 0x7fffffff;
863                 return 0;
864
865         case SVR4_SFILLIM:
866                 {
867                         struct rlimit krl;
868
869                         krl.rlim_cur = uap->newlimit * 512;
870                         PROC_LOCK(td->td_proc);
871                         krl.rlim_max = lim_max(td->td_proc, RLIMIT_FSIZE);
872                         PROC_UNLOCK(td->td_proc);
873
874                         error = kern_setrlimit(td, RLIMIT_FSIZE, &krl);
875                         if (error)
876                                 return error;
877
878                         PROC_LOCK(td->td_proc);
879                         *retval = lim_cur(td->td_proc, RLIMIT_FSIZE);
880                         PROC_UNLOCK(td->td_proc);
881                         if (*retval == -1)
882                                 *retval = 0x7fffffff;
883                         return 0;
884                 }
885
886         case SVR4_GMEMLIM:
887                 {
888                         struct vmspace *vm = td->td_proc->p_vmspace;
889                         register_t r;
890
891                         PROC_LOCK(td->td_proc);
892                         r = lim_cur(td->td_proc, RLIMIT_DATA);
893                         PROC_UNLOCK(td->td_proc);
894
895                         if (r == -1)
896                                 r = 0x7fffffff;
897                         mtx_lock(&Giant);       /* XXX */
898                         r += (long) vm->vm_daddr;
899                         mtx_unlock(&Giant);
900                         if (r < 0)
901                                 r = 0x7fffffff;
902                         *retval = r;
903                         return 0;
904                 }
905
906         case SVR4_GDESLIM:
907                 PROC_LOCK(td->td_proc);
908                 *retval = lim_cur(td->td_proc, RLIMIT_NOFILE);
909                 PROC_UNLOCK(td->td_proc);
910                 if (*retval == -1)
911                         *retval = 0x7fffffff;
912                 return 0;
913
914         default:
915                 return EINVAL;
916         }
917 }
918
919 static struct proc *
920 svr4_pfind(pid)
921         pid_t pid;
922 {
923         struct proc *p;
924
925         /* look in the live processes */
926         if ((p = pfind(pid)) == NULL)
927                 /* look in the zombies */
928                 p = zpfind(pid);
929
930         return p;
931 }
932
933
934 int
935 svr4_sys_pgrpsys(td, uap)
936         struct thread *td;
937         struct svr4_sys_pgrpsys_args *uap;
938 {
939         int *retval = td->td_retval;
940         struct proc *p = td->td_proc;
941
942         switch (uap->cmd) {
943         case 1:                 /* setpgrp() */
944                 /*
945                  * SVR4 setpgrp() (which takes no arguments) has the
946                  * semantics that the session ID is also created anew, so
947                  * in almost every sense, setpgrp() is identical to
948                  * setsid() for SVR4.  (Under BSD, the difference is that
949                  * a setpgid(0,0) will not create a new session.)
950                  */
951                 setsid(td, NULL);
952                 /*FALLTHROUGH*/
953
954         case 0:                 /* getpgrp() */
955                 PROC_LOCK(p);
956                 *retval = p->p_pgrp->pg_id;
957                 PROC_UNLOCK(p);
958                 return 0;
959
960         case 2:                 /* getsid(pid) */
961                 if (uap->pid == 0)
962                         PROC_LOCK(p);
963                 else if ((p = svr4_pfind(uap->pid)) == NULL)
964                         return ESRCH;
965                 /*
966                  * This has already been initialized to the pid of
967                  * the session leader.
968                  */
969                 *retval = (register_t) p->p_session->s_sid;
970                 PROC_UNLOCK(p);
971                 return 0;
972
973         case 3:                 /* setsid() */
974                 return setsid(td, NULL);
975
976         case 4:                 /* getpgid(pid) */
977
978                 if (uap->pid == 0)
979                         PROC_LOCK(p);
980                 else if ((p = svr4_pfind(uap->pid)) == NULL)
981                         return ESRCH;
982
983                 *retval = (int) p->p_pgrp->pg_id;
984                 PROC_UNLOCK(p);
985                 return 0;
986
987         case 5:                 /* setpgid(pid, pgid); */
988                 {
989                         struct setpgid_args sa;
990
991                         sa.pid = uap->pid;
992                         sa.pgid = uap->pgid;
993                         return setpgid(td, &sa);
994                 }
995
996         default:
997                 return EINVAL;
998         }
999 }
1000
1001 struct svr4_hrtcntl_args {
1002         int                     cmd;
1003         int                     fun;
1004         int                     clk;
1005         svr4_hrt_interval_t *   iv;
1006         svr4_hrt_time_t *       ti;
1007 };
1008
1009
1010 static int
1011 svr4_hrtcntl(td, uap, retval)
1012         struct thread *td;
1013         struct svr4_hrtcntl_args *uap;
1014         register_t *retval;
1015 {
1016         switch (uap->fun) {
1017         case SVR4_HRT_CNTL_RES:
1018                 DPRINTF(("htrcntl(RES)\n"));
1019                 *retval = SVR4_HRT_USEC;
1020                 return 0;
1021
1022         case SVR4_HRT_CNTL_TOFD:
1023                 DPRINTF(("htrcntl(TOFD)\n"));
1024                 {
1025                         struct timeval tv;
1026                         svr4_hrt_time_t t;
1027                         if (uap->clk != SVR4_HRT_CLK_STD) {
1028                                 DPRINTF(("clk == %d\n", uap->clk));
1029                                 return EINVAL;
1030                         }
1031                         if (uap->ti == NULL) {
1032                                 DPRINTF(("ti NULL\n"));
1033                                 return EINVAL;
1034                         }
1035                         microtime(&tv);
1036                         t.h_sec = tv.tv_sec;
1037                         t.h_rem = tv.tv_usec;
1038                         t.h_res = SVR4_HRT_USEC;
1039                         return copyout(&t, uap->ti, sizeof(t));
1040                 }
1041
1042         case SVR4_HRT_CNTL_START:
1043                 DPRINTF(("htrcntl(START)\n"));
1044                 return ENOSYS;
1045
1046         case SVR4_HRT_CNTL_GET:
1047                 DPRINTF(("htrcntl(GET)\n"));
1048                 return ENOSYS;
1049         default:
1050                 DPRINTF(("Bad htrcntl command %d\n", uap->fun));
1051                 return ENOSYS;
1052         }
1053 }
1054
1055
1056 int
1057 svr4_sys_hrtsys(td, uap) 
1058         struct thread *td;
1059         struct svr4_sys_hrtsys_args *uap;
1060 {
1061         int *retval = td->td_retval;
1062
1063         switch (uap->cmd) {
1064         case SVR4_HRT_CNTL:
1065                 return svr4_hrtcntl(td, (struct svr4_hrtcntl_args *) uap,
1066                                     retval);
1067
1068         case SVR4_HRT_ALRM:
1069                 DPRINTF(("hrtalarm\n"));
1070                 return ENOSYS;
1071
1072         case SVR4_HRT_SLP:
1073                 DPRINTF(("hrtsleep\n"));
1074                 return ENOSYS;
1075
1076         case SVR4_HRT_CAN:
1077                 DPRINTF(("hrtcancel\n"));
1078                 return ENOSYS;
1079
1080         default:
1081                 DPRINTF(("Bad hrtsys command %d\n", uap->cmd));
1082                 return EINVAL;
1083         }
1084 }
1085
1086
1087 static int
1088 svr4_setinfo(pid, ru, st, s)
1089         pid_t pid;
1090         struct rusage *ru;
1091         int st;
1092         svr4_siginfo_t *s;
1093 {
1094         svr4_siginfo_t i;
1095         int sig;
1096
1097         memset(&i, 0, sizeof(i));
1098
1099         i.svr4_si_signo = SVR4_SIGCHLD;
1100         i.svr4_si_errno = 0;    /* XXX? */
1101
1102         i.svr4_si_pid = pid;
1103         if (ru) {
1104                 i.svr4_si_stime = ru->ru_stime.tv_sec;
1105                 i.svr4_si_utime = ru->ru_utime.tv_sec;
1106         }
1107
1108         if (WIFEXITED(st)) {
1109                 i.svr4_si_status = WEXITSTATUS(st);
1110                 i.svr4_si_code = SVR4_CLD_EXITED;
1111         } else if (WIFSTOPPED(st)) {
1112                 sig = WSTOPSIG(st);
1113                 if (sig >= 0 && sig < NSIG)
1114                         i.svr4_si_status = SVR4_BSD2SVR4_SIG(sig);
1115
1116                 if (i.svr4_si_status == SVR4_SIGCONT)
1117                         i.svr4_si_code = SVR4_CLD_CONTINUED;
1118                 else
1119                         i.svr4_si_code = SVR4_CLD_STOPPED;
1120         } else {
1121                 sig = WTERMSIG(st);
1122                 if (sig >= 0 && sig < NSIG)
1123                         i.svr4_si_status = SVR4_BSD2SVR4_SIG(sig);
1124
1125                 if (WCOREDUMP(st))
1126                         i.svr4_si_code = SVR4_CLD_DUMPED;
1127                 else
1128                         i.svr4_si_code = SVR4_CLD_KILLED;
1129         }
1130
1131         DPRINTF(("siginfo [pid %ld signo %d code %d errno %d status %d]\n",
1132                  i.svr4_si_pid, i.svr4_si_signo, i.svr4_si_code, i.svr4_si_errno,
1133                  i.svr4_si_status));
1134
1135         return copyout(&i, s, sizeof(i));
1136 }
1137
1138
1139 int
1140 svr4_sys_waitsys(td, uap)
1141         struct thread *td;
1142         struct svr4_sys_waitsys_args *uap;
1143 {
1144         struct rusage ru;
1145         pid_t pid;
1146         int nfound, status;
1147         int error, *retval = td->td_retval;
1148         struct proc *p, *q;
1149
1150         DPRINTF(("waitsys(%d, %d, %p, %x)\n", 
1151                  uap->grp, uap->id,
1152                  uap->info, uap->options));
1153
1154         q = td->td_proc;
1155         switch (uap->grp) {
1156         case SVR4_P_PID:
1157                 pid = uap->id;
1158                 break;
1159
1160         case SVR4_P_PGID:
1161                 PROC_LOCK(q);
1162                 pid = -q->p_pgid;
1163                 PROC_UNLOCK(q);
1164                 break;
1165
1166         case SVR4_P_ALL:
1167                 pid = WAIT_ANY;
1168                 break;
1169
1170         default:
1171                 return EINVAL;
1172         }
1173
1174         /* Hand off the easy cases to kern_wait(). */
1175         if (!(uap->options & (SVR4_WNOWAIT)) &&
1176             (uap->options & (SVR4_WEXITED | SVR4_WTRAPPED))) {
1177                 int options;
1178
1179                 options = 0;
1180                 if (uap->options & SVR4_WSTOPPED)
1181                         options |= WUNTRACED;
1182                 if (uap->options & SVR4_WCONTINUED)
1183                         options |= WCONTINUED;
1184                 if (uap->options & SVR4_WNOHANG)
1185                         options |= WNOHANG;
1186
1187                 error = kern_wait(td, pid, &status, options, &ru);
1188                 if (error)
1189                         return (error);
1190                 if (uap->options & SVR4_WNOHANG && *retval == 0)
1191                         error = svr4_setinfo(*retval, NULL, 0, uap->info);
1192                 else
1193                         error = svr4_setinfo(*retval, &ru, status, uap->info);
1194                 *retval = 0;
1195                 return (error);
1196         }
1197
1198         /*
1199          * Ok, handle the weird cases.  Either WNOWAIT is set (meaning we
1200          * just want to see if there is a process to harvest, we dont'
1201          * want to actually harvest it), or WEXIT and WTRAPPED are clear
1202          * meaning we want to ignore zombies.  Either way, we don't have
1203          * to handle harvesting zombies here.  We do have to duplicate the
1204          * other portions of kern_wait() though, especially for the
1205          * WCONTINUED and WSTOPPED.
1206          */
1207 loop:
1208         nfound = 0;
1209         sx_slock(&proctree_lock);
1210         LIST_FOREACH(p, &q->p_children, p_sibling) {
1211                 PROC_LOCK(p);
1212                 if (pid != WAIT_ANY &&
1213                     p->p_pid != pid && p->p_pgid != -pid) {
1214                         PROC_UNLOCK(p);
1215                         DPRINTF(("pid %d pgid %d != %d\n", p->p_pid,
1216                                  p->p_pgid, pid));
1217                         continue;
1218                 }
1219                 if (p_canwait(td, p)) {
1220                         PROC_UNLOCK(p);
1221                         continue;
1222                 }
1223
1224                 nfound++;
1225
1226                 /*
1227                  * See if we have a zombie.  If so, WNOWAIT should be set,
1228                  * as otherwise we should have called kern_wait() up above.
1229                  */
1230                 if ((p->p_state == PRS_ZOMBIE) && 
1231                     ((uap->options & (SVR4_WEXITED|SVR4_WTRAPPED)))) {
1232                         KASSERT(uap->options & SVR4_WNOWAIT,
1233                             ("WNOWAIT is clear"));
1234
1235                         /* Found a zombie, so cache info in local variables. */
1236                         pid = p->p_pid;
1237                         status = p->p_xstat;
1238                         ru = *p->p_ru;
1239                         calcru(p, &ru.ru_utime, &ru.ru_stime);
1240                         PROC_UNLOCK(p);
1241                         sx_sunlock(&proctree_lock);
1242
1243                         /* Copy the info out to userland. */
1244                         *retval = 0;
1245                         DPRINTF(("found %d\n", pid));
1246                         return (svr4_setinfo(pid, &ru, status, uap->info));
1247                 }
1248
1249                 /*
1250                  * See if we have a stopped or continued process.
1251                  * XXX: This duplicates the same code in kern_wait().
1252                  */
1253                 mtx_lock_spin(&sched_lock);
1254                 if ((p->p_flag & P_STOPPED_SIG) &&
1255                     (p->p_suspcount == p->p_numthreads) &&
1256                     (p->p_flag & P_WAITED) == 0 &&
1257                     (p->p_flag & P_TRACED || uap->options & SVR4_WSTOPPED)) {
1258                         mtx_unlock_spin(&sched_lock);
1259                         if (((uap->options & SVR4_WNOWAIT)) == 0)
1260                                 p->p_flag |= P_WAITED;
1261                         sx_sunlock(&proctree_lock);
1262                         pid = p->p_pid;
1263                         status = W_STOPCODE(p->p_xstat);
1264                         ru = *p->p_ru;
1265                         calcru(p, &ru.ru_utime, &ru.ru_stime);
1266                         PROC_UNLOCK(p);
1267
1268                         if (((uap->options & SVR4_WNOWAIT)) == 0) {
1269                                 PROC_LOCK(q);
1270                                 sigqueue_take(p->p_ksi);
1271                                 PROC_UNLOCK(q);
1272                         }
1273
1274                         *retval = 0;
1275                         DPRINTF(("jobcontrol %d\n", pid));
1276                         return (svr4_setinfo(pid, &ru, status, uap->info));
1277                 }
1278                 mtx_unlock_spin(&sched_lock);
1279                 if (uap->options & SVR4_WCONTINUED &&
1280                     (p->p_flag & P_CONTINUED)) {
1281                         sx_sunlock(&proctree_lock);
1282                         if (((uap->options & SVR4_WNOWAIT)) == 0)
1283                                 p->p_flag &= ~P_CONTINUED;
1284                         pid = p->p_pid;
1285                         ru = *p->p_ru;
1286                         status = SIGCONT;
1287                         calcru(p, &ru.ru_utime, &ru.ru_stime);
1288                         PROC_UNLOCK(p);
1289
1290                         if (((uap->options & SVR4_WNOWAIT)) == 0) {
1291                                 PROC_LOCK(q);
1292                                 sigqueue_take(p->p_ksi);
1293                                 PROC_UNLOCK(q);
1294                         }
1295
1296                         *retval = 0;
1297                         DPRINTF(("jobcontrol %d\n", pid));
1298                         return (svr4_setinfo(pid, &ru, status, uap->info));
1299                 }
1300                 PROC_UNLOCK(p);
1301         }
1302
1303         if (nfound == 0) {
1304                 sx_sunlock(&proctree_lock);
1305                 return (ECHILD);
1306         }
1307
1308         if (uap->options & SVR4_WNOHANG) {
1309                 sx_sunlock(&proctree_lock);
1310                 *retval = 0;
1311                 return (svr4_setinfo(0, NULL, 0, uap->info));
1312         }
1313
1314         PROC_LOCK(q);
1315         sx_sunlock(&proctree_lock);
1316         if (q->p_flag & P_STATCHILD) {
1317                 q->p_flag &= ~P_STATCHILD;
1318                 error = 0;
1319         } else
1320                 error = msleep(q, &q->p_mtx, PWAIT | PCATCH, "svr4_wait", 0);
1321         PROC_UNLOCK(q);
1322         if (error)
1323                 return error;
1324         goto loop;
1325 }
1326
1327
1328 static void
1329 bsd_statfs_to_svr4_statvfs(bfs, sfs)
1330         const struct statfs *bfs;
1331         struct svr4_statvfs *sfs;
1332 {
1333         sfs->f_bsize = bfs->f_iosize; /* XXX */
1334         sfs->f_frsize = bfs->f_bsize;
1335         sfs->f_blocks = bfs->f_blocks;
1336         sfs->f_bfree = bfs->f_bfree;
1337         sfs->f_bavail = bfs->f_bavail;
1338         sfs->f_files = bfs->f_files;
1339         sfs->f_ffree = bfs->f_ffree;
1340         sfs->f_favail = bfs->f_ffree;
1341         sfs->f_fsid = bfs->f_fsid.val[0];
1342         memcpy(sfs->f_basetype, bfs->f_fstypename, sizeof(sfs->f_basetype));
1343         sfs->f_flag = 0;
1344         if (bfs->f_flags & MNT_RDONLY)
1345                 sfs->f_flag |= SVR4_ST_RDONLY;
1346         if (bfs->f_flags & MNT_NOSUID)
1347                 sfs->f_flag |= SVR4_ST_NOSUID;
1348         sfs->f_namemax = MAXNAMLEN;
1349         memcpy(sfs->f_fstr, bfs->f_fstypename, sizeof(sfs->f_fstr)); /* XXX */
1350         memset(sfs->f_filler, 0, sizeof(sfs->f_filler));
1351 }
1352
1353
1354 static void
1355 bsd_statfs_to_svr4_statvfs64(bfs, sfs)
1356         const struct statfs *bfs;
1357         struct svr4_statvfs64 *sfs;
1358 {
1359         sfs->f_bsize = bfs->f_iosize; /* XXX */
1360         sfs->f_frsize = bfs->f_bsize;
1361         sfs->f_blocks = bfs->f_blocks;
1362         sfs->f_bfree = bfs->f_bfree;
1363         sfs->f_bavail = bfs->f_bavail;
1364         sfs->f_files = bfs->f_files;
1365         sfs->f_ffree = bfs->f_ffree;
1366         sfs->f_favail = bfs->f_ffree;
1367         sfs->f_fsid = bfs->f_fsid.val[0];
1368         memcpy(sfs->f_basetype, bfs->f_fstypename, sizeof(sfs->f_basetype));
1369         sfs->f_flag = 0;
1370         if (bfs->f_flags & MNT_RDONLY)
1371                 sfs->f_flag |= SVR4_ST_RDONLY;
1372         if (bfs->f_flags & MNT_NOSUID)
1373                 sfs->f_flag |= SVR4_ST_NOSUID;
1374         sfs->f_namemax = MAXNAMLEN;
1375         memcpy(sfs->f_fstr, bfs->f_fstypename, sizeof(sfs->f_fstr)); /* XXX */
1376         memset(sfs->f_filler, 0, sizeof(sfs->f_filler));
1377 }
1378
1379
1380 int
1381 svr4_sys_statvfs(td, uap)
1382         struct thread *td;
1383         struct svr4_sys_statvfs_args *uap;
1384 {
1385         struct svr4_statvfs sfs;
1386         struct statfs bfs;
1387         char *path;
1388         int error;
1389
1390         CHECKALTEXIST(td, uap->path, &path);
1391
1392         error = kern_statfs(td, path, UIO_SYSSPACE, &bfs);
1393         free(path, M_TEMP);
1394         if (error)
1395                 return (error);
1396         bsd_statfs_to_svr4_statvfs(&bfs, &sfs);
1397         return copyout(&sfs, uap->fs, sizeof(sfs));
1398 }
1399
1400
1401 int
1402 svr4_sys_fstatvfs(td, uap)
1403         struct thread *td;
1404         struct svr4_sys_fstatvfs_args *uap;
1405 {
1406         struct svr4_statvfs sfs;
1407         struct statfs bfs;
1408         int error;
1409
1410         error = kern_fstatfs(td, uap->fd, &bfs);
1411         if (error)
1412                 return (error);
1413         bsd_statfs_to_svr4_statvfs(&bfs, &sfs);
1414         return copyout(&sfs, uap->fs, sizeof(sfs));
1415 }
1416
1417
1418 int
1419 svr4_sys_statvfs64(td, uap)
1420         struct thread *td;
1421         struct svr4_sys_statvfs64_args *uap;
1422 {
1423         struct svr4_statvfs64 sfs;
1424         struct statfs bfs;
1425         char *path;
1426         int error;
1427
1428         CHECKALTEXIST(td, uap->path, &path);
1429
1430         error = kern_statfs(td, path, UIO_SYSSPACE, &bfs);
1431         free(path, M_TEMP);
1432         if (error)
1433                 return (error);
1434         bsd_statfs_to_svr4_statvfs64(&bfs, &sfs);
1435         return copyout(&sfs, uap->fs, sizeof(sfs));
1436 }
1437
1438
1439 int
1440 svr4_sys_fstatvfs64(td, uap) 
1441         struct thread *td;
1442         struct svr4_sys_fstatvfs64_args *uap;
1443 {
1444         struct svr4_statvfs64 sfs;
1445         struct statfs bfs;
1446         int error;
1447
1448         error = kern_fstatfs(td, uap->fd, &bfs);
1449         if (error)
1450                 return (error);
1451         bsd_statfs_to_svr4_statvfs64(&bfs, &sfs);
1452         return copyout(&sfs, uap->fs, sizeof(sfs));
1453 }
1454
1455 int
1456 svr4_sys_alarm(td, uap)
1457         struct thread *td;
1458         struct svr4_sys_alarm_args *uap;
1459 {
1460         struct itimerval itv, oitv;
1461         int error;
1462
1463         timevalclear(&itv.it_interval);
1464         itv.it_value.tv_sec = uap->sec;
1465         itv.it_value.tv_usec = 0;
1466         error = kern_setitimer(td, ITIMER_REAL, &itv, &oitv);
1467         if (error)
1468                 return (error);
1469         if (oitv.it_value.tv_usec != 0)
1470                 oitv.it_value.tv_sec++;
1471         td->td_retval[0] = oitv.it_value.tv_sec;
1472         return (0);
1473 }
1474
1475 int
1476 svr4_sys_gettimeofday(td, uap)
1477         struct thread *td;
1478         struct svr4_sys_gettimeofday_args *uap;
1479 {
1480         if (uap->tp) {
1481                 struct timeval atv;
1482
1483                 microtime(&atv);
1484                 return copyout(&atv, uap->tp, sizeof (atv));
1485         }
1486
1487         return 0;
1488 }
1489
1490 int
1491 svr4_sys_facl(td, uap)
1492         struct thread *td;
1493         struct svr4_sys_facl_args *uap;
1494 {
1495         int *retval;
1496
1497         retval = td->td_retval;
1498         *retval = 0;
1499
1500         switch (uap->cmd) {
1501         case SVR4_SYS_SETACL:
1502                 /* We don't support acls on any filesystem */
1503                 return ENOSYS;
1504
1505         case SVR4_SYS_GETACL:
1506                 return copyout(retval, &uap->num,
1507                     sizeof(uap->num));
1508
1509         case SVR4_SYS_GETACLCNT:
1510                 return 0;
1511
1512         default:
1513                 return EINVAL;
1514         }
1515 }
1516
1517
1518 int
1519 svr4_sys_acl(td, uap)
1520         struct thread *td;
1521         struct svr4_sys_acl_args *uap;
1522 {
1523         /* XXX: for now the same */
1524         return svr4_sys_facl(td, (struct svr4_sys_facl_args *)uap);
1525 }
1526
1527 int
1528 svr4_sys_auditsys(td, uap)
1529         struct thread *td;
1530         struct svr4_sys_auditsys_args *uap;
1531 {
1532         /*
1533          * XXX: Big brother is *not* watching.
1534          */
1535         return 0;
1536 }
1537
1538 int
1539 svr4_sys_memcntl(td, uap)
1540         struct thread *td;
1541         struct svr4_sys_memcntl_args *uap;
1542 {
1543         switch (uap->cmd) {
1544         case SVR4_MC_SYNC:
1545                 {
1546                         struct msync_args msa;
1547
1548                         msa.addr = uap->addr;
1549                         msa.len = uap->len;
1550                         msa.flags = (int)uap->arg;
1551
1552                         return msync(td, &msa);
1553                 }
1554         case SVR4_MC_ADVISE:
1555                 {
1556                         struct madvise_args maa;
1557
1558                         maa.addr = uap->addr;
1559                         maa.len = uap->len;
1560                         maa.behav = (int)uap->arg;
1561
1562                         return madvise(td, &maa);
1563                 }
1564         case SVR4_MC_LOCK:
1565         case SVR4_MC_UNLOCK:
1566         case SVR4_MC_LOCKAS:
1567         case SVR4_MC_UNLOCKAS:
1568                 return EOPNOTSUPP;
1569         default:
1570                 return ENOSYS;
1571         }
1572 }
1573
1574
1575 int
1576 svr4_sys_nice(td, uap)
1577         struct thread *td;
1578         struct svr4_sys_nice_args *uap;
1579 {
1580         struct setpriority_args ap;
1581         int error;
1582
1583         ap.which = PRIO_PROCESS;
1584         ap.who = 0;
1585         ap.prio = uap->prio;
1586
1587         if ((error = setpriority(td, &ap)) != 0)
1588                 return error;
1589
1590         /* the cast is stupid, but the structures are the same */
1591         if ((error = getpriority(td, (struct getpriority_args *)&ap)) != 0)
1592                 return error;
1593
1594         return 0;
1595 }
1596
1597 int
1598 svr4_sys_resolvepath(td, uap)
1599         struct thread *td;
1600         struct svr4_sys_resolvepath_args *uap;
1601 {
1602         struct nameidata nd;
1603         int error, *retval = td->td_retval;
1604         unsigned int ncopy;
1605         int vfslocked;
1606
1607         NDINIT(&nd, LOOKUP, NOFOLLOW | SAVENAME | MPSAFE, UIO_USERSPACE,
1608             uap->path, td);
1609
1610         if ((error = namei(&nd)) != 0)
1611                 return error;
1612         vfslocked = NDHASGIANT(&nd);
1613
1614         ncopy = min(uap->bufsiz, strlen(nd.ni_cnd.cn_pnbuf) + 1);
1615         if ((error = copyout(nd.ni_cnd.cn_pnbuf, uap->buf, ncopy)) != 0)
1616                 goto bad;
1617
1618         *retval = ncopy;
1619 bad:
1620         NDFREE(&nd, NDF_ONLY_PNBUF);
1621         vput(nd.ni_vp);
1622         VFS_UNLOCK_GIANT(vfslocked);
1623         return error;
1624 }