xref: /netbsd-src/sys/compat/sunos32/sunos32_misc.c (revision 1ffa7b76c40339c17a0fb2a09fac93f287cfc046)
1 /*	$NetBSD: sunos32_misc.c,v 1.17 2003/01/29 07:00:38 atatat Exp $	*/
2 /* from :NetBSD: sunos_misc.c,v 1.107 2000/12/01 19:25:10 jdolecek Exp	*/
3 
4 /*
5  * Copyright (c) 2001 Matthew R. Green
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 1992, 1993
34  *	The Regents of the University of California.  All rights reserved.
35  *
36  * This software was developed by the Computer Systems Engineering group
37  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
38  * contributed to Berkeley.
39  *
40  * All advertising materials mentioning features or use of this software
41  * must display the following acknowledgement:
42  *	This product includes software developed by the University of
43  *	California, Lawrence Berkeley Laboratory.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  * 1. Redistributions of source code must retain the above copyright
49  *    notice, this list of conditions and the following disclaimer.
50  * 2. Redistributions in binary form must reproduce the above copyright
51  *    notice, this list of conditions and the following disclaimer in the
52  *    documentation and/or other materials provided with the distribution.
53  * 3. All advertising materials mentioning features or use of this software
54  *    must display the following acknowledgement:
55  *	This product includes software developed by the University of
56  *	California, Berkeley and its contributors.
57  * 4. Neither the name of the University nor the names of its contributors
58  *    may be used to endorse or promote products derived from this software
59  *    without specific prior written permission.
60  *
61  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
62  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
65  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71  * SUCH DAMAGE.
72  *
73  *	@(#)sunos_misc.c	8.1 (Berkeley) 6/18/93
74  *
75  *	Header: sunos_misc.c,v 1.16 93/04/07 02:46:27 torek Exp
76  */
77 
78 /*
79  * SunOS compatibility module, 64-bit kernel version
80  *
81  * SunOS system calls that are implemented differently in BSD are
82  * handled here.
83  */
84 
85 #include <sys/cdefs.h>
86 __KERNEL_RCSID(0, "$NetBSD: sunos32_misc.c,v 1.17 2003/01/29 07:00:38 atatat Exp $");
87 
88 #define COMPAT_SUNOS 1
89 
90 #if defined(_KERNEL_OPT)
91 #include "opt_nfsserver.h"
92 #include "opt_compat_43.h"
93 #include "opt_compat_netbsd.h"
94 #include "fs_nfs.h"
95 #endif
96 
97 #include <sys/param.h>
98 #include <sys/systm.h>
99 #include <sys/namei.h>
100 #include <sys/proc.h>
101 #include <sys/dirent.h>
102 #include <sys/file.h>
103 #include <sys/stat.h>
104 #include <sys/filedesc.h>
105 #include <sys/ioctl.h>
106 #include <sys/kernel.h>
107 #include <sys/reboot.h>
108 #include <sys/malloc.h>
109 #include <sys/mbuf.h>
110 #include <sys/mman.h>
111 #include <sys/mount.h>
112 #include <sys/ptrace.h>
113 #include <sys/resource.h>
114 #include <sys/resourcevar.h>
115 #include <sys/signal.h>
116 #include <sys/signalvar.h>
117 #include <sys/socket.h>
118 #include <sys/tty.h>
119 #include <sys/vnode.h>
120 #include <sys/uio.h>
121 #include <sys/wait.h>
122 #include <sys/utsname.h>
123 #include <sys/unistd.h>
124 #include <sys/sa.h>
125 #include <sys/syscallargs.h>
126 #include <sys/conf.h>
127 #include <sys/socketvar.h>
128 #include <sys/exec.h>
129 #include <sys/swap.h>
130 
131 #include <compat/netbsd32/netbsd32.h>
132 #include <compat/netbsd32/netbsd32_syscallargs.h>
133 #include <compat/sunos32/sunos32.h>
134 #include <compat/sunos32/sunos32_dirent.h>
135 #include <compat/sunos32/sunos32_syscallargs.h>
136 #include <compat/common/compat_util.h>
137 
138 #include <netinet/in.h>
139 
140 #include <miscfs/specfs/specdev.h>
141 
142 #include <nfs/rpcv2.h>
143 #include <nfs/nfsproto.h>
144 #include <nfs/nfs.h>
145 #include <nfs/nfsmount.h>
146 
147 static void sunos32_sigvec_to_sigaction(const struct netbsd32_sigvec *, struct sigaction *);
148 static void sunos32_sigvec_from_sigaction(struct netbsd32_sigvec *, const struct sigaction *);
149 
150 static int sunstatfs __P((struct statfs *, caddr_t));
151 
152 static void
153 sunos32_sigvec_to_sigaction(sv, sa)
154 	const struct netbsd32_sigvec *sv;
155 	struct sigaction *sa;
156 {
157 /*XXX*/ extern void compat_43_sigmask_to_sigset __P((const int *, sigset_t *));
158 
159 	sa->sa_handler = (void *)(u_long)sv->sv_handler;
160 	compat_43_sigmask_to_sigset(&sv->sv_mask, &sa->sa_mask);
161 	sa->sa_flags = sv->sv_flags ^ SA_RESTART;
162 }
163 
164 static
165 void sunos32_sigvec_from_sigaction(sv, sa)
166 	struct netbsd32_sigvec *sv;
167 	const struct sigaction *sa;
168 {
169 /*XXX*/ extern void compat_43_sigset_to_sigmask __P((const sigset_t *, int *));
170 
171 	sv->sv_handler = (netbsd32_voidp)(u_long)sa->sa_handler;
172 	compat_43_sigset_to_sigmask(&sa->sa_mask, &sv->sv_mask);
173 	sv->sv_flags = sa->sa_flags ^ SA_RESTART;
174 }
175 
176 int
177 sunos32_sys_stime(l, v, retval)
178 	struct lwp *l;
179 	void *v;
180 	register_t *retval;
181 {
182 	struct sunos32_sys_stime_args /* {
183 		syscallarg(sunos32_time_tp) tp;
184 	} */ *uap = v;
185 	struct proc *p = l->l_proc;
186 	struct sys_settimeofday_args ap;
187 	caddr_t sg = stackgap_init(p, 0);
188 	struct netbsd32_timeval ntv;
189 	struct timeval tv, *sgtvp;
190 	int error;
191 
192 	error = copyin((caddr_t)(u_long)SCARG(uap, tp), &ntv.tv_sec, sizeof(ntv.tv_sec));
193 	if (error)
194 		return error;
195 	tv.tv_sec = ntv.tv_sec;
196 	tv.tv_usec = 0;
197 
198 	SCARG(&ap, tv) = sgtvp = stackgap_alloc(p, &sg, sizeof(struct timeval));
199 	SCARG(&ap, tzp) = NULL;
200 
201 	error = copyout(&tv, sgtvp, sizeof(struct timeval));
202 	if (error)
203 		return error;
204 
205 	return sys_settimeofday(l, &ap, retval);
206 }
207 
208 int
209 sunos32_sys_wait4(l, v, retval)
210 	struct lwp *l;
211 	void *v;
212 	register_t *retval;
213 {
214 	struct sunos32_sys_wait4_args /* {
215 		syscallarg(int) pid;
216 		syscallarg(netbsd32_intp) status;
217 		syscallarg(int) options;
218 		syscallarg(netbsd32_rusagep_t) rusage;
219 	} */ *uap = v;
220 
221 	if (SCARG(uap, pid) == 0)
222 		SCARG(uap, pid) = WAIT_ANY;
223 	return (netbsd32_wait4(l, uap, retval));
224 }
225 
226 int
227 sunos32_sys_creat(l, v, retval)
228 	struct lwp *l;
229 	void *v;
230 	register_t *retval;
231 {
232 	struct sunos32_sys_creat_args /* {
233 		syscallarg(const netbsd32_charp) path;
234 		syscallarg(int) mode;
235 	} */ *uap = v;
236 	struct proc *p = l->l_proc;
237 	struct sys_open_args ua;
238 	caddr_t sg = stackgap_init(p, 0);
239 
240 	SUNOS32TOP_UAP(path, const char);
241 	SCARG(&ua, flags) = O_WRONLY | O_CREAT | O_TRUNC;
242 	SUNOS32TO64_UAP(mode);
243 
244 	SUNOS32_CHECK_ALT_CREAT(p, &sg, SCARG(&ua, path));
245 
246 	return (sys_open(l, &ua, retval));
247 }
248 
249 int
250 sunos32_sys_access(l, v, retval)
251 	struct lwp *l;
252 	void *v;
253 	register_t *retval;
254 {
255 	struct sunos32_sys_access_args /* {
256 		syscallarg(const netbsd32_charp) path;
257 		syscallarg(int) flags;
258 	} */ *uap = v;
259 	struct proc *p = l->l_proc;
260 	struct sys_access_args ua;
261 	caddr_t sg = stackgap_init(p, 0);
262 
263 	SUNOS32TOP_UAP(path, const char);
264 	SUNOS32TO64_UAP(flags);
265 	SUNOS32_CHECK_ALT_EXIST(p, &sg, SCARG(&ua, path));
266 
267 	return (sys_access(l, &ua, retval));
268 }
269 
270 static __inline void sunos32_from___stat13 __P((struct stat *, struct netbsd32_stat43 *));
271 
272 static __inline void
273 sunos32_from___stat13(sbp, sb32p)
274 	struct stat *sbp;
275 	struct netbsd32_stat43 *sb32p;
276 {
277 	sb32p->st_dev = sbp->st_dev;
278 	sb32p->st_ino = sbp->st_ino;
279 	sb32p->st_mode = sbp->st_mode;
280 	sb32p->st_nlink = sbp->st_nlink;
281 	sb32p->st_uid = sbp->st_uid;
282 	sb32p->st_gid = sbp->st_gid;
283 	sb32p->st_rdev = sbp->st_rdev;
284 	if (sbp->st_size < (quad_t)1 << 32)
285 		sb32p->st_size = sbp->st_size;
286 	else
287 		sb32p->st_size = -2;
288 	sb32p->st_atimespec.tv_sec = (netbsd32_time_t)sbp->st_atimespec.tv_sec;
289 	sb32p->st_atimespec.tv_nsec = (netbsd32_long)sbp->st_atimespec.tv_nsec;
290 	sb32p->st_mtimespec.tv_sec = (netbsd32_time_t)sbp->st_mtimespec.tv_sec;
291 	sb32p->st_mtimespec.tv_nsec = (netbsd32_long)sbp->st_mtimespec.tv_nsec;
292 	sb32p->st_ctimespec.tv_sec = (netbsd32_time_t)sbp->st_ctimespec.tv_sec;
293 	sb32p->st_ctimespec.tv_nsec = (netbsd32_long)sbp->st_ctimespec.tv_nsec;
294 	sb32p->st_blksize = sbp->st_blksize;
295 	sb32p->st_blocks = sbp->st_blocks;
296 	sb32p->st_flags = sbp->st_flags;
297 	sb32p->st_gen = sbp->st_gen;
298 }
299 
300 
301 int
302 sunos32_sys_stat(l, v, retval)
303 	struct lwp *l;
304 	void *v;
305 	register_t *retval;
306 {
307 	struct sunos32_sys_stat_args /* {
308 		syscallarg(const netbsd32_charp) path;
309 		syscallarg(netbsd32_stat43p_t) ub;
310 	} */ *uap = v;
311 	struct proc *p = l->l_proc;
312 	struct netbsd32_stat43 sb32;
313 	struct stat sb;
314 	struct nameidata nd;
315 	caddr_t sg;
316 	const char *path;
317 	int error;
318 
319 	path = (char *)(u_long)SCARG(uap, path);
320 	sg = stackgap_init(p, 0);
321 	SUNOS32_CHECK_ALT_EXIST(p, &sg, path);
322 
323 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, path, p);
324 	if ((error = namei(&nd)) != 0)
325 		return (error);
326 	error = vn_stat(nd.ni_vp, &sb, p);
327 	vput(nd.ni_vp);
328 	if (error)
329 		return (error);
330 	sunos32_from___stat13(&sb, &sb32);
331 	error = copyout((caddr_t)&sb32, (caddr_t)(u_long)SCARG(uap, ub), sizeof (sb32));
332 	return (error);
333 }
334 
335 int
336 sunos32_sys_lstat(l, v, retval)
337 	struct lwp *l;
338 	void *v;
339 	register_t *retval;
340 {
341 	struct sunos32_sys_lstat_args /* {
342 		syscallarg(const netbsd32_charp) path;
343 		syscallarg(netbsd32_stat43p_t) ub;
344 	} */ *uap = v;
345 	struct vnode *vp, *dvp;
346 	struct proc *p = l->l_proc;
347 	struct stat sb, sb1;
348 	struct netbsd32_stat43 sb32;
349 	int error;
350 	struct nameidata nd;
351 	int ndflags;
352 	const char *path;
353 	caddr_t sg;
354 
355 	path = (char *)(u_long)SCARG(uap, path);
356 	sg = stackgap_init(p, 0);
357 	SUNOS32_CHECK_ALT_EXIST(p, &sg, path);
358 
359 	ndflags = NOFOLLOW | LOCKLEAF | LOCKPARENT;
360 again:
361 	NDINIT(&nd, LOOKUP, ndflags, UIO_USERSPACE, path, p);
362 	if ((error = namei(&nd))) {
363 		if (error == EISDIR && (ndflags & LOCKPARENT) != 0) {
364 			/*
365 			 * Should only happen on '/'. Retry without LOCKPARENT;
366 			 * this is safe since the vnode won't be a VLNK.
367 			 */
368 			ndflags &= ~LOCKPARENT;
369 			goto again;
370 		}
371 		return (error);
372 	}
373 	/*
374 	 * For symbolic links, always return the attributes of its
375 	 * containing directory, except for mode, size, and links.
376 	 */
377 	vp = nd.ni_vp;
378 	dvp = nd.ni_dvp;
379 	if (vp->v_type != VLNK) {
380 		if ((ndflags & LOCKPARENT) != 0) {
381 			if (dvp == vp)
382 				vrele(dvp);
383 			else
384 				vput(dvp);
385 		}
386 		error = vn_stat(vp, &sb, p);
387 		vput(vp);
388 		if (error)
389 			return (error);
390 	} else {
391 		error = vn_stat(dvp, &sb, p);
392 		vput(dvp);
393 		if (error) {
394 			vput(vp);
395 			return (error);
396 		}
397 		error = vn_stat(vp, &sb1, p);
398 		vput(vp);
399 		if (error)
400 			return (error);
401 		sb.st_mode &= ~S_IFDIR;
402 		sb.st_mode |= S_IFLNK;
403 		sb.st_nlink = sb1.st_nlink;
404 		sb.st_size = sb1.st_size;
405 		sb.st_blocks = sb1.st_blocks;
406 	}
407 	sunos32_from___stat13(&sb, &sb32);
408 	error = copyout((caddr_t)&sb32, (caddr_t)(u_long)SCARG(uap, ub), sizeof (sb32));
409 	return (error);
410 }
411 
412 int
413 sunos32_sys_execv(l, v, retval)
414 	struct lwp *l;
415 	void *v;
416 	register_t *retval;
417 {
418 	struct sunos32_sys_execv_args /* {
419 		syscallarg(const char *) path;
420 		syscallarg(char **) argv;
421 	} */ *uap = v;
422 	struct proc *p = l->l_proc;
423 	struct sys_execve_args ua;
424 	caddr_t sg;
425 
426 	SUNOS32TOP_UAP(path, const char);
427 	SUNOS32TOP_UAP(argp, char *);
428 	SCARG(&ua, envp) = NULL;
429 	sg = stackgap_init(p, 0);
430 	SUNOS32_CHECK_ALT_EXIST(p, &sg, SCARG(&ua, path));
431 
432 	return netbsd32_execve2(l, &ua, retval);
433 }
434 
435 int
436 sunos32_sys_execve(l, v, retval)
437 	struct lwp *l;
438 	void *v;
439 	register_t *retval;
440 {
441 	struct sunos32_sys_execve_args /* {
442 		syscallarg(const char *) path;
443 		syscallarg(char **) argv;
444 		syscallarg(char **) envp;
445 	} */ *uap = v;
446 	struct proc *p = l->l_proc;
447 	struct sys_execve_args ua;
448 	caddr_t sg;
449 
450 	SUNOS32TOP_UAP(path, const char);
451 	SUNOS32TOP_UAP(argp, char *);
452 	SUNOS32TOP_UAP(envp, char *);
453 	sg = stackgap_init(p, 0);
454 	SUNOS32_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
455 
456 	return netbsd32_execve2(l, &ua, retval);
457 }
458 
459 int
460 sunos32_sys_omsync(l, v, retval)
461 	struct lwp *l;
462 	void *v;
463 	register_t *retval;
464 {
465 	struct sunos32_sys_omsync_args /* {
466 		syscallarg(netbsd32_caddr_t) addr;
467 		syscallarg(netbsd32_size_t) len;
468 		syscallarg(int) flags;
469 	} */ *uap = v;
470 	struct netbsd32___msync13_args ouap;
471 
472 	SCARG(&ouap, addr) = SCARG(uap, addr);
473 	SCARG(&ouap, len) = SCARG(uap, len);
474 	SCARG(&ouap, flags) = SCARG(uap, flags);
475 
476 	return (netbsd32___msync13(l, &ouap, retval));
477 }
478 
479 int
480 sunos32_sys_unmount(l, v, retval)
481 	struct lwp *l;
482 	void *v;
483 	register_t *retval;
484 {
485 	struct sunos32_sys_unmount_args /* {
486 		syscallarg(netbsd32_charp) path;
487 	} */ *uap = v;
488 	struct sys_unmount_args ua;
489 
490 	SUNOS32TOP_UAP(path, const char);
491 	SCARG(&ua, flags) = 0;
492 
493 	return (sys_unmount(l, &ua, retval));
494 }
495 
496 /*
497  * Conversion table for SunOS NFS mount flags.
498  */
499 static struct {
500 	int	sun_flg;
501 	int	bsd_flg;
502 } sunnfs_flgtab[] = {
503 	{ SUNNFS_SOFT,		NFSMNT_SOFT },
504 	{ SUNNFS_WSIZE,		NFSMNT_WSIZE },
505 	{ SUNNFS_RSIZE,		NFSMNT_RSIZE },
506 	{ SUNNFS_TIMEO,		NFSMNT_TIMEO },
507 	{ SUNNFS_RETRANS,	NFSMNT_RETRANS },
508 	{ SUNNFS_HOSTNAME,	0 },			/* Ignored */
509 	{ SUNNFS_INT,		NFSMNT_INT },
510 	{ SUNNFS_NOAC,		0 },			/* Ignored */
511 	{ SUNNFS_ACREGMIN,	0 },			/* Ignored */
512 	{ SUNNFS_ACREGMAX,	0 },			/* Ignored */
513 	{ SUNNFS_ACDIRMIN,	0 },			/* Ignored */
514 	{ SUNNFS_ACDIRMAX,	0 },			/* Ignored */
515 	{ SUNNFS_SECURE,	0 },			/* Ignored */
516 	{ SUNNFS_NOCTO,		0 },			/* Ignored */
517 	{ SUNNFS_POSIX,		0 }			/* Ignored */
518 };
519 
520 int
521 sunos32_sys_mount(l, v, retval)
522 	struct lwp *l;
523 	void *v;
524 	register_t *retval;
525 {
526 	struct sunos32_sys_mount_args /* {
527 		syscallarg(netbsd32_charp) type;
528 		syscallarg(netbsd32_charp) path;
529 		syscallarg(int) flags;
530 		syscallarg(netbsd32_caddr_t) data;
531 	} */ *uap = v;
532 	struct proc *p = l->l_proc;
533 	struct sys_mount_args ua;
534 	int oflags = SCARG(uap, flags), nflags, error;
535 	char fsname[MFSNAMELEN];
536 	caddr_t sg = stackgap_init(p, 0);
537 
538 	if (oflags & (SUNM_NOSUB | SUNM_SYS5))
539 		return (EINVAL);
540 	if ((oflags & SUNM_NEWTYPE) == 0)
541 		return (EINVAL);
542 	nflags = 0;
543 	if (oflags & SUNM_RDONLY)
544 		nflags |= MNT_RDONLY;
545 	if (oflags & SUNM_NOSUID)
546 		nflags |= MNT_NOSUID;
547 	if (oflags & SUNM_REMOUNT)
548 		nflags |= MNT_UPDATE;
549 	SCARG(uap, flags) = nflags;
550 
551 	error = copyinstr((caddr_t)(u_long)SCARG(uap, type), fsname,
552 	    sizeof fsname, (size_t *)0);
553 	if (error)
554 		return (error);
555 
556 	if (strncmp(fsname, "4.2", sizeof fsname) == 0) {
557 		SCARG(uap, type) = (netbsd32_charp)(u_long)stackgap_alloc(p, &sg, sizeof("ffs"));
558 		error = copyout("ffs", (caddr_t)(u_long)SCARG(uap, type), sizeof("ffs"));
559 		if (error)
560 			return (error);
561 	} else if (strncmp(fsname, "nfs", sizeof fsname) == 0) {
562 		struct sunos_nfs_args sna;
563 		struct sockaddr_in sain;
564 		struct nfs_args na;	/* XXX */
565 		struct sockaddr sa;
566 		int n;
567 
568 		error = copyin((caddr_t)(u_long)SCARG(uap, data), &sna, sizeof sna);
569 		if (error)
570 			return (error);
571 		error = copyin(sna.addr, &sain, sizeof sain);
572 		if (error)
573 			return (error);
574 		memcpy(&sa, &sain, sizeof sa);
575 		sa.sa_len = sizeof(sain);
576 		SCARG(uap, data) = (netbsd32_charp)(u_long)stackgap_alloc(p, &sg, sizeof(na));
577 		na.version = NFS_ARGSVERSION;
578 		na.addr = stackgap_alloc(p, &sg, sizeof(struct sockaddr));
579 		na.addrlen = sizeof(struct sockaddr);
580 		na.sotype = SOCK_DGRAM;
581 		na.proto = IPPROTO_UDP;
582 		na.fh = (void *)sna.fh;
583 		na.fhsize = NFSX_V2FH;
584 		na.flags = 0;
585 		n = sizeof(sunnfs_flgtab) / sizeof(sunnfs_flgtab[0]);
586 		while (--n >= 0)
587 			if (sna.flags & sunnfs_flgtab[n].sun_flg)
588 				na.flags |= sunnfs_flgtab[n].bsd_flg;
589 		na.wsize = sna.wsize;
590 		na.rsize = sna.rsize;
591 		if (na.flags & NFSMNT_RSIZE) {
592 			na.flags |= NFSMNT_READDIRSIZE;
593 			na.readdirsize = na.rsize;
594 		}
595 		na.timeo = sna.timeo;
596 		na.retrans = sna.retrans;
597 		na.hostname = (char *)(u_long)sna.hostname;
598 
599 		error = copyout(&sa, na.addr, sizeof sa);
600 		if (error)
601 			return (error);
602 		error = copyout(&na, (caddr_t)(u_long)SCARG(uap, data), sizeof na);
603 		if (error)
604 			return (error);
605 	}
606 	SUNOS32TOP_UAP(type, const char);
607 	SUNOS32TOP_UAP(path, const char);
608 	SUNOS32TO64_UAP(flags);
609 	SUNOS32TOP_UAP(data, void);
610 	return (sys_mount(l, &ua, retval));
611 }
612 
613 #if defined(NFS)
614 int
615 async_daemon(l, v, retval)
616 	struct lwp *l;
617 	void *v;
618 	register_t *retval;
619 {
620 	struct netbsd32_nfssvc_args ouap;
621 
622 	SCARG(&ouap, flag) = NFSSVC_BIOD;
623 	SCARG(&ouap, argp) = NULL;
624 
625 	return (netbsd32_nfssvc(l, &ouap, retval));
626 }
627 #endif /* NFS */
628 
629 void	native_to_sunos_sigset __P((const sigset_t *, int *));
630 void	sunos_to_native_sigset __P((const int, sigset_t *));
631 
632 __inline__ void
633 native_to_sunos_sigset(ss, mask)
634 	const sigset_t *ss;
635 	int *mask;
636 {
637 	*mask = ss->__bits[0];
638 }
639 
640 __inline__ void
641 sunos_to_native_sigset(mask, ss)
642 	const int mask;
643 	sigset_t *ss;
644 {
645 
646 	ss->__bits[0] = mask;
647 	ss->__bits[1] = 0;
648 	ss->__bits[2] = 0;
649 	ss->__bits[3] = 0;
650 }
651 
652 int
653 sunos32_sys_sigpending(l, v, retval)
654 	struct lwp *l;
655 	void *v;
656 	register_t *retval;
657 {
658 	struct sunos32_sys_sigpending_args /* {
659 		syscallarg(netbsd32_intp) mask;
660 	} */ *uap = v;
661 	struct proc *p = l->l_proc;
662 	sigset_t ss;
663 	int mask;
664 
665 	sigpending1(p, &ss);
666 	native_to_sunos_sigset(&ss, &mask);
667 
668 	return (copyout((caddr_t)(u_long)&mask, (caddr_t)(u_long)SCARG(uap, mask), sizeof(int)));
669 }
670 
671 int
672 sunos32_sys_sigsuspend(l, v, retval)
673 	struct lwp *l;
674 	void *v;
675 	register_t *retval;
676 {
677 	struct sunos32_sys_sigsuspend_args /* {
678 		syscallarg(int) mask;
679 	} */ *uap = v;
680 	int mask;
681 	struct proc *p = l->l_proc;
682 	sigset_t ss;
683 
684 	mask = SCARG(uap, mask);
685 	sunos_to_native_sigset(mask, &ss);
686 	return (sigsuspend1(p, &ss));
687 }
688 
689 /*
690  * Read Sun-style directory entries.  We suck them into kernel space so
691  * that they can be massaged before being copied out to user code.  Like
692  * SunOS, we squish out `empty' entries.
693  *
694  * This is quite ugly, but what do you expect from compatibility code?
695  */
696 int
697 sunos32_sys_getdents(l, v, retval)
698 	struct lwp *l;
699 	void *v;
700 	register_t *retval;
701 {
702 	struct sunos32_sys_getdents_args /* {
703 		syscallarg(int) fd;
704 		syscallarg(netbsd32_charp) buf;
705 		syscallarg(int) nbytes;
706 	} */ *uap = v;
707 	struct proc *p = l->l_proc;
708 	struct dirent *bdp;
709 	struct vnode *vp;
710 	caddr_t inp, buf;	/* BSD-format */
711 	int len, reclen;	/* BSD-format */
712 	caddr_t outp;		/* Sun-format */
713 	int resid, sunos_reclen;/* Sun-format */
714 	struct file *fp;
715 	struct uio auio;
716 	struct iovec aiov;
717 	struct sunos32_dirent idb;
718 	off_t off;			/* true file offset */
719 	int buflen, error, eofflag;
720 	off_t *cookiebuf, *cookie;
721 	int ncookies;
722 
723 	/* getvnode() will use the descriptor for us */
724 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
725 		return (error);
726 
727 	if ((fp->f_flag & FREAD) == 0) {
728 		error = EBADF;
729 		goto out1;
730 	}
731 
732 	vp = (struct vnode *)fp->f_data;
733 	if (vp->v_type != VDIR) {
734 		error = EINVAL;
735 		goto out1;
736 	}
737 
738 	buflen = min(MAXBSIZE, SCARG(uap, nbytes));
739 	buf = malloc(buflen, M_TEMP, M_WAITOK);
740 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
741 	off = fp->f_offset;
742 again:
743 	aiov.iov_base = buf;
744 	aiov.iov_len = buflen;
745 	auio.uio_iov = &aiov;
746 	auio.uio_iovcnt = 1;
747 	auio.uio_rw = UIO_READ;
748 	auio.uio_segflg = UIO_SYSSPACE;
749 	auio.uio_procp = p;
750 	auio.uio_resid = buflen;
751 	auio.uio_offset = off;
752 	/*
753 	 * First we read into the malloc'ed buffer, then
754 	 * we massage it into user space, one record at a time.
755 	 */
756 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
757 	    &ncookies);
758 	if (error)
759 		goto out;
760 
761 	inp = buf;
762 	outp = (caddr_t)(u_long)SCARG(uap, buf);
763 	resid = SCARG(uap, nbytes);
764 	if ((len = buflen - auio.uio_resid) == 0)
765 		goto eof;
766 
767 	for (cookie = cookiebuf; len > 0; len -= reclen) {
768 		bdp = (struct dirent *)inp;
769 		reclen = bdp->d_reclen;
770 		if (reclen & 3)
771 			panic("sunos_getdents");
772 		if ((*cookie >> 32) != 0) {
773 			compat_offseterr(vp, "sunos_getdents");
774 			error = EINVAL;
775 			goto out;
776 		}
777 		if (bdp->d_fileno == 0) {
778 			inp += reclen;	/* it is a hole; squish it out */
779 			off = *cookie++;
780 			continue;
781 		}
782 		sunos_reclen = SUNOS32_RECLEN(&idb, bdp->d_namlen);
783 		if (reclen > len || resid < sunos_reclen) {
784 			/* entry too big for buffer, so just stop */
785 			outp++;
786 			break;
787 		}
788 		off = *cookie++;	/* each entry points to next */
789 		/*
790 		 * Massage in place to make a Sun-shaped dirent (otherwise
791 		 * we have to worry about touching user memory outside of
792 		 * the copyout() call).
793 		 */
794 		idb.d_fileno = bdp->d_fileno;
795 		idb.d_off = off;
796 		idb.d_reclen = sunos_reclen;
797 		idb.d_namlen = bdp->d_namlen;
798 		strcpy(idb.d_name, bdp->d_name);
799 		if ((error = copyout((caddr_t)&idb, outp, sunos_reclen)) != 0)
800 			goto out;
801 		/* advance past this real entry */
802 		inp += reclen;
803 		/* advance output past Sun-shaped entry */
804 		outp += sunos_reclen;
805 		resid -= sunos_reclen;
806 	}
807 
808 	/* if we squished out the whole block, try again */
809 	if (outp == (caddr_t)(u_long)SCARG(uap, buf))
810 		goto again;
811 	fp->f_offset = off;		/* update the vnode offset */
812 
813 eof:
814 	*retval = SCARG(uap, nbytes) - resid;
815 out:
816 	VOP_UNLOCK(vp, 0);
817 	free(cookiebuf, M_TEMP);
818 	free(buf, M_TEMP);
819  out1:
820 	FILE_UNUSE(fp, p);
821 	return (error);
822 }
823 
824 #define	SUNOS32__MAP_NEW	0x80000000	/* if not, old mmap & cannot handle */
825 
826 int
827 sunos32_sys_mmap(l, v, retval)
828 	struct lwp *l;
829 	void *v;
830 	register_t *retval;
831 {
832 	struct sunos32_sys_mmap_args /* {
833 		syscallarg(netbsd32_voidp) addr;
834 		syscallarg(netbsd32_size_t) len;
835 		syscallarg(int) prot;
836 		syscallarg(int) flags;
837 		syscallarg(int) fd;
838 		syscallarg(netbsd32_long) pos;
839 	} */ *uap = v;
840 	struct sys_mmap_args ua;
841 	void *rt;
842 	int error;
843 
844 	/*
845 	 * Verify the arguments.
846 	 */
847 	if (SCARG(uap, prot) & ~(PROT_READ|PROT_WRITE|PROT_EXEC))
848 		return (EINVAL);			/* XXX still needed? */
849 
850 	if ((SCARG(uap, flags) & SUNOS32__MAP_NEW) == 0)
851 		return (EINVAL);
852 
853 	SUNOS32TOP_UAP(addr, void);
854 	SUNOS32TOX_UAP(len, size_t);
855 	SUNOS32TO64_UAP(prot);
856 	SCARG(&ua, flags) = SCARG(uap, flags) & ~SUNOS32__MAP_NEW;
857 	SUNOS32TO64_UAP(fd);
858 	SCARG(&ua, pad) = 0;
859 	SUNOS32TOX_UAP(pos, off_t);
860 
861 	error = sys_mmap(l, &ua, (register_t *)&rt);
862 	if ((long)rt > (long)UINT_MAX)
863 		printf("sunos32_mmap: retval out of range: %p", rt);
864 	*retval = (netbsd32_voidp)(u_long)rt;
865 	return (error);
866 }
867 
868 #define	MC_SYNC		1
869 #define	MC_LOCK		2
870 #define	MC_UNLOCK	3
871 #define	MC_ADVISE	4
872 #define	MC_LOCKAS	5
873 #define	MC_UNLOCKAS	6
874 
875 int
876 sunos32_sys_mctl(l, v, retval)
877 	struct lwp *l;
878 	void *v;
879 	register_t *retval;
880 {
881 	struct sunos32_sys_mctl_args /* {
882 		syscallarg(netbsd32_voidp) addr;
883 		syscallarg(int) len;
884 		syscallarg(int) func;
885 		syscallarg(netbsd32_voidp) arg;
886 	} */ *uap = v;
887 
888 	switch (SCARG(uap, func)) {
889 	case MC_ADVISE:		/* ignore for now */
890 		return (0);
891 	case MC_SYNC:		/* translate to msync */
892 		return (netbsd32___msync13(l, uap, retval));
893 	default:
894 		return (EINVAL);
895 	}
896 }
897 
898 int
899 sunos32_sys_setsockopt(l, v, retval)
900 	struct lwp *l;
901 	void *v;
902 	register_t *retval;
903 {
904 	struct sunos32_sys_setsockopt_args /* {
905 		syscallarg(int) s;
906 		syscallarg(int) level;
907 		syscallarg(int) name;
908 		syscallarg(netbsd32_caddr_t) val;
909 		syscallarg(int) valsize;
910 	} */ *uap = v;
911 	struct proc *p = l->l_proc;
912 	struct file *fp;
913 	struct mbuf *m = NULL;
914 	int error;
915 
916 	/* getsock() will use the descriptor for us */
917 	if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
918 		return (error);
919 #define	SO_DONTLINGER (~SO_LINGER)
920 	if (SCARG(uap, name) == SO_DONTLINGER) {
921 		m = m_get(M_WAIT, MT_SOOPTS);
922 		mtod(m, struct linger *)->l_onoff = 0;
923 		m->m_len = sizeof(struct linger);
924 		error = sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
925 		    SO_LINGER, m);
926 		goto out;
927 	}
928 	if (SCARG(uap, level) == IPPROTO_IP) {
929 #define		SUNOS_IP_MULTICAST_IF		2
930 #define		SUNOS_IP_MULTICAST_TTL		3
931 #define		SUNOS_IP_MULTICAST_LOOP		4
932 #define		SUNOS_IP_ADD_MEMBERSHIP		5
933 #define		SUNOS_IP_DROP_MEMBERSHIP	6
934 		static int ipoptxlat[] = {
935 			IP_MULTICAST_IF,
936 			IP_MULTICAST_TTL,
937 			IP_MULTICAST_LOOP,
938 			IP_ADD_MEMBERSHIP,
939 			IP_DROP_MEMBERSHIP
940 		};
941 		if (SCARG(uap, name) >= SUNOS_IP_MULTICAST_IF &&
942 		    SCARG(uap, name) <= SUNOS_IP_DROP_MEMBERSHIP) {
943 			SCARG(uap, name) =
944 			    ipoptxlat[SCARG(uap, name) - SUNOS_IP_MULTICAST_IF];
945 		}
946 	}
947 	if (SCARG(uap, valsize) > MLEN) {
948 		error = EINVAL;
949 		goto out;
950 	}
951 	if (SCARG(uap, val)) {
952 		m = m_get(M_WAIT, MT_SOOPTS);
953 		error = copyin((caddr_t)(u_long)SCARG(uap, val), mtod(m, caddr_t),
954 		    (u_int)SCARG(uap, valsize));
955 		if (error) {
956 			(void) m_free(m);
957 			goto out;
958 		}
959 		m->m_len = SCARG(uap, valsize);
960 	}
961 	error = sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
962 	    SCARG(uap, name), m);
963  out:
964 	FILE_UNUSE(fp, p);
965 	return (error);
966 }
967 
968 static __inline__ int sunos32_sys_socket_common(struct lwp *, register_t *,
969 					      int type);
970 static __inline__ int
971 sunos32_sys_socket_common(l, retval, type)
972 	struct lwp *l;
973 	register_t *retval;
974 	int type;
975 {
976 	struct socket *so;
977 	struct proc *p = l->l_proc;
978 	struct file *fp;
979 	int error, fd;
980 
981 	/* getsock() will use the descriptor for us */
982 	fd = (int)*retval;
983 	if ((error = getsock(p->p_fd, fd, &fp)) == 0) {
984 		so = (struct socket *)fp->f_data;
985 		if (type == SOCK_DGRAM)
986 			so->so_options |= SO_BROADCAST;
987 	}
988 	FILE_UNUSE(fp, p);
989 	return (error);
990 }
991 
992 int
993 sunos32_sys_socket(l, v, retval)
994 	struct lwp *l;
995 	void *v;
996 	register_t *retval;
997 {
998 	struct sunos32_sys_socket_args /* {
999 		syscallarg(int) domain;
1000 		syscallarg(int) type;
1001 		syscallarg(int) protocol;
1002 	} */ *uap = v;
1003 	int error;
1004 
1005 	error = netbsd32_socket(l, v, retval);
1006 	if (error)
1007 		return (error);
1008 	return sunos32_sys_socket_common(l, retval, SCARG(uap, type));
1009 }
1010 
1011 int
1012 sunos32_sys_socketpair(l, v, retval)
1013 	struct lwp *l;
1014 	void *v;
1015 	register_t *retval;
1016 {
1017 	struct sunos32_sys_socketpair_args /* {
1018 		syscallarg(int) domain;
1019 		syscallarg(int) type;
1020 		syscallarg(int) protocol;
1021 		syscallarg(int *) rsv;
1022 	} */ *uap = v;
1023 	int error;
1024 
1025 	error = netbsd32_socketpair(l, v, retval);
1026 	if (error)
1027 		return (error);
1028 	return sunos32_sys_socket_common(l, retval, SCARG(uap, type));
1029 }
1030 
1031 
1032 /*
1033  * XXX: This needs cleaning up.
1034  */
1035 int
1036 sunos32_sys_auditsys(l, v, retval)
1037 	struct lwp *l;
1038 	void *v;
1039 	register_t *retval;
1040 {
1041 	return 0;
1042 }
1043 
1044 int
1045 sunos32_sys_uname(l, v, retval)
1046 	struct lwp *l;
1047 	void *v;
1048 	register_t *retval;
1049 {
1050 	struct sunos32_sys_uname_args /* {
1051 		syscallarg(sunos32_utsnamep_t) name;
1052 	} */ *uap = v;
1053 	struct sunos_utsname sut;
1054 
1055 	memset(&sut, 0, sizeof(sut));
1056 
1057 	memcpy(sut.sysname, ostype, sizeof(sut.sysname) - 1);
1058 	memcpy(sut.nodename, hostname, sizeof(sut.nodename));
1059 	sut.nodename[sizeof(sut.nodename)-1] = '\0';
1060 	memcpy(sut.release, osrelease, sizeof(sut.release) - 1);
1061 	memcpy(sut.version, "1", sizeof(sut.version) - 1);
1062 	memcpy(sut.machine, machine, sizeof(sut.machine) - 1);
1063 
1064 	return copyout((caddr_t)&sut, (caddr_t)(u_long)SCARG(uap, name),
1065 	    sizeof(struct sunos_utsname));
1066 }
1067 
1068 int
1069 sunos32_sys_setpgrp(l, v, retval)
1070 	struct lwp *l;
1071 	void *v;
1072 	register_t *retval;
1073 {
1074 	struct sunos32_sys_setpgrp_args /* {
1075 		syscallarg(int) pid;
1076 		syscallarg(int) pgid;
1077 	} */ *uap = v;
1078 	struct proc *p = l->l_proc;
1079 
1080 	/*
1081 	 * difference to our setpgid call is to include backwards
1082 	 * compatibility to pre-setsid() binaries. Do setsid()
1083 	 * instead of setpgid() in those cases where the process
1084 	 * tries to create a new session the old way.
1085 	 */
1086 	if (!SCARG(uap, pgid) &&
1087 	    (!SCARG(uap, pid) || SCARG(uap, pid) == p->p_pid))
1088 		return sys_setsid(l, uap, retval);
1089 	else
1090 		return netbsd32_setpgid(l, uap, retval);
1091 }
1092 
1093 int
1094 sunos32_sys_open(l, v, retval)
1095 	struct lwp *l;
1096 	void *v;
1097 	register_t *retval;
1098 {
1099 	struct sunos32_sys_open_args /* {
1100 		syscallarg(const netbsd32_charp) path;
1101 		syscallarg(int) flags;
1102 		syscallarg(int) mode;
1103 	} */ *uap = v;
1104 	struct proc *p = l->l_proc;
1105 	struct sys_open_args ua;
1106 	int lf, r;
1107 	int noctty;
1108 	int ret;
1109 	caddr_t sg = stackgap_init(p, 0);
1110 
1111 	/* convert mode into NetBSD mode */
1112 	lf = SCARG(uap, flags);
1113 	noctty = lf & 0x8000;
1114 	r =	(lf & (0x0001 | 0x0002 | 0x0008 | 0x0040 | 0x0200 | 0x0400 | 0x0800));
1115 	r |=	((lf & (0x0004 | 0x1000 | 0x4000)) ? O_NONBLOCK : 0);
1116 	r |=	((lf & 0x0080) ? O_SHLOCK : 0);
1117 	r |=	((lf & 0x0100) ? O_EXLOCK : 0);
1118 	r |=	((lf & 0x2000) ? O_FSYNC : 0);
1119 
1120 	SUNOS32TOP_UAP(path, const char);
1121 	SCARG(&ua, flags) = r;
1122 	SUNOS32TO64_UAP(mode);
1123 
1124 	if (r & O_CREAT)
1125 		SUNOS32_CHECK_ALT_CREAT(p, &sg, SCARG(&ua, path));
1126 	else
1127 		SUNOS32_CHECK_ALT_EXIST(p, &sg, SCARG(&ua, path));
1128 
1129 	ret = sys_open(l, &ua, retval);
1130 
1131 	if (!ret && !noctty && SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
1132 		struct filedesc *fdp = p->p_fd;
1133 		struct file *fp;
1134 
1135 		fp = fd_getfile(fdp, *retval);
1136 
1137 		/* ignore any error, just give it a try */
1138 		if (fp != NULL && fp->f_type == DTYPE_VNODE)
1139 			(fp->f_ops->fo_ioctl)(fp, TIOCSCTTY, (caddr_t)0, p);
1140 	}
1141 	return ret;
1142 }
1143 
1144 #if defined (NFSSERVER)
1145 int
1146 sunos32_sys_nfssvc(l, v, retval)
1147 	struct lwp *l;
1148 	void *v;
1149 	register_t *retval;
1150 {
1151 #if 0
1152 	struct sunos32_sys_nfssvc_args *uap = v;
1153 	struct emul *e = p->p_emul;
1154 	struct sys_nfssvc_args outuap;
1155 	struct sockaddr sa;
1156 	int error;
1157 	caddr_t sg = stackgap_init(p, 0);
1158 
1159 	memset(&outuap, 0, sizeof outuap);
1160 	SCARG(&outuap, fd) = SCARG(uap, fd);
1161 	SCARG(&outuap, mskval) = stackgap_alloc(p, &sg, sizeof(sa));
1162 	SCARG(&outuap, msklen) = sizeof(sa);
1163 	SCARG(&outuap, mtchval) = stackgap_alloc(p, &sg, sizeof(sa));
1164 	SCARG(&outuap, mtchlen) = sizeof(sa);
1165 
1166 	memset(&sa, 0, sizeof sa);
1167 	if (error = copyout(&sa, SCARG(&outuap, mskval), SCARG(&outuap, msklen)))
1168 		return (error);
1169 	if (error = copyout(&sa, SCARG(&outuap, mtchval), SCARG(&outuap, mtchlen)))
1170 		return (error);
1171 
1172 	return nfssvc(l, &outuap, retval);
1173 #else
1174 	return (ENOSYS);
1175 #endif
1176 }
1177 #endif /* NFSSERVER */
1178 
1179 int
1180 sunos32_sys_ustat(l, v, retval)
1181 	struct lwp *l;
1182 	void *v;
1183 	register_t *retval;
1184 {
1185 	struct sunos32_sys_ustat_args /* {
1186 		syscallarg(int) dev;
1187 		syscallarg(sunos32_ustatp_t) buf;
1188 	} */ *uap = v;
1189 	struct sunos_ustat us;
1190 	int error;
1191 
1192 	memset(&us, 0, sizeof us);
1193 
1194 	/*
1195 	 * XXX: should set f_tfree and f_tinode at least
1196 	 * How do we translate dev -> fstat? (and then to sunos_ustat)
1197 	 */
1198 
1199 	if ((error = copyout(&us, (caddr_t)(u_long)SCARG(uap, buf), sizeof us)) != 0)
1200 		return (error);
1201 	return 0;
1202 }
1203 
1204 int
1205 sunos32_sys_quotactl(l, v, retval)
1206 	struct lwp *l;
1207 	void *v;
1208 	register_t *retval;
1209 {
1210 
1211 	return EINVAL;
1212 }
1213 
1214 int
1215 sunos32_sys_vhangup(l, v, retval)
1216 	struct lwp *l;
1217 	void *v;
1218 	register_t *retval;
1219 {
1220 	struct proc *p = l->l_proc;
1221 	struct session *sp = p->p_session;
1222 
1223 	if (sp->s_ttyvp == 0)
1224 		return 0;
1225 
1226 	if (sp->s_ttyp && sp->s_ttyp->t_session == sp && sp->s_ttyp->t_pgrp)
1227 		pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
1228 
1229 	(void) ttywait(sp->s_ttyp);
1230 	if (sp->s_ttyvp)
1231 		VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
1232 	if (sp->s_ttyvp)
1233 		vrele(sp->s_ttyvp);
1234 	sp->s_ttyvp = NULL;
1235 
1236 	return 0;
1237 }
1238 
1239 static int
1240 sunstatfs(sp, buf)
1241 	struct statfs *sp;
1242 	caddr_t buf;
1243 {
1244 	struct sunos_statfs ssfs;
1245 
1246 	memset(&ssfs, 0, sizeof ssfs);
1247 	ssfs.f_type = 0;
1248 	ssfs.f_bsize = sp->f_bsize;
1249 	ssfs.f_blocks = sp->f_blocks;
1250 	ssfs.f_bfree = sp->f_bfree;
1251 	ssfs.f_bavail = sp->f_bavail;
1252 	ssfs.f_files = sp->f_files;
1253 	ssfs.f_ffree = sp->f_ffree;
1254 	ssfs.f_fsid = sp->f_fsid;
1255 	return copyout((caddr_t)&ssfs, buf, sizeof ssfs);
1256 }
1257 
1258 int
1259 sunos32_sys_statfs(l, v, retval)
1260 	struct lwp *l;
1261 	void *v;
1262 	register_t *retval;
1263 {
1264 	struct sunos32_sys_statfs_args /* {
1265 		syscallarg(const netbsd32_charp) path;
1266 		syscallarg(sunos32_statfsp_t) buf;
1267 	} */ *uap = v;
1268 	struct proc *p = l->l_proc;
1269 	struct mount *mp;
1270 	struct statfs *sp;
1271 	int error;
1272 	struct nameidata nd;
1273 
1274 	caddr_t sg = stackgap_init(p, 0);
1275 	SUNOS32_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
1276 
1277 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, (caddr_t)(u_long)SCARG(uap, path), p);
1278 	if ((error = namei(&nd)) != 0)
1279 		return (error);
1280 	mp = nd.ni_vp->v_mount;
1281 	sp = &mp->mnt_stat;
1282 	vrele(nd.ni_vp);
1283 	if ((error = VFS_STATFS(mp, sp, p)) != 0)
1284 		return (error);
1285 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1286 	return sunstatfs(sp, (caddr_t)(u_long)SCARG(uap, buf));
1287 }
1288 
1289 int
1290 sunos32_sys_fstatfs(l, v, retval)
1291 	struct lwp *l;
1292 	void *v;
1293 	register_t *retval;
1294 {
1295 	struct sunos32_sys_fstatfs_args /* {
1296 		syscallarg(int) fd;
1297 		syscallarg(sunos32_statfsp_t) buf;
1298 	} */ *uap = v;
1299 	struct proc *p = l->l_proc;
1300 	struct file *fp;
1301 	struct mount *mp;
1302 	struct statfs *sp;
1303 	int error;
1304 
1305 	/* getvnode() will use the descriptor for us */
1306 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1307 		return (error);
1308 	mp = ((struct vnode *)fp->f_data)->v_mount;
1309 	sp = &mp->mnt_stat;
1310 	if ((error = VFS_STATFS(mp, sp, p)) != 0)
1311 		goto out;
1312 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1313 	error = sunstatfs(sp, (caddr_t)(u_long)SCARG(uap, buf));
1314  out:
1315 	FILE_UNUSE(fp, p);
1316 	return (error);
1317 }
1318 
1319 int
1320 sunos32_sys_exportfs(l, v, retval)
1321 	struct lwp *l;
1322 	void *v;
1323 	register_t *retval;
1324 {
1325 	/*
1326 	 * XXX: should perhaps translate into a mount(2)
1327 	 * with MOUNT_EXPORT?
1328 	 */
1329 	return 0;
1330 }
1331 
1332 int
1333 sunos32_sys_mknod(l, v, retval)
1334 	struct lwp *l;
1335 	void *v;
1336 	register_t *retval;
1337 {
1338 	struct sunos32_sys_mknod_args /* {
1339 		syscallarg(const netbsd32_charp) path;
1340 		syscallarg(int) mode;
1341 		syscallarg(int) dev;
1342 	} */ *uap = v;
1343 	struct proc *p = l->l_proc;
1344 
1345 	caddr_t sg = stackgap_init(p, 0);
1346 	SUNOS32_CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
1347 
1348 	/* netbsd32_mkfifo/mknod to not do alt checking */
1349 	if (S_ISFIFO(SCARG(uap, mode)))
1350 		return netbsd32_mkfifo(l, (struct netbsd32_mkfifo_args *)uap, retval);
1351 
1352 	return netbsd32_mknod(l, (struct netbsd32_mknod_args *)uap, retval);
1353 }
1354 
1355 #define SUNOS_SC_ARG_MAX	1
1356 #define SUNOS_SC_CHILD_MAX	2
1357 #define SUNOS_SC_CLK_TCK	3
1358 #define SUNOS_SC_NGROUPS_MAX	4
1359 #define SUNOS_SC_OPEN_MAX	5
1360 #define SUNOS_SC_JOB_CONTROL	6
1361 #define SUNOS_SC_SAVED_IDS	7
1362 #define SUNOS_SC_VERSION	8
1363 
1364 int
1365 sunos32_sys_sysconf(l, v, retval)
1366 	struct lwp *l;
1367 	void *v;
1368 	register_t *retval;
1369 {
1370 	struct sunos32_sys_sysconf_args /* {
1371 		syscallarg(int) name;
1372 	} */ *uap = v;
1373 	extern int maxfiles;
1374 
1375 	switch(SCARG(uap, name)) {
1376 	case SUNOS_SC_ARG_MAX:
1377 		*retval = ARG_MAX;
1378 		break;
1379 	case SUNOS_SC_CHILD_MAX:
1380 		*retval = maxproc;
1381 		break;
1382 	case SUNOS_SC_CLK_TCK:
1383 		*retval = 60;		/* should this be `hz', ie. 100? */
1384 		break;
1385 	case SUNOS_SC_NGROUPS_MAX:
1386 		*retval = NGROUPS_MAX;
1387 		break;
1388 	case SUNOS_SC_OPEN_MAX:
1389 		*retval = maxfiles;
1390 		break;
1391 	case SUNOS_SC_JOB_CONTROL:
1392 		*retval = 1;
1393 		break;
1394 	case SUNOS_SC_SAVED_IDS:
1395 #ifdef _POSIX_SAVED_IDS
1396 		*retval = 1;
1397 #else
1398 		*retval = 0;
1399 #endif
1400 		break;
1401 	case SUNOS_SC_VERSION:
1402 		*retval = 198808;
1403 		break;
1404 	default:
1405 		return EINVAL;
1406 	}
1407 	return 0;
1408 }
1409 
1410 #define SUNOS_RLIMIT_NOFILE	6	/* Other RLIMIT_* are the same */
1411 #define SUNOS_RLIM_NLIMITS	7
1412 
1413 int
1414 sunos32_sys_getrlimit(l, v, retval)
1415 	struct lwp *l;
1416 	void *v;
1417 	register_t *retval;
1418 {
1419 	struct sunos32_sys_getrlimit_args /* {
1420 		syscallarg(u_int) which;
1421 		syscallarg(netbsd32_orlimitp_t) rlp;
1422 	} */ *uap = v;
1423 
1424 	if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
1425 		return EINVAL;
1426 
1427 	if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
1428 		SCARG(uap, which) = RLIMIT_NOFILE;
1429 
1430 	return compat_43_netbsd32_ogetrlimit(l, uap, retval);
1431 }
1432 
1433 int
1434 sunos32_sys_setrlimit(l, v, retval)
1435 	struct lwp *l;
1436 	void *v;
1437 	register_t *retval;
1438 {
1439 	struct sunos32_sys_setrlimit_args /* {
1440 		syscallarg(u_int) which;
1441 		syscallarg(netbsd32_orlimitp_t) rlp;
1442 	} */ *uap = v;
1443 
1444 	if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
1445 		return EINVAL;
1446 
1447 	if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
1448 		SCARG(uap, which) = RLIMIT_NOFILE;
1449 
1450 	return compat_43_netbsd32_osetrlimit(l, uap, retval);
1451 }
1452 
1453 /* for the m68k machines */
1454 #ifndef PT_GETFPREGS
1455 #define PT_GETFPREGS -1
1456 #endif
1457 #ifndef PT_SETFPREGS
1458 #define PT_SETFPREGS -1
1459 #endif
1460 
1461 static int sreq2breq[] = {
1462 	PT_TRACE_ME,    PT_READ_I,      PT_READ_D,      -1,
1463 	PT_WRITE_I,     PT_WRITE_D,     -1,             PT_CONTINUE,
1464 	PT_KILL,        -1,             PT_ATTACH,      PT_DETACH,
1465 	PT_GETREGS,     PT_SETREGS,     PT_GETFPREGS,   PT_SETFPREGS
1466 };
1467 static int nreqs = sizeof(sreq2breq) / sizeof(sreq2breq[0]);
1468 
1469 int
1470 sunos32_sys_ptrace(l, v, retval)
1471 	struct lwp *l;
1472 	void *v;
1473 	register_t *retval;
1474 {
1475 	struct sunos32_sys_ptrace_args /* {
1476 		syscallarg(int) req;
1477 		syscallarg(pid_t) pid;
1478 		syscallarg(netbsd32_caddr_t) addr;
1479 		syscallarg(int) data;
1480 		syscallarg(netbsd32_charp) addr2;
1481 	} */ *uap = v;
1482 	struct netbsd32_ptrace_args pa;
1483 	int req;
1484 
1485 	req = SCARG(uap, req);
1486 
1487 	if (req < 0 || req >= nreqs)
1488 		return (EINVAL);
1489 
1490 	req = sreq2breq[req];
1491 	if (req == -1)
1492 		return (EINVAL);
1493 
1494 	SCARG(&pa, req) = req;
1495 	SCARG(&pa, pid) = (pid_t)SCARG(uap, pid);
1496 	SCARG(&pa, addr) = SCARG(uap, addr);
1497 	SCARG(&pa, data) = SCARG(uap, data);
1498 
1499 	return netbsd32_ptrace(l, &pa, retval);
1500 }
1501 
1502 /*
1503  * SunOS reboot system call (for compatibility).
1504  * Sun lets you pass in a boot string which the PROM
1505  * saves and provides to the next boot program.
1506  */
1507 
1508 #define SUNOS_RB_ASKNAME	0x001
1509 #define SUNOS_RB_SINGLE 	0x002
1510 #define SUNOS_RB_NOSYNC		0x004
1511 #define SUNOS_RB_HALT		0x008
1512 #define SUNOS_RB_DUMP		0x080
1513 #define	SUNOS_RB_STRING		0x200
1514 
1515 static struct sunos_howto_conv {
1516 	int sun_howto;
1517 	int bsd_howto;
1518 } sunos_howto_conv[] = {
1519 	{ SUNOS_RB_ASKNAME,	RB_ASKNAME },
1520 	{ SUNOS_RB_SINGLE,	RB_SINGLE },
1521 	{ SUNOS_RB_NOSYNC,	RB_NOSYNC },
1522 	{ SUNOS_RB_HALT,	RB_HALT },
1523 	{ SUNOS_RB_DUMP,	RB_DUMP },
1524 	{ SUNOS_RB_STRING,	RB_STRING },
1525 	{ 0x000,		0 },
1526 };
1527 
1528 int
1529 sunos32_sys_reboot(l, v, retval)
1530 	struct lwp *l;
1531 	void *v;
1532 	register_t *retval;
1533 {
1534 	struct sunos32_sys_reboot_args /* {
1535 		syscallarg(int) howto;
1536 		syscallarg(netbsd32_charp) bootstr;
1537 	} */ *uap = v;
1538 	struct proc *p = l->l_proc;
1539 	struct sys_reboot_args ua;
1540 	struct sunos_howto_conv *convp;
1541 	int error, bsd_howto, sun_howto;
1542 	char *bootstr;
1543 
1544 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1545 		return (error);
1546 
1547 	/*
1548 	 * Convert howto bits to BSD format.
1549 	 */
1550 	sun_howto = SCARG(uap, howto);
1551 	bsd_howto = 0;
1552 	convp = sunos_howto_conv;
1553 	while (convp->sun_howto) {
1554 		if (sun_howto & convp->sun_howto)
1555 			bsd_howto |= convp->bsd_howto;
1556 		convp++;
1557 	}
1558 
1559 	/*
1560 	 * Sun RB_STRING (Get user supplied bootstring.)
1561 	 * If the machine supports passing a string to the
1562 	 * next booted kernel.
1563 	 */
1564 	if (sun_howto & SUNOS_RB_STRING)
1565 		bootstr = (char *)(u_long)SCARG(uap, bootstr);
1566 	else
1567 		bootstr = NULL;
1568 
1569 	SCARG(&ua, opt) = bsd_howto;
1570 	SCARG(&ua, bootstr) = bootstr;
1571 	return (sys_reboot(l, &ua, retval));
1572 }
1573 
1574 /*
1575  * Generalized interface signal handler, 4.3-compatible.
1576  */
1577 /* ARGSUSED */
1578 int
1579 sunos32_sys_sigvec(l, v, retval)
1580 	struct lwp *l;
1581 	void *v;
1582 	register_t *retval;
1583 {
1584 	struct sunos32_sys_sigvec_args /* {
1585 		syscallarg(int) signum;
1586 		syscallarg(struct sigvec *) nsv;
1587 		syscallarg(struct sigvec *) osv;
1588 	} */ *uap = v;
1589 	struct proc *p = l->l_proc;
1590 	struct netbsd32_sigvec sv;
1591 	struct sigaction nsa, osa;
1592 	int error;
1593 
1594 	if (SCARG(uap, nsv)) {
1595 		error = copyin((caddr_t)(u_long)SCARG(uap, nsv), &sv, sizeof(sv));
1596 		if (error != 0)
1597 			return (error);
1598 
1599 		/*
1600 		 * SunOS uses the mask 0x0004 as SV_RESETHAND
1601 		 * meaning: `reset to SIG_DFL on delivery'.
1602 		 * We support only the bits in: 0xF
1603 		 * (those bits are the same as ours)
1604 		 */
1605 		if (sv.sv_flags & ~0xF)
1606 			return (EINVAL);
1607 
1608 		sunos32_sigvec_to_sigaction(&sv, &nsa);
1609 	}
1610 	error = sigaction1(p, SCARG(uap, signum),
1611 			   SCARG(uap, nsv) ? &nsa : 0,
1612 			   SCARG(uap, osv) ? &osa : 0,
1613 			   NULL, 0);
1614 	if (error != 0)
1615 		return (error);
1616 
1617 	if (SCARG(uap, osv)) {
1618 		sunos32_sigvec_from_sigaction(&sv, &osa);
1619 		error = copyout(&sv, (caddr_t)(u_long)SCARG(uap, osv), sizeof(sv));
1620 		if (error != 0)
1621 			return (error);
1622 	}
1623 
1624 	return (0);
1625 }
1626