From 62bf3c61f530024284c56a126a407c537220e28f Mon Sep 17 00:00:00 2001 From: Jay Sorg Date: Sat, 26 May 2012 21:20:19 -0700 Subject: [PATCH] xrdp: pid file fixes --- common/os_calls.c | 38 ++++++++++++++++++++++++++++++++++++++ common/os_calls.h | 2 ++ xrdp/xrdp.c | 32 +++++++++++++++++++------------- 3 files changed, 59 insertions(+), 13 deletions(-) diff --git a/common/os_calls.c b/common/os_calls.c index 65c6808a..211adc82 100644 --- a/common/os_calls.c +++ b/common/os_calls.c @@ -1404,6 +1404,44 @@ g_create_dir(const char* dirname) #endif } +/*****************************************************************************/ +/* will try to create directories up to last / in name + example /tmp/a/b/c/readme.txt will try to create /tmp/a/b/c + returns boolean */ +int APP_CC +g_create_path(const char* path) +{ + char* pp; + char* sp; + char* copypath; + int status; + + status = 1; + copypath = g_strdup(path); + pp = copypath; + sp = strchr(pp, '/'); + while (sp != 0) + { + if (sp != pp) + { + *sp = 0; + if (!g_directory_exist(copypath)) + { + if (!g_create_dir(copypath)) + { + status = 0; + break; + } + } + *sp = '/'; + } + pp = sp + 1; + sp = strchr(pp, '/'); + } + g_free(copypath); + return status; +} + /*****************************************************************************/ /* returns boolean */ int APP_CC diff --git a/common/os_calls.h b/common/os_calls.h index 009a9079..ddcb59d8 100644 --- a/common/os_calls.h +++ b/common/os_calls.h @@ -161,6 +161,8 @@ g_directory_exist(const char* dirname); int APP_CC g_create_dir(const char* dirname); int APP_CC +g_create_path(const char* path); +int APP_CC g_remove_dir(const char* dirname); int APP_CC g_file_delete(const char* filename); diff --git a/xrdp/xrdp.c b/xrdp/xrdp.c index 2975502e..95936cef 100644 --- a/xrdp/xrdp.c +++ b/xrdp/xrdp.c @@ -330,7 +330,7 @@ main(int argc, char** argv) } if (fd == -1) { - g_writeln("problem opening to xrdp.pid"); + g_writeln("problem opening to xrdp.pid [%s]", pid_file); g_writeln("maybe its not running"); } else @@ -389,6 +389,10 @@ main(int argc, char** argv) } if (!no_daemon) { + + /* make sure containing directory exists */ + g_create_path(pid_file); + /* make sure we can write to pid file */ fd = g_file_open(pid_file); /* xrdp.pid */ if (fd == -1) @@ -424,16 +428,6 @@ main(int argc, char** argv) g_exit(0); } g_sleep(1000); - g_file_close(0); - g_file_close(1); - g_file_close(2); - g_file_open("/dev/null"); - g_file_open("/dev/null"); - g_file_open("/dev/null"); - /* end of daemonizing code */ - } - if (!no_daemon) - { /* write the pid to file */ pid = g_getpid(); fd = g_file_open(pid_file); /* xrdp.pid */ @@ -449,6 +443,14 @@ main(int argc, char** argv) g_file_write(fd, text, g_strlen(text)); g_file_close(fd); } + g_sleep(1000); + g_file_close(0); + g_file_close(1); + g_file_close(2); + g_file_open("/dev/null"); + g_file_open("/dev/null"); + g_file_open("/dev/null"); + /* end of daemonizing code */ } g_threadid = tc_get_threadid(); g_listen = xrdp_listen_create(); @@ -475,8 +477,12 @@ main(int argc, char** argv) tc_mutex_delete(g_sync1_mutex); g_delete_wait_obj(g_term_event); g_delete_wait_obj(g_sync_event); - /* delete the xrdp.pid file */ - g_file_delete(pid_file); + /* only main process should delete pid file */ + if ((!no_daemon) && (pid == g_getpid())) + { + /* delete the xrdp.pid file */ + g_file_delete(pid_file); + } g_free(startup_params); g_deinit(); return 0;