[FIX] wrong pack size of system memory total/used
[platform/core/system/swap-manager.git] / daemon / da_protocol.c
index efceee6..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)
 {
@@ -543,6 +543,21 @@ static void reset_conf(struct conf_t *conf)
        memset(conf, 0, sizeof(*conf));
 }
 
+static void running_status_on(struct prof_session_t *prof_session)
+{
+       prof_session->running_status = 1;
+}
+
+static void running_status_off(struct prof_session_t *prof_session)
+{
+       prof_session->running_status = 0;
+}
+
+int check_running_status(struct prof_session_t *prof_session)
+{
+       return prof_session->running_status;
+}
+
 static void reset_app_inst(struct user_space_inst_t *us_inst)
 {
        free_data_list((struct data_list_t **)&us_inst->app_inst_list);
@@ -586,6 +601,7 @@ static void reset_prof_session(struct prof_session_t *prof_session)
        reset_conf(&prof_session->conf);
        reset_user_space_inst(&prof_session->user_space_inst);
        reset_replay_event_seq(&prof_session->replay_event_seq);
+       running_status_off(prof_session);
 }
 
 static struct msg_t *gen_binary_info_reply(struct app_info_t *app_info)
@@ -753,7 +769,7 @@ int sendACKToHost(enum HostMessageT resp, enum ErrorCode err_code,
                //copy payload data
                memcpy(p, payload, payload_size);
 
-               LOGI("ACK (%s) errcode<%s> payload=%d; size=%d\n", msg_ID_str(resp),
+               LOGI("ACK (%s) errcode<%s> payload=0x%08X; size=%d\n", msg_ID_str(resp),
                                msgErrStr(err_code), (int)payload, payload_size);
                printBuf((char *)msg, loglen);
 
@@ -778,33 +794,65 @@ struct msg_t *gen_stop_msg(void)
        return res;
 }
 
-enum ErrorCode stop_all(void)
+
+enum ErrorCode stop_all_no_lock(void)
 {
-       LOGI("entry\n");
        enum ErrorCode error_code = ERR_NO;
-       struct msg_t *msg = gen_stop_msg();
+       struct msg_t *msg;
 
-       terminate_all();
-       stop_profiling();
+       // stop all only if it has not been called yet
+       if (check_running_status(&prof_session)) {
+               msg = gen_stop_msg();
+               terminate_all();
+               stop_profiling();
 
-       if (msg == NULL) {
-               LOGE("cannot generate stop message\n");
-               return ERR_UNKNOWN;
-       } else {
-               if (ioctl_send_msg(msg) != 0) {
-                       LOGE("ioctl send filed\n");
+               if (msg == NULL) {
+                       LOGE("cannot generate stop message\n");
                        error_code = ERR_UNKNOWN;
+                       goto stop_all_exit;
+               } else {
+                       if (ioctl_send_msg(msg) != 0) {
+                               LOGE("ioctl send failed\n");
+                               error_code = ERR_UNKNOWN;
+                               free_msg(msg);
+                               goto stop_all_exit;
+                       }
+                       free_msg(msg);
                }
-               free_msg(msg);
-       }
 
-       //we reset only app inst no lib no confing reset
-       reset_app_inst(&prof_session.user_space_inst);
-       stop_transfer();
+               // we reset only app inst no lib no confing reset
+               reset_app_inst(&prof_session.user_space_inst);
+               stop_transfer();
+               running_status_off(&prof_session);
+       } else
+               LOGI("already stopped\n");
 
-       LOGI("finished\n");
+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;
@@ -817,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 */
@@ -951,7 +1000,7 @@ static int process_msg_start(struct msg_buf_t *msg_control)
        }
 
        if (msg_start(msg_control, &prof_session.user_space_inst,
-                     &msg_reply) != 0) {
+                     &msg_reply, &err_code) != 0) {
                LOGE("parse error\n");
                goto send_ack;
        }
@@ -966,8 +1015,14 @@ static int process_msg_start(struct msg_buf_t *msg_control)
                goto send_ack;
        }
 
+       running_status_on(&prof_session);
+
        if (start_profiling() < 0) {
                LOGE("cannot start profiling\n");
+               if (stop_all() != ERR_NO) {
+                       LOGE("Stop failed\n");
+                       write_msg_error("Stop failed");
+               }
                goto send_ack;
        }
 
@@ -1004,8 +1059,10 @@ int host_message_handler(struct msg_t *msg)
                return process_msg_start(&msg_control);
        case NMSG_STOP:
                sendACKToHost(msg->id, ERR_NO, 0, 0);
-               if (stop_all() != ERR_NO)
+               if (stop_all() != ERR_NO) {
+                       LOGE("Stop failed\n");
                        write_msg_error("Stop failed");
+               }
                break;
        case NMSG_CONFIG:
                error_code = ERR_NO;
@@ -1044,9 +1101,9 @@ int host_message_handler(struct msg_t *msg)
        case NMSG_BINARY_INFO:
                return process_msg_binary_info(&msg_control);
        case NMSG_SWAP_INST_ADD:
-               if (msg_swap_inst_add(&msg_control, &prof_session.user_space_inst, &msg_reply) != 0) {
+               if (msg_swap_inst_add(&msg_control, &prof_session.user_space_inst,
+                                     &msg_reply, &error_code) != 0) {
                        LOGE("swap inst add\n");
-                       error_code = ERR_UNKNOWN;
                        goto send_ack;
                }
                if (msg_reply != NULL)
@@ -1057,7 +1114,8 @@ int host_message_handler(struct msg_t *msg)
                //send ack to host
                goto send_ack;
        case NMSG_SWAP_INST_REMOVE:
-               if (msg_swap_inst_remove(&msg_control, &prof_session.user_space_inst, &msg_reply) != 0) {
+               if (msg_swap_inst_remove(&msg_control, &prof_session.user_space_inst,
+                                        &msg_reply, &error_code) != 0) {
                        LOGE("swap inst remove\n");
                        error_code = ERR_UNKNOWN;
                        goto send_ack;