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