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