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 shared memory file to rediscover sessions #800 #819

Closed
Closed
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
77 changes: 77 additions & 0 deletions common/os_calls.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
#include <pwd.h>
#include <time.h>
#include <grp.h>
#include <sys/mman.h>
#endif

#include <stdlib.h>
Expand Down Expand Up @@ -1474,6 +1475,17 @@ g_sleep(int msecs)
#endif
}

/*****************************************************************************/
void
g_sleep_secs(int secs)
{
#if defined(_WIN32)
Sleep(secs * 1000);
#else
sleep(secs);
#endif
}

/*****************************************************************************/
int
g_sck_last_error_would_block(int sck)
Expand Down Expand Up @@ -2105,6 +2117,17 @@ g_memcmp(const void *s1, const void *s2, int len)
return memcmp(s1, s2, len);
}

/*****************************************************************************/
int
g_ftruncate(int fd, int length)
{
#if defined(_WIN32)
return -1;
#else
return ftruncate(fd, length);
#endif
}

/*****************************************************************************/
/* returns -1 on error, else return handle or file descriptor */
int
Expand Down Expand Up @@ -2246,6 +2269,31 @@ g_file_seek(int fd, int offset)
#endif
}

/*****************************************************************************/
/* move file pointer relative to end of file,
* returns offset on success, -1 on failure */
int
g_file_seek_rel_end(int fd, int offset)
{
#if defined(_WIN32)
int rv;

rv = (int)SetFilePointer((HANDLE)fd, offset, 0, FILE_END);

if (rv == (int)INVALID_SET_FILE_POINTER)
{
return -1;
}
else
{
return rv;
}

#else
return (int)lseek(fd, offset, SEEK_END);
#endif
}

/*****************************************************************************/
/* do a write lock on a file */
/* return boolean */
Expand Down Expand Up @@ -3044,6 +3092,35 @@ g_get_proc_address(long lib, const char *name)
#endif
}

/*****************************************************************************/
/* does not work in win32 */
void *
g_map_file_shared(int fd, int length)
{
void *mapped;

mapped = mmap(NULL,
length,
PROT_READ|PROT_WRITE,
MAP_SHARED,
fd,
0);

/* MAP_FAILED != NULL but mmap() will not return 0 unless MAP_FIXED is
* specified. */
if (mapped == MAP_FAILED)
mapped = NULL;
return mapped;
}

/*****************************************************************************/
/* does not work in win32 */
int
g_unmap_file_shared(void *mapped, int length)
{
return munmap(mapped, length);
}

/*****************************************************************************/
/* does not work in win32 */
int
Expand Down
5 changes: 5 additions & 0 deletions common/os_calls.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ int g_sck_can_recv(int sck, int millis);
int g_sck_select(int sck1, int sck2);
void g_write_ip_address(int rcv_sck, char* ip_address, int bytes);
void g_sleep(int msecs);
void g_sleep_secs(int secs);
tintptr g_create_wait_obj(const char *name);
tintptr g_create_wait_obj_from_socket(tintptr socket, int write);
void g_delete_wait_obj_from_socket(tintptr wait_obj);
Expand All @@ -98,13 +99,15 @@ int g_obj_wait(tintptr* read_objs, int rcount, tintptr* write_objs,
void g_random(char* data, int len);
int g_abs(int i);
int g_memcmp(const void* s1, const void* s2, int len);
int g_ftruncate(int fd, int length);
int g_file_open(const char* file_name);
int g_file_open_ex(const char *file_name, int aread, int awrite,
int acreate, int atrunc);
int g_file_close(int fd);
int g_file_read(int fd, char* ptr, int len);
int g_file_write(int fd, const char *ptr, int len);
int g_file_seek(int fd, int offset);
int g_file_seek_rel_end(int fd, int offset);
int g_file_lock(int fd, int start, int len);
int g_chmod_hex(const char* filename, int flags);
int g_chown(const char* name, int uid, int gid);
Expand Down Expand Up @@ -142,6 +145,8 @@ int g_strtrim(char* str, int trim_flags);
long g_load_library(char* in);
int g_free_library(long lib);
void* g_get_proc_address(long lib, const char* name);
void* g_map_file_shared(int fd, int length);
int g_unmap_file_shared(void *mapped, int length);
int g_system(char* aexec);
char* g_get_strerror(void);
int g_get_errno(void);
Expand Down
44 changes: 44 additions & 0 deletions common/thread_calls.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,34 @@ tc_mutex_create(void)
#endif
}

/*****************************************************************************/
int
tc_shared_mutex_create(tbus mutex)
{
#if defined(_WIN32)
/* Not implemented yet */
return 1;
#else
pthread_mutexattr_t mutexattr;
int rc;

rc = pthread_mutexattr_init(&mutexattr);
if (rc != 0)
{
return 1;
}

rc = pthread_mutexattr_setpshared(&mutexattr, PTHREAD_PROCESS_SHARED);
if (rc != 0)
{
return 1;
}

rc = pthread_mutex_init((pthread_mutex_t *)mutex, &mutexattr);
return rc;
#endif
}

/*****************************************************************************/
void
tc_mutex_delete(tbus mutex)
Expand Down Expand Up @@ -140,6 +168,22 @@ tc_mutex_lock(tbus mutex)
#endif
}

/*****************************************************************************/
int
tc_mutex_timed_lock(tbus mutex, int timeout_ms)
{
#if defined(_WIN32)
WaitForSingleObject((HANDLE)mutex, timeout_ms);
return 0;
#else
struct timespec timeout;
timeout.tv_sec = timeout_ms / 1000;
timeout.tv_nsec = ((long)timeout_ms % 1000) * 1000;

return pthread_mutex_timedlock((pthread_mutex_t *)mutex, &timeout);
#endif
}

/*****************************************************************************/
int
tc_mutex_unlock(tbus mutex)
Expand Down
4 changes: 4 additions & 0 deletions common/thread_calls.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@ int
tc_threadid_equal(tbus tid1, tbus tid2);
tbus
tc_mutex_create(void);
int
tc_shared_mutex_create(tbus mutex);
void
tc_mutex_delete(tbus mutex);
int
tc_mutex_lock(tbus mutex);
int
tc_mutex_timed_lock(tbus mutex, int timeout_ms);
int
tc_mutex_unlock(tbus mutex);
tbus
tc_sem_create(int init_count);
Expand Down
2 changes: 2 additions & 0 deletions sesman/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ xrdp_sesman_SOURCES = \
scp_v1_mng.h \
sesman.c \
sesman.h \
sesshm.c \
sesshm.h \
session.c \
session.h \
sig.c \
Expand Down
8 changes: 8 additions & 0 deletions sesman/sesman.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#endif

#include "sesman.h"
#include "sesshm.h"

int g_sck;
int g_pid;
Expand Down Expand Up @@ -432,6 +433,12 @@ main(int argc, char **argv)
g_file_close(fd);
}

if (session_init_shared())
ben-cohen marked this conversation as resolved.
Show resolved Hide resolved
log_message(LOG_LEVEL_ERROR,
"error opening session shm file[%s]: %s",
SESMAN_SHAREDMEM_FILENAME, g_get_strerror());


/* start program main loop */
log_message(LOG_LEVEL_INFO,
"starting xrdp-sesman with pid %d", g_pid);
Expand Down Expand Up @@ -460,6 +467,7 @@ main(int argc, char **argv)
{
g_file_delete(pid_file);
}
session_close_shared();

g_delete_wait_obj(g_term_event);

Expand Down
Loading