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 19 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
144 changes: 88 additions & 56 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 @@ -2045,18 +2046,6 @@ g_obj_wait(tintptr *read_objs, int rcount, tintptr *write_objs, int wcount,
void
g_random(char *data, int len)
{
#if defined(_WIN32)
int index;

srand(g_time1());

for (index = 0; index < len; index++)
{
data[index] = (char)rand(); /* rand returns a number between 0 and
RAND_MAX */
}

#else
int fd;

memset(data, 0x44, len);
Expand All @@ -2075,8 +2064,6 @@ g_random(char *data, int len)

close(fd);
}

#endif
}

/*****************************************************************************/
Expand Down Expand Up @@ -2601,6 +2588,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 @@ -3778,51 +3782,21 @@ g_check_user_in_group(const char *username, int gid, int *ok)
#endif // HAVE_GETGROUPLIST

/*****************************************************************************/
/* returns the time since the Epoch (00:00:00 UTC, January 1, 1970),
measured in seconds.
for windows, returns the number of seconds since the machine was
started. */
int
g_time1(void)
{
#if defined(_WIN32)
return GetTickCount() / 1000;
#else
return time(0);
#endif
}

/*****************************************************************************/
/* returns the number of milliseconds since the machine was
started. */
int
g_time2(void)
unsigned int
g_get_elapsed_ms(void)
{
#if defined(_WIN32)
return (int)GetTickCount();
#else
struct tms tm;
clock_t num_ticks = 0;
g_memset(&tm, 0, sizeof(struct tms));
num_ticks = times(&tm);
return (int)(num_ticks * 10);
#endif
}
unsigned int result = 0;
struct timespec tp;

/*****************************************************************************/
/* returns time in milliseconds, uses gettimeofday
does not work in win32 */
int
g_time3(void)
{
#if defined(_WIN32)
return 0;
#else
struct timeval tp;
if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
{
result = (unsigned int)tp.tv_sec * 1000;
// POSIX 1003.1-2004 specifies that tv_nsec is a long (i.e. a
// signed type), but can only contain [0..999,999,999]
result += tp.tv_nsec / 1000000;
}

gettimeofday(&tp, 0);
return (tp.tv_sec * 1000) + (tp.tv_usec / 1000);
#endif
return result;
}

/******************************************************************************/
Expand Down Expand Up @@ -4243,3 +4217,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;
}
37 changes: 34 additions & 3 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 @@ -393,9 +394,26 @@ int g_getgroup_info(const char *groupname, int *gid);
* Primary group of username is also checked
*/
int g_check_user_in_group(const char *username, int gid, int *ok);
int g_time1(void);
int g_time2(void);
int g_time3(void);

/**
* Gets elapsed milliseconds since some arbitrary point in the past
*
* The returned value is unaffected by leap-seconds or time zone changes.
*
* @return elaped ms since some arbitrary point
*
* Calculate the duration of a task by calling this routine before and
* after the task, and subtracting the two values.
*
* The value wraps every so often (every 49.7 days on a 32-bit system),
* but as we are using unsigned arithmetic, the difference of any of these
* two values can be used to calculate elapsed time, whether-or-not a wrap
* occurs during the interval - provided of course the time being measured
* is less than the total wrap-around interval.
*/
unsigned int
g_get_elapsed_ms(void);

int g_save_to_bmp(const char *filename, char *data, int stride_bytes,
int width, int height, int depth, int bits_per_pixel);
void *g_shmat(int shmid);
Expand All @@ -411,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
12 changes: 6 additions & 6 deletions common/trans.c
Original file line number Diff line number Diff line change
Expand Up @@ -696,18 +696,18 @@ local_connect_shim(int fd, const char *server, const char *port)
/**************************************************************************//**
* Waits for an asynchronous connect to complete.
* @param self - Transport object
* @param start_time Start time of connect (from g_time3())
* @param start_time Start time of connect (from g_get_elapsed_ms())
* @param timeout Total wait timeout
* @return 0 - connect succeeded, 1 - Connect failed
*
* If the transport is set up for checking a termination object, this
* on a regular basis.
*/
static int
poll_for_async_connect(struct trans *self, int start_time, int timeout)
poll_for_async_connect(struct trans *self, unsigned int start_time, int timeout)
{
int rv = 1;
int ms_remaining = timeout - (g_time3() - start_time);
int ms_remaining = timeout - (int)(g_get_elapsed_ms() - start_time);

while (ms_remaining > 0)
{
Expand Down Expand Up @@ -736,7 +736,7 @@ poll_for_async_connect(struct trans *self, int start_time, int timeout)
break;
}

ms_remaining = timeout - (g_time3() - start_time);
ms_remaining = timeout - (int)(g_get_elapsed_ms() - start_time);
}
return rv;
}
Expand All @@ -747,7 +747,7 @@ int
trans_connect(struct trans *self, const char *server, const char *port,
int timeout)
{
int start_time = g_time3();
unsigned int start_time = g_get_elapsed_ms();
int error;
int ms_before_next_connect;

Expand Down Expand Up @@ -826,7 +826,7 @@ trans_connect(struct trans *self, const char *server, const char *port,
}

/* Have we reached the total timeout yet? */
int ms_left = timeout - (g_time3() - start_time);
int ms_left = timeout - (int)(g_get_elapsed_ms() - start_time);
if (ms_left <= 0)
{
error = 1;
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
Loading
Loading