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