net_helper: fix bugs in D&D logics
authorSeokYeon Hwang <syeon.hwang@samsung.com>
Sat, 25 Jun 2016 12:35:07 +0000 (21:35 +0900)
committerSeokYeon Hwang <syeon.hwang@samsung.com>
Mon, 27 Jun 2016 05:45:36 +0000 (14:45 +0900)
Local variable that is char[] type should be initialize with NULL char.
Unnecessary global variable is removed. And, now we can provide D&D
features even if we cannot identify default user home directory.
(At the time, we will use "/tmp" directory as a default.)

Change-Id: I365e782771982700e0096c076603c7d655fa7e55
Signed-off-by: SeokYeon Hwang <syeon.hwang@samsung.com>
tizen/src/emul_state.c
tizen/src/emul_state.h
tizen/src/ui/menu/sdbhelper.cpp
tizen/src/util/net_helper.c

index 2a43b82..baef305 100644 (file)
@@ -120,20 +120,6 @@ const char* get_emul_skin_path(void)
     return _emul_info.skin_path;
 }
 
-const char* get_emul_guest_home_path(void)
-{
-    return _emul_info.guest_home_path;
-}
-
-void set_emul_guest_home_path(char *home_path)
-{
-    if (!_emul_info.guest_home_path) {
-         _emul_info.guest_home_path = home_path;
-    } else {
-        LOG_INFO("guest home path is already set : %s\n", _emul_info.guest_home_path);
-    }
-}
-
 /* CPU virtualization */
 bool get_emul_cpu_accel(void)
 {
@@ -704,6 +690,23 @@ int get_display_resolution_height(void)
     return qemu_console_get_height(NULL, initial_resolution_height);
 }
 
+// guest default home
+static const char *platform_default_home = NULL;
+const char* get_platform_default_home(void)
+{
+    return platform_default_home;
+}
+
+void set_platform_default_home(const char *path)
+{
+    if (!platform_default_home) {
+         platform_default_home = g_strdup(path);
+    } else {
+        LOG_INFO("platform home path is already set : %s\n",
+                platform_default_home);
+    }
+}
+
 static void emul_state_notify_exit(Notifier *notifier, void *data)
 {
     g_free((void *)launch_conf_file);
@@ -738,8 +741,12 @@ static void emul_state_notify_exit(Notifier *notifier, void *data)
 
     g_free((void *)host_ip);
     host_ip = NULL;
+
+    g_free((void *)platform_default_home);
+    platform_default_home = NULL;
 }
 
+
 static Notifier emul_state_exit = { .notify = emul_state_notify_exit };
 
 void init_emul_state(void)
index 1a65aa5..62ccd78 100644 (file)
@@ -84,7 +84,6 @@ enum {
 typedef  struct EmulatorConfigInfo {
     const char *skin_path;
     const char *file_sharing_path;
-    const char *guest_home_path;
     /* add here */
 } EmulatorConfigInfo;
 
@@ -157,5 +156,8 @@ void set_initial_display_resolution(int width, int height);
 int get_display_resolution_width(void);
 int get_display_resolution_height(void);
 
+const char* get_platform_default_home(void);
+void set_platform_default_home(const char *path);
+
 void init_emul_state(void);
 #endif /* __EMUL_STATE_H__ */
index 4ac7814..742966f 100644 (file)
@@ -73,7 +73,7 @@ bool SdbHelper::isSdbReady()
         return false;
     }
 
