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. */ |
28ab034a JG |
33 | { "iter", required_argument, 0, 'i' }, |
34 | { "wait", required_argument, 0, 'w' }, | |
35 | { "sync-after-first-event", required_argument, 0, 'a' }, | |
36 | { "sync-before-last-event", required_argument, 0, 'b' }, | |
37 | { "sync-before-last-event-touch", required_argument, 0, 'c' }, | |
38 | { "sync-before-exit", required_argument, 0, 'd' }, | |
39 | { "sync-before-exit-touch", required_argument, 0, 'e' }, | |
40 | { "emit-end-event", no_argument, 0, 'f' }, | |
41 | { 0, 0, 0, 0 } | |
6c4a91d6 MD |
42 | }; |
43 | ||
7e0cc23b CB |
44 | int main(int argc, char **argv) |
45 | { | |
0fc2834c | 46 | unsigned int i, netint; |
6c4a91d6 MD |
47 | int option_index; |
48 | int option; | |
7e0cc23b CB |
49 | long values[] = { 1, 2, 3 }; |
50 | char text[10] = "test"; | |
6f9f5cd0 | 51 | char escape[10] = "\\*"; |
7e0cc23b CB |
52 | double dbl = 2.0; |
53 | float flt = 2222.0; | |
6f9f5cd0 | 54 | uint32_t net_values[] = { 1, 2, 3 }; |
b2047add | 55 | int nr_iter = 100, ret = 0, first_event_file_created = 0; |
7e0cc23b | 56 | useconds_t nr_usec = 0; |
5fcaccbc MD |
57 | char *after_first_event_file_path = NULL; |
58 | char *before_last_event_file_path = NULL; | |
4e88fe0a JR |
59 | /* |
60 | * Touch a file to indicate that all events except one were | |
61 | * generated. | |
62 | */ | |
63 | char *before_last_event_file_path_touch = NULL; | |
64 | /* Touch file when we are exiting */ | |
65 | char *before_exit_file_path_touch = NULL; | |
66 | /* Wait on file before exiting */ | |
67 | char *before_exit_file_path = NULL; | |
6ffce1f5 JR |
68 | /* Emit an end event */ |
69 | bool emit_end_event = false; | |
7e0cc23b | 70 | |
6f9f5cd0 JR |
71 | for (i = 0; i < 3; i++) { |
72 | net_values[i] = htonl(net_values[i]); | |
73 | } | |
74 | ||
28ab034a JG |
75 | while ((option = getopt_long(argc, argv, "i:w:a:b:c:d:e:f", long_options, &option_index)) != |
76 | -1) { | |
6c4a91d6 MD |
77 | switch (option) { |
78 | case 'a': | |
79 | after_first_event_file_path = strdup(optarg); | |
80 | break; | |
81 | case 'b': | |
82 | before_last_event_file_path = strdup(optarg); | |
83 | break; | |
4e88fe0a JR |
84 | case 'c': |
85 | before_last_event_file_path_touch = strdup(optarg); | |
86 | break; | |
87 | case 'd': | |
88 | before_exit_file_path = strdup(optarg); | |
89 | break; | |
90 | case 'e': | |
91 | before_exit_file_path_touch = strdup(optarg); | |
92 | break; | |
6ffce1f5 JR |
93 | case 'f': |
94 | emit_end_event = true; | |
95 | break; | |
6c4a91d6 MD |
96 | case 'i': |
97 | nr_iter = atoi(optarg); | |
98 | break; | |
99 | case 'w': | |
100 | nr_usec = atoi(optarg); | |
101 | break; | |
102 | case '?': | |
103 | /* getopt_long already printed an error message. */ | |
6c4a91d6 MD |
104 | default: |
105 | ret = -1; | |
106 | goto end; | |
107 | } | |
7e0cc23b CB |
108 | } |
109 | ||
6c4a91d6 MD |
110 | if (optind != argc) { |
111 | fprintf(stderr, "Error: takes long options only.\n"); | |
56d48389 MD |
112 | |
113 | /* | |
114 | * Aborting the test program for now because callers typically don't check | |
115 | * the test program return value, and the transition from positional | |
116 | * arguments to getopt causes hangs when caller scripts are not updated. | |
117 | * An abort is easier to diagnose and fix. This is a temporary solution: | |
118 | * we should eventually ensure that all scripts test and report the test | |
119 | * app return values. | |
120 | */ | |
121 | abort(); | |
122 | ||
6c4a91d6 MD |
123 | ret = -1; |
124 | goto end; | |
7e0cc23b CB |
125 | } |
126 | ||
6c4a91d6 MD |
127 | if (set_signal_handler()) { |
128 | ret = -1; | |
129 | goto end; | |
209b934f DG |
130 | } |
131 | ||
0fc2834c | 132 | for (i = 0; nr_iter < 0 || i < nr_iter; i++) { |
5fcaccbc | 133 | if (nr_iter >= 0 && i == nr_iter - 1) { |
4e88fe0a JR |
134 | if (before_last_event_file_path_touch) { |
135 | ret = create_file(before_last_event_file_path_touch); | |
136 | if (ret != 0) { | |
137 | goto end; | |
138 | } | |
139 | } | |
140 | ||
5fcaccbc MD |
141 | /* |
142 | * Wait on synchronization before writing last | |
143 | * event. | |
144 | */ | |
b2047add FD |
145 | if (before_last_event_file_path) { |
146 | ret = wait_on_file(before_last_event_file_path); | |
147 | if (ret != 0) { | |
148 | goto end; | |
149 | } | |
150 | } | |
5fcaccbc | 151 | } |
7e0cc23b | 152 | netint = htonl(i); |
28ab034a JG |
153 | tracepoint(tp, |
154 | tptest, | |
155 | i, | |
156 | netint, | |
157 | values, | |
158 | text, | |
159 | strlen(text), | |
160 | escape, | |
161 | net_values, | |
162 | dbl, | |
163 | flt); | |
209b934f DG |
164 | |
165 | /* | |
5fcaccbc MD |
166 | * First loop we create the file if asked to indicate |
167 | * that at least one tracepoint has been hit. | |
209b934f | 168 | */ |
b2047add FD |
169 | if (after_first_event_file_path && first_event_file_created == 0) { |
170 | ret = create_file(after_first_event_file_path); | |
171 | ||
172 | if (ret != 0) { | |
173 | goto end; | |
174 | } else { | |
175 | first_event_file_created = 1; | |
176 | } | |
177 | } | |
178 | ||
b734f5f7 | 179 | if (nr_usec) { |
28ab034a | 180 | if (usleep_safe(nr_usec)) { |
ae941114 JG |
181 | ret = -1; |
182 | goto end; | |
183 | } | |
b734f5f7 | 184 | } |
95983a02 JG |
185 | if (should_quit) { |
186 | break; | |
187 | } | |
7e0cc23b CB |
188 | } |
189 | ||
6ffce1f5 JR |
190 | if (emit_end_event) { |
191 | tracepoint(tp, end); | |
192 | } | |
193 | ||
4e88fe0a JR |
194 | if (before_exit_file_path_touch) { |
195 | ret = create_file(before_exit_file_path_touch); | |
196 | if (ret != 0) { | |
197 | goto end; | |
198 | } | |
199 | } | |
200 | if (before_exit_file_path) { | |
201 | ret = wait_on_file(before_exit_file_path); | |
202 | if (ret != 0) { | |
203 | goto end; | |
204 | } | |
205 | } | |
ae941114 | 206 | end: |
6c4a91d6 MD |
207 | free(after_first_event_file_path); |
208 | free(before_last_event_file_path); | |
4e88fe0a JR |
209 | free(before_last_event_file_path_touch); |
210 | free(before_exit_file_path); | |
211 | free(before_exit_file_path_touch); | |
ae941114 | 212 | exit(!ret ? EXIT_SUCCESS : EXIT_FAILURE); |
7e0cc23b | 213 | } |