xref: /netbsd-src/sys/compat/sunos32/sunos32_misc.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: sunos32_misc.c,v 1.55 2007/12/08 19:29:40 pooka 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.55 2007/12/08 19:29:40 pooka 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, void *v, register_t *retval)
176 {
177 	struct sunos32_sys_stime_args /* {
178 		syscallarg(sunos32_time_tp) tp;
179 	} */ *uap = v;
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, void *v, register_t *retval)
195 {
196 	struct sunos32_sys_wait4_args /* {
197 		syscallarg(int) pid;
198 		syscallarg(netbsd32_intp) status;
199 		syscallarg(int) options;
200 		syscallarg(netbsd32_rusagep_t) rusage;
201 	} */ *uap = v;
202 
203 	if (SCARG(uap, pid) == 0)
204 		SCARG(uap, pid) = WAIT_ANY;
205 	return (netbsd32_wait4(l, uap, retval));
206 }
207 
208 int
209 sunos32_sys_creat(struct lwp *l, void *v, register_t *retval)
210 {
211 	struct sunos32_sys_creat_args /* {
212 		syscallarg(const netbsd32_charp) path;
213 		syscallarg(int) mode;
214 	} */ *uap = v;
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, void *v, register_t *retval)
226 {
227 	struct sunos32_sys_access_args /* {
228 		syscallarg(const netbsd32_charp) path;
229 		syscallarg(int) flags;
230 	} */ *uap = v;
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, void *v, register_t *retval)
270 {
271 	struct sunos32_sys_stat_args /* {
272 		syscallarg(const netbsd32_charp) path;
273 		syscallarg(netbsd32_stat43p_t) ub;
274 	} */ *uap = v;
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(l, 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, void *v, register_t *retval)
292 {
293 	struct sunos32_sys_lstat_args /* {
294 		syscallarg(const netbsd32_charp) path;
295 		syscallarg(netbsd32_stat43p_t) ub;
296 	} */ *uap = v;
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, l);
335 		vput(vp);
336 		if (error)
337 			return (error);
338 	} else {
339 		error = vn_stat(dvp, &sb, l);
340 		vput(dvp);
341 		if (error) {
342 			vput(vp);
343 			return (error);
344 		}
345 		error = vn_stat(vp, &sb1, l);
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, void *v, register_t *retval)
376 {
377 	struct sunos32_sys_execv_args /* {
378 		syscallarg(const char *) path;
379 		syscallarg(char **) argv;
380 	} */ *uap = v;
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, void *v, register_t *retval)
389 {
390 	struct sunos32_sys_execve_args /* {
391 		syscallarg(const char *) path;
392 		syscallarg(char **) argv;
393 		syscallarg(char **) envp;
394 	} */ *uap = v;
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, void *v, register_t *retval)
404 {
405 	struct sunos32_sys_omsync_args /* {
406 		syscallarg(netbsd32_caddr_t) addr;
407 		syscallarg(netbsd32_size_t) len;
408 		syscallarg(int) flags;
409 	} */ *uap = v;
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, void *v, register_t *retval)
421 {
422 	struct sunos32_sys_unmount_args /* {
423 		syscallarg(netbsd32_charp) path;
424 	} */ *uap = v;
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, void *v, register_t *retval)
459 {
460 	struct sunos32_sys_mount_args /* {
461 		syscallarg(netbsd32_charp) type;
462 		syscallarg(netbsd32_charp) path;
463 		syscallarg(int) flags;
464 		syscallarg(netbsd32_caddr_t) data;
465 	} */ *uap = v;
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 #if defined(NFS)
531 int
532 async_daemon(struct lwp *l, void *v, register_t *retval)
533 {
534 	struct netbsd32_nfssvc_args ouap;
535 
536 	SCARG(&ouap, flag) = NFSSVC_BIOD;
537 	NETBSD32PTR32(SCARG(&ouap, argp), 0);
538 
539 	return (netbsd32_nfssvc(l, &ouap, retval));
540 }
541 #endif /* NFS */
542 
543 void	native_to_sunos_sigset(const sigset_t *, int *);
544 void	sunos_to_native_sigset(const int, sigset_t *);
545 
546 inline void
547 native_to_sunos_sigset(const sigset_t *ss, int *mask)
548 {
549 	*mask = ss->__bits[0];
550 }
551 
552 inline void
553 sunos_to_native_sigset(const int mask, sigset_t *ss)
554 {
555 
556 	ss->__bits[0] = mask;
557 	ss->__bits[1] = 0;
558 	ss->__bits[2] = 0;
559 	ss->__bits[3] = 0;
560 }
561 
562 int
563 sunos32_sys_sigpending(struct lwp *l, void *v, register_t *retval)
564 {
565 	struct sunos32_sys_sigpending_args /* {
566 		syscallarg(netbsd32_intp) mask;
567 	} */ *uap = v;
568 	sigset_t ss;
569 	int mask;
570 
571 	sigpending1(l, &ss);
572 	native_to_sunos_sigset(&ss, &mask);
573 
574 	return (copyout((void *)(u_long)&mask, SCARG_P32(uap, mask), sizeof(int)));
575 }
576 
577 int
578 sunos32_sys_sigsuspend(struct lwp *l, void *v, register_t *retval)
579 {
580 	struct sunos32_sys_sigsuspend_args /* {
581 		syscallarg(int) mask;
582 	} */ *uap = v;
583 	int mask;
584 	sigset_t ss;
585 
586 	mask = SCARG(uap, mask);
587 	sunos_to_native_sigset(mask, &ss);
588 	return (sigsuspend1(l, &ss));
589 }
590 
591 /*
592  * Read Sun-style directory entries.  We suck them into kernel space so
593  * that they can be massaged before being copied out to user code.  Like
594  * SunOS, we squish out `empty' entries.
595  *
596  * This is quite ugly, but what do you expect from compatibility code?
597  */
598 int
599 sunos32_sys_getdents(struct lwp *l, void *v, register_t *retval)
600 {
601 	struct sunos32_sys_getdents_args /* {
602 		syscallarg(int) fd;
603 		syscallarg(netbsd32_charp) buf;
604 		syscallarg(int) nbytes;
605 	} */ *uap = v;
606 	struct proc *p = l->l_proc;
607 	struct dirent *bdp;
608 	struct vnode *vp;
609 	char *inp, *sbuf;	/* BSD-format */
610 	int len, reclen;	/* BSD-format */
611 	char *outp;		/* Sun-format */
612 	int resid, sunos_reclen;/* Sun-format */
613 	struct file *fp;
614 	struct uio auio;
615 	struct iovec aiov;
616 	struct sunos32_dirent idb;
617 	off_t off;			/* true file offset */
618 	int buflen, error, eofflag;
619 	off_t *cookiebuf, *cookie;
620 	int ncookies;
621 
622 	/* getvnode() will use the descriptor for us */
623 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
624 		return (error);
625 
626 	if ((fp->f_flag & FREAD) == 0) {
627 		error = EBADF;
628 		goto out1;
629 	}
630 
631 	vp = (struct vnode *)fp->f_data;
632 	if (vp->v_type != VDIR) {
633 		error = EINVAL;
634 		goto out1;
635 	}
636 
637 	buflen = min(MAXBSIZE, SCARG(uap, nbytes));
638 	sbuf = malloc(buflen, M_TEMP, M_WAITOK);
639 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
640 	off = fp->f_offset;
641 again:
642 	aiov.iov_base = sbuf;
643 	aiov.iov_len = buflen;
644 	auio.uio_iov = &aiov;
645 	auio.uio_iovcnt = 1;
646 	auio.uio_rw = UIO_READ;
647 	auio.uio_resid = buflen;
648 	auio.uio_offset = off;
649 	UIO_SETUP_SYSSPACE(&auio);
650 	/*
651 	 * First we read into the malloc'ed buffer, then
652 	 * we massage it into user space, one record at a time.
653 	 */
654 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
655 	    &ncookies);
656 	if (error)
657 		goto out;
658 
659 	inp = sbuf;
660 	outp = SCARG_P32(uap, buf);
661 	resid = SCARG(uap, nbytes);
662 	if ((len = buflen - auio.uio_resid) == 0)
663 		goto eof;
664 
665 	for (cookie = cookiebuf; len > 0; len -= reclen) {
666 		bdp = (struct dirent *)inp;
667 		reclen = bdp->d_reclen;
668 		if (reclen & 3)
669 			panic("sunos_getdents");
670 		if (cookie && (*cookie >> 32) != 0) {
671 			compat_offseterr(vp, "sunos_getdents");
672 			error = EINVAL;
673 			goto out;
674 		}
675 		if (bdp->d_fileno == 0) {
676 			inp += reclen;	/* it is a hole; squish it out */
677 			if (cookie)
678 				off = *cookie++;
679 			else
680 				off += reclen;
681 			continue;
682 		}
683 		sunos_reclen = SUNOS32_RECLEN(&idb, bdp->d_namlen);
684 		if (reclen > len || resid < sunos_reclen) {
685 			/* entry too big for buffer, so just stop */
686 			outp++;
687 			break;
688 		}
689 		if (cookie)
690 			off = *cookie++;	/* each entry points to next */
691 		else
692 			off += reclen;
693 		/*
694 		 * Massage in place to make a Sun-shaped dirent (otherwise
695 		 * we have to worry about touching user memory outside of
696 		 * the copyout() call).
697 		 */
698 		idb.d_fileno = bdp->d_fileno;
699 		idb.d_off = off;
700 		idb.d_reclen = sunos_reclen;
701 		idb.d_namlen = bdp->d_namlen;
702 		strlcpy(idb.d_name, bdp->d_name, sizeof(idb.d_name));
703 		if ((error = copyout((void *)&idb, outp, sunos_reclen)) != 0)
704 			goto out;
705 		/* advance past this real entry */
706 		inp += reclen;
707 		/* advance output past Sun-shaped entry */
708 		outp += sunos_reclen;
709 		resid -= sunos_reclen;
710 	}
711 
712 	/* if we squished out the whole block, try again */
713 	if (outp == SCARG_P32(uap, buf))
714 		goto again;
715 	fp->f_offset = off;		/* update the vnode offset */
716 
717 eof:
718 	*retval = SCARG(uap, nbytes) - resid;
719 out:
720 	VOP_UNLOCK(vp, 0);
721 	free(cookiebuf, M_TEMP);
722 	free(sbuf, M_TEMP);
723  out1:
724 	FILE_UNUSE(fp, l);
725 	return (error);
726 }
727 
728 #define	SUNOS32__MAP_NEW	0x80000000	/* if not, old mmap & cannot handle */
729 
730 int
731 sunos32_sys_mmap(struct lwp *l, void *v, register_t *retval)
732 {
733 	struct sunos32_sys_mmap_args /* {
734 		syscallarg(netbsd32_voidp) addr;
735 		syscallarg(netbsd32_size_t) len;
736 		syscallarg(int) prot;
737 		syscallarg(int) flags;
738 		syscallarg(int) fd;
739 		syscallarg(netbsd32_long) pos;
740 	} */ *uap = v;
741 	struct sys_mmap_args ua;
742 	int error;
743 
744 	/*
745 	 * Verify the arguments.
746 	 */
747 	if (SCARG(uap, prot) & ~(PROT_READ|PROT_WRITE|PROT_EXEC))
748 		return (EINVAL);			/* XXX still needed? */
749 
750 	if ((SCARG(uap, flags) & SUNOS32__MAP_NEW) == 0)
751 		return (EINVAL);
752 
753 	SUNOS32TOP_UAP(addr, void);
754 	SUNOS32TOX_UAP(len, size_t);
755 	SUNOS32TO64_UAP(prot);
756 	SCARG(&ua, flags) = SCARG(uap, flags) & ~SUNOS32__MAP_NEW;
757 	SUNOS32TO64_UAP(fd);
758 	SCARG(&ua, pad) = 0;
759 	SUNOS32TOX_UAP(pos, off_t);
760 
761 	error = sys_mmap(l, &ua, retval);
762 	if ((u_long)*retval > (u_long)UINT_MAX) {
763 		printf("sunos32_mmap: retval out of range: 0x%lx",
764 		       (u_long)*retval);
765 		/* Should try to recover and return an error here. */
766 	}
767 	return (error);
768 }
769 
770 #define	MC_SYNC		1
771 #define	MC_LOCK		2
772 #define	MC_UNLOCK	3
773 #define	MC_ADVISE	4
774 #define	MC_LOCKAS	5
775 #define	MC_UNLOCKAS	6
776 
777 int
778 sunos32_sys_mctl(struct lwp *l, void *v, register_t *retval)
779 {
780 	struct sunos32_sys_mctl_args /* {
781 		syscallarg(netbsd32_voidp) addr;
782 		syscallarg(int) len;
783 		syscallarg(int) func;
784 		syscallarg(netbsd32_voidp) arg;
785 	} */ *uap = v;
786 
787 	switch (SCARG(uap, func)) {
788 	case MC_ADVISE:		/* ignore for now */
789 		return (0);
790 	case MC_SYNC:		/* translate to msync */
791 		return (netbsd32___msync13(l, uap, retval));
792 	default:
793 		return (EINVAL);
794 	}
795 }
796 
797 int
798 sunos32_sys_setsockopt(struct lwp *l, void *v, register_t *retval)
799 {
800 	struct sunos32_sys_setsockopt_args /* {
801 		syscallarg(int) s;
802 		syscallarg(int) level;
803 		syscallarg(int) name;
804 		syscallarg(netbsd32_caddr_t) val;
805 		syscallarg(int) valsize;
806 	} */ *uap = v;
807 	struct proc *p = l->l_proc;
808 	struct file *fp;
809 	struct mbuf *m = NULL;
810 	int error;
811 
812 	/* getsock() will use the descriptor for us */
813 	if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
814 		return (error);
815 #define	SO_DONTLINGER (~SO_LINGER)
816 	if (SCARG(uap, name) == SO_DONTLINGER) {
817 		m = m_get(M_WAIT, MT_SOOPTS);
818 		mtod(m, struct linger *)->l_onoff = 0;
819 		m->m_len = sizeof(struct linger);
820 		error = sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
821 		    SO_LINGER, m);
822 		goto out;
823 	}
824 	if (SCARG(uap, level) == IPPROTO_IP) {
825 #define		SUNOS_IP_MULTICAST_IF		2
826 #define		SUNOS_IP_MULTICAST_TTL		3
827 #define		SUNOS_IP_MULTICAST_LOOP		4
828 #define		SUNOS_IP_ADD_MEMBERSHIP		5
829 #define		SUNOS_IP_DROP_MEMBERSHIP	6
830 		static const int ipoptxlat[] = {
831 			IP_MULTICAST_IF,
832 			IP_MULTICAST_TTL,
833 			IP_MULTICAST_LOOP,
834 			IP_ADD_MEMBERSHIP,
835 			IP_DROP_MEMBERSHIP
836 		};
837 		if (SCARG(uap, name) >= SUNOS_IP_MULTICAST_IF &&
838 		    SCARG(uap, name) <= SUNOS_IP_DROP_MEMBERSHIP) {
839 			SCARG(uap, name) =
840 			    ipoptxlat[SCARG(uap, name) - SUNOS_IP_MULTICAST_IF];
841 		}
842 	}
843 	if (SCARG(uap, valsize) > MLEN) {
844 		error = EINVAL;
845 		goto out;
846 	}
847 	if (SCARG_P32(uap, val)) {
848 		m = m_get(M_WAIT, MT_SOOPTS);
849 		error = copyin(SCARG_P32(uap, val), mtod(m, void *),
850 		    (u_int)SCARG(uap, valsize));
851 		if (error) {
852 			(void) m_free(m);
853 			goto out;
854 		}
855 		m->m_len = SCARG(uap, valsize);
856 	}
857 	error = sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
858 	    SCARG(uap, name), m);
859  out:
860 	FILE_UNUSE(fp, l);
861 	return (error);
862 }
863 
864 static inline int sunos32_sys_socket_common(struct lwp *, register_t *,
865 					      int type);
866 static inline int
867 sunos32_sys_socket_common(struct lwp *l, register_t *retval, int type)
868 {
869 	struct socket *so;
870 	struct proc *p = l->l_proc;
871 	struct file *fp;
872 	int error, fd;
873 
874 	/* getsock() will use the descriptor for us */
875 	fd = (int)*retval;
876 	if ((error = getsock(p->p_fd, fd, &fp)) == 0) {
877 		so = (struct socket *)fp->f_data;
878 		if (type == SOCK_DGRAM)
879 			so->so_options |= SO_BROADCAST;
880 	}
881 	FILE_UNUSE(fp, l);
882 	return (error);
883 }
884 
885 int
886 sunos32_sys_socket(struct lwp *l, void *v, register_t *retval)
887 {
888 	struct sunos32_sys_socket_args /* {
889 		syscallarg(int) domain;
890 		syscallarg(int) type;
891 		syscallarg(int) protocol;
892 	} */ *uap = v;
893 	int error;
894 
895 	error = netbsd32_sys___socket30(l, v, retval);
896 	if (error)
897 		return (error);
898 	return sunos32_sys_socket_common(l, retval, SCARG(uap, type));
899 }
900 
901 int
902 sunos32_sys_socketpair(struct lwp *l, void *v, register_t *retval)
903 {
904 	struct sunos32_sys_socketpair_args /* {
905 		syscallarg(int) domain;
906 		syscallarg(int) type;
907 		syscallarg(int) protocol;
908 		syscallarg(int *) rsv;
909 	} */ *uap = v;
910 	int error;
911 
912 	error = netbsd32_socketpair(l, v, retval);
913 	if (error)
914 		return (error);
915 	return sunos32_sys_socket_common(l, retval, SCARG(uap, type));
916 }
917 
918 
919 /*
920  * XXX: This needs cleaning up.
921  */
922 int
923 sunos32_sys_auditsys(struct lwp *l, void *v, register_t *retval)
924 {
925 	return 0;
926 }
927 
928 int
929 sunos32_sys_uname(struct lwp *l, void *v, register_t *retval)
930 {
931 	struct sunos32_sys_uname_args /* {
932 		syscallarg(sunos32_utsnamep_t) name;
933 	} */ *uap = v;
934 	struct sunos_utsname sut;
935 
936 	memset(&sut, 0, sizeof(sut));
937 
938 	memcpy(sut.sysname, ostype, sizeof(sut.sysname) - 1);
939 	memcpy(sut.nodename, hostname, sizeof(sut.nodename));
940 	sut.nodename[sizeof(sut.nodename)-1] = '\0';
941 	memcpy(sut.release, osrelease, sizeof(sut.release) - 1);
942 	memcpy(sut.version, "1", sizeof(sut.version) - 1);
943 	memcpy(sut.machine, machine, sizeof(sut.machine) - 1);
944 
945 	return copyout((void *)&sut, SCARG_P32(uap, name),
946 	    sizeof(struct sunos_utsname));
947 }
948 
949 int
950 sunos32_sys_setpgrp(struct lwp *l, void *v, register_t *retval)
951 {
952 	struct sunos32_sys_setpgrp_args /* {
953 		syscallarg(int) pid;
954 		syscallarg(int) pgid;
955 	} */ *uap = v;
956 	struct proc *p = l->l_proc;
957 
958 	/*
959 	 * difference to our setpgid call is to include backwards
960 	 * compatibility to pre-setsid() binaries. Do setsid()
961 	 * instead of setpgid() in those cases where the process
962 	 * tries to create a new session the old way.
963 	 */
964 	if (!SCARG(uap, pgid) &&
965 	    (!SCARG(uap, pid) || SCARG(uap, pid) == p->p_pid))
966 		return sys_setsid(l, uap, retval);
967 	else
968 		return netbsd32_setpgid(l, uap, retval);
969 }
970 
971 int
972 sunos32_sys_open(struct lwp *l, void *v, register_t *retval)
973 {
974 	struct sunos32_sys_open_args /* {
975 		syscallarg(const netbsd32_charp) path;
976 		syscallarg(int) flags;
977 		syscallarg(int) mode;
978 	} */ *uap = v;
979 	struct proc *p = l->l_proc;
980 	struct sys_open_args ua;
981 	int lf, r;
982 	int noctty;
983 	int ret;
984 
985 	/* convert mode into NetBSD mode */
986 	lf = SCARG(uap, flags);
987 	noctty = lf & 0x8000;
988 	r =	(lf & (0x0001 | 0x0002 | 0x0008 | 0x0040 | 0x0200 | 0x0400 | 0x0800));
989 	r |=	((lf & (0x0004 | 0x1000 | 0x4000)) ? O_NONBLOCK : 0);
990 	r |=	((lf & 0x0080) ? O_SHLOCK : 0);
991 	r |=	((lf & 0x0100) ? O_EXLOCK : 0);
992 	r |=	((lf & 0x2000) ? O_FSYNC : 0);
993 
994 	SUNOS32TOP_UAP(path, const char);
995 	SCARG(&ua, flags) = r;
996 	SUNOS32TO64_UAP(mode);
997 
998 	ret = sys_open(l, &ua, retval);
999 
1000 	/* XXXSMP unlocked */
1001 	if (!ret && !noctty && SESS_LEADER(p) && !(p->p_lflag & PL_CONTROLT)) {
1002 		struct filedesc *fdp = p->p_fd;
1003 		struct file *fp;
1004 
1005 		fp = fd_getfile(fdp, *retval);
1006 
1007 		/* ignore any error, just give it a try */
1008 		if (fp != NULL && fp->f_type == DTYPE_VNODE)
1009 			(fp->f_ops->fo_ioctl)(fp, TIOCSCTTY, (void *)0, l);
1010 	}
1011 	return ret;
1012 }
1013 
1014 #if defined (NFSSERVER)
1015 int
1016 sunos32_sys_nfssvc(struct lwp *l, void *v, register_t *retval)
1017 {
1018 #if 0
1019 	struct sunos32_sys_nfssvc_args *uap = v;
1020 	struct emul *e = p->p_emul;
1021 	struct sys_nfssvc_args outuap;
1022 	struct sockaddr sa;
1023 	int error;
1024 	void *sg = stackgap_init(p, 0);
1025 
1026 	memset(&outuap, 0, sizeof outuap);
1027 	SCARG(&outuap, fd) = SCARG(uap, fd);
1028 	SCARG(&outuap, mskval) = stackgap_alloc(p, &sg, sizeof(sa));
1029 	SCARG(&outuap, msklen) = sizeof(sa);
1030 	SCARG(&outuap, mtchval) = stackgap_alloc(p, &sg, sizeof(sa));
1031 	SCARG(&outuap, mtchlen) = sizeof(sa);
1032 
1033 	memset(&sa, 0, sizeof sa);
1034 	if (error = copyout(&sa, SCARG(&outuap, mskval), SCARG(&outuap, msklen)))
1035 		return (error);
1036 	if (error = copyout(&sa, SCARG(&outuap, mtchval), SCARG(&outuap, mtchlen)))
1037 		return (error);
1038 
1039 	return nfssvc(l, &outuap, retval);
1040 #else
1041 	return (ENOSYS);
1042 #endif
1043 }
1044 #endif /* NFSSERVER */
1045 
1046 int
1047 sunos32_sys_ustat(struct lwp *l, void *v, register_t *retval)
1048 {
1049 	struct sunos32_sys_ustat_args /* {
1050 		syscallarg(int) dev;
1051 		syscallarg(sunos32_ustatp_t) buf;
1052 	} */ *uap = v;
1053 	struct sunos_ustat us;
1054 	int error;
1055 
1056 	memset(&us, 0, sizeof us);
1057 
1058 	/*
1059 	 * XXX: should set f_tfree and f_tinode at least
1060 	 * How do we translate dev -> fstat? (and then to sunos_ustat)
1061 	 */
1062 
1063 	if ((error = copyout(&us, SCARG_P32(uap, buf), sizeof us)) != 0)
1064 		return (error);
1065 	return 0;
1066 }
1067 
1068 int
1069 sunos32_sys_quotactl(struct lwp *l, void *v, register_t *retval)
1070 {
1071 
1072 	return EINVAL;
1073 }
1074 
1075 int
1076 sunos32_sys_vhangup(struct lwp *l, void *v, register_t *retval)
1077 {
1078 	struct proc *p = l->l_proc;
1079 	struct session *sp = p->p_session;
1080 
1081 	if (sp->s_ttyvp == 0)
1082 		return 0;
1083 
1084 	if (sp->s_ttyp && sp->s_ttyp->t_session == sp && sp->s_ttyp->t_pgrp)
1085 		pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
1086 
1087 	(void) ttywait(sp->s_ttyp);
1088 	if (sp->s_ttyvp)
1089 		VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
1090 	if (sp->s_ttyvp)
1091 		vrele(sp->s_ttyvp);
1092 	sp->s_ttyvp = NULL;
1093 
1094 	return 0;
1095 }
1096 
1097 static int
1098 sunstatfs(struct statvfs *sp, void *sbuf)
1099 {
1100 	struct sunos_statfs ssfs;
1101 
1102 	memset(&ssfs, 0, sizeof ssfs);
1103 	ssfs.f_type = 0;
1104 	ssfs.f_bsize = sp->f_bsize;
1105 	ssfs.f_blocks = sp->f_blocks;
1106 	ssfs.f_bfree = sp->f_bfree;
1107 	ssfs.f_bavail = sp->f_bavail;
1108 	ssfs.f_files = sp->f_files;
1109 	ssfs.f_ffree = sp->f_ffree;
1110 	ssfs.f_fsid = sp->f_fsidx;
1111 	return copyout((void *)&ssfs, sbuf, sizeof ssfs);
1112 }
1113 
1114 int
1115 sunos32_sys_statfs(struct lwp *l, void *v, register_t *retval)
1116 {
1117 	struct sunos32_sys_statfs_args /* {
1118 		syscallarg(const netbsd32_charp) path;
1119 		syscallarg(sunos32_statfsp_t) buf;
1120 	} */ *uap = v;
1121 	struct mount *mp;
1122 	struct statvfs *sp;
1123 	int error;
1124 	struct nameidata nd;
1125 	struct sys_statvfs1_args ua;
1126 
1127 	SUNOS32TOP_UAP(path, const char);
1128 
1129 	NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
1130 	    SCARG(&ua, path));
1131 	if ((error = namei(&nd)) != 0)
1132 		return (error);
1133 	mp = nd.ni_vp->v_mount;
1134 	sp = &mp->mnt_stat;
1135 	vrele(nd.ni_vp);
1136 	if ((error = VFS_STATVFS(mp, sp)) != 0)
1137 		return (error);
1138 	sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
1139 	return sunstatfs(sp, SCARG_P32(uap, buf));
1140 }
1141 
1142 int
1143 sunos32_sys_fstatfs(struct lwp *l, void *v, register_t *retval)
1144 {
1145 	struct sunos32_sys_fstatfs_args /* {
1146 		syscallarg(int) fd;
1147 		syscallarg(sunos32_statfsp_t) buf;
1148 	} */ *uap = v;
1149 	struct proc *p = l->l_proc;
1150 	struct file *fp;
1151 	struct mount *mp;
1152 	struct statvfs *sp;
1153 	int error;
1154 
1155 	/* getvnode() will use the descriptor for us */
1156 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1157 		return (error);
1158 	mp = ((struct vnode *)fp->f_data)->v_mount;
1159 	sp = &mp->mnt_stat;
1160 	if ((error = VFS_STATVFS(mp, sp)) != 0)
1161 		goto out;
1162 	sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
1163 	error = sunstatfs(sp, SCARG_P32(uap, buf));
1164  out:
1165 	FILE_UNUSE(fp, l);
1166 	return (error);
1167 }
1168 
1169 int
1170 sunos32_sys_exportfs(struct lwp *l, void *v, register_t *retval)
1171 {
1172 	/*
1173 	 * XXX: should perhaps translate into a mount(2)
1174 	 * with MOUNT_EXPORT?
1175 	 */
1176 	return 0;
1177 }
1178 
1179 int
1180 sunos32_sys_mknod(struct lwp *l, void *v, register_t *retval)
1181 {
1182 	struct sunos32_sys_mknod_args /* {
1183 		syscallarg(const netbsd32_charp) path;
1184 		syscallarg(int) mode;
1185 		syscallarg(int) dev;
1186 	} */ *uap = v;
1187 	struct sys_mknod_args ua;
1188 
1189 	SUNOS32TOP_UAP(path, const char);
1190 	SUNOS32TO64_UAP(mode);
1191 	SUNOS32TO64_UAP(dev);
1192 
1193 	/* netbsd32_mkfifo/mknod to not do alt checking */
1194 	if (S_ISFIFO(SCARG(uap, mode)))
1195 		return netbsd32_mkfifo(l, (struct netbsd32_mkfifo_args *)uap, retval);
1196 
1197 	return netbsd32_mknod(l, (struct netbsd32_mknod_args *)uap, retval);
1198 }
1199 
1200 #define SUNOS_SC_ARG_MAX	1
1201 #define SUNOS_SC_CHILD_MAX	2
1202 #define SUNOS_SC_CLK_TCK	3
1203 #define SUNOS_SC_NGROUPS_MAX	4
1204 #define SUNOS_SC_OPEN_MAX	5
1205 #define SUNOS_SC_JOB_CONTROL	6
1206 #define SUNOS_SC_SAVED_IDS	7
1207 #define SUNOS_SC_VERSION	8
1208 
1209 int
1210 sunos32_sys_sysconf(struct lwp *l, void *v, register_t *retval)
1211 {
1212 	struct sunos32_sys_sysconf_args /* {
1213 		syscallarg(int) name;
1214 	} */ *uap = v;
1215 	extern int maxfiles;
1216 
1217 	switch(SCARG(uap, name)) {
1218 	case SUNOS_SC_ARG_MAX:
1219 		*retval = ARG_MAX;
1220 		break;
1221 	case SUNOS_SC_CHILD_MAX:
1222 		*retval = maxproc;
1223 		break;
1224 	case SUNOS_SC_CLK_TCK:
1225 		*retval = 60;		/* should this be `hz', ie. 100? */
1226 		break;
1227 	case SUNOS_SC_NGROUPS_MAX:
1228 		*retval = NGROUPS_MAX;
1229 		break;
1230 	case SUNOS_SC_OPEN_MAX:
1231 		*retval = maxfiles;
1232 		break;
1233 	case SUNOS_SC_JOB_CONTROL:
1234 		*retval = 1;
1235 		break;
1236 	case SUNOS_SC_SAVED_IDS:
1237 #ifdef _POSIX_SAVED_IDS
1238 		*retval = 1;
1239 #else
1240 		*retval = 0;
1241 #endif
1242 		break;
1243 	case SUNOS_SC_VERSION:
1244 		*retval = 198808;
1245 		break;
1246 	default:
1247 		return EINVAL;
1248 	}
1249 	return 0;
1250 }
1251 
1252 #define SUNOS_RLIMIT_NOFILE	6	/* Other RLIMIT_* are the same */
1253 #define SUNOS_RLIM_NLIMITS	7
1254 
1255 int
1256 sunos32_sys_getrlimit(struct lwp *l, void *v, register_t *retval)
1257 {
1258 	struct sunos32_sys_getrlimit_args /* {
1259 		syscallarg(u_int) which;
1260 		syscallarg(netbsd32_orlimitp_t) rlp;
1261 	} */ *uap = v;
1262 
1263 	if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
1264 		return EINVAL;
1265 
1266 	if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
1267 		SCARG(uap, which) = RLIMIT_NOFILE;
1268 
1269 	return compat_43_netbsd32_ogetrlimit(l, uap, retval);
1270 }
1271 
1272 int
1273 sunos32_sys_setrlimit(struct lwp *l, void *v, register_t *retval)
1274 {
1275 	struct sunos32_sys_setrlimit_args /* {
1276 		syscallarg(u_int) which;
1277 		syscallarg(netbsd32_orlimitp_t) rlp;
1278 	} */ *uap = v;
1279 
1280 	if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
1281 		return EINVAL;
1282 
1283 	if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
1284 		SCARG(uap, which) = RLIMIT_NOFILE;
1285 
1286 	return compat_43_netbsd32_osetrlimit(l, uap, retval);
1287 }
1288 
1289 #if defined(PTRACE) || defined(_LKM)
1290 /* for the m68k machines */
1291 #ifndef PT_GETFPREGS
1292 #define PT_GETFPREGS -1
1293 #endif
1294 #ifndef PT_SETFPREGS
1295 #define PT_SETFPREGS -1
1296 #endif
1297 
1298 static const int sreq2breq[] = {
1299 	PT_TRACE_ME,    PT_READ_I,      PT_READ_D,      -1,
1300 	PT_WRITE_I,     PT_WRITE_D,     -1,             PT_CONTINUE,
1301 	PT_KILL,        -1,             PT_ATTACH,      PT_DETACH,
1302 	PT_GETREGS,     PT_SETREGS,     PT_GETFPREGS,   PT_SETFPREGS
1303 };
1304 static const int nreqs = sizeof(sreq2breq) / sizeof(sreq2breq[0]);
1305 #endif
1306 
1307 int
1308 sunos32_sys_ptrace(struct lwp *l, void *v, register_t *retval)
1309 {
1310 #if defined(PTRACE) || defined(_LKM)
1311 	struct sunos32_sys_ptrace_args /* {
1312 		syscallarg(int) req;
1313 		syscallarg(pid_t) pid;
1314 		syscallarg(netbsd32_caddr_t) addr;
1315 		syscallarg(int) data;
1316 		syscallarg(netbsd32_charp) addr2;
1317 	} */ *uap = v;
1318 	struct netbsd32_ptrace_args pa;
1319 	int req;
1320 
1321 #ifdef _LKM
1322 #define sys_ptrace sysent[SYS_ptrace].sy_call
1323 	if (sys_ptrace == sys_nosys)
1324 		return ENOSYS;
1325 #endif
1326 
1327 	req = SCARG(uap, req);
1328 	if ((unsigned int)req >= nreqs)
1329 		return (EINVAL);
1330 
1331 	req = sreq2breq[req];
1332 	if (req == -1)
1333 		return (EINVAL);
1334 
1335 	SCARG(&pa, req) = req;
1336 	SCARG(&pa, pid) = (pid_t)SCARG(uap, pid);
1337 	SCARG(&pa, addr) = SCARG(uap, addr);
1338 	SCARG(&pa, data) = SCARG(uap, data);
1339 
1340 	return netbsd32_ptrace(l, &pa, retval);
1341 #else
1342 	return (ENOSYS);
1343 #endif /* PTRACE || _LKM */
1344 }
1345 
1346 /*
1347  * SunOS reboot system call (for compatibility).
1348  * Sun lets you pass in a boot string which the PROM
1349  * saves and provides to the next boot program.
1350  */
1351 
1352 #define SUNOS_RB_ASKNAME	0x001
1353 #define SUNOS_RB_SINGLE 	0x002
1354 #define SUNOS_RB_NOSYNC		0x004
1355 #define SUNOS_RB_HALT		0x008
1356 #define SUNOS_RB_DUMP		0x080
1357 #define	SUNOS_RB_STRING		0x200
1358 
1359 static struct sunos_howto_conv {
1360 	int sun_howto;
1361 	int bsd_howto;
1362 } sunos_howto_conv[] = {
1363 	{ SUNOS_RB_ASKNAME,	RB_ASKNAME },
1364 	{ SUNOS_RB_SINGLE,	RB_SINGLE },
1365 	{ SUNOS_RB_NOSYNC,	RB_NOSYNC },
1366 	{ SUNOS_RB_HALT,	RB_HALT },
1367 	{ SUNOS_RB_DUMP,	RB_DUMP },
1368 	{ SUNOS_RB_STRING,	RB_STRING },
1369 	{ 0x000,		0 },
1370 };
1371 
1372 int
1373 sunos32_sys_reboot(struct lwp *l, void *v, register_t *retval)
1374 {
1375 	struct sunos32_sys_reboot_args /* {
1376 		syscallarg(int) howto;
1377 		syscallarg(netbsd32_charp) bootstr;
1378 	} */ *uap = v;
1379 	struct sys_reboot_args ua;
1380 	struct sunos_howto_conv *convp;
1381 	int error, bsd_howto, sun_howto;
1382 	char *bootstr;
1383 
1384 	if ((error = kauth_authorize_system(l->l_cred,
1385 	    KAUTH_SYSTEM_REBOOT, 0, NULL, NULL, NULL)) != 0)
1386 		return (error);
1387 
1388 	/*
1389 	 * Convert howto bits to BSD format.
1390 	 */
1391 	sun_howto = SCARG(uap, howto);
1392 	bsd_howto = 0;
1393 	convp = sunos_howto_conv;
1394 	while (convp->sun_howto) {
1395 		if (sun_howto & convp->sun_howto)
1396 			bsd_howto |= convp->bsd_howto;
1397 		convp++;
1398 	}
1399 
1400 	/*
1401 	 * Sun RB_STRING (Get user supplied bootstring.)
1402 	 * If the machine supports passing a string to the
1403 	 * next booted kernel.
1404 	 */
1405 	if (sun_howto & SUNOS_RB_STRING)
1406 		bootstr = SCARG_P32(uap, bootstr);
1407 	else
1408 		bootstr = NULL;
1409 
1410 	SCARG(&ua, opt) = bsd_howto;
1411 	SCARG(&ua, bootstr) = bootstr;
1412 	return (sys_reboot(l, &ua, retval));
1413 }
1414 
1415 /*
1416  * Generalized interface signal handler, 4.3-compatible.
1417  */
1418 /* ARGSUSED */
1419 int
1420 sunos32_sys_sigvec(struct lwp *l, void *v, register_t *retval)
1421 {
1422 	struct sunos32_sys_sigvec_args /* {
1423 		syscallarg(int) signum;
1424 		syscallarg(struct sigvec *) nsv;
1425 		syscallarg(struct sigvec *) osv;
1426 	} */ *uap = v;
1427 	struct netbsd32_sigvec sv;
1428 	struct sigaction nsa, osa;
1429 	int error;
1430 
1431 	if (SCARG_P32(uap, nsv)) {
1432 		error = copyin(SCARG_P32(uap, nsv), &sv, sizeof(sv));
1433 		if (error != 0)
1434 			return (error);
1435 
1436 		/*
1437 		 * SunOS uses the mask 0x0004 as SV_RESETHAND
1438 		 * meaning: `reset to SIG_DFL on delivery'.
1439 		 * We support only the bits in: 0xF
1440 		 * (those bits are the same as ours)
1441 		 */
1442 		if (sv.sv_flags & ~0xF)
1443 			return (EINVAL);
1444 
1445 		sunos32_sigvec_to_sigaction(&sv, &nsa);
1446 	}
1447 	error = sigaction1(l, SCARG(uap, signum),
1448 			   SCARG_P32(uap, nsv) ? &nsa : 0,
1449 			   SCARG_P32(uap, osv) ? &osa : 0,
1450 			   NULL, 0);
1451 	if (error != 0)
1452 		return (error);
1453 
1454 	if (SCARG_P32(uap, osv)) {
1455 		sunos32_sigvec_from_sigaction(&sv, &osa);
1456 		error = copyout(&sv, SCARG_P32(uap, osv), sizeof(sv));
1457 		if (error != 0)
1458 			return (error);
1459 	}
1460 
1461 	return (0);
1462 }
1463