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 |
6c4a91d6 | 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" |
95983a02 | 36 | #include "signal-helper.h" |
7e0cc23b CB |
37 | |
38 | #define TRACEPOINT_DEFINE | |
39 | #include "tp.h" | |
40 | ||
6c4a91d6 MD |
41 | static struct option long_options[] = |
42 | { | |
43 | /* These options set a flag. */ | |
44 | {"iter", required_argument, 0, 'i'}, | |
45 | {"wait", required_argument, 0, 'w'}, | |
46 | {"sync-after-first-event", required_argument, 0, 'a'}, | |
47 | {"sync-before-last-event", required_argument, 0, 'b'}, | |
4e88fe0a JR |
48 | {"sync-before-last-event-touch", required_argument, 0, 'c'}, |
49 | {"sync-before-exit", required_argument, 0, 'd'}, | |
50 | {"sync-before-exit-touch", required_argument, 0, 'e'}, | |
6c4a91d6 MD |
51 | {0, 0, 0, 0} |
52 | }; | |
53 | ||
7e0cc23b CB |
54 | int main(int argc, char **argv) |
55 | { | |
0fc2834c | 56 | unsigned int i, netint; |
6c4a91d6 MD |
57 | int option_index; |
58 | int option; | |
7e0cc23b CB |
59 | long values[] = { 1, 2, 3 }; |
60 | char text[10] = "test"; | |
61 | double dbl = 2.0; | |
62 | float flt = 2222.0; | |
b2047add | 63 | int nr_iter = 100, ret = 0, first_event_file_created = 0; |
7e0cc23b | 64 | useconds_t nr_usec = 0; |
5fcaccbc MD |
65 | char *after_first_event_file_path = NULL; |
66 | char *before_last_event_file_path = NULL; | |
4e88fe0a JR |
67 | /* |
68 | * Touch a file to indicate that all events except one were | |
69 | * generated. | |
70 | */ | |
71 | char *before_last_event_file_path_touch = NULL; | |
72 | /* Touch file when we are exiting */ | |
73 | char *before_exit_file_path_touch = NULL; | |
74 | /* Wait on file before exiting */ | |
75 | char *before_exit_file_path = NULL; | |
7e0cc23b | 76 | |
6c4a91d6 MD |
77 | while ((option = getopt_long(argc, argv, "i:w:a:b:c:d:", |
78 | long_options, &option_index)) != -1) { | |
79 | switch (option) { | |
80 | case 'a': | |
81 | after_first_event_file_path = strdup(optarg); | |
82 | break; | |
83 | case 'b': | |
84 | before_last_event_file_path = strdup(optarg); | |
85 | break; | |
4e88fe0a JR |
86 | case 'c': |
87 | before_last_event_file_path_touch = strdup(optarg); | |
88 | break; | |
89 | case 'd': | |
90 | before_exit_file_path = strdup(optarg); | |
91 | break; | |
92 | case 'e': | |
93 | before_exit_file_path_touch = strdup(optarg); | |
94 | break; | |
6c4a91d6 MD |
95 | case 'i': |
96 | nr_iter = atoi(optarg); | |
97 | break; | |
98 | case 'w': | |
99 | nr_usec = atoi(optarg); | |
100 | break; | |
101 | case '?': | |
102 | /* getopt_long already printed an error message. */ | |
103 | break; | |
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 | ||
5fcaccbc | 127 | |
6c4a91d6 MD |
128 | if (set_signal_handler()) { |
129 | ret = -1; | |
130 | goto end; | |
209b934f DG |
131 | } |
132 | ||
0fc2834c | 133 | for (i = 0; nr_iter < 0 || i < nr_iter; i++) { |
5fcaccbc | 134 | if (nr_iter >= 0 && i == nr_iter - 1) { |
4e88fe0a JR |
135 | if (before_last_event_file_path_touch) { |
136 | ret = create_file(before_last_event_file_path_touch); | |
137 | if (ret != 0) { | |
138 | goto end; | |
139 | } | |
140 | } | |
141 | ||
5fcaccbc MD |
142 | /* |
143 | * Wait on synchronization before writing last | |
144 | * event. | |
145 | */ | |
b2047add FD |
146 | if (before_last_event_file_path) { |
147 | ret = wait_on_file(before_last_event_file_path); | |
148 | if (ret != 0) { | |
149 | goto end; | |
150 | } | |
151 | } | |
5fcaccbc | 152 | } |
7e0cc23b | 153 | netint = htonl(i); |
5fcaccbc MD |
154 | tracepoint(tp, tptest, i, netint, values, text, |
155 | strlen(text), dbl, flt); | |
209b934f DG |
156 | |
157 | /* | |
5fcaccbc MD |
158 | * First loop we create the file if asked to indicate |
159 | * that at least one tracepoint has been hit. | |
209b934f | 160 | */ |
b2047add FD |
161 | if (after_first_event_file_path && first_event_file_created == 0) { |
162 | ret = create_file(after_first_event_file_path); | |
163 | ||
164 | if (ret != 0) { | |
165 | goto end; | |
166 | } else { | |
167 | first_event_file_created = 1; | |
168 | } | |
169 | } | |
170 | ||
b734f5f7 | 171 | if (nr_usec) { |
ae941114 JG |
172 | if (usleep_safe(nr_usec)) { |
173 | ret = -1; | |
174 | goto end; | |
175 | } | |
b734f5f7 | 176 | } |
95983a02 JG |
177 | if (should_quit) { |
178 | break; | |
179 | } | |
7e0cc23b CB |
180 | } |
181 | ||
4e88fe0a JR |
182 | if (before_exit_file_path_touch) { |
183 | ret = create_file(before_exit_file_path_touch); | |
184 | if (ret != 0) { | |
185 | goto end; | |
186 | } | |
187 | } | |
188 | if (before_exit_file_path) { | |
189 | ret = wait_on_file(before_exit_file_path); | |
190 | if (ret != 0) { | |
191 | goto end; | |
192 | } | |
193 | } | |
ae941114 | 194 | end: |
6c4a91d6 MD |
195 | free(after_first_event_file_path); |
196 | free(before_last_event_file_path); | |
4e88fe0a JR |
197 | free(before_last_event_file_path_touch); |
198 | free(before_exit_file_path); | |
199 | free(before_exit_file_path_touch); | |
ae941114 | 200 | exit(!ret ? EXIT_SUCCESS : EXIT_FAILURE); |
7e0cc23b | 201 | } |