Improve thumb thread 60/214760/4
authorMinje Ahn <minje.ahn@samsung.com>
Thu, 26 Sep 2019 06:18:33 +0000 (15:18 +0900)
committerMinje Ahn <minje.ahn@samsung.com>
Sun, 29 Sep 2019 23:40:39 +0000 (08:40 +0900)
Change-Id: I36fc4924cd2d59b93d81ce52fa64a9cd5a7a0463
Signed-off-by: Minje Ahn <minje.ahn@samsung.com>
src/server/media-server-thumb.c

index 71bb87c..ef1b0a1 100755 (executable)
 
 #define THUMB_SERVER_PATH tzplatform_mkpath(TZ_SYS_BIN, "media-thumbnail-server")
 
-gboolean _ms_thumb_agent_timer();
-
 static GMainLoop *g_thumb_agent_loop = NULL;
-static gboolean g_folk_thumb_server = FALSE;
+static gboolean g_thumb_server_forked = FALSE;
 static gboolean g_shutdowning_thumb_server = FALSE;
 static int g_communicate_sock = 0;
 static int g_timer_id = 0;
 static int g_server_pid = 0;
 
 static GQueue *g_request_queue = NULL;
-static int g_queue_work = 0;
+static gboolean g_queue_work = FALSE;
 
 typedef struct {
        int client_sock;
@@ -52,21 +50,19 @@ typedef struct {
 
 extern char MEDIA_IPC_PATH[][70];
 
-gboolean _ms_thumb_agent_prepare_tcp_socket(int *sock_fd, unsigned short serv_port);
-
 GMainLoop* ms_get_thumb_thread_mainloop(void)
 {
        return g_thumb_agent_loop;
 }
 
-int ms_thumb_get_server_pid()
+int ms_thumb_get_server_pid(void)
 {
        return g_server_pid;
 }
 
-void ms_thumb_reset_server_status()
+void ms_thumb_reset_server_status(void)
 {
-       g_folk_thumb_server = FALSE;
+       g_thumb_server_forked = FALSE;
        g_shutdowning_thumb_server = FALSE;
 
        if (g_timer_id > 0) {
@@ -75,35 +71,19 @@ void ms_thumb_reset_server_status()
        }
 
        g_server_pid = 0;
-
-       return;
-}
-
-void _ms_thumb_create_timer(int id)
-{
-       if (id > 0)
-               g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), id));
-
-       GSource *timer_src = g_timeout_source_new_seconds(MS_TIMEOUT_SEC_20);
-       g_source_set_callback(timer_src, _ms_thumb_agent_timer, NULL, NULL);
-       g_timer_id = g_source_attach(timer_src, g_main_context_get_thread_default());
-
 }
 
