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