Sync with tizen_2.4, 201511 22/69422/1 accepted/tizen/mobile/20160513.082551 accepted/tizen/tv/20160513.082542 accepted/tizen/wearable/20160513.082545 submit/tizen_mobile/20160513.064904 submit/tizen_tv/20160513.064919 submit/tizen_wearable/20160513.064938
authormin7.choi <min7.choi@samsung.com>
Fri, 13 May 2016 06:42:06 +0000 (15:42 +0900)
committermin7.choi <min7.choi@samsung.com>
Fri, 13 May 2016 06:42:36 +0000 (15:42 +0900)
- Add define to separate emul and target
- Remove () in candidate file name
- Fix max value problems of download id
- Debugging the log macro
- Add null check,remove secure error code
- Remove vconf for user agent
- Fix Prevent in tizen 2.4

Change-Id: Id1e5d2ed6dcc15633c3dc09521404e7291a59a78
Signed-off-by: min7.choi <min7.choi@samsung.com>
22 files changed:
agent/download-agent-file.c
agent/download-agent-plugin-conf.c
agent/include/download-agent-defs.h
packaging/download-provider.spec
provider-interface/download-provider-interface.c
provider/CMakeLists.txt
provider/download-provider-client-manager.c
provider/download-provider-client.c
provider/download-provider-db.c
provider/download-provider-ipc.c
provider/download-provider-main.c
provider/download-provider-notification-manager.c
provider/download-provider-notification.c
provider/download-provider-notify.c
provider/download-provider-plugin-download-agent.c
provider/download-provider-pthread.c
provider/download-provider-queue-manager.c
provider/download-provider-smack.c
provider/download-provider-utils.c
provider/include/download-provider-db.h
provider/include/download-provider-log.h
provider/include/download-provider.h

index f760341..6151569 100755 (executable)
@@ -439,6 +439,9 @@ da_ret_t __decide_file_path_for_resume(file_info_t *file_info)
        NULL_CHECK_RET(file_info);
 
        file_path = file_info->file_path;
+
+       NULL_CHECK_RET(file_path);
+
        ptr = strrchr(file_path, '/');
        if (ptr) {
                ptr++;
@@ -497,6 +500,9 @@ da_ret_t start_file_writing(da_info_t *da_info)
                file_info->file_path = strdup(file_path);
                free(origin_path);
                ret = __decide_file_path_for_resume(file_info);
+        if(file_info) {
+            ret = __decide_file_path_for_resume(file_info);
+        }
        } else {
                ret = __decide_file_path(da_info);
        }
index 6cb76cb..c3dadc7 100755 (executable)
@@ -49,7 +49,7 @@ da_ret_t get_user_agent_string(char **uagent_str)
                DA_LOGE("Invalid Argument");
                return DA_ERR_INVALID_ARGUMENT;
        }
-
+#if 0
        key = VCONFKEY_BROWSER_USER_AGENT;
        ret = __get_conf_string(key, uagent_str);
        if(ret == DA_RESULT_OK) {
@@ -58,6 +58,7 @@ da_ret_t get_user_agent_string(char **uagent_str)
                        return ret;
                }
        }
+#endif
        DA_LOGI("No UA information from vconf !!");
        *uagent_str = strdup(DEFAULT_UA_STR);
        DA_LOGV("Set default UA");
index d5b6a5e..a4efeea 100755 (executable)
@@ -79,7 +79,7 @@
 #define DA_ERR_DRM_FAIL                        -600
 
 // string to check invalid characters in path before using open() and fopen() API's
-#define DA_INVALID_PATH_STRING ";\\\":*?<>|()"
+#define DA_INVALID_PATH_STRING ";\\\":*?<>|"
 
 #endif
 
index c8d8b65..435fa41 100755 (executable)
@@ -1,7 +1,7 @@
 %define _ux_define tizen2.3
 Name:       download-provider
 Summary:    Download the contents in background
-Version:    2.1.26
+Version:    2.1.30
 Release:    0
 Group:      Development/Libraries
 License:    Apache-2.0
@@ -72,7 +72,11 @@ Description: Download the contents in background (development files)
 %define download_booster OFF
 %define sys_resource OFF
 %define support_oma_drm OFF
+%ifarch armv7l
 %define wifi_direct ON
+%else
+%define wifi_direct OFF
+%endif
 %define support_security_privilege OFF
 %define support_companion_mode OFF
 %define support_notification ON
index c0a5002..344982d 100755 (executable)
@@ -223,7 +223,7 @@ static int __create_socket()
        struct sockaddr_un clientaddr;
 
        if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
-               TRACE_STRERROR("[CRITICAL] socket system error");
+               TRACE_ERROR("[CRITICAL] socket system error");
                return -1;
        }
 
