sewardj | 5ed7a39 | 2002-11-03 23:03:24 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
| 3 | /*--- A simple program to listen for valgrind logfile data. ---*/ |
| 4 | /*--- valgrind-listener.c ---*/ |
| 5 | /*--------------------------------------------------------------------*/ |
| 6 | |
| 7 | /* |
| 8 | This file is part of Valgrind, an extensible x86 protected-mode |
| 9 | emulator for monitoring program execution on x86-Unixes. |
| 10 | |
njn | 0e1b514 | 2003-04-15 14:58:06 +0000 | [diff] [blame] | 11 | Copyright (C) 2000-2003 Julian Seward |
sewardj | 5ed7a39 | 2002-11-03 23:03:24 +0000 | [diff] [blame] | 12 | jseward@acm.org |
| 13 | |
| 14 | This program is free software; you can redistribute it and/or |
| 15 | modify it under the terms of the GNU General Public License as |
| 16 | published by the Free Software Foundation; either version 2 of the |
| 17 | License, or (at your option) any later version. |
| 18 | |
| 19 | This program is distributed in the hope that it will be useful, but |
| 20 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 22 | General Public License for more details. |
| 23 | |
| 24 | You should have received a copy of the GNU General Public License |
| 25 | along with this program; if not, write to the Free Software |
| 26 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 27 | 02111-1307, USA. |
| 28 | |
| 29 | The GNU General Public License is contained in the file COPYING. |
| 30 | */ |
| 31 | |
| 32 | |
| 33 | /*---------------------------------------------------------------*/ |
| 34 | |
sewardj | 5ed7a39 | 2002-11-03 23:03:24 +0000 | [diff] [blame] | 35 | #include <stdio.h> |
sewardj | ad19c69 | 2002-11-09 10:24:01 +0000 | [diff] [blame] | 36 | #include <unistd.h> |
sewardj | 5ed7a39 | 2002-11-03 23:03:24 +0000 | [diff] [blame] | 37 | #include <string.h> |
sewardj | 5ed7a39 | 2002-11-03 23:03:24 +0000 | [diff] [blame] | 38 | #include <time.h> |
| 39 | #include <fcntl.h> |
| 40 | #include <stdlib.h> |
sewardj | ad19c69 | 2002-11-09 10:24:01 +0000 | [diff] [blame] | 41 | #include <signal.h> |
| 42 | #include <sys/poll.h> |
| 43 | #include <sys/types.h> |
| 44 | #include <sys/socket.h> |
| 45 | #include <netinet/in.h> |
| 46 | |
sewardj | 5ed7a39 | 2002-11-03 23:03:24 +0000 | [diff] [blame] | 47 | |
sewardj | 4335409 | 2002-11-06 00:15:50 +0000 | [diff] [blame] | 48 | /* For VG_CLO_DEFAULT_LOGPORT and VG_EMAIL_ADDR. */ |
| 49 | #include "vg_include.h" |
| 50 | |
| 51 | |
sewardj | 5ed7a39 | 2002-11-03 23:03:24 +0000 | [diff] [blame] | 52 | /*---------------------------------------------------------------*/ |
| 53 | |
sewardj | 4335409 | 2002-11-06 00:15:50 +0000 | [diff] [blame] | 54 | /* The maximum allowable number concurrent connections. */ |
| 55 | #define M_CONNECTIONS 50 |
sewardj | 5ed7a39 | 2002-11-03 23:03:24 +0000 | [diff] [blame] | 56 | |
sewardj | 4335409 | 2002-11-06 00:15:50 +0000 | [diff] [blame] | 57 | |
| 58 | /*---------------------------------------------------------------*/ |
| 59 | |
| 60 | __attribute__ ((noreturn)) |
| 61 | static void panic ( Char* str ) |
| 62 | { |
| 63 | fprintf(stderr, |
| 64 | "\nvalgrind-listener: the " |
| 65 | "`impossible' happened:\n %s\n", str); |
| 66 | fprintf(stderr, |
| 67 | "Please report this bug to: %s\n\n", VG_EMAIL_ADDR); |
| 68 | exit(1); |
| 69 | } |
| 70 | |
| 71 | __attribute__ ((noreturn)) |
| 72 | static void my_assert_fail ( Char* expr, Char* file, Int line, Char* fn ) |
| 73 | { |
| 74 | fprintf(stderr, |
| 75 | "\nvalgrind-listener: %s:%d (%s): Assertion `%s' failed.\n", |
| 76 | file, line, fn, expr ); |
| 77 | fprintf(stderr, |
| 78 | "Please report this bug to: %s\n\n", VG_EMAIL_ADDR); |
| 79 | exit(1); |
| 80 | } |
| 81 | |
| 82 | #undef assert |
| 83 | #undef VG__STRING |
| 84 | |
| 85 | #define VG__STRING(__str) #__str |
| 86 | #define assert(expr) \ |
| 87 | ((void) ((expr) ? 0 : \ |
| 88 | (my_assert_fail (VG__STRING(expr), \ |
| 89 | __FILE__, __LINE__, \ |
| 90 | __PRETTY_FUNCTION__), 0))) |
| 91 | |
sewardj | 5ed7a39 | 2002-11-03 23:03:24 +0000 | [diff] [blame] | 92 | |
| 93 | /*---------------------------------------------------------------*/ |
| 94 | |
| 95 | /* holds the fds for connections; zero if slot not in use. */ |
| 96 | int conn_count = 0; |
sewardj | 4335409 | 2002-11-06 00:15:50 +0000 | [diff] [blame] | 97 | int conn_fd[M_CONNECTIONS]; |
| 98 | struct pollfd conn_pollfd[M_CONNECTIONS]; |
sewardj | 5ed7a39 | 2002-11-03 23:03:24 +0000 | [diff] [blame] | 99 | |
| 100 | |
| 101 | void set_nonblocking ( int sd ) |
| 102 | { |
| 103 | int res; |
| 104 | res = fcntl(sd, F_GETFL); |
| 105 | res = fcntl(sd, F_SETFL, res | O_NONBLOCK); |
| 106 | if (res != 0) { |
| 107 | perror("fcntl failed"); |
sewardj | 4335409 | 2002-11-06 00:15:50 +0000 | [diff] [blame] | 108 | panic("set_nonblocking"); |
sewardj | 5ed7a39 | 2002-11-03 23:03:24 +0000 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | void set_blocking ( int sd ) |
| 113 | { |
| 114 | int res; |
| 115 | res = fcntl(sd, F_GETFL); |
| 116 | res = fcntl(sd, F_SETFL, res & ~O_NONBLOCK); |
| 117 | if (res != 0) { |
| 118 | perror("fcntl failed"); |
sewardj | 4335409 | 2002-11-06 00:15:50 +0000 | [diff] [blame] | 119 | panic("set_blocking"); |
sewardj | 5ed7a39 | 2002-11-03 23:03:24 +0000 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
| 123 | |
| 124 | void copyout ( char* buf, int nbuf ) |
| 125 | { |
| 126 | int i; |
| 127 | for (i = 0; i < nbuf; i++) { |
sewardj | 4335409 | 2002-11-06 00:15:50 +0000 | [diff] [blame] | 128 | if (buf[i] == '\n') { |
| 129 | fprintf(stdout, "\n(%d) ", conn_count); |
| 130 | } else { |
| 131 | fwrite(&buf[i], 1, 1, stdout); |
| 132 | } |
sewardj | 5ed7a39 | 2002-11-03 23:03:24 +0000 | [diff] [blame] | 133 | } |
sewardj | 7d3d1e6 | 2002-11-03 23:27:40 +0000 | [diff] [blame] | 134 | fflush(stdout); |
sewardj | 5ed7a39 | 2002-11-03 23:03:24 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | int read_from_sd ( int sd ) |
| 138 | { |
sewardj | ad19c69 | 2002-11-09 10:24:01 +0000 | [diff] [blame] | 139 | char buf[100]; |
| 140 | int n; |
sewardj | 5ed7a39 | 2002-11-03 23:03:24 +0000 | [diff] [blame] | 141 | |
sewardj | ad19c69 | 2002-11-09 10:24:01 +0000 | [diff] [blame] | 142 | set_blocking(sd); |
| 143 | n = read(sd, buf, 99); |
| 144 | if (n <= 0) return 0; /* closed */ |
| 145 | copyout(buf, n); |
sewardj | 5ed7a39 | 2002-11-03 23:03:24 +0000 | [diff] [blame] | 146 | |
sewardj | ad19c69 | 2002-11-09 10:24:01 +0000 | [diff] [blame] | 147 | set_nonblocking(sd); |
| 148 | while (1) { |
| 149 | n = read(sd, buf, 100); |
| 150 | if (n <= 0) return 1; /* not closed */ |
| 151 | copyout(buf, n); |
| 152 | } |
sewardj | 5ed7a39 | 2002-11-03 23:03:24 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | |
| 156 | void snooze ( void ) |
| 157 | { |
sewardj | 4335409 | 2002-11-06 00:15:50 +0000 | [diff] [blame] | 158 | struct timespec req; |
| 159 | req.tv_sec = 0; |
| 160 | req.tv_nsec = 200 * 1000 * 1000; |
| 161 | nanosleep(&req,NULL); |
sewardj | 5ed7a39 | 2002-11-03 23:03:24 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | |
sewardj | ad19c69 | 2002-11-09 10:24:01 +0000 | [diff] [blame] | 165 | /* returns 0 if invalid, else port # */ |
| 166 | int atoi_portno ( char* str ) |
| 167 | { |
| 168 | int n = 0; |
| 169 | while (1) { |
| 170 | if (*str == 0) |
| 171 | break; |
| 172 | if (*str < '0' || *str > '9') |
| 173 | return 0; |
| 174 | n = 10*n + (int)(*str - '0'); |
| 175 | str++; |
| 176 | if (n >= 65536) |
| 177 | return 0; |
| 178 | } |
| 179 | if (n < 1024) |
| 180 | return 0; |
| 181 | return n; |
| 182 | } |
| 183 | |
| 184 | |
| 185 | void usage ( void ) |
| 186 | { |
| 187 | fprintf(stderr, |
| 188 | "\n" |
| 189 | "usage is:\n" |
| 190 | "\n" |
sewardj | cbf216e | 2002-11-13 19:42:30 +0000 | [diff] [blame] | 191 | " valgrind-listener [--exit-at-zero|-e] [port-number]\n" |
sewardj | ad19c69 | 2002-11-09 10:24:01 +0000 | [diff] [blame] | 192 | "\n" |
sewardj | cbf216e | 2002-11-13 19:42:30 +0000 | [diff] [blame] | 193 | " where --exit-at-zero or -e causes the listener to exit\n" |
sewardj | ad19c69 | 2002-11-09 10:24:01 +0000 | [diff] [blame] | 194 | " when the number of connections falls back to zero\n" |
| 195 | " (the default is to keep listening forever)\n" |
| 196 | "\n" |
| 197 | " port-number is the default port on which to listen for\n" |
| 198 | " connections. It must be between 1024 and 65535.\n" |
| 199 | " Current default is %d.\n" |
| 200 | "\n" |
| 201 | , |
| 202 | VG_CLO_DEFAULT_LOGPORT |
| 203 | ); |
| 204 | exit(1); |
| 205 | } |
| 206 | |
| 207 | |
| 208 | void banner ( char* str ) |
| 209 | { |
| 210 | time_t t; |
| 211 | t = time(NULL); |
| 212 | printf("valgrind-listener %s at %s", str, ctime(&t)); |
sewardj | 68948f7 | 2002-11-16 20:15:16 +0000 | [diff] [blame] | 213 | fflush(stdout); |
sewardj | ad19c69 | 2002-11-09 10:24:01 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | |
| 217 | void exit_routine ( void ) |
| 218 | { |
| 219 | banner("exited"); |
| 220 | exit(0); |
| 221 | } |
| 222 | |
| 223 | |
| 224 | void sigint_handler ( int signo ) |
| 225 | { |
| 226 | exit_routine(); |
| 227 | } |
| 228 | |
| 229 | |
sewardj | 4335409 | 2002-11-06 00:15:50 +0000 | [diff] [blame] | 230 | int main (int argc, char** argv) |
| 231 | { |
sewardj | ad19c69 | 2002-11-09 10:24:01 +0000 | [diff] [blame] | 232 | int i, j, k, res; |
| 233 | int main_sd, new_sd, client_len; |
sewardj | 4335409 | 2002-11-06 00:15:50 +0000 | [diff] [blame] | 234 | struct sockaddr_in client_addr, server_addr; |
sewardj | 5ed7a39 | 2002-11-03 23:03:24 +0000 | [diff] [blame] | 235 | |
sewardj | ad19c69 | 2002-11-09 10:24:01 +0000 | [diff] [blame] | 236 | char /*bool*/ exit_when_zero = 0; |
| 237 | int port = VG_CLO_DEFAULT_LOGPORT; |
| 238 | |
| 239 | for (i = 1; i < argc; i++) { |
sewardj | cbf216e | 2002-11-13 19:42:30 +0000 | [diff] [blame] | 240 | if (0==strcmp(argv[i], "--exit-at-zero") |
sewardj | ad19c69 | 2002-11-09 10:24:01 +0000 | [diff] [blame] | 241 | || 0==strcmp(argv[i], "-e")) { |
| 242 | exit_when_zero = 1; |
| 243 | } |
| 244 | else |
| 245 | if (atoi_portno(argv[i]) > 0) { |
| 246 | port = atoi_portno(argv[i]); |
| 247 | } |
| 248 | else |
| 249 | usage(); |
| 250 | } |
| 251 | |
| 252 | banner("started"); |
| 253 | signal(SIGINT, sigint_handler); |
| 254 | |
sewardj | 4335409 | 2002-11-06 00:15:50 +0000 | [diff] [blame] | 255 | conn_count = 0; |
| 256 | for (i = 0; i < M_CONNECTIONS; i++) |
| 257 | conn_fd[i] = 0; |
sewardj | 5ed7a39 | 2002-11-03 23:03:24 +0000 | [diff] [blame] | 258 | |
sewardj | 4335409 | 2002-11-06 00:15:50 +0000 | [diff] [blame] | 259 | /* create socket */ |
| 260 | main_sd = socket(AF_INET, SOCK_STREAM, 0); |
sewardj | 5ed7a39 | 2002-11-03 23:03:24 +0000 | [diff] [blame] | 261 | if (main_sd < 0) { |
sewardj | 4335409 | 2002-11-06 00:15:50 +0000 | [diff] [blame] | 262 | perror("cannot open socket "); |
| 263 | panic("main -- create socket"); |
| 264 | } |
sewardj | 5ed7a39 | 2002-11-03 23:03:24 +0000 | [diff] [blame] | 265 | |
sewardj | 4335409 | 2002-11-06 00:15:50 +0000 | [diff] [blame] | 266 | /* bind server port */ |
| 267 | server_addr.sin_family = AF_INET; |
| 268 | server_addr.sin_addr.s_addr = htonl(INADDR_ANY); |
sewardj | ad19c69 | 2002-11-09 10:24:01 +0000 | [diff] [blame] | 269 | server_addr.sin_port = htons(port); |
sewardj | 5ed7a39 | 2002-11-03 23:03:24 +0000 | [diff] [blame] | 270 | |
sewardj | 4335409 | 2002-11-06 00:15:50 +0000 | [diff] [blame] | 271 | if (bind(main_sd, (struct sockaddr *) &server_addr, |
| 272 | sizeof(server_addr) ) < 0) { |
| 273 | perror("cannot bind port "); |
| 274 | panic("main -- bind port"); |
| 275 | } |
sewardj | 5ed7a39 | 2002-11-03 23:03:24 +0000 | [diff] [blame] | 276 | |
sewardj | 4335409 | 2002-11-06 00:15:50 +0000 | [diff] [blame] | 277 | res = listen(main_sd,M_CONNECTIONS); |
| 278 | if (res != 0) { |
| 279 | perror("listen failed "); |
| 280 | panic("main -- listen"); |
| 281 | } |
sewardj | 5ed7a39 | 2002-11-03 23:03:24 +0000 | [diff] [blame] | 282 | |
sewardj | ad19c69 | 2002-11-09 10:24:01 +0000 | [diff] [blame] | 283 | while (1) { |
sewardj | 5ed7a39 | 2002-11-03 23:03:24 +0000 | [diff] [blame] | 284 | |
sewardj | 4335409 | 2002-11-06 00:15:50 +0000 | [diff] [blame] | 285 | snooze(); |
sewardj | 5ed7a39 | 2002-11-03 23:03:24 +0000 | [diff] [blame] | 286 | |
sewardj | 4335409 | 2002-11-06 00:15:50 +0000 | [diff] [blame] | 287 | /* enquire, using poll, whether there is any activity available on |
| 288 | the main socket descriptor. If so, someone is trying to |
| 289 | connect; get the fd and add it to our table thereof. */ |
| 290 | { struct pollfd ufd; |
| 291 | while (1) { |
| 292 | ufd.fd = main_sd; |
| 293 | ufd.events = POLLIN; |
| 294 | ufd.revents = 0; |
| 295 | res = poll(&ufd, 1, 0); |
| 296 | if (res == 0) break; |
sewardj | 5ed7a39 | 2002-11-03 23:03:24 +0000 | [diff] [blame] | 297 | |
sewardj | 4335409 | 2002-11-06 00:15:50 +0000 | [diff] [blame] | 298 | /* ok, we have someone waiting to connect. Get the sd. */ |
| 299 | client_len = sizeof(client_addr); |
sewardj | ad19c69 | 2002-11-09 10:24:01 +0000 | [diff] [blame] | 300 | new_sd = accept(main_sd, (struct sockaddr *) &client_addr, |
sewardj | 4335409 | 2002-11-06 00:15:50 +0000 | [diff] [blame] | 301 | &client_len); |
sewardj | ad19c69 | 2002-11-09 10:24:01 +0000 | [diff] [blame] | 302 | if (new_sd < 0) { |
sewardj | 4335409 | 2002-11-06 00:15:50 +0000 | [diff] [blame] | 303 | perror("cannot accept connection "); |
| 304 | panic("main -- accept connection"); |
| 305 | } |
| 306 | |
| 307 | /* find a place to put it. */ |
sewardj | ad19c69 | 2002-11-09 10:24:01 +0000 | [diff] [blame] | 308 | assert(new_sd > 0); |
sewardj | 4335409 | 2002-11-06 00:15:50 +0000 | [diff] [blame] | 309 | for (i = 0; i < M_CONNECTIONS; i++) |
| 310 | if (conn_fd[i] == 0) |
| 311 | break; |
| 312 | |
| 313 | if (i >= M_CONNECTIONS) { |
| 314 | fprintf(stderr, "Too many concurrent connections. " |
| 315 | "Increase M_CONNECTIONS and recompile.\n"); |
| 316 | panic("main -- too many concurrent connections"); |
| 317 | } |
| 318 | |
sewardj | ad19c69 | 2002-11-09 10:24:01 +0000 | [diff] [blame] | 319 | conn_fd[i] = new_sd; |
sewardj | 4335409 | 2002-11-06 00:15:50 +0000 | [diff] [blame] | 320 | conn_count++; |
| 321 | printf("\n(%d) -------------------- CONNECT " |
| 322 | "--------------------\n(%d)\n(%d) ", |
| 323 | conn_count, conn_count, conn_count); |
| 324 | fflush(stdout); |
| 325 | } /* while (1) */ |
| 326 | } |
| 327 | |
| 328 | /* We've processed all new connect requests. Listen for changes |
| 329 | to the current set of fds. */ |
| 330 | j = 0; |
| 331 | for (i = 0; i < M_CONNECTIONS; i++) { |
| 332 | if (conn_fd[i] == 0) |
| 333 | continue; |
| 334 | conn_pollfd[j].fd = conn_fd[i]; |
| 335 | conn_pollfd[j].events = POLLIN /* | POLLHUP | POLLNVAL */; |
| 336 | conn_pollfd[j].revents = 0; |
| 337 | j++; |
| 338 | } |
| 339 | |
| 340 | res = poll(conn_pollfd, j, 0 /* return immediately. */ ); |
| 341 | if (res < 0) { |
| 342 | perror("poll(main) failed"); |
| 343 | panic("poll(main) failed"); |
| 344 | } |
| 345 | |
| 346 | /* nothing happened. go round again. */ |
| 347 | if (res == 0) { |
| 348 | continue; |
| 349 | } |
| 350 | |
| 351 | /* inspect the fds. */ |
| 352 | for (i = 0; i < j; i++) { |
| 353 | |
| 354 | if (conn_pollfd[i].revents & POLLIN) { |
| 355 | /* data is available on this fd */ |
| 356 | res = read_from_sd(conn_pollfd[i].fd); |
| 357 | |
| 358 | if (res == 0) { |
| 359 | /* the connection has been closed. */ |
| 360 | /* this fd has been closed or otherwise gone bad; forget |
| 361 | about it. */ |
| 362 | for (k = 0; k < M_CONNECTIONS; k++) |
| 363 | if (conn_fd[k] == conn_pollfd[i].fd) |
| 364 | break; |
| 365 | assert(k < M_CONNECTIONS); |
| 366 | conn_fd[k] = 0; |
| 367 | conn_count--; |
| 368 | printf("\n(%d) ------------------- DISCONNECT " |
| 369 | "-------------------\n(%d)\n(%d) ", |
| 370 | conn_count, conn_count, conn_count); |
| 371 | fflush(stdout); |
sewardj | ad19c69 | 2002-11-09 10:24:01 +0000 | [diff] [blame] | 372 | if (conn_count == 0 && exit_when_zero) { |
| 373 | printf("\n"); |
sewardj | 68948f7 | 2002-11-16 20:15:16 +0000 | [diff] [blame] | 374 | fflush(stdout); |
sewardj | ad19c69 | 2002-11-09 10:24:01 +0000 | [diff] [blame] | 375 | exit_routine(); |
| 376 | } |
sewardj | 4335409 | 2002-11-06 00:15:50 +0000 | [diff] [blame] | 377 | } |
sewardj | 5ed7a39 | 2002-11-03 23:03:24 +0000 | [diff] [blame] | 378 | } |
| 379 | |
sewardj | 4335409 | 2002-11-06 00:15:50 +0000 | [diff] [blame] | 380 | } /* for (i = 0; i < j; i++) */ |
| 381 | |
sewardj | ad19c69 | 2002-11-09 10:24:01 +0000 | [diff] [blame] | 382 | } /* while (1) */ |
sewardj | 5ed7a39 | 2002-11-03 23:03:24 +0000 | [diff] [blame] | 383 | |
sewardj | ad19c69 | 2002-11-09 10:24:01 +0000 | [diff] [blame] | 384 | /* NOTREACHED */ |
sewardj | 5ed7a39 | 2002-11-03 23:03:24 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | |
| 388 | /*--------------------------------------------------------------------*/ |
| 389 | /*--- end valgrind-listener.c ---*/ |
| 390 | /*--------------------------------------------------------------------*/ |