[REFACTOR] add functions start_app_launch_timer stop_app_launch_timer
[platform/core/system/swap-manager.git] / daemon / daemon.c
index 8c36a5e..325abff 100644 (file)
@@ -69,6 +69,7 @@
 
 #define EPOLL_SIZE                             10
 #define MAX_APP_LAUNCH_TIME            60
+#define MAX_CONNECT_TIMEOUT_TIME       5*60
 
 #define MAX_DEVICE                             10
 #define MAX_FILENAME                   128
@@ -248,34 +249,63 @@ static void setEmptyTargetSlot(int index)
 }
 
 // =============================================================================
-// send functions to host
+// start and terminate control functions
 // =============================================================================
 
-//static
-int sendACKCodeToHost(enum HostMessageType resp, int msgcode)
+//start application launch timer function
+static int start_app_launch_timer()
 {
-       // FIXME:
-       //disabled string protocol
-       return 0;
-       if (manager.host.control_socket != -1)
-       {
-               char codestr[16];
-               char logstr[DA_MSG_MAX];
-               int loglen, codelen;
-
-               codelen = sprintf(codestr, "%d", msgcode);
-               loglen = sprintf(logstr, "%d|%d|%s", (int)resp, codelen, codestr);
+       int res = 0;
+       struct epoll_event ev;
 
-               send(manager.host.control_socket, logstr, loglen, MSG_NOSIGNAL);
-               return 0;
+       manager.app_launch_timerfd = timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC);
+       if(manager.app_launch_timerfd > 0)
+       {
+               struct itimerspec ctime;
+               ctime.it_value.tv_sec = MAX_APP_LAUNCH_TIME;
+               ctime.it_value.tv_nsec = 0;
+               ctime.it_interval.tv_sec = 0;
+               ctime.it_interval.tv_nsec = 0;
+               if (timerfd_settime(manager.app_launch_timerfd, 0, &ctime, NULL) < 0)
+               {
+                       LOGE("fail to set app launch timer\n");
+                       close(manager.app_launch_timerfd);
+                       manager.app_launch_timerfd = -1;
+                       res = -1;
+               }
+               else
+               {
+                       // add event fd to epoll list
+                       ev.events = EPOLLIN;
+                       ev.data.fd = manager.app_launch_timerfd;
+                       if (epoll_ctl(manager.efd, EPOLL_CTL_ADD,
+                                               manager.app_launch_timerfd, &ev) < 0)
+                       {
+                               // fail to add event fd
+                               LOGE("fail to add app launch timer fd to epoll list\n");
+                               close(manager.app_launch_timerfd);
+                               manager.app_launch_timerfd = -1;
+                               res = -2;
+                       } else {
+                               LOGI("application launch time started\n");
+                       }
+               }
+       } else {
+               LOGE("cannot create launch timer\n");
+               res = -3;
        }
-       else
-               return 1;
+
+       return res;
 }
 
-// =============================================================================
-// start and terminate control functions
-// =============================================================================
+//stop application launch timer
+static int stop_app_launch_timer()
+{
+       epoll_ctl(manager.efd, EPOLL_CTL_DEL,manager.app_launch_timerfd, NULL);
+       close(manager.app_launch_timerfd);
+       manager.app_launch_timerfd = -1;
+       return 0;
+}
 
 static int exec_app(const struct app_info_t *app_info)
 {
@@ -309,40 +339,51 @@ static int exec_app(const struct app_info_t *app_info)
                break;
        }
 
