compat_util.c revision 1.43 1 1.43 mrg /* $NetBSD: compat_util.c,v 1.43 2009/12/14 04:09:38 mrg 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 mrg __KERNEL_RCSID(0, "$NetBSD: compat_util.c,v 1.43 2009/12/14 04:09:38 mrg 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.35 dsl struct nameidata nd;
111 1.35 dsl unsigned int flags;
112 1.25 jdolecek
113 1.35 dsl /* If we haven't found the emulation root already, do so now */
114 1.35 dsl /* Maybe we should remember failures somehow ? */
115 1.35 dsl if (epp->ep_esch->es_emul->e_path != 0 && epp->ep_emul_root == NULL)
116 1.35 dsl emul_find_root(l, epp);
117 1.35 dsl
118 1.35 dsl if (epp->ep_interp != NULL)
119 1.35 dsl vrele(epp->ep_interp);
120 1.35 dsl
121 1.35 dsl /* We need to use the emulation root for the new program,
122 1.35 dsl * not the one for the current process. */
123 1.36 dsl if (epp->ep_emul_root == NULL)
124 1.36 dsl flags = FOLLOW;
125 1.36 dsl else {
126 1.36 dsl nd.ni_erootdir = epp->ep_emul_root;
127 1.36 dsl /* hack: Pass in the emulation path for ktrace calls */
128 1.36 dsl nd.ni_next = epp->ep_esch->es_emul->e_path;
129 1.35 dsl flags = FOLLOW | TRYEMULROOT | EMULROOTSET;
130 1.36 dsl }
131 1.25 jdolecek
132 1.39 pooka NDINIT(&nd, LOOKUP, flags, UIO_SYSSPACE, itp);
133 1.35 dsl error = namei(&nd);
134 1.35 dsl if (error != 0) {
135 1.35 dsl epp->ep_interp = NULL;
136 1.35 dsl return error;
137 1.35 dsl }
138 1.28 perry
139 1.35 dsl /* Save interpreter in case we actually need to load it */
140 1.35 dsl epp->ep_interp = nd.ni_vp;
141 1.25 jdolecek
142 1.35 dsl return 0;
143 1.12 cgd }
144 1.12 cgd
145 1.12 cgd /*
146 1.12 cgd * Translate one set of flags to another, based on the entries in
147 1.12 cgd * the given table. If 'leftover' is specified, it is filled in
148 1.12 cgd * with any flags which could not be translated.
149 1.12 cgd */
150 1.12 cgd unsigned long
151 1.12 cgd emul_flags_translate(const struct emul_flags_xtab *tab,
152 1.12 cgd unsigned long in, unsigned long *leftover)
153 1.12 cgd {
154 1.12 cgd unsigned long out;
155 1.12 cgd
156 1.12 cgd for (out = 0; tab->omask != 0; tab++) {
157 1.12 cgd if ((in & tab->omask) == tab->oval) {
158 1.12 cgd in &= ~tab->omask;
159 1.12 cgd out |= tab->nval;
160 1.12 cgd }
161 1.12 cgd }
162 1.12 cgd if (leftover != NULL)
163 1.12 cgd *leftover = in;
164 1.12 cgd return (out);
165 1.5 thorpej }
166 1.5 thorpej
167 1.7 fvdl void
168 1.38 dsl compat_offseterr(struct vnode *vp, const char *msg)
169 1.7 fvdl {
170 1.7 fvdl struct mount *mp;
171 1.7 fvdl
172 1.7 fvdl mp = vp->v_mount;
173 1.7 fvdl
174 1.7 fvdl log(LOG_ERR, "%s: dir offset too large on fs %s (mounted from %s)\n",
175 1.7 fvdl msg, mp->mnt_stat.f_mntonname, mp->mnt_stat.f_mntfromname);
176 1.7 fvdl uprintf("%s: dir offset too large for emulated program\n", msg);
177 1.1 christos }
178 1.43 mrg
179 1.43 mrg /*
180 1.43 mrg * Look for native NetBSD compatibility libraries, usually interp-ABI.
181 1.43 mrg * It returns 0 if it changed the interpreter, otherwise it returns
182 1.43 mrg * the error from namei(). Callers should not try any more processing
183 1.43 mrg * if this returns 0, and probably should just ignore the return value.
184 1.43 mrg */
185 1.43 mrg int
186 1.43 mrg compat_elf_check_interp(struct exec_package *epp,
187 1.43 mrg char *interp,
188 1.43 mrg const char *interp_suffix)
189 1.43 mrg {
190 1.43 mrg int error = 0;
191 1.43 mrg
192 1.43 mrg /*
193 1.43 mrg * Don't look for something else, if someone has already found and
194 1.43 mrg * setup the ep_interp already.
195 1.43 mrg */
196 1.43 mrg if (interp && epp->ep_interp == NULL) {
197 1.43 mrg /*
198 1.43 mrg * If the path is exactly "/usr/libexec/ld.elf_so", first
199 1.43 mrg * try to see if "/usr/libexec/ld.elf_so-<abi>" exists
200 1.43 mrg * and if so, use that instead.
201 1.43 mrg */
202 1.43 mrg if (strcmp(interp, "/usr/libexec/ld.elf_so") == 0 ||
203 1.43 mrg strcmp(interp, "/libexec/ld.elf_so") == 0) {
204 1.43 mrg struct vnode *vp;
205 1.43 mrg char *path;
206 1.43 mrg
207 1.43 mrg path = PNBUF_GET();
208 1.43 mrg snprintf(path, MAXPATHLEN, "%s-%s", interp, interp_suffix);
209 1.43 mrg error = namei_simple_kernel(path,
210 1.43 mrg NSM_FOLLOW_NOEMULROOT, &vp);
211 1.43 mrg /*
212 1.43 mrg * If that worked, replace interpreter in case we
213 1.43 mrg * actually need to load it.
214 1.43 mrg */
215 1.43 mrg if (error == 0) {
216 1.43 mrg epp->ep_interp = vp;
217 1.43 mrg snprintf(interp, MAXPATHLEN, "%s", path);
218 1.43 mrg }
219 1.43 mrg PNBUF_PUT(path);
220 1.43 mrg }
221 1.43 mrg }
222 1.43 mrg return error;
223 1.43 mrg }
224