2 /*--------------------------------------------------------------------*/
3 /*--- A simple program to listen for valgrind logfile data. ---*/
4 /*--- valgrind-listener.c ---*/
5 /*--------------------------------------------------------------------*/
8 This file is part of Valgrind, a dynamic binary instrumentation
11 Copyright (C) 2000-2013 Julian Seward
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.
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.
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
29 The GNU General Public License is contained in the file COPYING.
33 /*---------------------------------------------------------------*/
35 /* Include valgrind headers before system headers to avoid problems
36 with the system headers #defining things which are used as names
37 of structure members in vki headers. */
39 #include "pub_core_basics.h"
40 #include "pub_core_libcassert.h" // For VG_BUGS_TO
41 #include "pub_core_vki.h" // Avoids warnings from
42 // pub_core_libcfile.h
43 #include "pub_core_libcfile.h" // For VG_CLO_DEFAULT_LOGPORT
53 #include <sys/types.h>
54 #include <sys/socket.h>
55 #include <netinet/in.h>
58 /*---------------------------------------------------------------*/
60 /* The maximum allowable number concurrent connections. */
61 #define M_CONNECTIONS 50
64 /*---------------------------------------------------------------*/
66 __attribute__ ((noreturn
))
67 static void panic ( const char* str
)
70 "\nvalgrind-listener: the "
71 "'impossible' happened:\n %s\n", str
);
73 "Please report this bug at: %s\n\n", VG_BUGS_TO
);
77 __attribute__ ((noreturn
))
78 static void my_assert_fail ( const char* expr
, const char* file
, int line
, const char* fn
)
81 "\nvalgrind-listener: %s:%d (%s): Assertion '%s' failed.\n",
82 file
, line
, fn
, expr
);
84 "Please report this bug at: %s\n\n", VG_BUGS_TO
);
90 #define assert(expr) \
91 ((void) ((expr) ? 0 : \
92 (my_assert_fail (VG_STRINGIFY(expr), \
94 __PRETTY_FUNCTION__), 0)))
97 /*---------------------------------------------------------------*/
99 /* holds the fds for connections; zero if slot not in use. */
101 int conn_fd
[M_CONNECTIONS
];
102 struct pollfd conn_pollfd
[M_CONNECTIONS
];
105 static void set_nonblocking ( int sd
)
108 res
= fcntl(sd
, F_GETFL
);
109 res
= fcntl(sd
, F_SETFL
, res
| O_NONBLOCK
);
111 perror("fcntl failed");
112 panic("set_nonblocking");
116 static void set_blocking ( int sd
)
119 res
= fcntl(sd
, F_GETFL
);
120 res
= fcntl(sd
, F_SETFL
, res
& ~O_NONBLOCK
);
122 perror("fcntl failed");
123 panic("set_blocking");
128 static void copyout ( char* buf
, int nbuf
)
131 for (i
= 0; i
< nbuf
; i
++) {
132 if (buf
[i
] == '\n') {
133 fprintf(stdout
, "\n(%d) ", conn_count
);
135 __attribute__((unused
)) size_t ignored
136 = fwrite(&buf
[i
], 1, 1, stdout
);
142 static int read_from_sd ( int sd
)
148 n
= read(sd
, buf
, 99);
149 if (n
<= 0) return 0; /* closed */
154 n
= read(sd
, buf
, 100);
155 if (n
<= 0) return 1; /* not closed */
161 static void snooze ( void )
165 req
.tv_nsec
= 200 * 1000 * 1000;
166 nanosleep(&req
,NULL
);
170 /* returns 0 if invalid, else port # */
171 static int atoi_portno ( const char* str
)
177 if (*str
< '0' || *str
> '9')
179 n
= 10*n
+ (int)(*str
- '0');
190 static void usage ( void )
196 " valgrind-listener [--exit-at-zero|-e] [port-number]\n"
198 " where --exit-at-zero or -e causes the listener to exit\n"
199 " when the number of connections falls back to zero\n"
200 " (the default is to keep listening forever)\n"
202 " port-number is the default port on which to listen for\n"
203 " connections. It must be between 1024 and 65535.\n"
204 " Current default is %d.\n"
207 VG_CLO_DEFAULT_LOGPORT
213 static void banner ( const char* str
)
217 printf("valgrind-listener %s at %s", str
, ctime(&t
));
222 static void exit_routine ( void )
229 static void sigint_handler ( int signo
)
235 int main (int argc
, char** argv
)
237 int i
, j
, k
, res
, one
;
239 socklen_t client_len
;
240 struct sockaddr_in client_addr
, server_addr
;
242 char /*bool*/ exit_when_zero
= 0;
243 int port
= VG_CLO_DEFAULT_LOGPORT
;
245 for (i
= 1; i
< argc
; i
++) {
246 if (0==strcmp(argv
[i
], "--exit-at-zero")
247 || 0==strcmp(argv
[i
], "-e")) {
251 if (atoi_portno(argv
[i
]) > 0) {
252 port
= atoi_portno(argv
[i
]);
259 signal(SIGINT
, sigint_handler
);
262 for (i
= 0; i
< M_CONNECTIONS
; i
++)
266 main_sd
= socket(AF_INET
, SOCK_STREAM
, 0);
268 perror("cannot open socket ");
269 panic("main -- create socket");
272 /* allow address reuse to avoid "address already in use" errors */
275 if (setsockopt(main_sd
, SOL_SOCKET
, SO_REUSEADDR
,
276 &one
, sizeof(int)) < 0) {
277 perror("cannot enable address reuse ");
278 panic("main -- enable address reuse");
281 /* bind server port */
282 server_addr
.sin_family
= AF_INET
;
283 server_addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
284 server_addr
.sin_port
= htons(port
);
286 if (bind(main_sd
, (struct sockaddr
*) &server_addr
,
287 sizeof(server_addr
) ) < 0) {
288 perror("cannot bind port ");
289 panic("main -- bind port");
292 res
= listen(main_sd
,M_CONNECTIONS
);
294 perror("listen failed ");
295 panic("main -- listen");
302 /* enquire, using poll, whether there is any activity available on
303 the main socket descriptor. If so, someone is trying to
304 connect; get the fd and add it to our table thereof. */
310 res
= poll(&ufd
, 1, 0);
313 /* ok, we have someone waiting to connect. Get the sd. */
314 client_len
= sizeof(client_addr
);
315 new_sd
= accept(main_sd
, (struct sockaddr
*)&client_addr
,
318 perror("cannot accept connection ");
319 panic("main -- accept connection");
322 /* find a place to put it. */
324 for (i
= 0; i
< M_CONNECTIONS
; i
++)
328 if (i
>= M_CONNECTIONS
) {
329 fprintf(stderr
, "Too many concurrent connections. "
330 "Increase M_CONNECTIONS and recompile.\n");
331 panic("main -- too many concurrent connections");
336 printf("\n(%d) -------------------- CONNECT "
337 "--------------------\n(%d)\n(%d) ",
338 conn_count
, conn_count
, conn_count
);
343 /* We've processed all new connect requests. Listen for changes
344 to the current set of fds. */
346 for (i
= 0; i
< M_CONNECTIONS
; i
++) {
349 conn_pollfd
[j
].fd
= conn_fd
[i
];
350 conn_pollfd
[j
].events
= POLLIN
/* | POLLHUP | POLLNVAL */;
351 conn_pollfd
[j
].revents
= 0;
355 res
= poll(conn_pollfd
, j
, 0 /* return immediately. */ );
357 perror("poll(main) failed");
358 panic("poll(main) failed");
361 /* nothing happened. go round again. */
366 /* inspect the fds. */
367 for (i
= 0; i
< j
; i
++) {
369 if (conn_pollfd
[i
].revents
& POLLIN
) {
370 /* data is available on this fd */
371 res
= read_from_sd(conn_pollfd
[i
].fd
);
374 /* the connection has been closed. */
375 close(conn_pollfd
[i
].fd
);
376 /* this fd has been closed or otherwise gone bad; forget
378 for (k
= 0; k
< M_CONNECTIONS
; k
++)
379 if (conn_fd
[k
] == conn_pollfd
[i
].fd
)
381 assert(k
< M_CONNECTIONS
);
384 printf("\n(%d) ------------------- DISCONNECT "
385 "-------------------\n(%d)\n(%d) ",
386 conn_count
, conn_count
, conn_count
);
388 if (conn_count
== 0 && exit_when_zero
) {
396 } /* for (i = 0; i < j; i++) */
404 /*--------------------------------------------------------------------*/
405 /*--- end valgrind-listener.c ---*/
406 /*--------------------------------------------------------------------*/