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