From 7a8f55bdb4498c927b6f192b83868138b32cce72 Mon Sep 17 00:00:00 2001 From: dimitri staessens Date: Sat, 10 Dec 2016 11:48:29 +0100 Subject: tools: Shut down cbr gracefully Adds a simple cleanup handler that avoids abrupt termination during an sdu_write call, which potentially locks up the entire system due to the writes in the rdrbuff and rbuff not being handled as a single transaction. --- src/tools/cbr/cbr_client.c | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'src/tools/cbr/cbr_client.c') diff --git a/src/tools/cbr/cbr_client.c b/src/tools/cbr/cbr_client.c index 58198b86..8940c9d9 100644 --- a/src/tools/cbr/cbr_client.c +++ b/src/tools/cbr/cbr_client.c @@ -24,6 +24,25 @@ #include #include +#include + +volatile bool stop; + +static void shutdown_client(int signo, siginfo_t * info, void * c) +{ + (void) info; + (void) c; + + switch(signo) { + case SIGINT: + case SIGTERM: + case SIGHUP: + stop = true; + default: + return; + } +} + static void busy_wait_until(const struct timespec * deadline) { struct timespec now; @@ -42,9 +61,10 @@ int client_main(char * server, bool flood, bool sleep) { + struct sigaction sig_act; + int fd = 0; int result = 0; - bool stop = false; char buf[size]; long seqnr = 0; long gap = size * 8.0 * (BILLION / (double) rate); @@ -54,6 +74,20 @@ int client_main(char * server, struct timespec intv = {(gap / BILLION), gap % BILLION}; int ms; + stop = false; + + memset(&sig_act, 0, sizeof sig_act); + sig_act.sa_sigaction = &shutdown_client; + sig_act.sa_flags = 0; + + if (sigaction(SIGINT, &sig_act, NULL) || + sigaction(SIGTERM, &sig_act, NULL) || + sigaction(SIGHUP, &sig_act, NULL) || + sigaction(SIGPIPE, &sig_act, NULL)) { + printf("Failed to install sighandler.\n"); + return -1; + } + printf("Client started, duration %d, rate %lu b/s, size %d B.\n", duration, rate, size); -- cgit v1.2.3