vfs_getcwd.c revision 1.61 1 1.61 dholland /* $NetBSD: vfs_getcwd.c,v 1.61 2021/06/29 22:39:21 dholland Exp $ */
2 1.1 sommerfe
3 1.1 sommerfe /*-
4 1.57 ad * Copyright (c) 1999, 2020 The NetBSD Foundation, Inc.
5 1.1 sommerfe * All rights reserved.
6 1.1 sommerfe *
7 1.1 sommerfe * This code is derived from software contributed to The NetBSD Foundation
8 1.1 sommerfe * by Bill Sommerfeld.
9 1.1 sommerfe *
10 1.1 sommerfe * Redistribution and use in source and binary forms, with or without
11 1.1 sommerfe * modification, are permitted provided that the following conditions
12 1.1 sommerfe * are met:
13 1.1 sommerfe * 1. Redistributions of source code must retain the above copyright
14 1.1 sommerfe * notice, this list of conditions and the following disclaimer.
15 1.1 sommerfe * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 sommerfe * notice, this list of conditions and the following disclaimer in the
17 1.1 sommerfe * documentation and/or other materials provided with the distribution.
18 1.1 sommerfe *
19 1.1 sommerfe * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 sommerfe * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 sommerfe * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 sommerfe * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 sommerfe * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 sommerfe * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 sommerfe * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 sommerfe * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 sommerfe * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 sommerfe * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 sommerfe * POSSIBILITY OF SUCH DAMAGE.
30 1.1 sommerfe */
31 1.15 lukem
32 1.15 lukem #include <sys/cdefs.h>
33 1.61 dholland __KERNEL_RCSID(0, "$NetBSD: vfs_getcwd.c,v 1.61 2021/06/29 22:39:21 dholland Exp $");
34 1.1 sommerfe
35 1.1 sommerfe #include <sys/param.h>
36 1.1 sommerfe #include <sys/systm.h>
37 1.1 sommerfe #include <sys/namei.h>
38 1.1 sommerfe #include <sys/filedesc.h>
39 1.1 sommerfe #include <sys/kernel.h>
40 1.1 sommerfe #include <sys/file.h>
41 1.1 sommerfe #include <sys/stat.h>
42 1.1 sommerfe #include <sys/vnode.h>
43 1.1 sommerfe #include <sys/mount.h>
44 1.1 sommerfe #include <sys/proc.h>
45 1.1 sommerfe #include <sys/uio.h>
46 1.43 yamt #include <sys/kmem.h>
47 1.1 sommerfe #include <sys/dirent.h>
48 1.31 elad #include <sys/kauth.h>
49 1.31 elad
50 1.1 sommerfe #include <ufs/ufs/dir.h> /* XXX only for DIRBLKSIZ */
51 1.1 sommerfe
52 1.1 sommerfe #include <sys/syscallargs.h>
53 1.1 sommerfe
54 1.1 sommerfe /*
55 1.9 sommerfe * Vnode variable naming conventions in this file:
56 1.9 sommerfe *
57 1.9 sommerfe * rvp: the current root we're aiming towards.
58 1.9 sommerfe * lvp, *lvpp: the "lower" vnode
59 1.9 sommerfe * uvp, *uvpp: the "upper" vnode.
60 1.9 sommerfe *
61 1.9 sommerfe * Since all the vnodes we're dealing with are directories, and the
62 1.9 sommerfe * lookups are going *up* in the filesystem rather than *down*, the
63 1.9 sommerfe * usual "pvp" (parent) or "dvp" (directory) naming conventions are
64 1.9 sommerfe * too confusing.
65 1.9 sommerfe */
66 1.9 sommerfe
67 1.9 sommerfe /*
68 1.1 sommerfe * XXX Will infinite loop in certain cases if a directory read reliably
69 1.1 sommerfe * returns EINVAL on last block.
70 1.1 sommerfe * XXX is EINVAL the right thing to return if a directory is malformed?
71 1.1 sommerfe */
72 1.1 sommerfe
73 1.1 sommerfe /*
74 1.9 sommerfe * XXX Untested vs. mount -o union; probably does the wrong thing.
75 1.9 sommerfe */
76 1.9 sommerfe
77 1.9 sommerfe /*
78 1.9 sommerfe * Find parent vnode of *lvpp, return in *uvpp
79 1.9 sommerfe *
80 1.9 sommerfe * If we care about the name, scan it looking for name of directory
81 1.9 sommerfe * entry pointing at lvp.
82 1.1 sommerfe *
83 1.1 sommerfe * Place the name in the buffer which starts at bufp, immediately
84 1.1 sommerfe * before *bpp, and move bpp backwards to point at the start of it.
85 1.8 sommerfe *
86 1.9 sommerfe * On entry, *lvpp is a locked vnode reference; on exit, it is vput and NULL'ed
87 1.9 sommerfe * On exit, *uvpp is either NULL or is a locked vnode reference.
88 1.1 sommerfe */
89 1.1 sommerfe static int
90 1.57 ad getcwd_scandir(struct vnode *lvp, struct vnode **uvpp, char **bpp,
91 1.29 christos char *bufp, struct lwp *l)
92 1.1 sommerfe {
93 1.1 sommerfe int error = 0;
94 1.1 sommerfe int eofflag;
95 1.1 sommerfe off_t off;
96 1.1 sommerfe int tries;
97 1.1 sommerfe struct uio uio;
98 1.1 sommerfe struct iovec iov;
99 1.1 sommerfe char *dirbuf = NULL;
100 1.1 sommerfe int dirbuflen;
101 1.1 sommerfe ino_t fileno;
102 1.1 sommerfe struct vattr va;
103 1.9 sommerfe struct vnode *uvp = NULL;
104 1.32 ad kauth_cred_t cred = l->l_cred;
105 1.1 sommerfe struct componentname cn;
106 1.1 sommerfe int len, reclen;
107 1.1 sommerfe tries = 0;
108 1.1 sommerfe
109 1.57 ad /* Need exclusive for UFS VOP_GETATTR (itimes) & VOP_LOOKUP. */
110 1.57 ad KASSERT(VOP_ISLOCKED(lvp) == LK_EXCLUSIVE);
111 1.57 ad
112 1.1 sommerfe /*
113 1.8 sommerfe * If we want the filename, get some info we need while the
114 1.8 sommerfe * current directory is still locked.
115 1.8 sommerfe */
116 1.8 sommerfe if (bufp != NULL) {
117 1.38 pooka error = VOP_GETATTR(lvp, &va, cred);
118 1.8 sommerfe if (error) {
119 1.57 ad VOP_UNLOCK(lvp);
120 1.9 sommerfe *uvpp = NULL;
121 1.8 sommerfe return error;
122 1.8 sommerfe }
123 1.8 sommerfe }
124 1.8 sommerfe
125 1.8 sommerfe /*
126 1.1 sommerfe * Ok, we have to do it the hard way..
127 1.8 sommerfe * Next, get parent vnode using lookup of ..
128 1.1 sommerfe */
129 1.1 sommerfe cn.cn_nameiop = LOOKUP;
130 1.8 sommerfe cn.cn_flags = ISLASTCN | ISDOTDOT | RDONLY;
131 1.31 elad cn.cn_cred = cred;
132 1.1 sommerfe cn.cn_nameptr = "..";
133 1.1 sommerfe cn.cn_namelen = 2;
134 1.24 enami
135 1.50 hannken /* At this point, lvp is locked */
136 1.9 sommerfe error = VOP_LOOKUP(lvp, uvpp, &cn);
137 1.57 ad VOP_UNLOCK(lvp);
138 1.1 sommerfe if (error) {
139 1.9 sommerfe *uvpp = NULL;
140 1.1 sommerfe return error;
141 1.1 sommerfe }
142 1.9 sommerfe uvp = *uvpp;
143 1.1 sommerfe /* If we don't care about the pathname, we're done */
144 1.8 sommerfe if (bufp == NULL) {
145 1.1 sommerfe return 0;
146 1.8 sommerfe }
147 1.24 enami
148 1.1 sommerfe fileno = va.va_fileid;
149 1.1 sommerfe
150 1.49 dholland /* I guess UFS_DIRBLKSIZ is a good guess at a good size to use? */
151 1.49 dholland dirbuflen = UFS_DIRBLKSIZ;
152 1.1 sommerfe if (dirbuflen < va.va_blocksize)
153 1.1 sommerfe dirbuflen = va.va_blocksize;
154 1.43 yamt dirbuf = kmem_alloc(dirbuflen, KM_SLEEP);
155 1.1 sommerfe
156 1.57 ad /* Now lvp is unlocked, try to lock uvp */
157 1.57 ad error = vn_lock(uvp, LK_SHARED);
158 1.57 ad if (error) {
159 1.57 ad vrele(uvp);
160 1.57 ad *uvpp = NULL;
161 1.57 ad return error;
162 1.57 ad }
163 1.57 ad
164 1.1 sommerfe #if 0
165 1.1 sommerfe unionread:
166 1.1 sommerfe #endif
167 1.1 sommerfe off = 0;
168 1.1 sommerfe do {
169 1.1 sommerfe /* call VOP_READDIR of parent */
170 1.1 sommerfe iov.iov_base = dirbuf;
171 1.1 sommerfe iov.iov_len = dirbuflen;
172 1.1 sommerfe
173 1.1 sommerfe uio.uio_iov = &iov;
174 1.1 sommerfe uio.uio_iovcnt = 1;
175 1.1 sommerfe uio.uio_offset = off;
176 1.1 sommerfe uio.uio_resid = dirbuflen;
177 1.1 sommerfe uio.uio_rw = UIO_READ;
178 1.30 yamt UIO_SETUP_SYSSPACE(&uio);
179 1.1 sommerfe
180 1.1 sommerfe eofflag = 0;
181 1.1 sommerfe
182 1.31 elad error = VOP_READDIR(uvp, &uio, cred, &eofflag, 0, 0);
183 1.1 sommerfe
184 1.1 sommerfe off = uio.uio_offset;
185 1.1 sommerfe
186 1.1 sommerfe /*
187 1.1 sommerfe * Try again if NFS tosses its cookies.
188 1.1 sommerfe * XXX this can still loop forever if the directory is busted
189 1.1 sommerfe * such that the second or subsequent page of it always
190 1.1 sommerfe * returns EINVAL
191 1.1 sommerfe */
192 1.1 sommerfe if ((error == EINVAL) && (tries < 3)) {
193 1.1 sommerfe off = 0;
194 1.1 sommerfe tries++;
195 1.1 sommerfe continue; /* once more, with feeling */
196 1.1 sommerfe }
197 1.2 nathanw
198 1.1 sommerfe if (!error) {
199 1.1 sommerfe char *cpos;
200 1.1 sommerfe struct dirent *dp;
201 1.24 enami
202 1.1 sommerfe cpos = dirbuf;
203 1.1 sommerfe tries = 0;
204 1.24 enami
205 1.24 enami /* scan directory page looking for matching vnode */
206 1.24 enami for (len = (dirbuflen - uio.uio_resid); len > 0;
207 1.24 enami len -= reclen) {
208 1.1 sommerfe dp = (struct dirent *) cpos;
209 1.1 sommerfe reclen = dp->d_reclen;
210 1.1 sommerfe
211 1.1 sommerfe /* check for malformed directory.. */
212 1.52 riastrad if (reclen < _DIRENT_MINSIZE(dp) ||
213 1.52 riastrad reclen > len) {
214 1.1 sommerfe error = EINVAL;
215 1.1 sommerfe goto out;
216 1.1 sommerfe }
217 1.1 sommerfe /*
218 1.1 sommerfe * XXX should perhaps do VOP_LOOKUP to
219 1.1 sommerfe * check that we got back to the right place,
220 1.1 sommerfe * but getting the locking games for that
221 1.1 sommerfe * right would be heinous.
222 1.1 sommerfe */
223 1.1 sommerfe if ((dp->d_type != DT_WHT) &&
224 1.1 sommerfe (dp->d_fileno == fileno)) {
225 1.1 sommerfe char *bp = *bpp;
226 1.24 enami
227 1.1 sommerfe bp -= dp->d_namlen;
228 1.1 sommerfe if (bp <= bufp) {
229 1.1 sommerfe error = ERANGE;
230 1.1 sommerfe goto out;
231 1.1 sommerfe }
232 1.1 sommerfe memcpy(bp, dp->d_name, dp->d_namlen);
233 1.1 sommerfe error = 0;
234 1.1 sommerfe *bpp = bp;
235 1.1 sommerfe goto out;
236 1.1 sommerfe }
237 1.1 sommerfe cpos += reclen;
238 1.1 sommerfe }
239 1.14 fvdl } else
240 1.14 fvdl goto out;
241 1.2 nathanw } while (!eofflag);
242 1.1 sommerfe #if 0
243 1.1 sommerfe /*
244 1.1 sommerfe * Deal with mount -o union, which unions only the
245 1.1 sommerfe * root directory of the mount.
246 1.1 sommerfe */
247 1.37 ad if ((uvp->v_vflag & VV_ROOT) &&
248 1.9 sommerfe (uvp->v_mount->mnt_flag & MNT_UNION)) {
249 1.9 sommerfe struct vnode *tvp = uvp;
250 1.24 enami
251 1.9 sommerfe uvp = uvp->v_mount->mnt_vnodecovered;
252 1.1 sommerfe vput(tvp);
253 1.44 pooka vref(uvp);
254 1.9 sommerfe *uvpp = uvp;
255 1.57 ad vn_lock(uvp, LK_SHARED | LK_RETRY);
256 1.1 sommerfe goto unionread;
257 1.1 sommerfe }
258 1.24 enami #endif
259 1.1 sommerfe error = ENOENT;
260 1.24 enami
261 1.1 sommerfe out:
262 1.57 ad VOP_UNLOCK(uvp);
263 1.43 yamt kmem_free(dirbuf, dirbuflen);
264 1.1 sommerfe return error;
265 1.1 sommerfe }
266 1.1 sommerfe
267 1.1 sommerfe /*
268 1.1 sommerfe * common routine shared by sys___getcwd() and vn_isunder()
269 1.1 sommerfe */
270 1.24 enami int
271 1.27 thorpej getcwd_common(struct vnode *lvp, struct vnode *rvp, char **bpp, char *bufp,
272 1.29 christos int limit, int flags, struct lwp *l)
273 1.1 sommerfe {
274 1.29 christos struct cwdinfo *cwdi = l->l_proc->p_cwdi;
275 1.32 ad kauth_cred_t cred = l->l_cred;
276 1.9 sommerfe struct vnode *uvp = NULL;
277 1.4 sommerfe char *bp = NULL;
278 1.1 sommerfe int error;
279 1.60 christos accmode_t accmode = VEXEC;
280 1.10 sommerfe
281 1.34 chs error = 0;
282 1.1 sommerfe if (rvp == NULL) {
283 1.6 thorpej rvp = cwdi->cwdi_rdir;
284 1.1 sommerfe if (rvp == NULL)
285 1.1 sommerfe rvp = rootvnode;
286 1.1 sommerfe }
287 1.24 enami
288 1.44 pooka vref(rvp);
289 1.44 pooka vref(lvp);
290 1.1 sommerfe
291 1.1 sommerfe /*
292 1.1 sommerfe * Error handling invariant:
293 1.1 sommerfe * Before a `goto out':
294 1.57 ad * lvp is either NULL, or held.
295 1.57 ad * uvp is either NULL, or held.
296 1.1 sommerfe */
297 1.1 sommerfe
298 1.1 sommerfe if (bufp)
299 1.1 sommerfe bp = *bpp;
300 1.34 chs
301 1.1 sommerfe /*
302 1.1 sommerfe * this loop will terminate when one of the following happens:
303 1.1 sommerfe * - we hit the root
304 1.1 sommerfe * - getdirentries or lookup fails
305 1.1 sommerfe * - we run out of space in the buffer.
306 1.1 sommerfe */
307 1.9 sommerfe if (lvp == rvp) {
308 1.8 sommerfe if (bp)
309 1.8 sommerfe *(--bp) = '/';
310 1.1 sommerfe goto out;
311 1.1 sommerfe }
312 1.1 sommerfe do {
313 1.1 sommerfe /*
314 1.1 sommerfe * access check here is optional, depending on
315 1.1 sommerfe * whether or not caller cares.
316 1.1 sommerfe */
317 1.57 ad int chkaccess = (flags & GETCWD_CHECK_ACCESS);
318 1.57 ad bool locked = false;
319 1.24 enami
320 1.1 sommerfe /*
321 1.1 sommerfe * step up if we're a covered vnode..
322 1.57 ad * check access on the first vnode only.
323 1.1 sommerfe */
324 1.57 ad if (lvp->v_vflag & VV_ROOT) {
325 1.57 ad vn_lock(lvp, LK_SHARED | LK_RETRY);
326 1.57 ad if (chkaccess) {
327 1.60 christos error = VOP_ACCESS(lvp, accmode, cred);
328 1.57 ad if (error) {
329 1.57 ad VOP_UNLOCK(lvp);
330 1.57 ad goto out;
331 1.57 ad }
332 1.57 ad chkaccess = 0;
333 1.57 ad }
334 1.57 ad while (lvp->v_vflag & VV_ROOT) {
335 1.57 ad struct vnode *tvp;
336 1.1 sommerfe
337 1.57 ad if (lvp == rvp) {
338 1.57 ad VOP_UNLOCK(lvp);
339 1.57 ad goto out;
340 1.57 ad }
341 1.24 enami
342 1.57 ad tvp = lvp->v_mount->mnt_vnodecovered;
343 1.57 ad /*
344 1.57 ad * hodie natus est radici frater
345 1.57 ad */
346 1.57 ad if (tvp == NULL) {
347 1.57 ad VOP_UNLOCK(lvp);
348 1.57 ad error = ENOENT;
349 1.57 ad goto out;
350 1.57 ad }
351 1.57 ad vref(tvp);
352 1.57 ad vput(lvp);
353 1.57 ad lvp = tvp;
354 1.57 ad if (lvp->v_vflag & VV_ROOT)
355 1.57 ad vn_lock(lvp, LK_SHARED | LK_RETRY);
356 1.1 sommerfe }
357 1.57 ad }
358 1.57 ad
359 1.57 ad /* Do we need to check access to the directory? */
360 1.57 ad if (chkaccess && !cache_have_id(lvp)) {
361 1.57 ad /* Need exclusive for UFS VOP_GETATTR (itimes) & VOP_LOOKUP. */
362 1.57 ad vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
363 1.60 christos error = VOP_ACCESS(lvp, accmode, cred);
364 1.57 ad if (error) {
365 1.57 ad VOP_UNLOCK(lvp);
366 1.1 sommerfe goto out;
367 1.1 sommerfe }
368 1.57 ad chkaccess = 0;
369 1.57 ad locked = true;
370 1.1 sommerfe }
371 1.57 ad
372 1.1 sommerfe /*
373 1.1 sommerfe * Look in the name cache; if that fails, look in the
374 1.1 sommerfe * directory..
375 1.1 sommerfe */
376 1.57 ad error = cache_revlookup(lvp, &uvp, &bp, bufp, chkaccess,
377 1.60 christos accmode);
378 1.33 christos if (error == -1) {
379 1.57 ad if (!locked) {
380 1.57 ad locked = true;
381 1.57 ad vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
382 1.57 ad }
383 1.33 christos if (lvp->v_type != VDIR) {
384 1.57 ad VOP_UNLOCK(lvp);
385 1.33 christos error = ENOTDIR;
386 1.33 christos goto out;
387 1.33 christos }
388 1.57 ad error = getcwd_scandir(lvp, &uvp, &bp, bufp, l);
389 1.57 ad /* lvp now unlocked */
390 1.57 ad } else if (locked) {
391 1.57 ad VOP_UNLOCK(lvp);
392 1.33 christos }
393 1.1 sommerfe if (error)
394 1.1 sommerfe goto out;
395 1.24 enami #if DIAGNOSTIC
396 1.1 sommerfe if (bufp && (bp <= bufp)) {
397 1.1 sommerfe panic("getcwd: oops, went back too far");
398 1.1 sommerfe }
399 1.24 enami #endif
400 1.60 christos accmode = VEXEC | VREAD;
401 1.24 enami if (bp)
402 1.8 sommerfe *(--bp) = '/';
403 1.57 ad vrele(lvp);
404 1.9 sommerfe lvp = uvp;
405 1.9 sommerfe uvp = NULL;
406 1.1 sommerfe limit--;
407 1.24 enami } while ((lvp != rvp) && (limit > 0));
408 1.1 sommerfe
409 1.1 sommerfe out:
410 1.8 sommerfe if (bpp)
411 1.8 sommerfe *bpp = bp;
412 1.9 sommerfe if (uvp)
413 1.57 ad vrele(uvp);
414 1.9 sommerfe if (lvp)
415 1.57 ad vrele(lvp);
416 1.1 sommerfe vrele(rvp);
417 1.1 sommerfe return error;
418 1.1 sommerfe }
419 1.1 sommerfe
420 1.1 sommerfe /*
421 1.1 sommerfe * Check if one directory can be found inside another in the directory
422 1.1 sommerfe * hierarchy.
423 1.1 sommerfe *
424 1.1 sommerfe * Intended to be used in chroot, chdir, fchdir, etc., to ensure that
425 1.1 sommerfe * chroot() actually means something.
426 1.1 sommerfe */
427 1.11 jdolecek int
428 1.29 christos vn_isunder(struct vnode *lvp, struct vnode *rvp, struct lwp *l)
429 1.1 sommerfe {
430 1.1 sommerfe int error;
431 1.1 sommerfe
432 1.29 christos error = getcwd_common(lvp, rvp, NULL, NULL, MAXPATHLEN / 2, 0, l);
433 1.1 sommerfe
434 1.1 sommerfe if (!error)
435 1.1 sommerfe return 1;
436 1.1 sommerfe else
437 1.1 sommerfe return 0;
438 1.1 sommerfe }
439 1.3 sommerfe
440 1.3 sommerfe /*
441 1.3 sommerfe * Returns true if proc p1's root directory equal to or under p2's
442 1.3 sommerfe * root directory.
443 1.3 sommerfe *
444 1.3 sommerfe * Intended to be used from ptrace/procfs sorts of things.
445 1.3 sommerfe */
446 1.3 sommerfe
447 1.11 jdolecek int
448 1.29 christos proc_isunder(struct proc *p1, struct lwp *l2)
449 1.3 sommerfe {
450 1.6 thorpej struct vnode *r1 = p1->p_cwdi->cwdi_rdir;
451 1.29 christos struct vnode *r2 = l2->l_proc->p_cwdi->cwdi_rdir;
452 1.6 thorpej
453 1.3 sommerfe if (r1 == NULL)
454 1.3 sommerfe return (r2 == NULL);
455 1.3 sommerfe else if (r2 == NULL)
456 1.3 sommerfe return 1;
457 1.3 sommerfe else
458 1.29 christos return vn_isunder(r1, r2, l2);
459 1.3 sommerfe }
460 1.3 sommerfe
461 1.9 sommerfe /*
462 1.9 sommerfe * Find pathname of process's current directory.
463 1.9 sommerfe *
464 1.9 sommerfe * Use vfs vnode-to-name reverse cache; if that fails, fall back
465 1.9 sommerfe * to reading directory contents.
466 1.9 sommerfe */
467 1.9 sommerfe
468 1.11 jdolecek int
469 1.41 dsl sys___getcwd(struct lwp *l, const struct sys___getcwd_args *uap, register_t *retval)
470 1.1 sommerfe {
471 1.41 dsl /* {
472 1.1 sommerfe syscallarg(char *) bufp;
473 1.1 sommerfe syscallarg(size_t) length;
474 1.41 dsl } */
475 1.1 sommerfe
476 1.1 sommerfe int error;
477 1.1 sommerfe char *path;
478 1.1 sommerfe char *bp, *bend;
479 1.1 sommerfe int len = SCARG(uap, length);
480 1.1 sommerfe int lenused;
481 1.59 ad struct cwdinfo *cwdi;
482 1.1 sommerfe
483 1.24 enami if (len > MAXPATHLEN * 4)
484 1.24 enami len = MAXPATHLEN * 4;
485 1.7 sommerfe else if (len < 2)
486 1.1 sommerfe return ERANGE;
487 1.1 sommerfe
488 1.43 yamt path = kmem_alloc(len, KM_SLEEP);
489 1.1 sommerfe bp = &path[len];
490 1.1 sommerfe bend = bp;
491 1.1 sommerfe *(--bp) = '\0';
492 1.1 sommerfe
493 1.8 sommerfe /*
494 1.8 sommerfe * 5th argument here is "max number of vnodes to traverse".
495 1.8 sommerfe * Since each entry takes up at least 2 bytes in the output buffer,
496 1.8 sommerfe * limit it to N/2 vnodes for an N byte buffer.
497 1.8 sommerfe */
498 1.59 ad cwdi = l->l_proc->p_cwdi;
499 1.59 ad rw_enter(&cwdi->cwdi_lock, RW_READER);
500 1.59 ad error = getcwd_common(cwdi->cwdi_cdir, NULL, &bp, path,
501 1.29 christos len/2, GETCWD_CHECK_ACCESS, l);
502 1.59 ad rw_exit(&cwdi->cwdi_lock);
503 1.1 sommerfe
504 1.1 sommerfe if (error)
505 1.1 sommerfe goto out;
506 1.1 sommerfe lenused = bend - bp;
507 1.1 sommerfe *retval = lenused;
508 1.1 sommerfe /* put the result into user buffer */
509 1.1 sommerfe error = copyout(bp, SCARG(uap, bufp), lenused);
510 1.1 sommerfe
511 1.1 sommerfe out:
512 1.43 yamt kmem_free(path, len);
513 1.1 sommerfe return error;
514 1.1 sommerfe }
515 1.39 christos
516 1.39 christos /*
517 1.54 ad * Try to find a pathname for a vnode. Since there is no mapping vnode ->
518 1.54 ad * parent directory, this needs the namecache to succeed. Caller holds a
519 1.46 hannken * reference to the vnode.
520 1.39 christos */
521 1.39 christos int
522 1.39 christos vnode_to_path(char *path, size_t len, struct vnode *vp, struct lwp *curl,
523 1.39 christos struct proc *p)
524 1.39 christos {
525 1.39 christos struct proc *curp = curl->l_proc;
526 1.39 christos int error, lenused, elen;
527 1.39 christos char *bp, *bend;
528 1.39 christos struct vnode *dvp;
529 1.39 christos
530 1.58 ad KASSERT(vrefcnt(vp) > 0);
531 1.46 hannken
532 1.39 christos bp = bend = &path[len];
533 1.39 christos *(--bp) = '\0';
534 1.39 christos
535 1.57 ad error = cache_revlookup(vp, &dvp, &bp, path, false, 0);
536 1.39 christos if (error != 0)
537 1.39 christos return (error == -1 ? ENOENT : error);
538 1.39 christos
539 1.39 christos *(--bp) = '/';
540 1.46 hannken error = getcwd_common(dvp, NULL, &bp, path, len / 2,
541 1.46 hannken GETCWD_CHECK_ACCESS, curl);
542 1.46 hannken vrele(dvp);
543 1.53 christos if (error != 0)
544 1.53 christos return error;
545 1.39 christos
546 1.39 christos /*
547 1.39 christos * Strip off emulation path for emulated processes looking at
548 1.39 christos * the maps file of a process of the same emulation. (Won't
549 1.39 christos * work if /emul/xxx is a symlink..)
550 1.39 christos */
551 1.39 christos if (curp->p_emul == p->p_emul && curp->p_emul->e_path != NULL) {
552 1.39 christos elen = strlen(curp->p_emul->e_path);
553 1.39 christos if (!strncmp(bp, curp->p_emul->e_path, elen))
554 1.39 christos bp = &bp[elen];
555 1.39 christos }
556 1.39 christos
557 1.39 christos lenused = bend - bp;
558 1.39 christos
559 1.39 christos memcpy(path, bp, lenused);
560 1.53 christos path[lenused] = '\0';
561 1.39 christos
562 1.39 christos return 0;
563 1.39 christos }
564