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