| 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 <getopt.h> |
| 10 | #include <assert.h> |
| 11 | #include <arpa/inet.h> |
| 12 | #include <fcntl.h> |
| 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> |
| 21 | #include <stdbool.h> |
| 22 | #include <signal.h> |
| 23 | #include <poll.h> |
| 24 | #include <errno.h> |
| 25 | #include "utils.h" |
| 26 | #include "signal-helper.h" |
| 27 | |
| 28 | #define TRACEPOINT_DEFINE |
| 29 | #include "tp.h" |
| 30 | |
| 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'}, |
| 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'}, |
| 41 | {0, 0, 0, 0} |
| 42 | }; |
| 43 | |
| 44 | int main(int argc, char **argv) |
| 45 | { |
| 46 | unsigned int i, netint; |
| 47 | int option_index; |
| 48 | int option; |
| 49 | long values[] = { 1, 2, 3 }; |
| 50 | char text[10] = "test"; |
| 51 | double dbl = 2.0; |
| 52 | float flt = 2222.0; |
| 53 | int nr_iter = 100, ret = 0, first_event_file_created = 0; |
| 54 | useconds_t nr_usec = 0; |
| 55 | char *after_first_event_file_path = NULL; |
| 56 | char *before_last_event_file_path = NULL; |
| 57 | /* |
| 58 | * Touch a file to indicate that all events except one were |
| 59 | * generated. |
| 60 | */ |
| 61 | char *before_last_event_file_path_touch = NULL; |
| 62 | /* Touch file when we are exiting */ |
| 63 | char *before_exit_file_path_touch = NULL; |
| 64 | /* Wait on file before exiting */ |
| 65 | char *before_exit_file_path = NULL; |
| 66 | |
| 67 | while ((option = getopt_long(argc, argv, "i:w:a:b:c:d:", |
| 68 | long_options, &option_index)) != -1) { |
| 69 | switch (option) { |
| 70 | case 'a': |
| 71 | after_first_event_file_path = strdup(optarg); |
| 72 | break; |
| 73 | case 'b': |
| 74 | before_last_event_file_path = strdup(optarg); |
| 75 | break; |
| 76 | case 'c': |
| 77 | before_last_event_file_path_touch = strdup(optarg); |
| 78 | break; |
| 79 | case 'd': |
| 80 | before_exit_file_path = strdup(optarg); |
| 81 | break; |
| 82 | case 'e': |
| 83 | before_exit_file_path_touch = strdup(optarg); |
| 84 | break; |
| 85 | case 'i': |
| 86 | nr_iter = atoi(optarg); |
| 87 | break; |
| 88 | case 'w': |
| 89 | nr_usec = atoi(optarg); |
| 90 | break; |
| 91 | case '?': |
| 92 | /* getopt_long already printed an error message. */ |
| 93 | break; |
| 94 | default: |
| 95 | ret = -1; |
| 96 | goto end; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | if (optind != argc) { |
| 101 | fprintf(stderr, "Error: takes long options only.\n"); |
| 102 | |
| 103 | /* |
| 104 | * Aborting the test program for now because callers typically don't check |
| 105 | * the test program return value, and the transition from positional |
| 106 | * arguments to getopt causes hangs when caller scripts are not updated. |
| 107 | * An abort is easier to diagnose and fix. This is a temporary solution: |
| 108 | * we should eventually ensure that all scripts test and report the test |
| 109 | * app return values. |
| 110 | */ |
| 111 | abort(); |
| 112 | |
| 113 | ret = -1; |
| 114 | goto end; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | if (set_signal_handler()) { |
| 119 | ret = -1; |
| 120 | goto end; |
| 121 | } |
| 122 | |
| 123 | for (i = 0; nr_iter < 0 || i < nr_iter; i++) { |
| 124 | if (nr_iter >= 0 && i == nr_iter - 1) { |
| 125 | if (before_last_event_file_path_touch) { |
| 126 | ret = create_file(before_last_event_file_path_touch); |
| 127 | if (ret != 0) { |
| 128 | goto end; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * Wait on synchronization before writing last |
| 134 | * event. |
| 135 | */ |
| 136 | if (before_last_event_file_path) { |
| 137 | ret = wait_on_file(before_last_event_file_path); |
| 138 | if (ret != 0) { |
| 139 | goto end; |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | netint = htonl(i); |
| 144 | tracepoint(tp, tptest, i, netint, values, text, |
| 145 | strlen(text), dbl, flt); |
| 146 | |
| 147 | /* |
| 148 | * First loop we create the file if asked to indicate |
| 149 | * that at least one tracepoint has been hit. |
| 150 | */ |
| 151 | if (after_first_event_file_path && first_event_file_created == 0) { |
| 152 | ret = create_file(after_first_event_file_path); |
| 153 | |
| 154 | if (ret != 0) { |
| 155 | goto end; |
| 156 | } else { |
| 157 | first_event_file_created = 1; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | if (nr_usec) { |
| 162 | if (usleep_safe(nr_usec)) { |
| 163 | ret = -1; |
| 164 | goto end; |
| 165 | } |
| 166 | } |
| 167 | if (should_quit) { |
| 168 | break; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | if (before_exit_file_path_touch) { |
| 173 | ret = create_file(before_exit_file_path_touch); |
| 174 | if (ret != 0) { |
| 175 | goto end; |
| 176 | } |
| 177 | } |
| 178 | if (before_exit_file_path) { |
| 179 | ret = wait_on_file(before_exit_file_path); |
| 180 | if (ret != 0) { |
| 181 | goto end; |
| 182 | } |
| 183 | } |
| 184 | end: |
| 185 | free(after_first_event_file_path); |
| 186 | free(before_last_event_file_path); |
| 187 | free(before_last_event_file_path_touch); |
| 188 | free(before_exit_file_path); |
| 189 | free(before_exit_file_path_touch); |
| 190 | exit(!ret ? EXIT_SUCCESS : EXIT_FAILURE); |
| 191 | } |