Tests: Move script synchronization functions to utils library
[lttng-tools.git] / tests / utils / utils.c
1 /*
2 * Copyright (C) - 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as published by the
6 * Free Software Foundation; version 2.1 of the License.
7 *
8 * This library 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 Lesser General Public License
11 * for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18 #include <assert.h>
19 #include <common/compat/time.h>
20 #include <common/time.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <poll.h>
24 #include <stdbool.h>
25 #include <stdint.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31
32 #include "utils.h"
33
34 static inline
35 int64_t elapsed_time_ns(struct timespec *t1, struct timespec *t2)
36 {
37 struct timespec delta;
38
39 assert(t1 && t2);
40 delta.tv_sec = t2->tv_sec - t1->tv_sec;
41 delta.tv_nsec = t2->tv_nsec - t1->tv_nsec;
42 return ((int64_t) NSEC_PER_SEC * (int64_t) delta.tv_sec) +
43 (int64_t) delta.tv_nsec;
44 }
45
46 int usleep_safe(useconds_t usec)
47 {
48 int ret = 0;
49 struct timespec t1, t2;
50 int64_t time_remaining_ns = (int64_t) usec * (int64_t) NSEC_PER_USEC;
51
52 ret = lttng_clock_gettime(CLOCK_MONOTONIC, &t1);
53 if (ret) {
54 ret = -1;
55 perror("clock_gettime");
56 goto end;
57 }
58
59 while (time_remaining_ns > 0) {
60 ret = usleep(time_remaining_ns / (int64_t) NSEC_PER_USEC);
61 if (ret && errno != EINTR) {
62 perror("usleep");
63 goto end;
64 }
65
66 ret = lttng_clock_gettime(CLOCK_MONOTONIC, &t2);
67 if (ret) {
68 perror("clock_gettime");
69 goto end;
70 }
71
72 time_remaining_ns -= elapsed_time_ns(&t1, &t2);
73 }
74 end:
75 return ret;
76 }
77
78 int create_file(const char *path)
79 {
80 int ret;
81
82 if (!path) {
83 return -1;
84 }
85
86 ret = creat(path, S_IRWXU);
87 if (ret < 0) {
88 perror("creat");
89 return -1;
90 }
91
92 ret = close(ret);
93 if (ret < 0) {
94 perror("close");
95 return -1;
96 }
97
98 return 0;
99 }
100
101 int wait_on_file(const char *path)
102 {
103 int ret;
104 struct stat buf;
105
106 if (!path) {
107 return -1;
108 }
109
110 for (;;) {
111 ret = stat(path, &buf);
112 if (ret == -1 && errno == ENOENT) {
113 ret = poll(NULL, 0, 10); /* 10 ms delay */
114 /* Should return 0 everytime */
115 if (ret) {
116 if (ret < 0) {
117 perror("perror");
118 } else {
119 fprintf(stderr,
120 "poll return value is larger than zero\n");
121 }
122 return -1;
123 }
124 continue; /* retry */
125 }
126 if (ret) {
127 perror("stat");
128 return -1;
129 }
130 break; /* found */
131 }
132
133 return 0;
134 }
This page took 0.035026 seconds and 6 git commands to generate.