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