compat_util.c revision 1.19 1 /* $NetBSD: compat_util.c,v 1.19 2001/01/22 19:50:56 jdolecek 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/param.h>
40 #include <sys/systm.h>
41 #include <sys/namei.h>
42 #include <sys/proc.h>
43 #include <sys/file.h>
44 #include <sys/stat.h>
45 #include <sys/filedesc.h>
46 #include <sys/exec.h>
47 #include <sys/ioctl.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/vnode.h>
51 #include <sys/syslog.h>
52 #include <sys/mount.h>
53
54
55 #include <compat/common/compat_util.h>
56
57 /*
58 * Search an alternate path before passing pathname arguments on
59 * to system calls. Useful for keeping a separate 'emulation tree'.
60 *
61 * According to sflag, we either check for existance of the file or if
62 * it can be created or if the file is symlink.
63 *
64 * In case of success, emul_find returns 0:
65 * If sgp is provided, the path is in user space, and pbuf gets
66 * allocated in user space (in the stackgap). Otherwise the path
67 * is already in kernel space and a kernel buffer gets allocated
68 * and returned in pbuf, that must be freed by the user.
69 * In case of error, the error number is returned and *pbuf = path.
70 */
71 int
72 emul_find(p, sgp, prefix, path, pbuf, sflag)
73 struct proc *p;
74 caddr_t *sgp; /* Pointer to stackgap memory */
75 const char *prefix;
76 const char *path;
77 const char **pbuf;
78 int sflag;
79 {
80 struct nameidata nd;
81 struct nameidata ndroot;
82 struct vattr vat;
83 struct vattr vatroot;
84 int error;
85 char *ptr, *buf, *cp;
86 const char *pr;
87 size_t sz, len;
88
89 buf = (char *)malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
90 *pbuf = path;
91
92 for (ptr = buf, pr = prefix; (*ptr = *pr) != '\0'; ptr++, pr++)
93 continue;
94
95 sz = MAXPATHLEN - (ptr - buf);
96
97 /*
98 * If sgp is not given then the path is already in kernel space
99 */
100 if (sgp == NULL)
101 error = copystr(path, ptr, sz, &len);
102 else
103 error = copyinstr(path, ptr, sz, &len);
104
105 if (error)
106 goto bad;
107
108 if (*ptr != '/') {
109 error = EINVAL;
110 goto bad;
111 }
112
113 /*
114 * We provide an escape method, so that the user can
115 * always specify the real root. If the path is prefixed
116 * by /../ we kill the alternate search
117 */
118 if (ptr[1] == '.' && ptr[2] == '.' && ptr[3] == '/') {
119 len -= 3;
120 (void)memcpy(buf, &ptr[3], len);
121 ptr = buf;
122 goto good;
123 }
124
125 /*
126 * We know that there is a / somewhere in this pathname.
127 * Search backwards for it, to find the file's parent dir
128 * to see if it exists in the alternate tree. If it does,
129 * and we want to create a file (sflag is set). We don't
130 * need to worry about the root comparison in this case.
131 */
132
133 switch (sflag) {
134 case CHECK_ALT_FL_CREAT:
135 for (cp = &ptr[len] - 1; *cp != '/'; cp--)
136 ;
137 *cp = '\0';
138
139 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, p);
140
141 if ((error = namei(&nd)) != 0)
142 goto bad;
143
144 *cp = '/';
145 break;
146 case CHECK_ALT_FL_EXISTS:
147 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, p);
148
149 if ((error = namei(&nd)) != 0)
150 goto bad;
151
152 /*
153 * We now compare the vnode of the emulation root to the one
154 * vnode asked. If they resolve to be the same, then we
155 * ignore the match so that the real root gets used.
156 * This avoids the problem of traversing "../.." to find the
157 * root directory and never finding it, because "/" resolves
158 * to the emulation root directory. This is expensive :-(
159 */
160 NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, prefix, p);
161
162 if ((error = namei(&ndroot)) != 0)
163 goto bad2;
164
165 if ((error = VOP_GETATTR(nd.ni_vp, &vat, p->p_ucred, p)) != 0)
166 goto bad3;
167
168 if ((error = VOP_GETATTR(ndroot.ni_vp, &vatroot, p->p_ucred, p))
169 != 0)
170 goto bad3;
171
172 if (vat.va_fsid == vatroot.va_fsid &&
173 vat.va_fileid == vatroot.va_fileid) {
174 error = ENOENT;
175 goto bad3;
176 }
177
178 break;
179
180 case CHECK_ALT_FL_SYMLINK:
181 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, buf, p);
182
183 if ((error = namei(&nd)) != 0)
184 goto bad;
185
186 break;
187 }
188
189 vrele(nd.ni_vp);
190 if (sflag == CHECK_ALT_FL_EXISTS)
191 vrele(ndroot.ni_vp);
192
193 good:
194 if (sgp == NULL)
195 *pbuf = buf;
196 else {
197 sz = &ptr[len] - buf;
198 *pbuf = stackgap_alloc(sgp, sz + 1);
199 if (*pbuf == NULL) {
200 error = ENAMETOOLONG;
201 goto bad;
202 }
203 if ((error = copyout(buf, (void *)*pbuf, sz)) != 0) {
204 *pbuf = path;
205 goto bad;
206 }
207 free(buf, M_TEMP);
208 }
209 return 0;
210
211 bad3:
212 vrele(ndroot.ni_vp);
213 bad2:
214 vrele(nd.ni_vp);
215 bad:
216 free(buf, M_TEMP);
217 return error;
218 }
219
220 /*
221 * Translate one set of flags to another, based on the entries in
222 * the given table. If 'leftover' is specified, it is filled in
223 * with any flags which could not be translated.
224 */
225 unsigned long
226 emul_flags_translate(const struct emul_flags_xtab *tab,
227 unsigned long in, unsigned long *leftover)
228 {
229 unsigned long out;
230
231 for (out = 0; tab->omask != 0; tab++) {
232 if ((in & tab->omask) == tab->oval) {
233 in &= ~tab->omask;
234 out |= tab->nval;
235 }
236 }
237 if (leftover != NULL)
238 *leftover = in;
239 return (out);
240 }
241
242 caddr_t
243 stackgap_init(e)
244 const struct emul *e;
245 {
246 struct proc *p = curproc; /* XXX */
247
248 #define szsigcode ((caddr_t)(e->e_esigcode - e->e_sigcode))
249 return (caddr_t)(((unsigned long)p->p_psstr - (unsigned long)szsigcode
250 - STACKGAPLEN) & ~ALIGNBYTES);
251 #undef szsigcode
252 }
253
254
255 void *
256 stackgap_alloc(sgp, sz)
257 caddr_t *sgp;
258 size_t sz;
259 {
260 void *n = (void *) *sgp;
261 caddr_t nsgp;
262 struct proc *p = curproc; /* XXX */
263 const struct emul *e = p->p_emul;
264 int sigsize = e->e_esigcode - e->e_sigcode;
265
266 sz = ALIGN(sz);
267 nsgp = *sgp + sz;
268 if (nsgp > (((caddr_t)p->p_psstr) - sigsize))
269 return NULL;
270 *sgp = nsgp;
271 return n;
272 }
273
274 void
275 compat_offseterr(vp, msg)
276 struct vnode *vp;
277 char *msg;
278 {
279 struct mount *mp;
280
281 mp = vp->v_mount;
282
283 log(LOG_ERR, "%s: dir offset too large on fs %s (mounted from %s)\n",
284 msg, mp->mnt_stat.f_mntonname, mp->mnt_stat.f_mntfromname);
285 uprintf("%s: dir offset too large for emulated program\n", msg);
286 }
287