[FIX] daemon Oops on host connection broken
authorVitaliy Cherepanov <v.cherepanov@samsung.com>
Thu, 24 Oct 2013 16:43:27 +0000 (20:43 +0400)
committerVitaliy Cherepanov <v.cherepanov@samsung.com>
Fri, 25 Oct 2013 13:15:05 +0000 (17:15 +0400)
normal way termination by term kill for daemon

Change-Id: If18590aa97f105fb689971a35680f8f6bf2bd21c
Signed-off-by: Vitaliy Cherepanov <v.cherepanov@samsung.com>
daemon/da_protocol.c
daemon/da_protocol.h
daemon/daemon.c
daemon/main.c
daemon/threads.c

index cc06545..f57b1f3 100644 (file)
@@ -52,7 +52,7 @@
 #include <fcntl.h>
 #include <unistd.h>
 
-
+static pthread_mutex_t stop_all_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 void inline free_msg(struct msg_t *msg)
 {
@@ -794,13 +794,12 @@ struct msg_t *gen_stop_msg(void)
        return res;
 }
 
-enum ErrorCode stop_all(void)
+
+enum ErrorCode stop_all_no_lock(void)
 {
        enum ErrorCode error_code = ERR_NO;
        struct msg_t *msg;
 
-       LOGI("entry\n");
-
        // stop all only if it has not been called yet
        if (check_running_status(&prof_session)) {
                msg = gen_stop_msg();
@@ -832,6 +831,28 @@ stop_all_exit:
        LOGI("finished: ret = %d\n", error_code);
        return error_code;
 }
+
+int stop_all_in_process(void)
+{
+       return (pthread_mutex_trylock(&stop_all_mutex) != 0);
+}
+
+void stop_all_done(void)
+{
+       pthread_mutex_unlock(&stop_all_mutex);
+}
+
+enum ErrorCode stop_all(void)
+{
+       enum ErrorCode error_code = ERR_NO;
+
+       pthread_mutex_lock(&stop_all_mutex);
+       error_code = stop_all_no_lock();
+       pthread_mutex_unlock(&stop_all_mutex);
+
+       return error_code;
+}
+
 struct binary_ack {
        uint32_t type;
        char *binpath;
@@ -844,6 +865,7 @@ static void binary_ack_free(struct binary_ack *ba)
                free(ba->binpath);
        free(ba);
 }
+
 static size_t binary_ack_size(const struct binary_ack *ba)
 {
        /* MD5 is 16 bytes, so 16*2 hex digits */
index d4d92fb..18cfb80 100644 (file)
@@ -374,7 +374,11 @@ void free_msg_payload(struct msg_t *msg);
 void free_sys_info(struct system_info_t *sys_info);
 int start_replay(void);
 void stop_replay(void);
+
 enum ErrorCode stop_all(void);
+enum ErrorCode stop_all_no_lock(void);
+int stop_all_in_process(void);
+void stop_all_done(void);
 
 void reset_msg(struct msg_t *msg);
 void reset_replay_event_seq(struct replay_event_seq_t *res);
index 3607c3a..b6a76a6 100644 (file)
@@ -502,7 +502,9 @@ void terminate_all()
        // wait for all other thread exit
        for (i = 0; i < MAX_TARGET_COUNT; i++) {
                if (manager.target[i].recv_thread != -1) {
+                       LOGI("join recv thread [%d] is started\n", i);
                        pthread_join(manager.target[i].recv_thread, NULL);
+                       LOGI("join recv thread %d. done\n", i);
                }
        }
 }
index ddb071c..1c85cee 100644 (file)
@@ -366,14 +366,34 @@ static void remove_buf_modules(void)
 
 static void terminate(int sig)
 {
-       _unlink_files();
-       _close_server_socket();
-       exit_buf();
-       remove_buf_modules();
-       if (sig != 0) {
-               LOGW("Terminating due signal %s\n", strsignal(sig));
-               signal(sig, SIG_DFL);
-               raise(sig);
+       LOGI("terminate! sig = %d\n", sig);
+       if (!stop_all_in_process()) {
+               // we are up there if signal accept and stop_all func was not
+               // called yet.
+
+               // so we need stop_all firstly (if profiling was
+               // not started it will be dummy call) and release other sources
+               stop_all_no_lock();
+               _unlink_files();
+               _close_server_socket();
+               exit_buf();
+               remove_buf_modules();
+               if (sig != 0) {
+                       LOGW("Terminating due signal %s\n", strsignal(sig));
+                       signal(sig, SIG_DFL);
+                       raise(sig);
+               }
+               stop_all_done();
+       } else {
+               // we are there if stop_all function was called by some reasons
+
+               // if stop_all called we cannot call remove_buf_modules and
+               // other funcs because of threads are not stopped yet
+               LOGW("Stop in progress\n");
+               if (sig != 0) {
+                       LOGW("ignore signal %s\n", strsignal(sig));
+                       signal(sig, SIG_IGN);
+               }
        }
 }
 
index 3c43d84..642a241 100644 (file)
@@ -335,7 +335,9 @@ int samplingStop()
                setitimer(ITIMER_REAL, &stopval, NULL);
 
                pthread_kill(manager.sampling_thread, SIGUSR1);
+               LOGI("join sampling thread started\n");
                pthread_join(manager.sampling_thread, NULL);
+               LOGI("join sampling thread done\n");
 
                manager.sampling_thread = -1;
        }