Add timestamped "aborting" message

This commit is contained in:
Ludovic Pouzenc 2018-07-18 12:39:23 +02:00 committed by Matthieu Herrb
parent 16d5383fb8
commit 4a15bdac0a

21
echoc.c
View file

@ -37,6 +37,7 @@
int sock = -1;
int verbose = 0;
int aborting = 0;
struct sockaddr *server;
socklen_t serverlen;
unsigned int seq = 0;
@ -48,11 +49,16 @@ usage(void)
errx(2, "usage: echoc [-c nbr][-d][-i ms][-l len][-p port][-t ms][-v] server");
}
static void
sigint_handler(int unused)
{
aborting = 1;
}
static void
send_packet(int unused)
{
char *buf;
buf = malloc(len);
if (buf == NULL)
return;
@ -175,6 +181,7 @@ main(int argc, char *argv[])
err(2, "setsockopt IP_MTU_DISCOVER");
#endif
signal(SIGALRM, send_packet);
signal(SIGINT, sigint_handler);
/* timer values */
itv.it_interval.tv_usec = interval*1000;
@ -189,9 +196,9 @@ main(int argc, char *argv[])
strftime(date, sizeof(date), "%F %T", tm);
printf("%s.%06ld: starting\n", date, last_ts.tv_usec);
while (1) {
while (!aborting) {
/* poll() loop to handle interruptions by SIGALRM */
while (1) {
while (!aborting) {
pfd[0].fd = sock;
pfd[0].events = POLLIN;
nfds = poll(pfd, 1, timeout);
@ -207,6 +214,8 @@ main(int argc, char *argv[])
break;
}
}
if (aborting)
break;
if ((nfds == 0)) {
if (verbose)
printf("%d packet(s) dropped in %ld.%06ld s\n",
@ -262,6 +271,12 @@ main(int argc, char *argv[])
}
}
}
if (aborting) {
gettimeofday(&now, NULL);
tm = localtime((time_t *)&now.tv_sec);
strftime(date, sizeof(date), "%F %T", tm);
printf("%s.%06ld: aborting\n", date, now.tv_usec);
}
close(sock);
exit(0);
}