From c50f2a7c8edb13e1373515e78193af94d4d8d9c9 Mon Sep 17 00:00:00 2001 From: KHANH LUONG HONG DUY Date: Wed, 28 Oct 2020 18:04:51 +0700 Subject: dlt-receive: flush stdout buffer by signal When dlt-receive logs is redirecting to file then a terminating signal is sent to dlt-receive, it will stop without flushing all logs from stdout to file, due to the default size of stdout is 4096 byte. When dlt-receive receive the SIGTERM or SIGINT, it flushes stdout buffer to stdout before terminating. Signed-off-by: KHANH LUONG HONG DUY --- src/console/dlt-receive.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/console/dlt-receive.c b/src/console/dlt-receive.c index cdfa274..1b5c112 100644 --- a/src/console/dlt-receive.c +++ b/src/console/dlt-receive.c @@ -75,6 +75,8 @@ #include #include #include +#include +#include #ifdef __linux__ # include #else @@ -86,6 +88,24 @@ #define DLT_RECEIVE_ECU_ID "RECV" +DltClient dltclient; + +void signal_handler(int signal) +{ + switch (signal) { + case SIGHUP: + case SIGTERM: + case SIGINT: + case SIGQUIT: + /* stop main loop */ + shutdown(dltclient.receiver.fd, SHUT_RD); + break; + default: + /* This case should never happen! */ + break; + } /* switch */ +} + /* Function prototypes */ int dlt_receive_message_callback(DltMessage *message, void *data); @@ -290,7 +310,6 @@ void dlt_receive_close_output_file(DltReceiveData *dltdata) */ int main(int argc, char *argv[]) { - DltClient dltclient; DltReceiveData dltdata; int c; int index; @@ -313,6 +332,15 @@ int main(int argc, char *argv[]) dltdata.totalbytes = 0; dltdata.part_num = -1; + /* Config signal handler */ + struct sigaction act; + act.sa_handler = signal_handler; + sigemptyset(&act.sa_mask); + sigaction(SIGHUP, &act, 0); + sigaction(SIGTERM, &act, 0); + sigaction(SIGINT, &act, 0); + sigaction(SIGQUIT, &act, 0); + /* Fetch command line arguments */ opterr = 0; -- cgit v1.2.1