return errorcode;
}
-static int __dp_client_new(int clientfd, dp_client_slots_fmt *clients,
- dp_credential credential)
+static bool __dp_client_run_with_same_client(dp_client_slots_fmt *clients, int clientfd,
+ const char *pkgname, dp_credential credential, int *errorcode)
{
- // search in clients list.
- // if same pkgname. update it.
- // search same pkg or pid in clients
- int errorcode = DP_ERROR_NONE;
- int i = 0;
- int pkg_len = 0;
- char *pkgname = NULL;
-
- char buffer[256] = { 0, };
-
- // getting the package name via pid
- if (aul_app_get_appid_bypid_for_uid(credential.pid, buffer, sizeof(buffer), credential.uid) != AUL_R_OK)
- TRACE_ERROR("[CRITICAL] aul_app_get_appid_bypid_for_uid");
-
-#ifdef TIZEN_FEATURE_UNITTEST
- pkgname = strdup("download-provider");
-#else
- pkgname = strdup(buffer);
-#endif
- if (!pkgname || (pkg_len = strlen(pkgname)) <= 0) {
- TRACE_ERROR("[CRITICAL] pkgname:%s", pkgname);
- free(pkgname);
- return DP_ERROR_INVALID_PARAMETER;
- }
-
- errorcode = dp_check_permission(clientfd, pkgname);
- if (errorcode != DP_ERROR_NONE) {
- TRACE_ERROR("permission denied");
- free(pkgname);
- return errorcode;
- }
-
- // EINVAL: empty slot
- // EBUSY : occupied slot
- // locked & thread == 0 : downloading without client <= check target
- // thread == 0, requests == NULL : clear target
-
- // Have this client ever been connected before ?
- for (i = 0; i < DP_MAX_CLIENTS; i++) {
+ for (int i = 0; i < DP_MAX_CLIENTS; i++) {
int locked = CLIENT_MUTEX_TRYLOCK(&clients[i].mutex);
if (locked != 0) // empty or used by other thread. it would be same client, but it's busy
continue;
if (clients[i].pkgname != NULL) {
// check package name.
TRACE_DEBUG("check client[%s] slot:%d", clients[i].pkgname, i);
- int cname_len = strlen(clients[i].pkgname);
- if (pkg_len == cname_len &&
- strncmp(clients[i].pkgname, pkgname, pkg_len) == 0) {
+ if (strcmp(clients[i].pkgname, pkgname) == 0) {
TRACE_SECURE_INFO("update client[%s] slot:%d pid:%d sock:%d",
pkgname, i, credential.pid, clientfd);
if (clients[i].client.channel >= 0 &&
}
dp_notify_deinit(clients[i].credential.pid);
}
- errorcode = __dp_client_run(clientfd, &clients[i], credential);
+ *errorcode = __dp_client_run(clientfd, &clients[i], credential);
CLIENT_MUTEX_UNLOCK(&clients[i].mutex);
- if (errorcode != DP_ERROR_NONE)
+ if (*errorcode != DP_ERROR_NONE)
dp_mutex_destroy(&clients[i].mutex);
- free(pkgname);
- return errorcode;
+ return true;
}
}
if (clients[i].client.requests == NULL) { // clear
}
CLIENT_MUTEX_UNLOCK(&clients[i].mutex);
}
+ return false;
+}
- TRACE_DEBUG("search empty client[%s] slot:%d", pkgname, i);
+static int __dp_client_find_empty_slot(dp_client_slots_fmt *clients)
+{
// search empty slot
- for (i = 0; i < DP_MAX_CLIENTS; i++) {
+ for (int i = 0; i < DP_MAX_CLIENTS; i++) {
int locked = CLIENT_MUTEX_TRYLOCK(&clients[i].mutex);
if (locked == EINVAL) {
- if (dp_mutex_init(&clients[i].mutex, NULL) == 0) {
- CLIENT_MUTEX_LOCK(&clients[i].mutex);
- TRACE_DEBUG("found empty client[%s] slot:%d", pkgname, i);
- clients[i].pkgname = pkgname;
- clients[i].client.dbhandle = 0;
- clients[i].client.requests = NULL;
- errorcode = __dp_client_run(clientfd, &clients[i], credential);
- CLIENT_MUTEX_UNLOCK(&clients[i].mutex);
- if (errorcode != DP_ERROR_NONE)
- dp_mutex_destroy(&clients[i].mutex);
- return errorcode;
- }
+ if (dp_mutex_init(&clients[i].mutex, NULL) == 0)
+ return i;
}
if (locked == 0)
CLIENT_MUTEX_UNLOCK(&clients[i].mutex);
}
+ return -1;
+}
+
+static int __dp_client_run_in_empty_slot(dp_client_slots_fmt *client,
+ int clientfd, dp_credential credential, char *pkgname)
+{
+ int errorcode = DP_ERROR_NONE;
+ CLIENT_MUTEX_LOCK(&client->mutex);
+ client->pkgname = pkgname;
+ client->client.dbhandle = 0;
+ client->client.requests = NULL;
+ errorcode = __dp_client_run(clientfd, client, credential);
+ CLIENT_MUTEX_UNLOCK(&client->mutex);
+ if (errorcode != DP_ERROR_NONE)
+ dp_mutex_destroy(&client->mutex);
+ return errorcode;
+}
+
+static int __dp_client_new(int clientfd, dp_client_slots_fmt *clients,
+ dp_credential credential)
+{
+ // search in clients list.
+ // if same pkgname. update it.
+ // search same pkg or pid in clients
+ int errorcode = DP_ERROR_NONE;
+ char *pkgname = NULL;
+ char buffer[256] = { 0, };
+
+ // getting the package name via pid
+ if (aul_app_get_appid_bypid_for_uid(credential.pid, buffer, sizeof(buffer), credential.uid) != AUL_R_OK)
+ TRACE_ERROR("[CRITICAL] aul_app_get_appid_bypid_for_uid");
+
+#ifdef TIZEN_FEATURE_UNITTEST
+ pkgname = strdup("download-provider");
+#else
+ pkgname = strdup(buffer);
+#endif
+ if (!pkgname || strlen(pkgname) <= 0) {
+ TRACE_ERROR("[CRITICAL] pkgname:%s", pkgname);
+ free(pkgname);
+ return DP_ERROR_INVALID_PARAMETER;
+ }
+
+ errorcode = dp_check_permission(clientfd, pkgname);
+ if (errorcode != DP_ERROR_NONE) {
+ TRACE_ERROR("permission denied");
+ free(pkgname);
+ return errorcode;
+ }
+
+ // EINVAL: empty slot
+ // EBUSY : occupied slot
+ // locked & thread == 0 : downloading without client <= check target
+ // thread == 0, requests == NULL : clear target
+
+ // Have this client ever been connected before ?
+ if (__dp_client_run_with_same_client(clients, clientfd, pkgname, credential, &errorcode)) {
+ free(pkgname);
+ return errorcode;
+ }
+
+ TRACE_DEBUG("search empty client[%s]", pkgname);
+ int empty_slot = __dp_client_find_empty_slot(clients);
+ if (empty_slot >= 0) {
+ TRACE_DEBUG("found empty client[%s] slot:%d", pkgname, empty_slot);
+ return __dp_client_run_in_empty_slot(&clients[empty_slot], clientfd, credential, pkgname);
+ }
TRACE_SECURE_INFO("busy client[%s] pid:%d sock:%d", pkgname,
credential.pid, clientfd);