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