Commit | Line | Data |
---|---|---|
7e0cc23b CB |
1 | /* |
2 | * Copyright (C) - 2012 David Goulet <dgoulet@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 | ||
70dac0d7 | 18 | #define _LGPL_SOURCE |
e8526e3f | 19 | #include <getopt.h> |
209b934f | 20 | #include <assert.h> |
7e0cc23b | 21 | #include <arpa/inet.h> |
209b934f | 22 | #include <fcntl.h> |
7e0cc23b CB |
23 | #include <stdarg.h> |
24 | #include <stdio.h> | |
25 | #include <stdlib.h> | |
26 | #include <string.h> | |
27 | #include <sys/mman.h> | |
28 | #include <sys/stat.h> | |
29 | #include <sys/types.h> | |
30 | #include <unistd.h> | |
0fc2834c | 31 | #include <stdbool.h> |
5fcaccbc MD |
32 | #include <signal.h> |
33 | #include <poll.h> | |
34 | #include <errno.h> | |
ae941114 | 35 | #include "utils.h" |
95170e51 | 36 | #include "signal-helper.h" |
7e0cc23b CB |
37 | |
38 | #define TRACEPOINT_DEFINE | |
39 | #include "tp.h" | |
40 | ||
e8526e3f JR |
41 | static |
42 | int create_file(const char *path) | |
209b934f DG |
43 | { |
44 | int ret; | |
45 | ||
e8526e3f JR |
46 | if (!path) { |
47 | return -1; | |
5fcaccbc | 48 | } |
209b934f DG |
49 | |
50 | ret = creat(path, S_IRWXU); | |
51 | if (ret < 0) { | |
e8526e3f JR |
52 | perror("creat"); |
53 | return -1; | |
54 | } | |
55 | ||
56 | ret = close(ret); | |
57 | if (ret < 0) { | |
58 | perror("close"); | |
59 | return -1; | |
209b934f DG |
60 | } |
61 | ||
e8526e3f | 62 | return 0; |
5fcaccbc MD |
63 | } |
64 | ||
65 | static | |
e8526e3f | 66 | int wait_on_file(const char *path) |
5fcaccbc | 67 | { |
e8526e3f JR |
68 | int ret; |
69 | struct stat buf; | |
70 | ||
5fcaccbc | 71 | if (!path) { |
e8526e3f | 72 | return -1; |
5fcaccbc | 73 | } |
5fcaccbc | 74 | |
e8526e3f | 75 | for (;;) { |
5fcaccbc MD |
76 | ret = stat(path, &buf); |
77 | if (ret == -1 && errno == ENOENT) { | |
e8526e3f JR |
78 | ret = poll(NULL, 0, 10); /* 10 ms delay */ |
79 | /* Should return 0 everytime */ | |
80 | if (ret) { | |
81 | if (ret < 0) { | |
82 | perror("perror"); | |
83 | } else { | |
84 | fprintf(stderr, | |
85 | "poll return value is larger than zero\n"); | |
86 | } | |
87 | return -1; | |
88 | } | |
5fcaccbc MD |
89 | continue; /* retry */ |
90 | } | |
91 | if (ret) { | |
92 | perror("stat"); | |
e8526e3f | 93 | return -1; |
5fcaccbc MD |
94 | } |
95 | break; /* found */ | |
96 | } | |
e8526e3f JR |
97 | |
98 | return 0; | |
209b934f DG |
99 | } |
100 | ||
e8526e3f JR |
101 | static struct option long_options[] = |
102 | { | |
103 | /* These options set a flag. */ | |
104 | {"iter", required_argument, 0, 'i'}, | |
105 | {"wait", required_argument, 0, 'w'}, | |
106 | {"sync-after-first-event", required_argument, 0, 'a'}, | |
107 | {"sync-before-last-event", required_argument, 0, 'b'}, | |
91e72b36 | 108 | {"sync-before-last-event-touch", required_argument, 0, 'c'}, |
9a57f63e JR |
109 | {"sync-before-exit", required_argument, 0, 'd'}, |
110 | {"sync-before-exit-touch", required_argument, 0, 'e'}, | |
e8526e3f JR |
111 | {0, 0, 0, 0} |
112 | }; | |
113 | ||
7e0cc23b CB |
114 | int main(int argc, char **argv) |
115 | { | |
0fc2834c | 116 | unsigned int i, netint; |
e8526e3f JR |
117 | int option_index; |
118 | char option_char; | |
7e0cc23b CB |
119 | long values[] = { 1, 2, 3 }; |
120 | char text[10] = "test"; | |
121 | double dbl = 2.0; | |
122 | float flt = 2222.0; | |
e8526e3f | 123 | int nr_iter = 100, ret = 0, first_event_file_created = 0; |
7e0cc23b | 124 | useconds_t nr_usec = 0; |
5fcaccbc MD |
125 | char *after_first_event_file_path = NULL; |
126 | char *before_last_event_file_path = NULL; | |
91e72b36 JR |
127 | /* |
128 | * Touch a file to indicate that all events except one were | |
129 | * generated. | |
130 | */ | |
131 | char *before_last_event_file_path_touch = NULL; | |
9a57f63e JR |
132 | /* Touch file when we are exiting */ |
133 | char *before_exit_file_path_touch = NULL; | |
134 | /* Wait on file before exiting */ | |
135 | char *before_exit_file_path = NULL; | |
7e0cc23b | 136 | |
e8526e3f JR |
137 | while((option_char = getopt_long(argc, argv, "i:w:a:b:c:d:", long_options, &option_index)) != -1) { |
138 | switch (option_char) { | |
139 | case 'a': | |
140 | after_first_event_file_path = strdup(optarg); | |
141 | break; | |
142 | case 'b': | |
143 | before_last_event_file_path = strdup(optarg); | |
144 | break; | |
91e72b36 JR |
145 | case 'c': |
146 | before_last_event_file_path_touch = strdup(optarg); | |
147 | break; | |
9a57f63e JR |
148 | case 'd': |
149 | before_exit_file_path = strdup(optarg); | |
150 | break; | |
151 | case 'e': | |
152 | before_exit_file_path_touch = strdup(optarg); | |
153 | break; | |
e8526e3f JR |
154 | case 'i': |
155 | nr_iter = atoi(optarg); | |
156 | break; | |
157 | case 'w': | |
158 | nr_usec = atoi(optarg); | |
159 | break; | |
160 | case '?': | |
161 | /* getopt_long already printed an error message. */ | |
162 | break; | |
163 | default: | |
164 | ret = -1; | |
165 | goto end; | |
166 | } | |
7e0cc23b CB |
167 | } |
168 | ||
e8526e3f JR |
169 | if (optind != argc) { |
170 | fprintf(stderr, "Error: takes long options only."); | |
171 | ret = -1; | |
172 | goto end; | |
7e0cc23b CB |
173 | } |
174 | ||
5fcaccbc | 175 | |
e8526e3f JR |
176 | if (set_signal_handler()) { |
177 | ret = -1; | |
178 | goto end; | |
209b934f DG |
179 | } |
180 | ||
0fc2834c | 181 | for (i = 0; nr_iter < 0 || i < nr_iter; i++) { |
5fcaccbc MD |
182 | if (nr_iter >= 0 && i == nr_iter - 1) { |
183 | /* | |
184 | * Wait on synchronization before writing last | |
185 | * event. | |
186 | */ | |
91e72b36 JR |
187 | if (before_last_event_file_path_touch) { |
188 | ret = create_file(before_last_event_file_path_touch); | |
189 | if (ret != 0) { | |
190 | goto end; | |
191 | } | |
192 | } | |
e8526e3f JR |
193 | if (before_last_event_file_path) { |
194 | ret = wait_on_file(before_last_event_file_path); | |
195 | if (ret != 0) { | |
196 | goto end; | |
197 | } | |
198 | } | |
5fcaccbc | 199 | } |
7e0cc23b | 200 | netint = htonl(i); |
5fcaccbc MD |
201 | tracepoint(tp, tptest, i, netint, values, text, |
202 | strlen(text), dbl, flt); | |
209b934f DG |
203 | |
204 | /* | |
5fcaccbc MD |
205 | * First loop we create the file if asked to indicate |
206 | * that at least one tracepoint has been hit. | |
209b934f | 207 | */ |
e8526e3f JR |
208 | if (after_first_event_file_path && first_event_file_created == 0) { |
209 | ret = create_file(after_first_event_file_path); | |
210 | ||
211 | if (ret != 0) { | |
212 | goto end; | |
213 | } else { | |
214 | first_event_file_created = 1; | |
215 | } | |
216 | } | |
217 | ||
b734f5f7 | 218 | if (nr_usec) { |
ae941114 JG |
219 | if (usleep_safe(nr_usec)) { |
220 | ret = -1; | |
221 | goto end; | |
222 | } | |
b734f5f7 | 223 | } |
95170e51 JG |
224 | if (should_quit) { |
225 | break; | |
226 | } | |
7e0cc23b CB |
227 | } |
228 | ||
9a57f63e JR |
229 | if (before_exit_file_path_touch) { |
230 | ret = create_file(before_exit_file_path_touch); | |
231 | if (ret != 0) { | |
232 | goto end; | |
233 | } | |
234 | } | |
235 | if (before_exit_file_path) { | |
236 | ret = wait_on_file(before_exit_file_path); | |
237 | if (ret != 0) { | |
238 | goto end; | |
239 | } | |
240 | } | |
ae941114 | 241 | end: |
e8526e3f JR |
242 | free(after_first_event_file_path); |
243 | free(before_last_event_file_path); | |
91e72b36 | 244 | free(before_last_event_file_path_touch); |
9a57f63e JR |
245 | free(before_exit_file_path); |
246 | free(before_exit_file_path_touch); | |
ae941114 | 247 | exit(!ret ? EXIT_SUCCESS : EXIT_FAILURE); |
7e0cc23b | 248 | } |