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