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