xref: /netbsd-src/sys/compat/sunos32/sunos32_misc.c (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /*	$NetBSD: sunos32_misc.c,v 1.19 2003/05/17 01:35:54 nakayama 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.19 2003/05/17 01:35:54 nakayama 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 		strlcpy(idb.d_name, bdp->d_name, sizeof(idb.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 	int error;
842 
843 	/*
844 	 * Verify the arguments.
845 	 */
846 	if (SCARG(uap, prot) & ~(PROT_READ|PROT_WRITE|PROT_EXEC))
847 		return (EINVAL);			/* XXX still needed? */
848 
849 	if ((SCARG(uap, flags) & SUNOS32__MAP_NEW) == 0)
850 		return (EINVAL);
851 
852 	SUNOS32TOP_UAP(addr, void);
853 	SUNOS32TOX_UAP(len, size_t);
854 	SUNOS32TO64_UAP(prot);
855 	SCARG(&ua, flags) = SCARG(uap, flags) & ~SUNOS32__MAP_NEW;
856 	SUNOS32TO64_UAP(fd);
857 	SCARG(&ua, pad) = 0;
858 	SUNOS32TOX_UAP(pos, off_t);
859 
860 	error = sys_mmap(l, &ua, retval);
861 	if ((u_long)*retval > (u_long)UINT_MAX) {
862 		printf("sunos32_mmap: retval out of range: 0x%lx",
863 		       (u_long)*retval);
864 		/* Should try to recover and return an error here. */
865 	}
866 	return (error);
867 }
868 
869 #define	MC_SYNC		1
870 #define	MC_LOCK		2
871 #define	MC_UNLOCK	3
872 #define	MC_ADVISE	4
873 #define	MC_LOCKAS	5
874 #define	MC_UNLOCKAS	6
875 
876 int
877 sunos32_sys_mctl(l, v, retval)
878 	struct lwp *l;
879 	void *v;
880 	register_t *retval;
881 {
882 	struct sunos32_sys_mctl_args /* {
883 		syscallarg(netbsd32_voidp) addr;
884 		syscallarg(int) len;
885 		syscallarg(int) func;
886 		syscallarg(netbsd32_voidp) arg;
887 	} */ *uap = v;
888 
889 	switch (SCARG(uap, func)) {
890 	case MC_ADVISE:		/* ignore for now */
891 		return (0);
892 	case MC_SYNC:		/* translate to msync */
893 		return (netbsd32___msync13(l, uap, retval));
894 	default:
895 		return (EINVAL);
896 	}
897 }
898 
899 int
900 sunos32_sys_setsockopt(l, v, retval)
901 	struct lwp *l;
902 	void *v;
903 	register_t *retval;
904 {
905 	struct sunos32_sys_setsockopt_args /* {
906 		syscallarg(int) s;
907 		syscallarg(int) level;
908 		syscallarg(int) name;
909 		syscallarg(netbsd32_caddr_t) val;
910 		syscallarg(int) valsize;
911 	} */ *uap = v;
912 	struct proc *p = l->l_proc;
913 	struct file *fp;
914 	struct mbuf *m = NULL;
915 	int error;
916 
917 	/* getsock() will use the descriptor for us */
918 	if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
919 		return (error);
920 #define	SO_DONTLINGER (~SO_LINGER)
921 	if (SCARG(uap, name) == SO_DONTLINGER) {
922 		m = m_get(M_WAIT, MT_SOOPTS);
923 		mtod(m, struct linger *)->l_onoff = 0;
924 		m->m_len = sizeof(struct linger);
925 		error = sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
926 		    SO_LINGER, m);
927 		goto out;
928 	}
929 	if (SCARG(uap, level) == IPPROTO_IP) {
930 #define		SUNOS_IP_MULTICAST_IF		2
931 #define		SUNOS_IP_MULTICAST_TTL		3
932 #define		SUNOS_IP_MULTICAST_LOOP		4
933 #define		SUNOS_IP_ADD_MEMBERSHIP		5
934 #define		SUNOS_IP_DROP_MEMBERSHIP	6
935 		static int ipoptxlat[] = {
936 			IP_MULTICAST_IF,
937 			IP_MULTICAST_TTL,
938 			IP_MULTICAST_LOOP,
939 			IP_ADD_MEMBERSHIP,
940 			IP_DROP_MEMBERSHIP
941 		};
942 		if (SCARG(uap, name) >= SUNOS_IP_MULTICAST_IF &&
943 		    SCARG(uap, name) <= SUNOS_IP_DROP_MEMBERSHIP) {
944 			SCARG(uap, name) =
945 			    ipoptxlat[SCARG(uap, name) - SUNOS_IP_MULTICAST_IF];
946 		}
947 	}
948 	if (SCARG(uap, valsize) > MLEN) {
949 		error = EINVAL;
950 		goto out;
951 	}
952 	if (SCARG(uap, val)) {
953 		m = m_get(M_WAIT, MT_SOOPTS);
954 		error = copyin((caddr_t)(u_long)SCARG(uap, val), mtod(m, caddr_t),
955 		    (u_int)SCARG(uap, valsize));
956 		if (error) {
957 			(void) m_free(m);
958 			goto out;
959 		}
960 		m->m_len = SCARG(uap, valsize);
961 	}
962 	error = sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
963 	    SCARG(uap, name), m);
964  out:
965 	FILE_UNUSE(fp, p);
966 	return (error);
967 }
968 
969 static __inline__ int sunos32_sys_socket_common(struct lwp *, register_t *,
970 					      int type);
971 static __inline__ int
972 sunos32_sys_socket_common(l, retval, type)
973 	struct lwp *l;
974 	register_t *retval;
975 	int type;
976 {
977 	struct socket *so;
978 	struct proc *p = l->l_proc;
979 	struct file *fp;
980 	int error, fd;
981 
982 	/* getsock() will use the descriptor for us */
983 	fd = (int)*retval;
984 	if ((error = getsock(p->p_fd, fd, &fp)) == 0) {
985 		so = (struct socket *)fp->f_data;
986 		if (type == SOCK_DGRAM)
987 			so->so_options |= SO_BROADCAST;
988 	}
989 	FILE_UNUSE(fp, p);
990 	return (error);
991 }
992 
993 int
994 sunos32_sys_socket(l, v, retval)
995 	struct lwp *l;
996 	void *v;
997 	register_t *retval;
998 {
999 	struct sunos32_sys_socket_args /* {
1000 		syscallarg(int) domain;
1001 		syscallarg(int) type;
1002 		syscallarg(int) protocol;
1003 	} */ *uap = v;
1004 	int error;
1005 
1006 	error = netbsd32_socket(l, v, retval);
1007 	if (error)
1008 		return (error);
1009 	return sunos32_sys_socket_common(l, retval, SCARG(uap, type));
1010 }
1011 
1012 int
1013 sunos32_sys_socketpair(l, v, retval)
1014 	struct lwp *l;
1015 	void *v;
1016 	register_t *retval;
1017 {
1018 	struct sunos32_sys_socketpair_args /* {
1019 		syscallarg(int) domain;
1020 		syscallarg(int) type;
1021 		syscallarg(int) protocol;
1022 		syscallarg(int *) rsv;
1023 	} */ *uap = v;
1024 	int error;
1025 
1026 	error = netbsd32_socketpair(l, v, retval);
1027 	if (error)
1028 		return (error);
1029 	return sunos32_sys_socket_common(l, retval, SCARG(uap, type));
1030 }
1031 
1032 
1033 /*
1034  * XXX: This needs cleaning up.
1035  */
1036 int
1037 sunos32_sys_auditsys(l, v, retval)
1038 	struct lwp *l;
1039 	void *v;
1040 	register_t *retval;
1041 {
1042 	return 0;
1043 }
1044 
1045 int
1046 sunos32_sys_uname(l, v, retval)
1047 	struct lwp *l;
1048 	void *v;
1049 	register_t *retval;
1050 {
1051 	struct sunos32_sys_uname_args /* {
1052 		syscallarg(sunos32_utsnamep_t) name;
1053 	} */ *uap = v;
1054 	struct sunos_utsname sut;
1055 
1056 	memset(&sut, 0, sizeof(sut));
1057 
1058 	memcpy(sut.sysname, ostype, sizeof(sut.sysname) - 1);
1059 	memcpy(sut.nodename, hostname, sizeof(sut.nodename));
1060 	sut.nodename[sizeof(sut.nodename)-1] = '\0';
1061 	memcpy(sut.release, osrelease, sizeof(sut.release) - 1);
1062 	memcpy(sut.version, "1", sizeof(sut.version) - 1);
1063 	memcpy(sut.machine, machine, sizeof(sut.machine) - 1);
1064 
1065 	return copyout((caddr_t)&sut, (caddr_t)(u_long)SCARG(uap, name),
1066 	    sizeof(struct sunos_utsname));
1067 }
1068 
1069 int
1070 sunos32_sys_setpgrp(l, v, retval)
1071 	struct lwp *l;
1072 	void *v;
1073 	register_t *retval;
1074 {
1075 	struct sunos32_sys_setpgrp_args /* {
1076 		syscallarg(int) pid;
1077 		syscallarg(int) pgid;
1078 	} */ *uap = v;
1079 	struct proc *p = l->l_proc;
1080 
1081 	/*
1082 	 * difference to our setpgid call is to include backwards
1083 	 * compatibility to pre-setsid() binaries. Do setsid()
1084 	 * instead of setpgid() in those cases where the process
1085 	 * tries to create a new session the old way.
1086 	 */
1087 	if (!SCARG(uap, pgid) &&
1088 	    (!SCARG(uap, pid) || SCARG(uap, pid) == p->p_pid))
1089 		return sys_setsid(l, uap, retval);
1090 	else
1091 		return netbsd32_setpgid(l, uap, retval);
1092 }
1093 
1094 int
1095 sunos32_sys_open(l, v, retval)
1096 	struct lwp *l;
1097 	void *v;
1098 	register_t *retval;
1099 {
1100 	struct sunos32_sys_open_args /* {
1101 		syscallarg(const netbsd32_charp) path;
1102 		syscallarg(int) flags;
1103 		syscallarg(int) mode;
1104 	} */ *uap = v;
1105 	struct proc *p = l->l_proc;
1106 	struct sys_open_args ua;
1107 	int lf, r;
1108 	int noctty;
1109 	int ret;
1110 	caddr_t sg = stackgap_init(p, 0);
1111 
1112 	/* convert mode into NetBSD mode */
1113 	lf = SCARG(uap, flags);
1114 	noctty = lf & 0x8000;
1115 	r =	(lf & (0x0001 | 0x0002 | 0x0008 | 0x0040 | 0x0200 | 0x0400 | 0x0800));
1116 	r |=	((lf & (0x0004 | 0x1000 | 0x4000)) ? O_NONBLOCK : 0);
1117 	r |=	((lf & 0x0080) ? O_SHLOCK : 0);
1118 	r |=	((lf & 0x0100) ? O_EXLOCK : 0);
1119 	r |=	((lf & 0x2000) ? O_FSYNC : 0);
1120 
1121 	SUNOS32TOP_UAP(path, const char);
1122 	SCARG(&ua, flags) = r;
1123 	SUNOS32TO64_UAP(mode);
1124 
1125 	if (r & O_CREAT)
1126 		SUNOS32_CHECK_ALT_CREAT(p, &sg, SCARG(&ua, path));
1127 	else
1128 		SUNOS32_CHECK_ALT_EXIST(p, &sg, SCARG(&ua, path));
1129 
1130 	ret = sys_open(l, &ua, retval);
1131 
1132 	if (!ret && !noctty && SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
1133 		struct filedesc *fdp = p->p_fd;
1134 		struct file *fp;
1135 
1136 		fp = fd_getfile(fdp, *retval);
1137 
1138 		/* ignore any error, just give it a try */
1139 		if (fp != NULL && fp->f_type == DTYPE_VNODE)
1140 			(fp->f_ops->fo_ioctl)(fp, TIOCSCTTY, (caddr_t)0, p);
1141 	}
1142 	return ret;
1143 }
1144 
1145 #if defined (NFSSERVER)
1146 int
1147 sunos32_sys_nfssvc(l, v, retval)
1148 	struct lwp *l;
1149 	void *v;
1150 	register_t *retval;
1151 {
1152 #if 0
1153 	struct sunos32_sys_nfssvc_args *uap = v;
1154 	struct emul *e = p->p_emul;
1155 	struct sys_nfssvc_args outuap;
1156 	struct sockaddr sa;
1157 	int error;
1158 	caddr_t sg = stackgap_init(p, 0);
1159 
1160 	memset(&outuap, 0, sizeof outuap);
1161 	SCARG(&outuap, fd) = SCARG(uap, fd);
1162 	SCARG(&outuap, mskval) = stackgap_alloc(p, &sg, sizeof(sa));
1163 	SCARG(&outuap, msklen) = sizeof(sa);
1164 	SCARG(&outuap, mtchval) = stackgap_alloc(p, &sg, sizeof(sa));
1165 	SCARG(&outuap, mtchlen) = sizeof(sa);
1166 
1167 	memset(&sa, 0, sizeof sa);
1168 	if (error = copyout(&sa, SCARG(&outuap, mskval), SCARG(&outuap, msklen)))
1169 		return (error);
1170 	if (error = copyout(&sa, SCARG(&outuap, mtchval), SCARG(&outuap, mtchlen)))
1171 		return (error);
1172 
1173 	return nfssvc(l, &outuap, retval);
1174 #else
1175 	return (ENOSYS);
1176 #endif
1177 }
1178 #endif /* NFSSERVER */
1179 
1180 int
1181 sunos32_sys_ustat(l, v, retval)
1182 	struct lwp *l;
1183 	void *v;
1184 	register_t *retval;
1185 {
1186 	struct sunos32_sys_ustat_args /* {
1187 		syscallarg(int) dev;
1188 		syscallarg(sunos32_ustatp_t) buf;
1189 	} */ *uap = v;
1190 	struct sunos_ustat us;
1191 	int error;
1192 
1193 	memset(&us, 0, sizeof us);
1194 
1195 	/*
1196 	 * XXX: should set f_tfree and f_tinode at least
1197 	 * How do we translate dev -> fstat? (and then to sunos_ustat)
1198 	 */
1199 
1200 	if ((error = copyout(&us, (caddr_t)(u_long)SCARG(uap, buf), sizeof us)) != 0)
1201 		return (error);
1202 	return 0;
1203 }
1204 
1205 int
1206 sunos32_sys_quotactl(l, v, retval)
1207 	struct lwp *l;
1208 	void *v;
1209 	register_t *retval;
1210 {
1211 
1212 	return EINVAL;
1213 }
1214 
1215 int
1216 sunos32_sys_vhangup(l, v, retval)
1217 	struct lwp *l;
1218 	void *v;
1219 	register_t *retval;
1220 {
1221 	struct proc *p = l->l_proc;
1222 	struct session *sp = p->p_session;
1223 
1224 	if (sp->s_ttyvp == 0)
1225 		return 0;
1226 
1227 	if (sp->s_ttyp && sp->s_ttyp->t_session == sp && sp->s_ttyp->t_pgrp)
1228 		pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
1229 
1230 	(void) ttywait(sp->s_ttyp);
1231 	if (sp->s_ttyvp)
1232 		VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
1233 	if (sp->s_ttyvp)
1234 		vrele(sp->s_ttyvp);
1235 	sp->s_ttyvp = NULL;
1236 
1237 	return 0;
1238 }
1239 
1240 static int
1241 sunstatfs(sp, buf)
1242 	struct statfs *sp;
1243 	caddr_t buf;
1244 {
1245 	struct sunos_statfs ssfs;
1246 
1247 	memset(&ssfs, 0, sizeof ssfs);
1248 	ssfs.f_type = 0;
1249 	ssfs.f_bsize = sp->f_bsize;
1250 	ssfs.f_blocks = sp->f_blocks;
1251 	ssfs.f_bfree = sp->f_bfree;
1252 	ssfs.f_bavail = sp->f_bavail;
1253 	ssfs.f_files = sp->f_files;
1254 	ssfs.f_ffree = sp->f_ffree;
1255 	ssfs.f_fsid = sp->f_fsid;
1256 	return copyout((caddr_t)&ssfs, buf, sizeof ssfs);
1257 }
1258 
1259 int
1260 sunos32_sys_statfs(l, v, retval)
1261 	struct lwp *l;
1262 	void *v;
1263 	register_t *retval;
1264 {
1265 	struct sunos32_sys_statfs_args /* {
1266 		syscallarg(const netbsd32_charp) path;
1267 		syscallarg(sunos32_statfsp_t) buf;
1268 	} */ *uap = v;
1269 	struct proc *p = l->l_proc;
1270 	struct mount *mp;
1271 	struct statfs *sp;
1272 	int error;
1273 	struct nameidata nd;
1274 
1275 	caddr_t sg = stackgap_init(p, 0);
1276 	SUNOS32_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
1277 
1278 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, (caddr_t)(u_long)SCARG(uap, path), p);
1279 	if ((error = namei(&nd)) != 0)
1280 		return (error);
1281 	mp = nd.ni_vp->v_mount;
1282 	sp = &mp->mnt_stat;
1283 	vrele(nd.ni_vp);
1284 	if ((error = VFS_STATFS(mp, sp, p)) != 0)
1285 		return (error);
1286 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1287 	return sunstatfs(sp, (caddr_t)(u_long)SCARG(uap, buf));
1288 }
1289 
1290 int
1291 sunos32_sys_fstatfs(l, v, retval)
1292 	struct lwp *l;
1293 	void *v;
1294 	register_t *retval;
1295 {
1296 	struct sunos32_sys_fstatfs_args /* {
1297 		syscallarg(int) fd;
1298 		syscallarg(sunos32_statfsp_t) buf;
1299 	} */ *uap = v;
1300 	struct proc *p = l->l_proc;
1301 	struct file *fp;
1302 	struct mount *mp;
1303 	struct statfs *sp;
1304 	int error;
1305 
1306 	/* getvnode() will use the descriptor for us */
1307 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1308 		return (error);
1309 	mp = ((struct vnode *)fp->f_data)->v_mount;
1310 	sp = &mp->mnt_stat;
1311 	if ((error = VFS_STATFS(mp, sp, p)) != 0)
1312 		goto out;
1313 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1314 	error = sunstatfs(sp, (caddr_t)(u_long)SCARG(uap, buf));
1315  out:
1316 	FILE_UNUSE(fp, p);
1317 	return (error);
1318 }
1319 
1320 int
1321 sunos32_sys_exportfs(l, v, retval)
1322 	struct lwp *l;
1323 	void *v;
1324 	register_t *retval;
1325 {
1326 	/*
1327 	 * XXX: should perhaps translate into a mount(2)
1328 	 * with MOUNT_EXPORT?
1329 	 */
1330 	return 0;
1331 }
1332 
1333 int
1334 sunos32_sys_mknod(l, v, retval)
1335 	struct lwp *l;
1336 	void *v;
1337 	register_t *retval;
1338 {
1339 	struct sunos32_sys_mknod_args /* {
1340 		syscallarg(const netbsd32_charp) path;
1341 		syscallarg(int) mode;
1342 		syscallarg(int) dev;
1343 	} */ *uap = v;
1344 	struct proc *p = l->l_proc;
1345 
1346 	caddr_t sg = stackgap_init(p, 0);
1347 	SUNOS32_CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
1348 
1349 	/* netbsd32_mkfifo/mknod to not do alt checking */
1350 	if (S_ISFIFO(SCARG(uap, mode)))
1351 		return netbsd32_mkfifo(l, (struct netbsd32_mkfifo_args *)uap, retval);
1352 
1353 	return netbsd32_mknod(l, (struct netbsd32_mknod_args *)uap, retval);
1354 }
1355 
1356 #define SUNOS_SC_ARG_MAX	1
1357 #define SUNOS_SC_CHILD_MAX	2
1358 #define SUNOS_SC_CLK_TCK	3
1359 #define SUNOS_SC_NGROUPS_MAX	4
1360 #define SUNOS_SC_OPEN_MAX	5
1361 #define SUNOS_SC_JOB_CONTROL	6
1362 #define SUNOS_SC_SAVED_IDS	7
1363 #define SUNOS_SC_VERSION	8
1364 
1365 int
1366 sunos32_sys_sysconf(l, v, retval)
1367 	struct lwp *l;
1368 	void *v;
1369 	register_t *retval;
1370 {
1371 	struct sunos32_sys_sysconf_args /* {
1372 		syscallarg(int) name;
1373 	} */ *uap = v;
1374 	extern int maxfiles;
1375 
1376 	switch(SCARG(uap, name)) {
1377 	case SUNOS_SC_ARG_MAX:
1378 		*retval = ARG_MAX;
1379 		break;
1380 	case SUNOS_SC_CHILD_MAX:
1381 		*retval = maxproc;
1382 		break;
1383 	case SUNOS_SC_CLK_TCK:
1384 		*retval = 60;		/* should this be `hz', ie. 100? */
1385 		break;
1386 	case SUNOS_SC_NGROUPS_MAX:
1387 		*retval = NGROUPS_MAX;
1388 		break;
1389 	case SUNOS_SC_OPEN_MAX:
1390 		*retval = maxfiles;
1391 		break;
1392 	case SUNOS_SC_JOB_CONTROL:
1393 		*retval = 1;
1394 		break;
1395 	case SUNOS_SC_SAVED_IDS:
1396 #ifdef _POSIX_SAVED_IDS
1397 		*retval = 1;
1398 #else
1399 		*retval = 0;
1400 #endif
1401 		break;
1402 	case SUNOS_SC_VERSION:
1403 		*retval = 198808;
1404 		break;
1405 	default:
1406 		return EINVAL;
1407 	}
1408 	return 0;
1409 }
1410 
1411 #define SUNOS_RLIMIT_NOFILE	6	/* Other RLIMIT_* are the same */
1412 #define SUNOS_RLIM_NLIMITS	7
1413 
1414 int
1415 sunos32_sys_getrlimit(l, v, retval)
1416 	struct lwp *l;
1417 	void *v;
1418 	register_t *retval;
1419 {
1420 	struct sunos32_sys_getrlimit_args /* {
1421 		syscallarg(u_int) which;
1422 		syscallarg(netbsd32_orlimitp_t) rlp;
1423 	} */ *uap = v;
1424 
1425 	if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
1426 		return EINVAL;
1427 
1428 	if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
1429 		SCARG(uap, which) = RLIMIT_NOFILE;
1430 
1431 	return compat_43_netbsd32_ogetrlimit(l, uap, retval);
1432 }
1433 
1434 int
1435 sunos32_sys_setrlimit(l, v, retval)
1436 	struct lwp *l;
1437 	void *v;
1438 	register_t *retval;
1439 {
1440 	struct sunos32_sys_setrlimit_args /* {
1441 		syscallarg(u_int) which;
1442 		syscallarg(netbsd32_orlimitp_t) rlp;
1443 	} */ *uap = v;
1444 
1445 	if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
1446 		return EINVAL;
1447 
1448 	if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
1449 		SCARG(uap, which) = RLIMIT_NOFILE;
1450 
1451 	return compat_43_netbsd32_osetrlimit(l, uap, retval);
1452 }
1453 
1454 /* for the m68k machines */
1455 #ifndef PT_GETFPREGS
1456 #define PT_GETFPREGS -1
1457 #endif
1458 #ifndef PT_SETFPREGS
1459 #define PT_SETFPREGS -1
1460 #endif
1461 
1462 static int sreq2breq[] = {
1463 	PT_TRACE_ME,    PT_READ_I,      PT_READ_D,      -1,
1464 	PT_WRITE_I,     PT_WRITE_D,     -1,             PT_CONTINUE,
1465 	PT_KILL,        -1,             PT_ATTACH,      PT_DETACH,
1466 	PT_GETREGS,     PT_SETREGS,     PT_GETFPREGS,   PT_SETFPREGS
1467 };
1468 static int nreqs = sizeof(sreq2breq) / sizeof(sreq2breq[0]);
1469 
1470 int
1471 sunos32_sys_ptrace(l, v, retval)
1472 	struct lwp *l;
1473 	void *v;
1474 	register_t *retval;
1475 {
1476 	struct sunos32_sys_ptrace_args /* {
1477 		syscallarg(int) req;
1478 		syscallarg(pid_t) pid;
1479 		syscallarg(netbsd32_caddr_t) addr;
1480 		syscallarg(int) data;
1481 		syscallarg(netbsd32_charp) addr2;
1482 	} */ *uap = v;
1483 	struct netbsd32_ptrace_args pa;
1484 	int req;
1485 
1486 	req = SCARG(uap, req);
1487 
1488 	if (req < 0 || req >= nreqs)
1489 		return (EINVAL);
1490 
1491 	req = sreq2breq[req];
1492 	if (req == -1)
1493 		return (EINVAL);
1494 
1495 	SCARG(&pa, req) = req;
1496 	SCARG(&pa, pid) = (pid_t)SCARG(uap, pid);
1497 	SCARG(&pa, addr) = SCARG(uap, addr);
1498 	SCARG(&pa, data) = SCARG(uap, data);
1499 
1500 	return netbsd32_ptrace(l, &pa, retval);
1501 }
1502 
1503 /*
1504  * SunOS reboot system call (for compatibility).
1505  * Sun lets you pass in a boot string which the PROM
1506  * saves and provides to the next boot program.
1507  */
1508 
1509 #define SUNOS_RB_ASKNAME	0x001
1510 #define SUNOS_RB_SINGLE 	0x002
1511 #define SUNOS_RB_NOSYNC		0x004
1512 #define SUNOS_RB_HALT		0x008
1513 #define SUNOS_RB_DUMP		0x080
1514 #define	SUNOS_RB_STRING		0x200
1515 
1516 static struct sunos_howto_conv {
1517 	int sun_howto;
1518 	int bsd_howto;
1519 } sunos_howto_conv[] = {
1520 	{ SUNOS_RB_ASKNAME,	RB_ASKNAME },
1521 	{ SUNOS_RB_SINGLE,	RB_SINGLE },
1522 	{ SUNOS_RB_NOSYNC,	RB_NOSYNC },
1523 	{ SUNOS_RB_HALT,	RB_HALT },
1524 	{ SUNOS_RB_DUMP,	RB_DUMP },
1525 	{ SUNOS_RB_STRING,	RB_STRING },
1526 	{ 0x000,		0 },
1527 };
1528 
1529 int
1530 sunos32_sys_reboot(l, v, retval)
1531 	struct lwp *l;
1532 	void *v;
1533 	register_t *retval;
1534 {
1535 	struct sunos32_sys_reboot_args /* {
1536 		syscallarg(int) howto;
1537 		syscallarg(netbsd32_charp) bootstr;
1538 	} */ *uap = v;
1539 	struct proc *p = l->l_proc;
1540 	struct sys_reboot_args ua;
1541 	struct sunos_howto_conv *convp;
1542 	int error, bsd_howto, sun_howto;
1543 	char *bootstr;
1544 
1545 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1546 		return (error);
1547 
1548 	/*
1549 	 * Convert howto bits to BSD format.
1550 	 */
1551 	sun_howto = SCARG(uap, howto);
1552 	bsd_howto = 0;
1553 	convp = sunos_howto_conv;
1554 	while (convp->sun_howto) {
1555 		if (sun_howto & convp->sun_howto)
1556 			bsd_howto |= convp->bsd_howto;
1557 		convp++;
1558 	}
1559 
1560 	/*
1561 	 * Sun RB_STRING (Get user supplied bootstring.)
1562 	 * If the machine supports passing a string to the
1563 	 * next booted kernel.
1564 	 */
1565 	if (sun_howto & SUNOS_RB_STRING)
1566 		bootstr = (char *)(u_long)SCARG(uap, bootstr);
1567 	else
1568 		bootstr = NULL;
1569 
1570 	SCARG(&ua, opt) = bsd_howto;
1571 	SCARG(&ua, bootstr) = bootstr;
1572 	return (sys_reboot(l, &ua, retval));
1573 }
1574 
1575 /*
1576  * Generalized interface signal handler, 4.3-compatible.
1577  */
1578 /* ARGSUSED */
1579 int
1580 sunos32_sys_sigvec(l, v, retval)
1581 	struct lwp *l;
1582 	void *v;
1583 	register_t *retval;
1584 {
1585 	struct sunos32_sys_sigvec_args /* {
1586 		syscallarg(int) signum;
1587 		syscallarg(struct sigvec *) nsv;
1588 		syscallarg(struct sigvec *) osv;
1589 	} */ *uap = v;
1590 	struct proc *p = l->l_proc;
1591 	struct netbsd32_sigvec sv;
1592 	struct sigaction nsa, osa;
1593 	int error;
1594 
1595 	if (SCARG(uap, nsv)) {
1596 		error = copyin((caddr_t)(u_long)SCARG(uap, nsv), &sv, sizeof(sv));
1597 		if (error != 0)
1598 			return (error);
1599 
1600 		/*
1601 		 * SunOS uses the mask 0x0004 as SV_RESETHAND
1602 		 * meaning: `reset to SIG_DFL on delivery'.
1603 		 * We support only the bits in: 0xF
1604 		 * (those bits are the same as ours)
1605 		 */
1606 		if (sv.sv_flags & ~0xF)
1607 			return (EINVAL);
1608 
1609 		sunos32_sigvec_to_sigaction(&sv, &nsa);
1610 	}
1611 	error = sigaction1(p, SCARG(uap, signum),
1612 			   SCARG(uap, nsv) ? &nsa : 0,
1613 			   SCARG(uap, osv) ? &osa : 0,
1614 			   NULL, 0);
1615 	if (error != 0)
1616 		return (error);
1617 
1618 	if (SCARG(uap, osv)) {
1619 		sunos32_sigvec_from_sigaction(&sv, &osa);
1620 		error = copyout(&sv, (caddr_t)(u_long)SCARG(uap, osv), sizeof(sv));
1621 		if (error != 0)
1622 			return (error);
1623 	}
1624 
1625 	return (0);
1626 }
1627