From 9c05c99a8c280031592cb090808ce386705af9f9 Mon Sep 17 00:00:00 2001 From: YoungHun Kim Date: Thu, 18 Oct 2018 18:41:43 +0900 Subject: Update to wait until the child processe called by fork() completes Change-Id: Ia55631de7747a91d34a308c46cc22e099cec604e --- server/include/muse_server_private.h | 4 ++- server/src/muse_server.c | 49 ++++++++++++++++++++++++++++++------ server/src/muse_server_private.c | 11 ++++++-- 3 files changed, 54 insertions(+), 10 deletions(-) (limited to 'server') diff --git a/server/include/muse_server_private.h b/server/include/muse_server_private.h index 29da4e8..18c9eed 100644 --- a/server/include/muse_server_private.h +++ b/server/include/muse_server_private.h @@ -43,6 +43,8 @@ extern "C" { #define MS_TIMEOUT_MSEC 1000 #define MS_RECV_TRY_COUNT_MAX 3 +#define MSG_DONE "DONE" + gboolean ms_ipc_job_function(ms_workqueue_job_t *job); gboolean ms_ipc_data_job_function(ms_workqueue_job_t *job); @@ -89,7 +91,7 @@ ms_module_t *ms_get_module_instance(int idx); int ms_deinit(void); void ms_check_memory(int pid); void ms_new(void); -int ms_run(void); +int ms_run(int notify_fd); void ms_cmd_dispatch(muse_module_h m, muse_module_command_e cmd); void ms_exit_worker(muse_module_h m); void ms_respawn(int signo); diff --git a/server/src/muse_server.c b/server/src/muse_server.c index 3920c6e..de876cb 100644 --- a/server/src/muse_server.c +++ b/server/src/muse_server.c @@ -28,7 +28,8 @@ #endif static void _ms_setup_syslog(void); -static pid_t _ms_daemonize(void); +static void _ms_fork(int *notify_fd); +static pid_t _ms_daemonize(int *notify_fd); static void _ms_gst_init(char **cmd); static int _ms_pidfile_create(const char *path, pid_t pid); @@ -42,19 +43,52 @@ static void _ms_setup_syslog(void) LOGD("openlog - mused"); } -static pid_t _ms_daemonize(void) +static void _ms_fork(int *notify_fd) { pid_t pid; - int fd, result; + int result, fds[2]; char err_msg[MUSE_MSG_LEN_MAX] = {'\0',}; + char msg[MUSE_MSG_LEN_MAX] = {'\0',}; + + if (pipe(fds) == MUSE_ERR) { + strerror_r(errno, err_msg, MUSE_MSG_LEN_MAX); + LOGE("Failed to create pipe to get child status: %s", err_msg); + exit(EXIT_FAILURE); + } if ((pid = fork()) < 0) { strerror_r(errno, err_msg, MUSE_MSG_LEN_MAX); LOGE("Error: fork() failed: %s", err_msg); - exit(EXIT_SUCCESS); + exit(EXIT_FAILURE); } else if (pid != 0) { - exit(EXIT_SUCCESS); + close(fds[1]); + /* Read in a string from the pipe */ + result = read(fds[0], msg, sizeof(msg)); + close(fds[0]); + + /* Parent process closes up output side of pipe */ + if (!strcmp(msg, MSG_DONE)) { + LOGI("Successfully daemonized"); + exit(EXIT_SUCCESS); + } else { + LOGE("Daemonizing failed after fork"); + exit(EXIT_FAILURE); + } + } else if (pid == 0) { + /* Child process closes up input side of pipe */ + close(fds[0]); + *notify_fd = fds[1]; } +} + +static pid_t _ms_daemonize(int *notify_fd) +{ + pid_t pid; + int fd, result; + + muse_return_val_if_fail(notify_fd, MUSE_ERR); + + _ms_fork(notify_fd); if ((pid = setsid()) < 0) { LOGE("create new session"); @@ -401,6 +435,7 @@ int main(int argc, char **argv) { pid_t pid; int idx; + int notify_fd = -1; muse_module_cmd_dispatchfunc *cmd_dispatcher = NULL; #ifdef MUSE_TTRACE_LOG @@ -409,7 +444,7 @@ int main(int argc, char **argv) _ms_setup_syslog(); - pid = _ms_daemonize(); + pid = _ms_daemonize(¬ify_fd); if (_ms_pidfile_create(MUSE_DEFAULT_PIDFILE, pid) != MM_ERROR_NONE) exit(EXIT_FAILURE); @@ -448,5 +483,5 @@ int main(int argc, char **argv) #ifdef MUSE_TTRACE_LOG trace_end(); #endif - return ms_run(); + return ms_run(notify_fd); } diff --git a/server/src/muse_server_private.c b/server/src/muse_server_private.c index 67f4c9a..d24255c 100644 --- a/server/src/muse_server_private.c +++ b/server/src/muse_server_private.c @@ -298,6 +298,8 @@ static gboolean _ms_connection_handler(GIOChannel *source, GIOCondition conditio } client_len = sizeof(client_address); + + LOGI("[%d] Try to accept...", server_sockfd); client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, &client_len); if (!muse_core_fd_is_valid(client_sockfd)) { LOGE("Critical Error : accept %d is invalid", client_sockfd); @@ -693,7 +695,7 @@ void ms_new(void) _ms_create_new_server_from_fd(fd, READ | PERSIST); } -int ms_run(void) +int ms_run(int notify_fd) { char err_msg[MUSE_MSG_LEN_MAX] = {'\0',}; GError *error = NULL; @@ -704,6 +706,7 @@ int ms_run(void) LOGW("Enter"); + muse_return_val_if_fail(muse_core_fd_is_valid(notify_fd), MUSE_ERR); muse_return_val_if_fail(_ms_run() == MM_ERROR_NONE, MUSE_ERR); muse_return_val_if_fail(muse_server, MUSE_ERR); @@ -753,10 +756,13 @@ int ms_run(void) LOGE("Fail to subscribe external storage state change"); #endif + write(notify_fd, MSG_DONE, strlen(MSG_DONE) + 1); + LOGI("[%d] Notify parent process that child initialization is done", notify_fd); + close(notify_fd); + #ifdef MUSE_USE_WATCHDOG if (ms_watchdog_attach(muse_server->watchdog)) { LOGW("g_main_loop_run"); - g_main_loop_run(muse_server->main_loop); ms_watchdog_detach(muse_server->watchdog); @@ -765,6 +771,7 @@ int ms_run(void) ms_log_process_info(muse_server->pid); } #else + LOGW("g_main_loop_run"); g_main_loop_run(muse_server->main_loop); #endif -- cgit v1.2.3