diff options
author | matt335672 <30179339+matt335672@users.noreply.github.com> | 2022-11-08 14:18:34 +0300 |
---|---|---|
committer | matt335672 <30179339+matt335672@users.noreply.github.com> | 2022-11-09 12:46:36 +0300 |
commit | 3a0a93247279ed751a433b6d235ee27d8cededda (patch) | |
tree | e9e71d815927d1c8c016c1bd0a631c0a58ade97c | |
parent | 4ff968bc980f6ff72c2e8312ce98ed67c2f6f9d8 (diff) |
Add --reload option to sesman
Adds a --reload switch to sesman and plumbs this in
to systemctl reload xrdp-sesman.service
-rw-r--r-- | common/os_calls.c | 12 | ||||
-rw-r--r-- | common/os_calls.h | 1 | ||||
-rw-r--r-- | docs/man/xrdp-sesman.8.in | 11 | ||||
-rw-r--r-- | instfiles/xrdp-sesman.service.in | 1 | ||||
-rw-r--r-- | sesman/sesman.c | 113 |
5 files changed, 101 insertions, 37 deletions
diff --git a/common/os_calls.c b/common/os_calls.c index c29ac257..91646423 100644 --- a/common/os_calls.c +++ b/common/os_calls.c @@ -3044,6 +3044,18 @@ g_sigterm(int pid) } /*****************************************************************************/ +/* does not work in win32 */ +int +g_sighup(int pid) +{ +#if defined(_WIN32) + return 0; +#else + return kill(pid, SIGHUP); +#endif +} + +/*****************************************************************************/ /* returns 0 if ok */ /* the caller is responsible to free the buffs */ /* does not work in win32 */ diff --git a/common/os_calls.h b/common/os_calls.h index 5719606e..eb54aff3 100644 --- a/common/os_calls.h +++ b/common/os_calls.h @@ -192,6 +192,7 @@ char *g_getenv(const char *name); int g_exit(int exit_code); int g_getpid(void); int g_sigterm(int pid); +int g_sighup(int pid); int g_getuser_info(const char *username, int *gid, int *uid, char **shell, char **dir, char **gecos); int g_getgroup_info(const char *groupname, int *gid); diff --git a/docs/man/xrdp-sesman.8.in b/docs/man/xrdp-sesman.8.in index 08e30ecb..e676548e 100644 --- a/docs/man/xrdp-sesman.8.in +++ b/docs/man/xrdp-sesman.8.in @@ -7,6 +7,9 @@ xrdp\-sesman \- \fBxrdp\fR(8) session manager \-\-kill .br .B xrdp\-sesman +\-\-reload +.br +.B xrdp\-sesman \-\-help .br .B xrdp\-sesman @@ -25,6 +28,9 @@ It manages user sessions by authenticating the user and starting the appropriate \fB\-k\fR, \fB\-\-kill\fR Kills running \fBxrdp\-sesman\fR daemon. .TP +\fB\-r\fR, \fB\-\-reload\fR +Reloads running \fBxrdp\-sesman\fR daemon. +.TP \fB\-h\fR, \fB\-\-help\fR Output help information and exit. .TP @@ -48,6 +54,11 @@ If you use this option, be aware that you will have to have a the system (notably \fBxrdp(8)\fR and \fBxrdp\-chansrv(8)\fR) will want to read it. .RE +.SH "SIGNALS" +.TP +\fBSIGHUP\fR +Causes \fBxrdp\-sesman\fR to reload its configuration. Needed if you're +not running \fBxrdp\-sesman\fR as a daemon. .SH "FILES" @sbindir@/xrdp\-sesman .br diff --git a/instfiles/xrdp-sesman.service.in b/instfiles/xrdp-sesman.service.in index 8f754d60..fcd74094 100644 --- a/instfiles/xrdp-sesman.service.in +++ b/instfiles/xrdp-sesman.service.in @@ -12,6 +12,7 @@ EnvironmentFile=-@sysconfdir@/sysconfig/xrdp EnvironmentFile=-@sysconfdir@/default/xrdp ExecStart=@sbindir@/xrdp-sesman $SESMAN_OPTIONS ExecStop=@sbindir@/xrdp-sesman $SESMAN_OPTIONS --kill +ExecReload=@sbindir@/xrdp-sesman $SESMAN_OPTIONS --reload [Install] WantedBy=multi-user.target diff --git a/sesman/sesman.c b/sesman/sesman.c index 72afbe8e..0e0c6bcb 100644 --- a/sesman/sesman.c +++ b/sesman/sesman.c @@ -46,10 +46,20 @@ */ #define MAX_SHORT_LIVED_CONNECTIONS 16 +/** + * Define the mode of operation of the program + */ +enum sesman_mode +{ + SSM_NORMAL = 0, + SSM_KILL_DAEMON, + SSM_RELOAD_DAEMON +}; + struct sesman_startup_params { const char *sesman_ini; - int kill; + enum sesman_mode mode; int no_daemon; int help; int version; @@ -158,6 +168,8 @@ sesman_process_params(int argc, char **argv, const char *option; const char *value; + startup_params->mode = SSM_NORMAL; + index = 1; while (index < argc) @@ -179,7 +191,11 @@ sesman_process_params(int argc, char **argv, } else if (nocase_matches(option, "-kill", "--kill", "-k", NULL)) { - startup_params->kill = 1; + startup_params->mode = SSM_KILL_DAEMON; + } + else if (nocase_matches(option, "-reload", "--reload", "-r", NULL)) + { + startup_params->mode = SSM_RELOAD_DAEMON; } else if (nocase_matches(option, "-nodaemon", "--nodaemon", "-n", "-nd", "--nd", "-ns", "--ns", NULL)) @@ -594,6 +610,7 @@ print_help(void) { g_printf("Usage: xrdp-sesman [options]\n"); g_printf(" -k, --kill shut down xrdp-sesman\n"); + g_printf(" -r, --reload reload xrdp-sesman\n"); g_printf(" -h, --help show help\n"); g_printf(" -v, --version show version\n"); g_printf(" -n, --nodaemon don't fork into background\n"); @@ -603,57 +620,44 @@ print_help(void) } /******************************************************************************/ +/** + * Reads the PID file + */ static int -kill_running_sesman(const char *pid_file) +read_pid_file(const char *pid_file, int *pid) { - int error; + int rv = 1; int fd; - int pid; - char pid_s[32] = {0}; /* check if sesman is running */ if (!g_file_exist(pid_file)) { g_printf("sesman is not running (pid file not found - %s)\n", pid_file); - g_deinit(); - return 1; } - - fd = g_file_open(pid_file); - - if (-1 == fd) + else if ((fd = g_file_open(pid_file)) < 0) { g_printf("error opening pid file[%s]: %s\n", pid_file, g_get_strerror()); - return 1; } - - error = g_file_read(fd, pid_s, sizeof(pid_s) - 1); - - if (-1 == error) + else { - g_printf("error reading pid file: %s\n", g_get_strerror()); + char pid_s[32] = {0}; + int error = g_file_read(fd, pid_s, sizeof(pid_s) - 1); g_file_close(fd); - g_deinit(); - return 1; - } - - g_file_close(fd); - pid = g_atoi(pid_s); - - error = g_sigterm(pid); - if (0 != error) - { - g_printf("error killing sesman: %s\n", g_get_strerror()); - } - else - { - g_file_delete(pid_file); + if (error < 0) + { + g_printf("error reading pid file: %s\n", g_get_strerror()); + } + else + { + *pid = g_atoi(pid_s); + rv = 0; + } } - g_deinit(); - return error; + return rv; } + /******************************************************************************/ int main(int argc, char **argv) @@ -699,9 +703,44 @@ main(int argc, char **argv) } - if (startup_params.kill) + if (startup_params.mode == SSM_KILL_DAEMON) { - g_exit(kill_running_sesman(pid_file)); + int pid; + int error = 1; + if (read_pid_file(pid_file, &pid) == 0) + { + if (g_sigterm(pid) != 0) + { + g_printf("error killing sesman: %s\n", g_get_strerror()); + } + else + { + /* File is no longer required */ + g_file_delete(pid_file); + error = 0; + } + } + g_deinit(); + g_exit(error); + } + + if (startup_params.mode == SSM_RELOAD_DAEMON) + { + int pid; + int error = 1; + if (read_pid_file(pid_file, &pid) == 0) + { + if (g_sighup(pid) != 0) + { + g_printf("error reloading sesman: %s\n", g_get_strerror()); + } + else + { + error = 0; + } + } + g_deinit(); + g_exit(error); } if (g_file_exist(pid_file)) |