xref: /netbsd-src/sys/compat/sunos/sunos_misc.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: sunos_misc.c,v 1.110 2001/06/14 20:32:45 thorpej 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 #if defined(_KERNEL_OPT)
59 #include "opt_nfsserver.h"
60 #include "fs_nfs.h"
61 #endif
62 
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/namei.h>
66 #include <sys/proc.h>
67 #include <sys/dirent.h>
68 #include <sys/file.h>
69 #include <sys/stat.h>
70 #include <sys/filedesc.h>
71 #include <sys/ioctl.h>
72 #include <sys/kernel.h>
73 #include <sys/reboot.h>
74 #include <sys/malloc.h>
75 #include <sys/mbuf.h>
76 #include <sys/mman.h>
77 #include <sys/mount.h>
78 #include <sys/ptrace.h>
79 #include <sys/resource.h>
80 #include <sys/resourcevar.h>
81 #include <sys/signal.h>
82 #include <sys/signalvar.h>
83 #include <sys/socket.h>
84 #include <sys/tty.h>
85 #include <sys/vnode.h>
86 #include <sys/uio.h>
87 #include <sys/wait.h>
88 #include <sys/utsname.h>
89 #include <sys/unistd.h>
90 #include <sys/syscallargs.h>
91 #include <sys/conf.h>
92 #include <sys/socketvar.h>
93 #include <sys/exec.h>
94 #include <sys/swap.h>
95 
96 #include <compat/sunos/sunos.h>
97 #include <compat/sunos/sunos_syscallargs.h>
98 #include <compat/common/compat_util.h>
99 #include <compat/sunos/sunos_dirent.h>
100 
101 #include <netinet/in.h>
102 
103 #include <miscfs/specfs/specdev.h>
104 
105 #include <nfs/rpcv2.h>
106 #include <nfs/nfsproto.h>
107 #include <nfs/nfs.h>
108 #include <nfs/nfsmount.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 	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 	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 	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 	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 	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 	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 = (char *)(u_long)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 	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 	/* getvnode() will use the descriptor for us */
495 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
496 		return (error);
497 
498 	if ((fp->f_flag & FREAD) == 0) {
499 		error = EBADF;
500 		goto out1;
501 	}
502 
503 	vp = (struct vnode *)fp->f_data;
504 	if (vp->v_type != VDIR) {
505 		error = EINVAL;
506 		goto out1;
507 	}
508 
509 	buflen = min(MAXBSIZE, SCARG(uap, nbytes));
510 	buf = malloc(buflen, M_TEMP, M_WAITOK);
511 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
512 	off = fp->f_offset;
513 again:
514 	aiov.iov_base = buf;
515 	aiov.iov_len = buflen;
516 	auio.uio_iov = &aiov;
517 	auio.uio_iovcnt = 1;
518 	auio.uio_rw = UIO_READ;
519 	auio.uio_segflg = UIO_SYSSPACE;
520 	auio.uio_procp = p;
521 	auio.uio_resid = buflen;
522 	auio.uio_offset = off;
523 	/*
524 	 * First we read into the malloc'ed buffer, then
525 	 * we massage it into user space, one record at a time.
526 	 */
527 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
528 	    &ncookies);
529 	if (error)
530 		goto out;
531 
532 	inp = buf;
533 	outp = SCARG(uap, buf);
534 	resid = SCARG(uap, nbytes);
535 	if ((len = buflen - auio.uio_resid) == 0)
536 		goto eof;
537 
538 	for (cookie = cookiebuf; len > 0; len -= reclen) {
539 		bdp = (struct dirent *)inp;
540 		reclen = bdp->d_reclen;
541 		if (reclen & 3)
542 			panic("sunos_getdents");
543 		if ((*cookie >> 32) != 0) {
544 			compat_offseterr(vp, "sunos_getdents");
545 			error = EINVAL;
546 			goto out;
547 		}
548 		if (bdp->d_fileno == 0) {
549 			inp += reclen;	/* it is a hole; squish it out */
550 			off = *cookie++;
551 			continue;
552 		}
553 		sunos_reclen = SUNOS_RECLEN(&idb, bdp->d_namlen);
554 		if (reclen > len || resid < sunos_reclen) {
555 			/* entry too big for buffer, so just stop */
556 			outp++;
557 			break;
558 		}
559 		off = *cookie++;	/* each entry points to next */
560 		/*
561 		 * Massage in place to make a Sun-shaped dirent (otherwise
562 		 * we have to worry about touching user memory outside of
563 		 * the copyout() call).
564 		 */
565 		idb.d_fileno = bdp->d_fileno;
566 		idb.d_off = off;
567 		idb.d_reclen = sunos_reclen;
568 		idb.d_namlen = bdp->d_namlen;
569 		strcpy(idb.d_name, bdp->d_name);
570 		if ((error = copyout((caddr_t)&idb, outp, sunos_reclen)) != 0)
571 			goto out;
572 		/* advance past this real entry */
573 		inp += reclen;
574 		/* advance output past Sun-shaped entry */
575 		outp += sunos_reclen;
576 		resid -= sunos_reclen;
577 	}
578 
579 	/* if we squished out the whole block, try again */
580 	if (outp == SCARG(uap, buf))
581 		goto again;
582 	fp->f_offset = off;		/* update the vnode offset */
583 
584 eof:
585 	*retval = SCARG(uap, nbytes) - resid;
586 out:
587 	VOP_UNLOCK(vp, 0);
588 	free(cookiebuf, M_TEMP);
589 	free(buf, M_TEMP);
590  out1:
591 	FILE_UNUSE(fp, p);
592 	return (error);
593 }
594 
595 #define	SUNOS__MAP_NEW	0x80000000	/* if not, old mmap & cannot handle */
596 
597 int
598 sunos_sys_mmap(p, v, retval)
599 	struct proc *p;
600 	void *v;
601 	register_t *retval;
602 {
603 	struct sunos_sys_mmap_args *uap = v;
604 	struct sys_mmap_args ouap;
605 	struct filedesc *fdp;
606 	struct file *fp;
607 	struct vnode *vp;
608 
609 	/*
610 	 * Verify the arguments.
611 	 */
612 	if (SCARG(uap, prot) & ~(PROT_READ|PROT_WRITE|PROT_EXEC))
613 		return (EINVAL);			/* XXX still needed? */
614 
615 	if ((SCARG(uap, flags) & SUNOS__MAP_NEW) == 0)
616 		return (EINVAL);
617 
618 	SCARG(&ouap, flags) = SCARG(uap, flags) & ~SUNOS__MAP_NEW;
619 	SCARG(&ouap, addr) = SCARG(uap, addr);
620 
621 	if ((SCARG(&ouap, flags) & MAP_FIXED) == 0 &&
622 	    SCARG(&ouap, addr) != 0 &&
623 	    SCARG(&ouap, addr) < (void *)round_page((vaddr_t)p->p_vmspace->vm_daddr+MAXDSIZ))
624 		SCARG(&ouap, addr) = (caddr_t)round_page((vaddr_t)p->p_vmspace->vm_daddr+MAXDSIZ);
625 
626 	SCARG(&ouap, len) = SCARG(uap, len);
627 	SCARG(&ouap, prot) = SCARG(uap, prot);
628 	SCARG(&ouap, fd) = SCARG(uap, fd);
629 	SCARG(&ouap, pos) = SCARG(uap, pos);
630 
631 	/*
632 	 * Special case: if fd refers to /dev/zero, map as MAP_ANON.  (XXX)
633 	 */
634 	fdp = p->p_fd;
635 	if ((fp = fd_getfile(fdp, SCARG(&ouap, fd))) != NULL &&		/*XXX*/
636 	    fp->f_type == DTYPE_VNODE &&				/*XXX*/
637 	    (vp = (struct vnode *)fp->f_data)->v_type == VCHR &&	/*XXX*/
638 	    iszerodev(vp->v_rdev)) {					/*XXX*/
639 		SCARG(&ouap, flags) |= MAP_ANON;
640 		SCARG(&ouap, fd) = -1;
641 	}
642 
643 	return (sys_mmap(p, &ouap, retval));
644 }
645 
646 #define	MC_SYNC		1
647 #define	MC_LOCK		2
648 #define	MC_UNLOCK	3
649 #define	MC_ADVISE	4
650 #define	MC_LOCKAS	5
651 #define	MC_UNLOCKAS	6
652 
653 int
654 sunos_sys_mctl(p, v, retval)
655 	struct proc *p;
656 	void *v;
657 	register_t *retval;
658 {
659 	struct sunos_sys_mctl_args *uap = v;
660 
661 	switch (SCARG(uap, func)) {
662 	case MC_ADVISE:		/* ignore for now */
663 		return (0);
664 	case MC_SYNC:		/* translate to msync */
665 		return (sys___msync13(p, uap, retval));
666 	default:
667 		return (EINVAL);
668 	}
669 }
670 
671 int
672 sunos_sys_setsockopt(p, v, retval)
673 	struct proc *p;
674 	void *v;
675 	register_t *retval;
676 {
677 	struct sunos_sys_setsockopt_args *uap = v;
678 	struct file *fp;
679 	struct mbuf *m = NULL;
680 	int error;
681 
682 	/* getsock() will use the descriptor for us */
683 	if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
684 		return (error);
685 #define	SO_DONTLINGER (~SO_LINGER)
686 	if (SCARG(uap, name) == SO_DONTLINGER) {
687 		m = m_get(M_WAIT, MT_SOOPTS);
688 		mtod(m, struct linger *)->l_onoff = 0;
689 		m->m_len = sizeof(struct linger);
690 		error = sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
691 		    SO_LINGER, m);
692 		goto out;
693 	}
694 	if (SCARG(uap, level) == IPPROTO_IP) {
695 #define		SUNOS_IP_MULTICAST_IF		2
696 #define		SUNOS_IP_MULTICAST_TTL		3
697 #define		SUNOS_IP_MULTICAST_LOOP		4
698 #define		SUNOS_IP_ADD_MEMBERSHIP		5
699 #define		SUNOS_IP_DROP_MEMBERSHIP	6
700 		static int ipoptxlat[] = {
701 			IP_MULTICAST_IF,
702 			IP_MULTICAST_TTL,
703 			IP_MULTICAST_LOOP,
704 			IP_ADD_MEMBERSHIP,
705 			IP_DROP_MEMBERSHIP
706 		};
707 		if (SCARG(uap, name) >= SUNOS_IP_MULTICAST_IF &&
708 		    SCARG(uap, name) <= SUNOS_IP_DROP_MEMBERSHIP) {
709 			SCARG(uap, name) =
710 			    ipoptxlat[SCARG(uap, name) - SUNOS_IP_MULTICAST_IF];
711 		}
712 	}
713 	if (SCARG(uap, valsize) > MLEN) {
714 		error = EINVAL;
715 		goto out;
716 	}
717 	if (SCARG(uap, val)) {
718 		m = m_get(M_WAIT, MT_SOOPTS);
719 		error = copyin(SCARG(uap, val), mtod(m, caddr_t),
720 		    (u_int)SCARG(uap, valsize));
721 		if (error) {
722 			(void) m_free(m);
723 			goto out;
724 		}
725 		m->m_len = SCARG(uap, valsize);
726 	}
727 	error = sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
728 	    SCARG(uap, name), m);
729  out:
730 	FILE_UNUSE(fp, p);
731 	return (error);
732 }
733 
734 /*
735  * XXX: This needs cleaning up.
736  */
737 int
738 sunos_sys_auditsys(p, v, retval)
739 	struct proc *p;
740 	void *v;
741 	register_t *retval;
742 {
743 	return 0;
744 }
745 
746 int
747 sunos_sys_uname(p, v, retval)
748 	struct proc *p;
749 	void *v;
750 	register_t *retval;
751 {
752 	struct sunos_sys_uname_args *uap = v;
753 	struct sunos_utsname sut;
754 
755 	memset(&sut, 0, sizeof(sut));
756 
757 	memcpy(sut.sysname, ostype, sizeof(sut.sysname) - 1);
758 	memcpy(sut.nodename, hostname, sizeof(sut.nodename));
759 	sut.nodename[sizeof(sut.nodename)-1] = '\0';
760 	memcpy(sut.release, osrelease, sizeof(sut.release) - 1);
761 	memcpy(sut.version, "1", sizeof(sut.version) - 1);
762 	memcpy(sut.machine, machine, sizeof(sut.machine) - 1);
763 
764 	return copyout((caddr_t)&sut, (caddr_t)SCARG(uap, name),
765 	    sizeof(struct sunos_utsname));
766 }
767 
768 int
769 sunos_sys_setpgrp(p, v, retval)
770 	struct proc *p;
771 	void *v;
772 	register_t *retval;
773 {
774 	struct sunos_sys_setpgrp_args *uap = v;
775 
776 	/*
777 	 * difference to our setpgid call is to include backwards
778 	 * compatibility to pre-setsid() binaries. Do setsid()
779 	 * instead of setpgid() in those cases where the process
780 	 * tries to create a new session the old way.
781 	 */
782 	if (!SCARG(uap, pgid) &&
783 	    (!SCARG(uap, pid) || SCARG(uap, pid) == p->p_pid))
784 		return sys_setsid(p, uap, retval);
785 	else
786 		return sys_setpgid(p, uap, retval);
787 }
788 
789 int
790 sunos_sys_open(p, v, retval)
791 	struct proc *p;
792 	void *v;
793 	register_t *retval;
794 {
795 	struct sunos_sys_open_args *uap = v;
796 	int l, r;
797 	int noctty;
798 	int ret;
799 
800 	caddr_t sg = stackgap_init(p->p_emul);
801 
802 	/* convert mode into NetBSD mode */
803 	l = SCARG(uap, flags);
804 	noctty = l & 0x8000;
805 	r =	(l & (0x0001 | 0x0002 | 0x0008 | 0x0040 | 0x0200 | 0x0400 | 0x0800));
806 	r |=	((l & (0x0004 | 0x1000 | 0x4000)) ? O_NONBLOCK : 0);
807 	r |=	((l & 0x0080) ? O_SHLOCK : 0);
808 	r |=	((l & 0x0100) ? O_EXLOCK : 0);
809 	r |=	((l & 0x2000) ? O_FSYNC : 0);
810 
811 	if (r & O_CREAT)
812 		CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
813 	else
814 		CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
815 
816 	SCARG(uap, flags) = r;
817 	ret = sys_open(p, (struct sys_open_args *)uap, retval);
818 
819 	if (!ret && !noctty && SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
820 		struct filedesc *fdp = p->p_fd;
821 		struct file *fp;
822 
823 		fp = fd_getfile(fdp, *retval);
824 
825 		/* ignore any error, just give it a try */
826 		if (fp != NULL && fp->f_type == DTYPE_VNODE)
827 			(fp->f_ops->fo_ioctl)(fp, TIOCSCTTY, (caddr_t)0, p);
828 	}
829 	return ret;
830 }
831 
832 #if defined (NFSSERVER)
833 int
834 sunos_sys_nfssvc(p, v, retval)
835 	struct proc *p;
836 	void *v;
837 	register_t *retval;
838 {
839 #if 0
840 	struct sunos_sys_nfssvc_args *uap = v;
841 	struct emul *e = p->p_emul;
842 	struct sys_nfssvc_args outuap;
843 	struct sockaddr sa;
844 	int error;
845 	caddr_t sg = stackgap_init(p->p_emul);
846 
847 	memset(&outuap, 0, sizeof outuap);
848 	SCARG(&outuap, fd) = SCARG(uap, fd);
849 	SCARG(&outuap, mskval) = stackgap_alloc(&sg, sizeof(sa));
850 	SCARG(&outuap, msklen) = sizeof(sa);
851 	SCARG(&outuap, mtchval) = stackgap_alloc(&sg, sizeof(sa));
852 	SCARG(&outuap, mtchlen) = sizeof(sa);
853 
854 	memset(&sa, 0, sizeof sa);
855 	if (error = copyout(&sa, SCARG(&outuap, mskval), SCARG(&outuap, msklen)))
856 		return (error);
857 	if (error = copyout(&sa, SCARG(&outuap, mtchval), SCARG(&outuap, mtchlen)))
858 		return (error);
859 
860 	return nfssvc(p, &outuap, retval);
861 #else
862 	return (ENOSYS);
863 #endif
864 }
865 #endif /* NFSSERVER */
866 
867 int
868 sunos_sys_ustat(p, v, retval)
869 	struct proc *p;
870 	void *v;
871 	register_t *retval;
872 {
873 	struct sunos_sys_ustat_args *uap = v;
874 	struct sunos_ustat us;
875 	int error;
876 
877 	memset(&us, 0, sizeof us);
878 
879 	/*
880 	 * XXX: should set f_tfree and f_tinode at least
881 	 * How do we translate dev -> fstat? (and then to sunos_ustat)
882 	 */
883 
884 	if ((error = copyout(&us, SCARG(uap, buf), sizeof us)) != 0)
885 		return (error);
886 	return 0;
887 }
888 
889 int
890 sunos_sys_quotactl(p, v, retval)
891 	struct proc *p;
892 	void *v;
893 	register_t *retval;
894 {
895 
896 	return EINVAL;
897 }
898 
899 int
900 sunos_sys_vhangup(p, v, retval)
901 	struct proc *p;
902 	void *v;
903 	register_t *retval;
904 {
905 	struct session *sp = p->p_session;
906 
907 	if (sp->s_ttyvp == 0)
908 		return 0;
909 
910 	if (sp->s_ttyp && sp->s_ttyp->t_session == sp && sp->s_ttyp->t_pgrp)
911 		pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
912 
913 	(void) ttywait(sp->s_ttyp);
914 	if (sp->s_ttyvp)
915 		VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
916 	if (sp->s_ttyvp)
917 		vrele(sp->s_ttyvp);
918 	sp->s_ttyvp = NULL;
919 
920 	return 0;
921 }
922 
923 static int
924 sunstatfs(sp, buf)
925 	struct statfs *sp;
926 	caddr_t buf;
927 {
928 	struct sunos_statfs ssfs;
929 
930 	memset(&ssfs, 0, sizeof ssfs);
931 	ssfs.f_type = 0;
932 	ssfs.f_bsize = sp->f_bsize;
933 	ssfs.f_blocks = sp->f_blocks;
934 	ssfs.f_bfree = sp->f_bfree;
935 	ssfs.f_bavail = sp->f_bavail;
936 	ssfs.f_files = sp->f_files;
937 	ssfs.f_ffree = sp->f_ffree;
938 	ssfs.f_fsid = sp->f_fsid;
939 	return copyout((caddr_t)&ssfs, buf, sizeof ssfs);
940 }
941 
942 int
943 sunos_sys_statfs(p, v, retval)
944 	struct proc *p;
945 	void *v;
946 	register_t *retval;
947 {
948 	struct sunos_sys_statfs_args *uap = v;
949 	struct mount *mp;
950 	struct statfs *sp;
951 	int error;
952 	struct nameidata nd;
953 
954 	caddr_t sg = stackgap_init(p->p_emul);
955 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
956 
957 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
958 	if ((error = namei(&nd)) != 0)
959 		return (error);
960 	mp = nd.ni_vp->v_mount;
961 	sp = &mp->mnt_stat;
962 	vrele(nd.ni_vp);
963 	if ((error = VFS_STATFS(mp, sp, p)) != 0)
964 		return (error);
965 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
966 	return sunstatfs(sp, (caddr_t)SCARG(uap, buf));
967 }
968 
969 int
970 sunos_sys_fstatfs(p, v, retval)
971 	struct proc *p;
972 	void *v;
973 	register_t *retval;
974 {
975 	struct sunos_sys_fstatfs_args *uap = v;
976 	struct file *fp;
977 	struct mount *mp;
978 	struct statfs *sp;
979 	int error;
980 
981 	/* getvnode() will use the descriptor for us */
982 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
983 		return (error);
984 	mp = ((struct vnode *)fp->f_data)->v_mount;
985 	sp = &mp->mnt_stat;
986 	if ((error = VFS_STATFS(mp, sp, p)) != 0)
987 		goto out;
988 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
989 	error = sunstatfs(sp, (caddr_t)SCARG(uap, buf));
990  out:
991 	FILE_UNUSE(fp, p);
992 	return (error);
993 }
994 
995 int
996 sunos_sys_exportfs(p, v, retval)
997 	struct proc *p;
998 	void *v;
999 	register_t *retval;
1000 {
1001 	/*
1002 	 * XXX: should perhaps translate into a mount(2)
1003 	 * with MOUNT_EXPORT?
1004 	 */
1005 	return 0;
1006 }
1007 
1008 int
1009 sunos_sys_mknod(p, v, retval)
1010 	struct proc *p;
1011 	void *v;
1012 	register_t *retval;
1013 {
1014 	struct sunos_sys_mknod_args *uap = v;
1015 
1016 	caddr_t sg = stackgap_init(p->p_emul);
1017 	CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
1018 
1019 	if (S_ISFIFO(SCARG(uap, mode)))
1020 		return sys_mkfifo(p, uap, retval);
1021 
1022 	return sys_mknod(p, (struct sys_mknod_args *)uap, retval);
1023 }
1024 
1025 #define SUNOS_SC_ARG_MAX	1
1026 #define SUNOS_SC_CHILD_MAX	2
1027 #define SUNOS_SC_CLK_TCK	3
1028 #define SUNOS_SC_NGROUPS_MAX	4
1029 #define SUNOS_SC_OPEN_MAX	5
1030 #define SUNOS_SC_JOB_CONTROL	6
1031 #define SUNOS_SC_SAVED_IDS	7
1032 #define SUNOS_SC_VERSION	8
1033 
1034 int
1035 sunos_sys_sysconf(p, v, retval)
1036 	struct proc *p;
1037 	void *v;
1038 	register_t *retval;
1039 {
1040 	struct sunos_sys_sysconf_args *uap = v;
1041 	extern int maxfiles;
1042 
1043 	switch(SCARG(uap, name)) {
1044 	case SUNOS_SC_ARG_MAX:
1045 		*retval = ARG_MAX;
1046 		break;
1047 	case SUNOS_SC_CHILD_MAX:
1048 		*retval = maxproc;
1049 		break;
1050 	case SUNOS_SC_CLK_TCK:
1051 		*retval = 60;		/* should this be `hz', ie. 100? */
1052 		break;
1053 	case SUNOS_SC_NGROUPS_MAX:
1054 		*retval = NGROUPS_MAX;
1055 		break;
1056 	case SUNOS_SC_OPEN_MAX:
1057 		*retval = maxfiles;
1058 		break;
1059 	case SUNOS_SC_JOB_CONTROL:
1060 		*retval = 1;
1061 		break;
1062 	case SUNOS_SC_SAVED_IDS:
1063 #ifdef _POSIX_SAVED_IDS
1064 		*retval = 1;
1065 #else
1066 		*retval = 0;
1067 #endif
1068 		break;
1069 	case SUNOS_SC_VERSION:
1070 		*retval = 198808;
1071 		break;
1072 	default:
1073 		return EINVAL;
1074 	}
1075 	return 0;
1076 }
1077 
1078 #define SUNOS_RLIMIT_NOFILE	6	/* Other RLIMIT_* are the same */
1079 #define SUNOS_RLIM_NLIMITS	7
1080 
1081 int
1082 sunos_sys_getrlimit(p, v, retval)
1083 	struct proc *p;
1084 	void *v;
1085 	register_t *retval;
1086 {
1087 	struct sunos_sys_getrlimit_args *uap = v;
1088 
1089 	if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
1090 		return EINVAL;
1091 
1092 	if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
1093 		SCARG(uap, which) = RLIMIT_NOFILE;
1094 
1095 	return compat_43_sys_getrlimit(p, uap, retval);
1096 }
1097 
1098 int
1099 sunos_sys_setrlimit(p, v, retval)
1100 	struct proc *p;
1101 	void *v;
1102 	register_t *retval;
1103 {
1104 	struct sunos_sys_getrlimit_args *uap = v;
1105 
1106 	if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
1107 		return EINVAL;
1108 
1109 	if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
1110 		SCARG(uap, which) = RLIMIT_NOFILE;
1111 
1112 	return compat_43_sys_setrlimit(p, uap, retval);
1113 }
1114 
1115 /* for the m68k machines */
1116 #ifndef PT_GETFPREGS
1117 #define PT_GETFPREGS -1
1118 #endif
1119 #ifndef PT_SETFPREGS
1120 #define PT_SETFPREGS -1
1121 #endif
1122 
1123 static int sreq2breq[] = {
1124 	PT_TRACE_ME,    PT_READ_I,      PT_READ_D,      -1,
1125 	PT_WRITE_I,     PT_WRITE_D,     -1,             PT_CONTINUE,
1126 	PT_KILL,        -1,             PT_ATTACH,      PT_DETACH,
1127 	PT_GETREGS,     PT_SETREGS,     PT_GETFPREGS,   PT_SETFPREGS
1128 };
1129 static int nreqs = sizeof(sreq2breq) / sizeof(sreq2breq[0]);
1130 
1131 int
1132 sunos_sys_ptrace(p, v, retval)
1133 	struct proc *p;
1134 	void *v;
1135 	register_t *retval;
1136 {
1137 	struct sunos_sys_ptrace_args *uap = v;
1138 	struct sys_ptrace_args pa;
1139 	int req;
1140 
1141 	req = SCARG(uap, req);
1142 
1143 	if (req < 0 || req >= nreqs)
1144 		return (EINVAL);
1145 
1146 	req = sreq2breq[req];
1147 	if (req == -1)
1148 		return (EINVAL);
1149 
1150 	SCARG(&pa, req) = req;
1151 	SCARG(&pa, pid) = (pid_t)SCARG(uap, pid);
1152 	SCARG(&pa, addr) = (caddr_t)SCARG(uap, addr);
1153 	SCARG(&pa, data) = SCARG(uap, data);
1154 
1155 	return sys_ptrace(p, &pa, retval);
1156 }
1157 
1158 /*
1159  * SunOS reboot system call (for compatibility).
1160  * Sun lets you pass in a boot string which the PROM
1161  * saves and provides to the next boot program.
1162  */
1163 
1164 #define SUNOS_RB_ASKNAME	0x001
1165 #define SUNOS_RB_SINGLE 	0x002
1166 #define SUNOS_RB_NOSYNC		0x004
1167 #define SUNOS_RB_HALT		0x008
1168 #define SUNOS_RB_DUMP		0x080
1169 #define	SUNOS_RB_STRING		0x200
1170 
1171 static struct sunos_howto_conv {
1172 	int sun_howto;
1173 	int bsd_howto;
1174 } sunos_howto_conv[] = {
1175 	{ SUNOS_RB_ASKNAME,	RB_ASKNAME },
1176 	{ SUNOS_RB_SINGLE,	RB_SINGLE },
1177 	{ SUNOS_RB_NOSYNC,	RB_NOSYNC },
1178 	{ SUNOS_RB_HALT,	RB_HALT },
1179 	{ SUNOS_RB_DUMP,	RB_DUMP },
1180 	{ SUNOS_RB_STRING,	RB_STRING },
1181 	{ 0x000,		0 },
1182 };
1183 
1184 int
1185 sunos_sys_reboot(p, v, retval)
1186 	struct proc *p;
1187 	void *v;
1188 	register_t *retval;
1189 {
1190 	struct sunos_sys_reboot_args *uap = v;
1191 	struct sys_reboot_args ua;
1192 	struct sunos_howto_conv *convp;
1193 	int error, bsd_howto, sun_howto;
1194 	char *bootstr;
1195 
1196 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1197 		return (error);
1198 
1199 	/*
1200 	 * Convert howto bits to BSD format.
1201 	 */
1202 	sun_howto = SCARG(uap, howto);
1203 	bsd_howto = 0;
1204 	convp = sunos_howto_conv;
1205 	while (convp->sun_howto) {
1206 		if (sun_howto & convp->sun_howto)
1207 			bsd_howto |= convp->bsd_howto;
1208 		convp++;
1209 	}
1210 
1211 	/*
1212 	 * Sun RB_STRING (Get user supplied bootstring.)
1213 	 * If the machine supports passing a string to the
1214 	 * next booted kernel.
1215 	 */
1216 	if (sun_howto & SUNOS_RB_STRING) {
1217 		char bs[128];
1218 
1219 		error = copyinstr(SCARG(uap, bootstr), bs, sizeof(bs), 0);
1220 
1221 		if (error)
1222 			bootstr = NULL;
1223 		else
1224 			bootstr = bs;
1225 	} else
1226 		bootstr = NULL;
1227 
1228 	SCARG(&ua, opt) = bsd_howto;
1229 	SCARG(&ua, bootstr) = bootstr;
1230 	sys_reboot(p, &ua, retval);
1231 	return(0);
1232 }
1233 
1234 /*
1235  * Generalized interface signal handler, 4.3-compatible.
1236  */
1237 /* ARGSUSED */
1238 int
1239 sunos_sys_sigvec(p, v, retval)
1240 	struct proc *p;
1241 	void *v;
1242 	register_t *retval;
1243 {
1244 	struct sunos_sys_sigvec_args /* {
1245 		syscallarg(int) signum;
1246 		syscallarg(struct sigvec *) nsv;
1247 		syscallarg(struct sigvec *) osv;
1248 	} */ *uap = v;
1249 	struct sigvec nsv, osv;
1250 	struct sigaction nsa, osa;
1251 	int error;
1252 /*XXX*/extern	void compat_43_sigvec_to_sigaction
1253 		__P((const struct sigvec *, struct sigaction *));
1254 /*XXX*/extern	void compat_43_sigaction_to_sigvec
1255 		__P((const struct sigaction *, struct sigvec *));
1256 
1257 	if (SCARG(uap, nsv)) {
1258 		error = copyin(SCARG(uap, nsv), &nsv, sizeof(nsv));
1259 		if (error != 0)
1260 			return (error);
1261 
1262 		/*
1263 		 * SunOS uses the mask 0x0004 as SV_RESETHAND
1264 		 * meaning: `reset to SIG_DFL on delivery'.
1265 		 * We support only the bits in: 0xF
1266 		 * (those bits are the same as ours)
1267 		 */
1268 		if (nsv.sv_flags & ~0xF)
1269 			return (EINVAL);
1270 
1271 		compat_43_sigvec_to_sigaction(&nsv, &nsa);
1272 	}
1273 	error = sigaction1(p, SCARG(uap, signum),
1274 			   SCARG(uap, nsv) ? &nsa : 0,
1275 			   SCARG(uap, osv) ? &osa : 0);
1276 	if (error != 0)
1277 		return (error);
1278 
1279 	if (SCARG(uap, osv)) {
1280 		compat_43_sigaction_to_sigvec(&osa, &osv);
1281 		error = copyout(&osv, SCARG(uap, osv), sizeof(osv));
1282 		if (error != 0)
1283 			return (error);
1284 	}
1285 
1286 	return (0);
1287 }
1288