[Title] enhance graceful shutdown in abnormal case
authorSon Hyunjun <hj79.son@samsung.com>
Fri, 6 Apr 2012 11:22:34 +0000 (20:22 +0900)
committerSon Hyunjun <hj79.son@samsung.com>
Fri, 6 Apr 2012 11:22:34 +0000 (20:22 +0900)
[Type] Enhancement
[Module] Skin
[Priority] Minor
[CQ#]
[Redmine#]
[Problem]
[Cause]
[Solution]

Change-Id: I413b5906011bb503e0e2e73998fbb8c5a5908402

tizen/src/emulator.c
tizen/src/skin/maruskin_operation.c
tizen/src/skin/maruskin_operation.h
tizen/src/skin/maruskin_server.c

index 97f914e8ae8e8e56579435fd359f1ff904bb31c0..095a90ee3bc9d78aa134d520ec8ff1d880e6365a 100644 (file)
@@ -274,7 +274,6 @@ void prepare_maru(void)
 
     construct_main_window(skin_argc, skin_argv, qemu_argc, qemu_argv );
 
-    //TODO get port number by args from emulator manager
     int guest_server_port = tizen_base_port + SDB_UDP_SENSOR_INDEX;
     start_guest_server( guest_server_port );
 
index 3829f6efd2dfc83d7a7f5cb7ba4c4b334dcf266e..8c9f97e118e9400130e396afe74b7c8d43196c0c 100644 (file)
@@ -52,6 +52,8 @@ MULTI_DEBUG_CHANNEL(qemu, skin_operation);
 #define DATA_DELIMITER "#" // in detail info data
 #define TIMEOUT_FOR_SHUTDOWN 10 // seconds
 
+static int requested_shutdown_qemu_gracefully = 0;
+
 static void* run_timed_shutdown_thread( void* args );
 static void send_to_emuld( const char* request_type, int request_size, const char* send_buf, int buf_size );
 
@@ -330,6 +332,8 @@ void request_close( void )
 
 void shutdown_qemu_gracefully( void ) {
 
+    requested_shutdown_qemu_gracefully = 1;
+
     pthread_t thread_id;
     if( 0 > pthread_create( &thread_id, NULL, run_timed_shutdown_thread, NULL ) ) {
         ERR( "!!! Fail to create run_timed_shutdown_thread. shutdown qemu right now !!!\n"  );
@@ -338,6 +342,10 @@ void shutdown_qemu_gracefully( void ) {
 
 }
 
+int is_requested_shutdown_qemu_gracefully( void ) {
+    return requested_shutdown_qemu_gracefully;
+}
+
 static void* run_timed_shutdown_thread( void* args ) {
 
     send_to_emuld( "system\n\n\n\n", 10, "shutdown", 8 );
index 1b331055a1241a1148451c2f74258e6255c6d738..6f2cae4516247ccedf4d3efebbf92690e9932cc7 100644 (file)
@@ -70,4 +70,6 @@ void request_close( void );
 
 void shutdown_qemu_gracefully( void );
 
+int is_requested_shutdown_qemu_gracefully( void );
+
 #endif /* MARUSKIN_OPERATION_H_ */
index 9840ef9bf20a9218bf28a9207e92f382254d0df5..9df5778468decebb3cf9c75c6ca7c72990a65c5f 100644 (file)
@@ -155,21 +155,73 @@ int start_skin_server( int argc, char** argv, int qemu_argc, char** qemu_argv )
 }
 
 void shutdown_skin_server( void ) {
+
+    INFO( "shutdown_skin_server\n" );
+
     if ( client_sock ) {
+
         INFO( "send shutdown to skin.\n" );
+
         if ( 0 > send_skin_header_only( client_sock, SEND_SHUTDOWN ) ) {
+
             ERR( "fail to send SEND_SHUTDOWN to skin.\n" );
             // force close
+            is_force_close_client = 1;
 #ifdef _WIN32
             closesocket( client_sock );
 #else
             close( client_sock );
 #endif
+
         } else {
             // skin sent RECV_RESPONSE_SHUTDOWN.
         }
     }
 
+    if ( client_sock ) {
+#ifdef _WIN32
+        closesocket( client_sock );
+#else
+        close( client_sock );
+#endif
+    }
+
+    int close_server_socket = 0;
+    int count = 0;
+    int max_sleep_count = 10;
+
+    while( 1 ) {
+
+        if( max_sleep_count < count ) {
+            close_server_socket = 1;
+            break;
+        }
+
+        if( stop_server ) {
+            INFO( "skin client sent normal shutdown response.\n" );
+            break;
+        }else {
+#ifdef _WIN32
+            Sleep( 1 ); // 1ms
+#else
+            usleep( 1000 ); // 1ms
+#endif
+            count++;
+        }
+    }
+
+    if ( close_server_socket ) {
+        WARN( "skin client did not send normal shutdown response.\n" );
+        stop_server = 1;
+        if ( server_sock ) {
+#ifdef _WIN32
+            closesocket( server_sock );
+#else
+            close( server_sock );
+#endif
+        }
+    }
+
 }
 
 void notify_sensor_daemon_start( void ) {
@@ -384,6 +436,7 @@ static void* run_skin_server( void* args ) {
     while ( 1 ) {
 
         if ( stop_server ) {
+            INFO( "close server socket normally.\n" );
             break;
         }
 
@@ -410,7 +463,7 @@ static void* run_skin_server( void* args ) {
         while ( 1 ) {
 
             if ( stop_server ) {
-                INFO( "stop receiving this socket.\n" );
+                INFO( "stop receiving current client socket.\n" );
                 break;
             }
 
@@ -746,15 +799,13 @@ static void* run_skin_server( void* args ) {
     stop_heart_beat();
 
 cleanup:
+    if ( server_sock ) {
 #ifdef _WIN32
-    if(server_sock) {
         closesocket( server_sock );
-    }
 #else
-    if ( server_sock ) {
         close( server_sock );
-    }
 #endif
+    }
 
     if( shutdown_qmu ) {
         ERR( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n" );
@@ -970,18 +1021,34 @@ static void* do_heart_beat( void* args ) {
                 break;
             } else {
 
-                send_fail_count = 0;
-                recv_heartbeat_count = 0;
-                need_restart_skin_client = 0;
-                restart_client_count++;
+                if ( is_requested_shutdown_qemu_gracefully() ) {
+                    INFO( "requested shutdown_qemu_gracefully, do not retry starting skin client process.\n" );
+                    break;
+                }else {
 
-                WARN( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n" );
-                WARN( "!!! restart skin client process !!!\n" );
-                WARN( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n" );
+                    send_fail_count = 0;
+                    recv_heartbeat_count = 0;
+                    need_restart_skin_client = 0;
+                    restart_client_count++;
+
+                    WARN( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n" );
+                    WARN( "!!! restart skin client process !!!\n" );
+                    WARN( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n" );
+
+                    is_force_close_client = 1;
+                    if ( client_sock ) {
+                        if ( client_sock ) {
+#ifdef _WIN32
+                            closesocket( client_sock );
+#else
+                            close( client_sock );
+#endif
+                        }
+                    }
 
-                is_force_close_client = 1;
-                shutdown_skin_server();
-                start_skin_client( skin_argc, skin_argv );
+                    start_skin_client( skin_argc, skin_argv );
+
+                }
 
             }
 
@@ -994,7 +1061,15 @@ static void* do_heart_beat( void* args ) {
         INFO( "[HB] shutdown skin_server by heartbeat thread.\n" );
 
         is_force_close_client = 1;
-        shutdown_skin_server();
+        if ( client_sock ) {
+            if ( client_sock ) {
+#ifdef _WIN32
+                closesocket( client_sock );
+#else
+                close( client_sock );
+#endif
+            }
+        }
 
         stop_server = 1;