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