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