Fix signal handling in sesman

This commit is contained in:
matt335672 2022-03-03 15:37:46 +00:00
parent 2484928a5a
commit 8bd597a038
5 changed files with 195 additions and 191 deletions

View File

@ -52,11 +52,11 @@ struct sesman_startup_params
int dump_config;
};
int g_pid;
struct config_sesman *g_cfg;
unsigned char g_fixedkey[8] = { 23, 82, 107, 6, 35, 78, 88, 7 };
struct config_sesman *g_cfg; /* defined in config.h */
tintptr g_term_event = 0;
tintptr g_sigchld_event = 0;
tintptr g_reload_event = 0;
/**
* Items stored on the g_con_list
@ -67,8 +67,9 @@ struct sesman_con
struct SCP_SESSION *s;
};
static struct trans *g_list_trans = NULL;
static struct trans *g_list_trans;
static struct list *g_con_list = NULL;
static int g_pid;
/*****************************************************************************/
/**
@ -263,7 +264,7 @@ sesman_close_all(void)
struct sesman_con *sc;
LOG_DEVEL(LOG_LEVEL_TRACE, "sesman_close_all:");
trans_delete(g_list_trans);
sesman_delete_listening_transport();
for (index = 0; index < g_con_list->count; index++)
{
sc = (struct sesman_con *) list_get_item(g_con_list, index);
@ -340,6 +341,86 @@ sesman_listen_conn_in(struct trans *self, struct trans *new_self)
return 0;
}
/******************************************************************************/
/**
* Informs the main loop a termination signal has been received
*/
static void
set_term_event(int sig)
{
/* Don't try to use a wait obj in a child process */
if (g_getpid() == g_pid)
{
g_set_wait_obj(g_term_event);
}
}
/******************************************************************************/
/**
* Informs the main loop a SIGCHLD has been received
*/
static void
set_sigchld_event(int sig)
{
/* Don't try to use a wait obj in a child process */
if (g_getpid() == g_pid)
{
g_set_wait_obj(g_sigchld_event);
}
}
/******************************************************************************/
/**
* Informs the main loop a SIGHUP has been received
*/
static void
set_reload_event(int sig)
{
/* Don't try to use a wait obj in a child process */
if (g_getpid() == g_pid)
{
g_set_wait_obj(g_reload_event);
}
}
/******************************************************************************/
void
sesman_delete_listening_transport(void)
{
trans_delete(g_list_trans);
g_list_trans = NULL;
}
/******************************************************************************/
int
sesman_create_listening_transport(const struct config_sesman *cfg)
{
int rv = 1;
g_list_trans = trans_create(TRANS_MODE_TCP, 8192, 8192);
if (g_list_trans == NULL)
{
LOG(LOG_LEVEL_ERROR, "%s: trans_create failed", __func__);
}
else
{
LOG(LOG_LEVEL_DEBUG, "%s: address %s port %s",
__func__, cfg->listen_address, cfg->listen_port);
rv = trans_listen_address(g_list_trans, cfg->listen_port,
cfg->listen_address);
if (rv != 0)
{
LOG(LOG_LEVEL_ERROR, "%s: trans_listen_address failed", __func__);
sesman_delete_listening_transport();
}
else
{
g_list_trans->trans_conn_in = sesman_listen_conn_in;
}
}
return rv;
}
/******************************************************************************/
/**
*
@ -352,7 +433,6 @@ sesman_main_loop(void)
int error;
int robjs_count;
int wobjs_count;
int cont;
int timeout;
int index;
intptr_t robjs[32];
@ -365,33 +445,22 @@ sesman_main_loop(void)
LOG(LOG_LEVEL_ERROR, "sesman_main_loop: list_create failed");
return 1;
}
g_list_trans = trans_create(TRANS_MODE_TCP, 8192, 8192);
if (g_list_trans == NULL)
if (sesman_create_listening_transport(g_cfg) != 0)
{
LOG(LOG_LEVEL_ERROR, "sesman_main_loop: trans_create failed");
LOG(LOG_LEVEL_ERROR,
"sesman_main_loop: sesman_create_listening_transport failed");
list_delete(g_con_list);
return 1;
}
LOG(LOG_LEVEL_DEBUG, "sesman_main_loop: address %s port %s",
g_cfg->listen_address, g_cfg->listen_port);
error = trans_listen_address(g_list_trans, g_cfg->listen_port,
g_cfg->listen_address);
if (error != 0)
{
LOG(LOG_LEVEL_ERROR, "sesman_main_loop: trans_listen_address "
"failed");
trans_delete(g_list_trans);
list_delete(g_con_list);
return 1;
}
g_list_trans->trans_conn_in = sesman_listen_conn_in;
cont = 1;
while (cont)
error = 0;
while (!error)
{
timeout = -1;
robjs_count = 0;
robjs[robjs_count++] = g_term_event;
robjs[robjs_count++] = g_sigchld_event;
robjs[robjs_count++] = g_reload_event;
wobjs_count = 0;
for (index = 0; index < g_con_list->count; index++)
{
@ -413,6 +482,10 @@ sesman_main_loop(void)
{
break;
}
if (g_list_trans != NULL)
{
/* g_list_trans might be NULL on a reconfigure if sesman
* is unable to listen again */
error = trans_get_wait_objs_rw(g_list_trans, robjs, &robjs_count,
wobjs, &wobjs_count, &timeout);
if (error != 0)
@ -421,26 +494,41 @@ sesman_main_loop(void)
"trans_get_wait_objs_rw failed");
break;
}
}
error = g_obj_wait(robjs, robjs_count, wobjs, wobjs_count, timeout);
if (error != 0)
if (g_obj_wait(robjs, robjs_count, wobjs, wobjs_count, timeout) != 0)
{
/* error, should not get here */
/* should not get here */
LOG(LOG_LEVEL_WARNING, "sesman_main_loop: "
"Unexpected error from g_obj_wait()");
g_sleep(100);
}
if (g_is_wait_obj_set(g_term_event)) /* term */
{
LOG(LOG_LEVEL_INFO, "sesman_main_loop: "
"sesman asked to terminate");
break;
}
if (g_is_wait_obj_set(g_sigchld_event)) /* A child has exited */
{
g_reset_wait_obj(g_sigchld_event);
sig_sesman_session_end();
}
if (g_is_wait_obj_set(g_reload_event)) /* We're asked to reload */
{
g_reset_wait_obj(g_reload_event);
sig_sesman_reload_cfg();
}
for (index = 0; index < g_con_list->count; index++)
{
scon = (struct sesman_con *)list_get_item(g_con_list, index);
if (scon != NULL)
{
error = trans_check_wait_objs(scon->t);
if (error != 0)
if (trans_check_wait_objs(scon->t) != 0)
{
LOG(LOG_LEVEL_ERROR, "sesman_main_loop: "
"trans_check_wait_objs failed, removing trans");
@ -451,6 +539,9 @@ sesman_main_loop(void)
}
}
}
if (g_list_trans != NULL)
{
error = trans_check_wait_objs(g_list_trans);
if (error != 0)
{
@ -459,13 +550,14 @@ sesman_main_loop(void)
break;
}
}
}
for (index = 0; index < g_con_list->count; index++)
{
scon = (struct sesman_con *) list_get_item(g_con_list, index);
delete_connection(scon);
}
list_delete(g_con_list);
trans_delete(g_list_trans);
sesman_delete_listening_transport();
return 0;
}
@ -710,20 +802,17 @@ main(int argc, char **argv)
/* signal handling */
g_pid = g_getpid();
/* old style signal handling is now managed synchronously by a
* separate thread. uncomment this block if you need old style
* signal handling and comment out thread_sighandler_start()
* going back to old style for the time being
* problem with the sigaddset functions in sig.c - jts */
#if 1
g_signal_hang_up(sig_sesman_reload_cfg); /* SIGHUP */
g_signal_user_interrupt(sig_sesman_shutdown); /* SIGINT */
g_signal_terminate(sig_sesman_shutdown); /* SIGTERM */
g_signal_child_stop(sig_sesman_session_end); /* SIGCHLD */
#endif
#if 0
thread_sighandler_start();
#endif
g_snprintf(text, 255, "xrdp_sesman_%8.8x_main_term", g_pid);
g_term_event = g_create_wait_obj(text);
g_snprintf(text, 255, "xrdp_sesman_%8.8x_sigchld", g_pid);
g_sigchld_event = g_create_wait_obj(text);
g_snprintf(text, 255, "xrdp_sesman_%8.8x_reload", g_pid);
g_reload_event = g_create_wait_obj(text);
g_signal_hang_up(set_reload_event); /* SIGHUP */
g_signal_user_interrupt(set_term_event); /* SIGINT */
g_signal_terminate(set_term_event); /* SIGTERM */
g_signal_child_stop(set_sigchld_event); /* SIGCHLD */
if (daemon)
{
@ -765,9 +854,6 @@ main(int argc, char **argv)
g_chmod_hex("/tmp/.X11-unix", 0x1777);
}
g_snprintf(text, 255, "xrdp_sesman_%8.8x_main_term", g_pid);
g_term_event = g_create_wait_obj(text);
error = sesman_main_loop();
/* clean up PID file on exit */
@ -776,6 +862,8 @@ main(int argc, char **argv)
g_file_delete(pid_file);
}
g_delete_wait_obj(g_reload_event);
g_delete_wait_obj(g_sigchld_event);
g_delete_wait_obj(g_term_event);
if (!daemon)

