compat_util.c revision 1.3 1 1.3 mycroft /* $NetBSD: compat_util.c,v 1.3 1995/10/22 08:20:44 mycroft 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.3 mycroft if (error)
89 1.3 mycroft goto bad;
90 1.1 christos
91 1.1 christos if (*ptr != '/') {
92 1.3 mycroft error = EINVAL;
93 1.3 mycroft goto bad;
94 1.1 christos }
95 1.1 christos
96 1.1 christos /*
97 1.1 christos * We know that there is a / somewhere in this pathname.
98 1.1 christos * Search backwards for it, to find the file's parent dir
99 1.1 christos * to see if it exists in the alternate tree. If it does,
100 1.1 christos * and we want to create a file (cflag is set). We don't
101 1.1 christos * need to worry about the root comparison in this case.
102 1.1 christos */
103 1.1 christos
104 1.1 christos if (cflag) {
105 1.3 mycroft for (cp = &ptr[len] - 1; *cp != '/'; cp--)
106 1.3 mycroft ;
107 1.1 christos *cp = '\0';
108 1.1 christos
109 1.1 christos NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, p);
110 1.1 christos
111 1.3 mycroft if ((error = namei(&nd)) != 0)
112 1.3 mycroft goto bad;
113 1.1 christos
114 1.1 christos *cp = '/';
115 1.1 christos }
116 1.1 christos else {
117 1.1 christos NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, p);
118 1.1 christos
119 1.3 mycroft if ((error = namei(&nd)) != 0)
120 1.3 mycroft goto bad;
121 1.1 christos
122 1.1 christos /*
123 1.1 christos * We now compare the vnode of the emulation root to the one
124 1.1 christos * vnode asked. If they resolve to be the same, then we
125 1.1 christos * ignore the match so that the real root gets used.
126 1.1 christos * This avoids the problem of traversing "../.." to find the
127 1.1 christos * root directory and never finding it, because "/" resolves
128 1.1 christos * to the emulation root directory. This is expensive :-(
129 1.1 christos */
130 1.1 christos /* XXX: prototype should have const here for NDINIT */
131 1.1 christos NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE,
132 1.1 christos (char *) prefix, p);
133 1.1 christos
134 1.3 mycroft if ((error = namei(&ndroot)) != 0)
135 1.3 mycroft goto bad2;
136 1.1 christos
137 1.3 mycroft if ((error = VOP_GETATTR(nd.ni_vp, &vat, p->p_ucred, p)) != 0)
138 1.3 mycroft goto bad3;
139 1.1 christos
140 1.1 christos if ((error = VOP_GETATTR(ndroot.ni_vp, &vatroot, p->p_ucred, p))
141 1.3 mycroft != 0)
142 1.3 mycroft goto bad3;
143 1.1 christos
144 1.1 christos if (vat.va_fsid == vatroot.va_fsid &&
145 1.1 christos vat.va_fileid == vatroot.va_fileid) {
146 1.1 christos error = ENOENT;
147 1.3 mycroft goto bad3;
148 1.1 christos }
149 1.1 christos }
150 1.1 christos if (sgp == NULL)
151 1.1 christos *pbuf = buf;
152 1.1 christos else {
153 1.1 christos sz = &ptr[len] - buf;
154 1.1 christos *pbuf = stackgap_alloc(sgp, sz + 1);
155 1.1 christos error = copyout(buf, *pbuf, sz);
156 1.1 christos free(buf, M_TEMP);
157 1.1 christos }
158 1.1 christos
159 1.1 christos done:
160 1.1 christos vrele(nd.ni_vp);
161 1.1 christos if (!cflag)
162 1.1 christos vrele(ndroot.ni_vp);
163 1.3 mycroft return error;
164 1.3 mycroft
165 1.3 mycroft bad3:
166 1.3 mycroft vrele(ndroot.ni_vp);
167 1.3 mycroft bad2:
168 1.3 mycroft vrele(nd.ni_vp);
169 1.3 mycroft bad:
170 1.3 mycroft free(buf, M_TEMP);
171 1.1 christos return error;
172 1.1 christos }
173