xref: /netbsd/sys/compat/sunos32/sunos32_misc.c (revision 6550d01e)
1 /*	$NetBSD: sunos32_misc.c,v 1.73 2010/11/19 06:44:38 dholland 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.73 2010/11/19 06:44:38 dholland 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
158 void sunos32_sigvec_from_sigaction(sv, sa)
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, vfs_getopsbyname("nfs"), NULL,
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, vfs_getopsbyname(fsname), NULL,
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_data;
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 			panic("sunos_getdents");
664 		if (cookie && (*cookie >> 32) != 0) {
665 			compat_offseterr(vp, "sunos_getdents");
666 			error = EINVAL;
667 			goto out;
668 		}
669 		if (bdp->d_fileno == 0) {
670 			inp += reclen;	/* it is a hole; squish it out */
671 			if (cookie)
672 				off = *cookie++;
673 			else
674 				off += reclen;
675 			continue;
676 		}
677 		sunos_reclen = SUNOS32_RECLEN(&idb, bdp->d_namlen);
678 		if (reclen > len || resid < sunos_reclen) {
679 			/* entry too big for buffer, so just stop */
680 			outp++;
681 			break;
682 		}
683 		if (cookie)
684 			off = *cookie++;	/* each entry points to next */
685 		else
686 			off += reclen;
687 		/*
688 		 * Massage in place to make a Sun-shaped dirent (otherwise
689 		 * we have to worry about touching user memory outside of
690 		 * the copyout() call).
691 		 */
692 		idb.d_fileno = bdp->d_fileno;
693 		idb.d_off = off;
694 		idb.d_reclen = sunos_reclen;
695 		idb.d_namlen = bdp->d_namlen;
696 		strlcpy(idb.d_name, bdp->d_name, sizeof(idb.d_name));
697 		if ((error = copyout((void *)&idb, outp, sunos_reclen)) != 0)
698 			goto out;
699 		/* advance past this real entry */
700 		inp += reclen;
701 		/* advance output past Sun-shaped entry */
702 		outp += sunos_reclen;
703 		resid -= sunos_reclen;
704 	}
705 
706 	/* if we squished out the whole block, try again */
707 	if (outp == SCARG_P32(uap, buf)) {
708 		if (cookiebuf)
709 			free(cookiebuf, M_TEMP);
710 		cookiebuf = NULL;
711 		goto again;
712 	}
713 	fp->f_offset = off;		/* update the vnode offset */
714 
715 eof:
716 	*retval = SCARG(uap, nbytes) - resid;
717 out:
718 	VOP_UNLOCK(vp);
719 	free(cookiebuf, M_TEMP);
720 	free(sbuf, M_TEMP);
721  out1:
722  	fd_putfile(SCARG(uap, fd));
723 	return (error);
724 }
725 
726 #define	SUNOS32__MAP_NEW	0x80000000	/* if not, old mmap & cannot handle */
727 
728 int
729 sunos32_sys_mmap(struct lwp *l, const struct sunos32_sys_mmap_args *uap, register_t *retval)
730 {
731 	/* {
732 		syscallarg(netbsd32_voidp) addr;
733 		syscallarg(netbsd32_size_t) len;
734 		syscallarg(int) prot;
735 		syscallarg(int) flags;
736 		syscallarg(int) fd;
737 		syscallarg(netbsd32_long) pos;
738 	} */
739 	struct sys_mmap_args ua;
740 	int error;
741 
742 	/*
743 	 * Verify the arguments.
744 	 */
745 	if (SCARG(uap, prot) & ~(PROT_READ|PROT_WRITE|PROT_EXEC))
746 		return (EINVAL);			/* XXX still needed? */
747 
748 	if ((SCARG(uap, flags) & SUNOS32__MAP_NEW) == 0)
749 		return (EINVAL);
750 
751 	SUNOS32TOP_UAP(addr, void);
752 	SUNOS32TOX_UAP(len, size_t);
753 	SUNOS32TO64_UAP(prot);
754 	SCARG(&ua, flags) = SCARG(uap, flags) & ~SUNOS32__MAP_NEW;
755 	SUNOS32TO64_UAP(fd);
756 	SCARG(&ua, PAD) = 0;
757 	SUNOS32TOX_UAP(pos, off_t);
758 
759 	error = sys_mmap(l, &ua, retval);
760 	if ((u_long)*retval > (u_long)UINT_MAX) {
761 		printf("sunos32_mmap: retval out of range: 0x%lx",
762 		       (u_long)*retval);
763 		/* Should try to recover and return an error here. */
764 	}
765 	return (error);
766 }
767 
768 #define	MC_SYNC		1
769 #define	MC_LOCK		2
770 #define	MC_UNLOCK	3
771 #define	MC_ADVISE	4
772 #define	MC_LOCKAS	5
773 #define	MC_UNLOCKAS	6
774 
775 int
776 sunos32_sys_mctl(struct lwp *l, const struct sunos32_sys_mctl_args *uap, register_t *retval)
777 {
778 	/* {
779 		syscallarg(netbsd32_voidp) addr;
780 		syscallarg(int) len;
781 		syscallarg(int) func;
782 		syscallarg(netbsd32_voidp) arg;
783 	} */
784 
785 	switch (SCARG(uap, func)) {
786 	case MC_ADVISE:		/* ignore for now */
787 		return (0);
788 	case MC_SYNC:		/* translate to msync */
789 		return (netbsd32___msync13(l, (const void *)uap, retval));
790 	default:
791 		return (EINVAL);
792 	}
793 }
794 
795 int
796 sunos32_sys_setsockopt(struct lwp *l, const struct sunos32_sys_setsockopt_args *uap, register_t *retval)
797 {
798 	/* {
799 		syscallarg(int) s;
800 		syscallarg(int) level;
801 		syscallarg(int) name;
802 		syscallarg(netbsd32_caddr_t) val;
803 		syscallarg(int) valsize;
804 	} */
805 	struct sockopt sopt;
806 	struct socket *so;
807 	int name = SCARG(uap, name);
808 	int error;
809 
810 	/* fd_getsock() will use the descriptor for us */
811 	if ((error = fd_getsock(SCARG(uap, s), &so)) != 0)
812 		return (error);
813 #define	SO_DONTLINGER (~SO_LINGER)
814 	if (name == SO_DONTLINGER) {
815 		struct linger lg;
816 
817 		lg.l_onoff = 0;
818 		error = so_setsockopt(l, so, SCARG(uap, level), SO_LINGER,
819 		    &lg, sizeof(lg));
820 		goto out;
821 	}
822 	if (SCARG(uap, level) == IPPROTO_IP) {
823 #define		SUNOS_IP_MULTICAST_IF		2
824 #define		SUNOS_IP_MULTICAST_TTL		3
825 #define		SUNOS_IP_MULTICAST_LOOP		4
826 #define		SUNOS_IP_ADD_MEMBERSHIP		5
827 #define		SUNOS_IP_DROP_MEMBERSHIP	6
828 		static const int ipoptxlat[] = {
829 			IP_MULTICAST_IF,
830 			IP_MULTICAST_TTL,
831 			IP_MULTICAST_LOOP,
832 			IP_ADD_MEMBERSHIP,
833 			IP_DROP_MEMBERSHIP
834 		};
835 		if (name >= SUNOS_IP_MULTICAST_IF &&
836 		    name <= SUNOS_IP_DROP_MEMBERSHIP) {
837 			name = ipoptxlat[name - SUNOS_IP_MULTICAST_IF];
838 		}
839 	}
840 	if (SCARG(uap, valsize) > MLEN) {
841 		error = EINVAL;
842 		goto out;
843 	}
844 	sockopt_init(&sopt, SCARG(uap, level), name, SCARG(uap, valsize));
845 	if (SCARG_P32(uap, val)) {
846 		error = copyin(SCARG_P32(uap, val), sopt.sopt_data,
847 		    (u_int)SCARG(uap, valsize));
848 	}
849 	if (error == 0)
850 		error = sosetopt(so, &sopt);
851 	sockopt_destroy(&sopt);
852  out:
853  	fd_putfile(SCARG(uap, s));
854 	return (error);
855 }
856 
857 static inline int sunos32_sys_socket_common(struct lwp *, register_t *,
858 					      int type);
859 static inline int
860 sunos32_sys_socket_common(struct lwp *l, register_t *retval, int type)
861 {
862 	struct socket *so;
863 	int error, fd;
864 
865 	/* fd_getsock() will use the descriptor for us */
866 	fd = (int)*retval;
867 	if ((error = fd_getsock(fd, &so)) == 0) {
868 		if (type == SOCK_DGRAM)
869 			so->so_options |= SO_BROADCAST;
870 		fd_putfile(fd);
871 	}
872 	return (error);
873 }
874 
875 int
876 sunos32_sys_socket(struct lwp *l, const struct sunos32_sys_socket_args *uap, register_t *retval)
877 {
878 	/* {
879 		syscallarg(int) domain;
880 		syscallarg(int) type;
881 		syscallarg(int) protocol;
882 	} */
883 	int error;
884 
885 	error = netbsd32___socket30(l, (const void *)uap, retval);
886 	if (error)
887 		return (error);
888 	return sunos32_sys_socket_common(l, retval, SCARG(uap, type));
889 }
890 
891 int
892 sunos32_sys_socketpair(struct lwp *l, const struct sunos32_sys_socketpair_args *uap, register_t *retval)
893 {
894 	/* {
895 		syscallarg(int) domain;
896 		syscallarg(int) type;
897 		syscallarg(int) protocol;
898 		syscallarg(int *) rsv;
899 	} */
900 	int error;
901 
902 	error = netbsd32_socketpair(l, (const void *)uap, retval);
903 	if (error)
904 		return (error);
905 	return sunos32_sys_socket_common(l, retval, SCARG(uap, type));
906 }
907 
908 
909 /*
910  * XXX: This needs cleaning up.
911  */
912 int
913 sunos32_sys_auditsys(struct lwp *l, const struct sunos32_sys_auditsys_args *uap, register_t *retval)
914 {
915 	return 0;
916 }
917 
918 int
919 sunos32_sys_uname(struct lwp *l, const struct sunos32_sys_uname_args *uap, register_t *retval)
920 {
921 	/* {
922 		syscallarg(sunos32_utsnamep_t) name;
923 	} */
924 	struct sunos_utsname sut;
925 
926 	memset(&sut, 0, sizeof(sut));
927 
928 	memcpy(sut.sysname, ostype, sizeof(sut.sysname) - 1);
929 	memcpy(sut.nodename, hostname, sizeof(sut.nodename));
930 	sut.nodename[sizeof(sut.nodename)-1] = '\0';
931 	memcpy(sut.release, osrelease, sizeof(sut.release) - 1);
932 	memcpy(sut.version, "1", sizeof(sut.version) - 1);
933 	memcpy(sut.machine, machine, sizeof(sut.machine) - 1);
934 
935 	return copyout((void *)&sut, SCARG_P32(uap, name),
936 	    sizeof(struct sunos_utsname));
937 }
938 
939 int
940 sunos32_sys_setpgrp(struct lwp *l, const struct sunos32_sys_setpgrp_args *uap, register_t *retval)
941 {
942 	/* {
943 		syscallarg(int) pid;
944 		syscallarg(int) pgid;
945 	} */
946 	struct proc *p = l->l_proc;
947 
948 	/*
949 	 * difference to our setpgid call is to include backwards
950 	 * compatibility to pre-setsid() binaries. Do setsid()
951 	 * instead of setpgid() in those cases where the process
952 	 * tries to create a new session the old way.
953 	 */
954 	if (!SCARG(uap, pgid) &&
955 	    (!SCARG(uap, pid) || SCARG(uap, pid) == p->p_pid))
956 		return sys_setsid(l, NULL, retval);
957 	else
958 		return netbsd32_setpgid(l, (const void *)uap, retval);
959 }
960 
961 int
962 sunos32_sys_open(struct lwp *l, const struct sunos32_sys_open_args *uap, register_t *retval)
963 {
964 	/* {
965 		syscallarg(const netbsd32_charp) path;
966 		syscallarg(int) flags;
967 		syscallarg(int) mode;
968 	} */
969 	struct proc *p = l->l_proc;
970 	struct sys_open_args ua;
971 	int lf, r;
972 	int noctty;
973 	int ret;
974 
975 	/* convert mode into NetBSD mode */
976 	lf = SCARG(uap, flags);
977 	noctty = lf & 0x8000;
978 	r =	(lf & (0x0001 | 0x0002 | 0x0008 | 0x0040 | 0x0200 | 0x0400 | 0x0800));
979 	r |=	((lf & (0x0004 | 0x1000 | 0x4000)) ? O_NONBLOCK : 0);
980 	r |=	((lf & 0x0080) ? O_SHLOCK : 0);
981 	r |=	((lf & 0x0100) ? O_EXLOCK : 0);
982 	r |=	((lf & 0x2000) ? O_FSYNC : 0);
983 
984 	SUNOS32TOP_UAP(path, const char);
985 	SCARG(&ua, flags) = r;
986 	SUNOS32TO64_UAP(mode);
987 
988 	ret = sys_open(l, &ua, retval);
989 
990 	/* XXXSMP unlocked */
991 	if (!ret && !noctty && SESS_LEADER(p) && !(p->p_lflag & PL_CONTROLT)) {
992 		file_t *fp;
993 		int fd;
994 
995 		fd = (int)*retval;
996 		fp = fd_getfile(fd);
997 
998 		/* ignore any error, just give it a try */
999 		if (fp != NULL) {
1000 			if (fp->f_type == DTYPE_VNODE)
1001 				(fp->f_ops->fo_ioctl)(fp, TIOCSCTTY, NULL);
1002 			fd_putfile(fd);
1003 		}
1004 	}
1005 	return ret;
1006 }
1007 
1008 int
1009 sunos32_sys_ustat(struct lwp *l, const struct sunos32_sys_ustat_args *uap, register_t *retval)
1010 {
1011 	/* {
1012 		syscallarg(int) dev;
1013 		syscallarg(sunos32_ustatp_t) buf;
1014 	} */
1015 	struct sunos_ustat us;
1016 	int error;
1017 
1018 	memset(&us, 0, sizeof us);
1019 
1020 	/*
1021 	 * XXX: should set f_tfree and f_tinode at least
1022 	 * How do we translate dev -> fstat? (and then to sunos_ustat)
1023 	 */
1024 
1025 	if ((error = copyout(&us, SCARG_P32(uap, buf), sizeof us)) != 0)
1026 		return (error);
1027 	return 0;
1028 }
1029 
1030 int
1031 sunos32_sys_quotactl(struct lwp *l, const struct sunos32_sys_quotactl_args *uap, register_t *retval)
1032 {
1033 
1034 	return EINVAL;
1035 }
1036 
1037 int
1038 sunos32_sys_vhangup(struct lwp *l, const void *v, register_t *retval)
1039 {
1040 	struct proc *p = l->l_proc;
1041 	struct session *sp = p->p_session;
1042 
1043 	if (sp->s_ttyvp == 0)
1044 		return 0;
1045 
1046 	if (sp->s_ttyp && sp->s_ttyp->t_session == sp && sp->s_ttyp->t_pgrp)
1047 		pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
1048 
1049 	(void) ttywait(sp->s_ttyp);
1050 	if (sp->s_ttyvp)
1051 		VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
1052 	if (sp->s_ttyvp)
1053 		vrele(sp->s_ttyvp);
1054 	sp->s_ttyvp = NULL;
1055 
1056 	return 0;
1057 }
1058 
1059 static int
1060 sunstatfs(struct statvfs *sp, void *sbuf)
1061 {
1062 	struct sunos_statfs ssfs;
1063 
1064 	memset(&ssfs, 0, sizeof ssfs);
1065 	ssfs.f_type = 0;
1066 	ssfs.f_bsize = sp->f_bsize;
1067 	ssfs.f_blocks = sp->f_blocks;
1068 	ssfs.f_bfree = sp->f_bfree;
1069 	ssfs.f_bavail = sp->f_bavail;
1070 	ssfs.f_files = sp->f_files;
1071 	ssfs.f_ffree = sp->f_ffree;
1072 	ssfs.f_fsid = sp->f_fsidx;
1073 	return copyout((void *)&ssfs, sbuf, sizeof ssfs);
1074 }
1075 
1076 int
1077 sunos32_sys_statfs(struct lwp *l, const struct sunos32_sys_statfs_args *uap, register_t *retval)
1078 {
1079 	/* {
1080 		syscallarg(const netbsd32_charp) path;
1081 		syscallarg(sunos32_statfsp_t) buf;
1082 	} */
1083 	struct mount *mp;
1084 	struct statvfs *sp;
1085 	int error;
1086 	struct vnode *vp;
1087 	struct sys_statvfs1_args ua;
1088 
1089 	SUNOS32TOP_UAP(path, const char);
1090 
1091 	error = namei_simple_user(SCARG(&ua, path),
1092 				NSM_FOLLOW_TRYEMULROOT, &vp);
1093 	if (error != 0)
1094 		return (error);
1095 	mp = vp->v_mount;
1096 	sp = &mp->mnt_stat;
1097 	vrele(vp);
1098 	if ((error = VFS_STATVFS(mp, sp)) != 0)
1099 		return (error);
1100 	sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
1101 	return sunstatfs(sp, SCARG_P32(uap, buf));
1102 }
1103 
1104 int
1105 sunos32_sys_fstatfs(struct lwp *l, const struct sunos32_sys_fstatfs_args *uap, register_t *retval)
1106 {
1107 	/* {
1108 		syscallarg(int) fd;
1109 		syscallarg(sunos32_statfsp_t) buf;
1110 	} */
1111 	file_t *fp;
1112 	struct mount *mp;
1113 	struct statvfs *sp;
1114 	int error;
1115 
1116 	/* fd_getvnode() will use the descriptor for us */
1117 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
1118 		return (error);
1119 	mp = ((struct vnode *)fp->f_data)->v_mount;
1120 	sp = &mp->mnt_stat;
1121 	if ((error = VFS_STATVFS(mp, sp)) != 0)
1122 		goto out;
1123 	sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
1124 	error = sunstatfs(sp, SCARG_P32(uap, buf));
1125  out:
1126  	fd_putfile(SCARG(uap, fd));
1127 	return (error);
1128 }
1129 
1130 int
1131 sunos32_sys_exportfs(struct lwp *l, const struct sunos32_sys_exportfs_args *uap, register_t *retval)
1132 {
1133 	/*
1134 	 * XXX: should perhaps translate into a mount(2)
1135 	 * with MOUNT_EXPORT?
1136 	 */
1137 	return 0;
1138 }
1139 
1140 int
1141 sunos32_sys_mknod(struct lwp *l, const struct sunos32_sys_mknod_args *uap, register_t *retval)
1142 {
1143 	/* {
1144 		syscallarg(const netbsd32_charp) path;
1145 		syscallarg(int) mode;
1146 		syscallarg(int) dev;
1147 	} */
1148 
1149 	if (S_ISFIFO(SCARG(uap, mode)))
1150 		return netbsd32_mkfifo(l, (const struct netbsd32_mkfifo_args *)uap, retval);
1151 
1152 	return compat_50_netbsd32_mknod(l, (const struct compat_50_netbsd32_mknod_args *)uap, retval);
1153 }
1154 
1155 #define SUNOS_SC_ARG_MAX	1
1156 #define SUNOS_SC_CHILD_MAX	2
1157 #define SUNOS_SC_CLK_TCK	3
1158 #define SUNOS_SC_NGROUPS_MAX	4
1159 #define SUNOS_SC_OPEN_MAX	5
1160 #define SUNOS_SC_JOB_CONTROL	6
1161 #define SUNOS_SC_SAVED_IDS	7
1162 #define SUNOS_SC_VERSION	8
1163 
1164 int
1165 sunos32_sys_sysconf(struct lwp *l, const struct sunos32_sys_sysconf_args *uap, register_t *retval)
1166 {
1167 	/* {
1168 		syscallarg(int) name;
1169 	} */
1170 	extern u_int maxfiles;
1171 
1172 	switch(SCARG(uap, name)) {
1173 	case SUNOS_SC_ARG_MAX:
1174 		*retval = ARG_MAX;
1175 		break;
1176 	case SUNOS_SC_CHILD_MAX:
1177 		*retval = maxproc;
1178 		break;
1179 	case SUNOS_SC_CLK_TCK:
1180 		*retval = 60;		/* should this be `hz', ie. 100? */
1181 		break;
1182 	case SUNOS_SC_NGROUPS_MAX:
1183 		*retval = NGROUPS_MAX;
1184 		break;
1185 	case SUNOS_SC_OPEN_MAX:
1186 		*retval = maxfiles;
1187 		break;
1188 	case SUNOS_SC_JOB_CONTROL:
1189 		*retval = 1;
1190 		break;
1191 	case SUNOS_SC_SAVED_IDS:
1192 #ifdef _POSIX_SAVED_IDS
1193 		*retval = 1;
1194 #else
1195 		*retval = 0;
1196 #endif
1197 		break;
1198 	case SUNOS_SC_VERSION:
1199 		*retval = 198808;
1200 		break;
1201 	default:
1202 		return EINVAL;
1203 	}
1204 	return 0;
1205 }
1206 
1207 #define SUNOS_RLIMIT_NOFILE	6	/* Other RLIMIT_* are the same */
1208 #define SUNOS_RLIM_NLIMITS	7
1209 
1210 int
1211 sunos32_sys_getrlimit(struct lwp *l, const struct sunos32_sys_getrlimit_args *uap, register_t *retval)
1212 {
1213 	/* {
1214 		syscallarg(u_int) which;
1215 		syscallarg(netbsd32_orlimitp_t) rlp;
1216 	} */
1217 	struct compat_43_netbsd32_ogetrlimit_args ua_43;
1218 
1219 	if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
1220 		return EINVAL;
1221 
1222 	SCARG(&ua_43, which) = SCARG(uap, which) == SUNOS_RLIMIT_NOFILE ? RLIMIT_NOFILE : SCARG(uap, which);
1223 	SCARG(&ua_43, rlp) = SCARG(uap, rlp);
1224 
1225 	return compat_43_netbsd32_ogetrlimit(l, &ua_43, retval);
1226 }
1227 
1228 int
1229 sunos32_sys_setrlimit(struct lwp *l, const struct sunos32_sys_setrlimit_args *uap, register_t *retval)
1230 {
1231 	/* {
1232 		syscallarg(u_int) which;
1233 		syscallarg(netbsd32_orlimitp_t) rlp;
1234 	} */
1235 	struct compat_43_netbsd32_osetrlimit_args ua_43;
1236 
1237 	if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
1238 		return EINVAL;
1239 
1240 	SCARG(&ua_43, which) = SCARG(uap, which) == SUNOS_RLIMIT_NOFILE ? RLIMIT_NOFILE : SCARG(uap, which);
1241 	SCARG(&ua_43, rlp) = SCARG(uap, rlp);
1242 
1243 	return compat_43_netbsd32_osetrlimit(l, &ua_43, retval);
1244 }
1245 
1246 /* for the m68k machines */
1247 #ifndef PT_GETFPREGS
1248 #define PT_GETFPREGS -1
1249 #endif
1250 #ifndef PT_SETFPREGS
1251 #define PT_SETFPREGS -1
1252 #endif
1253 
1254 static const int sreq2breq[] = {
1255 	PT_TRACE_ME,    PT_READ_I,      PT_READ_D,      -1,
1256 	PT_WRITE_I,     PT_WRITE_D,     -1,             PT_CONTINUE,
1257 	PT_KILL,        -1,             PT_ATTACH,      PT_DETACH,
1258 	PT_GETREGS,     PT_SETREGS,     PT_GETFPREGS,   PT_SETFPREGS
1259 };
1260 static const int nreqs = sizeof(sreq2breq) / sizeof(sreq2breq[0]);
1261 
1262 int
1263 sunos32_sys_ptrace(struct lwp *l, const struct sunos32_sys_ptrace_args *uap, register_t *retval)
1264 {
1265 	/* {
1266 		syscallarg(int) req;
1267 		syscallarg(pid_t) pid;
1268 		syscallarg(netbsd32_caddr_t) addr;
1269 		syscallarg(int) data;
1270 		syscallarg(netbsd32_charp) addr2;
1271 	} */
1272 	struct netbsd32_ptrace_args pa;
1273 	int req;
1274 
1275 #define sys_ptrace sysent[SYS_ptrace].sy_call
1276 	if (sys_ptrace == sys_nosys)
1277 		return ENOSYS;
1278 
1279 	req = SCARG(uap, req);
1280 	if ((unsigned int)req >= nreqs)
1281 		return (EINVAL);
1282 
1283 	req = sreq2breq[req];
1284 	if (req == -1)
1285 		return (EINVAL);
1286 
1287 	SCARG(&pa, req) = req;
1288 	SCARG(&pa, pid) = (pid_t)SCARG(uap, pid);
1289 	SCARG(&pa, addr) = SCARG(uap, addr);
1290 	SCARG(&pa, data) = SCARG(uap, data);
1291 
1292 	return netbsd32_ptrace(l, &pa, retval);
1293 }
1294 
1295 /*
1296  * SunOS reboot system call (for compatibility).
1297  * Sun lets you pass in a boot string which the PROM
1298  * saves and provides to the next boot program.
1299  */
1300 
1301 #define SUNOS_RB_ASKNAME	0x001
1302 #define SUNOS_RB_SINGLE 	0x002
1303 #define SUNOS_RB_NOSYNC		0x004
1304 #define SUNOS_RB_HALT		0x008
1305 #define SUNOS_RB_DUMP		0x080
1306 #define	SUNOS_RB_STRING		0x200
1307 
1308 static struct sunos_howto_conv {
1309 	int sun_howto;
1310 	int bsd_howto;
1311 } sunos_howto_conv[] = {
1312 	{ SUNOS_RB_ASKNAME,	RB_ASKNAME },
1313 	{ SUNOS_RB_SINGLE,	RB_SINGLE },
1314 	{ SUNOS_RB_NOSYNC,	RB_NOSYNC },
1315 	{ SUNOS_RB_HALT,	RB_HALT },
1316 	{ SUNOS_RB_DUMP,	RB_DUMP },
1317 	{ SUNOS_RB_STRING,	RB_STRING },
1318 	{ 0x000,		0 },
1319 };
1320 
1321 int
1322 sunos32_sys_reboot(struct lwp *l, const struct sunos32_sys_reboot_args *uap, register_t *retval)
1323 {
1324 	/* {
1325 		syscallarg(int) howto;
1326 		syscallarg(netbsd32_charp) bootstr;
1327 	} */
1328 	struct sys_reboot_args ua;
1329 	struct sunos_howto_conv *convp;
1330 	int error, bsd_howto, sun_howto;
1331 	char *bootstr;
1332 
1333 	if ((error = kauth_authorize_system(l->l_cred,
1334 	    KAUTH_SYSTEM_REBOOT, 0, NULL, NULL, NULL)) != 0)
1335 		return (error);
1336 
1337 	/*
1338 	 * Convert howto bits to BSD format.
1339 	 */
1340 	sun_howto = SCARG(uap, howto);
1341 	bsd_howto = 0;
1342 	convp = sunos_howto_conv;
1343 	while (convp->sun_howto) {
1344 		if (sun_howto & convp->sun_howto)
1345 			bsd_howto |= convp->bsd_howto;
1346 		convp++;
1347 	}
1348 
1349 	/*
1350 	 * Sun RB_STRING (Get user supplied bootstring.)
1351 	 * If the machine supports passing a string to the
1352 	 * next booted kernel.
1353 	 */
1354 	if (sun_howto & SUNOS_RB_STRING)
1355 		bootstr = SCARG_P32(uap, bootstr);
1356 	else
1357 		bootstr = NULL;
1358 
1359 	SCARG(&ua, opt) = bsd_howto;
1360 	SCARG(&ua, bootstr) = bootstr;
1361 	return (sys_reboot(l, &ua, retval));
1362 }
1363 
1364 /*
1365  * Generalized interface signal handler, 4.3-compatible.
1366  */
1367 /* ARGSUSED */
1368 int
1369 sunos32_sys_sigvec(struct lwp *l, const struct sunos32_sys_sigvec_args *uap, register_t *retval)
1370 {
1371 	/* {
1372 		syscallarg(int) signum;
1373 		syscallarg(struct sigvec *) nsv;
1374 		syscallarg(struct sigvec *) osv;
1375 	} */
1376 	struct netbsd32_sigvec sv;
1377 	struct sigaction nsa, osa;
1378 	int error;
1379 
1380 	if (SCARG_P32(uap, nsv)) {
1381 		error = copyin(SCARG_P32(uap, nsv), &sv, sizeof(sv));
1382 		if (error != 0)
1383 			return (error);
1384 
1385 		/*
1386 		 * SunOS uses the mask 0x0004 as SV_RESETHAND
1387 		 * meaning: `reset to SIG_DFL on delivery'.
1388 		 * We support only the bits in: 0xF
1389 		 * (those bits are the same as ours)
1390 		 */
1391 		if (sv.sv_flags & ~0xF)
1392 			return (EINVAL);
1393 
1394 		sunos32_sigvec_to_sigaction(&sv, &nsa);
1395 	}
1396 	error = sigaction1(l, SCARG(uap, signum),
1397 			   SCARG_P32(uap, nsv) ? &nsa : 0,
1398 			   SCARG_P32(uap, osv) ? &osa : 0,
1399 			   NULL, 0);
1400 	if (error != 0)
1401 		return (error);
1402 
1403 	if (SCARG_P32(uap, osv)) {
1404 		sunos32_sigvec_from_sigaction(&sv, &osa);
1405 		error = copyout(&sv, SCARG_P32(uap, osv), sizeof(sv));
1406 		if (error != 0)
1407 			return (error);
1408 	}
1409 
1410 	return (0);
1411 }
1412