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