fix TC-1516
[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_parser.h"
39 #include "pkgmgr_parser_internal.h"
40 #include "pkgmgr_parser_db.h"
41 #include "pkgmgr-info.h"
42 #include "pkgmgr_parser_signature.h"
43 #include "pkgmgr-info-debug.h"
44
45 #ifdef LOG_TAG
46 #undef LOG_TAG
47 #endif
48 #define LOG_TAG "PKGMGR_PARSER"
49
50 #define ASCII(s) (const char *)s
51 #define XMLCHAR(s) (const xmlChar *)s
52
53 //#define METADATA_PARSER_LIST SYSCONFDIR "/package-manager/parserlib/metadata/metadata_parser_list.txt"
54 #define METADATA_PARSER_LIST SYSCONFDIR "/package-manager/parserlib/metadata/mdparser_list.txt"
55 #define METADATA_PARSER_NAME    "metadataparser:"
56
57 #define CATEGORY_PARSER_LIST SYSCONFDIR "/package-manager/parserlib/category/category_parser_list.txt"
58 #define CATEGORY_PARSER_NAME    "categoryparser:"
59
60 #define TAG_PARSER_LIST SYSCONFDIR "/package-manager/parserlib/tag_parser_list.txt"
61 #define TAG_PARSER_NAME "parserlib:"
62
63 #define PKG_TAG_LEN_MAX 128
64 #define OWNER_ROOT 0
65 #define BUFSIZE 4096
66 #define GLOBAL_USER 0
67
68 /* operation_type */
69 typedef enum {
70         ACTION_INSTALL = 0,
71         ACTION_UPGRADE,
72         ACTION_UNINSTALL,
73         ACTION_FOTA,
74         ACTION_MAX
75 } ACTION_TYPE;
76
77 /* plugin process_type */
78 typedef enum {
79         PLUGIN_PRE_PROCESS = 0,
80         PLUGIN_POST_PROCESS
81 } PLUGIN_PROCESS_TYPE;
82
83 typedef struct {
84         const char *key;
85         const char *value;
86 } __metadata_t;
87
88 typedef struct {
89         const char *name;
90 } __category_t;
91
92 const char *package;
93
94 static int __ps_process_label(xmlTextReaderPtr reader, label_x *label);
95 static int __ps_process_privilege(xmlTextReaderPtr reader, privilege_x *privilege);
96 static int __ps_process_privileges(xmlTextReaderPtr reader, privileges_x *privileges);
97 static int __ps_process_deviceprofile(xmlTextReaderPtr reader, deviceprofile_x *deviceprofile);
98 static int __ps_process_allowed(xmlTextReaderPtr reader, allowed_x *allowed);
99 static int __ps_process_operation(xmlTextReaderPtr reader, operation_x *operation);
100 static int __ps_process_uri(xmlTextReaderPtr reader, uri_x *uri);
101 static int __ps_process_mime(xmlTextReaderPtr reader, mime_x *mime);
102 static int __ps_process_subapp(xmlTextReaderPtr reader, subapp_x *subapp);
103 static int __ps_process_condition(xmlTextReaderPtr reader, condition_x *condition);
104 static int __ps_process_notification(xmlTextReaderPtr reader, notification_x *notifiation);
105 static int __ps_process_category(xmlTextReaderPtr reader, category_x *category);
106 static int __ps_process_metadata(xmlTextReaderPtr reader, metadata_x *metadata);
107 static int __ps_process_permission(xmlTextReaderPtr reader, permission_x *permission);
108 static int __ps_process_compatibility(xmlTextReaderPtr reader, compatibility_x *compatibility);
109 static int __ps_process_resolution(xmlTextReaderPtr reader, resolution_x *resolution);
110 static int __ps_process_request(xmlTextReaderPtr reader, request_x *request);
111 static int __ps_process_define(xmlTextReaderPtr reader, define_x *define);
112 static int __ps_process_appsvc(xmlTextReaderPtr reader, appsvc_x *appsvc);
113 static int __ps_process_launchconditions(xmlTextReaderPtr reader, launchconditions_x *launchconditions);
114 static int __ps_process_datashare(xmlTextReaderPtr reader, datashare_x *datashare);
115 static int __ps_process_icon(xmlTextReaderPtr reader, icon_x *icon, uid_t uid);
116 static int __ps_process_author(xmlTextReaderPtr reader, author_x *author);
117 static int __ps_process_description(xmlTextReaderPtr reader, description_x *description);
118 static int __ps_process_capability(xmlTextReaderPtr reader, capability_x *capability);
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 void __ps_free_label(label_x *label);
129 static void __ps_free_privilege(privilege_x *privilege);
130 static void __ps_free_privileges(privileges_x *privileges);
131 static void __ps_free_deviceprofile(deviceprofile_x * deviceprofile);
132 static void __ps_free_allowed(allowed_x *allowed);
133 static void __ps_free_operation(operation_x *operation);
134 static void __ps_free_uri(uri_x *uri);
135 static void __ps_free_mime(mime_x *mime);
136 static void __ps_free_subapp(subapp_x *subapp);
137 static void __ps_free_condition(condition_x *condition);
138 static void __ps_free_notification(notification_x *notifiation);
139 static void __ps_free_category(category_x *category);
140 static void __ps_free_metadata(metadata_x *metadata);
141 static void __ps_free_permission(permission_x *permission);
142 static void __ps_free_compatibility(compatibility_x *compatibility);
143 static void __ps_free_resolution(resolution_x *resolution);
144 static void __ps_free_request(request_x *request);
145 static void __ps_free_define(define_x *define);
146 static void __ps_free_appsvc(appsvc_x *appsvc);
147 static void __ps_free_launchconditions(launchconditions_x *launchconditions);
148 static void __ps_free_datashare(datashare_x *datashare);
149 static void __ps_free_icon(icon_x *icon);
150 static void __ps_free_author(author_x *author);
151 static void __ps_free_description(description_x *description);
152 static void __ps_free_capability(capability_x *capability);
153 static void __ps_free_license(license_x *license);
154 static void __ps_free_appcontrol(appcontrol_x *appcontrol);
155 static void __ps_free_datacontrol(datacontrol_x *datacontrol);
156 static void __ps_free_uiapplication(uiapplication_x *uiapplication);
157 static void __ps_free_serviceapplication(serviceapplication_x *serviceapplication);
158 static void __ps_free_font(font_x *font);
159 static void __ps_free_theme(theme_x *theme);
160 static void __ps_free_daemon(daemon_x *daemon);
161 static void __ps_free_ime(ime_x *ime);
162 static char *__pkgid_to_manifest(const char *pkgid, uid_t uid);
163 static int __next_child_element(xmlTextReaderPtr reader, int depth);
164 static int __start_process(xmlTextReaderPtr reader, manifest_x * mfx, uid_t uid);
165 static int __process_manifest(xmlTextReaderPtr reader, manifest_x * mfx, uid_t uid);
166 static void __str_trim(char *input);
167 static char *__get_parser_plugin(const char *type);
168 static int __ps_run_parser(xmlDocPtr docPtr, const char *tag, ACTION_TYPE action, const char *pkgid);
169 static int __run_parser_prestep(xmlTextReaderPtr reader, ACTION_TYPE action, const char *pkgid);
170 static void __processNode(xmlTextReaderPtr reader, ACTION_TYPE action, char *const tagv[], const char *pkgid);
171 static void __streamFile(const char *filename, ACTION_TYPE action, char *const tagv[], const char *pkgid);
172 static int __validate_appid(const char *pkgid, const char *appid, char **newappid);
173 API int __is_admin();
174
175 void *__open_lib_handle(char *tag)
176 {
177         char *lib_path = NULL;
178         void *lib_handle = NULL;
179
180         lib_path = __get_parser_plugin(tag);
181         retvm_if(!lib_path, NULL, "lib_path get fail");
182
183         lib_handle = dlopen(lib_path, RTLD_LAZY);
184         retvm_if(lib_handle == NULL, NULL, "dlopen is failed lib_path[%s]", lib_path);
185
186         return lib_handle;
187 }
188
189 void __close_lib_handle(void *lib_handle)
190 {
191         dlclose(lib_handle);
192 }
193
194 static void __str_trim(char *input)
195 {
196         char *trim_str = input;
197
198         if (input == NULL)
199                 return;
200
201         while (*input != 0) {
202                 if (!isspace(*input)) {
203                         *trim_str = *input;
204                         trim_str++;
205                 }
206                 input++;
207         }
208
209         *trim_str = 0;
210         return;
211 }
212
213 API int __is_admin()
214 {
215         uid_t uid = getuid();
216         if ((uid_t) 0 == uid )
217                 return 1;
218         else
219                 return 0;
220 }
221
222
223
224 static int __validate_appid(const char *pkgid, const char *appid, char **newappid)
225 {
226         if (!pkgid || !appid || !newappid) {
227                 _LOGD("Arg supplied is NULL\n");
228                 return -1;
229         }
230         int pkglen = strlen(pkgid);
231         int applen = strlen(appid);
232         char *ptr = NULL;
233         char *newapp = NULL;
234         int len = 0;
235         if (strncmp(appid, ".", 1) == 0) {
236                 len = pkglen + applen + 1;
237                 newapp = calloc(1,len);
238                 if (newapp == NULL) {
239                         _LOGD("Malloc failed\n");
240                         return -1;
241                 }
242                 strncpy(newapp, pkgid, pkglen);
243                 strncat(newapp, appid, applen);
244                 _LOGD("new appid is %s\n", newapp);
245                 *newappid = newapp;
246                 return 0;
247         }
248         if (applen < pkglen) {
249                 _LOGD("app id is not proper\n");
250                 *newappid = NULL;
251 #ifdef _VALIDATE_APPID_
252                 return -1;
253 #else
254                 return 0;
255 #endif
256         }
257         if (!strcmp(appid, pkgid)) {
258                 _LOGD("appid is proper\n");
259                 *newappid = NULL;
260                 return 0;
261         }
262         else if (strncmp(appid, pkgid, pkglen) == 0) {
263                 ptr = strstr(appid, pkgid);
264                 ptr = ptr + pkglen;
265                 if (strncmp(ptr, ".", 1) == 0) {
266                         _LOGD("appid is proper\n");
267                         *newappid = NULL;
268                         return 0;
269                 }
270                 else {
271                         _LOGD("appid is not proper\n");
272                         *newappid = NULL;
273 #ifdef _VALIDATE_APPID_
274                         return -1;
275 #else
276                         return 0;
277 #endif
278                 }
279         } else {
280                 _LOGD("appid is not proper\n");
281                 *newappid = NULL;
282 #ifdef _VALIDATE_APPID_
283                 return -1;
284 #else
285                 return 0;
286 #endif
287         }
288         return 0;
289 }
290
291 static char * __get_tag_by_key(char *md_key)
292 {
293         char *md_tag = NULL;
294
295         if (md_key == NULL) {
296                 _LOGD("md_key is NULL\n");
297                 return NULL;
298         }
299
300         md_tag = strrchr(md_key, 47) + 1;
301
302
303         return strdup(md_tag);
304 }
305
306 static char *__get_metadata_parser_plugin(const char *type)
307 {
308         FILE *fp = NULL;
309         char buffer[1024] = { 0 };
310         char temp_path[1024] = { 0 };
311         char *path = NULL;
312
313         if (type == NULL) {
314                 _LOGE("invalid argument\n");
315                 return NULL;
316         }
317
318         fp = fopen(PKG_PARSER_CONF_PATH, "r");
319         if (fp == NULL) {
320                 _LOGE("no matching metadata parser\n");
321                 return NULL;
322         }
323
324         while (fgets(buffer, sizeof(buffer), fp) != NULL) {
325                 if (buffer[0] == '#')
326                         continue;
327
328                 __str_trim(buffer);
329
330                 if ((path = strstr(buffer, METADATA_PARSER_NAME)) != NULL) {
331                         path = path + strlen(METADATA_PARSER_NAME);
332
333                         break;
334                 }
335
336                 memset(buffer, 0x00, 1024);
337         }
338
339         if (fp != NULL)
340                 fclose(fp);
341
342         if (path == NULL) {
343                 _LOGE("no matching [%s] [%s]\n", METADATA_PARSER_NAME,type);
344                 return NULL;
345         }
346
347         snprintf(temp_path, sizeof(temp_path) - 1, "%slib%s.so", path, type);
348
349         return strdup(temp_path);
350 }
351
352 static char *__get_category_parser_plugin(const char *type)
353 {
354         FILE *fp = NULL;
355         char buffer[1024] = { 0 };
356         char temp_path[1024] = { 0 };
357         char *path = NULL;
358
359         if (type == NULL) {
360                 _LOGE("invalid argument\n");
361                 return NULL;
362         }
363
364         fp = fopen(PKG_PARSER_CONF_PATH, "r");
365         if (fp == NULL) {
366                 _LOGE("no matching metadata parser\n");
367                 return NULL;
368         }
369
370         while (fgets(buffer, sizeof(buffer), fp) != NULL) {
371                 if (buffer[0] == '#')
372                         continue;
373
374                 __str_trim(buffer);
375
376                 if ((path = strstr(buffer, CATEGORY_PARSER_NAME)) != NULL) {
377                         path = path + strlen(CATEGORY_PARSER_NAME);
378
379                         break;
380                 }
381
382                 memset(buffer, 0x00, 1024);
383         }
384
385         if (fp != NULL)
386                 fclose(fp);
387
388         if (path == NULL) {
389                 _LOGE("no matching [%s] [%s]\n", CATEGORY_PARSER_NAME,type);
390                 return NULL;
391         }
392
393         snprintf(temp_path, sizeof(temp_path) - 1, "%slib%s.so", path, type);
394
395         return strdup(temp_path);
396 }
397
398 static char *__get_parser_plugin(const char *type)
399 {
400         FILE *fp = NULL;
401         char buffer[1024] = { 0 };
402         char temp_path[1024] = { 0 };
403         char *path = NULL;
404
405         if (type == NULL) {
406                 _LOGE("invalid argument\n");
407                 return NULL;
408         }
409
410         fp = fopen(PKG_PARSER_CONF_PATH, "r");
411         if (fp == NULL) {
412                 _LOGE("no matching backendlib\n");
413                 return NULL;
414         }
415
416         while (fgets(buffer, sizeof(buffer), fp) != NULL) {
417                 if (buffer[0] == '#')
418                         continue;
419
420                 __str_trim(buffer);
421
422                 if ((path = strstr(buffer, PKG_PARSERLIB)) != NULL) {
423                         path = path + strlen(PKG_PARSERLIB);
424                         break;
425                 }
426
427                 memset(buffer, 0x00, 1024);
428         }
429
430         if (fp != NULL)
431                 fclose(fp);
432
433         if (path == NULL) {
434                 _LOGE("no matching backendlib\n");
435                 return NULL;
436         }
437
438         snprintf(temp_path, sizeof(temp_path) - 1, "%slib%s.so", path, type);
439
440         return strdup(temp_path);
441 }
442
443 static int __ps_run_tag_parser(void *lib_handle, xmlDocPtr docPtr, const char *tag,
444                            ACTION_TYPE action, const char *pkgid)
445 {
446         int (*plugin_install) (xmlDocPtr, const char *);
447         int ret = -1;
448         char *ac = NULL;
449
450         switch (action) {
451         case ACTION_INSTALL:
452                 ac = "PKGMGR_PARSER_PLUGIN_INSTALL";
453                 break;
454         case ACTION_UPGRADE:
455                 ac = "PKGMGR_PARSER_PLUGIN_UPGRADE";
456                 break;
457         case ACTION_UNINSTALL:
458                 ac = "PKGMGR_PARSER_PLUGIN_UNINSTALL";
459                 break;
460         default:
461                 goto END;
462         }
463
464         if ((plugin_install =
465                 dlsym(lib_handle, ac)) == NULL || dlerror() != NULL) {
466                 _LOGE("can not find symbol[%s] \n", ac);
467                 goto END;
468         }
469
470         ret = plugin_install(docPtr, pkgid);
471         _LOGD("tag parser[%s, %s] ACTION_TYPE[%d] result[%d]\n", pkgid, tag, action, ret);
472
473 END:
474         return ret;
475 }
476
477 static int __ps_run_metadata_parser(GList *md_list, const char *tag,
478                                 ACTION_TYPE action, const char *pkgid, const char *appid)
479 {
480         char *lib_path = NULL;
481         void *lib_handle = NULL;
482         int (*metadata_parser_plugin) (const char *, const char *, GList *);
483         int ret = -1;
484         char *ac = NULL;
485
486         switch (action) {
487         case ACTION_INSTALL:
488                 ac = "PKGMGR_MDPARSER_PLUGIN_INSTALL";
489                 break;
490         case ACTION_UPGRADE:
491                 ac = "PKGMGR_MDPARSER_PLUGIN_UPGRADE";
492                 break;
493         case ACTION_UNINSTALL:
494                 ac = "PKGMGR_MDPARSER_PLUGIN_UNINSTALL";
495                 break;
496         default:
497                 goto END;
498         }
499
500         lib_path = __get_metadata_parser_plugin(tag);
501         if (!lib_path) {
502                 _LOGE("get %s parser fail\n", tag);
503                 goto END;
504         }
505
506         if ((lib_handle = dlopen(lib_path, RTLD_LAZY)) == NULL) {
507                 _LOGE("dlopen is failed lib_path[%s]\n", lib_path);
508                 goto END;
509         }
510
511         if ((metadata_parser_plugin =
512                 dlsym(lib_handle, ac)) == NULL || dlerror() != NULL) {
513                 _LOGE("can not find symbol[%s] \n",ac);
514                 goto END;
515         }
516
517         ret = metadata_parser_plugin(pkgid, appid, md_list);
518         if (ret < 0)
519                 _LOGD("[appid = %s, libpath = %s plugin fail\n", appid, lib_path);
520         else
521                 _LOGD("[appid = %s, libpath = %s plugin success\n", appid, lib_path);
522
523 END:
524         if (lib_path)
525                 free(lib_path);
526         if (lib_handle)
527                 dlclose(lib_handle);
528         return ret;
529 }
530
531 static int __ps_run_category_parser(GList *category_list, const char *tag,
532                                 ACTION_TYPE action, const char *pkgid, const char *appid)
533 {
534         char *lib_path = NULL;
535         void *lib_handle = NULL;
536         int (*category_parser_plugin) (const char *, const char *, GList *);
537         int ret = -1;
538         char *ac = NULL;
539
540         switch (action) {
541         case ACTION_INSTALL:
542                 ac = "PKGMGR_CATEGORY_PARSER_PLUGIN_INSTALL";
543                 break;
544         case ACTION_UPGRADE:
545                 ac = "PKGMGR_CATEGORY_PARSER_PLUGIN_UPGRADE";
546                 break;
547         case ACTION_UNINSTALL:
548                 ac = "PKGMGR_CATEGORY_PARSER_PLUGIN_UNINSTALL";
549                 break;
550         default:
551                 goto END;
552         }
553
554         lib_path = __get_category_parser_plugin(tag);
555         if (!lib_path) {
556                 _LOGE("get %s parser fail\n", tag);
557                 goto END;
558         }
559
560         if ((lib_handle = dlopen(lib_path, RTLD_LAZY)) == NULL) {
561                 _LOGE("dlopen is failed lib_path[%s]\n", lib_path);
562                 goto END;
563         }
564
565         if ((category_parser_plugin =
566                 dlsym(lib_handle, ac)) == NULL || dlerror() != NULL) {
567                 _LOGE("can not find symbol[%s] \n",ac);
568                 goto END;
569         }
570
571         ret = category_parser_plugin(pkgid, appid, category_list);
572         if (ret < 0)
573                 _LOGD("[appid = %s, libpath = %s plugin fail\n", appid, lib_path);
574         else
575                 _LOGD("[appid = %s, libpath = %s plugin success\n", appid, lib_path);
576
577 END:
578         if (lib_path)
579                 free(lib_path);
580         if (lib_handle)
581                 dlclose(lib_handle);
582         return ret;
583 }
584
585 static int __ps_run_parser(xmlDocPtr docPtr, const char *tag,
586                            ACTION_TYPE action, const char *pkgid)
587 {
588         char *lib_path = NULL;
589         void *lib_handle = NULL;
590         int (*plugin_install) (xmlDocPtr, const char *);
591         int ret = -1;
592         char *ac = NULL;
593
594         switch (action) {
595         case ACTION_INSTALL:
596                 ac = "PKGMGR_PARSER_PLUGIN_INSTALL";
597                 break;
598         case ACTION_UPGRADE:
599                 ac = "PKGMGR_PARSER_PLUGIN_UPGRADE";
600                 break;
601         case ACTION_UNINSTALL:
602                 ac = "PKGMGR_PARSER_PLUGIN_UNINSTALL";
603                 break;
604         default:
605                 goto END;
606         }
607
608         lib_path = __get_parser_plugin(tag);
609         if (!lib_path) {
610                 goto END;
611         }
612
613         if ((lib_handle = dlopen(lib_path, RTLD_LAZY)) == NULL) {
614                 _LOGE("dlopen is failed lib_path[%s]\n", lib_path);
615                 goto END;
616         }
617         if ((plugin_install =
618                 dlsym(lib_handle, ac)) == NULL || dlerror() != NULL) {
619                 _LOGE("can not find symbol[%s] \n", ac);
620                 goto END;
621         }
622
623         ret = plugin_install(docPtr, pkgid);
624         if (ret < 0)
625                 _LOGD("[pkgid = %s, libpath = %s plugin fail\n", pkgid, lib_path);
626         else
627                 _LOGD("[pkgid = %s, libpath = %s plugin success\n", pkgid, lib_path);
628
629 END:
630         if (lib_path)
631                 free(lib_path);
632         if (lib_handle)
633                 dlclose(lib_handle);
634         return ret;
635 }
636
637 static char *__pkgid_to_manifest(const char *pkgid, uid_t uid)
638 {
639         char *manifest;
640         int size;
641
642         if (pkgid == NULL) {
643                 _LOGE("pkgid is NULL");
644                 return NULL;
645         }
646
647         size = strlen(getUserManifestPath(uid)) + strlen(pkgid) + 10;
648         manifest = malloc(size);
649         if (manifest == NULL) {
650                 _LOGE("No memory");
651                 return NULL;
652         }
653         memset(manifest, '\0', size);
654         snprintf(manifest, size, "%s%s.xml", getUserManifestPath(uid), pkgid);
655
656         if (access(manifest, F_OK)) {
657                 snprintf(manifest, size, "%s%s.xml", getUserManifestPath(uid), pkgid);
658         }
659
660         return manifest;
661 }
662
663 static void __metadata_parser_clear_dir_list(GList* dir_list)
664 {
665         GList *list = NULL;
666         __metadata_t* detail = NULL;
667
668         if (dir_list) {
669                 list = g_list_first(dir_list);
670                 while (list) {
671                         detail = (__metadata_t *)list->data;
672                         if (detail) {
673                                 if (detail->key)
674                                         free(detail->key);
675                                 if (detail->value)
676                                         free(detail->value);
677                                 free(detail);
678                         }
679                         list = g_list_next(list);
680                 }
681                 g_list_free(dir_list);
682         }
683 }
684
685 static void __category_parser_clear_dir_list(GList* dir_list)
686 {
687         GList *list = NULL;
688         __category_t* detail = NULL;
689
690         if (dir_list) {
691                 list = g_list_first(dir_list);
692                 while (list) {
693                         detail = (__category_t *)list->data;
694                         if (detail) {
695                                 if (detail->name)
696                                         free(detail->name);
697
698                                 free(detail);
699                         }
700                         list = g_list_next(list);
701                 }
702                 g_list_free(dir_list);
703         }
704 }
705
706 static int __run_tag_parser_prestep(void *lib_handle, xmlTextReaderPtr reader, ACTION_TYPE action, const char *pkgid)
707 {
708         int ret = -1;
709         const xmlChar *name;
710
711         if (xmlTextReaderDepth(reader) != 1) {
712                 _LOGE("Node depth is not 1");
713                 goto END;
714         }
715
716         if (xmlTextReaderNodeType(reader) != 1) {
717                 _LOGE("Node type is not 1");
718                 goto END;
719         }
720
721         const xmlChar *value;
722         name = xmlTextReaderConstName(reader);
723         if (name == NULL) {
724                 _LOGE("TEST TEST TES\n");
725                 name = BAD_CAST "--";
726         }
727
728         value = xmlTextReaderConstValue(reader);
729         if (value != NULL) {
730                 if (xmlStrlen(value) > 40) {
731                         _LOGD(" %.40s...", value);
732                 } else {
733                         _LOGD(" %s", value);
734                 }
735         }
736
737         name = xmlTextReaderConstName(reader);
738         if (name == NULL) {
739                 _LOGE("TEST TEST TES\n");
740                 name = BAD_CAST "--";
741         }
742
743         xmlDocPtr docPtr = xmlTextReaderCurrentDoc(reader);
744         xmlDocPtr copyDocPtr = xmlCopyDoc(docPtr, 1);
745         if (copyDocPtr == NULL)
746                 return -1;
747         xmlNode *rootElement = xmlDocGetRootElement(copyDocPtr);
748         if (rootElement == NULL)
749                 return -1;
750         xmlNode *cur_node = xmlFirstElementChild(rootElement);
751         if (cur_node == NULL)
752                 return -1;
753         xmlNode *temp = xmlTextReaderExpand(reader);
754         if (temp == NULL)
755                 return -1;
756         xmlNode *next_node = NULL;
757         while(cur_node != NULL) {
758                 if ( (strcmp(ASCII(temp->name), ASCII(cur_node->name)) == 0) &&
759                         (temp->line == cur_node->line) ) {
760                         break;
761                 }
762                 else {
763                         next_node = xmlNextElementSibling(cur_node);
764                         xmlUnlinkNode(cur_node);
765                         xmlFreeNode(cur_node);
766                         cur_node = next_node;
767                 }
768         }
769         if (cur_node == NULL)
770                 return -1;
771         next_node = xmlNextElementSibling(cur_node);
772         if (next_node) {
773                 cur_node->next = NULL;
774                 next_node->prev = NULL;
775                 xmlFreeNodeList(next_node);
776                 xmlSetTreeDoc(cur_node, copyDocPtr);
777         } else {
778                 xmlSetTreeDoc(cur_node, copyDocPtr);
779         }
780
781         ret = __ps_run_tag_parser(lib_handle, copyDocPtr, ASCII(name), action, pkgid);
782  END:
783
784         return ret;
785 }
786
787 static int __run_metadata_parser_prestep (manifest_x *mfx, char *md_key, ACTION_TYPE action)
788 {
789         int ret = -1;
790         int tag_exist = 0;
791         char buffer[1024] = { 0, };
792         uiapplication_x *up = mfx->uiapplication;
793         metadata_x *md = NULL;
794         char *md_tag = NULL;
795
796         GList *md_list = NULL;
797         __metadata_t *md_detail = NULL;
798
799         md_tag = __get_tag_by_key(md_key);
800         if (md_tag == NULL) {
801                 _LOGD("md_tag is NULL\n");
802                 return -1;
803         }
804
805         while(up != NULL)
806         {
807                 md = up->metadata;
808                 while (md != NULL)
809                 {
810                         //get glist of metadata key and value combination
811                         memset(buffer, 0x00, 1024);
812                         snprintf(buffer, 1024, "%s/", md_key);
813                         if ((md->key && md->value) && (strncmp(md->key, md_key, strlen(md_key)) == 0) && (strncmp(buffer, md->key, strlen(buffer)) == 0)) {
814                                 md_detail = (__metadata_t*) calloc(1, sizeof(__metadata_t));
815                                 if (md_detail == NULL) {
816                                         _LOGD("Memory allocation failed\n");
817                                         goto END;
818                                 }
819
820                                 md_detail->key = (char*) calloc(1, sizeof(char)*(strlen(md->key)+2));
821                                 if (md_detail->key == NULL) {
822                                         _LOGD("Memory allocation failed\n");
823                                         free(md_detail);
824                                         goto END;
825                                 }
826                                 snprintf(md_detail->key, (strlen(md->key)+1), "%s", md->key);
827
828                                 md_detail->value = (char*) calloc(1, sizeof(char)*(strlen(md->value)+2));
829                                 if (md_detail->value == NULL) {
830                                         _LOGD("Memory allocation failed\n");
831                                         free(md_detail->key);
832                                         free(md_detail);
833                                         goto END;
834                                 }
835                                 snprintf(md_detail->value, (strlen(md->value)+1), "%s", md->value);
836
837                                 md_list = g_list_append(md_list, (gpointer)md_detail);
838                                 tag_exist = 1;
839                         }
840                         md = md->next;
841                 }
842
843                 //send glist to parser when tags for metadata plugin parser exist.
844                 if (tag_exist) {
845                         ret = __ps_run_metadata_parser(md_list, md_tag, action, mfx->package, up->appid);
846                         if (ret < 0){
847                                 _LOGD("metadata_parser failed[%d] for tag[%s]\n", ret, md_tag);
848                         }
849                         else{
850                                 _LOGD("metadata_parser success for tag[%s]\n", md_tag);
851                         }
852                 }
853                 __metadata_parser_clear_dir_list(md_list);
854                 md_list = NULL;
855                 tag_exist = 0;
856                 up = up->next;
857         }
858
859         return 0;
860 END:
861         __metadata_parser_clear_dir_list(md_list);
862
863         if (md_tag)
864                 free(md_tag);
865
866         return ret;
867 }
868
869 static int __run_category_parser_prestep (manifest_x *mfx, char *category_key, ACTION_TYPE action)
870 {
871         int ret = -1;
872         int tag_exist = 0;
873         char buffer[1024] = { 0, };
874         uiapplication_x *up = mfx->uiapplication;
875         category_x *category = NULL;
876         char *category_tag = NULL;
877
878         GList *category_list = NULL;
879         __category_t *category_detail = NULL;
880
881         category_tag = __get_tag_by_key(category_key);
882         if (category_tag == NULL) {
883                 _LOGD("md_tag is NULL\n");
884                 return -1;
885         }
886
887         while(up != NULL)
888         {
889                 category = up->category;
890                 while (category != NULL)
891                 {
892                         //get glist of category key and value combination
893                         memset(buffer, 0x00, 1024);
894                         snprintf(buffer, 1024, "%s/", category_key);
895                         if ((category->name) && (strncmp(category->name, category_key, strlen(category_key)) == 0)) {
896                                 category_detail = (__category_t*) calloc(1, sizeof(__category_t));
897                                 if (category_detail == NULL) {
898                                         _LOGD("Memory allocation failed\n");
899                                         goto END;
900                                 }
901
902                                 category_detail->name = (char*) calloc(1, sizeof(char)*(strlen(category->name)+2));
903                                 if (category_detail->name == NULL) {
904                                         _LOGD("Memory allocation failed\n");
905                                         free(category_detail);
906                                         goto END;
907                                 }
908                                 snprintf(category_detail->name, (strlen(category->name)+1), "%s", category->name);
909
910                                 category_list = g_list_append(category_list, (gpointer)category_detail);
911                                 tag_exist = 1;
912                         }
913                         category = category->next;
914                 }
915
916                 //send glist to parser when tags for metadata plugin parser exist.
917                 if (tag_exist) {
918                         ret = __ps_run_category_parser(category_list, category_tag, action, mfx->package, up->appid);
919                         if (ret < 0)
920                                 _LOGD("category_parser failed[%d] for tag[%s]\n", ret, category_tag);
921                         else
922                                 _LOGD("category_parser success for tag[%s]\n", category_tag);
923                 }
924                 __category_parser_clear_dir_list(category_list);
925                 category_list = NULL;
926                 tag_exist = 0;
927                 up = up->next;
928         }
929
930         return 0;
931 END:
932         __category_parser_clear_dir_list(category_list);
933
934         if (category_tag)
935                 free(category_tag);
936
937         return ret;
938 }
939
940 static int __run_parser_prestep(xmlTextReaderPtr reader, ACTION_TYPE action, const char *pkgid)
941 {
942         int ret = -1;
943         const xmlChar *name;
944
945 //      _LOGD("__run_parser_prestep");
946
947         if (xmlTextReaderDepth(reader) != 1) {
948                 _LOGE("Node depth is not 1");
949                 goto END;
950         }
951
952         if (xmlTextReaderNodeType(reader) != 1) {
953                 _LOGE("Node type is not 1");
954                 goto END;
955         }
956
957         const xmlChar *value;
958         name = xmlTextReaderConstName(reader);
959         if (name == NULL) {
960                 _LOGE("TEST TEST TES\n");
961                 name = BAD_CAST "--";
962         }
963
964         value = xmlTextReaderConstValue(reader);
965         _LOGD("%d %d %s %d %d",
966             xmlTextReaderDepth(reader),
967             xmlTextReaderNodeType(reader),
968             name,
969             xmlTextReaderIsEmptyElement(reader), xmlTextReaderHasValue(reader));
970
971         if (value != NULL) {
972                 if (xmlStrlen(value) > 40) {
973                         _LOGD(" %.40s...", value);
974                 } else {
975                         _LOGD(" %s", value);
976                 }
977         }
978
979         name = xmlTextReaderConstName(reader);
980         if (name == NULL) {
981                 _LOGE("TEST TEST TES\n");
982                 name = BAD_CAST "--";
983         }
984
985         xmlDocPtr docPtr = xmlTextReaderCurrentDoc(reader);
986         _LOGD("docPtr->URL %s\n", (char *)docPtr->URL);
987         xmlDocPtr copyDocPtr = xmlCopyDoc(docPtr, 1);
988         if (copyDocPtr == NULL)
989                 return -1;
990         xmlNode *rootElement = xmlDocGetRootElement(copyDocPtr);
991         if (rootElement == NULL)
992                 return -1;
993         xmlNode *cur_node = xmlFirstElementChild(rootElement);
994         if (cur_node == NULL)
995                 return -1;
996         xmlNode *temp = xmlTextReaderExpand(reader);
997         if (temp == NULL)
998                 return -1;
999         xmlNode *next_node = NULL;
1000         while(cur_node != NULL) {
1001                 if ( (strcmp(ASCII(temp->name), ASCII(cur_node->name)) == 0) &&
1002                         (temp->line == cur_node->line) ) {
1003                         break;
1004                 }
1005                 else {
1006                         next_node = xmlNextElementSibling(cur_node);
1007                         xmlUnlinkNode(cur_node);
1008                         xmlFreeNode(cur_node);
1009                         cur_node = next_node;
1010                 }
1011         }
1012         if (cur_node == NULL)
1013                 return -1;
1014         next_node = xmlNextElementSibling(cur_node);
1015         if (next_node) {
1016                 cur_node->next = NULL;
1017                 next_node->prev = NULL;
1018                 xmlFreeNodeList(next_node);
1019                 xmlSetTreeDoc(cur_node, copyDocPtr);
1020         } else {
1021                 xmlSetTreeDoc(cur_node, copyDocPtr);
1022         }
1023
1024 #ifdef __DEBUG__
1025
1026 //#else
1027         _LOGD("node type: %d, name: %s children->name: %s last->name: %s\n"
1028             "parent->name: %s next->name: %s prev->name: %s\n",
1029             cur_node->type, cur_node->name,
1030             cur_node->children ? cur_node->children->name : "NULL",
1031             cur_node->last ? cur_node->last->name : "NULL",
1032             cur_node->parent ? cur_node->parent->name : "NULL",
1033             cur_node->next ? cur_node->next->name : "NULL",
1034             cur_node->prev ? cur_node->prev->name : "NULL");
1035
1036         FILE *fp = fopen(tzplatform_mkpath(TZ_SYS_SHARE, "test.xml"), "a");
1037         xmlDocDump(fp, copyDocPtr);
1038         fclose(fp);
1039 #endif
1040
1041         ret = __ps_run_parser(copyDocPtr, ASCII(name), action, pkgid);
1042  END:
1043
1044         return ret;
1045 }
1046
1047 static void
1048 __processNode(xmlTextReaderPtr reader, ACTION_TYPE action, char *const tagv[], const char *pkgid)
1049 {
1050         char *tag = NULL;
1051         int i = 0;
1052
1053         switch (xmlTextReaderNodeType(reader)) {
1054         case XML_READER_TYPE_END_ELEMENT:
1055                 {
1056                         //            _LOGD("XML_READER_TYPE_END_ELEMENT");
1057                         break;
1058                 }
1059
1060         case XML_READER_TYPE_ELEMENT:
1061                 {
1062                         // Elements without closing tag don't receive
1063                         // XML_READER_TYPE_END_ELEMENT event.
1064
1065                         const xmlChar *elementName =
1066                             xmlTextReaderLocalName(reader);
1067                         if (elementName == NULL) {
1068 //                              _LOGD("elementName %s\n", (char *)elementName);
1069                                 break;
1070                         }
1071
1072                         const xmlChar *nameSpace =
1073                             xmlTextReaderConstNamespaceUri(reader);
1074                         if (nameSpace) {
1075 //                              _LOGD("nameSpace %s\n", (char *)nameSpace);
1076                         }
1077 /*
1078                         _LOGD("XML_READER_TYPE_ELEMENT %s, %s\n",
1079                             elementName ? elementName : "NULL",
1080                             nameSpace ? nameSpace : "NULL");
1081 */
1082                         if (tagv == NULL) {
1083                                 _LOGD("__run_parser_prestep pkgid[%s]\n", pkgid);
1084                                 __run_parser_prestep(reader, action, pkgid);
1085                         }
1086                         else {
1087                                 i = 0;
1088                                 for (tag = tagv[0]; tag; tag = tagv[++i])
1089                                         if (strcmp(tag, ASCII(elementName)) == 0) {
1090                                                 _LOGD("__run_parser_prestep tag[%s] pkgid[%s]\n", tag, pkgid);
1091                                                 __run_parser_prestep(reader,
1092                                                                      action, pkgid);
1093                                                 break;
1094                                         }
1095                         }
1096
1097                         break;
1098                 }
1099         case XML_READER_TYPE_TEXT:
1100         case XML_READER_TYPE_CDATA:
1101                 {
1102                         const xmlChar *value = xmlTextReaderConstValue(reader);
1103                         if (value) {
1104 //                              _LOGD("value %s\n", value);
1105                         }
1106
1107                         const xmlChar *lang = xmlTextReaderConstXmlLang(reader);
1108                         if (lang) {
1109 //                              _LOGD("lang\n", lang);
1110                         }
1111
1112 /*                      _LOGD("XML_READER_TYPE_TEXT %s, %s\n",
1113                             value ? value : "NULL", lang ? lang : "NULL");
1114 */
1115                         break;
1116                 }
1117         default:
1118 //              _LOGD("Ignoring Node of Type: %d", xmlTextReaderNodeType(reader));
1119                 break;
1120         }
1121 }
1122
1123 static void
1124 __processTag(void *lib_handle, xmlTextReaderPtr reader, ACTION_TYPE action, char *tag, const char *pkgid)
1125 {
1126         switch (xmlTextReaderNodeType(reader)) {
1127         case XML_READER_TYPE_END_ELEMENT:
1128                 {
1129                         break;
1130                 }
1131         case XML_READER_TYPE_ELEMENT:
1132                 {
1133                         // Elements without closing tag don't receive
1134                         const xmlChar *elementName =
1135                             xmlTextReaderLocalName(reader);
1136                         if (elementName == NULL) {
1137                                 break;
1138                         }
1139
1140                         if (strcmp(tag, ASCII(elementName)) == 0) {
1141                                 _LOGD("find : tag[%s] ACTION_TYPE[%d] pkg[%s]\n", tag, action, pkgid);
1142                                 __run_tag_parser_prestep(lib_handle, reader, action, pkgid);
1143                                 break;
1144                         }
1145                         break;
1146                 }
1147
1148         default:
1149                 break;
1150         }
1151 }
1152
1153 static int __parser_send_tag(void *lib_handle, ACTION_TYPE action, PLUGIN_PROCESS_TYPE process, const char *pkgid)
1154 {
1155         int (*plugin_install) (const char *);
1156         int ret = -1;
1157         char *ac = NULL;
1158
1159         if (process == PLUGIN_PRE_PROCESS) {
1160                 switch (action) {
1161                 case ACTION_INSTALL:
1162                         ac = "PKGMGR_PARSER_PLUGIN_PRE_INSTALL";
1163                         break;
1164                 case ACTION_UPGRADE:
1165                         ac = "PKGMGR_PARSER_PLUGIN_PRE_UPGRADE";
1166                         break;
1167                 case ACTION_UNINSTALL:
1168                         ac = "PKGMGR_PARSER_PLUGIN_PRE_UNINSTALL";
1169                         break;
1170                 default:
1171                         return -1;
1172                 }
1173         } else if (process == PLUGIN_POST_PROCESS) {
1174                 switch (action) {
1175                 case ACTION_INSTALL:
1176                         ac = "PKGMGR_PARSER_PLUGIN_POST_INSTALL";
1177                         break;
1178                 case ACTION_UPGRADE:
1179                         ac = "PKGMGR_PARSER_PLUGIN_POST_UPGRADE";
1180                         break;
1181                 case ACTION_UNINSTALL:
1182                         ac = "PKGMGR_PARSER_PLUGIN_POST_UNINSTALL";
1183                         break;
1184                 default:
1185                         return -1;
1186                 }
1187         } else
1188                 return -1;
1189
1190         if ((plugin_install =
1191                 dlsym(lib_handle, ac)) == NULL || dlerror() != NULL) {
1192                 return -1;
1193         }
1194
1195         ret = plugin_install(pkgid);
1196         return ret;
1197 }
1198
1199 static void __plugin_send_tag(const char *tag, ACTION_TYPE action, PLUGIN_PROCESS_TYPE process, const char *pkgid)
1200 {
1201         char *lib_path = NULL;
1202         void *lib_handle = NULL;
1203         int (*plugin_install) (const char *);
1204         int ret = -1;
1205         char *ac = NULL;
1206
1207         if (process == PLUGIN_PRE_PROCESS) {
1208                 switch (action) {
1209                 case ACTION_INSTALL:
1210                         ac = "PKGMGR_PARSER_PLUGIN_PRE_INSTALL";
1211                         break;
1212                 case ACTION_UPGRADE:
1213                         ac = "PKGMGR_PARSER_PLUGIN_PRE_UPGRADE";
1214                         break;
1215                 case ACTION_UNINSTALL:
1216                         ac = "PKGMGR_PARSER_PLUGIN_PRE_UNINSTALL";
1217                         break;
1218                 default:
1219                         goto END;
1220                 }
1221         } else if (process == PLUGIN_POST_PROCESS) {
1222                 switch (action) {
1223                 case ACTION_INSTALL:
1224                         ac = "PKGMGR_PARSER_PLUGIN_POST_INSTALL";
1225                         break;
1226                 case ACTION_UPGRADE:
1227                         ac = "PKGMGR_PARSER_PLUGIN_POST_UPGRADE";
1228                         break;
1229                 case ACTION_UNINSTALL:
1230                         ac = "PKGMGR_PARSER_PLUGIN_POST_UNINSTALL";
1231                         break;
1232                 default:
1233                         goto END;
1234                 }
1235         } else
1236                 goto END;
1237
1238         lib_path = __get_parser_plugin(tag);
1239         if (!lib_path) {
1240                 goto END;
1241         }
1242
1243         if ((lib_handle = dlopen(lib_path, RTLD_LAZY)) == NULL) {
1244                 _LOGE("dlopen is failed lib_path[%s] for tag[%s]\n", lib_path, tag);
1245                 goto END;
1246         }
1247         if ((plugin_install =
1248                 dlsym(lib_handle, ac)) == NULL || dlerror() != NULL) {
1249 //              _LOGE("can not find symbol[%s] for tag[%s] \n", ac, tag);
1250                 goto END;
1251         }
1252
1253         ret = plugin_install(pkgid);
1254         if (ret < 0)
1255                 _LOGD("[PLUGIN_PROCESS_TYPE[%d] pkgid=%s, tag=%s plugin fail\n", process, pkgid, tag);
1256         else
1257                 _LOGD("[PLUGIN_PROCESS_TYPE[%d] pkgid=%s, tag=%s plugin success\n", process, pkgid, tag);
1258
1259 END:
1260         if (lib_path)
1261                 free(lib_path);
1262         if (lib_handle)
1263                 dlclose(lib_handle);
1264 }
1265
1266 static void
1267 __plugin_process_tag(char *const tag_list[], ACTION_TYPE action, PLUGIN_PROCESS_TYPE process, const char *pkgid)
1268 {
1269         char *tag = NULL;
1270         int i = 0;
1271
1272         for (tag = tag_list[0]; tag; tag = tag_list[++i])
1273                 __plugin_send_tag(tag, action, process, pkgid);
1274
1275 }
1276
1277 static void
1278 __plugin_save_tag(xmlTextReaderPtr reader, char *const tagv[], char *tag_list[])
1279 {
1280         char *tag = NULL;
1281         int i = 0;
1282         static int pre_cnt=0;
1283
1284         switch (xmlTextReaderNodeType(reader)) {
1285         case XML_READER_TYPE_ELEMENT:
1286                 {
1287                         const xmlChar *elementName = xmlTextReaderLocalName(reader);
1288                         if (elementName == NULL) {
1289                                 break;
1290                         }
1291                         i = 0;
1292                         for (tag = tag_list[0]; tag; tag = tag_list[++i])
1293                                 if (strcmp(ASCII(elementName), tag) == 0) {
1294                                         return;
1295                                 }
1296                         i = 0;
1297                         for (tag = tagv[0]; tag; tag = tagv[++i])
1298                                 if (strcmp(tag, ASCII(elementName)) == 0) {
1299                                         tag_list[pre_cnt++] = tag;
1300                                         break;
1301                                 }
1302                         break;
1303                 }
1304         default:
1305 //              _LOGD("Ignoring Node of Type: %d", xmlTextReaderNodeType(reader));
1306                 break;
1307         }
1308 }
1309
1310 static void
1311 __streamFile(const char *filename, ACTION_TYPE action, char *const tagv[], const char *pkgid)
1312 {
1313         xmlTextReaderPtr reader;
1314         xmlDocPtr docPtr;
1315         int ret;
1316         __plugin_process_tag(tagv, action, PLUGIN_PRE_PROCESS, pkgid);
1317
1318         docPtr = xmlReadFile(filename, NULL, 0);
1319         reader = xmlReaderWalker(docPtr);
1320         if (reader != NULL) {
1321                 ret = xmlTextReaderRead(reader);
1322                 while (ret == 1) {
1323                         __processNode(reader, action, tagv, pkgid);
1324                         ret = xmlTextReaderRead(reader);
1325                 }
1326                 xmlFreeTextReader(reader);
1327
1328                 if (ret != 0) {
1329                         _LOGD("%s : failed to parse", filename);
1330                 }
1331         } else {
1332                 _LOGD("Unable to open %s", filename);
1333         }
1334
1335         __plugin_process_tag(tagv, action, PLUGIN_POST_PROCESS, pkgid);
1336 }
1337
1338 static int __next_child_element(xmlTextReaderPtr reader, int depth)
1339 {
1340         int ret = xmlTextReaderRead(reader);
1341         int cur = xmlTextReaderDepth(reader);
1342         while (ret == 1) {
1343
1344                 switch (xmlTextReaderNodeType(reader)) {
1345                 case XML_READER_TYPE_ELEMENT:
1346                         if (cur == depth + 1)
1347                                 return 1;
1348                         break;
1349                 case XML_READER_TYPE_TEXT:
1350                         /*text is handled by each function separately*/
1351                         if (cur == depth + 1)
1352                                 return 0;
1353                         break;
1354                 case XML_READER_TYPE_END_ELEMENT:
1355                         if (cur == depth)
1356                                 return 0;
1357                         break;
1358                 default:
1359                         if (cur <= depth)
1360                                 return 0;
1361                         break;
1362                 }
1363                 ret = xmlTextReaderRead(reader);
1364                 cur = xmlTextReaderDepth(reader);
1365         }
1366         return ret;
1367 }
1368
1369 static bool __check_action_fota(char *const tagv[])
1370 {
1371         int i = 0;
1372         char delims[] = "=";
1373         char *ret_result = NULL;
1374         char *tag = NULL;
1375         int ret = false;
1376
1377         if (tagv == NULL)
1378                 return ret;
1379
1380         for (tag = strdup(tagv[0]); tag != NULL; ) {
1381                 ret_result = strtok(tag, delims);
1382
1383                 /*check tag :  fota is true */
1384                 if (strcmp(ret_result, "fota") == 0) {
1385                         ret_result = strtok(NULL, delims);
1386                         if (strcmp(ret_result, "true") == 0) {
1387                                 ret = true;
1388                         }
1389                 } else
1390                         _LOGD("tag process [%s]is not defined\n", ret_result);
1391
1392                 free(tag);
1393
1394                 /*check next value*/
1395                 if (tagv[++i] != NULL)
1396                         tag = strdup(tagv[i]);
1397                 else {
1398                         _LOGD("tag process success...%d\n" , ret);
1399                         return ret;
1400                 }
1401         }
1402
1403         return ret;
1404 }
1405
1406 static void __ps_free_category(category_x *category)
1407 {
1408         if (category == NULL)
1409                 return;
1410         if (category->name) {
1411                 free((void *)category->name);
1412                 category->name = NULL;
1413         }
1414         free((void*)category);
1415         category = NULL;
1416 }
1417
1418 static void __ps_free_privilege(privilege_x *privilege)
1419 {
1420         if (privilege == NULL)
1421                 return;
1422         if (privilege->text) {
1423                 free((void *)privilege->text);
1424                 privilege->text = NULL;
1425         }
1426         free((void*)privilege);
1427         privilege = NULL;
1428 }
1429
1430 static void __ps_free_privileges(privileges_x *privileges)
1431 {
1432         if (privileges == NULL)
1433                 return;
1434         /*Free Privilege*/
1435         if (privileges->privilege) {
1436                 privilege_x *privilege = privileges->privilege;
1437                 privilege_x *tmp = NULL;
1438                 while(privilege != NULL) {
1439                         tmp = privilege->next;
1440                         __ps_free_privilege(privilege);
1441                         privilege = tmp;
1442                 }
1443         }
1444         free((void*)privileges);
1445         privileges = NULL;
1446 }
1447
1448 static void __ps_free_metadata(metadata_x *metadata)
1449 {
1450         if (metadata == NULL)
1451                 return;
1452         if (metadata->key) {
1453                 free((void *)metadata->key);
1454                 metadata->key = NULL;
1455         }
1456         if (metadata->value) {
1457                 free((void *)metadata->value);
1458                 metadata->value = NULL;
1459         }
1460         free((void*)metadata);
1461         metadata = NULL;
1462 }
1463
1464 static void __ps_free_permission(permission_x *permission)
1465 {
1466         if (permission == NULL)
1467                 return;
1468         if (permission->type) {
1469                 free((void *)permission->type);
1470                 permission->type = NULL;
1471         }
1472         if (permission->value) {
1473                 free((void *)permission->value);
1474                 permission->value = NULL;
1475         }
1476         free((void*)permission);
1477         permission = NULL;
1478 }
1479
1480 static void __ps_free_icon(icon_x *icon)
1481 {
1482         if (icon == NULL)
1483                 return;
1484         if (icon->text) {
1485                 free((void *)icon->text);
1486                 icon->text = NULL;
1487         }
1488         if (icon->lang) {
1489                 free((void *)icon->lang);
1490                 icon->lang = NULL;
1491         }
1492         if (icon->name) {
1493                 free((void *)icon->name);
1494                 icon->name= NULL;
1495         }
1496         if (icon->section) {
1497                 free((void *)icon->section);
1498                 icon->section = NULL;
1499         }
1500         if (icon->size) {
1501                 free((void *)icon->size);
1502                 icon->size = NULL;
1503         }
1504         if (icon->resolution) {
1505                 free((void *)icon->resolution);
1506                 icon->resolution = NULL;
1507         }
1508         free((void*)icon);
1509         icon = NULL;
1510 }
1511
1512 static void __ps_free_image(image_x *image)
1513 {
1514         if (image == NULL)
1515                 return;
1516         if (image->text) {
1517                 free((void *)image->text);
1518                 image->text = NULL;
1519         }
1520         if (image->lang) {
1521                 free((void *)image->lang);
1522                 image->lang = NULL;
1523         }
1524         if (image->name) {
1525                 free((void *)image->name);
1526                 image->name= NULL;
1527         }
1528         if (image->section) {
1529                 free((void *)image->section);
1530                 image->section = NULL;
1531         }
1532         free((void*)image);
1533         image = NULL;
1534 }
1535
1536 static void __ps_free_operation(operation_x *operation)
1537 {
1538         if (operation == NULL)
1539                 return;
1540         if (operation->text) {
1541                 free((void *)operation->text);
1542                 operation->text = NULL;
1543         }
1544         free((void*)operation);
1545         operation = NULL;
1546 }
1547
1548 static void __ps_free_uri(uri_x *uri)
1549 {
1550         if (uri == NULL)
1551                 return;
1552         if (uri->text) {
1553                 free((void *)uri->text);
1554                 uri->text = NULL;
1555         }
1556         free((void*)uri);
1557         uri = NULL;
1558 }
1559
1560 static void __ps_free_mime(mime_x *mime)
1561 {
1562         if (mime == NULL)
1563                 return;
1564         if (mime->text) {
1565                 free((void *)mime->text);
1566                 mime->text = NULL;
1567         }
1568         free((void*)mime);
1569         mime = NULL;
1570 }
1571
1572 static void __ps_free_subapp(subapp_x *subapp)
1573 {
1574         if (subapp == NULL)
1575                 return;
1576         if (subapp->text) {
1577                 free((void *)subapp->text);
1578                 subapp->text = NULL;
1579         }
1580         free((void*)subapp);
1581         subapp = NULL;
1582 }
1583
1584 static void __ps_free_condition(condition_x *condition)
1585 {
1586         if (condition == NULL)
1587                 return;
1588         if (condition->text) {
1589                 free((void *)condition->text);
1590                 condition->text = NULL;
1591         }
1592         if (condition->name) {
1593                 free((void *)condition->name);
1594                 condition->name = NULL;
1595         }
1596         free((void*)condition);
1597         condition = NULL;
1598 }
1599
1600 static void __ps_free_notification(notification_x *notification)
1601 {
1602         if (notification == NULL)
1603                 return;
1604         if (notification->text) {
1605                 free((void *)notification->text);
1606                 notification->text = NULL;
1607         }
1608         if (notification->name) {
1609                 free((void *)notification->name);
1610                 notification->name = NULL;
1611         }
1612         free((void*)notification);
1613         notification = NULL;
1614 }
1615
1616 static void __ps_free_compatibility(compatibility_x *compatibility)
1617 {
1618         if (compatibility == NULL)
1619                 return;
1620         if (compatibility->text) {
1621                 free((void *)compatibility->text);
1622                 compatibility->text = NULL;
1623         }
1624         if (compatibility->name) {
1625                 free((void *)compatibility->name);
1626                 compatibility->name = NULL;
1627         }
1628         free((void*)compatibility);
1629         compatibility = NULL;
1630 }
1631
1632 static void __ps_free_resolution(resolution_x *resolution)
1633 {
1634         if (resolution == NULL)
1635                 return;
1636         if (resolution->mimetype) {
1637                 free((void *)resolution->mimetype);
1638                 resolution->mimetype = NULL;
1639         }
1640         if (resolution->urischeme) {
1641                 free((void *)resolution->urischeme);
1642                 resolution->urischeme = NULL;
1643         }
1644         free((void*)resolution);
1645         resolution = NULL;
1646 }
1647
1648 static void __ps_free_capability(capability_x *capability)
1649 {
1650         if (capability == NULL)
1651                 return;
1652         if (capability->operationid) {
1653                 free((void *)capability->operationid);
1654                 capability->operationid = NULL;
1655         }
1656         /*Free Resolution*/
1657         if (capability->resolution) {
1658                 resolution_x *resolution = capability->resolution;
1659                 resolution_x *tmp = NULL;
1660                 while(resolution != NULL) {
1661                         tmp = resolution->next;
1662                         __ps_free_resolution(resolution);
1663                         resolution = tmp;
1664                 }
1665         }
1666         free((void*)capability);
1667         capability = NULL;
1668 }
1669
1670 static void __ps_free_allowed(allowed_x *allowed)
1671 {
1672         if (allowed == NULL)
1673                 return;
1674         if (allowed->name) {
1675                 free((void *)allowed->name);
1676                 allowed->name = NULL;
1677         }
1678         if (allowed->text) {
1679                 free((void *)allowed->text);
1680                 allowed->text = NULL;
1681         }
1682         free((void*)allowed);
1683         allowed = NULL;
1684 }
1685
1686 static void __ps_free_request(request_x *request)
1687 {
1688         if (request == NULL)
1689                 return;
1690         if (request->text) {
1691                 free((void *)request->text);
1692                 request->text = NULL;
1693         }
1694         free((void*)request);
1695         request = NULL;
1696 }
1697
1698 static void __ps_free_datacontrol(datacontrol_x *datacontrol)
1699 {
1700         if (datacontrol == NULL)
1701                 return;
1702         if (datacontrol->providerid) {
1703                 free((void *)datacontrol->providerid);
1704                 datacontrol->providerid = NULL;
1705         }
1706         /*Free Capability*/
1707         if (datacontrol->capability) {
1708                 capability_x *capability = datacontrol->capability;
1709                 capability_x *tmp = NULL;
1710                 while(capability != NULL) {
1711                         tmp = capability->next;
1712                         __ps_free_capability(capability);
1713                         capability = tmp;
1714                 }
1715         }
1716         free((void*)datacontrol);
1717         datacontrol = NULL;
1718 }
1719
1720 static void __ps_free_launchconditions(launchconditions_x *launchconditions)
1721 {
1722         if (launchconditions == NULL)
1723                 return;
1724         if (launchconditions->text) {
1725                 free((void *)launchconditions->text);
1726                 launchconditions->text = NULL;
1727         }
1728         /*Free Condition*/
1729         if (launchconditions->condition) {
1730                 condition_x *condition = launchconditions->condition;
1731                 condition_x *tmp = NULL;
1732                 while(condition != NULL) {
1733                         tmp = condition->next;
1734                         __ps_free_condition(condition);
1735                         condition = tmp;
1736                 }
1737         }
1738         free((void*)launchconditions);
1739         launchconditions = NULL;
1740 }
1741
1742 static void __ps_free_appcontrol(appcontrol_x *appcontrol)
1743 {
1744         if (appcontrol == NULL)
1745                 return;
1746         if (appcontrol->text) {
1747                 free((void *)appcontrol->text);
1748                 appcontrol->text = NULL;
1749         }
1750         /*Free Operation*/
1751         if (appcontrol->operation) {
1752                 operation_x *operation = appcontrol->operation;
1753                 operation_x *tmp = NULL;
1754                 while(operation != NULL) {
1755                         tmp = operation->next;
1756                         __ps_free_operation(operation);
1757                         operation = tmp;
1758                 }
1759         }
1760         /*Free Uri*/
1761         if (appcontrol->uri) {
1762                 uri_x *uri = appcontrol->uri;
1763                 uri_x *tmp = NULL;
1764                 while(uri != NULL) {
1765                         tmp = uri->next;
1766                         __ps_free_uri(uri);
1767                         uri = tmp;
1768                 }
1769         }
1770         /*Free Mime*/
1771         if (appcontrol->mime) {
1772                 mime_x *mime = appcontrol->mime;
1773                 mime_x *tmp = NULL;
1774                 while(mime != NULL) {
1775                         tmp = mime->next;
1776                         __ps_free_mime(mime);
1777                         mime = tmp;
1778                 }
1779         }
1780         /*Free subapp*/
1781         if (appcontrol->subapp) {
1782                 subapp_x *subapp = appcontrol->subapp;
1783                 subapp_x *tmp = NULL;
1784                 while(subapp != NULL) {
1785                         tmp = subapp->next;
1786                         __ps_free_subapp(subapp);
1787                         subapp = tmp;
1788                 }
1789         }
1790         free((void*)appcontrol);
1791         appcontrol = NULL;
1792 }
1793
1794 static void __ps_free_appsvc(appsvc_x *appsvc)
1795 {
1796         if (appsvc == NULL)
1797                 return;
1798         if (appsvc->text) {
1799                 free((void *)appsvc->text);
1800                 appsvc->text = NULL;
1801         }
1802         /*Free Operation*/
1803         if (appsvc->operation) {
1804                 operation_x *operation = appsvc->operation;
1805                 operation_x *tmp = NULL;
1806                 while(operation != NULL) {
1807                         tmp = operation->next;
1808                         __ps_free_operation(operation);
1809                         operation = tmp;
1810                 }
1811         }
1812         /*Free Uri*/
1813         if (appsvc->uri) {
1814                 uri_x *uri = appsvc->uri;
1815                 uri_x *tmp = NULL;
1816                 while(uri != NULL) {
1817                         tmp = uri->next;
1818                         __ps_free_uri(uri);
1819                         uri = tmp;
1820                 }
1821         }
1822         /*Free Mime*/
1823         if (appsvc->mime) {
1824                 mime_x *mime = appsvc->mime;
1825                 mime_x *tmp = NULL;
1826                 while(mime != NULL) {
1827                         tmp = mime->next;
1828                         __ps_free_mime(mime);
1829                         mime = tmp;
1830                 }
1831         }
1832         /*Free subapp*/
1833         if (appsvc->subapp) {
1834                 subapp_x *subapp = appsvc->subapp;
1835                 subapp_x *tmp = NULL;
1836                 while(subapp != NULL) {
1837                         tmp = subapp->next;
1838                         __ps_free_subapp(subapp);
1839                         subapp = tmp;
1840                 }
1841         }
1842         free((void*)appsvc);
1843         appsvc = NULL;
1844 }
1845
1846 static void __ps_free_deviceprofile(deviceprofile_x *deviceprofile)
1847 {
1848         return;
1849 }
1850
1851 static void __ps_free_define(define_x *define)
1852 {
1853         if (define == NULL)
1854                 return;
1855         if (define->path) {
1856                 free((void *)define->path);
1857                 define->path = NULL;
1858         }
1859         /*Free Request*/
1860         if (define->request) {
1861                 request_x *request = define->request;
1862                 request_x *tmp = NULL;
1863                 while(request != NULL) {
1864                         tmp = request->next;
1865                         __ps_free_request(request);
1866                         request = tmp;
1867                 }
1868         }
1869         /*Free Allowed*/
1870         if (define->allowed) {
1871                 allowed_x *allowed = define->allowed;
1872                 allowed_x *tmp = NULL;
1873                 while(allowed != NULL) {
1874                         tmp = allowed->next;
1875                         __ps_free_allowed(allowed);
1876                         allowed = tmp;
1877                 }
1878         }
1879         free((void*)define);
1880         define = NULL;
1881 }
1882
1883 static void __ps_free_datashare(datashare_x *datashare)
1884 {
1885         if (datashare == NULL)
1886                 return;
1887         /*Free Define*/
1888         if (datashare->define) {
1889                 define_x *define =  datashare->define;
1890                 define_x *tmp = NULL;
1891                 while(define != NULL) {
1892                         tmp = define->next;
1893                         __ps_free_define(define);
1894                         define = tmp;
1895                 }
1896         }
1897         /*Free Request*/
1898         if (datashare->request) {
1899                 request_x *request = datashare->request;
1900                 request_x *tmp = NULL;
1901                 while(request != NULL) {
1902                         tmp = request->next;
1903                         __ps_free_request(request);
1904                         request = tmp;
1905                 }
1906         }
1907         free((void*)datashare);
1908         datashare = NULL;
1909 }
1910
1911 static void __ps_free_label(label_x *label)
1912 {
1913         if (label == NULL)
1914                 return;
1915         if (label->name) {
1916                 free((void *)label->name);
1917                 label->name = NULL;
1918         }
1919         if (label->text) {
1920                 free((void *)label->text);
1921                 label->text = NULL;
1922         }
1923         if (label->lang) {
1924                 free((void *)label->lang);
1925                 label->lang= NULL;
1926         }
1927         free((void*)label);
1928         label = NULL;
1929 }
1930
1931 static void __ps_free_author(author_x *author)
1932 {
1933         if (author == NULL)
1934                 return;
1935         if (author->email) {
1936                 free((void *)author->email);
1937                 author->email = NULL;
1938         }
1939         if (author->text) {
1940                 free((void *)author->text);
1941                 author->text = NULL;
1942         }
1943         if (author->href) {
1944                 free((void *)author->href);
1945                 author->href = NULL;
1946         }
1947         if (author->lang) {
1948                 free((void *)author->lang);
1949                 author->lang = NULL;
1950         }
1951         free((void*)author);
1952         author = NULL;
1953 }
1954
1955 static void __ps_free_description(description_x *description)
1956 {
1957         if (description == NULL)
1958                 return;
1959         if (description->name) {
1960                 free((void *)description->name);
1961                 description->name = NULL;
1962         }
1963         if (description->text) {
1964                 free((void *)description->text);
1965                 description->text = NULL;
1966         }
1967         if (description->lang) {
1968                 free((void *)description->lang);
1969                 description->lang = NULL;
1970         }
1971         free((void*)description);
1972         description = NULL;
1973 }
1974
1975 static void __ps_free_license(license_x *license)
1976 {
1977         if (license == NULL)
1978                 return;
1979         if (license->text) {
1980                 free((void *)license->text);
1981                 license->text = NULL;
1982         }
1983         if (license->lang) {
1984                 free((void *)license->lang);
1985                 license->lang = NULL;
1986         }
1987         free((void*)license);
1988         license = NULL;
1989 }
1990
1991 static void __ps_free_uiapplication(uiapplication_x *uiapplication)
1992 {
1993         if (uiapplication == NULL)
1994                 return;
1995         if (uiapplication->exec) {
1996                 free((void *)uiapplication->exec);
1997                 uiapplication->exec = NULL;
1998         }
1999         if (uiapplication->appid) {
2000                 free((void *)uiapplication->appid);
2001                 uiapplication->appid = NULL;
2002         }
2003         if (uiapplication->nodisplay) {
2004                 free((void *)uiapplication->nodisplay);
2005                 uiapplication->nodisplay = NULL;
2006         }
2007         if (uiapplication->multiple) {
2008                 free((void *)uiapplication->multiple);
2009                 uiapplication->multiple = NULL;
2010         }
2011         if (uiapplication->type) {
2012                 free((void *)uiapplication->type);
2013                 uiapplication->type = NULL;
2014         }
2015         if (uiapplication->categories) {
2016                 free((void *)uiapplication->categories);
2017                 uiapplication->categories = NULL;
2018         }
2019         if (uiapplication->extraid) {
2020                 free((void *)uiapplication->extraid);
2021                 uiapplication->extraid = NULL;
2022         }
2023         if (uiapplication->taskmanage) {
2024                 free((void *)uiapplication->taskmanage);
2025                 uiapplication->taskmanage = NULL;
2026         }
2027         if (uiapplication->enabled) {
2028                 free((void *)uiapplication->enabled);
2029                 uiapplication->enabled = NULL;
2030         }
2031         if (uiapplication->hwacceleration) {
2032                 free((void *)uiapplication->hwacceleration);
2033                 uiapplication->hwacceleration = NULL;
2034         }
2035         if (uiapplication->screenreader) {
2036                 free((void *)uiapplication->screenreader);
2037                 uiapplication->screenreader = NULL;
2038         }
2039         if (uiapplication->mainapp) {
2040                 free((void *)uiapplication->mainapp);
2041                 uiapplication->mainapp = NULL;
2042         }
2043         if (uiapplication->recentimage) {
2044                 free((void *)uiapplication->recentimage);
2045                 uiapplication->recentimage = NULL;
2046         }
2047         if (uiapplication->package) {
2048                 free((void *)uiapplication->package);
2049                 uiapplication->package = NULL;
2050         }
2051         if (uiapplication->launchcondition) {
2052                 free((void *)uiapplication->launchcondition);
2053                 uiapplication->launchcondition = NULL;
2054         }
2055         /*Free Label*/
2056         if (uiapplication->label) {
2057                 label_x *label = uiapplication->label;
2058                 label_x *tmp = NULL;
2059                 while(label != NULL) {
2060                         tmp = label->next;
2061                         __ps_free_label(label);
2062                         label = tmp;
2063                 }
2064         }
2065         /*Free Icon*/
2066         if (uiapplication->icon) {
2067                 icon_x *icon = uiapplication->icon;
2068                 icon_x *tmp = NULL;
2069                 while(icon != NULL) {
2070                         tmp = icon->next;
2071                         __ps_free_icon(icon);
2072                         icon = tmp;
2073                 }
2074         }
2075         /*Free image*/
2076         if (uiapplication->image) {
2077                 image_x *image = uiapplication->image;
2078                 image_x *tmp = NULL;
2079                 while(image != NULL) {
2080                         tmp = image->next;
2081                         __ps_free_image(image);
2082                         image = tmp;
2083                 }
2084         }
2085         /*Free AppControl*/
2086         if (uiapplication->appcontrol) {
2087                 appcontrol_x *appcontrol = uiapplication->appcontrol;
2088                 appcontrol_x *tmp = NULL;
2089                 while(appcontrol != NULL) {
2090                         tmp = appcontrol->next;
2091                         __ps_free_appcontrol(appcontrol);
2092                         appcontrol = tmp;
2093                 }
2094         }
2095         /*Free LaunchConditions*/
2096         if (uiapplication->launchconditions) {
2097                 launchconditions_x *launchconditions = uiapplication->launchconditions;
2098                 launchconditions_x *tmp = NULL;
2099                 while(launchconditions != NULL) {
2100                         tmp = launchconditions->next;
2101                         __ps_free_launchconditions(launchconditions);
2102                         launchconditions = tmp;
2103                 }
2104         }
2105         /*Free Notification*/
2106         if (uiapplication->notification) {
2107                 notification_x *notification = uiapplication->notification;
2108                 notification_x *tmp = NULL;
2109                 while(notification != NULL) {
2110                         tmp = notification->next;
2111                         __ps_free_notification(notification);
2112                         notification = tmp;
2113                 }
2114         }
2115         /*Free DataShare*/
2116         if (uiapplication->datashare) {
2117                 datashare_x *datashare = uiapplication->datashare;
2118                 datashare_x *tmp = NULL;
2119                 while(datashare != NULL) {
2120                         tmp = datashare->next;
2121                         __ps_free_datashare(datashare);
2122                         datashare = tmp;
2123                 }
2124         }
2125         /*Free AppSvc*/
2126         if (uiapplication->appsvc) {
2127                 appsvc_x *appsvc = uiapplication->appsvc;
2128                 appsvc_x *tmp = NULL;
2129                 while(appsvc != NULL) {
2130                         tmp = appsvc->next;
2131                         __ps_free_appsvc(appsvc);
2132                         appsvc = tmp;
2133                 }
2134         }
2135         /*Free Category*/
2136         if (uiapplication->category) {
2137                 category_x *category = uiapplication->category;
2138                 category_x *tmp = NULL;
2139                 while(category != NULL) {
2140                         tmp = category->next;
2141                         __ps_free_category(category);
2142                         category = tmp;
2143                 }
2144         }
2145         /*Free Metadata*/
2146         if (uiapplication->metadata) {
2147                 metadata_x *metadata = uiapplication->metadata;
2148                 metadata_x *tmp = NULL;
2149                 while(metadata != NULL) {
2150                         tmp = metadata->next;
2151                         __ps_free_metadata(metadata);
2152                         metadata = tmp;
2153                 }
2154         }
2155         /*Free permission*/
2156         if (uiapplication->permission) {
2157                 permission_x *permission = uiapplication->permission;
2158                 permission_x *tmp = NULL;
2159                 while(permission != NULL) {
2160                         tmp = permission->next;
2161                         __ps_free_permission(permission);
2162                         permission = tmp;
2163                 }
2164         }
2165         /* _PRODUCT_LAUNCHING_ENHANCED_ START */
2166         if (uiapplication->indicatordisplay) {
2167                 free((void *)uiapplication->indicatordisplay);
2168                 uiapplication->indicatordisplay = NULL;
2169         }
2170         if (uiapplication->portraitimg) {
2171                 free((void *)uiapplication->portraitimg);
2172                 uiapplication->portraitimg = NULL;
2173         }
2174         if (uiapplication->landscapeimg) {
2175                 free((void *)uiapplication->landscapeimg);
2176                 uiapplication->landscapeimg = NULL;
2177         }
2178         /* _PRODUCT_LAUNCHING_ENHANCED_ END */
2179         if (uiapplication->guestmode_visibility) {
2180                 free((void *)uiapplication->guestmode_visibility);
2181                 uiapplication->guestmode_visibility = NULL;
2182         }
2183         if (uiapplication->app_component) {
2184                 free((void *)uiapplication->app_component);
2185                 uiapplication->app_component = NULL;
2186         }
2187         if (uiapplication->permission_type) {
2188                 free((void *)uiapplication->permission_type);
2189                 uiapplication->permission_type = NULL;
2190         }
2191         if (uiapplication->component_type) {
2192                 free((void *)uiapplication->component_type);
2193                 uiapplication->component_type = NULL;
2194         }
2195         if (uiapplication->preload) {
2196                 free((void *)uiapplication->preload);
2197                 uiapplication->preload = NULL;
2198         }
2199         if (uiapplication->submode) {
2200                 free((void *)uiapplication->submode);
2201                 uiapplication->submode = NULL;
2202         }
2203         if (uiapplication->submode_mainid) {
2204                 free((void *)uiapplication->submode_mainid);
2205                 uiapplication->submode_mainid = NULL;
2206         }
2207
2208         free((void*)uiapplication);
2209         uiapplication = NULL;
2210 }
2211
2212 static void __ps_free_serviceapplication(serviceapplication_x *serviceapplication)
2213 {
2214         if (serviceapplication == NULL)
2215                 return;
2216         if (serviceapplication->exec) {
2217                 free((void *)serviceapplication->exec);
2218                 serviceapplication->exec = NULL;
2219         }
2220         if (serviceapplication->appid) {
2221                 free((void *)serviceapplication->appid);
2222                 serviceapplication->appid = NULL;
2223         }
2224         if (serviceapplication->onboot) {
2225                 free((void *)serviceapplication->onboot);
2226                 serviceapplication->onboot = NULL;
2227         }
2228         if (serviceapplication->autorestart) {
2229                 free((void *)serviceapplication->autorestart);
2230                 serviceapplication->autorestart = NULL;
2231         }
2232         if (serviceapplication->type) {
2233                 free((void *)serviceapplication->type);
2234                 serviceapplication->type = NULL;
2235         }
2236         if (serviceapplication->enabled) {
2237                 free((void *)serviceapplication->enabled);
2238                 serviceapplication->enabled = NULL;
2239         }
2240         if (serviceapplication->package) {
2241                 free((void *)serviceapplication->package);
2242                 serviceapplication->package = NULL;
2243         }
2244         if (serviceapplication->permission_type) {
2245                 free((void *)serviceapplication->permission_type);
2246                 serviceapplication->permission_type = NULL;
2247         }
2248         /*Free Label*/
2249         if (serviceapplication->label) {
2250                 label_x *label = serviceapplication->label;
2251                 label_x *tmp = NULL;
2252                 while(label != NULL) {
2253                         tmp = label->next;
2254                         __ps_free_label(label);
2255                         label = tmp;
2256                 }
2257         }
2258         /*Free Icon*/
2259         if (serviceapplication->icon) {
2260                 icon_x *icon = serviceapplication->icon;
2261                 icon_x *tmp = NULL;
2262                 while(icon != NULL) {
2263                         tmp = icon->next;
2264                         __ps_free_icon(icon);
2265                         icon = tmp;
2266                 }
2267         }
2268         /*Free AppControl*/
2269         if (serviceapplication->appcontrol) {
2270                 appcontrol_x *appcontrol = serviceapplication->appcontrol;
2271                 appcontrol_x *tmp = NULL;
2272                 while(appcontrol != NULL) {
2273                         tmp = appcontrol->next;
2274                         __ps_free_appcontrol(appcontrol);
2275                         appcontrol = tmp;
2276                 }
2277         }
2278         /*Free DataControl*/
2279         if (serviceapplication->datacontrol) {
2280                 datacontrol_x *datacontrol = serviceapplication->datacontrol;
2281                 datacontrol_x *tmp = NULL;
2282                 while(datacontrol != NULL) {
2283                         tmp = datacontrol->next;
2284                         __ps_free_datacontrol(datacontrol);
2285                         datacontrol = tmp;
2286                 }
2287         }
2288         /*Free LaunchConditions*/
2289         if (serviceapplication->launchconditions) {
2290                 launchconditions_x *launchconditions = serviceapplication->launchconditions;
2291                 launchconditions_x *tmp = NULL;
2292                 while(launchconditions != NULL) {
2293                         tmp = launchconditions->next;
2294                         __ps_free_launchconditions(launchconditions);
2295                         launchconditions = tmp;
2296                 }
2297         }
2298         /*Free Notification*/
2299         if (serviceapplication->notification) {
2300                 notification_x *notification = serviceapplication->notification;
2301                 notification_x *tmp = NULL;
2302                 while(notification != NULL) {
2303                         tmp = notification->next;
2304                         __ps_free_notification(notification);
2305                         notification = tmp;
2306                 }
2307         }
2308         /*Free DataShare*/
2309         if (serviceapplication->datashare) {
2310                 datashare_x *datashare = serviceapplication->datashare;
2311                 datashare_x *tmp = NULL;
2312                 while(datashare != NULL) {
2313                         tmp = datashare->next;
2314                         __ps_free_datashare(datashare);
2315                         datashare = tmp;
2316                 }
2317         }
2318         /*Free AppSvc*/
2319         if (serviceapplication->appsvc) {
2320                 appsvc_x *appsvc = serviceapplication->appsvc;
2321                 appsvc_x *tmp = NULL;
2322                 while(appsvc != NULL) {
2323                         tmp = appsvc->next;
2324                         __ps_free_appsvc(appsvc);
2325                         appsvc = tmp;
2326                 }
2327         }
2328         /*Free Category*/
2329         if (serviceapplication->category) {
2330                 category_x *category = serviceapplication->category;
2331                 category_x *tmp = NULL;
2332                 while(category != NULL) {
2333                         tmp = category->next;
2334                         __ps_free_category(category);
2335                         category = tmp;
2336                 }
2337         }
2338         /*Free Metadata*/
2339         if (serviceapplication->metadata) {
2340                 metadata_x *metadata = serviceapplication->metadata;
2341                 metadata_x *tmp = NULL;
2342                 while(metadata != NULL) {
2343                         tmp = metadata->next;
2344                         __ps_free_metadata(metadata);
2345                         metadata = tmp;
2346                 }
2347         }
2348         /*Free permission*/
2349         if (serviceapplication->permission) {
2350                 permission_x *permission = serviceapplication->permission;
2351                 permission_x *tmp = NULL;
2352                 while(permission != NULL) {
2353                         tmp = permission->next;
2354                         __ps_free_permission(permission);
2355                         permission = tmp;
2356                 }
2357         }
2358         free((void*)serviceapplication);
2359         serviceapplication = NULL;
2360 }
2361
2362 static void __ps_free_font(font_x *font)
2363 {
2364         if (font == NULL)
2365                 return;
2366         if (font->name) {
2367                 free((void *)font->name);
2368                 font->name = NULL;
2369         }
2370         if (font->text) {
2371                 free((void *)font->text);
2372                 font->text = NULL;
2373         }
2374         free((void*)font);
2375         font = NULL;
2376 }
2377
2378 static void __ps_free_theme(theme_x *theme)
2379 {
2380         if (theme == NULL)
2381                 return;
2382         if (theme->name) {
2383                 free((void *)theme->name);
2384                 theme->name = NULL;
2385         }
2386         if (theme->text) {
2387                 free((void *)theme->text);
2388                 theme->text = NULL;
2389         }
2390         free((void*)theme);
2391         theme = NULL;
2392 }
2393
2394 static void __ps_free_daemon(daemon_x *daemon)
2395 {
2396         if (daemon == NULL)
2397                 return;
2398         if (daemon->name) {
2399                 free((void *)daemon->name);
2400                 daemon->name = NULL;
2401         }
2402         if (daemon->text) {
2403                 free((void *)daemon->text);
2404                 daemon->text = NULL;
2405         }
2406         free((void*)daemon);
2407         daemon = NULL;
2408 }
2409
2410 static void __ps_free_ime(ime_x *ime)
2411 {
2412         if (ime == NULL)
2413                 return;
2414         if (ime->name) {
2415                 free((void *)ime->name);
2416                 ime->name = NULL;
2417         }
2418         if (ime->text) {
2419                 free((void *)ime->text);
2420                 ime->text = NULL;
2421         }
2422         free((void*)ime);
2423         ime = NULL;
2424 }
2425
2426 int __ps_process_tag_parser(manifest_x *mfx, const char *filename, ACTION_TYPE action)
2427 {
2428         xmlTextReaderPtr reader;
2429         xmlDocPtr docPtr;
2430         int ret = -1;
2431         FILE *fp = NULL;
2432         void *lib_handle = NULL;
2433         char tag[PKG_STRING_LEN_MAX] = { 0 };
2434
2435         fp = fopen(TAG_PARSER_LIST, "r");
2436         retvm_if(fp == NULL, PMINFO_R_ERROR, "no preload list");
2437
2438         while (fgets(tag, sizeof(tag), fp) != NULL) {
2439                 __str_trim(tag);
2440
2441                 lib_handle = __open_lib_handle(tag);
2442                 if (lib_handle == NULL)
2443                         continue;
2444
2445                 ret = __parser_send_tag(lib_handle, action, PLUGIN_PRE_PROCESS, mfx->package);
2446                 _LOGD("PLUGIN_PRE_PROCESS[%s, %s] ACTION_TYPE[%d] result[%d]\n", mfx->package, tag, action, ret);
2447
2448                 docPtr = xmlReadFile(filename, NULL, 0);
2449                 reader = xmlReaderWalker(docPtr);
2450                 if (reader != NULL) {
2451                         ret = xmlTextReaderRead(reader);
2452                         while (ret == 1) {
2453                                 __processTag(lib_handle, reader, action, tag, mfx->package);
2454                                 ret = xmlTextReaderRead(reader);
2455                         }
2456                         xmlFreeTextReader(reader);
2457
2458                         if (ret != 0) {
2459                                 _LOGD("%s : failed to parse", filename);
2460                         }
2461                 } else {
2462                         _LOGD("Unable to open %s", filename);
2463                 }
2464
2465                 ret = __parser_send_tag(lib_handle, action, PLUGIN_POST_PROCESS, mfx->package);
2466                 _LOGD("PLUGIN_POST_PROCESS[%s, %s] ACTION_TYPE[%d] result[%d]\n", mfx->package, tag, action, ret);
2467
2468                 __close_lib_handle(lib_handle);
2469
2470                 memset(tag, 0x00, sizeof(tag));
2471         }
2472
2473         if (fp != NULL)
2474                 fclose(fp);
2475
2476         return 0;
2477 }
2478
2479 int __ps_process_metadata_parser(manifest_x *mfx, ACTION_TYPE action)
2480 {
2481         fprintf(stdout,"__ps_process_metadata_parser\n");
2482         int ret = -1;
2483         FILE *fp = NULL;
2484         char md_key[PKG_STRING_LEN_MAX] = { 0 };
2485
2486         fp = fopen(METADATA_PARSER_LIST, "r");
2487         if (fp == NULL) {
2488                 _LOGD("no preload list\n");
2489                 return -1;
2490         }
2491         
2492         while (fgets(md_key, sizeof(md_key), fp) != NULL) {
2493                 __str_trim(md_key);
2494                 ret = __run_metadata_parser_prestep(mfx, md_key, action);
2495
2496                 memset(md_key, 0x00, sizeof(md_key));
2497         }
2498
2499         if (fp != NULL)
2500                 fclose(fp);
2501
2502         return 0;
2503 }
2504
2505 int __ps_process_category_parser(manifest_x *mfx, ACTION_TYPE action)
2506 {
2507         int ret = -1;
2508         FILE *fp = NULL;
2509         char category_key[PKG_STRING_LEN_MAX] = { 0 };
2510
2511         fp = fopen(CATEGORY_PARSER_LIST, "r");
2512         if (fp == NULL) {
2513                 _LOGD("no category parser list\n");
2514                 return -1;
2515         }
2516
2517         while (fgets(category_key, sizeof(category_key), fp) != NULL) {
2518                 __str_trim(category_key);
2519                 ret = __run_category_parser_prestep(mfx, category_key, action);
2520
2521                 memset(category_key, 0x00, sizeof(category_key));
2522         }
2523
2524         if (fp != NULL)
2525                 fclose(fp);
2526
2527         return 0;
2528 }
2529
2530 static int __ps_process_allowed(xmlTextReaderPtr reader, allowed_x *allowed)
2531 {
2532         xmlTextReaderRead(reader);
2533         if (xmlTextReaderValue(reader))
2534                 allowed->text = ASCII(xmlTextReaderValue(reader));
2535         return 0;
2536 }
2537
2538 static int __ps_process_operation(xmlTextReaderPtr reader, operation_x *operation)
2539 {
2540         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
2541                 operation->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
2542 /* Text does not exist. Only attribute exists
2543         xmlTextReaderRead(reader);
2544         if (xmlTextReaderValue(reader))
2545                 operation->text = ASCII(xmlTextReaderValue(reader));
2546 */
2547         return 0;
2548 }
2549
2550 static int __ps_process_uri(xmlTextReaderPtr reader, uri_x *uri)
2551 {
2552         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
2553                 uri->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
2554 /* Text does not exist. Only attribute exists
2555         xmlTextReaderRead(reader);
2556         if (xmlTextReaderValue(reader))
2557                 uri->text = ASCII(xmlTextReaderValue(reader));
2558 */
2559         return 0;
2560 }
2561
2562 static int __ps_process_mime(xmlTextReaderPtr reader, mime_x *mime)
2563 {
2564         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
2565                 mime->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
2566 /* Text does not exist. Only attribute exists
2567         xmlTextReaderRead(reader);
2568         if (xmlTextReaderValue(reader))
2569                 mime->text = ASCII(xmlTextReaderValue(reader));
2570 */
2571         return 0;
2572 }
2573
2574 static int __ps_process_subapp(xmlTextReaderPtr reader, subapp_x *subapp)
2575 {
2576         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
2577                 subapp->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
2578 /* Text does not exist. Only attribute exists
2579         xmlTextReaderRead(reader);
2580         if (xmlTextReaderValue(reader))
2581                 mime->text = ASCII(xmlTextReaderValue(reader));
2582 */
2583         return 0;
2584 }
2585
2586 static int __ps_process_condition(xmlTextReaderPtr reader, condition_x *condition)
2587 {
2588         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
2589                 condition->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
2590         xmlTextReaderRead(reader);
2591         if (xmlTextReaderValue(reader))
2592                 condition->text = ASCII(xmlTextReaderValue(reader));
2593         return 0;
2594 }
2595
2596 static int __ps_process_notification(xmlTextReaderPtr reader, notification_x *notification)
2597 {
2598         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
2599                 notification->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
2600         xmlTextReaderRead(reader);
2601         if (xmlTextReaderValue(reader))
2602                 notification->text = ASCII(xmlTextReaderValue(reader));
2603         return 0;
2604 }
2605
2606 static int __ps_process_category(xmlTextReaderPtr reader, category_x *category)
2607 {
2608         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
2609                 category->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
2610         return 0;
2611 }
2612
2613 static int __ps_process_privilege(xmlTextReaderPtr reader, privilege_x *privilege)
2614 {
2615         xmlTextReaderRead(reader);
2616         if (xmlTextReaderValue(reader)) {
2617                 privilege->text = ASCII(xmlTextReaderValue(reader));
2618         }
2619         return 0;
2620 }
2621
2622 static int __ps_process_metadata(xmlTextReaderPtr reader, metadata_x *metadata)
2623 {
2624         if (xmlTextReaderGetAttribute(reader, XMLCHAR("key")))
2625                 metadata->key = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("key")));
2626         if (xmlTextReaderGetAttribute(reader, XMLCHAR("value")))
2627                 metadata->value = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("value")));
2628         return 0;
2629 }
2630
2631 static int __ps_process_permission(xmlTextReaderPtr reader, permission_x *permission)
2632 {
2633         if (xmlTextReaderGetAttribute(reader, XMLCHAR("type")))
2634                 permission->type = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("type")));
2635
2636         xmlTextReaderRead(reader);
2637         if (xmlTextReaderValue(reader))
2638                 permission->value = ASCII(xmlTextReaderValue(reader));
2639         return 0;
2640 }
2641
2642 static int __ps_process_compatibility(xmlTextReaderPtr reader, compatibility_x *compatibility)
2643 {
2644         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
2645                 compatibility->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
2646         xmlTextReaderRead(reader);
2647         if (xmlTextReaderValue(reader))
2648                 compatibility->text = ASCII(xmlTextReaderValue(reader));
2649         return 0;
2650 }
2651
2652 static int __ps_process_resolution(xmlTextReaderPtr reader, resolution_x *resolution)
2653 {
2654         if (xmlTextReaderGetAttribute(reader, XMLCHAR("mime-type")))
2655                 resolution->mimetype = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("mime-type")));
2656         if (xmlTextReaderGetAttribute(reader, XMLCHAR("uri-scheme")))
2657                 resolution->urischeme = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("uri-scheme")));
2658         return 0;
2659 }
2660
2661 static int __ps_process_request(xmlTextReaderPtr reader, request_x *request)
2662 {
2663         xmlTextReaderRead(reader);
2664         if (xmlTextReaderValue(reader))
2665                 request->text = ASCII(xmlTextReaderValue(reader));
2666         return 0;
2667 }
2668
2669 static int __ps_process_define(xmlTextReaderPtr reader, define_x *define)
2670 {
2671         const xmlChar *node;
2672         int ret = -1;
2673         int depth = -1;
2674         allowed_x *tmp1 = NULL;
2675         request_x *tmp2 = NULL;
2676
2677         if (xmlTextReaderGetAttribute(reader, XMLCHAR("path")))
2678                 define->path = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("path")));
2679
2680         depth = xmlTextReaderDepth(reader);
2681         while ((ret = __next_child_element(reader, depth))) {
2682                 node = xmlTextReaderConstName(reader);
2683                 if (!node) {
2684                         _LOGD("xmlTextReaderConstName value is NULL\n");
2685                         return -1;
2686                 }
2687
2688                 if (!strcmp(ASCII(node), "allowed")) {
2689                         allowed_x *allowed= malloc(sizeof(allowed_x));
2690                         if (allowed == NULL) {
2691                                 _LOGD("Malloc Failed\n");
2692                                 return -1;
2693                         }
2694                         memset(allowed, '\0', sizeof(allowed_x));
2695                         LISTADD(define->allowed, allowed);
2696                         ret = __ps_process_allowed(reader, allowed);
2697                 } else if (!strcmp(ASCII(node), "request")) {
2698                         request_x *request = malloc(sizeof(request_x));
2699                         if (request == NULL) {
2700                                 _LOGD("Malloc Failed\n");
2701                                 return -1;
2702                         }
2703                         memset(request, '\0', sizeof(request_x));
2704                         LISTADD(define->request, request);
2705                         ret = __ps_process_request(reader, request);
2706                 } else
2707                         return -1;
2708                 if (ret < 0) {
2709                         _LOGD("Processing define failed\n");
2710                         return ret;
2711                 }
2712         }
2713         if (define->allowed) {
2714                 LISTHEAD(define->allowed, tmp1);
2715                 define->allowed = tmp1;
2716         }
2717         if (define->request) {
2718                 LISTHEAD(define->request, tmp2);
2719                 define->request = tmp2;
2720         }
2721         return ret;
2722 }
2723
2724 static int __ps_process_appcontrol(xmlTextReaderPtr reader, appcontrol_x *appcontrol)
2725 {
2726         const xmlChar *node;
2727         int ret = -1;
2728         int depth = -1;
2729         operation_x *tmp1 = NULL;
2730         uri_x *tmp2 = NULL;
2731         mime_x *tmp3 = NULL;
2732         subapp_x *tmp4 = NULL;
2733
2734         depth = xmlTextReaderDepth(reader);
2735         while ((ret = __next_child_element(reader, depth))) {
2736                 node = xmlTextReaderConstName(reader);
2737                 if (!node) {
2738                         _LOGD("xmlTextReaderConstName value is NULL\n");
2739                         return -1;
2740                 }
2741
2742                 if (!strcmp(ASCII(node), "operation")) {
2743                         operation_x *operation = malloc(sizeof(operation_x));
2744                         if (operation == NULL) {
2745                                 _LOGD("Malloc Failed\n");
2746                                 return -1;
2747                         }
2748                         memset(operation, '\0', sizeof(operation_x));
2749                         LISTADD(appcontrol->operation, operation);
2750                         ret = __ps_process_operation(reader, operation);
2751                         _LOGD("operation processing\n");
2752                 } else if (!strcmp(ASCII(node), "uri")) {
2753                         uri_x *uri= malloc(sizeof(uri_x));
2754                         if (uri == NULL) {
2755                                 _LOGD("Malloc Failed\n");
2756                                 return -1;
2757                         }
2758                         memset(uri, '\0', sizeof(uri_x));
2759                         LISTADD(appcontrol->uri, uri);
2760                         ret = __ps_process_uri(reader, uri);
2761                         _LOGD("uri processing\n");
2762                 } else if (!strcmp(ASCII(node), "mime")) {
2763                         mime_x *mime = malloc(sizeof(mime_x));
2764                         if (mime == NULL) {
2765                                 _LOGD("Malloc Failed\n");
2766                                 return -1;
2767                         }
2768                         memset(mime, '\0', sizeof(mime_x));
2769                         LISTADD(appcontrol->mime, mime);
2770                         ret = __ps_process_mime(reader, mime);
2771                         _LOGD("mime processing\n");
2772                 } else if (!strcmp(ASCII(node), "subapp")) {
2773                         subapp_x *subapp = malloc(sizeof(subapp_x));
2774                         if (subapp == NULL) {
2775                                 _LOGD("Malloc Failed\n");
2776                                 return -1;
2777                         }
2778                         memset(subapp, '\0', sizeof(subapp_x));
2779                         LISTADD(appcontrol->subapp, subapp);
2780                         ret = __ps_process_subapp(reader, subapp);
2781                         _LOGD("subapp processing\n");
2782                 } else
2783                         return -1;
2784                 if (ret < 0) {
2785                         _LOGD("Processing appcontrol failed\n");
2786                         return ret;
2787                 }
2788         }
2789         if (appcontrol->operation) {
2790                 LISTHEAD(appcontrol->operation, tmp1);
2791                 appcontrol->operation = tmp1;
2792         }
2793         if (appcontrol->uri) {
2794                 LISTHEAD(appcontrol->uri, tmp2);
2795                 appcontrol->uri = tmp2;
2796         }
2797         if (appcontrol->mime) {
2798                 LISTHEAD(appcontrol->mime, tmp3);
2799                 appcontrol->mime = tmp3;
2800         }
2801         if (appcontrol->subapp) {
2802                 LISTHEAD(appcontrol->subapp, tmp4);
2803                 appcontrol->subapp = tmp4;
2804         }
2805
2806         xmlTextReaderRead(reader);
2807         if (xmlTextReaderValue(reader))
2808                 appcontrol->text = ASCII(xmlTextReaderValue(reader));
2809
2810         return ret;
2811 }
2812
2813 static int __ps_process_appsvc(xmlTextReaderPtr reader, appsvc_x *appsvc)
2814 {
2815         const xmlChar *node;
2816         int ret = -1;
2817         int depth = -1;
2818         operation_x *tmp1 = NULL;
2819         uri_x *tmp2 = NULL;
2820         mime_x *tmp3 = NULL;
2821         subapp_x *tmp4 = NULL;
2822
2823         depth = xmlTextReaderDepth(reader);
2824         while ((ret = __next_child_element(reader, depth))) {
2825                 node = xmlTextReaderConstName(reader);
2826                 if (!node) {
2827                         _LOGD("xmlTextReaderConstName value is NULL\n");
2828                         return -1;
2829                 }
2830
2831                 if (!strcmp(ASCII(node), "operation")) {
2832                         operation_x *operation = malloc(sizeof(operation_x));
2833                         if (operation == NULL) {
2834                                 _LOGD("Malloc Failed\n");
2835                                 return -1;
2836                         }
2837                         memset(operation, '\0', sizeof(operation_x));
2838                         LISTADD(appsvc->operation, operation);
2839                         ret = __ps_process_operation(reader, operation);
2840                         _LOGD("operation processing\n");
2841                 } else if (!strcmp(ASCII(node), "uri")) {
2842                         uri_x *uri= malloc(sizeof(uri_x));
2843                         if (uri == NULL) {
2844                                 _LOGD("Malloc Failed\n");
2845                                 return -1;
2846                         }
2847                         memset(uri, '\0', sizeof(uri_x));
2848                         LISTADD(appsvc->uri, uri);
2849                         ret = __ps_process_uri(reader, uri);
2850                         _LOGD("uri processing\n");
2851                 } else if (!strcmp(ASCII(node), "mime")) {
2852                         mime_x *mime = malloc(sizeof(mime_x));
2853                         if (mime == NULL) {
2854                                 _LOGD("Malloc Failed\n");
2855                                 return -1;
2856                         }
2857                         memset(mime, '\0', sizeof(mime_x));
2858                         LISTADD(appsvc->mime, mime);
2859                         ret = __ps_process_mime(reader, mime);
2860                         _LOGD("mime processing\n");
2861                 } else if (!strcmp(ASCII(node), "subapp")) {
2862                         subapp_x *subapp = malloc(sizeof(subapp_x));
2863                         if (subapp == NULL) {
2864                                 _LOGD("Malloc Failed\n");
2865                                 return -1;
2866                         }
2867                         memset(subapp, '\0', sizeof(subapp_x));
2868                         LISTADD(appsvc->subapp, subapp);
2869                         ret = __ps_process_subapp(reader, subapp);
2870                         _LOGD("subapp processing\n");
2871                 } else
2872                         return -1;
2873                 if (ret < 0) {
2874                         _LOGD("Processing appsvc failed\n");
2875                         return ret;
2876                 }
2877         }
2878         if (appsvc->operation) {
2879                 LISTHEAD(appsvc->operation, tmp1);
2880                 appsvc->operation = tmp1;
2881         }
2882         if (appsvc->uri) {
2883                 LISTHEAD(appsvc->uri, tmp2);
2884                 appsvc->uri = tmp2;
2885         }
2886         if (appsvc->mime) {
2887                 LISTHEAD(appsvc->mime, tmp3);
2888                 appsvc->mime = tmp3;
2889         }
2890         if (appsvc->subapp) {
2891                 LISTHEAD(appsvc->subapp, tmp4);
2892                 appsvc->subapp = tmp4;
2893         }
2894
2895         xmlTextReaderRead(reader);
2896         if (xmlTextReaderValue(reader))
2897                 appsvc->text = ASCII(xmlTextReaderValue(reader));
2898
2899         return ret;
2900 }
2901
2902
2903 static int __ps_process_privileges(xmlTextReaderPtr reader, privileges_x *privileges)
2904 {
2905         const xmlChar *node;
2906         int ret = -1;
2907         int depth = -1;
2908         privilege_x *tmp1 = NULL;
2909
2910         depth = xmlTextReaderDepth(reader);
2911         while ((ret = __next_child_element(reader, depth))) {
2912                 node = xmlTextReaderConstName(reader);
2913                 if (!node) {
2914                         _LOGD("xmlTextReaderConstName value is NULL\n");
2915                         return -1;
2916                 }
2917
2918                 if (strcmp(ASCII(node), "privilege") == 0) {
2919                         privilege_x *privilege = malloc(sizeof(privilege_x));
2920                         if (privilege == NULL) {
2921                                 _LOGD("Malloc Failed\n");
2922                                 return -1;
2923                         }
2924                         memset(privilege, '\0', sizeof(privilege_x));
2925                         LISTADD(privileges->privilege, privilege);
2926                         ret = __ps_process_privilege(reader, privilege);
2927                 } else
2928                         return -1;
2929                 if (ret < 0) {
2930                         _LOGD("Processing privileges failed\n");
2931                         return ret;
2932                 }
2933         }
2934         if (privileges->privilege) {
2935                 LISTHEAD(privileges->privilege, tmp1);
2936                 privileges->privilege = tmp1;
2937         }
2938         return ret;
2939 }
2940
2941 static int __ps_process_launchconditions(xmlTextReaderPtr reader, launchconditions_x *launchconditions)
2942 {
2943         const xmlChar *node;
2944         int ret = -1;
2945         int depth = -1;
2946         condition_x *tmp1 = NULL;
2947
2948         depth = xmlTextReaderDepth(reader);
2949         while ((ret = __next_child_element(reader, depth))) {
2950                 node = xmlTextReaderConstName(reader);
2951                 if (!node) {
2952                         _LOGD("xmlTextReaderConstName value is NULL\n");
2953                         return -1;
2954                 }
2955
2956                 if (strcmp(ASCII(node), "condition") == 0) {
2957                         condition_x *condition = malloc(sizeof(condition_x));
2958                         if (condition == NULL) {
2959                                 _LOGD("Malloc Failed\n");
2960                                 return -1;
2961                         }
2962                         memset(condition, '\0', sizeof(condition_x));
2963                         LISTADD(launchconditions->condition, condition);
2964                         ret = __ps_process_condition(reader, condition);
2965                 } else
2966                         return -1;
2967                 if (ret < 0) {
2968                         _LOGD("Processing launchconditions failed\n");
2969                         return ret;
2970                 }
2971         }
2972         if (launchconditions->condition) {
2973                 LISTHEAD(launchconditions->condition, tmp1);
2974                 launchconditions->condition = tmp1;
2975         }
2976
2977         xmlTextReaderRead(reader);
2978         if (xmlTextReaderValue(reader))
2979                 launchconditions->text = ASCII(xmlTextReaderValue(reader));
2980
2981         return ret;
2982 }
2983
2984 static int __ps_process_datashare(xmlTextReaderPtr reader, datashare_x *datashare)
2985 {
2986         const xmlChar *node;
2987         int ret = -1;
2988         int depth = -1;
2989         define_x *tmp1 = NULL;
2990         request_x *tmp2 = NULL;
2991         depth = xmlTextReaderDepth(reader);
2992         while ((ret = __next_child_element(reader, depth))) {
2993                 node = xmlTextReaderConstName(reader);
2994                 if (!node) {
2995                         _LOGD("xmlTextReaderConstName value is NULL\n");
2996                         return -1;
2997                 }
2998
2999                 if (!strcmp(ASCII(node), "define")) {
3000                         define_x *define= malloc(sizeof(define_x));
3001                         if (define == NULL) {
3002                                 _LOGD("Malloc Failed\n");
3003                                 return -1;
3004                         }
3005                         memset(define, '\0', sizeof(define_x));
3006                         LISTADD(datashare->define, define);
3007                         ret = __ps_process_define(reader, define);
3008                 } else if (!strcmp(ASCII(node), "request")) {
3009                         request_x *request= malloc(sizeof(request_x));
3010                         if (request == NULL) {
3011                                 _LOGD("Malloc Failed\n");
3012                                 return -1;
3013                         }
3014                         memset(request, '\0', sizeof(request_x));
3015                         LISTADD(datashare->request, request);
3016                         ret = __ps_process_request(reader, request);
3017                 } else
3018                         return -1;
3019                 if (ret < 0) {
3020                         _LOGD("Processing data-share failed\n");
3021                         return ret;
3022                 }
3023         }
3024         if (datashare->define) {
3025                 LISTHEAD(datashare->define, tmp1);
3026                 datashare->define = tmp1;
3027         }
3028         if (datashare->request) {
3029                 LISTHEAD(datashare->request, tmp2);
3030                 datashare->request = tmp2;
3031         }
3032         return ret;
3033 }
3034
3035 static char*
3036 __get_icon_with_path(const char* icon, uid_t uid)
3037 {
3038         if (!icon)
3039                 return NULL;
3040
3041         if (index(icon, '/') == NULL) {
3042                 char* theme = NULL;
3043                 char* iconPath = NULL;
3044                 char* icon_with_path = NULL;
3045                 int len;
3046
3047                 if (!package)
3048                         return NULL;
3049
3050 /* "db/setting/theme" is not exist */
3051 #if 0
3052                 theme = vconf_get_str("db/setting/theme");
3053                 if (!theme) {
3054                         theme = strdup("default");
3055                         if(!theme) {
3056                                 return NULL;
3057                         }
3058                 }
3059 #else
3060                 theme = strdup("default");
3061 #endif
3062
3063                 len = (0x01 << 7) + strlen(icon) + strlen(package) + strlen(theme);
3064                 icon_with_path = malloc(len);
3065                 if(icon_with_path == NULL) {
3066                         _LOGD("(icon_with_path == NULL) return\n");
3067                         free(theme);
3068                         return NULL;
3069                 }
3070
3071                 memset(icon_with_path, 0, len);
3072                 if (uid != GLOBAL_USER)
3073                         snprintf(icon_with_path, len, "%s%s", getIconPath(uid), icon);
3074                 else {
3075                         snprintf(icon_with_path, len, "%s%s/small/%s", getIconPath(GLOBAL_USER), theme, icon);
3076                         if (!access (icon_with_path, F_OK))
3077                                 snprintf( len, icon_with_path, "%s/%q/res/icons/%q/small/%q", tzplatform_getenv(TZ_SYS_RO_APP), package, theme, icon);
3078                         else if (!access (icon_with_path, F_OK))
3079                                 snprintf( len, icon_with_path, "%s/%q/res/icons/%q/small/%q", tzplatform_getenv(TZ_SYS_RW_APP), package, theme, icon);
3080                         }
3081                         if (!access (icon_with_path, F_OK))
3082                                 _LOGD("Cannot find icon path");
3083
3084                         free(theme);
3085
3086                         _LOGD("Icon path : %s ---> %s", icon, icon_with_path);
3087
3088                         return icon_with_path;
3089         } else {
3090                 char* confirmed_icon = NULL;
3091
3092                 confirmed_icon = strdup(icon);
3093                 if (!confirmed_icon)
3094                         return NULL;
3095                 return confirmed_icon;
3096         }
3097 }
3098
3099 static void __ps_process_tag(manifest_x * mfx, char *const tagv[])
3100 {
3101         int i = 0;
3102         char delims[] = "=";
3103         char *ret_result = NULL;
3104         char *tag = NULL;
3105
3106         if (tagv == NULL)
3107                 return;
3108
3109         for (tag = strdup(tagv[0]); tag != NULL; ) {
3110                 ret_result = strtok(tag, delims);
3111
3112                 /*check tag :  preload */
3113                 if (strcmp(ret_result, "preload") == 0) {
3114                         ret_result = strtok(NULL, delims);
3115                         if (strcmp(ret_result, "true") == 0) {
3116                                 free((void *)mfx->preload);
3117                                 mfx->preload = strdup("true");
3118                         } else if (strcmp(ret_result, "false") == 0) {
3119                                 free((void *)mfx->preload);
3120                                 mfx->preload = strdup("false");
3121                         }
3122                 /*check tag :  removable*/
3123                 } else if (strcmp(ret_result, "removable") == 0) {
3124                         ret_result = strtok(NULL, delims);
3125                         if (strcmp(ret_result, "true") == 0){
3126                                 free((void *)mfx->removable);
3127                                 mfx->removable = strdup("true");
3128                         } else if (strcmp(ret_result, "false") == 0) {
3129                                 free((void *)mfx->removable);
3130                                 mfx->removable = strdup("false");
3131                         }
3132                 /*check tag :  not matched*/
3133                 } else
3134                         _LOGD("tag process [%s]is not defined\n", ret_result);
3135
3136                 free(tag);
3137
3138                 /*check next value*/
3139                 if (tagv[++i] != NULL)
3140                         tag = strdup(tagv[i]);
3141                 else {
3142                         _LOGD("tag process success...\n");
3143                         return;
3144                 }
3145         }
3146 }
3147
3148 static int __ps_process_icon(xmlTextReaderPtr reader, icon_x *icon, uid_t uid)
3149 {
3150         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
3151                 icon->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
3152         if (xmlTextReaderConstXmlLang(reader)) {
3153                 icon->lang = strdup(ASCII(xmlTextReaderConstXmlLang(reader)));
3154                 if (icon->lang == NULL)
3155                         icon->lang = strdup(DEFAULT_LOCALE);
3156         } else {
3157                 icon->lang = strdup(DEFAULT_LOCALE);
3158         }
3159         if (xmlTextReaderGetAttribute(reader, XMLCHAR("section")))
3160                 icon->section = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("section")));
3161         if (xmlTextReaderGetAttribute(reader, XMLCHAR("size")))
3162                 icon->size = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("size")));
3163         if (xmlTextReaderGetAttribute(reader, XMLCHAR("resolution")))
3164                 icon->resolution = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("resolution")));
3165         xmlTextReaderRead(reader);
3166         if (xmlTextReaderValue(reader)) {
3167                 const char *text  = ASCII(xmlTextReaderValue(reader));
3168                 if(text) {
3169                         icon->text = (const char *)__get_icon_with_path(text, uid);
3170                         free((void *)text);
3171                 }
3172         }
3173
3174         return 0;
3175 }
3176
3177 static int __ps_process_image(xmlTextReaderPtr reader, image_x *image)
3178 {
3179         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
3180                 image->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
3181         if (xmlTextReaderConstXmlLang(reader)) {
3182                 image->lang = strdup(ASCII(xmlTextReaderConstXmlLang(reader)));
3183                 if (image->lang == NULL)
3184                         image->lang = strdup(DEFAULT_LOCALE);
3185         } else {
3186                 image->lang = strdup(DEFAULT_LOCALE);
3187         }
3188         if (xmlTextReaderGetAttribute(reader, XMLCHAR("section")))
3189                 image->section = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("section")));
3190         xmlTextReaderRead(reader);
3191         if (xmlTextReaderValue(reader))
3192                 image->text = ASCII(xmlTextReaderValue(reader));
3193
3194         return 0;
3195 }
3196
3197 static int __ps_process_label(xmlTextReaderPtr reader, label_x *label)
3198 {
3199         if (xmlTextReaderGetAttribute(reader, XMLCHAR("name")))
3200                 label->name = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("name")));
3201         if (xmlTextReaderConstXmlLang(reader)) {
3202                 label->lang = strdup(ASCII(xmlTextReaderConstXmlLang(reader)));
3203                 if (label->lang == NULL)
3204                         label->lang = strdup(DEFAULT_LOCALE);
3205         } else 
3206                 label->lang = strdup(DEFAULT_LOCALE);
3207         xmlTextReaderRead(reader);
3208         if (xmlTextReaderValue(reader))
3209                 label->text = ASCII(xmlTextReaderValue(reader));
3210
3211
3212 /*      _LOGD("lable name %s\n", label->name);
3213         _LOGD("lable lang %s\n", label->lang);
3214         _LOGD("lable text %s\n", label->text);
3215 */
3216         return 0;
3217
3218 }
3219
3220 static int __ps_process_author(xmlTextReaderPtr reader, author_x *author)
3221 {
3222         if (xmlTextReaderGetAttribute(reader, XMLCHAR("email")))
3223                 author->email = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("email")));
3224         if (xmlTextReaderGetAttribute(reader, XMLCHAR("href")))
3225                 author->href = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("href")));
3226         if (xmlTextReaderConstXmlLang(reader)) {
3227                 author->lang = strdup(ASCII(xmlTextReaderConstXmlLang(reader)));
3228                 if (author->lang == NULL)
3229                         author->lang = strdup(DEFAULT_LOCALE);
3230         } else {
3231                 author->lang = strdup(DEFAULT_LOCALE);
3232         }
3233         xmlTextReaderRead(reader);
3234         if (xmlTextReaderValue(reader)) {
3235                 const char *text  = ASCII(xmlTextReaderValue(reader));
3236                 if (*text == '\n') {
3237                         author->text = NULL;
3238                         free((void *)text);
3239                         return 0;
3240                 }
3241                 author->text = ASCII(xmlTextReaderValue(reader));
3242         }
3243         return 0;
3244 }
3245
3246 static int __ps_process_description(xmlTextReaderPtr reader, description_x *description)
3247 {
3248         if (xmlTextReaderConstXmlLang(reader)) {
3249                 description->lang = strdup(ASCII(xmlTextReaderConstXmlLang(reader)));
3250                 if (description->lang == NULL)
3251                         description->lang = strdup(DEFAULT_LOCALE);
3252         } else {
3253                 description->lang = strdup(DEFAULT_LOCALE);
3254         }
3255         xmlTextReaderRead(reader);
3256         if (xmlTextReaderValue(reader)) {
3257                 const char *text  = ASCII(xmlTextReaderValue(reader));
3258                 if (*text == '\n') {
3259                         description->text = NULL;
3260                         free((void *)text);
3261                         return 0;
3262                 }
3263                 description->text = ASCII(xmlTextReaderValue(reader));
3264         }
3265         return 0;
3266 }
3267
3268 static int __ps_process_license(xmlTextReaderPtr reader, license_x *license)
3269 {
3270         if (xmlTextReaderConstXmlLang(reader)) {
3271                 license->lang = strdup(ASCII(xmlTextReaderConstXmlLang(reader)));
3272                 if (license->lang == NULL)
3273                         license->lang = strdup(DEFAULT_LOCALE);
3274         } else {
3275                 license->lang = strdup(DEFAULT_LOCALE);
3276         }
3277         xmlTextReaderRead(reader);
3278         if (xmlTextReaderValue(reader))
3279                 license->text = ASCII(xmlTextReaderValue(reader));
3280         return 0;
3281 }
3282
3283 static int __ps_process_capability(xmlTextReaderPtr reader, capability_x *capability)
3284 {
3285         const xmlChar *node;
3286         int ret = -1;
3287         int depth = -1;
3288         resolution_x *tmp1 = NULL;
3289
3290         if (xmlTextReaderGetAttribute(reader, XMLCHAR("operation-id")))
3291                 capability->operationid = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("operation-id")));
3292
3293         depth = xmlTextReaderDepth(reader);
3294         while ((ret = __next_child_element(reader, depth))) {
3295                 node = xmlTextReaderConstName(reader);
3296                 if (!node) {
3297                         _LOGD("xmlTextReaderConstName value is NULL\n");
3298                         return -1;
3299                 }
3300
3301                 if (!strcmp(ASCII(node), "resolution")) {
3302                         resolution_x *resolution = malloc(sizeof(resolution_x));
3303                         if (resolution == NULL) {
3304                                 _LOGD("Malloc Failed\n");
3305                                 return -1;
3306                         }
3307                         memset(resolution, '\0', sizeof(resolution_x));
3308                         LISTADD(capability->resolution, resolution);
3309                         ret = __ps_process_resolution(reader, resolution);
3310                 } else
3311                         return -1;
3312                 if (ret < 0) {
3313                         _LOGD("Processing capability failed\n");
3314                         return ret;
3315                 }
3316         }
3317
3318         if (capability->resolution) {
3319                 LISTHEAD(capability->resolution, tmp1);
3320                 capability->resolution = tmp1;
3321         }
3322
3323         return ret;
3324 }
3325
3326 static int __ps_process_datacontrol(xmlTextReaderPtr reader, datacontrol_x *datacontrol)
3327 {
3328         const xmlChar *node;
3329         int ret = -1;
3330         int depth = -1;
3331         capability_x *tmp1 = NULL;
3332
3333         if (xmlTextReaderGetAttribute(reader, XMLCHAR("provider-id")))
3334                 datacontrol->providerid = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("provider-id")));
3335
3336         depth = xmlTextReaderDepth(reader);
3337         while ((ret = __next_child_element(reader, depth))) {
3338                 node = xmlTextReaderConstName(reader);
3339                 if (!node) {
3340                         _LOGD("xmlTextReaderConstName value is NULL\n");
3341                         return -1;
3342                 }
3343
3344                 if (!strcmp(ASCII(node), "capability")) {
3345                         capability_x *capability = malloc(sizeof(capability_x));
3346                         if (capability == NULL) {
3347                                 _LOGD("Malloc Failed\n");
3348                                 return -1;
3349                         }
3350                         memset(capability, '\0', sizeof(capability_x));
3351                         LISTADD(datacontrol->capability, capability);
3352                         ret = __ps_process_capability(reader, capability);
3353                 } else
3354                         return -1;
3355                 if (ret < 0) {
3356                         _LOGD("Processing datacontrol failed\n");
3357                         return ret;
3358                 }
3359         }
3360
3361         if (datacontrol->capability) {
3362                 LISTHEAD(datacontrol->capability, tmp1);
3363                 datacontrol->capability = tmp1;
3364         }
3365
3366         return ret;
3367 }
3368
3369 static int __ps_process_uiapplication(xmlTextReaderPtr reader, uiapplication_x *uiapplication, uid_t uid)
3370 {
3371         const xmlChar *node;
3372         int ret = -1;
3373         int depth = -1;
3374         char *newappid = NULL;
3375         label_x *tmp1 = NULL;
3376         icon_x *tmp2 = NULL;
3377         appsvc_x *tmp3 = NULL;
3378         appcontrol_x *tmp4 = NULL;
3379         launchconditions_x *tmp5 = NULL;
3380         notification_x *tmp6 = NULL;
3381         datashare_x *tmp7 = NULL;
3382         category_x *tmp8 = NULL;
3383         metadata_x *tmp9 = NULL;
3384         image_x *tmp10 = NULL;
3385         permission_x *tmp11 = NULL;
3386
3387         if (xmlTextReaderGetAttribute(reader, XMLCHAR("appid"))) {
3388                 uiapplication->appid = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("appid")));
3389                 if (uiapplication->appid == NULL) {
3390                         _LOGD("appid cant be NULL\n");
3391                         return -1;
3392                 }
3393         } else {
3394                 _LOGD("appid is mandatory\n");
3395                 return -1;
3396         }
3397         /*check appid*/
3398         ret = __validate_appid(package, uiapplication->appid, &newappid);
3399         if (ret == -1) {
3400                 _LOGD("appid is not proper\n");
3401                 return -1;
3402         } else {
3403                 if (newappid) {
3404                         if (uiapplication->appid)
3405                                 free((void *)uiapplication->appid);
3406                         uiapplication->appid = newappid;
3407                 }
3408                 uiapplication->package= strdup(package);
3409         }
3410         if (xmlTextReaderGetAttribute(reader, XMLCHAR("exec")))
3411                 uiapplication->exec = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("exec")));
3412         if (xmlTextReaderGetAttribute(reader, XMLCHAR("nodisplay"))) {
3413                 uiapplication->nodisplay = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("nodisplay")));
3414                 if (uiapplication->nodisplay == NULL)
3415                         uiapplication->nodisplay = strdup("false");
3416         } else {
3417                 uiapplication->nodisplay = strdup("false");
3418         }
3419         if (xmlTextReaderGetAttribute(reader, XMLCHAR("multiple"))) {
3420                 uiapplication->multiple = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("multiple")));
3421                 if (uiapplication->multiple == NULL)
3422                         uiapplication->multiple = strdup("false");
3423         } else {
3424                 uiapplication->multiple = strdup("false");
3425         }
3426         if (xmlTextReaderGetAttribute(reader, XMLCHAR("type")))
3427                 uiapplication->type = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("type")));
3428         if (xmlTextReaderGetAttribute(reader, XMLCHAR("categories")))
3429                 uiapplication->categories = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("categories")));
3430         if (xmlTextReaderGetAttribute(reader, XMLCHAR("extraid")))
3431                 uiapplication->extraid = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("extraid")));
3432         if (xmlTextReaderGetAttribute(reader, XMLCHAR("taskmanage"))) {
3433                 uiapplication->taskmanage = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("taskmanage")));
3434                 if (uiapplication->taskmanage == NULL)
3435                         uiapplication->taskmanage = strdup("true");
3436         } else {
3437                 uiapplication->taskmanage = strdup("true");
3438         }
3439         if (xmlTextReaderGetAttribute(reader, XMLCHAR("enabled"))) {
3440                 uiapplication->enabled = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("enabled")));
3441                 if (uiapplication->enabled == NULL)
3442                         uiapplication->enabled = strdup("true");
3443         } else {
3444                 uiapplication->enabled = strdup("true");
3445         }
3446         if (xmlTextReaderGetAttribute(reader, XMLCHAR("hw-acceleration"))) {
3447                 uiapplication->hwacceleration = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("hw-acceleration")));
3448                 if (uiapplication->hwacceleration == NULL)
3449                         uiapplication->hwacceleration = strdup("use-system-setting");
3450         } else {
3451                 uiapplication->hwacceleration = strdup("use-system-setting");
3452         }
3453         if (xmlTextReaderGetAttribute(reader, XMLCHAR("screen-reader"))) {
3454                 uiapplication->screenreader = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("screen-reader")));
3455                 if (uiapplication->screenreader == NULL)
3456                         uiapplication->screenreader = strdup("use-system-setting");
3457         } else {
3458                 uiapplication->screenreader = strdup("use-system-setting");
3459         }
3460         if (xmlTextReaderGetAttribute(reader, XMLCHAR("recentimage")))
3461                 uiapplication->recentimage = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("recentimage")));
3462         if (xmlTextReaderGetAttribute(reader, XMLCHAR("mainapp"))) {
3463                 uiapplication->mainapp = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("mainapp")));
3464                 if (uiapplication->mainapp == NULL)
3465                         uiapplication->mainapp = strdup("false");
3466         } else {
3467                 uiapplication->mainapp = strdup("false");
3468         }
3469         if (xmlTextReaderGetAttribute(reader, XMLCHAR("launchcondition"))) {
3470                 uiapplication->launchcondition = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("launchcondition")));
3471                 if (uiapplication->launchcondition == NULL)
3472                         uiapplication->launchcondition = strdup("false");
3473         } else {
3474                 uiapplication->launchcondition = strdup("false");
3475         }
3476
3477         if (xmlTextReaderGetAttribute(reader, XMLCHAR("indicatordisplay"))) {
3478                 uiapplication->indicatordisplay = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("indicatordisplay")));
3479                 if (uiapplication->indicatordisplay == NULL)
3480                         uiapplication->indicatordisplay = strdup("true");
3481         } else {
3482                 uiapplication->indicatordisplay = strdup("true");
3483         }
3484         if (xmlTextReaderGetAttribute(reader, XMLCHAR("portrait-effectimage")))
3485                 uiapplication->portraitimg = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("portrait-effectimage")));
3486         else
3487                 uiapplication->portraitimg = NULL;
3488         if (xmlTextReaderGetAttribute(reader, XMLCHAR("landscape-effectimage")))
3489                 uiapplication->landscapeimg = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("landscape-effectimage")));
3490         else
3491                 uiapplication->landscapeimg = NULL;
3492         if (xmlTextReaderGetAttribute(reader, XMLCHAR("guestmode-visibility"))) {
3493                 uiapplication->guestmode_visibility = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("guestmode-visibility")));
3494                 if (uiapplication->guestmode_visibility == NULL)
3495                         uiapplication->guestmode_visibility = strdup("true");
3496         } else {
3497                 uiapplication->guestmode_visibility = strdup("true");
3498         }
3499         if (xmlTextReaderGetAttribute(reader, XMLCHAR("permission-type"))) {
3500                 uiapplication->permission_type = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("permission-type")));
3501                 if (uiapplication->permission_type == NULL)
3502                         uiapplication->permission_type = strdup("normal");
3503         } else {
3504                 uiapplication->permission_type = strdup("normal");
3505         }
3506         if (xmlTextReaderGetAttribute(reader, XMLCHAR("component-type"))) {
3507                 uiapplication->component_type = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("component-type")));
3508                 if (uiapplication->component_type == NULL)
3509                         uiapplication->component_type = strdup("uiapp");
3510         } else {
3511                 uiapplication->component_type = strdup("uiapp");
3512         }
3513         if (xmlTextReaderGetAttribute(reader, XMLCHAR("submode"))) {
3514                 uiapplication->submode = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("submode")));
3515                 if (uiapplication->submode == NULL)
3516                         uiapplication->submode = strdup("false");
3517         } else {
3518                 uiapplication->submode = strdup("false");
3519         }
3520         if (xmlTextReaderGetAttribute(reader, XMLCHAR("submode-mainid")))
3521                 uiapplication->submode_mainid = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("submode-mainid")));
3522
3523         depth = xmlTextReaderDepth(reader);
3524         while ((ret = __next_child_element(reader, depth))) {
3525                 node = xmlTextReaderConstName(reader);
3526                 if (!node) {
3527                         _LOGD("xmlTextReaderConstName value is NULL\n");
3528                         return -1;
3529                 }
3530                 if (!strcmp(ASCII(node), "label")) {
3531                         label_x *label = malloc(sizeof(label_x));
3532                         if (label == NULL) {
3533                                 _LOGD("Malloc Failed\n");
3534                                 return -1;
3535                         }
3536                         memset(label, '\0', sizeof(label_x));
3537                         LISTADD(uiapplication->label, label);
3538                         ret = __ps_process_label(reader, label);
3539                 } else if (!strcmp(ASCII(node), "icon")) {
3540                         icon_x *icon = malloc(sizeof(icon_x));
3541                         if (icon == NULL) {
3542                                 _LOGD("Malloc Failed\n");
3543                                 return -1;
3544                         }
3545                         memset(icon, '\0', sizeof(icon_x));
3546                         LISTADD(uiapplication->icon, icon);
3547                         ret = __ps_process_icon(reader, icon, uid);
3548                 } else if (!strcmp(ASCII(node), "image")) {
3549                         image_x *image = malloc(sizeof(image_x));
3550                         if (image == NULL) {
3551                                 _LOGD("Malloc Failed\n");
3552                                 return -1;
3553                         }
3554                         memset(image, '\0', sizeof(image_x));
3555                         LISTADD(uiapplication->image, image);
3556                         ret = __ps_process_image(reader, image);
3557                 } else if (!strcmp(ASCII(node), "category")) {
3558                         category_x *category = malloc(sizeof(category_x));
3559                         if (category == NULL) {
3560                                 _LOGD("Malloc Failed\n");
3561                                 return -1;
3562                         }
3563                         memset(category, '\0', sizeof(category_x));
3564                         LISTADD(uiapplication->category, category);
3565                         ret = __ps_process_category(reader, category);
3566                 } else if (!strcmp(ASCII(node), "metadata")) {
3567                         metadata_x *metadata = malloc(sizeof(metadata_x));
3568                         if (metadata == NULL) {
3569                                 _LOGD("Malloc Failed\n");
3570                                 return -1;
3571                         }
3572                         memset(metadata, '\0', sizeof(metadata_x));
3573                         LISTADD(uiapplication->metadata, metadata);
3574                         ret = __ps_process_metadata(reader, metadata);
3575                 } else if (!strcmp(ASCII(node), "permission")) {
3576                         permission_x *permission = malloc(sizeof(permission_x));
3577                         if (permission == NULL) {
3578                                 _LOGD("Malloc Failed\n");
3579                                 return -1;
3580                         }
3581                         memset(permission, '\0', sizeof(permission_x));
3582                         LISTADD(uiapplication->permission, permission);
3583                         ret = __ps_process_permission(reader, permission);
3584                 } else if (!strcmp(ASCII(node), "app-control")) {
3585                         appcontrol_x *appcontrol = malloc(sizeof(appcontrol_x));
3586                         if (appcontrol == NULL) {
3587                                 _LOGD("Malloc Failed\n");
3588                                 return -1;
3589                         }
3590                         memset(appcontrol, '\0', sizeof(appcontrol_x));
3591                         LISTADD(uiapplication->appcontrol, appcontrol);
3592                         ret = __ps_process_appcontrol(reader, appcontrol);
3593                 } else if (!strcmp(ASCII(node), "application-service")) {
3594                         appsvc_x *appsvc = malloc(sizeof(appsvc_x));
3595                         if (appsvc == NULL) {
3596                                 _LOGD("Malloc Failed\n");
3597                                 return -1;
3598                         }
3599                         memset(appsvc, '\0', sizeof(appsvc_x));
3600                         LISTADD(uiapplication->appsvc, appsvc);
3601                         ret = __ps_process_appsvc(reader, appsvc);
3602                 } else if (!strcmp(ASCII(node), "data-share")) {
3603                         datashare_x *datashare = malloc(sizeof(datashare_x));
3604                         if (datashare == NULL) {
3605                                 _LOGD("Malloc Failed\n");
3606                                 return -1;
3607                         }
3608                         memset(datashare, '\0', sizeof(datashare_x));
3609                         LISTADD(uiapplication->datashare, datashare);
3610                         ret = __ps_process_datashare(reader, datashare);
3611                 } else if (!strcmp(ASCII(node), "launch-conditions")) {
3612                         launchconditions_x *launchconditions = malloc(sizeof(launchconditions_x));
3613                         if (launchconditions == NULL) {
3614                                 _LOGD("Malloc Failed\n");
3615                                 return -1;
3616                         }
3617                         memset(launchconditions, '\0', sizeof(launchconditions_x));
3618                         LISTADD(uiapplication->launchconditions, launchconditions);
3619                         ret = __ps_process_launchconditions(reader, launchconditions);
3620                 } else if (!strcmp(ASCII(node), "notification")) {
3621                         notification_x *notification = malloc(sizeof(notification_x));
3622                         if (notification == NULL) {
3623                                 _LOGD("Malloc Failed\n");
3624                                 return -1;
3625                         }
3626                         memset(notification, '\0', sizeof(notification_x));
3627                         LISTADD(uiapplication->notification, notification);
3628                         ret = __ps_process_notification(reader, notification);
3629                 } else
3630                         return -1;
3631                 if (ret < 0) {
3632                         _LOGD("Processing uiapplication failed\n");
3633                         return ret;
3634                 }
3635         }
3636
3637         if (uiapplication->label) {
3638                 LISTHEAD(uiapplication->label, tmp1);
3639                 uiapplication->label = tmp1;
3640         }
3641         if (uiapplication->icon) {
3642                 LISTHEAD(uiapplication->icon, tmp2);
3643                 uiapplication->icon = tmp2;
3644         }
3645         if (uiapplication->appsvc) {
3646                 LISTHEAD(uiapplication->appsvc, tmp3);
3647                 uiapplication->appsvc = tmp3;
3648         }
3649         if (uiapplication->appcontrol) {
3650                 LISTHEAD(uiapplication->appcontrol, tmp4);
3651                 uiapplication->appcontrol = tmp4;
3652         }
3653         if (uiapplication->launchconditions) {
3654                 LISTHEAD(uiapplication->launchconditions, tmp5);
3655                 uiapplication->launchconditions = tmp5;
3656         }
3657         if (uiapplication->notification) {
3658                 LISTHEAD(uiapplication->notification, tmp6);
3659                 uiapplication->notification = tmp6;
3660         }
3661         if (uiapplication->datashare) {
3662                 LISTHEAD(uiapplication->datashare, tmp7);
3663                 uiapplication->datashare = tmp7;
3664         }
3665         if (uiapplication->category) {
3666                 LISTHEAD(uiapplication->category, tmp8);
3667                 uiapplication->category = tmp8;
3668         }
3669         if (uiapplication->metadata) {
3670                 LISTHEAD(uiapplication->metadata, tmp9);
3671                 uiapplication->metadata = tmp9;
3672         }
3673         if (uiapplication->image) {
3674                 LISTHEAD(uiapplication->image, tmp10);
3675                 uiapplication->image = tmp10;
3676         }
3677         if (uiapplication->permission) {
3678                 LISTHEAD(uiapplication->permission, tmp11);
3679                 uiapplication->permission = tmp11;
3680         }
3681
3682         return ret;
3683 }
3684
3685 static int __ps_process_serviceapplication(xmlTextReaderPtr reader, serviceapplication_x *serviceapplication, uid_t uid)
3686 {
3687         const xmlChar *node;
3688         int ret = -1;
3689         int depth = -1;
3690         char *newappid = NULL;
3691         label_x *tmp1 = NULL;
3692         icon_x *tmp2 = NULL;
3693         appsvc_x *tmp3 = NULL;
3694         appcontrol_x *tmp4 = NULL;
3695         datacontrol_x *tmp5 = NULL;
3696         launchconditions_x *tmp6 = NULL;
3697         notification_x *tmp7 = NULL;
3698         datashare_x *tmp8 = NULL;
3699         category_x *tmp9 = NULL;
3700         metadata_x *tmp10 = NULL;
3701         permission_x *tmp11 = NULL;
3702
3703         if (xmlTextReaderGetAttribute(reader, XMLCHAR("appid"))) {
3704                 serviceapplication->appid = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("appid")));
3705                 if (serviceapplication->appid == NULL) {
3706                         _LOGD("appid cant be NULL\n");
3707                         return -1;
3708                 }
3709         } else {
3710                 _LOGD("appid is mandatory\n");
3711                 return -1;
3712         }
3713         /*check appid*/
3714         ret = __validate_appid(package, serviceapplication->appid, &newappid);
3715         if (ret == -1) {
3716                 _LOGD("appid is not proper\n");
3717                 return -1;
3718         } else {
3719                 if (newappid) {
3720                         if (serviceapplication->appid)
3721                                 free((void *)serviceapplication->appid);
3722                         serviceapplication->appid = newappid;
3723                 }
3724         }
3725         if (xmlTextReaderGetAttribute(reader, XMLCHAR("exec")))
3726                 serviceapplication->exec = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("exec")));
3727         if (xmlTextReaderGetAttribute(reader, XMLCHAR("type")))
3728                 serviceapplication->type = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("type")));
3729         if (xmlTextReaderGetAttribute(reader, XMLCHAR("on-boot"))) {
3730                 serviceapplication->onboot = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("on-boot")));
3731                 if (serviceapplication->onboot == NULL)
3732                         serviceapplication->onboot = strdup("false");
3733         } else {
3734                 serviceapplication->onboot = strdup("false");
3735         }
3736         if (xmlTextReaderGetAttribute(reader, XMLCHAR("auto-restart"))) {
3737                 serviceapplication->autorestart = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("auto-restart")));
3738                 if (serviceapplication->autorestart == NULL)
3739                         serviceapplication->autorestart = strdup("false");
3740         } else {
3741                 serviceapplication->autorestart = strdup("false");
3742         }
3743         if (xmlTextReaderGetAttribute(reader, XMLCHAR("permission-type"))) {
3744                 serviceapplication->permission_type = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("permission-type")));
3745                 if (serviceapplication->permission_type == NULL)
3746                         serviceapplication->permission_type = strdup("normal");
3747         } else {
3748                 serviceapplication->permission_type = strdup("normal");
3749         }
3750
3751         depth = xmlTextReaderDepth(reader);
3752         while ((ret = __next_child_element(reader, depth))) {
3753                 node = xmlTextReaderConstName(reader);
3754                 if (!node) {
3755                         _LOGD("xmlTextReaderConstName value is NULL\n");
3756                         return -1;
3757                 }
3758
3759                 if (!strcmp(ASCII(node), "label")) {
3760                         label_x *label = malloc(sizeof(label_x));
3761                         if (label == NULL) {
3762                                 _LOGD("Malloc Failed\n");
3763                                 return -1;
3764                         }
3765                         memset(label, '\0', sizeof(label_x));
3766                         LISTADD(serviceapplication->label, label);
3767                         ret = __ps_process_label(reader, label);
3768                 } else if (!strcmp(ASCII(node), "icon")) {
3769                         icon_x *icon = malloc(sizeof(icon_x));
3770                         if (icon == NULL) {
3771                                 _LOGD("Malloc Failed\n");
3772                                 return -1;
3773                         }
3774                         memset(icon, '\0', sizeof(icon_x));
3775                         LISTADD(serviceapplication->icon, icon);
3776                         ret = __ps_process_icon(reader, icon, uid);
3777                 } else if (!strcmp(ASCII(node), "category")) {
3778                         category_x *category = malloc(sizeof(category_x));
3779                         if (category == NULL) {
3780                                 _LOGD("Malloc Failed\n");
3781                                 return -1;
3782                         }
3783                         memset(category, '\0', sizeof(category_x));
3784                         LISTADD(serviceapplication->category, category);
3785                         ret = __ps_process_category(reader, category);
3786                 } else if (!strcmp(ASCII(node), "metadata")) {
3787                         metadata_x *metadata = malloc(sizeof(metadata_x));
3788                         if (metadata == NULL) {
3789                                 _LOGD("Malloc Failed\n");
3790                                 return -1;
3791                         }
3792                         memset(metadata, '\0', sizeof(metadata_x));
3793                         LISTADD(serviceapplication->metadata, metadata);
3794                         ret = __ps_process_metadata(reader, metadata);
3795                 } else if (!strcmp(ASCII(node), "permission")) {
3796                         permission_x *permission = malloc(sizeof(permission_x));
3797                         if (permission == NULL) {
3798                                 _LOGD("Malloc Failed\n");
3799                                 return -1;
3800                         }
3801                         memset(permission, '\0', sizeof(permission_x));
3802                         LISTADD(serviceapplication->permission, permission);
3803                         ret = __ps_process_permission(reader, permission);
3804                 } else if (!strcmp(ASCII(node), "app-control")) {
3805                         appcontrol_x *appcontrol = malloc(sizeof(appcontrol_x));
3806                         if (appcontrol == NULL) {
3807                                 _LOGD("Malloc Failed\n");
3808                                 return -1;
3809                         }
3810                         memset(appcontrol, '\0', sizeof(appcontrol_x));
3811                         LISTADD(serviceapplication->appcontrol, appcontrol);
3812                         ret = __ps_process_appcontrol(reader, appcontrol);
3813                 } else if (!strcmp(ASCII(node), "application-service")) {
3814                         appsvc_x *appsvc = malloc(sizeof(appsvc_x));
3815                         if (appsvc == NULL) {
3816                                 _LOGD("Malloc Failed\n");
3817                                 return -1;
3818                         }
3819                         memset(appsvc, '\0', sizeof(appsvc_x));
3820                         LISTADD(serviceapplication->appsvc, appsvc);
3821                         ret = __ps_process_appsvc(reader, appsvc);
3822                 } else if (!strcmp(ASCII(node), "data-share")) {
3823                         datashare_x *datashare = malloc(sizeof(datashare_x));
3824                         if (datashare == NULL) {
3825                                 _LOGD("Malloc Failed\n");
3826                                 return -1;
3827                         }
3828                         memset(datashare, '\0', sizeof(datashare_x));
3829                         LISTADD(serviceapplication->datashare, datashare);
3830                         ret = __ps_process_datashare(reader, datashare);
3831                 } else if (!strcmp(ASCII(node), "launch-conditions")) {
3832                         launchconditions_x *launchconditions = malloc(sizeof(launchconditions_x));
3833                         if (launchconditions == NULL) {
3834                                 _LOGD("Malloc Failed\n");
3835                                 return -1;
3836                         }
3837                         memset(launchconditions, '\0', sizeof(launchconditions_x));
3838                         LISTADD(serviceapplication->launchconditions, launchconditions);
3839                         ret = __ps_process_launchconditions(reader, launchconditions);
3840                 } else if (!strcmp(ASCII(node), "notification")) {
3841                         notification_x *notification = malloc(sizeof(notification_x));
3842                         if (notification == NULL) {
3843                                 _LOGD("Malloc Failed\n");
3844                                 return -1;
3845                         }
3846                         memset(notification, '\0', sizeof(notification_x));
3847                         LISTADD(serviceapplication->notification, notification);
3848                         ret = __ps_process_notification(reader, notification);
3849                 } else if (!strcmp(ASCII(node), "data-control")) {
3850                         datacontrol_x *datacontrol = malloc(sizeof(datacontrol_x));
3851                         if (datacontrol == NULL) {
3852                                 _LOGD("Malloc Failed\n");
3853                                 return -1;
3854                         }
3855                         memset(datacontrol, '\0', sizeof(datacontrol_x));
3856                         LISTADD(serviceapplication->datacontrol, datacontrol);
3857                         ret = __ps_process_datacontrol(reader, datacontrol);
3858                 } else
3859                         return -1;
3860                 if (ret < 0) {
3861                         _LOGD("Processing serviceapplication failed\n");
3862                         return ret;
3863                 }
3864         }
3865
3866         if (serviceapplication->label) {
3867                 LISTHEAD(serviceapplication->label, tmp1);
3868                 serviceapplication->label = tmp1;
3869         }
3870         if (serviceapplication->icon) {
3871                 LISTHEAD(serviceapplication->icon, tmp2);
3872                 serviceapplication->icon = tmp2;
3873         }
3874         if (serviceapplication->appsvc) {
3875                 LISTHEAD(serviceapplication->appsvc, tmp3);
3876                 serviceapplication->appsvc = tmp3;
3877         }
3878         if (serviceapplication->appcontrol) {
3879                 LISTHEAD(serviceapplication->appcontrol, tmp4);
3880                 serviceapplication->appcontrol = tmp4;
3881         }
3882         if (serviceapplication->datacontrol) {
3883                 LISTHEAD(serviceapplication->datacontrol, tmp5);
3884                 serviceapplication->datacontrol = tmp5;
3885         }
3886         if (serviceapplication->launchconditions) {
3887                 LISTHEAD(serviceapplication->launchconditions, tmp6);
3888                 serviceapplication->launchconditions = tmp6;
3889         }
3890         if (serviceapplication->notification) {
3891                 LISTHEAD(serviceapplication->notification, tmp7);
3892                 serviceapplication->notification = tmp7;
3893         }
3894         if (serviceapplication->datashare) {
3895                 LISTHEAD(serviceapplication->datashare, tmp8);
3896                 serviceapplication->datashare = tmp8;
3897         }
3898         if (serviceapplication->category) {
3899                 LISTHEAD(serviceapplication->category, tmp9);
3900                 serviceapplication->category = tmp9;
3901         }
3902         if (serviceapplication->metadata) {
3903                 LISTHEAD(serviceapplication->metadata, tmp10);
3904                 serviceapplication->metadata = tmp10;
3905         }
3906         if (serviceapplication->permission) {
3907                 LISTHEAD(serviceapplication->permission, tmp11);
3908                 serviceapplication->permission = tmp11;
3909         }
3910
3911         return ret;
3912 }
3913
3914 static int __ps_process_deviceprofile(xmlTextReaderPtr reader, deviceprofile_x *deviceprofile)
3915 {
3916         /*TODO: once policy is set*/
3917         return 0;
3918 }
3919
3920 static int __ps_process_font(xmlTextReaderPtr reader, font_x *font)
3921 {
3922         /*TODO: once policy is set*/
3923         return 0;
3924 }
3925
3926 static int __ps_process_theme(xmlTextReaderPtr reader, theme_x *theme)
3927 {
3928         /*TODO: once policy is set*/
3929         return 0;
3930 }
3931
3932 static int __ps_process_daemon(xmlTextReaderPtr reader, daemon_x *daemon)
3933 {
3934         /*TODO: once policy is set*/
3935         return 0;
3936 }
3937
3938 static int __ps_process_ime(xmlTextReaderPtr reader, ime_x *ime)
3939 {
3940         /*TODO: once policy is set*/
3941         return 0;
3942 }
3943
3944 static int __start_process(xmlTextReaderPtr reader, manifest_x * mfx, uid_t uid)
3945 {
3946         _LOGD("__start_process\n");
3947         const xmlChar *node;
3948         int ret = -1;
3949         int depth = -1;
3950         label_x *tmp1 = NULL;
3951         author_x *tmp2 = NULL;
3952         description_x *tmp3 = NULL;
3953         license_x *tmp4 = NULL;
3954         uiapplication_x *tmp5 = NULL;
3955         serviceapplication_x *tmp6 = NULL;
3956         daemon_x *tmp7 = NULL;
3957         theme_x *tmp8 = NULL;
3958         font_x *tmp9 = NULL;
3959         ime_x *tmp10 = NULL;
3960         icon_x *tmp11 = NULL;
3961         compatibility_x *tmp12 = NULL;
3962         deviceprofile_x *tmp13 = NULL;
3963         privileges_x *tmp14 = NULL;
3964
3965         depth = xmlTextReaderDepth(reader);
3966         int  i =0;
3967         while ((ret = __next_child_element(reader, depth))) {
3968                 node = xmlTextReaderConstName(reader);
3969                 if (!node) {
3970                         _LOGD("xmlTextReaderConstName value is NULL\n");
3971                         return -1;
3972                 }
3973
3974                 if (!strcmp(ASCII(node), "label")) {
3975                         label_x *label = malloc(sizeof(label_x));
3976                         if (label == NULL) {
3977                                 _LOGD("Malloc Failed\n");
3978                                 return -1;
3979                         }
3980                         memset(label, '\0', sizeof(label_x));
3981                         LISTADD(mfx->label, label);
3982                         ret = __ps_process_label(reader, label);
3983                 } else if (!strcmp(ASCII(node), "author")) {
3984                         author_x *author = malloc(sizeof(author_x));
3985                         if (author == NULL) {
3986                                 _LOGD("Malloc Failed\n");
3987                                 return -1;
3988                         }
3989                         memset(author, '\0', sizeof(author_x));
3990                         LISTADD(mfx->author, author);
3991                         ret = __ps_process_author(reader, author);
3992                 } else if (!strcmp(ASCII(node), "description")) {
3993                         description_x *description = malloc(sizeof(description_x));
3994                         if (description == NULL) {
3995                                 _LOGD("Malloc Failed\n");
3996                                 return -1;
3997                         }
3998                         memset(description, '\0', sizeof(description_x));
3999                         LISTADD(mfx->description, description);
4000                         ret = __ps_process_description(reader, description);
4001                 } else if (!strcmp(ASCII(node), "license")) {
4002                         license_x *license = malloc(sizeof(license_x));
4003                         if (license == NULL) {
4004                                 _LOGD("Malloc Failed\n");
4005                                 return -1;
4006                         }
4007                         memset(license, '\0', sizeof(license_x));
4008                         LISTADD(mfx->license, license);
4009                         ret = __ps_process_license(reader, license);
4010                 } else if (!strcmp(ASCII(node), "privileges")) {
4011                         privileges_x *privileges = malloc(sizeof(privileges_x));
4012                         if (privileges == NULL) {
4013                                 _LOGD("Malloc Failed\n");
4014                                 return -1;
4015                         }
4016                         memset(privileges, '\0', sizeof(privileges_x));
4017                         LISTADD(mfx->privileges, privileges);
4018                         ret = __ps_process_privileges(reader, privileges);
4019                 } else if (!strcmp(ASCII(node), "ui-application")) {
4020                         uiapplication_x *uiapplication = malloc(sizeof(uiapplication_x));
4021                         if (uiapplication == NULL) {
4022                                 _LOGD("Malloc Failed\n");
4023                                 return -1;
4024                         }
4025                         memset(uiapplication, '\0', sizeof(uiapplication_x));
4026                         LISTADD(mfx->uiapplication, uiapplication);
4027                         ret = __ps_process_uiapplication(reader, uiapplication, uid);
4028                 } else if (!strcmp(ASCII(node), "service-application")) {
4029                         serviceapplication_x *serviceapplication = malloc(sizeof(serviceapplication_x));
4030                         if (serviceapplication == NULL) {
4031                                 _LOGD("Malloc Failed\n");
4032                                 return -1;
4033                         }
4034                         memset(serviceapplication, '\0', sizeof(serviceapplication_x));
4035                         LISTADD(mfx->serviceapplication, serviceapplication);
4036                         ret = __ps_process_serviceapplication(reader, serviceapplication, uid);
4037                 } else if (!strcmp(ASCII(node), "daemon")) {
4038                         daemon_x *daemon = malloc(sizeof(daemon_x));
4039                         if (daemon == NULL) {
4040                                 _LOGD("Malloc Failed\n");
4041                                 return -1;
4042                         }
4043                         memset(daemon, '\0', sizeof(daemon_x));
4044                         LISTADD(mfx->daemon, daemon);
4045                         ret = __ps_process_daemon(reader, daemon);
4046                 } else if (!strcmp(ASCII(node), "theme")) {
4047                         theme_x *theme = malloc(sizeof(theme_x));
4048                         if (theme == NULL) {
4049                                 _LOGD("Malloc Failed\n");
4050                                 return -1;
4051                         }
4052                         memset(theme, '\0', sizeof(theme_x));
4053                         LISTADD(mfx->theme, theme);
4054                         ret = __ps_process_theme(reader, theme);
4055                 } else if (!strcmp(ASCII(node), "font")) {
4056                         font_x *font = malloc(sizeof(font_x));
4057                         if (font == NULL) {
4058                                 _LOGD("Malloc Failed\n");
4059                                 return -1;
4060                         }
4061                         memset(font, '\0', sizeof(font_x));
4062                         LISTADD(mfx->font, font);
4063                         ret = __ps_process_font(reader, font);
4064                 } else if (!strcmp(ASCII(node), "ime")) {
4065                         ime_x *ime = malloc(sizeof(ime_x));
4066                         if (ime == NULL) {
4067                                 _LOGD("Malloc Failed\n");
4068                                 return -1;
4069                         }
4070                         memset(ime, '\0', sizeof(ime_x));
4071                         LISTADD(mfx->ime, ime);
4072                         ret = __ps_process_ime(reader, ime);
4073                 } else if (!strcmp(ASCII(node), "icon")) {
4074                         icon_x *icon = malloc(sizeof(icon_x));
4075                         if (icon == NULL) {
4076                                 _LOGD("Malloc Failed\n");
4077                                 return -1;
4078                         }
4079                         memset(icon, '\0', sizeof(icon_x));
4080                         LISTADD(mfx->icon, icon);
4081                         ret = __ps_process_icon(reader, icon, uid);
4082                 } else if (!strcmp(ASCII(node), "device-profile")) {
4083                         deviceprofile_x *deviceprofile = malloc(sizeof(deviceprofile_x));
4084                         if (deviceprofile == NULL) {
4085                                 _LOGD("Malloc Failed\n");
4086                                 return -1;
4087                         }
4088                         memset(deviceprofile, '\0', sizeof(deviceprofile_x));
4089                         LISTADD(mfx->deviceprofile, deviceprofile);
4090                         ret = __ps_process_deviceprofile(reader, deviceprofile);
4091                 } else if (!strcmp(ASCII(node), "compatibility")) {
4092                         compatibility_x *compatibility = malloc(sizeof(compatibility_x));
4093                         if (compatibility == NULL) {
4094                                 _LOGD("Malloc Failed\n");
4095                                 return -1;
4096                         }
4097                         memset(compatibility, '\0', sizeof(compatibility_x));
4098                         LISTADD(mfx->compatibility, compatibility);
4099                         ret = __ps_process_compatibility(reader, compatibility);
4100                 } else if (!strcmp(ASCII(node), "shortcut-list")) {
4101                         continue;
4102                 } else if (!strcmp(ASCII(node), "livebox")) {
4103                         continue;
4104                 } else if (!strcmp(ASCII(node), "account")) {
4105                         continue;
4106                 } else if (!strcmp(ASCII(node), "notifications")) {
4107                         continue;
4108                 } else if (!strcmp(ASCII(node), "ime")) {
4109                         continue;
4110                 } else
4111                         return -1;
4112
4113                 if (ret < 0) {
4114                         _LOGD("Processing manifest failed\n");
4115                         return ret;
4116                 }
4117         }
4118         if (mfx->label) {
4119                 LISTHEAD(mfx->label, tmp1);
4120                 mfx->label = tmp1;
4121         }
4122         if (mfx->author) {
4123                 LISTHEAD(mfx->author, tmp2);
4124                 mfx->author = tmp2;
4125         }
4126         if (mfx->description) {
4127                 LISTHEAD(mfx->description, tmp3);
4128                 mfx->description= tmp3;
4129         }
4130         if (mfx->license) {
4131                 LISTHEAD(mfx->license, tmp4);
4132                 mfx->license= tmp4;
4133         }
4134         if (mfx->uiapplication) {
4135                 LISTHEAD(mfx->uiapplication, tmp5);
4136                 mfx->uiapplication = tmp5;
4137         }
4138         if (mfx->serviceapplication) {
4139                 LISTHEAD(mfx->serviceapplication, tmp6);
4140                 mfx->serviceapplication = tmp6;
4141         }
4142         if (mfx->daemon) {
4143                 LISTHEAD(mfx->daemon, tmp7);
4144                 mfx->daemon= tmp7;
4145         }
4146         if (mfx->theme) {
4147                 LISTHEAD(mfx->theme, tmp8);
4148                 mfx->theme= tmp8;
4149         }
4150         if (mfx->font) {
4151                 LISTHEAD(mfx->font, tmp9);
4152                 mfx->font= tmp9;
4153         }
4154         if (mfx->ime) {
4155                 LISTHEAD(mfx->ime, tmp10);
4156                 mfx->ime= tmp10;
4157         }
4158         if (mfx->icon) {
4159                 LISTHEAD(mfx->icon, tmp11);
4160                 mfx->icon= tmp11;
4161         }
4162         if (mfx->compatibility) {
4163                 LISTHEAD(mfx->compatibility, tmp12);
4164                 mfx->compatibility= tmp12;
4165         }
4166         if (mfx->deviceprofile) {
4167                 LISTHEAD(mfx->deviceprofile, tmp13);
4168                 mfx->deviceprofile= tmp13;
4169         }
4170         if (mfx->privileges) {
4171                 LISTHEAD(mfx->privileges, tmp14);
4172                 mfx->privileges = tmp14;
4173         }
4174         return ret;
4175 }
4176
4177 static int __process_manifest(xmlTextReaderPtr reader, manifest_x * mfx, uid_t uid)
4178 {
4179         const xmlChar *node;
4180         int ret = -1;
4181
4182         if ((ret = __next_child_element(reader, -1))) {
4183                 node = xmlTextReaderConstName(reader);
4184                 if (!node) {
4185                         _LOGD("xmlTextReaderConstName value is NULL\n");
4186                         return -1;
4187                 }
4188
4189                 if (!strcmp(ASCII(node), "manifest")) {
4190                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("xmlns"))){
4191                                 mfx->ns = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("xmlns")));
4192                         }
4193                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("package"))) {
4194                                 mfx->package= ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("package")));
4195                                 if (mfx->package == NULL) {
4196                                         _LOGD("package cant be NULL\n");
4197                                         return -1;
4198                                 }
4199                         } else {
4200                                 _LOGD("package field is mandatory\n");
4201                                 return -1;
4202                         }
4203                         package = mfx->package;
4204                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("version")))
4205                                 mfx->version= ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("version")));
4206                         /*app2ext needs package size for external installation*/
4207                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("size")))
4208                                 mfx->package_size = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("size")));
4209                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("install-location")))
4210                                 mfx->installlocation = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("install-location")));
4211                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("type")))
4212                                 mfx->type = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("type")));
4213                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("root_path")))
4214                                 mfx->root_path = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("root_path")));
4215                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("csc_path")))
4216                                 mfx->csc_path = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("csc_path")));
4217                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("main_package")))
4218                                 mfx->main_package = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("main_package")));
4219                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("appsetting"))) {
4220                                 mfx->appsetting = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("appsetting")));
4221                                 if (mfx->appsetting == NULL)
4222                                         mfx->appsetting = strdup("false");
4223                         } else {
4224                                 mfx->appsetting = strdup("false");
4225                         }
4226                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("storeclient-id")))
4227                                 mfx->storeclient_id= ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("storeclient-id")));
4228                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("nodisplay-setting"))) {
4229                                 mfx->nodisplay_setting = ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("nodisplay-setting")));
4230                                 if (mfx->nodisplay_setting == NULL)
4231                                         mfx->nodisplay_setting = strdup("false");
4232                         } else {
4233                                 mfx->nodisplay_setting = strdup("false");
4234                         }
4235                         if (xmlTextReaderGetAttribute(reader, XMLCHAR("url")))
4236                                 mfx->package_url= ASCII(xmlTextReaderGetAttribute(reader, XMLCHAR("url")));
4237                         /*Assign default values. If required it will be overwritten in __add_preload_info()*/
4238                         mfx->preload = strdup("False");
4239                         mfx->removable = strdup("True");
4240                         mfx->readonly = strdup("False");
4241                         mfx->update = strdup("False");
4242                         mfx->system = strdup("False");
4243                         char buf[PKG_STRING_LEN_MAX] = {'\0'};
4244                         char *val = NULL;
4245                         time_t current_time;
4246                         time(&current_time);
4247                         snprintf(buf, PKG_STRING_LEN_MAX - 1, "%d", current_time);
4248                         val = strndup(buf, PKG_STRING_LEN_MAX - 1);
4249                         mfx->installed_time = val;
4250                         mfx->installed_storage= strdup("installed_internal");
4251
4252                         ret = __start_process(reader, mfx, uid);
4253                 } else {
4254                         _LOGD("No Manifest element found\n");
4255                         return -1;
4256                 }
4257         }
4258         return ret;
4259 }
4260
4261 static char* __convert_to_system_locale(const char *mlocale)
4262 {
4263         if (mlocale == NULL)
4264                 return NULL;
4265         char *locale = NULL;
4266         locale = (char *)calloc(1, 6);
4267         if (!locale) {
4268                 _LOGE("Malloc Failed\n");
4269                 return NULL;
4270         }
4271
4272         strncpy(locale, mlocale, 2);
4273         strncat(locale, "_", 1);
4274         locale[3] = toupper(mlocale[3]);
4275         locale[4] = toupper(mlocale[4]);
4276         return locale;
4277 }
4278
4279 #define LIBAIL_PATH LIB_PATH "/libail.so.0"
4280
4281 /* operation_type */
4282 typedef enum {
4283         AIL_INSTALL = 0,
4284         AIL_UPDATE,
4285         AIL_REMOVE,
4286         AIL_CLEAN,
4287         AIL_FOTA,
4288         AIL_MAX
4289 } AIL_TYPE;
4290
4291 static int __ail_change_info(int op, const char *appid, uid_t uid)
4292 {
4293         void *lib_handle = NULL;
4294         char *aop = NULL;
4295         int ret = 0;
4296
4297         if ((lib_handle = dlopen(LIBAIL_PATH, RTLD_LAZY)) == NULL) {
4298                 _LOGE("dlopen is failed LIBAIL_PATH[%s]\n", LIBAIL_PATH);
4299                 goto END;
4300         }
4301 //is_admin
4302         if(uid != GLOBAL_USER)
4303         {
4304                 int (*ail_desktop_operation) (const char *, uid_t uid);
4305                 switch (op) {
4306                         case 0:
4307                                 aop  = "ail_usr_desktop_add";
4308                                 break;
4309                         case 1:
4310                                 aop  = "ail_usr_desktop_update";
4311                                 break;
4312                         case 2:
4313                                 aop  = "ail_usr_desktop_remove";
4314                                 break;
4315                         case 3:
4316                                 aop  = "ail_usr_desktop_clean";
4317                                 break;
4318                         case 4:
4319                                 aop  = "ail_usr_desktop_fota";
4320                                 break;
4321                         default:
4322                                 goto END;
4323                                 break;
4324                 }
4325
4326                 if ((ail_desktop_operation =
4327                         dlsym(lib_handle, aop)) == NULL || dlerror() != NULL) {
4328                         _LOGE("can not find symbol \n");
4329                         goto END;
4330                 }
4331
4332                 ret = ail_desktop_operation(appid, uid);
4333         }else{
4334                 int (*ail_desktop_operation) (const char *);
4335                 switch (op) {
4336                         case 0:
4337                                 aop  = "ail_desktop_add";
4338                                 break;
4339                         case 1:
4340                                 aop  = "ail_desktop_update";
4341                                 break;
4342                         case 2:
4343                                 aop  = "ail_desktop_remove";
4344                                 break;
4345                         case 3:
4346                                 aop  = "ail_desktop_clean";
4347                                 break;
4348                         case 4:
4349                                 aop  = "ail_desktop_fota";
4350                                 break;
4351                         default:
4352                                 goto END;
4353                                 break;
4354                 }
4355
4356                 if ((ail_desktop_operation =
4357                         dlsym(lib_handle, aop)) == NULL || dlerror() != NULL) {
4358                         _LOGE("can not find symbol \n");
4359                         goto END;
4360                 }
4361
4362                 ret = ail_desktop_operation(appid);
4363
4364         }
4365 END:
4366         if (lib_handle)
4367                 dlclose(lib_handle);
4368
4369         return ret;
4370 }
4371
4372 /* desktop shoud be generated automatically based on manifest */
4373 /* Currently removable, taskmanage, etc fields are not considerd. it will be decided soon.*/
4374 #define BUFMAX 1024*128
4375 static int __ps_make_nativeapp_desktop(manifest_x * mfx, const char *manifest, ACTION_TYPE action, uid_t uid)
4376 {
4377         FILE* file = NULL;
4378         int fd = 0;
4379         char filepath[PKG_STRING_LEN_MAX] = "";
4380         char *buf = NULL;
4381         char *buftemp = NULL;
4382         char *locale = NULL;
4383
4384         buf = (char *)calloc(1, BUFMAX);
4385         if (!buf) {
4386                 _LOGE("Malloc Failed\n");
4387                 return -1;
4388         }
4389
4390         buftemp = (char *)calloc(1, BUFMAX);
4391         if (!buftemp) {
4392                 _LOGE("Malloc Failed\n");
4393                 free(buf);
4394                 return -1;
4395         }
4396
4397         if (action == ACTION_UPGRADE)
4398                 __ail_change_info(AIL_CLEAN, mfx->package, uid);
4399
4400         for(; mfx->uiapplication; mfx->uiapplication=mfx->uiapplication->next) {
4401
4402                 if (manifest != NULL) {
4403                         /* skip making a deskfile and update ail, if preload app is updated */
4404                         if(strstr(manifest, getUserManifestPath(uid))) {
4405                                 __ail_change_info(AIL_INSTALL, mfx->uiapplication->appid, uid);
4406                     _LOGE("preload app is update : skip and update ail : %s", manifest);
4407                                 continue;
4408                         }
4409                 }
4410
4411                 snprintf(filepath, sizeof(filepath),"%s%s.desktop", getUserDesktopPath(uid), mfx->uiapplication->appid);
4412
4413                 /* skip if desktop exists
4414                 if (access(filepath, R_OK) == 0)
4415                         continue;
4416                 */
4417
4418                 file = fopen(filepath, "w");
4419                 if(file == NULL)
4420                 {
4421                     _LOGD("Can't open %s", filepath);
4422                     free(buf);
4423                     free(buftemp);
4424                     return -1;
4425                 }
4426
4427                 snprintf(buf, BUFMAX, "[Desktop Entry]\n");
4428                 fwrite(buf, 1, strlen(buf), file);
4429
4430                 for( ; mfx->uiapplication->label ; mfx->uiapplication->label = mfx->uiapplication->label->next) {
4431                         if(!strcmp(mfx->uiapplication->label->lang, DEFAULT_LOCALE)) {
4432                                 snprintf(buf, BUFMAX, "Name=%s\n",      mfx->uiapplication->label->text);
4433                         } else {
4434                                 locale = __convert_to_system_locale(mfx->uiapplication->label->lang);
4435                                 snprintf(buf, BUFMAX, "Name[%s]=%s\n", locale,
4436                                         mfx->uiapplication->label->text);
4437                                 free(locale);
4438                         }
4439                         fwrite(buf, 1, strlen(buf), file);
4440                 }
4441
4442                 if(mfx->uiapplication->label && mfx->uiapplication->label->text) {
4443                         snprintf(buf, BUFMAX, "Name=%s\n", mfx->uiapplication->label->text);
4444                         fwrite(buf, 1, strlen(buf), file);
4445                 }
4446 /*
4447                 else if(mfx->label && mfx->label->text) {
4448                         snprintf(buf, BUFMAX, "Name=%s\n", mfx->label->text);
4449                         fwrite(buf, 1, strlen(buf), file);
4450                 } else {
4451                         snprintf(buf, BUFMAX, "Name=%s\n", mfx->package);
4452                         fwrite(buf, 1, strlen(buf), file);
4453                 }
4454 */
4455
4456
4457                 snprintf(buf, BUFMAX, "Type=Application\n");
4458                 fwrite(buf, 1, strlen(buf), file);
4459
4460                 if(mfx->uiapplication->exec) {
4461                         snprintf(buf, BUFMAX, "Exec=%s\n", mfx->uiapplication->exec);
4462                         fwrite(buf, 1, strlen(buf), file);
4463                 }
4464
4465                 if(mfx->uiapplication->icon && mfx->uiapplication->icon->text) {
4466                         snprintf(buf, BUFMAX, "Icon=%s\n", mfx->uiapplication->icon->text);
4467                         fwrite(buf, 1, strlen(buf), file);
4468                 } else if(mfx->icon && mfx->icon->text) {
4469                         snprintf(buf, BUFMAX, "Icon=%s\n", mfx->icon->text);
4470                         fwrite(buf, 1, strlen(buf), file);
4471                 }
4472
4473                 // MIME types
4474                 if(mfx->uiapplication && mfx->uiapplication->appsvc) {
4475                         appsvc_x *asvc = mfx->uiapplication->appsvc;
4476                         mime_x *mi = NULL;
4477                         const char *mime = NULL;
4478                         const char *mime_delim = "; ";
4479                         int mime_count = 0;
4480
4481                         strncpy(buf, "MimeType=", BUFMAX-1);
4482                         while (asvc) {
4483                                 mi = asvc->mime;
4484                                 while (mi) {
4485                                         mime_count++;
4486                                         mime = mi->name;
4487                                         _LOGD("MIME type: %s\n", mime);
4488                                         strncat(buf, mime, BUFMAX-strlen(buf)-1);
4489                                         if(mi->next) {
4490                                                 strncat(buf, mime_delim, BUFMAX-strlen(buf)-1);
4491                                         }
4492
4493                                         mi = mi->next;
4494                                         mime = NULL;
4495                                 }
4496                                 asvc = asvc->next;
4497                         }
4498                         _LOGD("MIME types: buf[%s]\n", buf);
4499                         _LOGD("MIME count: %d\n", mime_count);
4500                         if(mime_count)
4501                                 fwrite(buf, 1, strlen(buf), file);
4502                 }
4503
4504                 if(mfx->version) {
4505                         snprintf(buf, BUFMAX, "Version=%s\n", mfx->version);
4506                         fwrite(buf, 1, strlen(buf), file);
4507                 }
4508
4509                 if(mfx->uiapplication->nodisplay) {
4510                         snprintf(buf, BUFMAX, "NoDisplay=%s\n", mfx->uiapplication->nodisplay);
4511                         fwrite(buf, 1, strlen(buf), file);
4512                 }
4513
4514                 if(mfx->uiapplication->categories) {
4515                         snprintf(buf, BUFMAX, "Categories=%s\n", mfx->uiapplication->categories);
4516                         fwrite(buf, 1, strlen(buf), file);
4517                 }
4518
4519                 if(mfx->uiapplication->taskmanage && !strcasecmp(mfx->uiapplication->taskmanage, "False")) {
4520                         snprintf(buf, BUFMAX, "X-TIZEN-TaskManage=False\n");
4521                         fwrite(buf, 1, strlen(buf), file);
4522                 }
4523
4524                 if(mfx->uiapplication->enabled && !strcasecmp(mfx->uiapplication->enabled, "False")) {
4525                         snprintf(buf, BUFMAX, "X-TIZEN-Enabled=False\n");
4526                         fwrite(buf, 1, strlen(buf), file);
4527                 }
4528
4529                 if(mfx->uiapplication->hwacceleration) {
4530                         snprintf(buf, BUFMAX, "Hw-Acceleration=%s\n", mfx->uiapplication->hwacceleration);
4531                         fwrite(buf, 1, strlen(buf), file);
4532                 }
4533
4534                 if(mfx->uiapplication->multiple && !strcasecmp(mfx->uiapplication->multiple, "True")) {
4535                         snprintf(buf, BUFMAX, "X-TIZEN-Multiple=True\n");
4536                         fwrite(buf, 1, strlen(buf), file);
4537                 }
4538
4539                 if(mfx->uiapplication->extraid) {
4540                         snprintf(buf, BUFMAX, "X-TIZEN-PackageID=%s\n", mfx->uiapplication->extraid);
4541                         fwrite(buf, 1, strlen(buf), file);
4542                 }
4543
4544                 if(mfx->removable && !strcasecmp(mfx->removable, "False")) {
4545                         snprintf(buf, BUFMAX, "X-TIZEN-Removable=False\n");
4546                         fwrite(buf, 1, strlen(buf), file);
4547                 }
4548
4549                 if(mfx->type) {
4550                         snprintf(buf, BUFMAX, "X-TIZEN-PackageType=%s\n", mfx->type);
4551                         fwrite(buf, 1, strlen(buf), file);
4552                 }
4553
4554                 if(mfx->uiapplication->submode && !strcasecmp(mfx->uiapplication->submode, "True")) {
4555                         snprintf(buf, BUFMAX, "X-TIZEN-Submode=%s\n", mfx->uiapplication->submode);
4556                         fwrite(buf, 1, strlen(buf), file);
4557                         snprintf(buf, BUFMAX, "X-TIZEN-SubmodeMainid=%s\n", mfx->uiapplication->submode_mainid);
4558                         fwrite(buf, 1, strlen(buf), file);
4559                 }
4560
4561                 snprintf(buf, BUFMAX, "X-TIZEN-PkgID=%s\n", mfx->package);
4562                 fwrite(buf, 1, strlen(buf), file);
4563
4564
4565 //              snprintf(buf, BUFMAX, "X-TIZEN-PackageType=rpm\n");
4566 //              fwrite(buf, 1, strlen(buf), file);
4567
4568
4569                 if(mfx->uiapplication->appsvc) {
4570                         snprintf(buf, BUFMAX, "X-TIZEN-Svc=");
4571                         _LOGD("buf[%s]\n", buf);
4572
4573
4574                         uiapplication_x *up = mfx->uiapplication;
4575                         appsvc_x *asvc = NULL;
4576                         operation_x *op = NULL;
4577                         mime_x *mi = NULL;
4578                         uri_x *ui = NULL;
4579                         subapp_x *sub = NULL;
4580                         const char *operation = NULL;
4581                         const char *mime = NULL;
4582                         const char *uri = NULL;
4583                         const char *subapp = NULL;
4584                         int i = 0;
4585
4586
4587                         asvc = up->appsvc;
4588                         while(asvc != NULL) {
4589                                 op = asvc->operation;
4590                                 while(op != NULL) {
4591                                         if (op)
4592                                                 operation = op->name;
4593                                         mi = asvc->mime;
4594
4595                                         do
4596                                         {
4597                                                 if (mi)
4598                                                         mime = mi->name;
4599                                                 sub = asvc->subapp;
4600                                                 do
4601                                                 {
4602                                                         if (sub)
4603                                                                 subapp = sub->name;
4604                                                         ui = asvc->uri;
4605                                                         do
4606                                                         {
4607                                                                 if (ui)
4608                                                                         uri = ui->name;
4609
4610                                                                 if(i++ > 0) {
4611                                                                         strncpy(buftemp, buf, BUFMAX);
4612                                                                         snprintf(buf, BUFMAX, "%s;", buftemp);
4613                                                                 }
4614
4615
4616                                                                 strncpy(buftemp, buf, BUFMAX);
4617                                                                 snprintf(buf, BUFMAX, "%s%s|%s|%s|%s", buftemp, operation?operation:"NULL", uri?uri:"NULL", mime?mime:"NULL", subapp?subapp:"NULL");
4618                                                                 _LOGD("buf[%s]\n", buf);
4619
4620                                                                 if (ui)
4621                                                                         ui = ui->next;
4622                                                                 uri = NULL;
4623                                                         } while(ui != NULL);
4624                                                 if (sub)
4625                                                                 sub = sub->next;
4626                                                         subapp = NULL;
4627                                                 }while(sub != NULL);
4628                                                 if (mi)
4629                                                         mi = mi->next;
4630                                                 mime = NULL;
4631                                         }while(mi != NULL);
4632                                         if (op)
4633                                                 op = op->next;
4634                                         operation = NULL;
4635                                 }
4636                                 asvc = asvc->next;
4637                         }
4638
4639
4640                         fwrite(buf, 1, strlen(buf), file);
4641
4642 //                      strncpy(buftemp, buf, BUFMAX);
4643 //                      snprintf(buf, BUFMAX, "%s\n", buftemp);
4644 //                      fwrite(buf, 1, strlen(buf), file);
4645                 }
4646
4647                 if(mfx->uiapplication->appcontrol) {
4648                         snprintf(buf, BUFMAX, "X-TIZEN-Svc=");
4649                         _LOGD("buf[%s]\n", buf);
4650
4651                         uiapplication_x *up = mfx->uiapplication;
4652                         appcontrol_x *acontrol = NULL;
4653                         operation_x *op = NULL;
4654                         mime_x *mi = NULL;
4655                         uri_x *ui = NULL;
4656                         subapp_x *sub = NULL;
4657                         const char *operation = NULL;
4658                         const char *mime = NULL;
4659                         const char *uri = NULL;
4660                         const char *subapp = NULL;
4661                         int i = 0;
4662
4663                         acontrol = up->appcontrol;
4664                         while(acontrol != NULL) {
4665                                 op = acontrol->operation;
4666                                 while(op != NULL) {
4667                                         if (op)
4668                                                 operation = op->name;
4669                                         mi = acontrol->mime;
4670
4671                                         do
4672                                         {
4673                                                 if (mi)
4674                                                         mime = mi->name;
4675                                                 sub = acontrol->subapp;
4676                                                 do
4677                                                 {
4678                                                         if (sub)
4679                                                                 subapp = sub->name;
4680                                                         ui = acontrol->uri;
4681                                                         do
4682                                                         {
4683                                                                 if (ui)
4684                                                                         uri = ui->name;
4685
4686                                                                 if(i++ > 0) {
4687                                                                         strncpy(buftemp, buf, BUFMAX);
4688                                                                         snprintf(buf, BUFMAX, "%s;", buftemp);
4689                                                                 }
4690
4691                                                                 strncpy(buftemp, buf, BUFMAX);
4692                                                                 snprintf(buf, BUFMAX, "%s%s|%s|%s|%s", buftemp, operation?operation:"NULL", uri?uri:"NULL", mime?mime:"NULL", subapp?subapp:"NULL");
4693                                                                 _LOGD("buf[%s]\n", buf);
4694
4695                                                                 if (ui)
4696                                                                         ui = ui->next;
4697                                                                 uri = NULL;
4698                                                         } while(ui != NULL);
4699                                                 if (sub)
4700                                                                 sub = sub->next;
4701                                                         subapp = NULL;
4702                                                 }while(sub != NULL);
4703                                                 if (mi)
4704                                                         mi = mi->next;
4705                                                 mime = NULL;
4706                                         }while(mi != NULL);
4707                                         if (op)
4708                                                 op = op->next;
4709                                         operation = NULL;
4710                                 }
4711                                 acontrol = acontrol->next;
4712                         }
4713
4714
4715                         fwrite(buf, 1, strlen(buf), file);
4716
4717 //                      strncpy(buftemp, buf, BUFMAX);
4718 //                      snprintf(buf, BUFMAX, "%s\n", buftemp);
4719 //                      fwrite(buf, 1, strlen(buf), file);
4720                 }
4721
4722                 fflush(file);
4723                 fd = fileno(file);
4724                 fsync(fd);
4725                 fclose(file);
4726                 if (action == ACTION_FOTA)
4727                         __ail_change_info(AIL_FOTA, mfx->uiapplication->appid, uid);
4728                 else
4729                         __ail_change_info(AIL_INSTALL, mfx->uiapplication->appid, uid);
4730         }
4731
4732         free(buf);
4733         free(buftemp);
4734
4735         return 0;
4736 }
4737
4738 static int __ps_remove_nativeapp_desktop(manifest_x *mfx, uid_t uid)
4739 {
4740         char filepath[PKG_STRING_LEN_MAX] = "";
4741         int ret = 0;
4742         uiapplication_x *uiapplication = mfx->uiapplication;
4743
4744         for(; uiapplication; uiapplication=uiapplication->next) {
4745                 snprintf(filepath, sizeof(filepath),"%s%s.desktop", getUserDesktopPath(uid), uiapplication->appid);
4746
4747                 __ail_change_info(AIL_REMOVE, uiapplication->appid, uid);
4748
4749                 ret = remove(filepath);
4750                 if (ret <0)
4751                         return -1;
4752         }
4753
4754         return 0;
4755 }
4756
4757 #define LIBAPPSVC_PATH LIB_PATH "/libappsvc.so.0"
4758
4759 static int __ps_remove_appsvc_db(manifest_x *mfx)
4760 {
4761         void *lib_handle = NULL;
4762         int (*appsvc_operation) (const char *);
4763         int ret = 0;
4764         uiapplication_x *uiapplication = mfx->uiapplication;
4765
4766         if ((lib_handle = dlopen(LIBAPPSVC_PATH, RTLD_LAZY)) == NULL) {
4767                 _LOGE("dlopen is failed LIBAIL_PATH[%s]\n", LIBAPPSVC_PATH);
4768                 goto END;
4769         }
4770
4771         if ((appsvc_operation =
4772                  dlsym(lib_handle, "appsvc_unset_defapp")) == NULL || dlerror() != NULL) {
4773                 _LOGE("can not find symbol \n");
4774                 goto END;
4775         }
4776
4777         for(; uiapplication; uiapplication=uiapplication->next) {
4778                 ret = appsvc_operation(uiapplication->appid);
4779                 if (ret <0)
4780                         _LOGE("can not operation  symbol \n");
4781         }
4782
4783 END:
4784         if (lib_handle)
4785                 dlclose(lib_handle);
4786
4787         return ret;
4788 }
4789
4790 #define PRELOAD_PACKAGE_LIST SYSCONFDIR "/package-manager/preload/preload_list.txt"
4791 static int __add_preload_info(manifest_x * mfx, const char *manifest, uid_t uid)
4792 {
4793         FILE *fp = NULL;
4794         char buffer[1024] = { 0 };
4795         int state = 0;
4796
4797         if(strstr(manifest, getUserManifestPath(uid))) {
4798                 free((void *)mfx->readonly);
4799                 mfx->readonly = strdup("True");
4800
4801                 free((void *)mfx->preload);
4802                 mfx->preload = strdup("True");
4803
4804                 free((void *)mfx->removable);
4805                 mfx->removable = strdup("False");
4806
4807                 free((void *)mfx->system);
4808                 mfx->system = strdup("True");
4809
4810                 return 0;
4811         }
4812
4813         fp = fopen(PRELOAD_PACKAGE_LIST, "r");
4814         if (fp == NULL) {
4815                 _LOGE("no preload list\n");
4816                 return -1;
4817         }
4818
4819         while (fgets(buffer, sizeof(buffer), fp) != NULL) {
4820                 if (buffer[0] == '#') {
4821                         if(strcasestr(buffer, "RW_NORM"))
4822                                 state = 2;
4823                         else if(strcasestr(buffer, "RW_RM"))
4824                                 state = 3;
4825                         else
4826                                 continue;
4827                 }
4828
4829                 __str_trim(buffer);
4830
4831                 if(!strcmp(mfx->package, buffer)) {
4832                         free((void *)mfx->preload);
4833                         mfx->preload = strdup("True");
4834                         if(state == 2){
4835                                 free((void *)mfx->readonly);
4836                                 mfx->readonly = strdup("False");
4837                                 free((void *)mfx->removable);
4838                                 mfx->removable = strdup("False");
4839                         } else if(state == 3){
4840                                 free((void *)mfx->readonly);
4841                                 mfx->readonly = strdup("False");
4842                                 free((void *)mfx->removable);
4843                                 mfx->removable = strdup("True");
4844                         }
4845                 }
4846
4847                 memset(buffer, 0x00, sizeof(buffer));
4848         }
4849
4850         if (fp != NULL)
4851                 fclose(fp);
4852
4853         return 0;
4854 }
4855
4856 static int __check_preload_updated(manifest_x * mfx, const char *manifest, uid_t uid)
4857 {
4858         char filepath[PKG_STRING_LEN_MAX] = "";
4859         int ret = 0;
4860         uiapplication_x *uiapplication = mfx->uiapplication;
4861
4862         if(strstr(manifest, getUserManifestPath(uid))) {
4863                 /* if preload app is updated, then remove previous desktop file on RW*/
4864                 for(; uiapplication; uiapplication=uiapplication->next) {
4865                                 snprintf(filepath, sizeof(filepath),"%s%s.desktop", getUserDesktopPath(uid), uiapplication->appid);
4866                         ret = remove(filepath);
4867                         if (ret <0)
4868                                 return -1;
4869                 }
4870         } else {
4871                 /* if downloaded app is updated, then update tag set true*/
4872                 free((void *)mfx->update);
4873                 mfx->update = strdup("true");
4874         }
4875
4876         return 0;
4877 }
4878
4879
4880 API int pkgmgr_parser_create_desktop_file(manifest_x *mfx)
4881 {
4882         int ret = 0;
4883         if (mfx == NULL) {
4884                 _LOGD("Manifest pointer is NULL\n");
4885                 return -1;
4886         }
4887         ret = __ps_make_nativeapp_desktop(mfx, NULL, ACTION_INSTALL, GLOBAL_USER);
4888         if (ret == -1)
4889                 _LOGD("Creating desktop file failed\n");
4890         else
4891                 _LOGD("Creating desktop file Success\n");
4892         return ret;
4893 }
4894
4895 API int pkgmgr_parser_create_usr_desktop_file(manifest_x *mfx, uid_t uid)
4896 {
4897         int ret = 0;
4898         if (mfx == NULL) {
4899                 _LOGD("Manifest pointer is NULL\n");
4900                 return -1;
4901         }
4902         ret = __ps_make_nativeapp_desktop(mfx, NULL, ACTION_INSTALL, uid);
4903         if (ret == -1)
4904                 _LOGD("Creating desktop file failed\n");
4905         else
4906                 _LOGD("Creating desktop file Success\n");
4907         return ret;
4908 }
4909
4910
4911 API void pkgmgr_parser_free_manifest_xml(manifest_x *mfx)
4912 {
4913         if (mfx == NULL)
4914                 return;
4915         if (mfx->ns) {
4916                 free((void *)mfx->ns);
4917                 mfx->ns = NULL;
4918         }
4919         if (mfx->package) {
4920                 free((void *)mfx->package);
4921                 mfx->package = NULL;
4922         }
4923         if (mfx->version) {
4924                 free((void *)mfx->version);
4925                 mfx->version = NULL;
4926         }
4927         if (mfx->installlocation) {
4928                 free((void *)mfx->installlocation);
4929                 mfx->installlocation = NULL;
4930         }
4931         if (mfx->preload) {
4932                 free((void *)mfx->preload);
4933                 mfx->preload = NULL;
4934         }
4935         if (mfx->readonly) {
4936                 free((void *)mfx->readonly);
4937                 mfx->readonly = NULL;
4938         }
4939         if (mfx->removable) {
4940                 free((void *)mfx->removable);
4941                 mfx->removable = NULL;
4942         }
4943         if (mfx->update) {
4944                 free((void *)mfx->update);
4945                 mfx->update = NULL;
4946         }
4947         if (mfx->system) {
4948                 free((void *)mfx->system);
4949                 mfx->system = NULL;
4950         }
4951         if (mfx->type) {
4952                 free((void *)mfx->type);
4953                 mfx->type = NULL;
4954         }
4955         if (mfx->package_size) {
4956                 free((void *)mfx->package_size);
4957                 mfx->package_size = NULL;
4958         }
4959         if (mfx->installed_time) {
4960                 free((void *)mfx->installed_time);
4961                 mfx->installed_time = NULL;
4962         }
4963         if (mfx->installed_storage) {
4964                 free((void *)mfx->installed_storage);
4965                 mfx->installed_storage = NULL;
4966         }
4967         if (mfx->storeclient_id) {
4968                 free((void *)mfx->storeclient_id);
4969                 mfx->storeclient_id = NULL;
4970         }
4971         if (mfx->mainapp_id) {
4972                 free((void *)mfx->mainapp_id);
4973                 mfx->mainapp_id = NULL;
4974         }
4975         if (mfx->package_url) {
4976                 free((void *)mfx->package_url);
4977                 mfx->package_url = NULL;
4978         }
4979         if (mfx->root_path) {
4980                 free((void *)mfx->root_path);
4981                 mfx->root_path = NULL;
4982         }
4983         if (mfx->csc_path) {
4984                 free((void *)mfx->csc_path);
4985                 mfx->csc_path = NULL;
4986         }
4987         if (mfx->appsetting) {
4988                 free((void *)mfx->appsetting);
4989                 mfx->appsetting = NULL;
4990         }
4991         if (mfx->nodisplay_setting) {
4992                 free((void *)mfx->nodisplay_setting);
4993                 mfx->nodisplay_setting = NULL;
4994         }
4995         if (mfx->main_package) {
4996                 free((void *)mfx->main_package);
4997                 mfx->main_package = NULL;
4998         }
4999
5000         /*Free Icon*/
5001         if (mfx->icon) {
5002                 icon_x *icon = mfx->icon;
5003                 icon_x *tmp = NULL;
5004                 while(icon != NULL) {
5005                         tmp = icon->next;
5006                         __ps_free_icon(icon);
5007                         icon = tmp;
5008                 }
5009         }
5010         /*Free Label*/
5011         if (mfx->label) {
5012                 label_x *label = mfx->label;
5013                 label_x *tmp = NULL;
5014                 while(label != NULL) {
5015                         tmp = label->next;
5016                         __ps_free_label(label);
5017                         label = tmp;
5018                 }
5019         }
5020         /*Free Author*/
5021         if (mfx->author) {
5022                 author_x *author = mfx->author;
5023                 author_x *tmp = NULL;
5024                 while(author != NULL) {
5025                         tmp = author->next;
5026                         __ps_free_author(author);
5027                         author = tmp;
5028                 }
5029         }
5030         /*Free Description*/
5031         if (mfx->description) {
5032                 description_x *description = mfx->description;
5033                 description_x *tmp = NULL;
5034                 while(description != NULL) {
5035                         tmp = description->next;
5036                         __ps_free_description(description);
5037                         description = tmp;
5038                 }
5039         }
5040         /*Free License*/
5041         if (mfx->license) {
5042                 license_x *license = mfx->license;
5043                 license_x *tmp = NULL;
5044                 while(license != NULL) {
5045                         tmp = license->next;
5046                         __ps_free_license(license);
5047                         license = tmp;
5048                 }
5049         }
5050         /*Free Privileges*/
5051         if (mfx->privileges) {
5052                 privileges_x *privileges = mfx->privileges;
5053                 privileges_x *tmp = NULL;
5054                 while(privileges != NULL) {
5055                         tmp = privileges->next;
5056                         __ps_free_privileges(privileges);
5057                         privileges = tmp;
5058                 }
5059         }
5060         /*Free UiApplication*/
5061         if (mfx->uiapplication) {
5062                 uiapplication_x *uiapplication = mfx->uiapplication;
5063                 uiapplication_x *tmp = NULL;
5064                 while(uiapplication != NULL) {
5065                         tmp = uiapplication->next;
5066                         __ps_free_uiapplication(uiapplication);
5067                         uiapplication = tmp;
5068                 }
5069         }
5070         /*Free ServiceApplication*/
5071         if (mfx->serviceapplication) {
5072                 serviceapplication_x *serviceapplication = mfx->serviceapplication;
5073                 serviceapplication_x *tmp = NULL;
5074                 while(serviceapplication != NULL) {
5075                         tmp = serviceapplication->next;
5076                         __ps_free_serviceapplication(serviceapplication);
5077                         serviceapplication = tmp;
5078                 }
5079         }
5080         /*Free Daemon*/
5081         if (mfx->daemon) {
5082                 daemon_x *daemon = mfx->daemon;
5083                 daemon_x *tmp = NULL;
5084                 while(daemon != NULL) {
5085                         tmp = daemon->next;
5086                         __ps_free_daemon(daemon);
5087                         daemon = tmp;
5088                 }
5089         }
5090         /*Free Theme*/
5091         if (mfx->theme) {
5092                 theme_x *theme = mfx->theme;
5093                 theme_x *tmp = NULL;
5094                 while(theme != NULL) {
5095                         tmp = theme->next;
5096                         __ps_free_theme(theme);
5097                         theme = tmp;
5098                 }
5099         }
5100         /*Free Font*/
5101         if (mfx->font) {
5102                 font_x *font = mfx->font;
5103                 font_x *tmp = NULL;
5104                 while(font != NULL) {
5105                         tmp = font->next;
5106                         __ps_free_font(font);
5107                         font = tmp;
5108                 }
5109         }
5110         /*Free Ime*/
5111         if (mfx->ime) {
5112                 ime_x *ime = mfx->ime;
5113                 ime_x *tmp = NULL;
5114                 while(ime != NULL) {
5115                         tmp = ime->next;
5116                         __ps_free_ime(ime);
5117                         ime = tmp;
5118                 }
5119         }
5120         /*Free Compatibility*/
5121         if (mfx->compatibility) {
5122                 compatibility_x *compatibility = mfx->compatibility;
5123                 compatibility_x *tmp = NULL;
5124                 while(compatibility != NULL) {
5125                         tmp = compatibility->next;
5126                         __ps_free_compatibility(compatibility);
5127                         compatibility = tmp;
5128                 }
5129         }
5130         /*Free DeviceProfile*/
5131         if (mfx->deviceprofile) {
5132                 deviceprofile_x *deviceprofile = mfx->deviceprofile;
5133                 deviceprofile_x *tmp = NULL;
5134                 while(deviceprofile != NULL) {
5135                         tmp = deviceprofile->next;
5136                         __ps_free_deviceprofile(deviceprofile);
5137                         deviceprofile = tmp;
5138                 }
5139         }
5140         free((void*)mfx);
5141         mfx = NULL;
5142         return;
5143 }
5144
5145 API manifest_x *pkgmgr_parser_process_manifest_xml(const char *manifest)
5146 {
5147         _LOGD("parsing start pkgmgr_parser_process_manifest_xml\n");
5148         xmlTextReaderPtr reader;
5149         manifest_x *mfx = NULL;
5150
5151         reader = xmlReaderForFile(manifest, NULL, 0);
5152         if (reader) {
5153                 mfx = malloc(sizeof(manifest_x));
5154                 if (mfx) {
5155                         memset(mfx, '\0', sizeof(manifest_x));
5156                         if (__process_manifest(reader, mfx, GLOBAL_USER) < 0) {
5157                                 _LOGD("Parsing Failed\n");
5158                                 pkgmgr_parser_free_manifest_xml(mfx);
5159                                 mfx = NULL;
5160                         } else
5161                                 _LOGD("Parsing Success\n");
5162                 } else {
5163                         _LOGD("Memory allocation error\n");
5164                 }
5165                 xmlFreeTextReader(reader);
5166         } else {
5167                 _LOGD("Unable to create xml reader\n");
5168         }
5169         return mfx;
5170 }
5171
5172
5173 API manifest_x *pkgmgr_parser_usr_process_manifest_xml(const char *manifest, uid_t uid)
5174 {
5175         _LOGD("parsing start pkgmgr_parser_usr_process_manifest_xml\n");
5176         xmlTextReaderPtr reader;
5177         manifest_x *mfx = NULL;
5178
5179         reader = xmlReaderForFile(manifest, NULL, 0);
5180         if (reader) {
5181                 mfx = malloc(sizeof(manifest_x));
5182                 if (mfx) {
5183                         memset(mfx, '\0', sizeof(manifest_x));
5184                         if (__process_manifest(reader, mfx, uid) < 0) {
5185                                 _LOGD("Parsing Failed\n");
5186                                 pkgmgr_parser_free_manifest_xml(mfx);
5187                                 mfx = NULL;
5188                         } else
5189                                 _LOGD("Parsing Success\n");
5190                 } else {
5191                         _LOGD("Memory allocation error\n");
5192                 }
5193                 xmlFreeTextReader(reader);
5194         } else {
5195                 _LOGD("Unable to create xml reader\n");
5196         }
5197         return mfx;
5198 }
5199
5200 /* These APIs are intended to call parser directly */
5201
5202 API int pkgmgr_parser_parse_manifest_for_installation(const char *manifest, char *const tagv[])
5203 {
5204 //      char *temp[] = {"shortcut-list", "livebox", "account", "notifications", "privileges", "ime", "font", NULL};
5205         retvm_if(manifest == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
5206         _LOGD("parsing manifest for installation: %s\n", manifest);
5207
5208         manifest_x *mfx = NULL;
5209         int ret = -1;
5210
5211         xmlInitParser();
5212         mfx = pkgmgr_parser_process_manifest_xml(manifest);
5213         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
5214
5215         _LOGD("Parsing Finished\n");
5216
5217 //      __streamFile(manifest, ACTION_INSTALL, temp, mfx->package);
5218         __ps_process_tag_parser(mfx, manifest, ACTION_INSTALL);
5219         __add_preload_info(mfx, manifest, GLOBAL_USER);
5220
5221         _LOGD("Added preload infomation\n");
5222
5223         __ps_process_tag(mfx, tagv);
5224
5225         ret = pkgmgr_parser_insert_manifest_info_in_db(mfx);
5226         retvm_if(ret == PMINFO_R_ERROR, PMINFO_R_ERROR, "DB Insert failed");
5227
5228         _LOGD("DB Insert Success\n");
5229
5230         ret = __ps_process_metadata_parser(mfx, ACTION_INSTALL);
5231         if (ret == -1)
5232                 _LOGD("Creating metadata parser failed\n");
5233
5234         ret = __ps_process_category_parser(mfx, ACTION_INSTALL);
5235         if (ret == -1)
5236                 _LOGD("Creating category parser failed\n");
5237
5238         if (__check_action_fota(tagv))
5239                 ret = __ps_make_nativeapp_desktop(mfx, NULL, ACTION_FOTA, GLOBAL_USER);
5240         else
5241                 ret = __ps_make_nativeapp_desktop(mfx, NULL, ACTION_INSTALL, GLOBAL_USER);
5242
5243         if (ret == -1)
5244                 _LOGD("Creating desktop file failed\n");
5245         else
5246                 _LOGD("Creating desktop file Success\n");
5247
5248         pkgmgr_parser_free_manifest_xml(mfx);
5249         _LOGD("Free Done\n");
5250         xmlCleanupParser();
5251
5252         return PMINFO_R_OK;
5253 }
5254 API int pkgmgr_parser_parse_usr_manifest_for_installation(const char *manifest, uid_t uid, char *const tagv[])
5255 {
5256 //      char *temp[] = {"shortcut-list", "livebox", "account", "notifications", "privileges", "ime", "font", NULL};
5257         retvm_if(manifest == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
5258         _LOGD("parsing manifest for installation: %s\n", manifest);
5259         manifest_x *mfx = NULL;
5260         int ret = -1;
5261
5262         xmlInitParser();
5263         mfx = pkgmgr_parser_usr_process_manifest_xml(manifest, uid);
5264         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
5265
5266         _LOGD("Parsing Finished\n");
5267 //      __streamFile(manifest, ACTION_INSTALL, temp, mfx->package);
5268         __ps_process_tag_parser(mfx, manifest, ACTION_INSTALL);
5269         __add_preload_info(mfx, manifest, uid);
5270
5271         _LOGD("Added preload infomation\n");
5272         __ps_process_tag(mfx, tagv);
5273
5274         ret = pkgmgr_parser_insert_manifest_info_in_usr_db(mfx, uid);
5275         retvm_if(ret == PMINFO_R_ERROR, PMINFO_R_ERROR, "DB Insert failed");
5276
5277         _LOGD("DB Insert Success\n");
5278         ret = __ps_process_metadata_parser(mfx, ACTION_INSTALL);
5279         if (ret == -1)
5280                 _LOGD("Creating metadata parser failed\n");
5281         ret = __ps_process_category_parser(mfx, ACTION_INSTALL);
5282         if (ret == -1)
5283                 _LOGD("Creating category parser failed\n");
5284
5285         if (__check_action_fota(tagv))
5286                 ret = __ps_make_nativeapp_desktop(mfx, NULL, ACTION_FOTA, uid);
5287         else
5288                 ret = __ps_make_nativeapp_desktop(mfx, NULL, ACTION_INSTALL, uid);
5289
5290         if (ret == -1)
5291                 _LOGD("Creating desktop file failed\n");
5292         else
5293                 _LOGD("Creating desktop file Success\n");
5294         pkgmgr_parser_free_manifest_xml(mfx);
5295         _LOGD("Free Done\n");
5296         xmlCleanupParser();
5297
5298         return PMINFO_R_OK;
5299 }
5300
5301 API int pkgmgr_parser_parse_manifest_for_upgrade(const char *manifest, char *const tagv[])
5302 {
5303 //      char *temp[] = {"shortcut-list", "livebox", "account", "notifications", "privileges", "ime", "font", NULL};
5304         retvm_if(manifest == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
5305         _LOGD("pkgmgr_parser_parse_manifest_for_upgrade  parsing manifest for upgradation: %s\n", manifest);
5306         manifest_x *mfx = NULL;
5307         int ret = -1;
5308         bool preload = false;
5309         bool system = false;
5310         char *csc_path = NULL;
5311         pkgmgrinfo_pkginfo_h handle = NULL;
5312
5313         xmlInitParser();
5314         mfx = pkgmgr_parser_process_manifest_xml(manifest);
5315         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
5316
5317         _LOGD("Parsing Finished\n");
5318 //      __streamFile(manifest, ACTION_UPGRADE, temp, mfx->package);
5319         __ps_process_tag_parser(mfx, manifest, ACTION_UPGRADE);
5320         __add_preload_info(mfx, manifest, GLOBAL_USER);
5321         _LOGD("Added preload infomation\n");
5322         __check_preload_updated(mfx, manifest, GLOBAL_USER);
5323
5324         ret = pkgmgrinfo_pkginfo_get_pkginfo(mfx->package, &handle);
5325         if (ret != PMINFO_R_OK)
5326                 _LOGD("pkgmgrinfo_pkginfo_get_pkginfo failed\n");
5327         ret = pkgmgrinfo_pkginfo_is_preload(handle, &preload);
5328         if (ret != PMINFO_R_OK)
5329                 _LOGD("pkgmgrinfo_pkginfo_is_preload failed\n");
5330
5331         if (preload) {
5332                 free((void *)mfx->preload);
5333                 mfx->preload = strdup("true");
5334         }
5335
5336         ret = pkgmgrinfo_pkginfo_is_system(handle, &system);
5337         if (ret != PMINFO_R_OK)
5338                 _LOGD("pkgmgrinfo_pkginfo_is_system failed\n");
5339         if (system) {
5340                 free((void *)mfx->system);
5341                 mfx->system = strdup("true");
5342         }
5343
5344         ret = pkgmgrinfo_pkginfo_get_csc_path(handle, &csc_path);
5345         if (ret != PMINFO_R_OK)
5346                 _LOGD("pkgmgrinfo_pkginfo_get_csc_path failed\n");
5347         
5348         if (csc_path != NULL) {
5349                 if (mfx->csc_path)
5350                         free((void *)mfx->csc_path);
5351                 mfx->csc_path = strdup(csc_path);
5352         }
5353
5354         ret = pkgmgr_parser_update_manifest_info_in_db(mfx);
5355         retvm_if(ret == PMINFO_R_ERROR, PMINFO_R_ERROR, "DB Insert failed");
5356         _LOGD("DB Update Success\n");
5357         ret = __ps_process_metadata_parser(mfx, ACTION_UPGRADE);
5358         if (ret == -1){
5359                 _LOGD("Upgrade metadata parser failed\n");
5360         }
5361         ret = __ps_process_category_parser(mfx, ACTION_UPGRADE);
5362         if (ret == -1)
5363                 _LOGD("Creating category parser failed\n");
5364         ret = __ps_make_nativeapp_desktop(mfx, manifest, ACTION_UPGRADE, GLOBAL_USER);
5365         if (ret == -1)
5366                 _LOGD("Creating desktop file failed\n");
5367         else
5368                 _LOGD("Creating desktop file Success\n");
5369         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
5370         pkgmgr_parser_free_manifest_xml(mfx);
5371         _LOGD("Free Done\n");
5372         xmlCleanupParser();
5373
5374         return PMINFO_R_OK;
5375 }
5376
5377 API int pkgmgr_parser_parse_usr_manifest_for_upgrade(const char *manifest, uid_t uid, char *const tagv[])
5378 {
5379 //      char *temp[] = {"shortcut-list", "livebox", "account", "notifications", "privileges", "ime", "font", NULL};
5380         retvm_if(manifest == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
5381         _LOGD(" pkgmgr_parser_parse_usr_manifest_for_upgrade parsing manifest for upgradation: %s\n", manifest);
5382         manifest_x *mfx = NULL;
5383         int ret = -1;
5384         bool preload = false;
5385         bool system = false;
5386         char *csc_path = NULL;
5387         pkgmgrinfo_pkginfo_h handle = NULL;
5388
5389         xmlInitParser();
5390         mfx = pkgmgr_parser_usr_process_manifest_xml(manifest, uid);
5391         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
5392
5393         _LOGD("Parsing Finished\n");
5394         //__streamFile(manifest, ACTION_UPGRADE, temp, mfx->package);
5395         __ps_process_tag_parser(mfx, manifest, ACTION_UPGRADE);
5396         __add_preload_info(mfx, manifest, uid);
5397         _LOGD("Added preload infomation\n");
5398         _LOGE("Added preload infomation\n");
5399         __check_preload_updated(mfx, manifest, uid);
5400
5401         ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(mfx->package, uid, &handle);
5402         if (ret != PMINFO_R_OK)
5403                 _LOGD("pkgmgrinfo_pkginfo_get_pkginfo failed\n");
5404         ret = pkgmgrinfo_pkginfo_is_preload(handle, &preload);
5405         if (ret != PMINFO_R_OK)
5406                 _LOGD("pkgmgrinfo_pkginfo_is_preload failed\n");
5407
5408         if (preload) {
5409                 free((void *)mfx->preload);
5410                 mfx->preload = strdup("true");
5411         }
5412
5413         ret = pkgmgrinfo_pkginfo_is_system(handle, &system);
5414         if (ret != PMINFO_R_OK)
5415                 _LOGD("pkgmgrinfo_pkginfo_is_system failed\n");
5416
5417         if (system) {
5418                 free((void *)mfx->system);
5419                 mfx->system = strdup("true");
5420         }
5421
5422         ret = pkgmgrinfo_pkginfo_get_csc_path(handle, &csc_path);
5423         if (ret != PMINFO_R_OK)
5424                 _LOGD("pkgmgrinfo_pkginfo_get_csc_path failed\n");
5425         if (csc_path != NULL) {
5426                 if (mfx->csc_path)
5427                         free((void *)mfx->csc_path);
5428                 mfx->csc_path = strdup(csc_path);
5429         }
5430
5431         ret = pkgmgr_parser_update_manifest_info_in_usr_db(mfx, uid);
5432         retvm_if(ret == PMINFO_R_ERROR, PMINFO_R_ERROR, "DB Insert failed");
5433         _LOGD("DB Update Success\n");
5434         _LOGE("DB Update Success\n" );
5435         ret = __ps_process_metadata_parser(mfx, ACTION_UPGRADE);
5436         if (ret == -1)
5437                 _LOGD("Upgrade metadata parser failed\n");
5438         ret = __ps_process_category_parser(mfx, ACTION_UPGRADE);
5439         if (ret == -1)
5440                 _LOGD("Creating category parser failed\n");
5441         ret = __ps_make_nativeapp_desktop(mfx, manifest, ACTION_UPGRADE, uid);
5442         if (ret == -1)
5443                 _LOGD("Creating desktop file failed\n");
5444         else
5445                 _LOGD("Creating desktop file Success\n");
5446         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
5447         pkgmgr_parser_free_manifest_xml(mfx);
5448         _LOGD("Free Done\n");
5449         xmlCleanupParser();
5450
5451         return PMINFO_R_OK;
5452 }
5453
5454 API int pkgmgr_parser_parse_manifest_for_uninstallation(const char *manifest, char *const tagv[])
5455 {
5456 //      char *temp[] = {"shortcut-list", "livebox", "account", "notifications", "privileges", "ime", "font", NULL};
5457         retvm_if(manifest == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
5458         _LOGD("parsing manifest for uninstallation: %s\n", manifest);
5459
5460         manifest_x *mfx = NULL;
5461         int ret = -1;
5462         xmlInitParser();
5463         mfx = pkgmgr_parser_process_manifest_xml(manifest);
5464         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
5465
5466         _LOGD("Parsing Finished\n");
5467
5468 //      __streamFile(manifest, ACTION_UNINSTALL, temp, mfx->package);
5469         __ps_process_tag_parser(mfx, manifest, ACTION_UNINSTALL);
5470
5471         __add_preload_info(mfx, manifest, GLOBAL_USER);
5472         _LOGD("Added preload infomation\n");
5473
5474         ret = __ps_process_metadata_parser(mfx, ACTION_UNINSTALL);
5475         if (ret == -1)
5476                 _LOGD("Removing metadata parser failed\n");
5477
5478         ret = __ps_process_category_parser(mfx, ACTION_UNINSTALL);
5479         if (ret == -1)
5480                 _LOGD("Creating category parser failed\n");
5481
5482         ret = pkgmgr_parser_delete_manifest_info_from_db(mfx);
5483         if (ret == -1)
5484                 _LOGD("DB Delete failed\n");
5485         else
5486                 _LOGD("DB Delete Success\n");
5487
5488         ret = __ps_remove_nativeapp_desktop(mfx, GLOBAL_USER);
5489         if (ret == -1)
5490                 _LOGD("Removing desktop file failed\n");
5491         else
5492                 _LOGD("Removing desktop file Success\n");
5493
5494         ret = __ps_remove_appsvc_db(mfx);
5495         if (ret == -1)
5496                 _LOGD("Removing appsvc_db failed\n");
5497         else
5498                 _LOGD("Removing appsvc_db Success\n");
5499
5500         pkgmgr_parser_free_manifest_xml(mfx);
5501         _LOGD("Free Done\n");
5502         xmlCleanupParser();
5503
5504         return PMINFO_R_OK;
5505 }
5506
5507
5508 API int pkgmgr_parser_parse_usr_manifest_for_uninstallation(const char *manifest, uid_t uid, char *const tagv[])
5509 {
5510 //      char *temp[] = {"shortcut-list", "livebox", "account", "notifications", "privileges", "ime", "font", NULL};
5511         retvm_if(manifest == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
5512         _LOGD("parsing manifest for uninstallation: %s\n", manifest);
5513
5514         manifest_x *mfx = NULL;
5515         int ret = -1;
5516         xmlInitParser();
5517         mfx = pkgmgr_parser_usr_process_manifest_xml(manifest, uid);
5518         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
5519
5520         _LOGD("Parsing Finished\n");
5521
5522 //      __streamFile(manifest, ACTION_UNINSTALL, temp, mfx->package);
5523         __ps_process_tag_parser(mfx, manifest, ACTION_UNINSTALL);
5524
5525         __add_preload_info(mfx, manifest, uid);
5526         _LOGD("Added preload infomation\n");
5527
5528         ret = __ps_process_metadata_parser(mfx, ACTION_UNINSTALL);
5529         if (ret == -1)
5530                 _LOGD("Removing metadata parser failed\n");
5531
5532         ret = __ps_process_category_parser(mfx, ACTION_UNINSTALL);
5533         if (ret == -1)
5534                 _LOGD("Creating category parser failed\n");
5535
5536         ret = pkgmgr_parser_delete_manifest_info_from_usr_db(mfx, uid);
5537         if (ret == -1)
5538                 _LOGD("DB Delete failed\n");
5539         else
5540                 _LOGD("DB Delete Success\n");
5541
5542         ret = __ps_remove_nativeapp_desktop(mfx, uid);
5543         if (ret == -1)
5544                 _LOGD("Removing desktop file failed\n");
5545         else
5546                 _LOGD("Removing desktop file Success\n");
5547
5548         ret = __ps_remove_appsvc_db(mfx);
5549         if (ret == -1)
5550                 _LOGD("Removing appsvc_db failed\n");
5551         else
5552                 _LOGD("Removing appsvc_db Success\n");
5553
5554         pkgmgr_parser_free_manifest_xml(mfx);
5555         _LOGD("Free Done\n");
5556         xmlCleanupParser();
5557
5558         return PMINFO_R_OK;
5559 }
5560
5561 API int pkgmgr_parser_parse_manifest_for_preload()
5562 {
5563         return pkgmgr_parser_update_preload_info_in_db();
5564 }
5565
5566 API int pkgmgr_parser_parse_usr_manifest_for_preload(uid_t uid)
5567 {
5568         return pkgmgr_parser_update_preload_info_in_usr_db(uid);
5569 }
5570
5571
5572 API char *pkgmgr_parser_get_usr_manifest_file(const char *pkgid, uid_t uid)
5573 {
5574         return __pkgid_to_manifest(pkgid, uid);
5575 }
5576
5577 API char *pkgmgr_parser_get_manifest_file(const char *pkgid)
5578 {
5579         return __pkgid_to_manifest(pkgid, GLOBAL_USER);
5580 }
5581
5582 API int pkgmgr_parser_run_parser_for_installation(xmlDocPtr docPtr, const char *tag, const char *pkgid)
5583 {
5584         return __ps_run_parser(docPtr, tag, ACTION_INSTALL, pkgid);
5585 }
5586
5587 API int pkgmgr_parser_run_parser_for_upgrade(xmlDocPtr docPtr, const char *tag, const char *pkgid)
5588 {
5589         return __ps_run_parser(docPtr, tag, ACTION_UPGRADE, pkgid);
5590 }
5591
5592 API int pkgmgr_parser_run_parser_for_uninstallation(xmlDocPtr docPtr, const char *tag, const char *pkgid)
5593 {
5594         return __ps_run_parser(docPtr, tag, ACTION_UNINSTALL, pkgid);
5595 }
5596
5597 #define SCHEMA_FILE SYSCONFDIR "/package-manager/preload/manifest.xsd"
5598 #if 1
5599 API int pkgmgr_parser_check_manifest_validation(const char *manifest)
5600 {
5601         if (manifest == NULL) {
5602                 _LOGE("manifest file is NULL\n");
5603                 return PMINFO_R_EINVAL;
5604         }
5605         int ret = -1;
5606         xmlSchemaParserCtxtPtr ctx;
5607         xmlSchemaValidCtxtPtr vctx;
5608         xmlSchemaPtr xschema;
5609         ctx = xmlSchemaNewParserCtxt(SCHEMA_FILE);
5610         if (ctx == NULL) {
5611                 _LOGE("xmlSchemaNewParserCtxt() Failed\n");
5612                 return PMINFO_R_ERROR;
5613         }
5614         xschema = xmlSchemaParse(ctx);
5615         if (xschema == NULL) {
5616                 _LOGE("xmlSchemaParse() Failed\n");
5617                 return PMINFO_R_ERROR;
5618         }
5619         vctx = xmlSchemaNewValidCtxt(xschema);
5620         if (vctx == NULL) {
5621                 _LOGE("xmlSchemaNewValidCtxt() Failed\n");
5622                 return PMINFO_R_ERROR;
5623         }
5624         xmlSchemaSetValidErrors(vctx, (xmlSchemaValidityErrorFunc) fprintf, (xmlSchemaValidityWarningFunc) fprintf, stderr);
5625         ret = xmlSchemaValidateFile(vctx, manifest, 0);
5626         if (ret == -1) {
5627                 _LOGE("xmlSchemaValidateFile() failed\n");
5628                 return PMINFO_R_ERROR;
5629         } else if (ret == 0) {
5630                 _LOGE("Manifest is Valid\n");
5631                 return PMINFO_R_OK;
5632         } else {
5633                 _LOGE("Manifest Validation Failed with error code %d\n", ret);
5634                 return PMINFO_R_ERROR;
5635         }
5636         return PMINFO_R_OK;
5637 }
5638
5639 #else
5640 API int pkgmgr_parser_check_manifest_validation(const char *manifest)
5641 {
5642         int err = 0;
5643         int status = 0;
5644         pid_t pid;
5645
5646         pid = fork();
5647
5648         switch (pid) {
5649         case -1:
5650                 _LOGE("fork failed\n");
5651                 return -1;
5652         case 0:
5653                 /* child */
5654                 {
5655                         int dev_null_fd = open ("/dev/null", O_RDWR);
5656                         if (dev_null_fd >= 0)
5657                         {
5658                                 dup2 (dev_null_fd, 0);/*stdin*/
5659                                 dup2 (dev_null_fd, 1);/*stdout*/
5660                                 dup2 (dev_null_fd, 2);/*stderr*/
5661                         }
5662
5663                         if (execl("/usr/bin/xmllint", "xmllint", manifest, "--schema",
5664                                 SCHEMA_FILE, NULL) < 0) {
5665                                 _LOGE("execl error\n");
5666                         }
5667
5668                         _exit(100);
5669                 }
5670         default:
5671                 /* parent */
5672                 break;
5673         }
5674
5675         while ((err = waitpid(pid, &status, WNOHANG)) != pid) {
5676                 if (err < 0) {
5677                         if (errno == EINTR)
5678                                 continue;
5679                         _LOGE("waitpid failed\n");
5680                         return -1;
5681                 }
5682         }
5683
5684
5685         if(WIFEXITED(status) && !WEXITSTATUS(status))
5686                 return 0;
5687         else
5688                 return -1;
5689 }
5690 #endif