| 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 _LGPL_SOURCE |
| 21 | #include <assert.h> |
| 22 | #include <ctype.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <limits.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <unistd.h> |
| 29 | #include <inttypes.h> |
| 30 | #include <grp.h> |
| 31 | #include <pwd.h> |
| 32 | #include <sys/file.h> |
| 33 | #include <unistd.h> |
| 34 | |
| 35 | #include <common/common.h> |
| 36 | #include <common/readwrite.h> |
| 37 | #include <common/runas.h> |
| 38 | #include <common/compat/getenv.h> |
| 39 | #include <common/compat/string.h> |
| 40 | #include <common/compat/dirent.h> |
| 41 | #include <common/compat/directory-handle.h> |
| 42 | #include <common/dynamic-buffer.h> |
| 43 | #include <common/string-utils/format.h> |
| 44 | #include <lttng/constant.h> |
| 45 | |
| 46 | #include "utils.h" |
| 47 | #include "defaults.h" |
| 48 | #include "time.h" |
| 49 | |
| 50 | #define PROC_MEMINFO_PATH "/proc/meminfo" |
| 51 | #define PROC_MEMINFO_MEMAVAILABLE_LINE "MemAvailable:" |
| 52 | #define PROC_MEMINFO_MEMTOTAL_LINE "MemTotal:" |
| 53 | |
| 54 | /* The length of the longest field of `/proc/meminfo`. */ |
| 55 | #define PROC_MEMINFO_FIELD_MAX_NAME_LEN 20 |
| 56 | |
| 57 | #if (PROC_MEMINFO_FIELD_MAX_NAME_LEN == 20) |
| 58 | #define MAX_NAME_LEN_SCANF_IS_A_BROKEN_API "19" |
| 59 | #else |
| 60 | #error MAX_NAME_LEN_SCANF_IS_A_BROKEN_API must be updated to match (PROC_MEMINFO_FIELD_MAX_NAME_LEN - 1) |
| 61 | #endif |
| 62 | |
| 63 | /* |
| 64 | * Return a partial realpath(3) of the path even if the full path does not |
| 65 | * exist. For instance, with /tmp/test1/test2/test3, if test2/ does not exist |
| 66 | * but the /tmp/test1 does, the real path for /tmp/test1 is concatened with |
| 67 | * /test2/test3 then returned. In normal time, realpath(3) fails if the end |
| 68 | * point directory does not exist. |
| 69 | * In case resolved_path is NULL, the string returned was allocated in the |
| 70 | * function and thus need to be freed by the caller. The size argument allows |
| 71 | * to specify the size of the resolved_path argument if given, or the size to |
| 72 | * allocate. |
| 73 | */ |
| 74 | LTTNG_HIDDEN |
| 75 | char *utils_partial_realpath(const char *path, char *resolved_path, size_t size) |
| 76 | { |
| 77 | char *cut_path = NULL, *try_path = NULL, *try_path_prev = NULL; |
| 78 | const char *next, *prev, *end; |
| 79 | |
| 80 | /* Safety net */ |
| 81 | if (path == NULL) { |
| 82 | goto error; |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * Identify the end of the path, we don't want to treat the |
| 87 | * last char if it is a '/', we will just keep it on the side |
| 88 | * to be added at the end, and return a value coherent with |
| 89 | * the path given as argument |
| 90 | */ |
| 91 | end = path + strlen(path); |
| 92 | if (*(end-1) == '/') { |
| 93 | end--; |
| 94 | } |
| 95 | |
| 96 | /* Initiate the values of the pointers before looping */ |
| 97 | next = path; |
| 98 | prev = next; |
| 99 | /* Only to ensure try_path is not NULL to enter the while */ |
| 100 | try_path = (char *)next; |
| 101 | |
| 102 | /* Resolve the canonical path of the first part of the path */ |
| 103 | while (try_path != NULL && next != end) { |
| 104 | char *try_path_buf = NULL; |
| 105 | |
| 106 | /* |
| 107 | * If there is not any '/' left, we want to try with |
| 108 | * the full path |
| 109 | */ |
| 110 | next = strpbrk(next + 1, "/"); |
| 111 | if (next == NULL) { |
| 112 | next = end; |
| 113 | } |
| 114 | |
| 115 | /* Cut the part we will be trying to resolve */ |
| 116 | cut_path = lttng_strndup(path, next - path); |
| 117 | if (cut_path == NULL) { |
| 118 | PERROR("lttng_strndup"); |
| 119 | goto error; |
| 120 | } |
| 121 | |
| 122 | try_path_buf = zmalloc(LTTNG_PATH_MAX); |
| 123 | if (!try_path_buf) { |
| 124 | PERROR("zmalloc"); |
| 125 | goto error; |
| 126 | } |
| 127 | |
| 128 | /* Try to resolve this part */ |
| 129 | try_path = realpath((char *) cut_path, try_path_buf); |
| 130 | if (try_path == NULL) { |
| 131 | free(try_path_buf); |
| 132 | /* |
| 133 | * There was an error, we just want to be assured it |
| 134 | * is linked to an unexistent directory, if it's another |
| 135 | * reason, we spawn an error |
| 136 | */ |
| 137 | switch (errno) { |
| 138 | case ENOENT: |
| 139 | /* Ignore the error */ |
| 140 | break; |
| 141 | default: |
| 142 | PERROR("realpath (partial_realpath)"); |
| 143 | goto error; |
| 144 | break; |
| 145 | } |
| 146 | } else { |
| 147 | /* Save the place we are before trying the next step */ |
| 148 | try_path_buf = NULL; |
| 149 | free(try_path_prev); |
| 150 | try_path_prev = try_path; |
| 151 | prev = next; |
| 152 | } |
| 153 | |
| 154 | /* Free the allocated memory */ |
| 155 | free(cut_path); |
| 156 | cut_path = NULL; |
| 157 | } |
| 158 | |
| 159 | /* Allocate memory for the resolved path if necessary */ |
| 160 | if (resolved_path == NULL) { |
| 161 | resolved_path = zmalloc(size); |
| 162 | if (resolved_path == NULL) { |
| 163 | PERROR("zmalloc resolved path"); |
| 164 | goto error; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * If we were able to solve at least partially the path, we can concatenate |
| 170 | * what worked and what didn't work |
| 171 | */ |
| 172 | if (try_path_prev != NULL) { |
| 173 | /* If we risk to concatenate two '/', we remove one of them */ |
| 174 | if (try_path_prev[strlen(try_path_prev) - 1] == '/' && prev[0] == '/') { |
| 175 | try_path_prev[strlen(try_path_prev) - 1] = '\0'; |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * Duplicate the memory used by prev in case resolved_path and |
| 180 | * path are pointers for the same memory space |
| 181 | */ |
| 182 | cut_path = strdup(prev); |
| 183 | if (cut_path == NULL) { |
| 184 | PERROR("strdup"); |
| 185 | goto error; |
| 186 | } |
| 187 | |
| 188 | /* Concatenate the strings */ |
| 189 | snprintf(resolved_path, size, "%s%s", try_path_prev, cut_path); |
| 190 | |
| 191 | /* Free the allocated memory */ |
| 192 | free(cut_path); |
| 193 | free(try_path_prev); |
| 194 | cut_path = NULL; |
| 195 | try_path_prev = NULL; |
| 196 | /* |
| 197 | * Else, we just copy the path in our resolved_path to |
| 198 | * return it as is |
| 199 | */ |
| 200 | } else { |
| 201 | strncpy(resolved_path, path, size); |
| 202 | } |
| 203 | |
| 204 | /* Then we return the 'partially' resolved path */ |
| 205 | return resolved_path; |
| 206 | |
| 207 | error: |
| 208 | free(resolved_path); |
| 209 | free(cut_path); |
| 210 | free(try_path); |
| 211 | if (try_path_prev != try_path) { |
| 212 | free(try_path_prev); |
| 213 | } |
| 214 | return NULL; |
| 215 | } |
| 216 | |
| 217 | static |
| 218 | int expand_double_slashes_dot_and_dotdot(char *path) |
| 219 | { |
| 220 | size_t expanded_path_len, path_len; |
| 221 | const char *curr_char, *path_last_char, *next_slash, *prev_slash; |
| 222 | |
| 223 | path_len = strlen(path); |
| 224 | path_last_char = &path[path_len]; |
| 225 | |
| 226 | if (path_len == 0) { |
| 227 | goto error; |
| 228 | } |
| 229 | |
| 230 | expanded_path_len = 0; |
| 231 | |
| 232 | /* We iterate over the provided path to expand the "//", "../" and "./" */ |
| 233 | for (curr_char = path; curr_char <= path_last_char; curr_char = next_slash + 1) { |
| 234 | /* Find the next forward slash. */ |
| 235 | size_t curr_token_len; |
| 236 | |
| 237 | if (curr_char == path_last_char) { |
| 238 | expanded_path_len++; |
| 239 | break; |
| 240 | } |
| 241 | |
| 242 | next_slash = memchr(curr_char, '/', path_last_char - curr_char); |
| 243 | if (next_slash == NULL) { |
| 244 | /* Reached the end of the provided path. */ |
| 245 | next_slash = path_last_char; |
| 246 | } |
| 247 | |
| 248 | /* Compute how long is the previous token. */ |
| 249 | curr_token_len = next_slash - curr_char; |
| 250 | switch(curr_token_len) { |
| 251 | case 0: |
| 252 | /* |
| 253 | * The pointer has not move meaning that curr_char is |
| 254 | * pointing to a slash. It that case there is no token |
| 255 | * to copy, so continue the iteration to find the next |
| 256 | * token |
| 257 | */ |
| 258 | continue; |
| 259 | case 1: |
| 260 | /* |
| 261 | * The pointer moved 1 character. Check if that |
| 262 | * character is a dot ('.'), if it is: omit it, else |
| 263 | * copy the token to the normalized path. |
| 264 | */ |
| 265 | if (curr_char[0] == '.') { |
| 266 | continue; |
| 267 | } |
| 268 | break; |
| 269 | case 2: |
| 270 | /* |
| 271 | * The pointer moved 2 characters. Check if these |
| 272 | * characters are double dots ('..'). If that is the |
| 273 | * case, we need to remove the last token of the |
| 274 | * normalized path. |
| 275 | */ |
| 276 | if (curr_char[0] == '.' && curr_char[1] == '.') { |
| 277 | /* |
| 278 | * Find the previous path component by |
| 279 | * using the memrchr function to find the |
| 280 | * previous forward slash and substract that |
| 281 | * len to the resulting path. |
| 282 | */ |
| 283 | prev_slash = lttng_memrchr(path, '/', expanded_path_len); |
| 284 | /* |
| 285 | * If prev_slash is NULL, we reached the |
| 286 | * beginning of the path. We can't go back any |
| 287 | * further. |
| 288 | */ |
| 289 | if (prev_slash != NULL) { |
| 290 | expanded_path_len = prev_slash - path; |
| 291 | } |
| 292 | continue; |
| 293 | } |
| 294 | break; |
| 295 | default: |
| 296 | break; |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | * Copy the current token which is neither a '.' nor a '..'. |
| 301 | */ |
| 302 | path[expanded_path_len++] = '/'; |
| 303 | memcpy(&path[expanded_path_len], curr_char, curr_token_len); |
| 304 | expanded_path_len += curr_token_len; |
| 305 | } |
| 306 | |
| 307 | if (expanded_path_len == 0) { |
| 308 | path[expanded_path_len++] = '/'; |
| 309 | } |
| 310 | |
| 311 | path[expanded_path_len] = '\0'; |
| 312 | return 0; |
| 313 | error: |
| 314 | return -1; |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | * Make a full resolution of the given path even if it doesn't exist. |
| 319 | * This function uses the utils_partial_realpath function to resolve |
| 320 | * symlinks and relatives paths at the start of the string, and |
| 321 | * implements functionnalities to resolve the './' and '../' strings |
| 322 | * in the middle of a path. This function is only necessary because |
| 323 | * realpath(3) does not accept to resolve unexistent paths. |
| 324 | * The returned string was allocated in the function, it is thus of |
| 325 | * the responsibility of the caller to free this memory. |
| 326 | */ |
| 327 | LTTNG_HIDDEN |
| 328 | char *_utils_expand_path(const char *path, bool keep_symlink) |
| 329 | { |
| 330 | int ret; |
| 331 | char *absolute_path = NULL; |
| 332 | char *last_token; |
| 333 | bool is_dot, is_dotdot; |
| 334 | |
| 335 | /* Safety net */ |
| 336 | if (path == NULL) { |
| 337 | goto error; |
| 338 | } |
| 339 | |
| 340 | /* Allocate memory for the absolute_path */ |
| 341 | absolute_path = zmalloc(LTTNG_PATH_MAX); |
| 342 | if (absolute_path == NULL) { |
| 343 | PERROR("zmalloc expand path"); |
| 344 | goto error; |
| 345 | } |
| 346 | |
| 347 | if (path[0] == '/') { |
| 348 | ret = lttng_strncpy(absolute_path, path, LTTNG_PATH_MAX); |
| 349 | if (ret) { |
| 350 | ERR("Path exceeds maximal size of %i bytes", LTTNG_PATH_MAX); |
| 351 | goto error; |
| 352 | } |
| 353 | } else { |
| 354 | /* |
| 355 | * This is a relative path. We need to get the present working |
| 356 | * directory and start the path walk from there. |
| 357 | */ |
| 358 | char current_working_dir[LTTNG_PATH_MAX]; |
| 359 | char *cwd_ret; |
| 360 | |
| 361 | cwd_ret = getcwd(current_working_dir, sizeof(current_working_dir)); |
| 362 | if (!cwd_ret) { |
| 363 | goto error; |
| 364 | } |
| 365 | /* |
| 366 | * Get the number of character in the CWD and allocate an array |
| 367 | * to can hold it and the path provided by the caller. |
| 368 | */ |
| 369 | ret = snprintf(absolute_path, LTTNG_PATH_MAX, "%s/%s", |
| 370 | current_working_dir, path); |
| 371 | if (ret >= LTTNG_PATH_MAX) { |
| 372 | ERR("Concatenating current working directory %s and path %s exceeds maximal size of %i bytes", |
| 373 | current_working_dir, path, LTTNG_PATH_MAX); |
| 374 | goto error; |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | if (keep_symlink) { |
| 379 | /* Resolve partially our path */ |
| 380 | absolute_path = utils_partial_realpath(absolute_path, |
| 381 | absolute_path, LTTNG_PATH_MAX); |
| 382 | } |
| 383 | |
| 384 | ret = expand_double_slashes_dot_and_dotdot(absolute_path); |
| 385 | if (ret) { |
| 386 | goto error; |
| 387 | } |
| 388 | |
| 389 | /* Identify the last token */ |
| 390 | last_token = strrchr(absolute_path, '/'); |
| 391 | |
| 392 | /* Verify that this token is not a relative path */ |
| 393 | is_dotdot = (strcmp(last_token, "/..") == 0); |
| 394 | is_dot = (strcmp(last_token, "/.") == 0); |
| 395 | |
| 396 | /* If it is, take action */ |
| 397 | if (is_dot || is_dotdot) { |
| 398 | /* For both, remove this token */ |
| 399 | *last_token = '\0'; |
| 400 | |
| 401 | /* If it was a reference to parent directory, go back one more time */ |
| 402 | if (is_dotdot) { |
| 403 | last_token = strrchr(absolute_path, '/'); |
| 404 | |
| 405 | /* If there was only one level left, we keep the first '/' */ |
| 406 | if (last_token == absolute_path) { |
| 407 | last_token++; |
| 408 | } |
| 409 | |
| 410 | *last_token = '\0'; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | return absolute_path; |
| 415 | |
| 416 | error: |
| 417 | free(absolute_path); |
| 418 | return NULL; |
| 419 | } |
| 420 | LTTNG_HIDDEN |
| 421 | char *utils_expand_path(const char *path) |
| 422 | { |
| 423 | return _utils_expand_path(path, true); |
| 424 | } |
| 425 | |
| 426 | LTTNG_HIDDEN |
| 427 | char *utils_expand_path_keep_symlink(const char *path) |
| 428 | { |
| 429 | return _utils_expand_path(path, false); |
| 430 | } |
| 431 | /* |
| 432 | * Create a pipe in dst. |
| 433 | */ |
| 434 | LTTNG_HIDDEN |
| 435 | int utils_create_pipe(int *dst) |
| 436 | { |
| 437 | int ret; |
| 438 | |
| 439 | if (dst == NULL) { |
| 440 | return -1; |
| 441 | } |
| 442 | |
| 443 | ret = pipe(dst); |
| 444 | if (ret < 0) { |
| 445 | PERROR("create pipe"); |
| 446 | } |
| 447 | |
| 448 | return ret; |
| 449 | } |
| 450 | |
| 451 | /* |
| 452 | * Create pipe and set CLOEXEC flag to both fd. |
| 453 | * |
| 454 | * Make sure the pipe opened by this function are closed at some point. Use |
| 455 | * utils_close_pipe(). |
| 456 | */ |
| 457 | LTTNG_HIDDEN |
| 458 | int utils_create_pipe_cloexec(int *dst) |
| 459 | { |
| 460 | int ret, i; |
| 461 | |
| 462 | if (dst == NULL) { |
| 463 | return -1; |
| 464 | } |
| 465 | |
| 466 | ret = utils_create_pipe(dst); |
| 467 | if (ret < 0) { |
| 468 | goto error; |
| 469 | } |
| 470 | |
| 471 | for (i = 0; i < 2; i++) { |
| 472 | ret = fcntl(dst[i], F_SETFD, FD_CLOEXEC); |
| 473 | if (ret < 0) { |
| 474 | PERROR("fcntl pipe cloexec"); |
| 475 | goto error; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | error: |
| 480 | return ret; |
| 481 | } |
| 482 | |
| 483 | /* |
| 484 | * Create pipe and set fd flags to FD_CLOEXEC and O_NONBLOCK. |
| 485 | * |
| 486 | * Make sure the pipe opened by this function are closed at some point. Use |
| 487 | * utils_close_pipe(). Using pipe() and fcntl rather than pipe2() to |
| 488 | * support OSes other than Linux 2.6.23+. |
| 489 | */ |
| 490 | LTTNG_HIDDEN |
| 491 | int utils_create_pipe_cloexec_nonblock(int *dst) |
| 492 | { |
| 493 | int ret, i; |
| 494 | |
| 495 | if (dst == NULL) { |
| 496 | return -1; |
| 497 | } |
| 498 | |
| 499 | ret = utils_create_pipe(dst); |
| 500 | if (ret < 0) { |
| 501 | goto error; |
| 502 | } |
| 503 | |
| 504 | for (i = 0; i < 2; i++) { |
| 505 | ret = fcntl(dst[i], F_SETFD, FD_CLOEXEC); |
| 506 | if (ret < 0) { |
| 507 | PERROR("fcntl pipe cloexec"); |
| 508 | goto error; |
| 509 | } |
| 510 | /* |
| 511 | * Note: we override any flag that could have been |
| 512 | * previously set on the fd. |
| 513 | */ |
| 514 | ret = fcntl(dst[i], F_SETFL, O_NONBLOCK); |
| 515 | if (ret < 0) { |
| 516 | PERROR("fcntl pipe nonblock"); |
| 517 | goto error; |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | error: |
| 522 | return ret; |
| 523 | } |
| 524 | |
| 525 | /* |
| 526 | * Close both read and write side of the pipe. |
| 527 | */ |
| 528 | LTTNG_HIDDEN |
| 529 | void utils_close_pipe(int *src) |
| 530 | { |
| 531 | int i, ret; |
| 532 | |
| 533 | if (src == NULL) { |
| 534 | return; |
| 535 | } |
| 536 | |
| 537 | for (i = 0; i < 2; i++) { |
| 538 | /* Safety check */ |
| 539 | if (src[i] < 0) { |
| 540 | continue; |
| 541 | } |
| 542 | |
| 543 | ret = close(src[i]); |
| 544 | if (ret) { |
| 545 | PERROR("close pipe"); |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | /* |
| 551 | * Create a new string using two strings range. |
| 552 | */ |
| 553 | LTTNG_HIDDEN |
| 554 | char *utils_strdupdelim(const char *begin, const char *end) |
| 555 | { |
| 556 | char *str; |
| 557 | |
| 558 | str = zmalloc(end - begin + 1); |
| 559 | if (str == NULL) { |
| 560 | PERROR("zmalloc strdupdelim"); |
| 561 | goto error; |
| 562 | } |
| 563 | |
| 564 | memcpy(str, begin, end - begin); |
| 565 | str[end - begin] = '\0'; |
| 566 | |
| 567 | error: |
| 568 | return str; |
| 569 | } |
| 570 | |
| 571 | /* |
| 572 | * Set CLOEXEC flag to the give file descriptor. |
| 573 | */ |
| 574 | LTTNG_HIDDEN |
| 575 | int utils_set_fd_cloexec(int fd) |
| 576 | { |
| 577 | int ret; |
| 578 | |
| 579 | if (fd < 0) { |
| 580 | ret = -EINVAL; |
| 581 | goto end; |
| 582 | } |
| 583 | |
| 584 | ret = fcntl(fd, F_SETFD, FD_CLOEXEC); |
| 585 | if (ret < 0) { |
| 586 | PERROR("fcntl cloexec"); |
| 587 | ret = -errno; |
| 588 | } |
| 589 | |
| 590 | end: |
| 591 | return ret; |
| 592 | } |
| 593 | |
| 594 | /* |
| 595 | * Create pid file to the given path and filename. |
| 596 | */ |
| 597 | LTTNG_HIDDEN |
| 598 | int utils_create_pid_file(pid_t pid, const char *filepath) |
| 599 | { |
| 600 | int ret; |
| 601 | FILE *fp; |
| 602 | |
| 603 | assert(filepath); |
| 604 | |
| 605 | fp = fopen(filepath, "w"); |
| 606 | if (fp == NULL) { |
| 607 | PERROR("open pid file %s", filepath); |
| 608 | ret = -1; |
| 609 | goto error; |
| 610 | } |
| 611 | |
| 612 | ret = fprintf(fp, "%d\n", (int) pid); |
| 613 | if (ret < 0) { |
| 614 | PERROR("fprintf pid file"); |
| 615 | goto error; |
| 616 | } |
| 617 | |
| 618 | if (fclose(fp)) { |
| 619 | PERROR("fclose"); |
| 620 | } |
| 621 | DBG("Pid %d written in file %s", (int) pid, filepath); |
| 622 | ret = 0; |
| 623 | error: |
| 624 | return ret; |
| 625 | } |
| 626 | |
| 627 | /* |
| 628 | * Create lock file to the given path and filename. |
| 629 | * Returns the associated file descriptor, -1 on error. |
| 630 | */ |
| 631 | LTTNG_HIDDEN |
| 632 | int utils_create_lock_file(const char *filepath) |
| 633 | { |
| 634 | int ret; |
| 635 | int fd; |
| 636 | struct flock lock; |
| 637 | |
| 638 | assert(filepath); |
| 639 | |
| 640 | memset(&lock, 0, sizeof(lock)); |
| 641 | fd = open(filepath, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | |
| 642 | S_IRGRP | S_IWGRP); |
| 643 | if (fd < 0) { |
| 644 | PERROR("open lock file %s", filepath); |
| 645 | fd = -1; |
| 646 | goto error; |
| 647 | } |
| 648 | |
| 649 | /* |
| 650 | * Attempt to lock the file. If this fails, there is |
| 651 | * already a process using the same lock file running |
| 652 | * and we should exit. |
| 653 | */ |
| 654 | lock.l_whence = SEEK_SET; |
| 655 | lock.l_type = F_WRLCK; |
| 656 | |
| 657 | ret = fcntl(fd, F_SETLK, &lock); |
| 658 | if (ret == -1) { |
| 659 | PERROR("fcntl lock file"); |
| 660 | ERR("Could not get lock file %s, another instance is running.", |
| 661 | filepath); |
| 662 | if (close(fd)) { |
| 663 | PERROR("close lock file"); |
| 664 | } |
| 665 | fd = ret; |
| 666 | goto error; |
| 667 | } |
| 668 | |
| 669 | error: |
| 670 | return fd; |
| 671 | } |
| 672 | |
| 673 | /* |
| 674 | * Create directory using the given path and mode. |
| 675 | * |
| 676 | * On success, return 0 else a negative error code. |
| 677 | */ |
| 678 | LTTNG_HIDDEN |
| 679 | int utils_mkdir(const char *path, mode_t mode, int uid, int gid) |
| 680 | { |
| 681 | int ret; |
| 682 | struct lttng_directory_handle handle; |
| 683 | const struct lttng_credentials creds = { |
| 684 | .uid = (uid_t) uid, |
| 685 | .gid = (gid_t) gid, |
| 686 | }; |
| 687 | |
| 688 | ret = lttng_directory_handle_init(&handle, NULL); |
| 689 | if (ret) { |
| 690 | goto end; |
| 691 | } |
| 692 | ret = lttng_directory_handle_create_subdirectory_as_user( |
| 693 | &handle, path, mode, |
| 694 | (uid >= 0 || gid >= 0) ? &creds : NULL); |
| 695 | lttng_directory_handle_fini(&handle); |
| 696 | end: |
| 697 | return ret; |
| 698 | } |
| 699 | |
| 700 | /* |
| 701 | * Recursively create directory using the given path and mode, under the |
| 702 | * provided uid and gid. |
| 703 | * |
| 704 | * On success, return 0 else a negative error code. |
| 705 | */ |
| 706 | LTTNG_HIDDEN |
| 707 | int utils_mkdir_recursive(const char *path, mode_t mode, int uid, int gid) |
| 708 | { |
| 709 | int ret; |
| 710 | struct lttng_directory_handle handle; |
| 711 | const struct lttng_credentials creds = { |
| 712 | .uid = (uid_t) uid, |
| 713 | .gid = (gid_t) gid, |
| 714 | }; |
| 715 | |
| 716 | ret = lttng_directory_handle_init(&handle, NULL); |
| 717 | if (ret) { |
| 718 | goto end; |
| 719 | } |
| 720 | ret = lttng_directory_handle_create_subdirectory_recursive_as_user( |
| 721 | &handle, path, mode, |
| 722 | (uid >= 0 || gid >= 0) ? &creds : NULL); |
| 723 | lttng_directory_handle_fini(&handle); |
| 724 | end: |
| 725 | return ret; |
| 726 | } |
| 727 | |
| 728 | /* |
| 729 | * out_stream_path is the output parameter. |
| 730 | * |
| 731 | * Return 0 on success or else a negative value. |
| 732 | */ |
| 733 | LTTNG_HIDDEN |
| 734 | int utils_stream_file_path(const char *path_name, const char *file_name, |
| 735 | uint64_t size, uint64_t count, const char *suffix, |
| 736 | char *out_stream_path, size_t stream_path_len) |
| 737 | { |
| 738 | int ret; |
| 739 | char count_str[MAX_INT_DEC_LEN(count) + 1] = {}; |
| 740 | const char *path_separator; |
| 741 | |
| 742 | if (path_name && path_name[strlen(path_name) - 1] == '/') { |
| 743 | path_separator = ""; |
| 744 | } else { |
| 745 | path_separator = "/"; |
| 746 | } |
| 747 | |
| 748 | path_name = path_name ? : ""; |
| 749 | suffix = suffix ? : ""; |
| 750 | if (size > 0) { |
| 751 | ret = snprintf(count_str, sizeof(count_str), "_%" PRIu64, |
| 752 | count); |
| 753 | assert(ret > 0 && ret < sizeof(count_str)); |
| 754 | } |
| 755 | |
| 756 | ret = snprintf(out_stream_path, stream_path_len, "%s%s%s%s%s", |
| 757 | path_name, path_separator, file_name, count_str, |
| 758 | suffix); |
| 759 | if (ret < 0 || ret >= stream_path_len) { |
| 760 | ERR("Truncation occurred while formatting stream path"); |
| 761 | ret = -1; |
| 762 | } else { |
| 763 | ret = 0; |
| 764 | } |
| 765 | return ret; |
| 766 | } |
| 767 | |
| 768 | /* |
| 769 | * Create the stream file on disk. |
| 770 | * |
| 771 | * Return 0 on success or else a negative value. |
| 772 | */ |
| 773 | LTTNG_HIDDEN |
| 774 | int utils_create_stream_file(const char *path_name, char *file_name, uint64_t size, |
| 775 | uint64_t count, int uid, int gid, char *suffix) |
| 776 | { |
| 777 | int ret, flags, mode; |
| 778 | char path[LTTNG_PATH_MAX]; |
| 779 | |
| 780 | ret = utils_stream_file_path(path_name, file_name, |
| 781 | size, count, suffix, path, sizeof(path)); |
| 782 | if (ret < 0) { |
| 783 | goto error; |
| 784 | } |
| 785 | |
| 786 | /* |
| 787 | * With the session rotation feature on the relay, we might need to seek |
| 788 | * and truncate a tracefile, so we need read and write access. |
| 789 | */ |
| 790 | flags = O_RDWR | O_CREAT | O_TRUNC; |
| 791 | /* Open with 660 mode */ |
| 792 | mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; |
| 793 | |
| 794 | if (uid < 0 || gid < 0) { |
| 795 | ret = open(path, flags, mode); |
| 796 | } else { |
| 797 | ret = run_as_open(path, flags, mode, uid, gid); |
| 798 | } |
| 799 | if (ret < 0) { |
| 800 | PERROR("open stream path %s", path); |
| 801 | } |
| 802 | error: |
| 803 | return ret; |
| 804 | } |
| 805 | |
| 806 | /* |
| 807 | * Unlink the stream tracefile from disk. |
| 808 | * |
| 809 | * Return 0 on success or else a negative value. |
| 810 | */ |
| 811 | LTTNG_HIDDEN |
| 812 | int utils_unlink_stream_file(const char *path_name, char *file_name, uint64_t size, |
| 813 | uint64_t count, int uid, int gid, char *suffix) |
| 814 | { |
| 815 | int ret; |
| 816 | char path[LTTNG_PATH_MAX]; |
| 817 | |
| 818 | ret = utils_stream_file_path(path_name, file_name, size, count, suffix, |
| 819 | path, sizeof(path)); |
| 820 | if (ret < 0) { |
| 821 | goto error; |
| 822 | } |
| 823 | if (uid < 0 || gid < 0) { |
| 824 | ret = unlink(path); |
| 825 | } else { |
| 826 | ret = run_as_unlink(path, uid, gid); |
| 827 | } |
| 828 | if (ret < 0) { |
| 829 | goto error; |
| 830 | } |
| 831 | error: |
| 832 | DBG("utils_unlink_stream_file %s returns %d", path, ret); |
| 833 | return ret; |
| 834 | } |
| 835 | |
| 836 | /* |
| 837 | * Change the output tracefile according to the given size and count The |
| 838 | * new_count pointer is set during this operation. |
| 839 | * |
| 840 | * From the consumer, the stream lock MUST be held before calling this function |
| 841 | * because we are modifying the stream status. |
| 842 | * |
| 843 | * Return 0 on success or else a negative value. |
| 844 | */ |
| 845 | LTTNG_HIDDEN |
| 846 | int utils_rotate_stream_file(char *path_name, char *file_name, uint64_t size, |
| 847 | uint64_t count, int uid, int gid, int out_fd, uint64_t *new_count, |
| 848 | int *stream_fd) |
| 849 | { |
| 850 | int ret; |
| 851 | |
| 852 | assert(stream_fd); |
| 853 | |
| 854 | ret = close(out_fd); |
| 855 | if (ret < 0) { |
| 856 | PERROR("Closing tracefile"); |
| 857 | goto error; |
| 858 | } |
| 859 | *stream_fd = -1; |
| 860 | |
| 861 | if (count > 0) { |
| 862 | /* |
| 863 | * In tracefile rotation, for the relay daemon we need |
| 864 | * to unlink the old file if present, because it may |
| 865 | * still be open in reading by the live thread, and we |
| 866 | * need to ensure that we do not overwrite the content |
| 867 | * between get_index and get_packet. Since we have no |
| 868 | * way to verify integrity of the data content compared |
| 869 | * to the associated index, we need to ensure the reader |
| 870 | * has exclusive access to the file content, and that |
| 871 | * the open of the data file is performed in get_index. |
| 872 | * Unlinking the old file rather than overwriting it |
| 873 | * achieves this. |
| 874 | */ |
| 875 | if (new_count) { |
| 876 | *new_count = (*new_count + 1) % count; |
| 877 | } |
| 878 | ret = utils_unlink_stream_file(path_name, file_name, size, |
| 879 | new_count ? *new_count : 0, uid, gid, 0); |
| 880 | if (ret < 0 && errno != ENOENT) { |
| 881 | goto error; |
| 882 | } |
| 883 | } else { |
| 884 | if (new_count) { |
| 885 | (*new_count)++; |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | ret = utils_create_stream_file(path_name, file_name, size, |
| 890 | new_count ? *new_count : 0, uid, gid, 0); |
| 891 | if (ret < 0) { |
| 892 | goto error; |
| 893 | } |
| 894 | *stream_fd = ret; |
| 895 | |
| 896 | /* Success. */ |
| 897 | ret = 0; |
| 898 | |
| 899 | error: |
| 900 | return ret; |
| 901 | } |
| 902 | |
| 903 | |
| 904 | /** |
| 905 | * Parse a string that represents a size in human readable format. It |
| 906 | * supports decimal integers suffixed by 'k', 'K', 'M' or 'G'. |
| 907 | * |
| 908 | * The suffix multiply the integer by: |
| 909 | * 'k': 1024 |
| 910 | * 'M': 1024^2 |
| 911 | * 'G': 1024^3 |
| 912 | * |
| 913 | * @param str The string to parse. |
| 914 | * @param size Pointer to a uint64_t that will be filled with the |
| 915 | * resulting size. |
| 916 | * |
| 917 | * @return 0 on success, -1 on failure. |
| 918 | */ |
| 919 | LTTNG_HIDDEN |
| 920 | int utils_parse_size_suffix(const char * const str, uint64_t * const size) |
| 921 | { |
| 922 | int ret; |
| 923 | uint64_t base_size; |
| 924 | long shift = 0; |
| 925 | const char *str_end; |
| 926 | char *num_end; |
| 927 | |
| 928 | if (!str) { |
| 929 | DBG("utils_parse_size_suffix: received a NULL string."); |
| 930 | ret = -1; |
| 931 | goto end; |
| 932 | } |
| 933 | |
| 934 | /* strtoull will accept a negative number, but we don't want to. */ |
| 935 | if (strchr(str, '-') != NULL) { |
| 936 | DBG("utils_parse_size_suffix: invalid size string, should not contain '-'."); |
| 937 | ret = -1; |
| 938 | goto end; |
| 939 | } |
| 940 | |
| 941 | /* str_end will point to the \0 */ |
| 942 | str_end = str + strlen(str); |
| 943 | errno = 0; |
| 944 | base_size = strtoull(str, &num_end, 0); |
| 945 | if (errno != 0) { |
| 946 | PERROR("utils_parse_size_suffix strtoull"); |
| 947 | ret = -1; |
| 948 | goto end; |
| 949 | } |
| 950 | |
| 951 | if (num_end == str) { |
| 952 | /* strtoull parsed nothing, not good. */ |
| 953 | DBG("utils_parse_size_suffix: strtoull had nothing good to parse."); |
| 954 | ret = -1; |
| 955 | goto end; |
| 956 | } |
| 957 | |
| 958 | /* Check if a prefix is present. */ |
| 959 | switch (*num_end) { |
| 960 | case 'G': |
| 961 | shift = GIBI_LOG2; |
| 962 | num_end++; |
| 963 | break; |
| 964 | case 'M': /* */ |
| 965 | shift = MEBI_LOG2; |
| 966 | num_end++; |
| 967 | break; |
| 968 | case 'K': |
| 969 | case 'k': |
| 970 | shift = KIBI_LOG2; |
| 971 | num_end++; |
| 972 | break; |
| 973 | case '\0': |
| 974 | break; |
| 975 | default: |
| 976 | DBG("utils_parse_size_suffix: invalid suffix."); |
| 977 | ret = -1; |
| 978 | goto end; |
| 979 | } |
| 980 | |
| 981 | /* Check for garbage after the valid input. */ |
| 982 | if (num_end != str_end) { |
| 983 | DBG("utils_parse_size_suffix: Garbage after size string."); |
| 984 | ret = -1; |
| 985 | goto end; |
| 986 | } |
| 987 | |
| 988 | *size = base_size << shift; |
| 989 | |
| 990 | /* Check for overflow */ |
| 991 | if ((*size >> shift) != base_size) { |
| 992 | DBG("utils_parse_size_suffix: oops, overflow detected."); |
| 993 | ret = -1; |
| 994 | goto end; |
| 995 | } |
| 996 | |
| 997 | ret = 0; |
| 998 | end: |
| 999 | return ret; |
| 1000 | } |
| 1001 | |
| 1002 | /** |
| 1003 | * Parse a string that represents a time in human readable format. It |
| 1004 | * supports decimal integers suffixed by: |
| 1005 | * "us" for microsecond, |
| 1006 | * "ms" for millisecond, |
| 1007 | * "s" for second, |
| 1008 | * "m" for minute, |
| 1009 | * "h" for hour |
| 1010 | * |
| 1011 | * The suffix multiply the integer by: |
| 1012 | * "us" : 1 |
| 1013 | * "ms" : 1000 |
| 1014 | * "s" : 1000000 |
| 1015 | * "m" : 60000000 |
| 1016 | * "h" : 3600000000 |
| 1017 | * |
| 1018 | * Note that unit-less numbers are assumed to be microseconds. |
| 1019 | * |
| 1020 | * @param str The string to parse, assumed to be NULL-terminated. |
| 1021 | * @param time_us Pointer to a uint64_t that will be filled with the |
| 1022 | * resulting time in microseconds. |
| 1023 | * |
| 1024 | * @return 0 on success, -1 on failure. |
| 1025 | */ |
| 1026 | LTTNG_HIDDEN |
| 1027 | int utils_parse_time_suffix(char const * const str, uint64_t * const time_us) |
| 1028 | { |
| 1029 | int ret; |
| 1030 | uint64_t base_time; |
| 1031 | uint64_t multiplier = 1; |
| 1032 | const char *str_end; |
| 1033 | char *num_end; |
| 1034 | |
| 1035 | if (!str) { |
| 1036 | DBG("utils_parse_time_suffix: received a NULL string."); |
| 1037 | ret = -1; |
| 1038 | goto end; |
| 1039 | } |
| 1040 | |
| 1041 | /* strtoull will accept a negative number, but we don't want to. */ |
| 1042 | if (strchr(str, '-') != NULL) { |
| 1043 | DBG("utils_parse_time_suffix: invalid time string, should not contain '-'."); |
| 1044 | ret = -1; |
| 1045 | goto end; |
| 1046 | } |
| 1047 | |
| 1048 | /* str_end will point to the \0 */ |
| 1049 | str_end = str + strlen(str); |
| 1050 | errno = 0; |
| 1051 | base_time = strtoull(str, &num_end, 10); |
| 1052 | if (errno != 0) { |
| 1053 | PERROR("utils_parse_time_suffix strtoull on string \"%s\"", str); |
| 1054 | ret = -1; |
| 1055 | goto end; |
| 1056 | } |
| 1057 | |
| 1058 | if (num_end == str) { |
| 1059 | /* strtoull parsed nothing, not good. */ |
| 1060 | DBG("utils_parse_time_suffix: strtoull had nothing good to parse."); |
| 1061 | ret = -1; |
| 1062 | goto end; |
| 1063 | } |
| 1064 | |
| 1065 | /* Check if a prefix is present. */ |
| 1066 | switch (*num_end) { |
| 1067 | case 'u': |
| 1068 | /* |
| 1069 | * Microsecond (us) |
| 1070 | * |
| 1071 | * Skip the "us" if the string matches the "us" suffix, |
| 1072 | * otherwise let the check for the end of the string handle |
| 1073 | * the error reporting. |
| 1074 | */ |
| 1075 | if (*(num_end + 1) == 's') { |
| 1076 | num_end += 2; |
| 1077 | } |
| 1078 | break; |
| 1079 | case 'm': |
| 1080 | if (*(num_end + 1) == 's') { |
| 1081 | /* Millisecond (ms) */ |
| 1082 | multiplier = USEC_PER_MSEC; |
| 1083 | /* Skip the 's' */ |
| 1084 | num_end++; |
| 1085 | } else { |
| 1086 | /* Minute (m) */ |
| 1087 | multiplier = USEC_PER_MINUTE; |
| 1088 | } |
| 1089 | num_end++; |
| 1090 | break; |
| 1091 | case 's': |
| 1092 | /* Second */ |
| 1093 | multiplier = USEC_PER_SEC; |
| 1094 | num_end++; |
| 1095 | break; |
| 1096 | case 'h': |
| 1097 | /* Hour */ |
| 1098 | multiplier = USEC_PER_HOURS; |
| 1099 | num_end++; |
| 1100 | break; |
| 1101 | case '\0': |
| 1102 | break; |
| 1103 | default: |
| 1104 | DBG("utils_parse_time_suffix: invalid suffix."); |
| 1105 | ret = -1; |
| 1106 | goto end; |
| 1107 | } |
| 1108 | |
| 1109 | /* Check for garbage after the valid input. */ |
| 1110 | if (num_end != str_end) { |
| 1111 | DBG("utils_parse_time_suffix: Garbage after time string."); |
| 1112 | ret = -1; |
| 1113 | goto end; |
| 1114 | } |
| 1115 | |
| 1116 | *time_us = base_time * multiplier; |
| 1117 | |
| 1118 | /* Check for overflow */ |
| 1119 | if ((*time_us / multiplier) != base_time) { |
| 1120 | DBG("utils_parse_time_suffix: oops, overflow detected."); |
| 1121 | ret = -1; |
| 1122 | goto end; |
| 1123 | } |
| 1124 | |
| 1125 | ret = 0; |
| 1126 | end: |
| 1127 | return ret; |
| 1128 | } |
| 1129 | |
| 1130 | /* |
| 1131 | * fls: returns the position of the most significant bit. |
| 1132 | * Returns 0 if no bit is set, else returns the position of the most |
| 1133 | * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit). |
| 1134 | */ |
| 1135 | #if defined(__i386) || defined(__x86_64) |
| 1136 | static inline unsigned int fls_u32(uint32_t x) |
| 1137 | { |
| 1138 | int r; |
| 1139 | |
| 1140 | asm("bsrl %1,%0\n\t" |
| 1141 | "jnz 1f\n\t" |
| 1142 | "movl $-1,%0\n\t" |
| 1143 | "1:\n\t" |
| 1144 | : "=r" (r) : "rm" (x)); |
| 1145 | return r + 1; |
| 1146 | } |
| 1147 | #define HAS_FLS_U32 |
| 1148 | #endif |
| 1149 | |
| 1150 | #if defined(__x86_64) |
| 1151 | static inline |
| 1152 | unsigned int fls_u64(uint64_t x) |
| 1153 | { |
| 1154 | long r; |
| 1155 | |
| 1156 | asm("bsrq %1,%0\n\t" |
| 1157 | "jnz 1f\n\t" |
| 1158 | "movq $-1,%0\n\t" |
| 1159 | "1:\n\t" |
| 1160 | : "=r" (r) : "rm" (x)); |
| 1161 | return r + 1; |
| 1162 | } |
| 1163 | #define HAS_FLS_U64 |
| 1164 | #endif |
| 1165 | |
| 1166 | #ifndef HAS_FLS_U64 |
| 1167 | static __attribute__((unused)) |
| 1168 | unsigned int fls_u64(uint64_t x) |
| 1169 | { |
| 1170 | unsigned int r = 64; |
| 1171 | |
| 1172 | if (!x) |
| 1173 | return 0; |
| 1174 | |
| 1175 | if (!(x & 0xFFFFFFFF00000000ULL)) { |
| 1176 | x <<= 32; |
| 1177 | r -= 32; |
| 1178 | } |
| 1179 | if (!(x & 0xFFFF000000000000ULL)) { |
| 1180 | x <<= 16; |
| 1181 | r -= 16; |
| 1182 | } |
| 1183 | if (!(x & 0xFF00000000000000ULL)) { |
| 1184 | x <<= 8; |
| 1185 | r -= 8; |
| 1186 | } |
| 1187 | if (!(x & 0xF000000000000000ULL)) { |
| 1188 | x <<= 4; |
| 1189 | r -= 4; |
| 1190 | } |
| 1191 | if (!(x & 0xC000000000000000ULL)) { |
| 1192 | x <<= 2; |
| 1193 | r -= 2; |
| 1194 | } |
| 1195 | if (!(x & 0x8000000000000000ULL)) { |
| 1196 | x <<= 1; |
| 1197 | r -= 1; |
| 1198 | } |
| 1199 | return r; |
| 1200 | } |
| 1201 | #endif |
| 1202 | |
| 1203 | #ifndef HAS_FLS_U32 |
| 1204 | static __attribute__((unused)) unsigned int fls_u32(uint32_t x) |
| 1205 | { |
| 1206 | unsigned int r = 32; |
| 1207 | |
| 1208 | if (!x) { |
| 1209 | return 0; |
| 1210 | } |
| 1211 | if (!(x & 0xFFFF0000U)) { |
| 1212 | x <<= 16; |
| 1213 | r -= 16; |
| 1214 | } |
| 1215 | if (!(x & 0xFF000000U)) { |
| 1216 | x <<= 8; |
| 1217 | r -= 8; |
| 1218 | } |
| 1219 | if (!(x & 0xF0000000U)) { |
| 1220 | x <<= 4; |
| 1221 | r -= 4; |
| 1222 | } |
| 1223 | if (!(x & 0xC0000000U)) { |
| 1224 | x <<= 2; |
| 1225 | r -= 2; |
| 1226 | } |
| 1227 | if (!(x & 0x80000000U)) { |
| 1228 | x <<= 1; |
| 1229 | r -= 1; |
| 1230 | } |
| 1231 | return r; |
| 1232 | } |
| 1233 | #endif |
| 1234 | |
| 1235 | /* |
| 1236 | * Return the minimum order for which x <= (1UL << order). |
| 1237 | * Return -1 if x is 0. |
| 1238 | */ |
| 1239 | LTTNG_HIDDEN |
| 1240 | int utils_get_count_order_u32(uint32_t x) |
| 1241 | { |
| 1242 | if (!x) { |
| 1243 | return -1; |
| 1244 | } |
| 1245 | |
| 1246 | return fls_u32(x - 1); |
| 1247 | } |
| 1248 | |
| 1249 | /* |
| 1250 | * Return the minimum order for which x <= (1UL << order). |
| 1251 | * Return -1 if x is 0. |
| 1252 | */ |
| 1253 | LTTNG_HIDDEN |
| 1254 | int utils_get_count_order_u64(uint64_t x) |
| 1255 | { |
| 1256 | if (!x) { |
| 1257 | return -1; |
| 1258 | } |
| 1259 | |
| 1260 | return fls_u64(x - 1); |
| 1261 | } |
| 1262 | |
| 1263 | /** |
| 1264 | * Obtain the value of LTTNG_HOME environment variable, if exists. |
| 1265 | * Otherwise returns the value of HOME. |
| 1266 | */ |
| 1267 | LTTNG_HIDDEN |
| 1268 | char *utils_get_home_dir(void) |
| 1269 | { |
| 1270 | char *val = NULL; |
| 1271 | struct passwd *pwd; |
| 1272 | |
| 1273 | val = lttng_secure_getenv(DEFAULT_LTTNG_HOME_ENV_VAR); |
| 1274 | if (val != NULL) { |
| 1275 | goto end; |
| 1276 | } |
| 1277 | val = lttng_secure_getenv(DEFAULT_LTTNG_FALLBACK_HOME_ENV_VAR); |
| 1278 | if (val != NULL) { |
| 1279 | goto end; |
| 1280 | } |
| 1281 | |
| 1282 | /* Fallback on the password file entry. */ |
| 1283 | pwd = getpwuid(getuid()); |
| 1284 | if (!pwd) { |
| 1285 | goto end; |
| 1286 | } |
| 1287 | val = pwd->pw_dir; |
| 1288 | |
| 1289 | DBG3("Home directory is '%s'", val); |
| 1290 | |
| 1291 | end: |
| 1292 | return val; |
| 1293 | } |
| 1294 | |
| 1295 | /** |
| 1296 | * Get user's home directory. Dynamically allocated, must be freed |
| 1297 | * by the caller. |
| 1298 | */ |
| 1299 | LTTNG_HIDDEN |
| 1300 | char *utils_get_user_home_dir(uid_t uid) |
| 1301 | { |
| 1302 | struct passwd pwd; |
| 1303 | struct passwd *result; |
| 1304 | char *home_dir = NULL; |
| 1305 | char *buf = NULL; |
| 1306 | long buflen; |
| 1307 | int ret; |
| 1308 | |
| 1309 | buflen = sysconf(_SC_GETPW_R_SIZE_MAX); |
| 1310 | if (buflen == -1) { |
| 1311 | goto end; |
| 1312 | } |
| 1313 | retry: |
| 1314 | buf = zmalloc(buflen); |
| 1315 | if (!buf) { |
| 1316 | goto end; |
| 1317 | } |
| 1318 | |
| 1319 | ret = getpwuid_r(uid, &pwd, buf, buflen, &result); |
| 1320 | if (ret || !result) { |
| 1321 | if (ret == ERANGE) { |
| 1322 | free(buf); |
| 1323 | buflen *= 2; |
| 1324 | goto retry; |
| 1325 | } |
| 1326 | goto end; |
| 1327 | } |
| 1328 | |
| 1329 | home_dir = strdup(pwd.pw_dir); |
| 1330 | end: |
| 1331 | free(buf); |
| 1332 | return home_dir; |
| 1333 | } |
| 1334 | |
| 1335 | /* |
| 1336 | * With the given format, fill dst with the time of len maximum siz. |
| 1337 | * |
| 1338 | * Return amount of bytes set in the buffer or else 0 on error. |
| 1339 | */ |
| 1340 | LTTNG_HIDDEN |
| 1341 | size_t utils_get_current_time_str(const char *format, char *dst, size_t len) |
| 1342 | { |
| 1343 | size_t ret; |
| 1344 | time_t rawtime; |
| 1345 | struct tm *timeinfo; |
| 1346 | |
| 1347 | assert(format); |
| 1348 | assert(dst); |
| 1349 | |
| 1350 | /* Get date and time for session path */ |
| 1351 | time(&rawtime); |
| 1352 | timeinfo = localtime(&rawtime); |
| 1353 | ret = strftime(dst, len, format, timeinfo); |
| 1354 | if (ret == 0) { |
| 1355 | ERR("Unable to strftime with format %s at dst %p of len %zu", format, |
| 1356 | dst, len); |
| 1357 | } |
| 1358 | |
| 1359 | return ret; |
| 1360 | } |
| 1361 | |
| 1362 | /* |
| 1363 | * Return 0 on success and set *gid to the group_ID matching the passed name. |
| 1364 | * Else -1 if it cannot be found or an error occurred. |
| 1365 | */ |
| 1366 | LTTNG_HIDDEN |
| 1367 | int utils_get_group_id(const char *name, bool warn, gid_t *gid) |
| 1368 | { |
| 1369 | static volatile int warn_once; |
| 1370 | int ret; |
| 1371 | long sys_len; |
| 1372 | size_t len; |
| 1373 | struct group grp; |
| 1374 | struct group *result; |
| 1375 | struct lttng_dynamic_buffer buffer; |
| 1376 | |
| 1377 | /* Get the system limit, if it exists. */ |
| 1378 | sys_len = sysconf(_SC_GETGR_R_SIZE_MAX); |
| 1379 | if (sys_len == -1) { |
| 1380 | len = 1024; |
| 1381 | } else { |
| 1382 | len = (size_t) sys_len; |
| 1383 | } |
| 1384 | |
| 1385 | lttng_dynamic_buffer_init(&buffer); |
| 1386 | ret = lttng_dynamic_buffer_set_size(&buffer, len); |
| 1387 | if (ret) { |
| 1388 | ERR("Failed to allocate group info buffer"); |
| 1389 | ret = -1; |
| 1390 | goto error; |
| 1391 | } |
| 1392 | |
| 1393 | while ((ret = getgrnam_r(name, &grp, buffer.data, buffer.size, &result)) == ERANGE) { |
| 1394 | const size_t new_len = 2 * buffer.size; |
| 1395 | |
| 1396 | /* Buffer is not big enough, increase its size. */ |
| 1397 | if (new_len < buffer.size) { |
| 1398 | ERR("Group info buffer size overflow"); |
| 1399 | ret = -1; |
| 1400 | goto error; |
| 1401 | } |
| 1402 | |
| 1403 | ret = lttng_dynamic_buffer_set_size(&buffer, new_len); |
| 1404 | if (ret) { |
| 1405 | ERR("Failed to grow group info buffer to %zu bytes", |
| 1406 | new_len); |
| 1407 | ret = -1; |
| 1408 | goto error; |
| 1409 | } |
| 1410 | } |
| 1411 | if (ret) { |
| 1412 | PERROR("Failed to get group file entry for group name \"%s\"", |
| 1413 | name); |
| 1414 | ret = -1; |
| 1415 | goto error; |
| 1416 | } |
| 1417 | |
| 1418 | /* Group not found. */ |
| 1419 | if (!result) { |
| 1420 | ret = -1; |
| 1421 | goto error; |
| 1422 | } |
| 1423 | |
| 1424 | *gid = result->gr_gid; |
| 1425 | ret = 0; |
| 1426 | |
| 1427 | error: |
| 1428 | if (ret && warn && !warn_once) { |
| 1429 | WARN("No tracing group detected"); |
| 1430 | warn_once = 1; |
| 1431 | } |
| 1432 | lttng_dynamic_buffer_reset(&buffer); |
| 1433 | return ret; |
| 1434 | } |
| 1435 | |
| 1436 | /* |
| 1437 | * Return a newly allocated option string. This string is to be used as the |
| 1438 | * optstring argument of getopt_long(), see GETOPT(3). opt_count is the number |
| 1439 | * of elements in the long_options array. Returns NULL if the string's |
| 1440 | * allocation fails. |
| 1441 | */ |
| 1442 | LTTNG_HIDDEN |
| 1443 | char *utils_generate_optstring(const struct option *long_options, |
| 1444 | size_t opt_count) |
| 1445 | { |
| 1446 | int i; |
| 1447 | size_t string_len = opt_count, str_pos = 0; |
| 1448 | char *optstring; |
| 1449 | |
| 1450 | /* |
| 1451 | * Compute the necessary string length. One letter per option, two when an |
| 1452 | * argument is necessary, and a trailing NULL. |
| 1453 | */ |
| 1454 | for (i = 0; i < opt_count; i++) { |
| 1455 | string_len += long_options[i].has_arg ? 1 : 0; |
| 1456 | } |
| 1457 | |
| 1458 | optstring = zmalloc(string_len); |
| 1459 | if (!optstring) { |
| 1460 | goto end; |
| 1461 | } |
| 1462 | |
| 1463 | for (i = 0; i < opt_count; i++) { |
| 1464 | if (!long_options[i].name) { |
| 1465 | /* Got to the trailing NULL element */ |
| 1466 | break; |
| 1467 | } |
| 1468 | |
| 1469 | if (long_options[i].val != '\0') { |
| 1470 | optstring[str_pos++] = (char) long_options[i].val; |
| 1471 | if (long_options[i].has_arg) { |
| 1472 | optstring[str_pos++] = ':'; |
| 1473 | } |
| 1474 | } |
| 1475 | } |
| 1476 | |
| 1477 | end: |
| 1478 | return optstring; |
| 1479 | } |
| 1480 | |
| 1481 | /* |
| 1482 | * Try to remove a hierarchy of empty directories, recursively. Don't unlink |
| 1483 | * any file. Try to rmdir any empty directory within the hierarchy. |
| 1484 | */ |
| 1485 | LTTNG_HIDDEN |
| 1486 | int utils_recursive_rmdir(const char *path) |
| 1487 | { |
| 1488 | DIR *dir; |
| 1489 | size_t path_len; |
| 1490 | int dir_fd, ret = 0, closeret, is_empty = 1; |
| 1491 | struct dirent *entry; |
| 1492 | |
| 1493 | /* Open directory */ |
| 1494 | dir = opendir(path); |
| 1495 | if (!dir) { |
| 1496 | PERROR("Cannot open '%s' path", path); |
| 1497 | return -1; |
| 1498 | } |
| 1499 | dir_fd = lttng_dirfd(dir); |
| 1500 | if (dir_fd < 0) { |
| 1501 | PERROR("lttng_dirfd"); |
| 1502 | return -1; |
| 1503 | } |
| 1504 | |
| 1505 | path_len = strlen(path); |
| 1506 | while ((entry = readdir(dir))) { |
| 1507 | struct stat st; |
| 1508 | size_t name_len; |
| 1509 | char filename[PATH_MAX]; |
| 1510 | |
| 1511 | if (!strcmp(entry->d_name, ".") |
| 1512 | || !strcmp(entry->d_name, "..")) { |
| 1513 | continue; |
| 1514 | } |
| 1515 | |
| 1516 | name_len = strlen(entry->d_name); |
| 1517 | if (path_len + name_len + 2 > sizeof(filename)) { |
| 1518 | ERR("Failed to remove file: path name too long (%s/%s)", |
| 1519 | path, entry->d_name); |
| 1520 | continue; |
| 1521 | } |
| 1522 | if (snprintf(filename, sizeof(filename), "%s/%s", |
| 1523 | path, entry->d_name) < 0) { |
| 1524 | ERR("Failed to format path."); |
| 1525 | continue; |
| 1526 | } |
| 1527 | |
| 1528 | if (stat(filename, &st)) { |
| 1529 | PERROR("stat"); |
| 1530 | continue; |
| 1531 | } |
| 1532 | |
| 1533 | if (S_ISDIR(st.st_mode)) { |
| 1534 | char subpath[PATH_MAX]; |
| 1535 | |
| 1536 | strncpy(subpath, path, PATH_MAX); |
| 1537 | subpath[PATH_MAX - 1] = '\0'; |
| 1538 | strncat(subpath, "/", |
| 1539 | PATH_MAX - strlen(subpath) - 1); |
| 1540 | strncat(subpath, entry->d_name, |
| 1541 | PATH_MAX - strlen(subpath) - 1); |
| 1542 | if (utils_recursive_rmdir(subpath)) { |
| 1543 | is_empty = 0; |
| 1544 | } |
| 1545 | } else if (S_ISREG(st.st_mode)) { |
| 1546 | is_empty = 0; |
| 1547 | } else { |
| 1548 | ret = -EINVAL; |
| 1549 | goto end; |
| 1550 | } |
| 1551 | } |
| 1552 | end: |
| 1553 | closeret = closedir(dir); |
| 1554 | if (closeret) { |
| 1555 | PERROR("closedir"); |
| 1556 | } |
| 1557 | if (is_empty) { |
| 1558 | DBG3("Attempting rmdir %s", path); |
| 1559 | ret = rmdir(path); |
| 1560 | } |
| 1561 | return ret; |
| 1562 | } |
| 1563 | |
| 1564 | LTTNG_HIDDEN |
| 1565 | int utils_truncate_stream_file(int fd, off_t length) |
| 1566 | { |
| 1567 | int ret; |
| 1568 | off_t lseek_ret; |
| 1569 | |
| 1570 | ret = ftruncate(fd, length); |
| 1571 | if (ret < 0) { |
| 1572 | PERROR("ftruncate"); |
| 1573 | goto end; |
| 1574 | } |
| 1575 | lseek_ret = lseek(fd, length, SEEK_SET); |
| 1576 | if (lseek_ret < 0) { |
| 1577 | PERROR("lseek"); |
| 1578 | ret = -1; |
| 1579 | goto end; |
| 1580 | } |
| 1581 | end: |
| 1582 | return ret; |
| 1583 | } |
| 1584 | |
| 1585 | static const char *get_man_bin_path(void) |
| 1586 | { |
| 1587 | char *env_man_path = lttng_secure_getenv(DEFAULT_MAN_BIN_PATH_ENV); |
| 1588 | |
| 1589 | if (env_man_path) { |
| 1590 | return env_man_path; |
| 1591 | } |
| 1592 | |
| 1593 | return DEFAULT_MAN_BIN_PATH; |
| 1594 | } |
| 1595 | |
| 1596 | LTTNG_HIDDEN |
| 1597 | int utils_show_help(int section, const char *page_name, |
| 1598 | const char *help_msg) |
| 1599 | { |
| 1600 | char section_string[8]; |
| 1601 | const char *man_bin_path = get_man_bin_path(); |
| 1602 | int ret = 0; |
| 1603 | |
| 1604 | if (help_msg) { |
| 1605 | printf("%s", help_msg); |
| 1606 | goto end; |
| 1607 | } |
| 1608 | |
| 1609 | /* Section integer -> section string */ |
| 1610 | ret = sprintf(section_string, "%d", section); |
| 1611 | assert(ret > 0 && ret < 8); |
| 1612 | |
| 1613 | /* |
| 1614 | * Execute man pager. |
| 1615 | * |
| 1616 | * We provide -M to man here because LTTng-tools can |
| 1617 | * be installed outside /usr, in which case its man pages are |
| 1618 | * not located in the default /usr/share/man directory. |
| 1619 | */ |
| 1620 | ret = execlp(man_bin_path, "man", "-M", MANPATH, |
| 1621 | section_string, page_name, NULL); |
| 1622 | |
| 1623 | end: |
| 1624 | return ret; |
| 1625 | } |
| 1626 | |
| 1627 | static |
| 1628 | int read_proc_meminfo_field(const char *field, size_t *value) |
| 1629 | { |
| 1630 | int ret; |
| 1631 | FILE *proc_meminfo; |
| 1632 | char name[PROC_MEMINFO_FIELD_MAX_NAME_LEN] = {}; |
| 1633 | |
| 1634 | proc_meminfo = fopen(PROC_MEMINFO_PATH, "r"); |
| 1635 | if (!proc_meminfo) { |
| 1636 | PERROR("Failed to fopen() " PROC_MEMINFO_PATH); |
| 1637 | ret = -1; |
| 1638 | goto fopen_error; |
| 1639 | } |
| 1640 | |
| 1641 | /* |
| 1642 | * Read the contents of /proc/meminfo line by line to find the right |
| 1643 | * field. |
| 1644 | */ |
| 1645 | while (!feof(proc_meminfo)) { |
| 1646 | unsigned long value_kb; |
| 1647 | |
| 1648 | ret = fscanf(proc_meminfo, |
| 1649 | "%" MAX_NAME_LEN_SCANF_IS_A_BROKEN_API "s %lu kB\n", |
| 1650 | name, &value_kb); |
| 1651 | if (ret == EOF) { |
| 1652 | /* |
| 1653 | * fscanf() returning EOF can indicate EOF or an error. |
| 1654 | */ |
| 1655 | if (ferror(proc_meminfo)) { |
| 1656 | PERROR("Failed to parse " PROC_MEMINFO_PATH); |
| 1657 | } |
| 1658 | break; |
| 1659 | } |
| 1660 | |
| 1661 | if (ret == 2 && strcmp(name, field) == 0) { |
| 1662 | /* |
| 1663 | * This number is displayed in kilo-bytes. Return the |
| 1664 | * number of bytes. |
| 1665 | */ |
| 1666 | *value = ((size_t) value_kb) * 1024; |
| 1667 | ret = 0; |
| 1668 | goto found; |
| 1669 | } |
| 1670 | } |
| 1671 | /* Reached the end of the file without finding the right field. */ |
| 1672 | ret = -1; |
| 1673 | |
| 1674 | found: |
| 1675 | fclose(proc_meminfo); |
| 1676 | fopen_error: |
| 1677 | return ret; |
| 1678 | } |
| 1679 | |
| 1680 | /* |
| 1681 | * Returns an estimate of the number of bytes of memory available based on the |
| 1682 | * the information in `/proc/meminfo`. The number returned by this function is |
| 1683 | * a best guess. |
| 1684 | */ |
| 1685 | LTTNG_HIDDEN |
| 1686 | int utils_get_memory_available(size_t *value) |
| 1687 | { |
| 1688 | return read_proc_meminfo_field(PROC_MEMINFO_MEMAVAILABLE_LINE, value); |
| 1689 | } |
| 1690 | |
| 1691 | /* |
| 1692 | * Returns the total size of the memory on the system in bytes based on the |
| 1693 | * the information in `/proc/meminfo`. |
| 1694 | */ |
| 1695 | LTTNG_HIDDEN |
| 1696 | int utils_get_memory_total(size_t *value) |
| 1697 | { |
| 1698 | return read_proc_meminfo_field(PROC_MEMINFO_MEMTOTAL_LINE, value); |
| 1699 | } |