-    if (QString(get_emul_guest_home_path()).isEmpty()) {
+    if (QString(get_platform_default_home()).isEmpty()) {
         return false;
     }
 
@@ -82,7 +82,7 @@ bool SdbHelper::isSdbReady()
 
 QString SdbHelper::getGuestDefaultPushPath()
 {
-    QString home_path = get_emul_guest_home_path();
+    QString home_path = get_platform_default_home();
     return home_path + "content" + "/";
 }
 
index 4b38eb1..12513a4 100644 (file)
@@ -44,7 +44,7 @@
 #include "ecs/ecs.h"
 #include "new_debug_ch.h"
 
-DECLARE_DEBUG_CHANNEL(sdb);
+DECLARE_DEBUG_CHANNEL(net_helper);
 
 #define BUF_SIZE 64
 #define MAX_TRIAL 20
@@ -54,7 +54,6 @@ DECLARE_DEBUG_CHANNEL(sdb);
 int emul_vm_base_socket;
 static bool sdb_daemon_is_initialized = false;
 static QemuThread thread_id;
-bool processing = false;
 
 int inet_strtoip(const char*  str, uint32_t  *ip)
 {
@@ -211,6 +210,7 @@ clients = QTAILQ_HEAD_INITIALIZER(clients);
 
 static SDB_Noti_Server *current_server;
 static QemuMutex mutex_clients;
+static QemuMutex mutex_request;
 
 static void remove_sdb_client(SDB_Client* client)
 {
@@ -396,33 +396,31 @@ static void suspend_lock_state(int state)
 static void *get_user_home_path(void *args)
 {
     FILE *fp;
-    char user_name[MAX_USER_NAME_LEN];
+    char user_name[MAX_USER_NAME_LEN] = { 0, } ;
     int index = 0;
 #ifndef CONFIG_WIN32
     const char *sdb_path = "../../../../../tools/sdb";
 #else
     const char *sdb_path = "..\\..\\..\\..\\..\\tools\\sdb.exe";
 #endif
-    if (processing) {
-        return NULL;
-    }
-    processing = true;
     const char *bin_dir = get_bin_path();
 
-    if (get_emul_guest_home_path()) {
-        processing = false;
+    qemu_mutex_lock(&mutex_request);
+
+    if (get_platform_default_home()) {
         return NULL;
     }
-    gchar *cmd_root_off = g_strdup_printf("\"%s%s\" -s emulator-%d root off", bin_dir, sdb_path, get_vm_device_serial_number());
-    gchar *cmd_get_home = g_strdup_printf("\"%s%s\" -s emulator-%d shell id -un",
-                                         bin_dir, sdb_path, get_vm_device_serial_number());
+
+    char *cmd_root_off = g_strdup_printf("\"%s%s\" -s emulator-%d root off",
+            bin_dir, sdb_path, get_vm_device_serial_number());
+    char *cmd_get_home = g_strdup_printf("\"%s%s\" -s emulator-%d shell id -un",
+            bin_dir, sdb_path, get_vm_device_serial_number());
 
     //FIXME: (sdb) cannot check sdb root status
     fp = popen(cmd_root_off, "r");
     if (fp == NULL) {
         LOG_WARNING("Failed to run command\n");
         g_free(cmd_root_off);
-        processing = false;
         return NULL;
     }
     g_free(cmd_root_off);
@@ -440,25 +438,30 @@ static void *get_user_home_path(void *args)
             break;
         }
 
-        while (fgets(user_name, sizeof(user_name), fp) != NULL) {
-            LOG_INFO("user name: %s", user_name);
-            if (strlen(user_name) > 2) {
-                /* to remove carrige return and new line character */
-                user_name[strlen(user_name) - 2] = '\0';
+        if (fgets(user_name, sizeof(user_name), fp)) {
+            g_strstrip(user_name);
+            if (strlen(user_name) > 0) {
+                char *path = g_strdup_printf("/home/%s/", user_name);
+                set_platform_default_home(path);
+                g_free(path);
+                pclose(fp);
+
+                break;
             }
         }
 
-        if (strlen(user_name) > 0) {
-            gchar *user_home_path = g_strdup_printf("/home/%s/", user_name);
-            LOG_INFO("guest_home_user_name: %s\n", user_home_path);
-            set_emul_guest_home_path(user_home_path);
-            pclose(fp);
-            break;
-        }
         pclose(fp);
     }
     g_free(cmd_get_home);
-    processing = false;
+
+    if (!get_platform_default_home()) {
+        set_platform_default_home("/tmp/");
+    }
+
+    LOG_INFO("platform_default_user_home: %s\n", get_platform_default_home());
+
+    qemu_mutex_unlock(&mutex_request);
+
     return NULL;
 }
 
@@ -473,7 +476,10 @@ static void command_handler(char* readbuf, struct sockaddr_in* client_addr)
     LOG_TRACE("command:%s\n", command);
     if (strcmp(command, "2\n" ) == 0) {
         sdb_daemon_is_initialized = true;
-        qemu_thread_create(&thread_id, "get_home_path", get_user_home_path, NULL, QEMU_THREAD_DETACHED);
+        if (!get_platform_default_home()) {
+            qemu_thread_create(&thread_id, "get_platform_default_home",
+                    get_user_home_path, NULL, QEMU_THREAD_DETACHED);
+        }
     } else if (strcmp(command, "5\n") == 0) {
         register_sdb_server(readbuf, client_addr);
         set_sdb_connection(true);
@@ -636,13 +642,14 @@ static void start_sdb_noti_server(int server_port)
         return;
     }
 
+    qemu_mutex_init(&mutex_clients);
+    qemu_mutex_init(&mutex_request);
+
     server->server_chan = io_channel_from_socket(server->server_fd);
     server->server_tag = g_io_add_watch(server->server_chan, G_IO_IN, sdb_noti_read, server);
 
     current_server = server;
 
-    qemu_mutex_init(&mutex_clients);
-
     LOG_INFO("success to bind port[127.0.0.1:%d/udp] for sdb noti server in host \n", server_port);
 
     emulator_add_exit_notifier(&sdb_noti_server_exit);