osutil: arrange source code about proxy
authorMunkyu Im <munkyu.im@samsung.com>
Mon, 15 Sep 2014 11:27:18 +0000 (20:27 +0900)
committerMunkyu Im <munkyu.im@samsung.com>
Tue, 16 Sep 2014 10:06:59 +0000 (19:06 +0900)
Integrity duplicated codes.
Add logs.

Change-Id: I8f58db623270c32dba5687310e6553f67833cc69
Signed-off-by: Munkyu Im <munkyu.im@samsung.com>
tizen/src/ecs/ecs_msg.c
tizen/src/emulator.c
tizen/src/osutil-darwin.c
tizen/src/osutil-linux.c
tizen/src/osutil-win32.c
tizen/src/osutil.c
tizen/src/osutil.h
tizen/src/sdb.c

index a4b706b1bef556d828cea6316d329de3e2ad63a0..c05cfca0013c0c4f5bbf2f0616c860f693378a0a 100644 (file)
@@ -1028,7 +1028,7 @@ static bool handle_sdcard(char* dataBuf, size_t dataLen)
                     TRACE("sdcard path: [%s]\n", sdcard_img_path);
 
                     //mloop_evcmd_usbdisk(sdcard_img_path);
-                    if (make_sdcard_lock_os(sdcard_img_path) && !is_sdcard_attached) {
+                    if (!is_sdcard_attached && make_sdcard_lock_os(sdcard_img_path)) {
                         mloop_evcmd_sdcard(sdcard_img_path);
                         is_sdcard_attached = true;
                     } else {
index 5f47faddb59e84cf02293efa7a93854014ade3f5..22e20e41e63a61d5c96cd48857890df67eb46e27 100644 (file)
@@ -309,9 +309,13 @@ static void prepare_basic_features(gchar * const kernel_cmdline)
         } else {
             proxy = g_strsplit(http_proxy, ":", -1);
         }
-        INFO("http_proxy address: %s, port: %s\n", proxy[0], proxy[1]);
-        set_emul_http_proxy_addr(proxy[0]);
-        set_emul_http_proxy_port(proxy[1]);
+        if(g_strv_length(proxy) > 1) {
+            if(proxy[0] != NULL && proxy[1] != NULL) {
+                INFO("http_proxy information= addr: %s, port: %s\n", proxy[0], proxy[1]);
+                set_emul_http_proxy_addr(proxy[0]);
+                set_emul_http_proxy_port(proxy[1]);
+            }
+        }
         if(proxy != NULL) {
             g_strfreev(proxy);
         }
index 759aa197e03145d6ebf87ce3c8c3b0349274df95..2eda16e0543897c19ec0e44b4ff7fe4544b5572c 100644 (file)
@@ -391,79 +391,12 @@ void get_host_proxy_os(char *http_proxy, char *https_proxy, char *ftp_proxy, cha
     }
 }
 
-static int fd_isopen(int fd)
-{
-    struct flock lock;
-
-    lock.l_type = F_WRLCK;
-    lock.l_start = 0;
-    lock.l_whence = SEEK_SET;
-    lock.l_len = 0;
-    lock.l_pid = getpid();
-
-    return fcntl(fd, F_SETLK, &lock);
-}
-
-static int fd_unlock(int fd)
-{
-    struct flock lock;
-
-    lock.l_type = F_UNLCK;
-    lock.l_start = 0;
-    lock.l_whence = SEEK_SET;
-    lock.l_len = 0;
-
-    return fcntl(fd, F_SETLK, &lock);
-}
-
 bool make_sdcard_lock_os(char *sdcard)
 {
-    char *lock_file = g_strdup_printf("%s.lck", sdcard);
-    int fd = open(lock_file, O_CREAT|O_RDWR, 0666);
-    if (fd == -1)
-    {
-        perror("file open error : ");
-       return false;
-    }
-    if (fd_isopen(fd) == -1)
-    {
-        perror("file is lock ");
-        return false;
-    }
-    INFO("Get file lock: %s\n", lock_file);
-    return true;
-
+    return make_sdcard_lock(sdcard);
 }
 
 int remove_sdcard_lock_os(char *sdcard)
 {
-    errno = 0;
-    char *lock_file = g_strdup_printf("%s.lck", sdcard);
-    int fd = open(lock_file, O_RDWR, 0666);
-    if (fd == -1)
-    {
-        perror("file open error : ");
-        if(errno == ENOENT) {
-            return ERR_NOENT;
-        }
-        return ERR_UNLCK;
-    }
-
-    if (fd_unlock(fd) == -1)
-    {
-        perror("file unlock error ");
-        return ERR_UNLCK;
-    }
-
-    if (unlink(lock_file) < 0) {
-        perror("unlink error ");
-        return ERR_UNLCK;
-    }
-
-    if (remove(lock_file) < 0) {
-        perror("remove error ");
-        return ERR_UNLCK;
-    }
-    INFO("unlock success: %s\n", lock_file);
-    return ERR_SUCCESS;
+    return remove_sdcard_lock(sdcard);
 }
