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