ibcs2_misc.c revision 22542
1/*
2 * Copyright (c) 1995 Steven Wallace
3 * Copyright (c) 1994, 1995 Scott Bartram
4 * Copyright (c) 1992, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * This software was developed by the Computer Systems Engineering group
8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 * contributed to Berkeley.
10 *
11 * All advertising materials mentioning features or use of this software
12 * must display the following acknowledgement:
13 *	This product includes software developed by the University of
14 *	California, Lawrence Berkeley Laboratory.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 *    notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 *    notice, this list of conditions and the following disclaimer in the
23 *    documentation and/or other materials provided with the distribution.
24 * 3. All advertising materials mentioning features or use of this software
25 *    must display the following acknowledgement:
26 *	This product includes software developed by the University of
27 *	California, Berkeley and its contributors.
28 * 4. Neither the name of the University nor the names of its contributors
29 *    may be used to endorse or promote products derived from this software
30 *    without specific prior written permission.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * SUCH DAMAGE.
43 *
44 * from: Header: sun_misc.c,v 1.16 93/04/07 02:46:27 torek Exp
45 *
46 *	@(#)sun_misc.c	8.1 (Berkeley) 6/18/93
47 *
48 * $FreeBSD: head/sys/i386/ibcs2/ibcs2_misc.c 22542 1997-02-10 16:32:52Z mpp $
49 */
50
51/*
52 * IBCS2 compatibility module.
53 *
54 * IBCS2 system calls that are implemented differently in BSD are
55 * handled here.
56 */
57#include <sys/param.h>
58#include <sys/systm.h>
59#include <sys/namei.h>
60#include <sys/proc.h>
61#include <sys/file.h>
62#include <sys/filedesc.h>
63#include <sys/ioctl.h>
64#include <sys/kernel.h>
65#include <sys/malloc.h>
66#include <sys/mbuf.h>
67#include <sys/mman.h>
68#include <sys/mount.h>
69#include <sys/reboot.h>
70#include <sys/resource.h>
71#include <sys/resourcevar.h>
72#include <sys/socket.h>
73#include <sys/stat.h>
74#include <sys/time.h>
75#include <sys/times.h>
76#include <sys/vnode.h>
77#include <sys/uio.h>
78#include <sys/wait.h>
79#include <sys/utsname.h>
80#include <sys/unistd.h>
81
82#include <ufs/ufs/dir.h>
83
84#include <netinet/in.h>
85#include <sys/sysproto.h>
86
87#include <miscfs/specfs/specdev.h>
88
89#include <vm/vm.h>
90#include <sys/sysctl.h>		/* must be included after vm.h */
91
92#include <machine/cpu.h>
93#include <machine/psl.h>
94#include <machine/reg.h>
95
96#include <i386/ibcs2/ibcs2_types.h>
97#include <i386/ibcs2/ibcs2_dirent.h>
98#include <i386/ibcs2/ibcs2_fcntl.h>
99#include <i386/ibcs2/ibcs2_time.h>
100#include <i386/ibcs2/ibcs2_signal.h>
101#include <i386/ibcs2/ibcs2_unistd.h>
102#include <i386/ibcs2/ibcs2_utsname.h>
103#include <i386/ibcs2/ibcs2_util.h>
104#include <i386/ibcs2/ibcs2_utime.h>
105#include <i386/ibcs2/ibcs2_proto.h>
106#include <i386/ibcs2/ibcs2_xenix.h>
107
108
109int
110ibcs2_ulimit(p, uap, retval)
111	struct proc *p;
112	struct ibcs2_ulimit_args *uap;
113	int *retval;
114{
115#ifdef notyet
116	int error;
117	struct rlimit rl;
118	struct setrlimit_args {
119		int resource;
120		struct rlimit *rlp;
121	} sra;
122#endif
123#define IBCS2_GETFSIZE		1
124#define IBCS2_SETFSIZE		2
125#define IBCS2_GETPSIZE		3
126#define IBCS2_GETDTABLESIZE	4
127
128	switch (SCARG(uap, cmd)) {
129	case IBCS2_GETFSIZE:
130		*retval = p->p_rlimit[RLIMIT_FSIZE].rlim_cur;
131		if (*retval == -1) *retval = 0x7fffffff;
132		return 0;
133	case IBCS2_SETFSIZE:	/* XXX - fix this */
134#ifdef notyet
135		rl.rlim_cur = SCARG(uap, newlimit);
136		sra.resource = RLIMIT_FSIZE;
137		sra.rlp = &rl;
138		error = setrlimit(p, &sra, retval);
139		if (!error)
140			*retval = p->p_rlimit[RLIMIT_FSIZE].rlim_cur;
141		else
142			DPRINTF(("failed "));
143		return error;
144#else
145		*retval = SCARG(uap, newlimit);
146		return 0;
147#endif
148	case IBCS2_GETPSIZE:
149		*retval = p->p_rlimit[RLIMIT_RSS].rlim_cur; /* XXX */
150		return 0;
151	case IBCS2_GETDTABLESIZE:
152		uap->cmd = IBCS2_SC_OPEN_MAX;
153		return ibcs2_sysconf(p, (struct ibcs2_sysconf_args *)uap,
154				     retval);
155	default:
156		return ENOSYS;
157	}
158}
159
160#define IBCS2_WSTOPPED       0177
161#define IBCS2_STOPCODE(sig)  ((sig) << 8 | IBCS2_WSTOPPED)
162int
163ibcs2_wait(p, uap, retval)
164	struct proc *p;
165	struct ibcs2_wait_args *uap;
166	int *retval;
167{
168	int error, status;
169	struct wait_args w4;
170        struct trapframe *tf = (struct trapframe *)p->p_md.md_regs;
171
172	SCARG(&w4, rusage) = NULL;
173        if ((tf->tf_eflags & (PSL_Z|PSL_PF|PSL_N|PSL_V))
174            == (PSL_Z|PSL_PF|PSL_N|PSL_V)) {
175		/* waitpid */
176		SCARG(&w4, pid) = SCARG(uap, a1);
177		SCARG(&w4, status) = (int *)SCARG(uap, a2);
178		SCARG(&w4, options) = SCARG(uap, a3);
179	} else {
180		/* wait */
181		SCARG(&w4, pid) = WAIT_ANY;
182		SCARG(&w4, status) = (int *)SCARG(uap, a1);
183		SCARG(&w4, options) = 0;
184	}
185	if ((error = wait4(p, &w4, retval)) != 0)
186		return error;
187	if (SCARG(&w4, status))	{	/* this is real iBCS brain-damage */
188		error = copyin((caddr_t)SCARG(&w4, status), (caddr_t)&status,
189			       sizeof(SCARG(&w4, status)));
190		if(error)
191		  return error;
192
193		/* convert status/signal result */
194		if(WIFSTOPPED(status))
195			status =
196			  IBCS2_STOPCODE(bsd_to_ibcs2_sig[WSTOPSIG(status)]);
197		else if(WIFSIGNALED(status))
198			status = bsd_to_ibcs2_sig[WTERMSIG(status)];
199		/* else exit status -- identical */
200
201		/* record result/status */
202		retval[1] = status;
203		return copyout((caddr_t)&status, (caddr_t)SCARG(&w4, status),
204			       sizeof(SCARG(&w4, status)));
205	}
206
207	return 0;
208}
209
210int
211ibcs2_execv(p, uap, retval)
212	struct proc *p;
213	struct ibcs2_execv_args *uap;
214	int *retval;
215{
216	struct execve_args ea;
217	caddr_t sg = stackgap_init();
218
219        CHECKALTEXIST(p, &sg, SCARG(uap, path));
220	SCARG(&ea, fname) = SCARG(uap, path);
221	SCARG(&ea, argv) = SCARG(uap, argp);
222	SCARG(&ea, envv) = NULL;
223	return execve(p, &ea, retval);
224}
225
226int
227ibcs2_execve(p, uap, retval)
228        struct proc *p;
229        struct ibcs2_execve_args *uap;
230        int *retval;
231{
232        caddr_t sg = stackgap_init();
233        CHECKALTEXIST(p, &sg, SCARG(uap, path));
234        return execve(p, (struct execve_args *)uap, retval);
235}
236
237int
238ibcs2_umount(p, uap, retval)
239	struct proc *p;
240	struct ibcs2_umount_args *uap;
241	int *retval;
242{
243	struct unmount_args um;
244
245	SCARG(&um, path) = SCARG(uap, name);
246	SCARG(&um, flags) = 0;
247	return unmount(p, &um, retval);
248}
249
250int
251ibcs2_mount(p, uap, retval)
252	struct proc *p;
253	struct ibcs2_mount_args *uap;
254	int *retval;
255{
256#ifdef notyet
257	int oflags = SCARG(uap, flags), nflags, error;
258	char fsname[MFSNAMELEN];
259
260	if (oflags & (IBCS2_MS_NOSUB | IBCS2_MS_SYS5))
261		return (EINVAL);
262	if ((oflags & IBCS2_MS_NEWTYPE) == 0)
263		return (EINVAL);
264	nflags = 0;
265	if (oflags & IBCS2_MS_RDONLY)
266		nflags |= MNT_RDONLY;
267	if (oflags & IBCS2_MS_NOSUID)
268		nflags |= MNT_NOSUID;
269	if (oflags & IBCS2_MS_REMOUNT)
270		nflags |= MNT_UPDATE;
271	SCARG(uap, flags) = nflags;
272
273	if (error = copyinstr((caddr_t)SCARG(uap, type), fsname, sizeof fsname,
274			      (u_int *)0))
275		return (error);
276
277	if (strcmp(fsname, "4.2") == 0) {
278		SCARG(uap, type) = (caddr_t)STACK_ALLOC();
279		if (error = copyout("ufs", SCARG(uap, type), sizeof("ufs")))
280			return (error);
281	} else if (strcmp(fsname, "nfs") == 0) {
282		struct ibcs2_nfs_args sna;
283		struct sockaddr_in sain;
284		struct nfs_args na;
285		struct sockaddr sa;
286
287		if (error = copyin(SCARG(uap, data), &sna, sizeof sna))
288			return (error);
289		if (error = copyin(sna.addr, &sain, sizeof sain))
290			return (error);
291		bcopy(&sain, &sa, sizeof sa);
292		sa.sa_len = sizeof(sain);
293		SCARG(uap, data) = (caddr_t)STACK_ALLOC();
294		na.addr = (struct sockaddr *)((int)SCARG(uap, data) + sizeof na);
295		na.sotype = SOCK_DGRAM;
296		na.proto = IPPROTO_UDP;
297		na.fh = (nfsv2fh_t *)sna.fh;
298		na.flags = sna.flags;
299		na.wsize = sna.wsize;
300		na.rsize = sna.rsize;
301		na.timeo = sna.timeo;
302		na.retrans = sna.retrans;
303		na.hostname = sna.hostname;
304
305		if (error = copyout(&sa, na.addr, sizeof sa))
306			return (error);
307		if (error = copyout(&na, SCARG(uap, data), sizeof na))
308			return (error);
309	}
310	return (mount(p, uap, retval));
311#else
312	return EINVAL;
313#endif
314}
315
316/*
317 * Read iBCS2-style directory entries.  We suck them into kernel space so
318 * that they can be massaged before being copied out to user code.  Like
319 * SunOS, we squish out `empty' entries.
320 *
321 * This is quite ugly, but what do you expect from compatibility code?
322 */
323
324int
325ibcs2_getdents(p, uap, retval)
326	struct proc *p;
327	register struct ibcs2_getdents_args *uap;
328	int *retval;
329{
330	register struct vnode *vp;
331	register caddr_t inp, buf;	/* BSD-format */
332	register int len, reclen;	/* BSD-format */
333	register caddr_t outp;		/* iBCS2-format */
334	register int resid;		/* iBCS2-format */
335	struct file *fp;
336	struct uio auio;
337	struct iovec aiov;
338	struct ibcs2_dirent idb;
339	off_t off;			/* true file offset */
340	int buflen, error, eofflag, blockoff;
341#define	BSD_DIRENT(cp)		((struct direct *)(cp))
342#define	IBCS2_RECLEN(reclen)	(reclen + sizeof(u_short))
343
344	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
345		return (error);
346	if ((fp->f_flag & FREAD) == 0)
347		return (EBADF);
348	vp = (struct vnode *)fp->f_data;
349	if (vp->v_type != VDIR)	/* XXX  vnode readdir op should do this */
350		return (EINVAL);
351
352	off = fp->f_offset;
353	blockoff = off % DIRBLKSIZ;
354	buflen = max(DIRBLKSIZ, SCARG(uap, nbytes) + blockoff);
355	buflen = min(buflen, MAXBSIZE);
356	buf = malloc(buflen, M_TEMP, M_WAITOK);
357	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
358again:
359	aiov.iov_base = buf;
360	aiov.iov_len = buflen;
361	auio.uio_iov = &aiov;
362	auio.uio_iovcnt = 1;
363	auio.uio_rw = UIO_READ;
364	auio.uio_segflg = UIO_SYSSPACE;
365	auio.uio_procp = p;
366	auio.uio_resid = buflen;
367	auio.uio_offset = off - (off_t)blockoff;
368	/*
369	 * First we read into the malloc'ed buffer, then
370	 * we massage it into user space, one record at a time.
371	 */
372	if (error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL))
373		goto out;
374	inp = buf;
375	inp += blockoff;
376	outp = SCARG(uap, buf);
377	resid = SCARG(uap, nbytes);
378	if ((len = buflen - auio.uio_resid - blockoff) == 0)
379		goto eof;
380	for (; len > 0; len -= reclen) {
381		reclen = BSD_DIRENT(inp)->d_reclen;
382		if (reclen & 3) {
383		        printf("ibcs2_getdents: reclen=%d\n", reclen);
384		        error = EFAULT;
385			goto out;
386		}
387		if (BSD_DIRENT(inp)->d_ino == 0) {
388			inp += reclen;	/* it is a hole; squish it out */
389			off += reclen;
390			continue;
391		}
392		if (reclen > len || resid < IBCS2_RECLEN(reclen)) {
393			/* entry too big for buffer, so just stop */
394			outp++;
395			break;
396		}
397		/*
398		 * Massage in place to make a iBCS2-shaped dirent (otherwise
399		 * we have to worry about touching user memory outside of
400		 * the copyout() call).
401		 */
402		idb.d_ino = (ibcs2_ino_t)BSD_DIRENT(inp)->d_ino;
403		idb.d_off = (ibcs2_off_t)off;
404		idb.d_reclen = (u_short)IBCS2_RECLEN(reclen);
405		if ((error = copyout((caddr_t)&idb, outp, 10)) != 0 ||
406		    (error = copyout(BSD_DIRENT(inp)->d_name, outp + 10,
407				     BSD_DIRENT(inp)->d_namlen + 1)) != 0)
408			goto out;
409		/* advance past this real entry */
410		off += reclen;
411		inp += reclen;
412		/* advance output past iBCS2-shaped entry */
413		outp += IBCS2_RECLEN(reclen);
414		resid -= IBCS2_RECLEN(reclen);
415	}
416	/* if we squished out the whole block, try again */
417	if (outp == SCARG(uap, buf))
418		goto again;
419	fp->f_offset = off;		/* update the vnode offset */
420eof:
421	*retval = SCARG(uap, nbytes) - resid;
422out:
423	VOP_UNLOCK(vp, 0, p);
424	free(buf, M_TEMP);
425	return (error);
426}
427
428int
429ibcs2_read(p, uap, retval)
430	struct proc *p;
431	struct ibcs2_read_args *uap;
432	int *retval;
433{
434	register struct vnode *vp;
435	register caddr_t inp, buf;	/* BSD-format */
436	register int len, reclen;	/* BSD-format */
437	register caddr_t outp;		/* iBCS2-format */
438	register int resid;		/* iBCS2-format */
439	struct file *fp;
440	struct uio auio;
441	struct iovec aiov;
442	struct ibcs2_direct {
443		ibcs2_ino_t ino;
444		char name[14];
445	} idb;
446	off_t off;			/* true file offset */
447	int buflen, error, eofflag, size, blockoff;
448
449	if (error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) {
450		if (error == EINVAL)
451			return read(p, (struct read_args *)uap, retval);
452		else
453			return error;
454	}
455	if ((fp->f_flag & FREAD) == 0)
456		return (EBADF);
457	vp = (struct vnode *)fp->f_data;
458	if (vp->v_type != VDIR)
459		return read(p, (struct read_args *)uap, retval);
460
461	DPRINTF(("ibcs2_read: read directory\n"));
462
463	off = fp->f_offset;
464	blockoff = off % DIRBLKSIZ;
465	buflen = max(DIRBLKSIZ, SCARG(uap, nbytes) + blockoff);
466	buflen = min(buflen, MAXBSIZE);
467	buf = malloc(buflen, M_TEMP, M_WAITOK);
468	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
469again:
470	aiov.iov_base = buf;
471	aiov.iov_len = buflen;
472	auio.uio_iov = &aiov;
473	auio.uio_iovcnt = 1;
474	auio.uio_rw = UIO_READ;
475	auio.uio_segflg = UIO_SYSSPACE;
476	auio.uio_procp = p;
477	auio.uio_resid = buflen;
478	auio.uio_offset = off - (off_t)blockoff;
479	/*
480	 * First we read into the malloc'ed buffer, then
481	 * we massage it into user space, one record at a time.
482	 */
483	if (error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, 0, 0)) {
484		DPRINTF(("VOP_READDIR failed: %d\n", error));
485		goto out;
486	}
487	inp = buf;
488	inp += blockoff;
489	outp = SCARG(uap, buf);
490	resid = SCARG(uap, nbytes);
491	if ((len = buflen - auio.uio_resid - blockoff) == 0)
492		goto eof;
493	for (; len > 0 && resid > 0; len -= reclen) {
494		reclen = BSD_DIRENT(inp)->d_reclen;
495		if (reclen & 3) {
496		        printf("ibcs2_read: reclen=%d\n", reclen);
497		        error = EFAULT;
498			goto out;
499		}
500		if (BSD_DIRENT(inp)->d_ino == 0) {
501			inp += reclen;	/* it is a hole; squish it out */
502			off += reclen;
503			continue;
504		}
505		if (reclen > len || resid < sizeof(struct ibcs2_direct)) {
506			/* entry too big for buffer, so just stop */
507			outp++;
508			break;
509		}
510		/*
511		 * Massage in place to make a iBCS2-shaped dirent (otherwise
512		 * we have to worry about touching user memory outside of
513		 * the copyout() call).
514		 *
515		 * TODO: if length(filename) > 14, then break filename into
516		 * multiple entries and set inode = 0xffff except last
517		 */
518		idb.ino = (BSD_DIRENT(inp)->d_ino > 0xfffe) ? 0xfffe :
519			BSD_DIRENT(inp)->d_ino;
520		(void)copystr(BSD_DIRENT(inp)->d_name, idb.name, 14, &size);
521		bzero(idb.name + size, 14 - size);
522		if (error = copyout(&idb, outp, sizeof(struct ibcs2_direct)))
523			goto out;
524		/* advance past this real entry */
525		off += reclen;
526		inp += reclen;
527		/* advance output past iBCS2-shaped entry */
528		outp += sizeof(struct ibcs2_direct);
529		resid -= sizeof(struct ibcs2_direct);
530	}
531	/* if we squished out the whole block, try again */
532	if (outp == SCARG(uap, buf))
533		goto again;
534	fp->f_offset = off;		/* update the vnode offset */
535eof:
536	*retval = SCARG(uap, nbytes) - resid;
537out:
538	VOP_UNLOCK(vp, 0, p);
539	free(buf, M_TEMP);
540	return (error);
541}
542
543int
544ibcs2_mknod(p, uap, retval)
545	struct proc *p;
546	struct ibcs2_mknod_args *uap;
547	int *retval;
548{
549        caddr_t sg = stackgap_init();
550
551        CHECKALTCREAT(p, &sg, SCARG(uap, path));
552	if (S_ISFIFO(SCARG(uap, mode))) {
553                struct mkfifo_args ap;
554                SCARG(&ap, path) = SCARG(uap, path);
555                SCARG(&ap, mode) = SCARG(uap, mode);
556		return mkfifo(p, &ap, retval);
557	} else {
558                struct mknod_args ap;
559                SCARG(&ap, path) = SCARG(uap, path);
560                SCARG(&ap, mode) = SCARG(uap, mode);
561                SCARG(&ap, dev) = SCARG(uap, dev);
562                return mknod(p, &ap, retval);
563	}
564}
565
566int
567ibcs2_getgroups(p, uap, retval)
568	struct proc *p;
569	struct ibcs2_getgroups_args *uap;
570	int *retval;
571{
572	int error, i;
573	ibcs2_gid_t *iset;
574	struct getgroups_args sa;
575	gid_t *gp;
576	caddr_t sg = stackgap_init();
577
578	SCARG(&sa, gidsetsize) = SCARG(uap, gidsetsize);
579	if (SCARG(uap, gidsetsize)) {
580		SCARG(&sa, gidset) = stackgap_alloc(&sg, NGROUPS_MAX *
581						    sizeof(gid_t *));
582	}
583	iset = stackgap_alloc(&sg, SCARG(uap, gidsetsize)*sizeof(ibcs2_gid_t));
584	if (error = getgroups(p, &sa, retval))
585		return error;
586	for (i = 0, gp = SCARG(&sa, gidset); i < retval[0]; i++)
587		iset[i] = (ibcs2_gid_t)*gp++;
588	if (retval[0] && (error = copyout((caddr_t)iset,
589					  (caddr_t)SCARG(uap, gidset),
590					  sizeof(ibcs2_gid_t) * retval[0])))
591		return error;
592        return 0;
593}
594
595int
596ibcs2_setgroups(p, uap, retval)
597	struct proc *p;
598	struct ibcs2_setgroups_args *uap;
599	int *retval;
600{
601	int error, i;
602	ibcs2_gid_t *iset;
603	struct setgroups_args sa;
604	gid_t *gp;
605	caddr_t sg = stackgap_init();
606
607	SCARG(&sa, gidsetsize) = SCARG(uap, gidsetsize);
608	SCARG(&sa, gidset) = stackgap_alloc(&sg, SCARG(&sa, gidsetsize) *
609					    sizeof(gid_t *));
610	iset = stackgap_alloc(&sg, SCARG(&sa, gidsetsize) *
611			      sizeof(ibcs2_gid_t *));
612	if (SCARG(&sa, gidsetsize)) {
613		if (error = copyin((caddr_t)SCARG(uap, gidset), (caddr_t)iset,
614				   sizeof(ibcs2_gid_t *) *
615				   SCARG(uap, gidsetsize)))
616			return error;
617	}
618	for (i = 0, gp = SCARG(&sa, gidset); i < SCARG(&sa, gidsetsize); i++)
619		*gp++ = (gid_t)iset[i];
620	return setgroups(p, &sa, retval);
621}
622
623int
624ibcs2_setuid(p, uap, retval)
625	struct proc *p;
626	struct ibcs2_setuid_args *uap;
627	int *retval;
628{
629	struct setuid_args sa;
630
631	SCARG(&sa, uid) = (uid_t)SCARG(uap, uid);
632	return setuid(p, &sa, retval);
633}
634
635int
636ibcs2_setgid(p, uap, retval)
637	struct proc *p;
638	struct ibcs2_setgid_args *uap;
639	int *retval;
640{
641	struct setgid_args sa;
642
643	SCARG(&sa, gid) = (gid_t)SCARG(uap, gid);
644	return setgid(p, &sa, retval);
645}
646
647int
648ibcs2_time(p, uap, retval)
649	struct proc *p;
650	struct ibcs2_time_args *uap;
651	int *retval;
652{
653	struct timeval tv;
654
655	microtime(&tv);
656	*retval = tv.tv_sec;
657	if (SCARG(uap, tp))
658		return copyout((caddr_t)&tv.tv_sec, (caddr_t)SCARG(uap, tp),
659			       sizeof(ibcs2_time_t));
660	else
661		return 0;
662}
663
664int
665ibcs2_pathconf(p, uap, retval)
666	struct proc *p;
667	struct ibcs2_pathconf_args *uap;
668	int *retval;
669{
670	SCARG(uap, name)++;	/* iBCS2 _PC_* defines are offset by one */
671        return pathconf(p, (struct pathconf_args *)uap, retval);
672}
673
674int
675ibcs2_fpathconf(p, uap, retval)
676	struct proc *p;
677	struct ibcs2_fpathconf_args *uap;
678	int *retval;
679{
680	SCARG(uap, name)++;	/* iBCS2 _PC_* defines are offset by one */
681        return fpathconf(p, (struct fpathconf_args *)uap, retval);
682}
683
684int
685ibcs2_sysconf(p, uap, retval)
686	struct proc *p;
687	struct ibcs2_sysconf_args *uap;
688	int *retval;
689{
690	int mib[2], value, len, error;
691	struct sysctl_args sa;
692	struct __getrlimit_args ga;
693
694	switch(SCARG(uap, name)) {
695	case IBCS2_SC_ARG_MAX:
696		mib[1] = KERN_ARGMAX;
697		break;
698
699	case IBCS2_SC_CHILD_MAX:
700	    {
701		caddr_t sg = stackgap_init();
702
703		SCARG(&ga, which) = RLIMIT_NPROC;
704		SCARG(&ga, rlp) = stackgap_alloc(&sg, sizeof(struct rlimit *));
705		if (error = getrlimit(p, &ga, retval))
706			return error;
707		*retval = SCARG(&ga, rlp)->rlim_cur;
708		return 0;
709	    }
710
711	case IBCS2_SC_CLK_TCK:
712		*retval = hz;
713		return 0;
714
715	case IBCS2_SC_NGROUPS_MAX:
716		mib[1] = KERN_NGROUPS;
717		break;
718
719	case IBCS2_SC_OPEN_MAX:
720	    {
721		caddr_t sg = stackgap_init();
722
723		SCARG(&ga, which) = RLIMIT_NOFILE;
724		SCARG(&ga, rlp) = stackgap_alloc(&sg, sizeof(struct rlimit *));
725		if (error = getrlimit(p, &ga, retval))
726			return error;
727		*retval = SCARG(&ga, rlp)->rlim_cur;
728		return 0;
729	    }
730
731	case IBCS2_SC_JOB_CONTROL:
732		mib[1] = KERN_JOB_CONTROL;
733		break;
734
735	case IBCS2_SC_SAVED_IDS:
736		mib[1] = KERN_SAVED_IDS;
737		break;
738
739	case IBCS2_SC_VERSION:
740		mib[1] = KERN_POSIX1;
741		break;
742
743	case IBCS2_SC_PASS_MAX:
744		*retval = 128;		/* XXX - should we create PASS_MAX ? */
745		return 0;
746
747	case IBCS2_SC_XOPEN_VERSION:
748		*retval = 2;		/* XXX: What should that be? */
749		return 0;
750
751	default:
752		return EINVAL;
753	}
754
755	mib[0] = CTL_KERN;
756	len = sizeof(value);
757	SCARG(&sa, name) = mib;
758	SCARG(&sa, namelen) = 2;
759	SCARG(&sa, old) = &value;
760	SCARG(&sa, oldlenp) = &len;
761	SCARG(&sa, new) = NULL;
762	SCARG(&sa, newlen) = 0;
763	if (error = __sysctl(p, &sa, retval))
764		return error;
765	*retval = value;
766	return 0;
767}
768
769int
770ibcs2_alarm(p, uap, retval)
771	struct proc *p;
772	struct ibcs2_alarm_args *uap;
773	int *retval;
774{
775	int error;
776        struct itimerval *itp, *oitp;
777	struct setitimer_args sa;
778	caddr_t sg = stackgap_init();
779
780        itp = stackgap_alloc(&sg, sizeof(*itp));
781	oitp = stackgap_alloc(&sg, sizeof(*oitp));
782        timerclear(&itp->it_interval);
783        itp->it_value.tv_sec = SCARG(uap, sec);
784        itp->it_value.tv_usec = 0;
785
786	SCARG(&sa, which) = ITIMER_REAL;
787	SCARG(&sa, itv) = itp;
788	SCARG(&sa, oitv) = oitp;
789        error = setitimer(p, &sa, retval);
790	if (error)
791		return error;
792        if (oitp->it_value.tv_usec)
793                oitp->it_value.tv_sec++;
794        *retval = oitp->it_value.tv_sec;
795        return 0;
796}
797
798int
799ibcs2_times(p, uap, retval)
800	struct proc *p;
801	struct ibcs2_times_args *uap;
802	int *retval;
803{
804	int error;
805	struct getrusage_args ga;
806	struct tms tms;
807        struct timeval t;
808	caddr_t sg = stackgap_init();
809        struct rusage *ru = stackgap_alloc(&sg, sizeof(*ru));
810#define CONVTCK(r)      (r.tv_sec * hz + r.tv_usec / (1000000 / hz))
811
812	SCARG(&ga, who) = RUSAGE_SELF;
813	SCARG(&ga, rusage) = ru;
814	error = getrusage(p, &ga, retval);
815	if (error)
816                return error;
817        tms.tms_utime = CONVTCK(ru->ru_utime);
818        tms.tms_stime = CONVTCK(ru->ru_stime);
819
820	SCARG(&ga, who) = RUSAGE_CHILDREN;
821        error = getrusage(p, &ga, retval);
822	if (error)
823		return error;
824        tms.tms_cutime = CONVTCK(ru->ru_utime);
825        tms.tms_cstime = CONVTCK(ru->ru_stime);
826
827	microtime(&t);
828        *retval = CONVTCK(t);
829
830	return copyout((caddr_t)&tms, (caddr_t)SCARG(uap, tp),
831		       sizeof(struct tms));
832}
833
834int
835ibcs2_stime(p, uap, retval)
836	struct proc *p;
837	struct ibcs2_stime_args *uap;
838	int *retval;
839{
840	int error;
841	struct settimeofday_args sa;
842	caddr_t sg = stackgap_init();
843
844	SCARG(&sa, tv) = stackgap_alloc(&sg, sizeof(*SCARG(&sa, tv)));
845	SCARG(&sa, tzp) = NULL;
846	if (error = copyin((caddr_t)SCARG(uap, timep),
847			   &(SCARG(&sa, tv)->tv_sec), sizeof(long)))
848		return error;
849	SCARG(&sa, tv)->tv_usec = 0;
850	if (error = settimeofday(p, &sa, retval))
851		return EPERM;
852	return 0;
853}
854
855int
856ibcs2_utime(p, uap, retval)
857	struct proc *p;
858	struct ibcs2_utime_args *uap;
859	int *retval;
860{
861	int error;
862	struct utimes_args sa;
863	struct timeval *tp;
864	caddr_t sg = stackgap_init();
865
866        CHECKALTEXIST(p, &sg, SCARG(uap, path));
867	SCARG(&sa, path) = SCARG(uap, path);
868	if (SCARG(uap, buf)) {
869		struct ibcs2_utimbuf ubuf;
870
871		if (error = copyin((caddr_t)SCARG(uap, buf), (caddr_t)&ubuf,
872				   sizeof(ubuf)))
873			return error;
874		SCARG(&sa, tptr) = stackgap_alloc(&sg,
875						  2 * sizeof(struct timeval *));
876		tp = (struct timeval *)SCARG(&sa, tptr);
877		tp->tv_sec = ubuf.actime;
878		tp->tv_usec = 0;
879		tp++;
880		tp->tv_sec = ubuf.modtime;
881		tp->tv_usec = 0;
882	} else
883		SCARG(&sa, tptr) = NULL;
884	return utimes(p, &sa, retval);
885}
886
887int
888ibcs2_nice(p, uap, retval)
889	struct proc *p;
890	struct ibcs2_nice_args *uap;
891	int *retval;
892{
893	int error;
894	struct setpriority_args sa;
895
896	SCARG(&sa, which) = PRIO_PROCESS;
897	SCARG(&sa, who) = 0;
898	SCARG(&sa, prio) = p->p_nice + SCARG(uap, incr);
899	if (error = setpriority(p, &sa, retval))
900		return EPERM;
901	*retval = p->p_nice;
902	return 0;
903}
904
905/*
906 * iBCS2 getpgrp, setpgrp, setsid, and setpgid
907 */
908
909int
910ibcs2_pgrpsys(p, uap, retval)
911	struct proc *p;
912	struct ibcs2_pgrpsys_args *uap;
913	int *retval;
914{
915	switch (SCARG(uap, type)) {
916	case 0:			/* getpgrp */
917		*retval = p->p_pgrp->pg_id;
918		return 0;
919
920	case 1:			/* setpgrp */
921	    {
922		struct setpgid_args sa;
923
924		SCARG(&sa, pid) = 0;
925		SCARG(&sa, pgid) = 0;
926		setpgid(p, &sa, retval);
927		*retval = p->p_pgrp->pg_id;
928		return 0;
929	    }
930
931	case 2:			/* setpgid */
932	    {
933		struct setpgid_args sa;
934
935		SCARG(&sa, pid) = SCARG(uap, pid);
936		SCARG(&sa, pgid) = SCARG(uap, pgid);
937		return setpgid(p, &sa, retval);
938	    }
939
940	case 3:			/* setsid */
941		return setsid(p, NULL, retval);
942
943	default:
944		return EINVAL;
945	}
946}
947
948/*
949 * XXX - need to check for nested calls
950 */
951
952int
953ibcs2_plock(p, uap, retval)
954	struct proc *p;
955	struct ibcs2_plock_args *uap;
956	int *retval;
957{
958	int error;
959#define IBCS2_UNLOCK	0
960#define IBCS2_PROCLOCK	1
961#define IBCS2_TEXTLOCK	2
962#define IBCS2_DATALOCK	4
963
964
965        if (error = suser(p->p_ucred, &p->p_acflag))
966                return EPERM;
967	switch(SCARG(uap, cmd)) {
968	case IBCS2_UNLOCK:
969	case IBCS2_PROCLOCK:
970	case IBCS2_TEXTLOCK:
971	case IBCS2_DATALOCK:
972		return 0;	/* XXX - TODO */
973	}
974	return EINVAL;
975}
976
977int
978ibcs2_uadmin(p, uap, retval)
979	struct proc *p;
980	struct ibcs2_uadmin_args *uap;
981	int *retval;
982{
983#define SCO_A_REBOOT        1
984#define SCO_A_SHUTDOWN      2
985#define SCO_A_REMOUNT       4
986#define SCO_A_CLOCK         8
987#define SCO_A_SETCONFIG     128
988#define SCO_A_GETDEV        130
989
990#define SCO_AD_HALT         0
991#define SCO_AD_BOOT         1
992#define SCO_AD_IBOOT        2
993#define SCO_AD_PWRDOWN      3
994#define SCO_AD_PWRNAP       4
995
996#define SCO_AD_PANICBOOT    1
997
998#define SCO_AD_GETBMAJ      0
999#define SCO_AD_GETCMAJ      1
1000
1001        if (suser(p->p_ucred, &p->p_acflag))
1002                return EPERM;
1003
1004	switch(SCARG(uap, cmd)) {
1005	case SCO_A_REBOOT:
1006	case SCO_A_SHUTDOWN:
1007		switch(SCARG(uap, func)) {
1008			struct reboot_args r;
1009		case SCO_AD_HALT:
1010		case SCO_AD_PWRDOWN:
1011		case SCO_AD_PWRNAP:
1012			r.opt = RB_HALT;
1013			reboot(p, &r, retval);
1014		case SCO_AD_BOOT:
1015		case SCO_AD_IBOOT:
1016			r.opt = RB_AUTOBOOT;
1017			reboot(p, &r, retval);
1018		}
1019		return EINVAL;
1020	case SCO_A_REMOUNT:
1021	case SCO_A_CLOCK:
1022	case SCO_A_SETCONFIG:
1023		return 0;
1024	case SCO_A_GETDEV:
1025		return EINVAL;	/* XXX - TODO */
1026	}
1027	return EINVAL;
1028}
1029
1030int
1031ibcs2_sysfs(p, uap, retval)
1032	struct proc *p;
1033	struct ibcs2_sysfs_args *uap;
1034	int *retval;
1035{
1036#define IBCS2_GETFSIND        1
1037#define IBCS2_GETFSTYP        2
1038#define IBCS2_GETNFSTYP       3
1039
1040	switch(SCARG(uap, cmd)) {
1041	case IBCS2_GETFSIND:
1042	case IBCS2_GETFSTYP:
1043	case IBCS2_GETNFSTYP:
1044	}
1045	return EINVAL;		/* XXX - TODO */
1046}
1047
1048int
1049ibcs2_unlink(p, uap, retval)
1050	struct proc *p;
1051	struct ibcs2_unlink_args *uap;
1052	int *retval;
1053{
1054        caddr_t sg = stackgap_init();
1055
1056	CHECKALTEXIST(p, &sg, SCARG(uap, path));
1057	return unlink(p, (struct unlink_args *)uap, retval);
1058}
1059
1060int
1061ibcs2_chdir(p, uap, retval)
1062	struct proc *p;
1063	struct ibcs2_chdir_args *uap;
1064	int *retval;
1065{
1066        caddr_t sg = stackgap_init();
1067
1068	CHECKALTEXIST(p, &sg, SCARG(uap, path));
1069	return chdir(p, (struct chdir_args *)uap, retval);
1070}
1071
1072int
1073ibcs2_chmod(p, uap, retval)
1074	struct proc *p;
1075	struct ibcs2_chmod_args *uap;
1076	int *retval;
1077{
1078        caddr_t sg = stackgap_init();
1079
1080	CHECKALTEXIST(p, &sg, SCARG(uap, path));
1081	return chmod(p, (struct chmod_args *)uap, retval);
1082}
1083
1084int
1085ibcs2_chown(p, uap, retval)
1086	struct proc *p;
1087	struct ibcs2_chown_args *uap;
1088	int *retval;
1089{
1090        caddr_t sg = stackgap_init();
1091
1092	CHECKALTEXIST(p, &sg, SCARG(uap, path));
1093	return chown(p, (struct chown_args *)uap, retval);
1094}
1095
1096int
1097ibcs2_rmdir(p, uap, retval)
1098	struct proc *p;
1099	struct ibcs2_rmdir_args *uap;
1100	int *retval;
1101{
1102        caddr_t sg = stackgap_init();
1103
1104	CHECKALTEXIST(p, &sg, SCARG(uap, path));
1105	return rmdir(p, (struct rmdir_args *)uap, retval);
1106}
1107
1108int
1109ibcs2_mkdir(p, uap, retval)
1110	struct proc *p;
1111	struct ibcs2_mkdir_args *uap;
1112	int *retval;
1113{
1114        caddr_t sg = stackgap_init();
1115
1116	CHECKALTCREAT(p, &sg, SCARG(uap, path));
1117	return mkdir(p, (struct mkdir_args *)uap, retval);
1118}
1119
1120int
1121ibcs2_symlink(p, uap, retval)
1122	struct proc *p;
1123	struct ibcs2_symlink_args *uap;
1124	int *retval;
1125{
1126        caddr_t sg = stackgap_init();
1127
1128	CHECKALTEXIST(p, &sg, SCARG(uap, path));
1129	CHECKALTCREAT(p, &sg, SCARG(uap, link));
1130	return symlink(p, (struct symlink_args *)uap, retval);
1131}
1132
1133int
1134ibcs2_rename(p, uap, retval)
1135	struct proc *p;
1136	struct ibcs2_rename_args *uap;
1137	int *retval;
1138{
1139        caddr_t sg = stackgap_init();
1140
1141	CHECKALTEXIST(p, &sg, SCARG(uap, from));
1142	CHECKALTCREAT(p, &sg, SCARG(uap, to));
1143	return rename(p, (struct rename_args *)uap, retval);
1144}
1145
1146int
1147ibcs2_readlink(p, uap, retval)
1148	struct proc *p;
1149	struct ibcs2_readlink_args *uap;
1150	int *retval;
1151{
1152        caddr_t sg = stackgap_init();
1153
1154	CHECKALTEXIST(p, &sg, SCARG(uap, path));
1155	return readlink(p, (struct readlink_args *) uap, retval);
1156}
1157