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