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