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