Skip to content

Commit

Permalink
Avoid subthread signal handling
Browse files Browse the repository at this point in the history
  • Loading branch information
xTire committed Aug 26, 2024
1 parent 67ca2c8 commit d312ddc
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/iperf_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,8 @@ enum {
IEPTHREADCANCEL=151, // Unable to cancel thread (check perror)
IEPTHREADJOIN=152, // Unable to join thread (check perror)
IEPTHREADATTRINIT=153, // Unable to initialize thread attribute (check perror)
IEPTHREADATTRDESTROY=154, // Unable to destroy thread attribute (check perror)
IEPTHREADSIGMASK=154, // Unable to initialize sub thread signal mask (check perror)
IEPTHREADATTRDESTROY=155, // Unable to destroy thread attribute (check perror)
/* Stream errors */
IECREATESTREAM = 200, // Unable to create a new stream (check herror/perror)
IEINITSTREAM = 201, // Unable to initialize stream (check herror/perror)
Expand Down
13 changes: 13 additions & 0 deletions src/iperf_client_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <sys/select.h>
#include <sys/uio.h>
#include <arpa/inet.h>
#include <signal.h>

#include "iperf.h"
#include "iperf_api.h"
Expand Down Expand Up @@ -687,6 +688,18 @@ iperf_run_client(struct iperf_test * test)
goto cleanup_and_fail;
}

/* Block signals that handled by main thread, sub thread(s) will
* inherit a copy of the signal mask */
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGTERM);
sigaddset(&set, SIGHUP);
sigaddset(&set, SIGINT);
if (pthread_sigmask(SIG_BLOCK, &set, NULL) != 0) {
i_errno = IEPTHREADSIGMASK;
goto cleanup_and_fail;
}

SLIST_FOREACH(sp, &test->streams, streams) {
if (pthread_create(&(sp->thr), &attr, &iperf_client_worker_run, sp) != 0) {
i_errno = IEPTHREADCREATE;
Expand Down
3 changes: 3 additions & 0 deletions src/iperf_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,9 @@ iperf_strerror(int int_errno)
snprintf(errstr, len, "unable to create thread attributes");
perr = 1;
break;
case IEPTHREADSIGMASK:
snprintf(errstr, len, "unable to change mask of blocked signals");
break;
case IEPTHREADATTRDESTROY:
snprintf(errstr, len, "unable to destroy thread attributes");
perr = 1;
Expand Down
13 changes: 13 additions & 0 deletions src/iperf_server_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include <sys/resource.h>
#include <sched.h>
#include <setjmp.h>
#include <signal.h>

#include "iperf.h"
#include "iperf_api.h"
Expand Down Expand Up @@ -885,6 +886,18 @@ iperf_run_server(struct iperf_test *test)
cleanup_server(test);
};

/* Block signals that handled by main thread, sub thread(s) will
* inherit a copy of the signal mask */
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGTERM);
sigaddset(&set, SIGHUP);
sigaddset(&set, SIGINT);
if (pthread_sigmask(SIG_BLOCK, &set, NULL) != 0) {
i_errno = IEPTHREADSIGMASK;
cleanup_server(test);
}

SLIST_FOREACH(sp, &test->streams, streams) {
if (pthread_create(&(sp->thr), &attr, &iperf_server_worker_run, sp) != 0) {
i_errno = IEPTHREADCREATE;
Expand Down

0 comments on commit d312ddc

Please sign in to comment.