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