Home | History | Annotate | Line # | Download | only in sunos
sunos_misc.c revision 1.174
      1 /*	$NetBSD: sunos_misc.c,v 1.174 2019/09/23 21:07:50 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This software was developed by the Computer Systems Engineering group
      8  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9  * contributed to Berkeley.
     10  *
     11  * All advertising materials mentioning features or use of this software
     12  * must display the following acknowledgement:
     13  *	This product includes software developed by the University of
     14  *	California, Lawrence Berkeley Laboratory.
     15  *
     16  * Redistribution and use in source and binary forms, with or without
     17  * modification, are permitted provided that the following conditions
     18  * are met:
     19  * 1. Redistributions of source code must retain the above copyright
     20  *    notice, this list of conditions and the following disclaimer.
     21  * 2. Redistributions in binary form must reproduce the above copyright
     22  *    notice, this list of conditions and the following disclaimer in the
     23  *    documentation and/or other materials provided with the distribution.
     24  * 3. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  *
     40  *	@(#)sunos_misc.c	8.1 (Berkeley) 6/18/93
     41  *
     42  *	Header: sunos_misc.c,v 1.16 93/04/07 02:46:27 torek Exp
     43  */
     44 
     45 /*
     46  * SunOS compatibility module.
     47  *
     48  * SunOS system calls that are implemented differently in BSD are
     49  * handled here.
     50  */
     51 
     52 #include <sys/cdefs.h>
     53 __KERNEL_RCSID(0, "$NetBSD: sunos_misc.c,v 1.174 2019/09/23 21:07:50 christos Exp $");
     54 
     55 #include <sys/param.h>
     56 #include <sys/systm.h>
     57 #include <sys/namei.h>
     58 #include <sys/proc.h>
     59 #include <sys/dirent.h>
     60 #include <sys/file.h>
     61 #include <sys/stat.h>
     62 #include <sys/filedesc.h>
     63 #include <sys/ioctl.h>
     64 #include <sys/kernel.h>
     65 #include <sys/reboot.h>
     66 #include <sys/malloc.h>
     67 #include <sys/mbuf.h>
     68 #include <sys/mman.h>
     69 #include <sys/mount.h>
     70 #include <sys/ptrace.h>
     71 #include <sys/resource.h>
     72 #include <sys/resourcevar.h>
     73 #include <sys/signal.h>
     74 #include <sys/signalvar.h>
     75 #include <sys/socket.h>
     76 #include <sys/tty.h>
     77 #include <sys/vnode.h>
     78 #include <sys/uio.h>
     79 #include <sys/wait.h>
     80 #include <sys/utsname.h>
     81 #include <sys/unistd.h>
     82 #include <sys/syscall.h>
     83 #include <sys/syscallargs.h>
     84 #include <sys/conf.h>
     85 #include <sys/socketvar.h>
     86 #include <sys/exec.h>
     87 #include <sys/swap.h>
     88 #include <sys/kauth.h>
     89 
     90 #include <compat/sys/signal.h>
     91 
     92 #include <compat/sunos/sunos.h>
     93 #include <compat/sunos/sunos_syscallargs.h>
     94 #include <compat/common/compat_util.h>
     95 #include <compat/sunos/sunos_dirent.h>
     96 #include <compat/sys/mount.h>
     97 
     98 #include <netinet/in.h>
     99 
    100 #include <miscfs/specfs/specdev.h>
    101 
    102 #include <nfs/rpcv2.h>
    103 #include <nfs/nfsproto.h>
    104 #include <nfs/nfs.h>
    105 #include <nfs/nfsmount.h>
    106 
    107 static int sunstatfs(struct statvfs *, void *);
    108 
    109 int
    110 sunos_sys_stime(struct lwp *l, const struct sunos_sys_stime_args *uap, register_t *retval)
    111 {
    112 	struct timeval tv;
    113 	int error;
    114 
    115 	error = copyin(SCARG(uap, tp), &tv.tv_sec, sizeof(tv.tv_sec));
    116 	if (error)
    117 		return error;
    118 	tv.tv_usec = 0;
    119 
    120 	return settimeofday1(&tv, false, NULL, l, true);
    121 }
    122 
    123 int
    124 sunos_sys_wait4(struct lwp *l, const struct sunos_sys_wait4_args *uap, register_t *retval)
    125 {
    126 	struct compat_50_sys_wait4_args bsd_ua;
    127 
    128 	SCARG(&bsd_ua, pid) = SCARG(uap, pid) == 0 ? WAIT_ANY : SCARG(uap, pid);
    129 	SCARG(&bsd_ua, status) = SCARG(uap, status);
    130 	SCARG(&bsd_ua, options) = SCARG(uap, options);
    131 	SCARG(&bsd_ua, rusage) = SCARG(uap, rusage);
    132 
    133 	return (compat_50_sys_wait4(l, &bsd_ua, retval));
    134 }
    135 
    136 int
    137 sunos_sys_creat(struct lwp *l, const struct sunos_sys_creat_args *uap, register_t *retval)
    138 {
    139 	struct sys_open_args ouap;
    140 
    141 	SCARG(&ouap, path) = SCARG(uap, path);
    142 	SCARG(&ouap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
    143 	SCARG(&ouap, mode) = SCARG(uap, mode);
    144 
    145 	return (sys_open(l, &ouap, retval));
    146 }
    147 
    148 int
    149 sunos_sys_execv(struct lwp *l, const struct sunos_sys_execv_args *uap, register_t *retval)
    150 {
    151 	/* {
    152 		syscallarg(const char *) path;
    153 		syscallarg(char **) argv;
    154 	} */
    155 	struct sys_execve_args ap;
    156 
    157 	SCARG(&ap, path) = SCARG(uap, path);
    158 	SCARG(&ap, argp) = SCARG(uap, argp);
    159 	SCARG(&ap, envp) = NULL;
    160 
    161 	return (sys_execve(l, &ap, retval));
    162 }
    163 
    164 int
    165 sunos_sys_execve(struct lwp *l, const struct sunos_sys_execve_args *uap, register_t *retval)
    166 {
    167 	/* {
    168 		syscallarg(const char *) path;
    169 		syscallarg(char **) argv;
    170 		syscallarg(char **) envp;
    171 	} */
    172 	struct sys_execve_args ap;
    173 
    174 	SCARG(&ap, path) = SCARG(uap, path);
    175 	SCARG(&ap, argp) = SCARG(uap, argp);
    176 	SCARG(&ap, envp) = SCARG(uap, envp);
    177 
    178 	return (sys_execve(l, &ap, retval));
    179 }
    180 
    181 int
    182 sunos_sys_omsync(struct lwp *l, const struct sunos_sys_omsync_args *uap, register_t *retval)
    183 {
    184 	struct sys___msync13_args ouap;
    185 
    186 	SCARG(&ouap, addr) = SCARG(uap, addr);
    187 	SCARG(&ouap, len) = SCARG(uap, len);
    188 	SCARG(&ouap, flags) = SCARG(uap, flags);
    189 
    190 	return (sys___msync13(l, &ouap, retval));
    191 }
    192 
    193 int
    194 sunos_sys_unmount(struct lwp *l, const struct sunos_sys_unmount_args *uap, register_t *retval)
    195 {
    196 	struct sys_unmount_args ouap;
    197 
    198 	SCARG(&ouap, path) = SCARG(uap, path);
    199 	SCARG(&ouap, flags) = 0;
    200 
    201 	return (sys_unmount(l, &ouap, retval));
    202 }
    203 
    204 /*
    205  * Conversion table for SunOS NFS mount flags.
    206  */
    207 static struct {
    208 	int	sun_flg;
    209 	int	bsd_flg;
    210 } sunnfs_flgtab[] = {
    211 	{ SUNNFS_SOFT,		NFSMNT_SOFT },
    212 	{ SUNNFS_WSIZE,		NFSMNT_WSIZE },
    213 	{ SUNNFS_RSIZE,		NFSMNT_RSIZE },
    214 	{ SUNNFS_TIMEO,		NFSMNT_TIMEO },
    215 	{ SUNNFS_RETRANS,	NFSMNT_RETRANS },
    216 	{ SUNNFS_HOSTNAME,	0 },			/* Ignored */
    217 	{ SUNNFS_INT,		NFSMNT_INT },
    218 	{ SUNNFS_NOAC,		0 },			/* Ignored */
    219 	{ SUNNFS_ACREGMIN,	0 },			/* Ignored */
    220 	{ SUNNFS_ACREGMAX,	0 },			/* Ignored */
    221 	{ SUNNFS_ACDIRMIN,	0 },			/* Ignored */
    222 	{ SUNNFS_ACDIRMAX,	0 },			/* Ignored */
    223 	{ SUNNFS_SECURE,	0 },			/* Ignored */
    224 	{ SUNNFS_NOCTO,		0 },			/* Ignored */
    225 	{ SUNNFS_POSIX,		0 }			/* Ignored */
    226 };
    227 
    228 int
    229 sunos_sys_mount(struct lwp *l, const struct sunos_sys_mount_args *uap, register_t *retval)
    230 {
    231 	int oflags = SCARG(uap, flags), nflags, error;
    232 	char fsname[MFSNAMELEN];
    233 	register_t dummy;
    234 
    235 	if (oflags & (SUNM_NOSUB | SUNM_SYS5))
    236 		return (EINVAL);
    237 	if ((oflags & SUNM_NEWTYPE) == 0)
    238 		return (EINVAL);
    239 	nflags = 0;
    240 	if (oflags & SUNM_RDONLY)
    241 		nflags |= MNT_RDONLY;
    242 	if (oflags & SUNM_NOSUID)
    243 		nflags |= MNT_NOSUID;
    244 	if (oflags & SUNM_REMOUNT)
    245 		nflags |= MNT_UPDATE;
    246 
    247 	error = copyinstr(SCARG(uap, type), fsname, sizeof fsname, NULL);
    248 	if (error)
    249 		return (error);
    250 
    251 	if (strcmp(fsname, "nfs") == 0) {
    252 		struct sunos_nfs_args sna;
    253 		struct nfs_args na;
    254 		int n;
    255 
    256 		error = copyin(SCARG(uap, data), &sna, sizeof sna);
    257 		if (error)
    258 			return (error);
    259 		/* sa.sa_len = sizeof(sain); */
    260 		na.version = NFS_ARGSVERSION;
    261 		na.addr = (void *)sna.addr;
    262 		na.addrlen = sizeof(struct sockaddr);
    263 		na.sotype = SOCK_DGRAM;
    264 		na.proto = IPPROTO_UDP;
    265 		na.fh = sna.fh;
    266 		na.fhsize = NFSX_V2FH;
    267 		na.flags = 0;
    268 		n = sizeof(sunnfs_flgtab) / sizeof(sunnfs_flgtab[0]);
    269 		while (--n >= 0)
    270 			if (sna.flags & sunnfs_flgtab[n].sun_flg)
    271 				na.flags |= sunnfs_flgtab[n].bsd_flg;
    272 		na.wsize = sna.wsize;
    273 		na.rsize = sna.rsize;
    274 		if (na.flags & NFSMNT_RSIZE) {
    275 			na.flags |= NFSMNT_READDIRSIZE;
    276 			na.readdirsize = na.rsize;
    277 		}
    278 		na.timeo = sna.timeo;
    279 		na.retrans = sna.retrans;
    280 #ifdef __arch64__
    281 		/* XXX */
    282 		na.hostname = (char *)(intptr_t)sna.hostname;
    283 #else
    284 		na.hostname = sna.hostname;
    285 #endif
    286 		return do_sys_mount(l, "nfs", UIO_SYSSPACE,
    287 		    SCARG(uap, dir), nflags, &na,
    288 		    UIO_SYSSPACE, sizeof na, &dummy);
    289 	}
    290 
    291 	if (strcmp(fsname, "4.2") == 0)
    292 		strcpy(fsname, "ffs");
    293 
    294 	return do_sys_mount(l, fsname, UIO_SYSSPACE,
    295 	    SCARG(uap, dir), nflags, SCARG(uap, data),
    296 	    UIO_USERSPACE, 0, &dummy);
    297 }
    298 
    299 int
    300 async_daemon(struct lwp *l, const void *v, register_t *retval)
    301 {
    302 
    303 	return kpause("fakeniod", false, 0, NULL);
    304 }
    305 
    306 void	native_to_sunos_sigset(const sigset_t *, int *);
    307 void	sunos_to_native_sigset(const int, sigset_t *);
    308 
    309 inline void
    310 native_to_sunos_sigset(const sigset_t *ss, int *mask)
    311 {
    312 	*mask = ss->__bits[0];
    313 }
    314 
    315 inline void
    316 sunos_to_native_sigset(const int mask, sigset_t *ss)
    317 {
    318 
    319 	ss->__bits[0] = mask;
    320 	ss->__bits[1] = 0;
    321 	ss->__bits[2] = 0;
    322 	ss->__bits[3] = 0;
    323 }
    324 
    325 int
    326 sunos_sys_sigpending(struct lwp *l, const struct sunos_sys_sigpending_args *uap, register_t *retval)
    327 {
    328 	sigset_t ss;
    329 	int mask;
    330 
    331 	sigpending1(l, &ss);
    332 	native_to_sunos_sigset(&ss, &mask);
    333 
    334 	return (copyout((void *)&mask, (void *)SCARG(uap, mask), sizeof(int)));
    335 }
    336 
    337 int
    338 sunos_sys_sigsuspend(struct lwp *l, const struct sunos_sys_sigsuspend_args *uap, register_t *retval)
    339 {
    340 	/* {
    341 		syscallarg(int) mask;
    342 	} */
    343 	int mask;
    344 	sigset_t ss;
    345 
    346 	mask = SCARG(uap, mask);
    347 	sunos_to_native_sigset(mask, &ss);
    348 	return (sigsuspend1(l, &ss));
    349 }
    350 
    351 /*
    352  * Read Sun-style directory entries.  We suck them into kernel space so
    353  * that they can be massaged before being copied out to user code.  Like
    354  * SunOS, we squish out `empty' entries.
    355  *
    356  * This is quite ugly, but what do you expect from compatibility code?
    357  */
    358 int
    359 sunos_sys_getdents(struct lwp *l, const struct sunos_sys_getdents_args *uap, register_t *retval)
    360 {
    361 	struct dirent *bdp;
    362 	struct vnode *vp;
    363 	char *inp, *buf;	/* BSD-format */
    364 	int len, reclen;	/* BSD-format */
    365 	char *outp;		/* Sun-format */
    366 	int resid, sunos_reclen;/* Sun-format */
    367 	struct file *fp;
    368 	struct uio auio;
    369 	struct iovec aiov;
    370 	struct sunos_dirent idb;
    371 	off_t off;			/* true file offset */
    372 	int buflen, error, eofflag;
    373 	off_t *cookiebuf, *cookie;
    374 	int ncookies;
    375 
    376 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
    377 		return (error);
    378 
    379 	if ((fp->f_flag & FREAD) == 0) {
    380 		error = EBADF;
    381 		goto out1;
    382 	}
    383 
    384 	vp = fp->f_vnode;
    385 	if (vp->v_type != VDIR) {
    386 		error = EINVAL;
    387 		goto out1;
    388 	}
    389 
    390 	buflen = uimin(MAXBSIZE, SCARG(uap, nbytes));
    391 	buf = malloc(buflen, M_TEMP, M_WAITOK);
    392 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    393 	off = fp->f_offset;
    394 again:
    395 	aiov.iov_base = buf;
    396 	aiov.iov_len = buflen;
    397 	auio.uio_iov = &aiov;
    398 	auio.uio_iovcnt = 1;
    399 	auio.uio_rw = UIO_READ;
    400 	auio.uio_resid = buflen;
    401 	auio.uio_offset = off;
    402 	UIO_SETUP_SYSSPACE(&auio);
    403 	/*
    404 	 * First we read into the malloc'ed buffer, then
    405 	 * we massage it into user space, one record at a time.
    406 	 */
    407 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
    408 	    &ncookies);
    409 	if (error)
    410 		goto out;
    411 
    412 	inp = buf;
    413 	outp = SCARG(uap, buf);
    414 	resid = SCARG(uap, nbytes);
    415 	if ((len = buflen - auio.uio_resid) == 0)
    416 		goto eof;
    417 
    418 	for (cookie = cookiebuf; len > 0; len -= reclen) {
    419 		bdp = (struct dirent *)inp;
    420 		reclen = bdp->d_reclen;
    421 		if (reclen & 3) {
    422 			error = EIO;
    423 			goto out;
    424 		}
    425 		if ((*cookie >> 32) != 0) {
    426 			compat_offseterr(vp, "sunos_getdents");
    427 			error = EINVAL;
    428 			goto out;
    429 		}
    430 		if (bdp->d_fileno == 0) {
    431 			inp += reclen;	/* it is a hole; squish it out */
    432 			if (cookie)
    433 				off = *cookie++;
    434 			else
    435 				off += reclen;
    436 			continue;
    437 		}
    438 		sunos_reclen = SUNOS_RECLEN(&idb, bdp->d_namlen);
    439 		if (reclen > len || resid < sunos_reclen) {
    440 			/* entry too big for buffer, so just stop */
    441 			outp++;
    442 			break;
    443 		}
    444 		if (cookie)
    445 			off = *cookie++;	/* each entry points to next */
    446 		else
    447 			off += reclen;
    448 		/*
    449 		 * Massage in place to make a Sun-shaped dirent (otherwise
    450 		 * we have to worry about touching user memory outside of
    451 		 * the copyout() call).
    452 		 */
    453 		idb.d_fileno = bdp->d_fileno;
    454 		idb.d_off = off;
    455 		idb.d_reclen = sunos_reclen;
    456 		idb.d_namlen = bdp->d_namlen;
    457 		strlcpy(idb.d_name, bdp->d_name, sizeof(idb.d_name));
    458 		if ((error = copyout((void *)&idb, outp, sunos_reclen)) != 0)
    459 			goto out;
    460 		/* advance past this real entry */
    461 		inp += reclen;
    462 		/* advance output past Sun-shaped entry */
    463 		outp += sunos_reclen;
    464 		resid -= sunos_reclen;
    465 	}
    466 
    467 	/* if we squished out the whole block, try again */
    468 	if (outp == SCARG(uap, buf)) {
    469 		if (cookiebuf)
    470 			free(cookiebuf, M_TEMP);
    471 		cookiebuf = NULL;
    472 		goto again;
    473 	}
    474 	fp->f_offset = off;		/* update the vnode offset */
    475 
    476 eof:
    477 	*retval = SCARG(uap, nbytes) - resid;
    478 out:
    479 	VOP_UNLOCK(vp);
    480 	free(cookiebuf, M_TEMP);
    481 	free(buf, M_TEMP);
    482  out1:
    483  	fd_putfile(SCARG(uap, fd));
    484 	return (error);
    485 }
    486 
    487 #define	SUNOS__MAP_NEW	0x80000000	/* if not, old mmap & cannot handle */
    488 
    489 int
    490 sunos_sys_mmap(struct lwp *l, const struct sunos_sys_mmap_args *uap, register_t *retval)
    491 {
    492 	struct sys_mmap_args ouap;
    493 
    494 	/*
    495 	 * Verify the arguments.
    496 	 */
    497 	if (SCARG(uap, prot) & ~(PROT_READ|PROT_WRITE|PROT_EXEC))
    498 		return (EINVAL);			/* XXX still needed? */
    499 
    500 	if ((SCARG(uap, flags) & SUNOS__MAP_NEW) == 0)
    501 		return (EINVAL);
    502 
    503 	SCARG(&ouap, flags) = SCARG(uap, flags) & ~SUNOS__MAP_NEW;
    504 	SCARG(&ouap, addr) = SCARG(uap, addr);
    505 	SCARG(&ouap, len) = SCARG(uap, len);
    506 	SCARG(&ouap, prot) = SCARG(uap, prot);
    507 	SCARG(&ouap, fd) = SCARG(uap, fd);
    508 	SCARG(&ouap, pos) = SCARG(uap, pos);
    509 
    510 	return (sys_mmap(l, &ouap, retval));
    511 }
    512 
    513 #define	MC_SYNC		1
    514 #define	MC_LOCK		2
    515 #define	MC_UNLOCK	3
    516 #define	MC_ADVISE	4
    517 #define	MC_LOCKAS	5
    518 #define	MC_UNLOCKAS	6
    519 
    520 int
    521 sunos_sys_mctl(struct lwp *l, const struct sunos_sys_mctl_args *uap, register_t *retval)
    522 {
    523 
    524 	switch (SCARG(uap, func)) {
    525 	case MC_ADVISE:		/* ignore for now */
    526 		return (0);
    527 	case MC_SYNC:		/* translate to msync */
    528 		return (sys___msync13(l, (const void *)uap, retval));
    529 	default:
    530 		return (EINVAL);
    531 	}
    532 }
    533 
    534 int
    535 sunos_sys_setsockopt(struct lwp *l, const struct sunos_sys_setsockopt_args *uap, register_t *retval)
    536 {
    537 	struct sockopt sopt;
    538 	struct socket *so;
    539 	int name = SCARG(uap, name);
    540 	int error;
    541 
    542 	/* fd_getsock() will use the descriptor for us */
    543 	if ((error = fd_getsock(SCARG(uap, s), &so)) != 0)
    544 		return (error);
    545 #define	SO_DONTLINGER (~SO_LINGER)
    546 	if (name == SO_DONTLINGER) {
    547 		struct linger lg;
    548 
    549 		lg.l_onoff = 0;
    550 		error = so_setsockopt(l, so, SCARG(uap, level), SO_LINGER,
    551 		    &lg, sizeof(lg));
    552 		goto out;
    553 	}
    554 	if (SCARG(uap, level) == IPPROTO_IP) {
    555 #define		SUNOS_IP_MULTICAST_IF		2
    556 #define		SUNOS_IP_MULTICAST_TTL		3
    557 #define		SUNOS_IP_MULTICAST_LOOP		4
    558 #define		SUNOS_IP_ADD_MEMBERSHIP		5
    559 #define		SUNOS_IP_DROP_MEMBERSHIP	6
    560 		static const int ipoptxlat[] = {
    561 			IP_MULTICAST_IF,
    562 			IP_MULTICAST_TTL,
    563 			IP_MULTICAST_LOOP,
    564 			IP_ADD_MEMBERSHIP,
    565 			IP_DROP_MEMBERSHIP
    566 		};
    567 		if (name >= SUNOS_IP_MULTICAST_IF &&
    568 		    name <= SUNOS_IP_DROP_MEMBERSHIP) {
    569 			name = ipoptxlat[name - SUNOS_IP_MULTICAST_IF];
    570 		}
    571 	}
    572 	if ((unsigned)SCARG(uap, valsize) > MLEN) {
    573 		error = EINVAL;
    574 		goto out;
    575 	}
    576 	sockopt_init(&sopt, SCARG(uap, level), name, SCARG(uap, valsize));
    577 	if (SCARG(uap, val)) {
    578 		error = copyin(SCARG(uap, val), sopt.sopt_data,
    579 		    (u_int)SCARG(uap, valsize));
    580 	}
    581 	if (error == 0)
    582 		error = sosetopt(so, &sopt);
    583 	sockopt_destroy(&sopt);
    584  out:
    585  	fd_putfile(SCARG(uap, s));
    586 	return (error);
    587 }
    588 
    589 static inline int sunos_sys_socket_common(struct lwp *, register_t *,
    590 					      int type);
    591 static inline int
    592 sunos_sys_socket_common(struct lwp *l, register_t *retval, int type)
    593 {
    594 	struct socket *so;
    595 	int error, fd;
    596 
    597 	/* fd_getsock() will use the descriptor for us */
    598 	fd = (int)*retval;
    599 	if ((error = fd_getsock(fd, &so)) == 0) {
    600 		if (type == SOCK_DGRAM)
    601 			so->so_options |= SO_BROADCAST;
    602 		fd_putfile(fd);
    603 	}
    604 	return (error);
    605 }
    606 
    607 int
    608 sunos_sys_socket(struct lwp *l, const struct sunos_sys_socket_args *uap, register_t *retval)
    609 {
    610 	/* {
    611 		syscallarg(int) domain;
    612 		syscallarg(int) type;
    613 		syscallarg(int) protocol;
    614 	} */
    615 	int error;
    616 
    617 	error = compat_30_sys_socket(l, (const void *)uap, retval);
    618 	if (error)
    619 		return (error);
    620 	return sunos_sys_socket_common(l, retval, SCARG(uap, type));
    621 }
    622 
    623 int
    624 sunos_sys_socketpair(struct lwp *l, const struct sunos_sys_socketpair_args *uap, register_t *retval)
    625 {
    626 	/* {
    627 		syscallarg(int) domain;
    628 		syscallarg(int) type;
    629 		syscallarg(int) protocol;
    630 		syscallarg(int *) rsv;
    631 	} */
    632 	int error;
    633 
    634 	error = sys_socketpair(l, (const void *)uap, retval);
    635 	if (error)
    636 		return (error);
    637 	return sunos_sys_socket_common(l, retval, SCARG(uap, type));
    638 }
    639 
    640 /*
    641  * XXX: This needs cleaning up.
    642  */
    643 int
    644 sunos_sys_auditsys(struct lwp *l, const struct sunos_sys_auditsys_args *uap, register_t *retval)
    645 {
    646 	return 0;
    647 }
    648 
    649 int
    650 sunos_sys_uname(struct lwp *l, const struct sunos_sys_uname_args *uap, register_t *retval)
    651 {
    652 	struct sunos_utsname sut;
    653 
    654 	memset(&sut, 0, sizeof(sut));
    655 
    656 	memcpy(sut.sysname, ostype, sizeof(sut.sysname) - 1);
    657 	memcpy(sut.nodename, hostname, sizeof(sut.nodename));
    658 	sut.nodename[sizeof(sut.nodename)-1] = '\0';
    659 	memcpy(sut.release, osrelease, sizeof(sut.release) - 1);
    660 	memcpy(sut.version, "1", sizeof(sut.version) - 1);
    661 	memcpy(sut.machine, machine, sizeof(sut.machine) - 1);
    662 
    663 	return copyout((void *)&sut, (void *)SCARG(uap, name),
    664 	    sizeof(struct sunos_utsname));
    665 }
    666 
    667 int
    668 sunos_sys_setpgrp(struct lwp *l, const struct sunos_sys_setpgrp_args *uap, register_t *retval)
    669 {
    670 	struct proc *p = l->l_proc;
    671 
    672 	/*
    673 	 * difference to our setpgid call is to include backwards
    674 	 * compatibility to pre-setsid() binaries. Do setsid()
    675 	 * instead of setpgid() in those cases where the process
    676 	 * tries to create a new session the old way.
    677 	 */
    678 	if (!SCARG(uap, pgid) &&
    679 	    (!SCARG(uap, pid) || SCARG(uap, pid) == p->p_pid))
    680 		return sys_setsid(l, NULL, retval);
    681 	else
    682 		return sys_setpgid(l, (const void *)uap, retval);
    683 }
    684 
    685 int
    686 sunos_sys_open(struct lwp *l, const struct sunos_sys_open_args *uap, register_t *retval)
    687 {
    688 	struct proc *p = l->l_proc;
    689 	struct sys_open_args open_ua;
    690 	int smode, nmode;
    691 	int noctty;
    692 	int ret;
    693 
    694 	/* convert mode into NetBSD mode */
    695 	smode = SCARG(uap, flags);
    696 	noctty = smode & 0x8000;
    697 	nmode =	smode &
    698 		(0x0001 | 0x0002 | 0x0008 | 0x0040 | 0x0200 | 0x0400 | 0x0800);
    699 	nmode |= ((smode & (0x0004 | 0x1000 | 0x4000)) ? O_NONBLOCK : 0);
    700 	nmode |= ((smode & 0x0080) ? O_SHLOCK : 0);
    701 	nmode |= ((smode & 0x0100) ? O_EXLOCK : 0);
    702 	nmode |= ((smode & 0x2000) ? O_FSYNC : 0);
    703 
    704 	SCARG(&open_ua, path) = SCARG(uap, path);
    705 	SCARG(&open_ua, flags) = nmode;
    706 	SCARG(&open_ua, mode) = SCARG(uap, mode);
    707 	ret = sys_open(l, &open_ua, retval);
    708 
    709 	/* XXXSMP */
    710 	if (!ret && !noctty && SESS_LEADER(p) && !(p->p_lflag & PL_CONTROLT)) {
    711 		file_t *fp;
    712 		int fd;
    713 
    714 		fd = (int)*retval;
    715 		fp = fd_getfile(fd);
    716 
    717 		/* ignore any error, just give it a try */
    718 		if (fp != NULL) {
    719 			if (fp->f_type == DTYPE_VNODE)
    720 				(fp->f_ops->fo_ioctl)(fp, TIOCSCTTY, NULL);
    721 			fd_putfile(fd);
    722 		}
    723 	}
    724 	return ret;
    725 }
    726 
    727 int
    728 sunos_sys_ustat(struct lwp *l, const struct sunos_sys_ustat_args *uap, register_t *retval)
    729 {
    730 	struct sunos_ustat us;
    731 	int error;
    732 
    733 	memset(&us, 0, sizeof us);
    734 
    735 	/*
    736 	 * XXX: should set f_tfree and f_tinode at least
    737 	 * How do we translate dev -> fstat? (and then to sunos_ustat)
    738 	 */
    739 
    740 	if ((error = copyout(&us, SCARG(uap, buf), sizeof us)) != 0)
    741 		return (error);
    742 	return 0;
    743 }
    744 
    745 int
    746 sunos_sys_quotactl(struct lwp *l, const struct sunos_sys_quotactl_args *uap, register_t *retval)
    747 {
    748 
    749 	return EINVAL;
    750 }
    751 
    752 int
    753 sunos_sys_vhangup(struct lwp *l, const void *v, register_t *retval)
    754 {
    755 	struct proc *p = l->l_proc;
    756 	struct session *sp = p->p_session;
    757 
    758 	if (sp->s_ttyvp == 0)
    759 		return 0;
    760 
    761 	if (sp->s_ttyp && sp->s_ttyp->t_session == sp && sp->s_ttyp->t_pgrp)
    762 		pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
    763 
    764 	(void) ttywait(sp->s_ttyp);
    765 	if (sp->s_ttyvp)
    766 		VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
    767 	if (sp->s_ttyvp)
    768 		vrele(sp->s_ttyvp);
    769 	sp->s_ttyvp = NULL;
    770 
    771 	return 0;
    772 }
    773 
    774 static int
    775 sunstatfs(struct statvfs *sp, void *buf)
    776 {
    777 	struct sunos_statfs ssfs;
    778 
    779 	memset(&ssfs, 0, sizeof ssfs);
    780 	ssfs.f_type = 0;
    781 	ssfs.f_bsize = sp->f_bsize;
    782 	ssfs.f_blocks = sp->f_blocks;
    783 	ssfs.f_bfree = sp->f_bfree;
    784 	ssfs.f_bavail = sp->f_bavail;
    785 	ssfs.f_files = sp->f_files;
    786 	ssfs.f_ffree = sp->f_ffree;
    787 	ssfs.f_fsid = sp->f_fsidx;
    788 	return copyout((void *)&ssfs, buf, sizeof ssfs);
    789 }
    790 
    791 int
    792 sunos_sys_statfs(struct lwp *l, const struct sunos_sys_statfs_args *uap, register_t *retval)
    793 {
    794 	struct mount *mp;
    795 	struct statvfs *sp;
    796 	int error;
    797 	struct vnode *vp;
    798 
    799 	error = namei_simple_user(SCARG(uap, path),
    800 				NSM_FOLLOW_TRYEMULROOT, &vp);
    801 	if (error != 0)
    802 		return (error);
    803 	mp = vp->v_mount;
    804 	sp = &mp->mnt_stat;
    805 	vrele(vp);
    806 	if ((error = VFS_STATVFS(mp, sp)) != 0)
    807 		return (error);
    808 	sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
    809 	return sunstatfs(sp, (void *)SCARG(uap, buf));
    810 }
    811 
    812 int
    813 sunos_sys_fstatfs(struct lwp *l, const struct sunos_sys_fstatfs_args *uap, register_t *retval)
    814 {
    815 	file_t *fp;
    816 	struct mount *mp;
    817 	struct statvfs *sp;
    818 	int error;
    819 
    820 	/* fd_getvnode() will use the descriptor for us */
    821 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
    822 		return (error);
    823 	mp = fp->f_vnode->v_mount;
    824 	sp = &mp->mnt_stat;
    825 	if ((error = VFS_STATVFS(mp, sp)) != 0)
    826 		goto out;
    827 	sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
    828 	error = sunstatfs(sp, (void *)SCARG(uap, buf));
    829  out:
    830  	fd_putfile(SCARG(uap, fd));
    831 	return (error);
    832 }
    833 
    834 int
    835 sunos_sys_exportfs(struct lwp *l, const struct sunos_sys_exportfs_args *uap, register_t *retval)
    836 {
    837 	/*
    838 	 * XXX: should perhaps translate into a mount(2)
    839 	 * with MOUNT_EXPORT?
    840 	 */
    841 	return 0;
    842 }
    843 
    844 int
    845 sunos_sys_mknod(struct lwp *l, const struct sunos_sys_mknod_args *uap, register_t *retval)
    846 {
    847 	struct sys_mkfifo_args fifo_ua;
    848 
    849 	if (S_ISFIFO(SCARG(uap, mode))) {
    850 		SCARG(&fifo_ua, path) = SCARG(uap, path);
    851 		SCARG(&fifo_ua, mode) = SCARG(uap, mode);
    852 		return sys_mkfifo(l, &fifo_ua, retval);
    853 	}
    854 
    855 	return compat_50_sys_mknod(l,
    856 	    (const struct compat_50_sys_mknod_args *)uap, retval);
    857 }
    858 
    859 #define SUNOS_SC_ARG_MAX	1
    860 #define SUNOS_SC_CHILD_MAX	2
    861 #define SUNOS_SC_CLK_TCK	3
    862 #define SUNOS_SC_NGROUPS_MAX	4
    863 #define SUNOS_SC_OPEN_MAX	5
    864 #define SUNOS_SC_JOB_CONTROL	6
    865 #define SUNOS_SC_SAVED_IDS	7
    866 #define SUNOS_SC_VERSION	8
    867 
    868 int
    869 sunos_sys_sysconf(struct lwp *l, const struct sunos_sys_sysconf_args *uap, register_t *retval)
    870 {
    871 
    872 	switch(SCARG(uap, name)) {
    873 	case SUNOS_SC_ARG_MAX:
    874 		*retval = ARG_MAX;
    875 		break;
    876 	case SUNOS_SC_CHILD_MAX:
    877 		*retval = maxproc;
    878 		break;
    879 	case SUNOS_SC_CLK_TCK:
    880 		*retval = 60;		/* should this be `hz', ie. 100? */
    881 		break;
    882 	case SUNOS_SC_NGROUPS_MAX:
    883 		*retval = NGROUPS_MAX;
    884 		break;
    885 	case SUNOS_SC_OPEN_MAX:
    886 		*retval = maxfiles;
    887 		break;
    888 	case SUNOS_SC_JOB_CONTROL:
    889 		*retval = 1;
    890 		break;
    891 	case SUNOS_SC_SAVED_IDS:
    892 #ifdef _POSIX_SAVED_IDS
    893 		*retval = 1;
    894 #else
    895 		*retval = 0;
    896 #endif
    897 		break;
    898 	case SUNOS_SC_VERSION:
    899 		*retval = 198808;
    900 		break;
    901 	default:
    902 		return EINVAL;
    903 	}
    904 	return 0;
    905 }
    906 
    907 #define SUNOS_RLIMIT_NOFILE	6	/* Other RLIMIT_* are the same */
    908 #define SUNOS_RLIM_NLIMITS	7
    909 
    910 int
    911 sunos_sys_getrlimit(struct lwp *l, const struct sunos_sys_getrlimit_args *uap, register_t *retval)
    912 {
    913 	struct compat_43_sys_getrlimit_args ua_43;
    914 
    915 	SCARG(&ua_43, which) = SCARG(uap, which);
    916 	SCARG(&ua_43, rlp) = SCARG(uap, rlp);
    917 
    918 	if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
    919 		return EINVAL;
    920 
    921 	if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
    922 		SCARG(&ua_43, which) = RLIMIT_NOFILE;
    923 
    924 	return compat_43_sys_getrlimit(l, &ua_43, retval);
    925 }
    926 
    927 int
    928 sunos_sys_setrlimit(struct lwp *l, const struct sunos_sys_setrlimit_args *uap, register_t *retval)
    929 {
    930 	struct compat_43_sys_setrlimit_args ua_43;
    931 
    932 	SCARG(&ua_43, which) = SCARG(uap, which);
    933 	SCARG(&ua_43, rlp) = SCARG(uap, rlp);
    934 
    935 	if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
    936 		return EINVAL;
    937 
    938 	if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
    939 		SCARG(&ua_43, which) = RLIMIT_NOFILE;
    940 
    941 	return compat_43_sys_setrlimit(l, &ua_43, retval);
    942 }
    943 
    944 /* for the m68k machines */
    945 #ifndef PT_GETFPREGS
    946 #define PT_GETFPREGS -1
    947 #endif
    948 #ifndef PT_SETFPREGS
    949 #define PT_SETFPREGS -1
    950 #endif
    951 
    952 static const int sreq2breq[] = {
    953 	PT_TRACE_ME,    PT_READ_I,      PT_READ_D,      -1,
    954 	PT_WRITE_I,     PT_WRITE_D,     -1,             PT_CONTINUE,
    955 	PT_KILL,        -1,             PT_ATTACH,      PT_DETACH,
    956 	PT_GETREGS,     PT_SETREGS,     PT_GETFPREGS,   PT_SETFPREGS
    957 };
    958 static const int nreqs = sizeof(sreq2breq) / sizeof(sreq2breq[0]);
    959 
    960 int
    961 sunos_sys_ptrace(struct lwp *l, const struct sunos_sys_ptrace_args *uap, register_t *retval)
    962 {
    963 	struct sys_ptrace_args pa;
    964 	int req;
    965 
    966 #define	sys_ptrace sysent[SYS_ptrace].sy_call
    967 	if (sys_ptrace == sys_nosys)
    968 		return ENOSYS;
    969 
    970 	req = SCARG(uap, req);
    971 
    972 	if (req < 0 || req >= nreqs)
    973 		return (EINVAL);
    974 
    975 	req = sreq2breq[req];
    976 	if (req == -1)
    977 		return (EINVAL);
    978 
    979 	SCARG(&pa, req) = req;
    980 	SCARG(&pa, pid) = (pid_t)SCARG(uap, pid);
    981 	SCARG(&pa, addr) = (void *)SCARG(uap, addr);
    982 	SCARG(&pa, data) = SCARG(uap, data);
    983 
    984 	return sys_ptrace(l, &pa, retval);
    985 }
    986 
    987 /*
    988  * SunOS reboot system call (for compatibility).
    989  * Sun lets you pass in a boot string which the PROM
    990  * saves and provides to the next boot program.
    991  */
    992 
    993 #define SUNOS_RB_ASKNAME	0x001
    994 #define SUNOS_RB_SINGLE 	0x002
    995 #define SUNOS_RB_NOSYNC		0x004
    996 #define SUNOS_RB_HALT		0x008
    997 #define SUNOS_RB_DUMP		0x080
    998 #define	SUNOS_RB_STRING		0x200
    999 
   1000 static struct sunos_howto_conv {
   1001 	int sun_howto;
   1002 	int bsd_howto;
   1003 } sunos_howto_conv[] = {
   1004 	{ SUNOS_RB_ASKNAME,	RB_ASKNAME },
   1005 	{ SUNOS_RB_SINGLE,	RB_SINGLE },
   1006 	{ SUNOS_RB_NOSYNC,	RB_NOSYNC },
   1007 	{ SUNOS_RB_HALT,	RB_HALT },
   1008 	{ SUNOS_RB_DUMP,	RB_DUMP },
   1009 	{ SUNOS_RB_STRING,	RB_STRING },
   1010 	{ 0x000,		0 },
   1011 };
   1012 
   1013 int
   1014 sunos_sys_reboot(struct lwp *l, const struct sunos_sys_reboot_args *uap, register_t *retval)
   1015 {
   1016 	struct sys_reboot_args ua;
   1017 	struct sunos_howto_conv *convp;
   1018 	int error, bsd_howto, sun_howto;
   1019 	char *bootstr;
   1020 	char bs[128];
   1021 
   1022 	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_REBOOT,
   1023 	    0, NULL, NULL, NULL)) != 0)
   1024 		return (error);
   1025 
   1026 	/*
   1027 	 * Convert howto bits to BSD format.
   1028 	 */
   1029 	sun_howto = SCARG(uap, howto);
   1030 	bsd_howto = 0;
   1031 	convp = sunos_howto_conv;
   1032 	while (convp->sun_howto) {
   1033 		if (sun_howto & convp->sun_howto)
   1034 			bsd_howto |= convp->bsd_howto;
   1035 		convp++;
   1036 	}
   1037 
   1038 	/*
   1039 	 * Sun RB_STRING (Get user supplied bootstring.)
   1040 	 * If the machine supports passing a string to the
   1041 	 * next booted kernel.
   1042 	 */
   1043 	if (sun_howto & SUNOS_RB_STRING) {
   1044 		error = copyinstr(SCARG(uap, bootstr), bs, sizeof(bs), 0);
   1045 
   1046 		if (error)
   1047 			bootstr = NULL;
   1048 		else
   1049 			bootstr = bs;
   1050 	} else
   1051 		bootstr = NULL;
   1052 
   1053 	SCARG(&ua, opt) = bsd_howto;
   1054 	SCARG(&ua, bootstr) = bootstr;
   1055 	sys_reboot(l, &ua, retval);
   1056 	return(0);
   1057 }
   1058 
   1059 /*
   1060  * Generalized interface signal handler, 4.3-compatible.
   1061  */
   1062 /* ARGSUSED */
   1063 int
   1064 sunos_sys_sigvec(struct lwp *l, const struct sunos_sys_sigvec_args *uap, register_t *retval)
   1065 {
   1066 	/* {
   1067 		syscallarg(int) signum;
   1068 		syscallarg(struct sigvec *) nsv;
   1069 		syscallarg(struct sigvec *) osv;
   1070 	} */
   1071 	struct sigvec nsv, osv;
   1072 	struct sigaction nsa, osa;
   1073 	int error;
   1074 /*XXX*/extern	void compat_43_sigvec_to_sigaction
   1075 (const struct sigvec *, struct sigaction *);
   1076 /*XXX*/extern	void compat_43_sigaction_to_sigvec
   1077 (const struct sigaction *, struct sigvec *);
   1078 
   1079 	if (SCARG(uap, nsv)) {
   1080 		error = copyin(SCARG(uap, nsv), &nsv, sizeof(nsv));
   1081 		if (error != 0)
   1082 			return (error);
   1083 
   1084 		/*
   1085 		 * SunOS uses the mask 0x0004 as SV_RESETHAND
   1086 		 * meaning: `reset to SIG_DFL on delivery'.
   1087 		 * We support only the bits in: 0xF
   1088 		 * (those bits are the same as ours)
   1089 		 */
   1090 		if (nsv.sv_flags & ~0xF)
   1091 			return (EINVAL);
   1092 
   1093 		compat_43_sigvec_to_sigaction(&nsv, &nsa);
   1094 	}
   1095 	error = sigaction1(l, SCARG(uap, signum),
   1096 			   SCARG(uap, nsv) ? &nsa : 0,
   1097 			   SCARG(uap, osv) ? &osa : 0,
   1098 			   NULL, 0);
   1099 	if (error != 0)
   1100 		return (error);
   1101 
   1102 	if (SCARG(uap, osv)) {
   1103 		compat_43_sigaction_to_sigvec(&osa, &osv);
   1104 		error = copyout(&osv, SCARG(uap, osv), sizeof(osv));
   1105 		if (error != 0)
   1106 			return (error);
   1107 	}
   1108 
   1109 	return (0);
   1110 }
   1111