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