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