2020-07-26 07:58:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
#include "sighandler.h"
|
2020-11-27 18:03:42 +00:00
|
|
|
#include "system.h"
|
2020-07-26 07:58:15 +00:00
|
|
|
|
|
|
|
static inline od_retcode_t
|
|
|
|
od_system_gracefully_killer_invoke(od_system_t *system)
|
|
|
|
{
|
|
|
|
od_instance_t *instance = system->global->instance;
|
2021-03-05 09:29:30 +00:00
|
|
|
if (instance->shutdown_worker_id != INVALID_COROUTINE_ID) {
|
2020-07-26 07:58:15 +00:00
|
|
|
return OK_RESPONSE;
|
|
|
|
}
|
|
|
|
int64_t mid;
|
|
|
|
mid = machine_create("shutdowner", od_grac_shutdown_worker, system);
|
|
|
|
if (mid == -1) {
|
2020-12-28 10:43:31 +00:00
|
|
|
od_error(&instance->logger, "gracefully_killer", NULL, NULL,
|
|
|
|
"failed to invoke gracefully killer coroutine");
|
2020-07-26 07:58:15 +00:00
|
|
|
return NOT_OK_RESPONSE;
|
|
|
|
}
|
2020-09-01 12:05:14 +00:00
|
|
|
instance->shutdown_worker_id = mid;
|
2020-07-26 07:58:15 +00:00
|
|
|
|
|
|
|
return OK_RESPONSE;
|
|
|
|
}
|
|
|
|
|
2020-12-28 10:43:31 +00:00
|
|
|
static inline void od_system_cleanup(od_system_t *system)
|
2020-07-26 07:58:15 +00:00
|
|
|
{
|
|
|
|
od_instance_t *instance = system->global->instance;
|
|
|
|
|
|
|
|
od_list_t *i;
|
|
|
|
od_list_foreach(&instance->config.listen, i)
|
|
|
|
{
|
|
|
|
od_config_listen_t *listen;
|
|
|
|
listen = od_container_of(i, od_config_listen_t, link);
|
|
|
|
if (listen->host)
|
|
|
|
continue;
|
|
|
|
/* remove unix socket files */
|
|
|
|
char path[PATH_MAX];
|
2020-12-28 10:43:31 +00:00
|
|
|
od_snprintf(path, sizeof(path), "%s/.s.PGSQL.%d",
|
|
|
|
instance->config.unix_socket_dir, listen->port);
|
2020-07-26 07:58:15 +00:00
|
|
|
unlink(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-25 10:17:15 +00:00
|
|
|
od_attribute_noreturn() void od_system_shutdown(od_system_t *system,
|
2020-12-28 10:43:31 +00:00
|
|
|
od_instance_t *instance)
|
2020-11-23 09:13:28 +00:00
|
|
|
{
|
2020-12-28 10:43:31 +00:00
|
|
|
od_log(&instance->logger, "system", NULL, NULL,
|
2020-11-23 09:13:28 +00:00
|
|
|
"SIGINT received, shutting down");
|
2021-03-05 09:29:30 +00:00
|
|
|
|
|
|
|
// lock here
|
|
|
|
od_cron_stop(system->global->cron);
|
|
|
|
|
2020-11-23 09:13:28 +00:00
|
|
|
od_worker_pool_stop(system->global->worker_pool);
|
|
|
|
od_router_free(system->global->router);
|
|
|
|
/* Prevent OpenSSL usage during deinitialization */
|
|
|
|
od_worker_pool_wait();
|
2021-04-21 13:11:18 +00:00
|
|
|
|
|
|
|
od_extention_free(&instance->logger, system->global->extentions);
|
|
|
|
|
2020-11-23 09:13:28 +00:00
|
|
|
od_system_cleanup(system);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2020-12-28 10:43:31 +00:00
|
|
|
void od_system_signal_handler(void *arg)
|
2020-07-26 07:58:15 +00:00
|
|
|
{
|
2020-12-28 10:43:31 +00:00
|
|
|
od_system_t *system = arg;
|
2020-07-26 07:58:15 +00:00
|
|
|
od_instance_t *instance = system->global->instance;
|
|
|
|
|
|
|
|
sigset_t mask;
|
|
|
|
sigemptyset(&mask);
|
|
|
|
sigaddset(&mask, SIGINT);
|
|
|
|
sigaddset(&mask, SIGTERM);
|
2020-12-17 12:27:29 +00:00
|
|
|
sigaddset(&mask, SIGHUP);
|
2020-10-17 16:01:57 +00:00
|
|
|
sigaddset(&mask, OD_SIG_LOG_ROTATE);
|
2020-07-26 07:58:15 +00:00
|
|
|
sigaddset(&mask, OD_SIG_GRACEFUL_SHUTDOWN);
|
|
|
|
|
|
|
|
sigset_t ignore_mask;
|
|
|
|
sigemptyset(&ignore_mask);
|
|
|
|
sigaddset(&mask, SIGPIPE);
|
|
|
|
int rc;
|
|
|
|
rc = machine_signal_init(&mask, &ignore_mask);
|
|
|
|
if (rc == -1) {
|
2020-12-28 10:43:31 +00:00
|
|
|
od_error(&instance->logger, "system", NULL, NULL,
|
|
|
|
"failed to init signal handler");
|
2020-07-26 07:58:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (;;) {
|
|
|
|
rc = machine_signal_wait(UINT32_MAX);
|
|
|
|
if (rc == -1)
|
|
|
|
break;
|
|
|
|
switch (rc) {
|
2020-12-28 10:43:31 +00:00
|
|
|
case SIGTERM:
|
|
|
|
case SIGINT:
|
|
|
|
od_system_shutdown(system, instance);
|
|
|
|
break;
|
|
|
|
case SIGHUP:
|
|
|
|
od_log(&instance->logger, "system", NULL, NULL,
|
|
|
|
"SIGHUP received");
|
|
|
|
od_system_config_reload(system);
|
|
|
|
break;
|
|
|
|
case OD_SIG_LOG_ROTATE:
|
|
|
|
if (instance->config.log_file) {
|
|
|
|
od_log(&instance->logger, "system", NULL, NULL,
|
|
|
|
"SIGUSR1 received, reopening log");
|
|
|
|
rc = od_logger_reopen(
|
|
|
|
&instance->logger,
|
|
|
|
instance->config.log_file);
|
|
|
|
if (rc == -1) {
|
|
|
|
od_error(
|
|
|
|
&instance->logger, "system",
|
|
|
|
NULL, NULL,
|
|
|
|
"failed to reopen log file '%s'",
|
|
|
|
instance->config.log_file);
|
2020-07-26 07:58:15 +00:00
|
|
|
}
|
2020-12-28 10:43:31 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case OD_SIG_GRACEFUL_SHUTDOWN:
|
|
|
|
if (instance->config.enable_online_restart_feature ||
|
|
|
|
instance->config.graceful_die_on_errors) {
|
|
|
|
od_log(&instance->logger, "system", NULL, NULL,
|
|
|
|
"SIG_GRACEFUL_SHUTDOWN received");
|
|
|
|
od_system_gracefully_killer_invoke(system);
|
|
|
|
} else {
|
|
|
|
od_log(&instance->logger, "system", NULL, NULL,
|
|
|
|
"SIGUSR2 received, but online restart feature not "
|
|
|
|
"enabled, doing nothing");
|
|
|
|
}
|
|
|
|
break;
|
2020-07-26 07:58:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|