vfs_getcwd.c revision 1.60 1 1.60 christos /* $NetBSD: vfs_getcwd.c,v 1.60 2020/05/16 18:31:50 christos 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.60 christos __KERNEL_RCSID(0, "$NetBSD: vfs_getcwd.c,v 1.60 2020/05/16 18:31:50 christos 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.1 sommerfe cn.cn_consume = 0;
135 1.24 enami
136 1.50 hannken /* At this point, lvp is locked */
137 1.9 sommerfe error = VOP_LOOKUP(lvp, uvpp, &cn);
138 1.57 ad VOP_UNLOCK(lvp);
139 1.1 sommerfe if (error) {
140 1.9 sommerfe *uvpp = NULL;
141 1.1 sommerfe return error;
142 1.1 sommerfe }
143 1.9 sommerfe uvp = *uvpp;
144 1.1 sommerfe /* If we don't care about the pathname, we're done */
145 1.8 sommerfe if (bufp == NULL) {
146 1.1 sommerfe return 0;
147 1.8 sommerfe }
148 1.24 enami
149 1.1 sommerfe fileno = va.va_fileid;
150 1.1 sommerfe
151 1.49 dholland /* I guess UFS_DIRBLKSIZ is a good guess at a good size to use? */
152 1.49 dholland dirbuflen = UFS_DIRBLKSIZ;
153 1.1 sommerfe if (dirbuflen < va.va_blocksize)
154 1.1 sommerfe dirbuflen = va.va_blocksize;
155 1.43 yamt dirbuf = kmem_alloc(dirbuflen, KM_SLEEP);
156 1.1 sommerfe
157 1.57 ad /* Now lvp is unlocked, try to lock uvp */
158 1.57 ad error = vn_lock(uvp, LK_SHARED);
159 1.57 ad if (error) {
160 1.57 ad vrele(uvp);
161 1.57 ad *uvpp = NULL;
162 1.57 ad return error;
163 1.57 ad }
164 1.57 ad
165 1.1 sommerfe #if 0
166 1.1 sommerfe unionread:
167 1.1 sommerfe #endif
168 1.1 sommerfe off = 0;
169 1.1 sommerfe do {
170 1.1 sommerfe /* call VOP_READDIR of parent */
171 1.1 sommerfe iov.iov_base = dirbuf;
172 1.1 sommerfe iov.iov_len = dirbuflen;
173 1.1 sommerfe
174 1.1 sommerfe uio.uio_iov = &iov;
175 1.1 sommerfe uio.uio_iovcnt = 1;
176 1.1 sommerfe uio.uio_offset = off;
177 1.1 sommerfe uio.uio_resid = dirbuflen;
178 1.1 sommerfe uio.uio_rw = UIO_READ;
179 1.30 yamt UIO_SETUP_SYSSPACE(&uio);
180 1.1 sommerfe
181 1.1 sommerfe eofflag = 0;
182 1.1 sommerfe
183 1.31 elad error = VOP_READDIR(uvp, &uio, cred, &eofflag, 0, 0);
184 1.1 sommerfe
185 1.1 sommerfe off = uio.uio_offset;
186 1.1 sommerfe
187 1.1 sommerfe /*
188 1.1 sommerfe * Try again if NFS tosses its cookies.
189 1.1 sommerfe * XXX this can still loop forever if the directory is busted
190 1.1 sommerfe * such that the second or subsequent page of it always
191 1.1 sommerfe * returns EINVAL
192 1.1 sommerfe */
193 1.1 sommerfe if ((error == EINVAL) && (tries < 3)) {
194 1.1 sommerfe off = 0;
195 1.1 sommerfe tries++;
196 1.1 sommerfe continue; /* once more, with feeling */
197 1.1 sommerfe }
198 1.2 nathanw
199 1.1 sommerfe if (!error) {
200 1.1 sommerfe char *cpos;
201 1.1 sommerfe struct dirent *dp;
202 1.24 enami
203 1.1 sommerfe cpos = dirbuf;
204 1.1 sommerfe tries = 0;
205 1.24 enami
206 1.24 enami /* scan directory page looking for matching vnode */
207 1.24 enami for (len = (dirbuflen - uio.uio_resid); len > 0;
208 1.24 enami len -= reclen) {
209 1.1 sommerfe dp = (struct dirent *) cpos;
210 1.1 sommerfe reclen = dp->d_reclen;
211 1.1 sommerfe
212 1.1 sommerfe /* check for malformed directory.. */
213 1.52 riastrad if (reclen < _DIRENT_MINSIZE(dp) ||
214 1.52 riastrad reclen > len) {
215 1.1 sommerfe error = EINVAL;
216 1.1 sommerfe goto out;
217 1.1 sommerfe }
218 1.1 sommerfe /*
219 1.1 sommerfe * XXX should perhaps do VOP_LOOKUP to
220 1.1 sommerfe * check that we got back to the right place,
221 1.1 sommerfe * but getting the locking games for that
222 1.1 sommerfe * right would be heinous.
223 1.1 sommerfe */
224 1.1 sommerfe if ((dp->d_type != DT_WHT) &&
225 1.1 sommerfe (dp->d_fileno == fileno)) {
226 1.1 sommerfe char *bp = *bpp;
227 1.24 enami
228 1.1 sommerfe bp -= dp->d_namlen;
229 1.1 sommerfe if (bp <= bufp) {
230 1.1 sommerfe error = ERANGE;
231 1.1 sommerfe goto out;
232 1.1 sommerfe }
233 1.1 sommerfe memcpy(bp, dp->d_name, dp->d_namlen);
234 1.1 sommerfe error = 0;
235 1.1 sommerfe *bpp = bp;
236 1.1 sommerfe goto out;
237 1.1 sommerfe }
238 1.1 sommerfe cpos += reclen;
239 1.1 sommerfe }
240 1.14 fvdl } else
241 1.14 fvdl goto out;
242 1.2 nathanw } while (!eofflag);
243 1.1 sommerfe #if 0
244 1.1 sommerfe /*
245 1.1 sommerfe * Deal with mount -o union, which unions only the
246 1.1 sommerfe * root directory of the mount.
247 1.1 sommerfe */
248 1.37 ad if ((uvp->v_vflag & VV_ROOT) &&
249 1.9 sommerfe (uvp->v_mount->mnt_flag & MNT_UNION)) {
250 1.9 sommerfe struct vnode *tvp = uvp;
251 1.24 enami
252 1.9 sommerfe uvp = uvp->v_mount->mnt_vnodecovered;
253 1.1 sommerfe vput(tvp);
254 1.44 pooka vref(uvp);
255 1.9 sommerfe *uvpp = uvp;
256 1.57 ad vn_lock(uvp, LK_SHARED | LK_RETRY);
257 1.1 sommerfe goto unionread;
258 1.1 sommerfe }
259 1.24 enami #endif
260 1.1 sommerfe error = ENOENT;
261 1.24 enami
262 1.1 sommerfe out:
263 1.57 ad VOP_UNLOCK(uvp);
264 1.43 yamt kmem_free(dirbuf, dirbuflen);
265 1.1 sommerfe return error;
266 1.1 sommerfe }
267 1.1 sommerfe
268 1.1 sommerfe /*
269 1.1 sommerfe * common routine shared by sys___getcwd() and vn_isunder()
270 1.1 sommerfe */
271 1.24 enami int
272 1.27 thorpej getcwd_common(struct vnode *lvp, struct vnode *rvp, char **bpp, char *bufp,
273 1.29 christos int limit, int flags, struct lwp *l)
274 1.1 sommerfe {
275 1.29 christos struct cwdinfo *cwdi = l->l_proc->p_cwdi;
276 1.32 ad kauth_cred_t cred = l->l_cred;
277 1.9 sommerfe struct vnode *uvp = NULL;
278 1.4 sommerfe char *bp = NULL;
279 1.1 sommerfe int error;
280 1.60 christos accmode_t accmode = VEXEC;
281 1.10 sommerfe
282 1.34 chs error = 0;
283 1.1 sommerfe if (rvp == NULL) {
284 1.6 thorpej rvp = cwdi->cwdi_rdir;
285 1.1 sommerfe if (rvp == NULL)
286 1.1 sommerfe rvp = rootvnode;
287 1.1 sommerfe }
288 1.24 enami
289 1.44 pooka vref(rvp);
290 1.44 pooka vref(lvp);
291 1.1 sommerfe
292 1.1 sommerfe /*
293 1.1 sommerfe * Error handling invariant:
294 1.1 sommerfe * Before a `goto out':
295 1.57 ad * lvp is either NULL, or held.
296 1.57 ad * uvp is either NULL, or held.
297 1.1 sommerfe */
298 1.1 sommerfe
299 1.1 sommerfe if (bufp)
300 1.1 sommerfe bp = *bpp;
301 1.34 chs
302 1.1 sommerfe /*
303 1.1 sommerfe * this loop will terminate when one of the following happens:
304 1.1 sommerfe * - we hit the root
305 1.1 sommerfe * - getdirentries or lookup fails
306 1.1 sommerfe * - we run out of space in the buffer.
307 1.1 sommerfe */
308 1.9 sommerfe if (lvp == rvp) {
309 1.8 sommerfe if (bp)
310 1.8 sommerfe *(--bp) = '/';
311 1.1 sommerfe goto out;
312 1.1 sommerfe }
313 1.1 sommerfe do {
314 1.1 sommerfe /*
315 1.1 sommerfe * access check here is optional, depending on
316 1.1 sommerfe * whether or not caller cares.
317 1.1 sommerfe */
318 1.57 ad int chkaccess = (flags & GETCWD_CHECK_ACCESS);
319 1.57 ad bool locked = false;
320 1.24 enami
321 1.1 sommerfe /*
322 1.1 sommerfe * step up if we're a covered vnode..
323 1.57 ad * check access on the first vnode only.
324 1.1 sommerfe */
325 1.57 ad if (lvp->v_vflag & VV_ROOT) {
326 1.57 ad vn_lock(lvp, LK_SHARED | LK_RETRY);
327 1.57 ad if (chkaccess) {
328 1.60 christos error = VOP_ACCESS(lvp, accmode, cred);
329 1.57 ad if (error) {
330 1.57 ad VOP_UNLOCK(lvp);
331 1.57 ad goto out;
332 1.57 ad }
333 1.57 ad chkaccess = 0;
334 1.57 ad }
335 1.57 ad while (lvp->v_vflag & VV_ROOT) {
336 1.57 ad struct vnode *tvp;
337 1.1 sommerfe
338 1.57 ad if (lvp == rvp) {
339 1.57 ad VOP_UNLOCK(lvp);
340 1.57 ad goto out;
341 1.57 ad }
342 1.24 enami
343 1.57 ad tvp = lvp->v_mount->mnt_vnodecovered;
344 1.57 ad /*
345 1.57 ad * hodie natus est radici frater
346 1.57 ad */
347 1.57 ad if (tvp == NULL) {
348 1.57 ad VOP_UNLOCK(lvp);
349 1.57 ad error = ENOENT;
350 1.57 ad goto out;
351 1.57 ad }
352 1.57 ad vref(tvp);
353 1.57 ad vput(lvp);
354 1.57 ad lvp = tvp;
355 1.57 ad if (lvp->v_vflag & VV_ROOT)
356 1.57 ad vn_lock(lvp, LK_SHARED | LK_RETRY);
357 1.1 sommerfe }
358 1.57 ad }
359 1.57 ad
360 1.57 ad /* Do we need to check access to the directory? */
361 1.57 ad if (chkaccess && !cache_have_id(lvp)) {
362 1.57 ad /* Need exclusive for UFS VOP_GETATTR (itimes) & VOP_LOOKUP. */
363 1.57 ad vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
364 1.60 christos error = VOP_ACCESS(lvp, accmode, cred);
365 1.57 ad if (error) {
366 1.57 ad VOP_UNLOCK(lvp);
367 1.1 sommerfe goto out;
368 1.1 sommerfe }
369 1.57 ad chkaccess = 0;
370 1.57 ad locked = true;
371 1.1 sommerfe }
372 1.57 ad
373 1.1 sommerfe /*
374 1.1 sommerfe * Look in the name cache; if that fails, look in the
375 1.1 sommerfe * directory..
376 1.1 sommerfe */
377 1.57 ad error = cache_revlookup(lvp, &uvp, &bp, bufp, chkaccess,
378 1.60 christos accmode);
379 1.33 christos if (error == -1) {
380 1.57 ad if (!locked) {
381 1.57 ad locked = true;
382 1.57 ad vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
383 1.57 ad }
384 1.33 christos if (lvp->v_type != VDIR) {
385 1.57 ad VOP_UNLOCK(lvp);
386 1.33 christos error = ENOTDIR;
387 1.33 christos goto out;
388 1.33 christos }
389 1.57 ad error = getcwd_scandir(lvp, &uvp, &bp, bufp, l);
390 1.57 ad /* lvp now unlocked */
391 1.57 ad } else if (locked) {
392 1.57 ad VOP_UNLOCK(lvp);
393 1.33 christos }
394 1.1 sommerfe if (error)
395 1.1 sommerfe goto out;
396 1.24 enami #if DIAGNOSTIC
397 1.1 sommerfe if (bufp && (bp <= bufp)) {
398 1.1 sommerfe panic("getcwd: oops, went back too far");
399 1.1 sommerfe }
400 1.24 enami #endif
401 1.60 christos accmode = VEXEC | VREAD;
402 1.24 enami if (bp)
403 1.8 sommerfe *(--bp) = '/';
404 1.57 ad vrele(lvp);
405 1.9 sommerfe lvp = uvp;
406 1.9 sommerfe uvp = NULL;
407 1.1 sommerfe limit--;
408 1.24 enami } while ((lvp != rvp) && (limit > 0));
409 1.1 sommerfe
410 1.1 sommerfe out:
411 1.8 sommerfe if (bpp)
412 1.8 sommerfe *bpp = bp;
413 1.9 sommerfe if (uvp)
414 1.57 ad vrele(uvp);
415 1.9 sommerfe if (lvp)
416 1.57 ad vrele(lvp);
417 1.1 sommerfe vrele(rvp);
418 1.1 sommerfe return error;
419 1.1 sommerfe }
420 1.1 sommerfe
421 1.1 sommerfe /*
422 1.1 sommerfe * Check if one directory can be found inside another in the directory
423 1.1 sommerfe * hierarchy.
424 1.1 sommerfe *
425 1.1 sommerfe * Intended to be used in chroot, chdir, fchdir, etc., to ensure that
426 1.1 sommerfe * chroot() actually means something.
427 1.1 sommerfe */
428 1.11 jdolecek int
429 1.29 christos vn_isunder(struct vnode *lvp, struct vnode *rvp, struct lwp *l)
430 1.1 sommerfe {
431 1.1 sommerfe int error;
432 1.1 sommerfe
433 1.29 christos error = getcwd_common(lvp, rvp, NULL, NULL, MAXPATHLEN / 2, 0, l);
434 1.1 sommerfe
435 1.1 sommerfe if (!error)
436 1.1 sommerfe return 1;
437 1.1 sommerfe else
438 1.1 sommerfe return 0;
439 1.1 sommerfe }
440 1.3 sommerfe
441 1.3 sommerfe /*
442 1.3 sommerfe * Returns true if proc p1's root directory equal to or under p2's
443 1.3 sommerfe * root directory.
444 1.3 sommerfe *
445 1.3 sommerfe * Intended to be used from ptrace/procfs sorts of things.
446 1.3 sommerfe */
447 1.3 sommerfe
448 1.11 jdolecek int
449 1.29 christos proc_isunder(struct proc *p1, struct lwp *l2)
450 1.3 sommerfe {
451 1.6 thorpej struct vnode *r1 = p1->p_cwdi->cwdi_rdir;
452 1.29 christos struct vnode *r2 = l2->l_proc->p_cwdi->cwdi_rdir;
453 1.6 thorpej
454 1.3 sommerfe if (r1 == NULL)
455 1.3 sommerfe return (r2 == NULL);
456 1.3 sommerfe else if (r2 == NULL)
457 1.3 sommerfe return 1;
458 1.3 sommerfe else
459 1.29 christos return vn_isunder(r1, r2, l2);
460 1.3 sommerfe }
461 1.3 sommerfe
462 1.9 sommerfe /*
463 1.9 sommerfe * Find pathname of process's current directory.
464 1.9 sommerfe *
465 1.9 sommerfe * Use vfs vnode-to-name reverse cache; if that fails, fall back
466 1.9 sommerfe * to reading directory contents.
467 1.9 sommerfe */
468 1.9 sommerfe
469 1.11 jdolecek int
470 1.41 dsl sys___getcwd(struct lwp *l, const struct sys___getcwd_args *uap, register_t *retval)
471 1.1 sommerfe {
472 1.41 dsl /* {
473 1.1 sommerfe syscallarg(char *) bufp;
474 1.1 sommerfe syscallarg(size_t) length;
475 1.41 dsl } */
476 1.1 sommerfe
477 1.1 sommerfe int error;
478 1.1 sommerfe char *path;
479 1.1 sommerfe char *bp, *bend;
480 1.1 sommerfe int len = SCARG(uap, length);
481 1.1 sommerfe int lenused;
482 1.59 ad struct cwdinfo *cwdi;
483 1.1 sommerfe
484 1.24 enami if (len > MAXPATHLEN * 4)
485 1.24 enami len = MAXPATHLEN * 4;
486 1.7 sommerfe else if (len < 2)
487 1.1 sommerfe return ERANGE;
488 1.1 sommerfe
489 1.43 yamt path = kmem_alloc(len, KM_SLEEP);
490 1.1 sommerfe bp = &path[len];
491 1.1 sommerfe bend = bp;
492 1.1 sommerfe *(--bp) = '\0';
493 1.1 sommerfe
494 1.8 sommerfe /*
495 1.8 sommerfe * 5th argument here is "max number of vnodes to traverse".
496 1.8 sommerfe * Since each entry takes up at least 2 bytes in the output buffer,
497 1.8 sommerfe * limit it to N/2 vnodes for an N byte buffer.
498 1.8 sommerfe */
499 1.59 ad cwdi = l->l_proc->p_cwdi;
500 1.59 ad rw_enter(&cwdi->cwdi_lock, RW_READER);
501 1.59 ad error = getcwd_common(cwdi->cwdi_cdir, NULL, &bp, path,
502 1.29 christos len/2, GETCWD_CHECK_ACCESS, l);
503 1.59 ad rw_exit(&cwdi->cwdi_lock);
504 1.1 sommerfe
505 1.1 sommerfe if (error)
506 1.1 sommerfe goto out;
507 1.1 sommerfe lenused = bend - bp;
508 1.1 sommerfe *retval = lenused;
509 1.1 sommerfe /* put the result into user buffer */
510 1.1 sommerfe error = copyout(bp, SCARG(uap, bufp), lenused);
511 1.1 sommerfe
512 1.1 sommerfe out:
513 1.43 yamt kmem_free(path, len);
514 1.1 sommerfe return error;
515 1.1 sommerfe }
516 1.39 christos
517 1.39 christos /*
518 1.54 ad * Try to find a pathname for a vnode. Since there is no mapping vnode ->
519 1.54 ad * parent directory, this needs the namecache to succeed. Caller holds a
520 1.46 hannken * reference to the vnode.
521 1.39 christos */
522 1.39 christos int
523 1.39 christos vnode_to_path(char *path, size_t len, struct vnode *vp, struct lwp *curl,
524 1.39 christos struct proc *p)
525 1.39 christos {
526 1.39 christos struct proc *curp = curl->l_proc;
527 1.39 christos int error, lenused, elen;
528 1.39 christos char *bp, *bend;
529 1.39 christos struct vnode *dvp;
530 1.39 christos
531 1.58 ad KASSERT(vrefcnt(vp) > 0);
532 1.46 hannken
533 1.39 christos bp = bend = &path[len];
534 1.39 christos *(--bp) = '\0';
535 1.39 christos
536 1.57 ad error = cache_revlookup(vp, &dvp, &bp, path, false, 0);
537 1.39 christos if (error != 0)
538 1.39 christos return (error == -1 ? ENOENT : error);
539 1.39 christos
540 1.39 christos *(--bp) = '/';
541 1.46 hannken error = getcwd_common(dvp, NULL, &bp, path, len / 2,
542 1.46 hannken GETCWD_CHECK_ACCESS, curl);
543 1.46 hannken vrele(dvp);
544 1.53 christos if (error != 0)
545 1.53 christos return error;
546 1.39 christos
547 1.39 christos /*
548 1.39 christos * Strip off emulation path for emulated processes looking at
549 1.39 christos * the maps file of a process of the same emulation. (Won't
550 1.39 christos * work if /emul/xxx is a symlink..)
551 1.39 christos */
552 1.39 christos if (curp->p_emul == p->p_emul && curp->p_emul->e_path != NULL) {
553 1.39 christos elen = strlen(curp->p_emul->e_path);
554 1.39 christos if (!strncmp(bp, curp->p_emul->e_path, elen))
555 1.39 christos bp = &bp[elen];
556 1.39 christos }
557 1.39 christos
558 1.39 christos lenused = bend - bp;
559 1.39 christos
560 1.39 christos memcpy(path, bp, lenused);
561 1.53 christos path[lenused] = '\0';
562 1.39 christos
563 1.39 christos return 0;
564 1.39 christos }
565