| 1 | /* |
| 2 | * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License, version 2 only, as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along with |
| 14 | * this program; if not, write to the Free Software Foundation, Inc., 51 |
| 15 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | */ |
| 17 | |
| 18 | #define _GNU_SOURCE |
| 19 | #include <assert.h> |
| 20 | #include <ctype.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <limits.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <unistd.h> |
| 28 | #include <inttypes.h> |
| 29 | #include <regex.h> |
| 30 | #include <grp.h> |
| 31 | |
| 32 | #include <common/common.h> |
| 33 | #include <common/runas.h> |
| 34 | |
| 35 | #include "utils.h" |
| 36 | #include "defaults.h" |
| 37 | |
| 38 | /* |
| 39 | * Return a partial realpath(3) of the path even if the full path does not |
| 40 | * exist. For instance, with /tmp/test1/test2/test3, if test2/ does not exist |
| 41 | * but the /tmp/test1 does, the real path for /tmp/test1 is concatened with |
| 42 | * /test2/test3 then returned. In normal time, realpath(3) fails if the end |
| 43 | * point directory does not exist. |
| 44 | * In case resolved_path is NULL, the string returned was allocated in the |
| 45 | * function and thus need to be freed by the caller. The size argument allows |
| 46 | * to specify the size of the resolved_path argument if given, or the size to |
| 47 | * allocate. |
| 48 | */ |
| 49 | LTTNG_HIDDEN |
| 50 | char *utils_partial_realpath(const char *path, char *resolved_path, size_t size) |
| 51 | { |
| 52 | char *cut_path, *try_path = NULL, *try_path_prev = NULL; |
| 53 | const char *next, *prev, *end; |
| 54 | |
| 55 | /* Safety net */ |
| 56 | if (path == NULL) { |
| 57 | goto error; |
| 58 | } |
| 59 | |
| 60 | /* |
| 61 | * Identify the end of the path, we don't want to treat the |
| 62 | * last char if it is a '/', we will just keep it on the side |
| 63 | * to be added at the end, and return a value coherent with |
| 64 | * the path given as argument |
| 65 | */ |
| 66 | end = path + strlen(path); |
| 67 | if (*(end-1) == '/') { |
| 68 | end--; |
| 69 | } |
| 70 | |
| 71 | /* Initiate the values of the pointers before looping */ |
| 72 | next = path; |
| 73 | prev = next; |
| 74 | /* Only to ensure try_path is not NULL to enter the while */ |
| 75 | try_path = (char *)next; |
| 76 | |
| 77 | /* Resolve the canonical path of the first part of the path */ |
| 78 | while (try_path != NULL && next != end) { |
| 79 | /* |
| 80 | * If there is not any '/' left, we want to try with |
| 81 | * the full path |
| 82 | */ |
| 83 | next = strpbrk(next + 1, "/"); |
| 84 | if (next == NULL) { |
| 85 | next = end; |
| 86 | } |
| 87 | |
| 88 | /* Cut the part we will be trying to resolve */ |
| 89 | cut_path = strndup(path, next - path); |
| 90 | |
| 91 | /* Try to resolve this part */ |
| 92 | try_path = realpath((char *)cut_path, NULL); |
| 93 | if (try_path == NULL) { |
| 94 | /* |
| 95 | * There was an error, we just want to be assured it |
| 96 | * is linked to an unexistent directory, if it's another |
| 97 | * reason, we spawn an error |
| 98 | */ |
| 99 | switch (errno) { |
| 100 | case ENOENT: |
| 101 | /* Ignore the error */ |
| 102 | break; |
| 103 | default: |
| 104 | PERROR("realpath (partial_realpath)"); |
| 105 | goto error; |
| 106 | break; |
| 107 | } |
| 108 | } else { |
| 109 | /* Save the place we are before trying the next step */ |
| 110 | free(try_path_prev); |
| 111 | try_path_prev = try_path; |
| 112 | prev = next; |
| 113 | } |
| 114 | |
| 115 | /* Free the allocated memory */ |
| 116 | free(cut_path); |
| 117 | }; |
| 118 | |
| 119 | /* Allocate memory for the resolved path if necessary */ |
| 120 | if (resolved_path == NULL) { |
| 121 | resolved_path = zmalloc(size); |
| 122 | if (resolved_path == NULL) { |
| 123 | PERROR("zmalloc resolved path"); |
| 124 | goto error; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * If we were able to solve at least partially the path, we can concatenate |
| 130 | * what worked and what didn't work |
| 131 | */ |
| 132 | if (try_path_prev != NULL) { |
| 133 | /* If we risk to concatenate two '/', we remove one of them */ |
| 134 | if (try_path_prev[strlen(try_path_prev) - 1] == '/' && prev[0] == '/') { |
| 135 | try_path_prev[strlen(try_path_prev) - 1] = '\0'; |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * Duplicate the memory used by prev in case resolved_path and |
| 140 | * path are pointers for the same memory space |
| 141 | */ |
| 142 | cut_path = strdup(prev); |
| 143 | |
| 144 | /* Concatenate the strings */ |
| 145 | snprintf(resolved_path, size, "%s%s", try_path_prev, cut_path); |
| 146 | |
| 147 | /* Free the allocated memory */ |
| 148 | free(cut_path); |
| 149 | free(try_path_prev); |
| 150 | /* |
| 151 | * Else, we just copy the path in our resolved_path to |
| 152 | * return it as is |
| 153 | */ |
| 154 | } else { |
| 155 | strncpy(resolved_path, path, size); |
| 156 | } |
| 157 | |
| 158 | /* Then we return the 'partially' resolved path */ |
| 159 | return resolved_path; |
| 160 | |
| 161 | error: |
| 162 | free(resolved_path); |
| 163 | return NULL; |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * Resolve the './' and '../' strings in the middle of a path using |
| 168 | * our very own way to do it, so that it works even if the directory |
| 169 | * does not exist |
| 170 | */ |
| 171 | LTTNG_HIDDEN |
| 172 | char *utils_resolve_relative(const char *path) |
| 173 | { |
| 174 | char *next, *previous, *slash, *start_path, *absolute_path = NULL; |
| 175 | |
| 176 | /* Safety net */ |
| 177 | if (path == NULL) { |
| 178 | goto error; |
| 179 | } |
| 180 | |
| 181 | /* Allocate memory for the absolute path */ |
| 182 | absolute_path = zmalloc(PATH_MAX); |
| 183 | if (absolute_path == NULL) { |
| 184 | PERROR("zmalloc expand path"); |
| 185 | goto error; |
| 186 | } |
| 187 | |
| 188 | /* Copy the path in the absolute path */ |
| 189 | strncpy(absolute_path, path, PATH_MAX); |
| 190 | |
| 191 | /* As long as we find '/./' in the path string */ |
| 192 | while ((next = strstr(absolute_path, "/./"))) { |
| 193 | |
| 194 | /* We prepare the start_path not containing it */ |
| 195 | start_path = strndup(absolute_path, next - absolute_path); |
| 196 | |
| 197 | /* And we concatenate it with the part after this string */ |
| 198 | snprintf(absolute_path, PATH_MAX, "%s%s", start_path, next + 2); |
| 199 | |
| 200 | free(start_path); |
| 201 | } |
| 202 | |
| 203 | /* As long as we find '/../' in the path string */ |
| 204 | while ((next = strstr(absolute_path, "/../"))) { |
| 205 | /* If the path starts with '/../', there's a problem */ |
| 206 | if (next == absolute_path) { |
| 207 | ERR("%s: Path cannot be resolved", path); |
| 208 | goto error; |
| 209 | } |
| 210 | |
| 211 | /* We find the last level of directory */ |
| 212 | previous = absolute_path; |
| 213 | while ((slash = strpbrk(previous + 1, "/")) && slash != next) { |
| 214 | previous = slash; |
| 215 | } |
| 216 | |
| 217 | /* Then we prepare the start_path not containing it */ |
| 218 | start_path = strndup(absolute_path, previous - absolute_path); |
| 219 | |
| 220 | /* And we concatenate it with the part after the '/../' */ |
| 221 | snprintf(absolute_path, PATH_MAX, "%s%s", start_path, next + 3); |
| 222 | |
| 223 | free(start_path); |
| 224 | } |
| 225 | |
| 226 | return absolute_path; |
| 227 | |
| 228 | error: |
| 229 | free(absolute_path); |
| 230 | return NULL; |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * Make a full resolution of the given path even if it doesn't exist. |
| 235 | * This function uses the utils_partial_realpath function to resolve |
| 236 | * symlinks and relatives paths at the start of the string, and |
| 237 | * implements functionnalities to resolve the './' and '../' strings |
| 238 | * in the middle of a path. This function is only necessary because |
| 239 | * realpath(3) does not accept to resolve unexistent paths. |
| 240 | * The returned string was allocated in the function, it is thus of |
| 241 | * the responsibility of the caller to free this memory. |
| 242 | */ |
| 243 | LTTNG_HIDDEN |
| 244 | char *utils_expand_path(const char *path) |
| 245 | { |
| 246 | char *next, *previous, *slash, *start_path, *absolute_path = NULL; |
| 247 | |
| 248 | /* Safety net */ |
| 249 | if (path == NULL) { |
| 250 | goto error; |
| 251 | } |
| 252 | |
| 253 | /* Allocate memory for the absolute_path */ |
| 254 | absolute_path = zmalloc(PATH_MAX); |
| 255 | if (absolute_path == NULL) { |
| 256 | PERROR("zmalloc expand path"); |
| 257 | goto error; |
| 258 | } |
| 259 | |
| 260 | /* |
| 261 | * If the path is not already absolute nor explicitly relative, |
| 262 | * consider we're in the current directory |
| 263 | */ |
| 264 | if (*path != '/' && strncmp(path, "./", 2) != 0 && |
| 265 | strncmp(path, "../", 3) != 0) { |
| 266 | snprintf(absolute_path, PATH_MAX, "./%s", path); |
| 267 | /* Else, we just copy the path */ |
| 268 | } else { |
| 269 | strncpy(absolute_path, path, PATH_MAX); |
| 270 | } |
| 271 | |
| 272 | /* Resolve partially our path */ |
| 273 | absolute_path = utils_partial_realpath(absolute_path, |
| 274 | absolute_path, PATH_MAX); |
| 275 | |
| 276 | /* As long as we find '/./' in the working_path string */ |
| 277 | while ((next = strstr(absolute_path, "/./"))) { |
| 278 | |
| 279 | /* We prepare the start_path not containing it */ |
| 280 | start_path = strndup(absolute_path, next - absolute_path); |
| 281 | |
| 282 | /* And we concatenate it with the part after this string */ |
| 283 | snprintf(absolute_path, PATH_MAX, "%s%s", start_path, next + 2); |
| 284 | |
| 285 | free(start_path); |
| 286 | } |
| 287 | |
| 288 | /* As long as we find '/../' in the working_path string */ |
| 289 | while ((next = strstr(absolute_path, "/../"))) { |
| 290 | /* We find the last level of directory */ |
| 291 | previous = absolute_path; |
| 292 | while ((slash = strpbrk(previous, "/")) && slash != next) { |
| 293 | previous = slash + 1; |
| 294 | } |
| 295 | |
| 296 | /* Then we prepare the start_path not containing it */ |
| 297 | start_path = strndup(absolute_path, previous - absolute_path); |
| 298 | |
| 299 | /* And we concatenate it with the part after the '/../' */ |
| 300 | snprintf(absolute_path, PATH_MAX, "%s%s", start_path, next + 4); |
| 301 | |
| 302 | /* We can free the memory used for the start path*/ |
| 303 | free(start_path); |
| 304 | |
| 305 | /* Then we verify for symlinks using partial_realpath */ |
| 306 | absolute_path = utils_partial_realpath(absolute_path, |
| 307 | absolute_path, PATH_MAX); |
| 308 | } |
| 309 | |
| 310 | return absolute_path; |
| 311 | |
| 312 | error: |
| 313 | free(absolute_path); |
| 314 | return NULL; |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | * Create a pipe in dst. |
| 319 | */ |
| 320 | LTTNG_HIDDEN |
| 321 | int utils_create_pipe(int *dst) |
| 322 | { |
| 323 | int ret; |
| 324 | |
| 325 | if (dst == NULL) { |
| 326 | return -1; |
| 327 | } |
| 328 | |
| 329 | ret = pipe(dst); |
| 330 | if (ret < 0) { |
| 331 | PERROR("create pipe"); |
| 332 | } |
| 333 | |
| 334 | return ret; |
| 335 | } |
| 336 | |
| 337 | /* |
| 338 | * Create pipe and set CLOEXEC flag to both fd. |
| 339 | * |
| 340 | * Make sure the pipe opened by this function are closed at some point. Use |
| 341 | * utils_close_pipe(). |
| 342 | */ |
| 343 | LTTNG_HIDDEN |
| 344 | int utils_create_pipe_cloexec(int *dst) |
| 345 | { |
| 346 | int ret, i; |
| 347 | |
| 348 | if (dst == NULL) { |
| 349 | return -1; |
| 350 | } |
| 351 | |
| 352 | ret = utils_create_pipe(dst); |
| 353 | if (ret < 0) { |
| 354 | goto error; |
| 355 | } |
| 356 | |
| 357 | for (i = 0; i < 2; i++) { |
| 358 | ret = fcntl(dst[i], F_SETFD, FD_CLOEXEC); |
| 359 | if (ret < 0) { |
| 360 | PERROR("fcntl pipe cloexec"); |
| 361 | goto error; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | error: |
| 366 | return ret; |
| 367 | } |
| 368 | |
| 369 | /* |
| 370 | * Create pipe and set fd flags to FD_CLOEXEC and O_NONBLOCK. |
| 371 | * |
| 372 | * Make sure the pipe opened by this function are closed at some point. Use |
| 373 | * utils_close_pipe(). Using pipe() and fcntl rather than pipe2() to |
| 374 | * support OSes other than Linux 2.6.23+. |
| 375 | */ |
| 376 | LTTNG_HIDDEN |
| 377 | int utils_create_pipe_cloexec_nonblock(int *dst) |
| 378 | { |
| 379 | int ret, i; |
| 380 | |
| 381 | if (dst == NULL) { |
| 382 | return -1; |
| 383 | } |
| 384 | |
| 385 | ret = utils_create_pipe(dst); |
| 386 | if (ret < 0) { |
| 387 | goto error; |
| 388 | } |
| 389 | |
| 390 | for (i = 0; i < 2; i++) { |
| 391 | ret = fcntl(dst[i], F_SETFD, FD_CLOEXEC); |
| 392 | if (ret < 0) { |
| 393 | PERROR("fcntl pipe cloexec"); |
| 394 | goto error; |
| 395 | } |
| 396 | /* |
| 397 | * Note: we override any flag that could have been |
| 398 | * previously set on the fd. |
| 399 | */ |
| 400 | ret = fcntl(dst[i], F_SETFL, O_NONBLOCK); |
| 401 | if (ret < 0) { |
| 402 | PERROR("fcntl pipe nonblock"); |
| 403 | goto error; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | error: |
| 408 | return ret; |
| 409 | } |
| 410 | |
| 411 | /* |
| 412 | * Close both read and write side of the pipe. |
| 413 | */ |
| 414 | LTTNG_HIDDEN |
| 415 | void utils_close_pipe(int *src) |
| 416 | { |
| 417 | int i, ret; |
| 418 | |
| 419 | if (src == NULL) { |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | for (i = 0; i < 2; i++) { |
| 424 | /* Safety check */ |
| 425 | if (src[i] < 0) { |
| 426 | continue; |
| 427 | } |
| 428 | |
| 429 | ret = close(src[i]); |
| 430 | if (ret) { |
| 431 | PERROR("close pipe"); |
| 432 | } |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | /* |
| 437 | * Create a new string using two strings range. |
| 438 | */ |
| 439 | LTTNG_HIDDEN |
| 440 | char *utils_strdupdelim(const char *begin, const char *end) |
| 441 | { |
| 442 | char *str; |
| 443 | |
| 444 | str = zmalloc(end - begin + 1); |
| 445 | if (str == NULL) { |
| 446 | PERROR("zmalloc strdupdelim"); |
| 447 | goto error; |
| 448 | } |
| 449 | |
| 450 | memcpy(str, begin, end - begin); |
| 451 | str[end - begin] = '\0'; |
| 452 | |
| 453 | error: |
| 454 | return str; |
| 455 | } |
| 456 | |
| 457 | /* |
| 458 | * Set CLOEXEC flag to the give file descriptor. |
| 459 | */ |
| 460 | LTTNG_HIDDEN |
| 461 | int utils_set_fd_cloexec(int fd) |
| 462 | { |
| 463 | int ret; |
| 464 | |
| 465 | if (fd < 0) { |
| 466 | ret = -EINVAL; |
| 467 | goto end; |
| 468 | } |
| 469 | |
| 470 | ret = fcntl(fd, F_SETFD, FD_CLOEXEC); |
| 471 | if (ret < 0) { |
| 472 | PERROR("fcntl cloexec"); |
| 473 | ret = -errno; |
| 474 | } |
| 475 | |
| 476 | end: |
| 477 | return ret; |
| 478 | } |
| 479 | |
| 480 | /* |
| 481 | * Create pid file to the given path and filename. |
| 482 | */ |
| 483 | LTTNG_HIDDEN |
| 484 | int utils_create_pid_file(pid_t pid, const char *filepath) |
| 485 | { |
| 486 | int ret; |
| 487 | FILE *fp; |
| 488 | |
| 489 | assert(filepath); |
| 490 | |
| 491 | fp = fopen(filepath, "w"); |
| 492 | if (fp == NULL) { |
| 493 | PERROR("open pid file %s", filepath); |
| 494 | ret = -1; |
| 495 | goto error; |
| 496 | } |
| 497 | |
| 498 | ret = fprintf(fp, "%d\n", pid); |
| 499 | if (ret < 0) { |
| 500 | PERROR("fprintf pid file"); |
| 501 | } |
| 502 | |
| 503 | fclose(fp); |
| 504 | DBG("Pid %d written in file %s", pid, filepath); |
| 505 | error: |
| 506 | return ret; |
| 507 | } |
| 508 | |
| 509 | /* |
| 510 | * Recursively create directory using the given path and mode. |
| 511 | * |
| 512 | * On success, return 0 else a negative error code. |
| 513 | */ |
| 514 | LTTNG_HIDDEN |
| 515 | int utils_mkdir_recursive(const char *path, mode_t mode) |
| 516 | { |
| 517 | char *p, tmp[PATH_MAX]; |
| 518 | size_t len; |
| 519 | int ret; |
| 520 | |
| 521 | assert(path); |
| 522 | |
| 523 | ret = snprintf(tmp, sizeof(tmp), "%s", path); |
| 524 | if (ret < 0) { |
| 525 | PERROR("snprintf mkdir"); |
| 526 | goto error; |
| 527 | } |
| 528 | |
| 529 | len = ret; |
| 530 | if (tmp[len - 1] == '/') { |
| 531 | tmp[len - 1] = 0; |
| 532 | } |
| 533 | |
| 534 | for (p = tmp + 1; *p; p++) { |
| 535 | if (*p == '/') { |
| 536 | *p = 0; |
| 537 | if (tmp[strlen(tmp) - 1] == '.' && |
| 538 | tmp[strlen(tmp) - 2] == '.' && |
| 539 | tmp[strlen(tmp) - 3] == '/') { |
| 540 | ERR("Using '/../' is not permitted in the trace path (%s)", |
| 541 | tmp); |
| 542 | ret = -1; |
| 543 | goto error; |
| 544 | } |
| 545 | ret = mkdir(tmp, mode); |
| 546 | if (ret < 0) { |
| 547 | if (errno != EEXIST) { |
| 548 | PERROR("mkdir recursive"); |
| 549 | ret = -errno; |
| 550 | goto error; |
| 551 | } |
| 552 | } |
| 553 | *p = '/'; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | ret = mkdir(tmp, mode); |
| 558 | if (ret < 0) { |
| 559 | if (errno != EEXIST) { |
| 560 | PERROR("mkdir recursive last piece"); |
| 561 | ret = -errno; |
| 562 | } else { |
| 563 | ret = 0; |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | error: |
| 568 | return ret; |
| 569 | } |
| 570 | |
| 571 | /* |
| 572 | * Create the stream tracefile on disk. |
| 573 | * |
| 574 | * Return 0 on success or else a negative value. |
| 575 | */ |
| 576 | LTTNG_HIDDEN |
| 577 | int utils_create_stream_file(const char *path_name, char *file_name, uint64_t size, |
| 578 | uint64_t count, int uid, int gid, char *suffix) |
| 579 | { |
| 580 | int ret, out_fd, flags, mode; |
| 581 | char full_path[PATH_MAX], *path_name_suffix = NULL, *path; |
| 582 | char *extra = NULL; |
| 583 | |
| 584 | assert(path_name); |
| 585 | assert(file_name); |
| 586 | |
| 587 | ret = snprintf(full_path, sizeof(full_path), "%s/%s", |
| 588 | path_name, file_name); |
| 589 | if (ret < 0) { |
| 590 | PERROR("snprintf create output file"); |
| 591 | goto error; |
| 592 | } |
| 593 | |
| 594 | /* Setup extra string if suffix or/and a count is needed. */ |
| 595 | if (size > 0 && suffix) { |
| 596 | ret = asprintf(&extra, "_%" PRIu64 "%s", count, suffix); |
| 597 | } else if (size > 0) { |
| 598 | ret = asprintf(&extra, "_%" PRIu64, count); |
| 599 | } else if (suffix) { |
| 600 | ret = asprintf(&extra, "%s", suffix); |
| 601 | } |
| 602 | if (ret < 0) { |
| 603 | PERROR("Allocating extra string to name"); |
| 604 | goto error; |
| 605 | } |
| 606 | |
| 607 | /* |
| 608 | * If we split the trace in multiple files, we have to add the count at the |
| 609 | * end of the tracefile name |
| 610 | */ |
| 611 | if (extra) { |
| 612 | ret = asprintf(&path_name_suffix, "%s%s", full_path, extra); |
| 613 | if (ret < 0) { |
| 614 | PERROR("Allocating path name with extra string"); |
| 615 | goto error_free_suffix; |
| 616 | } |
| 617 | path = path_name_suffix; |
| 618 | } else { |
| 619 | path = full_path; |
| 620 | } |
| 621 | |
| 622 | flags = O_WRONLY | O_CREAT | O_TRUNC; |
| 623 | /* Open with 660 mode */ |
| 624 | mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; |
| 625 | |
| 626 | if (uid < 0 || gid < 0) { |
| 627 | out_fd = open(path, flags, mode); |
| 628 | } else { |
| 629 | out_fd = run_as_open(path, flags, mode, uid, gid); |
| 630 | } |
| 631 | if (out_fd < 0) { |
| 632 | PERROR("open stream path %s", path); |
| 633 | goto error_open; |
| 634 | } |
| 635 | ret = out_fd; |
| 636 | |
| 637 | error_open: |
| 638 | free(path_name_suffix); |
| 639 | error_free_suffix: |
| 640 | free(extra); |
| 641 | error: |
| 642 | return ret; |
| 643 | } |
| 644 | |
| 645 | /* |
| 646 | * Change the output tracefile according to the given size and count The |
| 647 | * new_count pointer is set during this operation. |
| 648 | * |
| 649 | * From the consumer, the stream lock MUST be held before calling this function |
| 650 | * because we are modifying the stream status. |
| 651 | * |
| 652 | * Return 0 on success or else a negative value. |
| 653 | */ |
| 654 | LTTNG_HIDDEN |
| 655 | int utils_rotate_stream_file(char *path_name, char *file_name, uint64_t size, |
| 656 | uint64_t count, int uid, int gid, int out_fd, uint64_t *new_count, |
| 657 | int *stream_fd) |
| 658 | { |
| 659 | int ret; |
| 660 | |
| 661 | assert(new_count); |
| 662 | assert(stream_fd); |
| 663 | |
| 664 | ret = close(out_fd); |
| 665 | if (ret < 0) { |
| 666 | PERROR("Closing tracefile"); |
| 667 | goto error; |
| 668 | } |
| 669 | |
| 670 | if (count > 0) { |
| 671 | *new_count = (*new_count + 1) % count; |
| 672 | } else { |
| 673 | (*new_count)++; |
| 674 | } |
| 675 | |
| 676 | ret = utils_create_stream_file(path_name, file_name, size, *new_count, |
| 677 | uid, gid, 0); |
| 678 | if (ret < 0) { |
| 679 | goto error; |
| 680 | } |
| 681 | *stream_fd = ret; |
| 682 | |
| 683 | /* Success. */ |
| 684 | ret = 0; |
| 685 | |
| 686 | error: |
| 687 | return ret; |
| 688 | } |
| 689 | |
| 690 | /** |
| 691 | * Prints the error message corresponding to a regex error code. |
| 692 | * |
| 693 | * @param errcode The error code. |
| 694 | * @param regex The regex object that produced the error code. |
| 695 | */ |
| 696 | static void regex_print_error(int errcode, regex_t *regex) |
| 697 | { |
| 698 | /* Get length of error message and allocate accordingly */ |
| 699 | size_t length; |
| 700 | char *buffer; |
| 701 | |
| 702 | assert(regex != NULL); |
| 703 | |
| 704 | length = regerror(errcode, regex, NULL, 0); |
| 705 | if (length == 0) { |
| 706 | ERR("regerror returned a length of 0"); |
| 707 | return; |
| 708 | } |
| 709 | |
| 710 | buffer = zmalloc(length); |
| 711 | if (!buffer) { |
| 712 | ERR("regex_print_error: zmalloc failed"); |
| 713 | return; |
| 714 | } |
| 715 | |
| 716 | /* Get and print error message */ |
| 717 | regerror(errcode, regex, buffer, length); |
| 718 | ERR("regex error: %s\n", buffer); |
| 719 | free(buffer); |
| 720 | |
| 721 | } |
| 722 | |
| 723 | /** |
| 724 | * Parse a string that represents a size in human readable format. It |
| 725 | * supports decimal integers suffixed by 'k', 'M' or 'G'. |
| 726 | * |
| 727 | * The suffix multiply the integer by: |
| 728 | * 'k': 1024 |
| 729 | * 'M': 1024^2 |
| 730 | * 'G': 1024^3 |
| 731 | * |
| 732 | * @param str The string to parse. |
| 733 | * @param size Pointer to a size_t that will be filled with the |
| 734 | * resulting size. |
| 735 | * |
| 736 | * @return 0 on success, -1 on failure. |
| 737 | */ |
| 738 | LTTNG_HIDDEN |
| 739 | int utils_parse_size_suffix(char *str, uint64_t *size) |
| 740 | { |
| 741 | regex_t regex; |
| 742 | int ret; |
| 743 | const int nmatch = 3; |
| 744 | regmatch_t suffix_match, matches[nmatch]; |
| 745 | unsigned long long base_size; |
| 746 | long shift = 0; |
| 747 | |
| 748 | if (!str) { |
| 749 | return 0; |
| 750 | } |
| 751 | |
| 752 | /* Compile regex */ |
| 753 | ret = regcomp(®ex, "^\\(0x\\)\\{0,1\\}[0-9][0-9]*\\([kKMG]\\{0,1\\}\\)$", 0); |
| 754 | if (ret != 0) { |
| 755 | regex_print_error(ret, ®ex); |
| 756 | ret = -1; |
| 757 | goto end; |
| 758 | } |
| 759 | |
| 760 | /* Match regex */ |
| 761 | ret = regexec(®ex, str, nmatch, matches, 0); |
| 762 | if (ret != 0) { |
| 763 | ret = -1; |
| 764 | goto free; |
| 765 | } |
| 766 | |
| 767 | /* There is a match ! */ |
| 768 | errno = 0; |
| 769 | base_size = strtoull(str, NULL, 0); |
| 770 | if (errno != 0) { |
| 771 | PERROR("strtoull"); |
| 772 | ret = -1; |
| 773 | goto free; |
| 774 | } |
| 775 | |
| 776 | /* Check if there is a suffix */ |
| 777 | suffix_match = matches[2]; |
| 778 | if (suffix_match.rm_eo - suffix_match.rm_so == 1) { |
| 779 | switch (*(str + suffix_match.rm_so)) { |
| 780 | case 'K': |
| 781 | case 'k': |
| 782 | shift = KIBI_LOG2; |
| 783 | break; |
| 784 | case 'M': |
| 785 | shift = MEBI_LOG2; |
| 786 | break; |
| 787 | case 'G': |
| 788 | shift = GIBI_LOG2; |
| 789 | break; |
| 790 | default: |
| 791 | ERR("parse_human_size: invalid suffix"); |
| 792 | ret = -1; |
| 793 | goto free; |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | *size = base_size << shift; |
| 798 | |
| 799 | /* Check for overflow */ |
| 800 | if ((*size >> shift) != base_size) { |
| 801 | ERR("parse_size_suffix: oops, overflow detected."); |
| 802 | ret = -1; |
| 803 | goto free; |
| 804 | } |
| 805 | |
| 806 | ret = 0; |
| 807 | |
| 808 | free: |
| 809 | regfree(®ex); |
| 810 | end: |
| 811 | return ret; |
| 812 | } |
| 813 | |
| 814 | /* |
| 815 | * fls: returns the position of the most significant bit. |
| 816 | * Returns 0 if no bit is set, else returns the position of the most |
| 817 | * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit). |
| 818 | */ |
| 819 | #if defined(__i386) || defined(__x86_64) |
| 820 | static inline unsigned int fls_u32(uint32_t x) |
| 821 | { |
| 822 | int r; |
| 823 | |
| 824 | asm("bsrl %1,%0\n\t" |
| 825 | "jnz 1f\n\t" |
| 826 | "movl $-1,%0\n\t" |
| 827 | "1:\n\t" |
| 828 | : "=r" (r) : "rm" (x)); |
| 829 | return r + 1; |
| 830 | } |
| 831 | #define HAS_FLS_U32 |
| 832 | #endif |
| 833 | |
| 834 | #ifndef HAS_FLS_U32 |
| 835 | static __attribute__((unused)) unsigned int fls_u32(uint32_t x) |
| 836 | { |
| 837 | unsigned int r = 32; |
| 838 | |
| 839 | if (!x) { |
| 840 | return 0; |
| 841 | } |
| 842 | if (!(x & 0xFFFF0000U)) { |
| 843 | x <<= 16; |
| 844 | r -= 16; |
| 845 | } |
| 846 | if (!(x & 0xFF000000U)) { |
| 847 | x <<= 8; |
| 848 | r -= 8; |
| 849 | } |
| 850 | if (!(x & 0xF0000000U)) { |
| 851 | x <<= 4; |
| 852 | r -= 4; |
| 853 | } |
| 854 | if (!(x & 0xC0000000U)) { |
| 855 | x <<= 2; |
| 856 | r -= 2; |
| 857 | } |
| 858 | if (!(x & 0x80000000U)) { |
| 859 | x <<= 1; |
| 860 | r -= 1; |
| 861 | } |
| 862 | return r; |
| 863 | } |
| 864 | #endif |
| 865 | |
| 866 | /* |
| 867 | * Return the minimum order for which x <= (1UL << order). |
| 868 | * Return -1 if x is 0. |
| 869 | */ |
| 870 | LTTNG_HIDDEN |
| 871 | int utils_get_count_order_u32(uint32_t x) |
| 872 | { |
| 873 | if (!x) { |
| 874 | return -1; |
| 875 | } |
| 876 | |
| 877 | return fls_u32(x - 1); |
| 878 | } |
| 879 | |
| 880 | /** |
| 881 | * Obtain the value of LTTNG_HOME environment variable, if exists. |
| 882 | * Otherwise returns the value of HOME. |
| 883 | */ |
| 884 | LTTNG_HIDDEN |
| 885 | char *utils_get_home_dir(void) |
| 886 | { |
| 887 | char *val = NULL; |
| 888 | val = getenv(DEFAULT_LTTNG_HOME_ENV_VAR); |
| 889 | if (val != NULL) { |
| 890 | return val; |
| 891 | } |
| 892 | return getenv(DEFAULT_LTTNG_FALLBACK_HOME_ENV_VAR); |
| 893 | } |
| 894 | |
| 895 | /* |
| 896 | * With the given format, fill dst with the time of len maximum siz. |
| 897 | * |
| 898 | * Return amount of bytes set in the buffer or else 0 on error. |
| 899 | */ |
| 900 | LTTNG_HIDDEN |
| 901 | size_t utils_get_current_time_str(const char *format, char *dst, size_t len) |
| 902 | { |
| 903 | size_t ret; |
| 904 | time_t rawtime; |
| 905 | struct tm *timeinfo; |
| 906 | |
| 907 | assert(format); |
| 908 | assert(dst); |
| 909 | |
| 910 | /* Get date and time for session path */ |
| 911 | time(&rawtime); |
| 912 | timeinfo = localtime(&rawtime); |
| 913 | ret = strftime(dst, len, format, timeinfo); |
| 914 | if (ret == 0) { |
| 915 | ERR("Unable to strftime with format %s at dst %p of len %lu", format, |
| 916 | dst, len); |
| 917 | } |
| 918 | |
| 919 | return ret; |
| 920 | } |
| 921 | |
| 922 | /* |
| 923 | * Return the group ID matching name, else 0 if it cannot be found. |
| 924 | */ |
| 925 | LTTNG_HIDDEN |
| 926 | gid_t utils_get_group_id(const char *name) |
| 927 | { |
| 928 | struct group *grp; |
| 929 | |
| 930 | grp = getgrnam(name); |
| 931 | if (!grp) { |
| 932 | static volatile int warn_once; |
| 933 | |
| 934 | if (!warn_once) { |
| 935 | WARN("No tracing group detected"); |
| 936 | warn_once = 1; |
| 937 | } |
| 938 | return 0; |
| 939 | } |
| 940 | return grp->gr_gid; |
| 941 | } |