From 487d44bcd9e2b70304ebd8121ac4fa8ee9cb1d0e Mon Sep 17 00:00:00 2001 From: Minje Ahn Date: Tue, 10 Sep 2019 15:56:08 +0900 Subject: [PATCH] Improve owner_list Change-Id: I076539b5ad7ad1a7960b0641aa8741ce8c9e09ad Signed-off-by: Minje Ahn --- src/scanner/media-scanner-socket.c | 77 ++--- src/server/include/media-server-socket.h | 4 +- src/server/media-server-socket.c | 573 ++++++++++--------------------- 3 files changed, 204 insertions(+), 450 deletions(-) diff --git a/src/scanner/media-scanner-socket.c b/src/scanner/media-scanner-socket.c index 94ab035..d2b027d 100755 --- a/src/scanner/media-scanner-socket.c +++ b/src/scanner/media-scanner-socket.c @@ -37,27 +37,20 @@ gboolean msc_receive_request(GIOChannel *src, GIOCondition condition, gpointer d { ms_comm_msg_s *recv_msg = NULL; int sockfd = MS_SOCK_NOT_ALLOCATE; - int req_num = MS_MSG_MAX; int err = -1; sockfd = g_io_channel_unix_get_fd(src); - if (sockfd < 0) { - MS_DBG_ERR("sock fd is invalid!"); - return TRUE; - } + MS_DBG_RETVM_IF(sockfd < 0, TRUE, "sock fd is invalid!"); MS_MALLOC(recv_msg, sizeof(ms_comm_msg_s)); - if (recv_msg == NULL) { - MS_DBG_ERR("MS_MALLOC failed"); - return TRUE; - } + MS_DBG_RETVM_IF(!recv_msg, TRUE, "MS_MALLOC failed"); /* read() is blocked until media scanner sends message */ err = read(sockfd, recv_msg, sizeof(ms_comm_msg_s)); if (err < 0) { MS_DBG_STRERROR("fifo read failed"); MS_SAFE_FREE(recv_msg); - return MS_MEDIA_ERR_FILE_READ_FAIL; + return TRUE; } if (strlen(recv_msg->msg) == 0 || strlen(recv_msg->msg) >= MAX_MSG_SIZE) { @@ -74,47 +67,29 @@ gboolean msc_receive_request(GIOChannel *src, GIOCondition condition, gpointer d MS_DBG_SLOG("receive msg from [%d][%d] M[%.*s] S[%.*s]", recv_msg->pid, recv_msg->msg_type, MAX_MSG_SIZE, recv_msg->msg, MS_UUID_SIZE, recv_msg->storage_id); - /* copy from recived data */ - req_num = recv_msg->msg_type; - - switch (req_num) { + switch (recv_msg->msg_type) { case MS_MSG_BULK_INSERT: - { - MS_DBG_INFO("BULK INSERT"); - /* request bulk insert*/ - msc_push_scan_request(MS_SCAN_REGISTER, recv_msg); - } + msc_push_scan_request(MS_SCAN_REGISTER, recv_msg); break; case MS_MSG_DIRECTORY_SCANNING: case MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE: - { - /* this request from another apps */ - /* set the scan data for scanning thread */ - msc_push_scan_request(MS_SCAN_DIRECTORY, recv_msg); - } + msc_push_scan_request(MS_SCAN_DIRECTORY, recv_msg); break; case MS_MSG_STORAGE_ALL: case MS_MSG_STORAGE_PARTIAL: case MS_MSG_STORAGE_INVALID: - { - /* this request from media-server */ - msc_push_scan_request(MS_SCAN_STORAGE, recv_msg); - } + msc_push_scan_request(MS_SCAN_STORAGE, recv_msg); break; case MS_MSG_DIRECTORY_SCANNING_CANCEL: - { - msc_remove_dir_scan_request(recv_msg); - MS_SAFE_FREE(recv_msg); - } + msc_remove_dir_scan_request(recv_msg); + MS_SAFE_FREE(recv_msg); break; case MS_MSG_STORAGE_META: msc_metadata_update_thread(recv_msg); break; default: - { - MS_DBG_ERR("THIS REQUEST IS INVALID %d", req_num); - MS_SAFE_FREE(recv_msg); - } + MS_DBG_ERR("THIS REQUEST IS INVALID %d", recv_msg->msg_type); + MS_SAFE_FREE(recv_msg); break; } @@ -126,45 +101,36 @@ gboolean msc_receive_request(GIOChannel *src, GIOCondition condition, gpointer d int msc_send_ready(void) { - int res = MS_MEDIA_ERR_NONE; + int ret = MS_MEDIA_ERR_NONE; ms_comm_msg_s send_msg; int fd = -1; - int err = -1; fd = open(MS_SCANNER_FIFO_PATH_RES, O_WRONLY); - if (fd < 0) { - MS_DBG_STRERROR("fifo open failed"); - return MS_MEDIA_ERR_FILE_OPEN_FAIL; - } + MS_DBG_RETVM_IF(fd < 0, MS_MEDIA_ERR_FILE_OPEN_FAIL, "fifo open failed"); /* send ready message */ memset(&send_msg, 0, sizeof(send_msg)); send_msg.msg_type = MS_MSG_SCANNER_READY; /* send ready message */ - err = write(fd, &send_msg, sizeof(send_msg)); - if (err < 0) { + if (write(fd, &send_msg, sizeof(send_msg)) < 0) { MS_DBG_STRERROR("fifo write failed"); - res = MS_MEDIA_ERR_FILE_READ_FAIL; + ret = MS_MEDIA_ERR_FILE_READ_FAIL; } close(fd); - return res; + return ret; } int msc_send_result(int result, ms_comm_msg_s *res_data) { - int res = MS_MEDIA_ERR_NONE; + int ret = MS_MEDIA_ERR_NONE; ms_comm_msg_s send_msg; int fd = -1; - int err = -1; fd = open(MS_SCANNER_FIFO_PATH_RES, O_WRONLY); - if (fd < 0) { - MS_DBG_STRERROR("fifo open failed"); - return MS_MEDIA_ERR_FILE_OPEN_FAIL; - } + MS_DBG_RETVM_IF(fd < 0, MS_MEDIA_ERR_FILE_OPEN_FAIL, "fifo open failed"); /* send result message */ memset(&send_msg, 0x0, sizeof(ms_comm_msg_s)); @@ -175,14 +141,13 @@ int msc_send_result(int result, ms_comm_msg_s *res_data) SAFE_STRLCPY(send_msg.msg, res_data->msg, sizeof(send_msg.msg)); /* send ready message */ - err = write(fd, &send_msg, sizeof(send_msg)); - if (err < 0) { + if (write(fd, &send_msg, sizeof(send_msg)) < 0) { MS_DBG_STRERROR("fifo write failed"); - res = MS_MEDIA_ERR_FILE_READ_FAIL; + ret = MS_MEDIA_ERR_FILE_READ_FAIL; } close(fd); - return res; + return ret; } diff --git a/src/server/include/media-server-socket.h b/src/server/include/media-server-socket.h index a26fb11..3d98c66 100755 --- a/src/server/include/media-server-socket.h +++ b/src/server/include/media-server-socket.h @@ -26,12 +26,12 @@ #include "media-common-types.h" #include "media-server-ipc.h" -gboolean ms_read_socket(gpointer user_data); +gboolean ms_read_socket(GIOChannel *src, GIOCondition condition, gpointer user_data); gboolean ms_read_db_tcp_socket(GIOChannel *src, GIOCondition condition, gpointer data); int ms_send_scan_request(ms_comm_msg_s *send_msg, int client_sock); int ms_send_storage_scan_request(const char *root_path, const char *storage_id, ms_dir_scan_type_t scan_type, uid_t uid); gboolean ms_receive_message_from_scanner(GIOChannel *src, GIOCondition condition, gpointer data); -int ms_remove_request_owner(int pid, const char *req_path); +void ms_remove_request_owner(int pid, const char *req_path); int ms_send_storage_otg_scan_request(const char *path, const char *device_uuid, ms_dir_scan_type_t scan_type, uid_t uid); int ms_reset_ownerlist(); #ifdef _USE_SENIOR_MODE diff --git a/src/server/media-server-socket.c b/src/server/media-server-socket.c index b0fde54..ce0789b 100755 --- a/src/server/media-server-socket.c +++ b/src/server/media-server-socket.c @@ -44,123 +44,90 @@ extern bool power_off; typedef struct ms_req_owner_data { int pid; - int index; int client_sockfd; char *req_path; } ms_req_owner_data; -static int __ms_add_owner(int pid, int client_sock, char *path) +static void __destroy_owner_data(gpointer data) { - if (pid != 0) { - ms_req_owner_data *owner_data = NULL; - int len = strlen(path); - - if (len > 0) { - /* If owner list is NULL, create it */ - /* pid and client address are stored in ower list */ - /* These are used for sending result of scanning */ - if (owner_list == NULL) { - /*create array for processing overlay data*/ - owner_list = g_array_new(FALSE, FALSE, sizeof(ms_req_owner_data *)); - if (owner_list == NULL) { - MS_DBG_ERR("g_array_new error"); - return MS_MEDIA_ERR_OUT_OF_MEMORY; - } - } + ms_req_owner_data **_data = (ms_req_owner_data **) data; - /* store pid and client address */ - MS_MALLOC(owner_data, sizeof(ms_req_owner_data)); + close((*_data)->client_sockfd); + MS_SAFE_FREE((*_data)->req_path); + MS_SAFE_FREE(*_data); + MS_DBG("DELETE OWNER"); +} - if (owner_data == NULL) { - MS_DBG_ERR("MS_MALLOC failed"); - g_array_free(owner_list, FALSE); - owner_list = NULL; - return MS_MEDIA_ERR_OUT_OF_MEMORY; - } +static int __ms_add_owner(int pid, int client_sock, char *path) +{ + ms_req_owner_data *owner_data = NULL; - owner_data->pid = pid; - owner_data->client_sockfd = client_sock; + MS_DBG_RETV_IF(pid == 0, MS_MEDIA_ERR_NONE); - if (path[len -1] == '/') - path[len -1] = '\0'; - owner_data->req_path = strdup(path); - owner_data->index = -1; - g_array_append_val(owner_list, owner_data); - } + int len = strlen(path); + MS_DBG_RETV_IF(len == 0, MS_MEDIA_ERR_NONE); + + if (!owner_list) { + owner_list = g_array_new(FALSE, FALSE, sizeof(ms_req_owner_data)); + MS_DBG_RETVM_IF(!owner_list, MS_MEDIA_ERR_OUT_OF_MEMORY, "g_array_new failed"); + g_array_set_clear_func(owner_list, __destroy_owner_data); } + /* store pid and client address */ + MS_MALLOC(owner_data, sizeof(ms_req_owner_data)); + MS_DBG_RETVM_IF(!owner_data, MS_MEDIA_ERR_OUT_OF_MEMORY, "MS_MALLOC failed"); + + owner_data->pid = pid; + owner_data->client_sockfd = client_sock; + + if (path[len - 1] == '/') + path[len - 1] = '\0'; + owner_data->req_path = g_strdup(path); + + g_array_append_val(owner_list, owner_data); + return MS_MEDIA_ERR_NONE; } -static int __ms_find_owner(int pid, const char *req_path, ms_req_owner_data **owner_data) +static void __ms_delete_owner(int pid, const char *req_path) { int i; - int len; ms_req_owner_data *data = NULL; - *owner_data = NULL; - - if (owner_list != NULL) { - len = owner_list->len; - MS_DBG("length list : %d", len); - - for (i = 0; i < len; i++) { - data = g_array_index(owner_list, ms_req_owner_data*, i); - MS_DBG("%d %d", data->pid, pid); - MS_DBG("%s %s", data->req_path, req_path); - if (data->pid == pid && (strcmp(data->req_path, req_path) == 0)) { - data->index = i; - *owner_data = data; - MS_DBG("FIND OWNER"); - break; - } - } - } else { - MS_DBG_ERR("owner list is NULL"); - } - - return MS_MEDIA_ERR_NONE; -} + if (!owner_list) + return; -static int __ms_delete_owner(ms_req_owner_data *owner_data) -{ - if (owner_data->index != -1) { - g_array_remove_index(owner_list, owner_data->index); - MS_SAFE_FREE(owner_data->req_path); - MS_SAFE_FREE(owner_data); - MS_DBG("DELETE OWNER"); + for (i = 0; i < owner_list->len; i++) { + data = g_array_index(owner_list, ms_req_owner_data*, i); + if (data->pid == pid && (strcmp(data->req_path, req_path) == 0)) { + MS_DBG("OWNER FOUND"); + g_array_remove_index(owner_list, i); + break; + } } - - return MS_MEDIA_ERR_NONE; } int ms_reset_ownerlist() { - int i; - int len = 0; ms_req_owner_data *data = NULL; ms_comm_msg_s send_msg = {0,}; - if (owner_list != NULL) { - len = owner_list->len; - MS_DBG("length list : %d", len); - - for (i = 0; i < len; i++) { - data = g_array_index(owner_list, ms_req_owner_data*, 0); - g_array_remove_index(owner_list, 0); - MS_DBG("%d", data->pid); - MS_DBG("%s", data->req_path); - send_msg.pid = data->pid; - send_msg.result = MS_MEDIA_ERR_DB_RESET; - send_msg.msg_type = MS_MSG_MEDIA_DB_RESET; - SAFE_STRLCPY(send_msg.msg, data->req_path, sizeof(send_msg.msg)); - - /* owner data exists */ - /* send result to the owner of request */ - ms_ipc_send_msg_to_client_tcp(data->client_sockfd, &send_msg, NULL); - - close(data->client_sockfd); - } + if (!owner_list) + return MS_MEDIA_ERR_NONE; + + while (owner_list->len != 0) { + data = g_array_index(owner_list, ms_req_owner_data*, 0); + MS_DBG("%d", data->pid); + MS_DBG("%s", data->req_path); + send_msg.pid = data->pid; + send_msg.result = MS_MEDIA_ERR_DB_RESET; + send_msg.msg_type = MS_MSG_MEDIA_DB_RESET; + SAFE_STRLCPY(send_msg.msg, data->req_path, sizeof(send_msg.msg)); + + /* owner data exists */ + /* send result to the owner of request */ + ms_ipc_send_msg_to_client_tcp(data->client_sockfd, &send_msg, NULL); + g_array_remove_index(owner_list, 0); } return MS_MEDIA_ERR_NONE; @@ -168,69 +135,25 @@ int ms_reset_ownerlist() static int __ms_send_result_to_client(int pid, ms_comm_msg_s *recv_msg) { - if (recv_msg == NULL) { - MS_DBG_ERR("recv_msg is NULL"); - return MS_MEDIA_ERR_INVALID_PARAMETER; - } - - if (!MS_STRING_VALID(recv_msg->msg)) { - MS_DBG_ERR("msg is NULL"); - return MS_MEDIA_ERR_INVALID_PARAMETER; - } + int i = 0; + ms_req_owner_data *data = NULL; - if (owner_list != NULL) { - /* If the owner of result message is not media-server, media-server notify to the owner */ - /* The owner of message is distingushied by pid in received message*/ - /* find owner data */ - ms_req_owner_data *owner_data = NULL; - char *res_path = strdup(recv_msg->msg); - if (res_path == NULL) { - MS_DBG_ERR("res_path is NULL"); - return MS_MEDIA_ERR_OUT_OF_MEMORY; - } - if (recv_msg->msg_type == MS_MSG_SCANNER_PARTIAL) { - ms_req_owner_data *data = NULL; - int len = owner_list->len; - MS_DBG_ERR("length list : %d", len); - for (int i = 0; i < len; i++) { - data = g_array_index(owner_list, ms_req_owner_data*, i); - /*send partial message to all request pid, need to check path*/ - if (strcmp(data->req_path, res_path) == 0) { - MS_DBG_ERR("PARTIAL message:pid %d,path %s", data->pid, data->req_path); - ms_ipc_send_msg_to_client_tcp(data->client_sockfd, recv_msg, NULL); - } - } - MS_SAFE_FREE(res_path); - } else { - __ms_find_owner(pid, res_path, &owner_data); - if (owner_data != NULL) { - /* owner data exists */ - /* send result to the owner of request */ - ms_ipc_send_msg_to_client_tcp(owner_data->client_sockfd, recv_msg, NULL); - MS_DBG_ERR("OTHER message: pid %d, path %s", owner_data->pid, owner_data->req_path); - if ((recv_msg->msg_type != MS_MSG_SCANNER_COMPLETE) && (recv_msg->msg_type != MS_MSG_SCANNER_PARTIAL)) { - close(owner_data->client_sockfd); - } + MS_DBG_RETVM_IF(!recv_msg, MS_MEDIA_ERR_INVALID_PARAMETER, "recv_msg is NULL"); + MS_DBG_RETVM_IF(!MS_STRING_VALID(recv_msg->msg), MS_MEDIA_ERR_INVALID_PARAMETER, "msg is NULL"); + MS_DBG_RETVM_IF(!owner_list, MS_MEDIA_ERR_INTERNAL, "owner_list is NULL"); - MS_SAFE_FREE(res_path); + for (i = 0; i < owner_list->len; i++) { + data = g_array_index(owner_list, ms_req_owner_data*, i); + if (data->pid == pid && strcmp(data->req_path, recv_msg->msg) == 0) { + ms_ipc_send_msg_to_client_tcp(data->client_sockfd, recv_msg, NULL); + if (recv_msg->msg_type != MS_MSG_SCANNER_PARTIAL) + g_array_remove_index(owner_list, i); - if ((recv_msg->msg_type != MS_MSG_SCANNER_COMPLETE) && (recv_msg->msg_type != MS_MSG_SCANNER_PARTIAL)) - __ms_delete_owner(owner_data); - } else { - MS_DBG_ERR("Not found Owner"); - MS_SAFE_FREE(res_path); - return MS_MEDIA_ERR_INTERNAL; - } + break; } - } else { - /* owner data does not exist*/ - /* this is result of request of media server*/ - MS_DBG_ERR("There is no request, Owner list is NULL"); - return MS_MEDIA_ERR_INTERNAL; } return MS_MEDIA_ERR_NONE; - } static int __ms_recovery_media_db(uid_t uid) @@ -276,80 +199,62 @@ static int __ms_recovery_media_db(uid_t uid) return MS_MEDIA_ERR_NONE; } -gboolean ms_read_socket(gpointer user_data) +gboolean ms_read_socket(GIOChannel *src, GIOCondition condition, gpointer user_data) { ms_comm_msg_s recv_msg; + ms_comm_msg_s res_msg; int sockfd = MS_SOCK_NOT_ALLOCATE; - int ret; - int res; - int pid; - int req_num = -1; + int ret = MS_MEDIA_ERR_NONE; int client_sock = -1; - GIOChannel *src = user_data; ms_peer_credentials creds; sockfd = g_io_channel_unix_get_fd(src); - if (sockfd < 0) { - MS_DBG_ERR("sock fd is invalid!"); - return TRUE; - } + MS_DBG_RETVM_IF(sockfd < 0, TRUE, "sock fd is invalid!"); /* get client socket fd */ ret = ms_ipc_accept_client_tcp(sockfd, &client_sock); - if (ret != MS_MEDIA_ERR_NONE) - return TRUE; + MS_DBG_RETV_IF(ret != MS_MEDIA_ERR_NONE, TRUE); memset(&creds, 0, sizeof(creds)); ret = ms_cynara_receive_untrusted_message(client_sock, &recv_msg, &creds); - if (ret != MS_MEDIA_ERR_NONE) { - res = ret; + if (ret != MS_MEDIA_ERR_NONE) goto ERROR; - } - if (strncmp(recv_msg.msg, MEDIA_ROOT_PATH_EXTERNAL, strlen(MEDIA_ROOT_PATH_EXTERNAL)) == 0) { - if (ms_cynara_check(&creds, EXTERNAL_STORAGE_PRIVILEGE) != MS_MEDIA_ERR_NONE) { - res = ret; - MS_SAFE_FREE(creds.smack); - MS_SAFE_FREE(creds.uid); - goto ERROR; - } - } else { - if (ms_cynara_check(&creds, MEDIA_STORAGE_PRIVILEGE) != MS_MEDIA_ERR_NONE) { - res = ret; - MS_SAFE_FREE(creds.smack); - MS_SAFE_FREE(creds.uid); - goto ERROR; - } - } + if (strncmp(recv_msg.msg, MEDIA_ROOT_PATH_EXTERNAL, strlen(MEDIA_ROOT_PATH_EXTERNAL)) == 0) + ret = ms_cynara_check(&creds, EXTERNAL_STORAGE_PRIVILEGE); + else + ret = ms_cynara_check(&creds, MEDIA_STORAGE_PRIVILEGE); MS_SAFE_FREE(creds.smack); MS_SAFE_FREE(creds.uid); - /* copy received data */ - req_num = recv_msg.msg_type; - pid = recv_msg.pid; + if (ret != MS_MEDIA_ERR_NONE) + goto ERROR; /* register file request media server inserts the meta data of one file into media db */ - if (req_num == MS_MSG_DIRECTORY_SCANNING - || req_num == MS_MSG_BULK_INSERT - || req_num == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE - || req_num == MS_MSG_DIRECTORY_SCANNING_CANCEL) { - if ((ret = ms_send_scan_request(&recv_msg, client_sock)) != MS_MEDIA_ERR_NONE) { - res = ret; + switch (recv_msg.msg_type) { + case MS_MSG_DIRECTORY_SCANNING: + case MS_MSG_BULK_INSERT: + case MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE: + ret = ms_send_scan_request(&recv_msg, client_sock); + if (ret != MS_MEDIA_ERR_NONE) + goto ERROR; + break; + case MS_MSG_DIRECTORY_SCANNING_CANCEL: + ret = ms_send_scan_request(&recv_msg, client_sock); + if (ret != MS_MEDIA_ERR_NONE) goto ERROR; - } - if (req_num == MS_MSG_DIRECTORY_SCANNING_CANCEL) - ms_remove_request_owner(pid, recv_msg.msg); - } else if (req_num == MS_MSG_MEDIA_DB_MALFORMED) { + ms_remove_request_owner(recv_msg.pid, recv_msg.msg); + break; + case MS_MSG_MEDIA_DB_MALFORMED: /*media DB is corrupted, recovery media DB*/ ret = __ms_recovery_media_db(recv_msg.uid); - res = ret; goto ERROR; - } else { - /* NEED IMPLEMENTATION */ + default: close(client_sock); + break; } /*Active flush */ @@ -357,67 +262,52 @@ gboolean ms_read_socket(gpointer user_data) return TRUE; ERROR: - { - ms_comm_msg_s res_msg; - - memset(&res_msg, 0x0, sizeof(ms_comm_msg_s)); + memset(&res_msg, 0x0, sizeof(ms_comm_msg_s)); - switch (req_num) { - case MS_MSG_DIRECTORY_SCANNING: - case MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE: - res_msg.msg_type = MS_MSG_SCANNER_RESULT; - break; - case MS_MSG_BULK_INSERT: - res_msg.msg_type = MS_MSG_SCANNER_BULK_RESULT; - break; - case MS_MSG_MEDIA_DB_MALFORMED: - res_msg.msg_type = MS_MSG_MEDIA_DB_MALFORMED; - break; - default: - break; - } + switch (recv_msg.msg_type) { + case MS_MSG_DIRECTORY_SCANNING: + case MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE: + res_msg.msg_type = MS_MSG_SCANNER_RESULT; + break; + case MS_MSG_BULK_INSERT: + res_msg.msg_type = MS_MSG_SCANNER_BULK_RESULT; + break; + case MS_MSG_MEDIA_DB_MALFORMED: + res_msg.msg_type = MS_MSG_MEDIA_DB_MALFORMED; + break; + default: + break; + } - res_msg.result = res; + res_msg.result = ret; - ms_ipc_send_msg_to_client_tcp(client_sock, &res_msg, NULL); - close(client_sock); - } + ms_ipc_send_msg_to_client_tcp(client_sock, &res_msg, NULL); + close(client_sock); return TRUE; } static int __ms_send_request(ms_comm_msg_s *send_msg) { - int res = MS_MEDIA_ERR_NONE; int fd = -1; int err = -1; fd = open(MS_SCANNER_FIFO_PATH_REQ, O_WRONLY|O_NONBLOCK); - if (fd < 0) { - MS_DBG_STRERROR("fifo open failed"); - return MS_MEDIA_ERR_FILE_OPEN_FAIL; - } + MS_DBG_RETVM_IF(fd < 0, MS_MEDIA_ERR_FILE_OPEN_FAIL, "fifo open failed"); /* send message */ err = write(fd, send_msg, sizeof(ms_comm_msg_s)); - if (err < 0) { - MS_DBG_STRERROR("fifo write failed"); - close(fd); - return MS_MEDIA_ERR_FILE_READ_FAIL; - } - close(fd); + MS_DBG_RETVM_IF(err < 0, MS_MEDIA_ERR_FILE_READ_FAIL, "fifo write failed"); ms_req_num++; - return res; + return MS_MEDIA_ERR_NONE; } int ms_send_scan_request(ms_comm_msg_s *send_msg, int client_sock) { - int res = MS_MEDIA_ERR_NONE; - int err = MS_MEDIA_ERR_NONE; - int pid = send_msg->pid; + int ret = MS_MEDIA_ERR_NONE; g_mutex_lock(&scanner_mutex); @@ -430,44 +320,27 @@ int ms_send_scan_request(ms_comm_msg_s *send_msg, int client_sock) MS_DBG_WARN("Scanner starts"); g_mutex_unlock(&scanner_mutex); - err = ms_scanner_start(); - if (err == MS_MEDIA_ERR_NONE) { - err = __ms_send_request(send_msg); - if (err != MS_MEDIA_ERR_NONE) - MS_DBG_ERR("__ms_send_request failed [%d]", err); - } else { - MS_DBG_ERR("Scanner starting failed [%d]", err); - res = err; - } + ret = ms_scanner_start(); + MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "Scanner starting failed [%d]", ret); + + ret = __ms_send_request(send_msg); + MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "__ms_send_request failed [%d]", ret); } - if ((res == MS_MEDIA_ERR_NONE) && (send_msg->msg_type != MS_MSG_DIRECTORY_SCANNING_CANCEL)) { - /* this request process in media scanner */ - if ((err = __ms_add_owner(pid, client_sock, send_msg->msg)) != MS_MEDIA_ERR_NONE) { - MS_DBG_ERR("__ms_add_owner failed. %d", err); - res = err; - } + if (send_msg->msg_type != MS_MSG_DIRECTORY_SCANNING_CANCEL) { + ret = __ms_add_owner(send_msg->pid, client_sock, send_msg->msg); + MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "__ms_add_owner failed [%d]", ret); } - return res; + return ret; } int ms_send_storage_scan_request(const char *root_path, const char *storage_id, ms_dir_scan_type_t scan_type, uid_t uid) { - int ret = MS_MEDIA_ERR_NONE; - - if (uid == 0) { - MS_DBG_ERR("Invalid UID"); - return MS_MEDIA_ERR_INVALID_PARAMETER; - } + ms_comm_msg_s scan_msg = { 0, }; + MS_DBG_RETVM_IF(uid == 0, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid UID"); - ms_comm_msg_s scan_msg = { - .msg_type = MS_MSG_STORAGE_INVALID, - .pid = 0, /* pid 0 means media-server */ - .uid = 0, - .result = -1, - .msg = {0}, - }; + memset(&scan_msg, 0, sizeof(scan_msg)); /* msg_type */ switch (scan_type) { @@ -484,10 +357,8 @@ int ms_send_storage_scan_request(const char *root_path, const char *storage_id, scan_msg.msg_type = MS_MSG_STORAGE_META; break; default: - ret = MS_MEDIA_ERR_INVALID_PARAMETER; - MS_DBG_ERR("ms_send_storage_scan_request invalid parameter"); - goto ERROR; - break; + MS_DBG_ERR("Invalid scan type"); + return MS_MEDIA_ERR_INVALID_PARAMETER; } if (root_path != NULL) @@ -498,11 +369,7 @@ int ms_send_storage_scan_request(const char *root_path, const char *storage_id, scan_msg.uid = uid; - ret = ms_send_scan_request(&scan_msg, -1); - -ERROR: - - return ret; + return ms_send_scan_request(&scan_msg, -1); } #ifdef _USE_SENIOR_MODE @@ -572,74 +439,54 @@ gboolean ms_read_db_tcp_socket(GIOChannel *src, GIOCondition condition, gpointer { int sock = -1; int client_sock = -1; - char * sql_query = NULL; ms_comm_msg_s recv_msg; int ret = MS_MEDIA_ERR_NONE; sqlite3 *db_handle = (sqlite3 *)data; - int send_msg = MS_MEDIA_ERR_NONE; ms_peer_credentials creds; - if (power_off) { - MS_DBG_WARN("in the power off sequence"); - return TRUE; - } + MS_DBG_RETVM_IF(power_off, TRUE, "POWER OFF"); sock = g_io_channel_unix_get_fd(src); - if (sock < 0) { - MS_DBG_ERR("sock fd is invalid!"); - return TRUE; - } + MS_DBG_RETVM_IF(sock < 0, TRUE, "sock fd is invalid!"); /* get client socket fd */ ret = ms_ipc_accept_client_tcp(sock, &client_sock); - if (ret != MS_MEDIA_ERR_NONE) - return TRUE; + MS_DBG_RETV_IF(ret != MS_MEDIA_ERR_NONE, TRUE); memset(&creds, 0, sizeof(creds)); ret = ms_cynara_receive_untrusted_message(client_sock, &recv_msg, &creds); if (ret != MS_MEDIA_ERR_NONE) { MS_DBG_ERR("ms_ipc_receive_message_tcp failed [%d]", ret); - send_msg = ret; goto ERROR; } - if (ms_cynara_check(&creds, CONTENT_WRITE_PRIVILEGE) != MS_MEDIA_ERR_NONE) { + ret = ms_cynara_check(&creds, CONTENT_WRITE_PRIVILEGE); + MS_SAFE_FREE(creds.smack); + MS_SAFE_FREE(creds.uid); + if (ret != MS_MEDIA_ERR_NONE) { MS_DBG_ERR("Permission denied"); - MS_SAFE_FREE(creds.smack); - MS_SAFE_FREE(creds.uid); - send_msg = MS_MEDIA_ERR_PERMISSION_DENIED; goto ERROR; } - MS_SAFE_FREE(creds.smack); - MS_SAFE_FREE(creds.uid); - - if (media_db_connect(&db_handle, recv_msg.uid, true) != MS_MEDIA_ERR_NONE) { + ret = media_db_connect(&db_handle, recv_msg.uid, true); + if (ret != MS_MEDIA_ERR_NONE) { MS_DBG_ERR("Failed to connect DB"); goto ERROR; } if (MS_STRING_VALID(recv_msg.msg)) { - sql_query = g_strdup(recv_msg.msg); - if (sql_query != NULL) { - ret = media_db_update_db(db_handle, sql_query); - if (ret != MS_MEDIA_ERR_NONE) - MS_DBG_ERR("media_db_update_db error : %d", ret); - - send_msg = ret; - MS_SAFE_FREE(sql_query); - } else { - send_msg = MS_MEDIA_ERR_OUT_OF_MEMORY; - } + ret = media_db_update_db(db_handle, recv_msg.msg); + if (ret != MS_MEDIA_ERR_NONE) + MS_DBG_ERR("media_db_update_db error : %d", ret); } else { - send_msg = MS_MEDIA_ERR_INVALID_PARAMETER; + ret = MS_MEDIA_ERR_INVALID_PARAMETER; } media_db_disconnect(db_handle); ERROR: - if (write(client_sock, &send_msg, sizeof(send_msg)) != sizeof(send_msg)) + if (write(client_sock, &ret, sizeof(ret)) != sizeof(ret)) MS_DBG_STRERROR("send failed"); else MS_DBG("Sent successfully"); @@ -654,113 +501,63 @@ gboolean ms_receive_message_from_scanner(GIOChannel *src, GIOCondition condition { ms_comm_msg_s recv_msg; int fd = MS_SOCK_NOT_ALLOCATE; - int msg_type; - int pid = -1; int err; fd = g_io_channel_unix_get_fd(src); - if (fd < 0) { - MS_DBG_ERR("fd is invalid!"); - return TRUE; - } + MS_DBG_RETVM_IF(fd < 0, TRUE, "fd is invalid!"); /* read() is blocked until media scanner sends message */ err = read(fd, &recv_msg, sizeof(recv_msg)); if (err < 0) { MS_DBG_STRERROR("fifo read failed"); close(fd); - return MS_MEDIA_ERR_FILE_READ_FAIL; - } - - if (strlen(recv_msg.msg) == 0 || strlen(recv_msg.msg) >= MAX_MSG_SIZE) { - MS_DBG_ERR("msg size is invlid"); - return TRUE; + return FALSE; } - if (strlen(recv_msg.storage_id) >= MS_UUID_SIZE) { - MS_DBG_ERR("storage_id size is invlid"); - return TRUE; - } + MS_DBG_RETVM_IF(strlen(recv_msg.msg) == 0 || strlen(recv_msg.msg) >= MAX_MSG_SIZE, TRUE, "msg size is invalid!"); + MS_DBG_RETVM_IF(strlen(recv_msg.storage_id) >= MS_UUID_SIZE, TRUE, "storage_id size is invlid"); MS_DBG_SLOG("receive result from scanner [%d][%d] M[%.*s] S[%.*s]", recv_msg.pid, recv_msg.result, MAX_MSG_SIZE, recv_msg.msg, MS_UUID_SIZE, recv_msg.storage_id); - msg_type = recv_msg.msg_type; - pid = recv_msg.pid; - if ((msg_type == MS_MSG_SCANNER_RESULT) || - (msg_type == MS_MSG_SCANNER_BULK_RESULT) || - (msg_type == MS_MSG_SCANNER_COMPLETE) || - (msg_type == MS_MSG_SCANNER_PARTIAL) || - (msg_type == MS_MSG_EXTRACTOR_COMPLETE) || - (msg_type == MS_MSG_DIRECTORY_SCANNING_CANCEL)) { - MS_DBG_WARN("DB UPDATING IS DONE[%d]", msg_type); - - if ((msg_type == MS_MSG_SCANNER_RESULT) || - (msg_type == MS_MSG_SCANNER_BULK_RESULT) || - (msg_type == MS_MSG_EXTRACTOR_COMPLETE) || - (msg_type == MS_MSG_DIRECTORY_SCANNING_CANCEL)) { - ms_req_num--; - MS_DBG_WARN("ms_req_num-- [%d]", ms_req_num); - } - - if (pid != 0) - __ms_send_result_to_client(pid, &recv_msg); - else - MS_DBG_SLOG("This request is from media-server"); - } else { - MS_DBG_ERR("This result message is wrong : %d", recv_msg.msg_type); + switch (recv_msg.msg_type) { + case MS_MSG_SCANNER_RESULT: + case MS_MSG_SCANNER_BULK_RESULT: + case MS_MSG_DIRECTORY_SCANNING_CANCEL: + case MS_MSG_EXTRACTOR_COMPLETE: + MS_DBG_WARN("DB UPDATING IS DONE[%d]", recv_msg.msg_type); + ms_req_num--; + MS_DBG_WARN("ms_req_num-- [%d]", ms_req_num); + break; + case MS_MSG_SCANNER_PARTIAL: + case MS_MSG_SCANNER_COMPLETE: + MS_DBG_WARN("DB UPDATING IS DONE[%d]", recv_msg.msg_type); + break; + default: + MS_DBG_ERR("Wrong MSG [%d]", recv_msg.msg_type); + return TRUE; } + if (recv_msg.pid != 0) + __ms_send_result_to_client(recv_msg.pid, &recv_msg); + return TRUE; } -int ms_remove_request_owner(int pid, const char *req_path) +void ms_remove_request_owner(int pid, const char *req_path) { - ms_req_owner_data *owner_data = NULL; - - __ms_find_owner(pid, req_path, &owner_data); - if (owner_data != NULL) { - MS_DBG("PID : %d", owner_data->pid); - - close(owner_data->client_sockfd); - /* free owner data*/ - __ms_delete_owner(owner_data); - } else { - MS_DBG_ERR("Not found Owner"); - return MS_MEDIA_ERR_INTERNAL; - } - - return MS_MEDIA_ERR_NONE; + __ms_delete_owner(pid, req_path); } int ms_send_storage_otg_scan_request(const char *path, const char *device_uuid, ms_dir_scan_type_t scan_type, uid_t uid) { - int ret = MS_MEDIA_ERR_NONE; - - if (path == NULL) { - MS_DBG_ERR("Invalid path"); - return MS_MEDIA_ERR_INVALID_PARAMETER; - } - - if (device_uuid == NULL) { - MS_DBG_ERR("Invalid device_uuid"); - return MS_MEDIA_ERR_INVALID_PARAMETER; - } + ms_comm_msg_s scan_msg = { 0, }; - if (uid == 0) { - MS_DBG_ERR("Invalid UID"); - return MS_MEDIA_ERR_INVALID_PARAMETER; - } + MS_DBG_RETVM_IF(!path, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path"); + MS_DBG_RETVM_IF(!device_uuid, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid device_uuid"); + MS_DBG_RETVM_IF(uid == 0, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid UID"); - ms_comm_msg_s scan_msg = { - .msg_type = MS_MSG_STORAGE_INVALID, - .pid = 0, /* pid 0 means media-server */ - .uid = 0, - .result = -1, - .storage_id = {0}, - .msg = {0}, - }; + memset(&scan_msg, 0, sizeof(scan_msg)); - /* msg_type */ switch (scan_type) { case MS_SCAN_PART: scan_msg.msg_type = MS_MSG_STORAGE_PARTIAL; @@ -772,22 +569,14 @@ int ms_send_storage_otg_scan_request(const char *path, const char *device_uuid, scan_msg.msg_type = MS_MSG_STORAGE_INVALID; break; default: - ret = MS_MEDIA_ERR_INVALID_PARAMETER; - MS_DBG_ERR("ms_send_storage_scan_request invalid parameter"); - goto ERROR; - break; + MS_DBG_ERR("Invalid scan type"); + return MS_MEDIA_ERR_INVALID_PARAMETER; } SAFE_STRLCPY(scan_msg.msg, path, sizeof(scan_msg.msg)); SAFE_STRLCPY(scan_msg.storage_id, device_uuid, sizeof(scan_msg.storage_id)); - scan_msg.uid = uid; - ret = ms_send_scan_request(&scan_msg, -1); - -ERROR: - - return ret; + return ms_send_scan_request(&scan_msg, -1); } - -- 2.7.4