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