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