#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;
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) {
}
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;
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;
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;
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);
return TRUE;
}
-gboolean _ms_thumb_agent_execute_server()
+static gboolean __ms_thumb_agent_execute_server(void)
{
int pid;
pid = fork();
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;
}
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");
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);
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;
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);
/* 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;
}
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;
}
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;
}
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;
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");
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) {
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;
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);
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();
}
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);
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)
{
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);
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");