Home | History | Annotate | Line # | Download | only in common
compat_util.c revision 1.34.2.1
      1  1.34.2.1        ad /* 	$NetBSD: compat_util.c,v 1.34.2.1 2007/05/27 14:34:48 ad Exp $	*/
      2       1.8  christos 
      3       1.8  christos /*-
      4       1.8  christos  * Copyright (c) 1994 The NetBSD Foundation, Inc.
      5       1.8  christos  * All rights reserved.
      6       1.8  christos  *
      7       1.8  christos  * This code is derived from software contributed to The NetBSD Foundation
      8       1.9      fvdl  * by Christos Zoulas and Frank van der Linden.
      9       1.8  christos  *
     10       1.8  christos  * Redistribution and use in source and binary forms, with or without
     11       1.8  christos  * modification, are permitted provided that the following conditions
     12       1.8  christos  * are met:
     13       1.8  christos  * 1. Redistributions of source code must retain the above copyright
     14       1.8  christos  *    notice, this list of conditions and the following disclaimer.
     15       1.8  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.8  christos  *    notice, this list of conditions and the following disclaimer in the
     17       1.8  christos  *    documentation and/or other materials provided with the distribution.
     18       1.8  christos  * 3. All advertising materials mentioning features or use of this software
     19       1.8  christos  *    must display the following acknowledgement:
     20       1.8  christos  *        This product includes software developed by the NetBSD
     21       1.8  christos  *        Foundation, Inc. and its contributors.
     22       1.8  christos  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23       1.8  christos  *    contributors may be used to endorse or promote products derived
     24       1.8  christos  *    from this software without specific prior written permission.
     25       1.8  christos  *
     26       1.8  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27       1.8  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28       1.8  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29       1.8  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30       1.8  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31       1.8  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32       1.8  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33       1.8  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34       1.8  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35       1.8  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36       1.8  christos  * POSSIBILITY OF SUCH DAMAGE.
     37       1.1  christos  */
     38      1.21     lukem 
     39      1.21     lukem #include <sys/cdefs.h>
     40  1.34.2.1        ad __KERNEL_RCSID(0, "$NetBSD: compat_util.c,v 1.34.2.1 2007/05/27 14:34:48 ad Exp $");
     41       1.1  christos 
     42       1.1  christos #include <sys/param.h>
     43       1.1  christos #include <sys/systm.h>
     44       1.1  christos #include <sys/namei.h>
     45       1.1  christos #include <sys/proc.h>
     46       1.1  christos #include <sys/file.h>
     47       1.1  christos #include <sys/stat.h>
     48       1.1  christos #include <sys/filedesc.h>
     49       1.5   thorpej #include <sys/exec.h>
     50       1.1  christos #include <sys/ioctl.h>
     51       1.1  christos #include <sys/kernel.h>
     52       1.1  christos #include <sys/malloc.h>
     53       1.1  christos #include <sys/vnode.h>
     54       1.7      fvdl #include <sys/syslog.h>
     55       1.7      fvdl #include <sys/mount.h>
     56       1.1  christos 
     57       1.5   thorpej 
     58       1.1  christos #include <compat/common/compat_util.h>
     59       1.1  christos 
     60  1.34.2.1        ad void
     61  1.34.2.1        ad emul_find_root(struct lwp *l, struct exec_package *epp)
     62       1.1  christos {
     63  1.34.2.1        ad 	struct nameidata nd;
     64  1.34.2.1        ad 	const char *emul_path;
     65      1.11  christos 
     66  1.34.2.1        ad 	if (epp->ep_emul_root != NULL)
     67  1.34.2.1        ad 		/* We've already found it */
     68  1.34.2.1        ad 		return;
     69  1.34.2.1        ad 
     70  1.34.2.1        ad 	emul_path = epp->ep_esch->es_emul->e_path;
     71  1.34.2.1        ad 	if (emul_path == NULL)
     72  1.34.2.1        ad 		/* Emulation doesn't have a root */
     73  1.34.2.1        ad 		return;
     74  1.34.2.1        ad 
     75  1.34.2.1        ad 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, emul_path, l);
     76  1.34.2.1        ad 	if (namei(&nd) != 0)
     77  1.34.2.1        ad 		/* emulation root doesn't exist */
     78  1.34.2.1        ad 		return;
     79      1.19  jdolecek 
     80  1.34.2.1        ad 	epp->ep_emul_root = nd.ni_vp;
     81      1.25  jdolecek }
     82      1.25  jdolecek 
     83      1.25  jdolecek /*
     84      1.25  jdolecek  * Search the alternate path for dynamic binary interpreter. If not found
     85      1.25  jdolecek  * there, check if the interpreter exists in within 'proper' tree.
     86      1.25  jdolecek  */
     87      1.25  jdolecek int
     88  1.34.2.1        ad emul_find_interp(struct lwp *l, struct exec_package *epp, const char *itp)
     89      1.25  jdolecek {
     90      1.25  jdolecek 	int error;
     91  1.34.2.1        ad 	struct nameidata nd;
     92  1.34.2.1        ad 	unsigned int flags;
     93      1.25  jdolecek 
     94  1.34.2.1        ad 	/* If we haven't found the emulation root already, do so now */
     95  1.34.2.1        ad 	/* Maybe we should remember failures somehow ? */
     96  1.34.2.1        ad 	if (epp->ep_esch->es_emul->e_path != 0 && epp->ep_emul_root == NULL)
     97  1.34.2.1        ad 		emul_find_root(l, epp);
     98  1.34.2.1        ad 
     99  1.34.2.1        ad 	if (epp->ep_interp != NULL)
    100  1.34.2.1        ad 		vrele(epp->ep_interp);
    101  1.34.2.1        ad 
    102  1.34.2.1        ad 	/* We need to use the emulation root for the new program,
    103  1.34.2.1        ad 	 * not the one for the current process. */
    104  1.34.2.1        ad 	if (epp->ep_emul_root == NULL)
    105  1.34.2.1        ad 		flags = FOLLOW;
    106  1.34.2.1        ad 	else {
    107  1.34.2.1        ad 		nd.ni_erootdir = epp->ep_emul_root;
    108  1.34.2.1        ad 		/* hack: Pass in the emulation path for ktrace calls */
    109  1.34.2.1        ad 		nd.ni_next = epp->ep_esch->es_emul->e_path;
    110  1.34.2.1        ad 		flags = FOLLOW | TRYEMULROOT | EMULROOTSET;
    111  1.34.2.1        ad 	}
    112      1.25  jdolecek 
    113  1.34.2.1        ad 	NDINIT(&nd, LOOKUP, flags, UIO_SYSSPACE, itp, l);
    114  1.34.2.1        ad 	error = namei(&nd);
    115  1.34.2.1        ad 	if (error != 0) {
    116  1.34.2.1        ad 		epp->ep_interp = NULL;
    117  1.34.2.1        ad 		return error;
    118      1.25  jdolecek 	}
    119      1.25  jdolecek 
    120  1.34.2.1        ad 	/* Save interpreter in case we actually need to load it */
    121  1.34.2.1        ad 	epp->ep_interp = nd.ni_vp;
    122  1.34.2.1        ad 
    123  1.34.2.1        ad 	return 0;
    124      1.12       cgd }
    125      1.12       cgd 
    126      1.12       cgd /*
    127      1.12       cgd  * Translate one set of flags to another, based on the entries in
    128      1.12       cgd  * the given table.  If 'leftover' is specified, it is filled in
    129      1.12       cgd  * with any flags which could not be translated.
    130      1.12       cgd  */
    131      1.12       cgd unsigned long
    132      1.12       cgd emul_flags_translate(const struct emul_flags_xtab *tab,
    133      1.12       cgd 		     unsigned long in, unsigned long *leftover)
    134      1.12       cgd {
    135      1.12       cgd 	unsigned long out;
    136      1.12       cgd 
    137      1.12       cgd 	for (out = 0; tab->omask != 0; tab++) {
    138      1.12       cgd 		if ((in & tab->omask) == tab->oval) {
    139      1.12       cgd 			in &= ~tab->omask;
    140      1.12       cgd 			out |= tab->nval;
    141      1.12       cgd 		}
    142      1.12       cgd 	}
    143      1.12       cgd 	if (leftover != NULL)
    144      1.12       cgd 		*leftover = in;
    145      1.12       cgd 	return (out);
    146       1.5   thorpej }
    147       1.5   thorpej 
    148      1.34  christos void *
    149      1.22  christos stackgap_init(p, sz)
    150      1.22  christos 	const struct proc *p;
    151      1.22  christos 	size_t sz;
    152       1.5   thorpej {
    153      1.22  christos 	if (sz == 0)
    154      1.22  christos 		sz = STACKGAPLEN;
    155      1.23  christos 	if (sz > STACKGAPLEN)
    156      1.23  christos 		panic("size %lu > STACKGAPLEN", (unsigned long)sz);
    157      1.34  christos #define szsigcode ((void *)(p->p_emul->e_esigcode - p->p_emul->e_sigcode))
    158      1.34  christos 	return (void *)(((unsigned long)p->p_psstr - (unsigned long)szsigcode
    159      1.22  christos 		- sz) & ~ALIGNBYTES);
    160       1.5   thorpej #undef szsigcode
    161       1.5   thorpej }
    162       1.5   thorpej 
    163       1.5   thorpej 
    164       1.5   thorpej void *
    165      1.22  christos stackgap_alloc(p, sgp, sz)
    166      1.22  christos 	const struct proc *p;
    167      1.34  christos 	void **sgp;
    168       1.5   thorpej 	size_t sz;
    169       1.5   thorpej {
    170      1.34  christos 	void *n = *sgp;
    171      1.34  christos 	void *nsgp;
    172      1.18  jdolecek 	const struct emul *e = p->p_emul;
    173      1.16  sommerfe 	int sigsize = e->e_esigcode - e->e_sigcode;
    174      1.28     perry 
    175      1.16  sommerfe 	sz = ALIGN(sz);
    176      1.34  christos 	nsgp = (char *)*sgp + sz;
    177      1.34  christos 	if (nsgp > (void *)(((char *)p->p_psstr) - sigsize))
    178      1.16  sommerfe 		return NULL;
    179      1.16  sommerfe 	*sgp = nsgp;
    180      1.17       eeh 	return n;
    181       1.7      fvdl }
    182       1.7      fvdl 
    183       1.7      fvdl void
    184       1.7      fvdl compat_offseterr(vp, msg)
    185       1.7      fvdl 	struct vnode *vp;
    186      1.29  christos 	const char *msg;
    187       1.7      fvdl {
    188       1.7      fvdl 	struct mount *mp;
    189       1.7      fvdl 
    190       1.7      fvdl 	mp = vp->v_mount;
    191       1.7      fvdl 
    192       1.7      fvdl 	log(LOG_ERR, "%s: dir offset too large on fs %s (mounted from %s)\n",
    193       1.7      fvdl 	    msg, mp->mnt_stat.f_mntonname, mp->mnt_stat.f_mntfromname);
    194       1.7      fvdl 	uprintf("%s: dir offset too large for emulated program\n", msg);
    195       1.1  christos }
    196