Home | History | Annotate | Line # | Download | only in ultrix
ultrix_misc.c revision 1.28
      1 /*	$NetBSD: ultrix_misc.c,v 1.28 1996/10/19 12:40:31 jonathan Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995
      5  *	Jonathan Stone (hereinafter referred to as the author)
      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 Matthew R. Green 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 /*
     83  * SunOS compatibility module.
     84  *
     85  * SunOS system calls that are implemented differently in BSD are
     86  * handled here.
     87  */
     88 
     89 #include <sys/param.h>
     90 #include <sys/systm.h>
     91 #include <sys/namei.h>
     92 #include <sys/dirent.h>
     93 #include <sys/proc.h>
     94 #include <sys/file.h>
     95 #include <sys/filedesc.h>
     96 /*#include <sys/stat.h>*/
     97 /*#include <sys/ioctl.h>*/
     98 #include <sys/kernel.h>
     99 #include <sys/exec.h>
    100 #include <sys/malloc.h>
    101 #include <sys/mbuf.h>
    102 #include <sys/mman.h>
    103 #include <sys/mount.h>
    104 #include <sys/resource.h>
    105 #include <sys/resourcevar.h>
    106 #include <sys/signal.h>
    107 #include <sys/signalvar.h>
    108 #include <sys/socket.h>
    109 #include <sys/vnode.h>
    110 #include <sys/uio.h>
    111 #include <sys/wait.h>
    112 #include <sys/utsname.h>
    113 #include <sys/unistd.h>
    114 
    115 #include <sys/syscallargs.h>
    116 
    117 #include <compat/ultrix/ultrix_syscall.h>
    118 #include <compat/ultrix/ultrix_syscallargs.h>
    119 
    120 #include <netinet/in.h>
    121 
    122 #include <miscfs/specfs/specdev.h>
    123 
    124 #include <nfs/rpcv2.h>
    125 #include <nfs/nfsproto.h>
    126 #include <nfs/nfs.h>
    127 
    128 #include <vm/vm.h>
    129 
    130 #include <sys/conf.h>					/* iszerodev() */
    131 #include <sys/socketvar.h>				/* sosetopt() */
    132 
    133 extern struct sysent ultrix_sysent[];
    134 extern char *ultrix_syscallnames[];
    135 
    136 /*
    137  * Select the appropriate setregs callback for the target architecture.
    138  */
    139 #ifdef mips
    140 #define ULTRIX_EXEC_SETREGS cpu_exec_ecoff_setregs
    141 #endif /* mips */
    142 
    143 #ifdef vax
    144 #define ULTRIX_EXEC_SETREGS setregs
    145 #endif /* mips */
    146 
    147 
    148 extern void ULTRIX_EXEC_SETREGS __P((struct proc *, struct exec_package *,
    149 					u_long, register_t *));
    150 extern char sigcode[], esigcode[];
    151 
    152 struct emul emul_ultrix = {
    153 	"ultrix",
    154 	NULL,
    155 	sendsig,
    156 	ULTRIX_SYS_syscall,
    157 	ULTRIX_SYS_MAXSYSCALL,
    158 	ultrix_sysent,
    159 	ultrix_syscallnames,
    160 	0,
    161 	copyargs,
    162 	ULTRIX_EXEC_SETREGS,
    163 	sigcode,
    164 	esigcode,
    165 };
    166 
    167 #define GSI_PROG_ENV 1
    168 
    169 int
    170 ultrix_sys_getsysinfo(p, v, retval)
    171 	struct proc *p;
    172 	void *v;
    173 	register_t *retval;
    174 {
    175 	struct ultrix_sys_getsysinfo_args *uap = v;
    176 	static short progenv = 0;
    177 
    178 	switch (SCARG(uap, op)) {
    179 		/* operations implemented: */
    180 	case GSI_PROG_ENV:
    181 		if (SCARG(uap, nbytes) < sizeof(short))
    182 			return EINVAL;
    183 		*retval = 1;
    184 		return (copyout(&progenv, SCARG(uap, buffer), sizeof(short)));
    185 	default:
    186 		*retval = 0; /* info unavail */
    187 		return 0;
    188 	}
    189 }
    190 
    191 int
    192 ultrix_sys_setsysinfo(p, v, retval)
    193 	struct proc *p;
    194 	void *v;
    195 	register_t *retval;
    196 {
    197 
    198 #ifdef notyet
    199 	struct ultrix_sys_setsysinfo_args *uap = v;
    200 #endif
    201 
    202 	*retval = 0;
    203 	return 0;
    204 }
    205 
    206 int
    207 ultrix_sys_waitpid(p, v, retval)
    208 	struct proc *p;
    209 	void *v;
    210 	register_t *retval;
    211 {
    212 	struct ultrix_sys_waitpid_args *uap = v;
    213 	struct sys_wait4_args ua;
    214 
    215 	SCARG(&ua, pid) = SCARG(uap, pid);
    216 	SCARG(&ua, status) = SCARG(uap, status);
    217 	SCARG(&ua, options) = SCARG(uap, options);
    218 	SCARG(&ua, rusage) = 0;
    219 
    220 	return (sys_wait4(p, &ua, retval));
    221 }
    222 
    223 int
    224 ultrix_sys_wait3(p, v, retval)
    225 	struct proc *p;
    226 	void *v;
    227 	register_t *retval;
    228 {
    229 	struct ultrix_sys_wait3_args *uap = v;
    230 	struct sys_wait4_args ua;
    231 
    232 	SCARG(&ua, pid) = -1;
    233 	SCARG(&ua, status) = SCARG(uap, status);
    234 	SCARG(&ua, options) = SCARG(uap, options);
    235 	SCARG(&ua, rusage) = SCARG(uap, rusage);
    236 
    237 	return (sys_wait4(p, &ua, retval));
    238 }
    239 
    240 /*
    241  * Ultrix binaries pass in FD_MAX as the first arg to select().
    242  * On Ultrix, FD_MAX is 4096, which is more than the NetBSD sys_select()
    243  * can handle.
    244  * Since we can't have more than the (native) FD_MAX descriptors open,
    245  * limit nfds to at most FD_MAX.
    246  */
    247 int
    248 ultrix_sys_select(p, v, retval)
    249 	struct proc *p;
    250 	void *v;
    251 	register_t *retval;
    252 {
    253 	struct sys_select_args *uap = v;
    254 	struct timeval atv;
    255 	int error;
    256 
    257 	/* Limit number of FDs selected on to the native maximum */
    258 
    259 	if (SCARG(uap, nd) > FD_SETSIZE)
    260 		SCARG(uap, nd) = FD_SETSIZE;
    261 
    262 	/* Check for negative timeval */
    263 	if (SCARG(uap, tv)) {
    264 		error = copyin((caddr_t)SCARG(uap, tv), (caddr_t)&atv,
    265 			       sizeof(atv));
    266 		if (error)
    267 			goto done;
    268 #ifdef DEBUG
    269 		/* Ultrix clients sometimes give negative timeouts? */
    270 		if (atv.tv_sec < 0 || atv.tv_usec < 0)
    271 			printf("ultrix select( %ld, %ld): negative timeout\n",
    272 			    atv.tv_sec, atv.tv_usec);
    273 		/*tvp = (timeval *)STACKGAPBASE;*/
    274 #endif
    275 
    276 	}
    277 	error = sys_select(p, (void*) uap, retval);
    278 	if (error == EINVAL)
    279 		printf("ultrix select: bad args?\n");
    280 
    281 done:
    282 	return error;
    283 }
    284 
    285 #if defined(NFSCLIENT)
    286 int
    287 async_daemon(p, v, retval)
    288 	struct proc *p;
    289 	void *v;
    290 	register_t *retval;
    291 {
    292 	struct sys_nfssvc_args ouap;
    293 
    294 	SCARG(&ouap, flag) = NFSSVC_BIOD;
    295 	SCARG(&ouap, argp) = NULL;
    296 
    297 	return (sys_nfssvc(p, &ouap, retval));
    298 }
    299 #endif /* NFSCLIENT */
    300 
    301 
    302 #define	SUN__MAP_NEW	0x80000000	/* if not, old mmap & cannot handle */
    303 
    304 int
    305 ultrix_sys_mmap(p, v, retval)
    306 	register struct proc *p;
    307 	void *v;
    308 	register_t *retval;
    309 {
    310 	register struct ultrix_sys_mmap_args *uap = v;
    311 	struct sys_mmap_args ouap;
    312 	register struct filedesc *fdp;
    313 	register struct file *fp;
    314 	register struct vnode *vp;
    315 
    316 	/*
    317 	 * Verify the arguments.
    318 	 */
    319 	if (SCARG(uap, prot) & ~(PROT_READ|PROT_WRITE|PROT_EXEC))
    320 		return (EINVAL);			/* XXX still needed? */
    321 
    322 	if ((SCARG(uap, flags) & SUN__MAP_NEW) == 0)
    323 		return (EINVAL);
    324 
    325 	SCARG(&ouap, flags) = SCARG(uap, flags) & ~SUN__MAP_NEW;
    326 	SCARG(&ouap, addr) = SCARG(uap, addr);
    327 
    328 	if ((SCARG(&ouap, flags) & MAP_FIXED) == 0 &&
    329 	    SCARG(&ouap, addr) != 0 &&
    330 	    SCARG(&ouap, addr) < (caddr_t)round_page(p->p_vmspace->vm_daddr+MAXDSIZ))
    331 		SCARG(&ouap, addr) = (caddr_t)round_page(p->p_vmspace->vm_daddr+MAXDSIZ);
    332 
    333 	SCARG(&ouap, len) = SCARG(uap, len);
    334 	SCARG(&ouap, prot) = SCARG(uap, prot);
    335 	SCARG(&ouap, fd) = SCARG(uap, fd);
    336 	SCARG(&ouap, pos) = SCARG(uap, pos);
    337 
    338 	/*
    339 	 * Special case: if fd refers to /dev/zero, map as MAP_ANON.  (XXX)
    340 	 */
    341 	fdp = p->p_fd;
    342 	if ((unsigned)SCARG(&ouap, fd) < fdp->fd_nfiles &&		/*XXX*/
    343 	    (fp = fdp->fd_ofiles[SCARG(&ouap, fd)]) != NULL &&		/*XXX*/
    344 	    fp->f_type == DTYPE_VNODE &&				/*XXX*/
    345 	    (vp = (struct vnode *)fp->f_data)->v_type == VCHR &&	/*XXX*/
    346 	    iszerodev(vp->v_rdev)) {					/*XXX*/
    347 		SCARG(&ouap, flags) |= MAP_ANON;
    348 		SCARG(&ouap, fd) = -1;
    349 	}
    350 
    351 	return (sys_mmap(p, &ouap, retval));
    352 }
    353 
    354 int
    355 ultrix_sys_setsockopt(p, v, retval)
    356 	struct proc *p;
    357 	void *v;
    358 	register_t *retval;
    359 {
    360 	register struct ultrix_sys_setsockopt_args *uap = v;
    361 	struct file *fp;
    362 	struct mbuf *m = NULL;
    363 	int error;
    364 
    365 	if ((error = getsock(p->p_fd, SCARG(uap, s), &fp))  != 0)
    366 		return (error);
    367 #define	SO_DONTLINGER (~SO_LINGER)
    368 	if (SCARG(uap, name) == SO_DONTLINGER) {
    369 		m = m_get(M_WAIT, MT_SOOPTS);
    370 		mtod(m, struct linger *)->l_onoff = 0;
    371 		m->m_len = sizeof(struct linger);
    372 		return (sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
    373 		    SO_LINGER, m));
    374 	}
    375 	if (SCARG(uap, valsize) > MLEN)
    376 		return (EINVAL);
    377 	if (SCARG(uap, val)) {
    378 		m = m_get(M_WAIT, MT_SOOPTS);
    379 		if ((error = copyin(SCARG(uap, val), mtod(m, caddr_t),
    380 				    (u_int)SCARG(uap, valsize))) != 0) {
    381 			(void) m_free(m);
    382 			return (error);
    383 		}
    384 		m->m_len = SCARG(uap, valsize);
    385 	}
    386 	return (sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
    387 	    SCARG(uap, name), m));
    388 }
    389 
    390 struct ultrix_utsname {
    391 	char    sysname[9];
    392 	char    nodename[9];
    393 	char    nodeext[65-9];
    394 	char    release[9];
    395 	char    version[9];
    396 	char    machine[9];
    397 };
    398 
    399 int
    400 ultrix_sys_uname(p, v, retval)
    401 	struct proc *p;
    402 	void *v;
    403 	register_t *retval;
    404 {
    405 	struct ultrix_sys_uname_args *uap = v;
    406 	struct ultrix_utsname sut;
    407 	extern char ostype[], machine[], osrelease[];
    408 
    409 	bzero(&sut, sizeof(sut));
    410 
    411 	bcopy(ostype, sut.sysname, sizeof(sut.sysname) - 1);
    412 	bcopy(hostname, sut.nodename, sizeof(sut.nodename));
    413 	sut.nodename[sizeof(sut.nodename)-1] = '\0';
    414 	bcopy(osrelease, sut.release, sizeof(sut.release) - 1);
    415 	bcopy("1", sut.version, sizeof(sut.version) - 1);
    416 	bcopy(machine, sut.machine, sizeof(sut.machine) - 1);
    417 
    418 	return copyout((caddr_t)&sut, (caddr_t)SCARG(uap, name),
    419 	    sizeof(struct ultrix_utsname));
    420 }
    421 
    422 int
    423 ultrix_sys_setpgrp(p, v, retval)
    424 	struct proc *p;
    425 	void *v;
    426 	register_t *retval;
    427 {
    428 	struct ultrix_sys_setpgrp_args *uap = v;
    429 
    430 	/*
    431 	 * difference to our setpgid call is to include backwards
    432 	 * compatibility to pre-setsid() binaries. Do setsid()
    433 	 * instead of setpgid() in those cases where the process
    434 	 * tries to create a new session the old way.
    435 	 */
    436 	if (!SCARG(uap, pgid) &&
    437 	    (!SCARG(uap, pid) || SCARG(uap, pid) == p->p_pid))
    438 		return sys_setsid(p, uap, retval);
    439 	else
    440 		return sys_setpgid(p, uap, retval);
    441 }
    442 
    443 #if defined (NFSSERVER)
    444 int
    445 ultrix_sys_nfssvc(p, v, retval)
    446 	struct proc *p;
    447 	void *v;
    448 	register_t *retval;
    449 {
    450 
    451 #if 0	/* XXX */
    452 	struct ultrix_sys_nfssvc_args *uap = v;
    453 	struct emul *e = p->p_emul;
    454 	struct sys_nfssvc_args outuap;
    455 	struct sockaddr sa;
    456 	int error;
    457 
    458 	bzero(&outuap, sizeof outuap);
    459 	SCARG(&outuap, fd) = SCARG(uap, fd);
    460 	SCARG(&outuap, mskval) = STACKGAPBASE;
    461 	SCARG(&outuap, msklen) = sizeof sa;
    462 	SCARG(&outuap, mtchval) = outuap.mskval + sizeof sa;
    463 	SCARG(&outuap, mtchlen) = sizeof sa;
    464 
    465 	bzero(&sa, sizeof sa);
    466 	if (error = copyout(&sa, SCARG(&outuap, mskval), SCARG(&outuap, msklen)))
    467 		return (error);
    468 	if (error = copyout(&sa, SCARG(&outuap, mtchval), SCARG(&outuap, mtchlen)))
    469 		return (error);
    470 
    471 	return nfssvc(p, &outuap, retval);
    472 #else
    473 	return (ENOSYS);
    474 #endif
    475 }
    476 #endif /* NFSSERVER */
    477 
    478 struct ultrix_ustat {
    479 	daddr_t	f_tfree;	/* total free */
    480 	ino_t	f_tinode;	/* total inodes free */
    481 	char	f_fname[6];	/* filsys name */
    482 	char	f_fpack[6];	/* filsys pack name */
    483 };
    484 
    485 int
    486 ultrix_sys_ustat(p, v, retval)
    487 	struct proc *p;
    488 	void *v;
    489 	register_t *retval;
    490 {
    491 	struct ultrix_sys_ustat_args *uap = v;
    492 	struct ultrix_ustat us;
    493 	int error;
    494 
    495 	bzero(&us, sizeof us);
    496 
    497 	/*
    498 	 * XXX: should set f_tfree and f_tinode at least
    499 	 * How do we translate dev -> fstat? (and then to ultrix_ustat)
    500 	 */
    501 
    502 	if ((error = copyout(&us, SCARG(uap, buf), sizeof us)) != 0)
    503 		return (error);
    504 	return 0;
    505 }
    506 
    507 int
    508 ultrix_sys_quotactl(p, v, retval)
    509 	struct proc *p;
    510 	void *v;
    511 	register_t *retval;
    512 {
    513 
    514 #ifdef notyet
    515 	struct ultrix_sys_quotactl_args *uap = v;
    516 #endif
    517 
    518 	return EINVAL;
    519 }
    520 
    521 int
    522 ultrix_sys_vhangup(p, v, retval)
    523 	struct proc *p;
    524 	void *v;
    525 	register_t *retval;
    526 {
    527 
    528 	return 0;
    529 }
    530 
    531 int
    532 ultrix_sys_exportfs(p, v, retval)
    533 	struct proc *p;
    534 	void *v;
    535 	register_t *retval;
    536 {
    537 #ifdef notyet
    538 	struct ultrix_sys_exportfs_args *uap = v;
    539 #endif
    540 
    541 	/*
    542 	 * XXX: should perhaps translate into a mount(2)
    543 	 * with MOUNT_EXPORT?
    544 	 */
    545 	return 0;
    546 }
    547 
    548 int
    549 ultrix_sys_sigpending(p, v, retval)
    550 	struct proc *p;
    551 	void *v;
    552 	register_t *retval;
    553 {
    554 	struct ultrix_sys_sigpending_args *uap = v;
    555 	int mask = p->p_siglist & p->p_sigmask;
    556 
    557 	return (copyout((caddr_t)&mask, (caddr_t)SCARG(uap, mask), sizeof(int)));
    558 }
    559 
    560 int
    561 ultrix_sys_sigcleanup(p, v, retval)
    562 	struct proc *p;
    563 	void *v;
    564 	register_t *retval;
    565 {
    566 	struct ultrix_sys_sigcleanup_args *uap = v;
    567 
    568 	return sys_sigreturn(p, (struct sys_sigreturn_args *)uap, retval);
    569 }
    570 
    571 int
    572 ultrix_sys_sigreturn(p, v, retval)
    573 	struct proc *p;
    574 	void *v;
    575 	register_t *retval;
    576 {
    577 	struct ultrix_sys_sigcleanup_args *uap = v;
    578 
    579 #ifdef DEBUG
    580 	printf("ultrix sigreturn\n");
    581 #endif
    582 	return sys_sigreturn(p, (struct sys_sigreturn_args  *)uap, retval);
    583 }
    584