ibcs2_util.c revision 71489
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: head/sys/i386/ibcs2/ibcs2_util.c 71489 2001-01-23 23:59:38Z jhb $
31 */
32
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/namei.h>
36#include <sys/malloc.h>
37#include <sys/vnode.h>
38
39#include <i386/ibcs2/ibcs2_util.h>
40
41
42const char      ibcs2_emul_path[] = "/compat/ibcs2";
43
44/*
45 * Search an alternate path before passing pathname arguments on
46 * to system calls. Useful for keeping a seperate '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 */
52int
53ibcs2_emul_find(p, sgp, prefix, path, pbuf, cflag)
54	struct proc	 *p;
55	caddr_t		 *sgp;		/* Pointer to stackgap memory */
56	const char	 *prefix;
57	char		 *path;
58	char		**pbuf;
59	int		  cflag;
60{
61	struct nameidata	 nd;
62	struct nameidata	 ndroot;
63	struct vattr		 vat;
64	struct vattr		 vatroot;
65	struct ucred		*uc;
66	int			 error;
67	char			*ptr, *buf, *cp;
68	size_t			 sz, len;
69
70	buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
71	*pbuf = path;
72
73	for (ptr = buf; (*ptr = *prefix) != '\0'; ptr++, prefix++)
74		continue;
75
76	sz = MAXPATHLEN - (ptr - buf);
77
78	/*
79	 * If sgp is not given then the path is already in kernel space
80	 */
81	if (sgp == NULL)
82		error = copystr(path, ptr, sz, &len);
83	else
84		error = copyinstr(path, ptr, sz, &len);
85
86	if (error) {
87		free(buf, M_TEMP);
88		return error;
89	}
90
91	if (*ptr != '/') {
92		free(buf, M_TEMP);
93		return EINVAL;
94	}
95
96	/*
97	 * We know that there is a / somewhere in this pathname.
98	 * Search backwards for it, to find the file's parent dir
99	 * to see if it exists in the alternate tree. If it does,
100	 * and we want to create a file (cflag is set). We don't
101	 * need to worry about the root comparison in this case.
102	 */
103
104	if (cflag) {
105		for (cp = &ptr[len] - 1; *cp != '/'; cp--);
106		*cp = '\0';
107
108		NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, p);
109
110		if ((error = namei(&nd)) != 0) {
111			free(buf, M_TEMP);
112			return error;
113		}
114
115		*cp = '/';
116	}
117	else {
118		NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, p);
119
120		if ((error = namei(&nd)) != 0) {
121			free(buf, M_TEMP);
122			return error;
123		}
124
125		/*
126		 * We now compare the vnode of the ibcs2_root to the one
127		 * vnode asked. If they resolve to be the same, then we
128		 * ignore the match so that the real root gets used.
129		 * This avoids the problem of traversing "../.." to find the
130		 * root directory and never finding it, because "/" resolves
131		 * to the emulation root directory. This is expensive :-(
132		 */
133		NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, ibcs2_emul_path,
134		       p);
135
136		if ((error = namei(&ndroot)) != 0) {
137			/* Cannot happen! */
138			free(buf, M_TEMP);
139			NDFREE(&nd, NDF_ONLY_PNBUF);
140			vrele(nd.ni_vp);
141			return error;
142		}
143
144		PROC_LOCK(p);
145		uc = p->p_ucred;
146		crhold(uc);
147		PROC_UNLOCK(p);
148		if ((error = VOP_GETATTR(nd.ni_vp, &vat, uc, p)) != 0) {
149			crfree(uc);
150			goto done;
151		}
152
153		if ((error = VOP_GETATTR(ndroot.ni_vp, &vatroot, uc, p)) != 0) {
154			crfree(uc);
155			goto done;
156		}
157		crfree(uc);
158
159		if (vat.va_fsid == vatroot.va_fsid &&
160		    vat.va_fileid == vatroot.va_fileid) {
161			error = ENOENT;
162			goto done;
163		}
164
165	}
166	if (sgp == NULL)
167		*pbuf = buf;
168	else {
169		sz = &ptr[len] - buf;
170		*pbuf = stackgap_alloc(sgp, sz + 1);
171		error = copyout(buf, *pbuf, sz);
172		free(buf, M_TEMP);
173	}
174
175
176done:
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}
185