emuld : enhanced exception handling
authorDaiYoung Kim <daiyoung777.kim@samsung.com>
Mon, 8 Apr 2013 11:45:05 +0000 (20:45 +0900)
committerDaiYoung Kim <daiyoung777.kim@samsung.com>
Mon, 8 Apr 2013 11:45:05 +0000 (20:45 +0900)
mutex logic is added to solve synchronizatoin problem of vmodem connection flag.
graceful closing logic is added to avoid abnormal closing.

Signed-off-by : DaiYoung, Kim <daiyoung777.kim@samsung.com>

include/emuld.h
packaging/emuld.spec
src/emuld.c

index 0e361467ad694271dfd1c0e25d7a94ba481bec9a..9a07b11abec4d8bd2405f77987c76433eed83d4e 100644 (file)
@@ -43,6 +43,7 @@
 #include <assert.h>
 #include <errno.h>
 #include <sys/mount.h>
+#include <stdbool.h>
 
 /* definition */
 #define MAX_CLIENT             10000
@@ -60,6 +61,9 @@
 #define POWEROFF_DURATION      2
 
 /* function prototype */
+
+void set_vm_connect_status(const int v);
+bool is_vm_connected(void);
 void init_data0(void);            /* initialize data. */
 void init_server0(int svr_port);  /* server socket bind/listen */
 void* init_vm_connect(void* data);
@@ -71,7 +75,7 @@ int parse_val(char *buff, unsigned char data, char *parsbuf);
 void udp_init(void);
 int recv_data(int event_fd, char** r_databuf, int size);
 void client_recv(int event_fd);
-void server_process(void);
+bool server_process(void);
 void end_server(int sig);
 int is_mounted(void);
 
index 4302219051c89a9f2766dc2a33ff43de1306cb76..1f6ba9772b9a79e9e88ec4c6c31dca1ab964fbf0 100644 (file)
@@ -1,6 +1,6 @@
 #git:/slp/pkgs/e/emulator-daemon
 Name: emuld
-Version: 0.2.30
+Version: 0.2.31
 Release: 1
 Summary: emuld is used for communication emulator between and ide.
 License: Apache
index 112f1fe415fa071ab5c1ec0f8439caba2941c9ff..209a3cccda519ed3b40621e4925802e3927a6cdf 100644 (file)
@@ -36,6 +36,8 @@ License: GNU General Public License
 #include "emuld_common.h"
 #include "emuld.h"
 
+
+#define MAX_CONNECT_TRY_COUNT  (60 * 3)
 /* global definition */
 unsigned short sdbd_port = SDBD_PORT;
 unsigned short vmodem_port = VMODEM_PORT;
@@ -65,6 +67,8 @@ struct {
 
 int g_epoll_fd;                                /* epoll fd */
 
+static pthread_mutex_t mutex_vmconnect = PTHREAD_MUTEX_INITIALIZER;
+
 struct epoll_event g_events[MAX_EVENTS]; 
 
 void TAPIMessageInit(LXT_MESSAGE *packet)
@@ -97,6 +101,31 @@ void init_data0(void)
        }
 }
 
+bool is_vm_connected(void)
+{
+       bool ret = false;
+
+       pthread_mutex_lock(&mutex_vmconnect);
+
+       if (g_vm_connect_status == 1)
+       {
+               ret = true;
+       }
+
+       pthread_mutex_unlock(&mutex_vmconnect);
+
+       return ret;
+}
+
+void set_vm_connect_status(const int v)
+{
+       pthread_mutex_lock(&mutex_vmconnect);
+
+       g_vm_connect_status = v;
+
+       pthread_mutex_unlock(&mutex_vmconnect);
+}
+
 /*------------------------------------------------------------- 
 function: init_server0 
 io: input : integer - server port (must be positive)
@@ -151,13 +180,16 @@ void* init_vm_connect(void* data)
 {
        struct sockaddr_in vm_addr;
        int ret = -1;   
-       g_vm_connect_status = 0;
+       int connect_try_count = 0;
+       bool is_connected = false;
 
-       LOG("start");
+       set_vm_connect_status(0);
+
+       LOG("init_vm_connect start");
 
        pthread_detach(pthread_self());
        /* Open TCP Socket */
