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