| | varnish-cache/lib/libvarnish/vfil.c |
0 |
|
/*- |
1 |
|
* Copyright (c) 2006 Verdens Gang AS |
2 |
|
* Copyright (c) 2006-2011 Varnish Software AS |
3 |
|
* All rights reserved. |
4 |
|
* |
5 |
|
* Author: Dag-Erling Smørgrav <des@des.no> |
6 |
|
* |
7 |
|
* SPDX-License-Identifier: BSD-2-Clause |
8 |
|
* |
9 |
|
* Redistribution and use in source and binary forms, with or without |
10 |
|
* modification, are permitted provided that the following conditions |
11 |
|
* are met: |
12 |
|
* 1. Redistributions of source code must retain the above copyright |
13 |
|
* notice, this list of conditions and the following disclaimer. |
14 |
|
* 2. Redistributions in binary form must reproduce the above copyright |
15 |
|
* notice, this list of conditions and the following disclaimer in the |
16 |
|
* documentation and/or other materials provided with the distribution. |
17 |
|
* |
18 |
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
19 |
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
20 |
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
21 |
|
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
22 |
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
23 |
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
24 |
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
25 |
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
26 |
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
27 |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
28 |
|
* SUCH DAMAGE. |
29 |
|
*/ |
30 |
|
|
31 |
|
#include "config.h" |
32 |
|
|
33 |
|
#include <time.h> |
34 |
|
#include <sys/types.h> |
35 |
|
#include <sys/stat.h> |
36 |
|
#include <fcntl.h> |
37 |
|
#include <limits.h> |
38 |
|
#include <stdio.h> |
39 |
|
#include <stdlib.h> |
40 |
|
#include <unistd.h> |
41 |
|
#include <stdint.h> |
42 |
|
#include <string.h> |
43 |
|
#ifdef HAVE_SYS_MOUNT_H |
44 |
|
# include <sys/param.h> |
45 |
|
# include <sys/mount.h> |
46 |
|
#endif |
47 |
|
#ifdef HAVE_SYS_STATVFS_H |
48 |
|
# include <sys/statvfs.h> |
49 |
|
#endif |
50 |
|
#ifdef HAVE_SYS_VFS_H |
51 |
|
# include <sys/vfs.h> |
52 |
|
#endif |
53 |
|
#ifdef HAVE_FALLOCATE |
54 |
|
# include <linux/magic.h> |
55 |
|
#endif |
56 |
|
|
57 |
|
#include "vdef.h" |
58 |
|
|
59 |
|
#include "miniobj.h" |
60 |
|
#include "vas.h" |
61 |
|
#include "vsb.h" |
62 |
|
#include "vfil.h" |
63 |
|
#include "vqueue.h" |
64 |
|
|
65 |
|
void |
66 |
342847 |
VFIL_null_fd(int target) |
67 |
|
{ |
68 |
|
int fd; |
69 |
|
|
70 |
342847 |
assert(target >= 0); |
71 |
342847 |
fd = open("/dev/null", O_RDWR); |
72 |
342847 |
assert(fd >= 0); |
73 |
342847 |
assert(dup2(fd, target) == target); |
74 |
342847 |
closefd(&fd); |
75 |
342847 |
} |
76 |
|
|
77 |
|
static char * |
78 |
78975 |
vfil_readfd(int fd, ssize_t *sz) |
79 |
|
{ |
80 |
|
struct stat st; |
81 |
|
char *f; |
82 |
|
int i; |
83 |
|
|
84 |
78975 |
AZ(fstat(fd, &st)); |
85 |
78975 |
if (!S_ISREG(st.st_mode)) |
86 |
0 |
return (NULL); |
87 |
78975 |
f = malloc(st.st_size + 1); |
88 |
78975 |
assert(f != NULL); |
89 |
78975 |
i = read(fd, f, st.st_size + 1); |
90 |
78975 |
if (i != st.st_size) { |
91 |
0 |
free(f); |
92 |
0 |
return (NULL); |
93 |
|
} |
94 |
78975 |
f[i] = '\0'; |
95 |
78975 |
if (sz != NULL) |
96 |
275 |
*sz = st.st_size; |
97 |
78975 |
return (f); |
98 |
78975 |
} |
99 |
|
|
100 |
|
static int |
101 |
350 |
vfil_writefd(int fd, const char *buf, size_t sz) |
102 |
|
{ |
103 |
|
ssize_t len; |
104 |
|
|
105 |
700 |
while (sz > 0) { |
106 |
350 |
len = write(fd, buf, sz); |
107 |
350 |
if (len < 0) |
108 |
0 |
return (len); |
109 |
350 |
if (len == 0) |
110 |
0 |
break; |
111 |
350 |
buf += len; |
112 |
350 |
sz -= len; |
113 |
|
} |
114 |
|
|
115 |
350 |
return (sz == 0 ? 0 : -1); |
116 |
350 |
} |
117 |
|
|
118 |
|
static int |
119 |
79525 |
vfil_openfile(const char *pfx, const char *fn, int flags, int mode) |
120 |
|
{ |
121 |
|
char fnb[PATH_MAX + 1]; |
122 |
|
|
123 |
79525 |
if (fn[0] != '/' && pfx != NULL) { |
124 |
|
/* XXX: graceful length check */ |
125 |
0 |
bprintf(fnb, "/%s/%s", pfx, fn); |
126 |
0 |
fn = fnb; |
127 |
0 |
} |
128 |
|
|
129 |
79525 |
if (flags & O_CREAT) |
130 |
350 |
return (open(fn, flags, mode)); |
131 |
|
else |
132 |
79175 |
return (open(fn, flags)); |
133 |
79525 |
} |
134 |
|
|
135 |
|
char * |
136 |
79175 |
VFIL_readfile(const char *pfx, const char *fn, ssize_t *sz) |
137 |
|
{ |
138 |
|
int fd, err; |
139 |
|
char *r; |
140 |
|
|
141 |
79175 |
fd = vfil_openfile(pfx, fn, O_RDONLY, 0); |
142 |
79175 |
if (fd < 0) |
143 |
200 |
return (NULL); |
144 |
78975 |
r = vfil_readfd(fd, sz); |
145 |
78975 |
err = errno; |
146 |
78975 |
closefd(&fd); |
147 |
78975 |
errno = err; |
148 |
78975 |
return (r); |
149 |
79175 |
} |
150 |
|
|
151 |
|
int |
152 |
350 |
VFIL_writefile(const char *pfx, const char *fn, const char *buf, size_t sz) |
153 |
|
{ |
154 |
|
int fd, err; |
155 |
|
int r; |
156 |
|
|
157 |
350 |
fd = vfil_openfile(pfx, fn, O_WRONLY|O_CREAT|O_TRUNC, 0660); |
158 |
350 |
if (fd < 0) |
159 |
0 |
return (fd); |
160 |
350 |
r = vfil_writefd(fd, buf, sz); |
161 |
350 |
err = errno; |
162 |
350 |
closefd(&fd); |
163 |
350 |
errno = err; |
164 |
350 |
return (r); |
165 |
350 |
} |
166 |
|
|
167 |
|
int |
168 |
22775 |
VFIL_nonblocking(int fd) |
169 |
|
{ |
170 |
|
int i; |
171 |
|
|
172 |
22775 |
i = fcntl(fd, F_GETFL); |
173 |
22775 |
assert(i != -1); |
174 |
22775 |
i |= O_NONBLOCK; |
175 |
22775 |
i = fcntl(fd, F_SETFL, i); |
176 |
22775 |
assert(i != -1); |
177 |
22775 |
return (i); |
178 |
|
} |
179 |
|
|
180 |
|
/* |
181 |
|
* Get file system information from an fd |
182 |
|
* Returns block size, total size and space available in the passed pointers |
183 |
|
* Returns 0 on success, or -1 on failure with errno set |
184 |
|
*/ |
185 |
|
int |
186 |
987433 |
VFIL_fsinfo(int fd, unsigned *pbs, uintmax_t *psize, uintmax_t *pspace) |
187 |
|
{ |
188 |
|
unsigned bs; |
189 |
|
uintmax_t size, space; |
190 |
|
#if defined(HAVE_SYS_STATVFS_H) |
191 |
|
struct statvfs fsst; |
192 |
|
|
193 |
987433 |
if (fstatvfs(fd, &fsst)) |
194 |
0 |
return (-1); |
195 |
987433 |
bs = fsst.f_frsize; |
196 |
987433 |
size = fsst.f_blocks * fsst.f_frsize; |
197 |
987433 |
space = fsst.f_bavail * fsst.f_frsize; |
198 |
|
#elif defined(HAVE_SYS_MOUNT_H) || defined(HAVE_SYS_VFS_H) |
199 |
|
struct statfs fsst; |
200 |
|
|
201 |
|
if (fstatfs(fd, &fsst)) |
202 |
|
return (-1); |
203 |
|
bs = fsst.f_bsize; |
204 |
|
size = fsst.f_blocks * fsst.f_bsize; |
205 |
|
space = fsst.f_bavail * fsst.f_bsize; |
206 |
|
#else |
207 |
|
#error no struct statfs / struct statvfs |
208 |
|
#endif |
209 |
|
|
210 |
987433 |
if (pbs) |
211 |
1100 |
*pbs = bs; |
212 |
987433 |
if (psize) |
213 |
1100 |
*psize = size; |
214 |
987433 |
if (pspace) |
215 |
986333 |
*pspace = space; |
216 |
987433 |
return (0); |
217 |
987433 |
} |
218 |
|
|
219 |
|
/* |
220 |
|
* Make sure that the file system can accommodate the file of the given |
221 |
|
* size. Will use fallocate if available. If fallocate is not available |
222 |
|
* and insist is true, it will write size zero bytes. |
223 |
|
* |
224 |
|
* Returns 0 on success, -1 on failure with errno set. |
225 |
|
*/ |
226 |
|
|
227 |
|
int |
228 |
986333 |
VFIL_allocate(int fd, uintmax_t size, int insist) |
229 |
|
{ |
230 |
|
struct stat st; |
231 |
|
uintmax_t fsspace; |
232 |
|
size_t l; |
233 |
|
ssize_t l2, l3; |
234 |
|
char *buf; |
235 |
|
ssize_t bufsiz; |
236 |
986333 |
int retval = 0; |
237 |
|
|
238 |
986333 |
if (ftruncate(fd, size)) |
239 |
0 |
return (-1); |
240 |
986333 |
if (fstat(fd, &st)) |
241 |
0 |
return (-1); |
242 |
986333 |
if (VFIL_fsinfo(fd, NULL, NULL, &fsspace)) |
243 |
0 |
return (-1); |
244 |
986333 |
if ((st.st_blocks * 512) + fsspace < size) { |
245 |
|
/* Sum of currently allocated blocks and available space |
246 |
|
is less than requested size */ |
247 |
0 |
errno = ENOSPC; |
248 |
0 |
return (-1); |
249 |
|
} |
250 |
|
#if defined(__linux__) && defined(HAVE_FALLOCATE) |
251 |
|
{ |
252 |
|
/* fallocate will for some filesystems (e.g. xfs) not take |
253 |
|
the already allocated blocks of the file into |
254 |
|
account. This will cause fallocate to report ENOSPC |
255 |
|
when called on an existing fully allocated file unless |
256 |
|
the filesystem has enough free space to accommodate the |
257 |
|
complete new file size. Because of this we enable |
258 |
|
fallocate only on filesystems that are known to work as |
259 |
|
we expect. */ |
260 |
|
struct statfs stfs; |
261 |
|
if (!fstatfs(fd, &stfs) && stfs.f_type == EXT4_SUPER_MAGIC) { |
262 |
|
if (!fallocate(fd, 0, 0, size)) |
263 |
|
return (0); |
264 |
|
if (errno == ENOSPC) |
265 |
|
return (-1); |
266 |
|
} |
267 |
|
} |
268 |
|
#endif |
269 |
986333 |
if (!insist) |
270 |
125 |
return (0); |
271 |
|
|
272 |
|
/* Write size zero bytes to make sure the entire file is allocated |
273 |
|
in the file system */ |
274 |
986208 |
if (size > 65536) |
275 |
22197 |
bufsiz = 64 * 1024; |
276 |
|
else |
277 |
964011 |
bufsiz = size; |
278 |
986208 |
buf = calloc(1, bufsiz); |
279 |
986208 |
AN(buf); |
280 |
986208 |
assert(lseek(fd, 0, SEEK_SET) == 0); |
281 |
2720427 |
for (l = 0; l < size; l += l2) { |
282 |
1734219 |
l2 = bufsiz; |
283 |
1734219 |
if (l + l2 > size) |
284 |
0 |
l2 = size - l; |
285 |
1734219 |
l3 = write(fd, buf, l2); |
286 |
1734219 |
if (l3 != l2) { |
287 |
0 |
retval = -1; |
288 |
0 |
break; |
289 |
|
} |
290 |
1734219 |
} |
291 |
986208 |
assert(lseek(fd, 0, SEEK_SET) == 0); |
292 |
986208 |
free(buf); |
293 |
986208 |
return (retval); |
294 |
986333 |
} |
295 |
|
|
296 |
|
struct vfil_dir { |
297 |
|
unsigned magic; |
298 |
|
#define VFIL_DIR_MAGIC 0x3e214967 |
299 |
|
char *dir; |
300 |
|
VTAILQ_ENTRY(vfil_dir) list; |
301 |
|
}; |
302 |
|
|
303 |
|
struct vfil_path { |
304 |
|
unsigned magic; |
305 |
|
#define VFIL_PATH_MAGIC 0x92dbcc31 |
306 |
|
char *str; |
307 |
|
VTAILQ_HEAD(,vfil_dir) paths; |
308 |
|
}; |
309 |
|
|
310 |
|
/* |
311 |
|
* Path searching functions |
312 |
|
*/ |
313 |
|
|
314 |
|
void |
315 |
73325 |
VFIL_setpath(struct vfil_path **pp, const char *path) |
316 |
|
{ |
317 |
|
struct vfil_path *vp; |
318 |
|
struct vfil_dir *vd; |
319 |
|
char *p, *q; |
320 |
|
|
321 |
73325 |
AN(pp); |
322 |
73325 |
AN(path); |
323 |
|
|
324 |
73325 |
vp = *pp; |
325 |
73325 |
if (vp == NULL) { |
326 |
73225 |
ALLOC_OBJ(vp, VFIL_PATH_MAGIC); |
327 |
73225 |
AN(vp); |
328 |
73225 |
VTAILQ_INIT(&vp->paths); |
329 |
73225 |
*pp = vp; |
330 |
73225 |
} |
331 |
73325 |
REPLACE(vp->str, path); |
332 |
73525 |
while (!VTAILQ_EMPTY(&vp->paths)) { |
333 |
200 |
vd = VTAILQ_FIRST(&vp->paths); |
334 |
200 |
CHECK_OBJ_NOTNULL(vd, VFIL_DIR_MAGIC); |
335 |
200 |
VTAILQ_REMOVE(&vp->paths, vd, list); |
336 |
200 |
FREE_OBJ(vd); |
337 |
|
} |
338 |
183075 |
for (p = vp->str; p != NULL; p = q) { |
339 |
109750 |
q = strchr(p, ':'); |
340 |
109750 |
if (q != NULL) |
341 |
36425 |
*q++ = '\0'; |
342 |
109750 |
ALLOC_OBJ(vd, VFIL_DIR_MAGIC); |
343 |
109750 |
AN(vd); |
344 |
109750 |
vd->dir = p; |
345 |
109750 |
VTAILQ_INSERT_TAIL(&vp->paths, vd, list); |
346 |
109750 |
} |
347 |
73325 |
} |
348 |
|
|
349 |
|
static int |
350 |
1550 |
vfil_path_openfile(void *priv, const char *fn) |
351 |
|
{ |
352 |
|
char *p, **pp; |
353 |
|
|
354 |
1550 |
AN(priv); |
355 |
1550 |
AN(fn); |
356 |
1550 |
p = VFIL_readfile(NULL, fn, NULL); |
357 |
1550 |
if (p == NULL) |
358 |
50 |
return (1); |
359 |
|
|
360 |
1500 |
pp = priv; |
361 |
1500 |
*pp = p; |
362 |
1500 |
return (0); |
363 |
1550 |
} |
364 |
|
|
365 |
|
int |
366 |
15750 |
VFIL_searchpath(const struct vfil_path *vp, vfil_path_func_f *func, void *priv, |
367 |
|
const char *fni, char **fno) |
368 |
|
{ |
369 |
|
struct vsb *vsb; |
370 |
|
struct vfil_dir *vd; |
371 |
|
int i, e; |
372 |
|
|
373 |
15750 |
CHECK_OBJ_NOTNULL(vp, VFIL_PATH_MAGIC); |
374 |
15750 |
AN(fno); |
375 |
15750 |
*fno = NULL; |
376 |
|
|
377 |
15750 |
if (func == NULL) { |
378 |
1600 |
func = vfil_path_openfile; |
379 |
1600 |
AN(priv); |
380 |
1600 |
} |
381 |
|
|
382 |
15750 |
if (*fni == '/') { |
383 |
1275 |
i = func(priv, fni); |
384 |
1275 |
if (i <= 0) |
385 |
1225 |
REPLACE(*fno, fni); |
386 |
1275 |
return (i); |
387 |
|
} |
388 |
14475 |
vsb = VSB_new_auto(); |
389 |
14475 |
AN(vsb); |
390 |
14625 |
VTAILQ_FOREACH(vd, &vp->paths, list) { |
391 |
14550 |
VSB_clear(vsb); |
392 |
14550 |
VSB_printf(vsb, "%s/%s", vd->dir, fni); |
393 |
14550 |
AZ(VSB_finish(vsb)); |
394 |
14550 |
if (access(VSB_data(vsb), F_OK)) |
395 |
150 |
continue; |
396 |
14400 |
i = func(priv, VSB_data(vsb)); |
397 |
14400 |
if (i <= 0) { |
398 |
14400 |
e = errno; |
399 |
14400 |
*fno = strdup(VSB_data(vsb)); |
400 |
14400 |
AN(*fno); |
401 |
14400 |
VSB_destroy(&vsb); |
402 |
14400 |
errno = e; |
403 |
14400 |
return (i); |
404 |
|
} |
405 |
0 |
} |
406 |
75 |
VSB_destroy(&vsb); |
407 |
75 |
return (-1); |
408 |
15750 |
} |