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