summaryrefslogtreecommitdiffstats
path: root/sesman/session.c
diff options
context:
space:
mode:
Diffstat (limited to 'sesman/session.c')
-rw-r--r--sesman/session.c168
1 files changed, 158 insertions, 10 deletions
diff --git a/sesman/session.c b/sesman/session.c
index 250c8baf..1e6a22ce 100644
--- a/sesman/session.c
+++ b/sesman/session.c
@@ -35,6 +35,15 @@
#include <errno.h>
//#include <time.h>
+#include <arpa/inet.h>
+#include <sys/types.h>
+#include <netinet/in.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <netdb.h>
+
#include "libraptorsmiface.h"
extern unsigned char g_fixedkey[8];
@@ -146,6 +155,82 @@ session_get_bydata(char *name, int width, int height, int bpp, int type, char *c
/******************************************************************************/
/**
*
+ * @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_ports(const char* host, int display)
+{
+ char text[256];
+ int x_running;
+ int sck;
+
+ x_running = 0;
+ /* check 59xx */
+ {
+ g_sprintf(text, "59%2.2d", display);
+ x_running += check_port_status(host, text);
+ }
+ /* check 60xx */
+ {
+ g_sprintf(text, "60%2.2d", display);
+ x_running += check_port_status(host, text);
+ }
+ /* check 62xx */
+ {
+ g_sprintf(text, "62%2.2d", display);
+ x_running += check_port_status(host, text);
+ }
+
+ return x_running;
+}
+
+/******************************************************************************/
+/**
+ *
* @brief checks if there's a server running on a display
* @param display the display to check
* @return 0 if there isn't a display running, nonzero otherwise
@@ -383,14 +468,14 @@ wait_for_xserver(int display)
int i;
/* give X a bit to start */
- /* wait up to 10 secs for x server to start */
+ /* wait up to 15 secs for x server to start */
i = 0;
- while (!x_server_running(display))
+ while (!x_server_running_check_ports(display))
{
i++;
- if (i > 40)
+ if (i > 60)
{
log_message(LOG_LEVEL_ERROR,
"X server for display %d startup timeout",
@@ -405,6 +490,57 @@ wait_for_xserver(int display)
}
/******************************************************************************/
+static int APP_CC
+wait_for_remote_xserver(const char* host, int display)
+{
+ int i;
+
+ /* give X a bit to start */
+ /* wait up to 15 secs for x server to start */
+ i = 0;
+ //while (!x_server_running(display))
+ while (!x_server_running_check_remote_ports(host, display))
+ {
+ i++;
+ if (i > 60)
+ {
+ log_message(&(g_cfg->log), LOG_LEVEL_ERROR,
+ "X server for host %s and display %d startup timeout",
+ host, display);
+ break;
+ }
+ g_sleep(250);
+ }
+ return 0;
+}
+
+/******************************************************************************/
+static const char * APP_CC
+wait_for_remote_hostname(char* username)
+{
+ int i;
+
+ /* wait up to 5 secs for hostname to appear */
+ i = 0;
+ const char * hostname = raptor_sm_get_hostname_for_username(username, false);
+ while (strcmp(hostname, "") == 0)
+ {
+ g_free(hostname);
+ hostname = raptor_sm_get_hostname_for_username(username, false);
+ i++;
+ if (i > 20)
+ {
+ log_message(&(g_cfg->log), LOG_LEVEL_ERROR,
+ "Hostname allocation timeout");
+ break;
+ }
+ g_sleep(250);
+ }
+
+ return hostname;
+}
+
+/******************************************************************************/
/* called with the main thread */
static int APP_CC
session_start_fork(int width, int height, int bpp, char *username,
@@ -485,6 +621,7 @@ session_start_fork(int width, int height, int bpp, char *username,
if (display == 0)
{
+ log_message(&(g_cfg->log), LOG_LEVEL_ALWAYS, "Unable to allocate display for user %s", username);
g_free(temp->item);
g_free(temp);
return 0;
@@ -547,7 +684,6 @@ session_start_fork(int width, int height, int bpp, char *username,
}
else if (wmpid == 0)
{
- wait_for_xserver(display);
auth_start_session(data, display);
pampid = g_fork(); /* parent waits, todo
child becomes wm */
@@ -556,17 +692,24 @@ session_start_fork(int width, int height, int bpp, char *username,
}
else if (pampid == 0)
{
- env_set_user(username, 0, display,
- g_cfg->session_variables1,
- g_cfg->session_variables2);
if (session_was_already_running) {
g_exit(0);
}
+ char* remote_server = wait_for_remote_hostname(username);
+ wait_for_remote_xserver(remote_server, display);
env_set_user(username, 0, display,
g_cfg->session_variables1,
g_cfg->session_variables2);
- if (x_server_running(display))
+
+ //if (x_server_running(display))
+ if (x_server_running_check_remote_ports(remote_server, display))
{
+ g_free(remote_server);
+
+ // RAPTOR session management
+ raptor_sm_run_remote_desktop(username, display, "/opt/trinity/bin/starttde");
+ g_exit(0);
+
auth_set_env(data);
if (directory != 0)
{
@@ -633,6 +776,7 @@ session_start_fork(int width, int height, int bpp, char *username,
}
else
{
+ g_free(remote_server);
log_message(LOG_LEVEL_ERROR, "another Xserver might "
"already be active on display %d - see log", display);
}
@@ -754,7 +898,7 @@ session_start_fork(int width, int height, int bpp, char *username,
list_add_item(xserver_params, (tintptr)g_strdup("-depth"));
list_add_item(xserver_params, (tintptr)g_strdup(depth));
list_add_item(xserver_params, (tintptr)g_strdup("-reset"));
- list_add_item(xserver_params, (tintptr)g_strdup("-terminate"));
+// list_add_item(xserver_params, (tintptr)g_strdup("-terminate"));
/* additional parameters from sesman.ini file */
//config_read_xserver_params(SESMAN_SESSION_TYPE_XRDP,
@@ -768,6 +912,7 @@ session_start_fork(int width, int height, int bpp, char *username,
pid_t serverpid;
serverpid = raptor_sm_run_remote_server(username, pp1);
+ log_message(&(g_cfg->log), LOG_LEVEL_ALWAYS, "new server pid code was %d during login for user %s", serverpid, username);
if (serverpid >= 0) {
if (!session_was_already_running) {
@@ -818,7 +963,10 @@ session_start_fork(int width, int height, int bpp, char *username,
}
else
{
- wait_for_xserver(display);
+ //wait_for_xserver(display);
+ char* remote_server = wait_for_remote_hostname(username);
+ wait_for_remote_xserver(remote_server, display);
+ free(remote_server);
g_snprintf(text, 255, "%d", display);
g_setenv("XRDP_SESSVC_DISPLAY", text, 1);
g_snprintf(text, 255, ":%d.0", display);