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