From eeeebe6cb25907dbe79d6046f99dc4b3a7771fdc Mon Sep 17 00:00:00 2001 From: Gerhard Rieger Date: Mon, 8 May 2023 22:33:12 +0200 Subject: [PATCH] Fixed unwanted recvfrom() with signal handler logs --- CHANGES | 3 +++ error.c | 15 ++++++++++----- error.h | 6 +++--- socat.c | 1 - 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/CHANGES b/CHANGES index 090e222..1594fb8 100644 --- a/CHANGES +++ b/CHANGES @@ -56,6 +56,9 @@ Corrections: Procan tries to find out VSOCK CID only when running as root + The mechanism for deferring logs from signal handlers had an issue that + caused lots of unwanted recvfrom() calls. + Features: VSOCK, VSOCK-L support options pf, socktype, prototype (currently useless) diff --git a/error.c b/error.c index f38abf1..bb3f91d 100644 --- a/error.c +++ b/error.c @@ -58,9 +58,9 @@ static void msg2( int level, int exitcode, int handler, const char *text); static void _msg(int level, const char *buff, const char *syslp); -sig_atomic_t diag_in_handler; /* !=0 indicates to msg() that in signal handler */ -sig_atomic_t diag_immediate_msg; /* !=0 prints messages even from within signal handler instead of deferring them */ -sig_atomic_t diag_immediate_exit; /* !=0 calls exit() from diag_exit() even when in signal handler. For system() */ +volatile sig_atomic_t diag_in_handler; /* !=0 indicates to msg() that in signal handler */ +volatile sig_atomic_t diag_immediate_msg; /* !=0 prints messages even from within signal handler instead of deferring them */ +volatile sig_atomic_t diag_immediate_exit; /* !=0 calls exit() from diag_exit() even when in signal handler. For system() */ static struct wordent facilitynames[] = { {"auth", (void *)LOG_AUTH}, @@ -108,7 +108,7 @@ struct sermsg { static int diaginitialized; static int diag_sock_send = -1; static int diag_sock_recv = -1; -static int diag_msg_avail = 0; /* !=0: messages from within signal handler may be waiting */ +static volatile sig_atomic_t diag_msg_avail = 0; /* !=0: messages from within signal handler may be waiting */ static int diag_sock_pair(void) { @@ -278,7 +278,6 @@ void msg(int level, const char *format, ...) { /* in normal program flow (not in signal handler) */ /* first flush the queue of datagrams from the socket */ if (diag_msg_avail && !diag_in_handler) { - diag_msg_avail = 0; /* _before_ flush to prevent inconsistent state when signal occurs inbetween */ diag_flush(); } @@ -418,6 +417,11 @@ void diag_flush(void) { struct diag_dgram recv_dgram; char exitmsg[20]; + if (diag_msg_avail == 0) { + return; + } + diag_msg_avail = 0; + if (!diagopts.signalsafe) { return; } @@ -500,6 +504,7 @@ void diag_exit(int status) { |MSG_NOSIGNAL #endif ); + diag_msg_avail = 1; return; } _diag_exit(status); diff --git a/error.h b/error.h index 32e5a59..9853f8a 100644 --- a/error.h +++ b/error.h @@ -228,9 +228,9 @@ struct diag_dgram { char text[TEXTLEN]; } ; -extern sig_atomic_t diag_in_handler; -extern sig_atomic_t diag_immediate_msg; -extern sig_atomic_t diag_immediate_exit; +extern volatile sig_atomic_t diag_in_handler; +extern volatile sig_atomic_t diag_immediate_msg; +extern volatile sig_atomic_t diag_immediate_exit; extern void diag_set(char what, const char *arg); extern void diag_set_int(char what, int arg); diff --git a/socat.c b/socat.c index 9e7b34a..b2600b4 100644 --- a/socat.c +++ b/socat.c @@ -85,7 +85,6 @@ const char copyright_ssleay[] = "This product includes software written by Tim H bool havelock; - int main(int argc, const char *argv[]) { const char **arg1, *a; char *mainwaitstring;