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