Home | History | Annotate | Line # | Download | only in sunos
sunos_misc.c revision 1.53
      1 /*	$NetBSD: sunos_misc.c,v 1.53 1995/08/15 17:28:02 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/dir.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/vnode.h>
     78 #include <sys/uio.h>
     79 #include <sys/wait.h>
     80 #include <sys/utsname.h>
     81 #include <sys/unistd.h>
     82 #include <sys/syscallargs.h>
     83 #include <compat/sunos/sunos.h>
     84 #include <compat/sunos/sunos_syscallargs.h>
     85 #include <compat/sunos/sunos_util.h>
     86 
     87 #include <netinet/in.h>
     88 
     89 #include <miscfs/specfs/specdev.h>
     90 
     91 #include <nfs/rpcv2.h>
     92 #include <nfs/nfsv2.h>
     93 #include <nfs/nfs.h>
     94 
     95 #include <vm/vm.h>
     96 
     97 int
     98 sunos_wait4(p, uap, retval)
     99 	struct proc *p;
    100 	struct sunos_wait4_args *uap;
    101 	register_t *retval;
    102 {
    103 	if (SCARG(uap, pid) == 0)
    104 		SCARG(uap, pid) = WAIT_ANY;
    105 	return (wait4(p, uap, retval, 1));
    106 }
    107 
    108 int
    109 sunos_creat(p, uap, retval)
    110 	struct proc *p;
    111 	struct sunos_creat_args *uap;
    112 	register_t *retval;
    113 {
    114 	struct sunos_open_args ouap;
    115 
    116 	caddr_t sg = stackgap_init(p->p_emul);
    117 	SUNOS_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    118 
    119 	SCARG(&ouap, path) = SCARG(uap, path);
    120 	SCARG(&ouap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
    121 	SCARG(&ouap, mode) = SCARG(uap, mode);
    122 	return (open(p, &ouap, retval));
    123 }
    124 
    125 int
    126 sunos_access(p, uap, retval)
    127 	struct proc *p;
    128 	struct sunos_access_args *uap;
    129 	register_t *retval;
    130 {
    131 	caddr_t sg = stackgap_init(p->p_emul);
    132 	SUNOS_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    133 
    134 	return (access(p, uap, retval));
    135 }
    136 
    137 int
    138 sunos_stat(p, uap, retval)
    139 	struct proc *p;
    140 	struct sunos_stat_args *uap;
    141 	register_t *retval;
    142 {
    143 	caddr_t sg = stackgap_init(p->p_emul);
    144 	SUNOS_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    145 
    146 	return (compat_43_stat(p, uap, retval));
    147 }
    148 
    149 int
    150 sunos_lstat(p, uap, retval)
    151 	struct proc *p;
    152 	struct sunos_lstat_args *uap;
    153 	register_t *retval;
    154 {
    155 	caddr_t sg = stackgap_init(p->p_emul);
    156 	SUNOS_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    157 
    158 	return (compat_43_lstat(p, uap, retval));
    159 }
    160 
    161 int
    162 sunos_execv(p, uap, retval)
    163 	struct proc *p;
    164 	struct sunos_execv_args *uap;
    165 	register_t *retval;
    166 {
    167 	struct execve_args ouap;
    168 
    169 	caddr_t sg = stackgap_init(p->p_emul);
    170 	SUNOS_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    171 
    172 	SCARG(&ouap, path) = SCARG(uap, path);
    173 	SCARG(&ouap, argp) = SCARG(uap, argp);
    174 	SCARG(&ouap, envp) = NULL;
    175 	return (execve(p, &ouap, retval));
    176 }
    177 
    178 int
    179 sunos_omsync(p, uap, retval)
    180 	struct proc *p;
    181 	struct sunos_omsync_args *uap;
    182 	register_t *retval;
    183 {
    184 	struct msync_args ouap;
    185 
    186 	if (SCARG(uap, flags))
    187 		return (EINVAL);
    188 	SCARG(&ouap, addr) = SCARG(uap, addr);
    189 	SCARG(&ouap, len) = SCARG(uap, len);
    190 	return (msync(p, &ouap, retval));
    191 }
    192 
    193 int
    194 sunos_unmount(p, uap, retval)
    195 	struct proc *p;
    196 	struct sunos_unmount_args *uap;
    197 	register_t *retval;
    198 {
    199 	SCARG(uap, flags) = 0;
    200 	return (unmount(p, (struct unmount_args *)uap, retval));
    201 }
    202 
    203 int
    204 sunos_mount(p, uap, retval)
    205 	struct proc *p;
    206 	struct sunos_mount_args *uap;
    207 	register_t *retval;
    208 {
    209 	struct emul *e = p->p_emul;
    210 	int oflags = SCARG(uap, flags), nflags, error;
    211 	char fsname[MFSNAMELEN];
    212 
    213 	if (oflags & (SUNM_NOSUB | SUNM_SYS5))
    214 		return (EINVAL);
    215 	if ((oflags & SUNM_NEWTYPE) == 0)
    216 		return (EINVAL);
    217 	nflags = 0;
    218 	if (oflags & SUNM_RDONLY)
    219 		nflags |= MNT_RDONLY;
    220 	if (oflags & SUNM_NOSUID)
    221 		nflags |= MNT_NOSUID;
    222 	if (oflags & SUNM_REMOUNT)
    223 		nflags |= MNT_UPDATE;
    224 	SCARG(uap, flags) = nflags;
    225 
    226 	if (error = copyinstr((caddr_t)SCARG(uap, type), fsname,
    227 	    sizeof fsname, (size_t *)0))
    228 		return (error);
    229 
    230 	if (strncmp(fsname, "4.2", sizeof fsname) == 0) {
    231 		SCARG(uap, type) = STACKGAPBASE;
    232 		if (error = copyout("ufs", SCARG(uap, type), sizeof("ufs")))
    233 			return (error);
    234 	} else if (strncmp(fsname, "nfs", sizeof fsname) == 0) {
    235 		struct sunos_nfs_args sna;
    236 		struct sockaddr_in sain;
    237 		struct nfs_args na;
    238 		struct sockaddr sa;
    239 
    240 		if (error = copyin(SCARG(uap, data), &sna, sizeof sna))
    241 			return (error);
    242 		if (error = copyin(sna.addr, &sain, sizeof sain))
    243 			return (error);
    244 		bcopy(&sain, &sa, sizeof sa);
    245 		sa.sa_len = sizeof(sain);
    246 		SCARG(uap, data) = STACKGAPBASE;
    247 		na.addr = (struct sockaddr *)((int)SCARG(uap, data) + sizeof na);
    248 		na.addrlen = sizeof(struct sockaddr);
    249 		na.sotype = SOCK_DGRAM;
    250 		na.proto = IPPROTO_UDP;
    251 		na.fh = (nfsv2fh_t *)sna.fh;
    252 		na.flags = sna.flags;
    253 		na.wsize = sna.wsize;
    254 		na.rsize = sna.rsize;
    255 		na.timeo = sna.timeo;
    256 		na.retrans = sna.retrans;
    257 		na.hostname = sna.hostname;
    258 
    259 		if (error = copyout(&sa, na.addr, sizeof sa))
    260 			return (error);
    261 		if (error = copyout(&na, SCARG(uap, data), sizeof na))
    262 			return (error);
    263 	}
    264 	return (mount(p, (struct mount_args *)uap, retval));
    265 }
    266 
    267 #if defined(NFSCLIENT)
    268 int
    269 async_daemon(p, uap, retval)
    270 	struct proc *p;
    271 	void *uap;
    272 	register_t *retval;
    273 {
    274 	struct nfssvc_args ouap;
    275 
    276 	SCARG(&ouap, flag) = NFSSVC_BIOD;
    277 	SCARG(&ouap, argp) = NULL;
    278 	return nfssvc(p, &ouap, retval);
    279 }
    280 #endif /* NFSCLIENT */
    281 
    282 int
    283 sunos_sigpending(p, uap, retval)
    284 	struct proc *p;
    285 	struct sunos_sigpending_args *uap;
    286 	register_t *retval;
    287 {
    288 	int mask = p->p_siglist & p->p_sigmask;
    289 
    290 	return (copyout((caddr_t)&mask, (caddr_t)SCARG(uap, mask), sizeof(int)));
    291 }
    292 
    293 /*
    294  * Read Sun-style directory entries.  We suck them into kernel space so
    295  * that they can be massaged before being copied out to user code.  Like
    296  * SunOS, we squish out `empty' entries.
    297  *
    298  * This is quite ugly, but what do you expect from compatibility code?
    299  */
    300 int
    301 sunos_getdents(p, uap, retval)
    302 	struct proc *p;
    303 	register struct sunos_getdents_args *uap;
    304 	register_t *retval;
    305 {
    306 	register struct vnode *vp;
    307 	register caddr_t inp, buf;	/* BSD-format */
    308 	register int len, reclen;	/* BSD-format */
    309 	register caddr_t outp;		/* Sun-format */
    310 	register int resid;		/* Sun-format */
    311 	struct file *fp;
    312 	struct uio auio;
    313 	struct iovec aiov;
    314 	off_t off;			/* true file offset */
    315 	long soff;			/* Sun file offset */
    316 	int buflen, error, eofflag;
    317 #define	BSD_DIRENT(cp) ((struct dirent *)(cp))
    318 #define	SUNOS_RECLEN(reclen) (reclen + sizeof(long))
    319 
    320 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
    321 		return (error);
    322 	if ((fp->f_flag & FREAD) == 0)
    323 		return (EBADF);
    324 	vp = (struct vnode *)fp->f_data;
    325 	if (vp->v_type != VDIR)	/* XXX  vnode readdir op should do this */
    326 		return (EINVAL);
    327 	buflen = min(MAXBSIZE, SCARG(uap, nbytes));
    328 	buf = malloc(buflen, M_TEMP, M_WAITOK);
    329 	VOP_LOCK(vp);
    330 	off = fp->f_offset;
    331 again:
    332 	aiov.iov_base = buf;
    333 	aiov.iov_len = buflen;
    334 	auio.uio_iov = &aiov;
    335 	auio.uio_iovcnt = 1;
    336 	auio.uio_rw = UIO_READ;
    337 	auio.uio_segflg = UIO_SYSSPACE;
    338 	auio.uio_procp = p;
    339 	auio.uio_resid = buflen;
    340 	auio.uio_offset = off;
    341 	/*
    342 	 * First we read into the malloc'ed buffer, then
    343 	 * we massage it into user space, one record at a time.
    344 	 */
    345 	if (error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
    346 	    (u_long *)0, 0))
    347 		goto out;
    348 	inp = buf;
    349 	outp = SCARG(uap, buf);
    350 	resid = SCARG(uap, nbytes);
    351 	if ((len = buflen - auio.uio_resid) == 0)
    352 		goto eof;
    353 	for (; len > 0; len -= reclen) {
    354 		reclen = ((struct dirent *)inp)->d_reclen;
    355 		if (reclen & 3)
    356 			panic("sunos_getdents");
    357 		off += reclen;		/* each entry points to next */
    358 		if (BSD_DIRENT(inp)->d_fileno == 0) {
    359 			inp += reclen;	/* it is a hole; squish it out */
    360 			continue;
    361 		}
    362 		if (reclen > len || resid < SUNOS_RECLEN(reclen)) {
    363 			/* entry too big for buffer, so just stop */
    364 			outp++;
    365 			break;
    366 		}
    367 		/*
    368 		 * Massage in place to make a Sun-shaped dirent (otherwise
    369 		 * we have to worry about touching user memory outside of
    370 		 * the copyout() call).
    371 		 */
    372 		BSD_DIRENT(inp)->d_reclen = SUNOS_RECLEN(reclen);
    373 #if notdef
    374 		BSD_DIRENT(inp)->d_type = 0; 	/* 4.4 specific */
    375 #endif
    376 		soff = off;
    377 		if ((error = copyout((caddr_t)&soff, outp, sizeof soff)) != 0 ||
    378 		    (error = copyout(inp, outp + sizeof soff, reclen)) != 0)
    379 			goto out;
    380 		/* advance past this real entry */
    381 		inp += reclen;
    382 		/* advance output past Sun-shaped entry */
    383 		outp += SUNOS_RECLEN(reclen);
    384 		resid -= SUNOS_RECLEN(reclen);
    385 	}
    386 	/* if we squished out the whole block, try again */
    387 	if (outp == SCARG(uap, buf))
    388 		goto again;
    389 	fp->f_offset = off;		/* update the vnode offset */
    390 eof:
    391 	*retval = SCARG(uap, nbytes) - resid;
    392 out:
    393 	VOP_UNLOCK(vp);
    394 	free(buf, M_TEMP);
    395 	return (error);
    396 }
    397 
    398 #define	SUNOS__MAP_NEW	0x80000000	/* if not, old mmap & cannot handle */
    399 
    400 int
    401 sunos_mmap(p, uap, retval)
    402 	register struct proc *p;
    403 	register struct sunos_mmap_args *uap;
    404 	register_t *retval;
    405 {
    406 	struct mmap_args ouap;
    407 	register struct filedesc *fdp;
    408 	register struct file *fp;
    409 	register struct vnode *vp;
    410 
    411 	/*
    412 	 * Verify the arguments.
    413 	 */
    414 	if (SCARG(uap, prot) & ~(PROT_READ|PROT_WRITE|PROT_EXEC))
    415 		return (EINVAL);			/* XXX still needed? */
    416 
    417 	if ((SCARG(uap, flags) & SUNOS__MAP_NEW) == 0)
    418 		return (EINVAL);
    419 
    420 	SCARG(&ouap, flags) = SCARG(uap, flags) & ~SUNOS__MAP_NEW;
    421 	SCARG(&ouap, addr) = SCARG(uap, addr);
    422 
    423 	if ((SCARG(&ouap, flags) & MAP_FIXED) == 0 &&
    424 	    SCARG(&ouap, addr) != 0 &&
    425 	    SCARG(&ouap, addr) < (caddr_t)round_page(p->p_vmspace->vm_daddr+MAXDSIZ))
    426 		SCARG(&ouap, addr) = (caddr_t)round_page(p->p_vmspace->vm_daddr+MAXDSIZ);
    427 
    428 	SCARG(&ouap, len) = SCARG(uap, len);
    429 	SCARG(&ouap, prot) = SCARG(uap, prot);
    430 	SCARG(&ouap, fd) = SCARG(uap, fd);
    431 	SCARG(&ouap, pos) = SCARG(uap, pos);
    432 
    433 	/*
    434 	 * Special case: if fd refers to /dev/zero, map as MAP_ANON.  (XXX)
    435 	 */
    436 	fdp = p->p_fd;
    437 	if ((unsigned)SCARG(&ouap, fd) < fdp->fd_nfiles &&		/*XXX*/
    438 	    (fp = fdp->fd_ofiles[SCARG(&ouap, fd)]) != NULL &&		/*XXX*/
    439 	    fp->f_type == DTYPE_VNODE &&				/*XXX*/
    440 	    (vp = (struct vnode *)fp->f_data)->v_type == VCHR &&	/*XXX*/
    441 	    iszerodev(vp->v_rdev)) {					/*XXX*/
    442 		SCARG(&ouap, flags) |= MAP_ANON;
    443 		SCARG(&ouap, fd) = -1;
    444 	}
    445 
    446 	return (mmap(p, &ouap, retval));
    447 }
    448 
    449 #define	MC_SYNC		1
    450 #define	MC_LOCK		2
    451 #define	MC_UNLOCK	3
    452 #define	MC_ADVISE	4
    453 #define	MC_LOCKAS	5
    454 #define	MC_UNLOCKAS	6
    455 
    456 int
    457 sunos_mctl(p, uap, retval)
    458 	register struct proc *p;
    459 	register struct sunos_mctl_args *uap;
    460 	register_t *retval;
    461 {
    462 
    463 	switch (SCARG(uap, func)) {
    464 	case MC_ADVISE:		/* ignore for now */
    465 		return (0);
    466 	case MC_SYNC:		/* translate to msync */
    467 		return (msync(p, uap, retval));
    468 	default:
    469 		return (EINVAL);
    470 	}
    471 }
    472 
    473 int
    474 sunos_setsockopt(p, uap, retval)
    475 	struct proc *p;
    476 	register struct sunos_setsockopt_args *uap;
    477 	register_t *retval;
    478 {
    479 	struct file *fp;
    480 	struct mbuf *m = NULL;
    481 	int error;
    482 
    483 	if (error = getsock(p->p_fd, SCARG(uap, s), &fp))
    484 		return (error);
    485 #define	SO_DONTLINGER (~SO_LINGER)
    486 	if (SCARG(uap, name) == SO_DONTLINGER) {
    487 		m = m_get(M_WAIT, MT_SOOPTS);
    488 		if (m == NULL)
    489 			return (ENOBUFS);
    490 		mtod(m, struct linger *)->l_onoff = 0;
    491 		m->m_len = sizeof(struct linger);
    492 		return (sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
    493 		    SO_LINGER, m));
    494 	}
    495 	if (SCARG(uap, level) == IPPROTO_IP) {
    496 #define		SUNOS_IP_MULTICAST_IF		2
    497 #define		SUNOS_IP_MULTICAST_TTL		3
    498 #define		SUNOS_IP_MULTICAST_LOOP		4
    499 #define		SUNOS_IP_ADD_MEMBERSHIP		5
    500 #define		SUNOS_IP_DROP_MEMBERSHIP	6
    501 		static int ipoptxlat[] = {
    502 			IP_MULTICAST_IF,
    503 			IP_MULTICAST_TTL,
    504 			IP_MULTICAST_LOOP,
    505 			IP_ADD_MEMBERSHIP,
    506 			IP_DROP_MEMBERSHIP
    507 		};
    508 		if (SCARG(uap, name) >= SUNOS_IP_MULTICAST_IF &&
    509 		    SCARG(uap, name) <= SUNOS_IP_DROP_MEMBERSHIP) {
    510 			SCARG(uap, name) =
    511 			    ipoptxlat[SCARG(uap, name) - SUNOS_IP_MULTICAST_IF];
    512 		}
    513 	}
    514 	if (SCARG(uap, valsize) > MLEN)
    515 		return (EINVAL);
    516 	if (SCARG(uap, val)) {
    517 		m = m_get(M_WAIT, MT_SOOPTS);
    518 		if (m == NULL)
    519 			return (ENOBUFS);
    520 		if (error = copyin(SCARG(uap, val), mtod(m, caddr_t),
    521 		    (u_int)SCARG(uap, valsize))) {
    522 			(void) m_free(m);
    523 			return (error);
    524 		}
    525 		m->m_len = SCARG(uap, valsize);
    526 	}
    527 	return (sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
    528 	    SCARG(uap, name), m));
    529 }
    530 
    531 int
    532 sunos_fchroot(p, uap, retval)
    533 	register struct proc *p;
    534 	register struct sunos_fchroot_args *uap;
    535 	register_t *retval;
    536 {
    537 	register struct filedesc *fdp = p->p_fd;
    538 	register struct vnode *vp;
    539 	struct file *fp;
    540 	int error;
    541 
    542 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    543 		return (error);
    544 	if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
    545 		return (error);
    546 	vp = (struct vnode *)fp->f_data;
    547 	VOP_LOCK(vp);
    548 	if (vp->v_type != VDIR)
    549 		error = ENOTDIR;
    550 	else
    551 		error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
    552 	VOP_UNLOCK(vp);
    553 	if (error)
    554 		return (error);
    555 	VREF(vp);
    556 	if (fdp->fd_rdir != NULL)
    557 		vrele(fdp->fd_rdir);
    558 	fdp->fd_rdir = vp;
    559 	return (0);
    560 }
    561 
    562 /*
    563  * XXX: This needs cleaning up.
    564  */
    565 int
    566 sunos_auditsys(p, uap, retval)
    567 	struct proc *p;
    568 	void *uap;
    569 	register_t *retval;
    570 {
    571 	return 0;
    572 }
    573 
    574 int
    575 sunos_uname(p, uap, retval)
    576 	struct proc *p;
    577 	struct sunos_uname_args *uap;
    578 	register_t *retval;
    579 {
    580 	struct sunos_utsname sut;
    581 	extern char ostype[], machine[], osrelease[];
    582 
    583 	bzero(&sut, sizeof(sut));
    584 
    585 	bcopy(ostype, sut.sysname, sizeof(sut.sysname) - 1);
    586 	bcopy(hostname, sut.nodename, sizeof(sut.nodename));
    587 	sut.nodename[sizeof(sut.nodename)-1] = '\0';
    588 	bcopy(osrelease, sut.release, sizeof(sut.release) - 1);
    589 	bcopy("1", sut.version, sizeof(sut.version) - 1);
    590 	bcopy(machine, sut.machine, sizeof(sut.machine) - 1);
    591 
    592 	return copyout((caddr_t)&sut, (caddr_t)SCARG(uap, name),
    593 	    sizeof(struct sunos_utsname));
    594 }
    595 
    596 int
    597 sunos_setpgid(p, uap, retval)
    598 	struct proc *p;
    599 	struct sunos_setpgid_args *uap;
    600 	register_t *retval;
    601 {
    602 	/*
    603 	 * difference to our setpgid call is to include backwards
    604 	 * compatibility to pre-setsid() binaries. Do setsid()
    605 	 * instead of setpgid() in those cases where the process
    606 	 * tries to create a new session the old way.
    607 	 */
    608 	if (!SCARG(uap, pgid) && (!SCARG(uap, pid) || SCARG(uap, pid) == p->p_pid))
    609 		return setsid(p, uap, retval);
    610 	else
    611 		return setpgid(p, uap, retval);
    612 }
    613 
    614 int
    615 sunos_open(p, uap, retval)
    616 	struct proc *p;
    617 	struct sunos_open_args *uap;
    618 	register_t *retval;
    619 {
    620 	int l, r;
    621 	int noctty;
    622 	int ret;
    623 
    624 	caddr_t sg = stackgap_init(p->p_emul);
    625 	SUNOS_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    626 
    627 	/* convert mode into NetBSD mode */
    628 	l = SCARG(uap, flags);
    629 	noctty = l & 0x8000;
    630 	r =	(l & (0x0001 | 0x0002 | 0x0008 | 0x0040 | 0x0200 | 0x0400 | 0x0800));
    631 	r |=	((l & (0x0004 | 0x1000 | 0x4000)) ? O_NONBLOCK : 0);
    632 	r |=	((l & 0x0080) ? O_SHLOCK : 0);
    633 	r |=	((l & 0x0100) ? O_EXLOCK : 0);
    634 	r |=	((l & 0x2000) ? O_FSYNC : 0);
    635 
    636 	SCARG(uap, flags) = r;
    637 	ret = open(p, (struct open_args *)uap, retval);
    638 
    639 	if (!ret && !noctty && SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
    640 		struct filedesc *fdp = p->p_fd;
    641 		struct file *fp = fdp->fd_ofiles[*retval];
    642 
    643 		/* ignore any error, just give it a try */
    644 		if (fp->f_type == DTYPE_VNODE)
    645 			(fp->f_ops->fo_ioctl)(fp, TIOCSCTTY, (caddr_t) 0, p);
    646 	}
    647 	return ret;
    648 }
    649 
    650 #if defined (NFSSERVER)
    651 int
    652 sunos_nfssvc(p, uap, retval)
    653 	struct proc *p;
    654 	struct sunos_nfssvc_args *uap;
    655 	register_t *retval;
    656 {
    657 	struct emul *e = p->p_emul;
    658 	struct nfssvc_args outuap;
    659 	struct nfsd_srvargs nfsdarg;
    660 	struct sockaddr sa;
    661 	int error;
    662 
    663 #if 0
    664 	bzero(&outuap, sizeof outuap);
    665 	SCARG(&outuap, fd) = SCARG(uap, fd);
    666 	SCARG(&outuap, mskval) = STACKGAPBASE;
    667 	SCARG(&outuap, msklen) = sizeof sa;
    668 	SCARG(&outuap, mtchval) = SCARG(&outuap, mskval) + sizeof sa;
    669 	SCARG(&outuap, mtchlen) = sizeof sa;
    670 
    671 	bzero(&sa, sizeof sa);
    672 	if (error = copyout(&sa, SCARG(&outuap, mskval), SCARG(&outuap, msklen)))
    673 		return (error);
    674 	if (error = copyout(&sa, SCARG(&outuap, mtchval), SCARG(&outuap, mtchlen)))
    675 		return (error);
    676 
    677 	return nfssvc(p, &outuap, retval);
    678 #else
    679 	return (ENOSYS);
    680 #endif
    681 }
    682 #endif /* NFSSERVER */
    683 
    684 int
    685 sunos_ustat(p, uap, retval)
    686 	struct proc *p;
    687 	struct sunos_ustat_args *uap;
    688 	register_t *retval;
    689 {
    690 	struct sunos_ustat us;
    691 	int error;
    692 
    693 	bzero(&us, sizeof us);
    694 
    695 	/*
    696 	 * XXX: should set f_tfree and f_tinode at least
    697 	 * How do we translate dev -> fstat? (and then to sunos_ustat)
    698 	 */
    699 
    700 	if (error = copyout(&us, SCARG(uap, buf), sizeof us))
    701 		return (error);
    702 	return 0;
    703 }
    704 
    705 int
    706 sunos_quotactl(p, uap, retval)
    707 	struct proc *p;
    708 	struct sunos_quotactl_args *uap;
    709 	register_t *retval;
    710 {
    711 	return EINVAL;
    712 }
    713 
    714 int
    715 sunos_vhangup(p, uap, retval)
    716 	struct proc *p;
    717 	void *uap;
    718 	register_t *retval;
    719 {
    720 	return 0;
    721 }
    722 
    723 static
    724 sunstatfs(sp, buf)
    725 	struct statfs *sp;
    726 	caddr_t buf;
    727 {
    728 	struct sunos_statfs ssfs;
    729 
    730 	bzero(&ssfs, sizeof ssfs);
    731 	ssfs.f_type = 0;
    732 	ssfs.f_bsize = sp->f_bsize;
    733 	ssfs.f_blocks = sp->f_blocks;
    734 	ssfs.f_bfree = sp->f_bfree;
    735 	ssfs.f_bavail = sp->f_bavail;
    736 	ssfs.f_files = sp->f_files;
    737 	ssfs.f_ffree = sp->f_ffree;
    738 	ssfs.f_fsid = sp->f_fsid;
    739 	return copyout((caddr_t)&ssfs, buf, sizeof ssfs);
    740 }
    741 
    742 int
    743 sunos_statfs(p, uap, retval)
    744 	struct proc *p;
    745 	struct sunos_statfs_args *uap;
    746 	register_t *retval;
    747 {
    748 	register struct mount *mp;
    749 	register struct statfs *sp;
    750 	int error;
    751 	struct nameidata nd;
    752 
    753 	caddr_t sg = stackgap_init(p->p_emul);
    754 	SUNOS_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    755 
    756 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
    757 	if (error = namei(&nd))
    758 		return (error);
    759 	mp = nd.ni_vp->v_mount;
    760 	sp = &mp->mnt_stat;
    761 	vrele(nd.ni_vp);
    762 	if (error = VFS_STATFS(mp, sp, p))
    763 		return (error);
    764 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
    765 	return sunstatfs(sp, (caddr_t)SCARG(uap, buf));
    766 }
    767 
    768 int
    769 sunos_fstatfs(p, uap, retval)
    770 	struct proc *p;
    771 	struct sunos_fstatfs_args *uap;
    772 	register_t *retval;
    773 {
    774 	struct file *fp;
    775 	struct mount *mp;
    776 	register struct statfs *sp;
    777 	int error;
    778 
    779 	if (error = getvnode(p->p_fd, SCARG(uap, fd), &fp))
    780 		return (error);
    781 	mp = ((struct vnode *)fp->f_data)->v_mount;
    782 	sp = &mp->mnt_stat;
    783 	if (error = VFS_STATFS(mp, sp, p))
    784 		return (error);
    785 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
    786 	return sunstatfs(sp, (caddr_t)SCARG(uap, buf));
    787 }
    788 
    789 int
    790 sunos_exportfs(p, uap, retval)
    791 	struct proc *p;
    792 	struct sunos_exportfs_args *uap;
    793 	register_t *retval;
    794 {
    795 	/*
    796 	 * XXX: should perhaps translate into a mount(2)
    797 	 * with MOUNT_EXPORT?
    798 	 */
    799 	return 0;
    800 }
    801 
    802 int
    803 sunos_mknod(p, uap, retval)
    804 	struct proc *p;
    805 	struct sunos_mknod_args *uap;
    806 	register_t *retval;
    807 {
    808 	caddr_t sg = stackgap_init(p->p_emul);
    809 	SUNOS_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    810 
    811 	if (S_ISFIFO(SCARG(uap, mode)))
    812 		return mkfifo(p, uap, retval);
    813 
    814 	return mknod(p, (struct mknod_args *)uap, retval);
    815 }
    816 
    817 #define SUNOS_SC_ARG_MAX	1
    818 #define SUNOS_SC_CHILD_MAX	2
    819 #define SUNOS_SC_CLK_TCK	3
    820 #define SUNOS_SC_NGROUPS_MAX	4
    821 #define SUNOS_SC_OPEN_MAX	5
    822 #define SUNOS_SC_JOB_CONTROL	6
    823 #define SUNOS_SC_SAVED_IDS	7
    824 #define SUNOS_SC_VERSION	8
    825 
    826 int
    827 sunos_sysconf(p, uap, retval)
    828 	struct proc *p;
    829 	struct sunos_sysconf_args *uap;
    830 	register_t *retval;
    831 {
    832 	extern int maxfiles;
    833 
    834 	switch(SCARG(uap, name)) {
    835 	case SUNOS_SC_ARG_MAX:
    836 		*retval = ARG_MAX;
    837 		break;
    838 	case SUNOS_SC_CHILD_MAX:
    839 		*retval = maxproc;
    840 		break;
    841 	case SUNOS_SC_CLK_TCK:
    842 		*retval = 60;		/* should this be `hz', ie. 100? */
    843 		break;
    844 	case SUNOS_SC_NGROUPS_MAX:
    845 		*retval = NGROUPS_MAX;
    846 		break;
    847 	case SUNOS_SC_OPEN_MAX:
    848 		*retval = maxfiles;
    849 		break;
    850 	case SUNOS_SC_JOB_CONTROL:
    851 		*retval = 1;
    852 		break;
    853 	case SUNOS_SC_SAVED_IDS:
    854 #ifdef _POSIX_SAVED_IDS
    855 		*retval = 1;
    856 #else
    857 		*retval = 0;
    858 #endif
    859 		break;
    860 	case SUNOS_SC_VERSION:
    861 		*retval = 198808;
    862 		break;
    863 	default:
    864 		return EINVAL;
    865 	}
    866 	return 0;
    867 }
    868 
    869 #define SUNOS_RLIMIT_NOFILE	6	/* Other RLIMIT_* are the same */
    870 #define SUNOS_RLIM_NLIMITS	7
    871 
    872 int
    873 sunos_getrlimit(p, uap, retval)
    874 	struct proc *p;
    875 	struct sunos_getrlimit_args *uap;
    876 	register_t *retval;
    877 {
    878 	if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
    879 		return EINVAL;
    880 
    881 	if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
    882 		SCARG(uap, which) = RLIMIT_NOFILE;
    883 
    884 	return compat_43_getrlimit(p, uap, retval);
    885 }
    886 
    887 int
    888 sunos_setrlimit(p, uap, retval)
    889 	struct proc *p;
    890 	struct sunos_getrlimit_args *uap;
    891 	register_t *retval;
    892 {
    893 	if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
    894 		return EINVAL;
    895 
    896 	if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
    897 		SCARG(uap, which) = RLIMIT_NOFILE;
    898 
    899 	return compat_43_setrlimit(p, uap, retval);
    900 }
    901 
    902 /* for the m68k machines */
    903 #ifndef PT_GETFPREGS
    904 #define PT_GETFPREGS -1
    905 #endif
    906 #ifndef PT_SETFPREGS
    907 #define PT_SETFPREGS -1
    908 #endif
    909 
    910 static int sreq2breq[] = {
    911 	PT_TRACE_ME,    PT_READ_I,      PT_READ_D,      -1,
    912 	PT_WRITE_I,     PT_WRITE_D,     -1,             PT_CONTINUE,
    913 	PT_KILL,        -1,             PT_ATTACH,      PT_DETACH,
    914 	PT_GETREGS,     PT_SETREGS,     PT_GETFPREGS,   PT_SETFPREGS
    915 };
    916 static int nreqs = sizeof(sreq2breq) / sizeof(sreq2breq[0]);
    917 
    918 sunos_ptrace(p, uap, retval)
    919 	struct proc *p;
    920 	struct sunos_ptrace_args *uap;
    921 	int *retval;
    922 {
    923 	struct ptrace_args pa;
    924 	int req;
    925 
    926 	req = SCARG(uap, req);
    927 
    928 	if (req < 0 || req >= nreqs)
    929 		return (EINVAL);
    930 
    931 	req = sreq2breq[req];
    932 	if (req == -1)
    933 		return (EINVAL);
    934 
    935 	SCARG(&pa, req) = req;
    936 	SCARG(&pa, pid) = (pid_t)SCARG(uap, pid);
    937 	SCARG(&pa, addr) = (caddr_t)SCARG(uap, addr);
    938 	SCARG(&pa, data) = SCARG(uap, data);
    939 
    940 	return ptrace(p, &pa, retval);
    941 }
    942 
    943 static void
    944 sunos_pollscan(p, pl, nfd, retval)
    945 	struct proc *p;
    946 	struct sunos_pollfd *pl;
    947 	int nfd;
    948 	register_t *retval;
    949 {
    950 	register struct filedesc *fdp = p->p_fd;
    951 	register int msk, i;
    952 	struct file *fp;
    953 	int n = 0;
    954 	static int flag[3] = { FREAD, FWRITE, 0 };
    955 	static int pflag[3] = { SUNOS_POLLIN|SUNOS_POLLRDNORM,
    956 				SUNOS_POLLOUT, SUNOS_POLLERR };
    957 
    958 	/*
    959 	 * XXX: We need to implement the rest of the flags.
    960 	 */
    961 	for (i = 0; i < nfd; i++) {
    962 		fp = fdp->fd_ofiles[pl[i].fd];
    963 		if (fp == NULL) {
    964 			if (pl[i].events & SUNOS_POLLNVAL) {
    965 				pl[i].revents |= SUNOS_POLLNVAL;
    966 				n++;
    967 			}
    968 			continue;
    969 		}
    970 		for (msk = 0; msk < 3; msk++) {
    971 			if (pl[i].events & pflag[msk]) {
    972 				if ((*fp->f_ops->fo_select)(fp, flag[msk], p)) {
    973 					pl[i].revents |=
    974 					    pflag[msk] & pl[i].events;
    975 					n++;
    976 				}
    977 			}
    978 		}
    979 	}
    980 	*retval = n;
    981 }
    982 
    983 
    984 /*
    985  * We are using the same mechanism as select only we encode/decode args
    986  * differently.
    987  */
    988 int
    989 sunos_poll(p, uap, retval)
    990 	struct proc *p;
    991 	struct sunos_poll_args	*uap;
    992 	register_t *retval;
    993 {
    994 	int i, s;
    995 	int error, error2;
    996 	size_t sz = sizeof(struct sunos_pollfd) * SCARG(uap, nfds);
    997 	struct sunos_pollfd *pl;
    998 	int msec = SCARG(uap, timeout);
    999 	struct timeval atv;
   1000 	int timo;
   1001 	u_int ni;
   1002 	int ncoll;
   1003 	extern int nselcoll, selwait;
   1004 
   1005 	pl = (struct sunos_pollfd *) malloc(sz, M_TEMP, M_WAITOK);
   1006 
   1007 	if (error = copyin(SCARG(uap, fds), pl, sz))
   1008 		goto bad;
   1009 
   1010 	for (i = 0; i < SCARG(uap, nfds); i++)
   1011 		pl[i].revents = 0;
   1012 
   1013 	if (msec != -1) {
   1014 		atv.tv_sec = msec / 1000;
   1015 		atv.tv_usec = (msec - (atv.tv_sec * 1000)) * 1000;
   1016 
   1017 		if (itimerfix(&atv)) {
   1018 			error = EINVAL;
   1019 			goto done;
   1020 		}
   1021 		s = splclock();
   1022 		timeradd(&atv, &time, &atv);
   1023 		timo = hzto(&atv);
   1024 		/*
   1025 		 * Avoid inadvertently sleeping forever.
   1026 		 */
   1027 		if (timo == 0)
   1028 			timo = 1;
   1029 		splx(s);
   1030 	} else
   1031 		timo = 0;
   1032 
   1033 retry:
   1034 	ncoll = nselcoll;
   1035 	p->p_flag |= P_SELECT;
   1036 	sunos_pollscan(p, pl, SCARG(uap, nfds), retval);
   1037 	if (*retval)
   1038 		goto done;
   1039 	s = splhigh();
   1040 	if (timo && timercmp(&time, &atv, >=)) {
   1041 		splx(s);
   1042 		goto done;
   1043 	}
   1044 	if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
   1045 		splx(s);
   1046 		goto retry;
   1047 	}
   1048 	p->p_flag &= ~P_SELECT;
   1049 	error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "sunos_poll", timo);
   1050 	splx(s);
   1051 	if (error == 0)
   1052 		goto retry;
   1053 
   1054 done:
   1055 	p->p_flag &= ~P_SELECT;
   1056 	/* poll is not restarted after signals... */
   1057 	if (error == ERESTART)
   1058 		error = EINTR;
   1059 	if (error == EWOULDBLOCK)
   1060 		error = 0;
   1061 
   1062 	if (error2 = copyout(pl, SCARG(uap, fds), sz))
   1063 		error = error2;
   1064 
   1065 bad:
   1066 	free((char *) pl, M_TEMP);
   1067 
   1068 	return (error);
   1069 }
   1070 
   1071 /*
   1072  * SunOS reboot system call (for compatibility).
   1073  * Sun lets you pass in a boot string which the PROM
   1074  * saves and provides to the next boot program.
   1075  */
   1076 static struct sunos_howto_conv {
   1077 	int sun_howto;
   1078 	int bsd_howto;
   1079 } sunos_howto_conv[] = {
   1080 	{ 0x001,	RB_ASKNAME },
   1081 	{ 0x002,	RB_SINGLE },
   1082 	{ 0x004,	RB_NOSYNC },
   1083 	{ 0x008,	RB_HALT },
   1084 	{ 0x080,	RB_DUMP },
   1085 	{ 0x000,	0 },
   1086 };
   1087 #define	SUNOS_RB_STRING	0x200
   1088 
   1089 int
   1090 sunos_reboot(p, uap, retval)
   1091 	struct proc *p;
   1092 	struct sunos_reboot_args *uap;
   1093 	register_t *retval;
   1094 {
   1095 	struct sunos_howto_conv *convp;
   1096 	int error, bsd_howto, sun_howto;
   1097 
   1098 	if (error = suser(p->p_ucred, &p->p_acflag))
   1099 		return (error);
   1100 
   1101 	/*
   1102 	 * Convert howto bits to BSD format.
   1103 	 */
   1104 	sun_howto = SCARG(uap, howto);
   1105 	bsd_howto = 0;
   1106 	convp = sunos_howto_conv;
   1107 	while (convp->sun_howto) {
   1108 		if (sun_howto &  convp->sun_howto)
   1109 			bsd_howto |= convp->bsd_howto;
   1110 		convp++;
   1111 	}
   1112 
   1113 #ifdef sun3
   1114 	/*
   1115 	 * Sun RB_STRING (Get user supplied bootstring.)
   1116 	 * If the machine supports passing a string to the
   1117 	 * next booted kernel, add the machine name above
   1118 	 * and provide a reboot2() function (see sun3).
   1119 	 */
   1120 	if (sun_howto & SUNOS_RB_STRING) {
   1121 		char bs[128];
   1122 
   1123 		error = copyinstr(SCARG(uap, bootstr), bs, sizeof(bs), 0);
   1124 		if (error)
   1125 			return error;
   1126 
   1127 		return (reboot2(bsd_howto, bs));
   1128 	}
   1129 #endif	/* sun3 */
   1130 
   1131 	return (boot(bsd_howto));
   1132 }
   1133 
   1134 /*
   1135  * Generalized interface signal handler, 4.3-compatible.
   1136  */
   1137 /* ARGSUSED */
   1138 int
   1139 sunos_sigvec(p, uap, retval)
   1140 	struct proc *p;
   1141 	register struct sunos_sigvec_args /* {
   1142 		syscallarg(int) signum;
   1143 		syscallarg(struct sigvec *) nsv;
   1144 		syscallarg(struct sigvec *) osv;
   1145 	} */ *uap;
   1146 	register_t *retval;
   1147 {
   1148 	struct sigvec vec;
   1149 	register struct sigacts *ps = p->p_sigacts;
   1150 	register struct sigvec *sv;
   1151 	register int signum;
   1152 	int bit, error;
   1153 
   1154 	signum = SCARG(uap, signum);
   1155 	if (signum <= 0 || signum >= NSIG ||
   1156 	    signum == SIGKILL || signum == SIGSTOP)
   1157 		return (EINVAL);
   1158 	sv = &vec;
   1159 	if (SCARG(uap, osv)) {
   1160 		*(sig_t *)&sv->sv_handler = ps->ps_sigact[signum];
   1161 		sv->sv_mask = ps->ps_catchmask[signum];
   1162 		bit = sigmask(signum);
   1163 		sv->sv_flags = 0;
   1164 		if ((ps->ps_sigonstack & bit) != 0)
   1165 			sv->sv_flags |= SV_ONSTACK;
   1166 		if ((ps->ps_sigintr & bit) != 0)
   1167 			sv->sv_flags |= SV_INTERRUPT;
   1168 		if (error = copyout((caddr_t)sv, (caddr_t)SCARG(uap, osv),
   1169 		    sizeof (vec)))
   1170 			return (error);
   1171 	}
   1172 	if (SCARG(uap, nsv)) {
   1173 		if (error = copyin((caddr_t)SCARG(uap, nsv), (caddr_t)sv,
   1174 		    sizeof (vec)))
   1175 			return (error);
   1176 		/*
   1177 		 * SunOS uses the mask 0x0004 as SV_RESETHAND
   1178 		 * meaning: `reset to SIG_DFL on delivery'.
   1179 		 * We support only the bits in: 0xF
   1180 		 * (those bits are the same as ours)
   1181 		 */
   1182 		if (sv->sv_flags & ~0xF)
   1183 			return (EINVAL);
   1184 		/* SunOS binaries have a user-mode trampoline. */
   1185 		sv->sv_flags |= SA_USERTRAMP;
   1186 		/* Convert sigvec:SV_INTERRUPT to sigaction:SA_RESTART */
   1187 		sv->sv_flags ^= SA_RESTART;	/* same bit, inverted */
   1188 		setsigvec(p, signum, (struct sigaction *)sv);
   1189 	}
   1190 	return (0);
   1191 }
   1192