Revert "Fix UID/GID"
[platform/core/multimedia/libmedia-thumbnail.git] / src / ipc / media-thumb-ipc.c
1 /*
2  * libmedia-thumbnail
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyunjun Ko <zzoon.ko@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include "media-thumbnail.h"
23 #include "media-thumb-ipc.h"
24 #include "media-thumb-util.h"
25 #include "media-thumb-db.h"
26 #include "media-thumb-debug.h"
27 #include <glib.h>
28 #include <fcntl.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <grp.h>
32 #include <pwd.h>
33
34 #define THUMB_SOCK_BLOCK_SIZE 512
35 #define THUMB_IPC_PATH tzplatform_mkpath(TZ_SYS_RUN, "media-server/media_ipc_thumbcreator.socket")
36
37 static GQueue *g_request_queue = NULL;
38 static GQueue *g_manage_queue = NULL;
39 static GQueue *g_request_raw_queue = NULL;
40 static GQueue *g_manage_raw_queue = NULL;
41
42
43 typedef struct {
44         GIOChannel *channel;
45         int msg_type;
46         unsigned int request_id;
47         bool isCanceled;
48         int source_id;
49         uid_t uid;
50         char *path;
51         thumbUserData *userData;
52 } thumbReq;
53
54 typedef struct {
55         GIOChannel *channel;
56         int msg_type;
57         bool isCanceled;
58         int request_id;
59         int source_id;
60         int width;
61         int height;
62         uid_t uid;
63         char *path;
64         thumbRawUserData *userData;
65 } thumbRawReq;
66
67 int _media_thumb_send_request();
68 int _media_thumb_raw_data_send_request();
69
70
71 int _media_thumb_get_error()
72 {
73         if (errno == EWOULDBLOCK) {
74                 thumb_err("Timeout. Can't try any more");
75                 return MS_MEDIA_ERR_SOCKET_RECEIVE_TIMEOUT;
76         } else {
77                 thumb_stderror("recvfrom failed");
78                 return MS_MEDIA_ERR_SOCKET_RECEIVE;
79         }
80 }
81
82 void __media_thumb_shutdown_channel(bool only_shutdown)
83 {
84         int len = -1;
85         thumbReq *req = (thumbReq *)g_queue_peek_head(g_request_queue);
86
87         len = g_queue_get_length(g_manage_queue);
88
89         if (req != NULL) {
90                 if (!only_shutdown) {
91                         if (len == 0 && req->isCanceled == true) {
92                                 thumb_dbg("The manage queue will be released");
93                         } else {
94                                 thumb_dbg("There is remain item in the manage queue");
95                                 return;
96                         }
97                 }
98                 GSource *source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id);
99                 if (source_id != NULL) {
100                         g_source_destroy(source_id);
101                 } else {
102                         thumb_err("G_SOURCE_ID is NULL");
103                 }
104
105                 g_io_channel_shutdown(req->channel, TRUE, NULL);
106                 g_io_channel_unref(req->channel);
107                 g_queue_pop_head(g_request_queue);
108
109                 SAFE_FREE(req->path);
110                 SAFE_FREE(req->userData);
111                 SAFE_FREE(req);
112
113                 if (g_manage_queue && len == 0) {
114                         g_queue_free(g_manage_queue);
115                         g_manage_queue = NULL;
116                 }
117         }
118 }
119
120 int __media_thumb_pop_req_queue(const char *path)
121 {
122         int req_len = 0;
123
124         req_len = g_queue_get_length(g_request_queue);
125
126         if (req_len <= 0) {
127 //              thumb_dbg("There is no request in the queue");
128         } else {
129                 __media_thumb_shutdown_channel(true);
130         }
131
132         /* Check manage queue */
133         if (g_manage_queue) {
134                 req_len = g_queue_get_length(g_manage_queue);
135
136                 if (req_len > 0)
137                         _media_thumb_send_request();
138         }
139
140         return MS_MEDIA_ERR_NONE;
141 }
142
143 int __media_thumb_check_req_queue_for_cancel(unsigned int request_id, const char *path)
144 {
145         int req_len = 0;
146
147         req_len = g_queue_get_length(g_request_queue);
148
149         if (req_len <= 0) {
150 //              thumb_dbg("There is no request in the queue");
151         } else {
152                 thumbReq *req = NULL;
153                 req = (thumbReq *)g_queue_peek_head(g_request_queue);
154
155                 if (req != NULL && strncmp(path, req->path, strlen(path)) == 0 && request_id == req->request_id) {
156                         req->isCanceled = true;
157                         __media_thumb_shutdown_channel(false);
158
159                         return MS_MEDIA_ERR_NONE;
160                 }
161         }
162
163         return MS_MEDIA_ERR_INTERNAL;
164 }
165
166 int __media_thumb_pop_manage_queue(unsigned int request_id, const char *path)
167 {
168         int req_len = 0, i;
169         bool flag = false;
170
171         req_len = g_queue_get_length(g_manage_queue);
172
173         if (req_len < 0) {
174 //              thumb_dbg("There is no request in the queue");
175         } else {
176                 for (i = 0; i < req_len; i++) {
177                         thumbReq *req = NULL;
178                         req = (thumbReq *)g_queue_peek_nth(g_manage_queue, i);
179                         if (req == NULL) continue;
180
181                         if (strncmp(path, req->path, strlen(path)) == 0 && request_id == req->request_id) {
182                                 g_queue_pop_nth(g_manage_queue, i);
183
184                                 SAFE_FREE(req->path);
185                                 SAFE_FREE(req->userData);
186                                 SAFE_FREE(req);
187                                 flag = true;
188                                 break;
189                         }
190                 }
191                 if (!flag) {
192                         return __media_thumb_check_req_queue_for_cancel(request_id, path);
193                 } else {
194                         __media_thumb_shutdown_channel(false);
195
196                         return MS_MEDIA_ERR_NONE;
197                 }
198         }
199
200         return MS_MEDIA_ERR_NONE;
201 }
202
203
204 int __media_thumb_pop_raw_data_req_queue(int request_id, bool shutdown_channel)
205 {
206         int req_len = 0, i;
207
208         req_len = g_queue_get_length(g_request_raw_queue);
209
210         if (req_len <= 0) {
211 //              thumb_dbg("There is no request in the queue");
212         } else {
213
214                 for (i = 0; i < req_len; i++) {
215                         thumbRawReq *req = NULL;
216                         req = (thumbRawReq *)g_queue_peek_nth(g_request_raw_queue, i);
217                         if (req == NULL) continue;
218
219                         if (request_id == req->request_id) {
220                                 if (shutdown_channel) {
221                                         GSource *source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id);
222                                         if (source_id != NULL) {
223                                                 g_source_destroy(source_id);
224                                         } else {
225                                                 thumb_err("G_SOURCE_ID is NULL");
226                                         }
227
228                                         g_io_channel_shutdown(req->channel, TRUE, NULL);
229                                         g_io_channel_unref(req->channel);
230                                 }
231                                 g_queue_pop_nth(g_request_raw_queue, i);
232
233                                 SAFE_FREE(req->userData);
234                                 SAFE_FREE(req);
235
236                                 break;
237                         }
238                 }
239                 if (i == req_len) {
240 //                      thumb_dbg("There's no %s in the queue", path);
241                 }
242         }
243
244         return MS_MEDIA_ERR_NONE;
245 }
246
247 int __media_thumb_check_raw_data_req_queue_for_cancel(int request_id)
248 {
249         int req_len = 0;
250
251         req_len = g_queue_get_length(g_request_raw_queue);
252
253         if (req_len <= 0) {
254 //              thumb_dbg("There is no request in the queue");
255         } else {
256                 thumbRawReq *req = NULL;
257                 req = (thumbRawReq *)g_queue_peek_head(g_request_raw_queue);
258
259                 if (req != NULL && request_id == req->request_id) {
260                         req->isCanceled = true;
261                         return MS_MEDIA_ERR_NONE;
262                 }
263         }
264
265         return MS_MEDIA_ERR_INTERNAL;
266 }
267
268 int __media_thumb_pop_raw_data_manage_queue(int request_id)
269 {
270         int req_len = 0, i;
271         int flag = false;
272
273         req_len = g_queue_get_length(g_manage_raw_queue);
274
275         if (req_len < 0) {
276 //              thumb_dbg("There is no request in the queue");
277         } else {
278
279                 for (i = 0; i < req_len; i++) {
280                         thumbRawReq *req = NULL;
281                         req = (thumbRawReq *)g_queue_peek_nth(g_manage_raw_queue, i);
282                         if (req == NULL) continue;
283
284                         if (request_id == req->request_id) {
285                                 g_queue_pop_nth(g_manage_raw_queue, i);
286
287                                 SAFE_FREE(req->userData);
288                                 SAFE_FREE(req);
289                                 flag = true;
290                                 break;
291                         }
292                 }
293                 if (!flag) {
294                         return __media_thumb_check_raw_data_req_queue_for_cancel(request_id);
295                 }
296         }
297
298         return MS_MEDIA_ERR_NONE;
299 }
300 #if 0
301 int __media_thumb_check_req_queue(const char *path)
302 {
303         int req_len = 0, i;
304
305         req_len = g_queue_get_length(g_request_queue);
306
307 //      thumb_dbg("Queue length : %d", req_len);
308 //      thumb_dbg("Queue path : %s", path);
309
310         if (req_len <= 0) {
311 //              thumb_dbg("There is no request in the queue");
312         } else {
313
314                 for (i = 0; i < req_len; i++) {
315                         thumbReq *req = NULL;
316                         req = (thumbReq *)g_queue_peek_nth(g_request_queue, i);
317                         if (req == NULL) continue;
318
319                         if (strncmp(path, req->path, strlen(path)) == 0) {
320                                 //thumb_dbg("Same Request - %s", path);
321                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
322
323                                 break;
324                         }
325                 }
326         }
327
328         if (g_manage_queue != NULL) {
329                 req_len = g_queue_get_length(g_manage_queue);
330
331                 if (req_len <= 0) {
332 //              thumb_dbg("There is no request in the queue");
333                 } else {
334                         for (i = 0; i < req_len; i++) {
335                                 thumbReq *req = NULL;
336                                 req = (thumbReq *)g_queue_peek_nth(g_manage_queue, i);
337                                 if (req == NULL) continue;
338
339                                 if (strncmp(path, req->path, strlen(path)) == 0) {
340                                         //thumb_dbg("Same Request - %s", path);
341                                         return MS_MEDIA_ERR_INVALID_PARAMETER;
342
343                                         break;
344                                 }
345                         }
346                 }
347         }
348
349
350         return MS_MEDIA_ERR_NONE;
351 }
352 #endif
353 bool __media_thumb_check_cancel(void)
354 {
355         thumbReq *req = NULL;
356         req = (thumbReq *)g_queue_peek_head(g_request_queue);
357
358         if (req == NULL) {
359                 return false;
360         } else {
361                 if (req->isCanceled)
362                         return false;
363                 else
364                         return true;
365         }
366 }
367
368 bool __media_thumb_check_cancel_for_raw(void)
369 {
370         thumbRawReq *req = NULL;
371         req = (thumbRawReq *)g_queue_peek_head(g_request_raw_queue);
372
373         if (req == NULL) {
374                 return false;
375         } else {
376                 if (req->isCanceled)
377                         return false;
378                 else
379                         return true;
380         }
381 }
382
383 int
384 _media_thumb_recv_msg(int sock, int header_size, thumbMsg *msg)
385 {
386         int recv_msg_len = 0;
387         int remain_size = 0;
388         int block_size = THUMB_SOCK_BLOCK_SIZE;
389         int recv_block = 0;
390         unsigned char *buf = NULL;
391         unsigned char *block_buf = NULL;
392
393         THUMB_MALLOC(buf, header_size);
394         THUMB_MALLOC(block_buf, THUMB_SOCK_BLOCK_SIZE);
395         if (buf == NULL || block_buf == NULL) {
396                 thumb_err("memory allocation failed");
397                 SAFE_FREE(buf);
398                 SAFE_FREE(block_buf);
399                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
400         }
401
402         if ((recv_msg_len = recv(sock, buf, header_size, 0)) <= 0) {
403                 thumb_stderror("recv failed");
404                 SAFE_FREE(buf);
405                 SAFE_FREE(block_buf);
406                 return _media_thumb_get_error();
407         }
408
409         memcpy(msg, buf, header_size);
410         //thumb_dbg("origin_path_size : %d, dest_path_size : %d, thumb_size : %d", msg->origin_path_size, msg->dest_path_size, msg->thumb_size);
411
412         SAFE_FREE(buf);
413         if (msg->origin_path_size < 0 || msg->dest_path_size < 0 || msg->thumb_size < 0) {
414                 thumb_err("recv data is wrong");
415                 SAFE_FREE(block_buf);
416                 return MS_MEDIA_ERR_SOCKET_RECEIVE;
417         }
418
419         remain_size = msg->origin_path_size + msg->dest_path_size + msg->thumb_size;
420         THUMB_MALLOC(buf, remain_size);
421         if (buf == NULL) {
422                 SAFE_FREE(block_buf);
423                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
424         }
425
426         while (remain_size > 0) {
427                 if (remain_size < THUMB_SOCK_BLOCK_SIZE) {
428                         block_size = remain_size;
429                 }
430                 if ((recv_msg_len = recv(sock, block_buf, block_size, 0)) < 0) {
431                         thumb_stderror("recv failed");
432                         SAFE_FREE(buf);
433                         SAFE_FREE(block_buf);
434                         return _media_thumb_get_error();
435                 }
436                 memcpy(buf+recv_block, block_buf, block_size);
437                 recv_block += block_size;
438                 remain_size -= block_size;
439         }
440
441         strncpy(msg->org_path, (char *)buf, msg->origin_path_size);
442         strncpy(msg->dst_path, (char *)buf + msg->origin_path_size, msg->dest_path_size);
443
444         SAFE_FREE(msg->thumb_data);
445         if (msg->thumb_size > 0) {
446                 THUMB_MALLOC(msg->thumb_data, msg->thumb_size);
447                 if (msg->thumb_data != NULL) {
448                         memcpy(msg->thumb_data, buf + msg->origin_path_size + msg->dest_path_size, msg->thumb_size);
449                 } else {
450                         SAFE_FREE(buf);
451                         SAFE_FREE(block_buf);
452
453                         return MS_MEDIA_ERR_OUT_OF_MEMORY;
454                 }
455         }
456
457         SAFE_FREE(buf);
458         SAFE_FREE(block_buf);
459
460         return MS_MEDIA_ERR_NONE;
461 }
462
463 int
464 _media_thumb_recv_udp_msg(int sock, int header_size, thumbMsg *msg, struct sockaddr_un *from_addr, unsigned int *from_size)
465 {
466         int recv_msg_len = 0;
467         unsigned int from_addr_size = sizeof(struct sockaddr_un);
468         unsigned char *buf = NULL;
469
470         THUMB_MALLOC(buf, sizeof(thumbMsg));
471         if (buf == NULL) {
472                 thumb_err("memory allocation failed");
473                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
474         }
475
476         if ((recv_msg_len = recvfrom(sock, buf, sizeof(thumbMsg), 0, (struct sockaddr *)from_addr, &from_addr_size)) < 0) {
477                 thumb_stderror("recvform failed");
478                 SAFE_FREE(buf);
479                 return _media_thumb_get_error();
480         }
481
482         memcpy(msg, buf, header_size);
483
484         if (msg->origin_path_size <= 0 || msg->origin_path_size > MAX_PATH_SIZE) {
485                 SAFE_FREE(buf);
486                 thumb_err("msg->origin_path_size is invalid %d", msg->origin_path_size);
487                 return MS_MEDIA_ERR_INVALID_PARAMETER;
488         }
489
490         strncpy(msg->org_path, (char*)buf + header_size, msg->origin_path_size);
491
492         if (msg->dest_path_size <= 0 || msg->dest_path_size > MAX_PATH_SIZE) {
493                 SAFE_FREE(buf);
494                 thumb_err("msg->origin_path_size is invalid %d", msg->dest_path_size);
495                 return MS_MEDIA_ERR_INVALID_PARAMETER;
496         }
497
498         strncpy(msg->dst_path, (char*)buf + header_size + msg->origin_path_size, msg->dest_path_size);
499
500         SAFE_FREE(buf);
501         *from_size = from_addr_size;
502
503         return MS_MEDIA_ERR_NONE;
504 }
505
506 int
507 _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
508 {
509         if (req_msg == NULL || buf == NULL) {
510                 return MS_MEDIA_ERR_INVALID_PARAMETER;
511         }
512
513         int org_path_len = 0;
514         int dst_path_len = 0;
515         int thumb_data_len = 0;
516         int size = 0;
517         int header_size = 0;
518
519         header_size = sizeof(thumbMsg) -(MAX_FILEPATH_LEN * 2) - sizeof(unsigned char *);
520         org_path_len = req_msg->origin_path_size;
521         dst_path_len = req_msg->dest_path_size;
522         thumb_data_len = req_msg->thumb_size;
523
524         //thumb_dbg("Basic Size : %d, org_path : %s[%d], dst_path : %s[%d], thumb_data_len : %d", header_size, req_msg->org_path, org_path_len, req_msg->dst_path, dst_path_len, thumb_data_len);
525
526         size = header_size + org_path_len + dst_path_len + thumb_data_len;
527         THUMB_MALLOC(*buf, size);
528         if (*buf == NULL) {
529                 *buf_size = 0;
530                 return 0;
531         }
532         memcpy(*buf, req_msg, header_size);
533         memcpy((*buf)+header_size, req_msg->org_path, org_path_len);
534         memcpy((*buf)+header_size + org_path_len, req_msg->dst_path, dst_path_len);
535         if (thumb_data_len > 0)
536                 memcpy((*buf)+header_size + org_path_len + dst_path_len, req_msg->thumb_data, thumb_data_len);
537
538         *buf_size = size;
539
540         return MS_MEDIA_ERR_NONE;
541 }
542
543 int
544 _media_thumb_request(int msg_type, const char *origin_path, char *thumb_path, int max_length, media_thumb_info *thumb_info, uid_t uid)
545 {
546         int sock = -1;
547         struct sockaddr_un serv_addr;
548         ms_sock_info_s sock_info;
549         int recv_str_len = 0;
550         int err = MS_MEDIA_ERR_NONE;
551         int pid;
552         sock_info.port = MS_THUMB_CREATOR_PORT;
553
554         err = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
555         if (err != MS_MEDIA_ERR_NONE) {
556                 thumb_err("ms_ipc_create_client_socket failed");
557                 return err;
558         }
559
560         memset(&serv_addr, 0, sizeof(serv_addr));
561         sock = sock_info.sock_fd;
562         serv_addr.sun_family = AF_UNIX;
563         strncpy(serv_addr.sun_path, THUMB_IPC_PATH, strlen(THUMB_IPC_PATH));
564
565         /* Connecting to the thumbnail server */
566         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
567                 thumb_stderror("connect");
568                 ms_ipc_delete_client_socket(&sock_info);
569                 return MS_MEDIA_ERR_SOCKET_CONN;
570         }
571
572         thumbMsg req_msg;
573         thumbMsg recv_msg;
574
575         memset((void *)&req_msg, 0, sizeof(thumbMsg));
576         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
577
578         /* Get PID of client*/
579         pid = getpid();
580         req_msg.pid = pid;
581
582         /* Set requset message */
583         req_msg.msg_type = msg_type;
584         req_msg.uid = uid;
585         strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
586         req_msg.org_path[strlen(req_msg.org_path)] = '\0';
587
588         if (msg_type == THUMB_REQUEST_SAVE_FILE) {
589                 strncpy(req_msg.dst_path, thumb_path, sizeof(req_msg.dst_path));
590                 req_msg.dst_path[strlen(req_msg.dst_path)] = '\0';
591         }
592
593         req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
594         req_msg.dest_path_size = strlen(req_msg.dst_path) + 1;
595         req_msg.thumb_size = 0;
596
597         if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
598                 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
599                 ms_ipc_delete_client_socket(&sock_info);
600                 return MS_MEDIA_ERR_INVALID_PARAMETER;
601         }
602
603         unsigned char *buf = NULL;
604         int buf_size = 0;
605         int header_size = 0;
606
607         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
608         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
609
610         if (send(sock, buf, buf_size, 0) != buf_size) {
611                 thumb_err("sendto failed: %d", errno);
612                 SAFE_FREE(buf);
613                 ms_ipc_delete_client_socket(&sock_info);
614                 return MS_MEDIA_ERR_SOCKET_SEND;
615         }
616
617         thumb_dbg("Sending msg to thumbnail daemon is successful");
618
619         SAFE_FREE(buf);
620
621         if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
622                 thumb_err("_media_thumb_recv_msg failed ");
623                 ms_ipc_delete_client_socket(&sock_info);
624                 return err;
625         }
626
627         recv_str_len = strlen(recv_msg.org_path);
628         thumb_dbg_slog("recv %s(%d) from thumb daemon is successful", recv_msg.org_path, recv_str_len);
629
630         ms_ipc_delete_client_socket(&sock_info);
631
632         if (recv_str_len > max_length) {
633                 thumb_err("user buffer is too small. Output's length is %d", recv_str_len);
634                 SAFE_FREE(recv_msg.thumb_data);
635                 return MS_MEDIA_ERR_INVALID_PARAMETER;
636         }
637
638         if (recv_msg.status == THUMB_FAIL) {
639                 thumb_err("Failed to make thumbnail");
640                 SAFE_FREE(recv_msg.thumb_data);
641                 return MS_MEDIA_ERR_INVALID_PARAMETER;
642         }
643
644         if (msg_type != THUMB_REQUEST_SAVE_FILE) {
645                 strncpy(thumb_path, recv_msg.dst_path, max_length);
646         }
647
648         thumb_info->origin_width = recv_msg.origin_width;
649         thumb_info->origin_height = recv_msg.origin_height;
650
651         SAFE_FREE(recv_msg.thumb_data);
652
653         return MS_MEDIA_ERR_NONE;
654 }
655
656 gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
657 {
658         thumbMsg recv_msg;
659         int header_size = 0;
660         int sock = 0;
661         int err = MS_MEDIA_ERR_NONE;
662
663         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
664         sock = g_io_channel_unix_get_fd(src);
665
666         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
667
668         thumb_err("_media_thumb_write_socket socket : %d", sock);
669
670         if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
671                 thumb_err("_media_thumb_recv_msg failed ");
672                 if (recv_msg.origin_path_size > 0) {
673                         __media_thumb_pop_req_queue(recv_msg.org_path);
674                 } else {
675                         thumb_err("origin path size is wrong.");
676                 }
677
678                 return FALSE;
679         }
680
681         //thumb_dbg("Completed..%s", recv_msg.org_path);
682
683         if (recv_msg.status == THUMB_FAIL) {
684                 thumb_err("Failed to make thumbnail");
685                 err = MS_MEDIA_ERR_INTERNAL;
686         }
687
688         if (__media_thumb_check_cancel()) {
689                 if (data) {
690                         thumbUserData* cb = (thumbUserData*)data;
691                         if (cb->func != NULL)
692                                 cb->func(err, recv_msg.dst_path, cb->user_data);
693                 }
694         }
695
696         __media_thumb_pop_req_queue(recv_msg.org_path);
697
698         thumb_dbg("Done");
699
700         return FALSE;
701 }
702
703 gboolean _media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
704 {
705         thumbMsg recv_msg;
706         int header_size = 0;
707         int sock = 0;
708         int len = -1;
709         int err = MS_MEDIA_ERR_NONE;
710
711         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
712         sock = g_io_channel_unix_get_fd(src);
713
714         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
715
716         thumb_err("_media_thumb_write_socket socket : %d", sock);
717
718         if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
719                 thumb_err("_media_thumb_recv_msg failed ");
720                 if (recv_msg.request_id > 0) {
721                         __media_thumb_pop_raw_data_req_queue(recv_msg.request_id, TRUE);
722                 } else {
723                         thumb_err("origin path size is wrong.");
724                 }
725
726                 return FALSE;
727         }
728
729         g_io_channel_shutdown(src, TRUE, NULL);
730         g_io_channel_unref(src);
731
732         if (recv_msg.status == THUMB_FAIL) {
733                 thumb_err("Failed to make thumbnail");
734                 err = MS_MEDIA_ERR_INTERNAL;
735         }
736
737         if (__media_thumb_check_cancel_for_raw()) {
738                 if (data) {
739                         thumbRawUserData* cb = (thumbRawUserData*)data;
740                         if (cb->func != NULL)
741                                 cb->func(err, recv_msg.request_id, recv_msg.org_path, recv_msg.thumb_width, recv_msg.thumb_height, recv_msg.thumb_data, recv_msg.thumb_size, cb->user_data);
742                 }
743         }
744
745         __media_thumb_pop_raw_data_req_queue(recv_msg.request_id, FALSE);
746
747         thumb_dbg("Done");
748
749         SAFE_FREE(recv_msg.thumb_data);
750
751         /* Check manage queue */
752         if (g_manage_raw_queue) {
753                 len = g_queue_get_length(g_manage_raw_queue);
754
755                 if (len > 0) {
756                         _media_thumb_raw_data_send_request();
757                 } else {
758                         g_queue_free(g_manage_raw_queue);
759                         g_manage_raw_queue = NULL;
760                 }
761         }
762
763         return FALSE;
764 }
765
766 int _media_thumb_send_request()
767 {
768         int err = MS_MEDIA_ERR_NONE;
769         int sock = -1;
770         struct sockaddr_un serv_addr;
771         ms_sock_info_s sock_info;
772         thumbReq *req_manager = NULL;
773         int pid;
774         sock_info.port = MS_THUMB_CREATOR_PORT;
775
776         err = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
777         if (err != MS_MEDIA_ERR_NONE) {
778                 thumb_err("ms_ipc_create_client_socket failed");
779                 return err;
780         }
781
782         memset(&serv_addr, 0, sizeof(serv_addr));
783         sock = sock_info.sock_fd;
784         serv_addr.sun_family = AF_UNIX;
785         strncpy(serv_addr.sun_path, THUMB_IPC_PATH, strlen(THUMB_IPC_PATH));
786
787         GIOChannel *channel = NULL;
788         channel = g_io_channel_unix_new(sock);
789         int source_id = -1;
790
791         /* Connecting to the thumbnail server */
792         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
793                 thumb_stderror("connect");
794                 if (errno == EACCES)
795                         err = MS_MEDIA_ERR_PERMISSION_DENIED;
796                 else
797                         err = MS_MEDIA_ERR_SOCKET_CONN;
798
799                 g_io_channel_shutdown(channel, TRUE, NULL);
800                 g_io_channel_unref(channel);
801                 ms_ipc_delete_client_socket(&sock_info);
802
803                 return err;
804         }
805
806         req_manager = (thumbReq *)g_queue_pop_head(g_manage_queue);
807
808         if (req_manager == NULL) {
809                 thumb_err("queue pop fail");
810                 g_io_channel_shutdown(channel, TRUE, NULL);
811                 g_io_channel_unref(channel);
812                 ms_ipc_delete_client_socket(&sock_info);
813                 return MS_MEDIA_ERR_INVALID_PARAMETER;
814         }
815
816         GSource *source = NULL;
817         source = g_io_create_watch(channel, G_IO_IN);
818         g_source_set_callback(source, (GSourceFunc)_media_thumb_write_socket, req_manager->userData, NULL);
819         source_id = g_source_attach(source, g_main_context_get_thread_default());
820
821         thumbMsg req_msg;
822
823         memset((void *)&req_msg, 0, sizeof(thumbMsg));
824
825         pid = getpid();
826         req_msg.pid = pid;
827         req_msg.msg_type = req_manager->msg_type;
828         req_msg.request_id = 0;
829         req_msg.uid = req_manager->uid;
830         strncpy(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
831         req_msg.org_path[strlen(req_msg.org_path)] = '\0';
832         req_msg.dst_path[0] = '\0';
833         req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
834         req_msg.dest_path_size = 1;
835         req_msg.thumb_size = 0;
836
837         if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
838                 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
839                 g_io_channel_shutdown(channel, TRUE, NULL);
840                 g_io_channel_unref(channel);
841                 ms_ipc_delete_client_socket(&sock_info);
842                 return MS_MEDIA_ERR_INVALID_PARAMETER;
843         }
844
845         unsigned char *buf = NULL;
846         int buf_size = 0;
847         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
848
849         if (send(sock, buf, buf_size, 0) != buf_size) {
850                 thumb_err("sendto failed: %d", errno);
851                 SAFE_FREE(buf);
852                 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
853                 g_io_channel_shutdown(channel, TRUE, NULL);
854                 g_io_channel_unref(channel);
855                 ms_ipc_delete_client_socket(&sock_info);
856                 return MS_MEDIA_ERR_SOCKET_SEND;
857         }
858
859         SAFE_FREE(buf);
860         thumb_dbg("Sending msg to thumbnail daemon is successful");
861
862
863         if (req_manager->msg_type == THUMB_REQUEST_DB_INSERT) {
864                 if (g_request_queue == NULL) {
865                         g_request_queue = g_queue_new();
866                 }
867
868                 thumbReq *thumb_req = NULL;
869                 THUMB_MALLOC(thumb_req, sizeof(thumbReq));
870                 if (thumb_req == NULL) {
871                         thumb_err("Failed to create request element");
872                         return MS_MEDIA_ERR_INVALID_PARAMETER;
873                 }
874
875                 thumb_req->channel = channel;
876                 thumb_req->path = strdup(req_manager->path);
877                 thumb_req->source_id = source_id;
878                 thumb_req->userData = req_manager->userData;
879
880                 g_queue_push_tail(g_request_queue, (gpointer)thumb_req);
881         }
882
883         return err;
884 }
885
886 int _media_thumb_raw_data_send_request()
887 {
888         int err = MS_MEDIA_ERR_NONE;
889         int sock = -1;
890         struct sockaddr_un serv_addr;
891         ms_sock_info_s sock_info;
892         thumbRawReq *req_manager = NULL;
893         int pid;
894         sock_info.port = MS_THUMB_CREATOR_PORT;
895
896         err = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
897         if (err != MS_MEDIA_ERR_NONE) {
898                 thumb_err("ms_ipc_create_client_socket failed");
899                 return err;
900         }
901
902         memset(&serv_addr, 0, sizeof(serv_addr));
903         sock = sock_info.sock_fd;
904         serv_addr.sun_family = AF_UNIX;
905         strncpy(serv_addr.sun_path, THUMB_IPC_PATH, strlen(THUMB_IPC_PATH));
906
907         GIOChannel *channel = NULL;
908         channel = g_io_channel_unix_new(sock);
909         int source_id = -1;
910
911         /* Connecting to the thumbnail server */
912         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
913                 thumb_stderror("connect error");
914                 if (errno == EACCES)
915                         err = MS_MEDIA_ERR_PERMISSION_DENIED;
916                 else
917                         err = MS_MEDIA_ERR_SOCKET_CONN;
918
919                 g_io_channel_shutdown(channel, TRUE, NULL);
920                 g_io_channel_unref(channel);
921                 ms_ipc_delete_client_socket(&sock_info);
922                 return err;
923         }
924
925         req_manager = (thumbRawReq *)g_queue_pop_head(g_manage_raw_queue);
926
927         if (req_manager == NULL) {
928                 thumb_err("queue pop fail");
929                 g_io_channel_shutdown(channel, TRUE, NULL);
930                 g_io_channel_unref(channel);
931                 ms_ipc_delete_client_socket(&sock_info);
932                 return MS_MEDIA_ERR_INVALID_PARAMETER;
933         }
934
935         GSource *source = NULL;
936         source = g_io_create_watch(channel, G_IO_IN);
937         g_source_set_callback(source, (GSourceFunc)_media_thumb_raw_data_write_socket, req_manager->userData, NULL);
938         source_id = g_source_attach(source, g_main_context_get_thread_default());
939
940         thumbMsg req_msg;
941         memset((void *)&req_msg, 0, sizeof(thumbMsg));
942
943         pid = getpid();
944         req_msg.pid = pid;
945         req_msg.msg_type = req_manager->msg_type;
946         req_msg.request_id = req_manager->request_id;
947         req_msg.thumb_width = req_manager->width;
948         req_msg.thumb_height = req_manager->height;
949         req_msg.uid = req_manager->uid;
950
951         strncpy(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
952         req_msg.org_path[strlen(req_msg.org_path)] = '\0';
953         req_msg.dst_path[0] = '\0';
954
955         req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
956         req_msg.dest_path_size = 1;
957         req_msg.thumb_size = 0;
958
959         if (req_msg.origin_path_size > MAX_PATH_SIZE) {
960                 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
961                 g_io_channel_shutdown(channel, TRUE, NULL);
962                 g_io_channel_unref(channel);
963                 return MS_MEDIA_ERR_INVALID_PARAMETER;
964         }
965
966         unsigned char *buf = NULL;
967         int buf_size = 0;
968         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
969
970         if (send(sock, buf, buf_size, 0) != buf_size) {
971                 thumb_err("sendto failed: %d", errno);
972                 SAFE_FREE(buf);
973                 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
974                 g_io_channel_shutdown(channel, TRUE, NULL);
975                 g_io_channel_unref(channel);
976                 return MS_MEDIA_ERR_SOCKET_SEND;
977         }
978
979         SAFE_FREE(buf);
980
981         if (req_manager->msg_type == THUMB_REQUEST_RAW_DATA) {
982                 if (g_request_raw_queue == NULL) {
983                         g_request_raw_queue = g_queue_new();
984                 }
985                 thumbRawReq *thumb_req = NULL;
986                 THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
987                 if (thumb_req == NULL) {
988                         thumb_err("Failed to create request element");
989                         return MS_MEDIA_ERR_INVALID_PARAMETER;
990                 }
991                 thumb_req->channel = channel;
992                 thumb_req->request_id = req_manager->request_id;
993                 thumb_req->source_id = source_id;
994                 thumb_req->userData = req_manager->userData;
995
996                 g_queue_push_tail(g_request_raw_queue, (gpointer)thumb_req);
997         }
998         return MS_MEDIA_ERR_NONE;
999 }
1000
1001 int _media_thumb_request_async(int msg_type, unsigned int request_id, const char *origin_path, thumbUserData *userData, uid_t uid)
1002 {
1003         int err = MS_MEDIA_ERR_NONE;
1004
1005         if (g_manage_queue == NULL) {
1006                 if (msg_type == THUMB_REQUEST_CANCEL_MEDIA)
1007                         return MS_MEDIA_ERR_INTERNAL;
1008
1009                 g_manage_queue = g_queue_new();
1010
1011                 thumbReq *thumb_req = NULL;
1012                 THUMB_MALLOC(thumb_req, sizeof(thumbReq));
1013                 if (thumb_req == NULL) {
1014                         thumb_err("Failed to create request element");
1015                         return MS_MEDIA_ERR_INVALID_PARAMETER;
1016                 }
1017
1018                 thumb_req->msg_type = msg_type;
1019                 thumb_req->path = strdup(origin_path);
1020                 thumb_req->userData = userData;
1021                 thumb_req->isCanceled = false;
1022                 thumb_req->request_id = request_id;
1023                 thumb_req->uid = uid;
1024
1025                 g_queue_push_tail(g_manage_queue, (gpointer)thumb_req);
1026
1027                 /* directly request at first time */
1028                 err = _media_thumb_send_request();
1029
1030         } else {
1031                 if (msg_type != THUMB_REQUEST_CANCEL_MEDIA) {
1032                         /* Enqueue */
1033 #if 0
1034                         if ((msg_type == THUMB_REQUEST_DB_INSERT) && (__media_thumb_check_req_queue(origin_path) < 0)) {
1035                                 thumb_err("duplicated request");
1036                                 return MS_MEDIA_ERR_THUMB_DUPLICATED_REQUEST;
1037                         }
1038 #endif
1039                         thumbReq *thumb_req = NULL;
1040                         THUMB_MALLOC(thumb_req, sizeof(thumbReq));
1041                         if (thumb_req == NULL) {
1042                                 thumb_err("Failed to create request element");
1043                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1044                         }
1045
1046                         thumb_req->msg_type = msg_type;
1047                         thumb_req->path = strdup(origin_path);
1048                         thumb_req->userData = userData;
1049                         thumb_req->isCanceled = false;
1050                         thumb_req->request_id = request_id;
1051                         thumb_req->uid = uid;
1052
1053                         g_queue_push_tail(g_manage_queue, (gpointer)thumb_req);
1054                 } else {
1055                         /* Dequeue */
1056                         err = __media_thumb_pop_manage_queue(request_id, origin_path);
1057                 }
1058         }
1059
1060         return err;
1061 }
1062
1063 int _media_thumb_request_raw_data_async(int msg_type, int request_id, const char *origin_path, int width, int height, thumbRawUserData *userData, uid_t uid)
1064 {
1065         int err = MS_MEDIA_ERR_NONE;
1066
1067         if (g_manage_raw_queue == NULL) {
1068                 if (msg_type == THUMB_REQUEST_CANCEL_RAW_DATA)
1069                         return MS_MEDIA_ERR_INTERNAL;
1070
1071                 g_manage_raw_queue = g_queue_new();
1072
1073                 thumbRawReq *thumb_req = NULL;
1074                 THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
1075                 if (thumb_req == NULL) {
1076                         thumb_err("Failed to create request element");
1077                         return MS_MEDIA_ERR_INVALID_PARAMETER;
1078                 }
1079
1080                 thumb_req->msg_type = msg_type;
1081                 thumb_req->request_id = request_id;
1082                 thumb_req->path = strdup(origin_path);
1083                 thumb_req->width = width;
1084                 thumb_req->height = height;
1085                 thumb_req->userData = userData;
1086                 thumb_req->isCanceled = false;
1087                 thumb_req->uid = uid;
1088
1089                 g_queue_push_tail(g_manage_raw_queue, (gpointer)thumb_req);
1090
1091                 /* directly request at first time */
1092                 err = _media_thumb_raw_data_send_request();
1093
1094         } else {
1095                 if (msg_type != THUMB_REQUEST_CANCEL_RAW_DATA) {
1096                         /* Enqueue */
1097                         thumbRawReq *thumb_req = NULL;
1098                         THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
1099                         if (thumb_req == NULL) {
1100                                 thumb_err("Failed to create request element");
1101                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1102                         }
1103
1104                         thumb_req->msg_type = msg_type;
1105                         thumb_req->request_id = request_id;
1106                         thumb_req->path = strdup(origin_path);
1107                         thumb_req->width = width;
1108                         thumb_req->height = height;
1109                         thumb_req->userData = userData;
1110                         thumb_req->isCanceled = false;
1111                         thumb_req->uid = uid;
1112
1113                         g_queue_push_tail(g_manage_raw_queue, (gpointer)thumb_req);
1114
1115                 } else {
1116                         /* Dequeue */
1117                         err = __media_thumb_pop_raw_data_manage_queue(request_id);
1118                 }
1119         }
1120
1121         return err;
1122 }
1123
1124 int _media_thumb_request_cancel_all(bool isRaw)
1125 {
1126         int i;
1127         int req_len = -1;
1128         int len = 0;
1129
1130         if (isRaw) {
1131                 thumbRawReq *tmp_raw_req = NULL;
1132                 if (g_manage_raw_queue == NULL) {
1133                         thumb_err("manage_queue is NULL");
1134                         if (g_request_raw_queue != NULL) {
1135                                 /* Check request queue */
1136                                 len = g_queue_get_length(g_request_raw_queue);
1137                                 if (len > 0) {
1138                                         tmp_raw_req = g_queue_peek_head(g_request_raw_queue);
1139                                         if (tmp_raw_req != NULL)
1140                                                 tmp_raw_req->isCanceled = true;
1141                                         return MS_MEDIA_ERR_NONE;
1142                                 }
1143                         }
1144                         thumb_err("request_queue is NULL");
1145                         return MS_MEDIA_ERR_INTERNAL;
1146                 }
1147                 req_len = g_queue_get_length(g_manage_raw_queue);
1148                 for (i = 0; i < req_len; i++) {
1149                         tmp_raw_req = g_queue_pop_tail(g_manage_raw_queue);
1150                         if (tmp_raw_req) {
1151                                 SAFE_FREE(tmp_raw_req->path);
1152                                 SAFE_FREE(tmp_raw_req->userData);
1153                                 SAFE_FREE(tmp_raw_req);
1154                                 tmp_raw_req = NULL;
1155                         }
1156                 }
1157                 g_queue_free(g_manage_raw_queue);
1158                 g_manage_raw_queue = NULL;
1159                 if (g_request_raw_queue != NULL) {
1160                         /* Check request queue */
1161                         len = g_queue_get_length(g_request_raw_queue);
1162                         if (len > 0) {
1163                                 tmp_raw_req = g_queue_peek_head(g_request_raw_queue);
1164                                 if (tmp_raw_req != NULL)
1165                                         tmp_raw_req->isCanceled = true;
1166                         }
1167                 }
1168         } else {
1169                 thumbReq *tmp_req = NULL;
1170                 if (g_manage_queue == NULL) {
1171                         thumb_err("manage_queue is NULL");
1172                         if (g_request_queue != NULL) {
1173                                 /* Check request queue */
1174                                 len = g_queue_get_length(g_request_queue);
1175                                 if (len > 0) {
1176                                         tmp_req = g_queue_peek_head(g_request_queue);
1177                                         if (tmp_req != NULL)
1178                                                 tmp_req->isCanceled = true;
1179                                         return MS_MEDIA_ERR_NONE;
1180                                 }
1181                         }
1182                         thumb_err("request_queue is NULL");
1183                         return MS_MEDIA_ERR_INTERNAL;
1184                 }
1185                 req_len = g_queue_get_length(g_manage_queue);
1186                 for (i = 0; i < req_len; i++) {
1187                         tmp_req = g_queue_pop_tail(g_manage_queue);
1188                         if (tmp_req) {
1189                                 SAFE_FREE(tmp_req->path);
1190                                 SAFE_FREE(tmp_req->userData);
1191                                 SAFE_FREE(tmp_req);
1192                                 tmp_req = NULL;
1193                         }
1194                 }
1195                 g_queue_free(g_manage_queue);
1196                 g_manage_queue = NULL;
1197                 if (g_request_queue != NULL) {
1198                         /* Check request queue */
1199                         len = g_queue_get_length(g_request_queue);
1200                         if (len > 0) {
1201                                 tmp_req = g_queue_peek_head(g_request_queue);
1202                                 if (tmp_req != NULL)
1203                                         tmp_req->isCanceled = true;
1204                         }
1205                 }
1206         }
1207
1208         return MS_MEDIA_ERR_NONE;
1209 }