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