-int _media_thumb_get_error()
+static gboolean __ms_thumb_agent_prepare_tcp_socket(int *sock_fd, unsigned short serv_port)
 {
-       if (errno == EWOULDBLOCK) {
-               MS_DBG_ERR("Timeout. Can't try any more");
-               ms_thumb_reset_server_status();
-
-               return MS_MEDIA_ERR_SOCKET_RECEIVE_TIMEOUT;
-       } else {
-               MS_DBG_STRERROR("recvfrom failed");
-               return MS_MEDIA_ERR_SOCKET_RECEIVE;
+       if (ms_ipc_create_server_socket(serv_port, sock_fd) < 0) {
+               MS_DBG_ERR("_ms_thumb_create_socket failed");
+               return FALSE;
        }
+
+       return TRUE;
 }
 
-int _ms_thumb_recv_msg(int sock, thumbMsg *msg)
+static int __ms_thumb_recv_msg(int sock, thumbMsg *msg)
 {
        int remain_size = 0;
        int recv_pos = 0;
@@ -112,16 +92,13 @@ int _ms_thumb_recv_msg(int sock, thumbMsg *msg)
        unsigned int header_size = sizeof(thumbMsg) - sizeof(unsigned char *);
 
        MS_MALLOC(buf, header_size);
-       if (buf == NULL) {
-               MS_DBG_STRERROR("malloc failed");
-               return MS_MEDIA_ERR_OUT_OF_MEMORY;
-       }
+       MS_DBG_RETV_IF(!buf, MS_MEDIA_ERR_OUT_OF_MEMORY);
 
        while (header_size > 0) {
                if ((recv_len = recv(sock, buf + recv_pos, header_size, 0)) < 0) {
                        MS_DBG_STRERROR("recv failed");
                        MS_SAFE_FREE(buf);
-                       return _media_thumb_get_error();
+                       return MS_MEDIA_ERR_IPC;
                }
                header_size -= recv_len;
                recv_pos += recv_len;
@@ -131,104 +108,70 @@ int _ms_thumb_recv_msg(int sock, thumbMsg *msg)
        recv_pos = 0;
 
        memcpy(msg, buf, header_size);
-
-       MS_DBG("status[%d]", msg->status);
-
        MS_SAFE_FREE(buf);
 
-       if (msg->msg_type == THUMB_REQUEST_KILL_SERVER) {
-               MS_DBG("Thumb-server does not send KILL_SERVER[%d]", msg->msg_type);
-               return MS_MEDIA_ERR_DATA_TAINTED;
-       }
-
-       if (strlen(msg->org_path) == 0 || strlen(msg->org_path) >= MS_FILE_PATH_LEN_MAX) {
-               MS_DBG_ERR("org_path size is invalid %zu", strlen(msg->org_path));
-
-               return MS_MEDIA_ERR_DATA_TAINTED;
-       }
-
-       /* it can be empty string */
-       if (strlen(msg->dst_path) >= MS_FILE_PATH_LEN_MAX) {
-               MS_DBG_ERR("dst_path size is invalid %zu", strlen(msg->dst_path));
+       MS_DBG("status[%d]", msg->status);
 
-               return MS_MEDIA_ERR_DATA_TAINTED;
-       }
+       MS_DBG_RETVM_IF(msg->msg_type == THUMB_REQUEST_KILL_SERVER, MS_MEDIA_ERR_IPC, "Wrong msg");
+       MS_DBG_RETVM_IF(strlen(msg->org_path) == 0 || strlen(msg->org_path) >= MS_FILE_PATH_LEN_MAX, MS_MEDIA_ERR_IPC, "Invalid org_path");
+       MS_DBG_RETVM_IF(strlen(msg->dst_path) >= MS_FILE_PATH_LEN_MAX, MS_MEDIA_ERR_IPC, "Invalid dst_path");
+       MS_DBG_RETVM_IF(msg->thumb_size < 0, MS_MEDIA_ERR_IPC, "Invalid thumb_size");
+       MS_DBG_RETV_IF(msg->thumb_size == 0, MS_MEDIA_ERR_NONE);
 
-       if (msg->thumb_size < 0) {
-               MS_DBG_ERR("msg->thumb_size is invalid %d", msg->thumb_size);
-               return MS_MEDIA_ERR_DATA_TAINTED;
-       }
+       remain_size = msg->thumb_size;
+       MS_MALLOC(buf, remain_size);
+       MS_DBG_RETV_IF(!buf, MS_MEDIA_ERR_OUT_OF_MEMORY);
 
-       if (msg->thumb_size > 0) {
-               remain_size = msg->thumb_size;
-               MS_MALLOC(buf, remain_size);
-               if (buf == NULL)
-                       return MS_MEDIA_ERR_OUT_OF_MEMORY;
-
-               while (remain_size > 0) {
-                       if ((recv_len = recv(sock, buf + recv_pos, remain_size, 0)) < 0) {
-                               MS_DBG_STRERROR("recv failed");
-                               MS_SAFE_FREE(buf);
-                               return _media_thumb_get_error();
-                       }
-                       fsync(sock);
-
-                       recv_pos += recv_len;
-                       remain_size -= recv_len;
+       while (remain_size > 0) {
+               if ((recv_len = recv(sock, buf + recv_pos, remain_size, 0)) < 0) {
+                       MS_DBG_STRERROR("recv failed");
+                       MS_SAFE_FREE(buf);
+                       return MS_MEDIA_ERR_IPC;
                }
+               fsync(sock);
 
-               MS_SAFE_FREE(msg->thumb_data);
-
-               MS_MALLOC(msg->thumb_data, (unsigned int)(msg->thumb_size));
-               if (msg->thumb_data != NULL) {
-                       memcpy(msg->thumb_data, buf, msg->thumb_size);
-               } else {
-                       MS_SAFE_FREE(buf);
+               recv_pos += recv_len;
+               remain_size -= recv_len;
+       }
 
-                       return MS_MEDIA_ERR_OUT_OF_MEMORY;
-               }
+       MS_SAFE_FREE(msg->thumb_data);
 
+       MS_MALLOC(msg->thumb_data, (unsigned int)(msg->thumb_size));
+       if (msg->thumb_data) {
+               memcpy(msg->thumb_data, buf, msg->thumb_size);
+       } else {
                MS_SAFE_FREE(buf);
+               return MS_MEDIA_ERR_OUT_OF_MEMORY;
        }
 
+       MS_SAFE_FREE(buf);
+
        return MS_MEDIA_ERR_NONE;
 }
 
-int _ms_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
+static int __ms_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
 {
-       if (req_msg == NULL || buf == NULL)
-               return -1;
-
-       int data_len = 0;
-       int header_size = 0;
+       int header_size = sizeof(thumbMsg) - sizeof(unsigned char *);
        unsigned int size = 0;
 
-       header_size = sizeof(thumbMsg) - sizeof(unsigned char *);
-       data_len = req_msg->thumb_size;
-       if (data_len < 0) {
-               MS_DBG_ERR("msg size is wrong");
-               return -1;
-       }
+       MS_DBG_RETV_IF(!req_msg || !buf || req_msg->thumb_size < 0, MS_MEDIA_ERR_INVALID_PARAMETER);
 
        MS_DBG_SLOG("Basic Size[%d] org_path[%s] dst_path[%s] thumb_data[%d]", header_size, req_msg->org_path, req_msg->dst_path, req_msg->thumb_size);
 
-       size = header_size + data_len;
+       size = header_size + req_msg->thumb_size;
        MS_MALLOC(*buf, size);
-       if (*buf == NULL) {
-               MS_DBG_STRERROR("MALLOC failed");
-               return -1;
-       }
+       MS_DBG_RETV_IF(!(*buf), MS_MEDIA_ERR_OUT_OF_MEMORY);
 
        memcpy(*buf, req_msg, header_size);
-       if (data_len > 0)
-               memcpy((*buf) + header_size, req_msg->thumb_data, data_len);
+       if (req_msg->thumb_size > 0)
+               memcpy((*buf) + header_size, req_msg->thumb_data, req_msg->thumb_size);
 
        *buf_size = size;
 
-       return 0;
+       return MS_MEDIA_ERR_NONE;
 }
 
-gboolean _ms_thumb_agent_recv_msg_from_server()
+static gboolean __ms_thumb_agent_recv_msg_from_server(void)
 {
        struct sockaddr_un serv_addr;
        unsigned int serv_addr_len;
@@ -237,7 +180,7 @@ gboolean _ms_thumb_agent_recv_msg_from_server()
        int sockfd = -1;
 
        if (g_communicate_sock <= 0)
-               _ms_thumb_agent_prepare_tcp_socket(&g_communicate_sock, MS_THUMB_COMM_PORT);
+               __ms_thumb_agent_prepare_tcp_socket(&g_communicate_sock, MS_THUMB_COMM_PORT);
 
        serv_addr_len = sizeof(serv_addr);
 
@@ -259,7 +202,7 @@ gboolean _ms_thumb_agent_recv_msg_from_server()
        return TRUE;
 }
 
-gboolean _ms_thumb_agent_execute_server()
+static gboolean __ms_thumb_agent_execute_server(void)
 {
        int pid;
        pid = fork();
@@ -270,12 +213,12 @@ gboolean _ms_thumb_agent_execute_server()
                execl(THUMB_SERVER_PATH, "media-thumbnail-server", NULL);
        } else {
                MS_DBG("Child process is %d", pid);
-               g_folk_thumb_server = TRUE;
+               g_thumb_server_forked = TRUE;
        }
 
        g_server_pid = pid;
 
-       if (!_ms_thumb_agent_recv_msg_from_server()) {
+       if (!__ms_thumb_agent_recv_msg_from_server()) {
                MS_DBG_ERR("_ms_thumb_agent_recv_msg_from_server is failed");
                return FALSE;
        }
@@ -283,16 +226,14 @@ gboolean _ms_thumb_agent_execute_server()
        return TRUE;
 }
 
-gboolean _ms_thumb_agent_send_msg_to_thumb_server(thumbMsg *recv_msg, thumbMsg *res_msg)
+static gboolean __ms_thumb_agent_send_msg_to_thumb_server(thumbMsg *recv_msg, thumbMsg *res_msg)
 {
        int sock;
        struct sockaddr_un serv_addr;
-       int send_str_len = strlen(recv_msg->org_path);
+       int buf_size = 0;
+       unsigned char *buf = NULL;
 
-       if (send_str_len >= MAX_FILEPATH_LEN) {
-               MS_DBG_ERR("original path's length exceeds %d(max packet size)", MAX_FILEPATH_LEN);
-               return FALSE;
-       }
+       MS_DBG_RETVM_IF(!recv_msg || strlen(recv_msg->org_path) >= MAX_FILEPATH_LEN, FALSE, "Invalid msg");
 
        if (ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &sock) < 0) {
                MS_DBG_ERR("ms_ipc_create_client_socket failed");
@@ -310,11 +251,8 @@ gboolean _ms_thumb_agent_send_msg_to_thumb_server(thumbMsg *recv_msg, thumbMsg *
                return FALSE;
        }
 
-       int buf_size = 0;
-       unsigned char *buf = NULL;
-       _ms_thumb_set_buffer(recv_msg, &buf, &buf_size);
+       __ms_thumb_set_buffer(recv_msg, &buf, &buf_size);
 
-       //MS_DBG("buffer size : %d", buf_size);
        if (send(sock, buf, buf_size, 0) < 0) {
                MS_DBG_STRERROR("send failed");
                MS_SAFE_FREE(buf);
@@ -331,7 +269,7 @@ gboolean _ms_thumb_agent_send_msg_to_thumb_server(thumbMsg *recv_msg, thumbMsg *
                return TRUE;
        }
 
-       if (_ms_thumb_recv_msg(sock, res_msg) < 0) {
+       if (__ms_thumb_recv_msg(sock, res_msg) < 0) {
                MS_DBG_ERR("_ms_thumb_recv_msg failed");
                close(sock);
                return FALSE;
@@ -347,7 +285,7 @@ gboolean _ms_thumb_agent_send_msg_to_thumb_server(thumbMsg *recv_msg, thumbMsg *
        return TRUE;
 }
 
-gboolean _ms_thumb_agent_timer()
+static gboolean __ms_thumb_agent_timer(gpointer data)
 {
        g_timer_id = 0;
        MS_DBG("Timer is called.. Now killing media-thumbnail-server[%d]", g_server_pid);
@@ -363,7 +301,7 @@ gboolean _ms_thumb_agent_timer()
 
                /* Command Kill to thumbnail server */
                g_shutdowning_thumb_server = TRUE;
-               if (!_ms_thumb_agent_send_msg_to_thumb_server(&msg, NULL)) {
+               if (!__ms_thumb_agent_send_msg_to_thumb_server(&msg, NULL)) {
                        MS_DBG_ERR("_ms_thumb_agent_send_msg_to_thumb_server is failed");
                        g_shutdowning_thumb_server = FALSE;
                }
@@ -375,17 +313,24 @@ gboolean _ms_thumb_agent_timer()
        return FALSE;
 }
 
-gboolean _ms_thumb_request_to_server(gpointer data)
+static void __ms_thumb_create_timer(int id)
 {
-       int req_len = 0;
+       if (id > 0)
+               g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), id));
 
-       req_len = g_queue_get_length(g_request_queue);
+       GSource *timer_src = g_timeout_source_new_seconds(MS_TIMEOUT_SEC_20);
+       g_source_set_callback(timer_src, __ms_thumb_agent_timer, NULL, NULL);
+       g_timer_id = g_source_attach(timer_src, g_main_context_get_thread_default());
+}
 
-       MS_DBG("Queue length : %d", req_len);
+static gboolean __ms_thumb_request_to_server(gpointer data)
+{
+       thumbRequest *req = NULL;
+       guint req_len = g_queue_get_length(g_request_queue);
 
-       if (req_len <= 0) {
-               MS_DBG("There is no request job in the queue");
-               g_queue_work = 0;
+       MS_DBG("Queue length : %d", req_len);
+       if (req_len == 0) {
+               g_queue_work = FALSE;
                return FALSE;
        }
 
@@ -395,27 +340,18 @@ gboolean _ms_thumb_request_to_server(gpointer data)
                return TRUE;
        }
 
-       if (g_folk_thumb_server == FALSE) {
+       if (!g_thumb_server_forked) {
                MS_DBG_WARN("Thumb server is not running.. so start it");
-               if (!_ms_thumb_agent_execute_server()) {
+               if (!__ms_thumb_agent_execute_server()) {
                        MS_DBG_ERR("_ms_thumb_agent_execute_server is failed");
-                       g_queue_work = 0;
+                       g_queue_work = FALSE;
                        return FALSE;
-               } else {
-                       _ms_thumb_create_timer(g_timer_id);
                }
-       } else {
-               /* Timer is re-created*/
-               _ms_thumb_create_timer(g_timer_id);
        }
+       __ms_thumb_create_timer(g_timer_id);
 
-       thumbRequest *req = NULL;
        req = (thumbRequest *)g_queue_pop_head(g_request_queue);
-
-       if (req == NULL) {
-               MS_DBG_ERR("Failed to get a request job from queue");
-               return TRUE;
-       }
+       MS_DBG_RETVM_IF(!req, TRUE, "Failed to get a request job from queue");
 
        int client_sock = -1;
        thumbMsg *recv_msg = NULL;
@@ -433,7 +369,7 @@ gboolean _ms_thumb_request_to_server(gpointer data)
        }
 
        if (recv_msg) {
-               if (!_ms_thumb_agent_send_msg_to_thumb_server(recv_msg, &res_msg)) {
+               if (!__ms_thumb_agent_send_msg_to_thumb_server(recv_msg, &res_msg)) {
                        MS_DBG_ERR("_ms_thumb_agent_send_msg_to_thumb_server is failed");
 
                        thumbMsg res_msg;
@@ -448,7 +384,7 @@ gboolean _ms_thumb_request_to_server(gpointer data)
 
                        int buf_size = 0;
                        unsigned char *buf = NULL;
-                       _ms_thumb_set_buffer(&res_msg, &buf, &buf_size);
+                       __ms_thumb_set_buffer(&res_msg, &buf, &buf_size);
 
                        if (send(client_sock, buf, buf_size, 0) < 0)
                                MS_DBG_STRERROR("send failed");
@@ -473,7 +409,7 @@ gboolean _ms_thumb_request_to_server(gpointer data)
        int send_len = 0;
        int send_pos = 0;
        unsigned char *buf = NULL;
-       _ms_thumb_set_buffer(&res_msg, &buf, &buf_size);
+       __ms_thumb_set_buffer(&res_msg, &buf, &buf_size);
 
        while (buf_size > 0) {
                if ((send_len = send(client_sock, buf + send_pos, buf_size, 0)) < 0) {
@@ -494,7 +430,7 @@ gboolean _ms_thumb_request_to_server(gpointer data)
        return TRUE;
 }
 
-gboolean _ms_thumb_agent_read_socket(GIOChannel *src, GIOCondition condition, gpointer data)
+static gboolean __ms_thumb_agent_read_socket(GIOChannel *src, GIOCondition condition, gpointer data)
 {
        struct sockaddr_un client_addr;
        unsigned int client_addr_len;
@@ -502,12 +438,10 @@ gboolean _ms_thumb_agent_read_socket(GIOChannel *src, GIOCondition condition, gp
        int sock = -1;
        int client_sock = -1;
        unsigned char *buf = NULL;
+       thumbRequest *thumb_req = NULL;
 
        sock = g_io_channel_unix_get_fd(src);
-       if (sock < 0) {
-               MS_DBG_ERR("sock fd is invalid!");
-               return G_SOURCE_CONTINUE;
-       }
+       MS_DBG_RETVM_IF(sock < 0, G_SOURCE_CONTINUE, "sock fd is invalid!");
 
        client_addr_len = sizeof(client_addr);
 
@@ -519,35 +453,28 @@ gboolean _ms_thumb_agent_read_socket(GIOChannel *src, GIOCondition condition, gp
        MS_DBG("Client[%d] is accepted", client_sock);
 
        recv_msg = calloc(1, sizeof(thumbMsg));
-       if (recv_msg == NULL) {
+       if (!recv_msg) {
                MS_DBG_ERR("Failed to allocate memory");
                close(client_sock);
                return G_SOURCE_CONTINUE;
        }
 
-       if (_ms_thumb_recv_msg(client_sock, recv_msg) < 0) {
+       if (__ms_thumb_recv_msg(client_sock, recv_msg) < 0) {
                MS_DBG_ERR("_ms_thumb_recv_msg failed ");
-               close(client_sock);
-               MS_SAFE_FREE(recv_msg);
-               return G_SOURCE_CONTINUE;
+               goto ERROR;
        }
 
-       thumbRequest *thumb_req = NULL;
-
        MS_MALLOC(thumb_req, sizeof(thumbRequest));
-       if (thumb_req == NULL) {
+       if (!thumb_req) {
                MS_DBG_ERR("Failed to create request element");
-               close(client_sock);
-               MS_SAFE_FREE(recv_msg->thumb_data);
-               MS_SAFE_FREE(recv_msg);
-               return G_SOURCE_CONTINUE;
+               goto ERROR;
        }
 
        thumb_req->client_sock = client_sock;
        thumb_req->recv_msg = recv_msg;
 
-       if (g_request_queue == NULL) {
-               MS_DBG_WARN("queue is init");
+       if (!g_request_queue) {
+               MS_DBG_WARN("Creating queue");
                g_request_queue = g_queue_new();
        }
 
@@ -563,19 +490,15 @@ gboolean _ms_thumb_agent_read_socket(GIOChannel *src, GIOCondition condition, gp
                res_msg.thumb_size = 0;
 
                int buf_size = 0;
-               _ms_thumb_set_buffer(&res_msg, &buf, &buf_size);
+               __ms_thumb_set_buffer(&res_msg, &buf, &buf_size);
 
                if (send(client_sock, buf, buf_size, 0) < 0)
                        MS_DBG_STRERROR("send failed");
                else
                        MS_DBG("Sent Refuse msg from %s", recv_msg->org_path);
 
-               close(client_sock);
                MS_SAFE_FREE(buf);
-               MS_SAFE_FREE(thumb_req->recv_msg);
-               MS_SAFE_FREE(thumb_req);
-
-               return G_SOURCE_CONTINUE;
+               goto ERROR;
        }
 
        MS_DBG_SLOG("%s is queued", recv_msg->org_path);
@@ -584,27 +507,19 @@ gboolean _ms_thumb_agent_read_socket(GIOChannel *src, GIOCondition condition, gp
        if (!g_queue_work) {
                GSource *src_request = NULL;
                src_request = g_idle_source_new();
-               g_source_set_callback(src_request, _ms_thumb_request_to_server, NULL, NULL);
+               g_source_set_callback(src_request, __ms_thumb_request_to_server, NULL, NULL);
                g_source_attach(src_request, g_main_context_get_thread_default());
-               g_queue_work = 1;
+               g_queue_work = TRUE;
        }
 
        return G_SOURCE_CONTINUE;
+ERROR:
+       close(client_sock);
+       MS_SAFE_FREE(recv_msg->thumb_data);
+       MS_SAFE_FREE(recv_msg);
+       return G_SOURCE_CONTINUE;
 }
 
-gboolean _ms_thumb_agent_prepare_tcp_socket(int *sock_fd, unsigned short serv_port)
-{
-       int sock;
-
-       if (ms_ipc_create_server_socket(serv_port, &sock) < 0) {
-               MS_DBG_ERR("_ms_thumb_create_socket failed");
-               return FALSE;
-       }
-
-       *sock_fd = sock;
-
-       return TRUE;
-}
 
 gpointer ms_thumb_agent_start_thread(gpointer data)
 {
@@ -613,20 +528,17 @@ gpointer ms_thumb_agent_start_thread(gpointer data)
        GIOChannel *channel = NULL;
        GMainContext *context = NULL;
 
-       MS_DBG_FENTER();
-
        /* Create and bind new TCP socket */
-       if (!_ms_thumb_agent_prepare_tcp_socket(&sockfd, MS_THUMB_CREATOR_PORT)) {
+       if (!__ms_thumb_agent_prepare_tcp_socket(&sockfd, MS_THUMB_CREATOR_PORT)) {
                MS_DBG_ERR("Failed to create socket");
                return NULL;
        }
 
        context = g_main_context_new();
-
-       if (context == NULL)
+       if (!context) {
                MS_DBG_ERR("g_main_context_new failed");
-       else
-               MS_DBG("g_main_context_new success");
+               return NULL;
+       }
 
        g_thumb_agent_loop = g_main_loop_new(context, FALSE);
        g_main_context_push_thread_default(context);
@@ -636,7 +548,7 @@ gpointer ms_thumb_agent_start_thread(gpointer data)
        source = g_io_create_watch(channel, G_IO_IN);
 
        /* Set callback to be called when socket is readable */
-       g_source_set_callback(source, (GSourceFunc)_ms_thumb_agent_read_socket, NULL, NULL);
+       g_source_set_callback(source, (GSourceFunc)__ms_thumb_agent_read_socket, NULL, NULL);
        g_source_attach(source, context);
 
        MS_DBG_INFO("Thumbnail Agent thread is running");