Fix a number of problems

System is now mostly stabilized
ulab-next-nosound
Timothy Pearson 13 years ago committed by Timothy Pearson
parent ffeda4fd0c
commit 46a8e6373e

@ -21,6 +21,8 @@
#ifndef DEFINES_H #ifndef DEFINES_H
#define DEFINES_H #define DEFINES_H
#define DISABLE_UNIX_DOMAIN_SOCKETS 1
/* check for debug */ /* check for debug */
#ifdef XRDP_DEBUG #ifdef XRDP_DEBUG
#define DEBUG(args) g_writeln args; #define DEBUG(args) g_writeln args;

@ -490,6 +490,7 @@ trans_force_read(struct trans *self, int size)
return trans_force_read_s(self, self->in_s, size); return trans_force_read_s(self, self->in_s, size);
} }
#if 0
/*****************************************************************************/ /*****************************************************************************/
int APP_CC int APP_CC
trans_force_write_s(struct trans *self, struct stream *out_s) trans_force_write_s(struct trans *self, struct stream *out_s)
@ -550,6 +551,55 @@ trans_force_write_s(struct trans *self, struct stream *out_s)
} }
return 0; return 0;
} }
#else
// DEBUG ONLY
/*****************************************************************************/
int APP_CC
trans_force_write_s(struct trans* self, struct stream* out_s)
{
int size;
int total;
int sent;
if (self->status != TRANS_STATUS_UP)
{
return 1;
}
size = (int)(out_s->end - out_s->data);
total = 0;
while (total < size)
{
sent = g_tcp_send(self->sck, out_s->data + total, size - total, 0);
if (sent == -1)
{
if (g_tcp_last_error_would_block(self->sck))
{
if (!g_tcp_can_send(self->sck, 10))
{
/* check for term here */
}
}
else
{
/* error */
self->status = TRANS_STATUS_DOWN;
return 2;
}
}
else if (sent == 0)
{
/* error */
self->status = TRANS_STATUS_DOWN;
return 3;
}
else
{
total = total + sent;
}
}
return 0;
}
#endif
/*****************************************************************************/ /*****************************************************************************/
int APP_CC int APP_CC

