Home | History | Annotate | Line # | Download | only in sunos
sunos_misc.c revision 1.173.2.1
      1 /*	$NetBSD: sunos_misc.c,v 1.173.2.1 2022/08/03 11:11:31 martin 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.173.2.1 2022/08/03 11:11:31 martin 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 		na.hostname = /* (char *)(u_long) */ sna.hostname;
    281 
    282 		return do_sys_mount(l, "nfs", UIO_SYSSPACE,
    283 		    SCARG(uap, dir), nflags, &na,
    284 		    UIO_SYSSPACE, sizeof na, &dummy);
    285 	}
    286 
    287 	if (strcmp(fsname, "4.2") == 0)
    288 		strcpy(fsname, "ffs");
    289 
    290 	return do_sys_mount(l, fsname, UIO_SYSSPACE,
    291 	    SCARG(uap, dir), nflags, SCARG(uap, data),
    292 	    UIO_USERSPACE, 0, &dummy);
    293 }
    294 
    295 int
    296 async_daemon(struct lwp *l, const void *v, register_t *retval)
    297 {
    298 
    299 	return kpause("fakeniod", false, 0, NULL);
    300 }
    301 
    302 void	native_to_sunos_sigset(const sigset_t *, int *);
    303 void	sunos_to_native_sigset(const int, sigset_t *);
    304 
    305 inline void
    306 native_to_sunos_sigset(const sigset_t *ss, int *mask)
    307 {
    308 
    309 	*mask = ss->__bits[0];
    310 }
    311 
    312 inline void
    313 sunos_to_native_sigset(const int mask, sigset_t *ss)
    314 {
    315 
    316 	memset(ss, 0, sizeof(*ss));
    317 	ss->__bits[0] = mask;
    318 	ss->__bits[1] = 0;
    319 	ss->__bits[2] = 0;
    320 	ss->__bits[3] = 0;
    321 }
    322 
    323 int
    324 sunos_sys_sigpending(struct lwp *l, const struct sunos_sys_sigpending_args *uap, register_t *retval)
    325 {
    326 	sigset_t ss;
    327 	int mask;
    328 
    329 	sigpending1(l, &ss);
    330 	native_to_sunos_sigset(&ss, &mask);
    331 
    332 	return (copyout((void *)&mask, (void *)SCARG(uap, mask), sizeof(int)));
    333 }
    334 
    335 int
    336 sunos_sys_sigsuspend(struct lwp *l, const struct sunos_sys_sigsuspend_args *uap, register_t *retval)
    337 {
    338 	/* {
    339 		syscallarg(int) mask;
    340 	} */
    341 	int mask;
    342 	sigset_t ss;
    343 
    344 	mask = SCARG(uap, mask);
    345 	sunos_to_native_sigset(mask, &ss);
    346 	return (sigsuspend1(l, &ss));
    347 }
    348 
    349 /*
    350  * Read Sun-style directory entries.  We suck them into kernel space so
    351  * that they can be massaged before being copied out to user code.  Like
    352  * SunOS, we squish out `empty' entries.
    353  *
    354  * This is quite ugly, but what do you expect from compatibility code?
    355  */
    356 int
    357 sunos_sys_getdents(struct lwp *l, const struct sunos_sys_getdents_args *uap, register_t *retval)
    358 {
    359 	struct dirent *bdp;
    360 	struct vnode *vp;
    361 	char *inp, *buf;	/* BSD-format */
    362 	int len, reclen;	/* BSD-format */
    363 	char *outp;		/* Sun-format */
    364 	int resid, sunos_reclen;/* Sun-format */
    365 	struct file *fp;
    366 	struct uio auio;
    367 	struct iovec aiov;
    368 	struct sunos_dirent idb;
    369 	off_t off;			/* true file offset */
    370 	int buflen, error, eofflag;
    371 	off_t *cookiebuf, *cookie;
    372 	int ncookies;
    373 
    374 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
    375 		return (error);
    376 
    377 	if ((fp->f_flag & FREAD) == 0) {
    378 		error = EBADF;
    379 		goto out1;
    380 	}
    381 
    382 	vp = fp->f_vnode;
    383 	if (vp->v_type != VDIR) {
    384 		error = EINVAL;
    385 		goto out1;
    386 	}
    387 
    388 	buflen = uimin(MAXBSIZE, SCARG(uap, nbytes));
    389 	buf = malloc(buflen, M_TEMP, M_WAITOK);
    390 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    391 	off = fp->f_offset;
    392 again:
    393 	aiov.iov_base = buf;
    394 	aiov.iov_len = buflen;
    395 	auio.uio_iov = &aiov;
    396 	auio.uio_iovcnt = 1;
    397 	auio.uio_rw = UIO_READ;
    398 	auio.uio_resid = buflen;
    399 	auio.uio_offset = off;
    400 	UIO_SETUP_SYSSPACE(&auio);
    401 	/*
    402 	 * First we read into the malloc'ed buffer, then
    403 	 * we massage it into user space, one record at a time.
    404 	 */
    405 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
    406 	    &ncookies);
    407 	if (error)
    408 		goto out;
    409 
    410 	inp = buf;
    411 	outp = SCARG(uap, buf);
    412 	resid = SCARG(uap, nbytes);
    413 	if ((len = buflen - auio.uio_resid) == 0)
    414 		goto eof;
    415 
    416 	for (cookie = cookiebuf; len > 0; len -= reclen) {
    417 		bdp = (struct dirent *)inp;
    418 		reclen = bdp->d_reclen;
    419 		if (reclen & 3) {
    420 			error = EIO;
    421 			goto out;
    422 		}
    423 		if ((*cookie >> 32) != 0) {
    424 			compat_offseterr(vp, "sunos_getdents");
    425 			error = EINVAL;
    426 			goto out;
    427 		}
    428 		if (bdp->d_fileno == 0) {
    429 			inp += reclen;	/* it is a hole; squish it out */
    430 			if (cookie)
    431 				off = *cookie++;
    432 			else
    433 				off += reclen;
    434 			continue;
    435 		}
    436 		memset(&idb, 0, sizeof(idb));
    437 		sunos_reclen = SUNOS_RECLEN(&idb, bdp->d_namlen);
    438 		if (reclen > len || resid < sunos_reclen) {
    439 			/* entry too big for buffer, so just stop */
    440 			outp++;
    441 			break;
    442 		}
    443 		if (cookie)
    444 			off = *cookie++;	/* each entry points to next */
    445 		else
    446 			off += reclen;
    447 		/*
    448 		 * Massage in place to make a Sun-shaped dirent (otherwise
    449 		 * we have to worry about touching user memory outside of
    450 		 * the copyout() call).
    451 		 */
    452 		idb.d_fileno = bdp->d_fileno;
    453 		idb.d_off = off;
    454 		idb.d_reclen = sunos_reclen;
    455 		idb.d_namlen = bdp->d_namlen;
    456 		strlcpy(idb.d_name, bdp->d_name, sizeof(idb.d_name));
    457 		if ((error = copyout((void *)&idb, outp, sunos_reclen)) != 0)
    458 			goto out;
    459 		/* advance past this real entry */
    460 		inp += reclen;
    461 		/* advance output past Sun-shaped entry */
    462 		outp += sunos_reclen;
    463 		resid -= sunos_reclen;
    464 	}
    465 
    466 	/* if we squished out the whole block, try again */
    467 	if (outp == SCARG(uap, buf)) {
    468 		if (cookiebuf)
    469 			free(cookiebuf, M_TEMP);
    470 		cookiebuf = NULL;
    471 		goto again;
    472 	}
    473 	fp->f_offset = off;		/* update the vnode offset */
    474 
    475 eof:
    476 	*retval = SCARG(uap, nbytes) - resid;
    477 out:
    478 	VOP_UNLOCK(vp);
    479 	free(cookiebuf, M_TEMP);
    480 	free(buf, M_TEMP);
    481  out1:
    482  	fd_putfile(SCARG(uap, fd));
    483 	return (error);
    484 }
    485 
    486 #define	SUNOS__MAP_NEW	0x80000000	/* if not, old mmap & cannot handle */
    487 
    488 int
    489 sunos_sys_mmap(struct lwp *l, const struct sunos_sys_mmap_args *uap, register_t *retval)
    490 {
    491 	struct sys_mmap_args ouap;
    492 
    493 	/*
    494 	 * Verify the arguments.
    495 	 */
    496 	if (SCARG(uap, prot) & ~(PROT_READ|PROT_WRITE|PROT_EXEC))
    497 		return (EINVAL);			/* XXX still needed? */
    498 
    499 	if ((SCARG(uap, flags) & SUNOS__MAP_NEW) == 0)
    500 		return (EINVAL);
    501 
    502 	SCARG(&ouap, flags) = SCARG(uap, flags) & ~SUNOS__MAP_NEW;
    503 	SCARG(&ouap, addr) = SCARG(uap, addr);
    504 	SCARG(&ouap, len) = SCARG(uap, len);
    505 	SCARG(&ouap, prot) = SCARG(uap, prot);
    506 	SCARG(&ouap, fd) = SCARG(uap, fd);
    507 	SCARG(&ouap, pos) = SCARG(uap, pos);
    508 
    509 	return (sys_mmap(l, &ouap, retval));
    510 }
    511 
    512 #define	MC_SYNC		1
    513 #define	MC_LOCK		2
    514 #define	MC_UNLOCK	3
    515 #define	MC_ADVISE	4
    516 #define	MC_LOCKAS	5
    517 #define	MC_UNLOCKAS	6
    518 
    519 int
    520 sunos_sys_mctl(struct lwp *l, const struct sunos_sys_mctl_args *uap, register_t *retval)
    521 {
    522 
    523 	switch (SCARG(uap, func)) {
    524 	case MC_ADVISE:		/* ignore for now */
    525 		return (0);
    526 	case MC_SYNC:		/* translate to msync */
    527 		return (sys___msync13(l, (const void *)uap, retval));
    528 	default:
    529 		return (EINVAL);
    530 	}
    531 }
    532 
    533 int
    534 sunos_sys_setsockopt(struct lwp *l, const struct sunos_sys_setsockopt_args *uap, register_t *retval)
    535 {
    536 	struct sockopt sopt;
    537 	struct socket *so;
    538 	int name = SCARG(uap, name);
    539 	int error;
    540 
    541 	/* fd_getsock() will use the descriptor for us */
    542 	if ((error = fd_getsock(SCARG(uap, s), &so)) != 0)
    543 		return (error);
    544 #define	SO_DONTLINGER (~SO_LINGER)
    545 	if (name == SO_DONTLINGER) {
    546 		struct linger lg;
    547 
    548 		lg.l_onoff = 0;
    549 		error = so_setsockopt(l, so, SCARG(uap, level), SO_LINGER,
    550 		    &lg, sizeof(lg));
    551 		goto out;
    552 	}
    553 	if (SCARG(uap, level) == IPPROTO_IP) {
    554 #define		SUNOS_IP_MULTICAST_IF		2
    555 #define		SUNOS_IP_MULTICAST_TTL		3
    556 #define		SUNOS_IP_MULTICAST_LOOP		4
    557 #define		SUNOS_IP_ADD_MEMBERSHIP		5
    558 #define		SUNOS_IP_DROP_MEMBERSHIP	6
    559 		static const int ipoptxlat[] = {
    560 			IP_MULTICAST_IF,
    561 			IP_MULTICAST_TTL,
    562 			IP_MULTICAST_LOOP,
    563 			IP_ADD_MEMBERSHIP,
    564 			IP_DROP_MEMBERSHIP
    565 		};
    566 		if (name >= SUNOS_IP_MULTICAST_IF &&
    567 		    name <= SUNOS_IP_DROP_MEMBERSHIP) {
    568 			name = ipoptxlat[name - SUNOS_IP_MULTICAST_IF];
    569 		}
    570 	}
    571 	if (SCARG(uap, valsize) > MLEN) {
    572 		error = EINVAL;
    573 		goto out;
    574 	}
    575 	sockopt_init(&sopt, SCARG(uap, level), name, SCARG(uap, valsize));
    576 	if (SCARG(uap, val)) {
    577 		error = copyin(SCARG(uap, val), sopt.sopt_data,
    578 		    (u_int)SCARG(uap, valsize));
    579 	}
    580 	if (error == 0)
    581 		error = sosetopt(so, &sopt);
    582 	sockopt_destroy(&sopt);
    583  out:
    584  	fd_putfile(SCARG(uap, s));
    585 	return (error);
    586 }
    587 
    588 static inline int sunos_sys_socket_common(struct lwp *, register_t *,
    589 					      int type);
    590 static inline int
    591 sunos_sys_socket_common(struct lwp *l, register_t *retval, int type)
    592 {
    593 	struct socket *so;
    594 	int error, fd;
    595 
    596 	/* fd_getsock() will use the descriptor for us */
    597 	fd = (int)*retval;
    598 	if ((error = fd_getsock(fd, &so)) == 0) {
    599 		if (type == SOCK_DGRAM)
    600 			so->so_options |= SO_BROADCAST;
    601 		fd_putfile(fd);
    602 	}
    603 	return (error);
    604 }
    605 
    606 int
    607 sunos_sys_socket(struct lwp *l, const struct sunos_sys_socket_args *uap, register_t *retval)
    608 {
    609 	/* {
    610 		syscallarg(int) domain;
    611 		syscallarg(int) type;
    612 		syscallarg(int) protocol;
    613 	} */
    614 	int error;
    615 
    616 	error = compat_30_sys_socket(l, (const void *)uap, retval);
    617 	if (error)
    618 		return (error);
    619 	return sunos_sys_socket_common(l, retval, SCARG(uap, type));
    620 }
    621 
    622 int
    623 sunos_sys_socketpair(struct lwp *l, const struct sunos_sys_socketpair_args *uap, register_t *retval)
    624 {
    625 	/* {
    626 		syscallarg(int) domain;
    627 		syscallarg(int) type;
    628 		syscallarg(int) protocol;
    629 		syscallarg(int *) rsv;
    630 	} */
    631 	int error;
    632 
    633 	error = sys_socketpair(l, (const void *)uap, retval);
    634 	if (error)
    635 		return (error);
    636 	return sunos_sys_socket_common(l, retval, SCARG(uap, type));
    637 }
    638 
    639 /*
    640  * XXX: This needs cleaning up.
    641  */
    642 int
    643 sunos_sys_auditsys(struct lwp *l, const struct sunos_sys_auditsys_args *uap, register_t *retval)
    644 {
    645 	return 0;
    646 }
    647 
    648 int
    649 sunos_sys_uname(struct lwp *l, const struct sunos_sys_uname_args *uap, register_t *retval)
    650 {
    651 	struct sunos_utsname sut;
    652 
    653 	memset(&sut, 0, sizeof(sut));
    654 
    655 	memcpy(sut.sysname, ostype, sizeof(sut.sysname) - 1);
    656 	memcpy(sut.nodename, hostname, sizeof(sut.nodename));
    657 	sut.nodename[sizeof(sut.nodename)-1] = '\0';
    658 	memcpy(sut.release, osrelease, sizeof(sut.release) - 1);
    659 	memcpy(sut.version, "1", sizeof(sut.version) - 1);
    660 	memcpy(sut.machine, machine, sizeof(sut.machine) - 1);
    661 
    662 	return copyout((void *)&sut, (void *)SCARG(uap, name),
    663 	    sizeof(struct sunos_utsname));
    664 }
    665 
    666 int
    667 sunos_sys_setpgrp(struct lwp *l, const struct sunos_sys_setpgrp_args *uap, register_t *retval)
    668 {
    669 	struct proc *p = l->l_proc;
    670 
    671 	/*
    672 	 * difference to our setpgid call is to include backwards
    673 	 * compatibility to pre-setsid() binaries. Do setsid()
    674 	 * instead of setpgid() in those cases where the process
    675 	 * tries to create a new session the old way.
    676 	 */
    677 	if (!SCARG(uap, pgid) &&
    678 	    (!SCARG(uap, pid) || SCARG(uap, pid) == p->p_pid))
    679 		return sys_setsid(l, NULL, retval);
    680 	else
    681 		return sys_setpgid(l, (const void *)uap, retval);
    682 }
    683 
    684 int
    685 sunos_sys_open(struct lwp *l, const struct sunos_sys_open_args *uap, register_t *retval)
    686 {
    687 	struct proc *p = l->l_proc;
    688 	struct sys_open_args open_ua;
    689 	int smode, nmode;
    690 	int noctty;
    691 	int ret;
    692 
    693 	/* convert mode into NetBSD mode */
    694 	smode = SCARG(uap, flags);
    695 	noctty = smode & 0x8000;
    696 	nmode =	smode &
    697 		(0x0001 | 0x0002 | 0x0008 | 0x0040 | 0x0200 | 0x0400 | 0x0800);
    698 	nmode |= ((smode & (0x0004 | 0x1000 | 0x4000)) ? O_NONBLOCK : 0);
    699 	nmode |= ((smode & 0x0080) ? O_SHLOCK : 0);
    700 	nmode |= ((smode & 0x0100) ? O_EXLOCK : 0);
    701 	nmode |= ((smode & 0x2000) ? O_FSYNC : 0);
    702 
    703 	SCARG(&open_ua, path) = SCARG(uap, path);
    704 	SCARG(&open_ua, flags) = nmode;
    705 	SCARG(&open_ua, mode) = SCARG(uap, mode);
    706 	ret = sys_open(l, &open_ua, retval);
    707 
    708 	/* XXXSMP */
    709 	if (!ret && !noctty && SESS_LEADER(p) && !(p->p_lflag & PL_CONTROLT)) {
    710 		file_t *fp;
    711 		int fd;
    712 
    713 		fd = (int)*retval;
    714 		fp = fd_getfile(fd);
    715 
    716 		/* ignore any error, just give it a try */
    717 		if (fp != NULL) {
    718 			if (fp->f_type == DTYPE_VNODE)
    719 				(fp->f_ops->fo_ioctl)(fp, TIOCSCTTY, NULL);
    720 			fd_putfile(fd);
    721 		}
    722 	}
    723 	return ret;
    724 }
    725 
    726 int
    727 sunos_sys_ustat(struct lwp *l, const struct sunos_sys_ustat_args *uap, register_t *retval)
    728 {
    729 	struct sunos_ustat us;
    730 	int error;
    731 
    732 	memset(&us, 0, sizeof us);
    733 
    734 	/*
    735 	 * XXX: should set f_tfree and f_tinode at least
    736 	 * How do we translate dev -> fstat? (and then to sunos_ustat)
    737 	 */
    738 
    739 	if ((error = copyout(&us, SCARG(uap, buf), sizeof us)) != 0)
    740 		return (error);
    741 	return 0;
    742 }
    743 
    744 int
    745 sunos_sys_quotactl(struct lwp *l, const struct sunos_sys_quotactl_args *uap, register_t *retval)
    746 {
    747 
    748 	return EINVAL;
    749 }
    750 
    751 int
    752 sunos_sys_vhangup(struct lwp *l, const void *v, register_t *retval)
    753 {
    754 	struct proc *p = l->l_proc;
    755 	struct session *sp = p->p_session;
    756 
    757 	if (sp->s_ttyvp == 0)
    758 		return 0;
    759 
    760 	if (sp->s_ttyp && sp->s_ttyp->t_session == sp && sp->s_ttyp->t_pgrp)
    761 		pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
    762 
    763 	(void) ttywait(sp->s_ttyp);
    764 	if (sp->s_ttyvp)
    765 		VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
    766 	if (sp->s_ttyvp)
    767 		vrele(sp->s_ttyvp);
    768 	sp->s_ttyvp = NULL;
    769 
    770 	return 0;
    771 }
    772 
    773 static int
    774 sunstatfs(struct statvfs *sp, void *buf)
    775 {
    776 	struct sunos_statfs ssfs;
    777 
    778 	memset(&ssfs, 0, sizeof ssfs);
    779 	ssfs.f_type = 0;
    780 	ssfs.f_bsize = sp->f_bsize;
    781 	ssfs.f_blocks = sp->f_blocks;
    782 	ssfs.f_bfree = sp->f_bfree;
    783 	ssfs.f_bavail = sp->f_bavail;
    784 	ssfs.f_files = sp->f_files;
    785 	ssfs.f_ffree = sp->f_ffree;
    786 	ssfs.f_fsid = sp->f_fsidx;
    787 	return copyout((void *)&ssfs, buf, sizeof ssfs);
    788 }
    789 
    790 int
    791 sunos_sys_statfs(struct lwp *l, const struct sunos_sys_statfs_args *uap, register_t *retval)
    792 {
    793 	struct mount *mp;
    794 	struct statvfs *sp;
    795 	int error;
    796 	struct vnode *vp;
    797 
    798 	error = namei_simple_user(SCARG(uap, path),
    799 				NSM_FOLLOW_TRYEMULROOT, &vp);
    800 	if (error != 0)
    801 		return (error);
    802 	mp = vp->v_mount;
    803 	sp = &mp->mnt_stat;
    804 	vrele(vp);
    805 	if ((error = VFS_STATVFS(mp, sp)) != 0)
    806 		return (error);
    807 	sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
    808 	return sunstatfs(sp, (void *)SCARG(uap, buf));
    809 }
    810 
    811 int
    812 sunos_sys_fstatfs(struct lwp *l, const struct sunos_sys_fstatfs_args *uap, register_t *retval)
    813 {
    814 	file_t *fp;
    815 	struct mount *mp;
    816 	struct statvfs *sp;
    817 	int error;
    818 
    819 	/* fd_getvnode() will use the descriptor for us */
    820 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
    821 		return (error);
    822 	mp = fp->f_vnode->v_mount;
    823 	sp = &mp->mnt_stat;
    824 	if ((error = VFS_STATVFS(mp, sp)) != 0)
    825 		goto out;
    826 	sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
    827 	error = sunstatfs(sp, (void *)SCARG(uap, buf));
    828  out:
    829  	fd_putfile(SCARG(uap, fd));
    830 	return (error);
    831 }
    832 
    833 int
    834 sunos_sys_exportfs(struct lwp *l, const struct sunos_sys_exportfs_args *uap, register_t *retval)
    835 {
    836 	/*
    837 	 * XXX: should perhaps translate into a mount(2)
    838 	 * with MOUNT_EXPORT?
    839 	 */
    840 	return 0;
    841 }
    842 
    843 int
    844 sunos_sys_mknod(struct lwp *l, const struct sunos_sys_mknod_args *uap, register_t *retval)
    845 {
    846 	struct sys_mkfifo_args fifo_ua;
    847 
    848 	if (S_ISFIFO(SCARG(uap, mode))) {
    849 		SCARG(&fifo_ua, path) = SCARG(uap, path);
    850 		SCARG(&fifo_ua, mode) = SCARG(uap, mode);
    851 		return sys_mkfifo(l, &fifo_ua, retval);
    852 	}
    853 
    854 	return compat_50_sys_mknod(l,
    855 	    (const struct compat_50_sys_mknod_args *)uap, retval);
    856 }
    857 
    858 #define SUNOS_SC_ARG_MAX	1
    859 #define SUNOS_SC_CHILD_MAX	2
    860 #define SUNOS_SC_CLK_TCK	3
    861 #define SUNOS_SC_NGROUPS_MAX	4
    862 #define SUNOS_SC_OPEN_MAX	5
    863 #define SUNOS_SC_JOB_CONTROL	6
    864 #define SUNOS_SC_SAVED_IDS	7
    865 #define SUNOS_SC_VERSION	8
    866 
    867 int
    868 sunos_sys_sysconf(struct lwp *l, const struct sunos_sys_sysconf_args *uap, register_t *retval)
    869 {
    870 
    871 	switch(SCARG(uap, name)) {
    872 	case SUNOS_SC_ARG_MAX:
    873 		*retval = ARG_MAX;
    874 		break;
    875 	case SUNOS_SC_CHILD_MAX:
    876 		*retval = maxproc;
    877 		break;
    878 	case SUNOS_SC_CLK_TCK:
    879 		*retval = 60;		/* should this be `hz', ie. 100? */
    880 		break;
    881 	case SUNOS_SC_NGROUPS_MAX:
    882 		*retval = NGROUPS_MAX;
    883 		break;
    884 	case SUNOS_SC_OPEN_MAX:
    885 		*retval = maxfiles;
    886 		break;
    887 	case SUNOS_SC_JOB_CONTROL:
    888 		*retval = 1;
    889 		break;
    890 	case SUNOS_SC_SAVED_IDS:
    891 #ifdef _POSIX_SAVED_IDS
    892 		*retval = 1;
    893 #else
    894 		*retval = 0;
    895 #endif
    896 		break;
    897 	case SUNOS_SC_VERSION:
    898 		*retval = 198808;
    899 		break;
    900 	default:
    901 		return EINVAL;
    902 	}
    903 	return 0;
    904 }
    905 
    906 #define SUNOS_RLIMIT_NOFILE	6	/* Other RLIMIT_* are the same */
    907 #define SUNOS_RLIM_NLIMITS	7
    908 
    909 int
    910 sunos_sys_getrlimit(struct lwp *l, const struct sunos_sys_getrlimit_args *uap, register_t *retval)
    911 {
    912 	struct compat_43_sys_getrlimit_args ua_43;
    913 
    914 	SCARG(&ua_43, which) = SCARG(uap, which);
    915 	SCARG(&ua_43, rlp) = SCARG(uap, rlp);
    916 
    917 	if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
    918 		return EINVAL;
    919 
    920 	if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
    921 		SCARG(&ua_43, which) = RLIMIT_NOFILE;
    922 
    923 	return compat_43_sys_getrlimit(l, &ua_43, retval);
    924 }
    925 
    926 int
    927 sunos_sys_setrlimit(struct lwp *l, const struct sunos_sys_setrlimit_args *uap, register_t *retval)
    928 {
    929 	struct compat_43_sys_setrlimit_args ua_43;
    930 
    931 	SCARG(&ua_43, which) = SCARG(uap, which);
    932 	SCARG(&ua_43, rlp) = SCARG(uap, rlp);
    933 
    934 	if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
    935 		return EINVAL;
    936 
    937 	if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
    938 		SCARG(&ua_43, which) = RLIMIT_NOFILE;
    939 
    940 	return compat_43_sys_setrlimit(l, &ua_43, retval);
    941 }
    942 
    943 /* for the m68k machines */
    944 #ifndef PT_GETFPREGS
    945 #define PT_GETFPREGS -1
    946 #endif
    947 #ifndef PT_SETFPREGS
    948 #define PT_SETFPREGS -1
    949 #endif
    950 
    951 static const int sreq2breq[] = {
    952 	PT_TRACE_ME,    PT_READ_I,      PT_READ_D,      -1,
    953 	PT_WRITE_I,     PT_WRITE_D,     -1,             PT_CONTINUE,
    954 	PT_KILL,        -1,             PT_ATTACH,      PT_DETACH,
    955 	PT_GETREGS,     PT_SETREGS,     PT_GETFPREGS,   PT_SETFPREGS
    956 };
    957 static const int nreqs = sizeof(sreq2breq) / sizeof(sreq2breq[0]);
    958 
    959 int
    960 sunos_sys_ptrace(struct lwp *l, const struct sunos_sys_ptrace_args *uap, register_t *retval)
    961 {
    962 	struct sys_ptrace_args pa;
    963 	int req;
    964 
    965 #define	sys_ptrace sysent[SYS_ptrace].sy_call
    966 	if (sys_ptrace == sys_nosys)
    967 		return ENOSYS;
    968 
    969 	req = SCARG(uap, req);
    970 
    971 	if (req < 0 || req >= nreqs)
    972 		return (EINVAL);
    973 
    974 	req = sreq2breq[req];
    975 	if (req == -1)
    976 		return (EINVAL);
    977 
    978 	SCARG(&pa, req) = req;
    979 	SCARG(&pa, pid) = (pid_t)SCARG(uap, pid);
    980 	SCARG(&pa, addr) = (void *)SCARG(uap, addr);
    981 	SCARG(&pa, data) = SCARG(uap, data);
    982 
    983 	return sys_ptrace(l, &pa, retval);
    984 }
    985 
    986 /*
    987  * SunOS reboot system call (for compatibility).
    988  * Sun lets you pass in a boot string which the PROM
    989  * saves and provides to the next boot program.
    990  */
    991 
    992 #define SUNOS_RB_ASKNAME	0x001
    993 #define SUNOS_RB_SINGLE 	0x002
    994 #define SUNOS_RB_NOSYNC		0x004
    995 #define SUNOS_RB_HALT		0x008
    996 #define SUNOS_RB_DUMP		0x080
    997 #define	SUNOS_RB_STRING		0x200
    998 
    999 static struct sunos_howto_conv {
   1000 	int sun_howto;
   1001 	int bsd_howto;
   1002 } sunos_howto_conv[] = {
   1003 	{ SUNOS_RB_ASKNAME,	RB_ASKNAME },
   1004 	{ SUNOS_RB_SINGLE,	RB_SINGLE },
   1005 	{ SUNOS_RB_NOSYNC,	RB_NOSYNC },
   1006 	{ SUNOS_RB_HALT,	RB_HALT },
   1007 	{ SUNOS_RB_DUMP,	RB_DUMP },
   1008 	{ SUNOS_RB_STRING,	RB_STRING },
   1009 	{ 0x000,		0 },
   1010 };
   1011 
   1012 int
   1013 sunos_sys_reboot(struct lwp *l, const struct sunos_sys_reboot_args *uap, register_t *retval)
   1014 {
   1015 	struct sys_reboot_args ua;
   1016 	struct sunos_howto_conv *convp;
   1017 	int error, bsd_howto, sun_howto;
   1018 	char *bootstr;
   1019 	char bs[128];
   1020 
   1021 	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_REBOOT,
   1022 	    0, NULL, NULL, NULL)) != 0)
   1023 		return (error);
   1024 
   1025 	/*
   1026 	 * Convert howto bits to BSD format.
   1027 	 */
   1028 	sun_howto = SCARG(uap, howto);
   1029 	bsd_howto = 0;
   1030 	convp = sunos_howto_conv;
   1031 	while (convp->sun_howto) {
   1032 		if (sun_howto & convp->sun_howto)
   1033 			bsd_howto |= convp->bsd_howto;
   1034 		convp++;
   1035 	}
   1036 
   1037 	/*
   1038 	 * Sun RB_STRING (Get user supplied bootstring.)
   1039 	 * If the machine supports passing a string to the
   1040 	 * next booted kernel.
   1041 	 */
   1042 	if (sun_howto & SUNOS_RB_STRING) {
   1043 		error = copyinstr(SCARG(uap, bootstr), bs, sizeof(bs), 0);
   1044 
   1045 		if (error)
   1046 			bootstr = NULL;
   1047 		else
   1048 			bootstr = bs;
   1049 	} else
   1050 		bootstr = NULL;
   1051 
   1052 	SCARG(&ua, opt) = bsd_howto;
   1053 	SCARG(&ua, bootstr) = bootstr;
   1054 	sys_reboot(l, &ua, retval);
   1055 	return(0);
   1056 }
   1057 
   1058 /*
   1059  * Generalized interface signal handler, 4.3-compatible.
   1060  */
   1061 /* ARGSUSED */
   1062 int
   1063 sunos_sys_sigvec(struct lwp *l, const struct sunos_sys_sigvec_args *uap, register_t *retval)
   1064 {
   1065 	/* {
   1066 		syscallarg(int) signum;
   1067 		syscallarg(struct sigvec *) nsv;
   1068 		syscallarg(struct sigvec *) osv;
   1069 	} */
   1070 	struct sigvec nsv, osv;
   1071 	struct sigaction nsa, osa;
   1072 	int error;
   1073 /*XXX*/extern	void compat_43_sigvec_to_sigaction
   1074 (const struct sigvec *, struct sigaction *);
   1075 /*XXX*/extern	void compat_43_sigaction_to_sigvec
   1076 (const struct sigaction *, struct sigvec *);
   1077 
   1078 	if (SCARG(uap, nsv)) {
   1079 		error = copyin(SCARG(uap, nsv), &nsv, sizeof(nsv));
   1080 		if (error != 0)
   1081 			return (error);
   1082 
   1083 		/*
   1084 		 * SunOS uses the mask 0x0004 as SV_RESETHAND
   1085 		 * meaning: `reset to SIG_DFL on delivery'.
   1086 		 * We support only the bits in: 0xF
   1087 		 * (those bits are the same as ours)
   1088 		 */
   1089 		if (nsv.sv_flags & ~0xF)
   1090 			return (EINVAL);
   1091 
   1092 		compat_43_sigvec_to_sigaction(&nsv, &nsa);
   1093 	}
   1094 	error = sigaction1(l, SCARG(uap, signum),
   1095 			   SCARG(uap, nsv) ? &nsa : 0,
   1096 			   SCARG(uap, osv) ? &osa : 0,
   1097 			   NULL, 0);
   1098 	if (error != 0)
   1099 		return (error);
   1100 
   1101 	if (SCARG(uap, osv)) {
   1102 		compat_43_sigaction_to_sigvec(&osa, &osv);
   1103 		error = copyout(&osv, SCARG(uap, osv), sizeof(osv));
   1104 		if (error != 0)
   1105 			return (error);
   1106 	}
   1107 
   1108 	return (0);
   1109 }
   1110