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