| 1 | /* |
| 2 | * Copyright (C) 2012 David Goulet <dgoulet@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: LGPL-2.1-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #define _LGPL_SOURCE |
| 9 | #include "signal-helper.hpp" |
| 10 | #include "utils.h" |
| 11 | |
| 12 | #include <arpa/inet.h> |
| 13 | #include <errno.h> |
| 14 | #include <fcntl.h> |
| 15 | #include <getopt.h> |
| 16 | #include <poll.h> |
| 17 | #include <signal.h> |
| 18 | #include <stdarg.h> |
| 19 | #include <stdbool.h> |
| 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 | |
| 31 | static struct option long_options[] = { |
| 32 | /* These options set a flag. */ |
| 33 | { "iter", required_argument, nullptr, 'i' }, |
| 34 | { "wait", required_argument, nullptr, 'w' }, |
| 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' }, |
| 43 | { nullptr, 0, nullptr, 0 } |
| 44 | }; |
| 45 | |
| 46 | int main(int argc, char **argv) |
| 47 | { |
| 48 | unsigned int i, netint; |
| 49 | int option_index; |
| 50 | int option; |
| 51 | long values[] = { 1, 2, 3 }; |
| 52 | char text[10] = "test"; |
| 53 | char escape[10] = "\\*"; |
| 54 | double dbl = 2.0; |
| 55 | float flt = 2222.0; |
| 56 | uint32_t net_values[] = { 1, 2, 3 }; |
| 57 | int nr_iter = 100, ret = 0, first_event_file_created = 0; |
| 58 | useconds_t nr_usec = 0; |
| 59 | char *application_in_main_file_path = nullptr; |
| 60 | char *before_first_event_file_path = nullptr; |
| 61 | char *after_first_event_file_path = nullptr; |
| 62 | char *before_last_event_file_path = nullptr; |
| 63 | /* |
| 64 | * Touch a file to indicate that all events except one were |
| 65 | * generated. |
| 66 | */ |
| 67 | char *before_last_event_file_path_touch = nullptr; |
| 68 | /* Touch file when we are exiting */ |
| 69 | char *before_exit_file_path_touch = nullptr; |
| 70 | /* Wait on file before exiting */ |
| 71 | char *before_exit_file_path = nullptr; |
| 72 | /* Emit an end event */ |
| 73 | bool emit_end_event = false; |
| 74 | |
| 75 | for (i = 0; i < 3; i++) { |
| 76 | net_values[i] = htonl(net_values[i]); |
| 77 | } |
| 78 | |
| 79 | while ((option = getopt_long( |
| 80 | argc, argv, "i:w:a:b:c:d:e:f:g:h", long_options, &option_index)) != -1) { |
| 81 | switch (option) { |
| 82 | case 'a': |
| 83 | application_in_main_file_path = strdup(optarg); |
| 84 | break; |
| 85 | case 'b': |
| 86 | before_first_event_file_path = strdup(optarg); |
| 87 | break; |
| 88 | case 'c': |
| 89 | after_first_event_file_path = strdup(optarg); |
| 90 | break; |
| 91 | case 'd': |
| 92 | before_last_event_file_path = strdup(optarg); |
| 93 | break; |
| 94 | case 'e': |
| 95 | before_last_event_file_path_touch = strdup(optarg); |
| 96 | break; |
| 97 | case 'f': |
| 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': |
| 104 | emit_end_event = true; |
| 105 | break; |
| 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. */ |
| 114 | default: |
| 115 | ret = -1; |
| 116 | goto end; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | if (optind != argc) { |
| 121 | fprintf(stderr, "Error: takes long options only.\n"); |
| 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 | |
| 133 | ret = -1; |
| 134 | goto end; |
| 135 | } |
| 136 | |
| 137 | if (set_signal_handler()) { |
| 138 | ret = -1; |
| 139 | goto end; |
| 140 | } |
| 141 | |
| 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 | |
| 150 | for (i = 0; nr_iter < 0 || i < nr_iter; i++) { |
| 151 | if (nr_iter >= 0 && i == nr_iter - 1) { |
| 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 | |
| 159 | /* |
| 160 | * Wait on synchronization before writing last |
| 161 | * event. |
| 162 | */ |
| 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 | } |
| 169 | } |
| 170 | netint = htonl(i); |
| 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); |
| 182 | |
| 183 | /* |
| 184 | * First loop we create the file if asked to indicate |
| 185 | * that at least one tracepoint has been hit. |
| 186 | */ |
| 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 | |
| 197 | if (nr_usec) { |
| 198 | if (usleep_safe(nr_usec)) { |
| 199 | ret = -1; |
| 200 | goto end; |
| 201 | } |
| 202 | } |
| 203 | if (should_quit) { |
| 204 | break; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | if (emit_end_event) { |
| 209 | tracepoint(tp, end); |
| 210 | } |
| 211 | |
| 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 | } |
| 224 | end: |
| 225 | free(application_in_main_file_path); |
| 226 | free(before_first_event_file_path); |
| 227 | free(after_first_event_file_path); |
| 228 | free(before_last_event_file_path); |
| 229 | free(before_last_event_file_path_touch); |
| 230 | free(before_exit_file_path); |
| 231 | free(before_exit_file_path_touch); |
| 232 | exit(!ret ? EXIT_SUCCESS : EXIT_FAILURE); |
| 233 | } |