Home | History | Annotate | Line # | Download | only in sunos
sunos_misc.c revision 1.135.6.1
      1 /*	$NetBSD: sunos_misc.c,v 1.135.6.1 2006/04/22 11:38:17 simonb 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.135.6.1 2006/04/22 11:38:17 simonb Exp $");
     54 
     55 #if defined(_KERNEL_OPT)
     56 #include "opt_nfsserver.h"
     57 #include "fs_nfs.h"
     58 #endif
     59 
     60 #include <sys/param.h>
     61 #include <sys/systm.h>
     62 #include <sys/namei.h>
     63 #include <sys/proc.h>
     64 #include <sys/dirent.h>
     65 #include <sys/file.h>
     66 #include <sys/stat.h>
     67 #include <sys/filedesc.h>
     68 #include <sys/ioctl.h>
     69 #include <sys/kernel.h>
     70 #include <sys/reboot.h>
     71 #include <sys/malloc.h>
     72 #include <sys/mbuf.h>
     73 #include <sys/mman.h>
     74 #include <sys/mount.h>
     75 #include <sys/ptrace.h>
     76 #include <sys/resource.h>
     77 #include <sys/resourcevar.h>
     78 #include <sys/signal.h>
     79 #include <sys/signalvar.h>
     80 #include <sys/socket.h>
     81 #include <sys/tty.h>
     82 #include <sys/vnode.h>
     83 #include <sys/uio.h>
     84 #include <sys/wait.h>
     85 #include <sys/utsname.h>
     86 #include <sys/unistd.h>
     87 #include <sys/sa.h>
     88 #include <sys/syscallargs.h>
     89 #include <sys/conf.h>
     90 #include <sys/socketvar.h>
     91 #include <sys/exec.h>
     92 #include <sys/swap.h>
     93 
     94 #include <compat/sys/signal.h>
     95 
     96 #include <compat/sunos/sunos.h>
     97 #include <compat/sunos/sunos_syscallargs.h>
     98 #include <compat/common/compat_util.h>
     99 #include <compat/sunos/sunos_dirent.h>
    100 
    101 #include <netinet/in.h>
    102 
    103 #include <miscfs/specfs/specdev.h>
    104 
    105 #include <nfs/rpcv2.h>
    106 #include <nfs/nfsproto.h>
    107 #include <nfs/nfs.h>
    108 #include <nfs/nfsmount.h>
    109 
    110 static int sunstatfs __P((struct statvfs *, caddr_t));
    111 
    112 int
    113 sunos_sys_stime(l, v, retval)
    114 	struct lwp *l;
    115 	void *v;
    116 	register_t *retval;
    117 {
    118 	struct sunos_sys_stime_args *uap = v;
    119 	struct sys_settimeofday_args ap;
    120 	struct proc *p = l->l_proc;
    121 	caddr_t sg = stackgap_init(p, 0);
    122 	struct timeval tv, *sgtvp;
    123 	int error;
    124 
    125 	error = copyin(SCARG(uap, tp), &tv.tv_sec, sizeof(tv.tv_sec));
    126 	if (error)
    127 		return error;
    128 	tv.tv_usec = 0;
    129 
    130 	SCARG(&ap, tv) = sgtvp = stackgap_alloc(p, &sg, sizeof(struct timeval));
    131 	SCARG(&ap, tzp) = NULL;
    132 
    133 	error = copyout(&tv, sgtvp, sizeof(struct timeval));
    134 	if (error)
    135 		return error;
    136 
    137 	return sys_settimeofday(l, &ap, retval);
    138 }
    139 
    140 int
    141 sunos_sys_wait4(l, v, retval)
    142 	struct lwp *l;
    143 	void *v;
    144 	register_t *retval;
    145 {
    146 	struct sunos_sys_wait4_args *uap = v;
    147 	if (SCARG(uap, pid) == 0)
    148 		SCARG(uap, pid) = WAIT_ANY;
    149 	return (sys_wait4(l, uap, retval));
    150 }
    151 
    152 int
    153 sunos_sys_creat(l, v, retval)
    154 	struct lwp *l;
    155 	void *v;
    156 	register_t *retval;
    157 {
    158 	struct sunos_sys_creat_args *uap = v;
    159 	struct proc *p = l->l_proc;
    160 	struct sys_open_args ouap;
    161 
    162 	caddr_t sg = stackgap_init(p, 0);
    163 	CHECK_ALT_CREAT(l, &sg, SCARG(uap, path));
    164 
    165 	SCARG(&ouap, path) = SCARG(uap, path);
    166 	SCARG(&ouap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
    167 	SCARG(&ouap, mode) = SCARG(uap, mode);
    168 
    169 	return (sys_open(l, &ouap, retval));
    170 }
    171 
    172 int
    173 sunos_sys_access(l, v, retval)
    174 	struct lwp *l;
    175 	void *v;
    176 	register_t *retval;
    177 {
    178 	struct sunos_sys_access_args *uap = v;
    179 	struct proc *p = l->l_proc;
    180 	caddr_t sg = stackgap_init(p, 0);
    181 	CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
    182 
    183 	return (sys_access(l, uap, retval));
    184 }
    185 
    186 int
    187 sunos_sys_stat(l, v, retval)
    188 	struct lwp *l;
    189 	void *v;
    190 	register_t *retval;
    191 {
    192 	struct sunos_sys_stat_args *uap = v;
    193 	struct proc *p = l->l_proc;
    194 	caddr_t sg = stackgap_init(p, 0);
    195 	CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
    196 
    197 	return (compat_43_sys_stat(l, uap, retval));
    198 }
    199 
    200 int
    201 sunos_sys_lstat(l, v, retval)
    202 	struct lwp *l;
    203 	void *v;
    204 	register_t *retval;
    205 {
    206 	struct sunos_sys_lstat_args *uap = v;
    207 	struct proc *p = l->l_proc;
    208 	caddr_t sg = stackgap_init(p, 0);
    209 	CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
    210 
    211 	return (compat_43_sys_lstat(l, uap, retval));
    212 }
    213 
    214 int
    215 sunos_sys_execv(l, v, retval)
    216 	struct lwp *l;
    217 	void *v;
    218 	register_t *retval;
    219 {
    220 	struct sunos_sys_execv_args /* {
    221 		syscallarg(const char *) path;
    222 		syscallarg(char **) argv;
    223 	} */ *uap = v;
    224 	struct proc *p = l->l_proc;
    225 	struct sys_execve_args ap;
    226 	caddr_t sg;
    227 
    228 	sg = stackgap_init(p, 0);
    229 	CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
    230 
    231 	SCARG(&ap, path) = SCARG(uap, path);
    232 	SCARG(&ap, argp) = SCARG(uap, argp);
    233 	SCARG(&ap, envp) = NULL;
    234 
    235 	return (sys_execve(l, &ap, retval));
    236 }
    237 
    238 int
    239 sunos_sys_execve(l, v, retval)
    240 	struct lwp *l;
    241 	void *v;
    242 	register_t *retval;
    243 {
    244 	struct sunos_sys_execve_args /* {
    245 		syscallarg(const char *) path;
    246 		syscallarg(char **) argv;
    247 		syscallarg(char **) envp;
    248 	} */ *uap = v;
    249 	struct sys_execve_args ap;
    250 	struct proc *p = l->l_proc;
    251 	caddr_t sg;
    252 
    253 	sg = stackgap_init(p, 0);
    254 	CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
    255 
    256 	SCARG(&ap, path) = SCARG(uap, path);
    257 	SCARG(&ap, argp) = SCARG(uap, argp);
    258 	SCARG(&ap, envp) = SCARG(uap, envp);
    259 
    260 	return (sys_execve(l, &ap, retval));
    261 }
    262 
    263 int
    264 sunos_sys_omsync(l, v, retval)
    265 	struct lwp *l;
    266 	void *v;
    267 	register_t *retval;
    268 {
    269 	struct sunos_sys_omsync_args *uap = v;
    270 	struct sys___msync13_args ouap;
    271 
    272 	SCARG(&ouap, addr) = SCARG(uap, addr);
    273 	SCARG(&ouap, len) = SCARG(uap, len);
    274 	SCARG(&ouap, flags) = SCARG(uap, flags);
    275 
    276 	return (sys___msync13(l, &ouap, retval));
    277 }
    278 
    279 int
    280 sunos_sys_unmount(l, v, retval)
    281 	struct lwp *l;
    282 	void *v;
    283 	register_t *retval;
    284 {
    285 	struct sunos_sys_unmount_args *uap = v;
    286 	struct sys_unmount_args ouap;
    287 
    288 	SCARG(&ouap, path) = SCARG(uap, path);
    289 	SCARG(&ouap, flags) = 0;
    290 
    291 	return (sys_unmount(l, &ouap, retval));
    292 }
    293 
    294 /*
    295  * Conversion table for SunOS NFS mount flags.
    296  */
    297 static struct {
    298 	int	sun_flg;
    299 	int	bsd_flg;
    300 } sunnfs_flgtab[] = {
    301 	{ SUNNFS_SOFT,		NFSMNT_SOFT },
    302 	{ SUNNFS_WSIZE,		NFSMNT_WSIZE },
    303 	{ SUNNFS_RSIZE,		NFSMNT_RSIZE },
    304 	{ SUNNFS_TIMEO,		NFSMNT_TIMEO },
    305 	{ SUNNFS_RETRANS,	NFSMNT_RETRANS },
    306 	{ SUNNFS_HOSTNAME,	0 },			/* Ignored */
    307 	{ SUNNFS_INT,		NFSMNT_INT },
    308 	{ SUNNFS_NOAC,		0 },			/* Ignored */
    309 	{ SUNNFS_ACREGMIN,	0 },			/* Ignored */
    310 	{ SUNNFS_ACREGMAX,	0 },			/* Ignored */
    311 	{ SUNNFS_ACDIRMIN,	0 },			/* Ignored */
    312 	{ SUNNFS_ACDIRMAX,	0 },			/* Ignored */
    313 	{ SUNNFS_SECURE,	0 },			/* Ignored */
    314 	{ SUNNFS_NOCTO,		0 },			/* Ignored */
    315 	{ SUNNFS_POSIX,		0 }			/* Ignored */
    316 };
    317 
    318 int
    319 sunos_sys_mount(l, v, retval)
    320 	struct lwp *l;
    321 	void *v;
    322 	register_t *retval;
    323 {
    324 	struct sunos_sys_mount_args *uap = v;
    325 	struct proc *p = l->l_proc;
    326 	int oflags = SCARG(uap, flags), nflags, error;
    327 	char fsname[MFSNAMELEN];
    328 	caddr_t sg = stackgap_init(p, 0);
    329 
    330 	if (oflags & (SUNM_NOSUB | SUNM_SYS5))
    331 		return (EINVAL);
    332 	if ((oflags & SUNM_NEWTYPE) == 0)
    333 		return (EINVAL);
    334 	nflags = 0;
    335 	if (oflags & SUNM_RDONLY)
    336 		nflags |= MNT_RDONLY;
    337 	if (oflags & SUNM_NOSUID)
    338 		nflags |= MNT_NOSUID;
    339 	if (oflags & SUNM_REMOUNT)
    340 		nflags |= MNT_UPDATE;
    341 	SCARG(uap, flags) = nflags;
    342 
    343 	error = copyinstr((caddr_t)SCARG(uap, type), fsname,
    344 	    sizeof fsname, (size_t *)0);
    345 	if (error)
    346 		return (error);
    347 
    348 	if (strncmp(fsname, "4.2", sizeof fsname) == 0) {
    349 		SCARG(uap, type) = stackgap_alloc(p, &sg, sizeof("ffs"));
    350 		error = copyout("ffs", SCARG(uap, type), sizeof("ffs"));
    351 		if (error)
    352 			return (error);
    353 	} else if (strncmp(fsname, "nfs", sizeof fsname) == 0) {
    354 		struct sunos_nfs_args sna;
    355 		struct sockaddr_in sain;
    356 		struct nfs_args na;
    357 		struct sockaddr sa;
    358 		int n;
    359 
    360 		error = copyin(SCARG(uap, data), &sna, sizeof sna);
    361 		if (error)
    362 			return (error);
    363 		error = copyin(sna.addr, &sain, sizeof sain);
    364 		if (error)
    365 			return (error);
    366 		memcpy(&sa, &sain, sizeof sa);
    367 		sa.sa_len = sizeof(sain);
    368 		SCARG(uap, data) = stackgap_alloc(p, &sg, sizeof(na));
    369 		na.version = NFS_ARGSVERSION;
    370 		na.addr = stackgap_alloc(p, &sg, sizeof(struct sockaddr));
    371 		na.addrlen = sizeof(struct sockaddr);
    372 		na.sotype = SOCK_DGRAM;
    373 		na.proto = IPPROTO_UDP;
    374 		na.fh = (void *)sna.fh;
    375 		na.fhsize = NFSX_V2FH;
    376 		na.flags = 0;
    377 		n = sizeof(sunnfs_flgtab) / sizeof(sunnfs_flgtab[0]);
    378 		while (--n >= 0)
    379 			if (sna.flags & sunnfs_flgtab[n].sun_flg)
    380 				na.flags |= sunnfs_flgtab[n].bsd_flg;
    381 		na.wsize = sna.wsize;
    382 		na.rsize = sna.rsize;
    383 		if (na.flags & NFSMNT_RSIZE) {
    384 			na.flags |= NFSMNT_READDIRSIZE;
    385 			na.readdirsize = na.rsize;
    386 		}
    387 		na.timeo = sna.timeo;
    388 		na.retrans = sna.retrans;
    389 		na.hostname = (char *)(u_long)sna.hostname;
    390 
    391 		error = copyout(&sa, na.addr, sizeof sa);
    392 		if (error)
    393 			return (error);
    394 		error = copyout(&na, SCARG(uap, data), sizeof na);
    395 		if (error)
    396 			return (error);
    397 	}
    398 	return (sys_mount(l, (struct sys_mount_args *)uap, retval));
    399 }
    400 
    401 #if defined(NFS)
    402 int
    403 async_daemon(l, v, retval)
    404 	struct lwp *l;
    405 	void *v;
    406 	register_t *retval;
    407 {
    408 	struct sys_nfssvc_args ouap;
    409 
    410 	SCARG(&ouap, flag) = NFSSVC_BIOD;
    411 	SCARG(&ouap, argp) = NULL;
    412 
    413 	return (sys_nfssvc(l, &ouap, retval));
    414 }
    415 #endif /* NFS */
    416 
    417 void	native_to_sunos_sigset __P((const sigset_t *, int *));
    418 void	sunos_to_native_sigset __P((const int, sigset_t *));
    419 
    420 inline void
    421 native_to_sunos_sigset(ss, mask)
    422 	const sigset_t *ss;
    423 	int *mask;
    424 {
    425 	*mask = ss->__bits[0];
    426 }
    427 
    428 inline void
    429 sunos_to_native_sigset(mask, ss)
    430 	const int mask;
    431 	sigset_t *ss;
    432 {
    433 
    434 	ss->__bits[0] = mask;
    435 	ss->__bits[1] = 0;
    436 	ss->__bits[2] = 0;
    437 	ss->__bits[3] = 0;
    438 }
    439 
    440 int
    441 sunos_sys_sigpending(l, v, retval)
    442 	struct lwp *l;
    443 	void *v;
    444 	register_t *retval;
    445 {
    446 	struct sunos_sys_sigpending_args *uap = v;
    447 	sigset_t ss;
    448 	int mask;
    449 
    450 	sigpending1(l->l_proc, &ss);
    451 	native_to_sunos_sigset(&ss, &mask);
    452 
    453 	return (copyout((caddr_t)&mask, (caddr_t)SCARG(uap, mask), sizeof(int)));
    454 }
    455 
    456 int
    457 sunos_sys_sigsuspend(l, v, retval)
    458 	struct lwp *l;
    459 	void *v;
    460 	register_t *retval;
    461 {
    462 	struct sunos_sys_sigsuspend_args /* {
    463 		syscallarg(int) mask;
    464 	} */ *uap = v;
    465 	int mask;
    466 	sigset_t ss;
    467 
    468 	mask = SCARG(uap, mask);
    469 	sunos_to_native_sigset(mask, &ss);
    470 	return (sigsuspend1(l->l_proc, &ss));
    471 }
    472 
    473 /*
    474  * Read Sun-style directory entries.  We suck them into kernel space so
    475  * that they can be massaged before being copied out to user code.  Like
    476  * SunOS, we squish out `empty' entries.
    477  *
    478  * This is quite ugly, but what do you expect from compatibility code?
    479  */
    480 int
    481 sunos_sys_getdents(l, v, retval)
    482 	struct lwp *l;
    483 	void *v;
    484 	register_t *retval;
    485 {
    486 	struct sunos_sys_getdents_args *uap = v;
    487 	struct proc *p = l->l_proc;
    488 	struct dirent *bdp;
    489 	struct vnode *vp;
    490 	caddr_t inp, buf;	/* BSD-format */
    491 	int len, reclen;	/* BSD-format */
    492 	caddr_t outp;		/* Sun-format */
    493 	int resid, sunos_reclen;/* Sun-format */
    494 	struct file *fp;
    495 	struct uio auio;
    496 	struct iovec aiov;
    497 	struct sunos_dirent idb;
    498 	off_t off;			/* true file offset */
    499 	int buflen, error, eofflag;
    500 	off_t *cookiebuf, *cookie;
    501 	int ncookies;
    502 
    503 	/* getvnode() will use the descriptor for us */
    504 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
    505 		return (error);
    506 
    507 	if ((fp->f_flag & FREAD) == 0) {
    508 		error = EBADF;
    509 		goto out1;
    510 	}
    511 
    512 	vp = (struct vnode *)fp->f_data;
    513 	if (vp->v_type != VDIR) {
    514 		error = EINVAL;
    515 		goto out1;
    516 	}
    517 
    518 	buflen = min(MAXBSIZE, SCARG(uap, nbytes));
    519 	buf = malloc(buflen, M_TEMP, M_WAITOK);
    520 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    521 	off = fp->f_offset;
    522 again:
    523 	aiov.iov_base = buf;
    524 	aiov.iov_len = buflen;
    525 	auio.uio_iov = &aiov;
    526 	auio.uio_iovcnt = 1;
    527 	auio.uio_rw = UIO_READ;
    528 	auio.uio_resid = buflen;
    529 	auio.uio_offset = off;
    530 	UIO_SETUP_SYSSPACE(&auio);
    531 	/*
    532 	 * First we read into the malloc'ed buffer, then
    533 	 * we massage it into user space, one record at a time.
    534 	 */
    535 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
    536 	    &ncookies);
    537 	if (error)
    538 		goto out;
    539 
    540 	inp = buf;
    541 	outp = SCARG(uap, buf);
    542 	resid = SCARG(uap, nbytes);
    543 	if ((len = buflen - auio.uio_resid) == 0)
    544 		goto eof;
    545 
    546 	for (cookie = cookiebuf; len > 0; len -= reclen) {
    547 		bdp = (struct dirent *)inp;
    548 		reclen = bdp->d_reclen;
    549 		if (reclen & 3)
    550 			panic("sunos_getdents");
    551 		if ((*cookie >> 32) != 0) {
    552 			compat_offseterr(vp, "sunos_getdents");
    553 			error = EINVAL;
    554 			goto out;
    555 		}
    556 		if (bdp->d_fileno == 0) {
    557 			inp += reclen;	/* it is a hole; squish it out */
    558 			if (cookie)
    559 				off = *cookie++;
    560 			else
    561 				off += reclen;
    562 			continue;
    563 		}
    564 		sunos_reclen = SUNOS_RECLEN(&idb, bdp->d_namlen);
    565 		if (reclen > len || resid < sunos_reclen) {
    566 			/* entry too big for buffer, so just stop */
    567 			outp++;
    568 			break;
    569 		}
    570 		if (cookie)
    571 			off = *cookie++;	/* each entry points to next */
    572 		else
    573 			off += reclen;
    574 		/*
    575 		 * Massage in place to make a Sun-shaped dirent (otherwise
    576 		 * we have to worry about touching user memory outside of
    577 		 * the copyout() call).
    578 		 */
    579 		idb.d_fileno = bdp->d_fileno;
    580 		idb.d_off = off;
    581 		idb.d_reclen = sunos_reclen;
    582 		idb.d_namlen = bdp->d_namlen;
    583 		strlcpy(idb.d_name, bdp->d_name, sizeof(idb.d_name));
    584 		if ((error = copyout((caddr_t)&idb, outp, sunos_reclen)) != 0)
    585 			goto out;
    586 		/* advance past this real entry */
    587 		inp += reclen;
    588 		/* advance output past Sun-shaped entry */
    589 		outp += sunos_reclen;
    590 		resid -= sunos_reclen;
    591 	}
    592 
    593 	/* if we squished out the whole block, try again */
    594 	if (outp == SCARG(uap, buf))
    595 		goto again;
    596 	fp->f_offset = off;		/* update the vnode offset */
    597 
    598 eof:
    599 	*retval = SCARG(uap, nbytes) - resid;
    600 out:
    601 	VOP_UNLOCK(vp, 0);
    602 	free(cookiebuf, M_TEMP);
    603 	free(buf, M_TEMP);
    604  out1:
    605 	FILE_UNUSE(fp, l);
    606 	return (error);
    607 }
    608 
    609 #define	SUNOS__MAP_NEW	0x80000000	/* if not, old mmap & cannot handle */
    610 
    611 int
    612 sunos_sys_mmap(l, v, retval)
    613 	struct lwp *l;
    614 	void *v;
    615 	register_t *retval;
    616 {
    617 	struct sunos_sys_mmap_args *uap = v;
    618 	struct sys_mmap_args ouap;
    619 
    620 	/*
    621 	 * Verify the arguments.
    622 	 */
    623 	if (SCARG(uap, prot) & ~(PROT_READ|PROT_WRITE|PROT_EXEC))
    624 		return (EINVAL);			/* XXX still needed? */
    625 
    626 	if ((SCARG(uap, flags) & SUNOS__MAP_NEW) == 0)
    627 		return (EINVAL);
    628 
    629 	SCARG(&ouap, flags) = SCARG(uap, flags) & ~SUNOS__MAP_NEW;
    630 	SCARG(&ouap, addr) = SCARG(uap, addr);
    631 	SCARG(&ouap, len) = SCARG(uap, len);
    632 	SCARG(&ouap, prot) = SCARG(uap, prot);
    633 	SCARG(&ouap, fd) = SCARG(uap, fd);
    634 	SCARG(&ouap, pos) = SCARG(uap, pos);
    635 
    636 	return (sys_mmap(l, &ouap, retval));
    637 }
    638 
    639 #define	MC_SYNC		1
    640 #define	MC_LOCK		2
    641 #define	MC_UNLOCK	3
    642 #define	MC_ADVISE	4
    643 #define	MC_LOCKAS	5
    644 #define	MC_UNLOCKAS	6
    645 
    646 int
    647 sunos_sys_mctl(l, v, retval)
    648 	struct lwp *l;
    649 	void *v;
    650 	register_t *retval;
    651 {
    652 	struct sunos_sys_mctl_args *uap = v;
    653 
    654 	switch (SCARG(uap, func)) {
    655 	case MC_ADVISE:		/* ignore for now */
    656 		return (0);
    657 	case MC_SYNC:		/* translate to msync */
    658 		return (sys___msync13(l, uap, retval));
    659 	default:
    660 		return (EINVAL);
    661 	}
    662 }
    663 
    664 int
    665 sunos_sys_setsockopt(l, v, retval)
    666 	struct lwp *l;
    667 	void *v;
    668 	register_t *retval;
    669 {
    670 	struct sunos_sys_setsockopt_args *uap = v;
    671 	struct proc *p = l->l_proc;
    672 	struct file *fp;
    673 	struct mbuf *m = NULL;
    674 	int error;
    675 
    676 	/* getsock() will use the descriptor for us */
    677 	if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
    678 		return (error);
    679 #define	SO_DONTLINGER (~SO_LINGER)
    680 	if (SCARG(uap, name) == SO_DONTLINGER) {
    681 		m = m_get(M_WAIT, MT_SOOPTS);
    682 		mtod(m, struct linger *)->l_onoff = 0;
    683 		m->m_len = sizeof(struct linger);
    684 		error = sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
    685 		    SO_LINGER, m);
    686 		goto out;
    687 	}
    688 	if (SCARG(uap, level) == IPPROTO_IP) {
    689 #define		SUNOS_IP_MULTICAST_IF		2
    690 #define		SUNOS_IP_MULTICAST_TTL		3
    691 #define		SUNOS_IP_MULTICAST_LOOP		4
    692 #define		SUNOS_IP_ADD_MEMBERSHIP		5
    693 #define		SUNOS_IP_DROP_MEMBERSHIP	6
    694 		static const int ipoptxlat[] = {
    695 			IP_MULTICAST_IF,
    696 			IP_MULTICAST_TTL,
    697 			IP_MULTICAST_LOOP,
    698 			IP_ADD_MEMBERSHIP,
    699 			IP_DROP_MEMBERSHIP
    700 		};
    701 		if (SCARG(uap, name) >= SUNOS_IP_MULTICAST_IF &&
    702 		    SCARG(uap, name) <= SUNOS_IP_DROP_MEMBERSHIP) {
    703 			SCARG(uap, name) =
    704 			    ipoptxlat[SCARG(uap, name) - SUNOS_IP_MULTICAST_IF];
    705 		}
    706 	}
    707 	if (SCARG(uap, valsize) > MLEN) {
    708 		error = EINVAL;
    709 		goto out;
    710 	}
    711 	if (SCARG(uap, val)) {
    712 		m = m_get(M_WAIT, MT_SOOPTS);
    713 		error = copyin(SCARG(uap, val), mtod(m, caddr_t),
    714 		    (u_int)SCARG(uap, valsize));
    715 		if (error) {
    716 			(void) m_free(m);
    717 			goto out;
    718 		}
    719 		m->m_len = SCARG(uap, valsize);
    720 	}
    721 	error = sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
    722 	    SCARG(uap, name), m);
    723  out:
    724 	FILE_UNUSE(fp, l);
    725 	return (error);
    726 }
    727 
    728 static inline int sunos_sys_socket_common(struct lwp *, register_t *,
    729 					      int type);
    730 static inline int
    731 sunos_sys_socket_common(l, retval, type)
    732 	struct lwp *l;
    733 	register_t *retval;
    734 	int type;
    735 {
    736 	struct socket *so;
    737 	struct file *fp;
    738 	int error, fd;
    739 
    740 	/* getsock() will use the descriptor for us */
    741 	fd = (int)*retval;
    742 	if ((error = getsock(l->l_proc->p_fd, fd, &fp)) == 0) {
    743 		so = (struct socket *)fp->f_data;
    744 		if (type == SOCK_DGRAM)
    745 			so->so_options |= SO_BROADCAST;
    746 	}
    747 	FILE_UNUSE(fp, l);
    748 	return (error);
    749 }
    750 
    751 int
    752 sunos_sys_socket(l, v, retval)
    753 	struct lwp *l;
    754 	void *v;
    755 	register_t *retval;
    756 {
    757 	struct sunos_sys_socket_args /* {
    758 		syscallarg(int) domain;
    759 		syscallarg(int) type;
    760 		syscallarg(int) protocol;
    761 	} */ *uap = v;
    762 	int error;
    763 
    764 	error = sys_socket(l, v, retval);
    765 	if (error)
    766 		return (error);
    767 	return sunos_sys_socket_common(l, retval, SCARG(uap, type));
    768 }
    769 
    770 int
    771 sunos_sys_socketpair(l, v, retval)
    772 	struct lwp *l;
    773 	void *v;
    774 	register_t *retval;
    775 {
    776 	struct sunos_sys_socketpair_args /* {
    777 		syscallarg(int) domain;
    778 		syscallarg(int) type;
    779 		syscallarg(int) protocol;
    780 		syscallarg(int *) rsv;
    781 	} */ *uap = v;
    782 	int error;
    783 
    784 	error = sys_socketpair(l, v, retval);
    785 	if (error)
    786 		return (error);
    787 	return sunos_sys_socket_common(l, retval, SCARG(uap, type));
    788 }
    789 
    790 /*
    791  * XXX: This needs cleaning up.
    792  */
    793 int
    794 sunos_sys_auditsys(l, v, retval)
    795 	struct lwp *l;
    796 	void *v;
    797 	register_t *retval;
    798 {
    799 	return 0;
    800 }
    801 
    802 int
    803 sunos_sys_uname(l, v, retval)
    804 	struct lwp *l;
    805 	void *v;
    806 	register_t *retval;
    807 {
    808 	struct sunos_sys_uname_args *uap = v;
    809 	struct sunos_utsname sut;
    810 
    811 	memset(&sut, 0, sizeof(sut));
    812 
    813 	memcpy(sut.sysname, ostype, sizeof(sut.sysname) - 1);
    814 	memcpy(sut.nodename, hostname, sizeof(sut.nodename));
    815 	sut.nodename[sizeof(sut.nodename)-1] = '\0';
    816 	memcpy(sut.release, osrelease, sizeof(sut.release) - 1);
    817 	memcpy(sut.version, "1", sizeof(sut.version) - 1);
    818 	memcpy(sut.machine, machine, sizeof(sut.machine) - 1);
    819 
    820 	return copyout((caddr_t)&sut, (caddr_t)SCARG(uap, name),
    821 	    sizeof(struct sunos_utsname));
    822 }
    823 
    824 int
    825 sunos_sys_setpgrp(l, v, retval)
    826 	struct lwp *l;
    827 	void *v;
    828 	register_t *retval;
    829 {
    830 	struct sunos_sys_setpgrp_args *uap = v;
    831 	struct proc *p = l->l_proc;
    832 
    833 	/*
    834 	 * difference to our setpgid call is to include backwards
    835 	 * compatibility to pre-setsid() binaries. Do setsid()
    836 	 * instead of setpgid() in those cases where the process
    837 	 * tries to create a new session the old way.
    838 	 */
    839 	if (!SCARG(uap, pgid) &&
    840 	    (!SCARG(uap, pid) || SCARG(uap, pid) == p->p_pid))
    841 		return sys_setsid(l, uap, retval);
    842 	else
    843 		return sys_setpgid(l, uap, retval);
    844 }
    845 
    846 int
    847 sunos_sys_open(l, v, retval)
    848 	struct lwp *l;
    849 	void *v;
    850 	register_t *retval;
    851 {
    852 	struct sunos_sys_open_args *uap = v;
    853 	struct proc *p = l->l_proc;
    854 	int smode, nmode;
    855 	int noctty;
    856 	int ret;
    857 
    858 	caddr_t sg = stackgap_init(p, 0);
    859 
    860 	/* convert mode into NetBSD mode */
    861 	smode = SCARG(uap, flags);
    862 	noctty = smode & 0x8000;
    863 	nmode =	smode &
    864 		(0x0001 | 0x0002 | 0x0008 | 0x0040 | 0x0200 | 0x0400 | 0x0800);
    865 	nmode |= ((smode & (0x0004 | 0x1000 | 0x4000)) ? O_NONBLOCK : 0);
    866 	nmode |= ((smode & 0x0080) ? O_SHLOCK : 0);
    867 	nmode |= ((smode & 0x0100) ? O_EXLOCK : 0);
    868 	nmode |= ((smode & 0x2000) ? O_FSYNC : 0);
    869 
    870 	if (nmode & O_CREAT)
    871 		CHECK_ALT_CREAT(l, &sg, SCARG(uap, path));
    872 	else
    873 		CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
    874 
    875 	SCARG(uap, flags) = nmode;
    876 	ret = sys_open(l, (struct sys_open_args *)uap, retval);
    877 
    878 	if (!ret && !noctty && SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
    879 		struct filedesc *fdp = p->p_fd;
    880 		struct file *fp;
    881 
    882 		fp = fd_getfile(fdp, *retval);
    883 		simple_unlock(&fp->f_slock);
    884 
    885 		/* ignore any error, just give it a try */
    886 		if (fp != NULL && fp->f_type == DTYPE_VNODE)
    887 			(fp->f_ops->fo_ioctl)(fp, TIOCSCTTY, (caddr_t)0, l);
    888 	}
    889 	return ret;
    890 }
    891 
    892 #if defined (NFSSERVER)
    893 int
    894 sunos_sys_nfssvc(l, v, retval)
    895 	struct lwp *l;
    896 	void *v;
    897 	register_t *retval;
    898 {
    899 #if 0
    900 	struct sunos_sys_nfssvc_args *uap = v;
    901 	struct proc *p = l->l_proc;
    902 	struct emul *e = p->p_emul;
    903 	struct sys_nfssvc_args outuap;
    904 	struct sockaddr sa;
    905 	int error;
    906 	caddr_t sg = stackgap_init(p, 0);
    907 
    908 	memset(&outuap, 0, sizeof outuap);
    909 	SCARG(&outuap, fd) = SCARG(uap, fd);
    910 	SCARG(&outuap, mskval) = stackgap_alloc(p, &sg, sizeof(sa));
    911 	SCARG(&outuap, msklen) = sizeof(sa);
    912 	SCARG(&outuap, mtchval) = stackgap_alloc(p, &sg, sizeof(sa));
    913 	SCARG(&outuap, mtchlen) = sizeof(sa);
    914 
    915 	memset(&sa, 0, sizeof sa);
    916 	if (error = copyout(&sa, SCARG(&outuap, mskval), SCARG(&outuap, msklen)))
    917 		return (error);
    918 	if (error = copyout(&sa, SCARG(&outuap, mtchval), SCARG(&outuap, mtchlen)))
    919 		return (error);
    920 
    921 	return nfssvc(l, &outuap, retval);
    922 #else
    923 	return (ENOSYS);
    924 #endif
    925 }
    926 #endif /* NFSSERVER */
    927 
    928 int
    929 sunos_sys_ustat(l, v, retval)
    930 	struct lwp *l;
    931 	void *v;
    932 	register_t *retval;
    933 {
    934 	struct sunos_sys_ustat_args *uap = v;
    935 	struct sunos_ustat us;
    936 	int error;
    937 
    938 	memset(&us, 0, sizeof us);
    939 
    940 	/*
    941 	 * XXX: should set f_tfree and f_tinode at least
    942 	 * How do we translate dev -> fstat? (and then to sunos_ustat)
    943 	 */
    944 
    945 	if ((error = copyout(&us, SCARG(uap, buf), sizeof us)) != 0)
    946 		return (error);
    947 	return 0;
    948 }
    949 
    950 int
    951 sunos_sys_quotactl(l, v, retval)
    952 	struct lwp *l;
    953 	void *v;
    954 	register_t *retval;
    955 {
    956 
    957 	return EINVAL;
    958 }
    959 
    960 int
    961 sunos_sys_vhangup(l, v, retval)
    962 	struct lwp *l;
    963 	void *v;
    964 	register_t *retval;
    965 {
    966 	struct proc *p = l->l_proc;
    967 	struct session *sp = p->p_session;
    968 
    969 	if (sp->s_ttyvp == 0)
    970 		return 0;
    971 
    972 	if (sp->s_ttyp && sp->s_ttyp->t_session == sp && sp->s_ttyp->t_pgrp)
    973 		pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
    974 
    975 	(void) ttywait(sp->s_ttyp);
    976 	if (sp->s_ttyvp)
    977 		VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
    978 	if (sp->s_ttyvp)
    979 		vrele(sp->s_ttyvp);
    980 	sp->s_ttyvp = NULL;
    981 
    982 	return 0;
    983 }
    984 
    985 static int
    986 sunstatfs(sp, buf)
    987 	struct statvfs *sp;
    988 	caddr_t buf;
    989 {
    990 	struct sunos_statfs ssfs;
    991 
    992 	memset(&ssfs, 0, sizeof ssfs);
    993 	ssfs.f_type = 0;
    994 	ssfs.f_bsize = sp->f_bsize;
    995 	ssfs.f_blocks = sp->f_blocks;
    996 	ssfs.f_bfree = sp->f_bfree;
    997 	ssfs.f_bavail = sp->f_bavail;
    998 	ssfs.f_files = sp->f_files;
    999 	ssfs.f_ffree = sp->f_ffree;
   1000 	ssfs.f_fsid = sp->f_fsidx;
   1001 	return copyout((caddr_t)&ssfs, buf, sizeof ssfs);
   1002 }
   1003 
   1004 int
   1005 sunos_sys_statfs(l, v, retval)
   1006 	struct lwp *l;
   1007 	void *v;
   1008 	register_t *retval;
   1009 {
   1010 	struct sunos_sys_statfs_args *uap = v;
   1011 	struct proc *p = l->l_proc;
   1012 	struct mount *mp;
   1013 	struct statvfs *sp;
   1014 	int error;
   1015 	struct nameidata nd;
   1016 
   1017 	caddr_t sg = stackgap_init(p, 0);
   1018 	CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
   1019 
   1020 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), l);
   1021 	if ((error = namei(&nd)) != 0)
   1022 		return (error);
   1023 	mp = nd.ni_vp->v_mount;
   1024 	sp = &mp->mnt_stat;
   1025 	vrele(nd.ni_vp);
   1026 	if ((error = VFS_STATVFS(mp, sp, l)) != 0)
   1027 		return (error);
   1028 	sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
   1029 	return sunstatfs(sp, (caddr_t)SCARG(uap, buf));
   1030 }
   1031 
   1032 int
   1033 sunos_sys_fstatfs(l, v, retval)
   1034 	struct lwp *l;
   1035 	void *v;
   1036 	register_t *retval;
   1037 {
   1038 	struct sunos_sys_fstatfs_args *uap = v;
   1039 	struct proc *p = l->l_proc;
   1040 	struct file *fp;
   1041 	struct mount *mp;
   1042 	struct statvfs *sp;
   1043 	int error;
   1044 
   1045 	/* getvnode() will use the descriptor for us */
   1046 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   1047 		return (error);
   1048 	mp = ((struct vnode *)fp->f_data)->v_mount;
   1049 	sp = &mp->mnt_stat;
   1050 	if ((error = VFS_STATVFS(mp, sp, l)) != 0)
   1051 		goto out;
   1052 	sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
   1053 	error = sunstatfs(sp, (caddr_t)SCARG(uap, buf));
   1054  out:
   1055 	FILE_UNUSE(fp, l);
   1056 	return (error);
   1057 }
   1058 
   1059 int
   1060 sunos_sys_exportfs(l, v, retval)
   1061 	struct lwp *l;
   1062 	void *v;
   1063 	register_t *retval;
   1064 {
   1065 	/*
   1066 	 * XXX: should perhaps translate into a mount(2)
   1067 	 * with MOUNT_EXPORT?
   1068 	 */
   1069 	return 0;
   1070 }
   1071 
   1072 int
   1073 sunos_sys_mknod(l, v, retval)
   1074 	struct lwp *l;
   1075 	void *v;
   1076 	register_t *retval;
   1077 {
   1078 	struct sunos_sys_mknod_args *uap = v;
   1079 	struct proc *p = l->l_proc;
   1080 
   1081 	caddr_t sg = stackgap_init(p, 0);
   1082 	CHECK_ALT_CREAT(l, &sg, SCARG(uap, path));
   1083 
   1084 	if (S_ISFIFO(SCARG(uap, mode)))
   1085 		return sys_mkfifo(l, uap, retval);
   1086 
   1087 	return sys_mknod(l, (struct sys_mknod_args *)uap, retval);
   1088 }
   1089 
   1090 #define SUNOS_SC_ARG_MAX	1
   1091 #define SUNOS_SC_CHILD_MAX	2
   1092 #define SUNOS_SC_CLK_TCK	3
   1093 #define SUNOS_SC_NGROUPS_MAX	4
   1094 #define SUNOS_SC_OPEN_MAX	5
   1095 #define SUNOS_SC_JOB_CONTROL	6
   1096 #define SUNOS_SC_SAVED_IDS	7
   1097 #define SUNOS_SC_VERSION	8
   1098 
   1099 int
   1100 sunos_sys_sysconf(l, v, retval)
   1101 	struct lwp *l;
   1102 	void *v;
   1103 	register_t *retval;
   1104 {
   1105 	struct sunos_sys_sysconf_args *uap = v;
   1106 	extern int maxfiles;
   1107 
   1108 	switch(SCARG(uap, name)) {
   1109 	case SUNOS_SC_ARG_MAX:
   1110 		*retval = ARG_MAX;
   1111 		break;
   1112 	case SUNOS_SC_CHILD_MAX:
   1113 		*retval = maxproc;
   1114 		break;
   1115 	case SUNOS_SC_CLK_TCK:
   1116 		*retval = 60;		/* should this be `hz', ie. 100? */
   1117 		break;
   1118 	case SUNOS_SC_NGROUPS_MAX:
   1119 		*retval = NGROUPS_MAX;
   1120 		break;
   1121 	case SUNOS_SC_OPEN_MAX:
   1122 		*retval = maxfiles;
   1123 		break;
   1124 	case SUNOS_SC_JOB_CONTROL:
   1125 		*retval = 1;
   1126 		break;
   1127 	case SUNOS_SC_SAVED_IDS:
   1128 #ifdef _POSIX_SAVED_IDS
   1129 		*retval = 1;
   1130 #else
   1131 		*retval = 0;
   1132 #endif
   1133 		break;
   1134 	case SUNOS_SC_VERSION:
   1135 		*retval = 198808;
   1136 		break;
   1137 	default:
   1138 		return EINVAL;
   1139 	}
   1140 	return 0;
   1141 }
   1142 
   1143 #define SUNOS_RLIMIT_NOFILE	6	/* Other RLIMIT_* are the same */
   1144 #define SUNOS_RLIM_NLIMITS	7
   1145 
   1146 int
   1147 sunos_sys_getrlimit(l, v, retval)
   1148 	struct lwp *l;
   1149 	void *v;
   1150 	register_t *retval;
   1151 {
   1152 	struct sunos_sys_getrlimit_args *uap = v;
   1153 
   1154 	if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
   1155 		return EINVAL;
   1156 
   1157 	if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
   1158 		SCARG(uap, which) = RLIMIT_NOFILE;
   1159 
   1160 	return compat_43_sys_getrlimit(l, uap, retval);
   1161 }
   1162 
   1163 int
   1164 sunos_sys_setrlimit(l, v, retval)
   1165 	struct lwp *l;
   1166 	void *v;
   1167 	register_t *retval;
   1168 {
   1169 	struct sunos_sys_getrlimit_args *uap = v;
   1170 
   1171 	if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
   1172 		return EINVAL;
   1173 
   1174 	if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
   1175 		SCARG(uap, which) = RLIMIT_NOFILE;
   1176 
   1177 	return compat_43_sys_setrlimit(l, uap, retval);
   1178 }
   1179 
   1180 /* for the m68k machines */
   1181 #ifndef PT_GETFPREGS
   1182 #define PT_GETFPREGS -1
   1183 #endif
   1184 #ifndef PT_SETFPREGS
   1185 #define PT_SETFPREGS -1
   1186 #endif
   1187 
   1188 static const int sreq2breq[] = {
   1189 	PT_TRACE_ME,    PT_READ_I,      PT_READ_D,      -1,
   1190 	PT_WRITE_I,     PT_WRITE_D,     -1,             PT_CONTINUE,
   1191 	PT_KILL,        -1,             PT_ATTACH,      PT_DETACH,
   1192 	PT_GETREGS,     PT_SETREGS,     PT_GETFPREGS,   PT_SETFPREGS
   1193 };
   1194 static const int nreqs = sizeof(sreq2breq) / sizeof(sreq2breq[0]);
   1195 
   1196 int
   1197 sunos_sys_ptrace(l, v, retval)
   1198 	struct lwp *l;
   1199 	void *v;
   1200 	register_t *retval;
   1201 {
   1202 	struct sunos_sys_ptrace_args *uap = v;
   1203 	struct sys_ptrace_args pa;
   1204 	int req;
   1205 
   1206 	req = SCARG(uap, req);
   1207 
   1208 	if (req < 0 || req >= nreqs)
   1209 		return (EINVAL);
   1210 
   1211 	req = sreq2breq[req];
   1212 	if (req == -1)
   1213 		return (EINVAL);
   1214 
   1215 	SCARG(&pa, req) = req;
   1216 	SCARG(&pa, pid) = (pid_t)SCARG(uap, pid);
   1217 	SCARG(&pa, addr) = (caddr_t)SCARG(uap, addr);
   1218 	SCARG(&pa, data) = SCARG(uap, data);
   1219 
   1220 	return sys_ptrace(l, &pa, retval);
   1221 }
   1222 
   1223 /*
   1224  * SunOS reboot system call (for compatibility).
   1225  * Sun lets you pass in a boot string which the PROM
   1226  * saves and provides to the next boot program.
   1227  */
   1228 
   1229 #define SUNOS_RB_ASKNAME	0x001
   1230 #define SUNOS_RB_SINGLE 	0x002
   1231 #define SUNOS_RB_NOSYNC		0x004
   1232 #define SUNOS_RB_HALT		0x008
   1233 #define SUNOS_RB_DUMP		0x080
   1234 #define	SUNOS_RB_STRING		0x200
   1235 
   1236 static struct sunos_howto_conv {
   1237 	int sun_howto;
   1238 	int bsd_howto;
   1239 } sunos_howto_conv[] = {
   1240 	{ SUNOS_RB_ASKNAME,	RB_ASKNAME },
   1241 	{ SUNOS_RB_SINGLE,	RB_SINGLE },
   1242 	{ SUNOS_RB_NOSYNC,	RB_NOSYNC },
   1243 	{ SUNOS_RB_HALT,	RB_HALT },
   1244 	{ SUNOS_RB_DUMP,	RB_DUMP },
   1245 	{ SUNOS_RB_STRING,	RB_STRING },
   1246 	{ 0x000,		0 },
   1247 };
   1248 
   1249 int
   1250 sunos_sys_reboot(l, v, retval)
   1251 	struct lwp *l;
   1252 	void *v;
   1253 	register_t *retval;
   1254 {
   1255 	struct sunos_sys_reboot_args *uap = v;
   1256 	struct proc *p = l->l_proc;
   1257 	struct sys_reboot_args ua;
   1258 	struct sunos_howto_conv *convp;
   1259 	int error, bsd_howto, sun_howto;
   1260 	char *bootstr;
   1261 
   1262 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   1263 		return (error);
   1264 
   1265 	/*
   1266 	 * Convert howto bits to BSD format.
   1267 	 */
   1268 	sun_howto = SCARG(uap, howto);
   1269 	bsd_howto = 0;
   1270 	convp = sunos_howto_conv;
   1271 	while (convp->sun_howto) {
   1272 		if (sun_howto & convp->sun_howto)
   1273 			bsd_howto |= convp->bsd_howto;
   1274 		convp++;
   1275 	}
   1276 
   1277 	/*
   1278 	 * Sun RB_STRING (Get user supplied bootstring.)
   1279 	 * If the machine supports passing a string to the
   1280 	 * next booted kernel.
   1281 	 */
   1282 	if (sun_howto & SUNOS_RB_STRING) {
   1283 		char bs[128];
   1284 
   1285 		error = copyinstr(SCARG(uap, bootstr), bs, sizeof(bs), 0);
   1286 
   1287 		if (error)
   1288 			bootstr = NULL;
   1289 		else
   1290 			bootstr = bs;
   1291 	} else
   1292 		bootstr = NULL;
   1293 
   1294 	SCARG(&ua, opt) = bsd_howto;
   1295 	SCARG(&ua, bootstr) = bootstr;
   1296 	sys_reboot(l, &ua, retval);
   1297 	return(0);
   1298 }
   1299 
   1300 /*
   1301  * Generalized interface signal handler, 4.3-compatible.
   1302  */
   1303 /* ARGSUSED */
   1304 int
   1305 sunos_sys_sigvec(l, v, retval)
   1306 	struct lwp *l;
   1307 	void *v;
   1308 	register_t *retval;
   1309 {
   1310 	struct sunos_sys_sigvec_args /* {
   1311 		syscallarg(int) signum;
   1312 		syscallarg(struct sigvec *) nsv;
   1313 		syscallarg(struct sigvec *) osv;
   1314 	} */ *uap = v;
   1315 	struct sigvec nsv, osv;
   1316 	struct sigaction nsa, osa;
   1317 	int error;
   1318 /*XXX*/extern	void compat_43_sigvec_to_sigaction
   1319 		__P((const struct sigvec *, struct sigaction *));
   1320 /*XXX*/extern	void compat_43_sigaction_to_sigvec
   1321 		__P((const struct sigaction *, struct sigvec *));
   1322 
   1323 	if (SCARG(uap, nsv)) {
   1324 		error = copyin(SCARG(uap, nsv), &nsv, sizeof(nsv));
   1325 		if (error != 0)
   1326 			return (error);
   1327 
   1328 		/*
   1329 		 * SunOS uses the mask 0x0004 as SV_RESETHAND
   1330 		 * meaning: `reset to SIG_DFL on delivery'.
   1331 		 * We support only the bits in: 0xF
   1332 		 * (those bits are the same as ours)
   1333 		 */
   1334 		if (nsv.sv_flags & ~0xF)
   1335 			return (EINVAL);
   1336 
   1337 		compat_43_sigvec_to_sigaction(&nsv, &nsa);
   1338 	}
   1339 	error = sigaction1(l->l_proc, SCARG(uap, signum),
   1340 			   SCARG(uap, nsv) ? &nsa : 0,
   1341 			   SCARG(uap, osv) ? &osa : 0,
   1342 			   NULL, 0);
   1343 	if (error != 0)
   1344 		return (error);
   1345 
   1346 	if (SCARG(uap, osv)) {
   1347 		compat_43_sigaction_to_sigvec(&osa, &osv);
   1348 		error = copyout(&osv, SCARG(uap, osv), sizeof(osv));
   1349 		if (error != 0)
   1350 			return (error);
   1351 	}
   1352 
   1353 	return (0);
   1354 }
   1355