Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add restartability to sesman #3346

Open
wants to merge 16 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions common/os_calls.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#define ctid_t id_t
#endif
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
Expand Down Expand Up @@ -2588,6 +2589,23 @@ g_executable_exist(const char *exename)
return access(exename, R_OK | X_OK) == 0;
}

/*****************************************************************************/
/* returns boolean, non zero if the socket exists */
int
g_socket_exist(const char *sockname)
{
struct stat st;

if (stat(sockname, &st) == 0)
{
return S_ISSOCK(st.st_mode);
}
else
{
return 0;
}
}

/*****************************************************************************/
/* returns boolean */
int
Expand Down Expand Up @@ -4200,3 +4218,61 @@ g_qsort(void *base, size_t nitems, size_t size,
{
qsort(base, nitems, size, compar);
}

/*****************************************************************************/
struct list *
g_readdir(const char *dir)
{
DIR *handle;
struct list *result = NULL;
struct dirent *dent;
int saved_errno;

errno = 0; // See readdir(3)
if ((handle = opendir(dir)) != NULL &&
(result = list_create()) != NULL)
{
result->auto_free = 1;
while (1)
{
errno = 0;
dent = readdir(handle);
if (dent == NULL)
{
break; // errno = 0 for end-of-dir, or != 0 for error
}

// Ignore '.' and '..'
if (dent->d_name[0] == '.' && dent->d_name[1] == '\0')
{
continue;
}
if (dent->d_name[0] == '.' && dent->d_name[1] == '.' &&
dent->d_name[2] == '\0')
{
continue;
}

if (!list_add_strdup(result, dent->d_name))
{
// Memory allocation failure
errno = ENOMEM;
break;
}
}
}

saved_errno = errno;
if (errno != 0)
{
list_delete(result);
result = NULL;
}
if (handle != NULL)
{
closedir(handle);
}
errno = saved_errno;

return result;
}
14 changes: 14 additions & 0 deletions common/os_calls.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ int g_file_exist(const char *filename);
int g_file_readable(const char *filename);
int g_directory_exist(const char *dirname);
int g_executable_exist(const char *dirname);
int g_socket_exist(const char *dirname);
int g_create_dir(const char *dirname);
int g_create_path(const char *path);
int g_remove_dir(const char *dirname);
Expand Down Expand Up @@ -428,6 +429,19 @@ void
g_qsort(void *base, size_t nitems, size_t size,
int (*compar)(const void *, const void *));

/**
* Returns a list of the filenames contained within a directory
*
* @param dir Name of directory
* @return list of directory entry names
*
* If NULL is returned, further information may be available in errno. No
* other errors are specifically logged.
* The special files '.' and '..' are not returned.
*/
struct list *
g_readdir(const char *dir);

/* glib-style wrappers */
#define g_new(struct_type, n_structs) \
(struct_type *) malloc(sizeof(struct_type) * (n_structs))
Expand Down
7 changes: 7 additions & 0 deletions instfiles/xrdp.service.in
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ EnvironmentFile=-@sysconfdir@/default/xrdp
ExecStart=@sbindir@/xrdp $XRDP_OPTIONS --nodaemon
SystemCallArchitectures=native
SystemCallFilter=@system-service
# Uncomment the following line if you wish xrdp connections to survive
# an xrdp restart.
#
# This is not recommended, as the xrdp and xrdp-sesman processes can
# end up with API differences if the restart was caused by an upgrade. It
# may however be useful for some use cases.
#KillMode=process

[Install]
WantedBy=multi-user.target
31 changes: 27 additions & 4 deletions libipm/ercp.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,32 @@ ercp_trans_from_eicp_trans(struct trans *trans,

/*****************************************************************************/

struct trans *
ercp_connect(const char *port, int (*term_func)(void))
{
struct trans *t;

if ((t = trans_create(TRANS_MODE_UNIX, 128, 128)) != NULL)
{
t->is_term = term_func;

if (trans_connect(t, NULL, port, 3000) != 0)
{
trans_delete(t);
t = NULL;
}
else if (ercp_init_trans(t) != 0)
{
trans_delete(t);
t = NULL;
}
}

return t;
}

/*****************************************************************************/

int
ercp_msg_in_check_available(struct trans *trans, int *available)
{
Expand Down Expand Up @@ -182,10 +208,7 @@ ercp_get_session_announce_event(struct trans *trans,

if (rv == 0)
{
if (display != NULL)
{
*display = i_display;
}
*display = i_display;
*uid = (uid_t)i_uid;
*type = (enum scp_session_type)i_type;
*start_width = i_width;
Expand Down
11 changes: 10 additions & 1 deletion libipm/ercp.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ ercp_trans_from_eicp_trans(struct trans *trans,
ttrans_data_in callback_func,
void *callback_data);

/**
* Connects to an ERCP endpoint.
*
* @param port Path to UDS object in filesystem
* @param term_func Function to poll during connection for program
* termination, or NULL for none.
*/
struct trans *
ercp_connect(const char *port,
int (*term_func)(void));

/**
* Checks an ERCP transport to see if a complete message is
Expand Down Expand Up @@ -178,7 +188,6 @@ ercp_send_session_announce_event(struct trans *trans,
*
* @param trans EICP transport
* @param[out] display Display used by session.
* Pointer can be NULL if this is already known.
* @param[out] uid UID of user logged in to session
* @param[out] type Session type
* @param[out] start_width Starting width of seenio
Expand Down
2 changes: 2 additions & 0 deletions sesman/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ xrdp_sesman_SOURCES = \
sesexec_control.h \
session_list.c \
session_list.h \
sesman_restart.c \
sesman_restart.h \
sig.c \
sig.h

Expand Down
17 changes: 16 additions & 1 deletion sesman/ercp_process.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ process_session_announce_event(struct session_item *si)
{
int rv;
const char *start_ip_addr;
unsigned int display;

rv = ercp_get_session_announce_event(si->sesexec_trans,
NULL,
&display,
&si->uid,
&si->type,
&si->start_width,
Expand All @@ -53,10 +54,24 @@ process_session_announce_event(struct session_item *si)
&si->guid,
&start_ip_addr,
&si->start_time);
if (rv == 0)
{
// We may already know the display we sent sesexec. If we do,
// check sesexec sent the same value back.
if (si->display >= 0 && display != (unsigned int)si->display)
{
LOG(LOG_LEVEL_ERROR, "Bugcheck: sesman expected display %d, got %u",
si->display, display);
rv = 1;
}
}

if (rv == 0)
{
snprintf(si->start_ip_addr, sizeof(si->start_ip_addr),
"%s", start_ip_addr);
si->display = display;

si->state = E_SESSION_RUNNING;
}

Expand Down
5 changes: 4 additions & 1 deletion sesman/libsesman/sesman_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,11 @@ struct config_sesman
/**
* @var listen_port
* @brief Listening port
*
* This string is used to form the restart directory name, so
* can't be the full XRDP_SOCKETS_MAXPATH length.
*/
char listen_port[XRDP_SOCKETS_MAXPATH];
char listen_port[XRDP_SOCKETS_MAXPATH - 10];
/**
* @var enable_user_wm
* @brief Flag that enables user specific wm
Expand Down
2 changes: 2 additions & 0 deletions sesman/sesexec/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ xrdp_sesexec_SOURCES = \
env.h \
login_info.c \
login_info.h \
sesexec_discover.c \
sesexec_discover.h \
sessionrecord.c \
sessionrecord.h \
xauth.c \
Expand Down
Loading
Loading