From cce937bce655210060b15f2401795a53770c520a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gyuhwan=20Park=E2=98=85?= Date: Tue, 21 Feb 2023 08:00:38 +0900 Subject: [PATCH 01/15] feature(common/os_calls): add g_fork_execvp --- common/os_calls.c | 15 +++++++++++++++ common/os_calls.h | 1 + 2 files changed, 16 insertions(+) diff --git a/common/os_calls.c b/common/os_calls.c index c6bc7e7bbd..d4c63b29c7 100644 --- a/common/os_calls.c +++ b/common/os_calls.c @@ -2921,6 +2921,21 @@ g_fork(void) #endif } +/*****************************************************************************/ +int +g_fork_execvp(const char *p1, char *args[]) +{ + int pid; + + pid = g_fork(); + + if (pid == 0) { + g_execvp(p1, args); + } + + return pid; +} + /*****************************************************************************/ /* does not work in win32 */ int diff --git a/common/os_calls.h b/common/os_calls.h index 5304ae6640..1e9d4577cc 100644 --- a/common/os_calls.h +++ b/common/os_calls.h @@ -258,6 +258,7 @@ void g_signal_terminate(void (*func)(int)); void g_signal_pipe(void (*func)(int)); void g_signal_usr1(void (*func)(int)); int g_fork(void); +int g_fork_execvp(const char *p1, char *args[]); int g_setgid(int pid); int g_initgroups(const char *user); int g_getuid(void); From 5303f09ca89b4244fbc7c95858e37e0f1841915b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gyuhwan=20Park=E2=98=85?= Date: Tue, 21 Feb 2023 08:00:50 +0900 Subject: [PATCH 02/15] feature(common/os_calls): add g_get_executable_path --- common/os_calls.c | 19 +++++++++++++++++++ common/os_calls.h | 1 + 2 files changed, 20 insertions(+) diff --git a/common/os_calls.c b/common/os_calls.c index d4c63b29c7..e5fa9783d9 100644 --- a/common/os_calls.c +++ b/common/os_calls.c @@ -100,6 +100,10 @@ extern char **environ; #include #endif +#if defined(__APPLE__) +#include +#endif + /* for solaris */ #if !defined(PF_LOCAL) #define PF_LOCAL AF_UNIX @@ -3064,6 +3068,21 @@ g_set_allusercontext(int uid) return (rv != 0); /* Return 0 or 1 */ } #endif + +/*****************************************************************************/ +void +g_get_executable_path(char *buf, int bufsize) +{ +#if defined(__APPLE__) + uint32_t _bufsize = bufsize; + _NSGetExecutablePath(buf, &_bufsize); +#else + LOG(LOG_LEVEL_WARN, "g_get_executable_path(): not implemented yet!"); + buf = strdup("xrdp"); +#endif +} + + /*****************************************************************************/ /* does not work in win32 returns pid of process that exits or zero if signal occurred */ diff --git a/common/os_calls.h b/common/os_calls.h index 1e9d4577cc..886c31ca53 100644 --- a/common/os_calls.h +++ b/common/os_calls.h @@ -274,6 +274,7 @@ int g_setlogin(const char *name); */ int g_set_allusercontext(int uid); #endif +void g_get_executable_path(char *buf, int bufsize); int g_waitchild(void); int g_waitpid(int pid); struct exit_status g_waitpid_status(int pid); From 483253726b513dc9f4975a61343c23ea121b7b85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gyuhwan=20Park=E2=98=85?= Date: Tue, 21 Feb 2023 08:12:59 +0900 Subject: [PATCH 03/15] fix(xrdp_listen): use g_fork_execvp instead of g_fork to keep xpc connection working --- xrdp/xrdp_listen.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/xrdp/xrdp_listen.c b/xrdp/xrdp_listen.c index e91f98bfec..52abdc961d 100644 --- a/xrdp/xrdp_listen.c +++ b/xrdp/xrdp_listen.c @@ -771,16 +771,33 @@ xrdp_listen_process_startup_params(struct xrdp_listen *self) static int xrdp_listen_fork(struct xrdp_listen *self, struct trans *server_trans) { + char server_trans_fd_str[32]; + char executable_path[4096]; + struct list *child_arguments; + int pid; int index; struct xrdp_process *process; struct trans *ltrans; - pid = g_fork(); + g_snprintf(server_trans_fd_str, 32, "%d", (int) server_trans->sck); + + child_arguments = list_create(); + list_add_item(child_arguments, (intptr_t) g_strdup("--child-process")); + list_add_item(child_arguments, (intptr_t) g_strdup("--child-fd")); + list_add_item(child_arguments, (intptr_t) g_strdup(server_trans_fd_str)); + + g_get_executable_path(executable_path, 4096); + + pid = g_fork_execvp(executable_path, (char **) child_arguments->items); + + list_delete(child_arguments); if (pid == 0) { /* child */ + /* unreachable code */ + /* recreate some main globals */ xrdp_child_fork(); /* recreate the process done wait object, not used in fork mode */ From 4709e90dd1b5245a5a3946505af7ecc011eca490 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gyuhwan=20Park=E2=98=85?= Date: Tue, 21 Feb 2023 08:13:44 +0900 Subject: [PATCH 04/15] checkpoint(xrdp_process): implement child-process entrypoint --- xrdp/xrdp.h | 2 ++ xrdp/xrdp_process.c | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/xrdp/xrdp.h b/xrdp/xrdp.h index 004add1c31..9c62add323 100644 --- a/xrdp/xrdp.h +++ b/xrdp/xrdp.h @@ -184,6 +184,8 @@ void xrdp_process_delete(struct xrdp_process *self); int xrdp_process_main_loop(struct xrdp_process *self); +int +xrdp_process_child_entrypoint(struct xrdp_listen *owner); /* xrdp_listen.c */ struct xrdp_listen * diff --git a/xrdp/xrdp_process.c b/xrdp/xrdp_process.c index bf0555d647..a7d70e788d 100644 --- a/xrdp/xrdp_process.c +++ b/xrdp/xrdp_process.c @@ -312,3 +312,22 @@ xrdp_process_main_loop(struct xrdp_process *self) g_set_wait_obj(self->done_event); return 0; } + +/*****************************************************************************/ +int +xrdp_process_child_entrypoint(struct xrdp_listen *owner) +{ + struct trans *server_trans; + struct xrdp_process *process; + + // FIXME + server_trans = trans_create(TRANS_MODE_TCP, 16, 16); + server_trans->sck = 0; + + process = xrdp_process_create(owner, 0); + process->server_trans = server_trans; + + xrdp_process_main_loop(process); + + return 0; +} From 051ef1ac3a31753c5899a3eb226482fc99cc8855 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gyuhwan=20Park=E2=98=85?= Date: Sat, 18 Mar 2023 15:15:31 +0900 Subject: [PATCH 05/15] fix(common/os_calls): log error and quit application when execvp(3) fails --- common/os_calls.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/common/os_calls.c b/common/os_calls.c index e5fa9783d9..7bcb546c61 100644 --- a/common/os_calls.c +++ b/common/os_calls.c @@ -2932,9 +2932,16 @@ g_fork_execvp(const char *p1, char *args[]) int pid; pid = g_fork(); - + if (pid == 0) { g_execvp(p1, args); + + /* should not get here */ + LOG(LOG_LEVEL_ERROR, + "Failed to execute %s: execvp(3) failed with %s (%d)", + p1, g_get_strerror(), g_get_errno()); + + exit(1); } return pid; From 50beaaa7208b51cd336237ee364a2e577cedd29b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gyuhwan=20Park=E2=98=85?= Date: Sat, 18 Mar 2023 15:40:14 +0900 Subject: [PATCH 06/15] fix(common/os_calls): improve usability of g_get_executable_path --- common/os_calls.c | 34 +++++++++++++++++++++++++++++----- common/os_calls.h | 10 +++++++++- xrdp/xrdp_listen.c | 2 +- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/common/os_calls.c b/common/os_calls.c index 7bcb546c61..0a86185f42 100644 --- a/common/os_calls.c +++ b/common/os_calls.c @@ -3078,15 +3078,39 @@ g_set_allusercontext(int uid) /*****************************************************************************/ void -g_get_executable_path(char *buf, int bufsize) +g_get_executable_path(enum xrdp_exe xe, char *buf, int bufsize) { + int rv = -1; #if defined(__APPLE__) uint32_t _bufsize = bufsize; - _NSGetExecutablePath(buf, &_bufsize); -#else - LOG(LOG_LEVEL_WARN, "g_get_executable_path(): not implemented yet!"); - buf = strdup("xrdp"); #endif + + g_memset(buf, '\0', bufsize); + +#if defined(__APPLE__) + rv = _NSGetExecutablePath(buf, &_bufsize); +#elif defined(__linux__) + rv = readlink("/proc/self/exe", buf, bufsize); +#endif + + if (rv > 0) + { + return; + } + + // build executable path manually + switch (xe) + { + case E_XE_XRDP: + g_snprintf(buf, bufsize, XRDP_SBIN_PATH "/xrdp"); + break; + case E_XE_SESMAN: + g_snprintf(buf, bufsize, XRDP_SBIN_PATH "/xrdp-sesman"); + break; + + default: + LOG(LOG_LEVEL_WARNING, "g_get_executable_path(): Unsupported exe %d", (int)xe); + } } diff --git a/common/os_calls.h b/common/os_calls.h index 886c31ca53..f3604c9183 100644 --- a/common/os_calls.h +++ b/common/os_calls.h @@ -23,6 +23,14 @@ #include "arch.h" +enum xrdp_exe +{ + E_XE_XRDP = 0, + E_XE_SESMAN = 1, + + // TODO: add others below +}; + struct exit_status { /* set to -1 when the process exited via a signal */ @@ -274,7 +282,7 @@ int g_setlogin(const char *name); */ int g_set_allusercontext(int uid); #endif -void g_get_executable_path(char *buf, int bufsize); +void g_get_executable_path(enum xrdp_exe xe, char *buf, int bufsize); int g_waitchild(void); int g_waitpid(int pid); struct exit_status g_waitpid_status(int pid); diff --git a/xrdp/xrdp_listen.c b/xrdp/xrdp_listen.c index 52abdc961d..210b6049ab 100644 --- a/xrdp/xrdp_listen.c +++ b/xrdp/xrdp_listen.c @@ -787,7 +787,7 @@ xrdp_listen_fork(struct xrdp_listen *self, struct trans *server_trans) list_add_item(child_arguments, (intptr_t) g_strdup("--child-fd")); list_add_item(child_arguments, (intptr_t) g_strdup(server_trans_fd_str)); - g_get_executable_path(executable_path, 4096); + g_get_executable_path(E_XE_XRDP, executable_path, 4096); pid = g_fork_execvp(executable_path, (char **) child_arguments->items); From 1072dc80814af04d2719f8f520fbf8c8c280a137 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gyuhwan=20Park=E2=98=85?= Date: Sat, 18 Mar 2023 16:03:13 +0900 Subject: [PATCH 07/15] refactor(xrdp_listen_fork): use list_add_strdup_multi, set auto_free for child_arguments --- xrdp/xrdp_listen.c | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/xrdp/xrdp_listen.c b/xrdp/xrdp_listen.c index 210b6049ab..4f574a72c7 100644 --- a/xrdp/xrdp_listen.c +++ b/xrdp/xrdp_listen.c @@ -779,19 +779,36 @@ xrdp_listen_fork(struct xrdp_listen *self, struct trans *server_trans) int index; struct xrdp_process *process; struct trans *ltrans; - + + g_get_executable_path(E_XE_XRDP, executable_path, 4096); g_snprintf(server_trans_fd_str, 32, "%d", (int) server_trans->sck); - + child_arguments = list_create(); - list_add_item(child_arguments, (intptr_t) g_strdup("--child-process")); - list_add_item(child_arguments, (intptr_t) g_strdup("--child-fd")); - list_add_item(child_arguments, (intptr_t) g_strdup(server_trans_fd_str)); - - g_get_executable_path(E_XE_XRDP, executable_path, 4096); - - pid = g_fork_execvp(executable_path, (char **) child_arguments->items); - - list_delete(child_arguments); + + if (child_arguments != NULL) + { + child_arguments->auto_free = 1; + if (!list_add_strdup_multi(child_arguments, + "--child-process", + "--child-fd", server_trans_fd_str, + NULL)) + { + list_delete(child_arguments); + child_arguments = NULL; + } + } + + if (!child_arguments) + { + LOG(LOG_LEVEL_ERROR, "xrdp_listen_fork(): could not prepare arguments list for child process"); + return 1; + } + else + { + pid = g_fork_execvp(executable_path, (char **) child_arguments->items); + list_delete(child_arguments); + } + if (pid == 0) { From 95258d58ab64620a37b14a50b1b274b55acf0a64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gyuhwan=20Park=E2=98=85?= Date: Sat, 18 Mar 2023 16:52:42 +0900 Subject: [PATCH 08/15] fix(common/os_calls): respect coding style --- common/os_calls.c | 19 ++++++++++--------- common/os_calls.h | 4 ++-- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/common/os_calls.c b/common/os_calls.c index 0a86185f42..d84bc07d5e 100644 --- a/common/os_calls.c +++ b/common/os_calls.c @@ -2932,15 +2932,16 @@ g_fork_execvp(const char *p1, char *args[]) int pid; pid = g_fork(); - - if (pid == 0) { + + if (pid == 0) + { g_execvp(p1, args); - + /* should not get here */ LOG(LOG_LEVEL_ERROR, "Failed to execute %s: execvp(3) failed with %s (%d)", p1, g_get_strerror(), g_get_errno()); - + exit(1); } @@ -3084,20 +3085,20 @@ g_get_executable_path(enum xrdp_exe xe, char *buf, int bufsize) #if defined(__APPLE__) uint32_t _bufsize = bufsize; #endif - + g_memset(buf, '\0', bufsize); - + #if defined(__APPLE__) rv = _NSGetExecutablePath(buf, &_bufsize); #elif defined(__linux__) rv = readlink("/proc/self/exe", buf, bufsize); #endif - + if (rv > 0) { return; } - + // build executable path manually switch (xe) { @@ -3107,7 +3108,7 @@ g_get_executable_path(enum xrdp_exe xe, char *buf, int bufsize) case E_XE_SESMAN: g_snprintf(buf, bufsize, XRDP_SBIN_PATH "/xrdp-sesman"); break; - + default: LOG(LOG_LEVEL_WARNING, "g_get_executable_path(): Unsupported exe %d", (int)xe); } diff --git a/common/os_calls.h b/common/os_calls.h index f3604c9183..e683037b73 100644 --- a/common/os_calls.h +++ b/common/os_calls.h @@ -27,8 +27,8 @@ enum xrdp_exe { E_XE_XRDP = 0, E_XE_SESMAN = 1, - - // TODO: add others below + + /* TODO: add others below */ }; struct exit_status From c61fe0e0aace620c6f87c0fd91c5befff602b65f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gyuhwan=20Park=E2=98=85?= Date: Sat, 18 Mar 2023 17:34:45 +0900 Subject: [PATCH 09/15] checkpoint(xrdp): add entrypoint for child process --- xrdp/xrdp.c | 32 ++++++++++++++++++++++++++++++++ xrdp/xrdp.h | 2 +- xrdp/xrdp_listen.c | 11 ++++++----- xrdp/xrdp_process.c | 5 ++--- xrdp/xrdp_types.h | 2 ++ 5 files changed, 43 insertions(+), 9 deletions(-) diff --git a/xrdp/xrdp.c b/xrdp/xrdp.c index 508b0cd106..48dbbe1f50 100644 --- a/xrdp/xrdp.c +++ b/xrdp/xrdp.c @@ -213,6 +213,15 @@ xrdp_process_params(int argc, char **argv, index++; startup_params->xrdp_ini = value; } + else if (nocase_matches(option, "--child-process", NULL)) + { + startup_params->is_child = 1; + } + else if (nocase_matches(option, "--child-fd", NULL)) + { + index++; + startup_params->child_fd = g_atoi(value); + } else /* unknown option */ { return index; @@ -377,6 +386,29 @@ main(int argc, char **argv) g_exit(0); } + if (startup_params.is_child) + { + if (startup_params.child_fd < 3) + { + g_writeln("requested act as child process, but --child-fd option is not given"); + g_deinit(); + g_exit(1); + } + + g_set_threadid(tc_get_threadid()); + g_listen = xrdp_listen_create(); + g_signal_user_interrupt(xrdp_shutdown); /* SIGINT */ + g_signal_pipe(xrdp_sig_no_op); /* SIGPIPE */ + g_signal_terminate(xrdp_shutdown); /* SIGTERM */ + g_signal_child_stop(xrdp_child); /* SIGCHLD */ + g_signal_hang_up(xrdp_sig_no_op); /* SIGHUP */ + + xrdp_process_child_entrypoint(g_listen, startup_params.child_fd); + + g_deinit(); + g_exit(0); + } + /* starting logging subsystem */ error = log_start(startup_params.xrdp_ini, "xrdp", (startup_params.dump_config) ? LOG_START_DUMP_CONFIG : 0); diff --git a/xrdp/xrdp.h b/xrdp/xrdp.h index 9c62add323..452b615636 100644 --- a/xrdp/xrdp.h +++ b/xrdp/xrdp.h @@ -185,7 +185,7 @@ xrdp_process_delete(struct xrdp_process *self); int xrdp_process_main_loop(struct xrdp_process *self); int -xrdp_process_child_entrypoint(struct xrdp_listen *owner); +xrdp_process_child_entrypoint(struct xrdp_listen *owner, int socket_fd); /* xrdp_listen.c */ struct xrdp_listen * diff --git a/xrdp/xrdp_listen.c b/xrdp/xrdp_listen.c index 4f574a72c7..d47aef6c2f 100644 --- a/xrdp/xrdp_listen.c +++ b/xrdp/xrdp_listen.c @@ -779,14 +779,15 @@ xrdp_listen_fork(struct xrdp_listen *self, struct trans *server_trans) int index; struct xrdp_process *process; struct trans *ltrans; - + g_get_executable_path(E_XE_XRDP, executable_path, 4096); g_snprintf(server_trans_fd_str, 32, "%d", (int) server_trans->sck); - + child_arguments = list_create(); - + if (child_arguments != NULL) { + /* FIXME: pass log_fd to child */ child_arguments->auto_free = 1; if (!list_add_strdup_multi(child_arguments, "--child-process", @@ -797,7 +798,7 @@ xrdp_listen_fork(struct xrdp_listen *self, struct trans *server_trans) child_arguments = NULL; } } - + if (!child_arguments) { LOG(LOG_LEVEL_ERROR, "xrdp_listen_fork(): could not prepare arguments list for child process"); @@ -808,7 +809,7 @@ xrdp_listen_fork(struct xrdp_listen *self, struct trans *server_trans) pid = g_fork_execvp(executable_path, (char **) child_arguments->items); list_delete(child_arguments); } - + if (pid == 0) { diff --git a/xrdp/xrdp_process.c b/xrdp/xrdp_process.c index a7d70e788d..1cb52886e2 100644 --- a/xrdp/xrdp_process.c +++ b/xrdp/xrdp_process.c @@ -315,14 +315,13 @@ xrdp_process_main_loop(struct xrdp_process *self) /*****************************************************************************/ int -xrdp_process_child_entrypoint(struct xrdp_listen *owner) +xrdp_process_child_entrypoint(struct xrdp_listen *owner, int socket_fd) { struct trans *server_trans; struct xrdp_process *process; - // FIXME server_trans = trans_create(TRANS_MODE_TCP, 16, 16); - server_trans->sck = 0; + server_trans->sck = socket_fd; process = xrdp_process_create(owner, 0); process->server_trans = server_trans; diff --git a/xrdp/xrdp_types.h b/xrdp/xrdp_types.h index a1ced8c2d3..ae4a56b513 100644 --- a/xrdp/xrdp_types.h +++ b/xrdp/xrdp_types.h @@ -684,6 +684,8 @@ struct xrdp_startup_params int tcp_nodelay; int tcp_keepalive; int use_vsock; + int is_child; + int child_fd; }; /* From ba28ad047c8f29dc128ec31ad0212fabc66439ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gyuhwan=20Park=E2=98=85?= Date: Sat, 18 Mar 2023 17:51:19 +0900 Subject: [PATCH 10/15] fix(xrdp_listen): pass "xrdp" as ARGV[0] --- xrdp/xrdp_listen.c | 1 + 1 file changed, 1 insertion(+) diff --git a/xrdp/xrdp_listen.c b/xrdp/xrdp_listen.c index d47aef6c2f..492b218ad1 100644 --- a/xrdp/xrdp_listen.c +++ b/xrdp/xrdp_listen.c @@ -790,6 +790,7 @@ xrdp_listen_fork(struct xrdp_listen *self, struct trans *server_trans) /* FIXME: pass log_fd to child */ child_arguments->auto_free = 1; if (!list_add_strdup_multi(child_arguments, + "xrdp", "--child-process", "--child-fd", server_trans_fd_str, NULL)) From b859041048a155ce425f50d8f00a608f0e311c8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gyuhwan=20Park=E2=98=85?= Date: Sat, 18 Mar 2023 18:14:32 +0900 Subject: [PATCH 11/15] fix(xrdp): set g_listen->startup_params --- xrdp/xrdp.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/xrdp/xrdp.c b/xrdp/xrdp.c index 48dbbe1f50..aada3a2bd6 100644 --- a/xrdp/xrdp.c +++ b/xrdp/xrdp.c @@ -403,6 +403,8 @@ main(int argc, char **argv) g_signal_child_stop(xrdp_child); /* SIGCHLD */ g_signal_hang_up(xrdp_sig_no_op); /* SIGHUP */ + g_listen->startup_params = &startup_params; + xrdp_process_child_entrypoint(g_listen, startup_params.child_fd); g_deinit(); From 5d99ba9c0efb99bdb5bd6c1aaf00ce85e27eb2b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gyuhwan=20Park=E2=98=85?= Date: Sat, 18 Mar 2023 18:15:25 +0900 Subject: [PATCH 12/15] fix(xrdp_process_child_entrypoint): manually set trans->status to TRANS_STATUS_UP --- xrdp/xrdp_process.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/xrdp/xrdp_process.c b/xrdp/xrdp_process.c index 1cb52886e2..a3c3f91d48 100644 --- a/xrdp/xrdp_process.c +++ b/xrdp/xrdp_process.c @@ -321,6 +321,7 @@ xrdp_process_child_entrypoint(struct xrdp_listen *owner, int socket_fd) struct xrdp_process *process; server_trans = trans_create(TRANS_MODE_TCP, 16, 16); + server_trans->status = TRANS_STATUS_UP; server_trans->sck = socket_fd; process = xrdp_process_create(owner, 0); @@ -328,5 +329,7 @@ xrdp_process_child_entrypoint(struct xrdp_listen *owner, int socket_fd) xrdp_process_main_loop(process); + xrdp_process_delete(process); + g_set_term(1); return 0; } From 95226f0046a9f3277dbc19257c42806efe52e3df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gyuhwan=20Park=E2=98=85?= Date: Sat, 18 Mar 2023 18:19:47 +0900 Subject: [PATCH 13/15] xxx(xrdp): make execvp-ed child processes work and break logging subsystem instead TODO: implement log_restart_from(int log_fd, ...) --- xrdp/xrdp.c | 48 +++++++++++++++++++++++------------------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/xrdp/xrdp.c b/xrdp/xrdp.c index aada3a2bd6..92aed64b08 100644 --- a/xrdp/xrdp.c +++ b/xrdp/xrdp.c @@ -386,31 +386,6 @@ main(int argc, char **argv) g_exit(0); } - if (startup_params.is_child) - { - if (startup_params.child_fd < 3) - { - g_writeln("requested act as child process, but --child-fd option is not given"); - g_deinit(); - g_exit(1); - } - - g_set_threadid(tc_get_threadid()); - g_listen = xrdp_listen_create(); - g_signal_user_interrupt(xrdp_shutdown); /* SIGINT */ - g_signal_pipe(xrdp_sig_no_op); /* SIGPIPE */ - g_signal_terminate(xrdp_shutdown); /* SIGTERM */ - g_signal_child_stop(xrdp_child); /* SIGCHLD */ - g_signal_hang_up(xrdp_sig_no_op); /* SIGHUP */ - - g_listen->startup_params = &startup_params; - - xrdp_process_child_entrypoint(g_listen, startup_params.child_fd); - - g_deinit(); - g_exit(0); - } - /* starting logging subsystem */ error = log_start(startup_params.xrdp_ini, "xrdp", (startup_params.dump_config) ? LOG_START_DUMP_CONFIG : 0); @@ -439,7 +414,30 @@ main(int argc, char **argv) g_exit(1); } + if (startup_params.is_child) + { + if (startup_params.child_fd < 3) + { + g_writeln("requested act as child process, but --child-fd option is not given"); + g_deinit(); + g_exit(1); + } + + g_set_threadid(tc_get_threadid()); + g_listen = xrdp_listen_create(); + g_signal_user_interrupt(xrdp_shutdown); /* SIGINT */ + g_signal_pipe(xrdp_sig_no_op); /* SIGPIPE */ + g_signal_terminate(xrdp_shutdown); /* SIGTERM */ + g_signal_child_stop(xrdp_child); /* SIGCHLD */ + g_signal_hang_up(xrdp_sig_no_op); /* SIGHUP */ + + g_listen->startup_params = &startup_params; + + xrdp_process_child_entrypoint(g_listen, startup_params.child_fd); + g_deinit(); + g_exit(0); + } if (g_file_exist(pid_file)) /* xrdp.pid */ { From 3b1627dcd2af2814ecbf9bee47f6e33e7ca7c00a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gyuhwan=20Park=E2=98=85?= Date: Sat, 18 Mar 2023 19:58:54 +0900 Subject: [PATCH 14/15] refactor(common/os_calls): build executable path for xe, not current --- common/os_calls.c | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/common/os_calls.c b/common/os_calls.c index d84bc07d5e..7de977f982 100644 --- a/common/os_calls.c +++ b/common/os_calls.c @@ -100,10 +100,6 @@ extern char **environ; #include #endif -#if defined(__APPLE__) -#include -#endif - /* for solaris */ #if !defined(PF_LOCAL) #define PF_LOCAL AF_UNIX @@ -3081,25 +3077,6 @@ g_set_allusercontext(int uid) void g_get_executable_path(enum xrdp_exe xe, char *buf, int bufsize) { - int rv = -1; -#if defined(__APPLE__) - uint32_t _bufsize = bufsize; -#endif - - g_memset(buf, '\0', bufsize); - -#if defined(__APPLE__) - rv = _NSGetExecutablePath(buf, &_bufsize); -#elif defined(__linux__) - rv = readlink("/proc/self/exe", buf, bufsize); -#endif - - if (rv > 0) - { - return; - } - - // build executable path manually switch (xe) { case E_XE_XRDP: From fc7209dde1a4708f75e04c13681c5dddfd5e81a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gyuhwan=20Park=E2=98=85?= Date: Sat, 18 Mar 2023 19:59:37 +0900 Subject: [PATCH 15/15] refactor(xrdp_listen): remove unreachable code --- xrdp/xrdp_listen.c | 32 -------------------------------- 1 file changed, 32 deletions(-) diff --git a/xrdp/xrdp_listen.c b/xrdp/xrdp_listen.c index 492b218ad1..c3b2887fd3 100644 --- a/xrdp/xrdp_listen.c +++ b/xrdp/xrdp_listen.c @@ -811,38 +811,6 @@ xrdp_listen_fork(struct xrdp_listen *self, struct trans *server_trans) list_delete(child_arguments); } - - if (pid == 0) - { - /* child */ - /* unreachable code */ - - /* recreate some main globals */ - xrdp_child_fork(); - /* recreate the process done wait object, not used in fork mode */ - /* close, don't delete this */ - g_close_wait_obj(self->pro_done_event); - xrdp_listen_create_pro_done(self); - /* delete listener, child need not listen */ - for (index = 0; index < self->trans_list->count; index++) - { - ltrans = (struct trans *) list_get_item(self->trans_list, index); - trans_delete_from_child(ltrans); - } - list_delete(self->trans_list); - self->trans_list = NULL; - /* new connect instance */ - process = xrdp_process_create(self, 0); - process->server_trans = server_trans; - g_process = process; - xrdp_process_run(0); - tc_sem_dec(g_process_sem); - xrdp_process_delete(process); - /* mark this process to exit */ - g_set_term(1); - return 1; - } - /* parent */ trans_delete(server_trans); return 0;