-       if( (g_vm_sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0 ) 
+       if( (g_vm_sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
        {
                fprintf(stderr, "Server Start Fails. : Can't open stream socket \n");
                exit(0);
@@ -170,24 +202,36 @@ void* init_vm_connect(void* data)
        vm_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
        vm_addr.sin_port = htons(vmodem_port);
 
-       while(ret < 0)
+       while(ret < 0  /*&& (connect_try_count < MAX_CONNECT_TRY_COUNT)*/)
        {
                ret = connect(g_vm_sockfd, (struct sockaddr *)&vm_addr, sizeof(vm_addr));
 
                LOG("vm_sockfd: %d, connect ret: %d", g_vm_sockfd, ret);
 
                if(ret < 0) {
-                       LOG("connection failed to vmodem!");            
+                       LOG("connection failed to vmodem! try count = %d\n", connect_try_count);
                        sleep(1);
                }
+               else
+               {
+                       is_connected = true;
+               }
+               connect_try_count ++;
        }
+       /*
+       if (!is_connected)
+       {
+               LOG("try to connect to vmoden was failed!");
+               exit(0);
+       }*/
        
        userpool_add(g_vm_sockfd, vm_addr.sin_port);
 
        /* add client sockfd at event pool */
        epoll_cli_add(g_vm_sockfd);
        
-       g_vm_connect_status = 1;
+       set_vm_connect_status(1);
+
        pthread_exit((void *) 0); 
 }
 
@@ -271,11 +315,6 @@ void* mount_sdcard(void* data)
                                
                                if(ret == 0)
                                {
-                                       /* W/A for sdcard file permission start */
-                                       system("mkdir -p /opt/storage/sdcard/Camera /opt/storage/sdcard/Downloads /opt/storage/sdcard/Images /opt/storage/sdcard/Sounds /opt/storage/sdcard/Videos");
-                                       system("chown 5000:5000 -R /opt/storage/sdcard");
-                                       /* W/A for sdcard file permission end */
-                                       
                                        system("chmod -R 777 /opt/storage/sdcard");
                                        system("vconftool set -t int memory/sysman/mmc 1 -i -f");
                                }
@@ -623,6 +662,9 @@ void client_recv(int event_fd)
                        packet = NULL;
                        userpool_delete(event_fd);
                        close(event_fd); /* epoll set fd also deleted automatically by this call as a spec */
+
+                       set_vm_connect_status(0);
+
                        if(pthread_create(&tid[0], NULL, init_vm_connect, NULL) != 0)
                                LOG("pthread create fail!");
                        return;
@@ -712,7 +754,7 @@ void client_recv(int event_fd)
                if(strncmp(tmpbuf, "telephony", 9) == 0)
                {
                        g_sdbd_sockfd = event_fd;
-                       if(g_vm_connect_status != 1) {  // The connection is lost with vmodem
+                       if(!is_vm_connected()) {        // The connection is lost with vmodem
                                free(packet);
                                packet = NULL;
                                return;
@@ -743,7 +785,7 @@ void client_recv(int event_fd)
                                }
                        }                               
 
-                       if(g_vm_connect_status != 1)    // The connection is lost with vmodem
+                       if(!is_vm_connected())  // The connection is lost with vmodem
                                return;
 
                        recvd_size = recv_data(event_fd, &r_databuf, packet->length);
@@ -1013,7 +1055,7 @@ void client_recv(int event_fd)
        }
 }
 
-void server_process(void)
+bool server_process(void)
 {
        struct sockaddr_in cli_addr;
        int i,nfds;
@@ -1024,14 +1066,14 @@ void server_process(void)
 
        if(nfds == 0){
                /* no event , no work */
-               return;
+               return false;
        }
 
 
        if(nfds < 0) {
                fprintf(stderr, "epoll wait error\n");
                /* return but this is epoll wait error */
-               return;
+               return true;
        } 
 
        for( i = 0 ; i < nfds ; i++ )
@@ -1065,6 +1107,8 @@ void server_process(void)
 
                client_recv(g_events[i].data.fd);    
        } /* end of for 0-nfds */
+
+       return false;
 }
 /*------------------------------- end of function server_process */
 
@@ -1300,6 +1344,8 @@ void send_guest_server(char* databuf)
 
 int main( int argc , char *argv[])
 {
+       int state;
+
        if(log_print == 1)
        {
                // for emuld log file
@@ -1331,15 +1377,23 @@ int main( int argc , char *argv[])
        init_server0(g_svr_port);
 
        epoll_init();    /* epoll initialize  */
+
+       set_vm_connect_status(0);
+
        if(pthread_create(&tid[0], NULL, init_vm_connect, NULL) != 0)
                LOG("pthread create fail!");
 
        udp_init();
 
+       bool is_exit = false;
        /* main loop */
-       while(1)
+       while(!is_exit)
        {
-               server_process();  /* accept process. */
+               is_exit = server_process();  /* accept process. */
        } /* infinite loop while end. */
+
+       state = pthread_mutex_destroy(&mutex_vmconnect);
+
+       fprintf(stderr, "emuld exit\n");
 }