linux_util.c revision 124537
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: head/sys/compat/linux/linux_util.c 124537 2004-01-14 22:38:03Z truckman $");
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/namei.h>
38#include <sys/proc.h>
39#include <sys/malloc.h>
40#include <sys/vnode.h>
41
42#include <machine/stdarg.h>
43
44#include <compat/linux/linux_util.h>
45
46const char      linux_emul_path[] = "/compat/linux";
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 */
56int
57linux_emul_find(td, sgp, path, pbuf, cflag)
58	struct thread	 *td;
59	caddr_t		 *sgp;		/* Pointer to stackgap memory */
60	char		 *path;
61	char		**pbuf;
62	int		  cflag;
63{
64	char *newpath;
65	size_t sz;
66	int error;
67
68	error = linux_emul_convpath(td, path, (sgp == NULL) ? UIO_SYSSPACE :
69	    UIO_USERSPACE, &newpath, cflag);
70	if (newpath == NULL)
71		return (error);
72
73	if (sgp == NULL) {
74		*pbuf = newpath;
75		return (error);
76	}
77
78	sz = strlen(newpath);
79	*pbuf = stackgap_alloc(sgp, sz + 1);
80	if (*pbuf != NULL)
81		error = copyout(newpath, *pbuf, sz + 1);
82	else
83		error = ENAMETOOLONG;
84	free(newpath, M_TEMP);
85
86	return (error);
87}
88
89int
90linux_emul_convpath(td, path, pathseg, pbuf, cflag)
91	struct thread	 *td;
92	char		 *path;
93	enum uio_seg	  pathseg;
94	char		**pbuf;
95	int		  cflag;
96{
97	struct nameidata	 nd;
98	struct nameidata	 ndroot;
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 (nd.ni_vp == ndroot.ni_vp) {
171			error = ENOENT;
172			goto bad;
173		}
174
175	}
176
177	NDFREE(&nd, NDF_ONLY_PNBUF);
178	vrele(nd.ni_vp);
179	if (!cflag) {
180		NDFREE(&ndroot, NDF_ONLY_PNBUF);
181		vrele(ndroot.ni_vp);
182	}
183	return error;
184
185bad:
186	NDFREE(&ndroot, NDF_ONLY_PNBUF);
187	vrele(ndroot.ni_vp);
188	NDFREE(&nd, NDF_ONLY_PNBUF);
189	vrele(nd.ni_vp);
190keeporig:
191	/* Keep the original path; copy it back to the start of the buffer. */
192	bcopy(ptr, buf, len);
193	return error;
194}
195
196void
197linux_msg(const struct thread *td, const char *fmt, ...)
198{
199	va_list ap;
200	struct proc *p;
201
202	p = td->td_proc;
203	printf("linux: pid %d (%s): ", (int)p->p_pid, p->p_comm);
204	va_start(ap, fmt);
205	vprintf(fmt, ap);
206	va_end(ap);
207	printf("\n");
208}
209