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