| 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 | |
| 18 | #define _LGPL_SOURCE |
| 19 | #include <getopt.h> |
| 20 | #include <assert.h> |
| 21 | #include <arpa/inet.h> |
| 22 | #include <fcntl.h> |
| 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> |
| 31 | #include <stdbool.h> |
| 32 | #include <signal.h> |
| 33 | #include <poll.h> |
| 34 | #include <errno.h> |
| 35 | #include "utils.h" |
| 36 | #include "signal-helper.h" |
| 37 | |
| 38 | #define TRACEPOINT_DEFINE |
| 39 | #include "tp.h" |
| 40 | |
| 41 | static |
| 42 | int create_file(const char *path) |
| 43 | { |
| 44 | int ret; |
| 45 | |
| 46 | if (!path) { |
| 47 | return -1; |
| 48 | } |
| 49 | |
| 50 | ret = creat(path, S_IRWXU); |
| 51 | if (ret < 0) { |
| 52 | perror("creat"); |
| 53 | return -1; |
| 54 | } |
| 55 | |
| 56 | ret = close(ret); |
| 57 | if (ret < 0) { |
| 58 | perror("close"); |
| 59 | return -1; |
| 60 | } |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | static |
| 66 | int wait_on_file(const char *path) |
| 67 | { |
| 68 | int ret; |
| 69 | struct stat buf; |
| 70 | |
| 71 | if (!path) { |
| 72 | return -1; |
| 73 | } |
| 74 | |
| 75 | for (;;) { |
| 76 | ret = stat(path, &buf); |
| 77 | if (ret == -1 && errno == ENOENT) { |
| 78 | ret = poll(NULL, 0, 10); /* 10 ms delay */ |
| 79 | /* Should return 0 everytime */ |
| 80 | if (ret) { |
| 81 | if (ret < 0) { |
| 82 | perror("perror"); |
| 83 | } else { |
| 84 | fprintf(stderr, |
| 85 | "poll return value is larger than zero\n"); |
| 86 | } |
| 87 | return -1; |
| 88 | } |
| 89 | continue; /* retry */ |
| 90 | } |
| 91 | if (ret) { |
| 92 | perror("stat"); |
| 93 | return -1; |
| 94 | } |
| 95 | break; /* found */ |
| 96 | } |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static struct option long_options[] = |
| 102 | { |
| 103 | /* These options set a flag. */ |
| 104 | {"iter", required_argument, 0, 'i'}, |
| 105 | {"wait", required_argument, 0, 'w'}, |
| 106 | {"sync-after-first-event", required_argument, 0, 'a'}, |
| 107 | {"sync-before-last-event", required_argument, 0, 'b'}, |
| 108 | {0, 0, 0, 0} |
| 109 | }; |
| 110 | |
| 111 | int main(int argc, char **argv) |
| 112 | { |
| 113 | unsigned int i, netint; |
| 114 | int option_index; |
| 115 | char option_char; |
| 116 | long values[] = { 1, 2, 3 }; |
| 117 | char text[10] = "test"; |
| 118 | double dbl = 2.0; |
| 119 | float flt = 2222.0; |
| 120 | int nr_iter = 100, ret = 0, first_event_file_created = 0; |
| 121 | useconds_t nr_usec = 0; |
| 122 | char *after_first_event_file_path = NULL; |
| 123 | char *before_last_event_file_path = NULL; |
| 124 | |
| 125 | while((option_char = getopt_long(argc, argv, "i:w:a:b:c:d:", long_options, &option_index)) != -1) { |
| 126 | switch (option_char) { |
| 127 | case 'a': |
| 128 | after_first_event_file_path = strdup(optarg); |
| 129 | break; |
| 130 | case 'b': |
| 131 | before_last_event_file_path = strdup(optarg); |
| 132 | break; |
| 133 | case 'i': |
| 134 | nr_iter = atoi(optarg); |
| 135 | break; |
| 136 | case 'w': |
| 137 | nr_usec = atoi(optarg); |
| 138 | break; |
| 139 | case '?': |
| 140 | /* getopt_long already printed an error message. */ |
| 141 | break; |
| 142 | default: |
| 143 | ret = -1; |
| 144 | goto end; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | if (optind != argc) { |
| 149 | fprintf(stderr, "Error: takes long options only."); |
| 150 | ret = -1; |
| 151 | goto end; |
| 152 | } |
| 153 | |
| 154 | |
| 155 | if (set_signal_handler()) { |
| 156 | ret = -1; |
| 157 | goto end; |
| 158 | } |
| 159 | |
| 160 | for (i = 0; nr_iter < 0 || i < nr_iter; i++) { |
| 161 | if (nr_iter >= 0 && i == nr_iter - 1) { |
| 162 | /* |
| 163 | * Wait on synchronization before writing last |
| 164 | * event. |
| 165 | */ |
| 166 | if (before_last_event_file_path) { |
| 167 | ret = wait_on_file(before_last_event_file_path); |
| 168 | if (ret != 0) { |
| 169 | goto end; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | netint = htonl(i); |
| 174 | tracepoint(tp, tptest, i, netint, values, text, |
| 175 | strlen(text), dbl, flt); |
| 176 | |
| 177 | /* |
| 178 | * First loop we create the file if asked to indicate |
| 179 | * that at least one tracepoint has been hit. |
| 180 | */ |
| 181 | if (after_first_event_file_path && first_event_file_created == 0) { |
| 182 | ret = create_file(after_first_event_file_path); |
| 183 | |
| 184 | if (ret != 0) { |
| 185 | goto end; |
| 186 | } else { |
| 187 | first_event_file_created = 1; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | if (nr_usec) { |
| 192 | if (usleep_safe(nr_usec)) { |
| 193 | ret = -1; |
| 194 | goto end; |
| 195 | } |
| 196 | } |
| 197 | if (should_quit) { |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | end: |
| 203 | free(after_first_event_file_path); |
| 204 | free(before_last_event_file_path); |
| 205 | exit(!ret ? EXIT_SUCCESS : EXIT_FAILURE); |
| 206 | } |