Home | History | Annotate | Line # | Download | only in common
compat_util.c revision 1.43.4.1
      1  1.43.4.1     rmind /* 	$NetBSD: compat_util.c,v 1.43.4.1 2011/03/05 20:52:38 rmind 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  *
     19       1.8  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.8  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.8  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.8  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.8  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.8  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.8  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.8  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.8  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.8  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.8  christos  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1  christos  */
     31      1.21     lukem 
     32      1.43       mrg /*
     33      1.43       mrg  * Copyright (c) 2008, 2009 Matthew R. Green
     34      1.43       mrg  * All rights reserved.
     35      1.43       mrg  *
     36      1.43       mrg  * Redistribution and use in source and binary forms, with or without
     37      1.43       mrg  * modification, are permitted provided that the following conditions
     38      1.43       mrg  * are met:
     39      1.43       mrg  * 1. Redistributions of source code must retain the above copyright
     40      1.43       mrg  *    notice, this list of conditions and the following disclaimer.
     41      1.43       mrg  * 2. Redistributions in binary form must reproduce the above copyright
     42      1.43       mrg  *    notice, this list of conditions and the following disclaimer in the
     43      1.43       mrg  *    documentation and/or other materials provided with the distribution.
     44      1.43       mrg  * 3. The name of the author may not be used to endorse or promote products
     45      1.43       mrg  *    derived from this software without specific prior written permission.
     46      1.43       mrg  *
     47      1.43       mrg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     48      1.43       mrg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     49      1.43       mrg  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     50      1.43       mrg  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     51      1.43       mrg  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     52      1.43       mrg  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     53      1.43       mrg  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     54      1.43       mrg  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     55      1.43       mrg  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     56      1.43       mrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     57      1.43       mrg  * SUCH DAMAGE.
     58      1.43       mrg  */
     59      1.43       mrg 
     60      1.21     lukem #include <sys/cdefs.h>
     61  1.43.4.1     rmind __KERNEL_RCSID(0, "$NetBSD: compat_util.c,v 1.43.4.1 2011/03/05 20:52:38 rmind Exp $");
     62       1.1  christos 
     63       1.1  christos #include <sys/param.h>
     64       1.1  christos #include <sys/systm.h>
     65       1.1  christos #include <sys/namei.h>
     66       1.1  christos #include <sys/proc.h>
     67       1.1  christos #include <sys/file.h>
     68       1.1  christos #include <sys/stat.h>
     69       1.1  christos #include <sys/filedesc.h>
     70       1.5   thorpej #include <sys/exec.h>
     71       1.1  christos #include <sys/ioctl.h>
     72       1.1  christos #include <sys/kernel.h>
     73       1.1  christos #include <sys/malloc.h>
     74       1.1  christos #include <sys/vnode.h>
     75       1.7      fvdl #include <sys/syslog.h>
     76       1.7      fvdl #include <sys/mount.h>
     77       1.1  christos 
     78       1.1  christos #include <compat/common/compat_util.h>
     79       1.1  christos 
     80      1.35       dsl void
     81      1.35       dsl emul_find_root(struct lwp *l, struct exec_package *epp)
     82       1.1  christos {
     83      1.42  dholland 	struct vnode *vp;
     84      1.35       dsl 	const char *emul_path;
     85      1.13  christos 
     86      1.35       dsl 	if (epp->ep_emul_root != NULL)
     87      1.35       dsl 		/* We've already found it */
     88      1.35       dsl 		return;
     89      1.35       dsl 
     90      1.35       dsl 	emul_path = epp->ep_esch->es_emul->e_path;
     91      1.35       dsl 	if (emul_path == NULL)
     92      1.35       dsl 		/* Emulation doesn't have a root */
     93      1.35       dsl 		return;
     94      1.35       dsl 
     95      1.42  dholland 	if (namei_simple_kernel(emul_path, NSM_FOLLOW_NOEMULROOT, &vp) != 0)
     96      1.35       dsl 		/* emulation root doesn't exist */
     97      1.35       dsl 		return;
     98       1.3   mycroft 
     99      1.42  dholland 	epp->ep_emul_root = vp;
    100      1.25  jdolecek }
    101      1.25  jdolecek 
    102      1.25  jdolecek /*
    103      1.25  jdolecek  * Search the alternate path for dynamic binary interpreter. If not found
    104      1.25  jdolecek  * there, check if the interpreter exists in within 'proper' tree.
    105      1.25  jdolecek  */
    106      1.25  jdolecek int
    107      1.35       dsl emul_find_interp(struct lwp *l, struct exec_package *epp, const char *itp)
    108      1.25  jdolecek {
    109      1.25  jdolecek 	int error;
    110  1.43.4.1     rmind 	struct pathbuf *pb;
    111      1.35       dsl 	struct nameidata nd;
    112      1.35       dsl 	unsigned int flags;
    113      1.25  jdolecek 
    114  1.43.4.1     rmind 	pb = pathbuf_create(itp);
    115  1.43.4.1     rmind 	if (pb == NULL) {
    116  1.43.4.1     rmind 		return ENOMEM;
    117  1.43.4.1     rmind 	}
    118  1.43.4.1     rmind 
    119      1.35       dsl 	/* If we haven't found the emulation root already, do so now */
    120      1.35       dsl 	/* Maybe we should remember failures somehow ? */
    121      1.35       dsl 	if (epp->ep_esch->es_emul->e_path != 0 && epp->ep_emul_root == NULL)
    122      1.35       dsl 		emul_find_root(l, epp);
    123      1.35       dsl 
    124      1.35       dsl 	if (epp->ep_interp != NULL)
    125      1.35       dsl 		vrele(epp->ep_interp);
    126      1.35       dsl 
    127      1.35       dsl 	/* We need to use the emulation root for the new program,
    128      1.35       dsl 	 * not the one for the current process. */
    129      1.36       dsl 	if (epp->ep_emul_root == NULL)
    130      1.36       dsl 		flags = FOLLOW;
    131      1.36       dsl 	else {
    132      1.36       dsl 		nd.ni_erootdir = epp->ep_emul_root;
    133      1.36       dsl 		/* hack: Pass in the emulation path for ktrace calls */
    134      1.36       dsl 		nd.ni_next = epp->ep_esch->es_emul->e_path;
    135      1.35       dsl 		flags = FOLLOW | TRYEMULROOT | EMULROOTSET;
    136      1.36       dsl 	}
    137      1.25  jdolecek 
    138  1.43.4.1     rmind 	NDINIT(&nd, LOOKUP, flags, pb);
    139      1.35       dsl 	error = namei(&nd);
    140      1.35       dsl 	if (error != 0) {
    141      1.35       dsl 		epp->ep_interp = NULL;
    142      1.35       dsl 		return error;
    143      1.35       dsl 	}
    144      1.28     perry 
    145      1.35       dsl 	/* Save interpreter in case we actually need to load it */
    146      1.35       dsl 	epp->ep_interp = nd.ni_vp;
    147      1.25  jdolecek 
    148  1.43.4.1     rmind 	pathbuf_destroy(pb);
    149  1.43.4.1     rmind 
    150      1.35       dsl 	return 0;
    151      1.12       cgd }
    152      1.12       cgd 
    153      1.12       cgd /*
    154      1.12       cgd  * Translate one set of flags to another, based on the entries in
    155      1.12       cgd  * the given table.  If 'leftover' is specified, it is filled in
    156      1.12       cgd  * with any flags which could not be translated.
    157      1.12       cgd  */
    158      1.12       cgd unsigned long
    159      1.12       cgd emul_flags_translate(const struct emul_flags_xtab *tab,
    160      1.12       cgd 		     unsigned long in, unsigned long *leftover)
    161      1.12       cgd {
    162      1.12       cgd 	unsigned long out;
    163      1.12       cgd 
    164      1.12       cgd 	for (out = 0; tab->omask != 0; tab++) {
    165      1.12       cgd 		if ((in & tab->omask) == tab->oval) {
    166      1.12       cgd 			in &= ~tab->omask;
    167      1.12       cgd 			out |= tab->nval;
    168      1.12       cgd 		}
    169      1.12       cgd 	}
    170      1.12       cgd 	if (leftover != NULL)
    171      1.12       cgd 		*leftover = in;
    172      1.12       cgd 	return (out);
    173       1.5   thorpej }
    174       1.5   thorpej 
    175       1.7      fvdl void
    176      1.38       dsl compat_offseterr(struct vnode *vp, const char *msg)
    177       1.7      fvdl {
    178       1.7      fvdl 	struct mount *mp;
    179       1.7      fvdl 
    180       1.7      fvdl 	mp = vp->v_mount;
    181       1.7      fvdl 
    182       1.7      fvdl 	log(LOG_ERR, "%s: dir offset too large on fs %s (mounted from %s)\n",
    183       1.7      fvdl 	    msg, mp->mnt_stat.f_mntonname, mp->mnt_stat.f_mntfromname);
    184       1.7      fvdl 	uprintf("%s: dir offset too large for emulated program\n", msg);
    185       1.1  christos }
    186      1.43       mrg 
    187      1.43       mrg /*
    188      1.43       mrg  * Look for native NetBSD compatibility libraries, usually interp-ABI.
    189      1.43       mrg  * It returns 0 if it changed the interpreter, otherwise it returns
    190      1.43       mrg  * the error from namei().  Callers should not try any more processing
    191      1.43       mrg  * if this returns 0, and probably should just ignore the return value.
    192      1.43       mrg  */
    193      1.43       mrg int
    194      1.43       mrg compat_elf_check_interp(struct exec_package *epp,
    195      1.43       mrg 			char *interp,
    196      1.43       mrg 			const char *interp_suffix)
    197      1.43       mrg {
    198      1.43       mrg 	int error = 0;
    199      1.43       mrg 
    200      1.43       mrg 	/*
    201      1.43       mrg 	 * Don't look for something else, if someone has already found and
    202      1.43       mrg 	 * setup the ep_interp already.
    203      1.43       mrg 	 */
    204      1.43       mrg 	if (interp && epp->ep_interp == NULL) {
    205      1.43       mrg 		/*
    206      1.43       mrg 		 * If the path is exactly "/usr/libexec/ld.elf_so", first
    207      1.43       mrg 		 * try to see if "/usr/libexec/ld.elf_so-<abi>" exists
    208      1.43       mrg 		 * and if so, use that instead.
    209      1.43       mrg 		 */
    210      1.43       mrg 		if (strcmp(interp, "/usr/libexec/ld.elf_so") == 0 ||
    211      1.43       mrg 		    strcmp(interp, "/libexec/ld.elf_so") == 0) {
    212      1.43       mrg 			struct vnode *vp;
    213      1.43       mrg 			char *path;
    214      1.43       mrg 
    215      1.43       mrg 			path = PNBUF_GET();
    216      1.43       mrg 			snprintf(path, MAXPATHLEN, "%s-%s", interp, interp_suffix);
    217      1.43       mrg 			error = namei_simple_kernel(path,
    218      1.43       mrg 					NSM_FOLLOW_NOEMULROOT, &vp);
    219      1.43       mrg 			/*
    220      1.43       mrg 			 * If that worked, replace interpreter in case we
    221      1.43       mrg 			 * actually need to load it.
    222      1.43       mrg 			 */
    223      1.43       mrg 			if (error == 0) {
    224      1.43       mrg 				epp->ep_interp = vp;
    225      1.43       mrg 				snprintf(interp, MAXPATHLEN, "%s", path);
    226      1.43       mrg 			}
    227      1.43       mrg 			PNBUF_PUT(path);
    228      1.43       mrg 		}
    229      1.43       mrg 	}
    230      1.43       mrg 	return error;
    231      1.43       mrg }
    232