Home | History | Annotate | Line # | Download | only in sunos
sunos_misc.c revision 1.158.6.2
      1 /*	$NetBSD: sunos_misc.c,v 1.158.6.2 2008/06/29 09:33:04 mjf 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.158.6.2 2008/06/29 09:33:04 mjf Exp $");
     54 
     55 #if defined(_KERNEL_OPT)
     56 #include "opt_nfsserver.h"
     57 #include "opt_ptrace.h"
     58 #include "fs_nfs.h"
     59 #endif
     60 
     61 #include <sys/param.h>
     62 #include <sys/systm.h>
     63 #include <sys/namei.h>
     64 #include <sys/proc.h>
     65 #include <sys/dirent.h>
     66 #include <sys/file.h>
     67 #include <sys/stat.h>
     68 #include <sys/filedesc.h>
     69 #include <sys/ioctl.h>
     70 #include <sys/kernel.h>
     71 #include <sys/reboot.h>
     72 #include <sys/malloc.h>
     73 #include <sys/mbuf.h>
     74 #include <sys/mman.h>
     75 #include <sys/mount.h>
     76 #include <sys/ptrace.h>
     77 #include <sys/resource.h>
     78 #include <sys/resourcevar.h>
     79 #include <sys/signal.h>
     80 #include <sys/signalvar.h>
     81 #include <sys/socket.h>
     82 #include <sys/tty.h>
     83 #include <sys/vnode.h>
     84 #include <sys/uio.h>
     85 #include <sys/wait.h>
     86 #include <sys/utsname.h>
     87 #include <sys/unistd.h>
     88 #include <sys/syscall.h>
     89 #include <sys/syscallargs.h>
     90 #include <sys/conf.h>
     91 #include <sys/socketvar.h>
     92 #include <sys/exec.h>
     93 #include <sys/swap.h>
     94 #include <sys/kauth.h>
     95 
     96 #include <compat/sys/signal.h>
     97 
     98 #include <compat/sunos/sunos.h>
     99 #include <compat/sunos/sunos_syscallargs.h>
    100 #include <compat/common/compat_util.h>
    101 #include <compat/sunos/sunos_dirent.h>
    102 #include <compat/sys/mount.h>
    103 
    104 #include <netinet/in.h>
    105 
    106 #include <miscfs/specfs/specdev.h>
    107 
    108 #include <nfs/rpcv2.h>
    109 #include <nfs/nfsproto.h>
    110 #include <nfs/nfs.h>
    111 #include <nfs/nfsmount.h>
    112 
    113 static int sunstatfs(struct statvfs *, void *);
    114 
    115 int
    116 sunos_sys_stime(struct lwp *l, const struct sunos_sys_stime_args *uap, register_t *retval)
    117 {
    118 	struct timeval tv;
    119 	int error;
    120 
    121 	error = copyin(SCARG(uap, tp), &tv.tv_sec, sizeof(tv.tv_sec));
    122 	if (error)
    123 		return error;
    124 	tv.tv_usec = 0;
    125 
    126 	return settimeofday1(&tv, false, NULL, l, true);
    127 }
    128 
    129 int
    130 sunos_sys_wait4(struct lwp *l, const struct sunos_sys_wait4_args *uap, register_t *retval)
    131 {
    132 	struct sys_wait4_args bsd_ua;
    133 
    134 	SCARG(&bsd_ua, pid) = SCARG(uap, pid) == 0 ? WAIT_ANY : SCARG(uap, pid);
    135 	SCARG(&bsd_ua, status) = SCARG(uap, status);
    136 	SCARG(&bsd_ua, options) = SCARG(uap, options);
    137 	SCARG(&bsd_ua, rusage) = SCARG(uap, rusage);
    138 
    139 	return (sys_wait4(l, &bsd_ua, retval));
    140 }
    141 
    142 int
    143 sunos_sys_creat(struct lwp *l, const struct sunos_sys_creat_args *uap, register_t *retval)
    144 {
    145 	struct sys_open_args ouap;
    146 
    147 	SCARG(&ouap, path) = SCARG(uap, path);
    148 	SCARG(&ouap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
    149 	SCARG(&ouap, mode) = SCARG(uap, mode);
    150 
    151 	return (sys_open(l, &ouap, retval));
    152 }
    153 
    154 int
    155 sunos_sys_execv(struct lwp *l, const struct sunos_sys_execv_args *uap, register_t *retval)
    156 {
    157 	/* {
    158 		syscallarg(const char *) path;
    159 		syscallarg(char **) argv;
    160 	} */
    161 	struct sys_execve_args ap;
    162 
    163 	SCARG(&ap, path) = SCARG(uap, path);
    164 	SCARG(&ap, argp) = SCARG(uap, argp);
    165 	SCARG(&ap, envp) = NULL;
    166 
    167 	return (sys_execve(l, &ap, retval));
    168 }
    169 
    170 int
    171 sunos_sys_execve(struct lwp *l, const struct sunos_sys_execve_args *uap, register_t *retval)
    172 {
    173 	/* {
    174 		syscallarg(const char *) path;
    175 		syscallarg(char **) argv;
    176 		syscallarg(char **) envp;
    177 	} */
    178 	struct sys_execve_args ap;
    179 
    180 	SCARG(&ap, path) = SCARG(uap, path);
    181 	SCARG(&ap, argp) = SCARG(uap, argp);
    182 	SCARG(&ap, envp) = SCARG(uap, envp);
    183 
    184 	return (sys_execve(l, &ap, retval));
    185 }
    186 
    187 int
    188 sunos_sys_omsync(struct lwp *l, const struct sunos_sys_omsync_args *uap, register_t *retval)
    189 {
    190 	struct sys___msync13_args ouap;
    191 
    192 	SCARG(&ouap, addr) = SCARG(uap, addr);
    193 	SCARG(&ouap, len) = SCARG(uap, len);
    194 	SCARG(&ouap, flags) = SCARG(uap, flags);
    195 
    196 	return (sys___msync13(l, &ouap, retval));
    197 }
    198 
    199 int
    200 sunos_sys_unmount(struct lwp *l, const struct sunos_sys_unmount_args *uap, register_t *retval)
    201 {
    202 	struct sys_unmount_args ouap;
    203 
    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 		na.hostname = /* (char *)(u_long) */ sna.hostname;
    287 
    288 		return do_sys_mount(l, vfs_getopsbyname("nfs"), NULL,
    289 		    SCARG(uap, dir), nflags, &na,
    290 		    UIO_SYSSPACE, sizeof na, &dummy);
    291 	}
    292 
    293 	if (strcmp(fsname, "4.2") == 0)
    294 		strcpy(fsname, "ffs");
    295 
    296 	return do_sys_mount(l, vfs_getopsbyname(fsname), NULL,
    297 	    SCARG(uap, dir), nflags, SCARG(uap, data),
    298 	    UIO_USERSPACE, 0, &dummy);
    299 }
    300 
    301 #if defined(NFS)
    302 int
    303 async_daemon(struct lwp *l, const void *v, register_t *retval)
    304 {
    305 	struct sys_nfssvc_args ouap;
    306 
    307 	SCARG(&ouap, flag) = NFSSVC_BIOD;
    308 	SCARG(&ouap, argp) = NULL;
    309 
    310 	return (sys_nfssvc(l, &ouap, retval));
    311 }
    312 #endif /* NFS */
    313 
    314 void	native_to_sunos_sigset(const sigset_t *, int *);
    315 void	sunos_to_native_sigset(const int, sigset_t *);
    316 
    317 inline void
    318 native_to_sunos_sigset(const sigset_t *ss, int *mask)
    319 {
    320 	*mask = ss->__bits[0];
    321 }
    322 
    323 inline void
    324 sunos_to_native_sigset(const int mask, sigset_t *ss)
    325 {
    326 
    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_data;
    393 	if (vp->v_type != VDIR) {
    394 		error = EINVAL;
    395 		goto out1;
    396 	}
    397 
    398 	buflen = min(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 			panic("sunos_getdents");
    431 		if ((*cookie >> 32) != 0) {
    432 			compat_offseterr(vp, "sunos_getdents");
    433 			error = EINVAL;
    434 			goto out;
    435 		}
    436 		if (bdp->d_fileno == 0) {
    437 			inp += reclen;	/* it is a hole; squish it out */
    438 			if (cookie)
    439 				off = *cookie++;
    440 			else
    441 				off += reclen;
    442 			continue;
    443 		}
    444 		sunos_reclen = SUNOS_RECLEN(&idb, bdp->d_namlen);
    445 		if (reclen > len || resid < sunos_reclen) {
    446 			/* entry too big for buffer, so just stop */
    447 			outp++;
    448 			break;
    449 		}
    450 		if (cookie)
    451 			off = *cookie++;	/* each entry points to next */
    452 		else
    453 			off += reclen;
    454 		/*
    455 		 * Massage in place to make a Sun-shaped dirent (otherwise
    456 		 * we have to worry about touching user memory outside of
    457 		 * the copyout() call).
    458 		 */
    459 		idb.d_fileno = bdp->d_fileno;
    460 		idb.d_off = off;
    461 		idb.d_reclen = sunos_reclen;
    462 		idb.d_namlen = bdp->d_namlen;
    463 		strlcpy(idb.d_name, bdp->d_name, sizeof(idb.d_name));
    464 		if ((error = copyout((void *)&idb, outp, sunos_reclen)) != 0)
    465 			goto out;
    466 		/* advance past this real entry */
    467 		inp += reclen;
    468 		/* advance output past Sun-shaped entry */
    469 		outp += sunos_reclen;
    470 		resid -= sunos_reclen;
    471 	}
    472 
    473 	/* if we squished out the whole block, try again */
    474 	if (outp == SCARG(uap, buf))
    475 		goto again;
    476 	fp->f_offset = off;		/* update the vnode offset */
    477 
    478 eof:
    479 	*retval = SCARG(uap, nbytes) - resid;
    480 out:
    481 	VOP_UNLOCK(vp, 0);
    482 	free(cookiebuf, M_TEMP);
    483 	free(buf, M_TEMP);
    484  out1:
    485  	fd_putfile(SCARG(uap, fd));
    486 	return (error);
    487 }
    488 
    489 #define	SUNOS__MAP_NEW	0x80000000	/* if not, old mmap & cannot handle */
    490 
    491 int
    492 sunos_sys_mmap(struct lwp *l, const struct sunos_sys_mmap_args *uap, register_t *retval)
    493 {
    494 	struct sys_mmap_args ouap;
    495 
    496 	/*
    497 	 * Verify the arguments.
    498 	 */
    499 	if (SCARG(uap, prot) & ~(PROT_READ|PROT_WRITE|PROT_EXEC))
    500 		return (EINVAL);			/* XXX still needed? */
    501 
    502 	if ((SCARG(uap, flags) & SUNOS__MAP_NEW) == 0)
    503 		return (EINVAL);
    504 
    505 	SCARG(&ouap, flags) = SCARG(uap, flags) & ~SUNOS__MAP_NEW;
    506 	SCARG(&ouap, addr) = SCARG(uap, addr);
    507 	SCARG(&ouap, len) = SCARG(uap, len);
    508 	SCARG(&ouap, prot) = SCARG(uap, prot);
    509 	SCARG(&ouap, fd) = SCARG(uap, fd);
    510 	SCARG(&ouap, pos) = SCARG(uap, pos);
    511 
    512 	return (sys_mmap(l, &ouap, retval));
    513 }
    514 
    515 #define	MC_SYNC		1
    516 #define	MC_LOCK		2
    517 #define	MC_UNLOCK	3
    518 #define	MC_ADVISE	4
    519 #define	MC_LOCKAS	5
    520 #define	MC_UNLOCKAS	6
    521 
    522 int
    523 sunos_sys_mctl(struct lwp *l, const struct sunos_sys_mctl_args *uap, register_t *retval)
    524 {
    525 
    526 	switch (SCARG(uap, func)) {
    527 	case MC_ADVISE:		/* ignore for now */
    528 		return (0);
    529 	case MC_SYNC:		/* translate to msync */
    530 		return (sys___msync13(l, (const void *)uap, retval));
    531 	default:
    532 		return (EINVAL);
    533 	}
    534 }
    535 
    536 int
    537 sunos_sys_setsockopt(struct lwp *l, const struct sunos_sys_setsockopt_args *uap, register_t *retval)
    538 {
    539 	struct socket *so;
    540 	struct mbuf *m = NULL;
    541 	int name = SCARG(uap, name);
    542 	int error;
    543 
    544 	/* fd_getsock() will use the descriptor for us */
    545 	if ((error = fd_getsock(SCARG(uap, s), &so)) != 0)
    546 		return (error);
    547 #define	SO_DONTLINGER (~SO_LINGER)
    548 	if (name == SO_DONTLINGER) {
    549 		m = m_get(M_WAIT, MT_SOOPTS);
    550 		mtod(m, struct linger *)->l_onoff = 0;
    551 		m->m_len = sizeof(struct linger);
    552 		error = sosetopt(so, SCARG(uap, level), SO_LINGER, m);
    553 		goto out;
    554 	}
    555 	if (SCARG(uap, level) == IPPROTO_IP) {
    556 #define		SUNOS_IP_MULTICAST_IF		2
    557 #define		SUNOS_IP_MULTICAST_TTL		3
    558 #define		SUNOS_IP_MULTICAST_LOOP		4
    559 #define		SUNOS_IP_ADD_MEMBERSHIP		5
    560 #define		SUNOS_IP_DROP_MEMBERSHIP	6
    561 		static const int ipoptxlat[] = {
    562 			IP_MULTICAST_IF,
    563 			IP_MULTICAST_TTL,
    564 			IP_MULTICAST_LOOP,
    565 			IP_ADD_MEMBERSHIP,
    566 			IP_DROP_MEMBERSHIP
    567 		};
    568 		if (name >= SUNOS_IP_MULTICAST_IF &&
    569 		    name <= SUNOS_IP_DROP_MEMBERSHIP) {
    570 			name = ipoptxlat[name - SUNOS_IP_MULTICAST_IF];
    571 		}
    572 	}
    573 	if (SCARG(uap, valsize) > MLEN) {
    574 		error = EINVAL;
    575 		goto out;
    576 	}
    577 	if (SCARG(uap, val)) {
    578 		m = m_get(M_WAIT, MT_SOOPTS);
    579 		error = copyin(SCARG(uap, val), mtod(m, void *),
    580 		    (u_int)SCARG(uap, valsize));
    581 		if (error) {
    582 			(void) m_free(m);
    583 			goto out;
    584 		}
    585 		m->m_len = SCARG(uap, valsize);
    586 	}
    587 	error = sosetopt(so, SCARG(uap, level), name, m);
    588  out:
    589  	fd_putfile(SCARG(uap, s));
    590 	return (error);
    591 }
    592 
    593 static inline int sunos_sys_socket_common(struct lwp *, register_t *,
    594 					      int type);
    595 static inline int
    596 sunos_sys_socket_common(struct lwp *l, register_t *retval, int type)
    597 {
    598 	struct socket *so;
    599 	int error, fd;
    600 
    601 	/* fd_getsock() will use the descriptor for us */
    602 	fd = (int)*retval;
    603 	if ((error = fd_getsock(fd, &so)) == 0) {
    604 		if (type == SOCK_DGRAM)
    605 			so->so_options |= SO_BROADCAST;
    606 		fd_putfile(fd);
    607 	}
    608 	return (error);
    609 }
    610 
    611 int
    612 sunos_sys_socket(struct lwp *l, const struct sunos_sys_socket_args *uap, register_t *retval)
    613 {
    614 	/* {
    615 		syscallarg(int) domain;
    616 		syscallarg(int) type;
    617 		syscallarg(int) protocol;
    618 	} */
    619 	int error;
    620 
    621 	error = compat_30_sys_socket(l, (const void *)uap, retval);
    622 	if (error)
    623 		return (error);
    624 	return sunos_sys_socket_common(l, retval, SCARG(uap, type));
    625 }
    626 
    627 int
    628 sunos_sys_socketpair(struct lwp *l, const struct sunos_sys_socketpair_args *uap, register_t *retval)
    629 {
    630 	/* {
    631 		syscallarg(int) domain;
    632 		syscallarg(int) type;
    633 		syscallarg(int) protocol;
    634 		syscallarg(int *) rsv;
    635 	} */
    636 	int error;
    637 
    638 	error = sys_socketpair(l, (const void *)uap, retval);
    639 	if (error)
    640 		return (error);
    641 	return sunos_sys_socket_common(l, retval, SCARG(uap, type));
    642 }
    643 
    644 /*
    645  * XXX: This needs cleaning up.
    646  */
    647 int
    648 sunos_sys_auditsys(struct lwp *l, const struct sunos_sys_auditsys_args *uap, register_t *retval)
    649 {
    650 	return 0;
    651 }
    652 
    653 int
    654 sunos_sys_uname(struct lwp *l, const struct sunos_sys_uname_args *uap, register_t *retval)
    655 {
    656 	struct sunos_utsname sut;
    657 
    658 	memset(&sut, 0, sizeof(sut));
    659 
    660 	memcpy(sut.sysname, ostype, sizeof(sut.sysname) - 1);
    661 	memcpy(sut.nodename, hostname, sizeof(sut.nodename));
    662 	sut.nodename[sizeof(sut.nodename)-1] = '\0';
    663 	memcpy(sut.release, osrelease, sizeof(sut.release) - 1);
    664 	memcpy(sut.version, "1", sizeof(sut.version) - 1);
    665 	memcpy(sut.machine, machine, sizeof(sut.machine) - 1);
    666 
    667 	return copyout((void *)&sut, (void *)SCARG(uap, name),
    668 	    sizeof(struct sunos_utsname));
    669 }
    670 
    671 int
    672 sunos_sys_setpgrp(struct lwp *l, const struct sunos_sys_setpgrp_args *uap, register_t *retval)
    673 {
    674 	struct proc *p = l->l_proc;
    675 
    676 	/*
    677 	 * difference to our setpgid call is to include backwards
    678 	 * compatibility to pre-setsid() binaries. Do setsid()
    679 	 * instead of setpgid() in those cases where the process
    680 	 * tries to create a new session the old way.
    681 	 */
    682 	if (!SCARG(uap, pgid) &&
    683 	    (!SCARG(uap, pid) || SCARG(uap, pid) == p->p_pid))
    684 		return sys_setsid(l, NULL, retval);
    685 	else
    686 		return sys_setpgid(l, (const void *)uap, retval);
    687 }
    688 
    689 int
    690 sunos_sys_open(struct lwp *l, const struct sunos_sys_open_args *uap, register_t *retval)
    691 {
    692 	struct proc *p = l->l_proc;
    693 	struct sys_open_args open_ua;
    694 	int smode, nmode;
    695 	int noctty;
    696 	int ret;
    697 
    698 	/* convert mode into NetBSD mode */
    699 	smode = SCARG(uap, flags);
    700 	noctty = smode & 0x8000;
    701 	nmode =	smode &
    702 		(0x0001 | 0x0002 | 0x0008 | 0x0040 | 0x0200 | 0x0400 | 0x0800);
    703 	nmode |= ((smode & (0x0004 | 0x1000 | 0x4000)) ? O_NONBLOCK : 0);
    704 	nmode |= ((smode & 0x0080) ? O_SHLOCK : 0);
    705 	nmode |= ((smode & 0x0100) ? O_EXLOCK : 0);
    706 	nmode |= ((smode & 0x2000) ? O_FSYNC : 0);
    707 
    708 	SCARG(&open_ua, path) = SCARG(uap, path);
    709 	SCARG(&open_ua, flags) = nmode;
    710 	SCARG(&open_ua, mode) = SCARG(uap, mode);
    711 	ret = sys_open(l, &open_ua, retval);
    712 
    713 	/* XXXSMP */
    714 	if (!ret && !noctty && SESS_LEADER(p) && !(p->p_lflag & PL_CONTROLT)) {
    715 		file_t *fp;
    716 		int fd;
    717 
    718 		fd = (int)*retval;
    719 		fp = fd_getfile(fd);
    720 
    721 		/* ignore any error, just give it a try */
    722 		if (fp != NULL) {
    723 			if (fp->f_type == DTYPE_VNODE)
    724 				(fp->f_ops->fo_ioctl)(fp, TIOCSCTTY, NULL);
    725 			fd_putfile(fd);
    726 		}
    727 	}
    728 	return ret;
    729 }
    730 
    731 #if defined (NFSSERVER)
    732 int
    733 sunos_sys_nfssvc(struct lwp *l, const struct sunos_sys_nfssvc_args *uap, register_t *retval)
    734 {
    735 #if 0
    736 	struct proc *p = l->l_proc;
    737 	struct emul *e = p->p_emul;
    738 	struct sys_nfssvc_args outuap;
    739 	struct sockaddr sa;
    740 	int error;
    741 	void *sg = stackgap_init(p, 0);
    742 
    743 	memset(&outuap, 0, sizeof outuap);
    744 	SCARG(&outuap, fd) = SCARG(uap, fd);
    745 	SCARG(&outuap, mskval) = stackgap_alloc(p, &sg, sizeof(sa));
    746 	SCARG(&outuap, msklen) = sizeof(sa);
    747 	SCARG(&outuap, mtchval) = stackgap_alloc(p, &sg, sizeof(sa));
    748 	SCARG(&outuap, mtchlen) = sizeof(sa);
    749 
    750 	memset(&sa, 0, sizeof sa);
    751 	if (error = copyout(&sa, SCARG(&outuap, mskval), SCARG(&outuap, msklen)))
    752 		return (error);
    753 	if (error = copyout(&sa, SCARG(&outuap, mtchval), SCARG(&outuap, mtchlen)))
    754 		return (error);
    755 
    756 	return nfssvc(l, &outuap, retval);
    757 #else
    758 	return (ENOSYS);
    759 #endif
    760 }
    761 #endif /* NFSSERVER */
    762 
    763 int
    764 sunos_sys_ustat(struct lwp *l, const struct sunos_sys_ustat_args *uap, register_t *retval)
    765 {
    766 	struct sunos_ustat us;
    767 	int error;
    768 
    769 	memset(&us, 0, sizeof us);
    770 
    771 	/*
    772 	 * XXX: should set f_tfree and f_tinode at least
    773 	 * How do we translate dev -> fstat? (and then to sunos_ustat)
    774 	 */
    775 
    776 	if ((error = copyout(&us, SCARG(uap, buf), sizeof us)) != 0)
    777 		return (error);
    778 	return 0;
    779 }
    780 
    781 int
    782 sunos_sys_quotactl(struct lwp *l, const struct sunos_sys_quotactl_args *uap, register_t *retval)
    783 {
    784 
    785 	return EINVAL;
    786 }
    787 
    788 int
    789 sunos_sys_vhangup(struct lwp *l, const void *v, register_t *retval)
    790 {
    791 	struct proc *p = l->l_proc;
    792 	struct session *sp = p->p_session;
    793 
    794 	if (sp->s_ttyvp == 0)
    795 		return 0;
    796 
    797 	if (sp->s_ttyp && sp->s_ttyp->t_session == sp && sp->s_ttyp->t_pgrp)
    798 		pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
    799 
    800 	(void) ttywait(sp->s_ttyp);
    801 	if (sp->s_ttyvp)
    802 		VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
    803 	if (sp->s_ttyvp)
    804 		vrele(sp->s_ttyvp);
    805 	sp->s_ttyvp = NULL;
    806 
    807 	return 0;
    808 }
    809 
    810 static int
    811 sunstatfs(struct statvfs *sp, void *buf)
    812 {
    813 	struct sunos_statfs ssfs;
    814 
    815 	memset(&ssfs, 0, sizeof ssfs);
    816 	ssfs.f_type = 0;
    817 	ssfs.f_bsize = sp->f_bsize;
    818 	ssfs.f_blocks = sp->f_blocks;
    819 	ssfs.f_bfree = sp->f_bfree;
    820 	ssfs.f_bavail = sp->f_bavail;
    821 	ssfs.f_files = sp->f_files;
    822 	ssfs.f_ffree = sp->f_ffree;
    823 	ssfs.f_fsid = sp->f_fsidx;
    824 	return copyout((void *)&ssfs, buf, sizeof ssfs);
    825 }
    826 
    827 int
    828 sunos_sys_statfs(struct lwp *l, const struct sunos_sys_statfs_args *uap, register_t *retval)
    829 {
    830 	struct mount *mp;
    831 	struct statvfs *sp;
    832 	int error;
    833 	struct nameidata nd;
    834 
    835 	NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
    836 	    SCARG(uap, path));
    837 	if ((error = namei(&nd)) != 0)
    838 		return (error);
    839 	mp = nd.ni_vp->v_mount;
    840 	sp = &mp->mnt_stat;
    841 	vrele(nd.ni_vp);
    842 	if ((error = VFS_STATVFS(mp, sp)) != 0)
    843 		return (error);
    844 	sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
    845 	return sunstatfs(sp, (void *)SCARG(uap, buf));
    846 }
    847 
    848 int
    849 sunos_sys_fstatfs(struct lwp *l, const struct sunos_sys_fstatfs_args *uap, register_t *retval)
    850 {
    851 	file_t *fp;
    852 	struct mount *mp;
    853 	struct statvfs *sp;
    854 	int error;
    855 
    856 	/* fd_getvnode() will use the descriptor for us */
    857 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
    858 		return (error);
    859 	mp = ((struct vnode *)fp->f_data)->v_mount;
    860 	sp = &mp->mnt_stat;
    861 	if ((error = VFS_STATVFS(mp, sp)) != 0)
    862 		goto out;
    863 	sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
    864 	error = sunstatfs(sp, (void *)SCARG(uap, buf));
    865  out:
    866  	fd_putfile(SCARG(uap, fd));
    867 	return (error);
    868 }
    869 
    870 int
    871 sunos_sys_exportfs(struct lwp *l, const struct sunos_sys_exportfs_args *uap, register_t *retval)
    872 {
    873 	/*
    874 	 * XXX: should perhaps translate into a mount(2)
    875 	 * with MOUNT_EXPORT?
    876 	 */
    877 	return 0;
    878 }
    879 
    880 int
    881 sunos_sys_mknod(struct lwp *l, const struct sunos_sys_mknod_args *uap, register_t *retval)
    882 {
    883 	struct sys_mkfifo_args fifo_ua;
    884 
    885 	if (S_ISFIFO(SCARG(uap, mode))) {
    886 		SCARG(&fifo_ua, path) = SCARG(uap, path);
    887 		SCARG(&fifo_ua, mode) = SCARG(uap, mode);
    888 		return sys_mkfifo(l, &fifo_ua, retval);
    889 	}
    890 
    891 	return sys_mknod(l, (const struct sys_mknod_args *)uap, retval);
    892 }
    893 
    894 #define SUNOS_SC_ARG_MAX	1
    895 #define SUNOS_SC_CHILD_MAX	2
    896 #define SUNOS_SC_CLK_TCK	3
    897 #define SUNOS_SC_NGROUPS_MAX	4
    898 #define SUNOS_SC_OPEN_MAX	5
    899 #define SUNOS_SC_JOB_CONTROL	6
    900 #define SUNOS_SC_SAVED_IDS	7
    901 #define SUNOS_SC_VERSION	8
    902 
    903 int
    904 sunos_sys_sysconf(struct lwp *l, const struct sunos_sys_sysconf_args *uap, register_t *retval)
    905 {
    906 
    907 	switch(SCARG(uap, name)) {
    908 	case SUNOS_SC_ARG_MAX:
    909 		*retval = ARG_MAX;
    910 		break;
    911 	case SUNOS_SC_CHILD_MAX:
    912 		*retval = maxproc;
    913 		break;
    914 	case SUNOS_SC_CLK_TCK:
    915 		*retval = 60;		/* should this be `hz', ie. 100? */
    916 		break;
    917 	case SUNOS_SC_NGROUPS_MAX:
    918 		*retval = NGROUPS_MAX;
    919 		break;
    920 	case SUNOS_SC_OPEN_MAX:
    921 		*retval = maxfiles;
    922 		break;
    923 	case SUNOS_SC_JOB_CONTROL:
    924 		*retval = 1;
    925 		break;
    926 	case SUNOS_SC_SAVED_IDS:
    927 #ifdef _POSIX_SAVED_IDS
    928 		*retval = 1;
    929 #else
    930 		*retval = 0;
    931 #endif
    932 		break;
    933 	case SUNOS_SC_VERSION:
    934 		*retval = 198808;
    935 		break;
    936 	default:
    937 		return EINVAL;
    938 	}
    939 	return 0;
    940 }
    941 
    942 #define SUNOS_RLIMIT_NOFILE	6	/* Other RLIMIT_* are the same */
    943 #define SUNOS_RLIM_NLIMITS	7
    944 
    945 int
    946 sunos_sys_getrlimit(struct lwp *l, const struct sunos_sys_getrlimit_args *uap, register_t *retval)
    947 {
    948 	struct compat_43_sys_getrlimit_args ua_43;
    949 
    950 	SCARG(&ua_43, which) = SCARG(uap, which);
    951 	SCARG(&ua_43, rlp) = SCARG(uap, rlp);
    952 
    953 	if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
    954 		return EINVAL;
    955 
    956 	if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
    957 		SCARG(&ua_43, which) = RLIMIT_NOFILE;
    958 
    959 	return compat_43_sys_getrlimit(l, &ua_43, retval);
    960 }
    961 
    962 int
    963 sunos_sys_setrlimit(struct lwp *l, const struct sunos_sys_setrlimit_args *uap, register_t *retval)
    964 {
    965 	struct compat_43_sys_setrlimit_args ua_43;
    966 
    967 	SCARG(&ua_43, which) = SCARG(uap, which);
    968 	SCARG(&ua_43, rlp) = SCARG(uap, rlp);
    969 
    970 	if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
    971 		return EINVAL;
    972 
    973 	if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
    974 		SCARG(&ua_43, which) = RLIMIT_NOFILE;
    975 
    976 	return compat_43_sys_setrlimit(l, &ua_43, retval);
    977 }
    978 
    979 #if defined(PTRACE) || defined(_LKM)
    980 /* for the m68k machines */
    981 #ifndef PT_GETFPREGS
    982 #define PT_GETFPREGS -1
    983 #endif
    984 #ifndef PT_SETFPREGS
    985 #define PT_SETFPREGS -1
    986 #endif
    987 
    988 static const int sreq2breq[] = {
    989 	PT_TRACE_ME,    PT_READ_I,      PT_READ_D,      -1,
    990 	PT_WRITE_I,     PT_WRITE_D,     -1,             PT_CONTINUE,
    991 	PT_KILL,        -1,             PT_ATTACH,      PT_DETACH,
    992 	PT_GETREGS,     PT_SETREGS,     PT_GETFPREGS,   PT_SETFPREGS
    993 };
    994 static const int nreqs = sizeof(sreq2breq) / sizeof(sreq2breq[0]);
    995 #endif /* PTRACE || _LKM */
    996 
    997 int
    998 sunos_sys_ptrace(struct lwp *l, const struct sunos_sys_ptrace_args *uap, register_t *retval)
    999 {
   1000 #if defined(PTRACE) || defined(_LKM)
   1001 	struct sys_ptrace_args pa;
   1002 	int req;
   1003 
   1004 #ifdef _LKM
   1005 #define	sys_ptrace sysent[SYS_ptrace].sy_call
   1006 	if (sys_ptrace == sys_nosys)
   1007 		return ENOSYS;
   1008 #endif
   1009 
   1010 	req = SCARG(uap, req);
   1011 
   1012 	if (req < 0 || req >= nreqs)
   1013 		return (EINVAL);
   1014 
   1015 	req = sreq2breq[req];
   1016 	if (req == -1)
   1017 		return (EINVAL);
   1018 
   1019 	SCARG(&pa, req) = req;
   1020 	SCARG(&pa, pid) = (pid_t)SCARG(uap, pid);
   1021 	SCARG(&pa, addr) = (void *)SCARG(uap, addr);
   1022 	SCARG(&pa, data) = SCARG(uap, data);
   1023 
   1024 	return sys_ptrace(l, &pa, retval);
   1025 #else
   1026 	return ENOSYS;
   1027 #endif /* PTRACE || _LKM */
   1028 }
   1029 
   1030 /*
   1031  * SunOS reboot system call (for compatibility).
   1032  * Sun lets you pass in a boot string which the PROM
   1033  * saves and provides to the next boot program.
   1034  */
   1035 
   1036 #define SUNOS_RB_ASKNAME	0x001
   1037 #define SUNOS_RB_SINGLE 	0x002
   1038 #define SUNOS_RB_NOSYNC		0x004
   1039 #define SUNOS_RB_HALT		0x008
   1040 #define SUNOS_RB_DUMP		0x080
   1041 #define	SUNOS_RB_STRING		0x200
   1042 
   1043 static struct sunos_howto_conv {
   1044 	int sun_howto;
   1045 	int bsd_howto;
   1046 } sunos_howto_conv[] = {
   1047 	{ SUNOS_RB_ASKNAME,	RB_ASKNAME },
   1048 	{ SUNOS_RB_SINGLE,	RB_SINGLE },
   1049 	{ SUNOS_RB_NOSYNC,	RB_NOSYNC },
   1050 	{ SUNOS_RB_HALT,	RB_HALT },
   1051 	{ SUNOS_RB_DUMP,	RB_DUMP },
   1052 	{ SUNOS_RB_STRING,	RB_STRING },
   1053 	{ 0x000,		0 },
   1054 };
   1055 
   1056 int
   1057 sunos_sys_reboot(struct lwp *l, const struct sunos_sys_reboot_args *uap, register_t *retval)
   1058 {
   1059 	struct sys_reboot_args ua;
   1060 	struct sunos_howto_conv *convp;
   1061 	int error, bsd_howto, sun_howto;
   1062 	char *bootstr;
   1063 
   1064 	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_REBOOT,
   1065 	    0, NULL, NULL, NULL)) != 0)
   1066 		return (error);
   1067 
   1068 	/*
   1069 	 * Convert howto bits to BSD format.
   1070 	 */
   1071 	sun_howto = SCARG(uap, howto);
   1072 	bsd_howto = 0;
   1073 	convp = sunos_howto_conv;
   1074 	while (convp->sun_howto) {
   1075 		if (sun_howto & convp->sun_howto)
   1076 			bsd_howto |= convp->bsd_howto;
   1077 		convp++;
   1078 	}
   1079 
   1080 	/*
   1081 	 * Sun RB_STRING (Get user supplied bootstring.)
   1082 	 * If the machine supports passing a string to the
   1083 	 * next booted kernel.
   1084 	 */
   1085 	if (sun_howto & SUNOS_RB_STRING) {
   1086 		char bs[128];
   1087 
   1088 		error = copyinstr(SCARG(uap, bootstr), bs, sizeof(bs), 0);
   1089 
   1090 		if (error)
   1091 			bootstr = NULL;
   1092 		else
   1093 			bootstr = bs;
   1094 	} else
   1095 		bootstr = NULL;
   1096 
   1097 	SCARG(&ua, opt) = bsd_howto;
   1098 	SCARG(&ua, bootstr) = bootstr;
   1099 	sys_reboot(l, &ua, retval);
   1100 	return(0);
   1101 }
   1102 
   1103 /*
   1104  * Generalized interface signal handler, 4.3-compatible.
   1105  */
   1106 /* ARGSUSED */
   1107 int
   1108 sunos_sys_sigvec(struct lwp *l, const struct sunos_sys_sigvec_args *uap, register_t *retval)
   1109 {
   1110 	/* {
   1111 		syscallarg(int) signum;
   1112 		syscallarg(struct sigvec *) nsv;
   1113 		syscallarg(struct sigvec *) osv;
   1114 	} */
   1115 	struct sigvec nsv, osv;
   1116 	struct sigaction nsa, osa;
   1117 	int error;
   1118 /*XXX*/extern	void compat_43_sigvec_to_sigaction
   1119 (const struct sigvec *, struct sigaction *);
   1120 /*XXX*/extern	void compat_43_sigaction_to_sigvec
   1121 (const struct sigaction *, struct sigvec *);
   1122 
   1123 	if (SCARG(uap, nsv)) {
   1124 		error = copyin(SCARG(uap, nsv), &nsv, sizeof(nsv));
   1125 		if (error != 0)
   1126 			return (error);
   1127 
   1128 		/*
   1129 		 * SunOS uses the mask 0x0004 as SV_RESETHAND
   1130 		 * meaning: `reset to SIG_DFL on delivery'.
   1131 		 * We support only the bits in: 0xF
   1132 		 * (those bits are the same as ours)
   1133 		 */
   1134 		if (nsv.sv_flags & ~0xF)
   1135 			return (EINVAL);
   1136 
   1137 		compat_43_sigvec_to_sigaction(&nsv, &nsa);
   1138 	}
   1139 	error = sigaction1(l, SCARG(uap, signum),
   1140 			   SCARG(uap, nsv) ? &nsa : 0,
   1141 			   SCARG(uap, osv) ? &osa : 0,
   1142 			   NULL, 0);
   1143 	if (error != 0)
   1144 		return (error);
   1145 
   1146 	if (SCARG(uap, osv)) {
   1147 		compat_43_sigaction_to_sigvec(&osa, &osv);
   1148 		error = copyout(&osv, SCARG(uap, osv), sizeof(osv));
   1149 		if (error != 0)
   1150 			return (error);
   1151 	}
   1152 
   1153 	return (0);
   1154 }
   1155