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