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