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