@ -23,8 +23,6 @@
#include "libraptorsmiface.h" #include "libraptorsmiface.h"
MYSQL *conn = 0;
char *server = "localhost"; char *server = "localhost";
char *user = "remotelab"; char *user = "remotelab";
char *password = "rlpass123"; /* set me first */ char *password = "rlpass123"; /* set me first */
@ -38,7 +36,7 @@ void dprint(const char *fmt, ...)
#if 0 #if 0
vprintf(fmt, argp); vprintf(fmt, argp);
#else #else
char debug[1024]; char debug[512];
vsprintf(debug, fmt, argp); vsprintf(debug, fmt, argp);
FILE *fp = fopen("/raptorsmiface.debug", "a"); FILE *fp = fopen("/raptorsmiface.debug", "a");
if (fp != NULL) if (fp != NULL)
@ -51,14 +49,13 @@ void dprint(const char *fmt, ...)
va_end(argp); va_end(argp);
} }
void connect_if_needed() { MYSQL * connect_if_needed() {
if (!conn) { MYSQL *conn = mysql_init(NULL);
conn = mysql_init(NULL); if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) {
if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) { dprint("[ERROR] MySQL connection FAILED [%s]\n\r", mysql_error(conn));
dprint("[ERROR] MySQL connection FAILED [%s]\n\r", mysql_error(conn)); conn = 0;
conn = 0;
}
} }
return conn;
} }
char* get_mysql_escaped_string(MYSQL *sqlcn, char* rawstr) { char* get_mysql_escaped_string(MYSQL *sqlcn, char* rawstr) {
@ -69,6 +66,13 @@ char* get_mysql_escaped_string(MYSQL *sqlcn, char* rawstr) {
return escstr; return escstr;
} }
char mutex;
int mysql_query_internal(MYSQL *conn, const char * query) {
// For some reason this can hang rather badly
// It might be related to concurrent access to the same conn object though
return mysql_query(conn, query);
}
char* get_group_for_user(char* username) { char* get_group_for_user(char* username) {
struct passwd* pwd; struct passwd* pwd;
pwd = getpwnam(username); pwd = getpwnam(username);
@ -94,7 +98,7 @@ char raptor_sm_deallocate_session(char* username) {
MYSQL_ROW cnt_row; MYSQL_ROW cnt_row;
char* query; char* query;
connect_if_needed(); MYSQL *conn = connect_if_needed();
if (!conn) { if (!conn) {
return 1; return 1;
} }
@ -103,13 +107,15 @@ char raptor_sm_deallocate_session(char* username) {
char* safe_username = get_mysql_escaped_string(conn, username); char* safe_username = get_mysql_escaped_string(conn, username);
asprintf(&query, "DELETE FROM sessions WHERE username='%s'", safe_username); asprintf(&query, "DELETE FROM sessions WHERE username='%s'", safe_username);
free(safe_username); free(safe_username);
if (mysql_query(conn, query)) { if (mysql_query_internal(conn, query)) {
// Server error // Server error
free(query); free(query);
mysql_close(conn);
return 2; return 2;
} }
else { else {
free(query); free(query);
mysql_close(conn);
return 0; return 0;
} }
} }
@ -123,7 +129,7 @@ char* raptor_sm_allocate_session(char* username) {
MYSQL_ROW cnt_row; MYSQL_ROW cnt_row;
char* query; char* query;
connect_if_needed(); MYSQL *conn = connect_if_needed();
if (!conn) { if (!conn) {
return strdup("SQLERR001"); return strdup("SQLERR001");
} }
@ -132,9 +138,10 @@ char* raptor_sm_allocate_session(char* username) {
char* safe_username = get_mysql_escaped_string(conn, username); char* safe_username = get_mysql_escaped_string(conn, username);
asprintf(&query, "SELECT servername FROM sessions WHERE username='%s'", safe_username); asprintf(&query, "SELECT servername FROM sessions WHERE username='%s'", safe_username);
free(safe_username); free(safe_username);
if (mysql_query(conn, query)) { if (mysql_query_internal(conn, query)) {
// Server error // Server error
free(query); free(query);
mysql_close(conn);
return strdup("SQLERR002"); return strdup("SQLERR002");
} }
else { else {
@ -143,9 +150,10 @@ char* raptor_sm_allocate_session(char* username) {
if ((row = mysql_fetch_row(res)) == NULL) { if ((row = mysql_fetch_row(res)) == NULL) {
// User is not on a system // User is not on a system
// Find the least utilized node // Find the least utilized node
if (mysql_query(conn, "SELECT name FROM servers")) { if (mysql_query_internal(conn, "SELECT name FROM servers WHERE online='1'")) {
// Server error // Server error
mysql_free_result(res); mysql_free_result(res);
mysql_close(conn);
return strdup("SQLERR003"); return strdup("SQLERR003");
} }
else { else {
@ -156,12 +164,13 @@ char* raptor_sm_allocate_session(char* username) {
char* safe_servername = get_mysql_escaped_string(conn, svr_row[0]); char* safe_servername = get_mysql_escaped_string(conn, svr_row[0]);
asprintf(&query, "SELECT username FROM sessions WHERE servername='%s'", safe_servername); asprintf(&query, "SELECT username FROM sessions WHERE servername='%s'", safe_servername);
free(safe_servername); free(safe_servername);
if (mysql_query(conn, query)) { if (mysql_query_internal(conn, query)) {
// Server error // Server error
free(query); free(query);
free(bestserver); free(bestserver);
mysql_free_result(res); mysql_free_result(res);
mysql_free_result(svr_res); mysql_free_result(svr_res);
mysql_close(conn);
return strdup("SQLERR004"); return strdup("SQLERR004");
} }
else { else {
@ -188,13 +197,15 @@ char* raptor_sm_allocate_session(char* username) {
asprintf(&query, "INSERT INTO sessions (username, servername, state) VALUES ('%s', '%s', '%d')", safe_username, safe_servername, SM_STATUS_ALLOCATED); asprintf(&query, "INSERT INTO sessions (username, servername, state) VALUES ('%s', '%s', '%d')", safe_username, safe_servername, SM_STATUS_ALLOCATED);
free(safe_servername); free(safe_servername);
free(safe_username); free(safe_username);
if (mysql_query(conn, query)) { if (mysql_query_internal(conn, query)) {
// Server error // Server error
free(query); free(query);
mysql_close(conn);
return strdup("SQLERR005"); return strdup("SQLERR005");
} }
else { else {
free(query); free(query);
mysql_close(conn);
return strdup(bestserver); return strdup(bestserver);
} }
} }
@ -202,6 +213,7 @@ char* raptor_sm_allocate_session(char* username) {
else { else {
char* ret = strdup(row[0]); char* ret = strdup(row[0]);
mysql_free_result(res); mysql_free_result(res);
mysql_close(conn);
return ret; return ret;
} }
} }
@ -234,7 +246,7 @@ char* raptor_sm_get_hostname_for_username(char* username, bool create) {
MYSQL_ROW row; MYSQL_ROW row;
char* query; char* query;
connect_if_needed(); MYSQL *conn = connect_if_needed();
if (!conn) { if (!conn) {
return strdup("SQLERR100"); return strdup("SQLERR100");
} }
@ -242,9 +254,10 @@ char* raptor_sm_get_hostname_for_username(char* username, bool create) {
char* safe_username = get_mysql_escaped_string(conn, username); char* safe_username = get_mysql_escaped_string(conn, username);
asprintf(&query, "SELECT servername FROM sessions WHERE username='%s'", safe_username); asprintf(&query, "SELECT servername FROM sessions WHERE username='%s'", safe_username);
free(safe_username); free(safe_username);
if (mysql_query(conn, query)) { if (mysql_query_internal(conn, query)) {
// Server error // Server error
free(query); free(query);
mysql_close(conn);
return strdup("SQLERR101"); return strdup("SQLERR101");
} }
else { else {
@ -253,15 +266,18 @@ char* raptor_sm_get_hostname_for_username(char* username, bool create) {
while ((row = mysql_fetch_row(res)) != NULL) { while ((row = mysql_fetch_row(res)) != NULL) {
char* ret = strdup(row[0]); char* ret = strdup(row[0]);
mysql_free_result(res); mysql_free_result(res);
mysql_close(conn);
return ret; return ret;
} }
// Nothing in the DB // Nothing in the DB
mysql_free_result(res); mysql_free_result(res);
if (create) { if (create) {
// Try to allocate a new session on a node // Try to allocate a new session on a node
mysql_close(conn);
return raptor_sm_allocate_session(username); return raptor_sm_allocate_session(username);
} }
else { else {
mysql_close(conn);
return strdup(""); return strdup("");
} }
} }
@ -283,7 +299,7 @@ bool raptor_sm_sesslimit_reached(char* username) {
MYSQL_ROW row; MYSQL_ROW row;
char* query; char* query;
connect_if_needed(); MYSQL *conn = connect_if_needed();
if (!conn) { if (!conn) {
return true; return true;
} }
@ -297,9 +313,10 @@ bool raptor_sm_sesslimit_reached(char* username) {
free(groupname); free(groupname);
asprintf(&query, "SELECT sesslimit FROM groups WHERE groupname='%s'", safe_groupname); asprintf(&query, "SELECT sesslimit FROM groups WHERE groupname='%s'", safe_groupname);
free(safe_groupname); free(safe_groupname);
if (mysql_query(conn, query)) { if (mysql_query_internal(conn, query)) {
// Server error // Server error
free(query); free(query);
mysql_close(conn);
return true; return true;
} }
else { else {
@ -315,9 +332,10 @@ bool raptor_sm_sesslimit_reached(char* username) {
// Figure out how many users are online from this group // Figure out how many users are online from this group
int sesscount = 0; int sesscount = 0;
asprintf(&query, "SELECT username FROM sessions WHERE state<>'%d'", SM_STATUS_ALLOCATED); asprintf(&query, "SELECT username FROM sessions WHERE state<>'%d'", SM_STATUS_ALLOCATED);
if (mysql_query(conn, query)) { if (mysql_query_internal(conn, query)) {
// Server error // Server error
free(query); free(query);
mysql_close(conn);
return true; return true;
} }
else { else {
@ -335,12 +353,15 @@ bool raptor_sm_sesslimit_reached(char* username) {
mysql_free_result(res); mysql_free_result(res);
if (sesscount < sesslimit) { if (sesscount < sesslimit) {
mysql_close(conn);
return false; return false;
} }
mysql_close(conn);
return true; return true;
} }
// We should never end up here! // We should never end up here!
mysql_close(conn);
return true; return true;
} }
@ -349,24 +370,26 @@ pid_t raptor_sm_run_remote_server(char* username, char *const argv[]) {
MYSQL_ROW row; MYSQL_ROW row;
char* query; char* query;
connect_if_needed(); MYSQL *conn = connect_if_needed();
if (!conn) { if (!conn) {
return -1; return -1;
} }
// Respect maximum session number for the group for this user // Respect maximum session number for the group for this user
if (raptor_sm_sesslimit_reached(username)) { if (raptor_sm_sesslimit_reached(username)) {
mysql_close(conn);
return -5; return -5;
} }
// Make sure a server is not already running for this user // Make sure a server is not already running for this user
// Return the existing PID if it is // Return the existing PID if it is
char* safe_username = get_mysql_escaped_string(conn, username); char* safe_username = get_mysql_escaped_string(conn, username);
asprintf(&query, "SELECT pid FROM sessions WHERE username='%s' AND state<>'%d'", safe_username, SM_STATUS_ALLOCATED); asprintf(&query, "SELECT pid,servername FROM sessions WHERE username='%s' AND state<>'%d'", safe_username, SM_STATUS_ALLOCATED);
free(safe_username); free(safe_username);
if (mysql_query(conn, query)) { if (mysql_query_internal(conn, query)) {
// Server error // Server error
free(query); free(query);
mysql_close(conn);
return -2; return -2;
} }
else { else {
@ -376,8 +399,31 @@ pid_t raptor_sm_run_remote_server(char* username, char *const argv[]) {
if (row[0]) { if (row[0]) {
int ret = atoi(row[0]); int ret = atoi(row[0]);
if (ret >= 0) { if (ret >= 0) {
mysql_free_result(res); // Verify existence of PID on remote server
return ret; dprint("Verifying process %d on %s...\n\r", ret, row[1]);
char* ip = raptor_sm_get_ip_for_hostname(row[1], 0);
char* command_string;
asprintf(&command_string, "ssh root@%s \'ps -p %d | grep %d\'", ip, ret, ret);
FILE *fp;
char output[1024];
// Open the command for reading
fp = popen(command_string, "r");
if (fp == NULL) {
mysql_close(conn);
return -1;
}
// Read the output a line at a time
fgets(output, sizeof(output)-1, fp);
// Close output
pclose(fp);
free(command_string);
free(ip);
dprint("...result was %s\n\r", output);
if (strcmp(output, "") != 0) {
mysql_free_result(res);
mysql_close(conn);
return ret;
}
} }
} }
} }
@ -402,7 +448,7 @@ pid_t raptor_sm_run_remote_server(char* username, char *const argv[]) {
free(origstr); free(origstr);
} }
char* origstr = command_string; char* origstr = command_string;
asprintf(&command_string, "ssh %s \'%s & echo $! &\'", ipaddr, origstr); asprintf(&command_string, "ssh root@%s \'%s & echo $! &\'", ipaddr, origstr);
free(origstr); free(origstr);
FILE *fp; FILE *fp;
@ -411,6 +457,7 @@ pid_t raptor_sm_run_remote_server(char* username, char *const argv[]) {
// Open the command for reading // Open the command for reading
fp = popen(command_string, "r"); fp = popen(command_string, "r");
if (fp == NULL) { if (fp == NULL) {
mysql_close(conn);
return -1; return -1;
} }
@ -422,9 +469,51 @@ pid_t raptor_sm_run_remote_server(char* username, char *const argv[]) {
free(command_string); free(command_string);
mysql_close(conn);
return atoi(output); return atoi(output);
} }
pid_t raptor_sm_get_pid_for_username(char* username) {
MYSQL_RES *res;
MYSQL_ROW row;
char* query;
MYSQL *conn = connect_if_needed();
if (!conn) {
return -1;
}
// Make sure a server is not already running for this user
// Return the existing PID if it is
char* safe_username = get_mysql_escaped_string(conn, username);
asprintf(&query, "SELECT pid FROM sessions WHERE username='%s'", safe_username);
free(safe_username);
if (mysql_query_internal(conn, query)) {
// Server error
free(query);
mysql_close(conn);
return -2;
}
else {
free(query);
res = mysql_store_result(conn);
while ((row = mysql_fetch_row(res)) != NULL) {
if (row[0]) {
int ret = atoi(row[0]);
if (ret >= 0) {
mysql_free_result(res);
mysql_close(conn);
return ret;
}
}
}
mysql_free_result(res);
}
mysql_close(conn);
return -3;
}
char* raptor_sm_server_started(char* username, pid_t pid, int display) { char* raptor_sm_server_started(char* username, pid_t pid, int display) {
MYSQL_RES *res; MYSQL_RES *res;
MYSQL_ROW row; MYSQL_ROW row;
@ -432,7 +521,7 @@ char* raptor_sm_server_started(char* username, pid_t pid, int display) {
long long timestamp = time(NULL); long long timestamp = time(NULL);
connect_if_needed(); MYSQL *conn = connect_if_needed();
if (!conn) { if (!conn) {
return -1; return -1;
} }
@ -441,13 +530,15 @@ char* raptor_sm_server_started(char* username, pid_t pid, int display) {
char* safe_username = get_mysql_escaped_string(conn, username); char* safe_username = get_mysql_escaped_string(conn, username);
asprintf(&query, "UPDATE sessions SET pid='%d', stamp_start='%lld', state='%d', display='%d', stamp_statechange='%lld' WHERE username='%s' AND state='%d'", pid, timestamp, SM_STATUS_RUNNING, display, timestamp, safe_username, SM_STATUS_ALLOCATED); asprintf(&query, "UPDATE sessions SET pid='%d', stamp_start='%lld', state='%d', display='%d', stamp_statechange='%lld' WHERE username='%s' AND state='%d'", pid, timestamp, SM_STATUS_RUNNING, display, timestamp, safe_username, SM_STATUS_ALLOCATED);
free(safe_username); free(safe_username);
if (mysql_query(conn, query)) { if (mysql_query_internal(conn, query)) {
// Server error // Server error
free(query); free(query);
mysql_close(conn);
return -2; return -2;
} }
else { else {
free(query); free(query);
mysql_close(conn);
return 0; return 0;
} }
} }
@ -457,7 +548,7 @@ int raptor_sm_get_display_for_username(char* username) {
MYSQL_ROW row; MYSQL_ROW row;
char* query; char* query;
connect_if_needed(); MYSQL *conn = connect_if_needed();
if (!conn) { if (!conn) {
return -1; return -1;
} }
@ -465,9 +556,10 @@ int raptor_sm_get_display_for_username(char* username) {
char* safe_username = get_mysql_escaped_string(conn, username); char* safe_username = get_mysql_escaped_string(conn, username);
asprintf(&query, "SELECT display FROM sessions WHERE username='%s'", safe_username); asprintf(&query, "SELECT display FROM sessions WHERE username='%s'", safe_username);
free(safe_username); free(safe_username);
if (mysql_query(conn, query)) { if (mysql_query_internal(conn, query)) {
// Server error // Server error
free(query); free(query);
mysql_close(conn);
return -2; return -2;
} }
else { else {
@ -477,15 +569,18 @@ int raptor_sm_get_display_for_username(char* username) {
if (row[0]) { if (row[0]) {
int ret = atoi(row[0]); int ret = atoi(row[0]);
mysql_free_result(res); mysql_free_result(res);
mysql_close(conn);
return ret; return ret;
} }
else { else {
mysql_free_result(res); mysql_free_result(res);
mysql_close(conn);
return -3; return -3;
} }
} }
// Nothing in the DB // Nothing in the DB
mysql_free_result(res); mysql_free_result(res);
mysql_close(conn);
return -4; return -4;
} }
} }
@ -494,7 +589,7 @@ void raptor_sm_wait_for_pid_exit(char* username, pid_t pid) {
char* ipaddr = raptor_sm_get_ip_for_username(username, false); char* ipaddr = raptor_sm_get_ip_for_username(username, false);
char* command_string; char* command_string;
asprintf(&command_string, "ssh %s \'while [[ `ps -p %d | grep %d` != \"\" ]]; do sleep 1; done\'", ipaddr, pid, pid); asprintf(&command_string, "ssh root@%s \'while [[ `ps -p %d | grep %d` != \"\" ]]; do sleep 1; done\'", ipaddr, pid, pid);
system(command_string); system(command_string);
free(command_string); free(command_string);
} }
@ -508,15 +603,16 @@ int raptor_sm_get_new_unique_display(int mindisplay, int maxdisplay) {
MYSQL_ROW row; MYSQL_ROW row;
char* query; char* query;
connect_if_needed(); MYSQL *conn = connect_if_needed();
if (!conn) { if (!conn) {
return -1; return -1;
} }
asprintf(&query, "SELECT display FROM sessions"); asprintf(&query, "SELECT display FROM sessions");
if (mysql_query(conn, query)) { if (mysql_query_internal(conn, query)) {
// Server error // Server error
free(query); free(query);
mysql_close(conn);
return -2; return -2;
} }
else { else {
@ -538,6 +634,7 @@ int raptor_sm_get_new_unique_display(int mindisplay, int maxdisplay) {
} }
} }
mysql_free_result(res); mysql_free_result(res);
mysql_close(conn);
return freedisp; return freedisp;
} }
} }
@ -549,20 +646,38 @@ char raptor_sm_set_session_state(int display, int state) {
long long timestamp = time(NULL); long long timestamp = time(NULL);
connect_if_needed(); MYSQL *conn = connect_if_needed();
if (!conn) { if (!conn) {
return -1; return -1;
} }
// Update new state into the sessions database // Update new state into the sessions database
asprintf(&query, "UPDATE sessions SET state='%d', stamp_statechange='%lld' WHERE display='%d'", state, timestamp, display); asprintf(&query, "UPDATE sessions SET state='%d', stamp_statechange='%lld' WHERE display='%d'", state, timestamp, display);
if (mysql_query(conn, query)) { if (mysql_query_internal(conn, query)) {
// Server error // Server error
free(query); free(query);
mysql_close(conn);
return -2; return -2;
} }
else { else {
free(query); free(query);
mysql_close(conn);
return 0; return 0;
} }
}
void raptor_sm_run_remote_desktop(char* username, int display, char* executable) {
char* ipaddr = raptor_sm_get_ip_for_username(username, true);
char* command_string;
asprintf(&command_string, "ssh root@%s \"su %s -c \'export DISPLAY=:%d && %s && exit\' &> /dev/null\"", ipaddr, username, display, executable);
system(command_string);
free(command_string);
// Terminate remote X server
pid_t pid = raptor_sm_get_pid_for_username(username);
if (pid > 0) {
asprintf(&command_string, "ssh root@%s \'kill -9 %ld\'", ipaddr, pid);
system(command_string);
free(command_string);
}
} }

@ -25,10 +25,12 @@ char* raptor_sm_get_hostname_for_username(char* username, bool create);
char* raptor_sm_get_ip_for_username(char* username, bool create); char* raptor_sm_get_ip_for_username(char* username, bool create);
pid_t raptor_sm_run_remote_server(char* username, char *const argv[]); pid_t raptor_sm_run_remote_server(char* username, char *const argv[]);
pid_t raptor_sm_get_pid_for_username(char* username);
char* raptor_sm_server_started(char* username, pid_t pid, int display); char* raptor_sm_server_started(char* username, pid_t pid, int display);
int raptor_sm_get_display_for_username(char* username); int raptor_sm_get_display_for_username(char* username);
void raptor_sm_wait_for_pid_exit(char* username, pid_t pid); void raptor_sm_wait_for_pid_exit(char* username, pid_t pid);
void raptor_sm_session_terminated(char* username); void raptor_sm_session_terminated(char* username);
int raptor_sm_get_new_unique_display(int mindisplay, int maxdisplay); int raptor_sm_get_new_unique_display(int mindisplay, int maxdisplay);
bool raptor_sm_sesslimit_reached(char* username); bool raptor_sm_sesslimit_reached(char* username);
char raptor_sm_set_session_state(int display, int state); char raptor_sm_set_session_state(int display, int state);
void raptor_sm_run_remote_desktop(char* username, int display, char* executable);

@ -442,6 +442,7 @@ send_init_response_message(void)
out_uint32_le(s, 2); /* msg id */ out_uint32_le(s, 2); /* msg id */
out_uint32_le(s, 8); /* size */ out_uint32_le(s, 8); /* size */
s_mark_end(s); s_mark_end(s);
LOG(1, ("send_init_response_message: calling trans_force_write"));
return trans_write_copy(g_con_trans); return trans_write_copy(g_con_trans);
} }
@ -775,6 +776,7 @@ process_message(void)
if (rv != 0) if (rv != 0)
{ {
LOG(0, ("process_message: error in process_message: rv %d", rv));
break; break;
} }
@ -1173,7 +1175,6 @@ channel_thread_loop(void *in_val)
g_con_trans = 0; g_con_trans = 0;
// Use the display number to mark session disconnected in the Raptor session management database // Use the display number to mark session disconnected in the Raptor session management database
raptor_sm_set_session_state(g_display_num, SM_STATUS_RUNNING); raptor_sm_set_session_state(g_display_num, SM_STATUS_RUNNING);
exit(0); // RAPTOR session management
/* create new listener */ /* create new listener */
error = setup_listen(); error = setup_listen();
@ -1409,6 +1410,7 @@ read_ini(void)
name = (char *)list_get_item(names, index); name = (char *)list_get_item(names, index);
value = (char *)list_get_item(values, index); value = (char *)list_get_item(values, index);
#ifndef DISABLE_UNIX_DOMAIN_SOCKETS
if (g_strcasecmp(name, "ListenAddress") == 0) if (g_strcasecmp(name, "ListenAddress") == 0)
{ {
if (g_strcasecmp(value, "127.0.0.1") == 0) if (g_strcasecmp(value, "127.0.0.1") == 0)
@ -1416,6 +1418,7 @@ read_ini(void)
g_use_unix_socket = 1; g_use_unix_socket = 1;
} }
} }
#endif
} }
} }

@ -26,6 +26,8 @@
#include "sesman.h" #include "sesman.h"
#include "libraptorsmiface.h"
extern struct config_sesman *g_cfg; /* in sesman.c */ extern struct config_sesman *g_cfg; /* in sesman.c */
/******************************************************************************/ /******************************************************************************/
@ -76,6 +78,17 @@ scp_v0_process(struct SCP_CONNECTION *c, struct SCP_SESSION *s)
s_item = session_get_bydata(s->username, s->width, s->height, s_item = session_get_bydata(s->username, s->width, s->height,
s->bpp, s->type, s->client_ip); s->bpp, s->type, s->client_ip);
// RAPTOR session management
pid_t serverpid = raptor_sm_get_pid_for_username(s->username);
if (serverpid < 0) {
// Session NOT already running
if (s_item != 0) {
log_message(&(g_cfg->log), LOG_LEVEL_INFO, "++ [FIXME] scp claimed there was an active session, but the authoritative RAPTOR database disagrees: username %s", s->username);
}
s_item = 0;
}
if (s_item != 0) if (s_item != 0)
{ {
display = s_item->display; display = s_item->display;

@ -29,6 +29,8 @@
//#include "libscp_types.h" //#include "libscp_types.h"
#include "libscp.h" #include "libscp.h"
#include "libraptorsmiface.h"
extern struct config_sesman *g_cfg; /* in sesman.c */ extern struct config_sesman *g_cfg; /* in sesman.c */
static void parseCommonStates(enum SCP_SERVER_STATES_E e, char *f); static void parseCommonStates(enum SCP_SERVER_STATES_E e, char *f);
@ -107,6 +109,13 @@ scp_v1_process(struct SCP_CONNECTION *c, struct SCP_SESSION *s)
/* list disconnected sessions */ /* list disconnected sessions */
slist = session_get_byuser(s->username, &scount, SESMAN_SESSION_STATUS_DISCONNECTED); slist = session_get_byuser(s->username, &scount, SESMAN_SESSION_STATUS_DISCONNECTED);
// RAPTOR session management
pid_t serverpid = raptor_sm_get_pid_for_username(s->username);
if (serverpid < 0) {
// Session NOT already running
scount = 0;
}
if (scount == 0) if (scount == 0)
{ {
/* no disconnected sessions - start a new one */ /* no disconnected sessions - start a new one */

@ -35,6 +35,15 @@
#include <errno.h> #include <errno.h>
//#include <time.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" #include "libraptorsmiface.h"
extern unsigned char g_fixedkey[8]; extern unsigned char g_fixedkey[8];
@ -143,6 +152,82 @@ session_get_bydata(char *name, int width, int height, int bpp, int type, char *c
return 0; return 0;
} }
/******************************************************************************/
/**
*
* @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;
}
/******************************************************************************/ /******************************************************************************/
/** /**
* *
@ -383,14 +468,14 @@ wait_for_xserver(int display)
int i; int i;
/* give X a bit to start */ /* 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; i = 0;
while (!x_server_running(display)) while (!x_server_running_check_ports(display))
{ {
i++; i++;
if (i > 40) if (i > 60)
{ {
log_message(LOG_LEVEL_ERROR, log_message(LOG_LEVEL_ERROR,
"X server for display %d startup timeout", "X server for display %d startup timeout",
@ -404,6 +489,57 @@ wait_for_xserver(int display)
return 0; return 0;
} }
/******************************************************************************/
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 */ /* called with the main thread */
static int APP_CC static int APP_CC
@ -485,6 +621,7 @@ session_start_fork(int width, int height, int bpp, char *username,
if (display == 0) 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->item);
g_free(temp); g_free(temp);
return 0; return 0;
@ -547,7 +684,6 @@ session_start_fork(int width, int height, int bpp, char *username,
} }
else if (wmpid == 0) else if (wmpid == 0)
{ {
wait_for_xserver(display);
auth_start_session(data, display); auth_start_session(data, display);
pampid = g_fork(); /* parent waits, todo pampid = g_fork(); /* parent waits, todo
child becomes wm */ child becomes wm */
@ -556,17 +692,24 @@ session_start_fork(int width, int height, int bpp, char *username,
} }
else if (pampid == 0) else if (pampid == 0)
{ {
env_set_user(username, 0, display,
g_cfg->session_variables1,
g_cfg->session_variables2);
if (session_was_already_running) { if (session_was_already_running) {
g_exit(0); g_exit(0);
} }
char* remote_server = wait_for_remote_hostname(username);
wait_for_remote_xserver(remote_server, display);
env_set_user(username, 0, display, env_set_user(username, 0, display,
g_cfg->session_variables1, g_cfg->session_variables1,
g_cfg->session_variables2); 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); auth_set_env(data);
if (directory != 0) if (directory != 0)
{ {
@ -633,6 +776,7 @@ session_start_fork(int width, int height, int bpp, char *username,
} }
else else
{ {
g_free(remote_server);
log_message(LOG_LEVEL_ERROR, "another Xserver might " log_message(LOG_LEVEL_ERROR, "another Xserver might "
"already be active on display %d - see log", display); "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(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("-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 */ /* additional parameters from sesman.ini file */
//config_read_xserver_params(SESMAN_SESSION_TYPE_XRDP, //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; pid_t serverpid;
serverpid = raptor_sm_run_remote_server(username, pp1); 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 (serverpid >= 0) {
if (!session_was_already_running) { if (!session_was_already_running) {
@ -818,7 +963,10 @@ session_start_fork(int width, int height, int bpp, char *username,
} }
else 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_snprintf(text, 255, "%d", display);
g_setenv("XRDP_SESSVC_DISPLAY", text, 1); g_setenv("XRDP_SESSVC_DISPLAY", text, 1);
g_snprintf(text, 255, ":%d.0", display); g_snprintf(text, 255, ":%d.0", display);

@ -489,6 +489,7 @@ static int APP_CC
xrdp_mm_setup_mod2(struct xrdp_mm *self) xrdp_mm_setup_mod2(struct xrdp_mm *self)
{ {
char text[256]; char text[256];
char raptortext[256];
char *name; char *name;
char *value; char *value;
int i; int i;
@ -523,12 +524,12 @@ xrdp_mm_setup_mod2(struct xrdp_mm *self)
char* rsmip = raptor_sm_get_ip_for_username(self->login_username, true); char* rsmip = raptor_sm_get_ip_for_username(self->login_username, true);
int allocdisplay = raptor_sm_get_display_for_username(self->login_username); int allocdisplay = raptor_sm_get_display_for_username(self->login_username);
if ((raptor_sm_sesslimit_reached(self->login_username)) && (allocdisplay < 0)) { if ((raptor_sm_sesslimit_reached(self->login_username)) && (allocdisplay < 0)) {
g_snprintf(text, 255, "[LICENSE] Maximum concurrent session"); g_snprintf(raptortext, 255, "[LICENSE] Maximum concurrent session");
xrdp_wm_log_msg(self->wm, text); xrdp_wm_log_msg(self->wm, raptortext);
g_snprintf(text, 255, "[LICENSE] limit exceeded for group."); g_snprintf(raptortext, 255, "[LICENSE] limit exceeded for group.");
xrdp_wm_log_msg(self->wm, text); xrdp_wm_log_msg(self->wm, raptortext);
g_snprintf(text, 255, "[LICENSE] Login for user %s denied.", self->login_username); g_snprintf(raptortext, 255, "[LICENSE] Login for user %s denied.", self->login_username);
xrdp_wm_log_msg(self->wm, text); xrdp_wm_log_msg(self->wm, raptortext);
raptor_sm_session_terminated(self->login_username); raptor_sm_session_terminated(self->login_username);
return 1; return 1;
} }
@ -537,10 +538,14 @@ xrdp_mm_setup_mod2(struct xrdp_mm *self)
self->display = allocdisplay; self->display = allocdisplay;
} }
self->mod->mod_set_param(self->mod, "ip", rsmip); self->mod->mod_set_param(self->mod, "ip", rsmip);
#ifdef DISABLE_UNIX_DOMAIN_SOCKETS
use_uds = 0;
#else
use_uds = 1; use_uds = 1;
if (g_strcmp(rsmip, "127.0.0.1") != 0) { if (g_strcmp(rsmip, "127.0.0.1") != 0) {
use_uds = 0; use_uds = 0;
} }
#endif
} }
g_free(rsmip); g_free(rsmip);
@ -1138,7 +1143,11 @@ xrdp_mm_connect_chansrv(struct xrdp_mm *self, char *ip, char *port)
self->usechansrv = 1; self->usechansrv = 1;
/* connect channel redir */ /* connect channel redir */
#ifdef DISABLE_UNIX_DOMAIN_SOCKETS
if (0)
#else
if ((g_strcmp(ip, "127.0.0.1") == 0) || (ip[0] == 0)) if ((g_strcmp(ip, "127.0.0.1") == 0) || (ip[0] == 0))
#endif
{ {
/* unix socket */ /* unix socket */
self->chan_trans = trans_create(TRANS_MODE_UNIX, 8192, 8192); self->chan_trans = trans_create(TRANS_MODE_UNIX, 8192, 8192);
@ -1235,7 +1244,11 @@ xrdp_mm_process_login_response(struct xrdp_mm *self, struct stream *s)
self->wm->dragging = 0; self->wm->dragging = 0;
/* connect channel redir */ /* connect channel redir */
#ifdef DISABLE_UNIX_DOMAIN_SOCKETS
if (0)
#else
if ((g_strcmp(ip, "127.0.0.1") == 0) || (ip[0] == 0)) if ((g_strcmp(ip, "127.0.0.1") == 0) || (ip[0] == 0))
#endif
{ {
g_snprintf(port, 255, XRDP_CHANSRV_STR, display); g_snprintf(port, 255, XRDP_CHANSRV_STR, display);
} }

@ -160,6 +160,15 @@ lib_mod_connect(struct mod *mod)
return 1; return 1;
} }
char text[256];
g_snprintf(text, 255, "services starting on %s, please wait...\n\r", mod->ip);
mod->server_msg(mod, text, 0);
// FIXME CRITICAL
// Prevent an immediate RDP exit
// This delay needs to be long enough for everything to start up 100%
g_sleep(5000);
if (g_strcmp(mod->ip, "") == 0) if (g_strcmp(mod->ip, "") == 0)
{ {
mod->server_msg(mod, "error - no ip set", 0); mod->server_msg(mod, "error - no ip set", 0);

Loading…
Cancel
Save