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