index 653d5860a8cf4c9e918db52b8fa3047345e18d23..b8bc144daf1678603d8c81d0d81df106687d929e 100644 (file)
@@ -149,83 +149,6 @@ void make_vm_lock_os(void)
     }
 }
 
-static int fd_isopen(int fd)
-{
-    struct flock lock;
-
-    lock.l_type = F_WRLCK;
-    lock.l_start = 0;
-    lock.l_whence = SEEK_SET;
-    lock.l_len = 0;
-    lock.l_pid = getpid();
-
-    return fcntl(fd, F_SETLK, &lock);
-}
-
-static int fd_unlock(int fd)
-{
-    struct flock lock;
-
-    lock.l_type = F_UNLCK;
-    lock.l_start = 0;
-    lock.l_whence = SEEK_SET;
-    lock.l_len = 0;
-
-    return fcntl(fd, F_SETLK, &lock);
-}
-
-bool make_sdcard_lock_os(char *sdcard)
-{
-    char *lock_file = g_strdup_printf("%s.lck", sdcard);
-    int fd = open(lock_file, O_CREAT|O_RDWR, 0666);
-    if (fd == -1)
-    {
-        perror("file open error : ");
-       return false;
-    }
-    if (fd_isopen(fd) == -1)
-    {
-        perror("file is lock ");
-        return false;
-    }
-    INFO("Get file lock: %s\n", lock_file);
-    return true;
-
-}
-
-int remove_sdcard_lock_os(char *sdcard)
-{
-    errno = 0;
-    char *lock_file = g_strdup_printf("%s.lck", sdcard);
-    int fd = open(lock_file, O_RDWR, 0666);
-    if (fd == -1)
-    {
-        perror("file open error : ");
-        if(errno == ENOENT) {
-            return ERR_NOENT;
-        }
-        return ERR_UNLCK;
-    }
-
-    if (fd_unlock(fd) == -1)
-    {
-        perror("file unlock error ");
-        return ERR_UNLCK;
-    }
-
-    if (unlink(lock_file) < 0) {
-        perror("unlink error ");
-        return ERR_UNLCK;
-    }
-
-    if (remove(lock_file) < 0) {
-        perror("remove error ");
-        return ERR_UNLCK;
-    }
-    INFO("unlock success: %s\n", lock_file);
-    return ERR_SUCCESS;
-}
-
 void set_bin_path_os(gchar * exec_argv)
 {
     gchar link_path[PATH_MAX] = { 0, };
@@ -529,3 +452,14 @@ void get_host_proxy_os(char *http_proxy, char *https_proxy, char *ftp_proxy, cha
     }
     pclose(output);
 }