View File

@ -45,8 +45,8 @@
extern struct config_sesman *g_cfg;
extern unsigned char g_fixedkey[8];
extern tintptr g_term_event;
extern int g_pid;
extern tintptr g_sigchld_event;
extern tintptr g_reload_event;
/*
* Close all file descriptors used by sesman.
@ -60,4 +60,20 @@ extern int g_pid;
int
sesman_close_all(void);
/*
* Remove the listening transport
*
* Needed if reloading the config and the listener has changed
*/
void
sesman_delete_listening_transport(void);
/*
* Create the listening socket transport
*
* @return 0 for success
*/
int
sesman_create_listening_transport(const struct config_sesman *cfg);
#endif

View File

@ -47,10 +47,8 @@
#define PR_SET_NO_NEW_PRIVS 38
#endif
struct session_chain *g_sessions;
int g_session_count;
extern tbus g_term_event; /* in sesman.c */
static struct session_chain *g_sessions;
static int g_session_count;
/**
* Creates a string consisting of all parameters that is hosted in the param list
@ -522,8 +520,13 @@ session_start_fork(tbus data, tui8 type, struct SCP_SESSION *s)
"Failed to clone the session data - out of memory");
g_exit(1);
}
auth_start_session(data, display);
/* Wait objects created in a parent are not valid in a child */
g_delete_wait_obj(g_reload_event);
g_delete_wait_obj(g_sigchld_event);
g_delete_wait_obj(g_term_event);
auth_start_session(data, display);
sesman_close_all();
g_sprintf(geometry, "%dx%d", s->width, s->height);
g_sprintf(depth, "%d", s->bpp);

