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