compat_util.c revision 1.8 1 1.8 christos /* $NetBSD: compat_util.c,v 1.8 1998/09/05 14:50:25 christos 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.8 christos * by Christos Zoulas.
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.8 christos */
38 1.1 christos
39 1.1 christos /*
40 1.1 christos * Copyright (c) 1995 Frank van der Linden
41 1.1 christos * All rights reserved.
42 1.1 christos *
43 1.1 christos * Redistribution and use in source and binary forms, with or without
44 1.1 christos * modification, are permitted provided that the following conditions
45 1.1 christos * are met:
46 1.1 christos * 1. Redistributions of source code must retain the above copyright
47 1.1 christos * notice, this list of conditions and the following disclaimer.
48 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
49 1.1 christos * notice, this list of conditions and the following disclaimer in the
50 1.1 christos * documentation and/or other materials provided with the distribution.
51 1.1 christos * 3. The name of the author may not be used to endorse or promote products
52 1.1 christos * derived from this software without specific prior written permission
53 1.1 christos *
54 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
55 1.1 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
56 1.1 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
57 1.1 christos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
58 1.1 christos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
59 1.1 christos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
60 1.1 christos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
61 1.1 christos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
62 1.1 christos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
63 1.1 christos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64 1.1 christos *
65 1.1 christos */
66 1.1 christos
67 1.1 christos #include <sys/param.h>
68 1.1 christos #include <sys/systm.h>
69 1.1 christos #include <sys/namei.h>
70 1.1 christos #include <sys/proc.h>
71 1.1 christos #include <sys/file.h>
72 1.1 christos #include <sys/stat.h>
73 1.1 christos #include <sys/filedesc.h>
74 1.5 thorpej #include <sys/exec.h>
75 1.1 christos #include <sys/ioctl.h>
76 1.1 christos #include <sys/kernel.h>
77 1.1 christos #include <sys/malloc.h>
78 1.1 christos #include <sys/vnode.h>
79 1.7 fvdl #include <sys/syslog.h>
80 1.7 fvdl #include <sys/mount.h>
81 1.1 christos
82 1.5 thorpej #include <vm/vm_param.h>
83 1.5 thorpej
84 1.1 christos #include <compat/common/compat_util.h>
85 1.1 christos
86 1.1 christos /*
87 1.1 christos * Search an alternate path before passing pathname arguments on
88 1.1 christos * to system calls. Useful for keeping a separate 'emulation tree'.
89 1.1 christos *
90 1.1 christos * If cflag is set, we check if an attempt can be made to create
91 1.1 christos * the named file, i.e. we check if the directory it should
92 1.1 christos * be in exists.
93 1.1 christos */
94 1.1 christos int
95 1.1 christos emul_find(p, sgp, prefix, path, pbuf, cflag)
96 1.1 christos struct proc *p;
97 1.1 christos caddr_t *sgp; /* Pointer to stackgap memory */
98 1.1 christos const char *prefix;
99 1.1 christos char *path;
100 1.1 christos char **pbuf;
101 1.1 christos int cflag;
102 1.1 christos {
103 1.1 christos struct nameidata nd;
104 1.1 christos struct nameidata ndroot;
105 1.1 christos struct vattr vat;
106 1.1 christos struct vattr vatroot;
107 1.1 christos int error;
108 1.1 christos char *ptr, *buf, *cp;
109 1.2 christos const char *pr;
110 1.1 christos size_t sz, len;
111 1.1 christos
112 1.1 christos buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
113 1.1 christos *pbuf = path;
114 1.1 christos
115 1.2 christos for (ptr = buf, pr = prefix; (*ptr = *pr) != '\0'; ptr++, pr++)
116 1.1 christos continue;
117 1.1 christos
118 1.1 christos sz = MAXPATHLEN - (ptr - buf);
119 1.1 christos
120 1.1 christos /*
121 1.1 christos * If sgp is not given then the path is already in kernel space
122 1.1 christos */
123 1.1 christos if (sgp == NULL)
124 1.1 christos error = copystr(path, ptr, sz, &len);
125 1.1 christos else
126 1.1 christos error = copyinstr(path, ptr, sz, &len);
127 1.1 christos
128 1.3 mycroft if (error)
129 1.3 mycroft goto bad;
130 1.1 christos
131 1.1 christos if (*ptr != '/') {
132 1.3 mycroft error = EINVAL;
133 1.3 mycroft goto bad;
134 1.1 christos }
135 1.1 christos
136 1.1 christos /*
137 1.1 christos * We know that there is a / somewhere in this pathname.
138 1.1 christos * Search backwards for it, to find the file's parent dir
139 1.1 christos * to see if it exists in the alternate tree. If it does,
140 1.1 christos * and we want to create a file (cflag is set). We don't
141 1.1 christos * need to worry about the root comparison in this case.
142 1.1 christos */
143 1.1 christos
144 1.1 christos if (cflag) {
145 1.3 mycroft for (cp = &ptr[len] - 1; *cp != '/'; cp--)
146 1.3 mycroft ;
147 1.1 christos *cp = '\0';
148 1.1 christos
149 1.1 christos NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, p);
150 1.1 christos
151 1.3 mycroft if ((error = namei(&nd)) != 0)
152 1.3 mycroft goto bad;
153 1.1 christos
154 1.1 christos *cp = '/';
155 1.1 christos }
156 1.1 christos else {
157 1.1 christos NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, p);
158 1.1 christos
159 1.3 mycroft if ((error = namei(&nd)) != 0)
160 1.3 mycroft goto bad;
161 1.1 christos
162 1.1 christos /*
163 1.1 christos * We now compare the vnode of the emulation root to the one
164 1.1 christos * vnode asked. If they resolve to be the same, then we
165 1.1 christos * ignore the match so that the real root gets used.
166 1.1 christos * This avoids the problem of traversing "../.." to find the
167 1.1 christos * root directory and never finding it, because "/" resolves
168 1.1 christos * to the emulation root directory. This is expensive :-(
169 1.1 christos */
170 1.6 cgd NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, prefix, p);
171 1.1 christos
172 1.3 mycroft if ((error = namei(&ndroot)) != 0)
173 1.3 mycroft goto bad2;
174 1.1 christos
175 1.3 mycroft if ((error = VOP_GETATTR(nd.ni_vp, &vat, p->p_ucred, p)) != 0)
176 1.3 mycroft goto bad3;
177 1.1 christos
178 1.1 christos if ((error = VOP_GETATTR(ndroot.ni_vp, &vatroot, p->p_ucred, p))
179 1.3 mycroft != 0)
180 1.3 mycroft goto bad3;
181 1.1 christos
182 1.1 christos if (vat.va_fsid == vatroot.va_fsid &&
183 1.1 christos vat.va_fileid == vatroot.va_fileid) {
184 1.1 christos error = ENOENT;
185 1.3 mycroft goto bad3;
186 1.1 christos }
187 1.1 christos }
188 1.1 christos if (sgp == NULL)
189 1.1 christos *pbuf = buf;
190 1.1 christos else {
191 1.1 christos sz = &ptr[len] - buf;
192 1.1 christos *pbuf = stackgap_alloc(sgp, sz + 1);
193 1.1 christos error = copyout(buf, *pbuf, sz);
194 1.1 christos free(buf, M_TEMP);
195 1.1 christos }
196 1.1 christos
197 1.1 christos vrele(nd.ni_vp);
198 1.1 christos if (!cflag)
199 1.1 christos vrele(ndroot.ni_vp);
200 1.3 mycroft return error;
201 1.3 mycroft
202 1.3 mycroft bad3:
203 1.3 mycroft vrele(ndroot.ni_vp);
204 1.3 mycroft bad2:
205 1.3 mycroft vrele(nd.ni_vp);
206 1.3 mycroft bad:
207 1.3 mycroft free(buf, M_TEMP);
208 1.1 christos return error;
209 1.5 thorpej }
210 1.5 thorpej
211 1.5 thorpej caddr_t
212 1.5 thorpej stackgap_init(e)
213 1.5 thorpej struct emul *e;
214 1.5 thorpej {
215 1.5 thorpej
216 1.5 thorpej #define szsigcode ((caddr_t)(e->e_esigcode - e->e_sigcode))
217 1.5 thorpej return STACKGAPBASE;
218 1.5 thorpej #undef szsigcode
219 1.5 thorpej }
220 1.5 thorpej
221 1.5 thorpej
222 1.5 thorpej void *
223 1.5 thorpej stackgap_alloc(sgp, sz)
224 1.5 thorpej caddr_t *sgp;
225 1.5 thorpej size_t sz;
226 1.5 thorpej {
227 1.5 thorpej void *p = (void *) *sgp;
228 1.5 thorpej
229 1.5 thorpej *sgp += ALIGN(sz);
230 1.5 thorpej return p;
231 1.7 fvdl }
232 1.7 fvdl
233 1.7 fvdl void
234 1.7 fvdl compat_offseterr(vp, msg)
235 1.7 fvdl struct vnode *vp;
236 1.7 fvdl char *msg;
237 1.7 fvdl {
238 1.7 fvdl struct mount *mp;
239 1.7 fvdl
240 1.7 fvdl mp = vp->v_mount;
241 1.7 fvdl
242 1.7 fvdl log(LOG_ERR, "%s: dir offset too large on fs %s (mounted from %s)\n",
243 1.7 fvdl msg, mp->mnt_stat.f_mntonname, mp->mnt_stat.f_mntfromname);
244 1.7 fvdl uprintf("%s: dir offset too large for emulated program\n", msg);
245 1.1 christos }
246