From 30b512c201f2f477fc2a9e9abbc5921532c54752 Mon Sep 17 00:00:00 2001 From: Ben Walker Date: Fri, 21 Jul 2017 10:33:41 -0700 Subject: [PATCH] event: Move signal handler setup to separate function Just code movement, no changes. Change-Id: Ie4eba079a6a9eae7632c784985cccb9bd18cd3a6 Signed-off-by: Ben Walker Reviewed-on: https://review.gerrithub.io/370757 Tested-by: SPDK Automated Test System Reviewed-by: Daniel Verkamp Reviewed-by: Jim Harris --- lib/event/app.c | 107 ++++++++++++++++++++++++++---------------------- 1 file changed, 58 insertions(+), 49 deletions(-) diff --git a/lib/event/app.c b/lib/event/app.c index 07e3cb172e..2929397257 100644 --- a/lib/event/app.c +++ b/lib/event/app.c @@ -188,14 +188,69 @@ spdk_app_opts_init(struct spdk_app_opts *opts) opts->max_delay_us = 0; } +static int +spdk_app_setup_signal_handlers(struct spdk_app_opts *opts) +{ + struct sigaction sigact; + sigset_t sigmask; + int rc; + + /* Set up custom shutdown handling if the user requested it. */ + if (opts->shutdown_cb != NULL) { + g_shutdown_event = spdk_event_allocate(spdk_env_get_current_core(), + __shutdown_event_cb, + NULL, NULL); + } + + sigemptyset(&sigmask); + memset(&sigact, 0, sizeof(sigact)); + sigemptyset(&sigact.sa_mask); + + sigact.sa_handler = SIG_IGN; + rc = sigaction(SIGPIPE, &sigact, NULL); + if (rc < 0) { + SPDK_ERRLOG("sigaction(SIGPIPE) failed\n"); + return rc; + } + + /* Install the same handler for SIGINT and SIGTERM */ + sigact.sa_handler = __shutdown_signal; + + rc = sigaction(SIGINT, &sigact, NULL); + if (rc < 0) { + SPDK_ERRLOG("sigaction(SIGINT) failed\n"); + return rc; + } + sigaddset(&sigmask, SIGINT); + + rc = sigaction(SIGTERM, &sigact, NULL); + if (rc < 0) { + SPDK_ERRLOG("sigaction(SIGTERM) failed\n"); + return rc; + } + sigaddset(&sigmask, SIGTERM); + + if (opts->usr1_handler != NULL) { + sigact.sa_handler = opts->usr1_handler; + rc = sigaction(SIGUSR1, &sigact, NULL); + if (rc < 0) { + SPDK_ERRLOG("sigaction(SIGUSR1) failed\n"); + return rc; + } + sigaddset(&sigmask, SIGUSR1); + } + + pthread_sigmask(SIG_UNBLOCK, &sigmask, NULL); + + return 0; +} + int spdk_app_start(struct spdk_app_opts *opts, spdk_event_fn start_fn, void *arg1, void *arg2) { struct spdk_conf *config; struct spdk_conf_section *sp; - struct sigaction sigact; - sigset_t sigmask; char shm_name[64]; int rc; uint64_t tpoint_group_mask; @@ -283,57 +338,11 @@ spdk_app_start(struct spdk_app_opts *opts, spdk_event_fn start_fn, exit(EXIT_FAILURE); } - /* Set up custom shutdown handling if the user requested it. */ - if (opts->shutdown_cb != NULL) { - g_shutdown_event = spdk_event_allocate(spdk_env_get_current_core(), - __shutdown_event_cb, - NULL, NULL); - } - - sigemptyset(&sigmask); - memset(&sigact, 0, sizeof(sigact)); - sigemptyset(&sigact.sa_mask); - - sigact.sa_handler = SIG_IGN; - rc = sigaction(SIGPIPE, &sigact, NULL); - if (rc < 0) { - SPDK_ERRLOG("sigaction(SIGPIPE) failed\n"); + if (spdk_app_setup_signal_handlers(opts)) { spdk_conf_free(g_spdk_app.config); exit(EXIT_FAILURE); } - /* Install the same handler for SIGINT and SIGTERM */ - sigact.sa_handler = __shutdown_signal; - - rc = sigaction(SIGINT, &sigact, NULL); - if (rc < 0) { - SPDK_ERRLOG("sigaction(SIGINT) failed\n"); - spdk_conf_free(g_spdk_app.config); - exit(EXIT_FAILURE); - } - sigaddset(&sigmask, SIGINT); - - rc = sigaction(SIGTERM, &sigact, NULL); - if (rc < 0) { - SPDK_ERRLOG("sigaction(SIGTERM) failed\n"); - spdk_conf_free(g_spdk_app.config); - exit(EXIT_FAILURE); - } - sigaddset(&sigmask, SIGTERM); - - if (opts->usr1_handler != NULL) { - sigact.sa_handler = opts->usr1_handler; - rc = sigaction(SIGUSR1, &sigact, NULL); - if (rc < 0) { - SPDK_ERRLOG("sigaction(SIGUSR1) failed\n"); - spdk_conf_free(g_spdk_app.config); - exit(EXIT_FAILURE); - } - sigaddset(&sigmask, SIGUSR1); - } - - pthread_sigmask(SIG_UNBLOCK, &sigmask, NULL); - if (opts->shm_id >= 0) { snprintf(shm_name, sizeof(shm_name), "/%s_trace.%d", opts->name, opts->shm_id); } else {