Home | History | Annotate | Line # | Download | only in common
compat_util.c revision 1.11.2.2
      1  1.11.2.2       cgd /* 	$NetBSD: compat_util.c,v 1.11.2.2 1999/06/21 19:23:26 cgd Exp $	*/
      2       1.8  christos 
      3       1.8  christos /*-
      4       1.8  christos  * Copyright (c) 1994 The NetBSD Foundation, Inc.
      5       1.8  christos  * All rights reserved.
      6       1.8  christos  *
      7       1.8  christos  * This code is derived from software contributed to The NetBSD Foundation
      8       1.9      fvdl  * by Christos Zoulas and Frank van der Linden.
      9       1.8  christos  *
     10       1.8  christos  * Redistribution and use in source and binary forms, with or without
     11       1.8  christos  * modification, are permitted provided that the following conditions
     12       1.8  christos  * are met:
     13       1.8  christos  * 1. Redistributions of source code must retain the above copyright
     14       1.8  christos  *    notice, this list of conditions and the following disclaimer.
     15       1.8  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.8  christos  *    notice, this list of conditions and the following disclaimer in the
     17       1.8  christos  *    documentation and/or other materials provided with the distribution.
     18       1.8  christos  * 3. All advertising materials mentioning features or use of this software
     19       1.8  christos  *    must display the following acknowledgement:
     20       1.8  christos  *        This product includes software developed by the NetBSD
     21       1.8  christos  *        Foundation, Inc. and its contributors.
     22       1.8  christos  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23       1.8  christos  *    contributors may be used to endorse or promote products derived
     24       1.8  christos  *    from this software without specific prior written permission.
     25       1.8  christos  *
     26       1.8  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27       1.8  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28       1.8  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29       1.8  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30       1.8  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31       1.8  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32       1.8  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33       1.8  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34       1.8  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35       1.8  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36       1.8  christos  * POSSIBILITY OF SUCH DAMAGE.
     37       1.1  christos  */
     38       1.1  christos 
     39       1.1  christos #include <sys/param.h>
     40       1.1  christos #include <sys/systm.h>
     41       1.1  christos #include <sys/namei.h>
     42       1.1  christos #include <sys/proc.h>
     43       1.1  christos #include <sys/file.h>
     44       1.1  christos #include <sys/stat.h>
     45       1.1  christos #include <sys/filedesc.h>
     46       1.5   thorpej #include <sys/exec.h>
     47       1.1  christos #include <sys/ioctl.h>
     48       1.1  christos #include <sys/kernel.h>
     49       1.1  christos #include <sys/malloc.h>
     50       1.1  christos #include <sys/vnode.h>
     51       1.7      fvdl #include <sys/syslog.h>
     52       1.7      fvdl #include <sys/mount.h>
     53       1.1  christos 
     54       1.5   thorpej #include <vm/vm_param.h>
     55       1.5   thorpej 
     56       1.1  christos #include <compat/common/compat_util.h>
     57       1.1  christos 
     58       1.1  christos /*
     59       1.1  christos  * Search an alternate path before passing pathname arguments on
     60       1.1  christos  * to system calls. Useful for keeping a separate 'emulation tree'.
     61       1.1  christos  *
     62       1.1  christos  * If cflag is set, we check if an attempt can be made to create
     63       1.1  christos  * the named file, i.e. we check if the directory it should
     64       1.1  christos  * be in exists.
     65  1.11.2.1     perry  *
     66  1.11.2.1     perry  * In case of success, emul_find returns 0:
     67  1.11.2.1     perry  * 	If sgp is provided, the path is in user space, and pbuf gets
     68  1.11.2.1     perry  *	allocated in user space (in the stackgap). Otherwise the path
     69  1.11.2.1     perry  *	is already in kernel space and a kernel buffer gets allocated
     70  1.11.2.1     perry  *	and returned in pbuf, that must be freed by the user.
     71  1.11.2.1     perry  * In case of error, the error number is returned and *pbuf = path.
     72       1.1  christos  */
     73       1.1  christos int
     74       1.1  christos emul_find(p, sgp, prefix, path, pbuf, cflag)
     75       1.1  christos 	struct proc	 *p;
     76       1.1  christos 	caddr_t		 *sgp;		/* Pointer to stackgap memory */
     77       1.1  christos 	const char	 *prefix;
     78      1.10  christos 	const char	 *path;
     79      1.10  christos 	const char	**pbuf;
     80       1.1  christos 	int		  cflag;
     81       1.1  christos {
     82       1.1  christos 	struct nameidata	 nd;
     83       1.1  christos 	struct nameidata	 ndroot;
     84       1.1  christos 	struct vattr		 vat;
     85       1.1  christos 	struct vattr		 vatroot;
     86       1.1  christos 	int			 error;
     87       1.1  christos 	char			*ptr, *buf, *cp;
     88       1.2  christos 	const char		*pr;
     89       1.1  christos 	size_t			 sz, len;
     90       1.1  christos 
     91  1.11.2.1     perry 	buf = (char *)malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
     92       1.1  christos 	*pbuf = path;
     93       1.1  christos 
     94       1.2  christos 	for (ptr = buf, pr = prefix; (*ptr = *pr) != '\0'; ptr++, pr++)
     95       1.1  christos 		continue;
     96       1.1  christos 
     97       1.1  christos 	sz = MAXPATHLEN - (ptr - buf);
     98       1.1  christos 
     99       1.1  christos 	/*
    100       1.1  christos 	 * If sgp is not given then the path is already in kernel space
    101       1.1  christos 	 */
    102       1.1  christos 	if (sgp == NULL)
    103       1.1  christos 		error = copystr(path, ptr, sz, &len);
    104       1.1  christos 	else
    105       1.1  christos 		error = copyinstr(path, ptr, sz, &len);
    106       1.1  christos 
    107       1.3   mycroft 	if (error)
    108       1.3   mycroft 		goto bad;
    109       1.1  christos 
    110       1.1  christos 	if (*ptr != '/') {
    111       1.3   mycroft 		error = EINVAL;
    112      1.11  christos 		goto bad;
    113      1.11  christos 	}
    114      1.11  christos 
    115      1.11  christos 	/*
    116      1.11  christos 	 * We provide an escape method, so that the user can
    117      1.11  christos 	 * always specify the real root. If the path is prefixed
    118      1.11  christos 	 * by /../ we kill the alternate search
    119      1.11  christos 	 */
    120      1.11  christos 	if (ptr[1] == '.' && ptr[2] == '.' && ptr[3] == '/') {
    121  1.11.2.1     perry 		len -= 3;
    122  1.11.2.1     perry 		(void)memcpy(buf, &ptr[3], len);
    123  1.11.2.1     perry 		ptr = buf;
    124  1.11.2.1     perry 		goto good;
    125       1.1  christos 	}
    126       1.1  christos 
    127       1.1  christos 	/*
    128       1.1  christos 	 * We know that there is a / somewhere in this pathname.
    129       1.1  christos 	 * Search backwards for it, to find the file's parent dir
    130       1.1  christos 	 * to see if it exists in the alternate tree. If it does,
    131       1.1  christos 	 * and we want to create a file (cflag is set). We don't
    132       1.1  christos 	 * need to worry about the root comparison in this case.
    133       1.1  christos 	 */
    134       1.1  christos 
    135       1.1  christos 	if (cflag) {
    136       1.3   mycroft 		for (cp = &ptr[len] - 1; *cp != '/'; cp--)
    137       1.3   mycroft 			;
    138       1.1  christos 		*cp = '\0';
    139       1.1  christos 
    140       1.1  christos 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, p);
    141       1.1  christos 
    142       1.3   mycroft 		if ((error = namei(&nd)) != 0)
    143       1.3   mycroft 			goto bad;
    144       1.1  christos 
    145       1.1  christos 		*cp = '/';
    146       1.1  christos 	}
    147       1.1  christos 	else {
    148       1.1  christos 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, p);
    149       1.1  christos 
    150       1.3   mycroft 		if ((error = namei(&nd)) != 0)
    151       1.3   mycroft 			goto bad;
    152       1.1  christos 
    153       1.1  christos 		/*
    154       1.1  christos 		 * We now compare the vnode of the emulation root to the one
    155       1.1  christos 		 * vnode asked. If they resolve to be the same, then we
    156       1.1  christos 		 * ignore the match so that the real root gets used.
    157       1.1  christos 		 * This avoids the problem of traversing "../.." to find the
    158       1.1  christos 		 * root directory and never finding it, because "/" resolves
    159       1.1  christos 		 * to the emulation root directory. This is expensive :-(
    160       1.1  christos 		 */
    161       1.6       cgd 		NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, prefix, p);
    162       1.1  christos 
    163       1.3   mycroft 		if ((error = namei(&ndroot)) != 0)
    164       1.3   mycroft 			goto bad2;
    165       1.1  christos 
    166       1.3   mycroft 		if ((error = VOP_GETATTR(nd.ni_vp, &vat, p->p_ucred, p)) != 0)
    167       1.3   mycroft 			goto bad3;
    168       1.1  christos 
    169       1.1  christos 		if ((error = VOP_GETATTR(ndroot.ni_vp, &vatroot, p->p_ucred, p))
    170       1.3   mycroft 		    != 0)
    171       1.3   mycroft 			goto bad3;
    172       1.1  christos 
    173       1.1  christos 		if (vat.va_fsid == vatroot.va_fsid &&
    174       1.1  christos 		    vat.va_fileid == vatroot.va_fileid) {
    175       1.1  christos 			error = ENOENT;
    176       1.3   mycroft 			goto bad3;
    177       1.1  christos 		}
    178       1.1  christos 	}
    179  1.11.2.1     perry 
    180  1.11.2.1     perry 	vrele(nd.ni_vp);
    181  1.11.2.1     perry 	if (!cflag)
    182  1.11.2.1     perry 		vrele(ndroot.ni_vp);
    183  1.11.2.1     perry 
    184  1.11.2.1     perry good:
    185       1.1  christos 	if (sgp == NULL)
    186       1.1  christos 		*pbuf = buf;
    187       1.1  christos 	else {
    188       1.1  christos 		sz = &ptr[len] - buf;
    189       1.1  christos 		*pbuf = stackgap_alloc(sgp, sz + 1);
    190  1.11.2.1     perry 		if ((error = copyout(buf, (void *)*pbuf, sz)) != 0) {
    191  1.11.2.1     perry 			*pbuf = path;
    192  1.11.2.1     perry 			return error;
    193  1.11.2.1     perry 		}
    194       1.1  christos 		free(buf, M_TEMP);
    195       1.1  christos 	}
    196  1.11.2.1     perry 	return 0;
    197       1.3   mycroft 
    198       1.3   mycroft bad3:
    199       1.3   mycroft 	vrele(ndroot.ni_vp);
    200       1.3   mycroft bad2:
    201       1.3   mycroft 	vrele(nd.ni_vp);
    202       1.3   mycroft bad:
    203       1.3   mycroft 	free(buf, M_TEMP);
    204       1.1  christos 	return error;
    205  1.11.2.2       cgd }
    206  1.11.2.2       cgd 
    207  1.11.2.2       cgd /*
    208  1.11.2.2       cgd  * Translate one set of flags to another, based on the entries in
    209  1.11.2.2       cgd  * the given table.  If 'leftover' is specified, it is filled in
    210  1.11.2.2       cgd  * with any flags which could not be translated.
    211  1.11.2.2       cgd  */
    212  1.11.2.2       cgd unsigned long
    213  1.11.2.2       cgd emul_flags_translate(const struct emul_flags_xtab *tab,
    214  1.11.2.2       cgd 		     unsigned long in, unsigned long *leftover)
    215  1.11.2.2       cgd {
    216  1.11.2.2       cgd 	unsigned long out;
    217  1.11.2.2       cgd 
    218  1.11.2.2       cgd 	for (out = 0; tab->omask != 0; tab++) {
    219  1.11.2.2       cgd 		if ((in & tab->omask) == tab->oval) {
    220  1.11.2.2       cgd 			in &= ~tab->omask;
    221  1.11.2.2       cgd 			out |= tab->nval;
    222  1.11.2.2       cgd 		}
    223  1.11.2.2       cgd 	}
    224  1.11.2.2       cgd 	if (leftover != NULL)
    225  1.11.2.2       cgd 		*leftover = in;
    226  1.11.2.2       cgd 	return (out);
    227       1.5   thorpej }
    228       1.5   thorpej 
    229       1.5   thorpej caddr_t
    230       1.5   thorpej stackgap_init(e)
    231       1.5   thorpej 	struct emul *e;
    232       1.5   thorpej {
    233       1.5   thorpej 
    234       1.5   thorpej #define szsigcode ((caddr_t)(e->e_esigcode - e->e_sigcode))
    235       1.5   thorpej 	return STACKGAPBASE;
    236       1.5   thorpej #undef szsigcode
    237       1.5   thorpej }
    238       1.5   thorpej 
    239       1.5   thorpej 
    240       1.5   thorpej void *
    241       1.5   thorpej stackgap_alloc(sgp, sz)
    242       1.5   thorpej 	caddr_t *sgp;
    243       1.5   thorpej 	size_t sz;
    244       1.5   thorpej {
    245       1.5   thorpej 	void *p = (void *) *sgp;
    246       1.5   thorpej 
    247       1.5   thorpej 	*sgp += ALIGN(sz);
    248       1.5   thorpej 	return p;
    249       1.7      fvdl }
    250       1.7      fvdl 
    251       1.7      fvdl void
    252       1.7      fvdl compat_offseterr(vp, msg)
    253       1.7      fvdl 	struct vnode *vp;
    254       1.7      fvdl 	char *msg;
    255       1.7      fvdl {
    256       1.7      fvdl 	struct mount *mp;
    257       1.7      fvdl 
    258       1.7      fvdl 	mp = vp->v_mount;
    259       1.7      fvdl 
    260       1.7      fvdl 	log(LOG_ERR, "%s: dir offset too large on fs %s (mounted from %s)\n",
    261       1.7      fvdl 	    msg, mp->mnt_stat.f_mntonname, mp->mnt_stat.f_mntfromname);
    262       1.7      fvdl 	uprintf("%s: dir offset too large for emulated program\n", msg);
    263       1.1  christos }
    264