Commit | Line | Data |
---|---|---|
7e0cc23b | 1 | /* |
9d16b343 | 2 | * Copyright (C) 2012 David Goulet <dgoulet@efficios.com> |
7e0cc23b | 3 | * |
9d16b343 | 4 | * SPDX-License-Identifier: LGPL-2.1-only |
7e0cc23b | 5 | * |
7e0cc23b CB |
6 | */ |
7 | ||
70dac0d7 | 8 | #define _LGPL_SOURCE |
28ab034a JG |
9 | #include "signal-helper.hpp" |
10 | #include "utils.h" | |
11 | ||
7e0cc23b | 12 | #include <arpa/inet.h> |
28ab034a | 13 | #include <errno.h> |
209b934f | 14 | #include <fcntl.h> |
28ab034a JG |
15 | #include <getopt.h> |
16 | #include <poll.h> | |
17 | #include <signal.h> | |
7e0cc23b | 18 | #include <stdarg.h> |
28ab034a | 19 | #include <stdbool.h> |
7e0cc23b CB |
20 | #include <stdio.h> |
21 | #include <stdlib.h> | |
22 | #include <string.h> | |
23 | #include <sys/mman.h> | |
24 | #include <sys/stat.h> | |
25 | #include <sys/types.h> | |
26 | #include <unistd.h> | |
27 | ||
28 | #define TRACEPOINT_DEFINE | |
29 | #include "tp.h" | |
30 | ||
28ab034a | 31 | static struct option long_options[] = { |
6c4a91d6 | 32 | /* These options set a flag. */ |
cd9adb8b JG |
33 | { "iter", required_argument, nullptr, 'i' }, |
34 | { "wait", required_argument, nullptr, 'w' }, | |
ef07b7ae JG |
35 | { "sync-application-in-main-touch", required_argument, nullptr, 'a' }, |
36 | { "sync-before-first-event", required_argument, nullptr, 'b' }, | |
37 | { "sync-after-first-event", required_argument, nullptr, 'c' }, | |
38 | { "sync-before-last-event", required_argument, nullptr, 'd' }, | |
39 | { "sync-before-last-event-touch", required_argument, nullptr, 'e' }, | |
40 | { "sync-before-exit", required_argument, nullptr, 'f' }, | |
41 | { "sync-before-exit-touch", required_argument, nullptr, 'g' }, | |
42 | { "emit-end-event", no_argument, nullptr, 'h' }, | |
cd9adb8b | 43 | { nullptr, 0, nullptr, 0 } |
6c4a91d6 MD |
44 | }; |
45 | ||
7e0cc23b CB |
46 | int main(int argc, char **argv) |
47 | { | |
0fc2834c | 48 | unsigned int i, netint; |
6c4a91d6 MD |
49 | int option_index; |
50 | int option; | |
7e0cc23b CB |
51 | long values[] = { 1, 2, 3 }; |
52 | char text[10] = "test"; | |
6f9f5cd0 | 53 | char escape[10] = "\\*"; |
7e0cc23b CB |
54 | double dbl = 2.0; |
55 | float flt = 2222.0; | |
6f9f5cd0 | 56 | uint32_t net_values[] = { 1, 2, 3 }; |
b2047add | 57 | int nr_iter = 100, ret = 0, first_event_file_created = 0; |
7e0cc23b | 58 | useconds_t nr_usec = 0; |
ef07b7ae JG |
59 | char *application_in_main_file_path = nullptr; |
60 | char *before_first_event_file_path = nullptr; | |
cd9adb8b JG |
61 | char *after_first_event_file_path = nullptr; |
62 | char *before_last_event_file_path = nullptr; | |
4e88fe0a JR |
63 | /* |
64 | * Touch a file to indicate that all events except one were | |
65 | * generated. | |
66 | */ | |
cd9adb8b | 67 | char *before_last_event_file_path_touch = nullptr; |
4e88fe0a | 68 | /* Touch file when we are exiting */ |
cd9adb8b | 69 | char *before_exit_file_path_touch = nullptr; |
4e88fe0a | 70 | /* Wait on file before exiting */ |
cd9adb8b | 71 | char *before_exit_file_path = nullptr; |
6ffce1f5 JR |
72 | /* Emit an end event */ |
73 | bool emit_end_event = false; | |
7e0cc23b | 74 | |
6f9f5cd0 JR |
75 | for (i = 0; i < 3; i++) { |
76 | net_values[i] = htonl(net_values[i]); | |
77 | } | |
78 | ||
ef07b7ae | 79 | while ((option = getopt_long(argc, argv, "i:w:a:b:c:d:e:f:g:h", long_options, &option_index)) != |
28ab034a | 80 | -1) { |
6c4a91d6 MD |
81 | switch (option) { |
82 | case 'a': | |
ef07b7ae | 83 | application_in_main_file_path = strdup(optarg); |
6c4a91d6 MD |
84 | break; |
85 | case 'b': | |
ef07b7ae | 86 | before_first_event_file_path = strdup(optarg); |
6c4a91d6 | 87 | break; |
4e88fe0a | 88 | case 'c': |
ef07b7ae | 89 | after_first_event_file_path = strdup(optarg); |
4e88fe0a JR |
90 | break; |
91 | case 'd': | |
ef07b7ae | 92 | before_last_event_file_path = strdup(optarg); |
4e88fe0a JR |
93 | break; |
94 | case 'e': | |
ef07b7ae | 95 | before_last_event_file_path_touch = strdup(optarg); |
4e88fe0a | 96 | break; |
6ffce1f5 | 97 | case 'f': |
ef07b7ae JG |
98 | before_exit_file_path = strdup(optarg); |
99 | break; | |
100 | case 'g': | |
101 | before_exit_file_path_touch = strdup(optarg); | |
102 | break; | |
103 | case 'h': | |
6ffce1f5 JR |
104 | emit_end_event = true; |
105 | break; | |
6c4a91d6 MD |
106 | case 'i': |
107 | nr_iter = atoi(optarg); | |
108 | break; | |
109 | case 'w': | |
110 | nr_usec = atoi(optarg); | |
111 | break; | |
112 | case '?': | |
113 | /* getopt_long already printed an error message. */ | |
6c4a91d6 MD |
114 | default: |
115 | ret = -1; | |
116 | goto end; | |
117 | } | |
7e0cc23b CB |
118 | } |
119 | ||
6c4a91d6 MD |
120 | if (optind != argc) { |
121 | fprintf(stderr, "Error: takes long options only.\n"); | |
56d48389 MD |
122 | |
123 | /* | |
124 | * Aborting the test program for now because callers typically don't check | |
125 | * the test program return value, and the transition from positional | |
126 | * arguments to getopt causes hangs when caller scripts are not updated. | |
127 | * An abort is easier to diagnose and fix. This is a temporary solution: | |
128 | * we should eventually ensure that all scripts test and report the test | |
129 | * app return values. | |
130 | */ | |
131 | abort(); | |
132 | ||
6c4a91d6 MD |
133 | ret = -1; |
134 | goto end; | |
7e0cc23b CB |
135 | } |
136 | ||
6c4a91d6 MD |
137 | if (set_signal_handler()) { |
138 | ret = -1; | |
139 | goto end; | |
209b934f DG |
140 | } |
141 | ||
ef07b7ae JG |
142 | if (application_in_main_file_path) { |
143 | create_file(application_in_main_file_path); | |
144 | } | |
145 | ||
146 | if (before_first_event_file_path) { | |
147 | wait_on_file(before_first_event_file_path); | |
148 | } | |
149 | ||
0fc2834c | 150 | for (i = 0; nr_iter < 0 || i < nr_iter; i++) { |
5fcaccbc | 151 | if (nr_iter >= 0 && i == nr_iter - 1) { |
4e88fe0a JR |
152 | if (before_last_event_file_path_touch) { |
153 | ret = create_file(before_last_event_file_path_touch); | |
154 | if (ret != 0) { | |
155 | goto end; | |
156 | } | |
157 | } | |
158 | ||
5fcaccbc MD |
159 | /* |
160 | * Wait on synchronization before writing last | |
161 | * event. | |
162 | */ | |
b2047add FD |
163 | if (before_last_event_file_path) { |
164 | ret = wait_on_file(before_last_event_file_path); | |
165 | if (ret != 0) { | |
166 | goto end; | |
167 | } | |
168 | } | |
5fcaccbc | 169 | } |
7e0cc23b | 170 | netint = htonl(i); |
28ab034a JG |
171 | tracepoint(tp, |
172 | tptest, | |
173 | i, | |
174 | netint, | |
175 | values, | |
176 | text, | |
177 | strlen(text), | |
178 | escape, | |
179 | net_values, | |
180 | dbl, | |
181 | flt); | |
209b934f DG |
182 | |
183 | /* | |
5fcaccbc MD |
184 | * First loop we create the file if asked to indicate |
185 | * that at least one tracepoint has been hit. | |
209b934f | 186 | */ |
b2047add FD |
187 | if (after_first_event_file_path && first_event_file_created == 0) { |
188 | ret = create_file(after_first_event_file_path); | |
189 | ||
190 | if (ret != 0) { | |
191 | goto end; | |
192 | } else { | |
193 | first_event_file_created = 1; | |
194 | } | |
195 | } | |
196 | ||
b734f5f7 | 197 | if (nr_usec) { |
28ab034a | 198 | if (usleep_safe(nr_usec)) { |
ae941114 JG |
199 | ret = -1; |
200 | goto end; | |
201 | } | |
b734f5f7 | 202 | } |
95983a02 JG |
203 | if (should_quit) { |
204 | break; | |
205 | } | |
7e0cc23b CB |
206 | } |
207 | ||
6ffce1f5 JR |
208 | if (emit_end_event) { |
209 | tracepoint(tp, end); | |
210 | } | |
211 | ||
4e88fe0a JR |
212 | if (before_exit_file_path_touch) { |
213 | ret = create_file(before_exit_file_path_touch); | |
214 | if (ret != 0) { | |
215 | goto end; | |
216 | } | |
217 | } | |
218 | if (before_exit_file_path) { | |
219 | ret = wait_on_file(before_exit_file_path); | |
220 | if (ret != 0) { | |
221 | goto end; | |
222 | } | |
223 | } | |
ae941114 | 224 | end: |
ef07b7ae JG |
225 | free(application_in_main_file_path); |
226 | free(before_first_event_file_path); | |
6c4a91d6 MD |
227 | free(after_first_event_file_path); |
228 | free(before_last_event_file_path); | |
4e88fe0a JR |
229 | free(before_last_event_file_path_touch); |
230 | free(before_exit_file_path); | |
231 | free(before_exit_file_path_touch); | |
ae941114 | 232 | exit(!ret ? EXIT_SUCCESS : EXIT_FAILURE); |
7e0cc23b | 233 | } |