Update to wait until the child processe called by fork() completes 63/191563/11
authorYoungHun Kim <yh8004.kim@samsung.com>
Thu, 18 Oct 2018 09:41:43 +0000 (18:41 +0900)
committerYoungHun Kim <yh8004.kim@samsung.com>
Mon, 22 Oct 2018 04:22:09 +0000 (13:22 +0900)
Change-Id: Ia55631de7747a91d34a308c46cc22e099cec604e

server/include/muse_server_private.h
server/src/muse_server.c
server/src/muse_server_private.c

index 29da4e8ba859e5ceb450e32571cc4d9a83d02509..18c9eed34b98d1a5aa142802c2503abccc600a9d 100644 (file)
@@ -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);
index 3920c6e1df7595937f7f15f34bff9853cef2b7bf..de876cba2d1660ac41e7a9da5b4ea251037127ee 100644 (file)
@@ -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(&notify_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);
 }
index 67f4c9a58f553814adac9ee2b7f8e2209123f282..d24255c65b41b9e6cd89b37cdf88ebf8849de3a2 100644 (file)
@@ -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