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