Fix memory leak issue
[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_IPC_PATH tzplatform_mkpath(TZ_SYS_RUN, "media-server/media_ipc_thumbcreator.socket")
35
36 static GQueue *g_manage_queue = NULL;
37 static GQueue *g_manage_raw_queue = NULL;
38
39
40 typedef struct {
41         GIOChannel *channel;
42         int msg_type;
43         unsigned int request_id;
44         bool isCanceled;
45         bool isRequested;
46         int source_id;
47         uid_t uid;
48         char *path;
49         thumbUserData *userData;
50 } thumbReq;
51
52 typedef struct {
53         GIOChannel *channel;
54         int msg_type;
55         bool isCanceled;
56         bool isRequested;
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 static int _media_thumb_send_request();
67 static 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_pop()
82 {
83         int len = 0;
84         int sock = 0;
85
86         if (g_manage_queue != NULL) {
87                 thumbReq *req = (thumbReq *)g_queue_pop_head(g_manage_queue);
88                 if (req != NULL) {
89                         GSource *source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id);
90                         sock = g_io_channel_unix_get_fd(req->channel);
91
92                         g_io_channel_shutdown(req->channel, TRUE, NULL);
93                         g_io_channel_unref(req->channel);
94                         close(sock);
95                         if (source_id != NULL) {
96                                 g_source_destroy(source_id);
97                         } else {
98                                 thumb_err("G_SOURCE_ID is NULL");
99                         }
100
101                         SAFE_FREE(req->path);
102                         SAFE_FREE(req->userData);
103                         SAFE_FREE(req);
104                 }
105
106                 /* Check manage queue */
107                 len = g_queue_get_length(g_manage_queue);
108                 if (len > 0)
109                         _media_thumb_send_request();
110         }
111 }
112
113 int __media_thumb_cancel(unsigned int request_id)
114 {
115         int len = 0, i;
116         bool flag = false;
117
118         if (g_manage_queue != NULL) {
119                 len = g_queue_get_length(g_manage_queue);
120
121                 for (i = 0; i < len; i++) {
122                         thumbReq *req = NULL;
123                         req = (thumbReq *)g_queue_peek_nth(g_manage_queue, i);
124                         if (req == NULL) continue;
125
126                         if (req->request_id == request_id) {
127                                 if (req->isRequested == true) {
128                                         req->isCanceled = true;
129                                 } else {
130                                         g_queue_pop_nth(g_manage_queue, i);
131
132                                         SAFE_FREE(req->path);
133                                         SAFE_FREE(req->userData);
134                                         SAFE_FREE(req);
135                                 }
136
137                                 flag = true;
138
139                                 break;
140                         }
141                 }
142         }
143
144         if (flag == false)
145                 return MS_MEDIA_ERR_INTERNAL;
146
147         return MS_MEDIA_ERR_NONE;
148 }
149
150 void __media_thumb_pop_raw_data()
151 {
152         int len = 0;
153         int sock = 0;
154
155         if (g_manage_raw_queue != NULL) {
156                 thumbRawReq *req = (thumbRawReq *)g_queue_pop_head(g_manage_raw_queue);
157                 if (req != NULL) {
158                         GSource *source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id);
159                         sock = g_io_channel_unix_get_fd(req->channel);
160
161                         g_io_channel_shutdown(req->channel, TRUE, NULL);
162                         g_io_channel_unref(req->channel);
163                         close(sock);
164                         if (source_id != NULL) {
165                                 g_source_destroy(source_id);
166                         } else {
167                                 thumb_err("G_SOURCE_ID is NULL");
168                         }
169
170                         SAFE_FREE(req->path);
171                         SAFE_FREE(req->userData);
172                         SAFE_FREE(req);
173                 }
174
175                 /* Check manage queue */
176                 len = g_queue_get_length(g_manage_raw_queue);
177                 if (len > 0)
178                         _media_thumb_raw_data_send_request();
179         }
180 }
181
182 int __media_thumb_cancel_raw_data(int request_id)
183 {
184         int len = 0, i;
185         bool flag = false;
186
187         if (g_manage_raw_queue != NULL) {
188                 len = g_queue_get_length(g_manage_raw_queue);
189
190                 for (i = 0; i < len; i++) {
191                         thumbRawReq *req = NULL;
192                         req = (thumbRawReq *)g_queue_peek_nth(g_manage_raw_queue, i);
193                         if (req == NULL) continue;
194
195                         if (req->request_id == request_id) {
196                                 if (req->isRequested == true) {
197                                         req->isCanceled = true;
198                                 } else {
199                                         g_queue_pop_nth(g_manage_raw_queue, i);
200
201                                         SAFE_FREE(req->path);
202                                         SAFE_FREE(req->userData);
203                                         SAFE_FREE(req);
204                                 }
205
206                                 flag = true;
207
208                                 break;
209                         }
210                 }
211         }
212
213         if (flag == false)
214                 return MS_MEDIA_ERR_INTERNAL;
215
216         return MS_MEDIA_ERR_NONE;
217
218 }
219
220 bool __media_thumb_check_cancel(void)
221 {
222         thumbReq *req = NULL;
223         req = (thumbReq *)g_queue_peek_head(g_manage_queue);
224
225         if (req == NULL) {
226                 return false;
227         } else {
228                 if (req->isCanceled)
229                         return false;
230                 else
231                         return true;
232         }
233 }
234
235 bool __media_thumb_check_cancel_for_raw(void)
236 {
237         thumbRawReq *req = NULL;
238         req = (thumbRawReq *)g_queue_peek_head(g_manage_raw_queue);
239
240         if (req == NULL) {
241                 return false;
242         } else {
243                 if (req->isCanceled)
244                         return false;
245                 else
246                         return true;
247         }
248 }
249
250 int _media_thumb_recv_msg(int sock, int header_size, thumbMsg *msg)
251 {
252         int remain_size = 0;
253         int recv_len = 0;
254         int recv_pos = 0;
255         unsigned char *buf = NULL;
256
257         THUMB_MALLOC(buf, header_size);
258         if (buf == NULL) {
259                 thumb_err("memory allocation failed");
260                 SAFE_FREE(buf);
261                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
262         }
263
264         while (header_size > 0) {
265                 if ((recv_len = recv(sock, buf + recv_pos, header_size, 0)) < 0) {
266                         thumb_stderror("recv failed");
267                         SAFE_FREE(buf);
268                         return _media_thumb_get_error();
269                 }
270                 header_size -= recv_len;
271                 recv_pos += recv_len;
272         }
273
274         header_size = recv_pos;
275         recv_pos = 0;
276
277         memcpy(msg, buf, header_size);
278         SAFE_FREE(buf);
279
280         if (strlen(msg->org_path) == 0 || strlen(msg->org_path) >= MAX_FILEPATH_LEN) {
281                 thumb_err("org_path size is invalid %zu", strlen(msg->org_path));
282                 return MS_MEDIA_ERR_SOCKET_RECEIVE;
283         }
284
285         /* it can be empty string */
286         if (strlen(msg->dst_path) >= MAX_FILEPATH_LEN) {
287                 thumb_err("dst_path size is invalid %zu", strlen(msg->dst_path));
288                 return MS_MEDIA_ERR_SOCKET_RECEIVE;
289         }
290
291         if (msg->thumb_size < 0) {
292                 thumb_err("recv data is wrong");
293                 return MS_MEDIA_ERR_SOCKET_RECEIVE;
294         }
295
296         if (msg->thumb_size > 0) {
297                 remain_size = msg->thumb_size;
298                 THUMB_MALLOC(buf, remain_size);
299                 if (buf == NULL) {
300                         return MS_MEDIA_ERR_OUT_OF_MEMORY;
301                 }
302
303                 while (remain_size > 0) {
304                         if ((recv_len = recv(sock, buf + recv_pos, remain_size, 0)) < 0) {
305                                 thumb_stderror("recv failed");
306                                 SAFE_FREE(buf);
307                                 return _media_thumb_get_error();
308                         }
309
310                         fsync(sock);
311                         remain_size -= recv_len;
312                         recv_pos += recv_len;
313                 }
314
315                 SAFE_FREE(msg->thumb_data);
316                 THUMB_MALLOC(msg->thumb_data, msg->thumb_size);
317                 if (msg->thumb_data != NULL) {
318                         memcpy(msg->thumb_data, buf, msg->thumb_size);
319                 } else {
320                         SAFE_FREE(buf);
321
322                         return MS_MEDIA_ERR_OUT_OF_MEMORY;
323                 }
324         }
325
326         SAFE_FREE(buf);
327
328         return MS_MEDIA_ERR_NONE;
329 }
330
331 int _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
332 {
333         if (req_msg == NULL || buf == NULL)
334                 return MS_MEDIA_ERR_INVALID_PARAMETER;
335
336         int thumb_data_len = 0;
337         int size = 0;
338         int header_size = 0;
339
340         header_size = sizeof(thumbMsg) - sizeof(unsigned char *);
341         thumb_data_len = req_msg->thumb_size;
342         if (thumb_data_len < 0)
343                 return MS_MEDIA_ERR_INVALID_PARAMETER;
344
345         //thumb_dbg("Basic Size[%d] org_path[%s] dst_path[%s] thumb_data_len[%d]", header_size, req_msg->org_path, req_msg->dst_path, thumb_data_len);
346
347         size = header_size + thumb_data_len;
348         THUMB_MALLOC(*buf, size);
349         if (*buf == NULL) {
350                 *buf_size = 0;
351                 return 0;
352         }
353         memcpy(*buf, req_msg, header_size);
354         if (thumb_data_len > 0)
355                 memcpy((*buf) + header_size, req_msg->thumb_data, thumb_data_len);
356
357         *buf_size = size;
358
359         return MS_MEDIA_ERR_NONE;
360 }
361
362 gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
363 {
364         thumbMsg recv_msg;
365         int header_size = 0;
366         int sock = 0;
367         int err = MS_MEDIA_ERR_NONE;
368
369         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
370         sock = g_io_channel_unix_get_fd(src);
371
372         header_size = sizeof(thumbMsg) - sizeof(unsigned char *);
373
374         thumb_err("_media_thumb_write_socket socket : %d", sock);
375
376         if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
377                 thumb_err("_media_thumb_recv_msg failed ");
378                 __media_thumb_pop();
379
380                 return FALSE;
381         }
382
383         if (recv_msg.status != MS_MEDIA_ERR_NONE) {
384                 err = recv_msg.status;
385                 thumb_err("Failed to make thumbnail (%d)", err);
386         }
387
388         if (__media_thumb_check_cancel()) {
389                 if (data) {
390                         thumbUserData* cb = (thumbUserData*)data;
391                         if (cb->func != NULL)
392                                 cb->func(err, recv_msg.dst_path, cb->user_data);
393                 }
394         }
395
396         __media_thumb_pop();
397
398         thumb_dbg("Done");
399
400         return FALSE;
401 }
402
403 gboolean _media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
404 {
405         thumbMsg recv_msg;
406         int header_size = 0;
407         int sock = 0;
408         int err = MS_MEDIA_ERR_NONE;
409
410         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
411         sock = g_io_channel_unix_get_fd(src);
412
413         header_size = sizeof(thumbMsg) - sizeof(unsigned char *);
414
415         thumb_err("_media_thumb_write_socket socket : %d", sock);
416
417         if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
418                 thumb_err("_media_thumb_recv_msg failed ");
419                 __media_thumb_pop_raw_data();
420
421                 return FALSE;
422         }
423
424         if (recv_msg.status != MS_MEDIA_ERR_NONE) {
425                 err = recv_msg.status;
426                 thumb_err("Failed to make thumbnail (%d)", err);
427         }
428
429         if (__media_thumb_check_cancel_for_raw()) {
430                 if (data) {
431                         thumbRawUserData* cb = (thumbRawUserData*)data;
432                         if (cb->func != NULL)
433                                 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);
434                 }
435         }
436
437         __media_thumb_pop_raw_data();
438
439         thumb_dbg("Done");
440
441         return FALSE;
442 }
443
444 static int _media_thumb_send_request()
445 {
446         int err = MS_MEDIA_ERR_NONE;
447         int sock = -1;
448         struct sockaddr_un serv_addr;
449         thumbReq *req_manager = NULL;
450         int pid;
451
452         err = ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &sock);
453         if (err != MS_MEDIA_ERR_NONE) {
454                 thumb_err("ms_ipc_create_client_socket failed");
455                 return err;
456         }
457
458         memset(&serv_addr, 0, sizeof(serv_addr));
459         serv_addr.sun_family = AF_UNIX;
460         SAFE_STRLCPY(serv_addr.sun_path, THUMB_IPC_PATH, sizeof(serv_addr.sun_path));
461
462         GIOChannel *channel = NULL;
463         channel = g_io_channel_unix_new(sock);
464         int source_id = -1;
465
466         /* Connecting to the thumbnail server */
467         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
468                 thumb_stderror("connect");
469                 if (errno == EACCES)
470                         err = MS_MEDIA_ERR_PERMISSION_DENIED;
471                 else
472                         err = MS_MEDIA_ERR_SOCKET_CONN;
473
474                 g_io_channel_shutdown(channel, TRUE, NULL);
475                 g_io_channel_unref(channel);
476                 close(sock);
477
478                 return err;
479         }
480
481         req_manager = (thumbReq *)g_queue_peek_head(g_manage_queue);
482
483         if (req_manager == NULL) {
484                 thumb_err("queue peek fail");
485                 g_io_channel_shutdown(channel, TRUE, NULL);
486                 g_io_channel_unref(channel);
487                 close(sock);
488                 return MS_MEDIA_ERR_INVALID_PARAMETER;
489         }
490
491         GSource *source = NULL;
492         source = g_io_create_watch(channel, G_IO_IN);
493         g_source_set_callback(source, (GSourceFunc)_media_thumb_write_socket, req_manager->userData, NULL);
494         source_id = g_source_attach(source, g_main_context_get_thread_default());
495
496         thumbMsg req_msg;
497         memset((void *)&req_msg, 0, sizeof(thumbMsg));
498
499         pid = getpid();
500         req_msg.pid = pid;
501         req_msg.msg_type = req_manager->msg_type;
502         req_msg.request_id = 0;
503         req_msg.uid = req_manager->uid;
504         SAFE_STRLCPY(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
505         req_msg.dst_path[0] = '\0';
506         req_msg.thumb_size = 0;
507
508         unsigned char *buf = NULL;
509         int buf_size = 0;
510         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
511
512         if (send(sock, buf, buf_size, 0) < 0) {
513                 thumb_err("send failed: %d", errno);
514                 SAFE_FREE(buf);
515                 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
516                 g_io_channel_shutdown(channel, TRUE, NULL);
517                 g_io_channel_unref(channel);
518                 close(sock);
519                 return MS_MEDIA_ERR_SOCKET_SEND;
520         }
521
522         SAFE_FREE(buf);
523         thumb_dbg("Sending msg to thumbnail daemon is successful");
524
525         if (req_manager->msg_type == THUMB_REQUEST_DB_INSERT) {
526                 req_manager->channel = channel;
527                 req_manager->source_id = source_id;
528                 req_manager->isRequested = true;
529         }
530
531         return err;
532 }
533
534 static int _media_thumb_raw_data_send_request()
535 {
536         int err = MS_MEDIA_ERR_NONE;
537         int sock = -1;
538         struct sockaddr_un serv_addr;
539         thumbRawReq *req_manager = NULL;
540         int pid;
541
542         err = ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &sock);
543         if (err != MS_MEDIA_ERR_NONE) {
544                 thumb_err("ms_ipc_create_client_socket failed");
545                 return err;
546         }
547
548         memset(&serv_addr, 0, sizeof(serv_addr));
549         serv_addr.sun_family = AF_UNIX;
550         SAFE_STRLCPY(serv_addr.sun_path, THUMB_IPC_PATH, sizeof(serv_addr.sun_path));
551
552         GIOChannel *channel = NULL;
553         channel = g_io_channel_unix_new(sock);
554         int source_id = -1;
555
556         /* Connecting to the thumbnail server */
557         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
558                 thumb_stderror("connect error");
559                 if (errno == EACCES)
560                         err = MS_MEDIA_ERR_PERMISSION_DENIED;
561                 else
562                         err = MS_MEDIA_ERR_SOCKET_CONN;
563
564                 g_io_channel_shutdown(channel, TRUE, NULL);
565                 g_io_channel_unref(channel);
566                 close(sock);
567                 return err;
568         }
569
570         req_manager = (thumbRawReq *)g_queue_peek_head(g_manage_raw_queue);
571
572         if (req_manager == NULL) {
573                 thumb_err("queue peek fail");
574                 g_io_channel_shutdown(channel, TRUE, NULL);
575                 g_io_channel_unref(channel);
576                 close(sock);
577                 return MS_MEDIA_ERR_INVALID_PARAMETER;
578         }
579
580         GSource *source = NULL;
581         source = g_io_create_watch(channel, G_IO_IN);
582         g_source_set_callback(source, (GSourceFunc)_media_thumb_raw_data_write_socket, req_manager->userData, NULL);
583         source_id = g_source_attach(source, g_main_context_get_thread_default());
584
585         thumbMsg req_msg;
586         memset((void *)&req_msg, 0, sizeof(thumbMsg));
587
588         pid = getpid();
589         req_msg.pid = pid;
590         req_msg.msg_type = req_manager->msg_type;
591         req_msg.request_id = req_manager->request_id;
592         req_msg.thumb_width = req_manager->width;
593         req_msg.thumb_height = req_manager->height;
594         req_msg.uid = req_manager->uid;
595
596         SAFE_STRLCPY(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
597         req_msg.dst_path[0] = '\0';
598         req_msg.thumb_size = 0;
599
600         unsigned char *buf = NULL;
601         int buf_size = 0;
602         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
603
604         if (send(sock, buf, buf_size, 0) < 0) {
605                 thumb_err("send failed: %d", errno);
606                 SAFE_FREE(buf);
607                 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
608                 g_io_channel_shutdown(channel, TRUE, NULL);
609                 g_io_channel_unref(channel);
610                 return MS_MEDIA_ERR_SOCKET_SEND;
611         }
612
613         SAFE_FREE(buf);
614
615         if (req_manager->msg_type == THUMB_REQUEST_RAW_DATA) {
616                 req_manager->channel = channel;
617                 req_manager->source_id = source_id;
618                 req_manager->isRequested = true;
619         }
620         return MS_MEDIA_ERR_NONE;
621 }
622
623 int _media_thumb_request_async(int msg_type, unsigned int request_id, const char *origin_path, thumbUserData *userData, uid_t uid)
624 {
625         int err = MS_MEDIA_ERR_NONE;
626         int len = 0;
627
628         if (msg_type == THUMB_REQUEST_CANCEL_MEDIA)
629                 return __media_thumb_cancel(request_id);
630
631         if (g_manage_queue == NULL)
632                 g_manage_queue = g_queue_new();
633
634         thumbReq *thumb_req = NULL;
635         THUMB_MALLOC(thumb_req, sizeof(thumbReq));
636         thumb_retvm_if(thumb_req == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Failed to create request element");
637
638         thumb_req->msg_type = msg_type;
639         thumb_req->path = g_strdup(origin_path);
640         thumb_req->userData = userData;
641         thumb_req->isCanceled = false;
642         thumb_req->isRequested = false;
643         thumb_req->request_id = request_id;
644         thumb_req->uid = uid;
645
646         len = g_queue_get_length(g_manage_queue);
647         g_queue_push_tail(g_manage_queue, (gpointer)thumb_req);
648
649         if (len == 0)
650                 err = _media_thumb_send_request();
651
652         return err;
653 }
654
655 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)
656 {
657         int err = MS_MEDIA_ERR_NONE;
658         int len = 0;
659         thumbRawReq *thumb_req = NULL;
660
661         if (msg_type == THUMB_REQUEST_CANCEL_RAW_DATA)
662                 return __media_thumb_cancel_raw_data(request_id);
663
664         if (g_manage_raw_queue == NULL)
665                 g_manage_raw_queue = g_queue_new();
666
667         THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
668         if (thumb_req == NULL) {
669                 thumb_err("Failed to create request element");
670                 return MS_MEDIA_ERR_INVALID_PARAMETER;
671         }
672
673         thumb_req->msg_type = msg_type;
674         thumb_req->request_id = request_id;
675         thumb_req->path = g_strdup(origin_path);
676         thumb_req->width = width;
677         thumb_req->height = height;
678         thumb_req->userData = userData;
679         thumb_req->isCanceled = false;
680         thumb_req->isRequested = false;
681         thumb_req->uid = uid;
682
683         len = g_queue_get_length(g_manage_raw_queue);
684         g_queue_push_tail(g_manage_raw_queue, (gpointer)thumb_req);
685
686         if (len == 0)
687                 err = _media_thumb_raw_data_send_request();
688
689         return err;
690 }