fix: relayd: unaligned access in trace_chunk_registry_ht_key_hash
[lttng-tools.git] / tests / utils / utils.cpp
1 /*
2 * Copyright (C) 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #include "utils.h"
9
10 #include <common/compat/errno.hpp>
11 #include <common/compat/time.hpp>
12 #include <common/macros.hpp>
13 #include <common/time.hpp>
14
15 #include <fcntl.h>
16 #include <poll.h>
17 #include <stdbool.h>
18 #include <stdint.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24
25 int64_t elapsed_time_ns(struct timespec *t1, struct timespec *t2)
26 {
27 struct timespec delta;
28
29 LTTNG_ASSERT(t1 && t2);
30 delta.tv_sec = t2->tv_sec - t1->tv_sec;
31 delta.tv_nsec = t2->tv_nsec - t1->tv_nsec;
32 return ((int64_t) NSEC_PER_SEC * (int64_t) delta.tv_sec) + (int64_t) delta.tv_nsec;
33 }
34
35 int usleep_safe(useconds_t usec)
36 {
37 int ret = 0;
38 struct timespec t1, t2;
39 int64_t time_remaining_ns = (int64_t) usec * (int64_t) NSEC_PER_USEC;
40
41 ret = lttng_clock_gettime(CLOCK_MONOTONIC, &t1);
42 if (ret) {
43 ret = -1;
44 perror("clock_gettime");
45 goto end;
46 }
47
48 while (time_remaining_ns > 0) {
49 ret = usleep(time_remaining_ns / (int64_t) NSEC_PER_USEC);
50 if (ret && errno != EINTR) {
51 perror("usleep");
52 goto end;
53 }
54
55 ret = lttng_clock_gettime(CLOCK_MONOTONIC, &t2);
56 if (ret) {
57 perror("clock_gettime");
58 goto end;
59 }
60
61 time_remaining_ns -= elapsed_time_ns(&t1, &t2);
62 }
63 end:
64 return ret;
65 }
66
67 int create_file(const char *path)
68 {
69 int ret;
70
71 if (!path) {
72 return -1;
73 }
74
75 ret = creat(path, S_IRWXU);
76 if (ret < 0) {
77 perror("creat");
78 return -1;
79 }
80
81 ret = close(ret);
82 if (ret < 0) {
83 perror("close");
84 return -1;
85 }
86
87 return 0;
88 }
89
90 int wait_on_file(const char *path)
91 {
92 int ret;
93 struct stat buf;
94
95 if (!path) {
96 return -1;
97 }
98
99 for (;;) {
100 ret = stat(path, &buf);
101 if (ret == -1 && errno == ENOENT) {
102 ret = poll(nullptr, 0, 10); /* 10 ms delay */
103 /* Should return 0 everytime */
104 if (ret) {
105 if (ret < 0) {
106 perror("perror");
107 } else {
108 fprintf(stderr, "poll return value is larger than zero\n");
109 }
110 return -1;
111 }
112 continue; /* retry */
113 }
114 if (ret) {
115 perror("stat");
116 return -1;
117 }
118 break; /* found */
119 }
120
121 return 0;
122 }
This page took 0.030485 seconds and 4 git commands to generate.