Commit | Line | Data |
---|---|---|
81b86775 DG |
1 | /* |
2 | * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com> | |
66495845 | 3 | * Copyright (C) 2013 - Raphaël Beamonte <raphael.beamonte@gmail.com> |
8db0dc00 | 4 | * Copyright (C) 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
81b86775 DG |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License, version 2 only, as | |
8 | * published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
13 | * more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along with | |
16 | * this program; if not, write to the Free Software Foundation, Inc., 51 | |
17 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 | */ | |
19 | ||
20 | #define _GNU_SOURCE | |
6c1c0768 | 21 | #define _LGPL_SOURCE |
35f90c40 | 22 | #include <assert.h> |
81b86775 DG |
23 | #include <ctype.h> |
24 | #include <fcntl.h> | |
25 | #include <limits.h> | |
26 | #include <stdlib.h> | |
2d851108 | 27 | #include <sys/stat.h> |
0c7bcad5 | 28 | #include <sys/types.h> |
2d851108 | 29 | #include <unistd.h> |
fe4477ee | 30 | #include <inttypes.h> |
6c71277b | 31 | #include <grp.h> |
fb198a11 | 32 | #include <pwd.h> |
c9cb3e7d | 33 | #include <sys/file.h> |
3d071855 | 34 | #include <dirent.h> |
81b86775 DG |
35 | |
36 | #include <common/common.h> | |
fe4477ee | 37 | #include <common/runas.h> |
e8fa9fb0 | 38 | #include <common/compat/getenv.h> |
f5436bfc | 39 | #include <common/compat/string.h> |
81b86775 DG |
40 | |
41 | #include "utils.h" | |
feb0f3e5 | 42 | #include "defaults.h" |
81b86775 | 43 | |
5154230f RB |
44 | /* |
45 | * Return a partial realpath(3) of the path even if the full path does not | |
46 | * exist. For instance, with /tmp/test1/test2/test3, if test2/ does not exist | |
47 | * but the /tmp/test1 does, the real path for /tmp/test1 is concatened with | |
48 | * /test2/test3 then returned. In normal time, realpath(3) fails if the end | |
49 | * point directory does not exist. | |
50 | * In case resolved_path is NULL, the string returned was allocated in the | |
51 | * function and thus need to be freed by the caller. The size argument allows | |
52 | * to specify the size of the resolved_path argument if given, or the size to | |
53 | * allocate. | |
54 | */ | |
55 | LTTNG_HIDDEN | |
56 | char *utils_partial_realpath(const char *path, char *resolved_path, size_t size) | |
57 | { | |
9482daac | 58 | char *cut_path = NULL, *try_path = NULL, *try_path_prev = NULL; |
5154230f RB |
59 | const char *next, *prev, *end; |
60 | ||
61 | /* Safety net */ | |
62 | if (path == NULL) { | |
63 | goto error; | |
64 | } | |
65 | ||
66 | /* | |
67 | * Identify the end of the path, we don't want to treat the | |
68 | * last char if it is a '/', we will just keep it on the side | |
69 | * to be added at the end, and return a value coherent with | |
70 | * the path given as argument | |
71 | */ | |
72 | end = path + strlen(path); | |
73 | if (*(end-1) == '/') { | |
74 | end--; | |
75 | } | |
76 | ||
77 | /* Initiate the values of the pointers before looping */ | |
78 | next = path; | |
79 | prev = next; | |
80 | /* Only to ensure try_path is not NULL to enter the while */ | |
81 | try_path = (char *)next; | |
82 | ||
83 | /* Resolve the canonical path of the first part of the path */ | |
84 | while (try_path != NULL && next != end) { | |
85 | /* | |
86 | * If there is not any '/' left, we want to try with | |
87 | * the full path | |
88 | */ | |
89 | next = strpbrk(next + 1, "/"); | |
90 | if (next == NULL) { | |
91 | next = end; | |
92 | } | |
93 | ||
94 | /* Cut the part we will be trying to resolve */ | |
f5436bfc | 95 | cut_path = lttng_strndup(path, next - path); |
d9dbcf5e | 96 | if (cut_path == NULL) { |
f5436bfc | 97 | PERROR("lttng_strndup"); |
d9dbcf5e MD |
98 | goto error; |
99 | } | |
5154230f RB |
100 | |
101 | /* Try to resolve this part */ | |
102 | try_path = realpath((char *)cut_path, NULL); | |
103 | if (try_path == NULL) { | |
104 | /* | |
105 | * There was an error, we just want to be assured it | |
106 | * is linked to an unexistent directory, if it's another | |
107 | * reason, we spawn an error | |
108 | */ | |
109 | switch (errno) { | |
110 | case ENOENT: | |
111 | /* Ignore the error */ | |
112 | break; | |
113 | default: | |
114 | PERROR("realpath (partial_realpath)"); | |
115 | goto error; | |
116 | break; | |
117 | } | |
118 | } else { | |
119 | /* Save the place we are before trying the next step */ | |
120 | free(try_path_prev); | |
121 | try_path_prev = try_path; | |
122 | prev = next; | |
123 | } | |
124 | ||
125 | /* Free the allocated memory */ | |
126 | free(cut_path); | |
c14cc491 | 127 | cut_path = NULL; |
494a8e99 | 128 | } |
5154230f RB |
129 | |
130 | /* Allocate memory for the resolved path if necessary */ | |
131 | if (resolved_path == NULL) { | |
132 | resolved_path = zmalloc(size); | |
133 | if (resolved_path == NULL) { | |
134 | PERROR("zmalloc resolved path"); | |
135 | goto error; | |
136 | } | |
137 | } | |
138 | ||
139 | /* | |
140 | * If we were able to solve at least partially the path, we can concatenate | |
141 | * what worked and what didn't work | |
142 | */ | |
143 | if (try_path_prev != NULL) { | |
144 | /* If we risk to concatenate two '/', we remove one of them */ | |
145 | if (try_path_prev[strlen(try_path_prev) - 1] == '/' && prev[0] == '/') { | |
146 | try_path_prev[strlen(try_path_prev) - 1] = '\0'; | |
147 | } | |
148 | ||
149 | /* | |
150 | * Duplicate the memory used by prev in case resolved_path and | |
151 | * path are pointers for the same memory space | |
152 | */ | |
153 | cut_path = strdup(prev); | |
d9dbcf5e MD |
154 | if (cut_path == NULL) { |
155 | PERROR("strdup"); | |
156 | goto error; | |
157 | } | |
5154230f RB |
158 | |
159 | /* Concatenate the strings */ | |
160 | snprintf(resolved_path, size, "%s%s", try_path_prev, cut_path); | |
161 | ||
162 | /* Free the allocated memory */ | |
163 | free(cut_path); | |
164 | free(try_path_prev); | |
494a8e99 JG |
165 | cut_path = NULL; |
166 | try_path_prev = NULL; | |
5154230f RB |
167 | /* |
168 | * Else, we just copy the path in our resolved_path to | |
169 | * return it as is | |
170 | */ | |
171 | } else { | |
172 | strncpy(resolved_path, path, size); | |
173 | } | |
174 | ||
175 | /* Then we return the 'partially' resolved path */ | |
176 | return resolved_path; | |
177 | ||
178 | error: | |
179 | free(resolved_path); | |
9482daac | 180 | free(cut_path); |
5154230f RB |
181 | return NULL; |
182 | } | |
183 | ||
81b86775 | 184 | /* |
3d229795 RB |
185 | * Make a full resolution of the given path even if it doesn't exist. |
186 | * This function uses the utils_partial_realpath function to resolve | |
187 | * symlinks and relatives paths at the start of the string, and | |
188 | * implements functionnalities to resolve the './' and '../' strings | |
189 | * in the middle of a path. This function is only necessary because | |
190 | * realpath(3) does not accept to resolve unexistent paths. | |
191 | * The returned string was allocated in the function, it is thus of | |
192 | * the responsibility of the caller to free this memory. | |
81b86775 | 193 | */ |
90e535ef | 194 | LTTNG_HIDDEN |
81b86775 DG |
195 | char *utils_expand_path(const char *path) |
196 | { | |
3d229795 | 197 | char *next, *previous, *slash, *start_path, *absolute_path = NULL; |
5de083f4 RB |
198 | char *last_token; |
199 | int is_dot, is_dotdot; | |
81b86775 DG |
200 | |
201 | /* Safety net */ | |
202 | if (path == NULL) { | |
203 | goto error; | |
204 | } | |
205 | ||
3d229795 RB |
206 | /* Allocate memory for the absolute_path */ |
207 | absolute_path = zmalloc(PATH_MAX); | |
208 | if (absolute_path == NULL) { | |
81b86775 DG |
209 | PERROR("zmalloc expand path"); |
210 | goto error; | |
211 | } | |
212 | ||
3d229795 RB |
213 | /* |
214 | * If the path is not already absolute nor explicitly relative, | |
215 | * consider we're in the current directory | |
216 | */ | |
217 | if (*path != '/' && strncmp(path, "./", 2) != 0 && | |
218 | strncmp(path, "../", 3) != 0) { | |
219 | snprintf(absolute_path, PATH_MAX, "./%s", path); | |
2dcd84b7 | 220 | /* Else, we just copy the path */ |
116f95d9 | 221 | } else { |
3d229795 RB |
222 | strncpy(absolute_path, path, PATH_MAX); |
223 | } | |
116f95d9 | 224 | |
3d229795 RB |
225 | /* Resolve partially our path */ |
226 | absolute_path = utils_partial_realpath(absolute_path, | |
227 | absolute_path, PATH_MAX); | |
116f95d9 | 228 | |
3d229795 RB |
229 | /* As long as we find '/./' in the working_path string */ |
230 | while ((next = strstr(absolute_path, "/./"))) { | |
116f95d9 | 231 | |
3d229795 | 232 | /* We prepare the start_path not containing it */ |
f5436bfc | 233 | start_path = lttng_strndup(absolute_path, next - absolute_path); |
d9dbcf5e | 234 | if (!start_path) { |
f5436bfc | 235 | PERROR("lttng_strndup"); |
d9dbcf5e MD |
236 | goto error; |
237 | } | |
3d229795 RB |
238 | /* And we concatenate it with the part after this string */ |
239 | snprintf(absolute_path, PATH_MAX, "%s%s", start_path, next + 2); | |
116f95d9 | 240 | |
3d229795 RB |
241 | free(start_path); |
242 | } | |
116f95d9 | 243 | |
3d229795 RB |
244 | /* As long as we find '/../' in the working_path string */ |
245 | while ((next = strstr(absolute_path, "/../"))) { | |
246 | /* We find the last level of directory */ | |
247 | previous = absolute_path; | |
248 | while ((slash = strpbrk(previous, "/")) && slash != next) { | |
249 | previous = slash + 1; | |
81b86775 | 250 | } |
81b86775 | 251 | |
3d229795 | 252 | /* Then we prepare the start_path not containing it */ |
f5436bfc | 253 | start_path = lttng_strndup(absolute_path, previous - absolute_path); |
d9dbcf5e | 254 | if (!start_path) { |
f5436bfc | 255 | PERROR("lttng_strndup"); |
d9dbcf5e MD |
256 | goto error; |
257 | } | |
3d229795 RB |
258 | |
259 | /* And we concatenate it with the part after the '/../' */ | |
260 | snprintf(absolute_path, PATH_MAX, "%s%s", start_path, next + 4); | |
261 | ||
262 | /* We can free the memory used for the start path*/ | |
263 | free(start_path); | |
264 | ||
265 | /* Then we verify for symlinks using partial_realpath */ | |
266 | absolute_path = utils_partial_realpath(absolute_path, | |
267 | absolute_path, PATH_MAX); | |
116f95d9 | 268 | } |
81b86775 | 269 | |
5de083f4 RB |
270 | /* Identify the last token */ |
271 | last_token = strrchr(absolute_path, '/'); | |
272 | ||
273 | /* Verify that this token is not a relative path */ | |
274 | is_dotdot = (strcmp(last_token, "/..") == 0); | |
275 | is_dot = (strcmp(last_token, "/.") == 0); | |
276 | ||
277 | /* If it is, take action */ | |
278 | if (is_dot || is_dotdot) { | |
279 | /* For both, remove this token */ | |
280 | *last_token = '\0'; | |
281 | ||
282 | /* If it was a reference to parent directory, go back one more time */ | |
283 | if (is_dotdot) { | |
284 | last_token = strrchr(absolute_path, '/'); | |
285 | ||
286 | /* If there was only one level left, we keep the first '/' */ | |
287 | if (last_token == absolute_path) { | |
288 | last_token++; | |
289 | } | |
290 | ||
291 | *last_token = '\0'; | |
292 | } | |
293 | } | |
294 | ||
3d229795 | 295 | return absolute_path; |
81b86775 DG |
296 | |
297 | error: | |
3d229795 | 298 | free(absolute_path); |
81b86775 DG |
299 | return NULL; |
300 | } | |
301 | ||
302 | /* | |
303 | * Create a pipe in dst. | |
304 | */ | |
90e535ef | 305 | LTTNG_HIDDEN |
81b86775 DG |
306 | int utils_create_pipe(int *dst) |
307 | { | |
308 | int ret; | |
309 | ||
310 | if (dst == NULL) { | |
311 | return -1; | |
312 | } | |
313 | ||
314 | ret = pipe(dst); | |
315 | if (ret < 0) { | |
316 | PERROR("create pipe"); | |
317 | } | |
318 | ||
319 | return ret; | |
320 | } | |
321 | ||
322 | /* | |
323 | * Create pipe and set CLOEXEC flag to both fd. | |
324 | * | |
325 | * Make sure the pipe opened by this function are closed at some point. Use | |
326 | * utils_close_pipe(). | |
327 | */ | |
90e535ef | 328 | LTTNG_HIDDEN |
81b86775 DG |
329 | int utils_create_pipe_cloexec(int *dst) |
330 | { | |
331 | int ret, i; | |
332 | ||
333 | if (dst == NULL) { | |
334 | return -1; | |
335 | } | |
336 | ||
337 | ret = utils_create_pipe(dst); | |
338 | if (ret < 0) { | |
339 | goto error; | |
340 | } | |
341 | ||
342 | for (i = 0; i < 2; i++) { | |
343 | ret = fcntl(dst[i], F_SETFD, FD_CLOEXEC); | |
344 | if (ret < 0) { | |
345 | PERROR("fcntl pipe cloexec"); | |
346 | goto error; | |
347 | } | |
348 | } | |
349 | ||
350 | error: | |
351 | return ret; | |
352 | } | |
353 | ||
094f381c MD |
354 | /* |
355 | * Create pipe and set fd flags to FD_CLOEXEC and O_NONBLOCK. | |
356 | * | |
357 | * Make sure the pipe opened by this function are closed at some point. Use | |
358 | * utils_close_pipe(). Using pipe() and fcntl rather than pipe2() to | |
359 | * support OSes other than Linux 2.6.23+. | |
360 | */ | |
361 | LTTNG_HIDDEN | |
362 | int utils_create_pipe_cloexec_nonblock(int *dst) | |
363 | { | |
364 | int ret, i; | |
365 | ||
366 | if (dst == NULL) { | |
367 | return -1; | |
368 | } | |
369 | ||
370 | ret = utils_create_pipe(dst); | |
371 | if (ret < 0) { | |
372 | goto error; | |
373 | } | |
374 | ||
375 | for (i = 0; i < 2; i++) { | |
376 | ret = fcntl(dst[i], F_SETFD, FD_CLOEXEC); | |
377 | if (ret < 0) { | |
378 | PERROR("fcntl pipe cloexec"); | |
379 | goto error; | |
380 | } | |
381 | /* | |
382 | * Note: we override any flag that could have been | |
383 | * previously set on the fd. | |
384 | */ | |
385 | ret = fcntl(dst[i], F_SETFL, O_NONBLOCK); | |
386 | if (ret < 0) { | |
387 | PERROR("fcntl pipe nonblock"); | |
388 | goto error; | |
389 | } | |
390 | } | |
391 | ||
392 | error: | |
393 | return ret; | |
394 | } | |
395 | ||
81b86775 DG |
396 | /* |
397 | * Close both read and write side of the pipe. | |
398 | */ | |
90e535ef | 399 | LTTNG_HIDDEN |
81b86775 DG |
400 | void utils_close_pipe(int *src) |
401 | { | |
402 | int i, ret; | |
403 | ||
404 | if (src == NULL) { | |
405 | return; | |
406 | } | |
407 | ||
408 | for (i = 0; i < 2; i++) { | |
409 | /* Safety check */ | |
410 | if (src[i] < 0) { | |
411 | continue; | |
412 | } | |
413 | ||
414 | ret = close(src[i]); | |
415 | if (ret) { | |
416 | PERROR("close pipe"); | |
417 | } | |
418 | } | |
419 | } | |
a4b92340 DG |
420 | |
421 | /* | |
422 | * Create a new string using two strings range. | |
423 | */ | |
90e535ef | 424 | LTTNG_HIDDEN |
a4b92340 DG |
425 | char *utils_strdupdelim(const char *begin, const char *end) |
426 | { | |
427 | char *str; | |
428 | ||
429 | str = zmalloc(end - begin + 1); | |
430 | if (str == NULL) { | |
431 | PERROR("zmalloc strdupdelim"); | |
432 | goto error; | |
433 | } | |
434 | ||
435 | memcpy(str, begin, end - begin); | |
436 | str[end - begin] = '\0'; | |
437 | ||
438 | error: | |
439 | return str; | |
440 | } | |
b662582b DG |
441 | |
442 | /* | |
443 | * Set CLOEXEC flag to the give file descriptor. | |
444 | */ | |
90e535ef | 445 | LTTNG_HIDDEN |
b662582b DG |
446 | int utils_set_fd_cloexec(int fd) |
447 | { | |
448 | int ret; | |
449 | ||
450 | if (fd < 0) { | |
451 | ret = -EINVAL; | |
452 | goto end; | |
453 | } | |
454 | ||
455 | ret = fcntl(fd, F_SETFD, FD_CLOEXEC); | |
456 | if (ret < 0) { | |
457 | PERROR("fcntl cloexec"); | |
458 | ret = -errno; | |
459 | } | |
460 | ||
461 | end: | |
462 | return ret; | |
463 | } | |
35f90c40 DG |
464 | |
465 | /* | |
466 | * Create pid file to the given path and filename. | |
467 | */ | |
90e535ef | 468 | LTTNG_HIDDEN |
35f90c40 DG |
469 | int utils_create_pid_file(pid_t pid, const char *filepath) |
470 | { | |
471 | int ret; | |
472 | FILE *fp; | |
473 | ||
474 | assert(filepath); | |
475 | ||
476 | fp = fopen(filepath, "w"); | |
477 | if (fp == NULL) { | |
478 | PERROR("open pid file %s", filepath); | |
479 | ret = -1; | |
480 | goto error; | |
481 | } | |
482 | ||
483 | ret = fprintf(fp, "%d\n", pid); | |
484 | if (ret < 0) { | |
485 | PERROR("fprintf pid file"); | |
e205d79b | 486 | goto error; |
35f90c40 DG |
487 | } |
488 | ||
e205d79b MD |
489 | if (fclose(fp)) { |
490 | PERROR("fclose"); | |
491 | } | |
35f90c40 | 492 | DBG("Pid %d written in file %s", pid, filepath); |
e205d79b | 493 | ret = 0; |
35f90c40 DG |
494 | error: |
495 | return ret; | |
496 | } | |
2d851108 | 497 | |
c9cb3e7d JG |
498 | /* |
499 | * Create lock file to the given path and filename. | |
500 | * Returns the associated file descriptor, -1 on error. | |
501 | */ | |
502 | LTTNG_HIDDEN | |
503 | int utils_create_lock_file(const char *filepath) | |
504 | { | |
505 | int ret; | |
506 | int fd; | |
77e7fddf | 507 | struct flock lock; |
c9cb3e7d JG |
508 | |
509 | assert(filepath); | |
510 | ||
77e7fddf MJ |
511 | memset(&lock, 0, sizeof(lock)); |
512 | fd = open(filepath, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | | |
513 | S_IRGRP | S_IWGRP); | |
c9cb3e7d JG |
514 | if (fd < 0) { |
515 | PERROR("open lock file %s", filepath); | |
516 | ret = -1; | |
517 | goto error; | |
518 | } | |
519 | ||
520 | /* | |
521 | * Attempt to lock the file. If this fails, there is | |
522 | * already a process using the same lock file running | |
523 | * and we should exit. | |
524 | */ | |
77e7fddf MJ |
525 | lock.l_whence = SEEK_SET; |
526 | lock.l_type = F_WRLCK; | |
527 | ||
528 | ret = fcntl(fd, F_SETLK, &lock); | |
529 | if (ret == -1) { | |
530 | PERROR("fcntl lock file"); | |
208ff148 | 531 | ERR("Could not get lock file %s, another instance is running.", |
c9cb3e7d | 532 | filepath); |
ffb0b851 JG |
533 | if (close(fd)) { |
534 | PERROR("close lock file"); | |
535 | } | |
c9cb3e7d JG |
536 | fd = ret; |
537 | goto error; | |
538 | } | |
539 | ||
540 | error: | |
541 | return fd; | |
542 | } | |
543 | ||
2d851108 | 544 | /* |
d77dded2 | 545 | * Create directory using the given path and mode. |
2d851108 DG |
546 | * |
547 | * On success, return 0 else a negative error code. | |
548 | */ | |
90e535ef | 549 | LTTNG_HIDDEN |
d77dded2 JG |
550 | int utils_mkdir(const char *path, mode_t mode, int uid, int gid) |
551 | { | |
552 | int ret; | |
553 | ||
554 | if (uid < 0 || gid < 0) { | |
555 | ret = mkdir(path, mode); | |
556 | } else { | |
557 | ret = run_as_mkdir(path, mode, uid, gid); | |
558 | } | |
559 | if (ret < 0) { | |
560 | if (errno != EEXIST) { | |
561 | PERROR("mkdir %s, uid %d, gid %d", path ? path : "NULL", | |
562 | uid, gid); | |
563 | } else { | |
564 | ret = 0; | |
565 | } | |
566 | } | |
567 | ||
568 | return ret; | |
569 | } | |
570 | ||
571 | /* | |
572 | * Internal version of mkdir_recursive. Runs as the current user. | |
573 | * Don't call directly; use utils_mkdir_recursive(). | |
574 | * | |
575 | * This function is ominously marked as "unsafe" since it should only | |
576 | * be called by a caller that has transitioned to the uid and gid under which | |
577 | * the directory creation should occur. | |
578 | */ | |
579 | LTTNG_HIDDEN | |
580 | int _utils_mkdir_recursive_unsafe(const char *path, mode_t mode) | |
2d851108 DG |
581 | { |
582 | char *p, tmp[PATH_MAX]; | |
2d851108 DG |
583 | size_t len; |
584 | int ret; | |
585 | ||
586 | assert(path); | |
587 | ||
588 | ret = snprintf(tmp, sizeof(tmp), "%s", path); | |
589 | if (ret < 0) { | |
590 | PERROR("snprintf mkdir"); | |
591 | goto error; | |
592 | } | |
593 | ||
594 | len = ret; | |
595 | if (tmp[len - 1] == '/') { | |
596 | tmp[len - 1] = 0; | |
597 | } | |
598 | ||
599 | for (p = tmp + 1; *p; p++) { | |
600 | if (*p == '/') { | |
601 | *p = 0; | |
602 | if (tmp[strlen(tmp) - 1] == '.' && | |
603 | tmp[strlen(tmp) - 2] == '.' && | |
604 | tmp[strlen(tmp) - 3] == '/') { | |
605 | ERR("Using '/../' is not permitted in the trace path (%s)", | |
606 | tmp); | |
607 | ret = -1; | |
608 | goto error; | |
609 | } | |
0c7bcad5 | 610 | ret = mkdir(tmp, mode); |
2d851108 | 611 | if (ret < 0) { |
0c7bcad5 MD |
612 | if (errno != EEXIST) { |
613 | PERROR("mkdir recursive"); | |
614 | ret = -errno; | |
615 | goto error; | |
2d851108 DG |
616 | } |
617 | } | |
618 | *p = '/'; | |
619 | } | |
620 | } | |
621 | ||
622 | ret = mkdir(tmp, mode); | |
623 | if (ret < 0) { | |
624 | if (errno != EEXIST) { | |
d77dded2 | 625 | PERROR("mkdir recursive last element"); |
2d851108 DG |
626 | ret = -errno; |
627 | } else { | |
628 | ret = 0; | |
629 | } | |
630 | } | |
631 | ||
632 | error: | |
633 | return ret; | |
634 | } | |
fe4477ee | 635 | |
d77dded2 JG |
636 | /* |
637 | * Recursively create directory using the given path and mode, under the | |
638 | * provided uid and gid. | |
639 | * | |
640 | * On success, return 0 else a negative error code. | |
641 | */ | |
642 | LTTNG_HIDDEN | |
643 | int utils_mkdir_recursive(const char *path, mode_t mode, int uid, int gid) | |
644 | { | |
645 | int ret; | |
646 | ||
647 | if (uid < 0 || gid < 0) { | |
648 | /* Run as current user. */ | |
649 | ret = _utils_mkdir_recursive_unsafe(path, mode); | |
650 | } else { | |
651 | ret = run_as_mkdir_recursive(path, mode, uid, gid); | |
652 | } | |
653 | if (ret < 0) { | |
654 | PERROR("mkdir %s, uid %d, gid %d", path ? path : "NULL", | |
655 | uid, gid); | |
656 | } | |
657 | ||
658 | return ret; | |
659 | } | |
660 | ||
fe4477ee | 661 | /* |
d77dded2 | 662 | * path is the output parameter. It needs to be PATH_MAX len. |
fe4477ee JD |
663 | * |
664 | * Return 0 on success or else a negative value. | |
665 | */ | |
7591bab1 MD |
666 | static int utils_stream_file_name(char *path, |
667 | const char *path_name, const char *file_name, | |
668 | uint64_t size, uint64_t count, | |
669 | const char *suffix) | |
fe4477ee | 670 | { |
7591bab1 MD |
671 | int ret; |
672 | char full_path[PATH_MAX]; | |
673 | char *path_name_suffix = NULL; | |
309167d2 | 674 | char *extra = NULL; |
fe4477ee | 675 | |
fe4477ee JD |
676 | ret = snprintf(full_path, sizeof(full_path), "%s/%s", |
677 | path_name, file_name); | |
678 | if (ret < 0) { | |
679 | PERROR("snprintf create output file"); | |
680 | goto error; | |
681 | } | |
682 | ||
309167d2 JD |
683 | /* Setup extra string if suffix or/and a count is needed. */ |
684 | if (size > 0 && suffix) { | |
685 | ret = asprintf(&extra, "_%" PRIu64 "%s", count, suffix); | |
686 | } else if (size > 0) { | |
687 | ret = asprintf(&extra, "_%" PRIu64, count); | |
688 | } else if (suffix) { | |
689 | ret = asprintf(&extra, "%s", suffix); | |
690 | } | |
691 | if (ret < 0) { | |
692 | PERROR("Allocating extra string to name"); | |
693 | goto error; | |
694 | } | |
695 | ||
fe4477ee | 696 | /* |
7591bab1 MD |
697 | * If we split the trace in multiple files, we have to add the count at |
698 | * the end of the tracefile name. | |
fe4477ee | 699 | */ |
309167d2 JD |
700 | if (extra) { |
701 | ret = asprintf(&path_name_suffix, "%s%s", full_path, extra); | |
fe4477ee | 702 | if (ret < 0) { |
309167d2 JD |
703 | PERROR("Allocating path name with extra string"); |
704 | goto error_free_suffix; | |
fe4477ee | 705 | } |
7591bab1 MD |
706 | strncpy(path, path_name_suffix, PATH_MAX - 1); |
707 | path[PATH_MAX - 1] = '\0'; | |
fe4477ee | 708 | } else { |
7591bab1 MD |
709 | strncpy(path, full_path, PATH_MAX - 1); |
710 | } | |
711 | path[PATH_MAX - 1] = '\0'; | |
712 | ret = 0; | |
713 | ||
714 | free(path_name_suffix); | |
715 | error_free_suffix: | |
716 | free(extra); | |
717 | error: | |
718 | return ret; | |
719 | } | |
720 | ||
721 | /* | |
722 | * Create the stream file on disk. | |
723 | * | |
724 | * Return 0 on success or else a negative value. | |
725 | */ | |
726 | LTTNG_HIDDEN | |
727 | int utils_create_stream_file(const char *path_name, char *file_name, uint64_t size, | |
728 | uint64_t count, int uid, int gid, char *suffix) | |
729 | { | |
730 | int ret, flags, mode; | |
731 | char path[PATH_MAX]; | |
732 | ||
733 | ret = utils_stream_file_name(path, path_name, file_name, | |
734 | size, count, suffix); | |
735 | if (ret < 0) { | |
736 | goto error; | |
fe4477ee JD |
737 | } |
738 | ||
be96a7d1 | 739 | flags = O_WRONLY | O_CREAT | O_TRUNC; |
0f907de1 | 740 | /* Open with 660 mode */ |
be96a7d1 DG |
741 | mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; |
742 | ||
743 | if (uid < 0 || gid < 0) { | |
7591bab1 | 744 | ret = open(path, flags, mode); |
be96a7d1 | 745 | } else { |
7591bab1 | 746 | ret = run_as_open(path, flags, mode, uid, gid); |
be96a7d1 | 747 | } |
7591bab1 | 748 | if (ret < 0) { |
fe4477ee | 749 | PERROR("open stream path %s", path); |
fe4477ee | 750 | } |
7591bab1 MD |
751 | error: |
752 | return ret; | |
753 | } | |
fe4477ee | 754 | |
7591bab1 MD |
755 | /* |
756 | * Unlink the stream tracefile from disk. | |
757 | * | |
758 | * Return 0 on success or else a negative value. | |
759 | */ | |
760 | LTTNG_HIDDEN | |
761 | int utils_unlink_stream_file(const char *path_name, char *file_name, uint64_t size, | |
762 | uint64_t count, int uid, int gid, char *suffix) | |
763 | { | |
764 | int ret; | |
765 | char path[PATH_MAX]; | |
766 | ||
767 | ret = utils_stream_file_name(path, path_name, file_name, | |
768 | size, count, suffix); | |
769 | if (ret < 0) { | |
770 | goto error; | |
771 | } | |
772 | if (uid < 0 || gid < 0) { | |
773 | ret = unlink(path); | |
774 | } else { | |
775 | ret = run_as_unlink(path, uid, gid); | |
7591bab1 MD |
776 | } |
777 | if (ret < 0) { | |
778 | goto error; | |
779 | } | |
fe4477ee | 780 | error: |
7591bab1 | 781 | DBG("utils_unlink_stream_file %s returns %d", path, ret); |
fe4477ee JD |
782 | return ret; |
783 | } | |
784 | ||
785 | /* | |
786 | * Change the output tracefile according to the given size and count The | |
787 | * new_count pointer is set during this operation. | |
788 | * | |
789 | * From the consumer, the stream lock MUST be held before calling this function | |
790 | * because we are modifying the stream status. | |
791 | * | |
792 | * Return 0 on success or else a negative value. | |
793 | */ | |
bc182241 | 794 | LTTNG_HIDDEN |
fe4477ee | 795 | int utils_rotate_stream_file(char *path_name, char *file_name, uint64_t size, |
309167d2 JD |
796 | uint64_t count, int uid, int gid, int out_fd, uint64_t *new_count, |
797 | int *stream_fd) | |
fe4477ee JD |
798 | { |
799 | int ret; | |
800 | ||
309167d2 JD |
801 | assert(new_count); |
802 | assert(stream_fd); | |
803 | ||
fe4477ee JD |
804 | ret = close(out_fd); |
805 | if (ret < 0) { | |
806 | PERROR("Closing tracefile"); | |
807 | goto error; | |
808 | } | |
809 | ||
810 | if (count > 0) { | |
7591bab1 MD |
811 | /* |
812 | * In tracefile rotation, for the relay daemon we need | |
813 | * to unlink the old file if present, because it may | |
814 | * still be open in reading by the live thread, and we | |
815 | * need to ensure that we do not overwrite the content | |
816 | * between get_index and get_packet. Since we have no | |
817 | * way to verify integrity of the data content compared | |
818 | * to the associated index, we need to ensure the reader | |
819 | * has exclusive access to the file content, and that | |
820 | * the open of the data file is performed in get_index. | |
821 | * Unlinking the old file rather than overwriting it | |
822 | * achieves this. | |
823 | */ | |
fe4477ee | 824 | *new_count = (*new_count + 1) % count; |
7591bab1 MD |
825 | ret = utils_unlink_stream_file(path_name, file_name, |
826 | size, *new_count, uid, gid, 0); | |
827 | if (ret < 0 && errno != ENOENT) { | |
828 | goto error; | |
829 | } | |
fe4477ee JD |
830 | } else { |
831 | (*new_count)++; | |
832 | } | |
833 | ||
309167d2 JD |
834 | ret = utils_create_stream_file(path_name, file_name, size, *new_count, |
835 | uid, gid, 0); | |
836 | if (ret < 0) { | |
837 | goto error; | |
838 | } | |
839 | *stream_fd = ret; | |
840 | ||
841 | /* Success. */ | |
842 | ret = 0; | |
843 | ||
fe4477ee JD |
844 | error: |
845 | return ret; | |
846 | } | |
70d0b120 | 847 | |
70d0b120 SM |
848 | |
849 | /** | |
850 | * Parse a string that represents a size in human readable format. It | |
5983a922 | 851 | * supports decimal integers suffixed by 'k', 'K', 'M' or 'G'. |
70d0b120 SM |
852 | * |
853 | * The suffix multiply the integer by: | |
854 | * 'k': 1024 | |
855 | * 'M': 1024^2 | |
856 | * 'G': 1024^3 | |
857 | * | |
858 | * @param str The string to parse. | |
5983a922 | 859 | * @param size Pointer to a uint64_t that will be filled with the |
cfa9a5a2 | 860 | * resulting size. |
70d0b120 SM |
861 | * |
862 | * @return 0 on success, -1 on failure. | |
863 | */ | |
00a52467 | 864 | LTTNG_HIDDEN |
5983a922 | 865 | int utils_parse_size_suffix(const char * const str, uint64_t * const size) |
70d0b120 | 866 | { |
70d0b120 | 867 | int ret; |
5983a922 | 868 | uint64_t base_size; |
70d0b120 | 869 | long shift = 0; |
5983a922 SM |
870 | const char *str_end; |
871 | char *num_end; | |
70d0b120 SM |
872 | |
873 | if (!str) { | |
5983a922 | 874 | DBG("utils_parse_size_suffix: received a NULL string."); |
70d0b120 SM |
875 | ret = -1; |
876 | goto end; | |
877 | } | |
878 | ||
5983a922 SM |
879 | /* strtoull will accept a negative number, but we don't want to. */ |
880 | if (strchr(str, '-') != NULL) { | |
881 | DBG("utils_parse_size_suffix: invalid size string, should not contain '-'."); | |
70d0b120 | 882 | ret = -1; |
5983a922 | 883 | goto end; |
70d0b120 SM |
884 | } |
885 | ||
5983a922 SM |
886 | /* str_end will point to the \0 */ |
887 | str_end = str + strlen(str); | |
70d0b120 | 888 | errno = 0; |
5983a922 | 889 | base_size = strtoull(str, &num_end, 0); |
70d0b120 | 890 | if (errno != 0) { |
5983a922 | 891 | PERROR("utils_parse_size_suffix strtoull"); |
70d0b120 | 892 | ret = -1; |
5983a922 SM |
893 | goto end; |
894 | } | |
895 | ||
896 | if (num_end == str) { | |
897 | /* strtoull parsed nothing, not good. */ | |
898 | DBG("utils_parse_size_suffix: strtoull had nothing good to parse."); | |
899 | ret = -1; | |
900 | goto end; | |
901 | } | |
902 | ||
903 | /* Check if a prefix is present. */ | |
904 | switch (*num_end) { | |
905 | case 'G': | |
906 | shift = GIBI_LOG2; | |
907 | num_end++; | |
908 | break; | |
909 | case 'M': /* */ | |
910 | shift = MEBI_LOG2; | |
911 | num_end++; | |
912 | break; | |
913 | case 'K': | |
914 | case 'k': | |
915 | shift = KIBI_LOG2; | |
916 | num_end++; | |
917 | break; | |
918 | case '\0': | |
919 | break; | |
920 | default: | |
921 | DBG("utils_parse_size_suffix: invalid suffix."); | |
922 | ret = -1; | |
923 | goto end; | |
924 | } | |
925 | ||
926 | /* Check for garbage after the valid input. */ | |
927 | if (num_end != str_end) { | |
928 | DBG("utils_parse_size_suffix: Garbage after size string."); | |
929 | ret = -1; | |
930 | goto end; | |
70d0b120 SM |
931 | } |
932 | ||
933 | *size = base_size << shift; | |
934 | ||
935 | /* Check for overflow */ | |
936 | if ((*size >> shift) != base_size) { | |
5983a922 | 937 | DBG("utils_parse_size_suffix: oops, overflow detected."); |
70d0b120 | 938 | ret = -1; |
5983a922 | 939 | goto end; |
70d0b120 SM |
940 | } |
941 | ||
942 | ret = 0; | |
70d0b120 SM |
943 | end: |
944 | return ret; | |
945 | } | |
cfa9a5a2 DG |
946 | |
947 | /* | |
948 | * fls: returns the position of the most significant bit. | |
949 | * Returns 0 if no bit is set, else returns the position of the most | |
950 | * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit). | |
951 | */ | |
952 | #if defined(__i386) || defined(__x86_64) | |
953 | static inline unsigned int fls_u32(uint32_t x) | |
954 | { | |
955 | int r; | |
956 | ||
957 | asm("bsrl %1,%0\n\t" | |
958 | "jnz 1f\n\t" | |
959 | "movl $-1,%0\n\t" | |
960 | "1:\n\t" | |
961 | : "=r" (r) : "rm" (x)); | |
962 | return r + 1; | |
963 | } | |
964 | #define HAS_FLS_U32 | |
965 | #endif | |
966 | ||
967 | #ifndef HAS_FLS_U32 | |
968 | static __attribute__((unused)) unsigned int fls_u32(uint32_t x) | |
969 | { | |
970 | unsigned int r = 32; | |
971 | ||
972 | if (!x) { | |
973 | return 0; | |
974 | } | |
975 | if (!(x & 0xFFFF0000U)) { | |
976 | x <<= 16; | |
977 | r -= 16; | |
978 | } | |
979 | if (!(x & 0xFF000000U)) { | |
980 | x <<= 8; | |
981 | r -= 8; | |
982 | } | |
983 | if (!(x & 0xF0000000U)) { | |
984 | x <<= 4; | |
985 | r -= 4; | |
986 | } | |
987 | if (!(x & 0xC0000000U)) { | |
988 | x <<= 2; | |
989 | r -= 2; | |
990 | } | |
991 | if (!(x & 0x80000000U)) { | |
992 | x <<= 1; | |
993 | r -= 1; | |
994 | } | |
995 | return r; | |
996 | } | |
997 | #endif | |
998 | ||
999 | /* | |
1000 | * Return the minimum order for which x <= (1UL << order). | |
1001 | * Return -1 if x is 0. | |
1002 | */ | |
1003 | LTTNG_HIDDEN | |
1004 | int utils_get_count_order_u32(uint32_t x) | |
1005 | { | |
1006 | if (!x) { | |
1007 | return -1; | |
1008 | } | |
1009 | ||
1010 | return fls_u32(x - 1); | |
1011 | } | |
feb0f3e5 AM |
1012 | |
1013 | /** | |
1014 | * Obtain the value of LTTNG_HOME environment variable, if exists. | |
1015 | * Otherwise returns the value of HOME. | |
1016 | */ | |
00a52467 | 1017 | LTTNG_HIDDEN |
feb0f3e5 AM |
1018 | char *utils_get_home_dir(void) |
1019 | { | |
1020 | char *val = NULL; | |
04135dbd DG |
1021 | struct passwd *pwd; |
1022 | ||
e8fa9fb0 | 1023 | val = lttng_secure_getenv(DEFAULT_LTTNG_HOME_ENV_VAR); |
feb0f3e5 | 1024 | if (val != NULL) { |
04135dbd DG |
1025 | goto end; |
1026 | } | |
e8fa9fb0 | 1027 | val = lttng_secure_getenv(DEFAULT_LTTNG_FALLBACK_HOME_ENV_VAR); |
04135dbd DG |
1028 | if (val != NULL) { |
1029 | goto end; | |
feb0f3e5 | 1030 | } |
04135dbd DG |
1031 | |
1032 | /* Fallback on the password file entry. */ | |
1033 | pwd = getpwuid(getuid()); | |
1034 | if (!pwd) { | |
1035 | goto end; | |
1036 | } | |
1037 | val = pwd->pw_dir; | |
1038 | ||
1039 | DBG3("Home directory is '%s'", val); | |
1040 | ||
1041 | end: | |
1042 | return val; | |
feb0f3e5 | 1043 | } |
26fe5938 | 1044 | |
fb198a11 JG |
1045 | /** |
1046 | * Get user's home directory. Dynamically allocated, must be freed | |
1047 | * by the caller. | |
1048 | */ | |
1049 | LTTNG_HIDDEN | |
1050 | char *utils_get_user_home_dir(uid_t uid) | |
1051 | { | |
1052 | struct passwd pwd; | |
1053 | struct passwd *result; | |
1054 | char *home_dir = NULL; | |
1055 | char *buf = NULL; | |
1056 | long buflen; | |
1057 | int ret; | |
1058 | ||
1059 | buflen = sysconf(_SC_GETPW_R_SIZE_MAX); | |
1060 | if (buflen == -1) { | |
1061 | goto end; | |
1062 | } | |
1063 | retry: | |
1064 | buf = zmalloc(buflen); | |
1065 | if (!buf) { | |
1066 | goto end; | |
1067 | } | |
1068 | ||
1069 | ret = getpwuid_r(uid, &pwd, buf, buflen, &result); | |
1070 | if (ret || !result) { | |
1071 | if (ret == ERANGE) { | |
1072 | free(buf); | |
1073 | buflen *= 2; | |
1074 | goto retry; | |
1075 | } | |
1076 | goto end; | |
1077 | } | |
1078 | ||
1079 | home_dir = strdup(pwd.pw_dir); | |
1080 | end: | |
1081 | free(buf); | |
1082 | return home_dir; | |
1083 | } | |
1084 | ||
fbb9748b JG |
1085 | /* |
1086 | * Obtain the value of LTTNG_KMOD_PROBES environment variable, if exists. | |
c9d42407 | 1087 | * Otherwise returns NULL. |
fbb9748b JG |
1088 | */ |
1089 | LTTNG_HIDDEN | |
1090 | char *utils_get_kmod_probes_list(void) | |
1091 | { | |
e8fa9fb0 | 1092 | return lttng_secure_getenv(DEFAULT_LTTNG_KMOD_PROBES); |
fbb9748b JG |
1093 | } |
1094 | ||
c9d42407 PP |
1095 | /* |
1096 | * Obtain the value of LTTNG_EXTRA_KMOD_PROBES environment variable, if | |
1097 | * exists. Otherwise returns NULL. | |
1098 | */ | |
1099 | LTTNG_HIDDEN | |
1100 | char *utils_get_extra_kmod_probes_list(void) | |
1101 | { | |
e8fa9fb0 | 1102 | return lttng_secure_getenv(DEFAULT_LTTNG_EXTRA_KMOD_PROBES); |
c9d42407 PP |
1103 | } |
1104 | ||
26fe5938 DG |
1105 | /* |
1106 | * With the given format, fill dst with the time of len maximum siz. | |
1107 | * | |
1108 | * Return amount of bytes set in the buffer or else 0 on error. | |
1109 | */ | |
1110 | LTTNG_HIDDEN | |
1111 | size_t utils_get_current_time_str(const char *format, char *dst, size_t len) | |
1112 | { | |
1113 | size_t ret; | |
1114 | time_t rawtime; | |
1115 | struct tm *timeinfo; | |
1116 | ||
1117 | assert(format); | |
1118 | assert(dst); | |
1119 | ||
1120 | /* Get date and time for session path */ | |
1121 | time(&rawtime); | |
1122 | timeinfo = localtime(&rawtime); | |
1123 | ret = strftime(dst, len, format, timeinfo); | |
1124 | if (ret == 0) { | |
68e6efdd | 1125 | ERR("Unable to strftime with format %s at dst %p of len %zu", format, |
26fe5938 DG |
1126 | dst, len); |
1127 | } | |
1128 | ||
1129 | return ret; | |
1130 | } | |
6c71277b MD |
1131 | |
1132 | /* | |
1133 | * Return the group ID matching name, else 0 if it cannot be found. | |
1134 | */ | |
1135 | LTTNG_HIDDEN | |
1136 | gid_t utils_get_group_id(const char *name) | |
1137 | { | |
1138 | struct group *grp; | |
1139 | ||
1140 | grp = getgrnam(name); | |
1141 | if (!grp) { | |
1142 | static volatile int warn_once; | |
1143 | ||
1144 | if (!warn_once) { | |
1145 | WARN("No tracing group detected"); | |
1146 | warn_once = 1; | |
1147 | } | |
1148 | return 0; | |
1149 | } | |
1150 | return grp->gr_gid; | |
1151 | } | |
8db0dc00 JG |
1152 | |
1153 | /* | |
1154 | * Return a newly allocated option string. This string is to be used as the | |
1155 | * optstring argument of getopt_long(), see GETOPT(3). opt_count is the number | |
1156 | * of elements in the long_options array. Returns NULL if the string's | |
1157 | * allocation fails. | |
1158 | */ | |
1159 | LTTNG_HIDDEN | |
1160 | char *utils_generate_optstring(const struct option *long_options, | |
1161 | size_t opt_count) | |
1162 | { | |
1163 | int i; | |
1164 | size_t string_len = opt_count, str_pos = 0; | |
1165 | char *optstring; | |
1166 | ||
1167 | /* | |
1168 | * Compute the necessary string length. One letter per option, two when an | |
1169 | * argument is necessary, and a trailing NULL. | |
1170 | */ | |
1171 | for (i = 0; i < opt_count; i++) { | |
1172 | string_len += long_options[i].has_arg ? 1 : 0; | |
1173 | } | |
1174 | ||
1175 | optstring = zmalloc(string_len); | |
1176 | if (!optstring) { | |
1177 | goto end; | |
1178 | } | |
1179 | ||
1180 | for (i = 0; i < opt_count; i++) { | |
1181 | if (!long_options[i].name) { | |
1182 | /* Got to the trailing NULL element */ | |
1183 | break; | |
1184 | } | |
1185 | ||
a596dcb9 JG |
1186 | if (long_options[i].val != '\0') { |
1187 | optstring[str_pos++] = (char) long_options[i].val; | |
1188 | if (long_options[i].has_arg) { | |
1189 | optstring[str_pos++] = ':'; | |
1190 | } | |
8db0dc00 JG |
1191 | } |
1192 | } | |
1193 | ||
1194 | end: | |
1195 | return optstring; | |
1196 | } | |
3d071855 MD |
1197 | |
1198 | /* | |
1199 | * Try to remove a hierarchy of empty directories, recursively. Don't unlink | |
9529ec1b | 1200 | * any file. Try to rmdir any empty directory within the hierarchy. |
3d071855 MD |
1201 | */ |
1202 | LTTNG_HIDDEN | |
1203 | int utils_recursive_rmdir(const char *path) | |
1204 | { | |
1205 | DIR *dir; | |
7a946beb | 1206 | size_t path_len; |
9529ec1b | 1207 | int dir_fd, ret = 0, closeret, is_empty = 1; |
3d071855 MD |
1208 | struct dirent *entry; |
1209 | ||
1210 | /* Open directory */ | |
1211 | dir = opendir(path); | |
1212 | if (!dir) { | |
1213 | PERROR("Cannot open '%s' path", path); | |
1214 | return -1; | |
1215 | } | |
1216 | dir_fd = dirfd(dir); | |
1217 | if (dir_fd < 0) { | |
1218 | PERROR("dirfd"); | |
1219 | return -1; | |
1220 | } | |
1221 | ||
7a946beb | 1222 | path_len = strlen(path); |
3d071855 MD |
1223 | while ((entry = readdir(dir))) { |
1224 | if (!strcmp(entry->d_name, ".") | |
1225 | || !strcmp(entry->d_name, "..")) | |
1226 | continue; | |
7a946beb MJ |
1227 | |
1228 | struct stat st; | |
1229 | size_t name_len; | |
1230 | char filename[PATH_MAX]; | |
1231 | ||
1232 | name_len = strlen(entry->d_name); | |
1233 | if (path_len + name_len + 2 > sizeof(filename)) { | |
1234 | ERR("Failed to remove file: path name too long (%s/%s)", | |
1235 | path, entry->d_name); | |
1236 | continue; | |
1237 | } | |
1238 | if (snprintf(filename, sizeof(filename), "%s/%s", | |
1239 | path, entry->d_name) < 0) { | |
1240 | ERR("Failed to format path."); | |
1241 | continue; | |
1242 | } | |
1243 | ||
1244 | if (stat(filename, &st)) { | |
1245 | PERROR("stat"); | |
1246 | continue; | |
1247 | } | |
1248 | ||
1249 | if (S_ISDIR(st.st_mode)) { | |
3d071855 MD |
1250 | char subpath[PATH_MAX]; |
1251 | ||
1252 | strncpy(subpath, path, PATH_MAX); | |
1253 | subpath[PATH_MAX - 1] = '\0'; | |
1254 | strncat(subpath, "/", | |
1255 | PATH_MAX - strlen(subpath) - 1); | |
1256 | strncat(subpath, entry->d_name, | |
1257 | PATH_MAX - strlen(subpath) - 1); | |
9529ec1b MD |
1258 | if (utils_recursive_rmdir(subpath)) { |
1259 | is_empty = 0; | |
3d071855 | 1260 | } |
7a946beb | 1261 | } else if (S_ISREG(st.st_mode)) { |
9529ec1b | 1262 | is_empty = 0; |
7a946beb | 1263 | } else { |
3d071855 MD |
1264 | ret = -EINVAL; |
1265 | goto end; | |
1266 | } | |
1267 | } | |
1268 | end: | |
1269 | closeret = closedir(dir); | |
1270 | if (closeret) { | |
1271 | PERROR("closedir"); | |
1272 | } | |
9529ec1b | 1273 | if (is_empty) { |
3d071855 MD |
1274 | DBG3("Attempting rmdir %s", path); |
1275 | ret = rmdir(path); | |
1276 | } | |
1277 | return ret; | |
1278 | } |