diff --git a/xup/xup.c b/xup/xup.c index f77551fa..bc75f5a2 100644 --- a/xup/xup.c +++ b/xup/xup.c @@ -21,6 +21,99 @@ #include "xup.h" #include "log.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/******************************************************************************/ +/** + * + * @brief checks if there's a server running on a host and port + * @param display the display to check + * @return 0 if the port is closed, 1 if it is open + * + */ +static int DEFAULT_CC +check_port_status(const char* host, const char* port) +{ + char text[256]; + int x_running; + int sck; + + struct sockaddr_in servaddr; + int soc = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); + + g_memset( &servaddr, 0, sizeof(servaddr)); + servaddr.sin_family = AF_INET; + servaddr.sin_port = htons(atoi(port)); + + struct hostent* hostaddr; + hostaddr = gethostbyname(host); + g_memcpy(&servaddr.sin_addr, hostaddr->h_addr, hostaddr->h_length); + + int res = connect(soc, (struct sockaddr*)&servaddr, sizeof(servaddr)); + + close(soc); + + if (res == -1) + { + // Port is closed, no server there! + return 0; + } + else { + // Port is open + return 1; + } +} + +/******************************************************************************/ +/** + * + * @brief checks if there's a server running on a remote display + * @param display the display to check + * @return 0 if there isn't a display running, nonzero otherwise + * + */ +static int DEFAULT_CC +x_server_running_check_remote_port(const char* host, const char* port) +{ + int x_running; + int sck; + + x_running = 0; + x_running += check_port_status(host, port); + + return x_running; +} + +/******************************************************************************/ +static int APP_CC +wait_for_remote_xserver(const char* host, const char* port) +{ + int i; + + /* give X a bit to start */ + /* wait up to 15 secs for x server to start */ + i = 0; + while (!x_server_running_check_remote_port(host, port)) + { + i++; + if (i > 60) + { + break; + } + g_sleep(250); + } + return 0; +} + #define LOG_LEVEL 1 #define LLOG(_level, _args) \ do { if (_level < LOG_LEVEL) { g_write _args ; } } while (0) @@ -188,12 +281,15 @@ lib_mod_connect(struct mod *mod) } char text[256]; - g_snprintf(text, 255, "services starting on %s, please wait...\n\r", mod->ip); + g_snprintf(text, 255, "allocating resources on %s, please wait...\n\r", mod->ip); mod->server_msg(mod, text, 0); + // Prevent an immediate RDP exit + wait_for_remote_xserver(mod->ip, mod->port); + // FIXME CRITICAL // Prevent an immediate RDP exit - // This delay needs to be long enough for everything to start up 100% + // Why is this still needed even after waiting for the X11rdp server to start!?!? g_sleep(5000); if (g_strcmp(mod->ip, "") == 0)