119a4a4ee43fa7d481fc2ce1ffb4d1b3e78c5ef0
[framework/appfw/slp-pkgmgr.git] / client / src / pkgmgr.c
1 /*
2  * slp-pkgmgr
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>,
7  * Jaeho Lee <jaeho81.lee@samsung.com>, Shobhit Srivastava <shobhit.s@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <dlfcn.h>
28 #include <dirent.h>
29 #include <fcntl.h>
30 #include <sys/wait.h>
31 #include <sys/time.h>
32 #include <dbus/dbus.h>
33 #include <dbus/dbus-glib-lowlevel.h>
34 #include <ail.h>
35 #include <aul.h>
36 #include <vconf.h>
37 #include <db-util.h>
38 #include <pkgmgr-info.h>
39 #include <iniparser.h>
40 #include <security-server.h>
41
42 #include "package-manager.h"
43 #include "pkgmgr-internal.h"
44 #include "pkgmgr-debug.h"
45 #include "pkgmgr-api.h"
46 #include "comm_client.h"
47 #include "comm_status_broadcast_server.h"
48
49 #undef LOG_TAG
50 #ifndef LOG_TAG
51 #define LOG_TAG "PKGMGR"
52 #endif                          /* LOG_TAG */
53
54 #define PKG_TMP_PATH "/opt/usr/apps/tmp"
55
56 static int _get_request_id()
57 {
58         static int internal_req_id = 1;
59
60         return internal_req_id++;
61 }
62
63 typedef struct _req_cb_info {
64         int request_id;
65         char *req_key;
66         pkgmgr_handler event_cb;
67         void *data;
68         struct _req_cb_info *next;
69 } req_cb_info;
70
71 typedef struct _listen_cb_info {
72         int request_id;
73         pkgmgr_handler event_cb;
74         void *data;
75         struct _listen_cb_info *next;
76 } listen_cb_info;
77
78 typedef struct _pkgmgr_client_t {
79         client_type ctype;
80         union {
81                 struct _request {
82                         comm_client *cc;
83                         req_cb_info *rhead;
84                 } request;
85                 struct _listening {
86                         comm_client *cc;
87                         listen_cb_info *lhead;
88                 } listening;
89                 struct _broadcast {
90                         DBusConnection *bc;
91                 } broadcast;
92         } info;
93 } pkgmgr_client_t;
94
95 typedef struct _iter_data {
96         pkgmgr_iter_fn iter_fn;
97         void *data;
98 } iter_data;
99
100 static char *__get_cookie_from_security_server(void)
101 {
102         int ret = 0;
103         size_t cookie_size = 0;
104         char *e_cookie = NULL;
105
106         //calculage cookie size
107         cookie_size = security_server_get_cookie_size();
108         retvm_if(cookie_size <= 0, NULL, "security_server_get_cookie_size : cookie_size is %d", cookie_size);
109
110         //get cookie from security server
111         char cookie[cookie_size];
112         cookie[0] = '\0';
113         ret = security_server_request_cookie(cookie, cookie_size);
114         retvm_if(ret < 0, NULL, "security_server_request_cookie fail (%d)", ret);
115
116         //encode cookie
117         e_cookie = g_base64_encode((const guchar *)cookie, cookie_size);
118         retvm_if(e_cookie == NULL, NULL, "g_base64_encode e_cookie is NULL");
119
120         return e_cookie;
121 }
122
123 static int __xsystem(const char *argv[])
124 {
125         int status = 0;
126         pid_t pid;
127         pid = fork();
128         switch (pid) {
129         case -1:
130                 perror("fork failed");
131                 return -1;
132         case 0:
133                 /* child */
134                 execvp(argv[0], (char *const *)argv);
135                 _exit(-1);
136         default:
137                 /* parent */
138                 break;
139         }
140         if (waitpid(pid, &status, 0) == -1) {
141                 perror("waitpid failed");
142                 return -1;
143         }
144         if (WIFSIGNALED(status)) {
145                 perror("signal");
146                 return -1;
147         }
148         if (!WIFEXITED(status)) {
149                 /* shouldn't happen */
150                 perror("should not happen");
151                 return -1;
152         }
153         return WEXITSTATUS(status);
154 }
155
156 static void __error_to_string(int errnumber, char **errstr)
157 {
158         if (errstr == NULL)
159                 return;
160         switch (errnumber) {
161         case PKGCMD_ERR_PACKAGE_NOT_FOUND:
162                 *errstr = PKGCMD_ERR_PACKAGE_NOT_FOUND_STR;
163                 break;
164         case PKGCMD_ERR_PACKAGE_INVALID:
165                 *errstr = PKGCMD_ERR_PACKAGE_INVALID_STR;
166                 break;
167         case PKGCMD_ERR_PACKAGE_LOWER_VERSION:
168                 *errstr = PKGCMD_ERR_PACKAGE_LOWER_VERSION_STR;
169                 break;
170         case PKGCMD_ERR_PACKAGE_EXECUTABLE_NOT_FOUND:
171                 *errstr = PKGCMD_ERR_PACKAGE_EXECUTABLE_NOT_FOUND_STR;
172                 break;
173         case PKGCMD_ERR_MANIFEST_INVALID:
174                 *errstr = PKGCMD_ERR_MANIFEST_INVALID_STR;
175                 break;
176         case PKGCMD_ERR_CONFIG_NOT_FOUND:
177                 *errstr = PKGCMD_ERR_CONFIG_NOT_FOUND_STR;
178                 break;
179         case PKGCMD_ERR_CONFIG_INVALID:
180                 *errstr = PKGCMD_ERR_CONFIG_INVALID_STR;
181                 break;
182         case PKGCMD_ERR_SIGNATURE_NOT_FOUND:
183                 *errstr = PKGCMD_ERR_SIGNATURE_NOT_FOUND_STR;
184                 break;
185         case PKGCMD_ERR_SIGNATURE_INVALID:
186                 *errstr = PKGCMD_ERR_SIGNATURE_INVALID_STR;
187                 break;
188         case PKGCMD_ERR_SIGNATURE_VERIFICATION_FAILED:
189                 *errstr = PKGCMD_ERR_SIGNATURE_VERIFICATION_FAILED_STR;
190                 break;
191         case PKGCMD_ERR_ROOT_CERTIFICATE_NOT_FOUND:
192                 *errstr = PKGCMD_ERR_ROOT_CERTIFICATE_NOT_FOUND_STR;
193                 break;
194         case PKGCMD_ERR_CERTIFICATE_INVALID:
195                 *errstr = PKGCMD_ERR_CERTIFICATE_INVALID_STR;
196                 break;
197         case PKGCMD_ERR_CERTIFICATE_CHAIN_VERIFICATION_FAILED:
198                 *errstr = PKGCMD_ERR_CERTIFICATE_CHAIN_VERIFICATION_FAILED_STR;
199                 break;
200         case PKGCMD_ERR_CERTIFICATE_EXPIRED:
201                 *errstr = PKGCMD_ERR_CERTIFICATE_EXPIRED_STR;
202                 break;
203         case PKGCMD_ERR_INVALID_PRIVILEGE:
204                 *errstr = PKGCMD_ERR_INVALID_PRIVILEGE_STR;
205                 break;
206         case PKGCMD_ERR_MENU_ICON_NOT_FOUND:
207                 *errstr = PKGCMD_ERR_MENU_ICON_NOT_FOUND_STR;
208                 break;
209         case PKGCMD_ERR_FATAL_ERROR:
210                 *errstr = PKGCMD_ERR_FATAL_ERROR_STR;
211                 break;
212         case PKGCMD_ERR_OUT_OF_STORAGE:
213                 *errstr = PKGCMD_ERR_OUT_OF_STORAGE_STR;
214                 break;
215         case PKGCMD_ERR_OUT_OF_MEMORY:
216                 *errstr = PKGCMD_ERR_OUT_OF_MEMORY_STR;
217                 break;
218         case PKGCMD_ERR_ARGUMENT_INVALID:
219                 *errstr = PKGCMD_ERR_ARGUMENT_INVALID_STR;
220                 break;
221         default:
222                 *errstr = PKGCMD_ERR_UNKNOWN_STR;
223                 break;
224         }
225 }
226
227 static void __add_op_cbinfo(pkgmgr_client_t * pc, int request_id,
228                             const char *req_key, pkgmgr_handler event_cb,
229                             void *data)
230 {
231         req_cb_info *cb_info;
232         req_cb_info *current;
233         req_cb_info *prev;
234
235         cb_info = (req_cb_info *) calloc(1, sizeof(req_cb_info));
236         if (cb_info == NULL) {
237                 _LOGD("calloc failed");
238                 return;
239         }
240         cb_info->request_id = request_id;
241         cb_info->req_key = strdup(req_key);
242         cb_info->event_cb = event_cb;
243         cb_info->data = data;
244         cb_info->next = NULL;
245
246         if (pc->info.request.rhead == NULL)
247                 pc->info.request.rhead = cb_info;
248         else {
249                 current = prev = pc->info.request.rhead;
250                 while (current) {
251                         prev = current;
252                         current = current->next;
253                 }
254
255                 prev->next = cb_info;
256         }
257 }
258
259 static req_cb_info *__find_op_cbinfo(pkgmgr_client_t *pc, const char *req_key)
260 {
261         req_cb_info *tmp;
262
263         tmp = pc->info.request.rhead;
264
265         if (tmp == NULL) {
266                 _LOGE("tmp is NULL");
267                 return NULL;
268         }
269
270         _LOGD("tmp->req_key %s, req_key %s", tmp->req_key, req_key);
271
272         while (tmp) {
273                 if (strncmp(tmp->req_key, req_key, strlen(tmp->req_key)) == 0)
274                         return tmp;
275                 tmp = tmp->next;
276         }
277         return NULL;
278 }
279
280 static void __remove_op_cbinfo(pkgmgr_client_t *pc, req_cb_info *info)
281 {
282         req_cb_info *tmp;
283
284         if (pc == NULL || pc->info.request.rhead == NULL || info == NULL)
285                 return;
286
287         tmp = pc->info.request.rhead;
288         while (tmp) {
289                 if (tmp->next == info) {
290                         tmp->next = info->next;
291                         free(info);
292                         return;
293                 }
294                 tmp = tmp->next;
295         }
296 }
297
298
299 static void __add_stat_cbinfo(pkgmgr_client_t *pc, int request_id,
300                               pkgmgr_handler event_cb, void *data)
301 {
302         listen_cb_info *cb_info;
303         listen_cb_info *current;
304         listen_cb_info *prev;
305
306         cb_info = (listen_cb_info *) calloc(1, sizeof(listen_cb_info));
307         if (cb_info == NULL) {
308                 _LOGD("calloc failed");
309                 return;
310         }
311         cb_info->request_id = request_id;
312         cb_info->event_cb = event_cb;
313         cb_info->data = data;
314         cb_info->next = NULL;
315
316         /* TODO - check the order of callback - FIFO or LIFO => Should be changed to LIFO */
317         if (pc->info.listening.lhead == NULL)
318                 pc->info.listening.lhead = cb_info;
319         else {
320                 current = prev = pc->info.listening.lhead;
321                 while (current) {
322                         prev = current;
323                         current = current->next;
324                 }
325
326                 prev->next = cb_info;
327         }
328 }
329
330 static void __operation_callback(void *cb_data, const char *req_id,
331                                  const char *pkg_type, const char *pkgid,
332                                  const char *key, const char *val)
333 {
334         pkgmgr_client_t *pc;
335         req_cb_info *cb_info;
336
337         _LOGD("__operation_callback() req_id[%s] pkg_type[%s] pkgid[%s]"
338               "key[%s] val[%s]\n", req_id, pkg_type, pkgid, key, val);
339
340         pc = (pkgmgr_client_t *) cb_data;
341
342         /* find callback info */
343         cb_info = __find_op_cbinfo(pc, req_id);
344         if (cb_info == NULL)
345                 return;
346
347         _LOGD("__find_op_cbinfo");
348
349         /* call callback */
350         if (cb_info->event_cb) {
351                 cb_info->event_cb(cb_info->request_id, pkg_type, pkgid, key,
352                                   val, NULL, cb_info->data);
353                 _LOGD("event_cb is called");
354         }
355
356         /*remove callback for last call 
357            if (strcmp(key, "end") == 0) {
358            __remove_op_cbinfo(pc, cb_info);
359            _LOGD("__remove_op_cbinfo");
360            }
361          */
362
363         return;
364 }
365
366 static void __status_callback(void *cb_data, const char *req_id,
367                               const char *pkg_type, const char *pkgid,
368                               const char *key, const char *val)
369 {
370         pkgmgr_client_t *pc;
371         listen_cb_info *tmp;
372
373         _LOGD("__status_callback() req_id[%s] pkg_type[%s] pkgid[%s]"
374               "key[%s] val[%s]\n", req_id, pkg_type, pkgid, key, val);
375
376         pc = (pkgmgr_client_t *) cb_data;
377
378         tmp = pc->info.listening.lhead;
379         while (tmp) {
380                 if (tmp->event_cb(tmp->request_id, pkg_type, pkgid, key, val,
381                                   NULL, tmp->data) != 0)
382                         break;
383                 tmp = tmp->next;
384         }
385
386         return;
387 }
388
389 static char *__get_req_key(const char *pkg_path)
390 {
391         struct timeval tv;
392         long curtime;
393         char timestr[PKG_STRING_LEN_MAX];
394         char *str_req_key;
395         int size;
396
397         gettimeofday(&tv, NULL);
398         curtime = tv.tv_sec * 1000000 + tv.tv_usec;
399         snprintf(timestr, sizeof(timestr), "%ld", curtime);
400
401         size = strlen(pkg_path) + strlen(timestr) + 2;
402         str_req_key = (char *)calloc(size, sizeof(char));
403         if (str_req_key == NULL) {
404                 _LOGD("calloc failed");
405                 return NULL;
406         }
407         snprintf(str_req_key, size, "%s_%s", pkg_path, timestr);
408
409         return str_req_key;
410 }
411
412 static char *__get_type_from_path(const char *pkg_path)
413 {
414         int ret;
415         char mimetype[255] = { '\0', };
416         char extlist[256] = { '\0', };
417         char *pkg_type;
418
419         ret = _get_mime_from_file(pkg_path, mimetype, sizeof(mimetype));
420         if (ret) {
421                 _LOGE("_get_mime_from_file() failed - error code[%d]\n",
422                       ret);
423                 return NULL;
424         }
425
426         ret = _get_mime_extension(mimetype, extlist, sizeof(extlist));
427         if (ret) {
428                 _LOGE("_get_mime_extension() failed - error code[%d]\n",
429                       ret);
430                 return NULL;
431         }
432
433         if (strlen(extlist) == 0)
434                 return NULL;
435
436         if (strchr(extlist, ',')) {
437                 extlist[strlen(extlist) - strlen(strchr(extlist, ','))] = '\0';
438         }
439         pkg_type = strchr(extlist, '.') + 1;
440         return strdup(pkg_type);
441 }
442
443 static inline int __pkgmgr_read_proc(const char *path, char *buf, int size)
444 {
445         int fd;
446         int ret;
447
448         if (buf == NULL || path == NULL)
449                 return -1;
450
451         fd = open(path, O_RDONLY);
452         if (fd < 0)
453                 return -1;
454
455         ret = read(fd, buf, size - 1);
456         if (ret <= 0) {
457                 close(fd);
458                 return -1;
459         } else
460                 buf[ret] = 0;
461
462         close(fd);
463
464         return ret;
465 }
466
467 static inline int __pkgmgr_find_pid_by_cmdline(const char *dname,
468                                       const char *cmdline, const char *apppath)
469 {
470         int pid = 0;
471
472         if (strncmp(cmdline, apppath, PKG_STRING_LEN_MAX-1) == 0) {
473                 pid = atoi(dname);
474                 if (pid != getpgid(pid))
475                         pid = 0;
476         }
477
478         return pid;
479 }
480
481
482 static int __pkgmgr_proc_iter_kill_cmdline(const char *apppath)
483 {
484         DIR *dp;
485         struct dirent *dentry;
486         int pid;
487         int ret;
488         char buf[PKG_STRING_LEN_MAX];
489
490         dp = opendir("/proc");
491         if (dp == NULL) {
492                 return -1;
493         }
494
495         while ((dentry = readdir(dp)) != NULL) {
496                 if (!isdigit(dentry->d_name[0]))
497                         continue;
498
499                 snprintf(buf, sizeof(buf), "/proc/%s/cmdline", dentry->d_name);
500                 ret = __pkgmgr_read_proc(buf, buf, sizeof(buf));
501                 if (ret <= 0)
502                         continue;
503
504                 pid = __pkgmgr_find_pid_by_cmdline(dentry->d_name, buf, apppath);
505
506                 if (pid > 0) {
507                         int pgid;
508
509                         pgid = getpgid(pid);
510                         if (pgid <= 1) {
511                                 closedir(dp);
512                                 return -1;
513                         }
514
515                         if (killpg(pgid, SIGKILL) < 0) {
516                                 closedir(dp);
517                                 return -1;
518                         }
519                 }
520         }
521
522         closedir(dp);
523         return 0;
524 }
525
526
527 static int __app_list_cb (const pkgmgr_appinfo_h handle,
528                                                 void *user_data)
529 {
530         char *exec = NULL;
531         pkgmgr_appinfo_get_exec(handle, &exec);
532
533         __pkgmgr_proc_iter_kill_cmdline(exec);
534
535         return 0;
536 }
537
538 static int __sync_process(char *req_key)
539 {
540         int ret =0;
541         char info_file[PKG_STRING_LEN_MAX] = {'\0', };
542         int result = 0;
543         int check_cnt = 0;
544         FILE *fp;
545         char buffer[PKG_ARGC_MAX] = {'\0', };
546
547         snprintf(info_file, PKG_STRING_LEN_MAX, "%s/%s", PKG_TMP_PATH, req_key);
548         while(1)
549         {
550                 check_cnt ++;
551
552                 vconf_get_int(VCONFKEY_PKGMGR_STATUS, &result);
553                 if (result < 0) {
554                         _LOGD("file is not generated yet.... wait\n");
555                         usleep(10 * 1000);      /* 10ms sleep*/
556                 } else {
557                         _LOGD("info_file file is generated, result = %d. \n", result);
558                         break;
559                 }
560
561                 if (check_cnt > 500) {  /* 5s time over*/
562                         _LOGD("wait time over!!\n");
563                         break;
564                 }
565         }
566
567         vconf_set_int(VCONFKEY_PKGMGR_STATUS, -1);
568
569         return result;
570 }
571 static int __csc_process(const char *csc_path, char *result_path)
572 {
573         int ret = 0;
574         int cnt = 0;
575         int count = 0;
576         int csc_fail = 0;
577         int fd = 0;
578         char *pkgtype = NULL;
579         char *des = NULL;
580         char buf[PKG_STRING_LEN_MAX] = {0,};
581         char type_buf[1024] = { 0 };
582         char des_buf[1024] = { 0 };
583         dictionary *csc = NULL;
584         FILE* file = NULL;
585
586         csc = iniparser_load(csc_path);
587         retvm_if(csc == NULL, PKGMGR_R_EINVAL, "cannot open parse file [%s]", csc_path);
588
589         file = fopen(result_path, "w");
590         tryvm_if(file == NULL, ret = PKGMGR_R_EINVAL, "cannot open result file [%s]", result_path);
591
592         count = iniparser_getint(csc, "csc packages:count", -1);
593         tryvm_if(count == 0, ret = PKGMGR_R_ERROR, "csc [%s] dont have packages", csc_path);
594
595         snprintf(buf, PKG_STRING_LEN_MAX, "[result]\n");
596         fwrite(buf, 1, strlen(buf), file);
597         snprintf(buf, PKG_STRING_LEN_MAX, "count = %d\n", count);
598         fwrite(buf, 1, strlen(buf), file);
599
600         for(cnt = 1 ; cnt <= count ; cnt++)
601         {
602                 snprintf(type_buf, PKG_STRING_LEN_MAX - 1, "csc packages:type_%03d", cnt);
603                 snprintf(des_buf, PKG_STRING_LEN_MAX - 1, "csc packages:description_%03d", cnt);
604
605                 pkgtype = iniparser_getstr(csc, type_buf);
606                 des = iniparser_getstr(csc, des_buf);
607                 ret = 0;
608
609                 if (pkgtype == NULL) {
610                         csc_fail++;
611                         snprintf(buf, PKG_STRING_LEN_MAX, "%s = Fail to get pkgtype\n", type_buf);
612                         fwrite(buf, 1, strlen(buf), file);
613                         continue;
614                 } else if (des == NULL) {
615                         csc_fail++;
616                         snprintf(buf, PKG_STRING_LEN_MAX, "%s = Fail to get description\n", des_buf);
617                         fwrite(buf, 1, strlen(buf), file);
618                         continue;
619                 }
620
621                 snprintf(buf, PKG_STRING_LEN_MAX, "type_%03d = %s\n", cnt, pkgtype);
622                 fwrite(buf, 1, strlen(buf), file);
623                 snprintf(buf, PKG_STRING_LEN_MAX, "description_%03d = %s\n", cnt, des);
624                 fwrite(buf, 1, strlen(buf), file);
625
626                 if (strcmp(pkgtype, "tpk") == 0) {
627                         const char *ospinstaller_argv[] = { "/usr/bin/osp-installer", "-c", des, NULL };
628                         ret = __xsystem(ospinstaller_argv);
629                 } else if (strcmp(pkgtype, "wgt")== 0) {
630                         const char *wrtinstaller_argv[] = { "/usr/bin/wrt-installer", "-c", des, NULL };
631                         ret = __xsystem(wrtinstaller_argv);
632                 } else {
633                         csc_fail++;
634                         ret = -1;
635                 }
636
637                 if (ret != 0) {
638                         char *errstr = NULL;
639                         __error_to_string(ret, &errstr);
640                         snprintf(buf, PKG_STRING_LEN_MAX, "result_%03d = fail[%s]\n", cnt, errstr);
641                 }
642                 else
643                         snprintf(buf, PKG_STRING_LEN_MAX, "result_%03d = success\n", cnt);
644
645                 fwrite(buf, 1, strlen(buf), file);
646         }
647
648 catch:
649         iniparser_freedict(csc);
650         if (file != NULL) {
651                 fflush(file);
652                 fd = fileno(file);
653                 fsync(fd);
654                 fclose(file);
655         }
656         return ret;
657 }
658
659 static int __get_size_process(pkgmgr_client * pc, const char *pkgid, pkgmgr_getsize_type get_type, pkgmgr_handler event_cb, void *data)
660 {
661         char *req_key = NULL;
662         int req_id = 0;
663         int ret =0;
664         pkgmgrinfo_pkginfo_h handle;
665         char *pkgtype = NULL;
666         char *installer_path = NULL;
667         char *argv[PKG_ARGC_MAX] = { NULL, };
668         char *args = NULL;
669         int argcnt = 0;
670         int len = 0;
671         char *temp = NULL;
672         int i = 0;
673         char buf[128] = {'\0'};
674         char *cookie = NULL;
675
676         pkgmgr_client_t *mpc = (pkgmgr_client_t *) pc;
677         retvm_if(mpc->ctype != PC_REQUEST, PKGMGR_R_EINVAL, "mpc->ctype is not PC_REQUEST\n");
678
679         ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
680         retvm_if(ret < 0, PKGMGR_R_ERROR, "pkgmgr_pkginfo_get_pkginfo failed");
681
682         ret = pkgmgrinfo_pkginfo_get_type(handle, &pkgtype);
683         tryvm_if(ret < 0, ret = PKGMGR_R_ERROR, "pkgmgr_pkginfo_get_type failed");
684
685         installer_path = _get_backend_path_with_type(pkgtype);
686         req_key = __get_req_key(pkgid);
687         req_id = _get_request_id();
688
689         snprintf(buf, 128, "%d", get_type);
690         argv[argcnt++] = installer_path;
691         /* argv[1] */
692         argv[argcnt++] = strdup("-k");
693         /* argv[2] */
694         argv[argcnt++] = req_key;
695         /* argv[3] */
696         argv[argcnt++] = strdup("-d");
697         /* argv[4] */
698         argv[argcnt++] = strdup(pkgid);
699         /* argv[5] */
700         argv[argcnt++] = strdup("-t");
701         /* argv[6] */
702         argv[argcnt++] = strdup(buf);
703
704         /*** add quote in all string for special charactor like '\n'***   FIX */
705         for (i = 0; i < argcnt; i++) {
706                 temp = g_shell_quote(argv[i]);
707                 len += (strlen(temp) + 1);
708                 g_free(temp);
709         }
710
711         args = (char *)calloc(len, sizeof(char));
712         tryvm_if(args == NULL, ret = PKGMGR_R_EINVAL, "installer_path fail");
713
714         strncpy(args, argv[0], len - 1);
715
716         for (i = 1; i < argcnt; i++) {
717                 strncat(args, " ", strlen(" "));
718                 temp = g_shell_quote(argv[i]);
719                 strncat(args, temp, strlen(temp));
720                 g_free(temp);
721         }
722         _LOGD("[args] %s [len] %d\n", args, len);
723
724         /* get cookie from security-server */
725         cookie = __get_cookie_from_security_server();
726         tryvm_if(cookie == NULL, ret = PKGMGR_R_ERROR, "__get_cookie_from_security_server is NULL");
727
728         /* request */
729         ret = comm_client_request(mpc->info.request.cc, req_key, COMM_REQ_GET_SIZE, pkgtype, pkgid, args, cookie, 1);
730         if (ret < 0)
731                 _LOGE("comm_client_request failed, ret=%d\n", ret);
732
733         ret = __sync_process(req_key);
734         if (ret < 0)
735                 _LOGE("get size failed, ret=%d\n", ret);
736
737 catch:
738         for (i = 0; i < argcnt; i++)
739                 free(argv[i]);
740
741         if(args)
742                 free(args);
743         if (cookie)
744                 free(cookie);
745
746         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
747         return ret;
748 }
749
750 static int __move_pkg_process(pkgmgr_client * pc, const char *pkgid, pkgmgr_move_type move_type, pkgmgr_handler event_cb, void *data)
751 {
752         char *req_key = NULL;
753         int req_id = 0;
754         int ret =0;
755         pkgmgrinfo_pkginfo_h handle;
756         char *pkgtype = NULL;
757         char *installer_path = NULL;
758         char *argv[PKG_ARGC_MAX] = { NULL, };
759         char *args = NULL;
760         int argcnt = 0;
761         int len = 0;
762         char *temp = NULL;
763         int i = 0;
764         char buf[128] = {'\0'};
765         char info_file[PKG_STRING_LEN_MAX] = {'\0', };
766         char *cookie = NULL;
767
768         pkgmgr_client_t *mpc = (pkgmgr_client_t *) pc;
769         retvm_if(mpc->ctype != PC_REQUEST, PKGMGR_R_EINVAL, "mpc->ctype is not PC_REQUEST\n");
770
771         ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
772         retvm_if(ret < 0, PKGMGR_R_ERROR, "pkgmgr_pkginfo_get_pkginfo failed");
773
774         ret = pkgmgrinfo_pkginfo_get_type(handle, &pkgtype);
775         tryvm_if(ret < 0, ret = PKGMGR_R_ERROR, "pkgmgr_pkginfo_get_type failed");
776
777         installer_path = _get_backend_path_with_type(pkgtype);
778         req_key = __get_req_key(pkgid);
779         req_id = _get_request_id();
780
781         /* generate argv */
782         snprintf(buf, 128, "%d", move_type);
783         /* argv[0] installer path */
784         argv[argcnt++] = installer_path;
785         /* argv[1] */
786         argv[argcnt++] = strdup("-k");
787         /* argv[2] */
788         argv[argcnt++] = req_key;
789         /* argv[3] */
790         argv[argcnt++] = strdup("-m");
791         /* argv[4] */
792         argv[argcnt++] = strdup(pkgid);
793         /* argv[5] */
794         argv[argcnt++] = strdup("-t");
795         /* argv[6] */
796         argv[argcnt++] = strdup(buf);
797         /* argv[7] */
798         argv[argcnt++] = strdup("-q");
799
800         /*** add quote in all string for special charactor like '\n'***   FIX */
801         for (i = 0; i < argcnt; i++) {
802                 temp = g_shell_quote(argv[i]);
803                 len += (strlen(temp) + 1);
804                 g_free(temp);
805         }
806
807         args = (char *)calloc(len, sizeof(char));
808         tryvm_if(args == NULL, ret = PKGMGR_R_EINVAL, "installer_path fail");
809
810         strncpy(args, argv[0], len - 1);
811
812         for (i = 1; i < argcnt; i++) {
813                 strncat(args, " ", strlen(" "));
814                 temp = g_shell_quote(argv[i]);
815                 strncat(args, temp, strlen(temp));
816                 g_free(temp);
817         }
818         _LOGD("[args] %s [len] %d\n", args, len);
819
820         /* get cookie from security-server */
821         cookie = __get_cookie_from_security_server();
822         tryvm_if(cookie == NULL, ret = PKGMGR_R_ERROR, "__get_cookie_from_security_server is NULL");
823
824         /* 6. request */
825         ret = comm_client_request(mpc->info.request.cc, req_key, COMM_REQ_TO_MOVER, pkgtype, pkgid, args, cookie, 1);
826         if (ret < 0)
827                 _LOGE("comm_client_request failed, ret=%d\n", ret);
828
829         snprintf(info_file, PKG_STRING_LEN_MAX, "app2sd_%s", pkgid);
830         ret = __sync_process(info_file);
831         if (ret != 0)
832                 _LOGE("move pkg failed, ret=%d\n", ret);
833
834 catch:
835         for (i = 0; i < argcnt; i++)
836                 free(argv[i]);
837
838         if(args)
839                 free(args);
840         if (cookie)
841                 free(cookie);
842
843         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
844         return ret;
845 }
846
847 static int __kill_app_process(pkgmgr_client * pc, const char *pkgid)
848 {
849         const char *pkgtype;
850         char *req_key;
851         char *cookie = NULL;
852         int ret;
853         pkgmgrinfo_pkginfo_h handle;
854
855         /* Check for NULL value of pc */
856         pkgmgr_client_t *mpc = (pkgmgr_client_t *) pc;
857         retvm_if(mpc->ctype != PC_REQUEST, PKGMGR_R_EINVAL, "mpc->ctype is not PC_REQUEST\n");
858
859         ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
860         retvm_if(ret < 0, PKGMGR_R_ERROR, "pkgmgr_pkginfo_get_pkginfo failed");
861
862         ret = pkgmgrinfo_pkginfo_get_type(handle, &pkgtype);
863         tryvm_if(ret < 0, ret = PKGMGR_R_ERROR, "pkgmgr_pkginfo_get_type failed");
864
865         /* 2. generate req_key */
866         req_key = __get_req_key(pkgid);
867
868         /* 3. request activate */
869         ret = comm_client_request(mpc->info.request.cc, req_key, COMM_REQ_KILL_APP, pkgtype, pkgid, NULL, NULL, 1);
870         if (ret < 0)
871                 _LOGE("request failed, ret=%d\n", ret);
872
873 catch:
874         free(req_key);
875         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
876
877         return ret;
878
879 }
880
881 API pkgmgr_client *pkgmgr_client_new(client_type ctype)
882 {
883         pkgmgr_client_t *pc = NULL;
884         int ret = -1;
885
886         if (ctype != PC_REQUEST && ctype != PC_LISTENING
887             && ctype != PC_BROADCAST)
888                 return NULL;
889
890         /* Allocate memory for ADT:pkgmgr_client */
891         pc = calloc(1, sizeof(pkgmgr_client_t));
892         if (pc == NULL) {
893                 _LOGE("No memory");
894                 return NULL;
895         }
896
897         /* Manage pc */
898         pc->ctype = ctype;
899
900         if (pc->ctype == PC_REQUEST) {
901                 pc->info.request.cc = comm_client_new();
902                 if (pc->info.request.cc == NULL) {
903                         _LOGE("client creation failed");
904                         goto err;
905                 }
906                 ret = comm_client_set_status_callback(pc->info.request.cc,
907                                                       __operation_callback, pc);
908                 if (ret < 0) {
909                         _LOGE("comm_client_set_status_callback() failed - %d",
910                               ret);
911                         goto err;
912                 }
913         } else if (pc->ctype == PC_LISTENING) {
914                 pc->info.listening.cc = comm_client_new();
915                 if (pc->info.listening.cc == NULL) {
916                         _LOGE("client creation failed");
917                         goto err;
918                 }
919                 ret = comm_client_set_status_callback(pc->info.listening.cc,
920                                                       __status_callback, pc);
921                 if (ret < 0) {
922                         _LOGE("comm_client_set_status_callback() failed - %d",
923                               ret);
924                         goto err;
925                 }
926         } else if (pc->ctype == PC_BROADCAST) {
927                 pc->info.broadcast.bc = comm_status_broadcast_server_connect();
928                 if (pc->info.broadcast.bc == NULL) {
929                         _LOGE("client creation failed");
930                         goto err;
931                 }
932                 ret = 0;
933         }
934
935         return (pkgmgr_client *) pc;
936
937  err:
938         if (pc)
939                 free(pc);
940         return NULL;
941 }
942
943 API int pkgmgr_client_free(pkgmgr_client *pc)
944 {
945         int ret = -1;
946         pkgmgr_client_t *mpc = (pkgmgr_client_t *) pc;
947
948         if (mpc == NULL) {
949                 _LOGE("Invalid argument");
950                 return PKGMGR_R_EINVAL;
951         }
952
953         if (mpc->ctype == PC_REQUEST) {
954                 req_cb_info *tmp;
955                 req_cb_info *prev;
956                 for (tmp = mpc->info.request.rhead; tmp;) {
957                         prev = tmp;
958                         tmp = tmp->next;
959                         free(prev);
960                 }
961
962                 ret = comm_client_free(mpc->info.request.cc);
963                 if (ret < 0) {
964                         _LOGE("comm_client_free() failed - %d", ret);
965                         goto err;
966                 }
967         } else if (mpc->ctype == PC_LISTENING) {
968                 listen_cb_info *tmp;
969                 listen_cb_info *prev;
970                 for (tmp = mpc->info.listening.lhead; tmp;) {
971                         prev = tmp;
972                         tmp = tmp->next;
973                         free(prev);
974                 }
975
976                 ret = comm_client_free(mpc->info.listening.cc);
977                 if (ret < 0) {
978                         _LOGE("comm_client_free() failed - %d", ret);
979                         goto err;
980                 }
981         } else if (mpc->ctype == PC_BROADCAST) {
982                 comm_status_broadcast_server_disconnect(mpc->info.broadcast.bc);
983                 ret = 0;
984         } else {
985                 _LOGE("Invalid client type\n");
986                 return PKGMGR_R_EINVAL;
987         }
988
989         free(mpc);
990         mpc = NULL;
991         return PKGMGR_R_OK;
992
993  err:
994         if (mpc) {
995                 free(mpc);
996                 mpc = NULL;
997         }
998         return PKGMGR_R_ERROR;
999 }
1000
1001 API int pkgmgr_client_install(pkgmgr_client * pc, const char *pkg_type,
1002                               const char *descriptor_path, const char *pkg_path,
1003                               const char *optional_file, pkgmgr_mode mode,
1004                               pkgmgr_handler event_cb, void *data)
1005 {
1006         char *pkgtype = NULL;
1007         char *installer_path = NULL;
1008         char *req_key = NULL;
1009         int req_id = 0;
1010         int i = 0;
1011         char *argv[PKG_ARGC_MAX] = { NULL, };
1012         char *args = NULL;
1013         int argcnt = 0;
1014         int len = 0;
1015         char *temp = NULL;
1016         int ret = 0;
1017         char *cookie = NULL;
1018
1019         /* Check for NULL value of pc */
1020         retvm_if(pc == NULL, PKGMGR_R_EINVAL, "package manager client handle is NULL");
1021
1022         /* 0. check the pc type */
1023         pkgmgr_client_t *mpc = (pkgmgr_client_t *) pc;
1024         retvm_if(mpc->ctype != PC_REQUEST, PKGMGR_R_EINVAL, "mpc->ctype is not PC_REQUEST");
1025
1026         /* 1. check argument */
1027         if (descriptor_path) {
1028                 retvm_if(strlen(descriptor_path) >= PKG_STRING_LEN_MAX, PKGMGR_R_EINVAL, "descriptor_path over PKG_STRING_LEN_MAX");
1029                 retvm_if(access(descriptor_path, F_OK) != 0, PKGMGR_R_EINVAL, "descriptor_path access fail");
1030         }
1031
1032         retvm_if(pkg_path == NULL, PKGMGR_R_EINVAL, "pkg_path is NULL");
1033         retvm_if(strlen(pkg_path) >= PKG_STRING_LEN_MAX, PKGMGR_R_EINVAL, "pkg_path over PKG_STRING_LEN_MAX");
1034         retvm_if(access(pkg_path, F_OK) != 0, PKGMGR_R_EINVAL, "pkg_path access fail");
1035
1036         if (optional_file)
1037                 retvm_if(strlen(optional_file) >= PKG_STRING_LEN_MAX, PKGMGR_R_EINVAL, "optional_file over PKG_STRING_LEN_MAX");
1038
1039         /* 2. get installer path using pkg_path */
1040         if (pkg_type) {
1041                 installer_path = _get_backend_path_with_type(pkg_type);
1042                 pkgtype = strdup(pkg_type);
1043         } else {
1044                 installer_path = _get_backend_path(pkg_path);
1045                 pkgtype = __get_type_from_path(pkg_path);
1046         }
1047         if (installer_path == NULL) {
1048                 free(pkgtype);
1049                 _LOGE("installer_path is NULL\n");
1050                 return PKGMGR_R_EINVAL;
1051         }
1052
1053         /* 3. generate req_key */
1054         req_key = __get_req_key(pkg_path);
1055
1056         /* 4. add callback info - add callback info to pkgmgr_client */
1057         req_id = _get_request_id();
1058         __add_op_cbinfo(mpc, req_id, req_key, event_cb, data);
1059
1060         /* 5. generate argv */
1061
1062         /*  argv[0] installer path */
1063         argv[argcnt++] = installer_path;
1064         /* argv[1] */
1065         argv[argcnt++] = strdup("-k");
1066         /* argv[2] */
1067         argv[argcnt++] = req_key;
1068         /* argv[3] */
1069         argv[argcnt++] = strdup("-i");
1070         /* argv[(4)] if exists */
1071         if (descriptor_path)
1072                 argv[argcnt++] = strdup(descriptor_path);
1073         /* argv[4] */
1074         argv[argcnt++] = strdup(pkg_path);
1075         /* argv[(5)] if exists */
1076         if (optional_file){
1077                 argv[argcnt++] = strdup("-o");
1078                 argv[argcnt++] = strdup(optional_file);
1079         }
1080         /* argv[6] -q option should be located at the end of command !! */
1081         if (mode == PM_QUIET)
1082                 argv[argcnt++] = strdup("-q");
1083
1084         /*** add quote in all string for special charactor like '\n'***   FIX */
1085         for (i = 0; i < argcnt; i++) {
1086                 temp = g_shell_quote(argv[i]);
1087                 len += (strlen(temp) + 1);
1088                 g_free(temp);
1089         }
1090
1091         args = (char *)calloc(len, sizeof(char));
1092         tryvm_if(args == NULL, ret = PKGMGR_R_ERROR, "calloc failed");
1093
1094         strncpy(args, argv[0], len - 1);
1095
1096         for (i = 1; i < argcnt; i++) {
1097                 strncat(args, " ", strlen(" "));
1098                 temp = g_shell_quote(argv[i]);
1099                 strncat(args, temp, strlen(temp));
1100                 g_free(temp);
1101         }
1102         _LOGD("[args] %s [len] %d\n", args, len);
1103
1104         /* get cookie from security-server */
1105         cookie = __get_cookie_from_security_server();
1106         tryvm_if(cookie == NULL, ret = PKGMGR_R_ERROR, "__get_cookie_from_security_server is NULL");
1107         /******************* end of quote ************************/
1108
1109         /* 6. request install */
1110         ret = comm_client_request(mpc->info.request.cc, req_key, COMM_REQ_TO_INSTALLER, pkgtype, pkg_path, args, cookie, 1);
1111         tryvm_if(ret < 0, ret = PKGMGR_R_ECOMM, "request failed, ret=%d", ret);
1112
1113         ret = req_id;
1114
1115 catch:
1116         for (i = 0; i < argcnt; i++)
1117                 free(argv[i]);
1118
1119         if (args)
1120                 free(args);
1121         if (pkgtype)
1122                 free(pkgtype);
1123         if (cookie)
1124                 free(cookie);
1125
1126         return ret;
1127 }
1128
1129 API int pkgmgr_client_reinstall(pkgmgr_client * pc, const char *pkg_type, const char *pkgid,
1130                                   const char *optional_file, pkgmgr_mode mode,
1131                               pkgmgr_handler event_cb, void *data)
1132 {
1133         char *pkgtype = NULL;
1134         char *installer_path = NULL;
1135         char *req_key = NULL;
1136         int req_id = 0;
1137         int i = 0;
1138         char *argv[PKG_ARGC_MAX] = { NULL, };
1139         char *args = NULL;
1140         int argcnt = 0;
1141         int len = 0;
1142         char *temp = NULL;
1143         int ret = 0;
1144         char *cookie = NULL;
1145
1146         /* Check for NULL value of pc */
1147         retvm_if(pc == NULL, PKGMGR_R_EINVAL, "package manager client handle is NULL\n");
1148
1149         pkgmgr_client_t *mpc = (pkgmgr_client_t *) pc;
1150
1151         /* 0. check the pc type */
1152         retv_if(mpc->ctype != PC_REQUEST, PKGMGR_R_EINVAL);
1153
1154
1155         /* 1. check argument */
1156         retv_if(pkgid == NULL, PKGMGR_R_EINVAL);
1157         retv_if(strlen(pkgid) >= PKG_STRING_LEN_MAX, PKGMGR_R_EINVAL);
1158         if (optional_file) {
1159                 if (strlen(optional_file) >= PKG_STRING_LEN_MAX)
1160                         return PKGMGR_R_EINVAL;
1161         }
1162
1163         /* 2. get installer path using pkg_path */
1164         installer_path = _get_backend_path_with_type(pkg_type);
1165         pkgtype = strdup(pkg_type);
1166         tryvm_if(installer_path == NULL, ret = PKGMGR_R_EINVAL, "installer_path is null");
1167
1168         /* 3. generate req_key */
1169         req_key = __get_req_key(pkgid);
1170
1171         /* 4. add callback info - add callback info to pkgmgr_client */
1172         req_id = _get_request_id();
1173         __add_op_cbinfo(mpc, req_id, req_key, event_cb, data);
1174
1175         /* 5. generate argv */
1176
1177         /*  argv[0] installer path */
1178         argv[argcnt++] = installer_path;
1179         /* argv[1] */
1180         argv[argcnt++] = strdup("-k");
1181         /* argv[2] */
1182         argv[argcnt++] = req_key;
1183         /* argv[3] */
1184         argv[argcnt++] = strdup("-r");
1185         /* argv[4] */
1186         argv[argcnt++] = strdup(pkgid);
1187         /* argv[(5)] if exists */
1188         if (optional_file){
1189                 argv[argcnt++] = strdup("-o");
1190                 argv[argcnt++] = strdup(optional_file);
1191         }
1192
1193         /* argv[5] -q option should be located at the end of command !! */
1194         if (mode == PM_QUIET)
1195                 argv[argcnt++] = strdup("-q");
1196
1197         /*** add quote in all string for special charactor like '\n'***   FIX */
1198         for (i = 0; i < argcnt; i++) {
1199                 temp = g_shell_quote(argv[i]);
1200                 len += (strlen(temp) + 1);
1201                 g_free(temp);
1202         }
1203
1204         args = (char *)calloc(len, sizeof(char));
1205         tryvm_if(args == NULL, ret = PKGMGR_R_ERROR, "calloc failed");
1206
1207         strncpy(args, argv[0], len - 1);
1208
1209         for (i = 1; i < argcnt; i++) {
1210                 strncat(args, " ", strlen(" "));
1211                 temp = g_shell_quote(argv[i]);
1212                 strncat(args, temp, strlen(temp));
1213                 g_free(temp);
1214         }
1215         _LOGD("[args] %s [len] %d\n", args, len);
1216
1217         /* get cookie from security-server */
1218         cookie = __get_cookie_from_security_server();
1219         tryvm_if(cookie == NULL, ret = PKGMGR_R_ERROR, "__get_cookie_from_security_server is NULL");
1220         /******************* end of quote ************************/
1221
1222         /* 6. request install */
1223         ret = comm_client_request(mpc->info.request.cc, req_key, COMM_REQ_TO_INSTALLER, pkgtype, pkgid, args, cookie, 1);
1224         tryvm_if(ret < 0, ret = PKGMGR_R_ECOMM, "request failed");
1225
1226         ret = req_id;
1227
1228 catch:
1229         for (i = 0; i < argcnt; i++)
1230                 free(argv[i]);
1231
1232         if (args)
1233                 free(args);
1234         if (pkgtype)
1235                 free(pkgtype);
1236         if (cookie)
1237                 free(cookie);
1238
1239         return ret;
1240 }
1241
1242 API int pkgmgr_client_uninstall(pkgmgr_client *pc, const char *pkg_type,
1243                                 const char *pkgid, pkgmgr_mode mode,
1244                                 pkgmgr_handler event_cb, void *data)
1245 {
1246         char *pkgtype;
1247         char *installer_path;
1248         char *req_key;
1249         int req_id;
1250         int i = 0;
1251         char *argv[PKG_ARGC_MAX] = { NULL, };
1252         char *args = NULL;
1253         int argcnt = 0;
1254         int len = 0;
1255         char *temp = NULL;
1256         int ret = -1;
1257         char *cookie = NULL;
1258
1259         /* Check for NULL value of pc */
1260         retvm_if(pc == NULL, PKGMGR_R_EINVAL, "package manager client handle is NULL\n");
1261
1262         pkgmgr_client_t *mpc = (pkgmgr_client_t *) pc;
1263
1264         /* 0. check the pc type */
1265         retv_if(mpc->ctype != PC_REQUEST, PKGMGR_R_EINVAL);
1266
1267         /* 1. check argument */
1268         retv_if(pkgid == NULL, PKGMGR_R_EINVAL);
1269
1270         pkgmgr_pkginfo_h handle;
1271         ret = pkgmgr_pkginfo_get_pkginfo(pkgid, &handle);
1272
1273         /*check package id      */
1274         tryvm_if(ret < 0, ret = PKGMGR_R_EINVAL, "pkgmgr_pkginfo_get_pkginfo fail");
1275         tryvm_if(handle == NULL, ret = PKGMGR_R_EINVAL, "Pkgid(%s) can not find in installed pkg DB! \n", pkgid);
1276
1277         /*check running app , terminate app if it is running*/
1278         ret = pkgmgr_appinfo_get_list(handle, PM_UI_APP, __app_list_cb, NULL);
1279         tryvm_if(ret < 0, ret = PKGMGR_R_EINVAL, "pkgmgr_appinfo_get_list : PM_UI_APP fail");
1280
1281         /*check running app , terminate app if it is running*/
1282         ret = pkgmgr_appinfo_get_list(handle, PM_SVC_APP, __app_list_cb, NULL);
1283         tryvm_if(ret < 0, ret = PKGMGR_R_EINVAL, "pkgmgr_appinfo_get_list : PM_SVC_APP fail");
1284
1285         /*check type    */
1286         ret = pkgmgr_pkginfo_get_type(handle, &pkgtype);
1287         tryvm_if(ret < 0, ret = PKGMGR_R_EINVAL, "pkgmgr_pkginfo_get_type fail");
1288         tryvm_if(pkgtype == NULL, ret = PKGMGR_R_ERROR, "pkgtype is NULL");
1289
1290         /*check pkgid length    */
1291         tryvm_if(strlen(pkgid) >= PKG_STRING_LEN_MAX, ret = PKGMGR_R_EINVAL, "pkgid is too long");
1292
1293         /* 2. get installer path using pkgtype */
1294         installer_path = _get_backend_path_with_type(pkgtype);
1295         tryvm_if(installer_path == NULL, ret = PKGMGR_R_EINVAL, "installer_path fail");
1296
1297         /* 3. generate req_key */
1298         req_key = __get_req_key(pkgid);
1299
1300         /* 4. add callback info - add callback info to pkgmgr_client */
1301         req_id = _get_request_id();
1302         __add_op_cbinfo(mpc, req_id, req_key, event_cb, data);
1303
1304         /* 5. generate argv */
1305
1306         /* argv[0] installer path */
1307         argv[argcnt++] = installer_path;
1308         /* argv[1] */
1309         argv[argcnt++] = strdup("-k");
1310         /* argv[2] */
1311         argv[argcnt++] = req_key;
1312         /* argv[3] */
1313         argv[argcnt++] = strdup("-d");
1314         /* argv[4] */
1315         argv[argcnt++] = strdup(pkgid);
1316         /* argv[5] -q option should be located at the end of command !! */
1317         if (mode == PM_QUIET)
1318                 argv[argcnt++] = strdup("-q");
1319
1320         /*** add quote in all string for special charactor like '\n'***   FIX */
1321         for (i = 0; i < argcnt; i++) {
1322                 temp = g_shell_quote(argv[i]);
1323                 len += (strlen(temp) + 1);
1324                 g_free(temp);
1325         }
1326
1327         args = (char *)calloc(len, sizeof(char));
1328         tryvm_if(args == NULL, ret = PKGMGR_R_ERROR, "calloc failed");
1329
1330         strncpy(args, argv[0], len - 1);
1331
1332         for (i = 1; i < argcnt; i++) {
1333                 strncat(args, " ", strlen(" "));
1334                 temp = g_shell_quote(argv[i]);
1335                 strncat(args, temp, strlen(temp));
1336                 g_free(temp);
1337         }
1338         _LOGD("[args] %s [len] %d\n", args, len);
1339
1340         /* get cookie from security-server */
1341         cookie = __get_cookie_from_security_server();
1342         tryvm_if(cookie == NULL, ret = PKGMGR_R_ERROR, "__get_cookie_from_security_server is NULL");
1343         /******************* end of quote ************************/
1344
1345         /* 6. request install */
1346         ret = comm_client_request(mpc->info.request.cc, req_key, COMM_REQ_TO_INSTALLER, pkgtype, pkgid, args, cookie, 1);
1347         tryvm_if(ret < 0, ret = PKGMGR_R_ECOMM, "calloc failed");
1348
1349         for (i = 0; i < argcnt; i++)
1350                 free(argv[i]);
1351
1352         free(args);
1353
1354         pkgmgr_pkginfo_destroy_pkginfo(handle);
1355         return req_id;
1356
1357 catch:
1358         for (i = 0; i < argcnt; i++)
1359                 free(argv[i]);
1360
1361         if(args)
1362                 free(args);
1363         if (cookie)
1364                 free(cookie);
1365
1366         pkgmgr_pkginfo_destroy_pkginfo(handle);
1367         PKGMGR_END();\
1368         return ret;
1369 }
1370
1371 API int pkgmgr_client_move(pkgmgr_client *pc, const char *pkg_type,
1372                                 const char *pkgid, pkgmgr_move_type move_type, pkgmgr_mode mode)
1373 {
1374         const char *pkgtype = NULL;
1375         char *installer_path = NULL;
1376         char *req_key = NULL;
1377         int i = 0;
1378         char *argv[PKG_ARGC_MAX] = { NULL, };
1379         char *args = NULL;
1380         int argcnt = 0;
1381         int len = 0;
1382         char *temp = NULL;
1383         int ret = 0;
1384         int req_id = 0;
1385         char *cookie = NULL;
1386         char buf[128] = {'\0'};
1387
1388         /* Check for NULL value of pc */
1389         if (pc == NULL) {
1390                 _LOGD("package manager client handle is NULL\n");
1391                 return PKGMGR_R_EINVAL;
1392         }
1393         pkgmgr_client_t *mpc = (pkgmgr_client_t *) pc;
1394         /*check the pc type */
1395         if (mpc->ctype != PC_REQUEST)
1396                 return PKGMGR_R_EINVAL;
1397
1398         /*check argument */
1399         if (pkgid == NULL)
1400                 return PKGMGR_R_EINVAL;
1401
1402         if (pkg_type == NULL) {
1403                 pkgtype = _get_pkg_type_from_desktop_file(pkgid);
1404                 if (pkgtype == NULL)
1405                         return PKGMGR_R_EINVAL;
1406         } else
1407                 pkgtype = pkg_type;
1408
1409         if (strlen(pkgid) >= PKG_STRING_LEN_MAX)
1410                 return PKGMGR_R_EINVAL;
1411
1412         if ((move_type < PM_MOVE_TO_INTERNAL) || (move_type > PM_MOVE_TO_SDCARD))
1413                 return PKGMGR_R_EINVAL;
1414
1415         /* get installer path using pkg_path */
1416         installer_path = _get_backend_path_with_type(pkgtype);
1417         if (installer_path == NULL)
1418                 return PKGMGR_R_EINVAL;
1419
1420         /* generate req_key */
1421         req_key = __get_req_key(pkgid);
1422         req_id = _get_request_id();
1423
1424         /* generate argv */
1425         snprintf(buf, 128, "%d", move_type);
1426         /* argv[0] installer path */
1427         argv[argcnt++] = installer_path;
1428         /* argv[1] */
1429         argv[argcnt++] = strdup("-k");
1430         /* argv[2] */
1431         argv[argcnt++] = req_key;
1432         /* argv[3] */
1433         argv[argcnt++] = strdup("-m");
1434         /* argv[4] */
1435         argv[argcnt++] = strdup(pkgid);
1436         /* argv[5] */
1437         argv[argcnt++] = strdup("-t");
1438         /* argv[6] */
1439         argv[argcnt++] = strdup(buf);
1440         /* argv[7] -q option should be located at the end of command !! */
1441         if (mode == PM_QUIET)
1442                 argv[argcnt++] = strdup("-q");
1443
1444         /*** add quote in all string for special charactor like '\n'***   FIX */
1445         for (i = 0; i < argcnt; i++) {
1446                 temp = g_shell_quote(argv[i]);
1447                 len += (strlen(temp) + 1);
1448                 g_free(temp);
1449         }
1450
1451         args = (char *)calloc(len, sizeof(char));
1452         if (args == NULL) {
1453                 _LOGD("calloc failed");
1454
1455                 for (i = 0; i < argcnt; i++)
1456                         free(argv[i]);
1457
1458                 return PKGMGR_R_ERROR;
1459         }
1460         strncpy(args, argv[0], len - 1);
1461
1462         for (i = 1; i < argcnt; i++) {
1463                 strncat(args, " ", strlen(" "));
1464                 temp = g_shell_quote(argv[i]);
1465                 strncat(args, temp, strlen(temp));
1466                 g_free(temp);
1467         }
1468         _LOGD("[args] %s [len] %d\n", args, len);
1469         /******************* end of quote ************************/
1470
1471         /* 6. request install */
1472         ret = comm_client_request(mpc->info.request.cc, req_key,
1473                                   COMM_REQ_TO_MOVER, pkgtype, pkgid,
1474                                   args, cookie, 1);
1475         if (ret < 0) {
1476                 _LOGE("request failed, ret=%d\n", ret);
1477
1478                 for (i = 0; i < argcnt; i++)
1479                         free(argv[i]);
1480
1481                 free(args);
1482                 return PKGMGR_R_ECOMM;
1483         }
1484
1485         for (i = 0; i < argcnt; i++)
1486                 free(argv[i]);
1487
1488         if (args)
1489                 free(args);
1490
1491         return req_id;
1492 }
1493
1494 API int pkgmgr_client_activate(pkgmgr_client * pc, const char *pkg_type,
1495                                const char *pkgid)
1496 {
1497         const char *pkgtype;
1498         char *req_key;
1499         char *cookie = NULL;
1500         int ret;
1501         /* Check for NULL value of pc */
1502         if (pc == NULL) {
1503                 _LOGD("package manager client handle is NULL\n");
1504                 return PKGMGR_R_EINVAL;
1505         }
1506         pkgmgr_client_t *mpc = (pkgmgr_client_t *) pc;
1507
1508         /* 0. check the pc type */
1509         if (mpc->ctype != PC_REQUEST)
1510                 return PKGMGR_R_EINVAL;
1511
1512         /* 1. check argument */
1513         if (pkgid == NULL)
1514                 return PKGMGR_R_EINVAL;
1515
1516         if (pkg_type == NULL) {
1517                 pkgtype = _get_pkg_type_from_desktop_file(pkgid);
1518                 if (pkgtype == NULL)
1519                         return PKGMGR_R_EINVAL;
1520         } else
1521                 pkgtype = pkg_type;
1522
1523         if (strlen(pkgid) >= PKG_STRING_LEN_MAX)
1524                 return PKGMGR_R_EINVAL;
1525
1526         /* 2. generate req_key */
1527         req_key = __get_req_key(pkgid);
1528
1529         /* 3. request activate */
1530         ret = comm_client_request(mpc->info.request.cc, req_key,
1531                                   COMM_REQ_TO_ACTIVATOR, pkgtype,
1532                                   pkgid, "1 PKG", cookie, 1);
1533         if (ret < 0) {
1534                 _LOGE("request failed, ret=%d\n", ret);
1535                 free(req_key);
1536                 return PKGMGR_R_ECOMM;
1537         }
1538
1539         free(req_key);
1540
1541         return PKGMGR_R_OK;
1542 }
1543
1544 API int pkgmgr_client_deactivate(pkgmgr_client *pc, const char *pkg_type,
1545                                  const char *pkgid)
1546 {
1547         const char *pkgtype;
1548         char *req_key;
1549         char *cookie = NULL;
1550         int ret;
1551         /* Check for NULL value of pc */
1552         if (pc == NULL) {
1553                 _LOGD("package manager client handle is NULL\n");
1554                 return PKGMGR_R_EINVAL;
1555         }
1556         pkgmgr_client_t *mpc = (pkgmgr_client_t *) pc;
1557
1558         /* 0. check the pc type */
1559         if (mpc->ctype != PC_REQUEST)
1560                 return PKGMGR_R_EINVAL;
1561
1562         /* 1. check argument */
1563         if (pkgid == NULL)
1564                 return PKGMGR_R_EINVAL;
1565
1566         if (pkg_type == NULL) {
1567                 pkgtype = _get_pkg_type_from_desktop_file(pkgid);
1568                 if (pkgtype == NULL)
1569                         return PKGMGR_R_EINVAL;
1570         } else
1571                 pkgtype = pkg_type;
1572
1573         if (strlen(pkgid) >= PKG_STRING_LEN_MAX)
1574                 return PKGMGR_R_EINVAL;
1575
1576         /* 2. generate req_key */
1577         req_key = __get_req_key(pkgid);
1578
1579         /* 3. request activate */
1580         ret = comm_client_request(mpc->info.request.cc, req_key,
1581                                   COMM_REQ_TO_ACTIVATOR, pkgtype,
1582                                   pkgid, "0 PKG", cookie, 1);
1583         if (ret < 0) {
1584                 _LOGE("request failed, ret=%d\n", ret);
1585                 free(req_key);
1586                 return PKGMGR_R_ECOMM;
1587         }
1588
1589         free(req_key);
1590
1591         return PKGMGR_R_OK;
1592 }
1593
1594 API int pkgmgr_client_activate_app(pkgmgr_client * pc, const char *appid)
1595 {
1596         const char *pkgtype;
1597         char *req_key;
1598         char *cookie = NULL;
1599         int ret;
1600         /* Check for NULL value of pc */
1601         if (pc == NULL) {
1602                 _LOGD("package manager client handle is NULL\n");
1603                 return PKGMGR_R_EINVAL;
1604         }
1605         pkgmgr_client_t *mpc = (pkgmgr_client_t *) pc;
1606
1607         /* 0. check the pc type */
1608         if (mpc->ctype != PC_REQUEST)
1609                 return PKGMGR_R_EINVAL;
1610
1611         /* 1. check argument */
1612         if (appid == NULL)
1613                 return PKGMGR_R_EINVAL;
1614
1615         if (strlen(appid) >= PKG_STRING_LEN_MAX)
1616                 return PKGMGR_R_EINVAL;
1617
1618         pkgtype = _get_pkg_type_from_desktop_file(appid);
1619
1620         /* 2. generate req_key */
1621         req_key = __get_req_key(appid);
1622
1623         /* 3. request activate */
1624         ret = comm_client_request(mpc->info.request.cc, req_key,
1625                                   COMM_REQ_TO_ACTIVATOR, pkgtype,
1626                                   appid, "1 APP", cookie, 1);
1627         if (ret < 0) {
1628                 _LOGE("request failed, ret=%d\n", ret);
1629                 free(req_key);
1630                 return PKGMGR_R_ECOMM;
1631         }
1632
1633         free(req_key);
1634
1635         return PKGMGR_R_OK;
1636 }
1637
1638 API int pkgmgr_client_activate_appv(pkgmgr_client * pc, const char *appid, char *const argv[])
1639 {
1640         const char *pkgtype;
1641         char *req_key;
1642         char *cookie = NULL;
1643         int ret;
1644         int i = 0;
1645         char *temp = NULL;
1646         int len = 0;
1647         int argcnt = 0;
1648         char *args = NULL;
1649         char *argsr = NULL;
1650         /* Check for NULL value of pc */
1651         if (pc == NULL) {
1652                 _LOGD("package manager client handle is NULL\n");
1653                 return PKGMGR_R_EINVAL;
1654         }
1655         pkgmgr_client_t *mpc = (pkgmgr_client_t *) pc;
1656
1657         /* 0. check the pc type */
1658         if (mpc->ctype != PC_REQUEST)
1659                 return PKGMGR_R_EINVAL;
1660
1661         /* 1. check argument */
1662         if (appid == NULL)
1663                 return PKGMGR_R_EINVAL;
1664
1665         if (strlen(appid) >= PKG_STRING_LEN_MAX)
1666                 return PKGMGR_R_EINVAL;
1667
1668         pkgtype = _get_pkg_type_from_desktop_file(appid);
1669
1670         /* 2. generate req_key */
1671         req_key = __get_req_key(appid);
1672
1673         /*** add quote in all string for special charactor like '\n'***   FIX */
1674         if (argv) {
1675                 for (i = 0; argv[i]; i++) {
1676                         temp = g_shell_quote(argv[i]);
1677                         len += (strlen(temp) + 1);
1678                         g_free(temp);
1679                         argcnt++;
1680                 }
1681
1682                 if (argcnt) {
1683                         args = (char *)calloc(len, sizeof(char));
1684                         if (args == NULL) {
1685                                 _LOGE("calloc failed");
1686                                 free(req_key);
1687                                 return PKGMGR_R_ERROR;
1688                         }
1689                         strncpy(args, argv[0], len - 1);
1690
1691                         for (i = 1; i < argcnt; i++) {
1692                                 strncat(args, " ", strlen(" "));
1693                                 temp = g_shell_quote(argv[i]);
1694                                 strncat(args, temp, strlen(temp));
1695                                 g_free(temp);
1696                         }
1697                 }
1698         }
1699
1700         argsr = (char *)calloc(strlen("1 APP")+2+len, sizeof(char));
1701         if (argsr == NULL) {
1702                 _LOGE("calloc failed");
1703                 free(req_key);
1704                 free(args);
1705                 return PKGMGR_R_ERROR;
1706         }
1707         strncpy(argsr, "1 APP", strlen("1 APP"));
1708         if (argcnt) {
1709                 strncat(argsr, " ", strlen(" "));
1710                 strncat(argsr, args, strlen(args));
1711         }
1712
1713         _LOGD("argsr [%s]\n", argsr);
1714         /******************* end of quote ************************/
1715
1716         /* 3. request activate */
1717         ret = comm_client_request(mpc->info.request.cc, req_key,
1718                                   COMM_REQ_TO_ACTIVATOR, pkgtype,
1719                                   appid, argsr, cookie, 1);
1720         if (ret < 0) {
1721                 _LOGE("request failed, ret=%d\n", ret);
1722                 free(req_key);
1723                 free(args);
1724                 free(argsr);
1725                 return PKGMGR_R_ECOMM;
1726         }
1727
1728         free(req_key);
1729         free(args);
1730         free(argsr);
1731
1732         return PKGMGR_R_OK;
1733 }
1734
1735 API int pkgmgr_client_deactivate_app(pkgmgr_client *pc, const char *appid)
1736 {
1737         const char *pkgtype;
1738         char *req_key;
1739         char *cookie = NULL;
1740         int ret;
1741         /* Check for NULL value of pc */
1742         if (pc == NULL) {
1743                 _LOGD("package manager client handle is NULL\n");
1744                 return PKGMGR_R_EINVAL;
1745         }
1746         pkgmgr_client_t *mpc = (pkgmgr_client_t *) pc;
1747
1748         /* 0. check the pc type */
1749         if (mpc->ctype != PC_REQUEST)
1750                 return PKGMGR_R_EINVAL;
1751
1752         /* 1. check argument */
1753         if (appid == NULL)
1754                 return PKGMGR_R_EINVAL;
1755
1756         if (strlen(appid) >= PKG_STRING_LEN_MAX)
1757                 return PKGMGR_R_EINVAL;
1758
1759         pkgtype = _get_pkg_type_from_desktop_file(appid);
1760
1761         /* 2. generate req_key */
1762         req_key = __get_req_key(appid);
1763
1764         /* 3. request activate */
1765         ret = comm_client_request(mpc->info.request.cc, req_key,
1766                                   COMM_REQ_TO_ACTIVATOR, pkgtype,
1767                                   appid, "0 APP", cookie, 1);
1768         if (ret < 0) {
1769                 _LOGE("request failed, ret=%d\n", ret);
1770                 free(req_key);
1771                 return PKGMGR_R_ECOMM;
1772         }
1773
1774         free(req_key);
1775
1776         return PKGMGR_R_OK;
1777 }
1778
1779
1780 API int pkgmgr_client_clear_user_data(pkgmgr_client *pc, const char *pkg_type,
1781                                       const char *appid, pkgmgr_mode mode)
1782 {
1783         const char *pkgtype;
1784         char *installer_path;
1785         char *req_key;
1786         int i = 0;
1787         char *argv[PKG_ARGC_MAX] = { NULL, };
1788         char *args = NULL;
1789         int argcnt = 0;
1790         int len = 0;
1791         char *temp = NULL;
1792         int ret;
1793         char *cookie = NULL;
1794
1795         /* Check for NULL value of pc */
1796         if (pc == NULL) {
1797                 _LOGD("package manager client handle is NULL\n");
1798                 return PKGMGR_R_EINVAL;
1799         }
1800         pkgmgr_client_t *mpc = (pkgmgr_client_t *) pc;
1801
1802         /* 0. check the pc type */
1803         if (mpc->ctype != PC_REQUEST)
1804                 return PKGMGR_R_EINVAL;
1805
1806         /* 1. check argument */
1807         if (appid == NULL)
1808                 return PKGMGR_R_EINVAL;
1809
1810
1811         if (pkg_type == NULL) {
1812                 pkgtype = _get_pkg_type_from_desktop_file(appid);
1813                 if (pkgtype == NULL)
1814                         return PKGMGR_R_EINVAL;
1815         } else
1816                 pkgtype = pkg_type;
1817
1818         if (strlen(appid) >= PKG_STRING_LEN_MAX)
1819                 return PKGMGR_R_EINVAL;
1820
1821         /* 2. get installer path using pkg_path */
1822         installer_path = _get_backend_path_with_type(pkgtype);
1823         if (installer_path == NULL)
1824                 return PKGMGR_R_EINVAL;
1825
1826         /* 3. generate req_key */
1827         req_key = __get_req_key(appid);
1828
1829         /* 4. generate argv */
1830
1831         /* argv[0] installer path */
1832         argv[argcnt++] = installer_path;
1833         /* argv[1] */
1834         argv[argcnt++] = strdup("-k");
1835         /* argv[2] */
1836         argv[argcnt++] = req_key;
1837         /* argv[3] */
1838         argv[argcnt++] = strdup("-c");
1839         /* argv[4] */
1840         argv[argcnt++] = strdup(appid);
1841         /* argv[5] -q option should be located at the end of command !! */
1842         if (mode == PM_QUIET)
1843                 argv[argcnt++] = strdup("-q");
1844
1845         /*** add quote in all string for special charactor like '\n'***   FIX */
1846         for (i = 0; i < argcnt; i++) {
1847                 temp = g_shell_quote(argv[i]);
1848                 len += (strlen(temp) + 1);
1849                 g_free(temp);
1850         }
1851
1852         args = (char *)calloc(len, sizeof(char));
1853         if (args == NULL) {
1854                 _LOGD("calloc failed");
1855
1856                 for (i = 0; i < argcnt; i++)
1857                         free(argv[i]);
1858
1859                 return PKGMGR_R_ERROR;
1860         }
1861         strncpy(args, argv[0], len - 1);
1862
1863         for (i = 1; i < argcnt; i++) {
1864                 strncat(args, " ", strlen(" "));
1865                 temp = g_shell_quote(argv[i]);
1866                 strncat(args, temp, strlen(temp));
1867                 g_free(temp);
1868         }
1869         _LOGD("[args] %s [len] %d\n", args, len);
1870         /******************* end of quote ************************/
1871
1872         /* 6. request clear */
1873         ret = comm_client_request(mpc->info.request.cc, req_key,
1874                                   COMM_REQ_TO_CLEARER, pkgtype, appid,
1875                                   args, cookie, 1);
1876         if (ret < 0) {
1877                 _LOGE("request failed, ret=%d\n", ret);
1878
1879                 for (i = 0; i < argcnt; i++)
1880                         free(argv[i]);
1881
1882                 free(args);
1883                 return PKGMGR_R_ECOMM;
1884         }
1885
1886         for (i = 0; i < argcnt; i++)
1887                 free(argv[i]);
1888
1889         free(args);
1890
1891         return PKGMGR_R_OK;
1892 }
1893
1894 API int pkgmgr_client_listen_status(pkgmgr_client *pc, pkgmgr_handler event_cb,
1895                                     void *data)
1896 {
1897         int req_id;
1898         /* Check for NULL value of pc */
1899         if (pc == NULL) {
1900                 _LOGD("package manager client handle is NULL\n");
1901                 return PKGMGR_R_EINVAL;
1902         }
1903         pkgmgr_client_t *mpc = (pkgmgr_client_t *) pc;
1904
1905         /* 0. check the pc type */
1906         if (mpc->ctype != PC_LISTENING)
1907                 return PKGMGR_R_EINVAL;
1908
1909         /* 1. check argument */
1910         if (event_cb == NULL)
1911                 return PKGMGR_R_EINVAL;
1912
1913         /* 2. add callback info to pkgmgr_client */
1914         req_id = _get_request_id();
1915         __add_stat_cbinfo(mpc, req_id, event_cb, data);
1916
1917         return req_id;
1918 }
1919
1920 API int pkgmgr_client_broadcast_status(pkgmgr_client *pc, const char *pkg_type,
1921                                        const char *pkgid, const char *key,
1922                                        const char *val)
1923 {
1924         /* Check for NULL value of pc */
1925         if (pc == NULL) {
1926                 _LOGD("package manager client handle is NULL\n");
1927                 return PKGMGR_R_EINVAL;
1928         }
1929         /* Check for valid arguments. NULL parameter causes DBUS to abort */
1930         if (pkgid == NULL || pkg_type == NULL || key == NULL || val == NULL) {
1931                 _LOGD("Argument supplied is NULL\n");
1932                 return PKGMGR_R_EINVAL;
1933         }
1934         pkgmgr_client_t *mpc = (pkgmgr_client_t *) pc;
1935
1936         /* 0. check the pc type */
1937         if (mpc->ctype != PC_BROADCAST)
1938                 return PKGMGR_R_EINVAL;
1939
1940         comm_status_broadcast_server_send_signal(mpc->info.broadcast.bc,
1941                                                  PKG_STATUS, pkg_type,
1942                                                  pkgid, key, val);
1943
1944         return PKGMGR_R_OK;
1945 }
1946
1947 API pkgmgr_info *pkgmgr_client_check_pkginfo_from_file(const char *pkg_path)
1948 {
1949         return pkgmgr_info_new_from_file(NULL, pkg_path);
1950 }
1951
1952 API int pkgmgr_client_free_pkginfo(pkgmgr_info * pkg_info)
1953 {
1954         if (pkg_info == NULL)
1955                 return PKGMGR_R_EINVAL;
1956
1957         package_manager_pkg_detail_info_t *info = (package_manager_pkg_detail_info_t *)pkg_info;
1958
1959         if (info->icon_buf)
1960                 free(info->icon_buf);
1961
1962         free(info);
1963         info = NULL;
1964
1965         return PKGMGR_R_OK;
1966 }
1967
1968 API int pkgmgr_client_request_service(pkgmgr_request_service_type service_type, int service_mode,
1969                                   pkgmgr_client * pc, const char *pkg_type, const char *pkgid,
1970                               const char *custom_info, pkgmgr_handler event_cb, void *data)
1971 {
1972         int ret =0;
1973
1974         /* Check for NULL value of service type */
1975         retvm_if(service_type > PM_REQUEST_MAX, PKGMGR_R_EINVAL, "service type is not defined\n");
1976         retvm_if(service_type < 0, PKGMGR_R_EINVAL, "service type is error\n");
1977         vconf_set_int(VCONFKEY_PKGMGR_STATUS, -1);
1978
1979         switch (service_type) {
1980         case PM_REQUEST_CSC:
1981                 tryvm_if(custom_info == NULL, ret = PKGMGR_R_EINVAL, "custom_info is NULL\n");
1982                 tryvm_if(strlen(custom_info) >= PKG_STRING_LEN_MAX, ret = PKGMGR_R_EINVAL, "optional_file over PKG_STRING_LEN_MAX");
1983                 tryvm_if(data == NULL, ret = PKGMGR_R_EINVAL, "data is NULL\n");
1984
1985                 ret = __csc_process(custom_info, (char *)data);
1986                 if (ret < 0)
1987                         _LOGE("__csc_process fail \n");
1988                 else
1989                         ret = PKGMGR_R_OK;
1990
1991                 break;
1992
1993         case PM_REQUEST_MOVE:
1994                 tryvm_if(pkgid == NULL, ret = PKGMGR_R_EINVAL, "pkgid is NULL\n");
1995                 tryvm_if(pc == NULL, ret = PKGMGR_R_EINVAL, "pc is NULL\n");
1996                 tryvm_if((service_mode < PM_MOVE_TO_INTERNAL) || (service_mode > PM_MOVE_TO_SDCARD), ret = PKGMGR_R_EINVAL, "service_mode is wrong\n");
1997
1998                 ret = __move_pkg_process(pc, pkgid, (pkgmgr_move_type)service_mode, event_cb, data);
1999                 break;
2000
2001         case PM_REQUEST_GET_SIZE:
2002                 tryvm_if(pkgid == NULL, ret = PKGMGR_R_EINVAL, "pkgid is NULL\n");
2003                 tryvm_if(pc == NULL, ret = PKGMGR_R_EINVAL, "pc is NULL\n");
2004                 tryvm_if((service_mode < PM_GET_TOTAL_SIZE) || (service_mode > PM_GET_DATA_SIZE), ret = PKGMGR_R_EINVAL, "service_mode is wrong\n");
2005
2006                 ret = __get_size_process(pc, pkgid, (pkgmgr_getsize_type)service_mode, event_cb, data);
2007                 break;
2008
2009         case PM_REQUEST_KILL_APP:
2010                 tryvm_if(pkgid == NULL, ret = PKGMGR_R_EINVAL, "pkgid is NULL\n");
2011                 tryvm_if(pc == NULL, ret = PKGMGR_R_EINVAL, "pc is NULL\n");
2012
2013                 ret = __kill_app_process(pc, pkgid);
2014                 if (ret < 0)
2015                         _LOGE("__kill_app_process fail \n");
2016                 else
2017                         ret = PKGMGR_R_OK;
2018
2019                 break;
2020
2021         default:
2022                 _LOGE("Wrong Request\n");
2023                 ret = -1;
2024                 break;
2025         }
2026
2027 catch:
2028
2029         return ret;
2030 }
2031
2032
2033 #define __START_OF_OLD_API
2034 ail_cb_ret_e __appinfo_func(const ail_appinfo_h appinfo, void *user_data)
2035 {
2036         char *type;
2037         char *package;
2038         char *version;
2039
2040         iter_data *udata = (iter_data *) user_data;
2041
2042         ail_appinfo_get_str(appinfo, AIL_PROP_X_SLP_PACKAGETYPE_STR, &type);
2043         if (type == NULL)
2044                 type = "";
2045         ail_appinfo_get_str(appinfo, AIL_PROP_PACKAGE_STR, &package);
2046         if (package == NULL)
2047                 package = "";
2048         ail_appinfo_get_str(appinfo, AIL_PROP_VERSION_STR, &version);
2049         if (version == NULL)
2050                 version = "";
2051
2052         if (udata->iter_fn(type, package, version, udata->data) != 0)
2053                 return AIL_CB_RET_CANCEL;
2054
2055         return AIL_CB_RET_CONTINUE;
2056 }
2057
2058 API int pkgmgr_get_pkg_list(pkgmgr_iter_fn iter_fn, void *data)
2059 {
2060         int cnt = -1;
2061         ail_filter_h filter;
2062         ail_error_e ret;
2063
2064         if (iter_fn == NULL)
2065                 return PKGMGR_R_EINVAL;
2066
2067         ret = ail_filter_new(&filter);
2068         if (ret != AIL_ERROR_OK) {
2069                 return PKGMGR_R_ERROR;
2070         }
2071
2072         ret = ail_filter_add_str(filter, AIL_PROP_TYPE_STR, "Application");
2073         if (ret != AIL_ERROR_OK) {
2074                 ail_filter_destroy(filter);
2075                 return PKGMGR_R_ERROR;
2076         }
2077
2078         ret = ail_filter_add_bool(filter, AIL_PROP_X_SLP_REMOVABLE_BOOL, true);
2079         if (ret != AIL_ERROR_OK) {
2080                 ail_filter_destroy(filter);
2081                 return PKGMGR_R_ERROR;
2082         }
2083
2084         ret = ail_filter_count_appinfo(filter, &cnt);
2085         if (ret != AIL_ERROR_OK) {
2086                 ail_filter_destroy(filter);
2087                 return PKGMGR_R_ERROR;
2088         }
2089
2090         iter_data *udata = calloc(1, sizeof(iter_data));
2091         if (udata == NULL) {
2092                 _LOGE("calloc failed");
2093                 ail_filter_destroy(filter);
2094
2095                 return PKGMGR_R_ERROR;
2096         }
2097         udata->iter_fn = iter_fn;
2098         udata->data = data;
2099
2100         ail_filter_list_appinfo_foreach(filter, __appinfo_func, udata);
2101
2102         free(udata);
2103
2104         ret = ail_filter_destroy(filter);
2105         if (ret != AIL_ERROR_OK) {
2106                 return PKGMGR_R_ERROR;
2107         }
2108
2109         return PKGMGR_R_OK;
2110 }
2111
2112 API pkgmgr_info *pkgmgr_info_new(const char *pkg_type, const char *pkgid)
2113 {
2114         const char *pkgtype;
2115         pkg_plugin_set *plugin_set = NULL;
2116         package_manager_pkg_detail_info_t *pkg_detail_info = NULL;
2117
2118         /* 1. check argument */
2119         if (pkgid == NULL)
2120                 return NULL;
2121
2122         if (pkg_type == NULL) {
2123                 pkgtype = _get_pkg_type_from_desktop_file(pkgid);
2124                 if (pkgtype == NULL)
2125                         return NULL;
2126         } else
2127                 pkgtype = pkg_type;
2128
2129         if (strlen(pkgid) >= PKG_STRING_LEN_MAX)
2130                 return NULL;
2131
2132         pkg_detail_info = calloc(1, sizeof(package_manager_pkg_detail_info_t));
2133         if (pkg_detail_info == NULL) {
2134                 _LOGE("*** Failed to alloc package_handler_info.\n");
2135                 return NULL;
2136         }
2137
2138         plugin_set = _package_manager_load_library(pkgtype);
2139         if (plugin_set == NULL) {
2140                 _LOGE("*** Failed to load library");
2141                 free(pkg_detail_info);
2142                 return NULL;
2143         }
2144
2145         if (plugin_set->pkg_is_installed) {
2146                 if (plugin_set->pkg_is_installed(pkgid) != 0) {
2147                         _LOGE("*** Failed to call pkg_is_installed()");
2148                         free(pkg_detail_info);
2149                         return NULL;
2150                 }
2151
2152                 if (plugin_set->get_pkg_detail_info) {
2153                         if (plugin_set->get_pkg_detail_info(pkgid,
2154                                                             pkg_detail_info) != 0) {
2155                                 _LOGE("*** Failed to call get_pkg_detail_info()");
2156                                 free(pkg_detail_info);
2157                                 return NULL;
2158                         }
2159                 }
2160         }
2161
2162         return (pkgmgr_info *) pkg_detail_info;
2163 }
2164
2165 API char * pkgmgr_info_get_string(pkgmgr_info * pkg_info, const char *key)
2166 {
2167         package_manager_pkg_detail_info_t *pkg_detail_info;
2168
2169         if (pkg_info == NULL)
2170                 return NULL;
2171         if (key == NULL)
2172                 return NULL;
2173
2174         pkg_detail_info = (package_manager_pkg_detail_info_t *) pkg_info;
2175
2176         return _get_info_string(key, pkg_detail_info);
2177 }
2178
2179 API pkgmgr_info *pkgmgr_info_new_from_file(const char *pkg_type,
2180                                            const char *pkg_path)
2181 {
2182         pkg_plugin_set *plugin_set = NULL;
2183         package_manager_pkg_detail_info_t *pkg_detail_info = NULL;
2184         char *pkgtype;
2185         if (pkg_path == NULL) {
2186                 _LOGE("pkg_path is NULL\n");
2187                 return NULL;
2188         }
2189
2190         if (strlen(pkg_path) > PKG_URL_STRING_LEN_MAX) {
2191                 _LOGE("length of pkg_path is too long - %d.\n",
2192                       strlen(pkg_path));
2193                 return NULL;
2194         }
2195
2196         pkg_detail_info = calloc(1, sizeof(package_manager_pkg_detail_info_t));
2197         if (pkg_detail_info == NULL) {
2198                 _LOGE("*** Failed to alloc package_handler_info.\n");
2199                 return NULL;
2200         }
2201
2202         if (pkg_type == NULL)
2203                 pkgtype = __get_type_from_path(pkg_path);
2204         else
2205                 pkgtype = strdup(pkg_type);
2206
2207         plugin_set = _package_manager_load_library(pkgtype);
2208         if (plugin_set == NULL) {
2209                 free(pkg_detail_info);
2210                 free(pkgtype);
2211                 return NULL;
2212         }
2213
2214         if (plugin_set->get_pkg_detail_info_from_package) {
2215                 if (plugin_set->get_pkg_detail_info_from_package(pkg_path,
2216                                                                  pkg_detail_info) != 0) {
2217                         free(pkg_detail_info);
2218                         free(pkgtype);
2219                         return NULL;
2220                 }
2221         }
2222
2223         free(pkgtype);
2224         return (pkgmgr_info *) pkg_detail_info;
2225 }
2226
2227 API int pkgmgr_info_free(pkgmgr_info * pkg_info)
2228 {
2229         if (pkg_info == NULL)
2230                 return PKGMGR_R_EINVAL;
2231
2232         free(pkg_info);
2233         pkg_info = NULL;
2234
2235         return 0;
2236 }
2237
2238 #define __END_OF_OLD_API
2239
2240 API int pkgmgr_pkginfo_get_list(pkgmgr_info_pkg_list_cb pkg_list_cb, void *user_data)
2241 {
2242         int ret = 0;
2243         ret = pkgmgrinfo_pkginfo_get_list(pkg_list_cb, user_data);
2244         return ret;
2245 }
2246
2247 API int pkgmgr_pkginfo_get_pkginfo(const char *pkgid, pkgmgr_pkginfo_h *handle)
2248 {
2249         int ret = 0;
2250         ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, handle);
2251         return ret;
2252 }
2253
2254 API int pkgmgr_pkginfo_get_pkgname(pkgmgr_pkginfo_h handle, char **pkg_name)
2255 {
2256         int ret = 0;
2257         ret = pkgmgrinfo_pkginfo_get_pkgname(handle, pkg_name);
2258         return ret;
2259 }
2260
2261
2262 API int pkgmgr_pkginfo_get_pkgid(pkgmgr_pkginfo_h handle, char **pkgid)
2263 {
2264         int ret = 0;
2265         ret = pkgmgrinfo_pkginfo_get_pkgid(handle, pkgid);
2266         return ret;
2267 }
2268
2269 API int pkgmgr_pkginfo_get_type(pkgmgr_pkginfo_h handle, char **type)
2270 {
2271         int ret = 0;
2272         ret = pkgmgrinfo_pkginfo_get_type(handle, type);
2273         return ret;
2274 }
2275
2276 API int pkgmgr_pkginfo_get_version(pkgmgr_pkginfo_h handle, char **version)
2277 {
2278         int ret = 0;
2279         ret = pkgmgrinfo_pkginfo_get_version(handle, version);
2280         return ret;
2281 }
2282
2283 API int pkgmgr_pkginfo_get_install_location(pkgmgr_pkginfo_h handle, pkgmgr_install_location *location)
2284 {
2285         int ret = 0;
2286         pkgmgrinfo_install_location loc;
2287         ret = pkgmgrinfo_pkginfo_get_install_location(handle, &loc);
2288         *location = loc;
2289         return ret;
2290 }
2291
2292 API int pkgmgr_pkginfo_get_package_size(pkgmgr_pkginfo_h handle, int *size)
2293 {
2294         int ret = 0;
2295         ret = pkgmgrinfo_pkginfo_get_package_size(handle, size);
2296         return ret;
2297 }
2298
2299 API int pkgmgr_pkginfo_get_icon(pkgmgr_pkginfo_h handle, char **icon)
2300 {
2301         int ret = 0;
2302         ret = pkgmgrinfo_pkginfo_get_icon(handle, icon);
2303         return ret;
2304 }
2305
2306 API int pkgmgr_pkginfo_get_label(pkgmgr_pkginfo_h handle, char **label)
2307 {
2308         int ret = 0;
2309         ret = pkgmgrinfo_pkginfo_get_label(handle, label);
2310         return ret;
2311 }
2312
2313 API int pkgmgr_pkginfo_get_description(pkgmgr_pkginfo_h handle, char **description)
2314 {
2315         int ret = 0;
2316         ret = pkgmgrinfo_pkginfo_get_description(handle, description);
2317         return ret;
2318 }
2319
2320 API int pkgmgr_pkginfo_get_author_name(pkgmgr_pkginfo_h handle, char **author_name)
2321 {
2322         int ret = 0;
2323         ret = pkgmgrinfo_pkginfo_get_author_name(handle, author_name);
2324         return ret;
2325 }
2326
2327 API int pkgmgr_pkginfo_get_author_email(pkgmgr_pkginfo_h handle, char **author_email)
2328 {
2329         int ret = 0;
2330         ret = pkgmgrinfo_pkginfo_get_author_email(handle, author_email);
2331         return ret;
2332 }
2333
2334 API int pkgmgr_pkginfo_get_author_href(pkgmgr_pkginfo_h handle, char **author_href)
2335 {
2336         int ret = 0;
2337         ret = pkgmgrinfo_pkginfo_get_author_href(handle, author_href);
2338         return ret;
2339 }
2340
2341 API int pkgmgr_pkginfo_is_removable(pkgmgr_pkginfo_h handle, bool *removable)
2342 {
2343         int ret = 0;
2344         ret = pkgmgrinfo_pkginfo_is_removable(handle, removable);
2345         return ret;
2346 }
2347
2348 API int pkgmgr_pkginfo_is_preload(pkgmgr_pkginfo_h handle, bool *preload)
2349 {
2350         int ret = 0;
2351         ret = pkgmgrinfo_pkginfo_is_preload(handle, preload);
2352         return ret;
2353 }
2354
2355 API int pkgmgr_pkginfo_is_readonly(pkgmgr_pkginfo_h handle, bool *readonly)
2356 {
2357         int ret = 0;
2358         ret = pkgmgrinfo_pkginfo_is_readonly(handle, readonly);
2359         return ret;
2360 }
2361
2362 API int pkgmgr_pkginfo_is_accessible(pkgmgr_pkginfo_h handle, bool *accessible)
2363 {
2364         int ret = 0;
2365         ret = pkgmgrinfo_pkginfo_is_accessible(handle, accessible);
2366         return ret;
2367 }
2368
2369 API int pkgmgr_pkginfo_destroy_pkginfo(pkgmgr_pkginfo_h handle)
2370 {
2371         int ret = 0;
2372         ret = pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
2373         return ret;
2374 }
2375
2376 API int pkgmgr_pkginfo_get_installed_storage(pkgmgr_pkginfo_h handle, pkgmgr_installed_storage *storage)
2377 {
2378         int ret = 0;
2379         pkgmgrinfo_installed_storage sto;
2380         ret = pkgmgrinfo_pkginfo_get_installed_storage(handle, &sto);
2381         *storage = sto;
2382         return ret;
2383 }
2384
2385 API int pkgmgr_pkginfo_get_installed_time(pkgmgr_pkginfo_h handle, int *installed_time)
2386 {
2387         int ret = 0;
2388         ret = pkgmgrinfo_pkginfo_get_installed_time(handle, installed_time);
2389         return ret;
2390 }
2391
2392 API int pkgmgr_appinfo_get_list(pkgmgr_pkginfo_h handle, pkgmgr_app_component component,
2393                                                         pkgmgr_info_app_list_cb app_func, void *user_data)
2394 {
2395         int ret = 0;
2396         ret = pkgmgrinfo_appinfo_get_list(handle, component, app_func, user_data);
2397         return ret;
2398 }
2399
2400 API int pkgmgr_appinfo_foreach_category(pkgmgr_appinfo_h handle, pkgmgr_info_app_category_list_cb category_func,
2401                                                         void *user_data)
2402 {
2403         int ret = 0;
2404         ret = pkgmgrinfo_appinfo_foreach_category(handle, category_func, user_data);
2405         return ret;
2406 }
2407
2408 API int pkgmgr_appinfo_get_appinfo(const char *appid, pkgmgr_appinfo_h *handle)
2409 {
2410         int ret = 0;
2411         ret = pkgmgrinfo_appinfo_get_appinfo(appid, handle);
2412         return ret;
2413 }
2414
2415 API int pkgmgr_appinfo_get_appid(pkgmgr_appinfo_h  handle, char **appid)
2416 {
2417         int ret = 0;
2418         ret = pkgmgrinfo_appinfo_get_appid(handle, appid);
2419         return ret;
2420 }
2421
2422 API int pkgmgr_appinfo_get_pkgname(pkgmgr_appinfo_h  handle, char **pkg_name)
2423 {
2424         int ret = 0;
2425         ret = pkgmgrinfo_appinfo_get_pkgname(handle, pkg_name);
2426         return ret;
2427 }
2428
2429 API int pkgmgr_appinfo_get_pkgid(pkgmgr_appinfo_h  handle, char **pkgid)
2430 {
2431         int ret = 0;
2432         ret = pkgmgrinfo_appinfo_get_pkgid(handle, pkgid);
2433         return ret;
2434 }
2435
2436 API int pkgmgr_appinfo_get_icon(pkgmgr_appinfo_h handle, char **icon)
2437 {
2438         int ret = 0;
2439         ret = pkgmgrinfo_appinfo_get_icon(handle, icon);
2440         return ret;
2441 }
2442
2443 API int pkgmgr_appinfo_get_label(pkgmgr_appinfo_h handle, char **label)
2444 {
2445         int ret = 0;
2446         ret = pkgmgrinfo_appinfo_get_label(handle, label);
2447         return ret;
2448 }
2449
2450 API int pkgmgr_appinfo_get_exec(pkgmgr_appinfo_h  handle, char **exec)
2451 {
2452         int ret = 0;
2453         ret = pkgmgrinfo_appinfo_get_exec(handle, exec);
2454         return ret;
2455 }
2456
2457 API int pkgmgr_appinfo_get_component(pkgmgr_appinfo_h  handle, pkgmgr_app_component *component)
2458 {
2459         int ret = 0;
2460         pkgmgrinfo_app_component comp;
2461         ret = pkgmgrinfo_appinfo_get_component(handle, &comp);
2462         *component = comp;
2463         return ret;
2464 }
2465
2466 API int pkgmgr_appinfo_get_apptype(pkgmgr_appinfo_h  handle, char **app_type)
2467 {
2468         int ret = 0;
2469         ret = pkgmgrinfo_appinfo_get_apptype(handle, app_type);
2470         return ret;
2471 }
2472
2473 API int pkgmgr_appinfo_is_nodisplay(pkgmgr_appinfo_h  handle, bool *nodisplay)
2474 {
2475         int ret = 0;
2476         ret = pkgmgrinfo_appinfo_is_nodisplay(handle, nodisplay);
2477         return ret;
2478 }
2479
2480 API int pkgmgr_appinfo_is_multiple(pkgmgr_appinfo_h  handle, bool *multiple)
2481 {
2482         int ret = 0;
2483         ret = pkgmgrinfo_appinfo_is_multiple(handle, multiple);
2484         return ret;
2485 }
2486
2487 API int pkgmgr_appinfo_is_taskmanage(pkgmgr_appinfo_h  handle, bool *taskmanage)
2488 {
2489         int ret = 0;
2490         ret = pkgmgrinfo_appinfo_is_taskmanage(handle, taskmanage);
2491         return ret;
2492 }
2493
2494 API int pkgmgr_appinfo_get_hwacceleration(pkgmgr_appinfo_h  handle, pkgmgr_hwacceleration_type *hwacceleration)
2495 {
2496         int ret = 0;
2497         pkgmgrinfo_app_hwacceleration hwacc;
2498         ret = pkgmgrinfo_appinfo_get_hwacceleration(handle, &hwacc);
2499         *hwacceleration = hwacc;
2500         return ret;
2501 }
2502
2503 API int pkgmgr_appinfo_is_onboot(pkgmgr_appinfo_h  handle, bool *onboot)
2504 {
2505         int ret = 0;
2506         ret = pkgmgrinfo_appinfo_is_onboot(handle, onboot);
2507         return ret;
2508 }
2509
2510 API int pkgmgr_appinfo_is_autorestart(pkgmgr_appinfo_h  handle, bool *autorestart)
2511 {
2512         int ret = 0;
2513         ret = pkgmgrinfo_appinfo_is_autorestart(handle, autorestart);
2514         return ret;
2515 }
2516
2517 API int pkgmgr_appinfo_destroy_appinfo(pkgmgr_appinfo_h  handle)
2518 {
2519         int ret = 0;
2520         ret = pkgmgrinfo_appinfo_destroy_appinfo(handle);
2521         return ret;
2522 }
2523
2524 API int pkgmgr_pkginfo_create_certinfo(pkgmgr_certinfo_h *handle)
2525 {
2526         int ret = 0;
2527         ret = pkgmgrinfo_pkginfo_create_certinfo(handle);
2528         return ret;
2529 }
2530
2531 API int pkgmgr_pkginfo_load_certinfo(const char *pkgid, pkgmgr_certinfo_h handle)
2532 {
2533         int ret = 0;
2534         ret = pkgmgrinfo_pkginfo_load_certinfo(pkgid, handle);
2535         return ret;
2536 }
2537
2538 API int pkgmgr_pkginfo_get_cert_value(pkgmgr_certinfo_h handle, pkgmgr_cert_type cert_type, const char **cert_value)
2539 {
2540         int ret = 0;
2541         ret = pkgmgrinfo_pkginfo_get_cert_value(handle, cert_type, cert_value);
2542         return ret;
2543 }
2544
2545 API int pkgmgr_pkginfo_destroy_certinfo(pkgmgr_certinfo_h handle)
2546 {
2547         int ret = 0;
2548         ret = pkgmgrinfo_pkginfo_destroy_certinfo(handle);
2549         return ret;
2550 }
2551
2552 API int pkgmgr_datacontrol_get_info(const char *providerid, const char * type, char **appid, char **access)
2553 {
2554         int ret = 0;
2555         ret = pkgmgrinfo_datacontrol_get_info(providerid, type, appid, access);
2556         return ret;
2557 }