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