diff options
-rw-r--r-- | xrdp/xrdp.c | 12 | ||||
-rw-r--r-- | xrdp/xrdp.h | 2 | ||||
-rw-r--r-- | xrdp/xrdp_listen.c | 51 |
3 files changed, 64 insertions, 1 deletions
diff --git a/xrdp/xrdp.c b/xrdp/xrdp.c index 54613ac3..cdd611f5 100644 --- a/xrdp/xrdp.c +++ b/xrdp/xrdp.c @@ -550,7 +550,17 @@ main(int argc, char **argv) if (0 != pid) { - g_writeln("process %d started ok", 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(); g_exit(0); diff --git a/xrdp/xrdp.h b/xrdp/xrdp.h index 82e8a578..5201dada 100644 --- a/xrdp/xrdp.h +++ b/xrdp/xrdp.h @@ -163,6 +163,8 @@ void xrdp_listen_delete(struct xrdp_listen* self); int xrdp_listen_main_loop(struct xrdp_listen* self); +int +xrdp_listen_test(void); /* xrdp_region.c */ struct xrdp_region* diff --git a/xrdp/xrdp_listen.c b/xrdp/xrdp_listen.c index 0e75dab6..e6443bc9 100644 --- a/xrdp/xrdp_listen.c +++ b/xrdp/xrdp_listen.c @@ -550,3 +550,54 @@ xrdp_listen_main_loop(struct xrdp_listen *self) self->status = -1; return error; } + +/*****************************************************************************/ +/* returns 0 if xrdp can listen + returns 1 if xrdp cannot listen */ +int +xrdp_listen_test(void) +{ + int rv = 0; + char port[128]; + char address[256]; + int tcp_nodelay; + int tcp_keepalive; + struct xrdp_listen *xrdp_listen; + struct xrdp_startup_params *startup_params; + + + startup_params = (struct xrdp_startup_params *) + g_malloc(sizeof(struct xrdp_startup_params), 1); + xrdp_listen = xrdp_listen_create(); + xrdp_listen->startup_params = startup_params; + + + if (xrdp_listen_get_port_address(port, sizeof(port), + address, sizeof(address), + &tcp_nodelay, &tcp_keepalive, + xrdp_listen->startup_params) != 0) + { + log_message(LOG_LEVEL_DEBUG, "xrdp_listen_test: " + "xrdp_listen_get_port_address failed"); + rv = 1; + goto done; + } + + /* try to listen */ + log_message(LOG_LEVEL_DEBUG, "Testing if xrdp can listen on %s port %s.", + address, port); + rv = trans_listen_address(xrdp_listen->listen_trans, port, address); + if (rv == 0) + { + /* if listen succeeded, stop listen immediately */ + trans_delete(xrdp_listen->listen_trans); + xrdp_listen->listen_trans = 0; + } + + goto done; + +done: + xrdp_listen_delete(xrdp_listen); + g_free(startup_params); + return rv; +} |