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