View File

@ -28,48 +28,17 @@
#include <config_ac.h>
#endif
#include <signal.h>
#include "string_calls.h"
#include "sesman.h"
/******************************************************************************/
void
sig_sesman_shutdown(int sig)
{
char pid_file[256];
LOG(LOG_LEVEL_INFO, "shutting down sesman %d", 1);
if (g_getpid() != g_pid)
{
LOG_DEVEL(LOG_LEVEL_DEBUG, "g_getpid() [%d] differs from g_pid [%d]", (g_getpid()), g_pid);
return;
}
LOG_DEVEL(LOG_LEVEL_DEBUG, " - getting signal %d pid %d", sig, g_getpid());
g_set_wait_obj(g_term_event);
session_sigkill_all();
g_snprintf(pid_file, 255, "%s/xrdp-sesman.pid", XRDP_PID_PATH);
g_file_delete(pid_file);
}
/******************************************************************************/
void
sig_sesman_reload_cfg(int sig)
sig_sesman_reload_cfg(void)
{
int error;
struct config_sesman *cfg;
LOG(LOG_LEVEL_WARNING, "receiving SIGHUP %d", 1);
if (g_getpid() != g_pid)
{
LOG_DEVEL(LOG_LEVEL_DEBUG, "g_getpid() [%d] differs from g_pid [%d]", g_getpid(), g_pid);
return;
}
LOG(LOG_LEVEL_INFO, "receiving SIGHUP");
if ((cfg = config_read(g_cfg->sesman_ini)) == NULL)
{
@ -77,8 +46,18 @@ sig_sesman_reload_cfg(int sig)
return;
}
/* stop logging subsystem */
log_end();
/* Deal with significant config changes */
if (g_strcmp(g_cfg->listen_address, cfg->listen_address) != 0 ||
g_strcmp(g_cfg->listen_port, cfg->listen_port) != 0)
{
LOG(LOG_LEVEL_INFO, "sesman listen address changed to %s:%s",
cfg->listen_address, cfg->listen_port);
/* We have to delete the old port before listening to the new one
* in case they overlap in scope */
sesman_delete_listening_transport();
sesman_create_listening_transport(cfg);
}
/* free old config data */
config_free(g_cfg);
@ -86,8 +65,8 @@ sig_sesman_reload_cfg(int sig)
/* replace old config with newly read one */
g_cfg = cfg;
/* start again logging subsystem */
error = log_start(g_cfg->sesman_ini, "xrdp-sesman", 0);
/* Restart logging subsystem */
error = log_start(g_cfg->sesman_ini, "xrdp-sesman", LOG_START_RESTART);
if (error != LOG_STARTUP_OK)
{
@ -109,84 +88,21 @@ sig_sesman_reload_cfg(int sig)
/******************************************************************************/
void
sig_sesman_session_end(int sig)
sig_sesman_session_end(void)
{
int pid;
if (g_getpid() != g_pid)
{
return;
}
LOG(LOG_LEVEL_DEBUG, "receiving SIGCHLD");
do
{
pid = g_waitchild();
if (pid > 0)
{
LOG(LOG_LEVEL_INFO, "Process %d has exited", pid);
session_kill(pid);
}
}
while (pid > 0);
}
/******************************************************************************/
void *
sig_handler_thread(void *arg)
{
int recv_signal;
sigset_t sigmask;
sigset_t oldmask;
sigset_t waitmask;
/* mask signals to be able to wait for them... */
sigfillset(&sigmask);
/* it is a good idea not to block SIGILL SIGSEGV */
/* SIGFPE -- see sigaction(2) NOTES */
pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask);
/* building the signal wait mask... */
sigemptyset(&waitmask);
sigaddset(&waitmask, SIGHUP);
sigaddset(&waitmask, SIGCHLD);
sigaddset(&waitmask, SIGTERM);
sigaddset(&waitmask, SIGINT);
// sigaddset(&waitmask, SIGFPE);
// sigaddset(&waitmask, SIGILL);
// sigaddset(&waitmask, SIGSEGV);
do
{
LOG_DEVEL(LOG_LEVEL_DEBUG, "calling sigwait()");
sigwait(&waitmask, &recv_signal);
switch (recv_signal)
{
case SIGHUP:
//reload cfg
//we must stop & restart logging, or copy logging cfg!!!!
LOG_DEVEL(LOG_LEVEL_DEBUG, "sesman received SIGHUP");
//return 0;
break;
case SIGCHLD:
/* a session died */
LOG_DEVEL(LOG_LEVEL_DEBUG, "sesman received SIGCHLD");
sig_sesman_session_end(SIGCHLD);
break;
case SIGINT:
/* we die */
LOG_DEVEL(LOG_LEVEL_DEBUG, "sesman received SIGINT");
sig_sesman_shutdown(recv_signal);
break;
case SIGTERM:
/* we die */
LOG_DEVEL(LOG_LEVEL_DEBUG, "sesman received SIGTERM");
sig_sesman_shutdown(recv_signal);
break;
}
}
while (1);
return 0;
}

View File

@ -27,39 +27,20 @@
#ifndef SIG_H
#define SIG_H
/**
*
* @brief Shutdown signal code
* @param sig The received signal
*
*/
void
sig_sesman_shutdown(int sig);
/**
*
* @brief SIGHUP handling code
* @param sig The received signal
*
*/
void
sig_sesman_reload_cfg(int sig);
sig_sesman_reload_cfg(void);
/**
*
* @brief SIGCHLD handling code
* @param sig The received signal
*
*/
void
sig_sesman_session_end(int sig);
/**
*
* @brief signal handling thread
*
*/
void *
sig_handler_thread(void *arg);
sig_sesman_session_end(void);
#endif