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