Fix coding rules
[platform/core/appfw/pkgmgr-info.git] / parser / src / pkgmgr_parser.c
1 /*
2  * pkgmgr-info
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 #define _GNU_SOURCE
23 #include <dlfcn.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <ctype.h>
29 #include <time.h>
30 #include <string.h>
31 #include <libxml/parser.h>
32 #include <libxml/xmlreader.h>
33 #include <libxml/xmlschemas.h>
34 #include <vconf.h>
35 #include <glib.h>
36 #include <grp.h>
37
38 #include "pkgmgr-info.h"
39 #include "pkgmgrinfo_basic.h"
40 #include "pkgmgrinfo_debug.h"
41
42 #include "pkgmgr_parser.h"
43 #include "pkgmgr_parser_internal.h"
44 #include "pkgmgr_parser_db.h"
45
46 #ifdef LOG_TAG
47 #undef LOG_TAG
48 #endif
49 #define LOG_TAG "PKGMGR_PARSER"
50
51 #define ASCII(s) (char *)s
52 #define XMLCHAR(s) (const xmlChar *)s
53
54 #define METADATA_PARSER_LIST SYSCONFDIR "/package-manager/parserlib/metadata/mdparser_list.txt"
55 #define METADATA_PARSER_NAME    "metadataparser:"
56
57 #define CATEGORY_PARSER_LIST SYSCONFDIR "/package-manager/parserlib/category/category_parser_list.txt"
58 #define CATEGORY_PARSER_NAME    "categoryparser:"
59
60 #define TAG_PARSER_LIST SYSCONFDIR "/package-manager/parserlib/tag_parser_list.txt"
61 #define TAG_PARSER_NAME "parserlib:"
62
63 #define PKG_TAG_LEN_MAX 128
64 #define OWNER_ROOT 0
65 #define BUFSIZE 4096
66 #define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)
67
68 /* plugin process_type */
69 typedef enum {
70         PLUGIN_PRE_PROCESS = 0,
71         PLUGIN_POST_PROCESS
72 } PLUGIN_PROCESS_TYPE;
73
74 const char *package;
75
76 static int __ps_process_label(xmlTextReaderPtr reader, label_x *label);
77 static int __ps_process_privilege(xmlTextReaderPtr reader, privilege_x *privilege);
78 static int __ps_process_privileges(xmlTextReaderPtr reader, GList **privileges);
79 static int __ps_process_allowed(xmlTextReaderPtr reader, char **allowed);
80 static int __ps_process_condition(xmlTextReaderPtr reader, char **condition);
81 static int __ps_process_notification(xmlTextReaderPtr reader, notification_x *notifiation);
82 static int __ps_process_category(xmlTextReaderPtr reader, char **category);
83 static int __ps_process_metadata(xmlTextReaderPtr reader, metadata_x *metadata);
84 static int __ps_process_permission(xmlTextReaderPtr reader, permission_x *permission);
85 static int __ps_process_compatibility(xmlTextReaderPtr reader, compatibility_x *compatibility);
86 static int __ps_process_request(xmlTextReaderPtr reader, char **request);
87 static int __ps_process_define(xmlTextReaderPtr reader, define_x *define);
88 static int __ps_process_launchconditions(xmlTextReaderPtr reader, GList **launchconditions);
89 static int __ps_process_datashare(xmlTextReaderPtr reader, datashare_x *datashare);
90 static int __ps_process_icon(xmlTextReaderPtr reader, icon_x *icon, uid_t uid);
91 static int __ps_process_author(xmlTextReaderPtr reader, author_x *author);
92 static int __ps_process_description(xmlTextReaderPtr reader, description_x *description);
93 static int __ps_process_license(xmlTextReaderPtr reader, license_x *license);
94 static int __ps_process_appcontrol(xmlTextReaderPtr reader, GList **appcontrol);
95 static int __ps_process_datacontrol(xmlTextReaderPtr reader, datacontrol_x *datacontrol);
96 static int __ps_process_application(xmlTextReaderPtr reader, application_x *application, int type, uid_t uid);
97 static int __next_child_element(xmlTextReaderPtr reader, int depth);
98 static int __start_process(xmlTextReaderPtr reader, manifest_x * mfx, uid_t uid);
99 static int __process_manifest(xmlTextReaderPtr reader, manifest_x * mfx, uid_t uid);
100 static void __str_trim(char *input);
101 static char *__get_parser_plugin(const char *type);
102 API int __is_admin();
103
104 static void __save_xml_attribute(xmlTextReaderPtr reader, char *attribute, char **xml_attribute, char *default_value)
105 {
106         xmlChar *attrib_val = xmlTextReaderGetAttribute(reader, XMLCHAR(attribute));
107         if (attrib_val) {
108                 *xml_attribute = strdup((const char *)attrib_val);
109                 xmlFree(attrib_val);
110         } else {
111                 if (default_value != NULL)
112                         *xml_attribute = strdup(default_value);
113         }
114 }
115
116 static void __save_xml_lang(xmlTextReaderPtr reader, char **xml_attribute)
117 {
118         const xmlChar *attrib_val = xmlTextReaderConstXmlLang(reader);
119         if (attrib_val != NULL)
120                 *xml_attribute = strdup(ASCII(attrib_val));
121         else
122                 *xml_attribute = strdup(DEFAULT_LOCALE);
123 }
124
125 static void __save_xml_value(xmlTextReaderPtr reader, char **xml_attribute)
126 {
127         xmlTextReaderRead(reader);
128         const xmlChar *attrib_val = xmlTextReaderConstValue(reader);
129
130         if (attrib_val)
131                 *xml_attribute = strdup((const char *)attrib_val);
132 }
133
134 static void __save_xml_installed_time(manifest_x *mfx)
135 {
136         char buf[PKG_STRING_LEN_MAX] = {'\0'};
137         char *val = NULL;
138         time_t current_time;
139         time(&current_time);
140         snprintf(buf, PKG_STRING_LEN_MAX - 1, "%d", (int)current_time);
141         val = strndup(buf, PKG_STRING_LEN_MAX - 1);
142         mfx->installed_time = val;
143 }
144
145 static void __save_xml_root_path(manifest_x *mfx, uid_t uid)
146 {
147         char root[PKG_STRING_LEN_MAX] = { '\0' };
148         const char *path;
149
150         if (mfx->root_path)
151                 return;
152
153         tzplatform_set_user(uid);
154         path = tzplatform_getenv((uid == OWNER_ROOT || uid == GLOBAL_USER) ? TZ_SYS_RO_APP : TZ_USER_APP);
155         snprintf(root, PKG_STRING_LEN_MAX - 1, "%s/%s", path, mfx->package);
156
157         mfx->root_path = strdup(root);
158
159         tzplatform_reset_user();
160 }
161
162 static void __save_xml_default_value(manifest_x * mfx)
163 {
164         mfx->preload = strdup("False");
165         mfx->removable = strdup("True");
166         mfx->readonly = strdup("False");
167         mfx->update = strdup("False");
168         mfx->system = strdup("False");
169         mfx->installed_storage = strdup("installed_internal");
170         package = mfx->package;
171 }
172
173 void *__open_lib_handle(char *tag)
174 {
175         char *lib_path = NULL;
176         void *lib_handle = NULL;
177
178         lib_path = __get_parser_plugin(tag);
179         retvm_if(!lib_path, NULL, "lib_path get fail");
180
181         lib_handle = dlopen(lib_path, RTLD_LAZY);
182         retvm_if(lib_handle == NULL, NULL, "dlopen is failed lib_path[%s]", lib_path);
183
184         return lib_handle;
185 }
186
187 void __close_lib_handle(void *lib_handle)
188 {
189         dlclose(lib_handle);
190 }
191
192 static void __str_trim(char *input)
193 {
194         char *trim_str = input;
195
196         if (input == NULL)
197                 return;
198
199         while (*input != 0) {
200                 if (!isspace(*input)) {
201                         *trim_str = *input;
202                         trim_str++;
203                 }
204                 input++;
205         }
206
207         *trim_str = 0;
208         return;
209 }
210
211 API int __is_admin()
212 {
213         uid_t uid = getuid();
214         if ((uid_t)0 == uid)
215                 return 1;
216         else
217                 return 0;
218 }
219
220
221
222 static char * __get_tag_by_key(char *md_key)
223 {
224         char *md_tag = NULL;
225
226         if (md_key == NULL) {
227                 _LOGD("md_key is NULL\n");
228                 return NULL;
229         }
230
231         md_tag = strrchr(md_key, 47) + 1;
232
233
234         return strdup(md_tag);
235 }
236
237 static char *__get_metadata_parser_plugin(const char *type)
238 {
239         FILE *fp = NULL;
240         char buffer[1024] = { 0 };
241         char temp_path[1024] = { 0 };
242         char *path = NULL;
243
244         if (type == NULL) {
245                 _LOGE("invalid argument\n");
246                 return NULL;
247         }
248
249         fp = fopen(PKG_PARSER_CONF_PATH, "r");
250         if (fp == NULL) {
251                 _LOGE("no matching metadata parser\n");
252                 return NULL;
253         }
254
255         while (fgets(buffer, sizeof(buffer), fp) != NULL) {
256                 if (buffer[0] == '#')
257                         continue;
258
259                 __str_trim(buffer);
260
261                 if ((path = strstr(buffer, METADATA_PARSER_NAME)) != NULL) {
262                         path = path + strlen(METADATA_PARSER_NAME);
263
264                         break;
265                 }
266
267                 memset(buffer, 0x00, 1024);
268         }
269
270         if (fp != NULL)
271                 fclose(fp);
272
273         if (path == NULL) {
274                 _LOGE("no matching [%s] [%s]\n", METADATA_PARSER_NAME, type);
275                 return NULL;
276         }
277
278         snprintf(temp_path, sizeof(temp_path) - 1, "%slib%s.so", path, type);
279
280         return strdup(temp_path);
281 }
282
283 static char *__get_category_parser_plugin(const char *type)
284 {
285         FILE *fp = NULL;
286         char buffer[1024] = { 0 };
287         char temp_path[1024] = { 0 };
288         char *path = NULL;
289
290         if (type == NULL) {
291                 _LOGE("invalid argument\n");
292                 return NULL;
293         }
294
295         fp = fopen(PKG_PARSER_CONF_PATH, "r");
296         if (fp == NULL) {
297                 _LOGE("no matching metadata parser\n");
298                 return NULL;
299         }
300
301         while (fgets(buffer, sizeof(buffer), fp) != NULL) {
302                 if (buffer[0] == '#')
303                         continue;
304
305                 __str_trim(buffer);
306
307                 if ((path = strstr(buffer, CATEGORY_PARSER_NAME)) != NULL) {
308                         path = path + strlen(CATEGORY_PARSER_NAME);
309
310                         break;
311                 }
312
313                 memset(buffer, 0x00, 1024);
314         }
315
316         if (fp != NULL)
317                 fclose(fp);
318
319         if (path == NULL) {
320                 _LOGE("no matching [%s] [%s]\n", CATEGORY_PARSER_NAME, type);
321                 return NULL;
322         }
323
324         snprintf(temp_path, sizeof(temp_path) - 1, "%slib%s.so", path, type);
325
326         return strdup(temp_path);
327 }
328
329 static char *__get_parser_plugin(const char *type)
330 {
331         FILE *fp = NULL;
332         char buffer[1024] = { 0 };
333         char temp_path[1024] = { 0 };
334         char *path = NULL;
335
336         if (type == NULL) {
337                 _LOGE("invalid argument\n");
338                 return NULL;
339         }
340
341         fp = fopen(PKG_PARSER_CONF_PATH, "r");
342         if (fp == NULL) {
343                 _LOGE("no matching backendlib\n");
344                 return NULL;
345         }
346
347         while (fgets(buffer, sizeof(buffer), fp) != NULL) {
348                 if (buffer[0] == '#')
349                         continue;
350
351                 __str_trim(buffer);
352
353                 if ((path = strstr(buffer, PKG_PARSERLIB)) != NULL) {
354                         path = path + strlen(PKG_PARSERLIB);
355                         break;
356                 }
357
358                 memset(buffer, 0x00, 1024);
359         }
360
361         if (fp != NULL)
362                 fclose(fp);
363
364         if (path == NULL) {
365                 _LOGE("no matching backendlib\n");
366                 return NULL;
367         }
368
369         snprintf(temp_path, sizeof(temp_path) - 1, "%slib%s.so", path, type);
370
371         return strdup(temp_path);
372 }
373
374 static int __ps_run_tag_parser(void *lib_handle, xmlDocPtr docPtr, const char *tag,
375                            ACTION_TYPE action, const char *pkgid)
376 {
377         int (*plugin_install) (xmlDocPtr, const char *);
378         int ret = -1;
379         char *ac = NULL;
380
381         switch (action) {
382         case ACTION_INSTALL:
383                 ac = "PKGMGR_PARSER_PLUGIN_INSTALL";
384                 break;
385         case ACTION_UPGRADE:
386                 ac = "PKGMGR_PARSER_PLUGIN_UPGRADE";
387                 break;
388         case ACTION_UNINSTALL:
389                 ac = "PKGMGR_PARSER_PLUGIN_UNINSTALL";
390                 break;
391         default:
392                 goto END;
393         }
394
395         if ((plugin_install =
396                 dlsym(lib_handle, ac)) == NULL || dlerror() != NULL) {
397                 _LOGE("can not find symbol[%s] \n", ac);
398                 goto END;
399         }
400
401         ret = plugin_install(docPtr, pkgid);
402         _LOGD("tag parser[%s, %s] ACTION_TYPE[%d] result[%d]\n", pkgid, tag, action, ret);
403
404 END:
405         return ret;
406 }
407
408 static int __ps_run_metadata_parser(GList *md_list, const char *tag,
409                                 ACTION_TYPE action, const char *pkgid, const char *appid)
410 {
411         char *lib_path = NULL;
412         void *lib_handle = NULL;
413         int (*metadata_parser_plugin) (const char *, const char *, GList *);
414         int ret = -1;
415         char *ac = NULL;
416
417         switch (action) {
418         case ACTION_INSTALL:
419                 ac = "PKGMGR_MDPARSER_PLUGIN_INSTALL";
420                 break;
421         case ACTION_UPGRADE:
422                 ac = "PKGMGR_MDPARSER_PLUGIN_UPGRADE";
423                 break;
424         case ACTION_UNINSTALL:
425                 ac = "PKGMGR_MDPARSER_PLUGIN_UNINSTALL";
426                 break;
427         default:
428                 goto END;
429         }
430
431         lib_path = __get_metadata_parser_plugin(tag);
432         if (!lib_path) {
433                 _LOGE("get %s parser fail\n", tag);
434                 goto END;
435         }
436
437         if ((lib_handle = dlopen(lib_path, RTLD_LAZY)) == NULL) {
438                 _LOGE("dlopen is failed lib_path[%s]\n", lib_path);
439                 goto END;
440         }
441
442         if ((metadata_parser_plugin =
443                 dlsym(lib_handle, ac)) == NULL || dlerror() != NULL) {
444                 _LOGE("can not find symbol[%s] \n", ac);
445                 goto END;
446         }
447
448         ret = metadata_parser_plugin(pkgid, appid, md_list);
449         if (ret < 0)
450                 _LOGD("[appid = %s, libpath = %s plugin fail\n", appid, lib_path);
451         else
452                 _LOGD("[appid = %s, libpath = %s plugin success\n", appid, lib_path);
453
454 END:
455         if (lib_path)
456                 free(lib_path);
457         if (lib_handle)
458                 dlclose(lib_handle);
459         return ret;
460 }
461
462 static int __ps_run_category_parser(GList *category_list, const char *tag,
463                                 ACTION_TYPE action, const char *pkgid, const char *appid)
464 {
465         char *lib_path = NULL;
466         void *lib_handle = NULL;
467         int (*category_parser_plugin) (const char *, const char *, GList *);
468         int ret = -1;
469         char *ac = NULL;
470
471         switch (action) {
472         case ACTION_INSTALL:
473                 ac = "PKGMGR_CATEGORY_PARSER_PLUGIN_INSTALL";
474                 break;
475         case ACTION_UPGRADE:
476                 ac = "PKGMGR_CATEGORY_PARSER_PLUGIN_UPGRADE";
477                 break;
478         case ACTION_UNINSTALL:
479                 ac = "PKGMGR_CATEGORY_PARSER_PLUGIN_UNINSTALL";
480                 break;
481         default:
482                 goto END;
483         }
484
485         lib_path = __get_category_parser_plugin(tag);
486         if (!lib_path) {
487                 _LOGE("get %s parser fail\n", tag);
488                 goto END;
489         }
490
491         if ((lib_handle = dlopen(lib_path, RTLD_LAZY)) == NULL) {
492                 _LOGE("dlopen is failed lib_path[%s]\n", lib_path);
493                 goto END;
494         }
495
496         if ((category_parser_plugin =
497                 dlsym(lib_handle, ac)) == NULL || dlerror() != NULL) {
498                 _LOGE("can not find symbol[%s] \n", ac);
499                 goto END;
500         }
501
502         ret = category_parser_plugin(pkgid, appid, category_list);
503         if (ret < 0)
504                 _LOGD("[appid = %s, libpath = %s plugin fail\n", appid, lib_path);
505         else
506                 _LOGD("[appid = %s, libpath = %s plugin success\n", appid, lib_path);
507
508 END:
509         if (lib_path)
510                 free(lib_path);
511         if (lib_handle)
512                 dlclose(lib_handle);
513         return ret;
514 }
515
516 static void __metadata_parser_clear_dir_list(GList* dir_list)
517 {
518         GList *list = NULL;
519         __metadata_t* detail = NULL;
520
521         if (dir_list) {
522                 list = g_list_first(dir_list);
523                 while (list) {
524                         detail = (__metadata_t *)list->data;
525                         if (detail) {
526                                 if (detail->key)
527                                         free((void *)detail->key);
528                                 if (detail->value)
529                                         free((void *)detail->value);
530                                 free(detail);
531                         }
532                         list = g_list_next(list);
533                 }
534                 g_list_free(dir_list);
535         }
536 }
537
538 static void __category_parser_clear_dir_list(GList* dir_list)
539 {
540         GList *list = NULL;
541         __category_t* detail = NULL;
542
543         if (dir_list) {
544                 list = g_list_first(dir_list);
545                 while (list) {
546                         detail = (__category_t *)list->data;
547                         if (detail) {
548                                 if (detail->name)
549                                         free((void *)detail->name);
550
551                                 free(detail);
552                         }
553                         list = g_list_next(list);
554                 }
555                 g_list_free(dir_list);
556         }
557 }
558
559 static int __run_tag_parser_prestep(void *lib_handle, xmlTextReaderPtr reader, ACTION_TYPE action, const char *pkgid)
560 {
561         int ret = -1;
562         const xmlChar *name;
563
564         if (xmlTextReaderDepth(reader) != 1) {
565                 _LOGE("Node depth is not 1");
566                 goto END;
567         }
568
569         if (xmlTextReaderNodeType(reader) != 1) {
570                 _LOGE("Node type is not 1");
571                 goto END;
572         }
573
574         const xmlChar *value;
575         name = xmlTextReaderConstName(reader);
576         if (name == NULL) {
577                 _LOGE("TEST TEST TES\n");
578                 name = BAD_CAST "--";
579         }
580
581         value = xmlTextReaderConstValue(reader);
582         if (value != NULL) {
583                 if (xmlStrlen(value) > 40)
584                         _LOGD(" %.40s...", value);
585                 else
586                         _LOGD(" %s", value);
587         }
588
589         name = xmlTextReaderConstName(reader);
590         if (name == NULL) {
591                 _LOGE("TEST TEST TES\n");
592                 name = BAD_CAST "--";
593         }
594
595         xmlDocPtr docPtr = xmlTextReaderCurrentDoc(reader);
596         xmlDocPtr copyDocPtr = xmlCopyDoc(docPtr, 1);
597         if (copyDocPtr == NULL)
598                 return -1;
599         xmlNode *rootElement = xmlDocGetRootElement(copyDocPtr);
600         if (rootElement == NULL)
601                 return -1;
602         xmlNode *cur_node = xmlFirstElementChild(rootElement);
603         if (cur_node == NULL)
604                 return -1;
605         xmlNode *temp = xmlTextReaderExpand(reader);
606         if (temp == NULL)
607                 return -1;
608         xmlNode *next_node = NULL;
609         while (cur_node != NULL) {
610                 if ((strcmp(ASCII(temp->name), ASCII(cur_node->name)) == 0) &&
611                         (temp->line == cur_node->line)) {
612                         break;
613                 } else {
614                         next_node = xmlNextElementSibling(cur_node);
615                         xmlUnlinkNode(cur_node);
616                         xmlFreeNode(cur_node);
617                         cur_node = next_node;
618                 }
619         }
620         if (cur_node == NULL)
621                 return -1;
622         next_node = xmlNextElementSibling(cur_node);
623         if (next_node) {
624                 cur_node->next = NULL;
625                 next_node->prev = NULL;
626                 xmlFreeNodeList(next_node);
627                 xmlSetTreeDoc(cur_node, copyDocPtr);
628         } else {
629                 xmlSetTreeDoc(cur_node, copyDocPtr);
630         }
631
632         ret = __ps_run_tag_parser(lib_handle, copyDocPtr, ASCII(name), action, pkgid);
633  END:
634
635         return ret;
636 }
637
638 static int __run_metadata_parser_prestep(manifest_x *mfx, char *md_key, ACTION_TYPE action)
639 {
640         int ret = -1;
641         int tag_exist = 0;
642         char buffer[1024] = { 0, };
643         GList *app_tmp;
644         application_x *app;
645         GList *md_tmp = NULL;
646         metadata_x *md;
647         char *md_tag = NULL;
648
649         GList *md_list = NULL;
650         __metadata_t *md_detail = NULL;
651
652         md_tag = __get_tag_by_key(md_key);
653         if (md_tag == NULL) {
654                 _LOGD("md_tag is NULL\n");
655                 return -1;
656         }
657
658         for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
659                 app = (application_x *)app_tmp->data;
660                 if (app == NULL)
661                         continue;
662                 for (md_tmp = app->metadata; md_tmp; md_tmp = md_tmp->next) {
663                         md = (metadata_x *)md_tmp->data;
664                         if (md == NULL)
665                                 continue;
666                         /* get glist of metadata key and value combination */
667                         memset(buffer, 0x00, 1024);
668                         snprintf(buffer, 1024, "%s/", md_key);
669                         if ((md->key && md->value) && (strncmp(md->key, md_key, strlen(md_key)) == 0) && (strncmp(buffer, md->key, strlen(buffer)) == 0)) {
670                                 md_detail = (__metadata_t*) calloc(1, sizeof(__metadata_t));
671                                 if (md_detail == NULL) {
672                                         _LOGD("Memory allocation failed\n");
673                                         goto END;
674                                 }
675
676                                 md_detail->key = strdup(md->key);
677                                 if (md_detail->key == NULL) {
678                                         _LOGD("Memory allocation failed\n");
679                                         free(md_detail);
680                                         goto END;
681                                 }
682
683                                 md_detail->value = strdup(md->value);
684                                 if (md_detail->value == NULL) {
685                                         _LOGD("Memory allocation failed\n");
686                                         free((void *)md_detail->key);
687                                         free(md_detail);
688                                         goto END;
689                                 }
690
691                                 md_list = g_list_append(md_list, (gpointer)md_detail);
692                                 tag_exist = 1;
693                         }
694                 }
695
696                 /* send glist to parser when tags for metadata plugin parser exist. */
697                 if (tag_exist) {
698                         ret = __ps_run_metadata_parser(md_list, md_tag, action, mfx->package, app->appid);
699                         if (ret < 0)
700                                 _LOGD("metadata_parser failed[%d] for tag[%s]\n", ret, md_tag);
701                         else
702                                 _LOGD("metadata_parser success for tag[%s]\n", md_tag);
703                 }
704                 __metadata_parser_clear_dir_list(md_list);
705                 md_list = NULL;
706                 tag_exist = 0;
707         }
708
709         return 0;
710 END:
711         __metadata_parser_clear_dir_list(md_list);
712
713         if (md_tag)
714                 free(md_tag);
715
716         return ret;
717 }
718
719 static int __run_category_parser_prestep(manifest_x *mfx, char *category_key, ACTION_TYPE action)
720 {
721         int ret = -1;
722         int tag_exist = 0;
723         char buffer[1024] = { 0, };
724         GList *app_tmp;
725         application_x *app;
726         GList *category_tmp;
727         const char *category;
728         char *category_tag = NULL;
729
730         GList *category_list = NULL;
731         __category_t *category_detail = NULL;
732
733         category_tag = __get_tag_by_key(category_key);
734         if (category_tag == NULL) {
735                 _LOGD("md_tag is NULL\n");
736                 return -1;
737         }
738
739         for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
740                 app = (application_x *)app_tmp->data;
741                 if (app == NULL)
742                         continue;
743                 for (category_tmp = app->category; category_tmp; category_tmp = category_tmp->next) {
744                         category = (const char *)category_tmp->data;
745                         /* get glist of category key and value combination */
746                         memset(buffer, 0x00, 1024);
747                         snprintf(buffer, 1024, "%s/", category_key);
748                         if ((category) && (strncmp(category, category_key, strlen(category_key)) == 0)) {
749                                 category_detail = (__category_t*) calloc(1, sizeof(__category_t));
750                                 if (category_detail == NULL) {
751                                         _LOGD("Memory allocation failed\n");
752                                         goto END;
753                                 }
754
755                                 category_detail->name = strdup(category);
756                                 if (category_detail->name == NULL) {
757                                         _LOGD("Memory allocation failed\n");
758                                         free(category_detail);
759                                         goto END;
760                                 }
761
762                                 category_list = g_list_append(category_list, (gpointer)category_detail);
763                                 tag_exist = 1;
764                         }
765                 }
766
767                 /* send glist to parser when tags for metadata plugin parser exist. */
768                 if (tag_exist) {
769                         ret = __ps_run_category_parser(category_list, category_tag, action, mfx->package, app->appid);
770                         if (ret < 0)
771                                 _LOGD("category_parser failed[%d] for tag[%s]\n", ret, category_tag);
772                         else
773                                 _LOGD("category_parser success for tag[%s]\n", category_tag);
774                 }
775                 __category_parser_clear_dir_list(category_list);
776                 category_list = NULL;
777                 tag_exist = 0;
778         }
779
780         return 0;
781 END:
782         __category_parser_clear_dir_list(category_list);
783
784         if (category_tag)
785                 free(category_tag);
786
787         return ret;
788 }
789
790 static void __process_tag(void *lib_handle, xmlTextReaderPtr reader, ACTION_TYPE action, char *tag, const char *pkgid)
791 {
792         switch (xmlTextReaderNodeType(reader)) {
793         case XML_READER_TYPE_END_ELEMENT:
794                 {
795                         break;
796                 }
797         case XML_READER_TYPE_ELEMENT:
798                 {
799                         /* Elements without closing tag don't receive */
800                         const xmlChar *elementName =
801                             xmlTextReaderLocalName(reader);
802                         if (elementName == NULL)
803                                 break;
804
805                         if (strcmp(tag, ASCII(elementName)) == 0) {
806                                 _LOGD("find : tag[%s] ACTION_TYPE[%d] pkg[%s]\n", tag, action, pkgid);
807                                 __run_tag_parser_prestep(lib_handle, reader, action, pkgid);
808                                 break;
809                         }
810                         break;
811                 }
812
813         default:
814                 break;
815         }
816 }
817
818 static int __parser_send_tag(void *lib_handle, ACTION_TYPE action, PLUGIN_PROCESS_TYPE process, const char *pkgid)
819 {
820         int (*plugin_install) (const char *);
821         int ret = -1;
822         char *ac = NULL;
823
824         if (process == PLUGIN_PRE_PROCESS) {
825                 switch (action) {
826                 case ACTION_INSTALL:
827                         ac = "PKGMGR_PARSER_PLUGIN_PRE_INSTALL";
828                         break;
829                 case ACTION_UPGRADE:
830                         ac = "PKGMGR_PARSER_PLUGIN_PRE_UPGRADE";
831                         break;
832                 case ACTION_UNINSTALL:
833                         ac = "PKGMGR_PARSER_PLUGIN_PRE_UNINSTALL";
834                         break;
835                 default:
836                         return -1;
837                 }
838         } else if (process == PLUGIN_POST_PROCESS) {
839                 switch (action) {
840                 case ACTION_INSTALL:
841                         ac = "PKGMGR_PARSER_PLUGIN_POST_INSTALL";
842                         break;
843                 case ACTION_UPGRADE:
844                         ac = "PKGMGR_PARSER_PLUGIN_POST_UPGRADE";
845                         break;
846                 case ACTION_UNINSTALL:
847                         ac = "PKGMGR_PARSER_PLUGIN_POST_UNINSTALL";
848                         break;
849                 default:
850                         return -1;
851                 }
852         } else
853                 return -1;
854
855         if ((plugin_install =
856                 dlsym(lib_handle, ac)) == NULL || dlerror() != NULL) {
857                 return -1;
858         }
859
860         ret = plugin_install(pkgid);
861         return ret;
862 }
863
864 static int __next_child_element(xmlTextReaderPtr reader, int depth)
865 {
866         int ret = xmlTextReaderRead(reader);
867         int cur = xmlTextReaderDepth(reader);
868         while (ret == 1) {
869
870                 switch (xmlTextReaderNodeType(reader)) {
871                 case XML_READER_TYPE_ELEMENT:
872                         if (cur == depth + 1)
873                                 return 1;
874                         break;
875                 case XML_READER_TYPE_TEXT:
876                         /*text is handled by each function separately*/
877                         if (cur == depth + 1)
878                                 return 0;
879                         break;
880                 case XML_READER_TYPE_END_ELEMENT:
881                         if (cur == depth)
882                                 return 0;
883                         break;
884                 default:
885                         if (cur <= depth)
886                                 return 0;
887                         break;
888                 }
889                 ret = xmlTextReaderRead(reader);
890                 cur = xmlTextReaderDepth(reader);
891         }
892         return ret;
893 }
894 int __ps_process_tag_parser(manifest_x *mfx, const char *filename, ACTION_TYPE action)
895 {
896         xmlTextReaderPtr reader;
897         xmlDocPtr docPtr;
898         int ret = -1;
899         FILE *fp = NULL;
900         void *lib_handle = NULL;
901         char tag[PKG_STRING_LEN_MAX] = { 0 };
902
903         fp = fopen(TAG_PARSER_LIST, "r");
904         retvm_if(fp == NULL, PMINFO_R_ERROR, "no preload list");
905
906         while (fgets(tag, sizeof(tag), fp) != NULL) {
907                 __str_trim(tag);
908
909                 lib_handle = __open_lib_handle(tag);
910                 if (lib_handle == NULL)
911                         continue;
912
913                 ret = __parser_send_tag(lib_handle, action, PLUGIN_PRE_PROCESS, mfx->package);
914                 _LOGD("PLUGIN_PRE_PROCESS[%s, %s] ACTION_TYPE[%d] result[%d]\n", mfx->package, tag, action, ret);
915
916                 docPtr = xmlReadFile(filename, NULL, 0);
917                 reader = xmlReaderWalker(docPtr);
918                 if (reader != NULL) {
919                         ret = xmlTextReaderRead(reader);
920                         while (ret == 1) {
921                                 __process_tag(lib_handle, reader, action, tag, mfx->package);
922                                 ret = xmlTextReaderRead(reader);
923                         }
924                         xmlFreeTextReader(reader);
925
926                         if (ret != 0)
927                                 _LOGD("%s : failed to parse", filename);
928                 } else {
929                         _LOGD("Unable to open %s", filename);
930                 }
931
932                 ret = __parser_send_tag(lib_handle, action, PLUGIN_POST_PROCESS, mfx->package);
933                 _LOGD("PLUGIN_POST_PROCESS[%s, %s] ACTION_TYPE[%d] result[%d]\n", mfx->package, tag, action, ret);
934
935                 __close_lib_handle(lib_handle);
936
937                 memset(tag, 0x00, sizeof(tag));
938         }
939
940         if (fp != NULL)
941                 fclose(fp);
942
943         return 0;
944 }
945
946 int __ps_process_metadata_parser(manifest_x *mfx, ACTION_TYPE action)
947 {
948         fprintf(stdout, "__ps_process_metadata_parser\n");
949         int ret = 0;
950         FILE *fp = NULL;
951         char md_key[PKG_STRING_LEN_MAX] = { 0 };
952
953         fp = fopen(METADATA_PARSER_LIST, "r");
954         if (fp == NULL) {
955                 _LOGD("no preload list\n");
956                 return -1;
957         }
958
959         while (fgets(md_key, sizeof(md_key), fp) != NULL) {
960                 __str_trim(md_key);
961                 ret = __run_metadata_parser_prestep(mfx, md_key, action);
962                 if (ret < 0)
963                         break;
964         }
965
966         if (fp != NULL)
967                 fclose(fp);
968
969         return ret;
970 }
971
972 int __ps_process_category_parser(manifest_x *mfx, ACTION_TYPE action)
973 {
974         int ret = 0;
975         FILE *fp = NULL;
976         char category_key[PKG_STRING_LEN_MAX] = { 0 };
977
978         fp = fopen(CATEGORY_PARSER_LIST, "r");
979         if (fp == NULL) {
980                 _LOGD("no category parser list\n");
981                 return -1;
982         }
983
984         while (fgets(category_key, sizeof(category_key), fp) != NULL) {
985                 __str_trim(category_key);
986                 ret = __run_category_parser_prestep(mfx, category_key, action);
987                 if (ret < 0)
988                         break;
989         }
990
991         if (fp != NULL)
992                 fclose(fp);
993
994         return ret;
995 }
996
997 static int __ps_process_allowed(xmlTextReaderPtr reader, char **allowed)
998 {
999         __save_xml_value(reader, allowed);
1000         return 0;
1001 }
1002
1003 static int __ps_process_condition(xmlTextReaderPtr reader, char **condition)
1004 {
1005         __save_xml_attribute(reader, "name", condition, NULL);
1006         return 0;
1007 }
1008
1009 static int __ps_process_notification(xmlTextReaderPtr reader, notification_x *notification)
1010 {
1011         __save_xml_attribute(reader, "name", &notification->name, NULL);
1012         __save_xml_value(reader, &notification->text);
1013         return 0;
1014 }
1015
1016 static int __ps_process_category(xmlTextReaderPtr reader, char **category)
1017 {
1018         __save_xml_attribute(reader, "name", category, NULL);
1019         return 0;
1020 }
1021
1022 static int __ps_process_privilege(xmlTextReaderPtr reader, privilege_x *privilege)
1023 {
1024         __save_xml_attribute(reader, "type", &privilege->type, "tpk");
1025         __save_xml_value(reader, &privilege->value);
1026         return 0;
1027 }
1028
1029 static int __ps_process_metadata(xmlTextReaderPtr reader, metadata_x *metadata)
1030 {
1031         __save_xml_attribute(reader, "key", &metadata->key, NULL);
1032         __save_xml_attribute(reader, "value", &metadata->value, NULL);
1033         return 0;
1034 }
1035
1036 static int __ps_process_permission(xmlTextReaderPtr reader, permission_x *permission)
1037 {
1038         __save_xml_attribute(reader, "type", &permission->type, NULL);
1039         __save_xml_value(reader, &permission->value);
1040         return 0;
1041 }
1042
1043 static int __ps_process_compatibility(xmlTextReaderPtr reader, compatibility_x *compatibility)
1044 {
1045         __save_xml_attribute(reader, "name", &compatibility->name, NULL);
1046         __save_xml_value(reader, &compatibility->text);
1047         return 0;
1048 }
1049
1050 static int __ps_process_request(xmlTextReaderPtr reader, char **request)
1051 {
1052         __save_xml_value(reader, request);
1053         return 0;
1054 }
1055
1056 static int __ps_process_define(xmlTextReaderPtr reader, define_x *define)
1057 {
1058         const xmlChar *node;
1059         int ret = -1;
1060         int depth = -1;
1061         char *val;
1062
1063         __save_xml_attribute(reader, "path", &define->path, NULL);
1064
1065         depth = xmlTextReaderDepth(reader);
1066         while ((ret = __next_child_element(reader, depth))) {
1067                 node = xmlTextReaderConstName(reader);
1068                 if (!node) {
1069                         _LOGD("xmlTextReaderConstName value is NULL\n");
1070                         return -1;
1071                 }
1072
1073                 if (!strcmp(ASCII(node), "allowed")) {
1074                         val = NULL;
1075                         ret = __ps_process_allowed(reader, &val);
1076                         if (val)
1077                                 define->allowed = g_list_append(define->allowed, (gpointer)val);
1078                 } else if (!strcmp(ASCII(node), "request")) {
1079                         val = NULL;
1080                         ret = __ps_process_request(reader, &val);
1081                         if (val)
1082                                 define->request = g_list_append(define->request, (gpointer)val);
1083                 } else {
1084                         return -1;
1085                 }
1086                 if (ret < 0) {
1087                         _LOGD("Processing define failed\n");
1088                         return ret;
1089                 }
1090         }
1091         return ret;
1092 }
1093
1094 struct appcontrol_data {
1095         GList *operations;
1096         GList *uris;
1097         GList *mimes;
1098         GList *appcontrols;
1099         char operation[BUFSIZE];
1100         char uri[BUFSIZE];
1101         char mime[BUFSIZE];
1102 };
1103
1104 static void __ps_process_mime(gpointer data, gpointer user_data)
1105 {
1106         char *mime = (char *)data;
1107         struct appcontrol_data *ad = (struct appcontrol_data *)user_data;
1108         appcontrol_x *appcontrol;
1109
1110         snprintf(ad->mime, sizeof(ad->mime), "%s", mime);
1111
1112         appcontrol = calloc(1, sizeof(appcontrol_x));
1113         if (strlen(ad->operation))
1114                 appcontrol->operation = strdup(ad->operation);
1115         if (strlen(ad->uri))
1116                 appcontrol->uri = strdup(ad->uri);
1117         appcontrol->mime = strdup(ad->mime);
1118         ad->appcontrols = g_list_append(ad->appcontrols, appcontrol);
1119 }
1120
1121 static void __ps_process_uri(gpointer data, gpointer user_data)
1122 {
1123         char *uri = (char *)data;
1124         struct appcontrol_data *ad = (struct appcontrol_data *)user_data;
1125         appcontrol_x *appcontrol;
1126
1127         snprintf(ad->uri, sizeof(ad->uri), "%s", uri);
1128
1129         if (ad->mimes != NULL) {
1130                 g_list_foreach(ad->mimes, __ps_process_mime, user_data);
1131         } else {
1132                 appcontrol = calloc(1, sizeof(appcontrol_x));
1133                 if (strlen(ad->operation))
1134                         appcontrol->operation = strdup(ad->operation);
1135                 appcontrol->uri = strdup(ad->uri);
1136                 ad->appcontrols = g_list_append(ad->appcontrols, appcontrol);
1137         }
1138 }
1139
1140 static void __ps_process_operation(gpointer data, gpointer user_data)
1141 {
1142         char *operation = (char *)data;
1143         struct appcontrol_data *ad = (struct appcontrol_data *)user_data;
1144         appcontrol_x *appcontrol;
1145
1146         snprintf(ad->operation, sizeof(ad->operation), "%s", operation);
1147
1148         if (ad->uris != NULL) {
1149                 g_list_foreach(ad->uris, __ps_process_uri, user_data);
1150         } else if (ad->mimes != NULL) {
1151                 g_list_foreach(ad->mimes, __ps_process_mime, user_data);
1152         } else {
1153                 appcontrol = calloc(1, sizeof(appcontrol_x));
1154                 appcontrol->operation = strdup(ad->operation);
1155                 ad->appcontrols = g_list_append(ad->appcontrols, appcontrol);
1156         }
1157 }
1158
1159 static GList *__make_appcontrol_list(GList *operations, GList *uris, GList *mimes)
1160 {
1161         struct appcontrol_data ad = {0, };
1162
1163         ad.operations = operations;
1164         ad.uris = uris;
1165         ad.mimes = mimes;
1166
1167         if (ad.operations == NULL)
1168                 return NULL;
1169
1170         g_list_foreach(ad.operations, __ps_process_operation, (gpointer)&ad);
1171
1172         return ad.appcontrols;
1173 }
1174
1175 static int __ps_process_appcontrol(xmlTextReaderPtr reader, GList **appcontrol)
1176 {
1177         const xmlChar *node;
1178         int ret = -1;
1179         int depth = -1;
1180         char *val;
1181         GList *operations = NULL;
1182         GList *uris = NULL;
1183         GList *mimes = NULL;
1184         GList *result;
1185
1186         depth = xmlTextReaderDepth(reader);
1187         while ((ret = __next_child_element(reader, depth)) > 0) {
1188                 node = xmlTextReaderConstName(reader);
1189                 if (!node) {
1190                         _LOGD("xmlTextReaderConstName value is NULL\n");
1191                         return -1;
1192                 }
1193
1194                 val = NULL;
1195                 if (!strcmp(ASCII(node), "operation")) {
1196                         __save_xml_attribute(reader, "name", &val, NULL);
1197                         if (val)
1198                                 operations = g_list_append(operations, (gpointer)val);
1199                         _LOGD("operation processing\n");
1200                 } else if (!strcmp(ASCII(node), "uri")) {
1201                         __save_xml_attribute(reader, "name", &val, NULL);
1202                         if (val)
1203                                 uris = g_list_append(uris, (gpointer)val);
1204                         _LOGD("uri processing\n");
1205                 } else if (!strcmp(ASCII(node), "mime")) {
1206                         __save_xml_attribute(reader, "name", &val, NULL);
1207                         if (val)
1208                                 mimes = g_list_append(mimes, (gpointer)val);
1209                         _LOGD("mime processing\n");
1210                 } else if (!strcmp(ASCII(node), "subapp")) {
1211                         continue;
1212                 } else {
1213                         ret = -1;
1214                 }
1215         }
1216
1217         if (ret < 0) {
1218                 _LOGD("Processing appcontrol failed\n");
1219                 g_list_free_full(operations, free);
1220                 g_list_free_full(uris, free);
1221                 g_list_free_full(mimes, free);
1222                 return ret;
1223         }
1224
1225         result = __make_appcontrol_list(operations, uris, mimes);
1226         if (result)
1227                 *appcontrol = g_list_concat(*appcontrol, result);
1228         else
1229                 ret = -1;
1230
1231         g_list_free_full(operations, free);
1232         g_list_free_full(uris, free);
1233         g_list_free_full(mimes, free);
1234
1235         return ret;
1236 }
1237
1238 static int __ps_process_privileges(xmlTextReaderPtr reader, GList **privileges)
1239 {
1240         const xmlChar *node;
1241         int ret = -1;
1242         int depth = -1;
1243
1244         depth = xmlTextReaderDepth(reader);
1245         while ((ret = __next_child_element(reader, depth))) {
1246                 node = xmlTextReaderConstName(reader);
1247                 if (!node) {
1248                         _LOGD("xmlTextReaderConstName value is NULL\n");
1249                         return -1;
1250                 }
1251
1252                 if (strcmp(ASCII(node), "privilege") == 0) {
1253                         privilege_x *privilege = calloc(1, sizeof(privilege_x));
1254                         if (privilege == NULL) {
1255                                 _LOGD("Malloc Failed\n");
1256                                 return -1;
1257                         }
1258                         ret = __ps_process_privilege(reader, privilege);
1259                         if (ret < 0)
1260                                 free(privilege);
1261                         else
1262                                 *privileges = g_list_append(*privileges, (gpointer)privilege);
1263                 } else {
1264                         return -1;
1265                 }
1266                 if (ret < 0) {
1267                         _LOGD("Processing privileges failed\n");
1268                         return ret;
1269                 }
1270         }
1271         return ret;
1272 }
1273
1274 static int __ps_process_launchconditions(xmlTextReaderPtr reader, GList **launchconditions)
1275 {
1276         const xmlChar *node;
1277         int ret = -1;
1278         int depth = -1;
1279         char *val;
1280
1281         depth = xmlTextReaderDepth(reader);
1282         while ((ret = __next_child_element(reader, depth))) {
1283                 node = xmlTextReaderConstName(reader);
1284                 if (!node) {
1285                         _LOGD("xmlTextReaderConstName value is NULL\n");
1286                         return -1;
1287                 }
1288
1289                 if (strcmp(ASCII(node), "condition") == 0) {
1290                         val = NULL;
1291                         ret = __ps_process_condition(reader, &val);
1292                         if (val)
1293                                 *launchconditions = g_list_append(*launchconditions, (gpointer)val);
1294                 } else
1295                         return -1;
1296                 if (ret < 0) {
1297                         _LOGD("Processing launchconditions failed\n");
1298                         return ret;
1299                 }
1300         }
1301
1302         return ret;
1303 }
1304
1305 static int __ps_process_datashare(xmlTextReaderPtr reader, datashare_x *datashare)
1306 {
1307         const xmlChar *node;
1308         int ret = -1;
1309         int depth = -1;
1310         char *val;
1311         depth = xmlTextReaderDepth(reader);
1312         while ((ret = __next_child_element(reader, depth))) {
1313                 node = xmlTextReaderConstName(reader);
1314                 if (!node) {
1315                         _LOGD("xmlTextReaderConstName value is NULL\n");
1316                         return -1;
1317                 }
1318
1319                 if (!strcmp(ASCII(node), "define")) {
1320                         define_x *define = calloc(1, sizeof(define_x));
1321                         if (define == NULL) {
1322                                 _LOGD("Malloc Failed\n");
1323                                 return -1;
1324                         }
1325                         datashare->define = g_list_append(datashare->define, define);
1326                         ret = __ps_process_define(reader, define);
1327                 } else if (!strcmp(ASCII(node), "request")) {
1328                         val = NULL;
1329                         ret = __ps_process_request(reader, &val);
1330                         if (val)
1331                                 datashare->request = g_list_append(datashare->request, (gpointer)val);
1332                 } else
1333                         return -1;
1334                 if (ret < 0) {
1335                         _LOGD("Processing data-share failed\n");
1336                         return ret;
1337                 }
1338         }
1339         return ret;
1340 }
1341
1342 static char *__get_icon_with_path(const char *icon, uid_t uid)
1343 {
1344         char icon_with_path[BUFSIZE];
1345         const char *app_path;
1346
1347         if (!icon || !package)
1348                 return NULL;
1349
1350         /* just use absolute path */
1351         if (index(icon, '/'))
1352                 return strdup(icon);
1353
1354         do {
1355                 if (uid == GLOBAL_USER || uid == OWNER_ROOT) {
1356                         snprintf(icon_with_path, sizeof(icon_with_path),
1357                                 "%s%s", getIconPath(uid, true), icon);
1358                         if (access(icon_with_path, F_OK) == 0)
1359                                 break;
1360
1361                         snprintf(icon_with_path, sizeof(icon_with_path),
1362                                 "%s%s", getIconPath(uid, false), icon);
1363                         if (access(icon_with_path, F_OK) == 0)
1364                                 break;
1365
1366                         /* for backward compatibility (.../default/small/...)
1367                          * this should be removed
1368                          */
1369                         snprintf(icon_with_path, sizeof(icon_with_path),
1370                                 "%sdefault/small/%s",
1371                                 getIconPath(uid, true), icon);
1372                         if (access(icon_with_path, F_OK) == 0)
1373                                 break;
1374
1375                         snprintf(icon_with_path, sizeof(icon_with_path),
1376                                 "%sdefault/small/%s",
1377                                 getIconPath(uid, false), icon);
1378                         if (access(icon_with_path, F_OK) == 0)
1379                                 break;
1380
1381                         /* If doesn't exist in case of Global app,
1382                          * try to get icon directly into app's directory
1383                          */
1384                         app_path = tzplatform_getenv(TZ_SYS_RO_APP);
1385
1386                         snprintf(icon_with_path, sizeof(icon_with_path),
1387                                 "%s/%s/%s", app_path, package, icon);
1388                         if (access(icon_with_path, F_OK) == 0)
1389                                 break;
1390
1391                         app_path = tzplatform_getenv(TZ_SYS_RW_APP);
1392
1393                         snprintf(icon_with_path, sizeof(icon_with_path),
1394                                 "%s/%s/%s", app_path, package, icon);
1395                         if (access(icon_with_path, F_OK) == 0)
1396                                 break;
1397                 } else {
1398                         tzplatform_set_user(uid);
1399                         app_path = tzplatform_getenv(TZ_USER_APP);
1400                         tzplatform_reset_user();
1401
1402                         snprintf(icon_with_path, sizeof(icon_with_path),
1403                                 "%s/%s/%s", app_path, package, icon);
1404                         if (access(icon_with_path, F_OK) == 0)
1405                                 break;
1406                 }
1407
1408                 /* some preload package has icons at below path */
1409                 snprintf(icon_with_path, sizeof(icon_with_path),
1410                                 "%s/%s/res/icons/%s", app_path, package, icon);
1411                 if (access(icon_with_path, F_OK) == 0)
1412                         break;
1413
1414                 /* since 2.3 tpk package */
1415                 snprintf(icon_with_path, sizeof(icon_with_path),
1416                                 "%s/%s/shared/res/%s", app_path, package, icon);
1417                 if (access(icon_with_path, F_OK) == 0)
1418                         break;
1419
1420                 _LOGE("cannot find icon path for [%s]", icon);
1421                 return NULL;
1422         } while (0);
1423
1424         _LOGD("Icon path : %s ---> %s", icon, icon_with_path);
1425
1426         return strdup(icon_with_path);
1427 }
1428
1429 static void __ps_process_tag(manifest_x * mfx, char *const tagv[])
1430 {
1431         int i = 0;
1432         char delims[] = "=";
1433         char *ret_result = NULL;
1434         char *tag = NULL;
1435         char *ptr = NULL;
1436
1437         if (tagv == NULL)
1438                 return;
1439
1440         for (tag = strdup(tagv[0]); tag != NULL; ) {
1441                 ret_result = strtok_r(tag, delims, &ptr);
1442
1443                 /*check tag :  preload */
1444                 if (strcmp(ret_result, "preload") == 0) {
1445                         ret_result = strtok_r(NULL, delims, &ptr);
1446                         if (strcmp(ret_result, "true") == 0) {
1447                                 free((void *)mfx->preload);
1448                                 mfx->preload = strdup("true");
1449                         } else if (strcmp(ret_result, "false") == 0) {
1450                                 free((void *)mfx->preload);
1451                                 mfx->preload = strdup("false");
1452                         }
1453                 /*check tag :  removable*/
1454                 } else if (strcmp(ret_result, "removable") == 0) {
1455                         ret_result = strtok_r(NULL, delims, &ptr);
1456                         if (strcmp(ret_result, "true") == 0) {
1457                                 free((void *)mfx->removable);
1458                                 mfx->removable = strdup("true");
1459                         } else if (strcmp(ret_result, "false") == 0) {
1460                                 free((void *)mfx->removable);
1461                                 mfx->removable = strdup("false");
1462                         }
1463                 /*check tag :  not matched*/
1464                 } else
1465                         _LOGD("tag process [%s]is not defined\n", ret_result);
1466
1467                 free(tag);
1468
1469                 /*check next value*/
1470                 if (tagv[++i] != NULL)
1471                         tag = strdup(tagv[i]);
1472                 else {
1473                         _LOGD("tag process success...\n");
1474                         return;
1475                 }
1476         }
1477 }
1478
1479 static int __ps_process_icon(xmlTextReaderPtr reader, icon_x *icon, uid_t uid)
1480 {
1481         __save_xml_attribute(reader, "section", &icon->section, NULL);
1482         __save_xml_attribute(reader, "size", &icon->size, NULL);
1483         __save_xml_attribute(reader, "resolution", &icon->resolution, NULL);
1484         __save_xml_lang(reader, &icon->lang);
1485
1486         xmlTextReaderRead(reader);
1487         char *text  = ASCII(xmlTextReaderValue(reader));
1488         if (text) {
1489                 icon->text = __get_icon_with_path(text, uid);
1490                 free(text);
1491         }
1492
1493         return 0;
1494 }
1495
1496 static int __ps_process_image(xmlTextReaderPtr reader, image_x *image)
1497 {
1498         __save_xml_attribute(reader, "section", &image->section, NULL);
1499         __save_xml_lang(reader, &image->lang);
1500         __save_xml_value(reader, &image->text);
1501         return 0;
1502 }
1503
1504 static int __ps_process_label(xmlTextReaderPtr reader, label_x *label)
1505 {
1506         __save_xml_attribute(reader, "name", &label->name, NULL);
1507         __save_xml_lang(reader, &label->lang);
1508         __save_xml_value(reader, &label->text);
1509         return 0;
1510
1511 }
1512
1513 static int __ps_process_author(xmlTextReaderPtr reader, author_x *author)
1514 {
1515         __save_xml_attribute(reader, "email", &author->email, NULL);
1516         __save_xml_attribute(reader, "href", &author->href, NULL);
1517         __save_xml_value(reader, &author->text);
1518         return 0;
1519 }
1520
1521 static int __ps_process_description(xmlTextReaderPtr reader, description_x *description)
1522 {
1523         __save_xml_lang(reader, &description->lang);
1524         __save_xml_value(reader, &description->text);
1525         return 0;
1526 }
1527
1528 static int __ps_process_license(xmlTextReaderPtr reader, license_x *license)
1529 {
1530         __save_xml_lang(reader, &license->lang);
1531         __save_xml_value(reader, &license->text);
1532         return 0;
1533 }
1534
1535 static int __ps_process_datacontrol(xmlTextReaderPtr reader, datacontrol_x *datacontrol)
1536 {
1537         __save_xml_attribute(reader, "providerid", &datacontrol->providerid, NULL);
1538         __save_xml_attribute(reader, "access", &datacontrol->access, NULL);
1539         __save_xml_attribute(reader, "type", &datacontrol->type, NULL);
1540         return 0;
1541 }
1542
1543 static int __ps_process_splashscreen(xmlTextReaderPtr reader, splashscreen_x *splashscreen)
1544 {
1545         __save_xml_attribute(reader, "src", &splashscreen->src, NULL);
1546         __save_xml_attribute(reader, "type", &splashscreen->type, NULL);
1547         __save_xml_attribute(reader, "dpi", &splashscreen->dpi, NULL);
1548         __save_xml_attribute(reader, "orientation", &splashscreen->orientation, NULL);
1549         __save_xml_attribute(reader, "indicator-display", &splashscreen->indicatordisplay, NULL);
1550         __save_xml_attribute(reader, "app-control-operation", &splashscreen->operation, NULL);
1551         __save_xml_attribute(reader, "color-depth", &splashscreen->color_depth, NULL);
1552         return 0;
1553 }
1554
1555 static int __ps_process_splashscreens(xmlTextReaderPtr reader, GList **splashscreens)
1556 {
1557         const xmlChar *node;
1558         int ret = -1;
1559         int depth = -1;
1560         splashscreen_x *splashscreen;
1561
1562         depth = xmlTextReaderDepth(reader);
1563         while ((ret = __next_child_element(reader, depth))) {
1564                 node = xmlTextReaderConstName(reader);
1565                 if (!node) {
1566                         _LOGD("xmlTextReaderConstName value is NULL\n");
1567                         return -1;
1568                 }
1569
1570                 if (strcmp(ASCII(node), "splash-screen") == 0) {
1571                         splashscreen = calloc(1, sizeof(splashscreen_x));
1572                         if (splashscreen == NULL) {
1573                                 _LOGD("Malloc Failed\n");
1574                                 return -1;
1575                         }
1576                         *splashscreens = g_list_append(*splashscreens, splashscreen);
1577                         ret = __ps_process_splashscreen(reader, splashscreen);
1578                 } else {
1579                         return -1;
1580                 }
1581
1582                 if (ret < 0) {
1583                         _LOGD("Processing splash-screen failed\n");
1584                         return ret;
1585                 }
1586         }
1587         return 0;
1588 }
1589
1590 static int __ps_process_application(xmlTextReaderPtr reader, application_x *application, int type, uid_t uid)
1591 {
1592         const xmlChar *node;
1593         int ret = -1;
1594         int depth = -1;
1595         char *val;
1596
1597         __save_xml_attribute(reader, "appid", &application->appid, NULL);
1598         retvm_if(application->appid == NULL, PM_PARSER_R_ERROR, "appid cant be NULL, appid field is mandatory\n");
1599         __save_xml_attribute(reader, "exec", &application->exec, NULL);
1600         __save_xml_attribute(reader, "nodisplay", &application->nodisplay, "false");
1601         __save_xml_attribute(reader, "multiple", &application->multiple, "false");
1602         __save_xml_attribute(reader, "type", &application->type, NULL);
1603         __save_xml_attribute(reader, "categories", &application->categories, NULL);
1604         __save_xml_attribute(reader, "extraid", &application->extraid, NULL);
1605         __save_xml_attribute(reader, "taskmanage", &application->taskmanage, "true");
1606         __save_xml_attribute(reader, "enabled", &application->enabled, "true");
1607         __save_xml_attribute(reader, "hw-acceleration", &application->hwacceleration, "default");
1608         __save_xml_attribute(reader, "screen-reader", &application->screenreader, "use-system-setting");
1609         __save_xml_attribute(reader, "mainapp", &application->mainapp, "false");
1610         __save_xml_attribute(reader, "recentimage", &application->recentimage, "false");
1611         __save_xml_attribute(reader, "launchcondition", &application->launchcondition, "false");
1612         __save_xml_attribute(reader, "indicatordisplay", &application->indicatordisplay, "true");
1613         __save_xml_attribute(reader, "portrait-effectimage", &application->portraitimg, NULL);
1614         __save_xml_attribute(reader, "landscape-effectimage", &application->landscapeimg, NULL);
1615         __save_xml_attribute(reader, "guestmode-visibility", &application->guestmode_visibility, "true");
1616         __save_xml_attribute(reader, "permission-type", &application->permission_type, "normal");
1617         __save_xml_attribute(reader, "component-type", &application->component_type, type == PMINFO_UI_APP ? "uiapp" : type == PMINFO_SVC_APP ? "svcapp" : "widgetapp");
1618         /*component_type has "svcapp" or "uiapp", if it is not, parsing manifest is fail*/
1619         retvm_if(((strcmp(application->component_type, "svcapp") != 0) && (strcmp(application->component_type, "uiapp") != 0) && (strcmp(application->component_type, "widgetapp") != 0)), PM_PARSER_R_ERROR, "invalid component_type[%s]", application->component_type);
1620         __save_xml_attribute(reader, "submode", &application->submode, "false");
1621         __save_xml_attribute(reader, "submode-mainid", &application->submode_mainid, NULL);
1622         __save_xml_attribute(reader, "process-pool", &application->process_pool, "false");
1623         __save_xml_attribute(reader, "launch_mode", &application->launch_mode, "caller");
1624         __save_xml_attribute(reader, "ui-gadget", &application->ui_gadget, "false");
1625         __save_xml_attribute(reader, "auto-restart", &application->autorestart, "false");
1626         __save_xml_attribute(reader, "on-boot", &application->onboot, "false");
1627         __save_xml_attribute(reader, "splash-screen-display", &application->splash_screen_display, "true");
1628
1629         application->package = strdup(package);
1630         /* overwrite some attributes if the app is widgetapp */
1631         if (type == PMINFO_WIDGET_APP || type == PMINFO_WATCH_APP) {
1632                 free((void *)application->nodisplay);
1633                 application->nodisplay = strdup("true");
1634                 free((void *)application->multiple);
1635                 application->multiple = strdup("true");
1636                 free((void *)application->type);
1637                 application->type = strdup("capp");
1638                 free((void *)application->taskmanage);
1639                 application->taskmanage = strdup("false");
1640                 free((void *)application->indicatordisplay);
1641                 application->indicatordisplay = strdup("false");
1642         }
1643
1644         /* hw-acceleration values are changed from use-GL/not-use-GL/use-system-setting to on/off/default */
1645         if (strcmp(application->hwacceleration, "use-GL") == 0) {
1646                 free((void *)application->hwacceleration);
1647                 application->hwacceleration = strdup("on");
1648         } else if (strcmp(application->hwacceleration, "not-use-GL") == 0) {
1649                 free((void *)application->hwacceleration);
1650                 application->hwacceleration = strdup("off");
1651         } else if (strcmp(application->hwacceleration, "use-system-setting") == 0) {
1652                 free((void *)application->hwacceleration);
1653                 application->hwacceleration = strdup("default");
1654         }
1655
1656         depth = xmlTextReaderDepth(reader);
1657         while ((ret = __next_child_element(reader, depth))) {
1658                 node = xmlTextReaderConstName(reader);
1659                 if (!node) {
1660                         _LOGD("xmlTextReaderConstName value is NULL\n");
1661                         return -1;
1662                 }
1663                 if (!strcmp(ASCII(node), "label")) {
1664                         label_x *label = calloc(1, sizeof(label_x));
1665                         if (label == NULL) {
1666                                 _LOGD("Malloc Failed\n");
1667                                 return -1;
1668                         }
1669                         application->label = g_list_append(application->label, label);
1670                         ret = __ps_process_label(reader, label);
1671                 } else if (!strcmp(ASCII(node), "icon")) {
1672                         icon_x *icon = calloc(1, sizeof(icon_x));
1673                         if (icon == NULL) {
1674                                 _LOGD("Malloc Failed\n");
1675                                 return -1;
1676                         }
1677                         application->icon = g_list_append(application->icon, icon);
1678                         ret = __ps_process_icon(reader, icon, uid);
1679                 } else if (!strcmp(ASCII(node), "image")) {
1680                         image_x *image = calloc(1, sizeof(image_x));
1681                         if (image == NULL) {
1682                                 _LOGD("Malloc Failed\n");
1683                                 return -1;
1684                         }
1685                         application->image = g_list_append(application->image, image);
1686                         ret = __ps_process_image(reader, image);
1687                 } else if (!strcmp(ASCII(node), "category")) {
1688                         val = NULL;
1689                         ret = __ps_process_category(reader, &val);
1690                         if (val)
1691                                 application->category = g_list_append(application->category, (gpointer)val);
1692                 } else if (!strcmp(ASCII(node), "metadata")) {
1693                         metadata_x *metadata = calloc(1, sizeof(metadata_x));
1694                         if (metadata == NULL) {
1695                                 _LOGD("Malloc Failed\n");
1696                                 return -1;
1697                         }
1698                         application->metadata = g_list_append(application->metadata, metadata);
1699                         ret = __ps_process_metadata(reader, metadata);
1700                 } else if (!strcmp(ASCII(node), "permission")) {
1701                         permission_x *permission = calloc(1, sizeof(permission_x));
1702                         if (permission == NULL) {
1703                                 _LOGD("Malloc Failed\n");
1704                                 return -1;
1705                         }
1706                         application->permission = g_list_append(application->permission, permission);
1707                         ret = __ps_process_permission(reader, permission);
1708                 } else if (!strcmp(ASCII(node), "app-control")) {
1709                         ret = __ps_process_appcontrol(reader, &application->appcontrol);
1710                 } else if (!strcmp(ASCII(node), "application-service")) {
1711                         ret = __ps_process_appcontrol(reader, &application->appcontrol);
1712                 } else if (!strcmp(ASCII(node), "data-share")) {
1713                         datashare_x *datashare = calloc(1, sizeof(datashare_x));
1714                         if (datashare == NULL) {
1715                                 _LOGD("Malloc Failed\n");
1716                                 return -1;
1717                         }
1718                         application->datashare = g_list_append(application->datashare, datashare);
1719                         ret = __ps_process_datashare(reader, datashare);
1720                 } else if (!strcmp(ASCII(node), "launch-conditions")) {
1721                         ret = __ps_process_launchconditions(reader, &application->launchconditions);
1722                 } else if (!strcmp(ASCII(node), "notification")) {
1723                         notification_x *notification = calloc(1, sizeof(notification_x));
1724                         if (notification == NULL) {
1725                                 _LOGD("Malloc Failed\n");
1726                                 return -1;
1727                         }
1728                         application->notification = g_list_append(application->notification, notification);
1729                         ret = __ps_process_notification(reader, notification);
1730                 } else if (!strcmp(ASCII(node), "datacontrol")) {
1731                         datacontrol_x *datacontrol = calloc(1, sizeof(datacontrol_x));
1732                         if (datacontrol == NULL) {
1733                                 _LOGD("Malloc Failed\n");
1734                                 return -1;
1735                         }
1736                         application->datacontrol = g_list_append(application->datacontrol, datacontrol);
1737                         ret = __ps_process_datacontrol(reader, datacontrol);
1738                 } else if (!strcmp(ASCII(node), "splash-screens") == 0) {
1739                         ret = __ps_process_splashscreens(reader, &application->splashscreens);
1740                 } else
1741                         continue;
1742                 if (ret < 0) {
1743                         _LOGD("Processing application failed\n");
1744                         return ret;
1745                 }
1746         }
1747
1748         return ret;
1749 }
1750
1751 static int __start_process(xmlTextReaderPtr reader, manifest_x * mfx, uid_t uid)
1752 {
1753         _LOGD("__start_process\n");
1754         const xmlChar *node;
1755         int ret = -1;
1756         int depth = -1;
1757
1758         depth = xmlTextReaderDepth(reader);
1759         while ((ret = __next_child_element(reader, depth))) {
1760                 node = xmlTextReaderConstName(reader);
1761                 if (!node) {
1762                         _LOGD("xmlTextReaderConstName value is NULL\n");
1763                         return -1;
1764                 }
1765
1766                 if (!strcmp(ASCII(node), "label")) {
1767                         label_x *label = calloc(1, sizeof(label_x));
1768                         if (label == NULL) {
1769                                 _LOGD("Malloc Failed\n");
1770                                 return -1;
1771                         }
1772                         mfx->label = g_list_append(mfx->label, label);
1773                         ret = __ps_process_label(reader, label);
1774                 } else if (!strcmp(ASCII(node), "author")) {
1775                         author_x *author = calloc(1, sizeof(author_x));
1776                         if (author == NULL) {
1777                                 _LOGD("Malloc Failed\n");
1778                                 return -1;
1779                         }
1780                         mfx->author = g_list_append(mfx->author, author);
1781                         ret = __ps_process_author(reader, author);
1782                 } else if (!strcmp(ASCII(node), "description")) {
1783                         description_x *description = calloc(1, sizeof(description_x));
1784                         if (description == NULL) {
1785                                 _LOGD("Malloc Failed\n");
1786                                 return -1;
1787                         }
1788                         mfx->description = g_list_append(mfx->description, description);
1789                         ret = __ps_process_description(reader, description);
1790                 } else if (!strcmp(ASCII(node), "license")) {
1791                         license_x *license = calloc(1, sizeof(license_x));
1792                         if (license == NULL) {
1793                                 _LOGD("Malloc Failed\n");
1794                                 return -1;
1795                         }
1796                         mfx->license = g_list_append(mfx->license, license);
1797                         ret = __ps_process_license(reader, license);
1798                 } else if (!strcmp(ASCII(node), "privileges")) {
1799                         ret = __ps_process_privileges(reader, &mfx->privileges);
1800                 } else if (!strcmp(ASCII(node), "ui-application")) {
1801                         application_x *application = calloc(1, sizeof(application_x));
1802                         if (application == NULL) {
1803                                 _LOGD("Malloc Failed\n");
1804                                 return -1;
1805                         }
1806                         mfx->application = g_list_append(mfx->application, application);
1807                         ret = __ps_process_application(reader, application, PMINFO_UI_APP, uid);
1808                 } else if (!strcmp(ASCII(node), "service-application")) {
1809                         application_x *application = calloc(1, sizeof(application_x));
1810                         if (application == NULL) {
1811                                 _LOGD("Malloc Failed\n");
1812                                 return -1;
1813                         }
1814                         mfx->application = g_list_append(mfx->application, application);
1815                         ret = __ps_process_application(reader, application, PMINFO_SVC_APP, uid);
1816                 } else if (!strcmp(ASCII(node), "widget-application")) {
1817                         application_x *application = calloc(1, sizeof(application_x));
1818                         if (application == NULL) {
1819                                 _LOGD("Malloc Failed\n");
1820                                 return -1;
1821                         }
1822                         mfx->application = g_list_append(mfx->application, application);
1823                         ret = __ps_process_application(reader, application, PMINFO_WIDGET_APP, uid);
1824                 } else if (!strcmp(ASCII(node), "watch-application")) {
1825                         application_x *application = calloc(1, sizeof(application_x));
1826                         if (application == NULL) {
1827                                 _LOGD("Malloc Failed\n");
1828                                 return -1;
1829                         }
1830                         mfx->application = g_list_append(mfx->application, application);
1831                         ret = __ps_process_application(reader, application, PMINFO_WATCH_APP, uid);
1832                 } else if (!strcmp(ASCII(node), "icon")) {
1833                         icon_x *icon = calloc(1, sizeof(icon_x));
1834                         if (icon == NULL) {
1835                                 _LOGD("Malloc Failed\n");
1836                                 return -1;
1837                         }
1838                         mfx->icon = g_list_append(mfx->icon, icon);
1839                         ret = __ps_process_icon(reader, icon, uid);
1840                 } else if (!strcmp(ASCII(node), "compatibility")) {
1841                         compatibility_x *compatibility = calloc(1, sizeof(compatibility_x));
1842                         if (compatibility == NULL) {
1843                                 _LOGD("Malloc Failed\n");
1844                                 return -1;
1845                         }
1846                         mfx->compatibility = g_list_append(mfx->compatibility, compatibility);
1847                         ret = __ps_process_compatibility(reader, compatibility);
1848                 } else if (!strcmp(ASCII(node), "shortcut-list")) {
1849                         continue;
1850                 } else if (!strcmp(ASCII(node), "livebox")) {
1851                         continue;
1852                 } else if (!strcmp(ASCII(node), "account")) {
1853                         continue;
1854                 } else if (!strcmp(ASCII(node), "notifications")) {
1855                         continue;
1856                 } else if (!strcmp(ASCII(node), "ime")) {
1857                         continue;
1858                 } else if (!strcmp(ASCII(node), "feature")) {
1859                         continue;
1860                 } else {
1861                         _LOGI("Unknown element: %s", ASCII(node));
1862                         continue;
1863                 }
1864
1865                 if (ret < 0) {
1866                         _LOGD("Processing manifest failed\n");
1867                         return ret;
1868                 }
1869         }
1870         return ret;
1871 }
1872
1873 static int __process_manifest(xmlTextReaderPtr reader, manifest_x *mfx, uid_t uid)
1874 {
1875         const xmlChar *node;
1876         int ret = -1;
1877
1878         if ((ret = __next_child_element(reader, -1))) {
1879                 node = xmlTextReaderConstName(reader);
1880                 if (!node) {
1881                         _LOGD("xmlTextReaderConstName value is NULL\n");
1882                         return -1;
1883                 }
1884
1885                 if (!strcmp(ASCII(node), "manifest")) {
1886                         __save_xml_attribute(reader, "xmlns", &mfx->ns, NULL);
1887                         __save_xml_attribute(reader, "package", &mfx->package, NULL);
1888                         retvm_if(mfx->package == NULL, PM_PARSER_R_ERROR, "package cant be NULL, package field is mandatory\n");
1889                         __save_xml_attribute(reader, "version", &mfx->version, NULL);
1890                         __save_xml_attribute(reader, "size", &mfx->package_size, NULL);
1891                         __save_xml_attribute(reader, "install-location", &mfx->installlocation, "internal-only");
1892                         __save_xml_attribute(reader, "type", &mfx->type, "tpk");
1893                         __save_xml_attribute(reader, "root_path", &mfx->root_path, NULL);
1894                         __save_xml_attribute(reader, "csc_path", &mfx->csc_path, NULL);
1895                         __save_xml_attribute(reader, "appsetting", &mfx->appsetting, "false");
1896                         __save_xml_attribute(reader, "storeclient-id", &mfx->storeclient_id, NULL);
1897                         __save_xml_attribute(reader, "nodisplay-setting", &mfx->nodisplay_setting, "false");
1898                         __save_xml_attribute(reader, "url", &mfx->package_url, NULL);
1899                         __save_xml_attribute(reader, "api-version", &mfx->api_version, NULL);
1900                         __save_xml_attribute(reader, "support-disable", &mfx->support_disable, "false");
1901
1902                         __save_xml_installed_time(mfx);
1903                         __save_xml_root_path(mfx, uid);
1904                         /*Assign default values. If required it will be overwritten in __add_preload_info()*/
1905                         __save_xml_default_value(mfx);
1906
1907                         ret = __start_process(reader, mfx, uid);
1908                 } else {
1909                         _LOGD("No Manifest element found\n");
1910                         return -1;
1911                 }
1912         }
1913         return ret;
1914 }
1915
1916 #define LIBAPPSVC_PATH LIB_PATH "/libappsvc.so.0"
1917
1918 static int __ps_remove_appsvc_db(manifest_x *mfx, uid_t uid)
1919 {
1920         void *lib_handle = NULL;
1921         int (*appsvc_operation) (const char *, uid_t);
1922         int ret = 0;
1923         GList *tmp;
1924         application_x *application;
1925
1926         if ((lib_handle = dlopen(LIBAPPSVC_PATH, RTLD_LAZY)) == NULL) {
1927                 _LOGE("dlopen is failed LIBAPPSVC_PATH[%s]\n", LIBAPPSVC_PATH);
1928                 goto END;
1929         }
1930
1931         if ((appsvc_operation =
1932                  dlsym(lib_handle, "appsvc_unset_defapp")) == NULL || dlerror() != NULL) {
1933                 _LOGE("can not find symbol \n");
1934                 goto END;
1935         }
1936
1937         for (tmp = mfx->application; tmp; tmp = tmp->next) {
1938                 application = (application_x *)tmp->data;
1939                 if (application == NULL)
1940                         continue;
1941                 ret = appsvc_operation(application->appid, uid);
1942                 if (ret < 0)
1943                         _LOGE("can not operation  symbol \n");
1944         }
1945
1946 END:
1947         if (lib_handle)
1948                 dlclose(lib_handle);
1949
1950         return ret;
1951 }
1952
1953 static int __check_preload_updated(manifest_x * mfx, const char *manifest, uid_t uid)
1954 {
1955         if (!strstr(manifest, getUserManifestPath(uid,
1956                 strcmp(mfx->preload, "true") == 0))) {
1957                 /* if downloaded app is updated, then update tag set true*/
1958                 if (mfx->update)
1959                         free((void *)mfx->update);
1960                 mfx->update = strdup("true");
1961         }
1962
1963         return 0;
1964 }
1965
1966 API void pkgmgr_parser_free_manifest_xml(manifest_x *mfx)
1967 {
1968         pkgmgrinfo_basic_free_package((package_x *)mfx);
1969 }
1970
1971 DEPRECATED API manifest_x *pkgmgr_parser_process_manifest_xml(const char *manifest)
1972 {
1973         _LOGD("parsing start pkgmgr_parser_process_manifest_xml\n");
1974         xmlTextReaderPtr reader;
1975         manifest_x *mfx = NULL;
1976
1977         reader = xmlReaderForFile(manifest, NULL, 0);
1978         if (reader) {
1979                 mfx = malloc(sizeof(manifest_x));
1980                 if (mfx) {
1981                         memset(mfx, '\0', sizeof(manifest_x));
1982                         if (__process_manifest(reader, mfx, GLOBAL_USER) < 0) {
1983                                 _LOGD("Parsing Failed\n");
1984                                 pkgmgr_parser_free_manifest_xml(mfx);
1985                                 mfx = NULL;
1986                         } else
1987                                 _LOGD("Parsing Success\n");
1988                 } else {
1989                         _LOGD("Memory allocation error\n");
1990                 }
1991                 xmlFreeTextReader(reader);
1992         } else {
1993                 _LOGD("Unable to create xml reader\n");
1994         }
1995         return mfx;
1996 }
1997
1998
1999 DEPRECATED API manifest_x *pkgmgr_parser_usr_process_manifest_xml(const char *manifest, uid_t uid)
2000 {
2001         _LOGD("parsing start pkgmgr_parser_usr_process_manifest_xml\n");
2002         xmlTextReaderPtr reader;
2003         manifest_x *mfx = NULL;
2004
2005         reader = xmlReaderForFile(manifest, NULL, 0);
2006         if (reader) {
2007                 mfx = malloc(sizeof(manifest_x));
2008                 if (mfx) {
2009                         memset(mfx, '\0', sizeof(manifest_x));
2010                         if (__process_manifest(reader, mfx, uid) < 0) {
2011                                 _LOGD("Parsing Failed\n");
2012                                 pkgmgr_parser_free_manifest_xml(mfx);
2013                                 mfx = NULL;
2014                         } else
2015                                 _LOGD("Parsing Success\n");
2016                 } else {
2017                         _LOGD("Memory allocation error\n");
2018                 }
2019                 xmlFreeTextReader(reader);
2020         } else {
2021                 _LOGD("Unable to create xml reader\n");
2022         }
2023         return mfx;
2024 }
2025
2026 API int pkgmgr_parser_usr_update_tep(const char *pkgid, const char *tep_path, uid_t uid)
2027 {
2028         return pkgmgr_parser_update_tep_info_in_usr_db(pkgid, tep_path, uid);
2029 }
2030
2031 API int pkgmgr_parser_update_tep(const char *pkgid, const char *tep_path)
2032 {
2033         return pkgmgr_parser_update_tep_info_in_db(pkgid, tep_path);
2034 }
2035
2036 DEPRECATED API int pkgmgr_parser_parse_manifest_for_installation(const char *manifest, char *const tagv[])
2037 {
2038         retvm_if(manifest == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
2039         _LOGD("parsing manifest for installation: %s\n", manifest);
2040
2041         manifest_x *mfx = NULL;
2042         int ret = -1;
2043
2044         xmlInitParser();
2045         mfx = pkgmgr_parser_process_manifest_xml(manifest);
2046         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
2047
2048         _LOGD("Parsing Finished\n");
2049
2050         __ps_process_tag(mfx, tagv);
2051
2052         ret = pkgmgr_parser_insert_manifest_info_in_db(mfx);
2053         retvm_if(ret == PMINFO_R_ERROR, PMINFO_R_ERROR, "DB Insert failed");
2054
2055         _LOGD("DB Insert Success\n");
2056
2057         __ps_process_tag_parser(mfx, manifest, ACTION_INSTALL);
2058         ret = __ps_process_metadata_parser(mfx, ACTION_INSTALL);
2059         if (ret == -1)
2060                 _LOGD("Creating metadata parser failed\n");
2061
2062         ret = __ps_process_category_parser(mfx, ACTION_INSTALL);
2063         if (ret == -1)
2064                 _LOGD("Creating category parser failed\n");
2065
2066         pkgmgr_parser_free_manifest_xml(mfx);
2067         _LOGD("Free Done\n");
2068         xmlCleanupParser();
2069
2070         return PMINFO_R_OK;
2071 }
2072
2073 DEPRECATED API int pkgmgr_parser_parse_usr_manifest_for_installation(const char *manifest, uid_t uid, char *const tagv[])
2074 {
2075         retvm_if(manifest == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
2076         _LOGD("parsing manifest for installation: %s\n", manifest);
2077         manifest_x *mfx = NULL;
2078         int ret = -1;
2079
2080         xmlInitParser();
2081         mfx = pkgmgr_parser_usr_process_manifest_xml(manifest, uid);
2082         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
2083
2084         _LOGD("Parsing Finished\n");
2085
2086         __ps_process_tag(mfx, tagv);
2087
2088         ret = pkgmgr_parser_insert_manifest_info_in_usr_db(mfx, uid);
2089         retvm_if(ret == PMINFO_R_ERROR, PMINFO_R_ERROR, "DB Insert failed");
2090
2091         _LOGD("DB Insert Success\n");
2092
2093         __ps_process_tag_parser(mfx, manifest, ACTION_INSTALL);
2094         ret = __ps_process_metadata_parser(mfx, ACTION_INSTALL);
2095         if (ret == -1)
2096                 _LOGD("Creating metadata parser failed\n");
2097         ret = __ps_process_category_parser(mfx, ACTION_INSTALL);
2098         if (ret == -1)
2099                 _LOGD("Creating category parser failed\n");
2100
2101         pkgmgr_parser_free_manifest_xml(mfx);
2102         _LOGD("Free Done\n");
2103         xmlCleanupParser();
2104
2105         return PMINFO_R_OK;
2106 }
2107
2108 API int pkgmgr_parser_process_manifest_x_for_installation(manifest_x* mfx)
2109 {
2110         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
2111         int ret = -1;
2112
2113         ret = pkgmgr_parser_insert_manifest_info_in_db(mfx);
2114         retvm_if(ret == PMINFO_R_ERROR, PMINFO_R_ERROR, "DB Insert failed");
2115         _LOGD("DB Insert Success\n");
2116
2117         return PMINFO_R_OK;
2118 }
2119
2120 API int pkgmgr_parser_process_usr_manifest_x_for_installation(manifest_x* mfx, uid_t uid)
2121 {
2122         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
2123         int ret = -1;
2124
2125         ret = pkgmgr_parser_insert_manifest_info_in_usr_db(mfx, uid);
2126         retvm_if(ret == PMINFO_R_ERROR, PMINFO_R_ERROR, "DB Insert failed");
2127         _LOGD("DB Insert Success\n");
2128
2129         return PMINFO_R_OK;
2130 }
2131
2132 DEPRECATED API int pkgmgr_parser_parse_manifest_for_upgrade(const char *manifest, char *const tagv[])
2133 {
2134         retvm_if(manifest == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
2135         _LOGD("pkgmgr_parser_parse_manifest_for_upgrade  parsing manifest for upgradation: %s\n", manifest);
2136         manifest_x *mfx = NULL;
2137         int ret = -1;
2138         bool preload = false;
2139         bool system = false;
2140         char *csc_path = NULL;
2141         pkgmgrinfo_pkginfo_h handle = NULL;
2142
2143         xmlInitParser();
2144         mfx = pkgmgr_parser_process_manifest_xml(manifest);
2145         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
2146
2147         _LOGD("Parsing Finished\n");
2148         __check_preload_updated(mfx, manifest, GLOBAL_USER);
2149
2150         ret = pkgmgrinfo_pkginfo_get_pkginfo(mfx->package, &handle);
2151         if (ret != PMINFO_R_OK)
2152                 _LOGD("pkgmgrinfo_pkginfo_get_pkginfo failed\n");
2153         ret = pkgmgrinfo_pkginfo_is_preload(handle, &preload);
2154         if (ret != PMINFO_R_OK)
2155                 _LOGD("pkgmgrinfo_pkginfo_is_preload failed\n");
2156
2157         if (preload) {
2158                 free((void *)mfx->preload);
2159                 mfx->preload = strdup("true");
2160         }
2161
2162         ret = pkgmgrinfo_pkginfo_is_system(handle, &system);
2163         if (ret != PMINFO_R_OK)
2164                 _LOGD("pkgmgrinfo_pkginfo_is_system failed\n");
2165         if (system) {
2166                 free((void *)mfx->system);
2167                 mfx->system = strdup("true");
2168         }
2169
2170         ret = pkgmgrinfo_pkginfo_get_csc_path(handle, &csc_path);
2171         if (ret != PMINFO_R_OK)
2172                 _LOGD("pkgmgrinfo_pkginfo_get_csc_path failed\n");
2173
2174         if (csc_path != NULL) {
2175                 if (mfx->csc_path)
2176                         free((void *)mfx->csc_path);
2177                 mfx->csc_path = strdup(csc_path);
2178         }
2179
2180         /*Delete from cert table*/
2181         ret = pkgmgrinfo_delete_certinfo(mfx->package);
2182         if (ret) {
2183                 _LOGD("Cert Info  DB Delete Failed\n");
2184                 return -1;
2185         }
2186
2187         ret = pkgmgr_parser_update_manifest_info_in_db(mfx);
2188         retvm_if(ret == PMINFO_R_ERROR, PMINFO_R_ERROR, "DB Insert failed");
2189
2190         _LOGD("DB Update Success\n");
2191
2192         __ps_process_tag_parser(mfx, manifest, ACTION_UPGRADE);
2193         ret = __ps_process_metadata_parser(mfx, ACTION_UPGRADE);
2194         if (ret == -1)
2195                 _LOGD("Upgrade metadata parser failed\n");
2196         ret = __ps_process_category_parser(mfx, ACTION_UPGRADE);
2197         if (ret == -1)
2198                 _LOGD("Creating category parser failed\n");
2199         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
2200         pkgmgr_parser_free_manifest_xml(mfx);
2201         _LOGD("Free Done\n");
2202         xmlCleanupParser();
2203
2204         return PMINFO_R_OK;
2205 }
2206
2207 DEPRECATED API int pkgmgr_parser_parse_usr_manifest_for_upgrade(const char *manifest, uid_t uid, char *const tagv[])
2208 {
2209         retvm_if(manifest == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
2210         _LOGD(" pkgmgr_parser_parse_usr_manifest_for_upgrade parsing manifest for upgradation: %s\n", manifest);
2211         manifest_x *mfx = NULL;
2212         int ret = -1;
2213         bool preload = false;
2214         bool system = false;
2215         char *csc_path = NULL;
2216         pkgmgrinfo_pkginfo_h handle = NULL;
2217
2218         xmlInitParser();
2219         mfx = pkgmgr_parser_usr_process_manifest_xml(manifest, uid);
2220         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
2221
2222         _LOGD("Parsing Finished\n");
2223         __check_preload_updated(mfx, manifest, uid);
2224
2225         ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(mfx->package, uid, &handle);
2226         if (ret != PMINFO_R_OK)
2227                 _LOGD("pkgmgrinfo_pkginfo_get_pkginfo failed\n");
2228         ret = pkgmgrinfo_pkginfo_is_preload(handle, &preload);
2229         if (ret != PMINFO_R_OK)
2230                 _LOGD("pkgmgrinfo_pkginfo_is_preload failed\n");
2231
2232         if (preload) {
2233                 free((void *)mfx->preload);
2234                 mfx->preload = strdup("true");
2235         }
2236
2237         ret = pkgmgrinfo_pkginfo_is_system(handle, &system);
2238         if (ret != PMINFO_R_OK)
2239                 _LOGD("pkgmgrinfo_pkginfo_is_system failed\n");
2240
2241         if (system) {
2242                 free((void *)mfx->system);
2243                 mfx->system = strdup("true");
2244         }
2245
2246         ret = pkgmgrinfo_pkginfo_get_csc_path(handle, &csc_path);
2247         if (ret != PMINFO_R_OK)
2248                 _LOGD("pkgmgrinfo_pkginfo_get_csc_path failed\n");
2249         if (csc_path != NULL) {
2250                 if (mfx->csc_path)
2251                         free((void *)mfx->csc_path);
2252                 mfx->csc_path = strdup(csc_path);
2253         }
2254
2255         /*Delete from cert table*/
2256         ret = pkgmgrinfo_delete_certinfo(mfx->package);
2257         if (ret) {
2258                 _LOGD("Cert Info  DB Delete Failed\n");
2259                 return -1;
2260         }
2261
2262         ret = pkgmgr_parser_update_manifest_info_in_usr_db(mfx, uid);
2263         retvm_if(ret == PMINFO_R_ERROR, PMINFO_R_ERROR, "DB Insert failed");
2264         _LOGD("DB Update Success\n");
2265
2266         __ps_process_tag_parser(mfx, manifest, ACTION_UPGRADE);
2267         ret = __ps_process_metadata_parser(mfx, ACTION_UPGRADE);
2268         if (ret == -1)
2269                 _LOGD("Upgrade metadata parser failed\n");
2270         ret = __ps_process_category_parser(mfx, ACTION_UPGRADE);
2271         if (ret == -1)
2272                 _LOGD("Creating category parser failed\n");
2273         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
2274         pkgmgr_parser_free_manifest_xml(mfx);
2275         _LOGD("Free Done\n");
2276         xmlCleanupParser();
2277
2278         return PMINFO_R_OK;
2279 }
2280
2281 API int pkgmgr_parser_process_manifest_x_for_upgrade(manifest_x* mfx)
2282 {
2283         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
2284         int ret = -1;
2285
2286         ret = pkgmgr_parser_update_manifest_info_in_db(mfx);
2287         retvm_if(ret == PMINFO_R_ERROR, PMINFO_R_ERROR, "DB Insert failed");
2288         _LOGD("DB Update Success\n");
2289
2290         return PMINFO_R_OK;
2291 }
2292
2293 API int pkgmgr_parser_process_usr_manifest_x_for_upgrade(manifest_x* mfx, uid_t uid)
2294 {
2295         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
2296         int ret = -1;
2297
2298         ret = pkgmgr_parser_update_manifest_info_in_usr_db(mfx, uid);
2299         retvm_if(ret == PMINFO_R_ERROR, PMINFO_R_ERROR, "DB Insert failed");
2300         _LOGD("DB Update Success\n");
2301
2302         return PMINFO_R_OK;
2303 }
2304
2305 API int pkgmgr_parser_parse_manifest_for_uninstallation(const char *manifest, char *const tagv[])
2306 {
2307         retvm_if(manifest == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
2308         _LOGD("parsing manifest for uninstallation: %s\n", manifest);
2309
2310         manifest_x *mfx = NULL;
2311         int ret = -1;
2312         xmlInitParser();
2313         mfx = pkgmgr_parser_process_manifest_xml(manifest);
2314         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
2315
2316         _LOGD("Parsing Finished\n");
2317
2318         __ps_process_tag_parser(mfx, manifest, ACTION_UNINSTALL);
2319
2320         ret = __ps_process_metadata_parser(mfx, ACTION_UNINSTALL);
2321         if (ret == -1)
2322                 _LOGD("Removing metadata parser failed\n");
2323
2324         ret = __ps_process_category_parser(mfx, ACTION_UNINSTALL);
2325         if (ret == -1)
2326                 _LOGD("Creating category parser failed\n");
2327
2328         /*Delete from cert table*/
2329         ret = pkgmgrinfo_delete_certinfo(mfx->package);
2330         if (ret) {
2331                 _LOGD("Cert Info  DB Delete Failed\n");
2332                 return -1;
2333         }
2334
2335         ret = pkgmgr_parser_delete_manifest_info_from_db(mfx);
2336         if (ret == -1)
2337                 _LOGD("DB Delete failed\n");
2338         else
2339                 _LOGD("DB Delete Success\n");
2340
2341         pkgmgr_parser_free_manifest_xml(mfx);
2342         _LOGD("Free Done\n");
2343         xmlCleanupParser();
2344
2345         return PMINFO_R_OK;
2346 }
2347
2348
2349 API int pkgmgr_parser_parse_usr_manifest_for_uninstallation(const char *manifest, uid_t uid, char *const tagv[])
2350 {
2351         retvm_if(manifest == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
2352         _LOGD("parsing manifest for uninstallation: %s\n", manifest);
2353
2354         manifest_x *mfx = NULL;
2355         int ret = -1;
2356         xmlInitParser();
2357         mfx = pkgmgr_parser_usr_process_manifest_xml(manifest, uid);
2358         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
2359
2360         _LOGD("Parsing Finished\n");
2361
2362         __ps_process_tag_parser(mfx, manifest, ACTION_UNINSTALL);
2363
2364         ret = __ps_process_metadata_parser(mfx, ACTION_UNINSTALL);
2365         if (ret == -1)
2366                 _LOGD("Removing metadata parser failed\n");
2367
2368         ret = __ps_process_category_parser(mfx, ACTION_UNINSTALL);
2369         if (ret == -1)
2370                 _LOGD("Creating category parser failed\n");
2371
2372         /*Delete from cert table*/
2373         ret = pkgmgrinfo_delete_certinfo(mfx->package);
2374         if (ret) {
2375                 _LOGD("Cert Info  DB Delete Failed\n");
2376                 return -1;
2377         }
2378
2379         ret = pkgmgr_parser_delete_manifest_info_from_usr_db(mfx, uid);
2380         if (ret == -1)
2381                 _LOGD("DB Delete failed\n");
2382         else
2383                 _LOGD("DB Delete Success\n");
2384
2385         ret = __ps_remove_appsvc_db(mfx, uid);
2386         if (ret == -1)
2387                 _LOGD("Removing appsvc_db failed\n");
2388         else
2389                 _LOGD("Removing appsvc_db Success\n");
2390
2391         pkgmgr_parser_free_manifest_xml(mfx);
2392         _LOGD("Free Done\n");
2393         xmlCleanupParser();
2394
2395         return PMINFO_R_OK;
2396 }
2397
2398 API int pkgmgr_parser_process_manifest_x_for_uninstallation(manifest_x* mfx)
2399 {
2400         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
2401         int ret = -1;
2402
2403         ret = pkgmgr_parser_delete_manifest_info_from_db(mfx);
2404         if (ret == -1)
2405                 _LOGD("DB Delete failed\n");
2406         else
2407                 _LOGD("DB Delete Success\n");
2408
2409         return PMINFO_R_OK;
2410 }
2411
2412 API int pkgmgr_parser_process_usr_manifest_x_for_uninstallation(manifest_x* mfx, uid_t uid)
2413 {
2414         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
2415         int ret = -1;
2416
2417         ret = pkgmgr_parser_delete_manifest_info_from_usr_db(mfx, uid);
2418         if (ret == -1)
2419                 _LOGD("DB Delete failed\n");
2420         else
2421                 _LOGD("DB Delete Success\n");
2422
2423         ret = __ps_remove_appsvc_db(mfx, uid);
2424         if (ret == -1)
2425                 _LOGD("Removing appsvc_db failed\n");
2426         else
2427                 _LOGD("Removing appsvc_db Success\n");
2428
2429         return PMINFO_R_OK;
2430 }
2431
2432 #define SCHEMA_FILE SYSCONFDIR "/package-manager/preload/manifest.xsd"
2433 API int pkgmgr_parser_check_manifest_validation(const char *manifest)
2434 {
2435         if (manifest == NULL) {
2436                 _LOGE("manifest file is NULL\n");
2437                 return PMINFO_R_EINVAL;
2438         }
2439         int ret = -1;
2440         xmlSchemaParserCtxtPtr ctx;
2441         xmlSchemaValidCtxtPtr vctx;
2442         xmlSchemaPtr xschema;
2443         ctx = xmlSchemaNewParserCtxt(SCHEMA_FILE);
2444         if (ctx == NULL) {
2445                 _LOGE("xmlSchemaNewParserCtxt() Failed\n");
2446                 return PMINFO_R_ERROR;
2447         }
2448         xschema = xmlSchemaParse(ctx);
2449         if (xschema == NULL) {
2450                 _LOGE("xmlSchemaParse() Failed\n");
2451                 return PMINFO_R_ERROR;
2452         }
2453         vctx = xmlSchemaNewValidCtxt(xschema);
2454         if (vctx == NULL) {
2455                 _LOGE("xmlSchemaNewValidCtxt() Failed\n");
2456                 return PMINFO_R_ERROR;
2457         }
2458         xmlSchemaSetValidErrors(vctx, (xmlSchemaValidityErrorFunc) fprintf, (xmlSchemaValidityWarningFunc) fprintf, stderr);
2459         ret = xmlSchemaValidateFile(vctx, manifest, 0);
2460         if (ret == -1) {
2461                 _LOGE("xmlSchemaValidateFile() failed\n");
2462                 return PMINFO_R_ERROR;
2463         } else if (ret == 0) {
2464                 _LOGD("Manifest is Valid\n");
2465                 return PMINFO_R_OK;
2466         } else {
2467                 _LOGE("Manifest Validation Failed with error code %d\n", ret);
2468                 return PMINFO_R_ERROR;
2469         }
2470         return PMINFO_R_OK;
2471 }
2472