xref: /netbsd-src/sys/compat/common/compat_util.c (revision 76dfffe33547c37f8bdd446e3e4ab0f3c16cea4b)
1 /* 	$NetBSD: compat_util.c,v 1.6 1996/10/25 23:14:00 cgd Exp $	*/
2 
3 /*
4  * Copyright (c) 1994 Christos Zoulas
5  * Copyright (c) 1995 Frank van der Linden
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  */
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/namei.h>
35 #include <sys/proc.h>
36 #include <sys/file.h>
37 #include <sys/stat.h>
38 #include <sys/filedesc.h>
39 #include <sys/exec.h>
40 #include <sys/ioctl.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/vnode.h>
44 
45 #include <vm/vm_param.h>
46 
47 #include <compat/common/compat_util.h>
48 
49 /*
50  * Search an alternate path before passing pathname arguments on
51  * to system calls. Useful for keeping a separate 'emulation tree'.
52  *
53  * If cflag is set, we check if an attempt can be made to create
54  * the named file, i.e. we check if the directory it should
55  * be in exists.
56  */
57 int
58 emul_find(p, sgp, prefix, path, pbuf, cflag)
59 	struct proc	 *p;
60 	caddr_t		 *sgp;		/* Pointer to stackgap memory */
61 	const char	 *prefix;
62 	char		 *path;
63 	char		**pbuf;
64 	int		  cflag;
65 {
66 	struct nameidata	 nd;
67 	struct nameidata	 ndroot;
68 	struct vattr		 vat;
69 	struct vattr		 vatroot;
70 	int			 error;
71 	char			*ptr, *buf, *cp;
72 	const char		*pr;
73 	size_t			 sz, len;
74 
75 	buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
76 	*pbuf = path;
77 
78 	for (ptr = buf, pr = prefix; (*ptr = *pr) != '\0'; ptr++, pr++)
79 		continue;
80 
81 	sz = MAXPATHLEN - (ptr - buf);
82 
83 	/*
84 	 * If sgp is not given then the path is already in kernel space
85 	 */
86 	if (sgp == NULL)
87 		error = copystr(path, ptr, sz, &len);
88 	else
89 		error = copyinstr(path, ptr, sz, &len);
90 
91 	if (error)
92 		goto bad;
93 
94 	if (*ptr != '/') {
95 		error = EINVAL;
96 		goto bad;
97 	}
98 
99 	/*
100 	 * We know that there is a / somewhere in this pathname.
101 	 * Search backwards for it, to find the file's parent dir
102 	 * to see if it exists in the alternate tree. If it does,
103 	 * and we want to create a file (cflag is set). We don't
104 	 * need to worry about the root comparison in this case.
105 	 */
106 
107 	if (cflag) {
108 		for (cp = &ptr[len] - 1; *cp != '/'; cp--)
109 			;
110 		*cp = '\0';
111 
112 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, p);
113 
114 		if ((error = namei(&nd)) != 0)
115 			goto bad;
116 
117 		*cp = '/';
118 	}
119 	else {
120 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, p);
121 
122 		if ((error = namei(&nd)) != 0)
123 			goto bad;
124 
125 		/*
126 		 * We now compare the vnode of the emulation 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, prefix, p);
134 
135 		if ((error = namei(&ndroot)) != 0)
136 			goto bad2;
137 
138 		if ((error = VOP_GETATTR(nd.ni_vp, &vat, p->p_ucred, p)) != 0)
139 			goto bad3;
140 
141 		if ((error = VOP_GETATTR(ndroot.ni_vp, &vatroot, p->p_ucred, p))
142 		    != 0)
143 			goto bad3;
144 
145 		if (vat.va_fsid == vatroot.va_fsid &&
146 		    vat.va_fileid == vatroot.va_fileid) {
147 			error = ENOENT;
148 			goto bad3;
149 		}
150 	}
151 	if (sgp == NULL)
152 		*pbuf = buf;
153 	else {
154 		sz = &ptr[len] - buf;
155 		*pbuf = stackgap_alloc(sgp, sz + 1);
156 		error = copyout(buf, *pbuf, sz);
157 		free(buf, M_TEMP);
158 	}
159 
160 	vrele(nd.ni_vp);
161 	if (!cflag)
162 		vrele(ndroot.ni_vp);
163 	return error;
164 
165 bad3:
166 	vrele(ndroot.ni_vp);
167 bad2:
168 	vrele(nd.ni_vp);
169 bad:
170 	free(buf, M_TEMP);
171 	return error;
172 }
173 
174 caddr_t
175 stackgap_init(e)
176 	struct emul *e;
177 {
178 
179 #define szsigcode ((caddr_t)(e->e_esigcode - e->e_sigcode))
180 	return STACKGAPBASE;
181 #undef szsigcode
182 }
183 
184 
185 void *
186 stackgap_alloc(sgp, sz)
187 	caddr_t *sgp;
188 	size_t sz;
189 {
190 	void *p = (void *) *sgp;
191 
192 	*sgp += ALIGN(sz);
193 	return p;
194 }
195