xref: /netbsd-src/sys/compat/sunos/sunos_misc.c (revision 201fac6e3d8ac6a5503c4810b0378f73101d50c3)
1 /*	$NetBSD: sunos_misc.c,v 1.39 1994/12/15 09:41:36 pk Exp $	*/
2 
3 /*
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  *	@(#)sunos_misc.c	8.1 (Berkeley) 6/18/93
45  *
46  *	Header: sunos_misc.c,v 1.16 93/04/07 02:46:27 torek Exp
47  */
48 
49 /*
50  * SunOS compatibility module.
51  *
52  * SunOS system calls that are implemented differently in BSD are
53  * handled here.
54  */
55 
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/namei.h>
59 #include <sys/proc.h>
60 #include <sys/dir.h>
61 #include <sys/file.h>
62 #include <sys/stat.h>
63 #include <sys/filedesc.h>
64 #include <sys/ioctl.h>
65 #include <sys/kernel.h>
66 #include <sys/exec.h>
67 #include <sys/malloc.h>
68 #include <sys/mbuf.h>
69 #include <sys/mman.h>
70 #include <sys/mount.h>
71 #include <sys/ptrace.h>
72 #include <sys/resource.h>
73 #include <sys/resourcevar.h>
74 #include <sys/signal.h>
75 #include <sys/signalvar.h>
76 #include <sys/socket.h>
77 #include <sys/vnode.h>
78 #include <sys/uio.h>
79 #include <sys/wait.h>
80 #include <sys/utsname.h>
81 #include <sys/unistd.h>
82 #include <sys/syscallargs.h>
83 #include <compat/sunos/sunos.h>
84 #include <compat/sunos/sunos_syscallargs.h>
85 
86 #include <netinet/in.h>
87 
88 #include <miscfs/specfs/specdev.h>
89 
90 #include <nfs/rpcv2.h>
91 #include <nfs/nfsv2.h>
92 #include <nfs/nfs.h>
93 
94 #include <vm/vm.h>
95 
96 int
97 sunos_wait4(p, uap, retval)
98 	struct proc *p;
99 	struct sunos_wait4_args *uap;
100 	register_t *retval;
101 {
102 	if (SCARG(uap, pid) == 0)
103 		SCARG(uap, pid) = WAIT_ANY;
104 	return (wait4(p, uap, retval, 1));
105 }
106 
107 int
108 sunos_creat(p, uap, retval)
109 	struct proc *p;
110 	struct sunos_creat_args *uap;
111 	register_t *retval;
112 {
113 	struct sunos_open_args ouap;
114 
115 	SCARG(&ouap, path) = SCARG(uap, path);
116 	SCARG(&ouap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
117 	SCARG(&ouap, mode) = SCARG(uap, mode);
118 	return (open(p, &ouap, retval));
119 }
120 
121 int
122 sunos_execv(p, uap, retval)
123 	struct proc *p;
124 	struct sunos_execv_args *uap;
125 	register_t *retval;
126 {
127 	struct execve_args ouap;
128 
129 	SCARG(&ouap, path) = SCARG(uap, path);
130 	SCARG(&ouap, argp) = SCARG(uap, argp);
131 	SCARG(&ouap, envp) = NULL;
132 	return (execve(p, &ouap, retval));
133 }
134 
135 int
136 sunos_omsync(p, uap, retval)
137 	struct proc *p;
138 	struct sunos_omsync_args *uap;
139 	register_t *retval;
140 {
141 	struct msync_args ouap;
142 
143 	if (SCARG(uap, flags))
144 		return (EINVAL);
145 	SCARG(&ouap, addr) = SCARG(uap, addr);
146 	SCARG(&ouap, len) = SCARG(uap, len);
147 	return (msync(p, &ouap, retval));
148 }
149 
150 int
151 sunos_unmount(p, uap, retval)
152 	struct proc *p;
153 	struct sunos_unmount_args *uap;
154 	register_t *retval;
155 {
156 	SCARG(uap, flags) = 0;
157 	return (unmount(p, (struct unmount_args *)uap, retval));
158 }
159 
160 int
161 sunos_mount(p, uap, retval)
162 	struct proc *p;
163 	struct sunos_mount_args *uap;
164 	register_t *retval;
165 {
166 	int oflags = SCARG(uap, flags), nflags, error;
167 	extern char sigcode[], esigcode[];
168 	char fsname[MFSNAMELEN];
169 
170 #define	szsigcode	(esigcode - sigcode)
171 
172 	if (oflags & (SUNM_NOSUB | SUNM_SYS5))
173 		return (EINVAL);
174 	if ((oflags & SUNM_NEWTYPE) == 0)
175 		return (EINVAL);
176 	nflags = 0;
177 	if (oflags & SUNM_RDONLY)
178 		nflags |= MNT_RDONLY;
179 	if (oflags & SUNM_NOSUID)
180 		nflags |= MNT_NOSUID;
181 	if (oflags & SUNM_REMOUNT)
182 		nflags |= MNT_UPDATE;
183 	SCARG(uap, flags) = nflags;
184 
185 	if (error = copyinstr((caddr_t)SCARG(uap, type), fsname,
186 	    sizeof fsname, (u_int *)0))
187 		return (error);
188 
189 	if (strcmp(fsname, "4.2") == 0) {
190 		SCARG(uap, type) = STACKGAPBASE;
191 		if (error = copyout("ufs", SCARG(uap, type), sizeof("ufs")))
192 			return (error);
193 	} else if (strcmp(fsname, "nfs") == 0) {
194 		struct sunos_nfs_args sna;
195 		struct sockaddr_in sain;
196 		struct nfs_args na;
197 		struct sockaddr sa;
198 
199 		if (error = copyin(SCARG(uap, data), &sna, sizeof sna))
200 			return (error);
201 		if (error = copyin(sna.addr, &sain, sizeof sain))
202 			return (error);
203 		bcopy(&sain, &sa, sizeof sa);
204 		sa.sa_len = sizeof(sain);
205 		SCARG(uap, data) = STACKGAPBASE;
206 		na.addr = (struct sockaddr *)((int)SCARG(uap, data) + sizeof na);
207 		na.addrlen = sizeof(struct sockaddr);
208 		na.sotype = SOCK_DGRAM;
209 		na.proto = IPPROTO_UDP;
210 		na.fh = (nfsv2fh_t *)sna.fh;
211 		na.flags = sna.flags;
212 		na.wsize = sna.wsize;
213 		na.rsize = sna.rsize;
214 		na.timeo = sna.timeo;
215 		na.retrans = sna.retrans;
216 		na.hostname = sna.hostname;
217 
218 		if (error = copyout(&sa, na.addr, sizeof sa))
219 			return (error);
220 		if (error = copyout(&na, SCARG(uap, data), sizeof na))
221 			return (error);
222 	}
223 	return (mount(p, (struct mount_args *)uap, retval));
224 }
225 
226 #if defined(NFSCLIENT)
227 int
228 async_daemon(p, uap, retval)
229 	struct proc *p;
230 	void *uap;
231 	register_t *retval;
232 {
233 	struct nfssvc_args ouap;
234 
235 	SCARG(&ouap, flag) = NFSSVC_BIOD;
236 	SCARG(&ouap, argp) = NULL;
237 	return nfssvc(p, &ouap, retval);
238 }
239 #endif /* NFSCLIENT */
240 
241 int
242 sunos_sigpending(p, uap, retval)
243 	struct proc *p;
244 	struct sunos_sigpending_args *uap;
245 	register_t *retval;
246 {
247 	int mask = p->p_siglist & p->p_sigmask;
248 
249 	return (copyout((caddr_t)&mask, (caddr_t)SCARG(uap, mask), sizeof(int)));
250 }
251 
252 /*
253  * Read Sun-style directory entries.  We suck them into kernel space so
254  * that they can be massaged before being copied out to user code.  Like
255  * SunOS, we squish out `empty' entries.
256  *
257  * This is quite ugly, but what do you expect from compatibility code?
258  */
259 int
260 sunos_getdents(p, uap, retval)
261 	struct proc *p;
262 	register struct sunos_getdents_args *uap;
263 	register_t *retval;
264 {
265 	register struct vnode *vp;
266 	register caddr_t inp, buf;	/* BSD-format */
267 	register int len, reclen;	/* BSD-format */
268 	register caddr_t outp;		/* Sun-format */
269 	register int resid;		/* Sun-format */
270 	struct file *fp;
271 	struct uio auio;
272 	struct iovec aiov;
273 	off_t off;			/* true file offset */
274 	long soff;			/* Sun file offset */
275 	int buflen, error, eofflag;
276 #define	BSD_DIRENT(cp) ((struct dirent *)(cp))
277 #define	SUNOS_RECLEN(reclen) (reclen + sizeof(long))
278 
279 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
280 		return (error);
281 	if ((fp->f_flag & FREAD) == 0)
282 		return (EBADF);
283 	vp = (struct vnode *)fp->f_data;
284 	if (vp->v_type != VDIR)	/* XXX  vnode readdir op should do this */
285 		return (EINVAL);
286 	buflen = min(MAXBSIZE, SCARG(uap, nbytes));
287 	buf = malloc(buflen, M_TEMP, M_WAITOK);
288 	VOP_LOCK(vp);
289 	off = fp->f_offset;
290 again:
291 	aiov.iov_base = buf;
292 	aiov.iov_len = buflen;
293 	auio.uio_iov = &aiov;
294 	auio.uio_iovcnt = 1;
295 	auio.uio_rw = UIO_READ;
296 	auio.uio_segflg = UIO_SYSSPACE;
297 	auio.uio_procp = p;
298 	auio.uio_resid = buflen;
299 	auio.uio_offset = off;
300 	/*
301 	 * First we read into the malloc'ed buffer, then
302 	 * we massage it into user space, one record at a time.
303 	 */
304 	if (error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
305 	    (u_long *)0, 0))
306 		goto out;
307 	inp = buf;
308 	outp = SCARG(uap, buf);
309 	resid = SCARG(uap, nbytes);
310 	if ((len = buflen - auio.uio_resid) == 0)
311 		goto eof;
312 	for (; len > 0; len -= reclen) {
313 		reclen = ((struct dirent *)inp)->d_reclen;
314 		if (reclen & 3)
315 			panic("sunos_getdents");
316 		off += reclen;		/* each entry points to next */
317 		if (BSD_DIRENT(inp)->d_fileno == 0) {
318 			inp += reclen;	/* it is a hole; squish it out */
319 			continue;
320 		}
321 		if (reclen > len || resid < SUNOS_RECLEN(reclen)) {
322 			/* entry too big for buffer, so just stop */
323 			outp++;
324 			break;
325 		}
326 		/*
327 		 * Massage in place to make a Sun-shaped dirent (otherwise
328 		 * we have to worry about touching user memory outside of
329 		 * the copyout() call).
330 		 */
331 		BSD_DIRENT(inp)->d_reclen = SUNOS_RECLEN(reclen);
332 #if notdef
333 		BSD_DIRENT(inp)->d_type = 0; 	/* 4.4 specific */
334 #endif
335 		soff = off;
336 		if ((error = copyout((caddr_t)&soff, outp, sizeof soff)) != 0 ||
337 		    (error = copyout(inp, outp + sizeof soff, reclen)) != 0)
338 			goto out;
339 		/* advance past this real entry */
340 		inp += reclen;
341 		/* advance output past Sun-shaped entry */
342 		outp += SUNOS_RECLEN(reclen);
343 		resid -= SUNOS_RECLEN(reclen);
344 	}
345 	/* if we squished out the whole block, try again */
346 	if (outp == SCARG(uap, buf))
347 		goto again;
348 	fp->f_offset = off;		/* update the vnode offset */
349 eof:
350 	*retval = SCARG(uap, nbytes) - resid;
351 out:
352 	VOP_UNLOCK(vp);
353 	free(buf, M_TEMP);
354 	return (error);
355 }
356 
357 #define	SUNOS__MAP_NEW	0x80000000	/* if not, old mmap & cannot handle */
358 
359 int
360 sunos_mmap(p, uap, retval)
361 	register struct proc *p;
362 	register struct sunos_mmap_args *uap;
363 	register_t *retval;
364 {
365 	struct mmap_args ouap;
366 	register struct filedesc *fdp;
367 	register struct file *fp;
368 	register struct vnode *vp;
369 
370 	/*
371 	 * Verify the arguments.
372 	 */
373 	if (SCARG(uap, prot) & ~(PROT_READ|PROT_WRITE|PROT_EXEC))
374 		return (EINVAL);			/* XXX still needed? */
375 
376 	if ((SCARG(uap, flags) & SUNOS__MAP_NEW) == 0)
377 		return (EINVAL);
378 
379 	SCARG(&ouap, flags) = SCARG(uap, flags) & ~SUNOS__MAP_NEW;
380 	SCARG(&ouap, addr) = SCARG(uap, addr);
381 
382 	if ((SCARG(&ouap, flags) & MAP_FIXED) == 0 &&
383 	    SCARG(&ouap, addr) != 0 &&
384 	    SCARG(&ouap, addr) < (caddr_t)round_page(p->p_vmspace->vm_daddr+MAXDSIZ))
385 		SCARG(&ouap, addr) = (caddr_t)round_page(p->p_vmspace->vm_daddr+MAXDSIZ);
386 
387 	SCARG(&ouap, len) = SCARG(uap, len);
388 	SCARG(&ouap, prot) = SCARG(uap, prot);
389 	SCARG(&ouap, fd) = SCARG(uap, fd);
390 	SCARG(&ouap, pos) = SCARG(uap, pos);
391 
392 	/*
393 	 * Special case: if fd refers to /dev/zero, map as MAP_ANON.  (XXX)
394 	 */
395 	fdp = p->p_fd;
396 	if ((unsigned)SCARG(&ouap, fd) < fdp->fd_nfiles &&		/*XXX*/
397 	    (fp = fdp->fd_ofiles[SCARG(&ouap, fd)]) != NULL &&		/*XXX*/
398 	    fp->f_type == DTYPE_VNODE &&				/*XXX*/
399 	    (vp = (struct vnode *)fp->f_data)->v_type == VCHR &&	/*XXX*/
400 	    iszerodev(vp->v_rdev)) {					/*XXX*/
401 		SCARG(&ouap, flags) |= MAP_ANON;
402 		SCARG(&ouap, fd) = -1;
403 	}
404 
405 	return (mmap(p, &ouap, retval));
406 }
407 
408 #define	MC_SYNC		1
409 #define	MC_LOCK		2
410 #define	MC_UNLOCK	3
411 #define	MC_ADVISE	4
412 #define	MC_LOCKAS	5
413 #define	MC_UNLOCKAS	6
414 
415 int
416 sunos_mctl(p, uap, retval)
417 	register struct proc *p;
418 	register struct sunos_mctl_args *uap;
419 	register_t *retval;
420 {
421 
422 	switch (SCARG(uap, func)) {
423 	case MC_ADVISE:		/* ignore for now */
424 		return (0);
425 	case MC_SYNC:		/* translate to msync */
426 		return (msync(p, uap, retval));
427 	default:
428 		return (EINVAL);
429 	}
430 }
431 
432 int
433 sunos_setsockopt(p, uap, retval)
434 	struct proc *p;
435 	register struct sunos_setsockopt_args *uap;
436 	register_t *retval;
437 {
438 	struct file *fp;
439 	struct mbuf *m = NULL;
440 	int error;
441 
442 	if (error = getsock(p->p_fd, SCARG(uap, s), &fp))
443 		return (error);
444 #define	SO_DONTLINGER (~SO_LINGER)
445 	if (SCARG(uap, name) == SO_DONTLINGER) {
446 		m = m_get(M_WAIT, MT_SOOPTS);
447 		if (m == NULL)
448 			return (ENOBUFS);
449 		mtod(m, struct linger *)->l_onoff = 0;
450 		m->m_len = sizeof(struct linger);
451 		return (sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
452 		    SO_LINGER, m));
453 	}
454 	if (SCARG(uap, level) == IPPROTO_IP) {
455 #define		SUNOS_IP_MULTICAST_IF		2
456 #define		SUNOS_IP_MULTICAST_TTL		3
457 #define		SUNOS_IP_MULTICAST_LOOP		4
458 #define		SUNOS_IP_ADD_MEMBERSHIP		5
459 #define		SUNOS_IP_DROP_MEMBERSHIP	6
460 		static int ipoptxlat[] = {
461 			IP_MULTICAST_IF,
462 			IP_MULTICAST_TTL,
463 			IP_MULTICAST_LOOP,
464 			IP_ADD_MEMBERSHIP,
465 			IP_DROP_MEMBERSHIP
466 		};
467 		if (SCARG(uap, name) >= SUNOS_IP_MULTICAST_IF &&
468 		    SCARG(uap, name) <= SUNOS_IP_DROP_MEMBERSHIP) {
469 			SCARG(uap, name) =
470 			    ipoptxlat[SCARG(uap, name) - SUNOS_IP_MULTICAST_IF];
471 		}
472 	}
473 	if (SCARG(uap, valsize) > MLEN)
474 		return (EINVAL);
475 	if (SCARG(uap, val)) {
476 		m = m_get(M_WAIT, MT_SOOPTS);
477 		if (m == NULL)
478 			return (ENOBUFS);
479 		if (error = copyin(SCARG(uap, val), mtod(m, caddr_t),
480 		    (u_int)SCARG(uap, valsize))) {
481 			(void) m_free(m);
482 			return (error);
483 		}
484 		m->m_len = SCARG(uap, valsize);
485 	}
486 	return (sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
487 	    SCARG(uap, name), m));
488 }
489 
490 int
491 sunos_fchroot(p, uap, retval)
492 	register struct proc *p;
493 	register struct sunos_fchroot_args *uap;
494 	register_t *retval;
495 {
496 	register struct filedesc *fdp = p->p_fd;
497 	register struct vnode *vp;
498 	struct file *fp;
499 	int error;
500 
501 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
502 		return (error);
503 	if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
504 		return (error);
505 	vp = (struct vnode *)fp->f_data;
506 	VOP_LOCK(vp);
507 	if (vp->v_type != VDIR)
508 		error = ENOTDIR;
509 	else
510 		error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
511 	VOP_UNLOCK(vp);
512 	if (error)
513 		return (error);
514 	VREF(vp);
515 	if (fdp->fd_rdir != NULL)
516 		vrele(fdp->fd_rdir);
517 	fdp->fd_rdir = vp;
518 	return (0);
519 }
520 
521 /*
522  * XXX: This needs cleaning up.
523  */
524 int
525 sunos_auditsys(...)
526 {
527 	return 0;
528 }
529 
530 int
531 sunos_uname(p, uap, retval)
532 	struct proc *p;
533 	struct sunos_uname_args *uap;
534 	register_t *retval;
535 {
536 	struct sunos_utsname sut;
537 	extern char ostype[], machine[], osrelease[];
538 
539 	bzero(&sut, sizeof(sut));
540 
541 	bcopy(ostype, sut.sysname, sizeof(sut.sysname) - 1);
542 	bcopy(hostname, sut.nodename, sizeof(sut.nodename));
543 	sut.nodename[sizeof(sut.nodename)-1] = '\0';
544 	bcopy(osrelease, sut.release, sizeof(sut.release) - 1);
545 	bcopy("1", sut.version, sizeof(sut.version) - 1);
546 	bcopy(machine, sut.machine, sizeof(sut.machine) - 1);
547 
548 	return copyout((caddr_t)&sut, (caddr_t)SCARG(uap, name),
549 	    sizeof(struct sunos_utsname));
550 }
551 
552 int
553 sunos_setpgid(p, uap, retval)
554 	struct proc *p;
555 	struct sunos_setpgid_args *uap;
556 	register_t *retval;
557 {
558 	/*
559 	 * difference to our setpgid call is to include backwards
560 	 * compatibility to pre-setsid() binaries. Do setsid()
561 	 * instead of setpgid() in those cases where the process
562 	 * tries to create a new session the old way.
563 	 */
564 	if (!SCARG(uap, pgid) && (!SCARG(uap, pid) || SCARG(uap, pid) == p->p_pid))
565 		return setsid(p, uap, retval);
566 	else
567 		return setpgid(p, uap, retval);
568 }
569 
570 int
571 sunos_open(p, uap, retval)
572 	struct proc *p;
573 	struct sunos_open_args *uap;
574 	register_t *retval;
575 {
576 	int l, r;
577 	int noctty;
578 	int ret;
579 
580 	/* convert mode into NetBSD mode */
581 	l = SCARG(uap, flags);
582 	noctty = l & 0x8000;
583 	r =	(l & (0x0001 | 0x0002 | 0x0008 | 0x0040 | 0x0200 | 0x0400 | 0x0800));
584 	r |=	((l & (0x0004 | 0x1000 | 0x4000)) ? O_NONBLOCK : 0);
585 	r |=	((l & 0x0080) ? O_SHLOCK : 0);
586 	r |=	((l & 0x0100) ? O_EXLOCK : 0);
587 	r |=	((l & 0x2000) ? O_FSYNC : 0);
588 
589 	SCARG(uap, flags) = r;
590 	ret = open(p, (struct open_args *)uap, retval);
591 
592 	if (!ret && !noctty && SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
593 		struct filedesc *fdp = p->p_fd;
594 		struct file *fp = fdp->fd_ofiles[*retval];
595 
596 		/* ignore any error, just give it a try */
597 		if (fp->f_type == DTYPE_VNODE)
598 			(fp->f_ops->fo_ioctl)(fp, TIOCSCTTY, (caddr_t) 0, p);
599 	}
600 	return ret;
601 }
602 
603 #if defined (NFSSERVER)
604 int
605 sunos_nfssvc(p, uap, retval)
606 	struct proc *p;
607 	struct sunos_nfssvc_args *uap;
608 	register_t *retval;
609 {
610 	struct nfssvc_args outuap;
611 	struct nfsd_srvargs nfsdarg;
612 	struct sockaddr sa;
613 	int error;
614 	extern char sigcode[], esigcode[];
615 
616 #if 0
617 	bzero(&outuap, sizeof outuap);
618 	SCARG(&outuap, fd) = SCARG(uap, fd);
619 	SCARG(&outuap, mskval) = STACKGAPBASE;
620 	SCARG(&outuap, msklen) = sizeof sa;
621 	SCARG(&outuap, mtchval) = SCARG(&outuap, mskval) + sizeof sa;
622 	SCARG(&outuap, mtchlen) = sizeof sa;
623 
624 	bzero(&sa, sizeof sa);
625 	if (error = copyout(&sa, SCARG(&outuap, mskval), SCARG(&outuap, msklen)))
626 		return (error);
627 	if (error = copyout(&sa, SCARG(&outuap, mtchval), SCARG(&outuap, mtchlen)))
628 		return (error);
629 
630 	return nfssvc(p, &outuap, retval);
631 #else
632 	return (ENOSYS);
633 #endif
634 }
635 #endif /* NFSSERVER */
636 
637 int
638 sunos_ustat(p, uap, retval)
639 	struct proc *p;
640 	struct sunos_ustat_args *uap;
641 	register_t *retval;
642 {
643 	struct sunos_ustat us;
644 	int error;
645 
646 	bzero(&us, sizeof us);
647 
648 	/*
649 	 * XXX: should set f_tfree and f_tinode at least
650 	 * How do we translate dev -> fstat? (and then to sunos_ustat)
651 	 */
652 
653 	if (error = copyout(&us, SCARG(uap, buf), sizeof us))
654 		return (error);
655 	return 0;
656 }
657 
658 int
659 sunos_quotactl(p, uap, retval)
660 	struct proc *p;
661 	struct sunos_quotactl_args *uap;
662 	register_t *retval;
663 {
664 	return EINVAL;
665 }
666 
667 int
668 sunos_vhangup(p, uap, retval)
669 	struct proc *p;
670 	void *uap;
671 	register_t *retval;
672 {
673 	return 0;
674 }
675 
676 static
677 sunstatfs(sp, buf)
678 	struct statfs *sp;
679 	caddr_t buf;
680 {
681 	struct sunos_statfs ssfs;
682 
683 	bzero(&ssfs, sizeof ssfs);
684 	ssfs.f_type = 0;
685 	ssfs.f_bsize = sp->f_bsize;
686 	ssfs.f_blocks = sp->f_blocks;
687 	ssfs.f_bfree = sp->f_bfree;
688 	ssfs.f_bavail = sp->f_bavail;
689 	ssfs.f_files = sp->f_files;
690 	ssfs.f_ffree = sp->f_ffree;
691 	ssfs.f_fsid = sp->f_fsid;
692 	return copyout((caddr_t)&ssfs, buf, sizeof ssfs);
693 }
694 
695 int
696 sunos_statfs(p, uap, retval)
697 	struct proc *p;
698 	struct sunos_statfs_args *uap;
699 	register_t *retval;
700 {
701 	register struct mount *mp;
702 	register struct statfs *sp;
703 	int error;
704 	struct nameidata nd;
705 
706 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
707 	if (error = namei(&nd))
708 		return (error);
709 	mp = nd.ni_vp->v_mount;
710 	sp = &mp->mnt_stat;
711 	vrele(nd.ni_vp);
712 	if (error = VFS_STATFS(mp, sp, p))
713 		return (error);
714 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
715 	return sunstatfs(sp, (caddr_t)SCARG(uap, buf));
716 }
717 
718 int
719 sunos_fstatfs(p, uap, retval)
720 	struct proc *p;
721 	struct sunos_fstatfs_args *uap;
722 	register_t *retval;
723 {
724 	struct file *fp;
725 	struct mount *mp;
726 	register struct statfs *sp;
727 	int error;
728 
729 	if (error = getvnode(p->p_fd, SCARG(uap, fd), &fp))
730 		return (error);
731 	mp = ((struct vnode *)fp->f_data)->v_mount;
732 	sp = &mp->mnt_stat;
733 	if (error = VFS_STATFS(mp, sp, p))
734 		return (error);
735 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
736 	return sunstatfs(sp, (caddr_t)SCARG(uap, buf));
737 }
738 
739 int
740 sunos_exportfs(p, uap, retval)
741 	struct proc *p;
742 	struct sunos_exportfs_args *uap;
743 	register_t *retval;
744 {
745 	/*
746 	 * XXX: should perhaps translate into a mount(2)
747 	 * with MOUNT_EXPORT?
748 	 */
749 	return 0;
750 }
751 
752 int
753 sunos_mknod(p, uap, retval)
754 	struct proc *p;
755 	struct sunos_mknod_args *uap;
756 	register_t *retval;
757 {
758 	if (S_ISFIFO(SCARG(uap, mode)))
759 		return mkfifo(p, uap, retval);
760 
761 	return mknod(p, (struct mknod_args *)uap, retval);
762 }
763 
764 #define SUNOS_SC_ARG_MAX	1
765 #define SUNOS_SC_CHILD_MAX	2
766 #define SUNOS_SC_CLK_TCK	3
767 #define SUNOS_SC_NGROUPS_MAX	4
768 #define SUNOS_SC_OPEN_MAX	5
769 #define SUNOS_SC_JOB_CONTROL	6
770 #define SUNOS_SC_SAVED_IDS	7
771 #define SUNOS_SC_VERSION	8
772 
773 int
774 sunos_sysconf(p, uap, retval)
775 	struct proc *p;
776 	struct sunos_sysconf_args *uap;
777 	register_t *retval;
778 {
779 	extern int maxfiles;
780 
781 	switch(SCARG(uap, name)) {
782 	case SUNOS_SC_ARG_MAX:
783 		*retval = ARG_MAX;
784 		break;
785 	case SUNOS_SC_CHILD_MAX:
786 		*retval = maxproc;
787 		break;
788 	case SUNOS_SC_CLK_TCK:
789 		*retval = 60;		/* should this be `hz', ie. 100? */
790 		break;
791 	case SUNOS_SC_NGROUPS_MAX:
792 		*retval = NGROUPS_MAX;
793 		break;
794 	case SUNOS_SC_OPEN_MAX:
795 		*retval = maxfiles;
796 		break;
797 	case SUNOS_SC_JOB_CONTROL:
798 		*retval = 1;
799 		break;
800 	case SUNOS_SC_SAVED_IDS:
801 #ifdef _POSIX_SAVED_IDS
802 		*retval = 1;
803 #else
804 		*retval = 0;
805 #endif
806 		break;
807 	case SUNOS_SC_VERSION:
808 		*retval = 198808;
809 		break;
810 	default:
811 		return EINVAL;
812 	}
813 	return 0;
814 }
815 
816 #define SUNOS_RLIMIT_NOFILE	6	/* Other RLIMIT_* are the same */
817 #define SUNOS_RLIM_NLIMITS	7
818 
819 int
820 sunos_getrlimit(p, uap, retval)
821 	struct proc *p;
822 	struct sunos_getrlimit_args *uap;
823 	register_t *retval;
824 {
825 	if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
826 		return EINVAL;
827 
828 	if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
829 		SCARG(uap, which) = RLIMIT_NOFILE;
830 
831 	return compat_43_getrlimit(p, uap, retval);
832 }
833 
834 int
835 sunos_setrlimit(p, uap, retval)
836 	struct proc *p;
837 	struct sunos_getrlimit_args *uap;
838 	register_t *retval;
839 {
840 	if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
841 		return EINVAL;
842 
843 	if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
844 		SCARG(uap, which) = RLIMIT_NOFILE;
845 
846 	return compat_43_setrlimit(p, uap, retval);
847 }
848 
849 /* for the m68k machines */
850 #ifndef PT_GETFPREGS
851 #define PT_GETFPREGS -1
852 #endif
853 #ifndef PT_SETFPREGS
854 #define PT_SETFPREGS -1
855 #endif
856 
857 static int sreq2breq[] = {
858 	PT_TRACE_ME,    PT_READ_I,      PT_READ_D,      -1,
859 	PT_WRITE_I,     PT_WRITE_D,     -1,             PT_CONTINUE,
860 	PT_KILL,        -1,             PT_ATTACH,      PT_DETACH,
861 	PT_GETREGS,     PT_SETREGS,     PT_GETFPREGS,   PT_SETFPREGS
862 };
863 static int nreqs = sizeof(sreq2breq) / sizeof(sreq2breq[0]);
864 
865 sunos_ptrace(p, uap, retval)
866 	struct proc *p;
867 	struct sunos_ptrace_args *uap;
868 	int *retval;
869 {
870 	struct ptrace_args pa;
871 	int req;
872 
873 	req = SCARG(uap, req);
874 
875 	if (req < 0 || req >= nreqs)
876 		return (EINVAL);
877 
878 	req = sreq2breq[req];
879 	if (req == -1)
880 		return (EINVAL);
881 
882 	SCARG(&pa, req) = req;
883 	SCARG(&pa, pid) = (pid_t)SCARG(uap, pid);
884 	SCARG(&pa, addr) = (caddr_t)SCARG(uap, addr);
885 	SCARG(&pa, data) = SCARG(uap, data);
886 
887 	return ptrace(p, &pa, retval);
888 }
889 
890 static void
891 sunos_pollscan(p, pl, nfd, retval)
892 	struct proc *p;
893 	struct sunos_pollfd *pl;
894 	int nfd;
895 	register_t *retval;
896 {
897 	register struct filedesc *fdp = p->p_fd;
898 	register int msk, i;
899 	struct file *fp;
900 	int n = 0;
901 	static int flag[3] = { FREAD, FWRITE, 0 };
902 	static int pflag[3] = { SUNOS_POLLIN|SUNOS_POLLRDNORM,
903 				SUNOS_POLLOUT, SUNOS_POLLERR };
904 
905 	/*
906 	 * XXX: We need to implement the rest of the flags.
907 	 */
908 	for (i = 0; i < nfd; i++) {
909 		fp = fdp->fd_ofiles[pl[i].fd];
910 		if (fp == NULL) {
911 			if (pl[i].events & SUNOS_POLLNVAL) {
912 				pl[i].revents |= SUNOS_POLLNVAL;
913 				n++;
914 			}
915 			continue;
916 		}
917 		for (msk = 0; msk < 3; msk++) {
918 			if (pl[i].events & pflag[msk]) {
919 				if ((*fp->f_ops->fo_select)(fp, flag[msk], p)) {
920 					pl[i].revents |=
921 					    pflag[msk] & pl[i].events;
922 					n++;
923 				}
924 			}
925 		}
926 	}
927 	*retval = n;
928 }
929 
930 
931 /*
932  * We are using the same mechanism as select only we encode/decode args
933  * differently.
934  */
935 int
936 sunos_poll(p, uap, retval)
937 	struct proc *p;
938 	struct sunos_poll_args	*uap;
939 	register_t *retval;
940 {
941 	int i, s;
942 	int error, error2;
943 	size_t sz = sizeof(struct sunos_pollfd) * SCARG(uap, nfds);
944 	struct sunos_pollfd *pl;
945 	int msec = SCARG(uap, timeout);
946 	struct timeval atv;
947 	int timo;
948 	u_int ni;
949 	int ncoll;
950 	extern int nselcoll, selwait;
951 
952 	pl = (struct sunos_pollfd *) malloc(sz, M_TEMP, M_WAITOK);
953 
954 	if (error = copyin(SCARG(uap, fds), pl, sz))
955 		goto bad;
956 
957 	for (i = 0; i < SCARG(uap, nfds); i++)
958 		pl[i].revents = 0;
959 
960 	if (msec != -1) {
961 		atv.tv_sec = msec / 1000;
962 		atv.tv_usec = (msec - (atv.tv_sec * 1000)) * 1000;
963 
964 		if (itimerfix(&atv)) {
965 			error = EINVAL;
966 			goto done;
967 		}
968 		s = splclock();
969 		timevaladd(&atv, (struct timeval *)&time);
970 		timo = hzto(&atv);
971 		/*
972 		 * Avoid inadvertently sleeping forever.
973 		 */
974 		if (timo == 0)
975 			timo = 1;
976 		splx(s);
977 	} else
978 		timo = 0;
979 
980 retry:
981 	ncoll = nselcoll;
982 	p->p_flag |= P_SELECT;
983 	sunos_pollscan(p, pl, SCARG(uap, nfds), retval);
984 	if (*retval)
985 		goto done;
986 	s = splhigh();
987 	if (timo && timercmp(&time, &atv, >=)) {
988 		splx(s);
989 		goto done;
990 	}
991 	if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
992 		splx(s);
993 		goto retry;
994 	}
995 	p->p_flag &= ~P_SELECT;
996 	error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "sunos_poll", timo);
997 	splx(s);
998 	if (error == 0)
999 		goto retry;
1000 
1001 done:
1002 	p->p_flag &= ~P_SELECT;
1003 	/* poll is not restarted after signals... */
1004 	if (error == ERESTART)
1005 		error = EINTR;
1006 	if (error == EWOULDBLOCK)
1007 		error = 0;
1008 
1009 	if (error2 = copyout(pl, SCARG(uap, fds), sz))
1010 		error = error2;
1011 
1012 bad:
1013 	free((char *) pl, M_TEMP);
1014 
1015 	return (error);
1016 }
1017