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