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