xref: /openbsd-src/sys/kern/vfs_getcwd.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /* $OpenBSD: vfs_getcwd.c,v 1.17 2010/05/19 08:31:23 thib Exp $ */
2 /* $NetBSD: vfs_getcwd.c,v 1.3.2.3 1999/07/11 10:24:09 sommerfeld Exp $ */
3 
4 /*
5  * Copyright (c) 1999 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Bill Sommerfeld.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/namei.h>
36 #include <sys/filedesc.h>
37 #include <sys/kernel.h>
38 #include <sys/file.h>
39 #include <sys/stat.h>
40 #include <sys/vnode.h>
41 #include <sys/mount.h>
42 #include <sys/proc.h>
43 #include <sys/uio.h>
44 #include <sys/malloc.h>
45 #include <sys/dirent.h>
46 #include <ufs/ufs/dir.h>	/* only for DIRBLKSIZ */
47 
48 #include <sys/syscallargs.h>
49 
50 #define DIRENT_MINSIZE (sizeof(struct dirent) - (MAXNAMLEN + 1) + 4)
51 
52 /* Find parent vnode of *lvpp, return in *uvpp */
53 int
54 vfs_getcwd_scandir(struct vnode **lvpp, struct vnode **uvpp, char **bpp,
55     char *bufp, struct proc *p)
56 {
57 	int eofflag, tries, dirbuflen, len, reclen, error = 0;
58 	off_t off;
59 	struct uio uio;
60 	struct iovec iov;
61 	char *dirbuf = NULL;
62 	ino_t fileno;
63 	struct vattr va;
64 	struct vnode *uvp = NULL;
65 	struct vnode *lvp = *lvpp;
66 	struct componentname cn;
67 
68 	tries = 0;
69 
70 	/*
71 	 * If we want the filename, get some info we need while the
72 	 * current directory is still locked.
73 	 */
74 	if (bufp != NULL) {
75 		error = VOP_GETATTR(lvp, &va, p->p_ucred, p);
76 		if (error) {
77 			vput(lvp);
78 			*lvpp = NULL;
79 			*uvpp = NULL;
80 			return (error);
81 		}
82 	}
83 
84 	cn.cn_nameiop = LOOKUP;
85 	cn.cn_flags = ISLASTCN | ISDOTDOT | RDONLY;
86 	cn.cn_proc = p;
87 	cn.cn_cred = p->p_ucred;
88 	cn.cn_pnbuf = NULL;
89 	cn.cn_nameptr = "..";
90 	cn.cn_namelen = 2;
91 	cn.cn_consume = 0;
92 
93 	/* Get parent vnode using lookup of '..' */
94 	error = VOP_LOOKUP(lvp, uvpp, &cn);
95 	if (error) {
96 		vput(lvp);
97 		*lvpp = NULL;
98 		*uvpp = NULL;
99 		return (error);
100 	}
101 
102 	uvp = *uvpp;
103 
104 	/* If we don't care about the pathname, we're done */
105 	if (bufp == NULL) {
106 		vrele(lvp);
107 		*lvpp = NULL;
108 		return (0);
109 	}
110 
111 	fileno = va.va_fileid;
112 
113 	dirbuflen = DIRBLKSIZ;
114 
115 	if (dirbuflen < va.va_blocksize)
116 		dirbuflen = va.va_blocksize;
117 
118 	dirbuf = malloc(dirbuflen, M_TEMP, M_WAITOK);
119 
120 	off = 0;
121 
122 	do {
123 		char   *cpos;
124 		struct dirent *dp;
125 
126 		iov.iov_base = dirbuf;
127 		iov.iov_len = dirbuflen;
128 
129 		uio.uio_iov = &iov;
130 		uio.uio_iovcnt = 1;
131 		uio.uio_offset = off;
132 		uio.uio_resid = dirbuflen;
133 		uio.uio_segflg = UIO_SYSSPACE;
134 		uio.uio_rw = UIO_READ;
135 		uio.uio_procp = p;
136 
137 		eofflag = 0;
138 
139 		/* Call VOP_READDIR of parent */
140 		error = VOP_READDIR(uvp, &uio, p->p_ucred, &eofflag, 0, 0);
141 
142 		off = uio.uio_offset;
143 
144 		/* Try again if NFS tosses its cookies */
145 		if (error == EINVAL && tries < 3) {
146 			tries++;
147 			off = 0;
148 			continue;
149 		} else if (error) {
150 			goto out; /* Old userland getcwd() behaviour */
151 		}
152 
153 		cpos = dirbuf;
154 		tries = 0;
155 
156 		/* Scan directory page looking for matching vnode */
157 		for (len = (dirbuflen - uio.uio_resid); len > 0;
158 		     len -= reclen) {
159 			dp = (struct dirent *)cpos;
160 			reclen = dp->d_reclen;
161 
162 			/* Check for malformed directory */
163 			if (reclen < DIRENT_MINSIZE) {
164 				error = EINVAL;
165 				goto out;
166 			}
167 
168 			if (dp->d_fileno == fileno) {
169 				char *bp = *bpp;
170 				bp -= dp->d_namlen;
171 
172 				if (bp <= bufp) {
173 					error = ERANGE;
174 					goto out;
175 				}
176 
177 				bcopy(dp->d_name, bp, dp->d_namlen);
178 				error = 0;
179 				*bpp = bp;
180 
181 				goto out;
182 			}
183 
184 			cpos += reclen;
185 		}
186 
187 	} while (!eofflag);
188 
189 	error = ENOENT;
190 
191 out:
192 
193 	vrele(lvp);
194 	*lvpp = NULL;
195 
196 	free(dirbuf, M_TEMP);
197 
198 	return (error);
199 }
200 
201 /* Do a lookup in the vnode-to-name reverse */
202 int
203 vfs_getcwd_getcache(struct vnode **lvpp, struct vnode **uvpp, char **bpp,
204     char *bufp)
205 {
206 	struct vnode *lvp, *uvp = NULL;
207 	struct proc *p = curproc;
208 	char *obp;
209 	int error, vpid;
210 
211 	lvp = *lvpp;
212 	obp = *bpp;	/* Save original position to restore to on error */
213 
214 	error = cache_revlookup(lvp, uvpp, bpp, bufp);
215 	if (error) {
216 		if (error != -1) {
217 			vput(lvp);
218 			*lvpp = NULL;
219 			*uvpp = NULL;
220 		}
221 
222 		return (error);
223 	}
224 
225 	uvp = *uvpp;
226 	vpid = uvp->v_id;
227 
228 
229 	/* Release current lock before acquiring the parent lock */
230 	VOP_UNLOCK(lvp, 0, p);
231 
232 	error = vget(uvp, LK_EXCLUSIVE | LK_RETRY, p);
233 	if (error)
234 		*uvpp = NULL;
235 
236 	/*
237 	 * Verify that vget() succeeded, and check that vnode capability
238 	 * didn't change while we were waiting for the lock.
239 	 */
240 	if (error || (vpid != uvp->v_id)) {
241 		/*
242 		 * Try to get our lock back. If that works, tell the caller to
243 		 * try things the hard way, otherwise give up.
244 		 */
245 		if (!error)
246 			vput(uvp);
247 
248 		*uvpp = NULL;
249 
250 		error = vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY, p);
251 		if (!error) {
252 			*bpp = obp; /* restore the buffer */
253 			return (-1);
254 		}
255 	}
256 
257 	vrele(lvp);
258 	*lvpp = NULL;
259 
260 	return (error);
261 }
262 
263 #define GETCWD_CHECK_ACCESS 0x0001
264 
265 /* Common routine shared by sys___getcwd() and vn_isunder() */
266 int
267 vfs_getcwd_common(struct vnode *lvp, struct vnode *rvp, char **bpp, char *bufp,
268     int limit, int flags, struct proc *p)
269 {
270 	struct filedesc *fdp = p->p_fd;
271 	struct vnode *uvp = NULL;
272 	char *bp = NULL;
273 	int error, perms = VEXEC;
274 
275 	if (rvp == NULL) {
276 		rvp = fdp->fd_rdir;
277 		if (rvp == NULL)
278 			rvp = rootvnode;
279 	}
280 
281 	vref(rvp);
282 	vref(lvp);
283 
284 	error = vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY, p);
285 	if (error) {
286 		vrele(lvp);
287 		lvp = NULL;
288 		goto out;
289 	}
290 
291 	if (bufp)
292 		bp = *bpp;
293 
294 	if (lvp == rvp) {
295 		if (bp)
296 			*(--bp) = '/';
297 		goto out;
298 	}
299 
300 	/*
301 	 * This loop will terminate when we hit the root, VOP_READDIR() or
302 	 * VOP_LOOKUP() fails, or we run out of space in the user buffer.
303 	 */
304 	do {
305 		if (lvp->v_type != VDIR) {
306 			error = ENOTDIR;
307 			goto out;
308 		}
309 
310 		/* Check for access if caller cares */
311 		if (flags & GETCWD_CHECK_ACCESS) {
312 			error = VOP_ACCESS(lvp, perms, p->p_ucred, p);
313 			if (error)
314 				goto out;
315 			perms = VEXEC|VREAD;
316 		}
317 
318 		/* Step up if we're a covered vnode */
319 		while (lvp->v_flag & VROOT) {
320 			struct vnode *tvp;
321 
322 			if (lvp == rvp)
323 				goto out;
324 
325 			tvp = lvp;
326 			lvp = lvp->v_mount->mnt_vnodecovered;
327 
328 			vput(tvp);
329 
330 			if (lvp == NULL) {
331 				error = ENOENT;
332 				goto out;
333 			}
334 
335 			vref(lvp);
336 
337 			error = vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY, p);
338 			if (error) {
339 				vrele(lvp);
340 				lvp = NULL;
341 				goto out;
342 			}
343 		}
344 
345 		/* Look in the name cache */
346 		error = vfs_getcwd_getcache(&lvp, &uvp, &bp, bufp);
347 
348 		if (error == -1) {
349 			/* If that fails, look in the directory */
350 			error = vfs_getcwd_scandir(&lvp, &uvp, &bp, bufp, p);
351 		}
352 
353 		if (error)
354 			goto out;
355 
356 #ifdef DIAGNOSTIC
357 		if (lvp != NULL)
358 			panic("getcwd: oops, forgot to null lvp");
359 		if (bufp && (bp <= bufp)) {
360 			panic("getcwd: oops, went back too far");
361 		}
362 #endif
363 
364 		if (bp)
365 			*(--bp) = '/';
366 
367 		lvp = uvp;
368 		uvp = NULL;
369 		limit--;
370 
371 	} while ((lvp != rvp) && (limit > 0));
372 
373 out:
374 
375 	if (bpp)
376 		*bpp = bp;
377 
378 	if (uvp)
379 		vput(uvp);
380 
381 	if (lvp)
382 		vput(lvp);
383 
384 	vrele(rvp);
385 
386 	return (error);
387 }
388 
389 /* Find pathname of a process's current directory */
390 int
391 sys___getcwd(struct proc *p, void *v, register_t *retval)
392 {
393 	struct sys___getcwd_args *uap = v;
394 	int error, lenused, len = SCARG(uap, len);
395 	char *path, *bp, *bend;
396 
397 	if (len > MAXPATHLEN * 4)
398 		len = MAXPATHLEN * 4;
399 	else if (len < 2)
400 		return (ERANGE);
401 
402 	path = malloc(len, M_TEMP, M_WAITOK);
403 
404 	bp = &path[len];
405 	bend = bp;
406 	*(--bp) = '\0';
407 
408 	/*
409 	 * 5th argument here is "max number of vnodes to traverse".
410 	 * Since each entry takes up at least 2 bytes in the output
411 	 * buffer, limit it to N/2 vnodes for an N byte buffer.
412 	 */
413 	error = vfs_getcwd_common(p->p_fd->fd_cdir, NULL, &bp, path, len/2,
414 	    GETCWD_CHECK_ACCESS, p);
415 
416 	if (error)
417 		goto out;
418 
419 	lenused = bend - bp;
420 	*retval = lenused;
421 
422 	/* Put the result into user buffer */
423 	error = copyout(bp, SCARG(uap, buf), lenused);
424 
425 out:
426 	free(path, M_TEMP);
427 
428 	return (error);
429 }
430