diff --git a/xrdp/xrdp.c b/xrdp/xrdp.c index d2643e98..9431170e 100644 --- a/xrdp/xrdp.c +++ b/xrdp/xrdp.c @@ -575,6 +575,16 @@ main(int argc, char **argv) if (!no_daemon) { + /* if can't listen, exit with failure status */ + if (xrdp_listen_test(startup_params) != 0) + { + log_message(LOG_LEVEL_ERROR, "Failed to start xrdp daemon, " + "possibly address already in use."); + g_deinit(); + /* must exit with failure status, + or systemd cannot detect xrdp daemon couldn't start properly */ + g_exit(1); + } /* start of daemonizing code */ pid = g_fork(); @@ -587,16 +597,6 @@ main(int argc, char **argv) if (0 != pid) { - /* if can't listen, exit with failure status */ - if (xrdp_listen_test() != 0) - { - log_message(LOG_LEVEL_ERROR, "Failed to start xrdp daemon, " - "possibly address already in use."); - g_deinit(); - /* must exit with failure status, - or systemd cannot detect xrdp daemon couldn't start properly */ - g_exit(1); - } g_writeln("daemon process %d started ok", pid); /* exit, this is the main process */ g_deinit(); diff --git a/xrdp/xrdp.h b/xrdp/xrdp.h index 498dc141..697081db 100644 --- a/xrdp/xrdp.h +++ b/xrdp/xrdp.h @@ -170,7 +170,7 @@ xrdp_listen_delete(struct xrdp_listen* self); int xrdp_listen_main_loop(struct xrdp_listen* self); int -xrdp_listen_test(void); +xrdp_listen_test(struct xrdp_startup_params *startup_params); /* xrdp_region.c */ struct xrdp_region* diff --git a/xrdp/xrdp_listen.c b/xrdp/xrdp_listen.c index 1ce84a7d..9958d5b2 100644 --- a/xrdp/xrdp_listen.c +++ b/xrdp/xrdp_listen.c @@ -157,6 +157,8 @@ xrdp_listen_get_startup_params(struct xrdp_listen *self) { int fd; int index; + int port_override; + int fork_override; char *val; struct list *names; struct list *values; @@ -164,6 +166,8 @@ xrdp_listen_get_startup_params(struct xrdp_listen *self) struct xrdp_startup_params *startup_params; startup_params = self->startup_params; + port_override = startup_params->port[0] != 0; + fork_override = startup_params->fork; g_snprintf(cfg_file, 255, "%s/xrdp.ini", XRDP_CFG_PATH); fd = g_file_open(cfg_file); if (fd != -1) @@ -181,13 +185,20 @@ xrdp_listen_get_startup_params(struct xrdp_listen *self) { if (g_strcasecmp(val, "port") == 0) { - val = (char *)list_get_item(values, index); - g_strncpy(startup_params->port, val, sizeof(startup_params->port) - 1); + if (port_override == 0) + { + val = (char *) list_get_item(values, index); + g_strncpy(startup_params->port, val, + sizeof(startup_params->port) - 1); + } } if (g_strcasecmp(val, "fork") == 0) { - val = (char *)list_get_item(values, index); - startup_params->fork = g_text2bool(val); + if (fork_override == 0) + { + val = (char *) list_get_item(values, index); + startup_params->fork = g_text2bool(val); + } } if (g_strcasecmp(val, "tcp_nodelay") == 0) @@ -591,7 +602,7 @@ xrdp_listen_pp(struct xrdp_listen *self, int *index, static int xrdp_listen_process_startup_params(struct xrdp_listen *self) { - int mode; /* TRANS_MODE_TCP, TRANS_MODE_UNIX, TRANS_MODE_VSOCK */ + int mode; /* TRANS_MODE_TCP*, TRANS_MODE_UNIX, TRANS_MODE_VSOCK */ int error; int cont; int bytes; @@ -631,7 +642,9 @@ xrdp_listen_process_startup_params(struct xrdp_listen *self) xrdp_listen_stop_all_listen(self); return 1; } - if (mode == TRANS_MODE_TCP) + if ((mode == TRANS_MODE_TCP) || + (mode == TRANS_MODE_TCP4) || + (mode == TRANS_MODE_TCP6)) { if (startup_params->tcp_nodelay) { @@ -928,7 +941,22 @@ xrdp_listen_main_loop(struct xrdp_listen *self) /* returns 0 if xrdp can listen returns 1 if xrdp cannot listen */ int -xrdp_listen_test(void) +xrdp_listen_test(struct xrdp_startup_params *startup_params) { + struct xrdp_listen *xrdp_listen; + + xrdp_listen = xrdp_listen_create(); + xrdp_listen->startup_params = startup_params; + if (xrdp_listen_get_startup_params(xrdp_listen) != 0) + { + xrdp_listen_delete(xrdp_listen); + return 1; + } + if (xrdp_listen_process_startup_params(xrdp_listen) != 0) + { + xrdp_listen_delete(xrdp_listen); + return 1; + } + xrdp_listen_delete(xrdp_listen); return 0; }