xref: /netbsd-src/sys/compat/sunos/sunos_misc.c (revision bada23909e740596d0a3785a73bd3583a9807fb8)
1 /*	$NetBSD: sunos_misc.c,v 1.100 1999/03/22 17:30:37 sommerfe 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 #define COMPAT_SUNOS 1
57 
58 #include "opt_nfsserver.h"
59 #include "fs_nfs.h"
60 
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/namei.h>
64 #include <sys/proc.h>
65 #include <sys/dirent.h>
66 #include <sys/file.h>
67 #include <sys/stat.h>
68 #include <sys/filedesc.h>
69 #include <sys/ioctl.h>
70 #include <sys/kernel.h>
71 #include <sys/reboot.h>
72 #include <sys/malloc.h>
73 #include <sys/mbuf.h>
74 #include <sys/mman.h>
75 #include <sys/mount.h>
76 #include <sys/ptrace.h>
77 #include <sys/resource.h>
78 #include <sys/resourcevar.h>
79 #include <sys/signal.h>
80 #include <sys/signalvar.h>
81 #include <sys/socket.h>
82 #include <sys/tty.h>
83 #include <sys/vnode.h>
84 #include <sys/uio.h>
85 #include <sys/wait.h>
86 #include <sys/utsname.h>
87 #include <sys/unistd.h>
88 #include <sys/syscallargs.h>
89 #include <sys/conf.h>
90 #include <sys/socketvar.h>
91 #include <sys/exec.h>
92 #include <sys/swap.h>
93 
94 #include <compat/sunos/sunos.h>
95 #include <compat/sunos/sunos_syscallargs.h>
96 #include <compat/sunos/sunos_util.h>
97 #include <compat/sunos/sunos_dirent.h>
98 
99 #include <netinet/in.h>
100 
101 #include <miscfs/specfs/specdev.h>
102 
103 #include <nfs/rpcv2.h>
104 #include <nfs/nfsproto.h>
105 #include <nfs/nfs.h>
106 #include <nfs/nfsmount.h>
107 
108 #include <vm/vm.h>
109 
110 static int sunstatfs __P((struct statfs *, caddr_t));
111 
112 int
113 sunos_sys_stime(p, v, retval)
114 	struct proc *p;
115 	void *v;
116 	register_t *retval;
117 {
118 	struct sunos_sys_stime_args *uap = v;
119 	struct sys_settimeofday_args ap;
120 	caddr_t sg = stackgap_init(p->p_emul);
121 	struct timeval tv, *sgtvp;
122 	int error;
123 
124 	error = copyin(SCARG(uap, tp), &tv.tv_sec, sizeof(tv.tv_sec));
125 	if (error)
126 		return error;
127 	tv.tv_usec = 0;
128 
129 	SCARG(&ap, tv) = sgtvp = stackgap_alloc(&sg, sizeof(struct timeval));
130 	SCARG(&ap, tzp) = NULL;
131 
132 	error = copyout(&tv, sgtvp, sizeof(struct timeval));
133 	if (error)
134 		return error;
135 
136 	return sys_settimeofday(p, &ap, retval);
137 }
138 
139 int
140 sunos_sys_wait4(p, v, retval)
141 	struct proc *p;
142 	void *v;
143 	register_t *retval;
144 {
145 	struct sunos_sys_wait4_args *uap = v;
146 	if (SCARG(uap, pid) == 0)
147 		SCARG(uap, pid) = WAIT_ANY;
148 	return (sys_wait4(p, uap, retval));
149 }
150 
151 int
152 sunos_sys_creat(p, v, retval)
153 	struct proc *p;
154 	void *v;
155 	register_t *retval;
156 {
157 	struct sunos_sys_creat_args *uap = v;
158 	struct sys_open_args ouap;
159 
160 	caddr_t sg = stackgap_init(p->p_emul);
161 	SUNOS_CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
162 
163 	SCARG(&ouap, path) = SCARG(uap, path);
164 	SCARG(&ouap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
165 	SCARG(&ouap, mode) = SCARG(uap, mode);
166 
167 	return (sys_open(p, &ouap, retval));
168 }
169 
170 int
171 sunos_sys_access(p, v, retval)
172 	struct proc *p;
173 	void *v;
174 	register_t *retval;
175 {
176 	struct sunos_sys_access_args *uap = v;
177 	caddr_t sg = stackgap_init(p->p_emul);
178 	SUNOS_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
179 
180 	return (sys_access(p, uap, retval));
181 }
182 
183 int
184 sunos_sys_stat(p, v, retval)
185 	struct proc *p;
186 	void *v;
187 	register_t *retval;
188 {
189 	struct sunos_sys_stat_args *uap = v;
190 	caddr_t sg = stackgap_init(p->p_emul);
191 	SUNOS_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
192 
193 	return (compat_43_sys_stat(p, uap, retval));
194 }
195 
196 int
197 sunos_sys_lstat(p, v, retval)
198 	struct proc *p;
199 	void *v;
200 	register_t *retval;
201 {
202 	struct sunos_sys_lstat_args *uap = v;
203 	caddr_t sg = stackgap_init(p->p_emul);
204 	SUNOS_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
205 
206 	return (compat_43_sys_lstat(p, uap, retval));
207 }
208 
209 int
210 sunos_sys_execv(p, v, retval)
211 	struct proc *p;
212 	void *v;
213 	register_t *retval;
214 {
215 	struct sunos_sys_execv_args /* {
216 		syscallarg(const char *) path;
217 		syscallarg(char **) argv;
218 	} */ *uap = v;
219 	struct sys_execve_args ap;
220 	caddr_t sg;
221 
222 	sg = stackgap_init(p->p_emul);
223 	SUNOS_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
224 
225 	SCARG(&ap, path) = SCARG(uap, path);
226 	SCARG(&ap, argp) = SCARG(uap, argp);
227 	SCARG(&ap, envp) = NULL;
228 
229 	return (sys_execve(p, &ap, retval));
230 }
231 
232 int
233 sunos_sys_execve(p, v, retval)
234 	struct proc *p;
235 	void *v;
236 	register_t *retval;
237 {
238 	struct sunos_sys_execve_args /* {
239 		syscallarg(const char *) path;
240 		syscallarg(char **) argv;
241 		syscallarg(char **) envp;
242 	} */ *uap = v;
243 	struct sys_execve_args ap;
244 	caddr_t sg;
245 
246 	sg = stackgap_init(p->p_emul);
247 	SUNOS_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
248 
249 	SCARG(&ap, path) = SCARG(uap, path);
250 	SCARG(&ap, argp) = SCARG(uap, argp);
251 	SCARG(&ap, envp) = SCARG(uap, envp);
252 
253 	return (sys_execve(p, &ap, retval));
254 }
255 
256 int
257 sunos_sys_omsync(p, v, retval)
258 	struct proc *p;
259 	void *v;
260 	register_t *retval;
261 {
262 	struct sunos_sys_omsync_args *uap = v;
263 	struct sys___msync13_args ouap;
264 
265 	SCARG(&ouap, addr) = SCARG(uap, addr);
266 	SCARG(&ouap, len) = SCARG(uap, len);
267 	SCARG(&ouap, flags) = SCARG(uap, flags);
268 
269 	return (sys___msync13(p, &ouap, retval));
270 }
271 
272 int
273 sunos_sys_unmount(p, v, retval)
274 	struct proc *p;
275 	void *v;
276 	register_t *retval;
277 {
278 	struct sunos_sys_unmount_args *uap = v;
279 	struct sys_unmount_args ouap;
280 
281 	SCARG(&ouap, path) = SCARG(uap, path);
282 	SCARG(&ouap, flags) = 0;
283 
284 	return (sys_unmount(p, &ouap, retval));
285 }
286 
287 /*
288  * Conversion table for SunOS NFS mount flags.
289  */
290 static struct {
291 	int	sun_flg;
292 	int	bsd_flg;
293 } sunnfs_flgtab[] = {
294 	{ SUNNFS_SOFT,		NFSMNT_SOFT },
295 	{ SUNNFS_WSIZE,		NFSMNT_WSIZE },
296 	{ SUNNFS_RSIZE,		NFSMNT_RSIZE },
297 	{ SUNNFS_TIMEO,		NFSMNT_TIMEO },
298 	{ SUNNFS_RETRANS,	NFSMNT_RETRANS },
299 	{ SUNNFS_HOSTNAME,	0 },			/* Ignored */
300 	{ SUNNFS_INT,		NFSMNT_INT },
301 	{ SUNNFS_NOAC,		0 },			/* Ignored */
302 	{ SUNNFS_ACREGMIN,	0 },			/* Ignored */
303 	{ SUNNFS_ACREGMAX,	0 },			/* Ignored */
304 	{ SUNNFS_ACDIRMIN,	0 },			/* Ignored */
305 	{ SUNNFS_ACDIRMAX,	0 },			/* Ignored */
306 	{ SUNNFS_SECURE,	0 },			/* Ignored */
307 	{ SUNNFS_NOCTO,		0 },			/* Ignored */
308 	{ SUNNFS_POSIX,		0 }			/* Ignored */
309 };
310 
311 int
312 sunos_sys_mount(p, v, retval)
313 	struct proc *p;
314 	void *v;
315 	register_t *retval;
316 {
317 	struct sunos_sys_mount_args *uap = v;
318 	int oflags = SCARG(uap, flags), nflags, error;
319 	char fsname[MFSNAMELEN];
320 	caddr_t sg = stackgap_init(p->p_emul);
321 
322 	if (oflags & (SUNM_NOSUB | SUNM_SYS5))
323 		return (EINVAL);
324 	if ((oflags & SUNM_NEWTYPE) == 0)
325 		return (EINVAL);
326 	nflags = 0;
327 	if (oflags & SUNM_RDONLY)
328 		nflags |= MNT_RDONLY;
329 	if (oflags & SUNM_NOSUID)
330 		nflags |= MNT_NOSUID;
331 	if (oflags & SUNM_REMOUNT)
332 		nflags |= MNT_UPDATE;
333 	SCARG(uap, flags) = nflags;
334 
335 	error = copyinstr((caddr_t)SCARG(uap, type), fsname,
336 	    sizeof fsname, (size_t *)0);
337 	if (error)
338 		return (error);
339 
340 	if (strncmp(fsname, "4.2", sizeof fsname) == 0) {
341 		SCARG(uap, type) = stackgap_alloc(&sg, sizeof("ffs"));
342 		error = copyout("ffs", SCARG(uap, type), sizeof("ffs"));
343 		if (error)
344 			return (error);
345 	} else if (strncmp(fsname, "nfs", sizeof fsname) == 0) {
346 		struct sunos_nfs_args sna;
347 		struct sockaddr_in sain;
348 		struct nfs_args na;
349 		struct sockaddr sa;
350 		int n;
351 
352 		error = copyin(SCARG(uap, data), &sna, sizeof sna);
353 		if (error)
354 			return (error);
355 		error = copyin(sna.addr, &sain, sizeof sain);
356 		if (error)
357 			return (error);
358 		memcpy(&sa, &sain, sizeof sa);
359 		sa.sa_len = sizeof(sain);
360 		SCARG(uap, data) = stackgap_alloc(&sg, sizeof(na));
361 		na.version = NFS_ARGSVERSION;
362 		na.addr = stackgap_alloc(&sg, sizeof(struct sockaddr));
363 		na.addrlen = sizeof(struct sockaddr);
364 		na.sotype = SOCK_DGRAM;
365 		na.proto = IPPROTO_UDP;
366 		na.fh = (void *)sna.fh;
367 		na.fhsize = NFSX_V2FH;
368 		na.flags = 0;
369 		n = sizeof(sunnfs_flgtab) / sizeof(sunnfs_flgtab[0]);
370 		while (--n >= 0)
371 			if (sna.flags & sunnfs_flgtab[n].sun_flg)
372 				na.flags |= sunnfs_flgtab[n].bsd_flg;
373 		na.wsize = sna.wsize;
374 		na.rsize = sna.rsize;
375 		if (na.flags & NFSMNT_RSIZE) {
376 			na.flags |= NFSMNT_READDIRSIZE;
377 			na.readdirsize = na.rsize;
378 		}
379 		na.timeo = sna.timeo;
380 		na.retrans = sna.retrans;
381 		na.hostname = sna.hostname;
382 
383 		error = copyout(&sa, na.addr, sizeof sa);
384 		if (error)
385 			return (error);
386 		error = copyout(&na, SCARG(uap, data), sizeof na);
387 		if (error)
388 			return (error);
389 	}
390 	return (sys_mount(p, (struct sys_mount_args *)uap, retval));
391 }
392 
393 #if defined(NFS)
394 int
395 async_daemon(p, v, retval)
396 	struct proc *p;
397 	void *v;
398 	register_t *retval;
399 {
400 	struct sys_nfssvc_args ouap;
401 
402 	SCARG(&ouap, flag) = NFSSVC_BIOD;
403 	SCARG(&ouap, argp) = NULL;
404 
405 	return (sys_nfssvc(p, &ouap, retval));
406 }
407 #endif /* NFS */
408 
409 void	native_to_sunos_sigset __P((const sigset_t *, int *));
410 void	sunos_to_native_sigset __P((const int, sigset_t *));
411 
412 __inline__ void
413 native_to_sunos_sigset(ss, mask)
414 	const sigset_t *ss;
415 	int *mask;
416 {
417 	*mask = ss->__bits[0];
418 }
419 
420 __inline__ void
421 sunos_to_native_sigset(mask, ss)
422 	const int mask;
423 	sigset_t *ss;
424 {
425 
426 	ss->__bits[0] = mask;
427 	ss->__bits[1] = 0;
428 	ss->__bits[2] = 0;
429 	ss->__bits[3] = 0;
430 }
431 
432 int
433 sunos_sys_sigpending(p, v, retval)
434 	struct proc *p;
435 	void *v;
436 	register_t *retval;
437 {
438 	struct sunos_sys_sigpending_args *uap = v;
439 	sigset_t ss;
440 	int mask;
441 
442 	sigpending1(p, &ss);
443 	native_to_sunos_sigset(&ss, &mask);
444 
445 	return (copyout((caddr_t)&mask, (caddr_t)SCARG(uap, mask), sizeof(int)));
446 }
447 
448 int
449 sunos_sys_sigsuspend(p, v, retval)
450 	register struct proc *p;
451 	void *v;
452 	register_t *retval;
453 {
454 	struct sunos_sys_sigsuspend_args /* {
455 		syscallarg(int) mask;
456 	} */ *uap = v;
457 	int mask;
458 	sigset_t ss;
459 
460 	mask = SCARG(uap, mask);
461 	sunos_to_native_sigset(mask, &ss);
462 	return (sigsuspend1(p, &ss));
463 }
464 
465 /*
466  * Read Sun-style directory entries.  We suck them into kernel space so
467  * that they can be massaged before being copied out to user code.  Like
468  * SunOS, we squish out `empty' entries.
469  *
470  * This is quite ugly, but what do you expect from compatibility code?
471  */
472 int
473 sunos_sys_getdents(p, v, retval)
474 	struct proc *p;
475 	void *v;
476 	register_t *retval;
477 {
478 	struct sunos_sys_getdents_args *uap = v;
479 	struct dirent *bdp;
480 	struct vnode *vp;
481 	caddr_t inp, buf;	/* BSD-format */
482 	int len, reclen;	/* BSD-format */
483 	caddr_t outp;		/* Sun-format */
484 	int resid, sunos_reclen;/* Sun-format */
485 	struct file *fp;
486 	struct uio auio;
487 	struct iovec aiov;
488 	struct sunos_dirent idb;
489 	off_t off;			/* true file offset */
490 	int buflen, error, eofflag;
491 	off_t *cookiebuf, *cookie;
492 	int ncookies;
493 
494 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
495 		return (error);
496 
497 	if ((fp->f_flag & FREAD) == 0)
498 		return (EBADF);
499 
500 	vp = (struct vnode *)fp->f_data;
501 	if (vp->v_type != VDIR)
502 		return (EINVAL);
503 
504 	buflen = min(MAXBSIZE, SCARG(uap, nbytes));
505 	buf = malloc(buflen, M_TEMP, M_WAITOK);
506 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
507 	off = fp->f_offset;
508 again:
509 	aiov.iov_base = buf;
510 	aiov.iov_len = buflen;
511 	auio.uio_iov = &aiov;
512 	auio.uio_iovcnt = 1;
513 	auio.uio_rw = UIO_READ;
514 	auio.uio_segflg = UIO_SYSSPACE;
515 	auio.uio_procp = p;
516 	auio.uio_resid = buflen;
517 	auio.uio_offset = off;
518 	/*
519 	 * First we read into the malloc'ed buffer, then
520 	 * we massage it into user space, one record at a time.
521 	 */
522 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
523 	    &ncookies);
524 	if (error)
525 		goto out;
526 
527 	inp = buf;
528 	outp = SCARG(uap, buf);
529 	resid = SCARG(uap, nbytes);
530 	if ((len = buflen - auio.uio_resid) == 0)
531 		goto eof;
532 
533 	for (cookie = cookiebuf; len > 0; len -= reclen) {
534 		bdp = (struct dirent *)inp;
535 		reclen = bdp->d_reclen;
536 		if (reclen & 3)
537 			panic("sunos_getdents");
538 		if ((*cookie >> 32) != 0) {
539 			compat_offseterr(vp, "sunos_getdents");
540 			error = EINVAL;
541 			goto out;
542 		}
543 		if (bdp->d_fileno == 0) {
544 			inp += reclen;	/* it is a hole; squish it out */
545 			off = *cookie++;
546 			continue;
547 		}
548 		sunos_reclen = SUNOS_RECLEN(&idb, bdp->d_namlen);
549 		if (reclen > len || resid < sunos_reclen) {
550 			/* entry too big for buffer, so just stop */
551 			outp++;
552 			break;
553 		}
554 		off = *cookie++;	/* each entry points to next */
555 		/*
556 		 * Massage in place to make a Sun-shaped dirent (otherwise
557 		 * we have to worry about touching user memory outside of
558 		 * the copyout() call).
559 		 */
560 		idb.d_fileno = bdp->d_fileno;
561 		idb.d_off = off;
562 		idb.d_reclen = sunos_reclen;
563 		idb.d_namlen = bdp->d_namlen;
564 		strcpy(idb.d_name, bdp->d_name);
565 		if ((error = copyout((caddr_t)&idb, outp, sunos_reclen)) != 0)
566 			goto out;
567 		/* advance past this real entry */
568 		inp += reclen;
569 		/* advance output past Sun-shaped entry */
570 		outp += sunos_reclen;
571 		resid -= sunos_reclen;
572 	}
573 
574 	/* if we squished out the whole block, try again */
575 	if (outp == SCARG(uap, buf))
576 		goto again;
577 	fp->f_offset = off;		/* update the vnode offset */
578 
579 eof:
580 	*retval = SCARG(uap, nbytes) - resid;
581 out:
582 	VOP_UNLOCK(vp, 0);
583 	free(cookiebuf, M_TEMP);
584 	free(buf, M_TEMP);
585 	return (error);
586 }
587 
588 #define	SUNOS__MAP_NEW	0x80000000	/* if not, old mmap & cannot handle */
589 
590 int
591 sunos_sys_mmap(p, v, retval)
592 	register struct proc *p;
593 	void *v;
594 	register_t *retval;
595 {
596 	register struct sunos_sys_mmap_args *uap = v;
597 	struct sys_mmap_args ouap;
598 	register struct filedesc *fdp;
599 	register struct file *fp;
600 	register struct vnode *vp;
601 
602 	/*
603 	 * Verify the arguments.
604 	 */
605 	if (SCARG(uap, prot) & ~(PROT_READ|PROT_WRITE|PROT_EXEC))
606 		return (EINVAL);			/* XXX still needed? */
607 
608 	if ((SCARG(uap, flags) & SUNOS__MAP_NEW) == 0)
609 		return (EINVAL);
610 
611 	SCARG(&ouap, flags) = SCARG(uap, flags) & ~SUNOS__MAP_NEW;
612 	SCARG(&ouap, addr) = SCARG(uap, addr);
613 
614 	if ((SCARG(&ouap, flags) & MAP_FIXED) == 0 &&
615 	    SCARG(&ouap, addr) != 0 &&
616 	    SCARG(&ouap, addr) < (void *)round_page(p->p_vmspace->vm_daddr+MAXDSIZ))
617 		SCARG(&ouap, addr) = (caddr_t)round_page(p->p_vmspace->vm_daddr+MAXDSIZ);
618 
619 	SCARG(&ouap, len) = SCARG(uap, len);
620 	SCARG(&ouap, prot) = SCARG(uap, prot);
621 	SCARG(&ouap, fd) = SCARG(uap, fd);
622 	SCARG(&ouap, pos) = SCARG(uap, pos);
623 
624 	/*
625 	 * Special case: if fd refers to /dev/zero, map as MAP_ANON.  (XXX)
626 	 */
627 	fdp = p->p_fd;
628 	if ((unsigned)SCARG(&ouap, fd) < fdp->fd_nfiles &&		/*XXX*/
629 	    (fp = fdp->fd_ofiles[SCARG(&ouap, fd)]) != NULL &&		/*XXX*/
630 	    fp->f_type == DTYPE_VNODE &&				/*XXX*/
631 	    (vp = (struct vnode *)fp->f_data)->v_type == VCHR &&	/*XXX*/
632 	    iszerodev(vp->v_rdev)) {					/*XXX*/
633 		SCARG(&ouap, flags) |= MAP_ANON;
634 		SCARG(&ouap, fd) = -1;
635 	}
636 
637 	return (sys_mmap(p, &ouap, retval));
638 }
639 
640 #define	MC_SYNC		1
641 #define	MC_LOCK		2
642 #define	MC_UNLOCK	3
643 #define	MC_ADVISE	4
644 #define	MC_LOCKAS	5
645 #define	MC_UNLOCKAS	6
646 
647 int
648 sunos_sys_mctl(p, v, retval)
649 	register struct proc *p;
650 	void *v;
651 	register_t *retval;
652 {
653 	register struct sunos_sys_mctl_args *uap = v;
654 
655 	switch (SCARG(uap, func)) {
656 	case MC_ADVISE:		/* ignore for now */
657 		return (0);
658 	case MC_SYNC:		/* translate to msync */
659 		return (sys___msync13(p, uap, retval));
660 	default:
661 		return (EINVAL);
662 	}
663 }
664 
665 int
666 sunos_sys_setsockopt(p, v, retval)
667 	struct proc *p;
668 	void *v;
669 	register_t *retval;
670 {
671 	register struct sunos_sys_setsockopt_args *uap = v;
672 	struct file *fp;
673 	struct mbuf *m = NULL;
674 	int error;
675 
676 	if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
677 		return (error);
678 #define	SO_DONTLINGER (~SO_LINGER)
679 	if (SCARG(uap, name) == SO_DONTLINGER) {
680 		m = m_get(M_WAIT, MT_SOOPTS);
681 		mtod(m, struct linger *)->l_onoff = 0;
682 		m->m_len = sizeof(struct linger);
683 		return (sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
684 		    SO_LINGER, m));
685 	}
686 	if (SCARG(uap, level) == IPPROTO_IP) {
687 #define		SUNOS_IP_MULTICAST_IF		2
688 #define		SUNOS_IP_MULTICAST_TTL		3
689 #define		SUNOS_IP_MULTICAST_LOOP		4
690 #define		SUNOS_IP_ADD_MEMBERSHIP		5
691 #define		SUNOS_IP_DROP_MEMBERSHIP	6
692 		static int ipoptxlat[] = {
693 			IP_MULTICAST_IF,
694 			IP_MULTICAST_TTL,
695 			IP_MULTICAST_LOOP,
696 			IP_ADD_MEMBERSHIP,
697 			IP_DROP_MEMBERSHIP
698 		};
699 		if (SCARG(uap, name) >= SUNOS_IP_MULTICAST_IF &&
700 		    SCARG(uap, name) <= SUNOS_IP_DROP_MEMBERSHIP) {
701 			SCARG(uap, name) =
702 			    ipoptxlat[SCARG(uap, name) - SUNOS_IP_MULTICAST_IF];
703 		}
704 	}
705 	if (SCARG(uap, valsize) > MLEN)
706 		return (EINVAL);
707 	if (SCARG(uap, val)) {
708 		m = m_get(M_WAIT, MT_SOOPTS);
709 		error = copyin(SCARG(uap, val), mtod(m, caddr_t),
710 		    (u_int)SCARG(uap, valsize));
711 		if (error) {
712 			(void) m_free(m);
713 			return (error);
714 		}
715 		m->m_len = SCARG(uap, valsize);
716 	}
717 	return (sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
718 	    SCARG(uap, name), m));
719 }
720 
721 /*
722  * XXX: This needs cleaning up.
723  */
724 int
725 sunos_sys_auditsys(p, v, retval)
726 	struct proc *p;
727 	void *v;
728 	register_t *retval;
729 {
730 	return 0;
731 }
732 
733 int
734 sunos_sys_uname(p, v, retval)
735 	struct proc *p;
736 	void *v;
737 	register_t *retval;
738 {
739 	struct sunos_sys_uname_args *uap = v;
740 	struct sunos_utsname sut;
741 	extern char ostype[], machine[], osrelease[];
742 
743 	memset(&sut, 0, sizeof(sut));
744 
745 	memcpy(sut.sysname, ostype, sizeof(sut.sysname) - 1);
746 	memcpy(sut.nodename, hostname, sizeof(sut.nodename));
747 	sut.nodename[sizeof(sut.nodename)-1] = '\0';
748 	memcpy(sut.release, osrelease, sizeof(sut.release) - 1);
749 	memcpy(sut.version, "1", sizeof(sut.version) - 1);
750 	memcpy(sut.machine, machine, sizeof(sut.machine) - 1);
751 
752 	return copyout((caddr_t)&sut, (caddr_t)SCARG(uap, name),
753 	    sizeof(struct sunos_utsname));
754 }
755 
756 int
757 sunos_sys_setpgrp(p, v, retval)
758 	struct proc *p;
759 	void *v;
760 	register_t *retval;
761 {
762 	struct sunos_sys_setpgrp_args *uap = v;
763 
764 	/*
765 	 * difference to our setpgid call is to include backwards
766 	 * compatibility to pre-setsid() binaries. Do setsid()
767 	 * instead of setpgid() in those cases where the process
768 	 * tries to create a new session the old way.
769 	 */
770 	if (!SCARG(uap, pgid) &&
771 	    (!SCARG(uap, pid) || SCARG(uap, pid) == p->p_pid))
772 		return sys_setsid(p, uap, retval);
773 	else
774 		return sys_setpgid(p, uap, retval);
775 }
776 
777 int
778 sunos_sys_open(p, v, retval)
779 	struct proc *p;
780 	void *v;
781 	register_t *retval;
782 {
783 	struct sunos_sys_open_args *uap = v;
784 	int l, r;
785 	int noctty;
786 	int ret;
787 
788 	caddr_t sg = stackgap_init(p->p_emul);
789 
790 	/* convert mode into NetBSD mode */
791 	l = SCARG(uap, flags);
792 	noctty = l & 0x8000;
793 	r =	(l & (0x0001 | 0x0002 | 0x0008 | 0x0040 | 0x0200 | 0x0400 | 0x0800));
794 	r |=	((l & (0x0004 | 0x1000 | 0x4000)) ? O_NONBLOCK : 0);
795 	r |=	((l & 0x0080) ? O_SHLOCK : 0);
796 	r |=	((l & 0x0100) ? O_EXLOCK : 0);
797 	r |=	((l & 0x2000) ? O_FSYNC : 0);
798 
799 	if (r & O_CREAT)
800 		SUNOS_CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
801 	else
802 		SUNOS_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
803 
804 	SCARG(uap, flags) = r;
805 	ret = sys_open(p, (struct sys_open_args *)uap, retval);
806 
807 	if (!ret && !noctty && SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
808 		struct filedesc *fdp = p->p_fd;
809 		struct file *fp = fdp->fd_ofiles[*retval];
810 
811 		/* ignore any error, just give it a try */
812 		if (fp->f_type == DTYPE_VNODE)
813 			(fp->f_ops->fo_ioctl)(fp, TIOCSCTTY, (caddr_t)0, p);
814 	}
815 	return ret;
816 }
817 
818 #if defined (NFSSERVER)
819 int
820 sunos_sys_nfssvc(p, v, retval)
821 	struct proc *p;
822 	void *v;
823 	register_t *retval;
824 {
825 #if 0
826 	struct sunos_sys_nfssvc_args *uap = v;
827 	struct emul *e = p->p_emul;
828 	struct sys_nfssvc_args outuap;
829 	struct sockaddr sa;
830 	int error;
831 	caddr_t sg = stackgap_init(p->p_emul);
832 
833 	memset(&outuap, 0, sizeof outuap);
834 	SCARG(&outuap, fd) = SCARG(uap, fd);
835 	SCARG(&outuap, mskval) = stackgap_alloc(&sg, sizeof(sa));
836 	SCARG(&outuap, msklen) = sizeof(sa);
837 	SCARG(&outuap, mtchval) = stackgap_alloc(&sg, sizeof(sa));
838 	SCARG(&outuap, mtchlen) = sizeof(sa);
839 
840 	memset(&sa, 0, sizeof sa);
841 	if (error = copyout(&sa, SCARG(&outuap, mskval), SCARG(&outuap, msklen)))
842 		return (error);
843 	if (error = copyout(&sa, SCARG(&outuap, mtchval), SCARG(&outuap, mtchlen)))
844 		return (error);
845 
846 	return nfssvc(p, &outuap, retval);
847 #else
848 	return (ENOSYS);
849 #endif
850 }
851 #endif /* NFSSERVER */
852 
853 int
854 sunos_sys_ustat(p, v, retval)
855 	struct proc *p;
856 	void *v;
857 	register_t *retval;
858 {
859 	struct sunos_sys_ustat_args *uap = v;
860 	struct sunos_ustat us;
861 	int error;
862 
863 	memset(&us, 0, sizeof us);
864 
865 	/*
866 	 * XXX: should set f_tfree and f_tinode at least
867 	 * How do we translate dev -> fstat? (and then to sunos_ustat)
868 	 */
869 
870 	if ((error = copyout(&us, SCARG(uap, buf), sizeof us)) != 0)
871 		return (error);
872 	return 0;
873 }
874 
875 int
876 sunos_sys_quotactl(p, v, retval)
877 	struct proc *p;
878 	void *v;
879 	register_t *retval;
880 {
881 
882 	return EINVAL;
883 }
884 
885 int
886 sunos_sys_vhangup(p, v, retval)
887 	struct proc *p;
888 	void *v;
889 	register_t *retval;
890 {
891 	struct session *sp = p->p_session;
892 
893 	if (sp->s_ttyvp == 0)
894 		return 0;
895 
896 	if (sp->s_ttyp && sp->s_ttyp->t_session == sp && sp->s_ttyp->t_pgrp)
897 		pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
898 
899 	(void) ttywait(sp->s_ttyp);
900 	if (sp->s_ttyvp)
901 		VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
902 	if (sp->s_ttyvp)
903 		vrele(sp->s_ttyvp);
904 	sp->s_ttyvp = NULL;
905 
906 	return 0;
907 }
908 
909 static int
910 sunstatfs(sp, buf)
911 	struct statfs *sp;
912 	caddr_t buf;
913 {
914 	struct sunos_statfs ssfs;
915 
916 	memset(&ssfs, 0, sizeof ssfs);
917 	ssfs.f_type = 0;
918 	ssfs.f_bsize = sp->f_bsize;
919 	ssfs.f_blocks = sp->f_blocks;
920 	ssfs.f_bfree = sp->f_bfree;
921 	ssfs.f_bavail = sp->f_bavail;
922 	ssfs.f_files = sp->f_files;
923 	ssfs.f_ffree = sp->f_ffree;
924 	ssfs.f_fsid = sp->f_fsid;
925 	return copyout((caddr_t)&ssfs, buf, sizeof ssfs);
926 }
927 
928 int
929 sunos_sys_statfs(p, v, retval)
930 	struct proc *p;
931 	void *v;
932 	register_t *retval;
933 {
934 	struct sunos_sys_statfs_args *uap = v;
935 	register struct mount *mp;
936 	register struct statfs *sp;
937 	int error;
938 	struct nameidata nd;
939 
940 	caddr_t sg = stackgap_init(p->p_emul);
941 	SUNOS_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
942 
943 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
944 	if ((error = namei(&nd)) != 0)
945 		return (error);
946 	mp = nd.ni_vp->v_mount;
947 	sp = &mp->mnt_stat;
948 	vrele(nd.ni_vp);
949 	if ((error = VFS_STATFS(mp, sp, p)) != 0)
950 		return (error);
951 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
952 	return sunstatfs(sp, (caddr_t)SCARG(uap, buf));
953 }
954 
955 int
956 sunos_sys_fstatfs(p, v, retval)
957 	struct proc *p;
958 	void *v;
959 	register_t *retval;
960 {
961 	struct sunos_sys_fstatfs_args *uap = v;
962 	struct file *fp;
963 	struct mount *mp;
964 	register struct statfs *sp;
965 	int error;
966 
967 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
968 		return (error);
969 	mp = ((struct vnode *)fp->f_data)->v_mount;
970 	sp = &mp->mnt_stat;
971 	if ((error = VFS_STATFS(mp, sp, p)) != 0)
972 		return (error);
973 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
974 	return sunstatfs(sp, (caddr_t)SCARG(uap, buf));
975 }
976 
977 int
978 sunos_sys_exportfs(p, v, retval)
979 	struct proc *p;
980 	void *v;
981 	register_t *retval;
982 {
983 	/*
984 	 * XXX: should perhaps translate into a mount(2)
985 	 * with MOUNT_EXPORT?
986 	 */
987 	return 0;
988 }
989 
990 int
991 sunos_sys_mknod(p, v, retval)
992 	struct proc *p;
993 	void *v;
994 	register_t *retval;
995 {
996 	struct sunos_sys_mknod_args *uap = v;
997 
998 	caddr_t sg = stackgap_init(p->p_emul);
999 	SUNOS_CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
1000 
1001 	if (S_ISFIFO(SCARG(uap, mode)))
1002 		return sys_mkfifo(p, uap, retval);
1003 
1004 	return sys_mknod(p, (struct sys_mknod_args *)uap, retval);
1005 }
1006 
1007 #define SUNOS_SC_ARG_MAX	1
1008 #define SUNOS_SC_CHILD_MAX	2
1009 #define SUNOS_SC_CLK_TCK	3
1010 #define SUNOS_SC_NGROUPS_MAX	4
1011 #define SUNOS_SC_OPEN_MAX	5
1012 #define SUNOS_SC_JOB_CONTROL	6
1013 #define SUNOS_SC_SAVED_IDS	7
1014 #define SUNOS_SC_VERSION	8
1015 
1016 int
1017 sunos_sys_sysconf(p, v, retval)
1018 	struct proc *p;
1019 	void *v;
1020 	register_t *retval;
1021 {
1022 	struct sunos_sys_sysconf_args *uap = v;
1023 	extern int maxfiles;
1024 
1025 	switch(SCARG(uap, name)) {
1026 	case SUNOS_SC_ARG_MAX:
1027 		*retval = ARG_MAX;
1028 		break;
1029 	case SUNOS_SC_CHILD_MAX:
1030 		*retval = maxproc;
1031 		break;
1032 	case SUNOS_SC_CLK_TCK:
1033 		*retval = 60;		/* should this be `hz', ie. 100? */
1034 		break;
1035 	case SUNOS_SC_NGROUPS_MAX:
1036 		*retval = NGROUPS_MAX;
1037 		break;
1038 	case SUNOS_SC_OPEN_MAX:
1039 		*retval = maxfiles;
1040 		break;
1041 	case SUNOS_SC_JOB_CONTROL:
1042 		*retval = 1;
1043 		break;
1044 	case SUNOS_SC_SAVED_IDS:
1045 #ifdef _POSIX_SAVED_IDS
1046 		*retval = 1;
1047 #else
1048 		*retval = 0;
1049 #endif
1050 		break;
1051 	case SUNOS_SC_VERSION:
1052 		*retval = 198808;
1053 		break;
1054 	default:
1055 		return EINVAL;
1056 	}
1057 	return 0;
1058 }
1059 
1060 #define SUNOS_RLIMIT_NOFILE	6	/* Other RLIMIT_* are the same */
1061 #define SUNOS_RLIM_NLIMITS	7
1062 
1063 int
1064 sunos_sys_getrlimit(p, v, retval)
1065 	struct proc *p;
1066 	void *v;
1067 	register_t *retval;
1068 {
1069 	struct sunos_sys_getrlimit_args *uap = v;
1070 
1071 	if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
1072 		return EINVAL;
1073 
1074 	if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
1075 		SCARG(uap, which) = RLIMIT_NOFILE;
1076 
1077 	return compat_43_sys_getrlimit(p, uap, retval);
1078 }
1079 
1080 int
1081 sunos_sys_setrlimit(p, v, retval)
1082 	struct proc *p;
1083 	void *v;
1084 	register_t *retval;
1085 {
1086 	struct sunos_sys_getrlimit_args *uap = v;
1087 
1088 	if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
1089 		return EINVAL;
1090 
1091 	if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
1092 		SCARG(uap, which) = RLIMIT_NOFILE;
1093 
1094 	return compat_43_sys_setrlimit(p, uap, retval);
1095 }
1096 
1097 /* for the m68k machines */
1098 #ifndef PT_GETFPREGS
1099 #define PT_GETFPREGS -1
1100 #endif
1101 #ifndef PT_SETFPREGS
1102 #define PT_SETFPREGS -1
1103 #endif
1104 
1105 static int sreq2breq[] = {
1106 	PT_TRACE_ME,    PT_READ_I,      PT_READ_D,      -1,
1107 	PT_WRITE_I,     PT_WRITE_D,     -1,             PT_CONTINUE,
1108 	PT_KILL,        -1,             PT_ATTACH,      PT_DETACH,
1109 	PT_GETREGS,     PT_SETREGS,     PT_GETFPREGS,   PT_SETFPREGS
1110 };
1111 static int nreqs = sizeof(sreq2breq) / sizeof(sreq2breq[0]);
1112 
1113 int
1114 sunos_sys_ptrace(p, v, retval)
1115 	struct proc *p;
1116 	void *v;
1117 	register_t *retval;
1118 {
1119 	struct sunos_sys_ptrace_args *uap = v;
1120 	struct sys_ptrace_args pa;
1121 	int req;
1122 
1123 	req = SCARG(uap, req);
1124 
1125 	if (req < 0 || req >= nreqs)
1126 		return (EINVAL);
1127 
1128 	req = sreq2breq[req];
1129 	if (req == -1)
1130 		return (EINVAL);
1131 
1132 	SCARG(&pa, req) = req;
1133 	SCARG(&pa, pid) = (pid_t)SCARG(uap, pid);
1134 	SCARG(&pa, addr) = (caddr_t)SCARG(uap, addr);
1135 	SCARG(&pa, data) = SCARG(uap, data);
1136 
1137 	return sys_ptrace(p, &pa, retval);
1138 }
1139 
1140 /*
1141  * SunOS reboot system call (for compatibility).
1142  * Sun lets you pass in a boot string which the PROM
1143  * saves and provides to the next boot program.
1144  */
1145 
1146 #define SUNOS_RB_ASKNAME	0x001
1147 #define SUNOS_RB_SINGLE 	0x002
1148 #define SUNOS_RB_NOSYNC		0x004
1149 #define SUNOS_RB_HALT		0x008
1150 #define SUNOS_RB_DUMP		0x080
1151 #define	SUNOS_RB_STRING		0x200
1152 
1153 static struct sunos_howto_conv {
1154 	int sun_howto;
1155 	int bsd_howto;
1156 } sunos_howto_conv[] = {
1157 	{ SUNOS_RB_ASKNAME,	RB_ASKNAME },
1158 	{ SUNOS_RB_SINGLE,	RB_SINGLE },
1159 	{ SUNOS_RB_NOSYNC,	RB_NOSYNC },
1160 	{ SUNOS_RB_HALT,	RB_HALT },
1161 	{ SUNOS_RB_DUMP,	RB_DUMP },
1162 	{ SUNOS_RB_STRING,	RB_STRING },
1163 	{ 0x000,		0 },
1164 };
1165 
1166 int
1167 sunos_sys_reboot(p, v, retval)
1168 	struct proc *p;
1169 	void *v;
1170 	register_t *retval;
1171 {
1172 	struct sunos_sys_reboot_args *uap = v;
1173 	struct sys_reboot_args ua;
1174 	struct sunos_howto_conv *convp;
1175 	int error, bsd_howto, sun_howto;
1176 	char *bootstr;
1177 
1178 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1179 		return (error);
1180 
1181 	/*
1182 	 * Convert howto bits to BSD format.
1183 	 */
1184 	sun_howto = SCARG(uap, howto);
1185 	bsd_howto = 0;
1186 	convp = sunos_howto_conv;
1187 	while (convp->sun_howto) {
1188 		if (sun_howto & convp->sun_howto)
1189 			bsd_howto |= convp->bsd_howto;
1190 		convp++;
1191 	}
1192 
1193 	/*
1194 	 * Sun RB_STRING (Get user supplied bootstring.)
1195 	 * If the machine supports passing a string to the
1196 	 * next booted kernel.
1197 	 */
1198 	if (sun_howto & SUNOS_RB_STRING) {
1199 		char bs[128];
1200 
1201 		error = copyinstr(SCARG(uap, bootstr), bs, sizeof(bs), 0);
1202 
1203 		if (error)
1204 			bootstr = NULL;
1205 		else
1206 			bootstr = bs;
1207 	} else
1208 		bootstr = NULL;
1209 
1210 	SCARG(&ua, opt) = bsd_howto;
1211 	SCARG(&ua, bootstr) = bootstr;
1212 	sys_reboot(p, &ua, retval);
1213 	return(0);
1214 }
1215 
1216 /*
1217  * Generalized interface signal handler, 4.3-compatible.
1218  */
1219 /* ARGSUSED */
1220 int
1221 sunos_sys_sigvec(p, v, retval)
1222 	struct proc *p;
1223 	void *v;
1224 	register_t *retval;
1225 {
1226 	struct sunos_sys_sigvec_args /* {
1227 		syscallarg(int) signum;
1228 		syscallarg(struct sigvec *) nsv;
1229 		syscallarg(struct sigvec *) osv;
1230 	} */ *uap = v;
1231 	struct sigvec nsv, osv;
1232 	struct sigaction nsa, osa;
1233 	int error;
1234 /*XXX*/extern	void compat_43_sigvec_to_sigaction
1235 		__P((const struct sigvec *, struct sigaction *));
1236 /*XXX*/extern	void compat_43_sigaction_to_sigvec
1237 		__P((const struct sigaction *, struct sigvec *));
1238 
1239 	if (SCARG(uap, nsv)) {
1240 		error = copyin(SCARG(uap, nsv), &nsv, sizeof(nsv));
1241 		if (error != 0)
1242 			return (error);
1243 
1244 		/*
1245 		 * SunOS uses the mask 0x0004 as SV_RESETHAND
1246 		 * meaning: `reset to SIG_DFL on delivery'.
1247 		 * We support only the bits in: 0xF
1248 		 * (those bits are the same as ours)
1249 		 */
1250 		if (nsv.sv_flags & ~0xF)
1251 			return (EINVAL);
1252 
1253 		compat_43_sigvec_to_sigaction(&nsv, &nsa);
1254 	}
1255 	error = sigaction1(p, SCARG(uap, signum),
1256 			   SCARG(uap, nsv) ? &nsa : 0,
1257 			   SCARG(uap, osv) ? &osa : 0);
1258 	if (error != 0)
1259 		return (error);
1260 
1261 	if (SCARG(uap, osv)) {
1262 		compat_43_sigaction_to_sigvec(&osa, &osv);
1263 		error = copyout(&osv, SCARG(uap, osv), sizeof(osv));
1264 		if (error != 0)
1265 			return (error);
1266 	}
1267 
1268 	return (0);
1269 }
1270