-       if (res == 0 && app_info->app_type != APP_TYPE_RUNNING) {
-               manager.app_launch_timerfd = timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC);
-               if(manager.app_launch_timerfd > 0)
+       if (res == 0 && app_info->app_type != APP_TYPE_RUNNING)
+               if (start_app_launch_timer()<0)
+                       res = -1;
+
+       LOGI("ret=%d\n", res);
+       return res;
+}
+
+int launch_timer_start()
+{
+       static struct epoll_event ev;
+       int res = 0;
+
+       manager.connect_timeout_timerfd = timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC);
+       if(manager.connect_timeout_timerfd > 0)
+       {
+               struct itimerspec ctime;
+               ctime.it_value.tv_sec = MAX_CONNECT_TIMEOUT_TIME;
+               ctime.it_value.tv_nsec = 0;
+               ctime.it_interval.tv_sec = 0;
+               ctime.it_interval.tv_nsec = 0;
+               if (timerfd_settime(manager.connect_timeout_timerfd, 0, &ctime, NULL) < 0)
                {
-                       struct itimerspec ctime;
-                       ctime.it_value.tv_sec = MAX_APP_LAUNCH_TIME;
-                       ctime.it_value.tv_nsec = 0;
-                       ctime.it_interval.tv_sec = 0;
-                       ctime.it_interval.tv_nsec = 0;
-                       if (timerfd_settime(manager.app_launch_timerfd, 0, &ctime, NULL) < 0)
-                       {
-                               LOGE("fail to set app launch timer\n");
-                               close(manager.app_launch_timerfd);
-                               manager.app_launch_timerfd = -1;
-                       }
-                       else
+                       LOGE("fail to set connect timeout timer\n");
+                       close(manager.connect_timeout_timerfd);
+                       manager.connect_timeout_timerfd = -1;
+               }
+               else
+               {
+                       // add event fd to epoll list
+                       ev.events = EPOLLIN;
+                       ev.data.fd = manager.connect_timeout_timerfd;
+                       if (epoll_ctl(manager.efd, EPOLL_CTL_ADD,
+                                               manager.connect_timeout_timerfd, &ev) < 0)
                        {
-                               // add event fd to epoll list
-                               ev.events = EPOLLIN;
-                               ev.data.fd = manager.app_launch_timerfd;
-                               if (epoll_ctl(manager.efd, EPOLL_CTL_ADD,
-                                                       manager.app_launch_timerfd, &ev) < 0)
-                               {
-                                       // fail to add event fd
-                                       LOGE("fail to add app launch timer fd to epoll list\n");
-                                       close(manager.app_launch_timerfd);
-                                       manager.app_launch_timerfd = -1;
-                               } else {
-                                       LOGI("application launch time started\n");
-                               }
+                               // fail to add event fd
+                               LOGE("fail to add app connection timeout timer fd to epoll list\n");
+                               close(manager.connect_timeout_timerfd);
+                               manager.connect_timeout_timerfd = -1;
+                       } else {
+                               LOGI("connection timeout timer started\n");
                        }
-               } else {
-                       LOGE("cannot create launch timer\n");
                }
+       } else {
+               LOGE("cannot create connection timeout timer\n");
        }
 
        LOGI("ret=%d\n", res);
@@ -550,7 +591,7 @@ static int deviceEventHandler(input_dev* dev, int input_type)
        else
        {
                LOGW("unknown input_type\n");
-               ret = 1;
+               ret = 1; // it is not error
        }
        return ret;
 }
