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.1/sys/compat/linux/linux_util.c 111119 2003-02-19 05:47:46Z imp $
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 <machine/stdarg.h>
41
42 #include <compat/linux/linux_util.h>
43
44 const char linux_emul_path[] = "/compat/linux";
45
46 /*
47 * Search an alternate path before passing pathname arguments on
48 * to system calls. Useful for keeping a separate 'emulation tree'.
49 *
50 * If cflag is set, we check if an attempt can be made to create
51 * the named file, i.e. we check if the directory it should
52 * be in exists.
53 */
54 int
55 linux_emul_find(td, sgp, path, pbuf, cflag)
56 struct thread *td;
57 caddr_t *sgp; /* Pointer to stackgap memory */
58 char *path;
59 char **pbuf;
60 int cflag;
61 {
62 char *newpath;
63 size_t sz;
64 int error;
65
66 error = linux_emul_convpath(td, path, (sgp == NULL) ? UIO_SYSSPACE :
67 UIO_USERSPACE, &newpath, cflag);
68 if (newpath == NULL)
69 return (error);
70
71 if (sgp == NULL) {
72 *pbuf = newpath;
73 return (error);
74 }
75
76 sz = strlen(newpath);
77 *pbuf = stackgap_alloc(sgp, sz + 1);
78 if (*pbuf != NULL)
79 error = copyout(newpath, *pbuf, sz + 1);
80 else
81 error = ENAMETOOLONG;
82 free(newpath, M_TEMP);
83
84 return (error);
85 }
86
87 int
88 linux_emul_convpath(td, path, pathseg, pbuf, cflag)
89 struct thread *td;
90 char *path;
91 enum uio_seg pathseg;
92 char **pbuf;
93 int cflag;
94 {
95 struct nameidata nd;
96 struct nameidata ndroot;
97 struct vattr vat;
98 struct vattr vatroot;
99 int error;
100 const char *prefix;
101 char *ptr, *buf, *cp;
102 size_t len, sz;
103
104 buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
105 *pbuf = buf;
106
107 prefix = linux_emul_path;
108 for (ptr = buf; (*ptr = *prefix) != '\0'; ptr++, prefix++)
109 continue;
110 sz = MAXPATHLEN - (ptr - buf);
111
112 if (pathseg == UIO_SYSSPACE)
113 error = copystr(path, ptr, sz, &len);
114 else
115 error = copyinstr(path, ptr, sz, &len);
116
117 if (error) {
118 *pbuf = NULL;
119 free(buf, M_TEMP);
120 return error;
121 }
122
123 if (*ptr != '/') {
124 error = EINVAL;
125 goto keeporig;
126 }
127
128 /*
129 * We know that there is a / somewhere in this pathname.
130 * Search backwards for it, to find the file's parent dir
131 * to see if it exists in the alternate tree. If it does,
132 * and we want to create a file (cflag is set). We don't
133 * need to worry about the root comparison in this case.
134 */
135
136 if (cflag) {
137 for (cp = &ptr[len] - 1; *cp != '/'; cp--);
138 *cp = '\0';
139
140 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, td);
141 error = namei(&nd);
142 *cp = '/';
143 if (error != 0)
144 goto keeporig;
145 }
146 else {
147 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, td);
148
149 if ((error = namei(&nd)) != 0)
150 goto keeporig;
151
152 /*
153 * We now compare the vnode of the linux_root to the one
154 * vnode asked. If they resolve to be the same, then we
155 * ignore the match so that the real root gets used.
156 * This avoids the problem of traversing "../.." to find the
157 * root directory and never finding it, because "/" resolves
158 * to the emulation root directory. This is expensive :-(
159 */
160 NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, linux_emul_path,
161 td);
162
163 if ((error = namei(&ndroot)) != 0) {
164 /* Cannot happen! */
165 NDFREE(&nd, NDF_ONLY_PNBUF);
166 vrele(nd.ni_vp);
167 goto keeporig;
168 }
169
170 if ((error = VOP_GETATTR(nd.ni_vp, &vat, td->td_ucred, td)) != 0) {
171 goto bad;
172 }
173
174 if ((error = VOP_GETATTR(ndroot.ni_vp, &vatroot, td->td_ucred, td))
175 != 0) {
176 goto bad;
177 }
178
179 if (vat.va_fsid == vatroot.va_fsid &&
180 vat.va_fileid == vatroot.va_fileid) {
181 error = ENOENT;
182 goto bad;
183 }
184
185 }
186
187 NDFREE(&nd, NDF_ONLY_PNBUF);
188 vrele(nd.ni_vp);
189 if (!cflag) {
190 NDFREE(&ndroot, NDF_ONLY_PNBUF);
191 vrele(ndroot.ni_vp);
192 }
193 return error;
194
195 bad:
196 NDFREE(&ndroot, NDF_ONLY_PNBUF);
197 vrele(ndroot.ni_vp);
198 NDFREE(&nd, NDF_ONLY_PNBUF);
199 vrele(nd.ni_vp);
200 keeporig:
201 /* Keep the original path; copy it back to the start of the buffer. */
202 bcopy(ptr, buf, len);
203 return error;
204 }
205
206 void
207 linux_msg(const struct thread *td, const char *fmt, ...)
208 {
209 va_list ap;
210 struct proc *p;
211
212 p = td->td_proc;
213 printf("linux: pid %d (%s): ", (int)p->p_pid, p->p_comm);
214 va_start(ap, fmt);
215 vprintf(fmt, ap);
216 va_end(ap);
217 printf("\n");
218 }
Cache object: 2d8b7e095d9d345dbb2d23ef4b2712f7
|