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