From 4bab9bc39d08069976c519868fefa11c35f6c3f0 Mon Sep 17 00:00:00 2001 From: DavidBar-On Date: Thu, 6 Feb 2025 16:17:05 +0200 Subject: [PATCH 1/3] Exit with 0 on SIGTERM --- src/iperf_api.c | 8 ++++++-- src/iperf_api.h | 4 +++- src/iperf_error.c | 24 +++++++++++++++++++++--- src/main.c | 4 +++- 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/iperf_api.c b/src/iperf_api.c index 7d5c9e381..2012f807e 100644 --- a/src/iperf_api.c +++ b/src/iperf_api.c @@ -4845,7 +4845,7 @@ iperf_catch_sigend(void (*handler)(int)) * before cleaning up and exiting. */ void -iperf_got_sigend(struct iperf_test *test) +iperf_got_sigend(struct iperf_test *test, int sig) { /* * If we're the client, or if we're a server and running a test, @@ -4868,7 +4868,11 @@ iperf_got_sigend(struct iperf_test *test) (void) Nwrite(test->ctrl_sck, (char*) &test->state, sizeof(signed char), Ptcp); } i_errno = (test->role == 'c') ? IECLIENTTERM : IESERVERTERM; - iperf_errexit(test, "interrupt - %s", iperf_strerror(i_errno)); + if (sig == SIGTERM) { + iperf_signormalexit(test, "interrupt - %s by signal %s(%d)", iperf_strerror(i_errno), strsignal(sig), sig); + } else { + iperf_errexit(test, "interrupt - %s by signal %s(%d)", iperf_strerror(i_errno), strsignal(sig), sig); + } } /* Try to write a PID file if requested, return -1 on an error. */ diff --git a/src/iperf_api.h b/src/iperf_api.h index 2b71613e9..47011f58f 100644 --- a/src/iperf_api.h +++ b/src/iperf_api.h @@ -326,7 +326,7 @@ void iperf_check_throttle(struct iperf_stream *sp, struct iperf_time *nowP); int iperf_send_mt(struct iperf_stream *) /* __attribute__((hot)) */; int iperf_recv_mt(struct iperf_stream *); void iperf_catch_sigend(void (*handler)(int)); -void iperf_got_sigend(struct iperf_test *test) __attribute__ ((noreturn)); +void iperf_got_sigend(struct iperf_test *test, int sig) __attribute__ ((noreturn)); void usage(void); void usage_long(FILE * f); void warning(const char *); @@ -380,6 +380,8 @@ int iflush(struct iperf_test *test); /* Error routines. */ void iperf_err(struct iperf_test *test, const char *format, ...) __attribute__ ((format(printf,2,3))); void iperf_errexit(struct iperf_test *test, const char *format, ...) __attribute__ ((format(printf,2,3),noreturn)); +void iperf_signormalexit(struct iperf_test *test, const char *format, ...) __attribute__ ((format(printf,2,3),noreturn)); +void iperf_exit(struct iperf_test *test, int exit_code, const char *format, va_list argp) __attribute__ ((noreturn)); char *iperf_strerror(int); extern int i_errno; enum { diff --git a/src/iperf_error.c b/src/iperf_error.c index fede216bc..dccf506be 100644 --- a/src/iperf_error.c +++ b/src/iperf_error.c @@ -85,11 +85,30 @@ iperf_err(struct iperf_test *test, const char *format, ...) va_end(argp); } -/* Do a printf to stderr or log file as appropriate, then exit. */ +/* Do a printf to stderr or log file as appropriate, then exit(0). */ +void +iperf_signormalexit(struct iperf_test *test, const char *format, ...) +{ + va_list argp; + + va_start(argp, format); + iperf_exit(test, 0, format, argp); +} + +/* Do a printf to stderr or log file as appropriate, then exit(1). */ void iperf_errexit(struct iperf_test *test, const char *format, ...) { va_list argp; + + va_start(argp, format); + iperf_exit(test, 1, format, argp); +} + +/* Do a printf to stderr or log file as appropriate, then exit. */ +void +iperf_exit(struct iperf_test *test, int exit_code, const char *format, va_list argp) +{ char str[1000]; time_t now; struct tm *ltm = NULL; @@ -103,7 +122,6 @@ iperf_errexit(struct iperf_test *test, const char *format, ...) ct = iperf_timestrerr; } - va_start(argp, format); vsnprintf(str, sizeof(str), format, argp); if (test != NULL && test->json_output) { if (test->json_top != NULL) { @@ -136,7 +154,7 @@ iperf_errexit(struct iperf_test *test, const char *format, ...) va_end(argp); if (test) iperf_delete_pidfile(test); - exit(1); + exit(exit_code); } int i_errno; diff --git a/src/main.c b/src/main.c index eb29f87ac..708fd4f4c 100644 --- a/src/main.c +++ b/src/main.c @@ -131,10 +131,12 @@ main(int argc, char **argv) static jmp_buf sigend_jmp_buf; +static int signed_sig; static void __attribute__ ((noreturn)) sigend_handler(int sig) { + signed_sig = sig; longjmp(sigend_jmp_buf, 1); } @@ -145,7 +147,7 @@ run(struct iperf_test *test) /* Termination signals. */ iperf_catch_sigend(sigend_handler); if (setjmp(sigend_jmp_buf)) - iperf_got_sigend(test); + iperf_got_sigend(test, signed_sig); /* Ignore SIGPIPE to simplify error handling */ signal(SIGPIPE, SIG_IGN); From 7e8a19f40af1af89e4b4dd9482a4a22b6f00d407 Mon Sep 17 00:00:00 2001 From: DavidBar-On Date: Sat, 8 Feb 2025 12:12:19 +0200 Subject: [PATCH 2/3] Per reviwer comment added SIGHUP to the non-error signals and added #idef to the signals names --- src/iperf_api.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/iperf_api.c b/src/iperf_api.c index 2012f807e..80a788b25 100644 --- a/src/iperf_api.c +++ b/src/iperf_api.c @@ -4847,6 +4847,8 @@ iperf_catch_sigend(void (*handler)(int)) void iperf_got_sigend(struct iperf_test *test, int sig) { + int exit_type; + /* * If we're the client, or if we're a server and running a test, * then dump out the accumulated stats so far. @@ -4868,7 +4870,18 @@ iperf_got_sigend(struct iperf_test *test, int sig) (void) Nwrite(test->ctrl_sck, (char*) &test->state, sizeof(signed char), Ptcp); } i_errno = (test->role == 'c') ? IECLIENTTERM : IESERVERTERM; - if (sig == SIGTERM) { + + exit_type = 0; +#ifdef SIGTERM + if (sig == SIGTERM) + exit_type = 1; + else +#endif +#ifdef SIGHUP + if (sig == SIGHUP) + exit_type = 1; +#endif + if (exit_type == 1) { iperf_signormalexit(test, "interrupt - %s by signal %s(%d)", iperf_strerror(i_errno), strsignal(sig), sig); } else { iperf_errexit(test, "interrupt - %s by signal %s(%d)", iperf_strerror(i_errno), strsignal(sig), sig); From 46af7e9c0fea764f197fd7bf5d6c612d07b12b48 Mon Sep 17 00:00:00 2001 From: DavidBar-On Date: Tue, 11 Feb 2025 17:41:42 +0200 Subject: [PATCH 3/3] Add also SIGINT to the non-error signals --- src/iperf_api.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/iperf_api.c b/src/iperf_api.c index 80a788b25..37fd165a2 100644 --- a/src/iperf_api.c +++ b/src/iperf_api.c @@ -4847,7 +4847,7 @@ iperf_catch_sigend(void (*handler)(int)) void iperf_got_sigend(struct iperf_test *test, int sig) { - int exit_type; + int exit_normal; /* * If we're the client, or if we're a server and running a test, @@ -4871,17 +4871,20 @@ iperf_got_sigend(struct iperf_test *test, int sig) } i_errno = (test->role == 'c') ? IECLIENTTERM : IESERVERTERM; - exit_type = 0; + exit_normal = 0; #ifdef SIGTERM if (sig == SIGTERM) - exit_type = 1; - else + exit_normal = 1; +#endif +#ifdef SIGINT + if (sig == SIGINT) + exit_normal = 1; #endif #ifdef SIGHUP if (sig == SIGHUP) - exit_type = 1; + exit_normal = 1; #endif - if (exit_type == 1) { + if (exit_normal) { iperf_signormalexit(test, "interrupt - %s by signal %s(%d)", iperf_strerror(i_errno), strsignal(sig), sig); } else { iperf_errexit(test, "interrupt - %s by signal %s(%d)", iperf_strerror(i_errno), strsignal(sig), sig);