Home | History | Annotate | Line # | Download | only in common
compat_util.c revision 1.36
      1 /* 	$NetBSD: compat_util.c,v 1.36 2007/04/26 20:06:55 dsl 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/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: compat_util.c,v 1.36 2007/04/26 20:06:55 dsl Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/namei.h>
     45 #include <sys/proc.h>
     46 #include <sys/file.h>
     47 #include <sys/stat.h>
     48 #include <sys/filedesc.h>
     49 #include <sys/exec.h>
     50 #include <sys/ioctl.h>
     51 #include <sys/kernel.h>
     52 #include <sys/malloc.h>
     53 #include <sys/vnode.h>
     54 #include <sys/syslog.h>
     55 #include <sys/mount.h>
     56 
     57 
     58 #include <compat/common/compat_util.h>
     59 
     60 void
     61 emul_find_root(struct lwp *l, struct exec_package *epp)
     62 {
     63 	struct nameidata nd;
     64 	const char *emul_path;
     65 
     66 	if (epp->ep_emul_root != NULL)
     67 		/* We've already found it */
     68 		return;
     69 
     70 	emul_path = epp->ep_esch->es_emul->e_path;
     71 	if (emul_path == NULL)
     72 		/* Emulation doesn't have a root */
     73 		return;
     74 
     75 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, emul_path, l);
     76 	if (namei(&nd) != 0)
     77 		/* emulation root doesn't exist */
     78 		return;
     79 
     80 	epp->ep_emul_root = nd.ni_vp;
     81 }
     82 
     83 /*
     84  * Search the alternate path for dynamic binary interpreter. If not found
     85  * there, check if the interpreter exists in within 'proper' tree.
     86  */
     87 int
     88 emul_find_interp(struct lwp *l, struct exec_package *epp, const char *itp)
     89 {
     90 	int error;
     91 	struct nameidata nd;
     92 	unsigned int flags;
     93 
     94 	/* If we haven't found the emulation root already, do so now */
     95 	/* Maybe we should remember failures somehow ? */
     96 	if (epp->ep_esch->es_emul->e_path != 0 && epp->ep_emul_root == NULL)
     97 		emul_find_root(l, epp);
     98 
     99 	if (epp->ep_interp != NULL)
    100 		vrele(epp->ep_interp);
    101 
    102 	/* We need to use the emulation root for the new program,
    103 	 * not the one for the current process. */
    104 	if (epp->ep_emul_root == NULL)
    105 		flags = FOLLOW;
    106 	else {
    107 		nd.ni_erootdir = epp->ep_emul_root;
    108 		/* hack: Pass in the emulation path for ktrace calls */
    109 		nd.ni_next = epp->ep_esch->es_emul->e_path;
    110 		flags = FOLLOW | TRYEMULROOT | EMULROOTSET;
    111 	}
    112 
    113 	NDINIT(&nd, LOOKUP, flags, UIO_SYSSPACE, itp, l);
    114 	error = namei(&nd);
    115 	if (error != 0) {
    116 		epp->ep_interp = NULL;
    117 		return error;
    118 	}
    119 
    120 	/* Save interpreter in case we actually need to load it */
    121 	epp->ep_interp = nd.ni_vp;
    122 
    123 	return 0;
    124 }
    125 
    126 /*
    127  * Translate one set of flags to another, based on the entries in
    128  * the given table.  If 'leftover' is specified, it is filled in
    129  * with any flags which could not be translated.
    130  */
    131 unsigned long
    132 emul_flags_translate(const struct emul_flags_xtab *tab,
    133 		     unsigned long in, unsigned long *leftover)
    134 {
    135 	unsigned long out;
    136 
    137 	for (out = 0; tab->omask != 0; tab++) {
    138 		if ((in & tab->omask) == tab->oval) {
    139 			in &= ~tab->omask;
    140 			out |= tab->nval;
    141 		}
    142 	}
    143 	if (leftover != NULL)
    144 		*leftover = in;
    145 	return (out);
    146 }
    147 
    148 void *
    149 stackgap_init(p, sz)
    150 	const struct proc *p;
    151 	size_t sz;
    152 {
    153 	if (sz == 0)
    154 		sz = STACKGAPLEN;
    155 	if (sz > STACKGAPLEN)
    156 		panic("size %lu > STACKGAPLEN", (unsigned long)sz);
    157 #define szsigcode ((void *)(p->p_emul->e_esigcode - p->p_emul->e_sigcode))
    158 	return (void *)(((unsigned long)p->p_psstr - (unsigned long)szsigcode
    159 		- sz) & ~ALIGNBYTES);
    160 #undef szsigcode
    161 }
    162 
    163 
    164 void *
    165 stackgap_alloc(p, sgp, sz)
    166 	const struct proc *p;
    167 	void **sgp;
    168 	size_t sz;
    169 {
    170 	void *n = *sgp;
    171 	void *nsgp;
    172 	const struct emul *e = p->p_emul;
    173 	int sigsize = e->e_esigcode - e->e_sigcode;
    174 
    175 	sz = ALIGN(sz);
    176 	nsgp = (char *)*sgp + sz;
    177 	if (nsgp > (void *)(((char *)p->p_psstr) - sigsize))
    178 		return NULL;
    179 	*sgp = nsgp;
    180 	return n;
    181 }
    182 
    183 void
    184 compat_offseterr(vp, msg)
    185 	struct vnode *vp;
    186 	const char *msg;
    187 {
    188 	struct mount *mp;
    189 
    190 	mp = vp->v_mount;
    191 
    192 	log(LOG_ERR, "%s: dir offset too large on fs %s (mounted from %s)\n",
    193 	    msg, mp->mnt_stat.f_mntonname, mp->mnt_stat.f_mntfromname);
    194 	uprintf("%s: dir offset too large for emulated program\n", msg);
    195 }
    196