Home | History | Annotate | Line # | Download | only in common
compat_util.c revision 1.8
      1 /* 	$NetBSD: compat_util.c,v 1.8 1998/09/05 14:50:25 christos 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.
      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 /*
     40  * Copyright (c) 1995 Frank van der Linden
     41  * All rights reserved.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. The name of the author may not be used to endorse or promote products
     52  *    derived from this software without specific prior written permission
     53  *
     54  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     55  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     56  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     57  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     58  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     59  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     60  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     61  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     62  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     63  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     64  *
     65  */
     66 
     67 #include <sys/param.h>
     68 #include <sys/systm.h>
     69 #include <sys/namei.h>
     70 #include <sys/proc.h>
     71 #include <sys/file.h>
     72 #include <sys/stat.h>
     73 #include <sys/filedesc.h>
     74 #include <sys/exec.h>
     75 #include <sys/ioctl.h>
     76 #include <sys/kernel.h>
     77 #include <sys/malloc.h>
     78 #include <sys/vnode.h>
     79 #include <sys/syslog.h>
     80 #include <sys/mount.h>
     81 
     82 #include <vm/vm_param.h>
     83 
     84 #include <compat/common/compat_util.h>
     85 
     86 /*
     87  * Search an alternate path before passing pathname arguments on
     88  * to system calls. Useful for keeping a separate 'emulation tree'.
     89  *
     90  * If cflag is set, we check if an attempt can be made to create
     91  * the named file, i.e. we check if the directory it should
     92  * be in exists.
     93  */
     94 int
     95 emul_find(p, sgp, prefix, path, pbuf, cflag)
     96 	struct proc	 *p;
     97 	caddr_t		 *sgp;		/* Pointer to stackgap memory */
     98 	const char	 *prefix;
     99 	char		 *path;
    100 	char		**pbuf;
    101 	int		  cflag;
    102 {
    103 	struct nameidata	 nd;
    104 	struct nameidata	 ndroot;
    105 	struct vattr		 vat;
    106 	struct vattr		 vatroot;
    107 	int			 error;
    108 	char			*ptr, *buf, *cp;
    109 	const char		*pr;
    110 	size_t			 sz, len;
    111 
    112 	buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
    113 	*pbuf = path;
    114 
    115 	for (ptr = buf, pr = prefix; (*ptr = *pr) != '\0'; ptr++, pr++)
    116 		continue;
    117 
    118 	sz = MAXPATHLEN - (ptr - buf);
    119 
    120 	/*
    121 	 * If sgp is not given then the path is already in kernel space
    122 	 */
    123 	if (sgp == NULL)
    124 		error = copystr(path, ptr, sz, &len);
    125 	else
    126 		error = copyinstr(path, ptr, sz, &len);
    127 
    128 	if (error)
    129 		goto bad;
    130 
    131 	if (*ptr != '/') {
    132 		error = EINVAL;
    133 		goto bad;
    134 	}
    135 
    136 	/*
    137 	 * We know that there is a / somewhere in this pathname.
    138 	 * Search backwards for it, to find the file's parent dir
    139 	 * to see if it exists in the alternate tree. If it does,
    140 	 * and we want to create a file (cflag is set). We don't
    141 	 * need to worry about the root comparison in this case.
    142 	 */
    143 
    144 	if (cflag) {
    145 		for (cp = &ptr[len] - 1; *cp != '/'; cp--)
    146 			;
    147 		*cp = '\0';
    148 
    149 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, p);
    150 
    151 		if ((error = namei(&nd)) != 0)
    152 			goto bad;
    153 
    154 		*cp = '/';
    155 	}
    156 	else {
    157 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, p);
    158 
    159 		if ((error = namei(&nd)) != 0)
    160 			goto bad;
    161 
    162 		/*
    163 		 * We now compare the vnode of the emulation root to the one
    164 		 * vnode asked. If they resolve to be the same, then we
    165 		 * ignore the match so that the real root gets used.
    166 		 * This avoids the problem of traversing "../.." to find the
    167 		 * root directory and never finding it, because "/" resolves
    168 		 * to the emulation root directory. This is expensive :-(
    169 		 */
    170 		NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, prefix, p);
    171 
    172 		if ((error = namei(&ndroot)) != 0)
    173 			goto bad2;
    174 
    175 		if ((error = VOP_GETATTR(nd.ni_vp, &vat, p->p_ucred, p)) != 0)
    176 			goto bad3;
    177 
    178 		if ((error = VOP_GETATTR(ndroot.ni_vp, &vatroot, p->p_ucred, p))
    179 		    != 0)
    180 			goto bad3;
    181 
    182 		if (vat.va_fsid == vatroot.va_fsid &&
    183 		    vat.va_fileid == vatroot.va_fileid) {
    184 			error = ENOENT;
    185 			goto bad3;
    186 		}
    187 	}
    188 	if (sgp == NULL)
    189 		*pbuf = buf;
    190 	else {
    191 		sz = &ptr[len] - buf;
    192 		*pbuf = stackgap_alloc(sgp, sz + 1);
    193 		error = copyout(buf, *pbuf, sz);
    194 		free(buf, M_TEMP);
    195 	}
    196 
    197 	vrele(nd.ni_vp);
    198 	if (!cflag)
    199 		vrele(ndroot.ni_vp);
    200 	return error;
    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 caddr_t
    212 stackgap_init(e)
    213 	struct emul *e;
    214 {
    215 
    216 #define szsigcode ((caddr_t)(e->e_esigcode - e->e_sigcode))
    217 	return STACKGAPBASE;
    218 #undef szsigcode
    219 }
    220 
    221 
    222 void *
    223 stackgap_alloc(sgp, sz)
    224 	caddr_t *sgp;
    225 	size_t sz;
    226 {
    227 	void *p = (void *) *sgp;
    228 
    229 	*sgp += ALIGN(sz);
    230 	return p;
    231 }
    232 
    233 void
    234 compat_offseterr(vp, msg)
    235 	struct vnode *vp;
    236 	char *msg;
    237 {
    238 	struct mount *mp;
    239 
    240 	mp = vp->v_mount;
    241 
    242 	log(LOG_ERR, "%s: dir offset too large on fs %s (mounted from %s)\n",
    243 	    msg, mp->mnt_stat.f_mntonname, mp->mnt_stat.f_mntfromname);
    244 	uprintf("%s: dir offset too large for emulated program\n", msg);
    245 }
    246