xref: /netbsd-src/sys/compat/sunos/sunos_misc.c (revision 76dfffe33547c37f8bdd446e3e4ab0f3c16cea4b)
1 /*	$NetBSD: sunos_misc.c,v 1.74 1996/10/13 01:16:19 christos 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 
103 static int sunstatfs __P((struct statfs *, caddr_t));
104 
105 int
106 sunos_sys_stime(p, v, retval)
107 	struct proc *p;
108 	void *v;
109 	register_t *retval;
110 {
111 	struct sunos_sys_stime_args *uap = v;
112 	struct sys_settimeofday_args ap;
113 	caddr_t sg = stackgap_init(p->p_emul);
114 	struct timeval tv;
115 	int error;
116 
117 	error = copyin(SCARG(uap, tp), &tv.tv_sec, sizeof(tv.tv_sec));
118 	if (error)
119 		return error;
120 	tv.tv_usec = 0;
121 
122 	SCARG(&ap, tv) = stackgap_alloc(&sg, sizeof(struct timeval));
123 	SCARG(&ap, tzp) = NULL;
124 
125 	error = copyout(&tv, SCARG(&ap, tv), sizeof(struct timeval));
126 	if (error)
127 		return error;
128 
129 	return sys_settimeofday(p, &ap, retval);
130 }
131 
132 int
133 sunos_sys_wait4(p, v, retval)
134 	struct proc *p;
135 	void *v;
136 	register_t *retval;
137 {
138 	struct sunos_sys_wait4_args *uap = v;
139 	if (SCARG(uap, pid) == 0)
140 		SCARG(uap, pid) = WAIT_ANY;
141 	return (sys_wait4(p, uap, retval));
142 }
143 
144 int
145 sunos_sys_creat(p, v, retval)
146 	struct proc *p;
147 	void *v;
148 	register_t *retval;
149 {
150 	struct sunos_sys_creat_args *uap = v;
151 	struct sys_open_args ouap;
152 
153 	caddr_t sg = stackgap_init(p->p_emul);
154 	SUNOS_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
155 
156 	SCARG(&ouap, path) = SCARG(uap, path);
157 	SCARG(&ouap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
158 	SCARG(&ouap, mode) = SCARG(uap, mode);
159 
160 	return (sys_open(p, &ouap, retval));
161 }
162 
163 int
164 sunos_sys_access(p, v, retval)
165 	struct proc *p;
166 	void *v;
167 	register_t *retval;
168 {
169 	struct sunos_sys_access_args *uap = v;
170 	caddr_t sg = stackgap_init(p->p_emul);
171 	SUNOS_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
172 
173 	return (sys_access(p, uap, retval));
174 }
175 
176 int
177 sunos_sys_stat(p, v, retval)
178 	struct proc *p;
179 	void *v;
180 	register_t *retval;
181 {
182 	struct sunos_sys_stat_args *uap = v;
183 	caddr_t sg = stackgap_init(p->p_emul);
184 	SUNOS_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
185 
186 	return (compat_43_sys_stat(p, uap, retval));
187 }
188 
189 int
190 sunos_sys_lstat(p, v, retval)
191 	struct proc *p;
192 	void *v;
193 	register_t *retval;
194 {
195 	struct sunos_sys_lstat_args *uap = v;
196 	caddr_t sg = stackgap_init(p->p_emul);
197 	SUNOS_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
198 
199 	return (compat_43_sys_lstat(p, uap, retval));
200 }
201 
202 int
203 sunos_sys_execv(p, v, retval)
204 	struct proc *p;
205 	void *v;
206 	register_t *retval;
207 {
208 	struct sunos_sys_execv_args /* {
209 		syscallarg(char *) path;
210 		syscallarg(char **) argv;
211 	} */ *uap = v;
212 	struct sys_execve_args ap;
213 	caddr_t sg;
214 
215 	sg = stackgap_init(p->p_emul);
216 	SUNOS_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
217 
218 	SCARG(&ap, path) = SCARG(uap, path);
219 	SCARG(&ap, argp) = SCARG(uap, argp);
220 	SCARG(&ap, envp) = NULL;
221 
222 	return (sys_execve(p, &ap, retval));
223 }
224 
225 int
226 sunos_sys_execve(p, v, retval)
227 	struct proc *p;
228 	void *v;
229 	register_t *retval;
230 {
231 	struct sunos_sys_execve_args /* {
232 		syscallarg(char *) path;
233 		syscallarg(char **) argv;
234 		syscallarg(char **) envp;
235 	} */ *uap = v;
236 	struct sys_execve_args ap;
237 	caddr_t sg;
238 
239 	sg = stackgap_init(p->p_emul);
240 	SUNOS_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
241 
242 	SCARG(&ap, path) = SCARG(uap, path);
243 	SCARG(&ap, argp) = SCARG(uap, argp);
244 	SCARG(&ap, envp) = SCARG(uap, envp);
245 
246 	return (sys_execve(p, &ap, retval));
247 }
248 
249 int
250 sunos_sys_omsync(p, v, retval)
251 	struct proc *p;
252 	void *v;
253 	register_t *retval;
254 {
255 	struct sunos_sys_omsync_args *uap = v;
256 	struct sys_msync_args ouap;
257 
258 	if (SCARG(uap, flags))
259 		return (EINVAL);
260 	SCARG(&ouap, addr) = SCARG(uap, addr);
261 	SCARG(&ouap, len) = SCARG(uap, len);
262 
263 	return (sys_msync(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(NFSCLIENT)
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 /* NFSCLIENT */
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 	u_long *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 		off = *cookie++;	/* each entry points to next */
492 		if (bdp->d_fileno == 0) {
493 			inp += reclen;	/* it is a hole; squish it out */
494 			continue;
495 		}
496 		sunos_reclen = SUNOS_RECLEN(&idb, bdp->d_namlen);
497 		if (reclen > len || resid < sunos_reclen) {
498 			/* entry too big for buffer, so just stop */
499 			outp++;
500 			break;
501 		}
502 		/*
503 		 * Massage in place to make a Sun-shaped dirent (otherwise
504 		 * we have to worry about touching user memory outside of
505 		 * the copyout() call).
506 		 */
507 		idb.d_fileno = bdp->d_fileno;
508 		idb.d_off = off;
509 		idb.d_reclen = sunos_reclen;
510 		idb.d_namlen = bdp->d_namlen;
511 		strcpy(idb.d_name, bdp->d_name);
512 		if ((error = copyout((caddr_t)&idb, outp, sunos_reclen)) != 0)
513 			goto out;
514 		/* advance past this real entry */
515 		inp += reclen;
516 		/* advance output past Sun-shaped entry */
517 		outp += sunos_reclen;
518 		resid -= sunos_reclen;
519 	}
520 
521 	/* if we squished out the whole block, try again */
522 	if (outp == SCARG(uap, buf))
523 		goto again;
524 	fp->f_offset = off;		/* update the vnode offset */
525 
526 eof:
527 	*retval = SCARG(uap, nbytes) - resid;
528 out:
529 	VOP_UNLOCK(vp);
530 	free(cookiebuf, M_TEMP);
531 	free(buf, M_TEMP);
532 	return (error);
533 }
534 
535 #define	SUNOS__MAP_NEW	0x80000000	/* if not, old mmap & cannot handle */
536 
537 int
538 sunos_sys_mmap(p, v, retval)
539 	register struct proc *p;
540 	void *v;
541 	register_t *retval;
542 {
543 	register struct sunos_sys_mmap_args *uap = v;
544 	struct sys_mmap_args ouap;
545 	register struct filedesc *fdp;
546 	register struct file *fp;
547 	register struct vnode *vp;
548 
549 	/*
550 	 * Verify the arguments.
551 	 */
552 	if (SCARG(uap, prot) & ~(PROT_READ|PROT_WRITE|PROT_EXEC))
553 		return (EINVAL);			/* XXX still needed? */
554 
555 	if ((SCARG(uap, flags) & SUNOS__MAP_NEW) == 0)
556 		return (EINVAL);
557 
558 	SCARG(&ouap, flags) = SCARG(uap, flags) & ~SUNOS__MAP_NEW;
559 	SCARG(&ouap, addr) = SCARG(uap, addr);
560 
561 	if ((SCARG(&ouap, flags) & MAP_FIXED) == 0 &&
562 	    SCARG(&ouap, addr) != 0 &&
563 	    SCARG(&ouap, addr) < (caddr_t)round_page(p->p_vmspace->vm_daddr+MAXDSIZ))
564 		SCARG(&ouap, addr) = (caddr_t)round_page(p->p_vmspace->vm_daddr+MAXDSIZ);
565 
566 	SCARG(&ouap, len) = SCARG(uap, len);
567 	SCARG(&ouap, prot) = SCARG(uap, prot);
568 	SCARG(&ouap, fd) = SCARG(uap, fd);
569 	SCARG(&ouap, pos) = SCARG(uap, pos);
570 
571 	/*
572 	 * Special case: if fd refers to /dev/zero, map as MAP_ANON.  (XXX)
573 	 */
574 	fdp = p->p_fd;
575 	if ((unsigned)SCARG(&ouap, fd) < fdp->fd_nfiles &&		/*XXX*/
576 	    (fp = fdp->fd_ofiles[SCARG(&ouap, fd)]) != NULL &&		/*XXX*/
577 	    fp->f_type == DTYPE_VNODE &&				/*XXX*/
578 	    (vp = (struct vnode *)fp->f_data)->v_type == VCHR &&	/*XXX*/
579 	    iszerodev(vp->v_rdev)) {					/*XXX*/
580 		SCARG(&ouap, flags) |= MAP_ANON;
581 		SCARG(&ouap, fd) = -1;
582 	}
583 
584 	return (sys_mmap(p, &ouap, retval));
585 }
586 
587 #define	MC_SYNC		1
588 #define	MC_LOCK		2
589 #define	MC_UNLOCK	3
590 #define	MC_ADVISE	4
591 #define	MC_LOCKAS	5
592 #define	MC_UNLOCKAS	6
593 
594 int
595 sunos_sys_mctl(p, v, retval)
596 	register struct proc *p;
597 	void *v;
598 	register_t *retval;
599 {
600 	register struct sunos_sys_mctl_args *uap = v;
601 
602 	switch (SCARG(uap, func)) {
603 	case MC_ADVISE:		/* ignore for now */
604 		return (0);
605 	case MC_SYNC:		/* translate to msync */
606 		return (sys_msync(p, uap, retval));
607 	default:
608 		return (EINVAL);
609 	}
610 }
611 
612 int
613 sunos_sys_setsockopt(p, v, retval)
614 	struct proc *p;
615 	void *v;
616 	register_t *retval;
617 {
618 	register struct sunos_sys_setsockopt_args *uap = v;
619 	struct file *fp;
620 	struct mbuf *m = NULL;
621 	int error;
622 
623 	if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
624 		return (error);
625 #define	SO_DONTLINGER (~SO_LINGER)
626 	if (SCARG(uap, name) == SO_DONTLINGER) {
627 		m = m_get(M_WAIT, MT_SOOPTS);
628 		mtod(m, struct linger *)->l_onoff = 0;
629 		m->m_len = sizeof(struct linger);
630 		return (sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
631 		    SO_LINGER, m));
632 	}
633 	if (SCARG(uap, level) == IPPROTO_IP) {
634 #define		SUNOS_IP_MULTICAST_IF		2
635 #define		SUNOS_IP_MULTICAST_TTL		3
636 #define		SUNOS_IP_MULTICAST_LOOP		4
637 #define		SUNOS_IP_ADD_MEMBERSHIP		5
638 #define		SUNOS_IP_DROP_MEMBERSHIP	6
639 		static int ipoptxlat[] = {
640 			IP_MULTICAST_IF,
641 			IP_MULTICAST_TTL,
642 			IP_MULTICAST_LOOP,
643 			IP_ADD_MEMBERSHIP,
644 			IP_DROP_MEMBERSHIP
645 		};
646 		if (SCARG(uap, name) >= SUNOS_IP_MULTICAST_IF &&
647 		    SCARG(uap, name) <= SUNOS_IP_DROP_MEMBERSHIP) {
648 			SCARG(uap, name) =
649 			    ipoptxlat[SCARG(uap, name) - SUNOS_IP_MULTICAST_IF];
650 		}
651 	}
652 	if (SCARG(uap, valsize) > MLEN)
653 		return (EINVAL);
654 	if (SCARG(uap, val)) {
655 		m = m_get(M_WAIT, MT_SOOPTS);
656 		error = copyin(SCARG(uap, val), mtod(m, caddr_t),
657 		    (u_int)SCARG(uap, valsize));
658 		if (error) {
659 			(void) m_free(m);
660 			return (error);
661 		}
662 		m->m_len = SCARG(uap, valsize);
663 	}
664 	return (sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
665 	    SCARG(uap, name), m));
666 }
667 
668 int
669 sunos_sys_fchroot(p, v, retval)
670 	register struct proc *p;
671 	void *v;
672 	register_t *retval;
673 {
674 	register struct sunos_sys_fchroot_args *uap = v;
675 	register struct filedesc *fdp = p->p_fd;
676 	register struct vnode *vp;
677 	struct file *fp;
678 	int error;
679 
680 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
681 		return (error);
682 	if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
683 		return (error);
684 	vp = (struct vnode *)fp->f_data;
685 	VOP_LOCK(vp);
686 	if (vp->v_type != VDIR)
687 		error = ENOTDIR;
688 	else
689 		error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
690 	VOP_UNLOCK(vp);
691 	if (error)
692 		return (error);
693 	VREF(vp);
694 	if (fdp->fd_rdir != NULL)
695 		vrele(fdp->fd_rdir);
696 	fdp->fd_rdir = vp;
697 	return (0);
698 }
699 
700 /*
701  * XXX: This needs cleaning up.
702  */
703 int
704 sunos_sys_auditsys(p, v, retval)
705 	struct proc *p;
706 	void *v;
707 	register_t *retval;
708 {
709 	return 0;
710 }
711 
712 int
713 sunos_sys_uname(p, v, retval)
714 	struct proc *p;
715 	void *v;
716 	register_t *retval;
717 {
718 	struct sunos_sys_uname_args *uap = v;
719 	struct sunos_utsname sut;
720 	extern char ostype[], machine[], osrelease[];
721 
722 	bzero(&sut, sizeof(sut));
723 
724 	bcopy(ostype, sut.sysname, sizeof(sut.sysname) - 1);
725 	bcopy(hostname, sut.nodename, sizeof(sut.nodename));
726 	sut.nodename[sizeof(sut.nodename)-1] = '\0';
727 	bcopy(osrelease, sut.release, sizeof(sut.release) - 1);
728 	bcopy("1", sut.version, sizeof(sut.version) - 1);
729 	bcopy(machine, sut.machine, sizeof(sut.machine) - 1);
730 
731 	return copyout((caddr_t)&sut, (caddr_t)SCARG(uap, name),
732 	    sizeof(struct sunos_utsname));
733 }
734 
735 int
736 sunos_sys_setpgrp(p, v, retval)
737 	struct proc *p;
738 	void *v;
739 	register_t *retval;
740 {
741 	struct sunos_sys_setpgrp_args *uap = v;
742 
743 	/*
744 	 * difference to our setpgid call is to include backwards
745 	 * compatibility to pre-setsid() binaries. Do setsid()
746 	 * instead of setpgid() in those cases where the process
747 	 * tries to create a new session the old way.
748 	 */
749 	if (!SCARG(uap, pgid) &&
750 	    (!SCARG(uap, pid) || SCARG(uap, pid) == p->p_pid))
751 		return sys_setsid(p, uap, retval);
752 	else
753 		return sys_setpgid(p, uap, retval);
754 }
755 
756 int
757 sunos_sys_open(p, v, retval)
758 	struct proc *p;
759 	void *v;
760 	register_t *retval;
761 {
762 	struct sunos_sys_open_args *uap = v;
763 	int l, r;
764 	int noctty;
765 	int ret;
766 
767 	caddr_t sg = stackgap_init(p->p_emul);
768 	SUNOS_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
769 
770 	/* convert mode into NetBSD mode */
771 	l = SCARG(uap, flags);
772 	noctty = l & 0x8000;
773 	r =	(l & (0x0001 | 0x0002 | 0x0008 | 0x0040 | 0x0200 | 0x0400 | 0x0800));
774 	r |=	((l & (0x0004 | 0x1000 | 0x4000)) ? O_NONBLOCK : 0);
775 	r |=	((l & 0x0080) ? O_SHLOCK : 0);
776 	r |=	((l & 0x0100) ? O_EXLOCK : 0);
777 	r |=	((l & 0x2000) ? O_FSYNC : 0);
778 
779 	SCARG(uap, flags) = r;
780 	ret = sys_open(p, (struct sys_open_args *)uap, retval);
781 
782 	if (!ret && !noctty && SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
783 		struct filedesc *fdp = p->p_fd;
784 		struct file *fp = fdp->fd_ofiles[*retval];
785 
786 		/* ignore any error, just give it a try */
787 		if (fp->f_type == DTYPE_VNODE)
788 			(fp->f_ops->fo_ioctl)(fp, TIOCSCTTY, (caddr_t)0, p);
789 	}
790 	return ret;
791 }
792 
793 #if defined (NFSSERVER)
794 int
795 sunos_sys_nfssvc(p, v, retval)
796 	struct proc *p;
797 	void *v;
798 	register_t *retval;
799 {
800 #if 0
801 	struct sunos_sys_nfssvc_args *uap = v;
802 	struct emul *e = p->p_emul;
803 	struct sys_nfssvc_args outuap;
804 	struct sockaddr sa;
805 	int error;
806 	caddr_t sg = stackgap_init(p->p_emul);
807 
808 	bzero(&outuap, sizeof outuap);
809 	SCARG(&outuap, fd) = SCARG(uap, fd);
810 	SCARG(&outuap, mskval) = stackgap_alloc(&sg, sizeof(sa));
811 	SCARG(&outuap, msklen) = sizeof(sa);
812 	SCARG(&outuap, mtchval) = stackgap_alloc(&sg, sizeof(sa));
813 	SCARG(&outuap, mtchlen) = sizeof(sa);
814 
815 	bzero(&sa, sizeof sa);
816 	if (error = copyout(&sa, SCARG(&outuap, mskval), SCARG(&outuap, msklen)))
817 		return (error);
818 	if (error = copyout(&sa, SCARG(&outuap, mtchval), SCARG(&outuap, mtchlen)))
819 		return (error);
820 
821 	return nfssvc(p, &outuap, retval);
822 #else
823 	return (ENOSYS);
824 #endif
825 }
826 #endif /* NFSSERVER */
827 
828 int
829 sunos_sys_ustat(p, v, retval)
830 	struct proc *p;
831 	void *v;
832 	register_t *retval;
833 {
834 	struct sunos_sys_ustat_args *uap = v;
835 	struct sunos_ustat us;
836 	int error;
837 
838 	bzero(&us, sizeof us);
839 
840 	/*
841 	 * XXX: should set f_tfree and f_tinode at least
842 	 * How do we translate dev -> fstat? (and then to sunos_ustat)
843 	 */
844 
845 	if ((error = copyout(&us, SCARG(uap, buf), sizeof us)) != 0)
846 		return (error);
847 	return 0;
848 }
849 
850 int
851 sunos_sys_quotactl(p, v, retval)
852 	struct proc *p;
853 	void *v;
854 	register_t *retval;
855 {
856 
857 	return EINVAL;
858 }
859 
860 int
861 sunos_sys_vhangup(p, v, retval)
862 	struct proc *p;
863 	void *v;
864 	register_t *retval;
865 {
866 	struct session *sp = p->p_session;
867 
868 	if (sp->s_ttyvp == 0)
869 		return 0;
870 
871 	if (sp->s_ttyp && sp->s_ttyp->t_session == sp && sp->s_ttyp->t_pgrp)
872 		pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
873 
874 	(void) ttywait(sp->s_ttyp);
875 	if (sp->s_ttyvp)
876 		vgoneall(sp->s_ttyvp);
877 	if (sp->s_ttyvp)
878 		vrele(sp->s_ttyvp);
879 	sp->s_ttyvp = NULL;
880 
881 	return 0;
882 }
883 
884 static int
885 sunstatfs(sp, buf)
886 	struct statfs *sp;
887 	caddr_t buf;
888 {
889 	struct sunos_statfs ssfs;
890 
891 	bzero(&ssfs, sizeof ssfs);
892 	ssfs.f_type = 0;
893 	ssfs.f_bsize = sp->f_bsize;
894 	ssfs.f_blocks = sp->f_blocks;
895 	ssfs.f_bfree = sp->f_bfree;
896 	ssfs.f_bavail = sp->f_bavail;
897 	ssfs.f_files = sp->f_files;
898 	ssfs.f_ffree = sp->f_ffree;
899 	ssfs.f_fsid = sp->f_fsid;
900 	return copyout((caddr_t)&ssfs, buf, sizeof ssfs);
901 }
902 
903 int
904 sunos_sys_statfs(p, v, retval)
905 	struct proc *p;
906 	void *v;
907 	register_t *retval;
908 {
909 	struct sunos_sys_statfs_args *uap = v;
910 	register struct mount *mp;
911 	register struct statfs *sp;
912 	int error;
913 	struct nameidata nd;
914 
915 	caddr_t sg = stackgap_init(p->p_emul);
916 	SUNOS_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
917 
918 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
919 	if ((error = namei(&nd)) != 0)
920 		return (error);
921 	mp = nd.ni_vp->v_mount;
922 	sp = &mp->mnt_stat;
923 	vrele(nd.ni_vp);
924 	if ((error = VFS_STATFS(mp, sp, p)) != 0)
925 		return (error);
926 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
927 	return sunstatfs(sp, (caddr_t)SCARG(uap, buf));
928 }
929 
930 int
931 sunos_sys_fstatfs(p, v, retval)
932 	struct proc *p;
933 	void *v;
934 	register_t *retval;
935 {
936 	struct sunos_sys_fstatfs_args *uap = v;
937 	struct file *fp;
938 	struct mount *mp;
939 	register struct statfs *sp;
940 	int error;
941 
942 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
943 		return (error);
944 	mp = ((struct vnode *)fp->f_data)->v_mount;
945 	sp = &mp->mnt_stat;
946 	if ((error = VFS_STATFS(mp, sp, p)) != 0)
947 		return (error);
948 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
949 	return sunstatfs(sp, (caddr_t)SCARG(uap, buf));
950 }
951 
952 int
953 sunos_sys_exportfs(p, v, retval)
954 	struct proc *p;
955 	void *v;
956 	register_t *retval;
957 {
958 	/*
959 	 * XXX: should perhaps translate into a mount(2)
960 	 * with MOUNT_EXPORT?
961 	 */
962 	return 0;
963 }
964 
965 int
966 sunos_sys_mknod(p, v, retval)
967 	struct proc *p;
968 	void *v;
969 	register_t *retval;
970 {
971 	struct sunos_sys_mknod_args *uap = v;
972 
973 	caddr_t sg = stackgap_init(p->p_emul);
974 	SUNOS_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
975 
976 	if (S_ISFIFO(SCARG(uap, mode)))
977 		return sys_mkfifo(p, uap, retval);
978 
979 	return sys_mknod(p, (struct sys_mknod_args *)uap, retval);
980 }
981 
982 #define SUNOS_SC_ARG_MAX	1
983 #define SUNOS_SC_CHILD_MAX	2
984 #define SUNOS_SC_CLK_TCK	3
985 #define SUNOS_SC_NGROUPS_MAX	4
986 #define SUNOS_SC_OPEN_MAX	5
987 #define SUNOS_SC_JOB_CONTROL	6
988 #define SUNOS_SC_SAVED_IDS	7
989 #define SUNOS_SC_VERSION	8
990 
991 int
992 sunos_sys_sysconf(p, v, retval)
993 	struct proc *p;
994 	void *v;
995 	register_t *retval;
996 {
997 	struct sunos_sys_sysconf_args *uap = v;
998 	extern int maxfiles;
999 
1000 	switch(SCARG(uap, name)) {
1001 	case SUNOS_SC_ARG_MAX:
1002 		*retval = ARG_MAX;
1003 		break;
1004 	case SUNOS_SC_CHILD_MAX:
1005 		*retval = maxproc;
1006 		break;
1007 	case SUNOS_SC_CLK_TCK:
1008 		*retval = 60;		/* should this be `hz', ie. 100? */
1009 		break;
1010 	case SUNOS_SC_NGROUPS_MAX:
1011 		*retval = NGROUPS_MAX;
1012 		break;
1013 	case SUNOS_SC_OPEN_MAX:
1014 		*retval = maxfiles;
1015 		break;
1016 	case SUNOS_SC_JOB_CONTROL:
1017 		*retval = 1;
1018 		break;
1019 	case SUNOS_SC_SAVED_IDS:
1020 #ifdef _POSIX_SAVED_IDS
1021 		*retval = 1;
1022 #else
1023 		*retval = 0;
1024 #endif
1025 		break;
1026 	case SUNOS_SC_VERSION:
1027 		*retval = 198808;
1028 		break;
1029 	default:
1030 		return EINVAL;
1031 	}
1032 	return 0;
1033 }
1034 
1035 #define SUNOS_RLIMIT_NOFILE	6	/* Other RLIMIT_* are the same */
1036 #define SUNOS_RLIM_NLIMITS	7
1037 
1038 int
1039 sunos_sys_getrlimit(p, v, retval)
1040 	struct proc *p;
1041 	void *v;
1042 	register_t *retval;
1043 {
1044 	struct sunos_sys_getrlimit_args *uap = v;
1045 
1046 	if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
1047 		return EINVAL;
1048 
1049 	if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
1050 		SCARG(uap, which) = RLIMIT_NOFILE;
1051 
1052 	return compat_43_sys_getrlimit(p, uap, retval);
1053 }
1054 
1055 int
1056 sunos_sys_setrlimit(p, v, retval)
1057 	struct proc *p;
1058 	void *v;
1059 	register_t *retval;
1060 {
1061 	struct sunos_sys_getrlimit_args *uap = v;
1062 
1063 	if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
1064 		return EINVAL;
1065 
1066 	if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
1067 		SCARG(uap, which) = RLIMIT_NOFILE;
1068 
1069 	return compat_43_sys_setrlimit(p, uap, retval);
1070 }
1071 
1072 /* for the m68k machines */
1073 #ifndef PT_GETFPREGS
1074 #define PT_GETFPREGS -1
1075 #endif
1076 #ifndef PT_SETFPREGS
1077 #define PT_SETFPREGS -1
1078 #endif
1079 
1080 static int sreq2breq[] = {
1081 	PT_TRACE_ME,    PT_READ_I,      PT_READ_D,      -1,
1082 	PT_WRITE_I,     PT_WRITE_D,     -1,             PT_CONTINUE,
1083 	PT_KILL,        -1,             PT_ATTACH,      PT_DETACH,
1084 	PT_GETREGS,     PT_SETREGS,     PT_GETFPREGS,   PT_SETFPREGS
1085 };
1086 static int nreqs = sizeof(sreq2breq) / sizeof(sreq2breq[0]);
1087 
1088 int
1089 sunos_sys_ptrace(p, v, retval)
1090 	struct proc *p;
1091 	void *v;
1092 	register_t *retval;
1093 {
1094 	struct sunos_sys_ptrace_args *uap = v;
1095 	struct sys_ptrace_args pa;
1096 	int req;
1097 
1098 	req = SCARG(uap, req);
1099 
1100 	if (req < 0 || req >= nreqs)
1101 		return (EINVAL);
1102 
1103 	req = sreq2breq[req];
1104 	if (req == -1)
1105 		return (EINVAL);
1106 
1107 	SCARG(&pa, req) = req;
1108 	SCARG(&pa, pid) = (pid_t)SCARG(uap, pid);
1109 	SCARG(&pa, addr) = (caddr_t)SCARG(uap, addr);
1110 	SCARG(&pa, data) = SCARG(uap, data);
1111 
1112 	return sys_ptrace(p, &pa, retval);
1113 }
1114 
1115 /*
1116  * SunOS reboot system call (for compatibility).
1117  * Sun lets you pass in a boot string which the PROM
1118  * saves and provides to the next boot program.
1119  */
1120 
1121 #define SUNOS_RB_ASKNAME	0x001
1122 #define SUNOS_RB_SINGLE 	0x002
1123 #define SUNOS_RB_NOSYNC		0x004
1124 #define SUNOS_RB_HALT		0x008
1125 #define SUNOS_RB_DUMP		0x080
1126 #define	SUNOS_RB_STRING		0x200
1127 
1128 static struct sunos_howto_conv {
1129 	int sun_howto;
1130 	int bsd_howto;
1131 } sunos_howto_conv[] = {
1132 	{ SUNOS_RB_ASKNAME,	RB_ASKNAME },
1133 	{ SUNOS_RB_SINGLE,	RB_SINGLE },
1134 	{ SUNOS_RB_NOSYNC,	RB_NOSYNC },
1135 	{ SUNOS_RB_HALT,	RB_HALT },
1136 	{ SUNOS_RB_DUMP,	RB_DUMP },
1137 	{ SUNOS_RB_STRING,	RB_STRING },
1138 	{ 0x000,		0 },
1139 };
1140 
1141 int
1142 sunos_sys_reboot(p, v, retval)
1143 	struct proc *p;
1144 	void *v;
1145 	register_t *retval;
1146 {
1147 	struct sunos_sys_reboot_args *uap = v;
1148 	struct sys_reboot_args ua;
1149 	struct sunos_howto_conv *convp;
1150 	int error, bsd_howto, sun_howto;
1151 	char *bootstr;
1152 
1153 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1154 		return (error);
1155 
1156 	/*
1157 	 * Convert howto bits to BSD format.
1158 	 */
1159 	sun_howto = SCARG(uap, howto);
1160 	bsd_howto = 0;
1161 	convp = sunos_howto_conv;
1162 	while (convp->sun_howto) {
1163 		if (sun_howto & convp->sun_howto)
1164 			bsd_howto |= convp->bsd_howto;
1165 		convp++;
1166 	}
1167 
1168 	/*
1169 	 * Sun RB_STRING (Get user supplied bootstring.)
1170 	 * If the machine supports passing a string to the
1171 	 * next booted kernel.
1172 	 */
1173 	if (sun_howto & SUNOS_RB_STRING) {
1174 		char bs[128];
1175 
1176 		error = copyinstr(SCARG(uap, bootstr), bs, sizeof(bs), 0);
1177 
1178 		if (error)
1179 			bootstr = NULL;
1180 		else
1181 			bootstr = bs;
1182 	} else
1183 		bootstr = NULL;
1184 
1185 	SCARG(&ua, opt) = bsd_howto;
1186 	SCARG(&ua, bootstr) = bootstr;
1187 	sys_reboot(p, &ua, retval);
1188 	return(0);
1189 }
1190 
1191 /*
1192  * Generalized interface signal handler, 4.3-compatible.
1193  */
1194 /* ARGSUSED */
1195 int
1196 sunos_sys_sigvec(p, v, retval)
1197 	struct proc *p;
1198 	void *v;
1199 	register_t *retval;
1200 {
1201 	register struct sunos_sys_sigvec_args /* {
1202 		syscallarg(int) signum;
1203 		syscallarg(struct sigvec *) nsv;
1204 		syscallarg(struct sigvec *) osv;
1205 	} */ *uap = v;
1206 	struct sigvec vec;
1207 	register struct sigacts *ps = p->p_sigacts;
1208 	register struct sigvec *sv;
1209 	register int signum;
1210 	int bit, error;
1211 
1212 	signum = SCARG(uap, signum);
1213 	if (signum <= 0 || signum >= NSIG ||
1214 	    signum == SIGKILL || signum == SIGSTOP)
1215 		return (EINVAL);
1216 	sv = &vec;
1217 	if (SCARG(uap, osv)) {
1218 		*(sig_t *)&sv->sv_handler = ps->ps_sigact[signum];
1219 		sv->sv_mask = ps->ps_catchmask[signum];
1220 		bit = sigmask(signum);
1221 		sv->sv_flags = 0;
1222 		if ((ps->ps_sigonstack & bit) != 0)
1223 			sv->sv_flags |= SV_ONSTACK;
1224 		if ((ps->ps_sigintr & bit) != 0)
1225 			sv->sv_flags |= SV_INTERRUPT;
1226 		if ((ps->ps_sigreset & bit) != 0)
1227 			sv->sv_flags |= SA_RESETHAND;
1228 		sv->sv_mask &= ~bit;
1229 		error = copyout((caddr_t)sv, (caddr_t)SCARG(uap, osv),
1230 		    sizeof (vec));
1231 		if (error)
1232 			return (error);
1233 	}
1234 	if (SCARG(uap, nsv)) {
1235 		error = copyin((caddr_t)SCARG(uap, nsv), (caddr_t)sv,
1236 		    sizeof (vec));
1237 		if (error)
1238 			return (error);
1239 		/*
1240 		 * SunOS uses the mask 0x0004 as SV_RESETHAND
1241 		 * meaning: `reset to SIG_DFL on delivery'.
1242 		 * We support only the bits in: 0xF
1243 		 * (those bits are the same as ours)
1244 		 */
1245 		if (sv->sv_flags & ~0xF)
1246 			return (EINVAL);
1247 		/* SunOS binaries have a user-mode trampoline. */
1248 		sv->sv_flags |= SA_USERTRAMP;
1249 		/* Convert sigvec:SV_INTERRUPT to sigaction:SA_RESTART */
1250 		sv->sv_flags ^= SA_RESTART;	/* same bit, inverted */
1251 		setsigvec(p, signum, (struct sigaction *)sv);
1252 	}
1253 	return (0);
1254 }
1255