[Internal: merge sync-agent]
[platform/core/system/sync-agent.git] / src / framework / event / handler.c
index 947b676..dcb3a5d 100755 (executable)
@@ -61,7 +61,13 @@ EXPORT_API sync_agent_event_error_e sync_agent_run_event_handler(unsigned long i
 
        _DEBUG_INFO("sync_agent_run_event_handler() start\n");
 
-       pthread_create(&event_handler_thread_id, 0, _event_listener, 0);
+       int status_thread_create = 0;
+       status_thread_create = pthread_create(&event_handler_thread_id, 0, _event_listener, 0);
+       if(status_thread_create != 0) {
+               _DEBUG_WARNING("pthread_create [%d]", status_thread_create );
+               _EXTERN_FUNC_EXIT;
+               return SYNC_AGENT_EVENT_FAIL;
+       }
 
        *thread_id = event_handler_thread_id;
 
@@ -220,7 +226,7 @@ static void *_event_listener(void *arg)
 
        signal(SIGPIPE, SIG_IGN);
 
-       int client_sockfd;
+       int client_sockfd = -1;
        int state;
        socklen_t client_len;
        struct sockaddr_un clientaddr, serveraddr;
@@ -243,6 +249,7 @@ static void *_event_listener(void *arg)
        }
 
        client_len = sizeof(clientaddr);
+       _DEBUG_INFO("_event_listener - client_len: [%d]",client_len);
        if ((_event_server_socket_id = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
                return NULL;
        }
@@ -281,44 +288,64 @@ static void *_event_listener(void *arg)
        while (1) {
                _DEBUG_TRACE("In EventHandler loop=\n");
 
-               char inbuf[EVENT_MAX_STREAM_SIZE + 1];
-               char outbuf[EVENT_MAX_STREAM_SIZE + 1];
+               char * inbuf = NULL;
+               char * outbuf = NULL;
+               inbuf = (char*)malloc(sizeof(char)*(EVENT_MAX_STREAM_SIZE + 1));
+               if(inbuf == NULL) {
+                       _DEBUG_ERROR("inbuf malloc failed!!");
+                       goto error;
+               }
+
+               outbuf= (char*)malloc(sizeof(char)*(EVENT_MAX_STREAM_SIZE + 1));
+               if(outbuf == NULL) {
+                       _DEBUG_ERROR("outbuf malloc failed!!");
+                       goto error;
+               }
+
                memset(inbuf, 0x00, EVENT_MAX_STREAM_SIZE + 1);
                memset(outbuf, 0x00, EVENT_MAX_STREAM_SIZE + 1);
                sync_agent_event_data_s request;
                sync_agent_event_data_s response;
+
                request.data = inbuf;
                response.data = outbuf;
                event_init_event_data_iter(&request);
                event_init_event_data_iter(&response);
 
                client_sockfd = accept(_event_server_socket_id, (struct sockaddr *)&clientaddr, &client_len);
+
                if (client_sockfd < 0) {
                        _DEBUG_ERROR("socket accept() failed !!");
-                       return NULL;
+                       goto error;
                }
                _DEBUG_TRACE("accept() was called\n");
+               _DEBUG_INFO("_event_listener - client_sockfd: [%d]",client_sockfd);
 
                result = read(client_sockfd, (void *)inbuf, EVENT_MAX_STREAM_SIZE);
                if (result <= 0 || result > EVENT_MAX_STREAM_SIZE) {
                        _DEBUG_ERROR("read failed !!");
-                       close(client_sockfd);
-                       return NULL;
+                       goto error;
                }
-               if (strlen(inbuf) <= 0 || strlen(inbuf) > EVENT_MAX_STREAM_SIZE) {
-                       _DEBUG_ERROR("socket data read : size OUT OF BOUND !!");
-                       close(client_sockfd);
-                       return NULL;
+/*
+               _DEBUG_INFO("read result [%d]", result);
+
+               int print_i = 0;
+               for( print_i = 0; print_i < result ; print_i++)
+               {
+                       _DEBUG_INFO(" 0x%x ", inbuf[print_i]);
                }
+               _DEBUG_INFO("request.event_num [%d]", request.event_num);
+               _DEBUG_INFO("request.size [ox%x]", *(request.size));
+               _DEBUG_INFO("request.data [0x%x]", *(request.data));
+*/
 
                int event_num;
                sync_agent_get_event_data_param_int(&request, &event_num);
                _DEBUG_TRACE("Received Event Number : %d\n", event_num);
 
                __dispatch_event(event_num, &request, &response);
-               /*
-                * need synchronous response case
-                */
+
+               // need synchronous response case
                event_type_e event_type = event_get_event_type(event_num);
                if (event_type == EVENT_TYPE_NEED_RESPONSE) {
                        if (event_get_event_data_element_size(&response) > 0) {
@@ -330,7 +357,20 @@ static void *_event_listener(void *arg)
                        }
                }
 
-               close(client_sockfd);
+ error:
+
+               if (inbuf) {
+                       free(inbuf);
+                       inbuf = NULL;
+               }
+
+               if (outbuf) {
+                       free(outbuf);
+                       outbuf = NULL;
+               }
+
+               if (client_sockfd >= 0 )
+                       close(client_sockfd);
        }
 
        _INNER_FUNC_EXIT;