Introduce a new utils_partial_realpath function
[lttng-tools.git] / src / common / utils.c
CommitLineData
81b86775
DG
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
35f90c40 19#include <assert.h>
81b86775
DG
20#include <ctype.h>
21#include <fcntl.h>
22#include <limits.h>
23#include <stdlib.h>
24#include <string.h>
2d851108 25#include <sys/stat.h>
0c7bcad5 26#include <sys/types.h>
2d851108 27#include <unistd.h>
fe4477ee 28#include <inttypes.h>
70d0b120 29#include <regex.h>
6c71277b 30#include <grp.h>
81b86775
DG
31
32#include <common/common.h>
fe4477ee 33#include <common/runas.h>
81b86775
DG
34
35#include "utils.h"
feb0f3e5 36#include "defaults.h"
81b86775 37
5154230f
RB
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 */
49LTTNG_HIDDEN
50char *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
161error:
162 free(resolved_path);
163 return NULL;
164}
165
a2e78ff0
RB
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 */
171LTTNG_HIDDEN
172char *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
228error:
229 free(absolute_path);
230 return NULL;
231}
232
233
81b86775
DG
234/*
235 * Return the realpath(3) of the path even if the last directory token does not
236 * exist. For example, with /tmp/test1/test2, if test2/ does not exist but the
237 * /tmp/test1 does, the real path is returned. In normal time, realpath(3)
238 * fails if the end point directory does not exist.
239 */
90e535ef 240LTTNG_HIDDEN
81b86775
DG
241char *utils_expand_path(const char *path)
242{
116f95d9 243 const char *end_path = NULL;
81b86775
DG
244 char *next, *cut_path = NULL, *expanded_path = NULL;
245
246 /* Safety net */
247 if (path == NULL) {
248 goto error;
249 }
250
116f95d9 251 /* Allocate memory for the expanded path */
81b86775
DG
252 expanded_path = zmalloc(PATH_MAX);
253 if (expanded_path == NULL) {
254 PERROR("zmalloc expand path");
255 goto error;
256 }
257
116f95d9
RB
258 /* If given path is already absolute */
259 if (*path == '/') {
260 strncpy(expanded_path, path, PATH_MAX);
261 /* Else, we have some work to do */
262 } else {
263 /* Pointer to the last char of the path */
264 const char *last_char = path + strlen(path) - 1;
265
266 end_path = path;
267
268 /* Split part that will be resolved by realpath (relative path from
269 * current directory using ./ or ../ only) and part that could not
270 * (directory names)
271 */
272 while ((next = strpbrk(end_path, "/")) && (next != last_char)) {
273 end_path = next + 1;
274 if (strncmp(end_path, "./", 2) != 0 &&
275 strncmp(end_path, "../", 3) != 0) {
276 break;
277 }
278 }
279
280 /* If this is the end of the string, and we still can resolve it */
281 if (strncmp(end_path, "..\0", 3) == 0 ||
282 strncmp(end_path, ".\0", 2) == 0) {
283 end_path += strlen(end_path);
284 }
285
286 /* If the end part is the whole path, we are in the current dir */
287 if (end_path == path) {
288 cut_path = strdup(".");
289 /* Else, cut the resolvable part from original path */
290 } else {
291 cut_path = strndup(path, end_path - path);
292 }
293
294 /* Resolve the canonical path of the first part of the path */
295 expanded_path = realpath((char *)cut_path, expanded_path);
296 if (expanded_path == NULL) {
297 switch (errno) {
298 case ENOENT:
299 ERR("%s: No such file or directory", cut_path);
300 break;
301 default:
302 PERROR("realpath utils expand path");
303 break;
304 }
305 goto error;
306 }
307
308 /* Add end part to expanded path if not empty */
309 if (*end_path != 0) {
310 strncat(expanded_path, "/", PATH_MAX - strlen(expanded_path) - 1);
311 strncat(expanded_path, end_path,
312 PATH_MAX - strlen(expanded_path) - 1);
81b86775 313 }
81b86775
DG
314 }
315
116f95d9
RB
316 /* Resolve the internal './' and '../' strings */
317 next = utils_resolve_relative(expanded_path);
318 if (next == NULL) {
319 goto error;
320 }
81b86775 321
116f95d9 322 free(expanded_path);
81b86775 323 free(cut_path);
116f95d9 324 return next;
81b86775
DG
325
326error:
327 free(expanded_path);
328 free(cut_path);
329 return NULL;
330}
331
332/*
333 * Create a pipe in dst.
334 */
90e535ef 335LTTNG_HIDDEN
81b86775
DG
336int utils_create_pipe(int *dst)
337{
338 int ret;
339
340 if (dst == NULL) {
341 return -1;
342 }
343
344 ret = pipe(dst);
345 if (ret < 0) {
346 PERROR("create pipe");
347 }
348
349 return ret;
350}
351
352/*
353 * Create pipe and set CLOEXEC flag to both fd.
354 *
355 * Make sure the pipe opened by this function are closed at some point. Use
356 * utils_close_pipe().
357 */
90e535ef 358LTTNG_HIDDEN
81b86775
DG
359int utils_create_pipe_cloexec(int *dst)
360{
361 int ret, i;
362
363 if (dst == NULL) {
364 return -1;
365 }
366
367 ret = utils_create_pipe(dst);
368 if (ret < 0) {
369 goto error;
370 }
371
372 for (i = 0; i < 2; i++) {
373 ret = fcntl(dst[i], F_SETFD, FD_CLOEXEC);
374 if (ret < 0) {
375 PERROR("fcntl pipe cloexec");
376 goto error;
377 }
378 }
379
380error:
381 return ret;
382}
383
094f381c
MD
384/*
385 * Create pipe and set fd flags to FD_CLOEXEC and O_NONBLOCK.
386 *
387 * Make sure the pipe opened by this function are closed at some point. Use
388 * utils_close_pipe(). Using pipe() and fcntl rather than pipe2() to
389 * support OSes other than Linux 2.6.23+.
390 */
391LTTNG_HIDDEN
392int utils_create_pipe_cloexec_nonblock(int *dst)
393{
394 int ret, i;
395
396 if (dst == NULL) {
397 return -1;
398 }
399
400 ret = utils_create_pipe(dst);
401 if (ret < 0) {
402 goto error;
403 }
404
405 for (i = 0; i < 2; i++) {
406 ret = fcntl(dst[i], F_SETFD, FD_CLOEXEC);
407 if (ret < 0) {
408 PERROR("fcntl pipe cloexec");
409 goto error;
410 }
411 /*
412 * Note: we override any flag that could have been
413 * previously set on the fd.
414 */
415 ret = fcntl(dst[i], F_SETFL, O_NONBLOCK);
416 if (ret < 0) {
417 PERROR("fcntl pipe nonblock");
418 goto error;
419 }
420 }
421
422error:
423 return ret;
424}
425
81b86775
DG
426/*
427 * Close both read and write side of the pipe.
428 */
90e535ef 429LTTNG_HIDDEN
81b86775
DG
430void utils_close_pipe(int *src)
431{
432 int i, ret;
433
434 if (src == NULL) {
435 return;
436 }
437
438 for (i = 0; i < 2; i++) {
439 /* Safety check */
440 if (src[i] < 0) {
441 continue;
442 }
443
444 ret = close(src[i]);
445 if (ret) {
446 PERROR("close pipe");
447 }
448 }
449}
a4b92340
DG
450
451/*
452 * Create a new string using two strings range.
453 */
90e535ef 454LTTNG_HIDDEN
a4b92340
DG
455char *utils_strdupdelim(const char *begin, const char *end)
456{
457 char *str;
458
459 str = zmalloc(end - begin + 1);
460 if (str == NULL) {
461 PERROR("zmalloc strdupdelim");
462 goto error;
463 }
464
465 memcpy(str, begin, end - begin);
466 str[end - begin] = '\0';
467
468error:
469 return str;
470}
b662582b
DG
471
472/*
473 * Set CLOEXEC flag to the give file descriptor.
474 */
90e535ef 475LTTNG_HIDDEN
b662582b
DG
476int utils_set_fd_cloexec(int fd)
477{
478 int ret;
479
480 if (fd < 0) {
481 ret = -EINVAL;
482 goto end;
483 }
484
485 ret = fcntl(fd, F_SETFD, FD_CLOEXEC);
486 if (ret < 0) {
487 PERROR("fcntl cloexec");
488 ret = -errno;
489 }
490
491end:
492 return ret;
493}
35f90c40
DG
494
495/*
496 * Create pid file to the given path and filename.
497 */
90e535ef 498LTTNG_HIDDEN
35f90c40
DG
499int utils_create_pid_file(pid_t pid, const char *filepath)
500{
501 int ret;
502 FILE *fp;
503
504 assert(filepath);
505
506 fp = fopen(filepath, "w");
507 if (fp == NULL) {
508 PERROR("open pid file %s", filepath);
509 ret = -1;
510 goto error;
511 }
512
513 ret = fprintf(fp, "%d\n", pid);
514 if (ret < 0) {
515 PERROR("fprintf pid file");
516 }
517
518 fclose(fp);
519 DBG("Pid %d written in file %s", pid, filepath);
520error:
521 return ret;
522}
2d851108
DG
523
524/*
525 * Recursively create directory using the given path and mode.
526 *
527 * On success, return 0 else a negative error code.
528 */
90e535ef 529LTTNG_HIDDEN
2d851108
DG
530int utils_mkdir_recursive(const char *path, mode_t mode)
531{
532 char *p, tmp[PATH_MAX];
2d851108
DG
533 size_t len;
534 int ret;
535
536 assert(path);
537
538 ret = snprintf(tmp, sizeof(tmp), "%s", path);
539 if (ret < 0) {
540 PERROR("snprintf mkdir");
541 goto error;
542 }
543
544 len = ret;
545 if (tmp[len - 1] == '/') {
546 tmp[len - 1] = 0;
547 }
548
549 for (p = tmp + 1; *p; p++) {
550 if (*p == '/') {
551 *p = 0;
552 if (tmp[strlen(tmp) - 1] == '.' &&
553 tmp[strlen(tmp) - 2] == '.' &&
554 tmp[strlen(tmp) - 3] == '/') {
555 ERR("Using '/../' is not permitted in the trace path (%s)",
556 tmp);
557 ret = -1;
558 goto error;
559 }
0c7bcad5 560 ret = mkdir(tmp, mode);
2d851108 561 if (ret < 0) {
0c7bcad5
MD
562 if (errno != EEXIST) {
563 PERROR("mkdir recursive");
564 ret = -errno;
565 goto error;
2d851108
DG
566 }
567 }
568 *p = '/';
569 }
570 }
571
572 ret = mkdir(tmp, mode);
573 if (ret < 0) {
574 if (errno != EEXIST) {
575 PERROR("mkdir recursive last piece");
576 ret = -errno;
577 } else {
578 ret = 0;
579 }
580 }
581
582error:
583 return ret;
584}
fe4477ee
JD
585
586/*
587 * Create the stream tracefile on disk.
588 *
589 * Return 0 on success or else a negative value.
590 */
bc182241 591LTTNG_HIDDEN
07b86b52 592int utils_create_stream_file(const char *path_name, char *file_name, uint64_t size,
309167d2 593 uint64_t count, int uid, int gid, char *suffix)
fe4477ee 594{
be96a7d1 595 int ret, out_fd, flags, mode;
309167d2
JD
596 char full_path[PATH_MAX], *path_name_suffix = NULL, *path;
597 char *extra = NULL;
fe4477ee
JD
598
599 assert(path_name);
600 assert(file_name);
601
602 ret = snprintf(full_path, sizeof(full_path), "%s/%s",
603 path_name, file_name);
604 if (ret < 0) {
605 PERROR("snprintf create output file");
606 goto error;
607 }
608
309167d2
JD
609 /* Setup extra string if suffix or/and a count is needed. */
610 if (size > 0 && suffix) {
611 ret = asprintf(&extra, "_%" PRIu64 "%s", count, suffix);
612 } else if (size > 0) {
613 ret = asprintf(&extra, "_%" PRIu64, count);
614 } else if (suffix) {
615 ret = asprintf(&extra, "%s", suffix);
616 }
617 if (ret < 0) {
618 PERROR("Allocating extra string to name");
619 goto error;
620 }
621
fe4477ee
JD
622 /*
623 * If we split the trace in multiple files, we have to add the count at the
624 * end of the tracefile name
625 */
309167d2
JD
626 if (extra) {
627 ret = asprintf(&path_name_suffix, "%s%s", full_path, extra);
fe4477ee 628 if (ret < 0) {
309167d2
JD
629 PERROR("Allocating path name with extra string");
630 goto error_free_suffix;
fe4477ee 631 }
309167d2 632 path = path_name_suffix;
fe4477ee
JD
633 } else {
634 path = full_path;
635 }
636
be96a7d1 637 flags = O_WRONLY | O_CREAT | O_TRUNC;
0f907de1 638 /* Open with 660 mode */
be96a7d1
DG
639 mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
640
641 if (uid < 0 || gid < 0) {
642 out_fd = open(path, flags, mode);
643 } else {
644 out_fd = run_as_open(path, flags, mode, uid, gid);
645 }
fe4477ee
JD
646 if (out_fd < 0) {
647 PERROR("open stream path %s", path);
648 goto error_open;
649 }
650 ret = out_fd;
651
652error_open:
309167d2
JD
653 free(path_name_suffix);
654error_free_suffix:
655 free(extra);
fe4477ee
JD
656error:
657 return ret;
658}
659
660/*
661 * Change the output tracefile according to the given size and count The
662 * new_count pointer is set during this operation.
663 *
664 * From the consumer, the stream lock MUST be held before calling this function
665 * because we are modifying the stream status.
666 *
667 * Return 0 on success or else a negative value.
668 */
bc182241 669LTTNG_HIDDEN
fe4477ee 670int utils_rotate_stream_file(char *path_name, char *file_name, uint64_t size,
309167d2
JD
671 uint64_t count, int uid, int gid, int out_fd, uint64_t *new_count,
672 int *stream_fd)
fe4477ee
JD
673{
674 int ret;
675
309167d2
JD
676 assert(new_count);
677 assert(stream_fd);
678
fe4477ee
JD
679 ret = close(out_fd);
680 if (ret < 0) {
681 PERROR("Closing tracefile");
682 goto error;
683 }
684
685 if (count > 0) {
686 *new_count = (*new_count + 1) % count;
687 } else {
688 (*new_count)++;
689 }
690
309167d2
JD
691 ret = utils_create_stream_file(path_name, file_name, size, *new_count,
692 uid, gid, 0);
693 if (ret < 0) {
694 goto error;
695 }
696 *stream_fd = ret;
697
698 /* Success. */
699 ret = 0;
700
fe4477ee
JD
701error:
702 return ret;
703}
70d0b120
SM
704
705/**
706 * Prints the error message corresponding to a regex error code.
707 *
708 * @param errcode The error code.
709 * @param regex The regex object that produced the error code.
710 */
711static void regex_print_error(int errcode, regex_t *regex)
712{
713 /* Get length of error message and allocate accordingly */
714 size_t length;
715 char *buffer;
716
717 assert(regex != NULL);
718
719 length = regerror(errcode, regex, NULL, 0);
720 if (length == 0) {
721 ERR("regerror returned a length of 0");
722 return;
723 }
724
725 buffer = zmalloc(length);
726 if (!buffer) {
727 ERR("regex_print_error: zmalloc failed");
728 return;
729 }
730
731 /* Get and print error message */
732 regerror(errcode, regex, buffer, length);
733 ERR("regex error: %s\n", buffer);
734 free(buffer);
735
736}
737
738/**
739 * Parse a string that represents a size in human readable format. It
740 * supports decimal integers suffixed by 'k', 'M' or 'G'.
741 *
742 * The suffix multiply the integer by:
743 * 'k': 1024
744 * 'M': 1024^2
745 * 'G': 1024^3
746 *
747 * @param str The string to parse.
748 * @param size Pointer to a size_t that will be filled with the
cfa9a5a2 749 * resulting size.
70d0b120
SM
750 *
751 * @return 0 on success, -1 on failure.
752 */
00a52467 753LTTNG_HIDDEN
70d0b120
SM
754int utils_parse_size_suffix(char *str, uint64_t *size)
755{
756 regex_t regex;
757 int ret;
758 const int nmatch = 3;
759 regmatch_t suffix_match, matches[nmatch];
760 unsigned long long base_size;
761 long shift = 0;
762
763 if (!str) {
764 return 0;
765 }
766
767 /* Compile regex */
768 ret = regcomp(&regex, "^\\(0x\\)\\{0,1\\}[0-9][0-9]*\\([kKMG]\\{0,1\\}\\)$", 0);
769 if (ret != 0) {
770 regex_print_error(ret, &regex);
771 ret = -1;
772 goto end;
773 }
774
775 /* Match regex */
776 ret = regexec(&regex, str, nmatch, matches, 0);
777 if (ret != 0) {
778 ret = -1;
779 goto free;
780 }
781
782 /* There is a match ! */
783 errno = 0;
784 base_size = strtoull(str, NULL, 0);
785 if (errno != 0) {
786 PERROR("strtoull");
787 ret = -1;
788 goto free;
789 }
790
791 /* Check if there is a suffix */
792 suffix_match = matches[2];
793 if (suffix_match.rm_eo - suffix_match.rm_so == 1) {
794 switch (*(str + suffix_match.rm_so)) {
795 case 'K':
796 case 'k':
797 shift = KIBI_LOG2;
798 break;
799 case 'M':
800 shift = MEBI_LOG2;
801 break;
802 case 'G':
803 shift = GIBI_LOG2;
804 break;
805 default:
806 ERR("parse_human_size: invalid suffix");
807 ret = -1;
808 goto free;
809 }
810 }
811
812 *size = base_size << shift;
813
814 /* Check for overflow */
815 if ((*size >> shift) != base_size) {
816 ERR("parse_size_suffix: oops, overflow detected.");
817 ret = -1;
818 goto free;
819 }
820
821 ret = 0;
822
823free:
824 regfree(&regex);
825end:
826 return ret;
827}
cfa9a5a2
DG
828
829/*
830 * fls: returns the position of the most significant bit.
831 * Returns 0 if no bit is set, else returns the position of the most
832 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
833 */
834#if defined(__i386) || defined(__x86_64)
835static inline unsigned int fls_u32(uint32_t x)
836{
837 int r;
838
839 asm("bsrl %1,%0\n\t"
840 "jnz 1f\n\t"
841 "movl $-1,%0\n\t"
842 "1:\n\t"
843 : "=r" (r) : "rm" (x));
844 return r + 1;
845}
846#define HAS_FLS_U32
847#endif
848
849#ifndef HAS_FLS_U32
850static __attribute__((unused)) unsigned int fls_u32(uint32_t x)
851{
852 unsigned int r = 32;
853
854 if (!x) {
855 return 0;
856 }
857 if (!(x & 0xFFFF0000U)) {
858 x <<= 16;
859 r -= 16;
860 }
861 if (!(x & 0xFF000000U)) {
862 x <<= 8;
863 r -= 8;
864 }
865 if (!(x & 0xF0000000U)) {
866 x <<= 4;
867 r -= 4;
868 }
869 if (!(x & 0xC0000000U)) {
870 x <<= 2;
871 r -= 2;
872 }
873 if (!(x & 0x80000000U)) {
874 x <<= 1;
875 r -= 1;
876 }
877 return r;
878}
879#endif
880
881/*
882 * Return the minimum order for which x <= (1UL << order).
883 * Return -1 if x is 0.
884 */
885LTTNG_HIDDEN
886int utils_get_count_order_u32(uint32_t x)
887{
888 if (!x) {
889 return -1;
890 }
891
892 return fls_u32(x - 1);
893}
feb0f3e5
AM
894
895/**
896 * Obtain the value of LTTNG_HOME environment variable, if exists.
897 * Otherwise returns the value of HOME.
898 */
00a52467 899LTTNG_HIDDEN
feb0f3e5
AM
900char *utils_get_home_dir(void)
901{
902 char *val = NULL;
903 val = getenv(DEFAULT_LTTNG_HOME_ENV_VAR);
904 if (val != NULL) {
905 return val;
906 }
907 return getenv(DEFAULT_LTTNG_FALLBACK_HOME_ENV_VAR);
908}
26fe5938
DG
909
910/*
911 * With the given format, fill dst with the time of len maximum siz.
912 *
913 * Return amount of bytes set in the buffer or else 0 on error.
914 */
915LTTNG_HIDDEN
916size_t utils_get_current_time_str(const char *format, char *dst, size_t len)
917{
918 size_t ret;
919 time_t rawtime;
920 struct tm *timeinfo;
921
922 assert(format);
923 assert(dst);
924
925 /* Get date and time for session path */
926 time(&rawtime);
927 timeinfo = localtime(&rawtime);
928 ret = strftime(dst, len, format, timeinfo);
929 if (ret == 0) {
930 ERR("Unable to strftime with format %s at dst %p of len %lu", format,
931 dst, len);
932 }
933
934 return ret;
935}
6c71277b
MD
936
937/*
938 * Return the group ID matching name, else 0 if it cannot be found.
939 */
940LTTNG_HIDDEN
941gid_t utils_get_group_id(const char *name)
942{
943 struct group *grp;
944
945 grp = getgrnam(name);
946 if (!grp) {
947 static volatile int warn_once;
948
949 if (!warn_once) {
950 WARN("No tracing group detected");
951 warn_once = 1;
952 }
953 return 0;
954 }
955 return grp->gr_gid;
956}
This page took 0.070847 seconds and 5 git commands to generate.