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