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