Home | History | Annotate | Line # | Download | only in common
compat_util.c revision 1.14.2.3
      1  1.14.2.3    bouyer /* 	$NetBSD: compat_util.c,v 1.14.2.3 2001/02/11 19:13:23 bouyer 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 
     55       1.1  christos #include <compat/common/compat_util.h>
     56       1.1  christos 
     57       1.1  christos /*
     58       1.1  christos  * Search an alternate path before passing pathname arguments on
     59       1.1  christos  * to system calls. Useful for keeping a separate 'emulation tree'.
     60       1.1  christos  *
     61  1.14.2.3    bouyer  * According to sflag, we either check for existance of the file or if
     62  1.14.2.3    bouyer  * it can be created or if the file is symlink.
     63      1.13  christos  *
     64      1.13  christos  * In case of success, emul_find returns 0:
     65      1.13  christos  * 	If sgp is provided, the path is in user space, and pbuf gets
     66      1.13  christos  *	allocated in user space (in the stackgap). Otherwise the path
     67      1.13  christos  *	is already in kernel space and a kernel buffer gets allocated
     68      1.13  christos  *	and returned in pbuf, that must be freed by the user.
     69      1.14  christos  * In case of error, the error number is returned and *pbuf = path.
     70       1.1  christos  */
     71       1.1  christos int
     72  1.14.2.3    bouyer emul_find(p, sgp, prefix, path, pbuf, sflag)
     73       1.1  christos 	struct proc	 *p;
     74       1.1  christos 	caddr_t		 *sgp;		/* Pointer to stackgap memory */
     75       1.1  christos 	const char	 *prefix;
     76      1.10  christos 	const char	 *path;
     77      1.10  christos 	const char	**pbuf;
     78  1.14.2.3    bouyer 	int		  sflag;
     79       1.1  christos {
     80       1.1  christos 	struct nameidata	 nd;
     81       1.1  christos 	struct nameidata	 ndroot;
     82       1.1  christos 	struct vattr		 vat;
     83       1.1  christos 	struct vattr		 vatroot;
     84       1.1  christos 	int			 error;
     85       1.1  christos 	char			*ptr, *buf, *cp;
     86       1.2  christos 	const char		*pr;
     87       1.1  christos 	size_t			 sz, len;
     88       1.1  christos 
     89      1.13  christos 	buf = (char *)malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
     90       1.1  christos 	*pbuf = path;
     91       1.1  christos 
     92       1.2  christos 	for (ptr = buf, pr = prefix; (*ptr = *pr) != '\0'; ptr++, pr++)
     93       1.1  christos 		continue;
     94       1.1  christos 
     95       1.1  christos 	sz = MAXPATHLEN - (ptr - buf);
     96       1.1  christos 
     97       1.1  christos 	/*
     98       1.1  christos 	 * If sgp is not given then the path is already in kernel space
     99       1.1  christos 	 */
    100       1.1  christos 	if (sgp == NULL)
    101       1.1  christos 		error = copystr(path, ptr, sz, &len);
    102       1.1  christos 	else
    103       1.1  christos 		error = copyinstr(path, ptr, sz, &len);
    104       1.1  christos 
    105       1.3   mycroft 	if (error)
    106       1.3   mycroft 		goto bad;
    107       1.1  christos 
    108       1.1  christos 	if (*ptr != '/') {
    109       1.3   mycroft 		error = EINVAL;
    110      1.11  christos 		goto bad;
    111      1.11  christos 	}
    112      1.11  christos 
    113      1.11  christos 	/*
    114      1.11  christos 	 * We provide an escape method, so that the user can
    115      1.11  christos 	 * always specify the real root. If the path is prefixed
    116      1.11  christos 	 * by /../ we kill the alternate search
    117      1.11  christos 	 */
    118      1.11  christos 	if (ptr[1] == '.' && ptr[2] == '.' && ptr[3] == '/') {
    119      1.13  christos 		len -= 3;
    120      1.13  christos 		(void)memcpy(buf, &ptr[3], len);
    121      1.13  christos 		ptr = buf;
    122      1.13  christos 		goto good;
    123       1.1  christos 	}
    124       1.1  christos 
    125       1.1  christos 	/*
    126       1.1  christos 	 * We know that there is a / somewhere in this pathname.
    127       1.1  christos 	 * Search backwards for it, to find the file's parent dir
    128       1.1  christos 	 * to see if it exists in the alternate tree. If it does,
    129  1.14.2.3    bouyer 	 * and we want to create a file (sflag is set). We don't
    130       1.1  christos 	 * need to worry about the root comparison in this case.
    131       1.1  christos 	 */
    132       1.1  christos 
    133  1.14.2.3    bouyer 	switch (sflag) {
    134  1.14.2.3    bouyer 	case CHECK_ALT_FL_CREAT:
    135       1.3   mycroft 		for (cp = &ptr[len] - 1; *cp != '/'; cp--)
    136       1.3   mycroft 			;
    137       1.1  christos 		*cp = '\0';
    138       1.1  christos 
    139       1.1  christos 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, p);
    140       1.1  christos 
    141       1.3   mycroft 		if ((error = namei(&nd)) != 0)
    142       1.3   mycroft 			goto bad;
    143       1.1  christos 
    144       1.1  christos 		*cp = '/';
    145  1.14.2.3    bouyer 		break;
    146  1.14.2.3    bouyer 	case CHECK_ALT_FL_EXISTS:
    147  1.14.2.3    bouyer 	case CHECK_ALT_FL_SYMLINK:
    148  1.14.2.3    bouyer 		NDINIT(&nd, LOOKUP,
    149  1.14.2.3    bouyer 			(sflag == CHECK_ALT_FL_SYMLINK) ? NOFOLLOW : FOLLOW,
    150  1.14.2.3    bouyer 			UIO_SYSSPACE, buf, p);
    151       1.1  christos 
    152       1.3   mycroft 		if ((error = namei(&nd)) != 0)
    153       1.3   mycroft 			goto bad;
    154       1.1  christos 
    155       1.1  christos 		/*
    156       1.1  christos 		 * We now compare the vnode of the emulation root to the one
    157       1.1  christos 		 * vnode asked. If they resolve to be the same, then we
    158       1.1  christos 		 * ignore the match so that the real root gets used.
    159       1.1  christos 		 * This avoids the problem of traversing "../.." to find the
    160       1.1  christos 		 * root directory and never finding it, because "/" resolves
    161       1.1  christos 		 * to the emulation root directory. This is expensive :-(
    162       1.1  christos 		 */
    163       1.6       cgd 		NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, prefix, p);
    164       1.1  christos 
    165       1.3   mycroft 		if ((error = namei(&ndroot)) != 0)
    166       1.3   mycroft 			goto bad2;
    167       1.1  christos 
    168       1.3   mycroft 		if ((error = VOP_GETATTR(nd.ni_vp, &vat, p->p_ucred, p)) != 0)
    169       1.3   mycroft 			goto bad3;
    170       1.1  christos 
    171       1.1  christos 		if ((error = VOP_GETATTR(ndroot.ni_vp, &vatroot, p->p_ucred, p))
    172       1.3   mycroft 		    != 0)
    173       1.3   mycroft 			goto bad3;
    174       1.1  christos 
    175       1.1  christos 		if (vat.va_fsid == vatroot.va_fsid &&
    176       1.1  christos 		    vat.va_fileid == vatroot.va_fileid) {
    177       1.1  christos 			error = ENOENT;
    178       1.3   mycroft 			goto bad3;
    179       1.1  christos 		}
    180  1.14.2.3    bouyer 
    181  1.14.2.3    bouyer 		break;
    182       1.1  christos 	}
    183      1.13  christos 
    184      1.13  christos 	vrele(nd.ni_vp);
    185  1.14.2.3    bouyer 	if (sflag == CHECK_ALT_FL_EXISTS)
    186      1.13  christos 		vrele(ndroot.ni_vp);
    187      1.13  christos 
    188      1.13  christos good:
    189       1.1  christos 	if (sgp == NULL)
    190       1.1  christos 		*pbuf = buf;
    191       1.1  christos 	else {
    192       1.1  christos 		sz = &ptr[len] - buf;
    193       1.1  christos 		*pbuf = stackgap_alloc(sgp, sz + 1);
    194  1.14.2.1    bouyer 		if (*pbuf == NULL) {
    195  1.14.2.1    bouyer 			error = ENAMETOOLONG;
    196  1.14.2.1    bouyer 			goto bad;
    197  1.14.2.1    bouyer 		}
    198      1.13  christos 		if ((error = copyout(buf, (void *)*pbuf, sz)) != 0) {
    199      1.14  christos 			*pbuf = path;
    200  1.14.2.1    bouyer 			goto bad;
    201      1.13  christos 		}
    202       1.1  christos 		free(buf, M_TEMP);
    203       1.1  christos 	}
    204      1.13  christos 	return 0;
    205       1.3   mycroft 
    206       1.3   mycroft bad3:
    207       1.3   mycroft 	vrele(ndroot.ni_vp);
    208       1.3   mycroft bad2:
    209       1.3   mycroft 	vrele(nd.ni_vp);
    210       1.3   mycroft bad:
    211       1.3   mycroft 	free(buf, M_TEMP);
    212       1.1  christos 	return error;
    213      1.12       cgd }
    214      1.12       cgd 
    215      1.12       cgd /*
    216      1.12       cgd  * Translate one set of flags to another, based on the entries in
    217      1.12       cgd  * the given table.  If 'leftover' is specified, it is filled in
    218      1.12       cgd  * with any flags which could not be translated.
    219      1.12       cgd  */
    220      1.12       cgd unsigned long
    221      1.12       cgd emul_flags_translate(const struct emul_flags_xtab *tab,
    222      1.12       cgd 		     unsigned long in, unsigned long *leftover)
    223      1.12       cgd {
    224      1.12       cgd 	unsigned long out;
    225      1.12       cgd 
    226      1.12       cgd 	for (out = 0; tab->omask != 0; tab++) {
    227      1.12       cgd 		if ((in & tab->omask) == tab->oval) {
    228      1.12       cgd 			in &= ~tab->omask;
    229      1.12       cgd 			out |= tab->nval;
    230      1.12       cgd 		}
    231      1.12       cgd 	}
    232      1.12       cgd 	if (leftover != NULL)
    233      1.12       cgd 		*leftover = in;
    234      1.12       cgd 	return (out);
    235       1.5   thorpej }
    236       1.5   thorpej 
    237       1.5   thorpej caddr_t
    238       1.5   thorpej stackgap_init(e)
    239  1.14.2.2    bouyer 	const struct emul *e;
    240       1.5   thorpej {
    241  1.14.2.1    bouyer 	struct proc *p = curproc;		/* XXX */
    242       1.5   thorpej 
    243       1.5   thorpej #define szsigcode ((caddr_t)(e->e_esigcode - e->e_sigcode))
    244  1.14.2.1    bouyer 	return (caddr_t)(((unsigned long)p->p_psstr - (unsigned long)szsigcode
    245  1.14.2.1    bouyer 		- STACKGAPLEN) & ~ALIGNBYTES);
    246       1.5   thorpej #undef szsigcode
    247       1.5   thorpej }
    248       1.5   thorpej 
    249       1.5   thorpej 
    250       1.5   thorpej void *
    251       1.5   thorpej stackgap_alloc(sgp, sz)
    252       1.5   thorpej 	caddr_t *sgp;
    253       1.5   thorpej 	size_t sz;
    254       1.5   thorpej {
    255  1.14.2.1    bouyer 	void *n = (void *) *sgp;
    256  1.14.2.1    bouyer 	caddr_t nsgp;
    257  1.14.2.1    bouyer 	struct proc *p = curproc;		/* XXX */
    258  1.14.2.2    bouyer 	const struct emul *e = p->p_emul;
    259  1.14.2.1    bouyer 	int sigsize = e->e_esigcode - e->e_sigcode;
    260  1.14.2.1    bouyer 
    261  1.14.2.1    bouyer 	sz = ALIGN(sz);
    262  1.14.2.1    bouyer 	nsgp = *sgp + sz;
    263  1.14.2.1    bouyer 	if (nsgp > (((caddr_t)p->p_psstr) - sigsize))
    264  1.14.2.1    bouyer 		return NULL;
    265  1.14.2.1    bouyer 	*sgp = nsgp;
    266  1.14.2.1    bouyer 	return n;
    267       1.7      fvdl }
    268       1.7      fvdl 
    269       1.7      fvdl void
    270       1.7      fvdl compat_offseterr(vp, msg)
    271       1.7      fvdl 	struct vnode *vp;
    272       1.7      fvdl 	char *msg;
    273       1.7      fvdl {
    274       1.7      fvdl 	struct mount *mp;
    275       1.7      fvdl 
    276       1.7      fvdl 	mp = vp->v_mount;
    277       1.7      fvdl 
    278       1.7      fvdl 	log(LOG_ERR, "%s: dir offset too large on fs %s (mounted from %s)\n",
    279       1.7      fvdl 	    msg, mp->mnt_stat.f_mntonname, mp->mnt_stat.f_mntfromname);
    280       1.7      fvdl 	uprintf("%s: dir offset too large for emulated program\n", msg);
    281       1.1  christos }
    282