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