Home | History | Annotate | Line # | Download | only in ultrix
ultrix_misc.c revision 1.67
      1 /*	$NetBSD: ultrix_misc.c,v 1.67 2001/01/07 04:13:17 simonb Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995, 1997 Jonathan Stone (hereinafter referred to as the author)
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed by Jonathan Stone for
     18  *      the NetBSD Project.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * Copyright (c) 1992, 1993
     37  *	The Regents of the University of California.  All rights reserved.
     38  *
     39  * This software was developed by the Computer Systems Engineering group
     40  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     41  * contributed to Berkeley.
     42  *
     43  * All advertising materials mentioning features or use of this software
     44  * must display the following acknowledgement:
     45  *	This product includes software developed by the University of
     46  *	California, Lawrence Berkeley Laboratory.
     47  *
     48  * Redistribution and use in source and binary forms, with or without
     49  * modification, are permitted provided that the following conditions
     50  * are met:
     51  * 1. Redistributions of source code must retain the above copyright
     52  *    notice, this list of conditions and the following disclaimer.
     53  * 2. Redistributions in binary form must reproduce the above copyright
     54  *    notice, this list of conditions and the following disclaimer in the
     55  *    documentation and/or other materials provided with the distribution.
     56  * 3. All advertising materials mentioning features or use of this software
     57  *    must display the following acknowledgement:
     58  *	This product includes software developed by the University of
     59  *	California, Berkeley and its contributors.
     60  * 4. Neither the name of the University nor the names of its contributors
     61  *    may be used to endorse or promote products derived from this software
     62  *    without specific prior written permission.
     63  *
     64  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     65  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     66  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     67  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     68  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     69  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     70  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     71  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     72  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     73  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     74  * SUCH DAMAGE.
     75  *
     76  *
     77  *	@(#)sun_misc.c	8.1 (Berkeley) 6/18/93
     78  *
     79  * from: Header: sun_misc.c,v 1.16 93/04/07 02:46:27 torek Exp
     80  */
     81 
     82 #if defined(_KERNEL) && !defined(_LKM)
     83 #include "opt_nfsserver.h"
     84 #include "opt_sysv.h"
     85 #endif
     86 
     87 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     88 
     89 /*
     90  * Ultrix compatibility module.
     91  *
     92  * Ultrix system calls that are implemented differently in BSD are
     93  * handled here.
     94  */
     95 
     96 #if defined(_KERNEL) && !defined(_LKM)
     97 #include "fs_nfs.h"
     98 #endif
     99 
    100 #include <sys/param.h>
    101 #include <sys/systm.h>
    102 #include <sys/namei.h>
    103 #include <sys/dirent.h>
    104 #include <sys/proc.h>
    105 #include <sys/file.h>
    106 #include <sys/filedesc.h>
    107 #include <sys/kernel.h>
    108 #include <sys/exec.h>
    109 #include <sys/malloc.h>
    110 #include <sys/mbuf.h>
    111 #include <sys/mman.h>
    112 #include <sys/mount.h>
    113 #include <sys/resource.h>
    114 #include <sys/resourcevar.h>
    115 #include <sys/signal.h>
    116 #include <sys/signalvar.h>
    117 #include <sys/socket.h>
    118 #include <sys/vnode.h>
    119 #include <sys/uio.h>
    120 #include <sys/wait.h>
    121 #include <sys/utsname.h>
    122 #include <sys/unistd.h>
    123 #include <sys/ipc.h>
    124 
    125 #include <sys/syscallargs.h>
    126 
    127 #include <compat/ultrix/ultrix_syscall.h>
    128 #include <compat/ultrix/ultrix_syscallargs.h>
    129 #include <compat/common/compat_util.h>
    130 
    131 #include <netinet/in.h>
    132 
    133 #include <miscfs/specfs/specdev.h>
    134 
    135 #include <nfs/rpcv2.h>
    136 #include <nfs/nfsproto.h>
    137 #include <nfs/nfs.h>
    138 
    139 #include <sys/conf.h>					/* iszerodev() */
    140 #include <sys/socketvar.h>				/* sosetopt() */
    141 
    142 #include <compat/ultrix/ultrix_flock.h>
    143 
    144 #ifdef __mips
    145 #include <mips/cachectl.h>
    146 #endif
    147 
    148 static int ultrix_to_bsd_flock __P((struct ultrix_flock *, struct flock *));
    149 static void bsd_to_ultrix_flock __P((struct flock *, struct ultrix_flock *));
    150 
    151 extern struct sysent ultrix_sysent[];
    152 extern const char * const ultrix_syscallnames[];
    153 
    154 #if 0
    155 /* XXX what executable format does Ultrix/vax use ? */
    156 /*
    157  * Select the appropriate setregs callback for the target architecture.
    158  */
    159 #ifdef __mips__
    160 #include <machine/ecoff_machdep.h>
    161 #define ULTRIX_EXEC_SETREGS cpu_exec_ecoff_setregs
    162 #endif /* mips */
    163 
    164 #ifdef __vax__
    165 #define ULTRIX_EXEC_SETREGS setregs
    166 #endif /* vax */
    167 #endif
    168 
    169 
    170 extern char ultrix_sigcode[], ultrix_esigcode[];
    171 void syscall __P((void));
    172 
    173 const struct emul emul_ultrix = {
    174 	"ultrix",
    175 	"/emul/ultrix",
    176 	0,
    177 	NULL,
    178 	ULTRIX_SYS_syscall,
    179 	ULTRIX_SYS_MAXSYSCALL,
    180 	ultrix_sysent,
    181 	ultrix_syscallnames,
    182 	sendsig,
    183 	ultrix_sigcode,
    184 	ultrix_esigcode,
    185 	NULL,
    186 	NULL,
    187 	NULL,
    188 	syscall
    189 };
    190 
    191 #define GSI_PROG_ENV 1
    192 
    193 int
    194 ultrix_sys_getsysinfo(p, v, retval)
    195 	struct proc *p;
    196 	void *v;
    197 	register_t *retval;
    198 {
    199 	struct ultrix_sys_getsysinfo_args *uap = v;
    200 	static short progenv = 0;
    201 
    202 	switch (SCARG(uap, op)) {
    203 		/* operations implemented: */
    204 	case GSI_PROG_ENV:
    205 		if (SCARG(uap, nbytes) < sizeof(short))
    206 			return EINVAL;
    207 		*retval = 1;
    208 		return (copyout(&progenv, SCARG(uap, buffer), sizeof(short)));
    209 	default:
    210 		*retval = 0; /* info unavail */
    211 		return 0;
    212 	}
    213 }
    214 
    215 int
    216 ultrix_sys_setsysinfo(p, v, retval)
    217 	struct proc *p;
    218 	void *v;
    219 	register_t *retval;
    220 {
    221 
    222 #ifdef notyet
    223 	struct ultrix_sys_setsysinfo_args *uap = v;
    224 #endif
    225 
    226 	*retval = 0;
    227 	return 0;
    228 }
    229 
    230 int
    231 ultrix_sys_waitpid(p, v, retval)
    232 	struct proc *p;
    233 	void *v;
    234 	register_t *retval;
    235 {
    236 	struct ultrix_sys_waitpid_args *uap = v;
    237 	struct sys_wait4_args ua;
    238 
    239 	SCARG(&ua, pid) = SCARG(uap, pid);
    240 	SCARG(&ua, status) = SCARG(uap, status);
    241 	SCARG(&ua, options) = SCARG(uap, options);
    242 	SCARG(&ua, rusage) = 0;
    243 
    244 	return (sys_wait4(p, &ua, retval));
    245 }
    246 
    247 int
    248 ultrix_sys_wait3(p, v, retval)
    249 	struct proc *p;
    250 	void *v;
    251 	register_t *retval;
    252 {
    253 	struct ultrix_sys_wait3_args *uap = v;
    254 	struct sys_wait4_args ua;
    255 
    256 	SCARG(&ua, pid) = -1;
    257 	SCARG(&ua, status) = SCARG(uap, status);
    258 	SCARG(&ua, options) = SCARG(uap, options);
    259 	SCARG(&ua, rusage) = SCARG(uap, rusage);
    260 
    261 	return (sys_wait4(p, &ua, retval));
    262 }
    263 
    264 /*
    265  * Ultrix binaries pass in FD_MAX as the first arg to select().
    266  * On Ultrix, FD_MAX is 4096, which is more than the NetBSD sys_select()
    267  * can handle.
    268  * Since we can't have more than the (native) FD_MAX descriptors open,
    269  * limit nfds to at most FD_MAX.
    270  */
    271 int
    272 ultrix_sys_select(p, v, retval)
    273 	struct proc *p;
    274 	void *v;
    275 	register_t *retval;
    276 {
    277 	struct sys_select_args *uap = v;
    278 	struct timeval atv;
    279 	int error;
    280 
    281 	/* Limit number of FDs selected on to the native maximum */
    282 
    283 	if (SCARG(uap, nd) > FD_SETSIZE)
    284 		SCARG(uap, nd) = FD_SETSIZE;
    285 
    286 	/* Check for negative timeval */
    287 	if (SCARG(uap, tv)) {
    288 		error = copyin((caddr_t)SCARG(uap, tv), (caddr_t)&atv,
    289 			       sizeof(atv));
    290 		if (error)
    291 			goto done;
    292 #ifdef DEBUG
    293 		/* Ultrix clients sometimes give negative timeouts? */
    294 		if (atv.tv_sec < 0 || atv.tv_usec < 0)
    295 			printf("ultrix select( %ld, %ld): negative timeout\n",
    296 			    atv.tv_sec, atv.tv_usec);
    297 		/*tvp = (timeval *)STACKGAPBASE;*/
    298 #endif
    299 
    300 	}
    301 	error = sys_select(p, (void*) uap, retval);
    302 	if (error == EINVAL)
    303 		printf("ultrix select: bad args?\n");
    304 
    305 done:
    306 	return error;
    307 }
    308 
    309 #if defined(NFS)
    310 int
    311 async_daemon(p, v, retval)
    312 	struct proc *p;
    313 	void *v;
    314 	register_t *retval;
    315 {
    316 	struct sys_nfssvc_args ouap;
    317 
    318 	SCARG(&ouap, flag) = NFSSVC_BIOD;
    319 	SCARG(&ouap, argp) = NULL;
    320 
    321 	return (sys_nfssvc(p, &ouap, retval));
    322 }
    323 #endif /* NFS */
    324 
    325 
    326 #define	SUN__MAP_NEW	0x80000000	/* if not, old mmap & cannot handle */
    327 
    328 int
    329 ultrix_sys_mmap(p, v, retval)
    330 	struct proc *p;
    331 	void *v;
    332 	register_t *retval;
    333 {
    334 	struct ultrix_sys_mmap_args *uap = v;
    335 	struct sys_mmap_args ouap;
    336 	struct filedesc *fdp;
    337 	struct file *fp;
    338 	struct vnode *vp;
    339 
    340 	/*
    341 	 * Verify the arguments.
    342 	 */
    343 	if (SCARG(uap, prot) & ~(PROT_READ|PROT_WRITE|PROT_EXEC))
    344 		return (EINVAL);			/* XXX still needed? */
    345 
    346 	if ((SCARG(uap, flags) & SUN__MAP_NEW) == 0)
    347 		return (EINVAL);
    348 
    349 	SCARG(&ouap, flags) = SCARG(uap, flags) & ~SUN__MAP_NEW;
    350 	SCARG(&ouap, addr) = SCARG(uap, addr);
    351 
    352 	if ((SCARG(&ouap, flags) & MAP_FIXED) == 0 &&
    353 	    SCARG(&ouap, addr) != 0 &&
    354 	    SCARG(&ouap, addr) < (void *)round_page((vaddr_t)p->p_vmspace->vm_daddr+MAXDSIZ))
    355 		SCARG(&ouap, addr) = (void *)round_page((vaddr_t)p->p_vmspace->vm_daddr+MAXDSIZ);
    356 
    357 	SCARG(&ouap, len) = SCARG(uap, len);
    358 	SCARG(&ouap, prot) = SCARG(uap, prot);
    359 	SCARG(&ouap, fd) = SCARG(uap, fd);
    360 	SCARG(&ouap, pos) = SCARG(uap, pos);
    361 
    362 	/*
    363 	 * Special case: if fd refers to /dev/zero, map as MAP_ANON.  (XXX)
    364 	 */
    365 	fdp = p->p_fd;
    366 	if ((unsigned)SCARG(&ouap, fd) < fdp->fd_nfiles &&		/*XXX*/
    367 	    (fp = fdp->fd_ofiles[SCARG(&ouap, fd)]) != NULL &&		/*XXX*/
    368 	    fp->f_type == DTYPE_VNODE &&				/*XXX*/
    369 	    (vp = (struct vnode *)fp->f_data)->v_type == VCHR &&	/*XXX*/
    370 	    iszerodev(vp->v_rdev)) {					/*XXX*/
    371 		SCARG(&ouap, flags) |= MAP_ANON;
    372 		SCARG(&ouap, fd) = -1;
    373 	}
    374 
    375 	return (sys_mmap(p, &ouap, retval));
    376 }
    377 
    378 int
    379 ultrix_sys_setsockopt(p, v, retval)
    380 	struct proc *p;
    381 	void *v;
    382 	register_t *retval;
    383 {
    384 	struct ultrix_sys_setsockopt_args *uap = v;
    385 	struct file *fp;
    386 	struct mbuf *m = NULL;
    387 	int error;
    388 
    389 	/* getsock() will use the descriptor for us */
    390 	if ((error = getsock(p->p_fd, SCARG(uap, s), &fp))  != 0)
    391 		return (error);
    392 #define	SO_DONTLINGER (~SO_LINGER)
    393 	if (SCARG(uap, name) == SO_DONTLINGER) {
    394 		m = m_get(M_WAIT, MT_SOOPTS);
    395 		mtod(m, struct linger *)->l_onoff = 0;
    396 		m->m_len = sizeof(struct linger);
    397 		error = sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
    398 		    SO_LINGER, m);
    399 	}
    400 	if (SCARG(uap, level) == IPPROTO_IP) {
    401 #define		EMUL_IP_MULTICAST_IF		2
    402 #define		EMUL_IP_MULTICAST_TTL		3
    403 #define		EMUL_IP_MULTICAST_LOOP		4
    404 #define		EMUL_IP_ADD_MEMBERSHIP		5
    405 #define		EMUL_IP_DROP_MEMBERSHIP	6
    406 		static int ipoptxlat[] = {
    407 			IP_MULTICAST_IF,
    408 			IP_MULTICAST_TTL,
    409 			IP_MULTICAST_LOOP,
    410 			IP_ADD_MEMBERSHIP,
    411 			IP_DROP_MEMBERSHIP
    412 		};
    413 		if (SCARG(uap, name) >= EMUL_IP_MULTICAST_IF &&
    414 		    SCARG(uap, name) <= EMUL_IP_DROP_MEMBERSHIP) {
    415 			SCARG(uap, name) =
    416 			    ipoptxlat[SCARG(uap, name) - EMUL_IP_MULTICAST_IF];
    417 		}
    418 	}
    419 	if (SCARG(uap, valsize) > MLEN) {
    420 		error = EINVAL;
    421 		goto out;
    422 	}
    423 	if (SCARG(uap, val)) {
    424 		m = m_get(M_WAIT, MT_SOOPTS);
    425 		error = copyin(SCARG(uap, val), mtod(m, caddr_t),
    426 		    (u_int)SCARG(uap, valsize));
    427 		if (error) {
    428 			(void) m_free(m);
    429 			goto out;
    430 		}
    431 		m->m_len = SCARG(uap, valsize);
    432 	}
    433 	error = sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
    434 	    SCARG(uap, name), m);
    435  out:
    436 	FILE_UNUSE(fp, p);
    437 	return (error);
    438 }
    439 
    440 #define	ULTRIX__SYS_NMLN	32
    441 
    442 struct ultrix_utsname {
    443 	char    sysname[ULTRIX__SYS_NMLN];
    444 	char    nodename[ULTRIX__SYS_NMLN];
    445 	char    release[ULTRIX__SYS_NMLN];
    446 	char    version[ULTRIX__SYS_NMLN];
    447 	char    machine[ULTRIX__SYS_NMLN];
    448 };
    449 
    450 int
    451 ultrix_sys_uname(p, v, retval)
    452 	struct proc *p;
    453 	void *v;
    454 	register_t *retval;
    455 {
    456 	struct ultrix_sys_uname_args *uap = v;
    457 	struct ultrix_utsname sut;
    458 	const char *cp;
    459 	char *dp, *ep;
    460 
    461 	memset(&sut, 0, sizeof(sut));
    462 
    463 	strncpy(sut.sysname, ostype, sizeof(sut.sysname) - 1);
    464 	strncpy(sut.nodename, hostname, sizeof(sut.nodename) - 1);
    465 	strncpy(sut.release, osrelease, sizeof(sut.release) - 1);
    466 	dp = sut.version;
    467 	ep = &sut.version[sizeof(sut.version) - 1];
    468 	for (cp = version; *cp && *cp != '('; cp++)
    469 		;
    470 	for (cp++; *cp && *cp != ')' && dp < ep; cp++)
    471 		*dp++ = *cp;
    472 	for (; *cp && *cp != '#'; cp++)
    473 		;
    474 	for (; *cp && *cp != ':' && dp < ep; cp++)
    475 		*dp++ = *cp;
    476 	*dp = '\0';
    477 	strncpy(sut.machine, machine, sizeof(sut.machine) - 1);
    478 
    479 	return copyout((caddr_t)&sut, (caddr_t)SCARG(uap, name),
    480 	    sizeof(struct ultrix_utsname));
    481 }
    482 
    483 int
    484 ultrix_sys_setpgrp(p, v, retval)
    485 	struct proc *p;
    486 	void *v;
    487 	register_t *retval;
    488 {
    489 	struct ultrix_sys_setpgrp_args *uap = v;
    490 
    491 	/*
    492 	 * difference to our setpgid call is to include backwards
    493 	 * compatibility to pre-setsid() binaries. Do setsid()
    494 	 * instead of setpgid() in those cases where the process
    495 	 * tries to create a new session the old way.
    496 	 */
    497 	if (!SCARG(uap, pgid) &&
    498 	    (!SCARG(uap, pid) || SCARG(uap, pid) == p->p_pid))
    499 		return sys_setsid(p, uap, retval);
    500 	else
    501 		return sys_setpgid(p, uap, retval);
    502 }
    503 
    504 #if defined (NFSSERVER)
    505 int
    506 ultrix_sys_nfssvc(p, v, retval)
    507 	struct proc *p;
    508 	void *v;
    509 	register_t *retval;
    510 {
    511 
    512 #if 0	/* XXX */
    513 	struct ultrix_sys_nfssvc_args *uap = v;
    514 	struct emul *e = p->p_emul;
    515 	struct sys_nfssvc_args outuap;
    516 	struct sockaddr sa;
    517 	int error;
    518 
    519 	memset(&outuap, 0, sizeof outuap);
    520 	SCARG(&outuap, fd) = SCARG(uap, fd);
    521 	SCARG(&outuap, mskval) = STACKGAPBASE;
    522 	SCARG(&outuap, msklen) = sizeof sa;
    523 	SCARG(&outuap, mtchval) = outuap.mskval + sizeof sa;
    524 	SCARG(&outuap, mtchlen) = sizeof sa;
    525 
    526 	memset(&sa, 0, sizeof sa);
    527 	if (error = copyout(&sa, SCARG(&outuap, mskval), SCARG(&outuap, msklen)))
    528 		return (error);
    529 	if (error = copyout(&sa, SCARG(&outuap, mtchval), SCARG(&outuap, mtchlen)))
    530 		return (error);
    531 
    532 	return nfssvc(p, &outuap, retval);
    533 #else
    534 	return (ENOSYS);
    535 #endif
    536 }
    537 #endif /* NFSSERVER */
    538 
    539 struct ultrix_ustat {
    540 	daddr_t	f_tfree;	/* total free */
    541 	ino_t	f_tinode;	/* total inodes free */
    542 	char	f_fname[6];	/* filsys name */
    543 	char	f_fpack[6];	/* filsys pack name */
    544 };
    545 
    546 int
    547 ultrix_sys_ustat(p, v, retval)
    548 	struct proc *p;
    549 	void *v;
    550 	register_t *retval;
    551 {
    552 	struct ultrix_sys_ustat_args *uap = v;
    553 	struct ultrix_ustat us;
    554 	int error;
    555 
    556 	memset(&us, 0, sizeof us);
    557 
    558 	/*
    559 	 * XXX: should set f_tfree and f_tinode at least
    560 	 * How do we translate dev -> fstat? (and then to ultrix_ustat)
    561 	 */
    562 
    563 	if ((error = copyout(&us, SCARG(uap, buf), sizeof us)) != 0)
    564 		return (error);
    565 	return 0;
    566 }
    567 
    568 int
    569 ultrix_sys_quotactl(p, v, retval)
    570 	struct proc *p;
    571 	void *v;
    572 	register_t *retval;
    573 {
    574 
    575 #ifdef notyet
    576 	struct ultrix_sys_quotactl_args *uap = v;
    577 #endif
    578 
    579 	return EINVAL;
    580 }
    581 
    582 int
    583 ultrix_sys_vhangup(p, v, retval)
    584 	struct proc *p;
    585 	void *v;
    586 	register_t *retval;
    587 {
    588 
    589 	return 0;
    590 }
    591 
    592 
    593 /*
    594  * RISC Ultrix cache control syscalls
    595  */
    596 #ifdef __mips
    597 int
    598 ultrix_sys_cacheflush(p, v, retval)
    599 	struct proc *p;
    600 	void *v;
    601 	register_t *retval;
    602 {
    603 	struct ultrix_sys_cacheflush_args /* {
    604 		syscallarg(void *) addr;
    605 		syscallarg(int) nbytes;
    606 		syscallarg(int) flag;
    607 	} */ *uap = v;
    608 	vaddr_t va  = (vaddr_t)SCARG(uap, addr);
    609 	int nbytes     = SCARG(uap, nbytes);
    610 	int whichcache = SCARG(uap, whichcache);
    611 
    612 	return (mips_user_cacheflush(p, va, nbytes, whichcache));
    613 }
    614 
    615 
    616 int
    617 ultrix_sys_cachectl(p, v, retval)
    618 	struct proc *p;
    619 	void *v;
    620 	register_t *retval;
    621 {
    622 	struct ultrix_sys_cachectl_args /* {
    623 		syscallarg(void *) addr;
    624 		syscallarg(int) nbytes;
    625 		syscallarg(int) cacheop;
    626 	} */ *uap = v;
    627 	vaddr_t va  = (vaddr_t)SCARG(uap, addr);
    628 	int nbytes  = SCARG(uap, nbytes);
    629 	int cacheop = SCARG(uap, cacheop);
    630 
    631 	return mips_user_cachectl(p, va, nbytes, cacheop);
    632 }
    633 
    634 #endif	/* __mips */
    635 
    636 
    637 int
    638 ultrix_sys_exportfs(p, v, retval)
    639 	struct proc *p;
    640 	void *v;
    641 	register_t *retval;
    642 {
    643 #ifdef notyet
    644 	struct ultrix_sys_exportfs_args *uap = v;
    645 #endif
    646 
    647 	/*
    648 	 * XXX: should perhaps translate into a mount(2)
    649 	 * with MOUNT_EXPORT?
    650 	 */
    651 	return 0;
    652 }
    653 
    654 int
    655 ultrix_sys_sigpending(p, v, retval)
    656 	struct proc *p;
    657 	void *v;
    658 	register_t *retval;
    659 {
    660 	struct ultrix_sys_sigpending_args *uap = v;
    661 	sigset_t ss;
    662 	int mask;
    663 
    664 	sigpending1(p, &ss);
    665 	mask = ss.__bits[0];
    666 
    667 	return (copyout((caddr_t)&mask, (caddr_t)SCARG(uap, mask), sizeof(int)));
    668 }
    669 
    670 int
    671 ultrix_sys_sigreturn(p, v, retval)
    672 	struct proc *p;
    673 	void *v;
    674 	register_t *retval;
    675 {
    676 	struct ultrix_sys_sigreturn_args *uap = v;
    677 
    678 	/* struct sigcontext13 is close enough to Ultrix */
    679 
    680 	return (compat_13_sys_sigreturn(p, uap, retval));
    681 }
    682 
    683 int
    684 ultrix_sys_sigcleanup(p, v, retval)
    685 	struct proc *p;
    686 	void *v;
    687 	register_t *retval;
    688 {
    689 	struct ultrix_sys_sigcleanup_args *uap = v;
    690 
    691 	/* struct sigcontext13 is close enough to Ultrix */
    692 
    693 	return (compat_13_sys_sigreturn(p, uap, retval));
    694 }
    695 
    696 int
    697 ultrix_sys_sigsuspend(p, v, retval)
    698 	struct proc *p;
    699 	void *v;
    700 	register_t *retval;
    701 {
    702 	struct ultrix_sys_sigsuspend_args *uap = v;
    703 	int mask = SCARG(uap, mask);
    704 	sigset_t ss;
    705 
    706 	ss.__bits[0] = mask;
    707 	ss.__bits[1] = 0;
    708 	ss.__bits[2] = 0;
    709 	ss.__bits[3] = 0;
    710 
    711 	return (sigsuspend1(p, &ss));
    712 }
    713 
    714 #define ULTRIX_SV_ONSTACK 0x0001  /* take signal on signal stack */
    715 #define ULTRIX_SV_INTERRUPT 0x0002  /* do not restart system on signal return */
    716 #define ULTRIX_SV_OLDSIG 0x1000  /* Emulate old signal() for POSIX */
    717 
    718 int
    719 ultrix_sys_sigvec(p, v, retval)
    720 	struct proc *p;
    721 	void *v;
    722 	register_t *retval;
    723 {
    724 	struct ultrix_sys_sigvec_args *uap = v;
    725 	struct sigvec nsv, osv;
    726 	struct sigaction nsa, osa;
    727 	int error;
    728 
    729 	if (SCARG(uap, nsv)) {
    730 		error = copyin(SCARG(uap, nsv), &nsv, sizeof(nsv));
    731 		if (error)
    732 			return (error);
    733 		nsa.sa_handler = nsv.sv_handler;
    734 #if 0 /* documentation */
    735 		/* ONSTACK is identical */
    736 		nsa.sa_flags = nsv.sv_flags & ULTRIX_SV_ONSTACK;
    737 		if ((nsv.sv_flags & ULTRIX_SV_OLDSIG)
    738 		    /* old signal() - always restart */
    739 		    || (!(nsv.sv_flags & ULTRIX_SV_INTERRUPT))
    740 		    /* inverted meaning (same bit) */
    741 		    )
    742 			nsa.sa_flags |= SA_RESTART;
    743 #else /* optimized - assuming ULTRIX_SV_OLDSIG=>!ULTRIX_SV_INTERRUPT */
    744 		nsa.sa_flags = nsv.sv_flags & ~ULTRIX_SV_OLDSIG;
    745 		nsa.sa_flags ^= SA_RESTART;
    746 #endif
    747 		native_sigset13_to_sigset(&nsv.sv_mask, &nsa.sa_mask);
    748 	}
    749 	error = sigaction1(p, SCARG(uap, signum),
    750 	    SCARG(uap, nsv) ? &nsa : 0, SCARG(uap, osv) ? &osa : 0);
    751 	if (error)
    752 		return (error);
    753 	if (SCARG(uap, osv)) {
    754 		osv.sv_handler = osa.sa_handler;
    755 		osv.sv_flags = osa.sa_flags ^ SA_RESTART;
    756 		osv.sv_flags &= (ULTRIX_SV_ONSTACK | ULTRIX_SV_INTERRUPT);
    757 		native_sigset_to_sigset13(&osa.sa_mask, &osv.sv_mask);
    758 		error = copyout(&osv, SCARG(uap, osv), sizeof(osv));
    759 		if (error)
    760 			return (error);
    761 	}
    762 	return (0);
    763 }
    764 
    765 int
    766 ultrix_sys_shmsys(p, v, retval)
    767 	struct proc *p;
    768 	void *v;
    769 	register_t *retval;
    770 {
    771 
    772 #ifdef SYSVSHM
    773 
    774 	/* Ultrix SVSHM weirndess: */
    775 	struct ultrix_sys_shmsys_args *uap = v;
    776 	struct sys_shmat_args shmat_args;
    777 	struct compat_14_sys_shmctl_args shmctl_args;
    778 	struct sys_shmdt_args shmdt_args;
    779 	struct sys_shmget_args shmget_args;
    780 
    781 
    782 	switch (SCARG(uap, shmop)) {
    783 	case 0:						/* Ultrix shmat() */
    784 		SCARG(&shmat_args, shmid) = SCARG(uap, a2);
    785 		SCARG(&shmat_args, shmaddr) = (void *)SCARG(uap, a3);
    786 		SCARG(&shmat_args, shmflg) = SCARG(uap, a4);
    787 		return (sys_shmat(p, &shmat_args, retval));
    788 
    789 	case 1:						/* Ultrix shmctl() */
    790 		SCARG(&shmctl_args, shmid) = SCARG(uap, a2);
    791 		SCARG(&shmctl_args, cmd) = SCARG(uap, a3);
    792 		SCARG(&shmctl_args, buf) = (struct shmid_ds14 *)SCARG(uap, a4);
    793 		return (compat_14_sys_shmctl(p, &shmctl_args, retval));
    794 
    795 	case 2:						/* Ultrix shmdt() */
    796 		SCARG(&shmat_args, shmaddr) = (void *)SCARG(uap, a2);
    797 		return (sys_shmdt(p, &shmdt_args, retval));
    798 
    799 	case 3:						/* Ultrix shmget() */
    800 		SCARG(&shmget_args, key) = SCARG(uap, a2);
    801 		SCARG(&shmget_args, size) = SCARG(uap, a3);
    802 		SCARG(&shmget_args, shmflg) = SCARG(uap, a4)
    803 		    & (IPC_CREAT|IPC_EXCL|IPC_NOWAIT);
    804 		return (sys_shmget(p, &shmget_args, retval));
    805 
    806 	default:
    807 		return (EINVAL);
    808 	}
    809 #else
    810 	return (EOPNOTSUPP);
    811 #endif	/* SYSVSHM */
    812 }
    813 
    814 static int
    815 ultrix_to_bsd_flock(ufl, fl)
    816 	struct ultrix_flock *ufl;
    817 	struct flock *fl;
    818 {
    819 
    820 	fl->l_start = ufl->l_start;
    821 	fl->l_len = ufl->l_len;
    822 	fl->l_pid = ufl->l_pid;
    823 	fl->l_whence = ufl->l_whence;
    824 
    825 	switch (ufl->l_type) {
    826 	case ULTRIX_F_RDLCK:
    827 		fl->l_type = F_RDLCK;
    828 		break;
    829 	case ULTRIX_F_WRLCK:
    830 		fl->l_type = F_WRLCK;
    831 		break;
    832 	case ULTRIX_F_UNLCK:
    833 		fl->l_type = F_UNLCK;
    834 		break;
    835 	default:
    836 		return (EINVAL);
    837 	}
    838 
    839 	return (0);
    840 }
    841 
    842 static void
    843 bsd_to_ultrix_flock(fl, ufl)
    844 	struct flock *fl;
    845 	struct ultrix_flock *ufl;
    846 {
    847 
    848 	ufl->l_start = fl->l_start;
    849 	ufl->l_len = fl->l_len;
    850 	ufl->l_pid = fl->l_pid;
    851 	ufl->l_whence = fl->l_whence;
    852 
    853 	switch (fl->l_type) {
    854 	case F_RDLCK:
    855 		ufl->l_type = ULTRIX_F_RDLCK;
    856 		break;
    857 	case F_WRLCK:
    858 		ufl->l_type = ULTRIX_F_WRLCK;
    859 		break;
    860 	case F_UNLCK:
    861 		ufl->l_type = ULTRIX_F_UNLCK;
    862 		break;
    863 	}
    864 }
    865 
    866 int
    867 ultrix_sys_fcntl(p, v, retval)
    868 	struct proc *p;
    869 	void *v;
    870 	register_t *retval;
    871 {
    872 	struct ultrix_sys_fcntl_args *uap = v;
    873 	int error;
    874 	struct ultrix_flock ufl;
    875 	struct flock fl, *flp;
    876 	caddr_t sg;
    877 	struct sys_fcntl_args *args, fca;
    878 
    879 	switch (SCARG(uap, cmd)) {
    880 	case F_GETLK:
    881 	case F_SETLK:
    882 	case F_SETLKW:
    883 		error = copyin(SCARG(uap, arg), &ufl, sizeof(ufl));
    884 		if (error)
    885 			return (error);
    886 		error = ultrix_to_bsd_flock(&ufl, &fl);
    887 		if (error)
    888 			return (error);
    889 		sg = stackgap_init(p->p_emul);
    890 		flp = (struct flock *)stackgap_alloc(&sg, sizeof(*flp));
    891 		error = copyout(&fl, flp, sizeof(*flp));
    892 		if (error)
    893 			return (error);
    894 
    895 		SCARG(&fca, fd) = SCARG(uap, fd);
    896 		SCARG(&fca, cmd) = SCARG(uap, cmd);
    897 		SCARG(&fca, arg) = flp;
    898 		args = &fca;
    899 		break;
    900 	default:
    901 		args = v;
    902 		break;
    903 	}
    904 
    905 	error = sys_fcntl(p, args, retval);
    906 	if (error)
    907 		return (error);
    908 
    909 	switch (SCARG(uap, cmd)) {
    910 	case F_GETLK:
    911 		error = copyin(flp, &fl, sizeof(fl));
    912 		if (error)
    913 			return (error);
    914 		bsd_to_ultrix_flock(&fl, &ufl);
    915 		error = copyout(&ufl, SCARG(uap, arg), sizeof(ufl));
    916 		break;
    917 	}
    918 
    919 	return (error);
    920 }
    921