xref: /netbsd-src/sys/compat/sunos32/sunos32_misc.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: sunos32_misc.c,v 1.72 2010/06/24 13:03:07 hannken 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.72 2010/06/24 13:03:07 hannken 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 nameidata nd;
302 	int ndflags;
303 	const char *path;
304 
305 	path = SCARG_P32(uap, path);
306 
307 	ndflags = NOFOLLOW | LOCKLEAF | LOCKPARENT | TRYEMULROOT;
308 again:
309 	NDINIT(&nd, LOOKUP, ndflags, UIO_USERSPACE, path);
310 	if ((error = namei(&nd))) {
311 		if (error == EISDIR && (ndflags & LOCKPARENT) != 0) {
312 			/*
313 			 * Should only happen on '/'. Retry without LOCKPARENT;
314 			 * this is safe since the vnode won't be a VLNK.
315 			 */
316 			ndflags &= ~LOCKPARENT;
317 			goto again;
318 		}
319 		return (error);
320 	}
321 	/*
322 	 * For symbolic links, always return the attributes of its
323 	 * containing directory, except for mode, size, and links.
324 	 */
325 	vp = nd.ni_vp;
326 	dvp = nd.ni_dvp;
327 	if (vp->v_type != VLNK) {
328 		if ((ndflags & LOCKPARENT) != 0) {
329 			if (dvp == vp)
330 				vrele(dvp);
331 			else
332 				vput(dvp);
333 		}
334 		error = vn_stat(vp, &sb);
335 		vput(vp);
336 		if (error)
337 			return (error);
338 	} else {
339 		error = vn_stat(dvp, &sb);
340 		vput(dvp);
341 		if (error) {
342 			vput(vp);
343 			return (error);
344 		}
345 		error = vn_stat(vp, &sb1);
346 		vput(vp);
347 		if (error)
348 			return (error);
349 		sb.st_mode &= ~S_IFDIR;
350 		sb.st_mode |= S_IFLNK;
351 		sb.st_nlink = sb1.st_nlink;
352 		sb.st_size = sb1.st_size;
353 		sb.st_blocks = sb1.st_blocks;
354 	}
355 	sunos32_from___stat13(&sb, &sb32);
356 	error = copyout((void *)&sb32, SCARG_P32(uap, ub), sizeof (sb32));
357 	return (error);
358 }
359 
360 static int
361 sunos32_execve_fetch_element(char * const *array, size_t index, char **value)
362 {
363 	int error;
364 	netbsd32_charp const *a32 = (void const *)array;
365 	netbsd32_charp e;
366 
367 	error = copyin(a32 + index, &e, sizeof(e));
368 	if (error)
369 		return error;
370 	*value = NETBSD32PTR64(e);
371 	return 0;
372 }
373 
374 int
375 sunos32_sys_execv(struct lwp *l, const struct sunos32_sys_execv_args *uap, register_t *retval)
376 {
377 	/* {
378 		syscallarg(const char *) path;
379 		syscallarg(char **) argv;
380 	} */
381 	const char *path = SCARG_P32(uap, path);
382 
383 	return execve1(l, path, SCARG_P32(uap, argp), NULL,
384 	    sunos32_execve_fetch_element);
385 }
386 
387 int
388 sunos32_sys_execve(struct lwp *l, const struct sunos32_sys_execve_args *uap, register_t *retval)
389 {
390 	/* {
391 		syscallarg(const char *) path;
392 		syscallarg(char **) argv;
393 		syscallarg(char **) envp;
394 	} */
395 	const char *path = SCARG_P32(uap, path);
396 
397 	return execve1(l, path, SCARG_P32(uap, argp),
398 	    SCARG_P32(uap, envp),
399 	    sunos32_execve_fetch_element);
400 }
401 
402 int
403 sunos32_sys_omsync(struct lwp *l, const struct sunos32_sys_omsync_args *uap, register_t *retval)
404 {
405 	/* {
406 		syscallarg(netbsd32_caddr_t) addr;
407 		syscallarg(netbsd32_size_t) len;
408 		syscallarg(int) flags;
409 	} */
410 	struct netbsd32___msync13_args ouap;
411 
412 	SCARG(&ouap, addr) = SCARG(uap, addr);
413 	SCARG(&ouap, len) = SCARG(uap, len);
414 	SCARG(&ouap, flags) = SCARG(uap, flags);
415 
416 	return (netbsd32___msync13(l, &ouap, retval));
417 }
418 
419 int
420 sunos32_sys_unmount(struct lwp *l, const struct sunos32_sys_unmount_args *uap, register_t *retval)
421 {
422 	/* {
423 		syscallarg(netbsd32_charp) path;
424 	} */
425 	struct sys_unmount_args ua;
426 
427 	SUNOS32TOP_UAP(path, const char);
428 	SCARG(&ua, flags) = 0;
429 
430 	return (sys_unmount(l, &ua, retval));
431 }
432 
433 /*
434  * Conversion table for SunOS NFS mount flags.
435  */
436 static struct {
437 	int	sun_flg;
438 	int	bsd_flg;
439 } sunnfs_flgtab[] = {
440 	{ SUNNFS_SOFT,		NFSMNT_SOFT },
441 	{ SUNNFS_WSIZE,		NFSMNT_WSIZE },
442 	{ SUNNFS_RSIZE,		NFSMNT_RSIZE },
443 	{ SUNNFS_TIMEO,		NFSMNT_TIMEO },
444 	{ SUNNFS_RETRANS,	NFSMNT_RETRANS },
445 	{ SUNNFS_HOSTNAME,	0 },			/* Ignored */
446 	{ SUNNFS_INT,		NFSMNT_INT },
447 	{ SUNNFS_NOAC,		0 },			/* Ignored */
448 	{ SUNNFS_ACREGMIN,	0 },			/* Ignored */
449 	{ SUNNFS_ACREGMAX,	0 },			/* Ignored */
450 	{ SUNNFS_ACDIRMIN,	0 },			/* Ignored */
451 	{ SUNNFS_ACDIRMAX,	0 },			/* Ignored */
452 	{ SUNNFS_SECURE,	0 },			/* Ignored */
453 	{ SUNNFS_NOCTO,		0 },			/* Ignored */
454 	{ SUNNFS_POSIX,		0 }			/* Ignored */
455 };
456 
457 int
458 sunos32_sys_mount(struct lwp *l, const struct sunos32_sys_mount_args *uap, register_t *retval)
459 {
460 	/* {
461 		syscallarg(netbsd32_charp) type;
462 		syscallarg(netbsd32_charp) path;
463 		syscallarg(int) flags;
464 		syscallarg(netbsd32_caddr_t) data;
465 	} */
466 	int oflags = SCARG(uap, flags), nflags, error;
467 	char fsname[MFSNAMELEN];
468 	register_t dummy;
469 
470 	if (oflags & (SUNM_NOSUB | SUNM_SYS5))
471 		return (EINVAL);
472 	if ((oflags & SUNM_NEWTYPE) == 0)
473 		return (EINVAL);
474 	nflags = 0;
475 	if (oflags & SUNM_RDONLY)
476 		nflags |= MNT_RDONLY;
477 	if (oflags & SUNM_NOSUID)
478 		nflags |= MNT_NOSUID;
479 	if (oflags & SUNM_REMOUNT)
480 		nflags |= MNT_UPDATE;
481 
482 	error = copyinstr(SCARG_P32(uap, type), fsname, sizeof fsname, NULL);
483 	if (error)
484 		return (error);
485 
486 	if (strncmp(fsname, "nfs", sizeof fsname) == 0) {
487 		struct sunos_nfs_args sna;
488 		struct nfs_args na;	/* XXX */
489 		int n;
490 
491 		error = copyin(SCARG_P32(uap, data), &sna, sizeof sna);
492 		if (error)
493 			return (error);
494 		/* sa.sa_len = sizeof(sain); */
495 		na.version = NFS_ARGSVERSION;
496 		na.addr = (void *)sna.addr;
497 		na.addrlen = sizeof(struct sockaddr);
498 		na.sotype = SOCK_DGRAM;
499 		na.proto = IPPROTO_UDP;
500 		na.fh = sna.fh;
501 		na.fhsize = NFSX_V2FH;
502 		na.flags = 0;
503 		n = sizeof(sunnfs_flgtab) / sizeof(sunnfs_flgtab[0]);
504 		while (--n >= 0)
505 			if (sna.flags & sunnfs_flgtab[n].sun_flg)
506 				na.flags |= sunnfs_flgtab[n].bsd_flg;
507 		na.wsize = sna.wsize;
508 		na.rsize = sna.rsize;
509 		if (na.flags & NFSMNT_RSIZE) {
510 			na.flags |= NFSMNT_READDIRSIZE;
511 			na.readdirsize = na.rsize;
512 		}
513 		na.timeo = sna.timeo;
514 		na.retrans = sna.retrans;
515 		na.hostname = (char *)(u_long)sna.hostname;
516 
517 		return do_sys_mount(l, vfs_getopsbyname("nfs"), NULL,
518 		    SCARG_P32(uap, path), nflags, &na, UIO_SYSSPACE, sizeof na,
519 		    &dummy);
520 	}
521 
522 	if (strcmp(fsname, "4.2") == 0)
523 		strcpy(fsname, "ffs");
524 
525 	return do_sys_mount(l, vfs_getopsbyname(fsname), NULL,
526 	    SCARG_P32(uap, path), nflags, SCARG_P32(uap, data), UIO_USERSPACE,
527 	    0, &dummy);
528 }
529 
530 void	native_to_sunos_sigset(const sigset_t *, int *);
531 void	sunos_to_native_sigset(const int, sigset_t *);
532 
533 inline void
534 native_to_sunos_sigset(const sigset_t *ss, int *mask)
535 {
536 	*mask = ss->__bits[0];
537 }
538 
539 inline void
540 sunos_to_native_sigset(const int mask, sigset_t *ss)
541 {
542 
543 	ss->__bits[0] = mask;
544 	ss->__bits[1] = 0;
545 	ss->__bits[2] = 0;
546 	ss->__bits[3] = 0;
547 }
548 
549 int
550 sunos32_sys_sigpending(struct lwp *l, const struct sunos32_sys_sigpending_args *uap, register_t *retval)
551 {
552 	/* {
553 		syscallarg(netbsd32_intp) mask;
554 	} */
555 	sigset_t ss;
556 	int mask;
557 
558 	sigpending1(l, &ss);
559 	native_to_sunos_sigset(&ss, &mask);
560 
561 	return (copyout((void *)(u_long)&mask, SCARG_P32(uap, mask), sizeof(int)));
562 }
563 
564 int
565 sunos32_sys_sigsuspend(struct lwp *l, const struct sunos32_sys_sigsuspend_args *uap, register_t *retval)
566 {
567 	/* {
568 		syscallarg(int) mask;
569 	} */
570 	int mask;
571 	sigset_t ss;
572 
573 	mask = SCARG(uap, mask);
574 	sunos_to_native_sigset(mask, &ss);
575 	return (sigsuspend1(l, &ss));
576 }
577 
578 /*
579  * Read Sun-style directory entries.  We suck them into kernel space so
580  * that they can be massaged before being copied out to user code.  Like
581  * SunOS, we squish out `empty' entries.
582  *
583  * This is quite ugly, but what do you expect from compatibility code?
584  */
585 int
586 sunos32_sys_getdents(struct lwp *l, const struct sunos32_sys_getdents_args *uap, register_t *retval)
587 {
588 	/* {
589 		syscallarg(int) fd;
590 		syscallarg(netbsd32_charp) buf;
591 		syscallarg(int) nbytes;
592 	} */
593 	struct dirent *bdp;
594 	struct vnode *vp;
595 	char *inp, *sbuf;	/* BSD-format */
596 	int len, reclen;	/* BSD-format */
597 	char *outp;		/* Sun-format */
598 	int resid, sunos_reclen;/* Sun-format */
599 	struct file *fp;
600 	struct uio auio;
601 	struct iovec aiov;
602 	struct sunos32_dirent idb;
603 	off_t off;			/* true file offset */
604 	int buflen, error, eofflag;
605 	off_t *cookiebuf, *cookie;
606 	int ncookies;
607 
608 	/* fd_getvnode() will use the descriptor for us */
609 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
610 		return (error);
611 
612 	if ((fp->f_flag & FREAD) == 0) {
613 		error = EBADF;
614 		goto out1;
615 	}
616 
617 	vp = fp->f_data;
618 	if (vp->v_type != VDIR) {
619 		error = EINVAL;
620 		goto out1;
621 	}
622 
623 	buflen = min(MAXBSIZE, SCARG(uap, nbytes));
624 	sbuf = malloc(buflen, M_TEMP, M_WAITOK);
625 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
626 	off = fp->f_offset;
627 again:
628 	aiov.iov_base = sbuf;
629 	aiov.iov_len = buflen;
630 	auio.uio_iov = &aiov;
631 	auio.uio_iovcnt = 1;
632 	auio.uio_rw = UIO_READ;
633 	auio.uio_resid = buflen;
634 	auio.uio_offset = off;
635 	UIO_SETUP_SYSSPACE(&auio);
636 	/*
637 	 * First we read into the malloc'ed buffer, then
638 	 * we massage it into user space, one record at a time.
639 	 */
640 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
641 	    &ncookies);
642 	if (error)
643 		goto out;
644 
645 	inp = sbuf;
646 	outp = SCARG_P32(uap, buf);
647 	resid = SCARG(uap, nbytes);
648 	if ((len = buflen - auio.uio_resid) == 0)
649 		goto eof;
650 
651 	for (cookie = cookiebuf; len > 0; len -= reclen) {
652 		bdp = (struct dirent *)inp;
653 		reclen = bdp->d_reclen;
654 		if (reclen & 3)
655 			panic("sunos_getdents");
656 		if (cookie && (*cookie >> 32) != 0) {
657 			compat_offseterr(vp, "sunos_getdents");
658 			error = EINVAL;
659 			goto out;
660 		}
661 		if (bdp->d_fileno == 0) {
662 			inp += reclen;	/* it is a hole; squish it out */
663 			if (cookie)
664 				off = *cookie++;
665 			else
666 				off += reclen;
667 			continue;
668 		}
669 		sunos_reclen = SUNOS32_RECLEN(&idb, bdp->d_namlen);
670 		if (reclen > len || resid < sunos_reclen) {
671 			/* entry too big for buffer, so just stop */
672 			outp++;
673 			break;
674 		}
675 		if (cookie)
676 			off = *cookie++;	/* each entry points to next */
677 		else
678 			off += reclen;
679 		/*
680 		 * Massage in place to make a Sun-shaped dirent (otherwise
681 		 * we have to worry about touching user memory outside of
682 		 * the copyout() call).
683 		 */
684 		idb.d_fileno = bdp->d_fileno;
685 		idb.d_off = off;
686 		idb.d_reclen = sunos_reclen;
687 		idb.d_namlen = bdp->d_namlen;
688 		strlcpy(idb.d_name, bdp->d_name, sizeof(idb.d_name));
689 		if ((error = copyout((void *)&idb, outp, sunos_reclen)) != 0)
690 			goto out;
691 		/* advance past this real entry */
692 		inp += reclen;
693 		/* advance output past Sun-shaped entry */
694 		outp += sunos_reclen;
695 		resid -= sunos_reclen;
696 	}
697 
698 	/* if we squished out the whole block, try again */
699 	if (outp == SCARG_P32(uap, buf)) {
700 		if (cookiebuf)
701 			free(cookiebuf, M_TEMP);
702 		cookiebuf = NULL;
703 		goto again;
704 	}
705 	fp->f_offset = off;		/* update the vnode offset */
706 
707 eof:
708 	*retval = SCARG(uap, nbytes) - resid;
709 out:
710 	VOP_UNLOCK(vp);
711 	free(cookiebuf, M_TEMP);
712 	free(sbuf, M_TEMP);
713  out1:
714  	fd_putfile(SCARG(uap, fd));
715 	return (error);
716 }
717 
718 #define	SUNOS32__MAP_NEW	0x80000000	/* if not, old mmap & cannot handle */
719 
720 int
721 sunos32_sys_mmap(struct lwp *l, const struct sunos32_sys_mmap_args *uap, register_t *retval)
722 {
723 	/* {
724 		syscallarg(netbsd32_voidp) addr;
725 		syscallarg(netbsd32_size_t) len;
726 		syscallarg(int) prot;
727 		syscallarg(int) flags;
728 		syscallarg(int) fd;
729 		syscallarg(netbsd32_long) pos;
730 	} */
731 	struct sys_mmap_args ua;
732 	int error;
733 
734 	/*
735 	 * Verify the arguments.
736 	 */
737 	if (SCARG(uap, prot) & ~(PROT_READ|PROT_WRITE|PROT_EXEC))
738 		return (EINVAL);			/* XXX still needed? */
739 
740 	if ((SCARG(uap, flags) & SUNOS32__MAP_NEW) == 0)
741 		return (EINVAL);
742 
743 	SUNOS32TOP_UAP(addr, void);
744 	SUNOS32TOX_UAP(len, size_t);
745 	SUNOS32TO64_UAP(prot);
746 	SCARG(&ua, flags) = SCARG(uap, flags) & ~SUNOS32__MAP_NEW;
747 	SUNOS32TO64_UAP(fd);
748 	SCARG(&ua, PAD) = 0;
749 	SUNOS32TOX_UAP(pos, off_t);
750 
751 	error = sys_mmap(l, &ua, retval);
752 	if ((u_long)*retval > (u_long)UINT_MAX) {
753 		printf("sunos32_mmap: retval out of range: 0x%lx",
754 		       (u_long)*retval);
755 		/* Should try to recover and return an error here. */
756 	}
757 	return (error);
758 }
759 
760 #define	MC_SYNC		1
761 #define	MC_LOCK		2
762 #define	MC_UNLOCK	3
763 #define	MC_ADVISE	4
764 #define	MC_LOCKAS	5
765 #define	MC_UNLOCKAS	6
766 
767 int
768 sunos32_sys_mctl(struct lwp *l, const struct sunos32_sys_mctl_args *uap, register_t *retval)
769 {
770 	/* {
771 		syscallarg(netbsd32_voidp) addr;
772 		syscallarg(int) len;
773 		syscallarg(int) func;
774 		syscallarg(netbsd32_voidp) arg;
775 	} */
776 
777 	switch (SCARG(uap, func)) {
778 	case MC_ADVISE:		/* ignore for now */
779 		return (0);
780 	case MC_SYNC:		/* translate to msync */
781 		return (netbsd32___msync13(l, (const void *)uap, retval));
782 	default:
783 		return (EINVAL);
784 	}
785 }
786 
787 int
788 sunos32_sys_setsockopt(struct lwp *l, const struct sunos32_sys_setsockopt_args *uap, register_t *retval)
789 {
790 	/* {
791 		syscallarg(int) s;
792 		syscallarg(int) level;
793 		syscallarg(int) name;
794 		syscallarg(netbsd32_caddr_t) val;
795 		syscallarg(int) valsize;
796 	} */
797 	struct sockopt sopt;
798 	struct socket *so;
799 	int name = SCARG(uap, name);
800 	int error;
801 
802 	/* fd_getsock() will use the descriptor for us */
803 	if ((error = fd_getsock(SCARG(uap, s), &so)) != 0)
804 		return (error);
805 #define	SO_DONTLINGER (~SO_LINGER)
806 	if (name == SO_DONTLINGER) {
807 		struct linger lg;
808 
809 		lg.l_onoff = 0;
810 		error = so_setsockopt(l, so, SCARG(uap, level), SO_LINGER,
811 		    &lg, sizeof(lg));
812 		goto out;
813 	}
814 	if (SCARG(uap, level) == IPPROTO_IP) {
815 #define		SUNOS_IP_MULTICAST_IF		2
816 #define		SUNOS_IP_MULTICAST_TTL		3
817 #define		SUNOS_IP_MULTICAST_LOOP		4
818 #define		SUNOS_IP_ADD_MEMBERSHIP		5
819 #define		SUNOS_IP_DROP_MEMBERSHIP	6
820 		static const int ipoptxlat[] = {
821 			IP_MULTICAST_IF,
822 			IP_MULTICAST_TTL,
823 			IP_MULTICAST_LOOP,
824 			IP_ADD_MEMBERSHIP,
825 			IP_DROP_MEMBERSHIP
826 		};
827 		if (name >= SUNOS_IP_MULTICAST_IF &&
828 		    name <= SUNOS_IP_DROP_MEMBERSHIP) {
829 			name = ipoptxlat[name - SUNOS_IP_MULTICAST_IF];
830 		}
831 	}
832 	if (SCARG(uap, valsize) > MLEN) {
833 		error = EINVAL;
834 		goto out;
835 	}
836 	sockopt_init(&sopt, SCARG(uap, level), name, SCARG(uap, valsize));
837 	if (SCARG_P32(uap, val)) {
838 		error = copyin(SCARG_P32(uap, val), sopt.sopt_data,
839 		    (u_int)SCARG(uap, valsize));
840 	}
841 	if (error == 0)
842 		error = sosetopt(so, &sopt);
843 	sockopt_destroy(&sopt);
844  out:
845  	fd_putfile(SCARG(uap, s));
846 	return (error);
847 }
848 
849 static inline int sunos32_sys_socket_common(struct lwp *, register_t *,
850 					      int type);
851 static inline int
852 sunos32_sys_socket_common(struct lwp *l, register_t *retval, int type)
853 {
854 	struct socket *so;
855 	int error, fd;
856 
857 	/* fd_getsock() will use the descriptor for us */
858 	fd = (int)*retval;
859 	if ((error = fd_getsock(fd, &so)) == 0) {
860 		if (type == SOCK_DGRAM)
861 			so->so_options |= SO_BROADCAST;
862 		fd_putfile(fd);
863 	}
864 	return (error);
865 }
866 
867 int
868 sunos32_sys_socket(struct lwp *l, const struct sunos32_sys_socket_args *uap, register_t *retval)
869 {
870 	/* {
871 		syscallarg(int) domain;
872 		syscallarg(int) type;
873 		syscallarg(int) protocol;
874 	} */
875 	int error;
876 
877 	error = netbsd32___socket30(l, (const void *)uap, retval);
878 	if (error)
879 		return (error);
880 	return sunos32_sys_socket_common(l, retval, SCARG(uap, type));
881 }
882 
883 int
884 sunos32_sys_socketpair(struct lwp *l, const struct sunos32_sys_socketpair_args *uap, register_t *retval)
885 {
886 	/* {
887 		syscallarg(int) domain;
888 		syscallarg(int) type;
889 		syscallarg(int) protocol;
890 		syscallarg(int *) rsv;
891 	} */
892 	int error;
893 
894 	error = netbsd32_socketpair(l, (const void *)uap, retval);
895 	if (error)
896 		return (error);
897 	return sunos32_sys_socket_common(l, retval, SCARG(uap, type));
898 }
899 
900 
901 /*
902  * XXX: This needs cleaning up.
903  */
904 int
905 sunos32_sys_auditsys(struct lwp *l, const struct sunos32_sys_auditsys_args *uap, register_t *retval)
906 {
907 	return 0;
908 }
909 
910 int
911 sunos32_sys_uname(struct lwp *l, const struct sunos32_sys_uname_args *uap, register_t *retval)
912 {
913 	/* {
914 		syscallarg(sunos32_utsnamep_t) name;
915 	} */
916 	struct sunos_utsname sut;
917 
918 	memset(&sut, 0, sizeof(sut));
919 
920 	memcpy(sut.sysname, ostype, sizeof(sut.sysname) - 1);
921 	memcpy(sut.nodename, hostname, sizeof(sut.nodename));
922 	sut.nodename[sizeof(sut.nodename)-1] = '\0';
923 	memcpy(sut.release, osrelease, sizeof(sut.release) - 1);
924 	memcpy(sut.version, "1", sizeof(sut.version) - 1);
925 	memcpy(sut.machine, machine, sizeof(sut.machine) - 1);
926 
927 	return copyout((void *)&sut, SCARG_P32(uap, name),
928 	    sizeof(struct sunos_utsname));
929 }
930 
931 int
932 sunos32_sys_setpgrp(struct lwp *l, const struct sunos32_sys_setpgrp_args *uap, register_t *retval)
933 {
934 	/* {
935 		syscallarg(int) pid;
936 		syscallarg(int) pgid;
937 	} */
938 	struct proc *p = l->l_proc;
939 
940 	/*
941 	 * difference to our setpgid call is to include backwards
942 	 * compatibility to pre-setsid() binaries. Do setsid()
943 	 * instead of setpgid() in those cases where the process
944 	 * tries to create a new session the old way.
945 	 */
946 	if (!SCARG(uap, pgid) &&
947 	    (!SCARG(uap, pid) || SCARG(uap, pid) == p->p_pid))
948 		return sys_setsid(l, NULL, retval);
949 	else
950 		return netbsd32_setpgid(l, (const void *)uap, retval);
951 }
952 
953 int
954 sunos32_sys_open(struct lwp *l, const struct sunos32_sys_open_args *uap, register_t *retval)
955 {
956 	/* {
957 		syscallarg(const netbsd32_charp) path;
958 		syscallarg(int) flags;
959 		syscallarg(int) mode;
960 	} */
961 	struct proc *p = l->l_proc;
962 	struct sys_open_args ua;
963 	int lf, r;
964 	int noctty;
965 	int ret;
966 
967 	/* convert mode into NetBSD mode */
968 	lf = SCARG(uap, flags);
969 	noctty = lf & 0x8000;
970 	r =	(lf & (0x0001 | 0x0002 | 0x0008 | 0x0040 | 0x0200 | 0x0400 | 0x0800));
971 	r |=	((lf & (0x0004 | 0x1000 | 0x4000)) ? O_NONBLOCK : 0);
972 	r |=	((lf & 0x0080) ? O_SHLOCK : 0);
973 	r |=	((lf & 0x0100) ? O_EXLOCK : 0);
974 	r |=	((lf & 0x2000) ? O_FSYNC : 0);
975 
976 	SUNOS32TOP_UAP(path, const char);
977 	SCARG(&ua, flags) = r;
978 	SUNOS32TO64_UAP(mode);
979 
980 	ret = sys_open(l, &ua, retval);
981 
982 	/* XXXSMP unlocked */
983 	if (!ret && !noctty && SESS_LEADER(p) && !(p->p_lflag & PL_CONTROLT)) {
984 		file_t *fp;
985 		int fd;
986 
987 		fd = (int)*retval;
988 		fp = fd_getfile(fd);
989 
990 		/* ignore any error, just give it a try */
991 		if (fp != NULL) {
992 			if (fp->f_type == DTYPE_VNODE)
993 				(fp->f_ops->fo_ioctl)(fp, TIOCSCTTY, NULL);
994 			fd_putfile(fd);
995 		}
996 	}
997 	return ret;
998 }
999 
1000 int
1001 sunos32_sys_ustat(struct lwp *l, const struct sunos32_sys_ustat_args *uap, register_t *retval)
1002 {
1003 	/* {
1004 		syscallarg(int) dev;
1005 		syscallarg(sunos32_ustatp_t) buf;
1006 	} */
1007 	struct sunos_ustat us;
1008 	int error;
1009 
1010 	memset(&us, 0, sizeof us);
1011 
1012 	/*
1013 	 * XXX: should set f_tfree and f_tinode at least
1014 	 * How do we translate dev -> fstat? (and then to sunos_ustat)
1015 	 */
1016 
1017 	if ((error = copyout(&us, SCARG_P32(uap, buf), sizeof us)) != 0)
1018 		return (error);
1019 	return 0;
1020 }
1021 
1022 int
1023 sunos32_sys_quotactl(struct lwp *l, const struct sunos32_sys_quotactl_args *uap, register_t *retval)
1024 {
1025 
1026 	return EINVAL;
1027 }
1028 
1029 int
1030 sunos32_sys_vhangup(struct lwp *l, const void *v, register_t *retval)
1031 {
1032 	struct proc *p = l->l_proc;
1033 	struct session *sp = p->p_session;
1034 
1035 	if (sp->s_ttyvp == 0)
1036 		return 0;
1037 
1038 	if (sp->s_ttyp && sp->s_ttyp->t_session == sp && sp->s_ttyp->t_pgrp)
1039 		pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
1040 
1041 	(void) ttywait(sp->s_ttyp);
1042 	if (sp->s_ttyvp)
1043 		VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
1044 	if (sp->s_ttyvp)
1045 		vrele(sp->s_ttyvp);
1046 	sp->s_ttyvp = NULL;
1047 
1048 	return 0;
1049 }
1050 
1051 static int
1052 sunstatfs(struct statvfs *sp, void *sbuf)
1053 {
1054 	struct sunos_statfs ssfs;
1055 
1056 	memset(&ssfs, 0, sizeof ssfs);
1057 	ssfs.f_type = 0;
1058 	ssfs.f_bsize = sp->f_bsize;
1059 	ssfs.f_blocks = sp->f_blocks;
1060 	ssfs.f_bfree = sp->f_bfree;
1061 	ssfs.f_bavail = sp->f_bavail;
1062 	ssfs.f_files = sp->f_files;
1063 	ssfs.f_ffree = sp->f_ffree;
1064 	ssfs.f_fsid = sp->f_fsidx;
1065 	return copyout((void *)&ssfs, sbuf, sizeof ssfs);
1066 }
1067 
1068 int
1069 sunos32_sys_statfs(struct lwp *l, const struct sunos32_sys_statfs_args *uap, register_t *retval)
1070 {
1071 	/* {
1072 		syscallarg(const netbsd32_charp) path;
1073 		syscallarg(sunos32_statfsp_t) buf;
1074 	} */
1075 	struct mount *mp;
1076 	struct statvfs *sp;
1077 	int error;
1078 	struct vnode *vp;
1079 	struct sys_statvfs1_args ua;
1080 
1081 	SUNOS32TOP_UAP(path, const char);
1082 
1083 	error = namei_simple_user(SCARG(&ua, path),
1084 				NSM_FOLLOW_TRYEMULROOT, &vp);
1085 	if (error != 0)
1086 		return (error);
1087 	mp = vp->v_mount;
1088 	sp = &mp->mnt_stat;
1089 	vrele(vp);
1090 	if ((error = VFS_STATVFS(mp, sp)) != 0)
1091 		return (error);
1092 	sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
1093 	return sunstatfs(sp, SCARG_P32(uap, buf));
1094 }
1095 
1096 int
1097 sunos32_sys_fstatfs(struct lwp *l, const struct sunos32_sys_fstatfs_args *uap, register_t *retval)
1098 {
1099 	/* {
1100 		syscallarg(int) fd;
1101 		syscallarg(sunos32_statfsp_t) buf;
1102 	} */
1103 	file_t *fp;
1104 	struct mount *mp;
1105 	struct statvfs *sp;
1106 	int error;
1107 
1108 	/* fd_getvnode() will use the descriptor for us */
1109 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
1110 		return (error);
1111 	mp = ((struct vnode *)fp->f_data)->v_mount;
1112 	sp = &mp->mnt_stat;
1113 	if ((error = VFS_STATVFS(mp, sp)) != 0)
1114 		goto out;
1115 	sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
1116 	error = sunstatfs(sp, SCARG_P32(uap, buf));
1117  out:
1118  	fd_putfile(SCARG(uap, fd));
1119 	return (error);
1120 }
1121 
1122 int
1123 sunos32_sys_exportfs(struct lwp *l, const struct sunos32_sys_exportfs_args *uap, register_t *retval)
1124 {
1125 	/*
1126 	 * XXX: should perhaps translate into a mount(2)
1127 	 * with MOUNT_EXPORT?
1128 	 */
1129 	return 0;
1130 }
1131 
1132 int
1133 sunos32_sys_mknod(struct lwp *l, const struct sunos32_sys_mknod_args *uap, register_t *retval)
1134 {
1135 	/* {
1136 		syscallarg(const netbsd32_charp) path;
1137 		syscallarg(int) mode;
1138 		syscallarg(int) dev;
1139 	} */
1140 
1141 	if (S_ISFIFO(SCARG(uap, mode)))
1142 		return netbsd32_mkfifo(l, (const struct netbsd32_mkfifo_args *)uap, retval);
1143 
1144 	return compat_50_netbsd32_mknod(l, (const struct compat_50_netbsd32_mknod_args *)uap, retval);
1145 }
1146 
1147 #define SUNOS_SC_ARG_MAX	1
1148 #define SUNOS_SC_CHILD_MAX	2
1149 #define SUNOS_SC_CLK_TCK	3
1150 #define SUNOS_SC_NGROUPS_MAX	4
1151 #define SUNOS_SC_OPEN_MAX	5
1152 #define SUNOS_SC_JOB_CONTROL	6
1153 #define SUNOS_SC_SAVED_IDS	7
1154 #define SUNOS_SC_VERSION	8
1155 
1156 int
1157 sunos32_sys_sysconf(struct lwp *l, const struct sunos32_sys_sysconf_args *uap, register_t *retval)
1158 {
1159 	/* {
1160 		syscallarg(int) name;
1161 	} */
1162 	extern u_int maxfiles;
1163 
1164 	switch(SCARG(uap, name)) {
1165 	case SUNOS_SC_ARG_MAX:
1166 		*retval = ARG_MAX;
1167 		break;
1168 	case SUNOS_SC_CHILD_MAX:
1169 		*retval = maxproc;
1170 		break;
1171 	case SUNOS_SC_CLK_TCK:
1172 		*retval = 60;		/* should this be `hz', ie. 100? */
1173 		break;
1174 	case SUNOS_SC_NGROUPS_MAX:
1175 		*retval = NGROUPS_MAX;
1176 		break;
1177 	case SUNOS_SC_OPEN_MAX:
1178 		*retval = maxfiles;
1179 		break;
1180 	case SUNOS_SC_JOB_CONTROL:
1181 		*retval = 1;
1182 		break;
1183 	case SUNOS_SC_SAVED_IDS:
1184 #ifdef _POSIX_SAVED_IDS
1185 		*retval = 1;
1186 #else
1187 		*retval = 0;
1188 #endif
1189 		break;
1190 	case SUNOS_SC_VERSION:
1191 		*retval = 198808;
1192 		break;
1193 	default:
1194 		return EINVAL;
1195 	}
1196 	return 0;
1197 }
1198 
1199 #define SUNOS_RLIMIT_NOFILE	6	/* Other RLIMIT_* are the same */
1200 #define SUNOS_RLIM_NLIMITS	7
1201 
1202 int
1203 sunos32_sys_getrlimit(struct lwp *l, const struct sunos32_sys_getrlimit_args *uap, register_t *retval)
1204 {
1205 	/* {
1206 		syscallarg(u_int) which;
1207 		syscallarg(netbsd32_orlimitp_t) rlp;
1208 	} */
1209 	struct compat_43_netbsd32_ogetrlimit_args ua_43;
1210 
1211 	if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
1212 		return EINVAL;
1213 
1214 	SCARG(&ua_43, which) = SCARG(uap, which) == SUNOS_RLIMIT_NOFILE ? RLIMIT_NOFILE : SCARG(uap, which);
1215 	SCARG(&ua_43, rlp) = SCARG(uap, rlp);
1216 
1217 	return compat_43_netbsd32_ogetrlimit(l, &ua_43, retval);
1218 }
1219 
1220 int
1221 sunos32_sys_setrlimit(struct lwp *l, const struct sunos32_sys_setrlimit_args *uap, register_t *retval)
1222 {
1223 	/* {
1224 		syscallarg(u_int) which;
1225 		syscallarg(netbsd32_orlimitp_t) rlp;
1226 	} */
1227 	struct compat_43_netbsd32_osetrlimit_args ua_43;
1228 
1229 	if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
1230 		return EINVAL;
1231 
1232 	SCARG(&ua_43, which) = SCARG(uap, which) == SUNOS_RLIMIT_NOFILE ? RLIMIT_NOFILE : SCARG(uap, which);
1233 	SCARG(&ua_43, rlp) = SCARG(uap, rlp);
1234 
1235 	return compat_43_netbsd32_osetrlimit(l, &ua_43, retval);
1236 }
1237 
1238 /* for the m68k machines */
1239 #ifndef PT_GETFPREGS
1240 #define PT_GETFPREGS -1
1241 #endif
1242 #ifndef PT_SETFPREGS
1243 #define PT_SETFPREGS -1
1244 #endif
1245 
1246 static const int sreq2breq[] = {
1247 	PT_TRACE_ME,    PT_READ_I,      PT_READ_D,      -1,
1248 	PT_WRITE_I,     PT_WRITE_D,     -1,             PT_CONTINUE,
1249 	PT_KILL,        -1,             PT_ATTACH,      PT_DETACH,
1250 	PT_GETREGS,     PT_SETREGS,     PT_GETFPREGS,   PT_SETFPREGS
1251 };
1252 static const int nreqs = sizeof(sreq2breq) / sizeof(sreq2breq[0]);
1253 
1254 int
1255 sunos32_sys_ptrace(struct lwp *l, const struct sunos32_sys_ptrace_args *uap, register_t *retval)
1256 {
1257 	/* {
1258 		syscallarg(int) req;
1259 		syscallarg(pid_t) pid;
1260 		syscallarg(netbsd32_caddr_t) addr;
1261 		syscallarg(int) data;
1262 		syscallarg(netbsd32_charp) addr2;
1263 	} */
1264 	struct netbsd32_ptrace_args pa;
1265 	int req;
1266 
1267 #define sys_ptrace sysent[SYS_ptrace].sy_call
1268 	if (sys_ptrace == sys_nosys)
1269 		return ENOSYS;
1270 
1271 	req = SCARG(uap, req);
1272 	if ((unsigned int)req >= nreqs)
1273 		return (EINVAL);
1274 
1275 	req = sreq2breq[req];
1276 	if (req == -1)
1277 		return (EINVAL);
1278 
1279 	SCARG(&pa, req) = req;
1280 	SCARG(&pa, pid) = (pid_t)SCARG(uap, pid);
1281 	SCARG(&pa, addr) = SCARG(uap, addr);
1282 	SCARG(&pa, data) = SCARG(uap, data);
1283 
1284 	return netbsd32_ptrace(l, &pa, retval);
1285 }
1286 
1287 /*
1288  * SunOS reboot system call (for compatibility).
1289  * Sun lets you pass in a boot string which the PROM
1290  * saves and provides to the next boot program.
1291  */
1292 
1293 #define SUNOS_RB_ASKNAME	0x001
1294 #define SUNOS_RB_SINGLE 	0x002
1295 #define SUNOS_RB_NOSYNC		0x004
1296 #define SUNOS_RB_HALT		0x008
1297 #define SUNOS_RB_DUMP		0x080
1298 #define	SUNOS_RB_STRING		0x200
1299 
1300 static struct sunos_howto_conv {
1301 	int sun_howto;
1302 	int bsd_howto;
1303 } sunos_howto_conv[] = {
1304 	{ SUNOS_RB_ASKNAME,	RB_ASKNAME },
1305 	{ SUNOS_RB_SINGLE,	RB_SINGLE },
1306 	{ SUNOS_RB_NOSYNC,	RB_NOSYNC },
1307 	{ SUNOS_RB_HALT,	RB_HALT },
1308 	{ SUNOS_RB_DUMP,	RB_DUMP },
1309 	{ SUNOS_RB_STRING,	RB_STRING },
1310 	{ 0x000,		0 },
1311 };
1312 
1313 int
1314 sunos32_sys_reboot(struct lwp *l, const struct sunos32_sys_reboot_args *uap, register_t *retval)
1315 {
1316 	/* {
1317 		syscallarg(int) howto;
1318 		syscallarg(netbsd32_charp) bootstr;
1319 	} */
1320 	struct sys_reboot_args ua;
1321 	struct sunos_howto_conv *convp;
1322 	int error, bsd_howto, sun_howto;
1323 	char *bootstr;
1324 
1325 	if ((error = kauth_authorize_system(l->l_cred,
1326 	    KAUTH_SYSTEM_REBOOT, 0, NULL, NULL, NULL)) != 0)
1327 		return (error);
1328 
1329 	/*
1330 	 * Convert howto bits to BSD format.
1331 	 */
1332 	sun_howto = SCARG(uap, howto);
1333 	bsd_howto = 0;
1334 	convp = sunos_howto_conv;
1335 	while (convp->sun_howto) {
1336 		if (sun_howto & convp->sun_howto)
1337 			bsd_howto |= convp->bsd_howto;
1338 		convp++;
1339 	}
1340 
1341 	/*
1342 	 * Sun RB_STRING (Get user supplied bootstring.)
1343 	 * If the machine supports passing a string to the
1344 	 * next booted kernel.
1345 	 */
1346 	if (sun_howto & SUNOS_RB_STRING)
1347 		bootstr = SCARG_P32(uap, bootstr);
1348 	else
1349 		bootstr = NULL;
1350 
1351 	SCARG(&ua, opt) = bsd_howto;
1352 	SCARG(&ua, bootstr) = bootstr;
1353 	return (sys_reboot(l, &ua, retval));
1354 }
1355 
1356 /*
1357  * Generalized interface signal handler, 4.3-compatible.
1358  */
1359 /* ARGSUSED */
1360 int
1361 sunos32_sys_sigvec(struct lwp *l, const struct sunos32_sys_sigvec_args *uap, register_t *retval)
1362 {
1363 	/* {
1364 		syscallarg(int) signum;
1365 		syscallarg(struct sigvec *) nsv;
1366 		syscallarg(struct sigvec *) osv;
1367 	} */
1368 	struct netbsd32_sigvec sv;
1369 	struct sigaction nsa, osa;
1370 	int error;
1371 
1372 	if (SCARG_P32(uap, nsv)) {
1373 		error = copyin(SCARG_P32(uap, nsv), &sv, sizeof(sv));
1374 		if (error != 0)
1375 			return (error);
1376 
1377 		/*
1378 		 * SunOS uses the mask 0x0004 as SV_RESETHAND
1379 		 * meaning: `reset to SIG_DFL on delivery'.
1380 		 * We support only the bits in: 0xF
1381 		 * (those bits are the same as ours)
1382 		 */
1383 		if (sv.sv_flags & ~0xF)
1384 			return (EINVAL);
1385 
1386 		sunos32_sigvec_to_sigaction(&sv, &nsa);
1387 	}
1388 	error = sigaction1(l, SCARG(uap, signum),
1389 			   SCARG_P32(uap, nsv) ? &nsa : 0,
1390 			   SCARG_P32(uap, osv) ? &osa : 0,
1391 			   NULL, 0);
1392 	if (error != 0)
1393 		return (error);
1394 
1395 	if (SCARG_P32(uap, osv)) {
1396 		sunos32_sigvec_from_sigaction(&sv, &osa);
1397 		error = copyout(&sv, SCARG_P32(uap, osv), sizeof(sv));
1398 		if (error != 0)
1399 			return (error);
1400 	}
1401 
1402 	return (0);
1403 }
1404