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