compat_util.c revision 1.5 1 1.5 thorpej /* $NetBSD: compat_util.c,v 1.5 1996/10/12 02:12:55 thorpej 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.1 christos
45 1.5 thorpej #include <vm/vm_param.h>
46 1.5 thorpej
47 1.1 christos #include <compat/common/compat_util.h>
48 1.1 christos
49 1.1 christos /*
50 1.1 christos * Search an alternate path before passing pathname arguments on
51 1.1 christos * to system calls. Useful for keeping a separate 'emulation tree'.
52 1.1 christos *
53 1.1 christos * If cflag is set, we check if an attempt can be made to create
54 1.1 christos * the named file, i.e. we check if the directory it should
55 1.1 christos * be in exists.
56 1.1 christos */
57 1.1 christos int
58 1.1 christos emul_find(p, sgp, prefix, path, pbuf, cflag)
59 1.1 christos struct proc *p;
60 1.1 christos caddr_t *sgp; /* Pointer to stackgap memory */
61 1.1 christos const char *prefix;
62 1.1 christos char *path;
63 1.1 christos char **pbuf;
64 1.1 christos int cflag;
65 1.1 christos {
66 1.1 christos struct nameidata nd;
67 1.1 christos struct nameidata ndroot;
68 1.1 christos struct vattr vat;
69 1.1 christos struct vattr vatroot;
70 1.1 christos int error;
71 1.1 christos char *ptr, *buf, *cp;
72 1.2 christos const char *pr;
73 1.1 christos size_t sz, len;
74 1.1 christos
75 1.1 christos buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
76 1.1 christos *pbuf = path;
77 1.1 christos
78 1.2 christos for (ptr = buf, pr = prefix; (*ptr = *pr) != '\0'; ptr++, pr++)
79 1.1 christos continue;
80 1.1 christos
81 1.1 christos sz = MAXPATHLEN - (ptr - buf);
82 1.1 christos
83 1.1 christos /*
84 1.1 christos * If sgp is not given then the path is already in kernel space
85 1.1 christos */
86 1.1 christos if (sgp == NULL)
87 1.1 christos error = copystr(path, ptr, sz, &len);
88 1.1 christos else
89 1.1 christos error = copyinstr(path, ptr, sz, &len);
90 1.1 christos
91 1.3 mycroft if (error)
92 1.3 mycroft goto bad;
93 1.1 christos
94 1.1 christos if (*ptr != '/') {
95 1.3 mycroft error = EINVAL;
96 1.3 mycroft goto bad;
97 1.1 christos }
98 1.1 christos
99 1.1 christos /*
100 1.1 christos * We know that there is a / somewhere in this pathname.
101 1.1 christos * Search backwards for it, to find the file's parent dir
102 1.1 christos * to see if it exists in the alternate tree. If it does,
103 1.1 christos * and we want to create a file (cflag is set). We don't
104 1.1 christos * need to worry about the root comparison in this case.
105 1.1 christos */
106 1.1 christos
107 1.1 christos if (cflag) {
108 1.3 mycroft for (cp = &ptr[len] - 1; *cp != '/'; cp--)
109 1.3 mycroft ;
110 1.1 christos *cp = '\0';
111 1.1 christos
112 1.1 christos NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, p);
113 1.1 christos
114 1.3 mycroft if ((error = namei(&nd)) != 0)
115 1.3 mycroft goto bad;
116 1.1 christos
117 1.1 christos *cp = '/';
118 1.1 christos }
119 1.1 christos else {
120 1.1 christos NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, p);
121 1.1 christos
122 1.3 mycroft if ((error = namei(&nd)) != 0)
123 1.3 mycroft goto bad;
124 1.1 christos
125 1.1 christos /*
126 1.1 christos * We now compare the vnode of the emulation root to the one
127 1.1 christos * vnode asked. If they resolve to be the same, then we
128 1.1 christos * ignore the match so that the real root gets used.
129 1.1 christos * This avoids the problem of traversing "../.." to find the
130 1.1 christos * root directory and never finding it, because "/" resolves
131 1.1 christos * to the emulation root directory. This is expensive :-(
132 1.1 christos */
133 1.1 christos /* XXX: prototype should have const here for NDINIT */
134 1.1 christos NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE,
135 1.1 christos (char *) 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.1 christos }
197