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