@@ -236,7 +236,7 @@ static int __create_socket()
                (struct sockaddr*)&clientaddr, sizeof(clientaddr)) < 0) {
                close(sockfd);
                if (errno == EACCES || errno == EPERM) {
-                       TRACE_STRERROR("check permission");
+                       TRACE_ERROR("check permission");
                        return -DP_ERROR_PERMISSION_DENIED;
                }
                return -1;
@@ -268,7 +268,7 @@ static int __bp_disconnect(const char *funcname)
        if (g_dp_event_thread_id > 0 &&
                        pthread_kill(g_dp_event_thread_id, 0) != ESRCH) {
                if (pthread_cancel(g_dp_event_thread_id) != 0) {
-                       TRACE_STRERROR("pthread:%d", (int)g_dp_event_thread_id);
+                       TRACE_ERROR("pthread:%d", (int)g_dp_event_thread_id);
                }
                g_dp_event_thread_id = 0;
        }
@@ -280,7 +280,7 @@ static int __bp_disconnect(const char *funcname)
 static void *__dp_event_manager(void *arg)
 {
        if (g_dp_client == NULL) {
-               TRACE_STRERROR("[CRITICAL] INTERFACE null");
+               TRACE_ERROR("[CRITICAL] INTERFACE null");
                return 0;
        }
 
@@ -290,7 +290,7 @@ static void *__dp_event_manager(void *arg)
        TRACE_DEBUG("IPC ESTABILISH %s", notify_fifo);
        g_dp_client->notify = open(notify_fifo, O_RDONLY, 0600);
        if (g_dp_client->notify < 0) {
-               TRACE_STRERROR("[CRITICAL] failed to ESTABILISH IPC %s", notify_fifo);
+               TRACE_ERROR("[CRITICAL] failed to ESTABILISH IPC %s", notify_fifo);
                g_dp_event_thread_id = 0;
                CLIENT_MUTEX_LOCK(&g_function_mutex);
                __clear_interface();
@@ -389,7 +389,7 @@ static int __dp_ipc_response(int sock, int download_id, short section,
        if (ipc_info == NULL || ipc_info->section != section ||
                        ipc_info->property != property ||
                        (download_id >= 0 && ipc_info->id != download_id)) {
-               TRACE_STRERROR("socket read ipcinfo");
+               TRACE_ERROR("socket read ipcinfo");
                free(ipc_info);
                return DP_ERROR_IO_ERROR;
        }
@@ -422,7 +422,7 @@ static int __connect_to_provider()
                while(g_dp_client->channel < 0 && connect_retry-- > 0) {
                        int ret = __create_socket();
                        if (ret == -1) {
-                               TRACE_STRERROR("failed to connect to provider(remains:%d)", connect_retry);
+                               TRACE_ERROR("failed to connect to provider(remains:%d)", connect_retry);
                                struct timespec ts;
                                ts.tv_sec = 0;
                                ts.tv_nsec = 20000000;
@@ -431,18 +431,18 @@ static int __connect_to_provider()
                                struct timeval tv_timeo = { 5, 500000 }; // 5.5 second
                                g_dp_client->channel = ret;
                                if (setsockopt(g_dp_client->channel, SOL_SOCKET, SO_RCVTIMEO, &tv_timeo, sizeof(tv_timeo)) < 0) {
-                                       TRACE_STRERROR("[CRITICAL] setsockopt SO_RCVTIMEO");
+                                       TRACE_ERROR("[CRITICAL] setsockopt SO_RCVTIMEO");
                                        errorcode = DP_ERROR_IO_ERROR;
                                        goto EXIT_CONNECT;
                                }
                        } else {
                                errorcode = -ret;
-                               TRACE_STRERROR("check error:%d", errorcode);
+                               TRACE_ERROR("check error:%d", errorcode);
                                goto EXIT_CONNECT;
                        }
                }
                if (g_dp_client->channel < 0) {
-                       TRACE_STRERROR("[CRITICAL] connect system error");
+                       TRACE_ERROR("[CRITICAL] connect system error");
                        errorcode = DP_ERROR_IO_ERROR;
                        goto EXIT_CONNECT;
                }
@@ -479,7 +479,7 @@ static int __connect_to_provider()
                if (errorcode == DP_ERROR_NONE && g_dp_event_thread_id <= 0) {
                        if (pthread_create(&g_dp_event_thread_id, NULL,
                                        __dp_event_manager, g_dp_client) != 0) {
-                               TRACE_STRERROR("failed to create event-manager");
+                               TRACE_ERROR("failed to create event-manager");
                                errorcode = DP_ERROR_IO_ERROR;
                        } else {
                                pthread_detach(g_dp_event_thread_id);
@@ -600,7 +600,7 @@ static int __dp_ipc_get_string(const int id, const unsigned property,
                        if (string_length > 0) {
                                char *recv_str = (char *)calloc((string_length + (size_t)1), sizeof(char));
                                if (recv_str == NULL) {
-                                       TRACE_STRERROR("check memory length:%d", string_length);
+                                       TRACE_ERROR("check memory length:%d", string_length);
                                        errorcode = DP_ERROR_OUT_OF_MEMORY;
                                        __dp_ipc_clear_garbage(sock, string_length);
                                } else {
@@ -1197,7 +1197,7 @@ int dp_interface_get_notification_bundle(const int id, const int type, void **bu
        if (errorcode == DP_ERROR_NONE && extra_size > 0) {
                unsigned char *recv_raws = (unsigned char *)calloc(extra_size, sizeof(unsigned char));
                if (recv_raws == NULL) {
-                       TRACE_STRERROR("sock:%d check memory length:%d", sock, extra_size);
+                       TRACE_ERROR("sock:%d check memory length:%d", sock, extra_size);
                        errorcode = DP_ERROR_OUT_OF_MEMORY;
                        __dp_ipc_clear_garbage(sock, extra_size);
                } else {
@@ -1301,7 +1301,7 @@ int dp_interface_get_notification_service_handle(const int id, const int type, v
        if (errorcode == DP_ERROR_NONE && extra_size > 0) {
                unsigned char *recv_raws = (unsigned char *)calloc(extra_size, sizeof(unsigned char));
                if (recv_raws == NULL) {
-                       TRACE_STRERROR("sock:%d check memory length:%d", sock, extra_size);
+                       TRACE_ERROR("sock:%d check memory length:%d", sock, extra_size);
                        errorcode = DP_ERROR_OUT_OF_MEMORY;
                        __dp_ipc_clear_garbage(sock, extra_size);
                } else {
@@ -1433,7 +1433,7 @@ int dp_interface_get_http_header_field(const int id, const char *field,
                                if (string_length > 0) {
                                        char *recv_str = (char *)calloc((string_length + (size_t)1), sizeof(char));
                                        if (recv_str == NULL) {
-                                               TRACE_STRERROR("check memory length:%d", string_length);
+                                               TRACE_ERROR("check memory length:%d", string_length);
                                                errorcode = DP_ERROR_OUT_OF_MEMORY;
                                                __dp_ipc_clear_garbage(sock, string_length);
                                        } else {
@@ -1502,7 +1502,7 @@ int dp_interface_get_http_header_field_list(const int id, char ***fields,
                                                char **recv_strings = NULL;
                                                recv_strings = (char **)calloc(array_size, sizeof(char *));
                                                if (recv_strings == NULL) {
-                                                       TRACE_STRERROR("check memory size:%d", array_size);
+                                                       TRACE_ERROR("check memory size:%d", array_size);
                                                        errorcode = DP_ERROR_OUT_OF_MEMORY;
                                                        *length = 0;
                                                } else {
@@ -1514,7 +1514,7 @@ int dp_interface_get_http_header_field_list(const int id, char ***fields,
                                                                if (errorcode == DP_ERROR_NONE && string_length > 0) {
                                                                        char *recv_str = (char *)calloc((string_length + (size_t)1), sizeof(char));
                                                                        if (recv_str == NULL) {
-                                                                               TRACE_STRERROR("check memory length:%d", string_length * sizeof(char));
+                                                                               TRACE_ERROR("check memory length:%d", string_length * sizeof(char));
                                                                                errorcode = DP_ERROR_OUT_OF_MEMORY;
                                                                                break;
                                                                        } else {
index ab93960..12f1c8f 100755 (executable)
@@ -21,6 +21,7 @@ pkg_check_modules(dp2_pkgs REQUIRED glib-2.0
                dlog
                libsystemd-daemon
                vconf
+               libcurl
                cynara-client
                cynara-client-async
                cynara-creds-socket
index 8f6dab3..a10bdf2 100644 (file)
@@ -57,12 +57,15 @@ int g_dp_sock = -1;
 dp_client_slots_fmt *g_dp_client_slots = NULL;
 static void *g_db_handle = 0;
 static pthread_mutex_t g_db_mutex = PTHREAD_MUTEX_INITIALIZER;
+extern pthread_t g_client_manager_tid;
 
 void dp_terminate(int signo)
 {
        TRACE_DEBUG("Received SIGTERM:%d", signo);
        close(g_dp_sock);
        g_dp_sock = -1;
+       if (g_client_manager_tid > 0)
+           pthread_kill(g_client_manager_tid, SIGUSR1);
 }
 
 void dp_broadcast_signal()
@@ -98,14 +101,15 @@ char *dp_db_get_client_smack_label(const char *pkgname)
 
 static int __dp_db_open_client_manager()
 {
-       CLIENT_MUTEX_LOCK(&g_db_mutex);
-       if (dp_db_open_client_manager(&g_db_handle) < 0) {
-               TRACE_ERROR("[CRITICAL] can not open SQL");
-               CLIENT_MUTEX_UNLOCK(&g_db_mutex);
-               return -1;
-       }
-       CLIENT_MUTEX_UNLOCK(&g_db_mutex);
-       return 0;
+    int errorcode = DP_ERROR_NONE;
+    CLIENT_MUTEX_LOCK(&g_db_mutex);
+    if (g_db_handle == 0 || dp_db_check_connection(g_db_handle) < 0) {
+        if (dp_db_open_client_manager(&g_db_handle, &errorcode) < 0) {
+            TRACE_ERROR("failed to open database errorcode:%d", errorcode);
+        }
+    }
+    CLIENT_MUTEX_UNLOCK(&g_db_mutex);
+    return errorcode;
 }
 
 static void __dp_db_free_client_manager()
@@ -330,7 +334,7 @@ static int __dp_client_run(int clientfd, dp_client_slots_fmt *slot,
        // make notify fifo
        slot->client.notify = dp_notify_init(credential.pid);
        if (slot->client.notify < 0) {
-               TRACE_STRERROR("failed to open fifo slot:%d", clientfd);
+               TRACE_ERROR("failed to open fifo slot:%d", clientfd);
                errorcode = DP_ERROR_IO_ERROR;
        } else {
                char *smack_label = NULL;
@@ -561,7 +565,7 @@ void *dp_client_manager(void *arg)
 
        g_dp_sock = __dp_accept_socket_new();
        if (g_dp_sock < 0) {
-               TRACE_STRERROR("failed to open listen socket");
+               TRACE_ERROR("failed to open listen socket");
                g_main_loop_quit(event_loop);
                return 0;
        }
@@ -589,12 +593,6 @@ void *dp_client_manager(void *arg)
        dp_rebuild_dir(NOTIFY_DIR, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
 #endif
 
-       if (__dp_db_open_client_manager() < 0) {
-               TRACE_STRERROR("failed to open database for client-manager");
-               g_main_loop_quit(event_loop);
-               return 0;
-       }
-
        dp_client_slots_fmt *clients =
                (dp_client_slots_fmt *)calloc(DP_MAX_CLIENTS,
                        sizeof(dp_client_slots_fmt));
@@ -629,7 +627,7 @@ void *dp_client_manager(void *arg)
                eset = except_fdset;
 
                if (select((maxfd + 1), &rset, 0, &eset, &timeout) < 0) {
-                       TRACE_STRERROR("interrupted by terminating");
+                       TRACE_ERROR("interrupted by terminating");
                        break;
                }
 
@@ -639,7 +637,7 @@ void *dp_client_manager(void *arg)
                }
 
                if (FD_ISSET(g_dp_sock, &eset) > 0) {
-                       TRACE_STRERROR("exception of socket");
+                       TRACE_ERROR("exception of socket");
                        break;
                } else if (FD_ISSET(g_dp_sock, &rset) > 0) {
 
@@ -648,9 +646,9 @@ void *dp_client_manager(void *arg)
                        clientfd = accept(g_dp_sock, (struct sockaddr *)&clientaddr,
                                        &clientlen);
                        if (clientfd < 0) {
-                               TRACE_STRERROR("too many client ? accept failure");
+                               TRACE_ERROR("too many client ? accept failure");
                                // provider need the time of refresh.
-                               break;
+                               errorcode = DP_ERROR_DISK_BUSY;
                        }
 
                        // blocking & timeout to prevent the lockup by client.
@@ -658,8 +656,6 @@ void *dp_client_manager(void *arg)
                        if (setsockopt(clientfd, SOL_SOCKET, SO_RCVTIMEO, &tv_timeo,
                                        sizeof(tv_timeo)) < 0) {
                                TRACE_ERROR("failed to set timeout in blocking socket");
-                               close(clientfd);
-                               continue;
                        }
 
                        dp_ipc_fmt ipc_info;
@@ -670,8 +666,7 @@ void *dp_client_manager(void *arg)
                                        ipc_info.id != -1 ||
                                        ipc_info.size != 0) {
                                TRACE_ERROR("peer terminate ? ignore this connection");
-                               close(clientfd);
-                               continue;
+                               errorcode = DP_ERROR_INVALID_PARAMETER;
                        }
 
 #ifdef SO_PEERCRED // getting the info of client
@@ -679,49 +674,44 @@ void *dp_client_manager(void *arg)
                        if (getsockopt(clientfd, SOL_SOCKET, SO_PEERCRED,
                                        &credential, &cr_len) < 0) {
                                TRACE_ERROR("failed to cred from sock:%d", clientfd);
-                               close(clientfd);
-                               continue;
+                               errorcode = DP_ERROR_PERMISSION_DENIED;
                        }
 #else // In case of not supported SO_PEERCRED
                        if (read(clientfd, &credential, sizeof(dp_credential)) <= 0) {
                                TRACE_ERROR("failed to cred from client:%d", clientfd);
-                               close(clientfd);
-                               continue;
+                               errorcode = DP_ERROR_PERMISSION_DENIED;
                        }
 #endif
 
-                       CLIENT_MUTEX_LOCK(&g_db_mutex);
-                       if (dp_db_check_connection(g_db_handle) < 0) {
-                               TRACE_ERROR("check database, provider can't work anymore");
-                               CLIENT_MUTEX_UNLOCK(&g_db_mutex);
-                               close(clientfd);
-                               break;
-                       }
-                       CLIENT_MUTEX_UNLOCK(&g_db_mutex);
-
-                       if (ipc_info.section == DP_SEC_INIT) {
-
-                               // new client
-                               errorcode = __dp_client_new(clientfd, clients, credential);
-
-                       } else {
-                               errorcode = DP_ERROR_INVALID_PARAMETER;
-                       }
-                       if (dp_ipc_query(clientfd, -1, DP_SEC_INIT, DP_PROP_NONE, errorcode, 0) < 0) {
-                               TRACE_ERROR("check ipc sock:%d", clientfd);
-                       }
-                       if (errorcode == DP_ERROR_NONE) {
-                               // write client info into database
-                               
-                       } else {
-                               TRACE_ERROR("sock:%d id:%d section:%s property:%s errorcode:%s size:%d",
-                                       clientfd, ipc_info.id,
-                                       dp_print_section(ipc_info.section),
-                                       dp_print_property(ipc_info.property),
-                                       dp_print_errorcode(ipc_info.errorcode),
-                                       ipc_info.size);
-                               close(clientfd); // ban this client
-                       }
+            errorcode = __dp_db_open_client_manager();
+
+            if (errorcode == DP_ERROR_NONE) {
+                if (ipc_info.section == DP_SEC_INIT) {
+
+                    // new client
+                    errorcode = __dp_client_new(clientfd, clients, credential);
+
+                } else {
+                    errorcode = DP_ERROR_INVALID_PARAMETER;
+                }
+            }
+            if (dp_ipc_query(clientfd, -1, ipc_info.section, DP_PROP_NONE, errorcode, 0) < 0) {
+                TRACE_ERROR("check ipc sock:%d", clientfd);
+            }
+
+            if (errorcode != DP_ERROR_NONE) {
+                TRACE_ERROR("sock:%d id:%d section:%s property:%s errorcode:%s size:%d",
+                        clientfd, ipc_info.id,
+                        dp_print_section(ipc_info.section),
+                        dp_print_property(ipc_info.property),
+                        dp_print_errorcode(ipc_info.errorcode),
+                        ipc_info.size);
+                close(clientfd); // ban this client
+            }
+            if (errorcode == DP_ERROR_NO_SPACE || errorcode == DP_ERROR_DISK_BUSY) {
+                TRACE_ERROR("provider can't work anymore errorcode:%s", dp_print_errorcode(errorcode));
+                //break;  // provider will be terminated after sending errorcode by each thread
+            }
 
                } else {
 
index b2f9f31..3b210c8 100755 (executable)
@@ -19,6 +19,7 @@
 #include <stdlib.h>
 #include <sys/stat.h>
 #include <signal.h>
+#include <limits.h>
 
 #include <download-provider-log.h>
 #include <download-provider-config.h>
@@ -219,42 +220,56 @@ char *dp_print_property(unsigned property)
 
 static int __dp_get_download_id(dp_client_fmt *client)
 {
-       int download_id = -1;
-       static int last_download_id = 0;
-       int check_duplicate = 0;
-       int errorcode = DP_ERROR_NONE;
-
-       do {
-               do {
-                       struct timeval tval;
-                       int cipher = 1;
-                       int c = 0;
-
-                       download_id = -1;
-                       gettimeofday(&tval, NULL);
-
-                       int usec = tval.tv_usec;
-                       for (c = 0; ; c++, cipher++) {
-                               if ((usec /= 10) <= 0)
-                                       break;
-                       }
-                       if (tval.tv_usec == 0)
-                               tval.tv_usec = (tval.tv_sec & 0x0fff);
-                       int disit_unit = 10;
-                       for (c = 0; c < cipher - 3; c++)
-                               disit_unit = disit_unit * 10;
-                       download_id = tval.tv_sec + ((tval.tv_usec << 2) * 100) +
-                                       ((tval.tv_usec >> (cipher - 1)) * disit_unit) +
-                                       ((tval.tv_usec + (tval.tv_usec % 10)) & 0x0fff);
-               } while (last_download_id == download_id);
-               last_download_id = download_id;
-               check_duplicate = dp_db_check_duplicated_int(client->dbhandle,
-                               DP_TABLE_LOGGING, DP_DB_COL_ID, download_id, &errorcode);
-               if (errorcode != DP_ERROR_NONE) {
-                       TRACE_ERROR("ERROR [%d]", errorcode);
-               }
-       } while (check_duplicate != 0); // means duplicated id
-       return download_id;
+    int download_id = -1;
+    int check_duplicate = 1;
+    int retry_count = 0;
+    int errorcode = DP_ERROR_NONE;
+    struct timeval tval;
+
+    if (dp_db_get_max_download_id(client->dbhandle,
+                DP_TABLE_LOGGING, &download_id, &errorcode) < 0) {
+        TRACE_ERROR("ERROR [%d]", errorcode);
+        // database is empty start with id : 1
+        download_id = DP_FIRST_DOWNLOAD_ID;
+    } else {
+        if (download_id < INT_MAX)
+            download_id++;
+    }
+
+    retry_count = 0;
+    do {
+        retry_count++;
+        if (download_id < INT_MAX) {
+            TRACE_DEBUG("download_id [%d]", download_id);
+            check_duplicate = dp_db_check_duplicated_int(client->dbhandle,
+                    DP_TABLE_LOGGING, DP_DB_COL_ID, download_id, &errorcode);
+            if (errorcode == DP_ERROR_NONE) {
+                if (check_duplicate == 0) {
+                    break;
+                }
+            } else {
+                TRACE_ERROR("ERROR [%d]", errorcode);
+            }
+            if (retry_count < 3) {
+                download_id++;
+            } else if (retry_count < 10) {
+                gettimeofday(&tval, NULL);
+                tval.tv_usec = (tval.tv_usec & 0x0fff);
+                download_id += 1171 * retry_count + tval.tv_usec;
+            } else if (retry_count < 20) {
+                gettimeofday(&tval, NULL);
+                tval.tv_usec = (tval.tv_usec & 0xff33ff) + retry_count;
+                download_id += tval.tv_usec;
+            } else {
+                TRACE_ERROR("failed to generate unique download_id [%d]", download_id);
+                return -1;
+            }
+        } else {
+            TRACE_ERROR("reached INT_MAX limit");
+            download_id = DP_FIRST_DOWNLOAD_ID;
+        }
+    } while (check_duplicate != 0);
+    return download_id;
 }
 
 void dp_request_create(dp_client_fmt *client, dp_request_fmt *request)
@@ -293,11 +308,11 @@ static int __dp_request_create(dp_client_fmt *client, dp_ipc_fmt *ipc_info)
                return DP_ERROR_OUT_OF_MEMORY;
        }
 
-       if (dp_db_new_logging(client->dbhandle, download_id, DP_STATE_READY, DP_ERROR_NONE, &errorcode) < 0) {
-               TRACE_ERROR("new log sock:%d download-id:%d", client->channel, download_id);
-               free(request);
-               return DP_ERROR_DISK_BUSY;
-       }
+    if (dp_db_new_logging(client->dbhandle, download_id, DP_STATE_READY, DP_ERROR_NONE, &errorcode) < 0) {
+        TRACE_ERROR("new log sock:%d download-id:%d errorcode:%s", client->channel, download_id, dp_print_errorcode(errorcode));
+        free(request);
+        return errorcode;
+    }
 
        request->id = download_id;
        request->agent_id = -1;
@@ -483,7 +498,7 @@ static int __dp_request_read_string(int sock, dp_ipc_fmt *ipc_info, char **strin
        if (ipc_info->size > 0) {
                char *recv_str = (char *)calloc((ipc_info->size + (size_t)1), sizeof(char));
                if (recv_str == NULL) {
-                       TRACE_STRERROR("sock:%d check memory length:%d", sock, ipc_info->size);
+                       TRACE_ERROR("sock:%d check memory length:%d", sock, ipc_info->size);
                        errorcode = DP_ERROR_OUT_OF_MEMORY;
                } else {
                        if (dp_ipc_read(sock, recv_str, ipc_info->size, __FUNCTION__) <= 0) {
@@ -579,7 +594,7 @@ static int __dp_request_get_info(dp_client_fmt *client, dp_ipc_fmt *ipc_info, dp
                        callback = requestp->state_cb;
                } else {
                        if (dp_db_get_property_int(client->dbhandle, ipc_info->id, DP_TABLE_REQUEST, DP_DB_COL_STATE_EVENT, &callback, &errorcode) < 0) {
-                               TRACE_ERROR("failed to get %d", dp_print_property(ipc_info->property));
+                               TRACE_ERROR("failed to get %s", dp_print_property(ipc_info->property));
                                errorcode = DP_ERROR_NO_DATA;
                        }
                }
@@ -597,7 +612,7 @@ static int __dp_request_get_info(dp_client_fmt *client, dp_ipc_fmt *ipc_info, dp
                        callback = requestp->progress_cb;
                } else {
                        if (dp_db_get_property_int(client->dbhandle, ipc_info->id, DP_TABLE_REQUEST, DP_DB_COL_PROGRESS_EVENT, &callback, &errorcode) < 0) {
-                               TRACE_ERROR("failed to get %d", dp_print_property(ipc_info->property));
+                               TRACE_ERROR("failed to get %s", dp_print_property(ipc_info->property));
                                errorcode = DP_ERROR_NO_DATA;
                        }
                }
@@ -612,7 +627,7 @@ static int __dp_request_get_info(dp_client_fmt *client, dp_ipc_fmt *ipc_info, dp
        {
                int autodownload = 0;
                if (dp_db_get_property_int(client->dbhandle, ipc_info->id, DP_TABLE_LOGGING, DP_DB_COL_AUTO_DOWNLOAD, &autodownload, &errorcode) < 0) {
-                       TRACE_ERROR("failed to get %d", dp_print_property(ipc_info->property));
+                       TRACE_ERROR("failed to get %s", dp_print_property(ipc_info->property));
                        errorcode = DP_ERROR_NO_DATA;
                }
                int result = __dp_request_feedback_int(client->channel, ipc_info, (void *)&autodownload, errorcode, sizeof(int));
@@ -629,7 +644,7 @@ static int __dp_request_get_info(dp_client_fmt *client, dp_ipc_fmt *ipc_info, dp
                        network = requestp->network_type;
                } else {
                        if (dp_db_get_property_int(client->dbhandle, ipc_info->id, DP_TABLE_REQUEST, DP_DB_COL_NETWORK_TYPE, &network, &errorcode) < 0) {
-                               TRACE_ERROR("failed to get %d", dp_print_property(ipc_info->property));
+                               TRACE_ERROR("failed to get %s", dp_print_property(ipc_info->property));
                                errorcode = DP_ERROR_NO_DATA;
                        }
                }
@@ -644,7 +659,7 @@ static int __dp_request_get_info(dp_client_fmt *client, dp_ipc_fmt *ipc_info, dp
        {
                int network_bonding = 0;
                if (dp_db_get_property_int(client->dbhandle, ipc_info->id, DP_TABLE_REQUEST, DP_DB_COL_NETWORK_BONDING, &network_bonding, &errorcode) < 0) {
-                       TRACE_ERROR("failed to get %d", dp_print_property(ipc_info->property));
+                       TRACE_ERROR("failed to get %s", dp_print_property(ipc_info->property));
                        errorcode = DP_ERROR_NO_DATA;
                }
                int result = __dp_request_feedback_int(client->channel, ipc_info, (void *)&network_bonding, errorcode, sizeof(int));
@@ -659,7 +674,7 @@ static int __dp_request_get_info(dp_client_fmt *client, dp_ipc_fmt *ipc_info, dp
                char *string = NULL;
                unsigned length = 0;
                if (dp_db_get_property_string(client->dbhandle, ipc_info->id, DP_TABLE_DOWNLOAD, DP_DB_COL_SAVED_PATH, (unsigned char **)&string, &length, &errorcode) < 0) {
-                       TRACE_ERROR("failed to get %d", dp_print_property(ipc_info->property));
+                       TRACE_ERROR("failed to get %s", dp_print_property(ipc_info->property));
                        errorcode = DP_ERROR_NO_DATA;
                }
                int result = __dp_request_feedback_string(client->channel, ipc_info, string, length, errorcode);
@@ -675,7 +690,7 @@ static int __dp_request_get_info(dp_client_fmt *client, dp_ipc_fmt *ipc_info, dp
                char *string = NULL;
                unsigned length = 0;
                if (dp_db_get_property_string(client->dbhandle, ipc_info->id, DP_TABLE_DOWNLOAD, DP_DB_COL_TMP_SAVED_PATH, (unsigned char **)&string, &length, &errorcode) < 0) {
-                       TRACE_ERROR("failed to get %d", dp_print_property(ipc_info->property));
+                       TRACE_ERROR("failed to get %s", dp_print_property(ipc_info->property));
                        errorcode = DP_ERROR_NO_DATA;
                }
                int result = __dp_request_feedback_string(client->channel, ipc_info, string, length, errorcode);
@@ -691,7 +706,7 @@ static int __dp_request_get_info(dp_client_fmt *client, dp_ipc_fmt *ipc_info, dp
                char *string = NULL;
                unsigned length = 0;
                if (dp_db_get_property_string(client->dbhandle, ipc_info->id, DP_TABLE_DOWNLOAD, DP_DB_COL_MIMETYPE, (unsigned char **)&string, &length, &errorcode) < 0) {
-                       TRACE_ERROR("failed to get %d", dp_print_property(ipc_info->property));
+                       TRACE_ERROR("failed to get %s", dp_print_property(ipc_info->property));
                        errorcode = DP_ERROR_NO_DATA;
                }
                int result = __dp_request_feedback_string(client->channel, ipc_info, string, length, errorcode);
@@ -725,7 +740,7 @@ static int __dp_request_get_info(dp_client_fmt *client, dp_ipc_fmt *ipc_info, dp
                } else {
                        // load content_size(INT64) from database;
                        if (dp_db_get_property_int(client->dbhandle, ipc_info->id, DP_TABLE_DOWNLOAD, DP_DB_COL_CONTENT_SIZE, &file_size, &errorcode) < 0) {
-                               TRACE_ERROR("failed to get %d", dp_print_property(ipc_info->property));
+                               TRACE_ERROR("failed to get %s", dp_print_property(ipc_info->property));
                                errorcode = DP_ERROR_NO_DATA;
                        }
                }
@@ -741,7 +756,7 @@ static int __dp_request_get_info(dp_client_fmt *client, dp_ipc_fmt *ipc_info, dp
                char *string = NULL;
                unsigned length = 0;
                if (dp_db_get_property_string(client->dbhandle, ipc_info->id, DP_TABLE_DOWNLOAD, DP_DB_COL_CONTENT_NAME, (unsigned char **)&string, &length, &errorcode) < 0) {
-                       TRACE_ERROR("failed to get %d", dp_print_property(ipc_info->property));
+                       TRACE_ERROR("failed to get %s", dp_print_property(ipc_info->property));
                        errorcode = DP_ERROR_NO_DATA;
                }
                int result = __dp_request_feedback_string(client->channel, ipc_info, string, length, errorcode);
@@ -756,7 +771,7 @@ static int __dp_request_get_info(dp_client_fmt *client, dp_ipc_fmt *ipc_info, dp
        {
                int httpstatus = 0;
                if (dp_db_get_property_int(client->dbhandle, ipc_info->id, DP_TABLE_DOWNLOAD, DP_DB_COL_HTTP_STATUS, &httpstatus, &errorcode) < 0) {
-                       TRACE_ERROR("failed to get %d", dp_print_property(ipc_info->property));
+                       TRACE_ERROR("failed to get %s", dp_print_property(ipc_info->property));
                        errorcode = DP_ERROR_NO_DATA;
                }
                int result = __dp_request_feedback_int(client->channel, ipc_info, (void *)&httpstatus, errorcode, sizeof(int));
@@ -771,7 +786,7 @@ static int __dp_request_get_info(dp_client_fmt *client, dp_ipc_fmt *ipc_info, dp
                char *string = NULL;
                unsigned length = 0;
                if (dp_db_get_property_string(client->dbhandle, ipc_info->id, DP_TABLE_DOWNLOAD, DP_DB_COL_ETAG, (unsigned char **)&string, &length, &errorcode) < 0) {
-                       TRACE_ERROR("failed to get %d", dp_print_property(ipc_info->property));
+                       TRACE_ERROR("failed to get %s", dp_print_property(ipc_info->property));
                        errorcode = DP_ERROR_NO_DATA;
                }
                int result = __dp_request_feedback_string(client->channel, ipc_info, string, length, errorcode);
@@ -789,7 +804,7 @@ static int __dp_request_get_info(dp_client_fmt *client, dp_ipc_fmt *ipc_info, dp
                        state = requestp->state;
                } else {
                        if (dp_db_get_property_int(client->dbhandle, ipc_info->id, DP_TABLE_LOGGING, DP_DB_COL_STATE, &state, &errorcode) < 0) {
-                               TRACE_ERROR("failed to get %d", dp_print_property(ipc_info->property));
+                               TRACE_ERROR("failed to get %s", dp_print_property(ipc_info->property));
                                errorcode = DP_ERROR_ID_NOT_FOUND;
                        }
                }
@@ -807,7 +822,7 @@ static int __dp_request_get_info(dp_client_fmt *client, dp_ipc_fmt *ipc_info, dp
                        errorvalue = requestp->error;
                } else {
                        if (dp_db_get_property_int(client->dbhandle, ipc_info->id, DP_TABLE_LOGGING, DP_DB_COL_ERRORCODE, &errorvalue, &errorcode) < 0) {
-                               TRACE_ERROR("failed to get %d", dp_print_property(ipc_info->property));
+                               TRACE_ERROR("failed to get %s", dp_print_property(ipc_info->property));
                                errorcode = DP_ERROR_ID_NOT_FOUND;
                        }
                }
@@ -826,7 +841,7 @@ static int __dp_request_get_info(dp_client_fmt *client, dp_ipc_fmt *ipc_info, dp
                        // if already notification, unregister from notification bar.
                } else {
                        if (dp_db_get_property_int(client->dbhandle, ipc_info->id, DP_TABLE_NOTIFICATION, DP_DB_COL_NOTI_TYPE, &noti_type, &errorcode) < 0) {
-                               TRACE_ERROR("failed to get %d", dp_print_property(ipc_info->property));
+                               TRACE_ERROR("failed to get %s", dp_print_property(ipc_info->property));
                                errorcode = DP_ERROR_NO_DATA;
                        }
                }
@@ -842,7 +857,7 @@ static int __dp_request_get_info(dp_client_fmt *client, dp_ipc_fmt *ipc_info, dp
                char *string = NULL;
                unsigned length = 0;
                if (dp_db_get_property_string(client->dbhandle, ipc_info->id, DP_TABLE_NOTIFICATION, DP_DB_COL_NOTI_SUBJECT, (unsigned char **)&string, &length, &errorcode) < 0) {
-                       TRACE_ERROR("failed to get %d", dp_print_property(ipc_info->property));
+                       TRACE_ERROR("failed to get %s", dp_print_property(ipc_info->property));
                        errorcode = DP_ERROR_NO_DATA;
                }
                int result = __dp_request_feedback_string(client->channel, ipc_info, string, length, errorcode);
@@ -858,7 +873,7 @@ static int __dp_request_get_info(dp_client_fmt *client, dp_ipc_fmt *ipc_info, dp
                char *string = NULL;
                unsigned length = 0;
                if (dp_db_get_property_string(client->dbhandle, ipc_info->id, DP_TABLE_NOTIFICATION, DP_DB_COL_NOTI_DESCRIPTION, (unsigned char **)&string, &length, &errorcode) < 0) {
-                       TRACE_ERROR("failed to get %d", dp_print_property(ipc_info->property));
+                       TRACE_ERROR("failed to get %s", dp_print_property(ipc_info->property));
                        errorcode = DP_ERROR_NO_DATA;
                }
                int result = __dp_request_feedback_string(client->channel, ipc_info, string, length, errorcode);
@@ -884,7 +899,7 @@ static int __dp_request_get_info(dp_client_fmt *client, dp_ipc_fmt *ipc_info, dp
 
                if (raw_column == NULL) {
                        errorcode = DP_ERROR_INVALID_PARAMETER;
-                       TRACE_ERROR("invalid type %d type:%d", dp_print_property(ipc_info->property), bundle_type);
+                       TRACE_ERROR("invalid type %s type:%d", dp_print_property(ipc_info->property), bundle_type);
                        if (dp_ipc_query(client->channel, ipc_info->id, ipc_info->section, ipc_info->property, errorcode, 0) < 0) {
                                errorcode = DP_ERROR_IO_ERROR;
                                TRACE_ERROR("check ipc sock:%d", client->channel);
@@ -895,7 +910,7 @@ static int __dp_request_get_info(dp_client_fmt *client, dp_ipc_fmt *ipc_info, dp
                        unsigned length = 0;
                        // get blob binary from database by raw_column
                        if (dp_db_get_property_string(client->dbhandle, ipc_info->id, DP_TABLE_NOTIFICATION, raw_column, &raws_buffer, &length, &errorcode) < 0) {
-                               TRACE_ERROR("failed to get %d", dp_print_property(ipc_info->property));
+                               TRACE_ERROR("failed to get %s", dp_print_property(ipc_info->property));
                                errorcode = DP_ERROR_NO_DATA;
                        }
                        int result = __dp_request_feedback_string(client->channel, ipc_info, raws_buffer, (size_t)length, errorcode);
@@ -1310,7 +1325,7 @@ static int __dp_request_set_info(dp_client_slots_fmt *slot, dp_ipc_fmt *ipc_info
                        if (raw_info != NULL && raw_info->size > 0) {
                                unsigned char *recv_raws = (unsigned char *)calloc(raw_info->size, sizeof(unsigned char));
                                if (recv_raws == NULL) {
-                                       TRACE_STRERROR("sock:%d check memory length:%d", client->channel, raw_info->size);
+                                       TRACE_ERROR("sock:%d check memory length:%d", client->channel, raw_info->size);
                                        errorcode = DP_ERROR_OUT_OF_MEMORY;
                                } else {
                                        if (dp_ipc_read(client->channel, recv_raws, raw_info->size, __FUNCTION__) <= 0) {
@@ -1983,7 +1998,7 @@ void *dp_client_request_thread(void *arg)
                                CLIENT_MUTEX_UNLOCK(&slot->mutex);
                                continue;
                        } else {
-                               TRACE_STRERROR("interrupted by client-manager sock:%d", client_sock);
+                               TRACE_ERROR("interrupted by client-manager sock:%d", client_sock);
                                break;
                        }
                }
@@ -1991,13 +2006,7 @@ void *dp_client_request_thread(void *arg)
 
                        CLIENT_MUTEX_LOCK(&slot->mutex);
 
-                       if (client->dbhandle == 0 || dp_db_check_connection(client->dbhandle) < 0) {
-                               if (dp_db_open_client(&client->dbhandle, slot->pkgname) < 0) {
-                                       TRACE_ERROR("failed to open database for sock:%d", client_sock);
-                                       CLIENT_MUTEX_UNLOCK(&slot->mutex);
-                                       break;
-                               }
-                       }
+                       errorcode = DP_ERROR_NONE;
 
                        pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
                        client->access_time = (int)time(NULL);
@@ -2005,7 +2014,7 @@ void *dp_client_request_thread(void *arg)
                        // read ipc_fmt first. below func will deal followed packets
                        dp_ipc_fmt *ipc_info = dp_ipc_get_fmt(client_sock);
                        if (ipc_info == NULL) {
-                               TRACE_STRERROR("sock:%d maybe closed", client_sock);
+                               TRACE_ERROR("sock:%d maybe closed", client_sock);
                                errorcode = DP_ERROR_IO_ERROR;
                        } else {
                                TRACE_DEBUG("sock:%d id:%d section:%s property:%s errorcode:%s size:%d",
@@ -2015,8 +2024,21 @@ void *dp_client_request_thread(void *arg)
                                        dp_print_errorcode(ipc_info->errorcode),
                                        ipc_info->size);
 
-                               // JOB
-                               errorcode = __dp_client_requests(slot, ipc_info);
+                               if (client->dbhandle == 0 || dp_db_check_connection(client->dbhandle) < 0) {
+                                   if (dp_db_open_client(&client->dbhandle, slot->pkgname, &errorcode) < 0) {
+                                       TRACE_ERROR("failed to open database for sock:%d errorcode:%s", client_sock, dp_print_errorcode(errorcode));
+                                       if (dp_ipc_query(client->channel, ipc_info->id,
+                                               ipc_info->section, ipc_info->property, errorcode, 0) < 0) {
+                                           TRACE_ERROR("check ipc sock:%d", client->channel);
+                                        }
+                    }
+                }
+
+              if (errorcode == DP_ERROR_NONE) {
+                    // JOB
+                    errorcode = __dp_client_requests(slot, ipc_info);
+                }
+
                        }
                        free(ipc_info);
 
@@ -2041,15 +2063,16 @@ void *dp_client_request_thread(void *arg)
                                // 1. clear zombie requests. clean requests finished. paused or ready for long time
                                dp_client_clear_requests(slot);
 
-                               int sql_errorcode = DP_ERROR_NONE;
-
-                               // 2. maintain only 1000 rows for each client
-                               if (dp_db_limit_rows(client->dbhandle, DP_TABLE_LOGGING, DP_LOG_DB_LIMIT_ROWS, &sql_errorcode) < 0) {
-                                       TRACE_INFO("limit rows error:%s", dp_print_errorcode(sql_errorcode));
-                               }
-                               // 3. maintain for 48 hours
-                               if (dp_db_limit_time(client->dbhandle, DP_TABLE_LOGGING, DP_CARE_CLIENT_INFO_PERIOD, &sql_errorcode) < 0) {
-                                       TRACE_INFO("limit rows error:%s", dp_print_errorcode(sql_errorcode));
+                               if (client->dbhandle != 0) {
+                                   int sql_errorcode = DP_ERROR_NONE;
+                                   // 2. maintain only 1000 rows for each client
+                                   if (dp_db_limit_rows(client->dbhandle, DP_TABLE_LOGGING, DP_LOG_DB_LIMIT_ROWS, &sql_errorcode) < 0) {
+                                       TRACE_INFO("limit rows error:%s", dp_print_errorcode(sql_errorcode));
+                                   }
+                                   // 3. maintain for 48 hours
+                                   if (dp_db_limit_time(client->dbhandle, DP_TABLE_LOGGING, DP_CARE_CLIENT_INFO_PERIOD, &sql_errorcode) < 0) {
+                                       TRACE_INFO("limit rows error:%s", dp_print_errorcode(sql_errorcode));
+                                   }
                                }
                                // 4. if no requests, exit by itself.
                                if (slot->client.requests == NULL) {
@@ -2068,7 +2091,7 @@ void *dp_client_request_thread(void *arg)
        // if no requests, clear slot after disconnect with client.
        CLIENT_MUTEX_LOCK(&slot->mutex);
 
-       TRACE_INFO("thread done slot %p thread:%0x sock:%d", slot, slot->thread, client_sock);
+       TRACE_INFO("thread done slot %p thread:%0x", slot, slot->thread);
 
        slot->thread = 0;// to prevent kill thread twice
 
index d05eabd..dfc70c4 100755 (executable)
@@ -118,33 +118,41 @@ static int __rebuild_client_manager_tables(sqlite3 *handle)
        return 0;
 }
 
-static int __db_open(sqlite3 **handle, char *database)
+static int __db_open(sqlite3 **handle, char *database, int *errorcode)
 {
-       if (sqlite3_open_v2(database, handle, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK) {
-               int errorcode = sqlite3_errcode(*handle);
-               TRACE_ERROR("error(%d):%s", errorcode, sqlite3_errmsg(*handle));
-               *handle = 0;
-               if (errorcode == SQLITE_CORRUPT) { // remove & re-create
-                       TRACE_SECURE_INFO("unlink [%s]", database);
-                       unlink(database);
-                       errorcode = SQLITE_CANTOPEN;
-               }
-               if (errorcode == SQLITE_CANTOPEN) {
-                       // create empty database
-                       if (sqlite3_open(database, handle) != SQLITE_OK ) {
-                               TRACE_SECURE_INFO("failed to connect:%s", database);
-                               unlink(database);
-                               *handle = 0;
-                               return -1;
-                       }
-               } else {
-                       TRACE_ERROR("can not handle this error:%d", errorcode);
-                       *handle = 0;
-                       return -1;
-               }
-       }
-       __basic_property(*handle);
-       return 0;
+    if (sqlite3_open_v2(database, handle, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK) {
+        int sqlerrcode = sqlite3_errcode(*handle);
+        TRACE_ERROR("database open error(%d):%s", sqlerrcode, sqlite3_errmsg(*handle));
+        if (sqlerrcode == SQLITE_CORRUPT) { // remove & re-create
+            TRACE_SECURE_INFO("unlink [%s]", database);
+            unlink(database);
+            sqlerrcode = SQLITE_CANTOPEN;
+        }
+        if (sqlerrcode == SQLITE_CANTOPEN) {
+            *handle = 0;
+            // create empty database
+            if (sqlite3_open(database, handle) != SQLITE_OK ) {
+                sqlerrcode = sqlite3_errcode(*handle);
+                TRACE_SECURE_ERROR("failed to create %s error:%d", database, sqlerrcode);
+                TRACE_ERROR("database new error(%d):%s", sqlerrcode, sqlite3_errmsg(*handle));
+                if (sqlerrcode == SQLITE_FULL || sqlerrcode == SQLITE_CANTOPEN) { // can not create temporary file for connection
+                    *errorcode = DP_ERROR_NO_SPACE; // DP_ERROR_OUT_OF_MEMORY
+                } else {
+                    *errorcode = DP_ERROR_DISK_BUSY;
+                    unlink(database);
+                }
+                *handle = 0;
+                return -1;
+            }
+        } else {
+            TRACE_ERROR("can not handle this error(%d):%s", sqlerrcode, sqlite3_errmsg(*handle));
+            *errorcode = DP_ERROR_OUT_OF_MEMORY;
+            *handle = 0;
+            return -1;
+        }
+    }
+    __basic_property(*handle);
+    return 0;
 }
 
 int dp_db_check_connection(void *handle)
@@ -163,27 +171,29 @@ int dp_db_check_connection(void *handle)
        return 0;
 }
 
-int dp_db_open_client_manager(void **handle)
+int dp_db_open_client_manager(void **handle, int *errorcode)
 {
-       if (*handle == 0) {
-               char *database = sqlite3_mprintf("%s/%s", DATABASE_DIR, DP_DBFILE_CLIENTS);
-               if (database == NULL) {
-                       TRACE_ERROR("failed to make clients database file path");
-                       return -1;
-               }
-               if (__db_open((sqlite3 **)handle, database) < 0) {
-                       TRACE_ERROR("failed to open %s", database);
-                       *handle = 0;
-               } else {
-                       // whenever open new handle, check all tables. it's simple
-                       if (__rebuild_client_manager_tables(*handle) < 0) {
-                               dp_db_close(*handle);
-                               *handle = 0;
-                       }
-               }
-               sqlite3_free(database);
-       }
-       return *handle ? 0 : -1;
+    if (*handle == 0) {
+        char *database = sqlite3_mprintf("%s/%s", DATABASE_DIR, DP_DBFILE_CLIENTS);
+        if (database == NULL) {
+            TRACE_ERROR("failed to make clients database file path");
+            *errorcode = DP_ERROR_OUT_OF_MEMORY;
+            return -1;
+        }
+        if (__db_open((sqlite3 **)handle, database, errorcode) < 0) {
+            TRACE_ERROR("failed to open clients database file");
+            *handle = 0;
+        } else {
+            // whenever open new handle, check all tables. it's simple
+            if (__rebuild_client_manager_tables(*handle) < 0) {
+                *errorcode = DP_ERROR_NO_SPACE;
+                dp_db_close(*handle);
+                *handle = 0;
+            }
+        }
+        sqlite3_free(database);
+    }
+    return *handle ? 0 : -1;
 }
 
 static char *__dp_db_get_client_db_path(char *pkgname)
@@ -247,27 +257,29 @@ int dp_db_open_client_v2(void **handle, char *pkgname)
        return 0;
 }
 
-int dp_db_open_client(void **handle, char *pkgname)
+int dp_db_open_client(void **handle, char *pkgname, int *errorcode)
 {
-       if (*handle == 0) {
-               char *database = __dp_db_get_client_db_path(pkgname);
-               if (database == NULL) {
-                       TRACE_ERROR("failed to make db file path");
-                       return -1;
-               }
-               if (__db_open((sqlite3 **)handle, database) < 0) {
-                       TRACE_SECURE_ERROR("failed to open %s", database);
-                       *handle = 0;
-               } else {
-                       // whenever open new handle, check all tables. it's simple
-                       if (__rebuild_client_tables(*handle) < 0) {
-                               dp_db_close(*handle);
-                               *handle = 0;
-                       }
-               }
-               sqlite3_free(database);
-       }
-       return *handle ? 0 : -1;
+    if (*handle == 0) {
+        char *database = __dp_db_get_client_db_path(pkgname);
+        if (database == NULL) {
+            TRACE_ERROR("failed to make db file path");
+            *errorcode = DP_ERROR_OUT_OF_MEMORY;
+            return -1;
+        }
+        if (__db_open((sqlite3 **)handle, database, errorcode) < 0) {
+            TRACE_SECURE_ERROR("failed to open %s", database);
+            *handle = 0;
+        } else {
+            // whenever open new handle, check all tables. it's simple
+            if (__rebuild_client_tables(*handle) < 0) {
+                *errorcode = DP_ERROR_NO_SPACE;
+                dp_db_close(*handle);
+                *handle = 0;
+            }
+        }
+        sqlite3_free(database);
+    }
+    return *handle ? 0 : -1;
 }
 
 void dp_db_close(void *handle)
@@ -1320,3 +1332,47 @@ int dp_db_get_http_headers_list(void *handle, int id, char **headers, int *error
        return headers_index;
 }
 
+int dp_db_get_max_download_id(void *handle, const char *table, int *pvalue, int *error)
+{
+    TRACE_DEBUG("");
+    int errorcode = SQLITE_OK;
+    sqlite3_stmt *stmt = NULL;
+    *error = DP_ERROR_NONE;
+    DP_DB_PARAM_NULL_CHECK;
+
+    char *query = sqlite3_mprintf("SELECT MAX(%s) FROM %s", DP_DB_COL_ID, table);
+    DP_DB_BUFFER_NULL_CHECK(query);
+    //TRACE_DEBUG("debug query:%s", query);
+    errorcode = sqlite3_prepare_v2(handle, query, -1, &stmt, NULL);
+    DP_DB_BASIC_EXCEPTION_CHECK;
+
+    errorcode = sqlite3_step(stmt);
+    if (errorcode == SQLITE_ROW) {
+        int data_type = sqlite3_column_type(stmt, 0);
+        if (data_type == SQLITE_INTEGER) {
+            int recv_int = sqlite3_column_int(stmt, 0);
+            *pvalue = recv_int;
+        } else if (data_type == SQLITE_NULL) {
+            /* table has no any entry */
+            TRACE_DEBUG("table has no entry");
+            *error = DP_ERROR_NO_DATA;
+        } else {
+            TRACE_ERROR("check column type:%d", data_type);
+            *error = DP_ERROR_INVALID_PARAMETER;
+        }
+    } else if (errorcode == SQLITE_DONE) {
+        TRACE_DEBUG("no data");
+        *error = DP_ERROR_NO_DATA;
+    } else {
+        if ((*error = dp_db_get_errorcode(handle)) == DP_ERROR_NONE) {
+            TRACE_ERROR("ERROR :: unknown");
+            *error = DP_ERROR_DISK_BUSY;
+        }
+    }
+    __dp_finalize(stmt);
+    if (*error != DP_ERROR_NONE)
+        return -1;
+    return 0;
+}
+
+
index e79a6f7..0cb2b84 100644 (file)
@@ -28,19 +28,19 @@ int dp_ipc_check_stderr(int basecode)
 {
        int errorcode = basecode;
        if (errno == EPIPE) {
-               TRACE_STRERROR("[EPIPE:%d] Broken Pipe", errno);
+               TRACE_ERROR("[EPIPE:%d] Broken Pipe", errno);
                errorcode = DP_ERROR_IO_ERROR;
        } else if (errno == EAGAIN) {
-               TRACE_STRERROR("[EAGAIN:%d]", errno);
+               TRACE_ERROR("[EAGAIN:%d]", errno);
                errorcode = DP_ERROR_IO_EAGAIN;
        } else if (errno == EINTR) {
-               TRACE_STRERROR("[EINTR:%d]", errno);
+               TRACE_ERROR("[EINTR:%d]", errno);
                errorcode = DP_ERROR_IO_EINTR;
        } else if (errno == ENOENT) {
-               TRACE_STRERROR("[ENOENT:%d]", errno);
+               TRACE_ERROR("[ENOENT:%d]", errno);
                errorcode = DP_ERROR_IO_ERROR;
        } else {
-               TRACE_STRERROR("[errno:%d]", errno);
+               TRACE_ERROR("[errno:%d]", errno);
        }
        return errorcode;
 }
@@ -54,7 +54,7 @@ int dp_ipc_write(int sock, void *value, size_t type_size)
                TRACE_ERROR("[ERROR] check buffer sock:%d", sock);
                return -1;
        } else if (write(sock, value, type_size) <= 0) {
-               TRACE_STRERROR("[IPC.Write] exception sock:%d", sock);
+               TRACE_ERROR("[IPC.Write] exception sock:%d", sock);
                return -1;
        }
        return 0;
@@ -101,7 +101,7 @@ dp_ipc_fmt *dp_ipc_get_fmt(int sock)
        memset(ipc_info, 0x00, sizeof(dp_ipc_fmt));
        ssize_t recv_size = read(sock, ipc_info, sizeof(dp_ipc_fmt));
        if (recv_size <= 0 || recv_size != sizeof(dp_ipc_fmt)) {
-               TRACE_STRERROR("socket read ipcinfo read size:%d", recv_size);
+               TRACE_ERROR("socket read ipcinfo read size:%d", recv_size);
                free(ipc_info);
                return NULL;
        }
index ded45db..10a13cc 100755 (executable)
 
 #include <systemd/sd-daemon.h>
 #include <glib-object.h>
+#include <curl/curl.h>
 
 #include "download-provider-config.h"
 #include "download-provider-log.h"
 #include "download-provider-client-manager.h"
 #include "download-provider-network.h"
 
+pthread_t g_client_manager_tid;
 void *dp_client_manager(void *arg);
 
 int main(int argc, char **argv)
 {
        GMainLoop *event_loop;
-       pthread_t tid;
        TRACE_INFO("download-provider's working is started");
 
        g_type_init();
@@ -47,17 +48,22 @@ int main(int argc, char **argv)
                TRACE_ERROR("failed to init network-manager");
                return 0;
        }
-       // create a thread for main thread
-       if (pthread_create(&tid, NULL, dp_client_manager, (void *)event_loop) != 0) {
-               TRACE_ERROR("failed to create main thread");
-               return 0;
-       } else {
-               pthread_detach(tid);
-               TRACE_INFO("download main thread is created[%lu]", tid);
-       }
+    //Requested By platform team, It should be in main thread.
+    curl_global_init(CURL_GLOBAL_ALL);
+
+    // create a thread for main thread
+    if (pthread_create(&g_client_manager_tid, NULL, dp_client_manager, (void *)event_loop) != 0) {
+        TRACE_ERROR("failed to create main thread");
+        curl_global_cleanup();
+        return 0;
+    } else {
+        pthread_detach(g_client_manager_tid);
+        TRACE_INFO("download main thread is created[%lu]", g_client_manager_tid);
+    }
 
        TRACE_INFO("g main loop is started");
        g_main_loop_run(event_loop);
+       curl_global_cleanup();
        dp_network_connection_destroy();
        g_main_loop_unref(event_loop);
 
index c076611..d4f8b8f 100644 (file)
@@ -515,7 +515,7 @@ static int __dp_notification_manager_start()
                TRACE_DEBUG("try to create notification-manager");
                if (pthread_create(&g_dp_notification_manager_tid, NULL,
                                __dp_notification_manager, NULL) != 0) {
-                       TRACE_STRERROR("failed to create notification-manager");
+                       TRACE_ERROR("failed to create notification-manager");
                        return -1;
                }
        }
index 533ac2c..6539dac 100755 (executable)
@@ -182,6 +182,8 @@ char *__dp_noti_get_sender(char *url)
                snprintf(sender, len + 1, "%s", temp);
        } else {
                sender = dp_strdup(temp);
+        if (sender == NULL)
+            return NULL;
        }
 
        // For credential URL
index 3288fde..c40245c 100644 (file)
@@ -30,11 +30,11 @@ static char *__dp_notify_get_path(pid_t pid)
        size_t path_size = sizeof(NOTIFY_DIR) + 21;
        char *notify_fifo = (char *)calloc(path_size, sizeof(char));
        if (notify_fifo == NULL) {
-               TRACE_STRERROR("failed to alocalte fifo path pid:%d", (int)pid);
+               TRACE_ERROR("failed to alocalte fifo path pid:%d", (int)pid);
                return NULL;
        }
        if (snprintf(notify_fifo, path_size,"%s/%d", NOTIFY_DIR, pid) < 0) {
-               TRACE_STRERROR("failed to make fifo path pid:%d", (int)pid);
+               TRACE_ERROR("failed to make fifo path pid:%d", (int)pid);
                free(notify_fifo);
                return NULL;
        }
@@ -51,7 +51,7 @@ int dp_notify_init(pid_t pid)
        if (stat(notify_fifo, &fifo_state) == 0) // found
                unlink(notify_fifo);
        if (mkfifo(notify_fifo, 0644/*-rwrr*/) < 0) {
-               TRACE_STRERROR("failed to make fifo %s", notify_fifo);
+               TRACE_ERROR("failed to make fifo %s", notify_fifo);
        } else {
                notify_fd = open(notify_fifo, O_RDWR | O_NONBLOCK, 0644);
        }
index 953cbe0..abba454 100755 (executable)
@@ -219,16 +219,16 @@ static int __set_file_permission_to_client(dp_client_slots_fmt *slot, dp_request
                                        if ((fchown(fd, cred.uid, cred.gid) != 0) ||
                                                (fchmod(fd, S_IRUSR | S_IWUSR |
                                                        S_IRGRP | S_IROTH) != 0)) {
-                                               TRACE_STRERROR("[ERROR][%d] permission user:%d group:%d",
+                                               TRACE_ERROR("[ERROR][%d] permission user:%d group:%d",
                                                        request->id, cred.uid, cred.gid);
                                                errorcode = DP_ERROR_PERMISSION_DENIED;
                                        }
                                } else {
-                                       TRACE_STRERROR("fstat & lstat info have not matched");
+                                       TRACE_ERROR("fstat & lstat info have not matched");
                                        errorcode = DP_ERROR_PERMISSION_DENIED;
                                }
                        } else {
-                               TRACE_STRERROR("fstat call failed");
+                               TRACE_ERROR("fstat call failed");
                                errorcode = DP_ERROR_PERMISSION_DENIED;
                        }
                        close(fd);
@@ -237,7 +237,7 @@ static int __set_file_permission_to_client(dp_client_slots_fmt *slot, dp_request
                        errorcode = DP_ERROR_IO_ERROR;
                }
        } else {
-               TRACE_STRERROR("lstat call failed");
+               TRACE_ERROR("lstat call failed");
                errorcode = DP_ERROR_PERMISSION_DENIED;
        }
        if (errorcode == DP_ERROR_NONE && dp_smack_is_mounted() == 1) {
@@ -254,7 +254,7 @@ static int __set_file_permission_to_client(dp_client_slots_fmt *slot, dp_request
                                errorcode = dp_smack_set_label(smack_label, dir_path, saved_path);
                                free(dir_path);
                        } else {
-                               TRACE_STRERROR("[ERROR] calloc");
+                               TRACE_ERROR("[ERROR] calloc");
                                errorcode = DP_ERROR_OUT_OF_MEMORY;
                        }
                        free(smack_label);
@@ -925,7 +925,7 @@ int dp_start_agent_download(void *slot, void *request)
                                base_req->id, tmp_saved_path);
                        if (dp_is_file_exist(tmp_saved_path) == 0) {
                                if (unlink(tmp_saved_path) != 0)
-                                       TRACE_STRERROR("failed to remove file id:%d", base_req->id);
+                                       TRACE_ERROR("failed to remove file id:%d", base_req->id);
                        }
                }
        }
index 3ef2816..9d60077 100644 (file)
@@ -45,7 +45,7 @@ int dp_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
        if (0 == ret || EBUSY == ret)
                return 0;
        else
-               TRACE_STRERROR("error:%d.%s", ret, __print_pthread_error(ret));
+               TRACE_ERROR("error:%d.%s", ret, __print_pthread_error(ret));
        return -1;
 }
 
@@ -53,7 +53,7 @@ void dp_mutex_lock(pthread_mutex_t *mutex, const char *func, int line)
 {
        int ret = pthread_mutex_lock(mutex);
        if (ret != 0)
-               TRACE_STRERROR("%s:%d error:%d.%s", func, line, ret,
+               TRACE_ERROR("%s:%d error:%d.%s", func, line, ret,
                        __print_pthread_error(ret));
 }
 
@@ -61,7 +61,7 @@ int dp_mutex_check_lock(pthread_mutex_t *mutex, const char *func, int line)
 {
        int ret = pthread_mutex_lock(mutex);
        if (ret != 0)
-               TRACE_STRERROR("%s:%d error:%d.%s", func, line, ret,
+               TRACE_ERROR("%s:%d error:%d.%s", func, line, ret,
                        __print_pthread_error(ret));
        return ret;
 }
@@ -70,7 +70,7 @@ int dp_mutex_trylock(pthread_mutex_t *mutex, const char *func, int line)
 {
        int ret = pthread_mutex_trylock(mutex);
        if (ret != 0 && ret != EINVAL) {
-               TRACE_STRERROR("%s:%d error:%d.%s", func, line, ret,
+               TRACE_ERROR("%s:%d error:%d.%s", func, line, ret,
                        __print_pthread_error(ret));
        }
        return ret;
@@ -83,7 +83,7 @@ int dp_mutex_timedlock(pthread_mutex_t *mutex, int sec, const char *func, int li
        deltatime.tv_nsec = 0;
        int ret = pthread_mutex_timedlock(mutex, &deltatime);
        if (ret != 0) {
-               TRACE_STRERROR("%s:%d error:%d.%s", func, line, ret,
+               TRACE_ERROR("%s:%d error:%d.%s", func, line, ret,
                        __print_pthread_error(ret));
        }
        return ret;
@@ -93,7 +93,7 @@ void dp_mutex_unlock(pthread_mutex_t *mutex, const char *func, int line)
 {
        int ret = pthread_mutex_unlock(mutex);
        if (ret != 0)
-               TRACE_STRERROR("%s:%d error:%d.%s", func, line, ret,
+               TRACE_ERROR("%s:%d error:%d.%s", func, line, ret,
                        __print_pthread_error(ret));
 }
 
@@ -101,7 +101,7 @@ void dp_mutex_destroy(pthread_mutex_t *mutex)
 {
        int ret = pthread_mutex_destroy(mutex);
        if (ret != 0) {
-               TRACE_STRERROR("error:%d.%s", ret, __print_pthread_error(ret));
+               TRACE_ERROR("error:%d.%s", ret, __print_pthread_error(ret));
                if(EBUSY == ret) {
                        if (pthread_mutex_unlock(mutex) == 0)
                                pthread_mutex_destroy(mutex);
index 4094b02..e01b7ea 100644 (file)
@@ -278,7 +278,7 @@ static int __dp_queue_manager_start()
                TRACE_DEBUG("try to create queue-manager");
                if (pthread_create(&g_dp_queue_manager_tid, NULL,
                                __dp_queue_manager, NULL) != 0) {
-                       TRACE_STRERROR("failed to create queue-manager");
+                       TRACE_ERROR("failed to create queue-manager");
                        return -1;
                }
        }
index 5e89c45..216822e 100644 (file)
@@ -159,7 +159,7 @@ void dp_rebuild_dir(const char *dirpath, mode_t mode)
                                TRACE_SECURE_ERROR("failed to set smack label:%s", dirpath);
                        }
                } else {
-                       TRACE_STRERROR("failed to create directory:%s", dirpath);
+                       TRACE_ERROR("failed to create directory:%s", dirpath);
                }
        }
 }
index a1fb736..d29b51f 100644 (file)
@@ -45,7 +45,7 @@ char *dp_strdup(char *src)
 
        dest = (char *)calloc(src_len + 1, sizeof(char));
        if (dest == NULL) {
-               TRACE_STRERROR("[CHECK] allocation");
+               TRACE_ERROR("[CHECK] allocation");
                return NULL;
        }
        memcpy(dest, src, src_len * sizeof(char));
@@ -94,7 +94,7 @@ int dp_remove_file(const char *file_path)
        if ((file_path != NULL && strlen(file_path) > 0) &&
                        dp_is_file_exist(file_path) == 0) {
                if (unlink(file_path) != 0) {
-                       TRACE_STRERROR("failed to remove file");
+                       TRACE_ERROR("failed to remove file");
                        return -1;
                }
                return 0;
index 6f3a938..bb0ce00 100755 (executable)
@@ -18,8 +18,8 @@
 #define DOWNLOAD_PROVIDER_DB_H
 
 int dp_db_check_connection(void *handle);
-int dp_db_open_client_manager(void **handle);
-int dp_db_open_client(void **handle, char *pkgname);
+int dp_db_open_client_manager(void **handle, int *errorcode);
+int dp_db_open_client(void **handle, char *pkgname, int *errorcode);
 int dp_db_open_client_v2(void **handle, char *pkgname);
 int dp_db_remove_database(char *pkgname, long now_time, long diff_time);
 int dp_db_get_ids(void *handle, const char *table, char *idcolumn, int *ids, const char *where, const int limit, char *ordercolumn, char *ordering, int *error);
@@ -51,5 +51,6 @@ int dp_db_get_cond_string(void *handle, const char *table, char *wherecolumn, co
 int dp_db_limit_rows(void *handle, const char *table, int limit, int *error);
 int dp_db_limit_time(void *handle, const char *table, int hours, int *error);
 int dp_db_get_http_headers_list(void *handle, int id, char **headers, int *error);
+int dp_db_get_max_download_id(void *handle, const char *table, int *pvalue, int *error);
 
 #endif
index 5d04e3e..cca3914 100755 (executable)
@@ -53,7 +53,7 @@
 #else
 #define TRACE_DEBUG(...) do { } while(0)
 #define TRACE_ERROR(...) do { } while(0)
-#define TRACE_STRERROR(...) do { } while(0)
+#define TRACE_ERROR(...) do { } while(0)
 #define TRACE_INFO(...) do { } while(0)
 #define TRACE_WARN(...) do { } while(0)
 #define TRACE_SECURE_DEBUG(...) do { } while(0)
index 06cfefa..29d7874 100755 (executable)
@@ -98,7 +98,8 @@ typedef enum {
 
 // string to check invalid characters in path before using open() and fopen() API's
 #define DP_INVALID_PATH_STRING ";\\\":*?<>|()"
-
+// download provider first download id.
+#define DP_FIRST_DOWNLOAD_ID 1
 
 #include <unistd.h>