Apply tizen coding rule
[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 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         return MS_MEDIA_ERR_NONE;
328 }
329
330 bool __media_thumb_check_cancel(void)
331 {
332         thumbReq *req = NULL;
333         req = (thumbReq *)g_queue_peek_head(g_request_queue);
334
335         if (req == NULL) {
336                 return false;
337         } else {
338                 if (req->isCanceled)
339                         return false;
340                 else
341                         return true;
342         }
343 }
344
345 bool __media_thumb_check_cancel_for_raw(void)
346 {
347         thumbRawReq *req = NULL;
348         req = (thumbRawReq *)g_queue_peek_head(g_request_raw_queue);
349
350         if (req == NULL) {
351                 return false;
352         } else {
353                 if (req->isCanceled)
354                         return false;
355                 else
356                         return true;
357         }
358 }
359
360 int
361 _media_thumb_recv_msg(int sock, int header_size, thumbMsg *msg)
362 {
363         int recv_msg_len = 0;
364         int remain_size = 0;
365         int block_size = THUMB_SOCK_BLOCK_SIZE;
366         int recv_block = 0;
367         unsigned char *buf = NULL;
368         unsigned char *block_buf = NULL;
369
370         THUMB_MALLOC(buf, header_size);
371         THUMB_MALLOC(block_buf, THUMB_SOCK_BLOCK_SIZE);
372         if (buf == NULL || block_buf == NULL) {
373                 thumb_err("memory allocation failed");
374                 SAFE_FREE(buf);
375                 SAFE_FREE(block_buf);
376                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
377         }
378
379         if ((recv_msg_len = recv(sock, buf, header_size, 0)) <= 0) {
380                 thumb_stderror("recv failed");
381                 SAFE_FREE(buf);
382                 SAFE_FREE(block_buf);
383                 return _media_thumb_get_error();
384         }
385
386         memcpy(msg, buf, header_size);
387         //thumb_dbg("origin_path_size : %d, dest_path_size : %d, thumb_size : %d", msg->origin_path_size, msg->dest_path_size, msg->thumb_size);
388
389         SAFE_FREE(buf);
390         if (msg->origin_path_size < 0 || msg->dest_path_size < 0 || msg->thumb_size < 0) {
391                 thumb_err("recv data is wrong");
392                 SAFE_FREE(block_buf);
393                 return MS_MEDIA_ERR_SOCKET_RECEIVE;
394         }
395
396         remain_size = msg->origin_path_size + msg->dest_path_size + msg->thumb_size;
397         THUMB_MALLOC(buf, remain_size);
398         if (buf == NULL) {
399                 SAFE_FREE(block_buf);
400                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
401         }
402
403         while (remain_size > 0) {
404                 if (remain_size < THUMB_SOCK_BLOCK_SIZE) {
405                         block_size = remain_size;
406                 }
407                 if ((recv_msg_len = recv(sock, block_buf, block_size, 0)) < 0) {
408                         thumb_stderror("recv failed");
409                         SAFE_FREE(buf);
410                         SAFE_FREE(block_buf);
411                         return _media_thumb_get_error();
412                 }
413                 memcpy(buf+recv_block, block_buf, block_size);
414                 recv_block += block_size;
415                 remain_size -= block_size;
416         }
417
418         strncpy(msg->org_path, (char *)buf, msg->origin_path_size);
419         strncpy(msg->dst_path, (char *)buf + msg->origin_path_size, msg->dest_path_size);
420
421         SAFE_FREE(msg->thumb_data);
422         if (msg->thumb_size > 0) {
423                 THUMB_MALLOC(msg->thumb_data, msg->thumb_size);
424                 if (msg->thumb_data != NULL) {
425                         memcpy(msg->thumb_data, buf + msg->origin_path_size + msg->dest_path_size, msg->thumb_size);
426                 } else {
427                         SAFE_FREE(buf);
428                         SAFE_FREE(block_buf);
429
430                         return MS_MEDIA_ERR_OUT_OF_MEMORY;
431                 }
432         }
433
434         SAFE_FREE(buf);
435         SAFE_FREE(block_buf);
436
437         return MS_MEDIA_ERR_NONE;
438 }
439
440 int
441 _media_thumb_recv_udp_msg(int sock, int header_size, thumbMsg *msg, struct sockaddr_un *from_addr, unsigned int *from_size)
442 {
443         int recv_msg_len = 0;
444         unsigned int from_addr_size = sizeof(struct sockaddr_un);
445         unsigned char *buf = NULL;
446
447         THUMB_MALLOC(buf, sizeof(thumbMsg));
448         if (buf == NULL) {
449                 thumb_err("memory allocation failed");
450                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
451         }
452
453         if ((recv_msg_len = recvfrom(sock, buf, sizeof(thumbMsg), 0, (struct sockaddr *)from_addr, &from_addr_size)) < 0) {
454                 thumb_stderror("recvform failed");
455                 SAFE_FREE(buf);
456                 return _media_thumb_get_error();
457         }
458
459         memcpy(msg, buf, header_size);
460
461         if (msg->origin_path_size <= 0 || msg->origin_path_size > MAX_PATH_SIZE) {
462                 SAFE_FREE(buf);
463                 thumb_err("msg->origin_path_size is invalid %d", msg->origin_path_size);
464                 return MS_MEDIA_ERR_INVALID_PARAMETER;
465         }
466
467         strncpy(msg->org_path, (char*)buf + header_size, msg->origin_path_size);
468
469         if (msg->dest_path_size <= 0 || msg->dest_path_size > MAX_PATH_SIZE) {
470                 SAFE_FREE(buf);
471                 thumb_err("msg->origin_path_size is invalid %d", msg->dest_path_size);
472                 return MS_MEDIA_ERR_INVALID_PARAMETER;
473         }
474
475         strncpy(msg->dst_path, (char*)buf + header_size + msg->origin_path_size, msg->dest_path_size);
476
477         SAFE_FREE(buf);
478         *from_size = from_addr_size;
479
480         return MS_MEDIA_ERR_NONE;
481 }
482
483 int
484 _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
485 {
486         if (req_msg == NULL || buf == NULL) {
487                 return MS_MEDIA_ERR_INVALID_PARAMETER;
488         }
489
490         int org_path_len = 0;
491         int dst_path_len = 0;
492         int thumb_data_len = 0;
493         int size = 0;
494         int header_size = 0;
495
496         header_size = sizeof(thumbMsg) -(MAX_FILEPATH_LEN * 2) - sizeof(unsigned char *);
497         org_path_len = req_msg->origin_path_size;
498         dst_path_len = req_msg->dest_path_size;
499         thumb_data_len = req_msg->thumb_size;
500
501         //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);
502
503         size = header_size + org_path_len + dst_path_len + thumb_data_len;
504         THUMB_MALLOC(*buf, size);
505         if (*buf == NULL) {
506                 *buf_size = 0;
507                 return 0;
508         }
509         memcpy(*buf, req_msg, header_size);
510         memcpy((*buf)+header_size, req_msg->org_path, org_path_len);
511         memcpy((*buf)+header_size + org_path_len, req_msg->dst_path, dst_path_len);
512         if (thumb_data_len > 0)
513                 memcpy((*buf)+header_size + org_path_len + dst_path_len, req_msg->thumb_data, thumb_data_len);
514
515         *buf_size = size;
516
517         return MS_MEDIA_ERR_NONE;
518 }
519
520 int
521 _media_thumb_request(int msg_type, const char *origin_path, char *thumb_path, int max_length, media_thumb_info *thumb_info, uid_t uid)
522 {
523         int sock = -1;
524         struct sockaddr_un serv_addr;
525         ms_sock_info_s sock_info;
526         int recv_str_len = 0;
527         int err = MS_MEDIA_ERR_NONE;
528         int pid;
529         sock_info.port = MS_THUMB_CREATOR_PORT;
530
531         err = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
532         if (err != MS_MEDIA_ERR_NONE) {
533                 thumb_err("ms_ipc_create_client_socket failed");
534                 return err;
535         }
536
537         memset(&serv_addr, 0, sizeof(serv_addr));
538         sock = sock_info.sock_fd;
539         serv_addr.sun_family = AF_UNIX;
540         strncpy(serv_addr.sun_path, THUMB_IPC_PATH, strlen(THUMB_IPC_PATH));
541
542         /* Connecting to the thumbnail server */
543         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
544                 thumb_stderror("connect");
545                 ms_ipc_delete_client_socket(&sock_info);
546                 return MS_MEDIA_ERR_SOCKET_CONN;
547         }
548
549         thumbMsg req_msg;
550         thumbMsg recv_msg;
551
552         memset((void *)&req_msg, 0, sizeof(thumbMsg));
553         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
554
555         /* Get PID of client*/
556         pid = getpid();
557         req_msg.pid = pid;
558
559         /* Set requset message */
560         req_msg.msg_type = msg_type;
561         req_msg.uid = uid;
562         strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
563         req_msg.org_path[strlen(req_msg.org_path)] = '\0';
564
565         if (msg_type == THUMB_REQUEST_SAVE_FILE) {
566                 strncpy(req_msg.dst_path, thumb_path, sizeof(req_msg.dst_path));
567                 req_msg.dst_path[strlen(req_msg.dst_path)] = '\0';
568         }
569
570         req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
571         req_msg.dest_path_size = strlen(req_msg.dst_path) + 1;
572         req_msg.thumb_size = 0;
573
574         if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
575                 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
576                 ms_ipc_delete_client_socket(&sock_info);
577                 return MS_MEDIA_ERR_INVALID_PARAMETER;
578         }
579
580         unsigned char *buf = NULL;
581         int buf_size = 0;
582         int header_size = 0;
583
584         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
585         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
586
587         if (send(sock, buf, buf_size, 0) != buf_size) {
588                 thumb_err("sendto failed: %d", errno);
589                 SAFE_FREE(buf);
590                 ms_ipc_delete_client_socket(&sock_info);
591                 return MS_MEDIA_ERR_SOCKET_SEND;
592         }
593
594         thumb_dbg("Sending msg to thumbnail daemon is successful");
595
596         SAFE_FREE(buf);
597
598         if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
599                 thumb_err("_media_thumb_recv_msg failed ");
600                 ms_ipc_delete_client_socket(&sock_info);
601                 return err;
602         }
603
604         recv_str_len = strlen(recv_msg.org_path);
605         thumb_dbg_slog("recv %s(%d) from thumb daemon is successful", recv_msg.org_path, recv_str_len);
606
607         ms_ipc_delete_client_socket(&sock_info);
608
609         if (recv_str_len > max_length) {
610                 thumb_err("user buffer is too small. Output's length is %d", recv_str_len);
611                 SAFE_FREE(recv_msg.thumb_data);
612                 return MS_MEDIA_ERR_INVALID_PARAMETER;
613         }
614
615         if (recv_msg.status == THUMB_FAIL) {
616                 thumb_err("Failed to make thumbnail");
617                 SAFE_FREE(recv_msg.thumb_data);
618                 return MS_MEDIA_ERR_INVALID_PARAMETER;
619         }
620
621         if (msg_type != THUMB_REQUEST_SAVE_FILE) {
622                 strncpy(thumb_path, recv_msg.dst_path, max_length);
623         }
624
625         thumb_info->origin_width = recv_msg.origin_width;
626         thumb_info->origin_height = recv_msg.origin_height;
627
628         SAFE_FREE(recv_msg.thumb_data);
629
630         return MS_MEDIA_ERR_NONE;
631 }
632
633 gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
634 {
635         thumbMsg recv_msg;
636         int header_size = 0;
637         int sock = 0;
638         int err = MS_MEDIA_ERR_NONE;
639
640         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
641         sock = g_io_channel_unix_get_fd(src);
642
643         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
644
645         thumb_err("_media_thumb_write_socket socket : %d", sock);
646
647         if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
648                 thumb_err("_media_thumb_recv_msg failed ");
649                 if (recv_msg.origin_path_size > 0) {
650                         __media_thumb_pop_req_queue(recv_msg.org_path);
651                 } else {
652                         thumb_err("origin path size is wrong.");
653                 }
654
655                 return FALSE;
656         }
657
658         //thumb_dbg("Completed..%s", recv_msg.org_path);
659
660         if (recv_msg.status == THUMB_FAIL) {
661                 thumb_err("Failed to make thumbnail");
662                 err = MS_MEDIA_ERR_INTERNAL;
663         }
664
665         if (__media_thumb_check_cancel()) {
666                 if (data) {
667                         thumbUserData* cb = (thumbUserData*)data;
668                         if (cb->func != NULL)
669                                 cb->func(err, recv_msg.dst_path, cb->user_data);
670                 }
671         }
672
673         __media_thumb_pop_req_queue(recv_msg.org_path);
674
675         thumb_dbg("Done");
676
677         return FALSE;
678 }
679
680 gboolean _media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
681 {
682         thumbMsg recv_msg;
683         int header_size = 0;
684         int sock = 0;
685         int len = -1;
686         int err = MS_MEDIA_ERR_NONE;
687
688         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
689         sock = g_io_channel_unix_get_fd(src);
690
691         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
692
693         thumb_err("_media_thumb_write_socket socket : %d", sock);
694
695         if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
696                 thumb_err("_media_thumb_recv_msg failed ");
697                 if (recv_msg.request_id > 0) {
698                         __media_thumb_pop_raw_data_req_queue(recv_msg.request_id, TRUE);
699                 } else {
700                         thumb_err("origin path size is wrong.");
701                 }
702
703                 return FALSE;
704         }
705
706         g_io_channel_shutdown(src, TRUE, NULL);
707         g_io_channel_unref(src);
708
709         if (recv_msg.status == THUMB_FAIL) {
710                 thumb_err("Failed to make thumbnail");
711                 err = MS_MEDIA_ERR_INTERNAL;
712         }
713
714         if (__media_thumb_check_cancel_for_raw()) {
715                 if (data) {
716                         thumbRawUserData* cb = (thumbRawUserData*)data;
717                         if (cb->func != NULL)
718                                 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);
719                 }
720         }
721
722         __media_thumb_pop_raw_data_req_queue(recv_msg.request_id, FALSE);
723
724         thumb_dbg("Done");
725
726         SAFE_FREE(recv_msg.thumb_data);
727
728         /* Check manage queue */
729         if (g_manage_raw_queue) {
730                 len = g_queue_get_length(g_manage_raw_queue);
731
732                 if (len > 0) {
733                         _media_thumb_raw_data_send_request();
734                 } else {
735                         g_queue_free(g_manage_raw_queue);
736                         g_manage_raw_queue = NULL;
737                 }
738         }
739
740         return FALSE;
741 }
742
743 int _media_thumb_send_request()
744 {
745         int err = MS_MEDIA_ERR_NONE;
746         int sock = -1;
747         struct sockaddr_un serv_addr;
748         ms_sock_info_s sock_info;
749         thumbReq *req_manager = NULL;
750         int pid;
751         sock_info.port = MS_THUMB_CREATOR_PORT;
752
753         err = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
754         if (err != MS_MEDIA_ERR_NONE) {
755                 thumb_err("ms_ipc_create_client_socket failed");
756                 return err;
757         }
758
759         memset(&serv_addr, 0, sizeof(serv_addr));
760         sock = sock_info.sock_fd;
761         serv_addr.sun_family = AF_UNIX;
762         strncpy(serv_addr.sun_path, THUMB_IPC_PATH, strlen(THUMB_IPC_PATH));
763
764         GIOChannel *channel = NULL;
765         channel = g_io_channel_unix_new(sock);
766         int source_id = -1;
767
768         /* Connecting to the thumbnail server */
769         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
770                 thumb_stderror("connect");
771                 g_io_channel_shutdown(channel, TRUE, NULL);
772                 g_io_channel_unref(channel);
773                 ms_ipc_delete_client_socket(&sock_info);
774                 return MS_MEDIA_ERR_SOCKET_CONN;
775         }
776
777         req_manager = (thumbReq *)g_queue_pop_head(g_manage_queue);
778
779         if (req_manager == NULL) {
780                 thumb_err("queue pop fail");
781                 g_io_channel_shutdown(channel, TRUE, NULL);
782                 g_io_channel_unref(channel);
783                 ms_ipc_delete_client_socket(&sock_info);
784                 return MS_MEDIA_ERR_INVALID_PARAMETER;
785         }
786
787         GSource *source = NULL;
788         source = g_io_create_watch(channel, G_IO_IN);
789         g_source_set_callback(source, (GSourceFunc)_media_thumb_write_socket, req_manager->userData, NULL);
790         source_id = g_source_attach(source, g_main_context_get_thread_default());
791
792         thumbMsg req_msg;
793
794         memset((void *)&req_msg, 0, sizeof(thumbMsg));
795
796         pid = getpid();
797         req_msg.pid = pid;
798         req_msg.msg_type = req_manager->msg_type;
799         req_msg.request_id = 0;
800         req_msg.uid = req_manager->uid;
801         strncpy(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
802         req_msg.org_path[strlen(req_msg.org_path)] = '\0';
803         req_msg.dst_path[0] = '\0';
804         req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
805         req_msg.dest_path_size = 1;
806         req_msg.thumb_size = 0;
807
808         if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
809                 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
810                 g_io_channel_shutdown(channel, TRUE, NULL);
811                 g_io_channel_unref(channel);
812                 ms_ipc_delete_client_socket(&sock_info);
813                 return MS_MEDIA_ERR_INVALID_PARAMETER;
814         }
815
816         unsigned char *buf = NULL;
817         int buf_size = 0;
818         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
819
820         if (send(sock, buf, buf_size, 0) != buf_size) {
821                 thumb_err("sendto failed: %d", errno);
822                 SAFE_FREE(buf);
823                 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
824                 g_io_channel_shutdown(channel, TRUE, NULL);
825                 g_io_channel_unref(channel);
826                 ms_ipc_delete_client_socket(&sock_info);
827                 return MS_MEDIA_ERR_SOCKET_SEND;
828         }
829
830         SAFE_FREE(buf);
831         thumb_dbg("Sending msg to thumbnail daemon is successful");
832
833
834         if (req_manager->msg_type == THUMB_REQUEST_DB_INSERT) {
835                 if (g_request_queue == NULL) {
836                         g_request_queue = g_queue_new();
837                 }
838
839                 thumbReq *thumb_req = NULL;
840                 THUMB_MALLOC(thumb_req, sizeof(thumbReq));
841                 if (thumb_req == NULL) {
842                         thumb_err("Failed to create request element");
843                         return MS_MEDIA_ERR_INVALID_PARAMETER;
844                 }
845
846                 thumb_req->channel = channel;
847                 thumb_req->path = strdup(req_manager->path);
848                 thumb_req->source_id = source_id;
849                 thumb_req->userData = req_manager->userData;
850
851                 g_queue_push_tail(g_request_queue, (gpointer)thumb_req);
852         }
853
854         return err;
855 }
856
857 int _media_thumb_raw_data_send_request()
858 {
859         int err = MS_MEDIA_ERR_NONE;
860         int sock = -1;
861         struct sockaddr_un serv_addr;
862         ms_sock_info_s sock_info;
863         thumbRawReq *req_manager = NULL;
864         int pid;
865         sock_info.port = MS_THUMB_CREATOR_PORT;
866
867         err = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
868         if (err != MS_MEDIA_ERR_NONE) {
869                 thumb_err("ms_ipc_create_client_socket failed");
870                 return err;
871         }
872
873         memset(&serv_addr, 0, sizeof(serv_addr));
874         sock = sock_info.sock_fd;
875         serv_addr.sun_family = AF_UNIX;
876         strncpy(serv_addr.sun_path, THUMB_IPC_PATH, strlen(THUMB_IPC_PATH));
877
878         GIOChannel *channel = NULL;
879         channel = g_io_channel_unix_new(sock);
880         int source_id = -1;
881
882         /* Connecting to the thumbnail server */
883         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
884                 thumb_stderror("connect error");
885                 g_io_channel_shutdown(channel, TRUE, NULL);
886                 g_io_channel_unref(channel);
887                 ms_ipc_delete_client_socket(&sock_info);
888                 return MS_MEDIA_ERR_SOCKET_CONN;
889         }
890
891         req_manager = (thumbRawReq *)g_queue_pop_head(g_manage_raw_queue);
892
893         if (req_manager == NULL) {
894                 thumb_err("queue pop fail");
895                 g_io_channel_shutdown(channel, TRUE, NULL);
896                 g_io_channel_unref(channel);
897                 ms_ipc_delete_client_socket(&sock_info);
898                 return MS_MEDIA_ERR_INVALID_PARAMETER;
899         }
900
901         GSource *source = NULL;
902         source = g_io_create_watch(channel, G_IO_IN);
903         g_source_set_callback(source, (GSourceFunc)_media_thumb_raw_data_write_socket, req_manager->userData, NULL);
904         source_id = g_source_attach(source, g_main_context_get_thread_default());
905
906         thumbMsg req_msg;
907         memset((void *)&req_msg, 0, sizeof(thumbMsg));
908
909         pid = getpid();
910         req_msg.pid = pid;
911         req_msg.msg_type = req_manager->msg_type;
912         req_msg.request_id = req_manager->request_id;
913         req_msg.thumb_width = req_manager->width;
914         req_msg.thumb_height = req_manager->height;
915         req_msg.uid = req_manager->uid;
916
917         strncpy(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
918         req_msg.org_path[strlen(req_msg.org_path)] = '\0';
919         req_msg.dst_path[0] = '\0';
920
921         req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
922         req_msg.dest_path_size = 1;
923         req_msg.thumb_size = 0;
924
925         if (req_msg.origin_path_size > MAX_PATH_SIZE) {
926                 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
927                 g_io_channel_shutdown(channel, TRUE, NULL);
928                 g_io_channel_unref(channel);
929                 return MS_MEDIA_ERR_INVALID_PARAMETER;
930         }
931
932         unsigned char *buf = NULL;
933         int buf_size = 0;
934         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
935
936         if (send(sock, buf, buf_size, 0) != buf_size) {
937                 thumb_err("sendto failed: %d", errno);
938                 SAFE_FREE(buf);
939                 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
940                 g_io_channel_shutdown(channel, TRUE, NULL);
941                 g_io_channel_unref(channel);
942                 return MS_MEDIA_ERR_SOCKET_SEND;
943         }
944
945         SAFE_FREE(buf);
946
947         if (req_manager->msg_type == THUMB_REQUEST_RAW_DATA) {
948                 if (g_request_raw_queue == NULL) {
949                         g_request_raw_queue = g_queue_new();
950                 }
951                 thumbRawReq *thumb_req = NULL;
952                 THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
953                 if (thumb_req == NULL) {
954                         thumb_err("Failed to create request element");
955                         return MS_MEDIA_ERR_INVALID_PARAMETER;
956                 }
957                 thumb_req->channel = channel;
958                 thumb_req->request_id = req_manager->request_id;
959                 thumb_req->source_id = source_id;
960                 thumb_req->userData = req_manager->userData;
961
962                 g_queue_push_tail(g_request_raw_queue, (gpointer)thumb_req);
963         }
964         return MS_MEDIA_ERR_NONE;
965 }
966
967 int _media_thumb_request_async(int msg_type, const char *origin_path, thumbUserData *userData, uid_t uid)
968 {
969         int err = MS_MEDIA_ERR_NONE;
970
971         if (g_manage_queue == NULL) {
972                 if (msg_type == THUMB_REQUEST_CANCEL_MEDIA)
973                         return MS_MEDIA_ERR_INTERNAL;
974
975                 g_manage_queue = g_queue_new();
976
977                 thumbReq *thumb_req = NULL;
978                 THUMB_MALLOC(thumb_req, sizeof(thumbReq));
979                 if (thumb_req == NULL) {
980                         thumb_err("Failed to create request element");
981                         return MS_MEDIA_ERR_INVALID_PARAMETER;
982                 }
983
984                 thumb_req->msg_type = msg_type;
985                 thumb_req->path = strdup(origin_path);
986                 thumb_req->userData = userData;
987                 thumb_req->isCanceled = false;
988                 thumb_req->uid = uid;
989
990                 g_queue_push_tail(g_manage_queue, (gpointer)thumb_req);
991
992                 /* directly request at first time */
993                 err = _media_thumb_send_request();
994
995         } else {
996                 if (msg_type != THUMB_REQUEST_CANCEL_MEDIA) {
997                         /* Enqueue */
998                         if ((msg_type == THUMB_REQUEST_DB_INSERT) && (__media_thumb_check_req_queue(origin_path) < 0)) {
999                                 thumb_err("duplicated request");
1000                                 return MS_MEDIA_ERR_THUMB_DUPLICATED_REQUEST;
1001                         }
1002
1003                         thumbReq *thumb_req = NULL;
1004                         THUMB_MALLOC(thumb_req, sizeof(thumbReq));
1005                         if (thumb_req == NULL) {
1006                                 thumb_err("Failed to create request element");
1007                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1008                         }
1009
1010                         thumb_req->msg_type = msg_type;
1011                         thumb_req->path = strdup(origin_path);
1012                         thumb_req->userData = userData;
1013                         thumb_req->isCanceled = false;
1014                         thumb_req->uid = uid;
1015
1016                         g_queue_push_tail(g_manage_queue, (gpointer)thumb_req);
1017                 } else {
1018                         /* Dequeue */
1019                         err = __media_thumb_pop_manage_queue(origin_path);
1020                 }
1021         }
1022
1023         return err;
1024 }
1025
1026 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)
1027 {
1028         int err = MS_MEDIA_ERR_NONE;
1029
1030         if (g_manage_raw_queue == NULL) {
1031                 if (msg_type == THUMB_REQUEST_CANCEL_RAW_DATA)
1032                         return MS_MEDIA_ERR_INTERNAL;
1033
1034                 g_manage_raw_queue = g_queue_new();
1035
1036                 thumbRawReq *thumb_req = NULL;
1037                 THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
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->request_id = request_id;
1045                 thumb_req->path = strdup(origin_path);
1046                 thumb_req->width = width;
1047                 thumb_req->height = height;
1048                 thumb_req->userData = userData;
1049                 thumb_req->isCanceled = false;
1050                 thumb_req->uid = uid;
1051
1052                 g_queue_push_tail(g_manage_raw_queue, (gpointer)thumb_req);
1053
1054                 /* directly request at first time */
1055                 err = _media_thumb_raw_data_send_request();
1056
1057         } else {
1058                 if (msg_type != THUMB_REQUEST_CANCEL_RAW_DATA) {
1059                         /* Enqueue */
1060                         thumbRawReq *thumb_req = NULL;
1061                         THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
1062                         if (thumb_req == NULL) {
1063                                 thumb_err("Failed to create request element");
1064                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1065                         }
1066
1067                         thumb_req->msg_type = msg_type;
1068                         thumb_req->request_id = request_id;
1069                         thumb_req->path = strdup(origin_path);
1070                         thumb_req->width = width;
1071                         thumb_req->height = height;
1072                         thumb_req->userData = userData;
1073                         thumb_req->isCanceled = false;
1074                         thumb_req->uid = uid;
1075
1076                         g_queue_push_tail(g_manage_raw_queue, (gpointer)thumb_req);
1077
1078                 } else {
1079                         /* Dequeue */
1080                         err = __media_thumb_pop_raw_data_manage_queue(request_id);
1081                 }
1082         }
1083
1084         return err;
1085 }
1086
1087 int _media_thumb_request_cancel_all(bool isRaw)
1088 {
1089         int i;
1090         int req_len = -1;
1091         int len = 0;
1092
1093         if (isRaw) {
1094                 thumbRawReq *tmp_raw_req = NULL;
1095                 if (g_manage_raw_queue == NULL) {
1096                         thumb_err("manage_queue is NULL");
1097                         if (g_request_raw_queue != NULL) {
1098                                 /* Check request queue */
1099                                 len = g_queue_get_length(g_request_raw_queue);
1100                                 if (len > 0) {
1101                                         tmp_raw_req = g_queue_peek_head(g_request_raw_queue);
1102                                         if (tmp_raw_req != NULL)
1103                                                 tmp_raw_req->isCanceled = true;
1104                                         return MS_MEDIA_ERR_NONE;
1105                                 }
1106                         }
1107                         thumb_err("request_queue is NULL");
1108                         return MS_MEDIA_ERR_INTERNAL;
1109                 }
1110                 req_len = g_queue_get_length(g_manage_raw_queue);
1111                 for (i = 0; i < req_len; i++) {
1112                         tmp_raw_req = g_queue_pop_tail(g_manage_raw_queue);
1113                         if (tmp_raw_req) {
1114                                 SAFE_FREE(tmp_raw_req->path);
1115                                 SAFE_FREE(tmp_raw_req->userData);
1116                                 SAFE_FREE(tmp_raw_req);
1117                                 tmp_raw_req = NULL;
1118                         }
1119                 }
1120                 g_queue_free(g_manage_raw_queue);
1121                 g_manage_raw_queue = NULL;
1122                 if (g_request_raw_queue != NULL) {
1123                         /* Check request queue */
1124                         len = g_queue_get_length(g_request_raw_queue);
1125                         if (len > 0) {
1126                                 tmp_raw_req = g_queue_peek_head(g_request_raw_queue);
1127                                 if (tmp_raw_req != NULL)
1128                                         tmp_raw_req->isCanceled = true;
1129                         }
1130                 }
1131         } else {
1132                 thumbReq *tmp_req = NULL;
1133                 if (g_manage_queue == NULL) {
1134                         thumb_err("manage_queue is NULL");
1135                         if (g_request_queue != NULL) {
1136                                 /* Check request queue */
1137                                 len = g_queue_get_length(g_request_queue);
1138                                 if (len > 0) {
1139                                         tmp_req = g_queue_peek_head(g_request_queue);
1140                                         if (tmp_req != NULL)
1141                                                 tmp_req->isCanceled = true;
1142                                         return MS_MEDIA_ERR_NONE;
1143                                 }
1144                         }
1145                         thumb_err("request_queue is NULL");
1146                         return MS_MEDIA_ERR_INTERNAL;
1147                 }
1148                 req_len = g_queue_get_length(g_manage_queue);
1149                 for (i = 0; i < req_len; i++) {
1150                         tmp_req = g_queue_pop_tail(g_manage_queue);
1151                         if (tmp_req) {
1152                                 SAFE_FREE(tmp_req->path);
1153                                 SAFE_FREE(tmp_req->userData);
1154                                 SAFE_FREE(tmp_req);
1155                                 tmp_req = NULL;
1156                         }
1157                 }
1158                 g_queue_free(g_manage_queue);
1159                 g_manage_queue = NULL;
1160                 if (g_request_queue != NULL) {
1161                         /* Check request queue */
1162                         len = g_queue_get_length(g_request_queue);
1163                         if (len > 0) {
1164                                 tmp_req = g_queue_peek_head(g_request_queue);
1165                                 if (tmp_req != NULL)
1166                                         tmp_req->isCanceled = true;
1167                         }
1168                 }
1169         }
1170
1171         return MS_MEDIA_ERR_NONE;
1172 }