Skip to content

Commit

Permalink
Merge pull request #2586 from matt335672/move_xrdp_waitforx
Browse files Browse the repository at this point in the history
waitforx logging improvements
  • Loading branch information
matt335672 authored Mar 18, 2023
2 parents eeaa803 + 5c01729 commit 42be576
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 80 deletions.
2 changes: 1 addition & 1 deletion sesman/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ AM_CPPFLAGS = \
-DXRDP_SYSCONF_PATH=\"${sysconfdir}\" \
-DXRDP_CFG_PATH=\"${sysconfdir}/xrdp\" \
-DXRDP_SBIN_PATH=\"${sbindir}\" \
-DXRDP_BIN_PATH=\"${bindir}\" \
-DXRDP_LIBEXEC_PATH=\"${libexecdir}/xrdp\" \
-DXRDP_SHARE_PATH=\"${datadir}/xrdp\" \
-DXRDP_PID_PATH=\"${localstatedir}/run\" \
-DXRDP_SOCKET_PATH=\"${socketdir}\" \
Expand Down
26 changes: 20 additions & 6 deletions sesman/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,6 @@ session_start_chansrv(int uid, int display)
chansrv_pid = g_fork();
if (chansrv_pid == 0)
{
LOG(LOG_LEVEL_INFO,
"Starting the xrdp channel server for display %d", display);

chansrv_params = list_create();
chansrv_params->auto_free = 1;

Expand All @@ -364,6 +361,9 @@ session_start_chansrv(int uid, int display)
g_cfg->env_names,
g_cfg->env_values);

LOG(LOG_LEVEL_INFO,
"Starting the xrdp channel server for display %d", display);

/* executing chansrv */
g_execvp_list(exe_path, chansrv_params);

Expand Down Expand Up @@ -585,12 +585,16 @@ session_start(struct auth_info *auth_info,
}
else if (window_manager_pid == 0)
{
enum xwait_status xws;

env_set_user(s->uid,
0,
display,
g_cfg->env_names,
g_cfg->env_values);
if (wait_for_xserver(display))
xws = wait_for_xserver(display);

if (xws == XW_STATUS_OK)
{
auth_set_env(auth_info);
if (s->directory != 0)
Expand Down Expand Up @@ -661,8 +665,18 @@ session_start(struct auth_info *auth_info,
}
else
{
LOG(LOG_LEVEL_ERROR,
"There is no X server active on display %d", display);
switch (xws)
{
case XW_STATUS_TIMED_OUT:
LOG(LOG_LEVEL_ERROR, "Timed out waiting for X server");
break;
case XW_STATUS_FAILED_TO_START:
LOG(LOG_LEVEL_ERROR, "X server failed to start");
break;
default:
LOG(LOG_LEVEL_ERROR,
"An error occurred waiting for the X server");
}
}

LOG(LOG_LEVEL_ERROR, "A fatal error has occurred attempting to start "
Expand Down
71 changes: 57 additions & 14 deletions sesman/xwait.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,82 @@

#include <stdio.h>
#include <string.h>
#include <sys/wait.h>

/******************************************************************************/
int
enum xwait_status
wait_for_xserver(int display)
{
FILE *dp = NULL;
int ret = 0;
enum xwait_status rv = XW_STATUS_MISC_ERROR;
int ret;

char buffer[100];
char exe_cmd[262];
const char exe[] = XRDP_LIBEXEC_PATH "/waitforx";
char cmd[sizeof(exe) + 64];

LOG(LOG_LEVEL_DEBUG, "Waiting for X server to start on display %d", display);
if (!g_file_exist(exe))
{
LOG(LOG_LEVEL_ERROR, "Unable to find %s", exe);
return rv;
}

g_snprintf(exe_cmd, sizeof(exe_cmd), "%s/xrdp-waitforx", XRDP_BIN_PATH);
dp = popen(exe_cmd, "r");
g_snprintf(cmd, sizeof(cmd), "%s -d :%d", exe, display);
LOG(LOG_LEVEL_DEBUG, "Waiting for X server to start on display :%d",
display);

dp = popen(cmd, "r");
if (dp == NULL)
{
LOG(LOG_LEVEL_ERROR, "Unable to launch xrdp-waitforx");
return 1;
LOG(LOG_LEVEL_ERROR, "Unable to launch waitforx");
return rv;
}

while (fgets(buffer, 100, dp))
{
const char *msg = buffer;
enum logLevels level = LOG_LEVEL_ERROR;

g_strtrim(buffer, 2);
LOG(LOG_LEVEL_DEBUG, "%s", buffer);

// Has the message got a class at the start?
if (strlen(buffer) > 3 && buffer[0] == '<' && buffer[2] == '>')
{
switch (buffer[1])
{
case 'D':
level = LOG_LEVEL_DEBUG;
break;
case 'I':
level = LOG_LEVEL_INFO;
break;
case 'W':
level = LOG_LEVEL_WARNING;
break;
default:
level = LOG_LEVEL_ERROR;
break;
}
msg = buffer + 3;
}

if (strlen(msg) > 0)
{
LOG(level, "waitforx: %s", msg);
}
}

ret = pclose(dp);
if (ret != 0)
if (WIFEXITED(ret))
{
LOG(LOG_LEVEL_ERROR, "An error occurred while running xrdp-waitforx");
return 0;
rv = (enum xwait_status)WEXITSTATUS(ret);
}
else if (WIFSIGNALED(ret))
{
int sig = WTERMSIG(ret);
LOG(LOG_LEVEL_ERROR, "waitforx failed with unexpected signal %d",
sig);
}


return 1;
return rv;
}
13 changes: 11 additions & 2 deletions sesman/xwait.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
#ifndef XWAIT_H
#define XWAIT_H

enum xwait_status
{
XW_STATUS_OK = 0,
XW_STATUS_MISC_ERROR,
XW_STATUS_TIMED_OUT,
XW_STATUS_FAILED_TO_START
};

/**
*
* @brief waits for X to start
* @param display number
* @return 0 on error, 1 if X has outputs
* @return status
*
*/
int
enum xwait_status
wait_for_xserver(int display);
#endif
12 changes: 7 additions & 5 deletions waitforx/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
bin_PROGRAMS = \
xrdp-waitforx
pkglibexec_PROGRAMS = \
waitforx

AM_LDFLAGS = -lX11 -lXrandr

AM_CPPFLAGS = -I$(top_srcdir)/common
AM_CPPFLAGS = \
-I$(top_srcdir)/sesman \
-I$(top_srcdir)/common

AM_CFLAGS = $(X_CFLAGS)

xrdp_waitforx_SOURCES = waitforx.c
waitforx_SOURCES = waitforx.c

xrdp_waitforx_LDADD = \
waitforx_LDADD = \
$(top_builddir)/common/libcommon.la
Loading

0 comments on commit 42be576

Please sign in to comment.