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