+
+bool make_sdcard_lock_os(char *sdcard)
+{
+    return make_sdcard_lock(sdcard);
+}
+
+
+int remove_sdcard_lock_os(char *sdcard)
+{
+    return remove_sdcard_lock(sdcard);
+}
index 28997fdf9b917328a5afc6b510be43df8962aa99..b0a0ea9831b2cc1564ee7491326298d28b4e5291 100644 (file)
@@ -295,7 +295,7 @@ void get_host_proxy_os(char *http_proxy, char *https_proxy, char *ftp_proxy, cha
         //if exists
         url = (char*)malloc(dwLength);
         if (url == NULL) {
-            ERR( "Failed to allocate a buffer\n");
+            ERR("Failed to allocate a buffer\n");
         }
         else {
             memset(url, 0x00, dwLength);
@@ -310,14 +310,14 @@ void get_host_proxy_os(char *http_proxy, char *https_proxy, char *ftp_proxy, cha
     //check manual proxy key exists
     lRet = RegQueryValueEx(hKey, "ProxyEnable", 0, NULL, NULL, &dwLength);
     if (lRet != ERROR_SUCCESS && dwLength == 0) {
-        ERR(stderr, "Failed to query value from %s\n",
+        ERR("Failed to query value from %s\n",
                 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ProxyEnable");
         RegCloseKey(hKey);
         return;
     }
     proxyenable = (BYTE*)malloc(dwLength);
     if (proxyenable == NULL) {
-        ERR( "Failed to allocate a buffer\n");
+        ERR("Failed to allocate a buffer\n");
         RegCloseKey(hKey);
         return;
     }
@@ -402,7 +402,7 @@ void get_host_proxy_os(char *http_proxy, char *https_proxy, char *ftp_proxy, cha
 
 bool make_sdcard_lock_os(char *sdcard)
 {
-   char *lock_file = g_strdup_printf("%s.lck", sdcard);
+    char *lock_file = g_strdup_printf("%s.lck", sdcard);
 
     HANDLE hFile = CreateFile(lock_file, GENERIC_READ,
             0,
@@ -413,17 +413,18 @@ bool make_sdcard_lock_os(char *sdcard)
 
     if (hFile == INVALID_HANDLE_VALUE) {
         ERR("file open error : (%d)\n", GetLastError());
-               if(GetLastError() == 32) {
-                       if(strcmp(g_sdcard, sdcard) == 0) {
-                               INFO("try to mount same sdcard!\n");
-                       }
-                       return false;
-               }
+        if(GetLastError() == 32) {
+            if(strcmp(g_sdcard, sdcard) == 0) {
+                INFO("try to mount same sdcard!\n");
+            }
+            return false;
+        }
         return true;
     }
-       g_hFile = hFile;
-       strncpy(g_sdcard, sdcard, strlen(sdcard));
+    g_hFile = hFile;
     INFO("Get file lock: %s\n", lock_file);
+    strncpy(g_sdcard, sdcard, strlen(sdcard));
+    CloseHandle(hFile);
     return true;
 
 }
@@ -457,7 +458,6 @@ int remove_sdcard_lock_os(char *sdcard)
                 ERR("DeleteFile failed (%d)\n", GetLastError());
                 return ERR_UNLCK;
             }
-
             return ERR_SUCCESS;
         }
         return ERR_NOENT;
@@ -480,5 +480,3 @@ int remove_sdcard_lock_os(char *sdcard)
     return ERR_SUCCESS;
 
 }
-
-
index 07fc65d1960903be048e800eab9303225d7f1af1..edd90b2d96f4138dbe25ca13d5462c4a1c78b594 100644 (file)
@@ -44,7 +44,9 @@ MULTI_DEBUG_CHANNEL(emulator, osutil);
 
 
 const char *pac_tempfile = ".autoproxy";
-
+#ifndef CONFIG_WIN32
+static int g_fd;
+#endif
 inline size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
 {
     size_t written;
@@ -96,3 +98,86 @@ inline void remove_string(char *src, char *dst, const char *toremove)
 
     dst[j] = '\0';
 }
+
+#ifndef CONFIG_WIN32
+static int fd_lock(int fd)
+{
+    struct flock lock;
+
+    lock.l_type = F_WRLCK;
+    lock.l_start = 0;
+    lock.l_whence = SEEK_SET;
+    lock.l_len = 0;
+    lock.l_pid = getpid();
+
+    return fcntl(fd, F_SETLK, &lock);
+}
+
+static int fd_unlock(int fd)
+{
+    struct flock lock;
+
+    lock.l_type = F_UNLCK;
+    lock.l_start = 0;
+    lock.l_whence = SEEK_SET;
+    lock.l_len = 0;
+
+    return fcntl(fd, F_SETLK, &lock);
+}
+
+inline bool make_sdcard_lock(char *sdcard)
+{
+    char *lock_file = g_strdup_printf("%s.lck", sdcard);
+    int fd = open(lock_file, O_CREAT|O_RDWR, 0666);
+    if (fd == -1)
+    {
+        perror("file open error : ");
+        return false;
+    }
+    if (fd_lock(fd) == -1)
+    {
+        perror("file is lock ");
+        close(fd);
+        return false;
+    }
+
+    INFO("Get file lock: %s\n", lock_file);
+    g_fd = fd;
+    close(fd);
+    return true;
+
+}
+
+inline int remove_sdcard_lock(char *sdcard)
+{
+    errno = 0;
+    char *lock_file = g_strdup_printf("%s.lck", sdcard);
+    int fd = open(lock_file, O_RDWR, 0666);
+    if (fd == -1)
+    {
+        perror("file open error : ");
+        if(errno == ENOENT) {
+            return ERR_NOENT;
+        }
+        return ERR_UNLCK;
+    }
+
+    if (fd_unlock(fd) == -1)
+    {
+        perror("file unlock error ");
+        close(fd);
+        return ERR_UNLCK;
+    }
+
+    if (unlink(lock_file) < 0) {
+        perror("unlink error ");
+        close(fd);
+        return ERR_UNLCK;
+    }
+
+    INFO("unlock success: %s\n", lock_file);
+    close(fd);
+    close(g_fd);
+    return ERR_SUCCESS;
+}
+#endif
index 27721e306b0a4b80247e062d9f1b518386d815b4..82c40a697591409f99a15ee7ef4c06ac0cc92d5f 100644 (file)
@@ -69,7 +69,10 @@ void check_vm_lock_os(void);
 void make_vm_lock_os(void);
 bool make_sdcard_lock_os(char *sdcard);
 int remove_sdcard_lock_os(char *sdcard);
-
+#ifndef CONFIG_WIN32
+bool make_sdcard_lock(char *sdcard);
+int remove_sdcard_lock(char *sdcard);
+#endif
 void set_bin_path_os(gchar *);
 
 void print_system_info_os(void);
index 090c170f08858779efc619095c8c90ebfe9cf11d..15d4c2a80854c93ca3771e59b93c6d44d1497b98 100644 (file)
@@ -93,7 +93,7 @@ int inet_strtoip(const char*  str, uint32_t  *ip)
 int check_port_bind_listen(uint32_t port)
 {
     struct sockaddr_in addr;
-    int s, opt = 1;
+    int s = 1;
     int ret = -1;
     socklen_t addrlen = sizeof(addr);
     memset(&addr, 0, addrlen);
@@ -109,6 +109,7 @@ int check_port_bind_listen(uint32_t port)
     }
 
 #ifndef _WIN32
+    int opt = 1;
     ret = setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
                     (char *)&opt, sizeof(int));
     if (ret < 0) {