@@ -668,9 +709,8 @@ static int targetServerHandler(int efd)
                if(manager.app_launch_timerfd >= 0)
                {
                        LOGI("release launch timer\n");
-                       epoll_ctl(efd, EPOLL_CTL_DEL, manager.app_launch_timerfd, NULL);
-                       close(manager.app_launch_timerfd);
-                       manager.app_launch_timerfd = -1;
+                       if (stop_app_launch_timer()<0)
+                               LOGE("cannot stop app launch timer\n");
                }
 
                LOGI("target connected = %d(running %d target)\n",
@@ -758,6 +798,14 @@ static int controlSocketHandler(int efd)
        struct msg_t *msg;
        int res = 0;
 
+       if(manager.connect_timeout_timerfd >= 0)
+       {
+               LOGI("release connect timeout timer\n");
+               epoll_ctl(efd, EPOLL_CTL_DEL, manager.connect_timeout_timerfd, NULL);
+               close(manager.connect_timeout_timerfd);
+               manager.connect_timeout_timerfd = -1;
+       }
+
        // Receive header
        recv_len = recv(manager.host.control_socket,
                       &msg_head,
@@ -769,7 +817,7 @@ static int controlSocketHandler(int efd)
                msg = malloc(MSG_CMD_HDR_LEN + msg_head.len);
                if (!msg) {
                        LOGE("Cannot alloc msg\n");
-                       sendACKCodeToHost(MSG_NOTOK, ERR_WRONG_MESSAGE_FORMAT);
+                       sendACKToHost(msg_head.id, ERR_WRONG_MESSAGE_FORMAT, 0, 0);
                        return -1;
                }
                msg->id = msg_head.id;
@@ -885,6 +933,11 @@ int daemonLoop()
                goto END_EFD;
        }
 
+       if (launch_timer_start() < 0) {
+               LOGE("Launch timer start failed\n");
+               return_value = -1;
+               goto END_EFD;
+       }
        // handler loop
        while (1) {
                int i, k;
@@ -929,15 +982,14 @@ int daemonLoop()
                        // check for request from device fd
                        for(k = 0; g_touch_dev[k].fd != ARRAY_END; k++)
                        {
-                               if(g_touch_dev[k].fd >= 0 &&
+                               if(g_touch_dev[k].fd >= 0 && 
                                                events[i].data.fd == g_touch_dev[k].fd)
                                {
                                        if(deviceEventHandler(&g_touch_dev[k], INPUT_ID_TOUCH) < 0)
                                        {
-                                               terminate_error("Internal DA framework error, "
-                                                                               "Please re-run the profiling.", 1);
-                                               return_value = -1;
-                                               goto END_EFD;
+                                               LOGE("Internal DA framework error, "
+                                                        "Please re-run the profiling (touch dev)\n");
+                                               continue;
                                        }
                                        break;
                                }
@@ -948,15 +1000,14 @@ int daemonLoop()
 
                        for(k = 0; g_key_dev[k].fd != ARRAY_END; k++)
                        {
-                               if(g_key_dev[k].fd >= 0 &&
+                               if(g_key_dev[k].fd >= 0 && 
                                                events[i].data.fd == g_key_dev[k].fd)
                                {
                                        if(deviceEventHandler(&g_key_dev[k], INPUT_ID_KEY) < 0)
                                        {
-                                               terminate_error("Internal DA framework error, "
-                                                                               "Please re-run the profiling.", 1);
-                                               return_value = -1;
-                                               goto END_EFD;
+                                               LOGE("Internal DA framework error, "
+                                                        "Please re-run the profiling (key dev)\n");
+                                               continue;
                                        }
                                        break;
                                }
@@ -966,52 +1017,52 @@ int daemonLoop()
                                continue;
 
                        // connect request from target
-                       if(events[i].data.fd == manager.target_server_socket)
+                       if (events[i].data.fd == manager.target_server_socket)
                        {
-                               if(targetServerHandler(manager.efd) < 0)        // critical error
+                               if (targetServerHandler(manager.efd) < 0)       // critical error
                                {
                                        terminate_error("Internal DA framework error, "
-                                                                       "Please re-run the profiling.", 1);
-                                       return_value = -1;
-                                       goto END_EFD;
+                                                                       "Please re-run the profiling (targetServerHandler)\n", 1);
+                                       continue;
                                }
                        }
                        // connect request from host
-                       else if(events[i].data.fd == manager.host_server_socket)
+                       else if (events[i].data.fd == manager.host_server_socket)
                        {
                                int result = hostServerHandler(manager.efd);
-                               if(result < 0)
+                               if (result < 0)
                                {
-                                       terminate_error("Internal DA framework error, "
-                                                                       "Please re-run the profiling.", 1);
-                                       return_value = -1;
-                                       goto END_EFD;
+                                       LOGE("Internal DA framework error (hostServerHandler)\n");
+                                       continue;
                                }
                        }
                        // control message from host
-                       else if(events[i].data.fd == manager.host.control_socket)
+                       else if (events[i].data.fd == manager.host.control_socket)
                        {
                                int result = controlSocketHandler(manager.efd);
-                               if(result == -11)       // socket close
+                               if (result == -11)      // socket close
                                {
-                                       // close target and host socket and quit
-                                       LOGI("host close = %d\n", manager.host.control_socket);
+                                       //if the host disconnected.
+                                       //In all other cases daemon must report an error and continue the loop
+                                       //close connect_timeoutt and host socket and quit
+                                       LOGI("Connection closed. Termination. (%d)\n",
+                                            manager.host.control_socket);
                                        return_value = 0;
                                        goto END_EFD;
                                }
-                               else if(result < 0)
+                               else if (result < 0)
                                {
                                        terminate_error("Internal DA framework error, "
-                                                                       "Please re-run the profiling.", 1);
+                                                       "Please re-run the profiling.", 1);
                                        return_value = -1;
                                        goto END_EFD;
                                }
                        }
-                       else if(events[i].data.fd == manager.host.data_socket)
+                       else if (events[i].data.fd == manager.host.data_socket)
                        {
                                char recvBuf[32];
                                recvLen = recv(manager.host.data_socket, recvBuf, 32, MSG_DONTWAIT);
-                               if(recvLen == 0)
+                               if (recvLen == 0)
                                {       // close data socket
                                        epoll_ctl(manager.efd, EPOLL_CTL_DEL,
                                                  manager.host.data_socket,
@@ -1024,15 +1075,24 @@ int daemonLoop()
                                LOGI("host message from data socket %d\n", recvLen);
                        }
                        // check for application launch timerfd
-                       else if(events[i].data.fd == manager.app_launch_timerfd)
+                       else if (events[i].data.fd == manager.app_launch_timerfd)
                        {
                                // send to host timeout error message for launching application
-                               terminate_error("Failed to launch application", 1);
+                               LOGE("Failed to launch application\n");
+                               if (stop_app_launch_timer()<0)
+                                       LOGE("cannot stop app launch timer\n");
+                               continue;
+                       }
+                       // check for connection timeout timerfd
+                       else if (events[i].data.fd == manager.connect_timeout_timerfd)
+                       {
+                               // send to host timeout error message for launching application
+                               terminate_error("no incoming connections", 1);
                                epoll_ctl(manager.efd, EPOLL_CTL_DEL,
-                                         manager.app_launch_timerfd, NULL);
-                               close(manager.app_launch_timerfd);
-                               manager.app_launch_timerfd = -1;
-                               return_value = -1;
+                                         manager.connect_timeout_timerfd, NULL);
+                               close(manager.connect_timeout_timerfd);
+                               manager.connect_timeout_timerfd = -1;
+                               LOGE("No connection in %d sec. shutdown.\n",MAX_CONNECT_TIMEOUT_TIME);
                                goto END_EFD;
                        }
                        // unknown socket