xref: /netbsd-src/sys/compat/common/compat_util.c (revision b5677b36047b601b9addaaa494a58ceae82c2a6c)
1 /* 	$NetBSD: compat_util.c,v 1.41 2008/04/28 20:23:41 martin 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: compat_util.c,v 1.41 2008/04/28 20:23:41 martin Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/namei.h>
38 #include <sys/proc.h>
39 #include <sys/file.h>
40 #include <sys/stat.h>
41 #include <sys/filedesc.h>
42 #include <sys/exec.h>
43 #include <sys/ioctl.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/vnode.h>
47 #include <sys/syslog.h>
48 #include <sys/mount.h>
49 
50 #include <compat/common/compat_util.h>
51 
52 void
53 emul_find_root(struct lwp *l, struct exec_package *epp)
54 {
55 	struct nameidata nd;
56 	const char *emul_path;
57 
58 	if (epp->ep_emul_root != NULL)
59 		/* We've already found it */
60 		return;
61 
62 	emul_path = epp->ep_esch->es_emul->e_path;
63 	if (emul_path == NULL)
64 		/* Emulation doesn't have a root */
65 		return;
66 
67 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, emul_path);
68 	if (namei(&nd) != 0)
69 		/* emulation root doesn't exist */
70 		return;
71 
72 	epp->ep_emul_root = nd.ni_vp;
73 }
74 
75 /*
76  * Search the alternate path for dynamic binary interpreter. If not found
77  * there, check if the interpreter exists in within 'proper' tree.
78  */
79 int
80 emul_find_interp(struct lwp *l, struct exec_package *epp, const char *itp)
81 {
82 	int error;
83 	struct nameidata nd;
84 	unsigned int flags;
85 
86 	/* If we haven't found the emulation root already, do so now */
87 	/* Maybe we should remember failures somehow ? */
88 	if (epp->ep_esch->es_emul->e_path != 0 && epp->ep_emul_root == NULL)
89 		emul_find_root(l, epp);
90 
91 	if (epp->ep_interp != NULL)
92 		vrele(epp->ep_interp);
93 
94 	/* We need to use the emulation root for the new program,
95 	 * not the one for the current process. */
96 	if (epp->ep_emul_root == NULL)
97 		flags = FOLLOW;
98 	else {
99 		nd.ni_erootdir = epp->ep_emul_root;
100 		/* hack: Pass in the emulation path for ktrace calls */
101 		nd.ni_next = epp->ep_esch->es_emul->e_path;
102 		flags = FOLLOW | TRYEMULROOT | EMULROOTSET;
103 	}
104 
105 	NDINIT(&nd, LOOKUP, flags, UIO_SYSSPACE, itp);
106 	error = namei(&nd);
107 	if (error != 0) {
108 		epp->ep_interp = NULL;
109 		return error;
110 	}
111 
112 	/* Save interpreter in case we actually need to load it */
113 	epp->ep_interp = nd.ni_vp;
114 
115 	return 0;
116 }
117 
118 /*
119  * Translate one set of flags to another, based on the entries in
120  * the given table.  If 'leftover' is specified, it is filled in
121  * with any flags which could not be translated.
122  */
123 unsigned long
124 emul_flags_translate(const struct emul_flags_xtab *tab,
125 		     unsigned long in, unsigned long *leftover)
126 {
127 	unsigned long out;
128 
129 	for (out = 0; tab->omask != 0; tab++) {
130 		if ((in & tab->omask) == tab->oval) {
131 			in &= ~tab->omask;
132 			out |= tab->nval;
133 		}
134 	}
135 	if (leftover != NULL)
136 		*leftover = in;
137 	return (out);
138 }
139 
140 void
141 compat_offseterr(struct vnode *vp, const char *msg)
142 {
143 	struct mount *mp;
144 
145 	mp = vp->v_mount;
146 
147 	log(LOG_ERR, "%s: dir offset too large on fs %s (mounted from %s)\n",
148 	    msg, mp->mnt_stat.f_mntonname, mp->mnt_stat.f_mntfromname);
149 	uprintf("%s: dir offset too large for emulated program\n", msg);
150 }
151