Skip to content

Commit

Permalink
Merge pull request #1829 from davidBar-On/issue-1009-exit-code-0-on-s…
Browse files Browse the repository at this point in the history
…igterm

Exit with 0 on SIGTERM/SIGHUP/SIGINT
  • Loading branch information
bmah888 authored Feb 21, 2025
2 parents 6b69d06 + 46af7e9 commit 7b2af28
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
24 changes: 22 additions & 2 deletions src/iperf_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -4874,8 +4874,10 @@ 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)
{
int exit_normal;

/*
* If we're the client, or if we're a server and running a test,
* then dump out the accumulated stats so far.
Expand All @@ -4897,7 +4899,25 @@ 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));

exit_normal = 0;
#ifdef SIGTERM
if (sig == SIGTERM)
exit_normal = 1;
#endif
#ifdef SIGINT
if (sig == SIGINT)
exit_normal = 1;
#endif
#ifdef SIGHUP
if (sig == SIGHUP)
exit_normal = 1;
#endif
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);
}
}

/* Try to write a PID file if requested, return -1 on an error. */
Expand Down
4 changes: 3 additions & 1 deletion src/iperf_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,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 *);
Expand Down Expand Up @@ -381,6 +381,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 {
Expand Down
24 changes: 21 additions & 3 deletions src/iperf_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
Expand Down

0 comments on commit 7b2af28

Please sign in to comment.