Fix socket bind bug.
[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 GLOBAL_USER    0 //#define     tzplatform_getenv(TZ_GLOBAL) //TODO
35 #define THUMB_SOCK_BLOCK_SIZE 512
36
37 static __thread GQueue *g_request_queue = NULL;
38 typedef struct {
39         GIOChannel *channel;
40         char *path;
41         int source_id;
42         thumbUserData *userData;
43 } thumbReq;
44
45 typedef struct {
46         GIOChannel *channel;
47         int request_id;
48         int source_id;
49         thumbRawUserData *userData;
50 } thumbRawReq;
51
52
53
54 int _media_thumb_get_error()
55 {
56         if (errno == EWOULDBLOCK) {
57                 thumb_err("Timeout. Can't try any more");
58                 return MS_MEDIA_ERR_SOCKET_RECEIVE_TIMEOUT;
59         } else {
60                 thumb_err("recvfrom failed : %s", strerror(errno));
61                 return MS_MEDIA_ERR_SOCKET_RECEIVE;
62         }
63 }
64
65 int __media_thumb_pop_req_queue(const char *path, bool shutdown_channel)
66 {
67         int req_len = 0, i;
68
69         if (g_request_queue == NULL) return MS_MEDIA_ERR_INVALID_PARAMETER;
70         req_len = g_queue_get_length(g_request_queue);
71
72         if (req_len <= 0) {
73 //              thumb_dbg("There is no request in the queue");
74         } else {
75
76                 for (i = 0; i < req_len; i++) {
77                         thumbReq *req = NULL;
78                         req = (thumbReq *)g_queue_peek_nth(g_request_queue, i);
79                         if (req == NULL) continue;
80
81                         if (strncmp(path, req->path, strlen(path)) == 0) {
82                                 if (shutdown_channel) {
83                                         GSource *source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id);
84                                         if (source_id != NULL) {
85                                                 g_source_destroy(source_id);
86                                         } else {
87                                                 thumb_err("G_SOURCE_ID is NULL");
88                                         }
89
90                                         g_io_channel_shutdown(req->channel, TRUE, NULL);
91                                         g_io_channel_unref(req->channel);
92                                 }
93                                 g_queue_pop_nth(g_request_queue, i);
94
95                                 SAFE_FREE(req->path);
96                                 SAFE_FREE(req->userData);
97                                 SAFE_FREE(req);
98
99                                 break;
100                         }
101                 }
102                 if (i == req_len) {
103 //                      thumb_dbg("There's no %s in the queue", path);
104                 }
105         }
106
107         return MS_MEDIA_ERR_NONE;
108 }
109
110 int __media_thumb_pop_raw_data_req_queue(int request_id, bool shutdown_channel)
111 {
112         int req_len = 0, i;
113
114         if (g_request_queue == NULL) return MS_MEDIA_ERR_INVALID_PARAMETER;
115         req_len = g_queue_get_length(g_request_queue);
116
117         if (req_len <= 0) {
118 //              thumb_dbg("There is no request in the queue");
119         } else {
120
121                 for (i = 0; i < req_len; i++) {
122                         thumbRawReq *req = NULL;
123                         req = (thumbRawReq *)g_queue_peek_nth(g_request_queue, i);
124                         if (req == NULL) continue;
125
126                         if (request_id == req->request_id) {
127                                 if (shutdown_channel) {
128                                         GSource *source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id);
129                                         if (source_id != NULL) {
130                                                 g_source_destroy(source_id);
131                                         } else {
132                                                 thumb_err("G_SOURCE_ID is NULL");
133                                         }
134
135                                         g_io_channel_shutdown(req->channel, TRUE, NULL);
136                                         g_io_channel_unref(req->channel);
137                                 }
138                                 g_queue_pop_nth(g_request_queue, i);
139
140                                 SAFE_FREE(req->userData);
141                                 SAFE_FREE(req);
142
143                                 break;
144                         }
145                 }
146                 if (i == req_len) {
147 //                      thumb_dbg("There's no %s in the queue", path);
148                 }
149         }
150
151         return MS_MEDIA_ERR_NONE;
152 }
153
154 int __media_thumb_check_req_queue(const char *path)
155 {
156         int req_len = 0, i;
157
158         req_len = g_queue_get_length(g_request_queue);
159
160 //      thumb_dbg("Queue length : %d", req_len);
161 //      thumb_dbg("Queue path : %s", path);
162
163         if (req_len <= 0) {
164 //              thumb_dbg("There is no request in the queue");
165         } else {
166
167                 for (i = 0; i < req_len; i++) {
168                         thumbReq *req = NULL;
169                         req = (thumbReq *)g_queue_peek_nth(g_request_queue, i);
170                         if (req == NULL) continue;
171
172                         if (strncmp(path, req->path, strlen(path)) == 0) {
173                                 //thumb_dbg("Same Request - %s", path);
174                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
175
176                                 break;
177                         }
178                 }
179         }
180
181         return MS_MEDIA_ERR_NONE;
182 }
183
184 int
185 _media_thumb_recv_msg(int sock, int header_size, thumbMsg *msg)
186 {
187         int recv_msg_len = 0;
188         int remain_size = 0;
189         int block_size = THUMB_SOCK_BLOCK_SIZE;
190         int recv_block = 0;
191         unsigned char *buf = NULL;
192         unsigned char *block_buf = NULL;
193
194         buf = (unsigned char*)malloc(header_size * sizeof(unsigned char));
195         block_buf = (unsigned char*)malloc(THUMB_SOCK_BLOCK_SIZE * sizeof(unsigned char));
196         if (buf == NULL || block_buf == NULL) {
197                 thumb_err("memory allocation failed");
198                 SAFE_FREE(buf);
199                 SAFE_FREE(block_buf);
200                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
201         }
202
203         if ((recv_msg_len = recv(sock, buf, header_size, 0)) <= 0) {
204                 thumb_stderror("recv failed");
205                 SAFE_FREE(buf);
206                 return _media_thumb_get_error();
207         }
208
209         memcpy(msg, buf, header_size);
210         //thumb_dbg("origin_path_size : %d, dest_path_size : %d, thumb_size : %d", msg->origin_path_size, msg->dest_path_size, msg->thumb_size);
211
212         SAFE_FREE(buf);
213
214         remain_size = msg->origin_path_size + msg->dest_path_size + msg->thumb_size + 2;
215         buf = malloc(remain_size * sizeof(unsigned char));
216         memset(buf, 0, remain_size * sizeof(unsigned char));
217
218         while(remain_size > 0) {
219                 if(remain_size < THUMB_SOCK_BLOCK_SIZE) {
220                         block_size = remain_size;
221                 }
222                 if ((recv_msg_len = recv(sock, block_buf, block_size, 0)) < 0) {
223                         thumb_err("recv failed : %s", strerror(errno));
224                         SAFE_FREE(buf);
225                         SAFE_FREE(block_buf);
226                         return _media_thumb_get_error();
227                 }
228                 memcpy(buf+recv_block, block_buf, block_size);
229                 recv_block += block_size;
230                 remain_size -= block_size;
231         }
232
233         strncpy(msg->org_path, (char *)buf, msg->origin_path_size);
234         strncpy(msg->dst_path, (char *)buf + msg->origin_path_size + 1, msg->dest_path_size);
235         SAFE_FREE(msg->thumb_data);
236         msg->thumb_data = malloc(msg->thumb_size);
237         memset(msg->thumb_data, 0, msg->thumb_size);
238         memcpy(msg->thumb_data, buf + msg->origin_path_size + msg->dest_path_size + 2, msg->thumb_size);
239
240         SAFE_FREE(buf);
241         SAFE_FREE(block_buf);
242
243         return MS_MEDIA_ERR_NONE;
244 }
245
246 int
247 _media_thumb_recv_udp_msg(int sock, int header_size, thumbMsg *msg, struct sockaddr_un *from_addr, unsigned int *from_size)
248 {
249         int recv_msg_len = 0;
250         unsigned int from_addr_size = sizeof(struct sockaddr_un);
251         unsigned char *buf = NULL;
252
253         buf = (unsigned char*)malloc(sizeof(thumbMsg));
254
255         if ((recv_msg_len = recvfrom(sock, buf, sizeof(thumbMsg), 0, (struct sockaddr *)from_addr, &from_addr_size)) < 0) {
256                 thumb_stderror("recvform failed");
257                 SAFE_FREE(buf);
258                 return _media_thumb_get_error();
259         }
260
261         memcpy(msg, buf, header_size);
262
263         if (msg->origin_path_size <= 0  || msg->origin_path_size > MAX_PATH_SIZE) {
264                 SAFE_FREE(buf);
265                 thumb_err("msg->origin_path_size is invalid %d", msg->origin_path_size );
266                 return MS_MEDIA_ERR_INVALID_PARAMETER;
267         }
268
269         strncpy(msg->org_path, (char*)buf + header_size, msg->origin_path_size);
270
271         if (msg->dest_path_size <= 0  || msg->dest_path_size > MAX_PATH_SIZE) {
272                 SAFE_FREE(buf);
273                 thumb_err("msg->origin_path_size is invalid %d", msg->dest_path_size );
274                 return MS_MEDIA_ERR_INVALID_PARAMETER;
275         }
276
277         strncpy(msg->dst_path, (char*)buf + header_size + msg->origin_path_size, msg->dest_path_size);
278
279         SAFE_FREE(buf);
280         *from_size = from_addr_size;
281
282         return MS_MEDIA_ERR_NONE;
283 }
284
285 int
286 _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
287 {
288         if (req_msg == NULL || buf == NULL) {
289                 return MS_MEDIA_ERR_INVALID_PARAMETER;
290         }
291
292         int org_path_len = 0;
293         int dst_path_len = 0;
294         int thumb_data_len = 0;
295         int size = 0;
296         int header_size = 0;
297
298         header_size = sizeof(thumbMsg) -(MAX_FILEPATH_LEN * 2) - sizeof(unsigned char *);
299         org_path_len = req_msg->origin_path_size + 1;
300         dst_path_len = req_msg->dest_path_size + 1;
301         thumb_data_len = req_msg->thumb_size;
302
303         //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);
304
305         size = header_size + org_path_len + dst_path_len + thumb_data_len;
306         *buf = malloc(size);
307
308         if (*buf == NULL) {
309                 *buf_size = 0;
310                 return 0;
311         }
312         memcpy(*buf, req_msg, header_size);
313         memcpy((*buf)+header_size, req_msg->org_path, org_path_len);
314         memcpy((*buf)+header_size + org_path_len, req_msg->dst_path, dst_path_len);
315         memcpy((*buf)+header_size + org_path_len + dst_path_len, req_msg->thumb_data, thumb_data_len);
316
317         *buf_size = size;
318
319         return MS_MEDIA_ERR_NONE;
320 }
321
322 int
323 _media_thumb_set_buffer_for_response(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
324 {
325         if (req_msg == NULL || buf == NULL) {
326                 return MS_MEDIA_ERR_INVALID_PARAMETER;
327         }
328
329         int org_path_len = 0;
330         int dst_path_len = 0;
331         int thumb_data_len = 0;
332         int size = 0;
333         int header_size = 0;
334
335         header_size = sizeof(thumbMsg) -(MAX_FILEPATH_LEN * 2) - sizeof(unsigned char *);
336         org_path_len = req_msg->origin_path_size + 1;
337         dst_path_len = req_msg->dest_path_size + 1;
338         thumb_data_len = 1;
339
340         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);
341
342         size = header_size + org_path_len + dst_path_len + thumb_data_len;
343         *buf = malloc(size);
344
345         if (*buf == NULL) {
346                 *buf_size = 0;
347                 return 0;
348         }
349         memcpy(*buf, req_msg, header_size);
350         memcpy((*buf)+header_size, req_msg->org_path, org_path_len);
351         memcpy((*buf)+header_size + org_path_len, req_msg->dst_path, dst_path_len);
352         memcpy((*buf)+header_size + org_path_len + dst_path_len, req_msg->thumb_data, thumb_data_len);
353
354         *buf_size = size;
355
356         return MS_MEDIA_ERR_NONE;
357 }
358
359
360 int
361 _media_thumb_set_add_raw_data_buffer(thumbRawAddMsg *req_msg, unsigned char **buf, int *buf_size)
362 {
363         if (req_msg == NULL || buf == NULL) {
364                 return MS_MEDIA_ERR_INVALID_PARAMETER;
365         }
366         int thumb_len = 0;
367         int size = 0;
368         int header_size = 0;
369
370         header_size = sizeof(thumbRawAddMsg);
371         thumb_len = req_msg->thumb_size;
372
373         size = header_size + thumb_len;
374         *buf = malloc(size);
375         memcpy(*buf, req_msg, header_size);
376         memcpy((*buf)+header_size, req_msg->thumb_data, thumb_len);
377
378         *buf_size = size;
379
380         return MS_MEDIA_ERR_NONE;
381 }
382
383 int
384 _media_thumb_request(int msg_type, media_thumb_type thumb_type, const char *origin_path, char *thumb_path, int max_length, media_thumb_info *thumb_info, uid_t uid)
385 {
386         int sock = -1;
387         struct sockaddr_un serv_addr;
388         ms_sock_info_s sock_info;
389         int recv_str_len = 0;
390         int err = MS_MEDIA_ERR_NONE;
391         int pid;
392         sock_info.port = MS_THUMB_CREATOR_PORT;
393
394         err = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
395         if (err != MS_MEDIA_ERR_NONE) {
396                 thumb_err("ms_ipc_create_client_socket failed");
397                 return err;
398         }
399
400         memset(&serv_addr, 0, sizeof(serv_addr));
401         sock = sock_info.sock_fd;
402         serv_addr.sun_family = AF_UNIX;
403         strcpy(serv_addr.sun_path, "/var/run/media-server/media_ipc_thumbcreator.socket");
404
405         /* Connecting to the thumbnail server */
406         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
407                 thumb_stderror("connect");
408                 ms_ipc_delete_client_socket(&sock_info);
409                 return MS_MEDIA_ERR_SOCKET_CONN;
410         }
411
412         thumbMsg req_msg;
413         thumbMsg recv_msg;
414
415         memset((void *)&req_msg, 0, sizeof(thumbMsg));
416         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
417
418         /* Get PID of client*/
419         pid = getpid();
420         req_msg.pid = pid;
421
422         /* Set requset message */
423         req_msg.msg_type = msg_type;
424         req_msg.thumb_type = thumb_type;
425         req_msg.uid = uid;
426         strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
427         req_msg.org_path[strlen(req_msg.org_path)] = '\0';
428
429         if (msg_type == THUMB_REQUEST_SAVE_FILE) {
430                 strncpy(req_msg.dst_path, thumb_path, sizeof(req_msg.dst_path));
431                 req_msg.dst_path[strlen(req_msg.dst_path)] = '\0';
432         }
433
434         req_msg.thumb_data = (unsigned char *) "\0";
435
436         req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
437         req_msg.dest_path_size = strlen(req_msg.dst_path) + 1;
438         req_msg.thumb_size = 1;
439
440         if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
441                 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
442                 ms_ipc_delete_client_socket(&sock_info);
443                 return MS_MEDIA_ERR_INVALID_PARAMETER;
444         }
445
446         unsigned char *buf = NULL;
447         int buf_size = 0;
448         int header_size = 0;
449
450         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
451         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
452
453         if (send(sock, buf, buf_size, 0) != buf_size) {
454                 thumb_err("sendto failed: %d", errno);
455                 SAFE_FREE(buf);
456                 ms_ipc_delete_client_socket(&sock_info);
457                 return MS_MEDIA_ERR_SOCKET_SEND;
458         }
459
460         thumb_dbg("Sending msg to thumbnail daemon is successful");
461
462         SAFE_FREE(buf);
463
464         if(msg_type != THUMB_REQUEST_CANCEL_ALL_RAW_DATA && msg_type != THUMB_REQUEST_CANCEL_ALL) {             //No response..
465                 if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
466                         thumb_err("_media_thumb_recv_msg failed ");
467                         ms_ipc_delete_client_socket(&sock_info);
468                         return err;
469                 }
470
471                 recv_str_len = strlen(recv_msg.org_path);
472                 thumb_dbg_slog("recv %s(%d) from thumb daemon is successful", recv_msg.org_path, recv_str_len);
473
474                 ms_ipc_delete_client_socket(&sock_info);
475
476                 if (recv_str_len > max_length) {
477                         thumb_err("user buffer is too small. Output's length is %d", recv_str_len);
478                         return MS_MEDIA_ERR_INVALID_PARAMETER;
479                 }
480
481                 if (recv_msg.status == THUMB_FAIL) {
482                         thumb_err("Failed to make thumbnail");
483                         return MS_MEDIA_ERR_INVALID_PARAMETER;
484                 }
485
486                 if (msg_type != THUMB_REQUEST_SAVE_FILE) {
487                         strncpy(thumb_path, recv_msg.dst_path, max_length);
488                 }
489
490                 thumb_info->origin_width = recv_msg.origin_width;
491                 thumb_info->origin_height = recv_msg.origin_height;
492         }else {
493                 thumb_dbg("No response msg_type:[%d]", msg_type);
494         }
495         return MS_MEDIA_ERR_NONE;
496 }
497
498 gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
499 {
500         thumbMsg recv_msg;
501         int header_size = 0;
502         int sock = 0;
503         int err = MS_MEDIA_ERR_NONE;
504
505         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
506         sock = g_io_channel_unix_get_fd(src);
507
508         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
509
510         thumb_err("_media_thumb_write_socket socket : %d", sock);
511
512         if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
513                 thumb_err("_media_thumb_recv_msg failed ");
514                 if (recv_msg.origin_path_size > 0) {
515                         __media_thumb_pop_req_queue(recv_msg.org_path, TRUE);
516                 } else {
517                         thumb_err("origin path size is wrong.");
518                 }
519
520                 return FALSE;
521         }
522
523         g_io_channel_shutdown(src, TRUE, NULL);
524         g_io_channel_unref(src);
525         //thumb_dbg("Completed..%s", recv_msg.org_path);
526
527         if (recv_msg.status == THUMB_FAIL) {
528                 thumb_err("Failed to make thumbnail");
529                 err = MS_MEDIA_ERR_INTERNAL;
530         }
531
532         if (data) {
533                 thumbUserData* cb = (thumbUserData*)data;
534                 if (cb->func != NULL)
535                         cb->func(err, recv_msg.dst_path, cb->user_data);
536         }
537
538         __media_thumb_pop_req_queue(recv_msg.org_path, FALSE);
539
540         thumb_dbg("Done");
541         return FALSE;
542 }
543
544 gboolean _media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
545 {
546         thumbMsg recv_msg;
547         int header_size = 0;
548         int sock = 0;
549         int err = MS_MEDIA_ERR_NONE;
550
551         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
552         sock = g_io_channel_unix_get_fd(src);
553
554         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
555
556         thumb_err("_media_thumb_write_socket socket : %d", sock);
557
558         if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
559                 thumb_err("_media_thumb_recv_msg failed ");
560                 if (recv_msg.request_id > 0) {
561                         __media_thumb_pop_raw_data_req_queue(recv_msg.request_id, TRUE);
562                 } else {
563                         thumb_err("origin path size is wrong.");
564                 }
565
566                 return FALSE;
567         }
568
569         g_io_channel_shutdown(src, TRUE, NULL);
570         g_io_channel_unref(src);
571
572         if (recv_msg.status == THUMB_FAIL) {
573                 thumb_err("Failed to make thumbnail");
574                 err = MS_MEDIA_ERR_INTERNAL;
575         }
576
577         if (data) {
578                 thumbRawUserData* cb = (thumbRawUserData*)data;
579                 if (cb->func != NULL)
580                         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);
581         }
582
583         __media_thumb_pop_raw_data_req_queue(recv_msg.request_id, FALSE);
584
585         thumb_dbg("Done");
586
587         SAFE_FREE(recv_msg.thumb_data);
588
589         return FALSE;
590 }
591
592 int _media_thumb_request_async(int msg_type, media_thumb_type thumb_type, const char *origin_path, thumbUserData *userData, uid_t uid)
593 {
594         int err = MS_MEDIA_ERR_NONE;
595         int sock = -1;
596         struct sockaddr_un serv_addr;
597         ms_sock_info_s sock_info;
598         int pid;
599         sock_info.port = MS_THUMB_CREATOR_PORT;
600
601         if ((msg_type == THUMB_REQUEST_DB_INSERT) && (__media_thumb_check_req_queue(origin_path) < 0)) {
602                 return MS_MEDIA_ERR_THUMB_DUPLICATED_REQUEST;
603         }
604
605         err = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
606         if(err != MS_MEDIA_ERR_NONE)
607         {
608                 thumb_err("ms_ipc_create_client_socket failed");
609                 return err;
610         }
611
612         memset(&serv_addr, 0, sizeof(serv_addr));
613         sock = sock_info.sock_fd;
614         serv_addr.sun_family = AF_UNIX;
615         strcpy(serv_addr.sun_path, "/var/run/media-server/media_ipc_thumbcreator.socket");
616
617         GIOChannel *channel = NULL;
618         channel = g_io_channel_unix_new(sock);
619         int source_id = -1;
620
621         /* Connecting to the thumbnail server */
622         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
623                 thumb_stderror("connect");
624                 g_io_channel_shutdown(channel, TRUE, NULL);
625                 g_io_channel_unref(channel);
626                 return MS_MEDIA_ERR_SOCKET_CONN;
627         }
628
629         if (msg_type != THUMB_REQUEST_CANCEL_MEDIA) {
630                 //source_id = g_io_add_watch(channel, G_IO_IN, _media_thumb_write_socket, userData );
631
632                 /* Create new channel to watch udp socket */
633                 GSource *source = NULL;
634                 source = g_io_create_watch(channel, G_IO_IN);
635
636                 /* Set callback to be called when socket is readable */
637                 g_source_set_callback(source, (GSourceFunc)_media_thumb_write_socket, userData, NULL);
638                 source_id = g_source_attach(source, g_main_context_get_thread_default());
639         }
640
641         thumbMsg req_msg;
642         memset((void *)&req_msg, 0, sizeof(thumbMsg));
643
644         pid = getpid();
645         req_msg.pid = pid;
646         req_msg.msg_type = msg_type;
647         req_msg.thumb_type = thumb_type;
648         req_msg.uid = uid;
649
650         strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
651         req_msg.org_path[strlen(req_msg.org_path)] = '\0';
652         req_msg.dst_path[0] = '\0';
653         req_msg.thumb_data = (unsigned char *)"\0";
654
655         req_msg.origin_path_size = strlen(req_msg.org_path);
656         req_msg.dest_path_size = 1;
657         req_msg.thumb_size = 1;
658
659         if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
660                 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
661                 g_io_channel_shutdown(channel, TRUE, NULL);
662                 g_io_channel_unref(channel);
663                 ms_ipc_delete_client_socket(&sock_info);
664                 return MS_MEDIA_ERR_INVALID_PARAMETER;
665         }
666
667         unsigned char *buf = NULL;
668         int buf_size = 0;
669         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
670
671         if (send(sock, buf, buf_size, 0) != buf_size) {
672                 thumb_err("sendto failed: %d", errno);
673                 SAFE_FREE(buf);
674                 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
675                 g_io_channel_shutdown(channel, TRUE, NULL);
676                 g_io_channel_unref(channel);
677                 ms_ipc_delete_client_socket(&sock_info);
678                 return MS_MEDIA_ERR_SOCKET_SEND;
679         }
680
681         SAFE_FREE(buf);
682         thumb_dbg("Sending msg to thumbnail daemon is successful");
683
684         if (msg_type == THUMB_REQUEST_CANCEL_MEDIA) {
685                 g_io_channel_shutdown(channel, TRUE, NULL);
686
687                 //thumb_dbg("Cancel : %s[%d]", origin_path, sock);
688                 __media_thumb_pop_req_queue(origin_path, TRUE);
689         } else if (msg_type == THUMB_REQUEST_DB_INSERT) {
690                 if (g_request_queue == NULL) {
691                         g_request_queue = g_queue_new();
692                 }
693
694                 thumbReq *thumb_req = NULL;
695                 thumb_req = calloc(1, sizeof(thumbReq));
696                 if (thumb_req == NULL) {
697                         thumb_err("Failed to create request element");
698                         return MS_MEDIA_ERR_INVALID_PARAMETER;
699                 }
700
701                 thumb_req->channel = channel;
702                 thumb_req->path = strdup(origin_path);
703                 thumb_req->source_id = source_id;
704                 thumb_req->userData = userData;
705
706                 g_queue_push_tail(g_request_queue, (gpointer)thumb_req);
707         }
708
709         return MS_MEDIA_ERR_NONE;
710 }
711
712 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)
713 {
714         int err = MS_MEDIA_ERR_NONE;
715         int sock = -1;
716         struct sockaddr_un serv_addr;
717         ms_sock_info_s sock_info;
718         int pid;
719         sock_info.port = MS_THUMB_CREATOR_PORT;
720
721         err = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
722         if(err != MS_MEDIA_ERR_NONE)
723         {
724                 thumb_err("ms_ipc_create_client_socket failed");
725                 return err;
726         }
727
728         memset(&serv_addr, 0, sizeof(serv_addr));
729         sock = sock_info.sock_fd;
730         serv_addr.sun_family = AF_UNIX;
731         strcpy(serv_addr.sun_path, "/var/run/media-server/media_ipc_thumbcreator.socket");
732
733         GIOChannel *channel = NULL;
734         channel = g_io_channel_unix_new(sock);
735         int source_id = -1;
736
737         /* Connecting to the thumbnail server */
738         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
739                 thumb_err("connect error : %s", strerror(errno));
740                 g_io_channel_shutdown(channel, TRUE, NULL);
741                 g_io_channel_unref(channel);
742                 return MS_MEDIA_ERR_SOCKET_CONN;
743         }
744
745         if (msg_type != THUMB_REQUEST_CANCEL_RAW_DATA) {
746                 /* Create new channel to watch udp socket */
747                 GSource *source = NULL;
748                 source = g_io_create_watch(channel, G_IO_IN);
749
750                 /* Set callback to be called when socket is readable */
751                 /*NEED UPDATE SOCKET FILE DELETE*/
752                 g_source_set_callback(source, (GSourceFunc)_media_thumb_raw_data_write_socket, userData, NULL);
753                 source_id = g_source_attach(source, g_main_context_get_thread_default());
754         }
755
756         thumbMsg req_msg;
757         memset((void *)&req_msg, 0, sizeof(thumbMsg));
758
759         pid = getpid();
760         req_msg.pid = pid;
761         req_msg.msg_type = msg_type;
762         req_msg.thumb_type = MEDIA_THUMB_LARGE;
763         req_msg.request_id = request_id;
764         req_msg.thumb_width = width;
765         req_msg.thumb_height = height;
766         req_msg.uid = uid;
767
768         strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
769         req_msg.org_path[strlen(req_msg.org_path)] = '\0';
770         req_msg.dst_path[0] = '\0';
771         req_msg.thumb_data = (unsigned char *)"\0";
772
773         req_msg.origin_path_size = strlen(req_msg.org_path);
774         req_msg.dest_path_size = 1;
775         req_msg.thumb_size = 1;
776
777         if (req_msg.origin_path_size > MAX_PATH_SIZE) {
778                 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
779                 g_io_channel_shutdown(channel, TRUE, NULL);
780                 g_io_channel_unref(channel);
781                 return MS_MEDIA_ERR_INVALID_PARAMETER;
782         }
783
784         unsigned char *buf = NULL;
785         int buf_size = 0;
786         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
787
788         if (send(sock, buf, buf_size, 0) != buf_size) {
789                 thumb_err("sendto failed: %d", errno);
790                 SAFE_FREE(buf);
791                 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
792                 g_io_channel_shutdown(channel, TRUE, NULL);
793                 g_io_channel_unref(channel);
794                 return MS_MEDIA_ERR_SOCKET_SEND;
795         }
796
797         SAFE_FREE(buf);
798         thumb_dbg("Sending msg to thumbnail daemon is successful");
799
800         if (msg_type == THUMB_REQUEST_CANCEL_RAW_DATA) {
801                 g_io_channel_shutdown(channel, TRUE, NULL);
802                 __media_thumb_pop_raw_data_req_queue(request_id, TRUE);
803         } else if (msg_type == THUMB_REQUEST_RAW_DATA) {
804                 if (g_request_queue == NULL) {
805                         g_request_queue = g_queue_new();
806                 }
807                 thumbRawReq *thumb_req = NULL;
808                 thumb_req = calloc(1, sizeof(thumbReq));
809                 if (thumb_req == NULL) {
810                         thumb_err("Failed to create request element");
811                         return MS_MEDIA_ERR_INVALID_PARAMETER;
812                 }
813                 thumb_req->channel = channel;
814                 thumb_req->request_id = request_id;
815                 thumb_req->source_id = source_id;
816                 thumb_req->userData = userData;
817
818                 g_queue_push_tail(g_request_queue, (gpointer)thumb_req);
819         }
820         return MS_MEDIA_ERR_NONE;
821 }