xref: /netbsd-src/sys/compat/sunos/sunos_misc.c (revision ce0bb6e8d2e560ecacbe865a848624f94498063b)
1 /*	$NetBSD: sunos_misc.c,v 1.43 1995/03/21 13:34:15 mycroft 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, (size_t *)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(p, uap, retval)
526 	struct proc *p;
527 	void *uap;
528 	register_t *retval;
529 {
530 	return 0;
531 }
532 
533 int
534 sunos_uname(p, uap, retval)
535 	struct proc *p;
536 	struct sunos_uname_args *uap;
537 	register_t *retval;
538 {
539 	struct sunos_utsname sut;
540 	extern char ostype[], machine[], osrelease[];
541 
542 	bzero(&sut, sizeof(sut));
543 
544 	bcopy(ostype, sut.sysname, sizeof(sut.sysname) - 1);
545 	bcopy(hostname, sut.nodename, sizeof(sut.nodename));
546 	sut.nodename[sizeof(sut.nodename)-1] = '\0';
547 	bcopy(osrelease, sut.release, sizeof(sut.release) - 1);
548 	bcopy("1", sut.version, sizeof(sut.version) - 1);
549 	bcopy(machine, sut.machine, sizeof(sut.machine) - 1);
550 
551 	return copyout((caddr_t)&sut, (caddr_t)SCARG(uap, name),
552 	    sizeof(struct sunos_utsname));
553 }
554 
555 int
556 sunos_setpgid(p, uap, retval)
557 	struct proc *p;
558 	struct sunos_setpgid_args *uap;
559 	register_t *retval;
560 {
561 	/*
562 	 * difference to our setpgid call is to include backwards
563 	 * compatibility to pre-setsid() binaries. Do setsid()
564 	 * instead of setpgid() in those cases where the process
565 	 * tries to create a new session the old way.
566 	 */
567 	if (!SCARG(uap, pgid) && (!SCARG(uap, pid) || SCARG(uap, pid) == p->p_pid))
568 		return setsid(p, uap, retval);
569 	else
570 		return setpgid(p, uap, retval);
571 }
572 
573 int
574 sunos_open(p, uap, retval)
575 	struct proc *p;
576 	struct sunos_open_args *uap;
577 	register_t *retval;
578 {
579 	int l, r;
580 	int noctty;
581 	int ret;
582 
583 	/* convert mode into NetBSD mode */
584 	l = SCARG(uap, flags);
585 	noctty = l & 0x8000;
586 	r =	(l & (0x0001 | 0x0002 | 0x0008 | 0x0040 | 0x0200 | 0x0400 | 0x0800));
587 	r |=	((l & (0x0004 | 0x1000 | 0x4000)) ? O_NONBLOCK : 0);
588 	r |=	((l & 0x0080) ? O_SHLOCK : 0);
589 	r |=	((l & 0x0100) ? O_EXLOCK : 0);
590 	r |=	((l & 0x2000) ? O_FSYNC : 0);
591 
592 	SCARG(uap, flags) = r;
593 	ret = open(p, (struct open_args *)uap, retval);
594 
595 	if (!ret && !noctty && SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
596 		struct filedesc *fdp = p->p_fd;
597 		struct file *fp = fdp->fd_ofiles[*retval];
598 
599 		/* ignore any error, just give it a try */
600 		if (fp->f_type == DTYPE_VNODE)
601 			(fp->f_ops->fo_ioctl)(fp, TIOCSCTTY, (caddr_t) 0, p);
602 	}
603 	return ret;
604 }
605 
606 #if defined (NFSSERVER)
607 int
608 sunos_nfssvc(p, uap, retval)
609 	struct proc *p;
610 	struct sunos_nfssvc_args *uap;
611 	register_t *retval;
612 {
613 	struct nfssvc_args outuap;
614 	struct nfsd_srvargs nfsdarg;
615 	struct sockaddr sa;
616 	int error;
617 	extern char sigcode[], esigcode[];
618 
619 #if 0
620 	bzero(&outuap, sizeof outuap);
621 	SCARG(&outuap, fd) = SCARG(uap, fd);
622 	SCARG(&outuap, mskval) = STACKGAPBASE;
623 	SCARG(&outuap, msklen) = sizeof sa;
624 	SCARG(&outuap, mtchval) = SCARG(&outuap, mskval) + sizeof sa;
625 	SCARG(&outuap, mtchlen) = sizeof sa;
626 
627 	bzero(&sa, sizeof sa);
628 	if (error = copyout(&sa, SCARG(&outuap, mskval), SCARG(&outuap, msklen)))
629 		return (error);
630 	if (error = copyout(&sa, SCARG(&outuap, mtchval), SCARG(&outuap, mtchlen)))
631 		return (error);
632 
633 	return nfssvc(p, &outuap, retval);
634 #else
635 	return (ENOSYS);
636 #endif
637 }
638 #endif /* NFSSERVER */
639 
640 int
641 sunos_ustat(p, uap, retval)
642 	struct proc *p;
643 	struct sunos_ustat_args *uap;
644 	register_t *retval;
645 {
646 	struct sunos_ustat us;
647 	int error;
648 
649 	bzero(&us, sizeof us);
650 
651 	/*
652 	 * XXX: should set f_tfree and f_tinode at least
653 	 * How do we translate dev -> fstat? (and then to sunos_ustat)
654 	 */
655 
656 	if (error = copyout(&us, SCARG(uap, buf), sizeof us))
657 		return (error);
658 	return 0;
659 }
660 
661 int
662 sunos_quotactl(p, uap, retval)
663 	struct proc *p;
664 	struct sunos_quotactl_args *uap;
665 	register_t *retval;
666 {
667 	return EINVAL;
668 }
669 
670 int
671 sunos_vhangup(p, uap, retval)
672 	struct proc *p;
673 	void *uap;
674 	register_t *retval;
675 {
676 	return 0;
677 }
678 
679 static
680 sunstatfs(sp, buf)
681 	struct statfs *sp;
682 	caddr_t buf;
683 {
684 	struct sunos_statfs ssfs;
685 
686 	bzero(&ssfs, sizeof ssfs);
687 	ssfs.f_type = 0;
688 	ssfs.f_bsize = sp->f_bsize;
689 	ssfs.f_blocks = sp->f_blocks;
690 	ssfs.f_bfree = sp->f_bfree;
691 	ssfs.f_bavail = sp->f_bavail;
692 	ssfs.f_files = sp->f_files;
693 	ssfs.f_ffree = sp->f_ffree;
694 	ssfs.f_fsid = sp->f_fsid;
695 	return copyout((caddr_t)&ssfs, buf, sizeof ssfs);
696 }
697 
698 int
699 sunos_statfs(p, uap, retval)
700 	struct proc *p;
701 	struct sunos_statfs_args *uap;
702 	register_t *retval;
703 {
704 	register struct mount *mp;
705 	register struct statfs *sp;
706 	int error;
707 	struct nameidata nd;
708 
709 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
710 	if (error = namei(&nd))
711 		return (error);
712 	mp = nd.ni_vp->v_mount;
713 	sp = &mp->mnt_stat;
714 	vrele(nd.ni_vp);
715 	if (error = VFS_STATFS(mp, sp, p))
716 		return (error);
717 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
718 	return sunstatfs(sp, (caddr_t)SCARG(uap, buf));
719 }
720 
721 int
722 sunos_fstatfs(p, uap, retval)
723 	struct proc *p;
724 	struct sunos_fstatfs_args *uap;
725 	register_t *retval;
726 {
727 	struct file *fp;
728 	struct mount *mp;
729 	register struct statfs *sp;
730 	int error;
731 
732 	if (error = getvnode(p->p_fd, SCARG(uap, fd), &fp))
733 		return (error);
734 	mp = ((struct vnode *)fp->f_data)->v_mount;
735 	sp = &mp->mnt_stat;
736 	if (error = VFS_STATFS(mp, sp, p))
737 		return (error);
738 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
739 	return sunstatfs(sp, (caddr_t)SCARG(uap, buf));
740 }
741 
742 int
743 sunos_exportfs(p, uap, retval)
744 	struct proc *p;
745 	struct sunos_exportfs_args *uap;
746 	register_t *retval;
747 {
748 	/*
749 	 * XXX: should perhaps translate into a mount(2)
750 	 * with MOUNT_EXPORT?
751 	 */
752 	return 0;
753 }
754 
755 int
756 sunos_mknod(p, uap, retval)
757 	struct proc *p;
758 	struct sunos_mknod_args *uap;
759 	register_t *retval;
760 {
761 	if (S_ISFIFO(SCARG(uap, mode)))
762 		return mkfifo(p, uap, retval);
763 
764 	return mknod(p, (struct mknod_args *)uap, retval);
765 }
766 
767 #define SUNOS_SC_ARG_MAX	1
768 #define SUNOS_SC_CHILD_MAX	2
769 #define SUNOS_SC_CLK_TCK	3
770 #define SUNOS_SC_NGROUPS_MAX	4
771 #define SUNOS_SC_OPEN_MAX	5
772 #define SUNOS_SC_JOB_CONTROL	6
773 #define SUNOS_SC_SAVED_IDS	7
774 #define SUNOS_SC_VERSION	8
775 
776 int
777 sunos_sysconf(p, uap, retval)
778 	struct proc *p;
779 	struct sunos_sysconf_args *uap;
780 	register_t *retval;
781 {
782 	extern int maxfiles;
783 
784 	switch(SCARG(uap, name)) {
785 	case SUNOS_SC_ARG_MAX:
786 		*retval = ARG_MAX;
787 		break;
788 	case SUNOS_SC_CHILD_MAX:
789 		*retval = maxproc;
790 		break;
791 	case SUNOS_SC_CLK_TCK:
792 		*retval = 60;		/* should this be `hz', ie. 100? */
793 		break;
794 	case SUNOS_SC_NGROUPS_MAX:
795 		*retval = NGROUPS_MAX;
796 		break;
797 	case SUNOS_SC_OPEN_MAX:
798 		*retval = maxfiles;
799 		break;
800 	case SUNOS_SC_JOB_CONTROL:
801 		*retval = 1;
802 		break;
803 	case SUNOS_SC_SAVED_IDS:
804 #ifdef _POSIX_SAVED_IDS
805 		*retval = 1;
806 #else
807 		*retval = 0;
808 #endif
809 		break;
810 	case SUNOS_SC_VERSION:
811 		*retval = 198808;
812 		break;
813 	default:
814 		return EINVAL;
815 	}
816 	return 0;
817 }
818 
819 #define SUNOS_RLIMIT_NOFILE	6	/* Other RLIMIT_* are the same */
820 #define SUNOS_RLIM_NLIMITS	7
821 
822 int
823 sunos_getrlimit(p, uap, retval)
824 	struct proc *p;
825 	struct sunos_getrlimit_args *uap;
826 	register_t *retval;
827 {
828 	if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
829 		return EINVAL;
830 
831 	if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
832 		SCARG(uap, which) = RLIMIT_NOFILE;
833 
834 	return compat_43_getrlimit(p, uap, retval);
835 }
836 
837 int
838 sunos_setrlimit(p, uap, retval)
839 	struct proc *p;
840 	struct sunos_getrlimit_args *uap;
841 	register_t *retval;
842 {
843 	if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
844 		return EINVAL;
845 
846 	if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
847 		SCARG(uap, which) = RLIMIT_NOFILE;
848 
849 	return compat_43_setrlimit(p, uap, retval);
850 }
851 
852 /* for the m68k machines */
853 #ifndef PT_GETFPREGS
854 #define PT_GETFPREGS -1
855 #endif
856 #ifndef PT_SETFPREGS
857 #define PT_SETFPREGS -1
858 #endif
859 
860 static int sreq2breq[] = {
861 	PT_TRACE_ME,    PT_READ_I,      PT_READ_D,      -1,
862 	PT_WRITE_I,     PT_WRITE_D,     -1,             PT_CONTINUE,
863 	PT_KILL,        -1,             PT_ATTACH,      PT_DETACH,
864 	PT_GETREGS,     PT_SETREGS,     PT_GETFPREGS,   PT_SETFPREGS
865 };
866 static int nreqs = sizeof(sreq2breq) / sizeof(sreq2breq[0]);
867 
868 sunos_ptrace(p, uap, retval)
869 	struct proc *p;
870 	struct sunos_ptrace_args *uap;
871 	int *retval;
872 {
873 	struct ptrace_args pa;
874 	int req;
875 
876 	req = SCARG(uap, req);
877 
878 	if (req < 0 || req >= nreqs)
879 		return (EINVAL);
880 
881 	req = sreq2breq[req];
882 	if (req == -1)
883 		return (EINVAL);
884 
885 	SCARG(&pa, req) = req;
886 	SCARG(&pa, pid) = (pid_t)SCARG(uap, pid);
887 	SCARG(&pa, addr) = (caddr_t)SCARG(uap, addr);
888 	SCARG(&pa, data) = SCARG(uap, data);
889 
890 	return ptrace(p, &pa, retval);
891 }
892 
893 static void
894 sunos_pollscan(p, pl, nfd, retval)
895 	struct proc *p;
896 	struct sunos_pollfd *pl;
897 	int nfd;
898 	register_t *retval;
899 {
900 	register struct filedesc *fdp = p->p_fd;
901 	register int msk, i;
902 	struct file *fp;
903 	int n = 0;
904 	static int flag[3] = { FREAD, FWRITE, 0 };
905 	static int pflag[3] = { SUNOS_POLLIN|SUNOS_POLLRDNORM,
906 				SUNOS_POLLOUT, SUNOS_POLLERR };
907 
908 	/*
909 	 * XXX: We need to implement the rest of the flags.
910 	 */
911 	for (i = 0; i < nfd; i++) {
912 		fp = fdp->fd_ofiles[pl[i].fd];
913 		if (fp == NULL) {
914 			if (pl[i].events & SUNOS_POLLNVAL) {
915 				pl[i].revents |= SUNOS_POLLNVAL;
916 				n++;
917 			}
918 			continue;
919 		}
920 		for (msk = 0; msk < 3; msk++) {
921 			if (pl[i].events & pflag[msk]) {
922 				if ((*fp->f_ops->fo_select)(fp, flag[msk], p)) {
923 					pl[i].revents |=
924 					    pflag[msk] & pl[i].events;
925 					n++;
926 				}
927 			}
928 		}
929 	}
930 	*retval = n;
931 }
932 
933 
934 /*
935  * We are using the same mechanism as select only we encode/decode args
936  * differently.
937  */
938 int
939 sunos_poll(p, uap, retval)
940 	struct proc *p;
941 	struct sunos_poll_args	*uap;
942 	register_t *retval;
943 {
944 	int i, s;
945 	int error, error2;
946 	size_t sz = sizeof(struct sunos_pollfd) * SCARG(uap, nfds);
947 	struct sunos_pollfd *pl;
948 	int msec = SCARG(uap, timeout);
949 	struct timeval atv;
950 	int timo;
951 	u_int ni;
952 	int ncoll;
953 	extern int nselcoll, selwait;
954 
955 	pl = (struct sunos_pollfd *) malloc(sz, M_TEMP, M_WAITOK);
956 
957 	if (error = copyin(SCARG(uap, fds), pl, sz))
958 		goto bad;
959 
960 	for (i = 0; i < SCARG(uap, nfds); i++)
961 		pl[i].revents = 0;
962 
963 	if (msec != -1) {
964 		atv.tv_sec = msec / 1000;
965 		atv.tv_usec = (msec - (atv.tv_sec * 1000)) * 1000;
966 
967 		if (itimerfix(&atv)) {
968 			error = EINVAL;
969 			goto done;
970 		}
971 		s = splclock();
972 		timeradd(&atv, &time, &atv);
973 		timo = hzto(&atv);
974 		/*
975 		 * Avoid inadvertently sleeping forever.
976 		 */
977 		if (timo == 0)
978 			timo = 1;
979 		splx(s);
980 	} else
981 		timo = 0;
982 
983 retry:
984 	ncoll = nselcoll;
985 	p->p_flag |= P_SELECT;
986 	sunos_pollscan(p, pl, SCARG(uap, nfds), retval);
987 	if (*retval)
988 		goto done;
989 	s = splhigh();
990 	if (timo && timercmp(&time, &atv, >=)) {
991 		splx(s);
992 		goto done;
993 	}
994 	if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
995 		splx(s);
996 		goto retry;
997 	}
998 	p->p_flag &= ~P_SELECT;
999 	error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "sunos_poll", timo);
1000 	splx(s);
1001 	if (error == 0)
1002 		goto retry;
1003 
1004 done:
1005 	p->p_flag &= ~P_SELECT;
1006 	/* poll is not restarted after signals... */
1007 	if (error == ERESTART)
1008 		error = EINTR;
1009 	if (error == EWOULDBLOCK)
1010 		error = 0;
1011 
1012 	if (error2 = copyout(pl, SCARG(uap, fds), sz))
1013 		error = error2;
1014 
1015 bad:
1016 	free((char *) pl, M_TEMP);
1017 
1018 	return (error);
1019 }
1020