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