fix timeout error
[profile/ivi/download-provider.git] / src / download-provider-receiver.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/socket.h>
4 #include <sys/un.h>
5 #include <unistd.h>
6 #include <sys/stat.h>
7 #include <time.h>
8 #include <sys/time.h>
9
10 #include <glib.h>
11
12 #include <net_connection.h>
13
14 #include "download-provider-config.h"
15 #include "download-provider-log.h"
16 #include "download-provider-pthread.h"
17 #include "download-provider-notification.h"
18 #include "download-provider-ipc.h"
19 #include "download-provider-db.h"
20 #include "download-provider-utils.h"
21
22 #include "download-agent-defs.h"
23 #include "download-agent-interface.h"
24
25 int _init_agent(void);
26 void _deinit_agent(void);
27 static void __downloading_info_cb(user_downloading_info_t *download_info,
28                                         void *user_data);
29 static void __download_info_cb(user_download_info_t *download_info,
30                                         void *user_data);
31 static void __notify_cb(user_notify_info_t *notify_info, void *user_data);
32 static int __change_error(int err);
33 static int __change_state(da_state state);
34
35 void TerminateDaemon(int signo);
36
37 pthread_attr_t g_download_provider_thread_attr;
38 fd_set g_download_provider_socket_readset;
39 fd_set g_download_provider_socket_exceptset;
40
41 void *_start_download(void *args)
42 {
43         int da_ret = -1;
44         int req_dl_id = -1;
45
46         download_clientinfo_slot *clientinfoslot =
47                 (download_clientinfo_slot *) args;
48         if (!clientinfoslot) {
49                 TRACE_DEBUG_MSG("[NULL-CHECK] download_clientinfo_slot");
50                 return 0;
51         }
52         download_clientinfo *clientinfo =
53                 (download_clientinfo *) clientinfoslot->clientinfo;
54         if (!clientinfo) {
55                 TRACE_DEBUG_MSG("[NULL-CHECK] download_clientinfo");
56                 return 0;
57         }
58
59         CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
60         clientinfo->state = DOWNLOAD_STATE_READY;
61         clientinfo->err = DOWNLOAD_ERROR_NONE;
62         CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
63
64         // call start_download() of download-agent
65         if (clientinfo->requestinfo->headers.rows > 0) {
66                 int len = 0;
67                 int i = 0;
68                 char **req_header = NULL;
69                 len = clientinfo->requestinfo->headers.rows;
70                 req_header = calloc(len, sizeof(char *));
71                 if (!req_header) {
72                         TRACE_DEBUG_MSG("fail to calloc");
73                         return 0;
74                 }
75                 for (i = 0; i < len; i++)
76                         req_header[i] =
77                                 strdup(clientinfo->requestinfo->headers.str[i].str);
78                 if (clientinfo->requestinfo->install_path.length > 1) {
79                         if (clientinfo->requestinfo->filename.length > 1)
80                                 da_ret =
81                                         da_start_download_with_extension(clientinfo->requestinfo->
82                                                         url.str, &req_dl_id,
83                                                         DA_FEATURE_REQUEST_HEADER,
84                                                         req_header, &len,
85                                                         DA_FEATURE_INSTALL_PATH,
86                                                         clientinfo->requestinfo->install_path.str,
87                                                         DA_FEATURE_FILE_NAME,
88                                                         clientinfo->requestinfo->filename.str,
89                                                         DA_FEATURE_USER_DATA,
90                                                         (void *)clientinfoslot,
91                                                         NULL);
92                         else
93                                 da_ret =
94                                         da_start_download_with_extension(clientinfo->requestinfo->
95                                                         url.str, &req_dl_id,
96                                                         DA_FEATURE_REQUEST_HEADER,
97                                                         req_header, &len,
98                                                         DA_FEATURE_INSTALL_PATH,
99                                                         clientinfo->requestinfo->install_path.str,
100                                                         DA_FEATURE_USER_DATA,
101                                                         (void *)clientinfoslot,
102                                                         NULL);
103                 } else {
104                         if (clientinfo->requestinfo->filename.length > 1)
105                                 da_ret =
106                                         da_start_download_with_extension(clientinfo->requestinfo->
107                                                         url.str, &req_dl_id,
108                                                         DA_FEATURE_REQUEST_HEADER,
109                                                         req_header, &len,
110                                                         DA_FEATURE_FILE_NAME,
111                                                         clientinfo->requestinfo->filename.str,
112                                                         DA_FEATURE_USER_DATA,
113                                                         (void *)clientinfoslot,
114                                                         NULL);
115                         else
116                                 da_ret =
117                                         da_start_download_with_extension(clientinfo->requestinfo->
118                                                         url.str, &req_dl_id,
119                                                         DA_FEATURE_REQUEST_HEADER,
120                                                         req_header, &len,
121                                                         DA_FEATURE_USER_DATA,
122                                                         (void *)clientinfoslot,
123                                                         NULL);
124                 }
125                 for (i = 0; i < len; i++) {
126                         if (req_header[i])
127                                 free(req_header[i]);
128                 }
129         } else {
130                 if (clientinfo->requestinfo->install_path.length > 1) {
131                         if (clientinfo->requestinfo->filename.length > 1)
132                                 da_ret =
133                                         da_start_download_with_extension(clientinfo->requestinfo->
134                                                         url.str, &req_dl_id,
135                                                         DA_FEATURE_INSTALL_PATH,
136                                                         clientinfo->requestinfo->install_path.str,
137                                                         DA_FEATURE_FILE_NAME,
138                                                         clientinfo->requestinfo->filename.str,
139                                                         DA_FEATURE_USER_DATA,
140                                                         (void *)clientinfoslot,
141                                                         NULL);
142                         else
143                                 da_ret =
144                                         da_start_download_with_extension(clientinfo->requestinfo->
145                                                         url.str, &req_dl_id,
146                                                         DA_FEATURE_INSTALL_PATH,
147                                                         clientinfo->requestinfo->install_path.str,
148                                                         DA_FEATURE_USER_DATA,
149                                                         (void *)clientinfoslot,
150                                                         NULL);
151                 } else {
152                         if (clientinfo->requestinfo->filename.length > 1)
153                                 da_ret =
154                                         da_start_download_with_extension(clientinfo->requestinfo->
155                                                         url.str, &req_dl_id,
156                                                         DA_FEATURE_FILE_NAME,
157                                                         clientinfo->requestinfo->filename.str,
158                                                         DA_FEATURE_USER_DATA,
159                                                         (void *)clientinfoslot,
160                                                         NULL);
161                         else
162                                 da_ret =
163                                         da_start_download_with_extension(clientinfo->requestinfo->
164                                                         url.str, &req_dl_id,
165                                                         DA_FEATURE_USER_DATA,
166                                                         (void *)clientinfoslot,
167                                                         NULL);
168                 }
169         }
170
171         // if start_download() return error cause of maximun download limitation, set state to DOWNLOAD_STATE_PENDED.
172         if (da_ret == DA_ERR_ALREADY_MAX_DOWNLOAD) {
173                 TRACE_DEBUG_INFO_MSG("change to pended request [%d]", da_ret);
174                 CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
175                 clientinfo->state = DOWNLOAD_STATE_PENDED;
176                 clientinfo->err = DOWNLOAD_ERROR_TOO_MANY_DOWNLOADS;
177                 download_provider_db_requestinfo_update_column(clientinfo,
178                                                                         DOWNLOAD_DB_STATE);
179                 ipc_send_request_stateinfo(clientinfo);
180                 CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
181                 return 0;
182         } else if (da_ret != DA_RESULT_OK) {
183                 TRACE_DEBUG_INFO_MSG("Fail to request start [%d]", da_ret);
184                 /* FIXME : need to seperate in detail according to error return values */
185                 CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
186                 clientinfo->state = DOWNLOAD_STATE_FAILED;
187                 clientinfo->err = DOWNLOAD_ERROR_INVALID_PARAMETER;
188                 download_provider_db_requestinfo_remove(clientinfo->
189                                                         requestinfo->requestid);
190                 ipc_send_request_stateinfo(clientinfo);
191                 CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
192                 return 0;
193         }
194
195         CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
196
197         TRACE_DEBUG_INFO_MSG("started download [%d]", da_ret);
198
199         clientinfo->req_id = req_dl_id;
200         clientinfo->state = DOWNLOAD_STATE_DOWNLOADING;
201         clientinfo->err = DOWNLOAD_ERROR_NONE;
202
203         download_provider_db_requestinfo_update_column(clientinfo,
204                                                                 DOWNLOAD_DB_STATE);
205
206         // sync return  // client should be alive till this line at least.
207         ipc_send_request_stateinfo(clientinfo);
208
209         CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
210         return 0;
211 }
212
213 int _handle_new_connection(download_clientinfo_slot *clientinfo_list, download_clientinfo *request_clientinfo)
214 {
215         uint searchslot = 0;
216
217         // NULL - checking
218         if (!clientinfo_list || !request_clientinfo ) {
219                 TRACE_DEBUG_MSG("NULL-CHECK");
220                 return -1;
221         }
222
223         CLIENT_MUTEX_INIT(&(request_clientinfo->client_mutex), NULL);
224
225 #ifdef SO_PEERCRED
226         socklen_t cr_len =
227                 sizeof(request_clientinfo->credentials);
228         if (getsockopt
229                 (request_clientinfo->clientfd, SOL_SOCKET,
230                 SO_PEERCRED, &request_clientinfo->credentials,
231                 &cr_len) == 0) {
232                 TRACE_DEBUG_INFO_MSG
233                         ("Client Info : pid=%d, uid=%d, gid=%d\n",
234                         request_clientinfo->credentials.pid,
235                         request_clientinfo->credentials.uid,
236                         request_clientinfo->credentials.gid);
237         }
238 #endif
239
240         download_controls type =
241                 ipc_receive_header(request_clientinfo->clientfd);
242         TRACE_DEBUG_INFO_MSG("[ACCEPT] HEADER : [%d] ", type);
243         // first of all, receive requestinfo .
244         if (type <= 0 || ipc_receive_request_msg(request_clientinfo) < 0) {
245                 TRACE_DEBUG_MSG("Ignore this connection, Invalid command");
246                 clear_clientinfo(request_clientinfo);
247                 return -1;
248         }
249
250         if (type == DOWNLOAD_CONTROL_STOP
251                 || type == DOWNLOAD_CONTROL_GET_STATE_INFO
252                 || type == DOWNLOAD_CONTROL_RESUME
253                 || type == DOWNLOAD_CONTROL_PAUSE) {
254                 // get requestid from socket.
255                 if (request_clientinfo->requestinfo
256                         && request_clientinfo->requestinfo->requestid > 0) {
257                         // search requestid in slots.
258                         int searchindex = get_same_request_slot_index
259                                                         (clientinfo_list,
260                                                         request_clientinfo->requestinfo->requestid);
261                         if (type == DOWNLOAD_CONTROL_STOP) {
262                                 TRACE_DEBUG_INFO_MSG("Request : DOWNLOAD_CONTROL_STOP");
263                                 if (searchindex >= 0) {
264                                         if (da_cancel_download
265                                                 (clientinfo_list[searchindex].clientinfo->req_id)
266                                                 == DA_RESULT_OK) {
267                                                 request_clientinfo->state = DOWNLOAD_STATE_STOPPED;
268                                                 request_clientinfo->err = DOWNLOAD_ERROR_NONE;
269                                                 if (clientinfo_list[searchindex].clientinfo->requestinfo
270                                                         && clientinfo_list[searchindex].clientinfo->requestinfo->notification)
271                                                         set_downloadedinfo_appfw_notification(clientinfo_list[searchindex].clientinfo);
272                                                 download_provider_db_requestinfo_remove(request_clientinfo->requestinfo->requestid);
273                                                 download_provider_db_history_new(clientinfo_list[searchindex].clientinfo);
274                                         } else {
275                                                 request_clientinfo->state = DOWNLOAD_STATE_FAILED;
276                                                 request_clientinfo->err = DOWNLOAD_ERROR_INVALID_PARAMETER;
277                                         }
278                                 } else { // no found
279                                         request_clientinfo->state = DOWNLOAD_STATE_NONE;
280                                         request_clientinfo->err = DOWNLOAD_ERROR_NONE;
281                                 }
282                                 ipc_send_stateinfo(request_clientinfo);
283                         } else if (type == DOWNLOAD_CONTROL_GET_STATE_INFO) {
284                                 // search slots/downloading db/history db
285                                 if (searchindex > 0) { // exist in slots (memory)
286                                         request_clientinfo->state =
287                                                 clientinfo_list[searchindex].clientinfo->state;
288                                         request_clientinfo->err =
289                                                 clientinfo_list[searchindex].clientinfo->err;
290                                 } else {  //search downloading db or history db
291                                         download_dbinfo* dbinfo =
292                                                 download_provider_db_get_info(
293                                                         request_clientinfo->requestinfo->requestid);
294                                         if (dbinfo) { // found in downloading db..it means crashed job
295                                                 request_clientinfo->state = DOWNLOAD_STATE_PENDED;
296                                                 request_clientinfo->err = DOWNLOAD_ERROR_TOO_MANY_DOWNLOADS;
297                                         } else { // no exist in downloading db
298                                                 dbinfo = download_provider_db_history_get_info(
299                                                         request_clientinfo->requestinfo->requestid);
300                                                 if (!dbinfo) { // no info anywhere
301                                                         request_clientinfo->state = DOWNLOAD_STATE_NONE;
302                                                         request_clientinfo->err = DOWNLOAD_ERROR_NONE;
303                                                 } else { //history info
304                                                         request_clientinfo->state = dbinfo->state;
305                                                         request_clientinfo->err = DOWNLOAD_ERROR_NONE;
306                                                 }
307                                         }
308                                         download_provider_db_info_free(dbinfo);
309                                         free(dbinfo);
310                                 }
311                                 ipc_send_stateinfo(request_clientinfo);
312                                 // estabilish the spec of return value.
313                         } else if (type == DOWNLOAD_CONTROL_PAUSE) {
314                                 if (searchindex >= 0) {
315                                         if (da_suspend_download
316                                                 (clientinfo_list[searchindex].clientinfo->req_id)
317                                                 == DA_RESULT_OK) {
318                                                 request_clientinfo->state = DOWNLOAD_STATE_PAUSE_REQUESTED;
319                                                 request_clientinfo->err = DOWNLOAD_ERROR_NONE;
320                                         } else {
321                                                 request_clientinfo->state = DOWNLOAD_STATE_FAILED;
322                                                 request_clientinfo->err = DOWNLOAD_ERROR_INVALID_PARAMETER;
323                                         }
324                                 } else { // no found
325                                         request_clientinfo->state = DOWNLOAD_STATE_NONE;
326                                         request_clientinfo->err = DOWNLOAD_ERROR_NONE;
327                                 }
328                                 ipc_send_stateinfo(request_clientinfo);
329                         } else if (type == DOWNLOAD_CONTROL_RESUME) {
330                                 if (searchindex >= 0) {
331                                         if (da_resume_download
332                                                 (clientinfo_list[searchindex].clientinfo->req_id)
333                                                 == DA_RESULT_OK) {
334                                                 request_clientinfo->state = DOWNLOAD_STATE_DOWNLOADING;
335                                                 request_clientinfo->err = DOWNLOAD_ERROR_NONE;
336                                         } else {
337                                                 request_clientinfo->state = DOWNLOAD_STATE_FAILED;
338                                                 request_clientinfo->err = DOWNLOAD_ERROR_INVALID_PARAMETER;
339                                         }
340                                 } else { // no found
341                                         request_clientinfo->state = DOWNLOAD_STATE_NONE;
342                                         request_clientinfo->err = DOWNLOAD_ERROR_NONE;
343                                 }
344                                 ipc_send_stateinfo(request_clientinfo);
345                         }
346                 }
347                 clear_clientinfo(request_clientinfo);
348                 return 0;
349         }
350
351         if (type != DOWNLOAD_CONTROL_START) {
352                 TRACE_DEBUG_MSG
353                         ("Now, DOWNLOAD_CONTROL_START is only supported");
354                 clear_clientinfo(request_clientinfo);
355                 return -1;
356         }
357
358         // check whether requestinfo has requestid or not.
359         if (request_clientinfo->requestinfo
360                 && request_clientinfo->requestinfo->requestid > 0) {
361                 // search same request id.
362                 int searchindex = get_same_request_slot_index(clientinfo_list,
363                                                 request_clientinfo->requestinfo->requestid);
364                 if (searchindex < 0) {
365                         TRACE_DEBUG_INFO_MSG("Not Found Same Request ID");
366                         // Invalid id
367                         request_clientinfo->state = DOWNLOAD_STATE_FAILED;
368                         request_clientinfo->err = DOWNLOAD_ERROR_INVALID_PARAMETER;
369                         ipc_send_request_stateinfo(request_clientinfo);
370                         clear_clientinfo(request_clientinfo);
371                         return 0;
372                 } else {        // found request id. // how to deal etag ?
373                         // connect to slot.
374                         TRACE_DEBUG_INFO_MSG("Found Same Request ID slot[%d]", searchindex);
375                         CLIENT_MUTEX_LOCK(&(request_clientinfo->client_mutex));
376                         // close previous socket.
377                         if (clientinfo_list[searchindex].clientinfo->clientfd > 0)
378                                 close(clientinfo_list[searchindex].clientinfo->clientfd);
379                         // change to new socket.
380                         clientinfo_list[searchindex].clientinfo->clientfd =
381                                 request_clientinfo->clientfd;
382                         ipc_send_request_stateinfo(clientinfo_list[searchindex].clientinfo);
383                         // update some info.
384                         clientinfo_list[searchindex].clientinfo->requestinfo->callbackinfo =
385                                 request_clientinfo->requestinfo->callbackinfo;
386                         clientinfo_list[searchindex].clientinfo->requestinfo->notification =
387                                 request_clientinfo->requestinfo->notification;
388                         request_clientinfo->clientfd = 0;       // prevent to not be disconnected.
389                         CLIENT_MUTEX_UNLOCK(&(request_clientinfo->client_mutex));
390                         clear_clientinfo(request_clientinfo);
391                         return 0;
392                 }
393         }
394
395         // new request.
396         searchslot = get_empty_slot_index(clientinfo_list);
397         if (searchslot < 0) {
398                 TRACE_DEBUG_MSG("download-provider is busy, try later");
399                 clear_clientinfo(request_clientinfo);
400                 sleep(5);       // provider need the time of refresh.
401                 return -1;
402         }
403         // create new unique id, and insert info to DB.
404         if (request_clientinfo->requestinfo
405                 && request_clientinfo->requestinfo->requestid <= 0) {
406                 request_clientinfo->requestinfo->requestid =
407                         get_download_request_id();
408                 if (download_provider_db_requestinfo_new
409                         (request_clientinfo) < 0) {
410                         clear_clientinfo(request_clientinfo);
411                         sleep(5);       // provider need the time of refresh.
412                         return -1;
413                 }
414         }
415
416         clientinfo_list[searchslot].clientinfo = request_clientinfo;
417
418         TRACE_DEBUG_INFO_MSG("New Connection slot [%d] max [%d] max once [%d]",
419                                                                                         searchslot,
420                                                                                         MAX_CLIENT,
421                                                                                         DA_MAX_DOWNLOAD_REQ_AT_ONCE);
422
423         if (get_downloading_count(clientinfo_list) >=
424                 DA_MAX_DOWNLOAD_REQ_AT_ONCE) {
425                 CLIENT_MUTEX_LOCK(&(clientinfo_list[searchslot].clientinfo->client_mutex));
426                 // deal as pended job.
427                 clientinfo_list[searchslot].clientinfo->state = DOWNLOAD_STATE_PENDED;
428                 clientinfo_list[searchslot].clientinfo->err = DOWNLOAD_ERROR_TOO_MANY_DOWNLOADS;
429                 download_provider_db_requestinfo_update_column
430                         (clientinfo_list[searchslot].clientinfo,
431                         DOWNLOAD_DB_STATE);
432                 ipc_send_request_stateinfo(clientinfo_list[searchslot].clientinfo);
433                 TRACE_DEBUG_INFO_MSG ("Pended Request is saved to [%d/%d]",
434                         searchslot, MAX_CLIENT);
435                 CLIENT_MUTEX_UNLOCK(&(clientinfo_list[searchslot].clientinfo->client_mutex));
436                 sleep(5);       // provider need the time of refresh.
437         } else {
438                 // create thread for receiving the reqeust info from client.
439                 // and if possible, it will create the thread for listening the event.
440                 if (pthread_create
441                         (&clientinfo_list[searchslot].clientinfo->thread_pid,
442                         &g_download_provider_thread_attr, _start_download,
443                         &clientinfo_list[searchslot]) != 0) {
444                         TRACE_DEBUG_INFO_MSG("failed to call pthread_create for client");
445                         TRACE_DEBUG_INFO_MSG("Change to pended job");
446                         CLIENT_MUTEX_LOCK(&(clientinfo_list[searchslot].clientinfo->client_mutex));
447                         clientinfo_list[searchslot].clientinfo->state =
448                                 DOWNLOAD_STATE_PENDED;
449                         clientinfo_list[searchslot].clientinfo->err =
450                                 DOWNLOAD_ERROR_TOO_MANY_DOWNLOADS;
451                         download_provider_db_requestinfo_update_column
452                                 (clientinfo_list[searchslot].clientinfo,
453                                 DOWNLOAD_DB_STATE);
454                         ipc_send_request_stateinfo(clientinfo_list[searchslot].clientinfo);
455                         CLIENT_MUTEX_UNLOCK(&(clientinfo_list[searchslot].clientinfo->client_mutex));
456                         sleep(5);       // provider need the time of refresh.
457                 }
458         }
459         return 0;
460 }
461
462 int _handle_client_request(download_clientinfo* clientinfo)
463 {
464         int da_ret = 0;
465         int msgType = 0;
466
467         // NULL - checking
468         if (!clientinfo) {
469                 TRACE_DEBUG_MSG("NULL-CHECK");
470                 return -1;
471         }
472
473         switch (msgType = ipc_receive_header(clientinfo->clientfd)) {
474         case DOWNLOAD_CONTROL_STOP:
475                 if (clientinfo->state >= DOWNLOAD_STATE_FINISHED) {
476                         // clear slot requested by client after finished download
477                         TRACE_DEBUG_INFO_MSG("request Free slot to main thread");
478                         clear_socket(clientinfo);
479                         break;
480                 }
481                 TRACE_DEBUG_INFO_MSG("DOWNLOAD_CONTROL_STOP");
482                 da_ret = da_cancel_download(clientinfo->req_id);
483                 CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
484                 if (da_ret != DA_RESULT_OK) {
485                         /* FIXME : need to seperate in detail according to error return values */
486                         clientinfo->state = DOWNLOAD_STATE_FAILED;
487                         clientinfo->err = DOWNLOAD_ERROR_INVALID_PARAMETER;
488                         TRACE_DEBUG_MSG("Fail to request cancel [%d]", da_ret);
489                 } else {
490                         clientinfo->state = DOWNLOAD_STATE_STOPPED;
491                         clientinfo->err = DOWNLOAD_ERROR_NONE;
492                         if (clientinfo->requestinfo) {
493                                 if (clientinfo->requestinfo->notification)
494                                         set_downloadedinfo_appfw_notification(clientinfo);
495                                 download_provider_db_requestinfo_remove(clientinfo->
496                                         requestinfo->requestid);
497                         }
498                         download_provider_db_history_new(clientinfo);
499                 }
500                 ipc_send_stateinfo(clientinfo);
501                 CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
502                 break;
503         case DOWNLOAD_CONTROL_PAUSE:
504                 TRACE_DEBUG_INFO_MSG("DOWNLOAD_CONTROL_PAUSE");
505                 da_ret = da_suspend_download(clientinfo->req_id);
506                 CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
507                 if (da_ret != DA_RESULT_OK) {
508                         /* FIXME : need to seperate in detail according to error return values */
509                         clientinfo->state = DOWNLOAD_STATE_FAILED;
510                         clientinfo->err = DOWNLOAD_ERROR_INVALID_PARAMETER;
511                         TRACE_DEBUG_MSG("Fail to request suspend [%d]", da_ret);
512                 } else {
513                         clientinfo->state = DOWNLOAD_STATE_PAUSE_REQUESTED;
514                         clientinfo->err = DOWNLOAD_ERROR_NONE;
515                 }
516                 ipc_send_stateinfo(clientinfo);
517                 CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
518                 break;
519         case DOWNLOAD_CONTROL_RESUME:
520                 TRACE_DEBUG_INFO_MSG("DOWNLOAD_CONTROL_RESUME");
521                 da_ret = da_resume_download(clientinfo->req_id);
522                 CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
523                 if (da_ret != DA_RESULT_OK) {
524                         /* FIXME : need to seperate in detail according to error return values */
525                         clientinfo->state = DOWNLOAD_STATE_FAILED;
526                         clientinfo->err = DOWNLOAD_ERROR_INVALID_PARAMETER;
527                         TRACE_DEBUG_MSG("Fail to request resume [%d]", da_ret);
528                 } else {
529                         clientinfo->state = DOWNLOAD_STATE_DOWNLOADING;
530                         clientinfo->err = DOWNLOAD_ERROR_NONE;
531                 }
532                 ipc_send_stateinfo(clientinfo);
533                 CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
534                 break;
535         case DOWNLOAD_CONTROL_GET_STATE_INFO:   // sync call
536                 TRACE_DEBUG_INFO_MSG("DOWNLOAD_CONTROL_GET_STATE_INFO");
537                 CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
538                 ipc_send_stateinfo(clientinfo);
539                 CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
540                 break;
541         case DOWNLOAD_CONTROL_GET_DOWNLOAD_INFO:        // sync call
542                 TRACE_DEBUG_INFO_MSG("DOWNLOAD_CONTROL_GET_DOWNLOAD_INFO");
543                 CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
544                 ipc_send_downloadinfo(clientinfo);
545                 CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
546                 break;
547         case -1:
548         case 0:
549                 TRACE_DEBUG_MSG("(Closed Socket ) terminate event thread (%d)",
550                         msgType);
551                 // bloken socket... it seems the client is dead or closed by agent thread.
552                 // close socket, this will break the loop. and terminate this thread.
553                 clear_socket(clientinfo);
554                 return -1;
555         default:
556                 TRACE_DEBUG_MSG("Unknow message [%d]", msgType);
557                 return -1;
558         }
559         return 0;
560 }
561
562 void *run_manage_download_server(void *args)
563 {
564         int listenfd = 0;       // main socket to be albe to listen the new connection
565         int maxfd;
566         int ret = 0;
567         fd_set rset, exceptset;
568         struct timeval timeout;
569         long flexible_timeout;
570         download_clientinfo_slot *clientinfo_list;
571         uint searchslot = 0;
572         uint count_downloading_threads = 0;
573         download_clientinfo *request_clientinfo;
574         int check_retry = 1;
575         int i = 0;
576         int is_timeout = 0;
577
578         socklen_t clientlen;
579         struct sockaddr_un listenaddr, clientaddr;
580
581         GMainLoop *mainloop = (GMainLoop *) args;
582
583         ret = _init_agent();
584         if (ret != DOWNLOAD_ERROR_NONE) {
585                 TRACE_DEBUG_MSG("failed to init agent");
586                 TerminateDaemon(SIGTERM);
587                 return 0;
588         }
589         clear_downloadinginfo_appfw_notification();
590
591         if ((listenfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
592                 TRACE_DEBUG_MSG("failed to create socket");
593                 TerminateDaemon(SIGTERM);
594                 return 0;
595         }
596
597         bzero(&listenaddr, sizeof(listenaddr));
598         listenaddr.sun_family = AF_UNIX;
599         strcpy(listenaddr.sun_path, DOWNLOAD_PROVIDER_IPC);
600
601         if (bind(listenfd, (struct sockaddr *)&listenaddr, sizeof listenaddr) !=
602                 0) {
603                 TRACE_DEBUG_MSG("failed to call bind");
604                 TerminateDaemon(SIGTERM);
605                 return 0;
606         }
607
608         if (chmod(listenaddr.sun_path, 0777) < 0) {
609                 TRACE_DEBUG_MSG
610                         ("failed to change the permission of socket file");
611                 TerminateDaemon(SIGTERM);
612                 return 0;
613         }
614
615         if (listen(listenfd, MAX_CLIENT) != 0) {
616                 TRACE_DEBUG_MSG("failed to call listen");
617                 TerminateDaemon(SIGTERM);
618                 return 0;
619         }
620
621         maxfd = listenfd;
622         TRACE_DEBUG_INFO_MSG("Ready to listen IPC [%d][%s]", listenfd,
623                         DOWNLOAD_PROVIDER_IPC);
624
625         // allocation the array structure for managing the clients.
626         clientinfo_list =
627                 (download_clientinfo_slot *) calloc(MAX_CLIENT,
628                                                         sizeof(download_clientinfo_slot));
629         if (clientinfo_list == NULL) {
630                 TRACE_DEBUG_MSG("failed to allocate the memory for client list");
631                 TerminateDaemon(SIGTERM);
632                 return 0;
633         }
634
635         if (pthread_attr_init(&g_download_provider_thread_attr) != 0) {
636                 TRACE_DEBUG_MSG("failed to call pthread_attr_init for client");
637                 TerminateDaemon(SIGTERM);
638                 return 0;
639         }
640         if (pthread_attr_setdetachstate(&g_download_provider_thread_attr,
641                                                                         PTHREAD_CREATE_DETACHED) != 0) {
642                 TRACE_DEBUG_MSG("failed to set detach option");
643                 TerminateDaemon(SIGTERM);
644                 return 0;
645         }
646
647         flexible_timeout = DOWNLOAD_PROVIDER_CARE_CLIENT_MIN_INTERVAL;
648
649         FD_ZERO(&g_download_provider_socket_readset);
650         FD_ZERO(&g_download_provider_socket_exceptset);
651         FD_SET(listenfd, &g_download_provider_socket_readset);
652         FD_SET(listenfd, &g_download_provider_socket_exceptset);
653
654         while (g_main_loop_is_running(mainloop)) {
655
656                 // clean slots
657                 for (i = 0; i < MAX_CLIENT; i++) {
658                         if (!clientinfo_list[i].clientinfo)
659                                 continue;
660                         // clear slot.
661                         if (clientinfo_list[i].clientinfo->state >= DOWNLOAD_STATE_FINISHED) {
662                                 if (clientinfo_list[i].clientinfo->clientfd <= 0)
663                                         clear_clientinfoslot(&clientinfo_list[i]);
664                                 continue;
665                         }
666                 }
667
668                 is_timeout = 1;
669                 rset = g_download_provider_socket_readset;
670                 exceptset = g_download_provider_socket_exceptset;
671
672                 memset(&timeout, 0x00, sizeof(struct timeval));
673                 timeout.tv_sec = flexible_timeout;
674
675                 if (select((maxfd + 1), &rset, 0, &exceptset, &timeout) < 0) {
676                         TRACE_DEBUG_MSG
677                                 ("select error, provider can't receive any request from client.");
678                         TerminateDaemon(SIGTERM);
679                         break;
680                 }
681
682                 for (i = 0; i < MAX_CLIENT; i++) {  // find the socket received the packet.
683                         if (!clientinfo_list[i].clientinfo)
684                                 continue;
685                         //Even if no socket, downloading should be progressed.
686                         if (clientinfo_list[i].clientinfo->clientfd <= 0)
687                                 continue;
688                         if (FD_ISSET(clientinfo_list[i].clientinfo->clientfd, &rset) > 0) {
689                                 // ignore it is not started yet.
690                                 if (clientinfo_list[i].clientinfo->state <= DOWNLOAD_STATE_READY)
691                                         continue;
692                                 TRACE_DEBUG_INFO_MSG("FD_ISSET [%d] readset slot[%d]",
693                                         clientinfo_list[i].clientinfo->clientfd, i);
694                                 _handle_client_request(clientinfo_list[i].clientinfo);
695                                 if (is_timeout)
696                                         is_timeout = 0;
697                         } else if (FD_ISSET(clientinfo_list[i].clientinfo->clientfd, &exceptset) > 0) {
698                                 TRACE_DEBUG_MSG("FD_ISSET [%d] exceptset slot[%d]", clientinfo_list[i].clientinfo->clientfd, i);
699                                 clear_clientinfoslot(&clientinfo_list[i]);
700                         }
701                 } // MAX_CLIENT
702
703                 if (FD_ISSET(listenfd, &exceptset) > 0) {
704                         TRACE_DEBUG_MSG("meet listenfd Exception of socket");
705                         TerminateDaemon(SIGTERM);
706                         break;
707                 } else if (FD_ISSET(listenfd, &rset) > 0) { // new connection
708                         TRACE_DEBUG_INFO_MSG("FD_ISSET listenfd rset");
709                         if (is_timeout)
710                                 is_timeout = 0;
711                         // reset timeout.
712                         flexible_timeout =
713                                 DOWNLOAD_PROVIDER_CARE_CLIENT_MIN_INTERVAL;
714                         // ready the buffer.
715                         request_clientinfo =
716                                 (download_clientinfo *) calloc(1,
717                                                         sizeof(download_clientinfo));
718                         if (!request_clientinfo) {
719                                 TRACE_DEBUG_MSG
720                                         ("download-provider can't allocate the memory, try later");
721                                 clientlen = sizeof(clientaddr);
722                                 int clientfd = accept(listenfd,
723                                         (struct sockaddr *)&clientaddr, &clientlen);
724                                 close(clientfd);        // disconnect.
725                                 sleep(5);       // provider need the time of refresh.
726                                 continue;
727                         }
728                         // accept client.
729                         clientlen = sizeof(clientaddr);
730                         request_clientinfo->clientfd = accept(listenfd,
731                                                                 (struct sockaddr*)&clientaddr,
732                                                                 &clientlen);
733                         if (request_clientinfo->clientfd < 0) {
734                                 clear_clientinfo(request_clientinfo);
735                                 sleep(5);       // provider need the time of refresh.
736                                 continue;
737                         }
738                         if (_handle_new_connection(clientinfo_list, request_clientinfo) < 0) {
739                                 sleep(1);
740                                 continue;
741                         }
742                         // after starting the download by DA, event thread will start to get the event from client.
743                         if (request_clientinfo && request_clientinfo->clientfd > 0) {
744                                 FD_SET(request_clientinfo->clientfd, &g_download_provider_socket_readset);      // add new descriptor to set
745                                 FD_SET(request_clientinfo->clientfd, &g_download_provider_socket_exceptset);
746                                 if (request_clientinfo->clientfd > maxfd )
747                                         maxfd = request_clientinfo->clientfd;   /* for select */
748                         }
749                 }
750
751                 if (is_timeout && i >= MAX_CLIENT) { // timeout
752                         // If there is better solution to be able to know
753                         // the number of downloading threads, replace below rough codes.
754                         count_downloading_threads =
755                                 get_downloading_count(clientinfo_list);
756                         // check whether the number of downloading is already maximum.
757                         if (count_downloading_threads >=
758                                 DA_MAX_DOWNLOAD_REQ_AT_ONCE)
759                                 continue;
760
761                         // search pended request
762                         for (searchslot = 0; searchslot < MAX_CLIENT;
763                                 searchslot++) {
764                                 if (clientinfo_list[searchslot].clientinfo) {
765                                         if (clientinfo_list[searchslot].clientinfo->state ==
766                                                 DOWNLOAD_STATE_PENDED) {
767                                                 TRACE_DEBUG_INFO_MSG
768                                                         ("Retry Pended Request [%d/%d] state [%d/%d]",
769                                                         searchslot, MAX_CLIENT,
770                                                         count_downloading_threads,
771                                                         DA_MAX_DOWNLOAD_REQ_AT_ONCE);
772                                                 // create thread for restarting the pended download.
773                                                 if (pthread_create
774                                                         (&clientinfo_list[searchslot].clientinfo->thread_pid,
775                                                         &g_download_provider_thread_attr,
776                                                         _start_download,
777                                                         &clientinfo_list[searchslot]) != 0) {
778                                                         TRACE_DEBUG_MSG
779                                                                 ("failed to call pthread_create for client");
780                                                 }
781                                                 count_downloading_threads++;
782                                                 usleep(1000);   // sleep in busy state.
783                                                 break;
784                                         }
785                                 }
786                         }
787
788                         if (check_retry
789                                 && count_downloading_threads <
790                                 DA_MAX_DOWNLOAD_REQ_AT_ONCE) {
791                                 // Auto re-download feature. ethernet may be connected with other downloading items.
792                                 connection_h network_handle = NULL;
793                                 if (connection_create(&network_handle) < 0) {
794                                         TRACE_DEBUG_MSG
795                                                 ("Failed connection_create");
796                                         continue;
797                                 }
798
799                                 connection_ethernet_state_e system_network_state
800                                         = CONNECTION_ETHERNET_STATE_DISCONNECTED;
801                                 if (connection_get_ethernet_state(network_handle,
802                                                                                         &system_network_state) !=
803                                         CONNECTION_ERROR_NONE)
804                                         TRACE_DEBUG_MSG
805                                                 ("Failed connection_get_ethernet_state");
806                                 TRACE_DEBUG_INFO_MSG
807                                         ("ethernet check result : [%d]", (int)system_network_state);
808
809                                 connection_cellular_state_e system_cellular_state
810                                         = CONNECTION_CELLULAR_STATE_OUT_OF_SERVICE;
811                                 if (connection_get_cellular_state(network_handle,
812                                                                                         &system_cellular_state) !=
813                                         CONNECTION_ERROR_NONE)
814                                         TRACE_DEBUG_MSG
815                                                 ("Failed connection_get_ethernet_state");
816                                 TRACE_DEBUG_INFO_MSG
817                                         ("cellula check result : [%d]", (int)system_cellular_state);
818
819                                 connection_wifi_state_e system_wifi_state
820                                         = CONNECTION_WIFI_STATE_DEACTIVATED;
821                                 if (connection_get_wifi_state(network_handle,
822                                                                                         &system_wifi_state) !=
823                                         CONNECTION_ERROR_NONE)
824                                         TRACE_DEBUG_MSG
825                                                 ("Failed connection_get_ethernet_state");
826                                 TRACE_DEBUG_INFO_MSG
827                                         ("wifi check result : [%d]", (int)system_wifi_state);
828
829                                 if (connection_destroy(network_handle) !=
830                                         CONNECTION_ERROR_NONE)
831                                         TRACE_DEBUG_MSG
832                                                 ("Failed connection_destroy");
833
834                                 if (!(system_network_state
835                                                 == CONNECTION_ETHERNET_STATE_CONNECTED
836                                         || system_cellular_state
837                                                 == CONNECTION_CELLULAR_STATE_AVAILABLE
838                                         || system_wifi_state
839                                                 == CONNECTION_WIFI_STATE_CONNECTED))
840                                         continue;
841
842                                 // check auto-retrying list regardless state. pended state is also included to checking list.
843                                 int i = 0;
844                                 download_dbinfo_list *db_list =
845                                         download_provider_db_get_list(DOWNLOAD_STATE_NONE);
846                                 if (!db_list || db_list->count <= 0) {
847                                         TRACE_DEBUG_INFO_MSG
848                                                 ("provider does not need to check DB anymore. in this life.");
849                                         check_retry = 0;        // provider does not need to check DB anymore. in this life.
850                                         if (db_list)
851                                                 download_provider_db_list_free(db_list);
852                                         continue;
853                                 }
854                                 for (i = 0;
855                                         count_downloading_threads <
856                                         DA_MAX_DOWNLOAD_REQ_AT_ONCE
857                                         && i < db_list->count; i++) {
858                                         if (db_list->item[i].requestid <= 0)
859                                                 continue;
860                                         if (get_same_request_slot_index
861                                                 (clientinfo_list,db_list->item[i].requestid) < 0) {
862                                                 // not found requestid in memory
863                                                 TRACE_DEBUG_INFO_MSG
864                                                         ("Retry download [%d]",
865                                                         db_list->item[i].requestid);
866                                                 //search empty slot. copy db info to slot.
867                                                 searchslot =
868                                                         get_empty_slot_index(clientinfo_list);
869                                                 if (searchslot < 0) {
870                                                         TRACE_DEBUG_INFO_MSG
871                                                                 ("download-provider is busy, try later");
872                                                         flexible_timeout =
873                                                                 flexible_timeout * 2;
874                                                         break;
875                                                 }
876                                                 // allocte requestinfo to empty slot.
877                                                 request_clientinfo =
878                                                         (download_clientinfo *)
879                                                         calloc(1, sizeof(download_clientinfo));
880                                                 if (!request_clientinfo)
881                                                         continue;
882                                                 request_clientinfo->requestinfo
883                                                         =
884                                                         download_provider_db_get_requestinfo
885                                                         (&db_list->item[i]);
886                                                 if (!request_clientinfo->requestinfo) {
887                                                         free(request_clientinfo);
888                                                         request_clientinfo = NULL;
889                                                         continue;
890                                                 }
891
892                                                 CLIENT_MUTEX_INIT(&(request_clientinfo->client_mutex), NULL);
893                                                 request_clientinfo->state = DOWNLOAD_STATE_READY;
894                                                 clientinfo_list[searchslot].clientinfo =
895                                                         request_clientinfo;
896
897                                                 TRACE_DEBUG_INFO_MSG
898                                                         ("Retry download [%d/%d][%d/%d]",
899                                                         searchslot, MAX_CLIENT,
900                                                         count_downloading_threads,
901                                                         DA_MAX_DOWNLOAD_REQ_AT_ONCE);
902                                                 if (pthread_create
903                                                         (&clientinfo_list[searchslot].clientinfo->thread_pid,
904                                                         &g_download_provider_thread_attr,
905                                                         _start_download,
906                                                         &clientinfo_list[searchslot])
907                                                         != 0) {
908                                                         TRACE_DEBUG_MSG
909                                                                 ("failed to call pthread_create for client");
910                                                         clientinfo_list[searchslot].clientinfo->state =
911                                                                 DOWNLOAD_STATE_PENDED;
912                                                         clientinfo_list[searchslot].clientinfo->err =
913                                                                 DOWNLOAD_ERROR_TOO_MANY_DOWNLOADS;
914                                                         sleep(5);
915                                                 }
916                                                 count_downloading_threads++;
917                                                 usleep(1000);   // sleep in busy state.
918                                         }
919                                 }
920                                 if (i >= db_list->count)        // do not search anymore.
921                                         check_retry = 0;
922                                 download_provider_db_list_free(db_list);
923                         }
924
925                         // save system resource (CPU)
926                         if (check_retry == 0 && count_downloading_threads == 0
927                                 && flexible_timeout <
928                                 DOWNLOAD_PROVIDER_CARE_CLIENT_MAX_INTERVAL)
929                                 flexible_timeout = flexible_timeout * 2;
930                         if (flexible_timeout >
931                                 DOWNLOAD_PROVIDER_CARE_CLIENT_MAX_INTERVAL)
932                                 flexible_timeout =
933                                         DOWNLOAD_PROVIDER_CARE_CLIENT_MAX_INTERVAL;
934                         TRACE_DEBUG_INFO_MSG("Next Timeout after [%ld] sec",
935                                         flexible_timeout);
936
937                 } // if (i >= MAX_CLIENT) { // timeout
938         }
939
940         FD_CLR(listenfd, &rset);
941         FD_CLR(listenfd, &exceptset);
942         FD_CLR(listenfd, &g_download_provider_socket_readset);
943         FD_CLR(listenfd, &g_download_provider_socket_exceptset);
944
945         // close accept socket.
946         if (listenfd)
947                 close(listenfd);
948
949         _deinit_agent();
950
951         // close all sockets for client. .. 
952         // client thread will terminate by itself through catching this closing.
953         for (searchslot = 0; searchslot < MAX_CLIENT; searchslot++)
954                 if (clientinfo_list[searchslot].clientinfo)
955                         clear_clientinfoslot(&clientinfo_list[searchslot]);
956
957         if (clientinfo_list)
958                 free(clientinfo_list);
959
960         pthread_exit(NULL);
961         return 0;
962 }
963
964 void __download_info_cb(user_download_info_t *download_info, void *user_data)
965 {
966         int len = 0;
967         if (!user_data) {
968                 TRACE_DEBUG_MSG("[CRITICAL] clientinfoslot is NULL");
969                 return;
970         }
971         download_clientinfo_slot *clientinfoslot =
972                 (download_clientinfo_slot *) user_data;
973         download_clientinfo *clientinfo =
974                 (download_clientinfo *) clientinfoslot->clientinfo;
975         if (!clientinfo) {
976                 TRACE_DEBUG_MSG("[CRITICAL] clientinfo is NULL");
977                 return;
978         }
979         TRACE_DEBUG_INFO_MSG("id[%d],size[%lu]",
980                         download_info->da_dl_req_id, download_info->file_size);
981
982         if (clientinfo->req_id != download_info->da_dl_req_id) {
983                 TRACE_DEBUG_MSG("[CRITICAL] req_id[%d] da_dl_req_id[%d}",
984                                 clientinfo->req_id,
985                                 download_info->da_dl_req_id);
986                 return;
987         }
988         CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
989         if (!clientinfo->downloadinfo)
990                 clientinfo->downloadinfo =
991                         (download_content_info *) calloc(1, sizeof(download_content_info));
992         if (clientinfo->downloadinfo)
993                 clientinfo->downloadinfo->file_size = download_info->file_size;
994         if (download_info->file_type) {
995                 TRACE_DEBUG_INFO_MSG("mime[%s]", download_info->file_type);
996
997                 len = strlen(download_info->file_type);
998                 if (len > (DP_MAX_STR_LEN - 1))
999                         len = DP_MAX_STR_LEN - 1;
1000                 if (clientinfo->downloadinfo) {
1001                         strncpy(clientinfo->downloadinfo->mime_type,
1002                                 download_info->file_type, len);
1003                         download_provider_db_requestinfo_update_column
1004                                 (clientinfo, DOWNLOAD_DB_MIMETYPE);
1005                 }
1006         }
1007         if (download_info->tmp_saved_path) {
1008                 char *str = NULL;
1009                 TRACE_DEBUG_INFO_MSG("tmp path[%s]", download_info->tmp_saved_path);
1010                 clientinfo->tmp_saved_path =
1011                         strdup(download_info->tmp_saved_path);
1012                 download_provider_db_requestinfo_update_column(clientinfo,
1013                                                                         DOWNLOAD_DB_SAVEDPATH);
1014                 str = strrchr(download_info->tmp_saved_path, '/');
1015                 if (str) {
1016                         str++;
1017                         len = strlen(str);
1018                         if (len > (DP_MAX_STR_LEN - 1))
1019                                 len = DP_MAX_STR_LEN - 1;
1020                         if (clientinfo->downloadinfo) {
1021                                 strncpy(clientinfo->downloadinfo->content_name,
1022                                         str, len);
1023                                 download_provider_db_requestinfo_update_column
1024                                         (clientinfo, DOWNLOAD_DB_FILENAME);
1025                                 TRACE_DEBUG_INFO_MSG("content_name[%s]",
1026                                                 clientinfo->downloadinfo->
1027                                                 content_name);
1028                         }
1029                 }
1030         }
1031
1032         if (clientinfo->requestinfo->callbackinfo.started)
1033                 ipc_send_downloadinfo(clientinfo);
1034
1035         CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
1036 }
1037
1038 void __downloading_info_cb(user_downloading_info_t *download_info,
1039                            void *user_data)
1040 {
1041         int len = 0;
1042         if (!user_data) {
1043                 TRACE_DEBUG_MSG("[CRITICAL] clientinfoslot is NULL");
1044                 return;
1045         }
1046         download_clientinfo_slot *clientinfoslot =
1047                 (download_clientinfo_slot *) user_data;
1048         download_clientinfo *clientinfo =
1049                 (download_clientinfo *) clientinfoslot->clientinfo;
1050         if (!clientinfo) {
1051                 TRACE_DEBUG_MSG("[CRITICAL] clientinfo is NULL");
1052                 return;
1053         }
1054         CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
1055         if (clientinfo->req_id != download_info->da_dl_req_id) {
1056                 TRACE_DEBUG_MSG("[CRITICAL] req_id[%d] da_dl_req_id[%d}",
1057                                 clientinfo->req_id,
1058                                 download_info->da_dl_req_id);
1059                 CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
1060                 return;
1061         }
1062         if (!clientinfo->downloadinginfo)
1063                 clientinfo->downloadinginfo = (downloading_state_info *) calloc(1,
1064                                                                         sizeof(downloading_state_info));
1065         if (clientinfo->downloadinginfo)
1066                 clientinfo->downloadinginfo->received_size =
1067                         download_info->total_received_size;
1068         if (download_info->saved_path) {
1069                 TRACE_DEBUG_INFO_MSG("tmp path[%s]", download_info->saved_path);
1070                 len = strlen(download_info->saved_path);
1071                 if (len > (DP_MAX_PATH_LEN - 1))
1072                         len = DP_MAX_PATH_LEN - 1;
1073                 if (clientinfo->downloadinginfo)
1074                         strncpy(clientinfo->downloadinginfo->saved_path,
1075                                 download_info->saved_path, len);
1076                 /* FIXME : This should be reviewd again after smack rules is applied */
1077                 if (chown(clientinfo->downloadinginfo->saved_path,
1078                                 clientinfo->credentials.uid,
1079                                 clientinfo->credentials.gid) < 0)
1080                         TRACE_DEBUG_INFO_MSG("Fail to chown [%s]", strerror(errno));
1081         }
1082
1083         static size_t updated_second;
1084         time_t tt = time(NULL);
1085         struct tm *localTime = localtime(&tt);
1086
1087         if (updated_second != localTime->tm_sec || download_info->saved_path) { // every 1 second.
1088                 if (clientinfo->requestinfo
1089                         && clientinfo->requestinfo->notification)
1090                         set_downloadinginfo_appfw_notification(clientinfo);
1091                 if (clientinfo->requestinfo->callbackinfo.progress)
1092                         ipc_send_downloadinginfo(clientinfo);
1093                 updated_second = localTime->tm_sec;
1094         }
1095         CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
1096 }
1097
1098 void __notify_cb(user_notify_info_t *notify_info, void *user_data)
1099 {
1100         if (!user_data) {
1101                 TRACE_DEBUG_MSG("[CRITICAL] clientinfoslot is NULL");
1102                 return;
1103         }
1104         download_clientinfo_slot *clientinfoslot =
1105                 (download_clientinfo_slot *) user_data;
1106         download_clientinfo *clientinfo =
1107                 (download_clientinfo *) clientinfoslot->clientinfo;
1108         if (!clientinfo) {
1109                 TRACE_DEBUG_MSG("[CRITICAL] clientinfo is NULL");
1110                 return;
1111         }
1112
1113         TRACE_DEBUG_INFO_MSG("id[%d],state[%d],err[%d]",
1114                         notify_info->da_dl_req_id,
1115                         notify_info->state, notify_info->err);
1116         if (clientinfo->req_id != notify_info->da_dl_req_id) {
1117                 TRACE_DEBUG_MSG("[CRITICAL] req_id[%d] da_dl_req_id[%d}",
1118                                 clientinfo->req_id, notify_info->da_dl_req_id);
1119                 return;
1120         }
1121
1122         CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
1123
1124         clientinfo->state = __change_state(notify_info->state);
1125         clientinfo->err = __change_error(notify_info->err);
1126         if (clientinfo->state == DOWNLOAD_STATE_FINISHED ||
1127                         clientinfo->state == DOWNLOAD_STATE_FAILED) {
1128                 if (clientinfo->requestinfo) {
1129                         if (clientinfo->requestinfo->notification)
1130                                 set_downloadedinfo_appfw_notification(clientinfo);
1131                         download_provider_db_requestinfo_remove(clientinfo->
1132                                 requestinfo->requestid);
1133                 }
1134                 download_provider_db_history_new(clientinfo);
1135                 TRACE_DEBUG_INFO_MSG("[TEST]Finish clientinfo[%p], fd[%d]",
1136                         clientinfo, clientinfo->clientfd);
1137         }
1138
1139         TRACE_DEBUG_INFO_MSG("state[%d]", clientinfo->state);
1140         ipc_send_stateinfo(clientinfo);
1141
1142         CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
1143 }
1144
1145 int __change_state(da_state state)
1146 {
1147         int ret = DOWNLOAD_STATE_NONE;
1148         switch (state) {
1149         case DA_STATE_WAITING:
1150         case DA_STATE_DOWNLOAD_STARTED:
1151                 TRACE_DEBUG_INFO_MSG("DA_STATE_DOWNLOAD_STARTED");
1152                 ret = DOWNLOAD_STATE_READY;
1153                 break;
1154         case DA_STATE_DOWNLOADING:
1155                 TRACE_DEBUG_INFO_MSG("DA_STATE_DOWNLOADING");
1156                 ret = DOWNLOAD_STATE_DOWNLOADING;
1157                 break;
1158         case DA_STATE_DOWNLOAD_COMPLETE:
1159                 TRACE_DEBUG_INFO_MSG("DA_STATE_COMPLETE");
1160                 ret = DOWNLOAD_STATE_INSTALLING;
1161                 break;
1162         case DA_STATE_CANCELED:
1163                 TRACE_DEBUG_INFO_MSG("DA_STATE_CANCELED");
1164                 ret = DOWNLOAD_STATE_STOPPED;
1165                 break;
1166         case DA_STATE_CANCELED_ALL:
1167                 TRACE_DEBUG_INFO_MSG("DA_STATE_CANCELED_ALL");
1168                 break;
1169         case DA_STATE_SUSPENDED:
1170                 TRACE_DEBUG_INFO_MSG("DA_STATE_SUSPENDED");
1171                 ret = DOWNLOAD_STATE_PAUSED;
1172                 break;
1173         case DA_STATE_SUSPENDED_ALL:
1174                 TRACE_DEBUG_INFO_MSG("DA_STATE_SUSPENDED_ALL");
1175                 break;
1176         case DA_STATE_RESUMED:
1177                 TRACE_DEBUG_INFO_MSG("DA_STATE_RESUMED");
1178                 ret = DOWNLOAD_STATE_DOWNLOADING;
1179                 break;
1180         case DA_STATE_RESUMED_ALL:
1181                 TRACE_DEBUG_INFO_MSG("DA_STATE_RESUMED_ALL");
1182                 break;
1183         case DA_STATE_FINISHED:
1184                 TRACE_DEBUG_INFO_MSG("DA_STATE_FINISHED");
1185                 ret = DOWNLOAD_STATE_FINISHED;
1186                 break;
1187         case DA_STATE_FAILED:
1188                 TRACE_DEBUG_INFO_MSG("DA_STATE_FAILED");
1189                 ret = DOWNLOAD_STATE_FAILED;
1190                 break;
1191         default:
1192                 break;
1193         }
1194         return ret;
1195 }
1196
1197 int __change_error(int err)
1198 {
1199         int ret = DOWNLOAD_ERROR_UNKOWN;
1200         switch (err) {
1201         case DA_RESULT_OK:
1202                 ret = DOWNLOAD_ERROR_NONE;
1203                 break;
1204         case DA_ERR_INVALID_ARGUMENT:
1205                 ret = DOWNLOAD_ERROR_INVALID_PARAMETER;
1206                 break;
1207         case DA_ERR_FAIL_TO_MEMALLOC:
1208                 ret = DOWNLOAD_ERROR_OUT_OF_MEMORY;
1209                 break;
1210         case DA_ERR_UNREACHABLE_SERVER:
1211                 ret = DOWNLOAD_ERROR_NETWORK_UNREACHABLE;
1212                 break;
1213         case DA_ERR_HTTP_TIMEOUT:
1214                 ret = DOWNLOAD_ERROR_CONNECTION_TIMED_OUT;
1215                 break;
1216         case DA_ERR_DISK_FULL:
1217                 ret = DOWNLOAD_ERROR_NO_SPACE;
1218                 break;
1219         case DA_ERR_INVALID_STATE:
1220                 ret = DOWNLOAD_ERROR_INVALID_STATE;
1221                 break;
1222         case DA_ERR_NETWORK_FAIL:
1223                 ret = DOWNLOAD_ERROR_CONNECTION_FAILED;
1224                 break;
1225         case DA_ERR_INVALID_URL:
1226                 ret = DOWNLOAD_ERROR_INVALID_URL;
1227                 break;
1228         case DA_ERR_INVALID_INSTALL_PATH:
1229                 ret = DOWNLOAD_ERROR_INVALID_DESTINATION;
1230                 break;
1231         case DA_ERR_ALREADY_MAX_DOWNLOAD:
1232                 ret = DOWNLOAD_ERROR_TOO_MANY_DOWNLOADS;
1233                 break;
1234         case DA_ERR_FAIL_TO_INSTALL_FILE:
1235                 ret = DOWNLOAD_ERROR_INSTALL_FAIL;
1236                 break;
1237         case DA_ERR_FAIL_TO_CREATE_THREAD:
1238         case DA_ERR_FAIL_TO_OBTAIN_MUTEX:
1239         case DA_ERR_FAIL_TO_ACCESS_FILE:
1240         case DA_ERR_FAIL_TO_GET_CONF_VALUE:
1241         case DA_ERR_FAIL_TO_ACCESS_STORAGE:
1242         case DA_ERR_DLOPEN_FAIL:
1243                 ret = DOWNLOAD_ERROR_IO_ERROR;
1244                 break;
1245         }
1246         return ret;
1247 }
1248
1249 int _init_agent()
1250 {
1251         int da_ret = 0;
1252         da_client_cb_t da_cb = {
1253                 __notify_cb,
1254                 __download_info_cb,
1255                 __downloading_info_cb
1256         };
1257         da_ret = da_init(&da_cb, DA_DOWNLOAD_MANAGING_METHOD_AUTO);
1258         if (da_ret != DA_RESULT_OK) {
1259                 return DOWNLOAD_ERROR_FAIL_INIT_AGENT;
1260         }
1261         return DOWNLOAD_ERROR_NONE;
1262 }
1263
1264 void _deinit_agent()
1265 {
1266         da_deinit();
1267 }