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