Tizen 2.4.0 rev3 SDK Public Release
[framework/base/rpm-installer.git] / backend / src / rpm / rpm-installer.c
index 864a2cb..462390f 100755 (executable)
@@ -61,6 +61,7 @@
 #include <package-manager.h>
 #include <privilege-control.h>
 #include <app_manager.h>
+#include <app_manager_extension.h>
 #include <aul.h>
 #include <dlfcn.h>
 #define APP2EXT_ENABLE
 #include <app2ext_interface.h>
 #endif
 
-#include "rpm-installer-util.h"
 #include "rpm-installer-signature.h"
 #include "rpm-installer.h"
 #include "rpm-frontend.h"
-#include "rpm-installer-type.h"
+#include "installer-type.h"
+#include "installer-util.h"
 #include "coretpk-installer-internal.h"
-
-
-enum rpm_sig_type {
-       SIG_AUTH,
-       SIG_DIST1,
-       SIG_DIST2,
-};
-
-enum rpm_sig_sub_type {
-       SIG_SIGNER,
-       SIG_INTERMEDIATE,
-       SIG_ROOT,
-};
-
-typedef struct cert_chain_t {
-       int cert_type;
-       char *cert_value;
-} cert_chain;
-
-cert_chain list[MAX_CERT_NUM];
-GList *privilege_list;
-
-#define PKGMGR_DB              "/opt/dbspace/.pkgmgr_parser.db"
-#define EXT_APPDATA_PRIVILEGE_NAME     "http://tizen.org/privilege/externalstorage.appdata"
+#include "pkgmgr_parser_resource.h"
 
 extern char *gpkgname;
 extern int sig_enable;
+char *sig1_capath;
+int sig1_visibility;
 
 static void __rpm_process_line(char *line);
 static void __rpm_perform_read(int fd);
@@ -111,9 +91,7 @@ static int __ri_create_cert_chain(int sigtype, int sigsubtype, char *value);
 static void __ri_free_cert_chain(void);
 static char *__ri_get_cert_from_file(const char *file);
 static int __privilege_func(const char *name, void *user_data);
-static char *__ri_get_str(const char* str, const char* pKey);
-static void __ri_xmlsec_debug_print(const char* file, int line, const char* func,
-                                                                       const char* errorObject, const char* errorSubject, int reason, const char* msg);
+static void __ri_xmlsec_debug_print(const char *file, int line, const char *func, const char *errorObject, const char *errorSubject, int reason, const char *msg);
 
 int _ri_set_group_id(const char *pkgid, const char *groupid);
 
@@ -136,131 +114,87 @@ static void __str_trim(char *input)
        return;
 }
 
-static int __ri_check_pkgid_for_deactivation(const char *pkgid)
-{
-       FILE *fp = NULL;
-       char deactivation_str[FILENAME_MAX] = { 0 };
-       char *deactivation_pkgid = NULL;
-       char *deactivation_state = NULL;
-
-       fp = fopen(DEACTIVATION_PKGID_LIST, "r");
-       if (fp == NULL) {
-               _LOGE("fopen fail\n");
-               return 0;
-       }
-
-       while (fgets(deactivation_str, sizeof(deactivation_str), fp) != NULL) {
-               __str_trim(deactivation_str);
-
-               deactivation_pkgid = __ri_get_str(deactivation_str, TOKEN_PKGID_STR);
-               if(deactivation_pkgid == NULL)
-                       continue;
-
-               deactivation_state = __ri_get_str(deactivation_str, TOKEN_STATE_STR);
-               if(deactivation_state == NULL) {
-                       free(deactivation_pkgid);
-                       continue;
-               }
-
-               if ((strcmp(deactivation_pkgid, pkgid) == 0) && (strcmp(deactivation_state, "off") == 0)) {
-                       fclose(fp);
-                       free(deactivation_pkgid);
-                       free(deactivation_state);
-                       _LOGE("Find pkgid[%s] form deactivation list.\n", pkgid);
-                       return -1;
-               }
-
-               free(deactivation_pkgid);
-               free(deactivation_state);
-               memset(deactivation_str, 0x00, sizeof(deactivation_str));
-       }
-
-       if (fp != NULL)
-               fclose(fp);
-
-       return 0;
-
-}
-
 static int __ri_get_op_type(char *op_str)
 {
-       if (strcmp(op_str,"install")==0)
+       if (strcmp(op_str, "install") == 0)
                return INSTALL_REQ;
-       else if (strcmp(op_str,"update")==0)
+       else if (strcmp(op_str, "update") == 0)
                return UPGRADE_REQ;
-       else if (strcmp(op_str,"uninstall")==0)
+       else if (strcmp(op_str, "uninstall") == 0)
                return UNINSTALL_REQ;
        else
                return -1;
 }
 
-static char *__ri_get_str(const char* str, const char* pKey)
-{
-       const char* p = NULL;
-       const char* pStart = NULL;
-       const char* pEnd = NULL;
+int __get_version_from_xml(char* manifest, char** version){
 
-       if (str == NULL)
-               return NULL;
+       const char *val = NULL;
+       const xmlChar *node;
+       xmlTextReaderPtr reader;
+       int ret = PMINFO_R_OK;
 
-       char *pBuf = strdup(str);
-       if(!pBuf){
-               _LOGE("Malloc failed !");
-               return NULL;
+       if(manifest == NULL) {
+               _LOGE("Input argument is NULL\n");
+               return PMINFO_R_ERROR;
        }
 
-       p = strstr(pBuf, pKey);
-       if (p == NULL){
-               if(pBuf){
-                       free(pBuf);
-                       pBuf = NULL;
-               }
-               return NULL;
+       if(version == NULL) {
+               _LOGE("Argument supplied to hold return value is NULL\n");
+               return PMINFO_R_ERROR;
        }
 
-       pStart = p + strlen(pKey);
-       pEnd = strchr(pStart, SEPERATOR_END);
-       if (pEnd == NULL){
-               if(pBuf){
-                       free(pBuf);
-                       pBuf = NULL;
-               }
-               return NULL;
-       }
-       size_t len = pEnd - pStart;
-       if (len <= 0){
-               if(pBuf){
-                       free(pBuf);
-                       pBuf = NULL;
-               }
-               return NULL;
-       }
-       char *pRes = (char*)malloc(len + 1);
-       if(!pRes){
-               if(pBuf){
-                       free(pBuf);
-                       pBuf = NULL;
+       reader = xmlReaderForFile(manifest, NULL, 0);
+
+       if (reader){
+               if ( _child_element(reader, -1)) {
+                       node = xmlTextReaderConstName(reader);
+                       if (!node) {
+                               _LOGE("xmlTextReaderConstName value is NULL\n");
+                               ret =  PMINFO_R_ERROR;
+                               goto end;
+                       }
+
+                       if (!strcmp(ASCII(node), "manifest")) {
+                               ret = _ri_get_attribute(reader, "version", &val);
+                               if(ret != 0){
+                                       _LOGE("@Error in getting attribute value");
+                                       ret = PMINFO_R_ERROR;
+                                       goto end;
+                               }
+
+                               if(val){
+                                       *version = strdup(val);
+                                       if(*version == NULL){
+                                               _LOGE("Malloc Failed!!");
+                                               ret = PMINFO_R_ERROR;
+                                               goto end;
+                                       }
+                               }
+                       } else {
+                               _LOGE("Unable to create xml reader\n");
+                               ret =  PMINFO_R_ERROR;
+                       }
                }
-               _LOGE("Malloc failed!");
-               return NULL;
+       } else {
+               _LOGE("xmlReaderForFile value is NULL\n");
+               return PMINFO_R_ERROR;
        }
-       strncpy(pRes, pStart, len);
-       pRes[len] = 0;
 
-       if(pBuf){
-               free(pBuf);
-               pBuf = NULL;
-       }
+end:
+       xmlFreeTextReader(reader);
 
-       return pRes;
+       if(val)
+               free((void*)val);
+
+       return ret;
 }
 
 static int __ri_init_csc_xml(char *xml_path, char *removable)
 {
        int ret = 0;
-       char* csc_tags[3] = {NULL, };
+       char *csc_tags[3] = { NULL, };
 
-       if (strcmp(removable,"true")==0)
+       if (strcmp(removable, "true") == 0)
                csc_tags[0] = "removable=true";
        else
                csc_tags[0] = "removable=false";
@@ -277,7 +211,7 @@ static int __ri_create_cert_chain(int sigtype, int sigsubtype, char *value)
 {
        if (value == NULL)
                return -1;
-       // _LOGD("Push in list [%d] [%d] [%s]", sigtype, sigsubtype, value);
+       /* _LOGD("Push in list [%d] [%d] [%s]", sigtype, sigsubtype, value); */
        switch (sigtype) {
        case SIG_AUTH:
                switch (sigsubtype) {
@@ -290,7 +224,7 @@ static int __ri_create_cert_chain(int sigtype, int sigsubtype, char *value)
                        list[PMINFO_SET_AUTHOR_INTERMEDIATE_CERT].cert_value = strdup(value);
                        break;
                case SIG_ROOT:
-                       /*value is already a mallocd pointer*/
+                       /* value is already a mallocd pointer */
                        list[PMINFO_SET_AUTHOR_ROOT_CERT].cert_type = PMINFO_SET_AUTHOR_ROOT_CERT;
                        list[PMINFO_SET_AUTHOR_ROOT_CERT].cert_value = value;
                        break;
@@ -309,7 +243,7 @@ static int __ri_create_cert_chain(int sigtype, int sigsubtype, char *value)
                        list[PMINFO_SET_DISTRIBUTOR_INTERMEDIATE_CERT].cert_value = strdup(value);
                        break;
                case SIG_ROOT:
-                       /*value is already a mallocd pointer*/
+                       /* value is already a mallocd pointer */
                        list[PMINFO_SET_DISTRIBUTOR_ROOT_CERT].cert_type = PMINFO_SET_DISTRIBUTOR_ROOT_CERT;
                        list[PMINFO_SET_DISTRIBUTOR_ROOT_CERT].cert_value = value;
                        break;
@@ -328,7 +262,7 @@ static int __ri_create_cert_chain(int sigtype, int sigsubtype, char *value)
                        list[PMINFO_SET_DISTRIBUTOR2_INTERMEDIATE_CERT].cert_value = strdup(value);
                        break;
                case SIG_ROOT:
-                       /*value is already a mallocd pointer*/
+                       /* value is already a mallocd pointer */
                        list[PMINFO_SET_DISTRIBUTOR2_ROOT_CERT].cert_type = PMINFO_SET_DISTRIBUTOR2_ROOT_CERT;
                        list[PMINFO_SET_DISTRIBUTOR2_ROOT_CERT].cert_value = value;
                        break;
@@ -339,6 +273,7 @@ static int __ri_create_cert_chain(int sigtype, int sigsubtype, char *value)
        default:
                break;
        }
+
        return 0;
 }
 
@@ -346,21 +281,22 @@ static void __ri_free_cert_chain()
 {
        int i = 0;
        for (i = 0; i < MAX_CERT_NUM; i++) {
-               if (list[i].cert_value)
+               if (list[i].cert_value) {
                        free(list[i].cert_value);
+                       list[i].cert_value = NULL;
+               }
        }
 }
 
-static void __ri_xmlsec_debug_print(const char* file, int line, const char* func,
-                                  const char* errorObject, const char* errorSubject, int reason, const char* msg)
+static void __ri_xmlsec_debug_print(const char *file, int line, const char *func, const char *errorObject, const char *errorSubject, int reason, const char *msg)
 {
        char total[BUF_SIZE];
        snprintf(total, sizeof(total), "[%s(%d)] : [%s] : [%s] : [%s]", func, line, errorObject, errorSubject, msg);
-       if(reason != 256) {
+       if (reason != 256) {
                fprintf(stderr, "## [validate error]: %s\n", total);
-               _LOGE("%s",total);
+               _LOGE("%s", total);
        } else {
-               _LOGE("%s",total);
+               _LOGE("%s", total);
        }
 }
 
@@ -412,10 +348,10 @@ static int __ri_verify_file(xmlSecKeysMngrPtr sec_key_mngr, const char *sigxmlfi
 
 err:
        /* cleanup */
-       if(dsigCtx != NULL) {
+       if (dsigCtx != NULL) {
                xmlSecDSigCtxDestroy(dsigCtx);
        }
-       if(doc != NULL) {
+       if (doc != NULL) {
                xmlFreeDoc(doc);
        }
        return res;
@@ -426,24 +362,29 @@ static xmlSecKeysMngrPtr __ri_load_trusted_certs(char *files, int files_size)
        xmlSecKeysMngrPtr sec_key_mngr;
        if (files == NULL)
                return NULL;
+
        if (files_size < 0)
                return NULL;
+
        sec_key_mngr = xmlSecKeysMngrCreate();
        if (sec_key_mngr == NULL) {
                _LOGE("failed to create keys manager.\n");
                return NULL;
        }
+
        if (xmlSecCryptoAppDefaultKeysMngrInit(sec_key_mngr) < 0) {
                _LOGE("failed to initialize keys manager.\n");
                xmlSecKeysMngrDestroy(sec_key_mngr);
                return NULL;
        }
+
        /* load trusted cert */
        if (xmlSecCryptoAppKeysMngrCertLoad(sec_key_mngr, files, xmlSecKeyDataFormatPem, xmlSecKeyDataTypeTrusted) < 0) {
                _LOGE("failed to load pem certificate from \"%s\"\n", files);
                xmlSecKeysMngrDestroy(sec_key_mngr);
                return NULL;
        }
+
        return sec_key_mngr;
 }
 
@@ -457,11 +398,11 @@ static int __ri_xmlsec_verify_signature(const char *sigxmlfile, char *rootca)
 #ifndef XMLSEC_NO_XSLT
        xmlIndentTreeOutput = 1;
        xsltSecurityPrefsPtr sec_prefs = xsltNewSecurityPrefs();
-       xsltSetSecurityPrefs(sec_prefs,  XSLT_SECPREF_WRITE_FILE, xsltSecurityForbid);
-       xsltSetSecurityPrefs(sec_prefs,  XSLT_SECPREF_READ_FILE, xsltSecurityForbid);
-       xsltSetSecurityPrefs(sec_prefs,  XSLT_SECPREF_CREATE_DIRECTORY, xsltSecurityForbid);
-       xsltSetSecurityPrefs(sec_prefs,  XSLT_SECPREF_WRITE_NETWORK, xsltSecurityForbid);
-       xsltSetSecurityPrefs(sec_prefs,  XSLT_SECPREF_READ_NETWORK, xsltSecurityForbid);
+       xsltSetSecurityPrefs(sec_prefs, XSLT_SECPREF_WRITE_FILE, xsltSecurityForbid);
+       xsltSetSecurityPrefs(sec_prefs, XSLT_SECPREF_READ_FILE, xsltSecurityForbid);
+       xsltSetSecurityPrefs(sec_prefs, XSLT_SECPREF_CREATE_DIRECTORY, xsltSecurityForbid);
+       xsltSetSecurityPrefs(sec_prefs, XSLT_SECPREF_WRITE_NETWORK, xsltSecurityForbid);
+       xsltSetSecurityPrefs(sec_prefs, XSLT_SECPREF_READ_NETWORK, xsltSecurityForbid);
        xsltSetDefaultSecurityPrefs(sec_prefs);
 #endif
 
@@ -508,6 +449,7 @@ static int __ri_xmlsec_verify_signature(const char *sigxmlfile, char *rootca)
 end:
        if (sec_key_mngr)
                xmlSecKeysMngrDestroy(sec_key_mngr);
+
        xmlSecCryptoShutdown();
        xmlSecCryptoAppShutdown();
        xmlSecShutdown();
@@ -515,18 +457,19 @@ end:
        xsltFreeSecurityPrefs(sec_prefs);
        xsltCleanupGlobals();
 #endif
+
        return ret;
 }
 
-int _rpm_installer_get_group_id(char *pkgid, char **result)
+int _rpm_installer_get_group_id(const char *pkgid, char **result)
 {
        int ret = 0;
        const char *value = NULL;
-       char author_signature[BUF_SIZE] = {'\0'};
+       char author_signature[BUF_SIZE] = { '\0' };
        char *e_rootcert = NULL;
        char *d_rootcert = NULL;
        gsize d_size = 0;
-       unsigned char hashout[BUF_SIZE] = {'\0'};
+       unsigned char hashout[BUF_SIZE] = { '\0' };
        unsigned int h_size = 0;
        int e_size = 0;
        int length = 0;
@@ -550,22 +493,22 @@ int _rpm_installer_get_group_id(char *pkgid, char **result)
                goto err;
        }
 
-       // get root certificate
+       /* get root certificate */
        ret = pkgmgrinfo_pkginfo_get_cert_value(handle, PMINFO_AUTHOR_SIGNER_CERT, &value);
        if (ret < 0 || value == NULL) {
                _LOGE("pkgmgrinfo_pkginfo_get_cert_value(%s) failed. [%d]", pkgid, ret);
                goto err;
        }
 
-       // decode cert
+       /* decode cert */
        d_rootcert = (char *)g_base64_decode(value, &d_size);
-       if (d_rootcert == NULL) {
+       if (d_rootcert == NULL) {
                _LOGE("g_base64_decode() failed.");
                goto err;
        }
        _LOGD("g_base64_decode() succeed, d_size=[%d]", d_size);
 
-       // hash
+       /* hash */
        EVP_Digest(d_rootcert, d_size, hashout, &h_size, EVP_sha1(), NULL);
        if (h_size <= 0) {
                _LOGE("EVP_Digest(hash) failed.");
@@ -573,7 +516,7 @@ int _rpm_installer_get_group_id(char *pkgid, char **result)
        }
        _LOGD("EVP_Digest() succeed, h_size=[%d]", h_size);
 
-       // encode cert
+       /* encode cert */
        e_rootcert = g_base64_encode((const guchar *)hashout, h_size);
        if (e_rootcert == NULL) {
                _LOGE("g_base64_encode() failed.");
@@ -582,7 +525,7 @@ int _rpm_installer_get_group_id(char *pkgid, char **result)
        e_size = strlen(e_rootcert);
        _LOGD("g_base64_encode() succeed, e_size=[%d]", e_size);
 
-       // replace / to #
+       /* replace / to # */
        for (length = e_size; length >= 0; --length) {
                if (e_rootcert[length] == '/') {
                        e_rootcert[length] = '#';
@@ -596,7 +539,7 @@ err:
                free(d_rootcert);
        }
 
-       // destroy cert
+       /* destroy cert */
        if (handle) {
                pkgmgrinfo_pkginfo_destroy_certinfo(handle);
        }
@@ -604,32 +547,49 @@ err:
        return ret;
 }
 
-void __rpm_apply_smack(char *pkgname, int flag)
+void __rpm_apply_smack(const char *pkgname, int flag, char *smack_label)
 {
        int ret = -1;
-       char dirpath[BUF_SIZE] = {'\0'};
-       chargroupid = NULL;
+       char dirpath[BUF_SIZE] = { '\0' };
+       char *groupid = NULL;
        char *shared_data_label = NULL;
+       char buf[BUF_SIZE] = { 0, };
+
+       if (smack_label == NULL || strlen(smack_label) == 0) {
+               smack_label = (char *)pkgname;
+       }
 
-       /*execute privilege APIs. The APIs should not fail*/
-       _ri_privilege_register_package(pkgname);
+       /* execute privilege APIs. The APIs should not fail */
+       _ri_privilege_register_package(smack_label);
 
-       /*home dir. Dont setup path but change smack access to "_" */
+       /* home dir. Dont setup path but change smack access to "_" */
        snprintf(dirpath, BUF_SIZE, "%s/%s", USR_APPS, pkgname);
        if (__is_dir(dirpath))
-               _ri_privilege_change_smack_label(dirpath, "_", 0);/*0 is SMACK_LABEL_ACCESS*/
+               _ri_privilege_change_smack_label(dirpath, "_", 0);      /* 0 is SMACK_LABEL_ACCESS */
        memset(dirpath, '\0', BUF_SIZE);
        snprintf(dirpath, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgname);
        if (__is_dir(dirpath))
-               _ri_privilege_change_smack_label(dirpath, "_", 0);/*0 is SMACK_LABEL_ACCESS*/
+               _ri_privilege_change_smack_label(dirpath, "_", 0);      /* 0 is SMACK_LABEL_ACCESS */
 
-       // data
+       /* data */
        memset(dirpath, '\0', BUF_SIZE);
        snprintf(dirpath, BUF_SIZE, "%s/%s/data", OPT_USR_APPS, pkgname);
-       if (__is_dir(dirpath))
-               _ri_privilege_setup_path(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
+       if (!__is_dir(dirpath)) {
+               ret = mkdir(dirpath, DIRECTORY_PERMISSION_755);
+               if (ret < 0) {
+                       _LOGE("directory making is failed.\n");
+               }
+       }
+
+       ret = chown(dirpath, APP_OWNER_ID, APP_GROUP_ID);
+       if (ret != 0) {
+               if( strerror_r(errno, buf, sizeof(buf)) == 0) {
+                       _LOGE("chown failed!! [%s]", buf);
+               }
+       }
+       _ri_privilege_setup_path(smack_label, dirpath, APP_PATH_PRIVATE, smack_label);
 
-       // cache
+       /* cache */
        memset(dirpath, '\0', BUF_SIZE);
        snprintf(dirpath, BUF_SIZE, "%s/%s/cache", OPT_USR_APPS, pkgname);
        if (!__is_dir(dirpath)) {
@@ -639,58 +599,64 @@ void __rpm_apply_smack(char *pkgname, int flag)
                }
        }
        ret = chown(dirpath, APP_OWNER_ID, APP_GROUP_ID);
-       if(ret != 0){
-               _LOGE("chown failed!! [%s]",strerror(errno));
+       if (ret != 0) {
+               if( strerror_r(errno, buf, sizeof(buf)) == 0) {
+                       _LOGE("chown failed!! [%s]", buf);
+               }
        }
-       _ri_privilege_setup_path(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
+       _ri_privilege_setup_path(smack_label, dirpath, APP_PATH_PRIVATE, smack_label);
 
-       //shared
+       /* shared */
        memset(dirpath, '\0', BUF_SIZE);
        snprintf(dirpath, BUF_SIZE, "%s/%s/shared", USR_APPS, pkgname);
        if (__is_dir(dirpath))
-               _ri_privilege_change_smack_label(dirpath, "_", 0);/*0 is SMACK_LABEL_ACCESS*/
+               _ri_privilege_change_smack_label(dirpath, "_", 0);      /* 0 is SMACK_LABEL_ACCESS */
        memset(dirpath, '\0', BUF_SIZE);
        snprintf(dirpath, BUF_SIZE, "%s/%s/shared", OPT_USR_APPS, pkgname);
-       if (!__is_dir(dirpath))
+       if (!__is_dir(dirpath)) {
                ret = mkdir(dirpath, DIRECTORY_PERMISSION_755);
-               if (ret < 0) {
+               if (ret < 0)
                        _LOGE("directory making is failed.\n");
-               }
-       _ri_privilege_change_smack_label(dirpath, "_", 0);/*0 is SMACK_LABEL_ACCESS*/
+       }
+       _ri_privilege_change_smack_label(dirpath, "_", 0);      /* 0 is SMACK_LABEL_ACCESS */
        memset(dirpath, '\0', BUF_SIZE);
 
-       //shared/res
+       /* shared/res */
        snprintf(dirpath, BUF_SIZE, "%s/%s/shared/res", USR_APPS, pkgname);
        if (__is_dir(dirpath))
-               _ri_privilege_setup_path(pkgname, dirpath, PERM_APP_PATH_ANY_LABEL, "_");
+               _ri_privilege_setup_path(smack_label, dirpath, PERM_APP_PATH_ANY_LABEL, "_");
        memset(dirpath, '\0', BUF_SIZE);
 
        snprintf(dirpath, BUF_SIZE, "%s/%s/shared/res", OPT_USR_APPS, pkgname);
        if (__is_dir(dirpath))
-               _ri_privilege_setup_path(pkgname, dirpath, PERM_APP_PATH_ANY_LABEL, "_");
+               _ri_privilege_setup_path(smack_label, dirpath, PERM_APP_PATH_ANY_LABEL, "_");
        memset(dirpath, '\0', BUF_SIZE);
 
-       //shared/data
+       /* shared/data */
        snprintf(dirpath, BUF_SIZE, "%s/%s/shared/data", USR_APPS, pkgname);
        if (__is_dir(dirpath)) {
                ret = chown(dirpath, APP_OWNER_ID, APP_GROUP_ID);
-               if(ret != 0){
-                       _LOGE("chown failed!! [%s]",strerror(errno));
+               if (ret != 0) {
+                       if( strerror_r(errno, buf, sizeof(buf)) == 0) {
+                               _LOGE("chown failed!! [%s]", buf);
+                       }
                }
-               _ri_privilege_setup_path(pkgname, dirpath, PERM_APP_PATH_PUBLIC, NULL);
+               _ri_privilege_setup_path(smack_label, dirpath, PERM_APP_PATH_PUBLIC, NULL);
        }
        memset(dirpath, '\0', BUF_SIZE);
 
        snprintf(dirpath, BUF_SIZE, "%s/%s/shared/data", OPT_USR_APPS, pkgname);
        if (__is_dir(dirpath)) {
                ret = chown(dirpath, APP_OWNER_ID, APP_GROUP_ID);
-               if(ret != 0){
-                       _LOGE("chown failed!! [%s]",strerror(errno));
+               if (ret != 0) {
+                       if( strerror_r(errno, buf, sizeof(buf)) == 0) {
+                               _LOGE("chown failed!! [%s]", buf);
+                       }
                }
-               _ri_privilege_setup_path(pkgname, dirpath, PERM_APP_PATH_PUBLIC, NULL);
+               _ri_privilege_setup_path(smack_label, dirpath, PERM_APP_PATH_PUBLIC, NULL);
        }
 
-       // shared/cache
+       /* shared/cache */
        ret = _coretpk_installer_get_smack_label_access(dirpath, &shared_data_label);
        if (ret != 0) {
                _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", dirpath, ret);
@@ -704,10 +670,12 @@ void __rpm_apply_smack(char *pkgname, int flag)
                }
        }
        ret = chown(dirpath, APP_OWNER_ID, APP_GROUP_ID);
-       if(ret != 0){
-               _LOGE("chown failed!! [%s]",strerror(errno));
+       if (ret != 0) {
+               if( strerror_r(errno, buf, sizeof(buf)) == 0) {
+                       _LOGE("chown failed!! [%s]", buf);
+               }
        }
-       ret  = _coretpk_installer_set_smack_label_access(dirpath, shared_data_label);
+       ret = _coretpk_installer_set_smack_label_access(dirpath, shared_data_label);
        if (ret != 0) {
                _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", dirpath, ret);
        }
@@ -716,19 +684,21 @@ void __rpm_apply_smack(char *pkgname, int flag)
                _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", dirpath, ret);
        }
 
-       /*/shared/trusted/*/
+       /* /shared/trusted/ */
        memset(dirpath, '\0', BUF_SIZE);
        snprintf(dirpath, BUF_SIZE, "%s/%s/shared/trusted", USR_APPS, pkgname);
        if (__is_dir(dirpath)) {
                ret = chown(dirpath, APP_OWNER_ID, APP_GROUP_ID);
-               if(ret != 0){
-                       _LOGE("chown failed!! [%s]",strerror(errno));
+               if (ret != 0) {
+                       if( strerror_r(errno, buf, sizeof(buf)) == 0) {
+                               _LOGE("chown failed!! [%s]", buf);
+                       }
                }
 
                ret = _rpm_installer_get_group_id(pkgname, &groupid);
                if (ret == 0) {
                        _LOGD("group id for trusted directory is [%s]", groupid);
-                       _ri_privilege_setup_path(pkgname, dirpath, APP_PATH_GROUP_RW, groupid);
+                       _ri_privilege_setup_path(smack_label, dirpath, APP_PATH_GROUP_RW, groupid);
                        FREE_AND_NULL(groupid);
                }
        }
@@ -745,27 +715,31 @@ void __rpm_apply_smack(char *pkgname, int flag)
 
                        ret = mkdir(dirpath, DIRECTORY_PERMISSION_755);
                        if (ret == -1 && errno != EEXIST) {
-                               _LOGE("mkdir failed!! [%s]",strerror(errno));
+                               if( strerror_r(errno, buf, sizeof(buf)) == 0) {
+                                       _LOGE("mkdir failed!! [%s]", buf);
+                               }
                        }
                }
 
                ret = chown(dirpath, APP_OWNER_ID, APP_GROUP_ID);
-               if(ret != 0){
-                       _LOGE("chown failed!! [%s]",strerror(errno));
+               if (ret != 0) {
+                       if( strerror_r(errno, buf, sizeof(buf)) == 0) {
+                               _LOGE("chown failed!! [%s]", buf);
+                       }
                }
 
                _LOGD("group id for trusted directory is [%s]", groupid);
-               _ri_privilege_setup_path(pkgname, dirpath, APP_PATH_GROUP_RW, groupid);
+               _ri_privilege_setup_path(smack_label, dirpath, APP_PATH_GROUP_RW, groupid);
                _ri_set_group_id(pkgname, groupid);
 
                FREE_AND_NULL(groupid);
        }
 }
 
-int __is_dir(char *dirname)
+int __is_dir(const char *dirname)
 {
        struct stat stFileInfo;
-       if(dirname == NULL) {
+       if (dirname == NULL) {
                _LOGE("dirname is null\n");
                return -1;
        }
@@ -781,17 +755,14 @@ int __is_dir(char *dirname)
 static void __rpm_process_line(char *line)
 {
        char *tok = NULL;
-       tok = strtok(line, " ");
+       char *save_str = NULL;
+       tok = strtok_r(line, " ", &save_str);
        if (tok) {
                if (!strncmp(tok, "%%", 2)) {
-                       tok = strtok(NULL, " ");
+                       tok = strtok_r(NULL, " ", &save_str);
                        if (tok) {
-                               _LOGD("Install percentage is %s\n",
-                                      tok);
-                               _ri_broadcast_status_notification(gpkgname,
-                                                                 "rpm",
-                                                                 "install_percent",
-                                                                 tok);
+                               _LOGD("Install percentage is %s\n", tok);
+                               _ri_broadcast_status_notification(gpkgname, PKGTYPE_RPM, "install_percent", tok);
                                _ri_stat_cb(gpkgname, "install_percent", tok);
                        }
                        return;
@@ -808,24 +779,21 @@ static void __rpm_perform_read(int fd)
        static char buffer[BUF_SIZE] = { 0, };
        static int buffer_position;
 
-       size = read(fd, &buffer[buffer_position],
-                   sizeof(buffer) - buffer_position);
+       size = read(fd, &buffer[buffer_position], sizeof(buffer) - buffer_position);
        buffer_position += size;
        if (size <= 0)
                return;
 
        /* Process each line of the recieved buffer */
        buf_ptr = tmp_ptr = buffer;
-       while ((tmp_ptr = (char *)memchr(buf_ptr, '\n',
-                                        buffer + buffer_position - buf_ptr)) !=
-              NULL) {
+       while ((tmp_ptr = (char *)memchr(buf_ptr, '\n', buffer + buffer_position - buf_ptr)) != NULL) {
                *tmp_ptr = 0;
                __rpm_process_line(buf_ptr);
                /* move to next line and continue */
                buf_ptr = tmp_ptr + 1;
        }
 
-       /*move the remaining bits at the start of the buffer
+       /* move the remaining bits at the start of the buffer
           and update the buffer position */
        buf_ptr = (char *)memrchr(buffer, 0, buffer_position);
        if (buf_ptr == NULL)
@@ -839,7 +807,6 @@ static void __rpm_perform_read(int fd)
        buffer_position = buffer + buffer_position - buf_ptr;
 }
 
-
 int _rpm_xsystem(const char *argv[])
 {
        int err = 0;
@@ -853,7 +820,7 @@ int _rpm_xsystem(const char *argv[])
                _LOGE("pipe creation failed\n");
                return -1;
        }
-       /*Read progress info via pipe */
+       /* Read progress info via pipe */
        pid = fork();
 
        switch (pid) {
@@ -862,27 +829,25 @@ int _rpm_xsystem(const char *argv[])
                return -1;
        case 0:
                /* child */
-               {
-                       close(pipefd[0]);
-                       close(1);
-                       close(2);
-                       fd = dup(pipefd[1]);
-                       if (fd < 0) {
-                               _LOGE("dup failed\n");
-                               _exit(100);
-                       }
-
-                       result = dup(pipefd[1]);
-                       if (result < 0) {
-                               _LOGE("dup failed\n");
-                               _exit(100);
-                       }
+               close(pipefd[0]);
+               close(1);
+               close(2);
+               fd = dup(pipefd[1]);
+               if (fd < 0) {
+                       _LOGE("dup failed\n");
+                       _exit(100);
+               }
 
-                       if (execvp(argv[0], (char *const *)argv) == -1) {
-                               _LOGE("execvp failed\n");
-                       }
+               result = dup(pipefd[1]);
+               if (result < 0) {
+                       _LOGE("dup failed\n");
                        _exit(100);
                }
+
+               if (execvp(argv[0], (char *const *)argv) == -1) {
+                       _LOGE("execvp failed\n");
+               }
+               _exit(100);
        default:
                /* parent */
                break;
@@ -906,8 +871,7 @@ int _rpm_xsystem(const char *argv[])
                FD_SET(pipefd[0], &rfds);
                tv.tv_sec = 1;
                tv.tv_nsec = 0;
-               select_ret =
-                   pselect(pipefd[0] + 1, &rfds, NULL, NULL, &tv, NULL);
+               select_ret = pselect(pipefd[0] + 1, &rfds, NULL, NULL, &tv, NULL);
                if (select_ret == 0)
                        continue;
 
@@ -924,14 +888,10 @@ int _rpm_xsystem(const char *argv[])
        close(pipefd[0]);
        /* Check for an error code. */
        if (WIFEXITED(status) == 0 || WEXITSTATUS(status) != 0) {
-
                if (WIFSIGNALED(status) != 0 && WTERMSIG(status) == SIGSEGV) {
-                       printf
-                           ("Sub-process %s received a segmentation fault. \n",
-                            argv[0]);
+                       printf("Sub-process %s received a segmentation fault. \n", argv[0]);
                } else if (WIFEXITED(status) != 0) {
-                       printf("Sub-process %s returned an error code (%u)\n",
-                              argv[0], WEXITSTATUS(status));
+                       printf("Sub-process %s returned an error code (%u)\n", argv[0], WEXITSTATUS(status));
                } else {
                        printf("Sub-process %s exited unexpectedly\n", argv[0]);
                }
@@ -939,14 +899,14 @@ int _rpm_xsystem(const char *argv[])
        return WEXITSTATUS(status);
 }
 
-void __rpm_clear_dir_list(GListdir_list)
+void __rpm_clear_dir_list(GList *dir_list)
 {
        GList *list = NULL;
-       app2ext_dir_detailsdir_detail = NULL;
+       app2ext_dir_details *dir_detail = NULL;
        if (dir_list) {
                list = g_list_first(dir_list);
                while (list) {
-                       dir_detail = (app2ext_dir_details *)list->data;
+                       dir_detail = (app2ext_dir_details *) list->data;
                        if (dir_detail && dir_detail->name) {
                                free(dir_detail->name);
                        }
@@ -957,44 +917,45 @@ void __rpm_clear_dir_list(GList* dir_list)
        }
 }
 
-GList * __rpm_populate_dir_list()
+GList *__rpm_populate_dir_list()
 {
        GList *dir_list = NULL;
        GList *list = NULL;
-       app2ext_dir_detailsdir_detail = NULL;
+       app2ext_dir_details *dir_detail = NULL;
        int i;
        char pkg_ro_content_rpm[3][5] = { "bin", "res", "lib" };
 
-
-       for (i=0; i<3; i++) {
-               dir_detail = (app2ext_dir_details*) calloc(1, sizeof(app2ext_dir_details));
+       for (i = 0; i < 3; i++) {
+               dir_detail = (app2ext_dir_details *) calloc(1, sizeof(app2ext_dir_details));
                if (dir_detail == NULL) {
                        printf("\nMemory allocation failed\n");
                        goto FINISH_OFF;
                }
-               dir_detail->name = (char*) calloc(1, sizeof(char)*(strlen(pkg_ro_content_rpm[i])+2));
+               dir_detail->name = (char *)calloc(1, sizeof(char) * (strlen(pkg_ro_content_rpm[i]) + 2));
                if (dir_detail->name == NULL) {
                        printf("\nMemory allocation failed\n");
                        free(dir_detail);
                        goto FINISH_OFF;
                }
-               snprintf(dir_detail->name, (strlen(pkg_ro_content_rpm[i])+1), "%s", pkg_ro_content_rpm[i]);
+               snprintf(dir_detail->name, (strlen(pkg_ro_content_rpm[i]) + 1), "%s", pkg_ro_content_rpm[i]);
                dir_detail->type = APP2EXT_DIR_RO;
                dir_list = g_list_append(dir_list, dir_detail);
        }
        if (dir_list) {
                list = g_list_first(dir_list);
                while (list) {
-                       dir_detail = (app2ext_dir_details *)list->data;
+                       dir_detail = (app2ext_dir_details *) list->data;
                        list = g_list_next(list);
                }
        }
+
        return dir_list;
+
 FINISH_OFF:
        if (dir_list) {
                list = g_list_first(dir_list);
                while (list) {
-                       dir_detail = (app2ext_dir_details *)list->data;
+                       dir_detail = (app2ext_dir_details *) list->data;
                        if (dir_detail && dir_detail->name) {
                                free(dir_detail->name);
                        }
@@ -1003,6 +964,7 @@ FINISH_OFF:
                }
                g_list_free(dir_list);
        }
+
        return NULL;
 }
 
@@ -1019,13 +981,19 @@ static char *__ri_get_cert_from_file(const char *file)
        int ch = 0;
        int error = 0;
 
-       if(!(fp_cert = fopen(file, "r"))) {
+       if (!(fp_cert = fopen(file, "r"))) {
                _LOGE("[ERR][%s] Fail to open file, [%s]\n", __func__, file);
                return NULL;
        }
 
        fseek(fp_cert, 0L, SEEK_END);
 
+       if (ftell(fp_cert) < 0) {
+               _LOGE("[ERR][%s] Fail to find EOF\n", __func__);
+               error = 1;
+               goto err;
+       }
+
        certlen = ftell(fp_cert);
        if (certlen < 0) {
                _LOGE("[ERR][%s] Fail to find EOF\n", __func__);
@@ -1035,7 +1003,7 @@ static char *__ri_get_cert_from_file(const char *file)
 
        fseek(fp_cert, 0L, SEEK_SET);
 
-       if(!(certbuf = (char*)malloc(sizeof(char) * (int)certlen))) {
+       if (!(certbuf = (char *)malloc(sizeof(char) * (int)certlen))) {
                _LOGE("[ERR][%s] Fail to allocate memory\n", __func__);
                error = 1;
                goto err;
@@ -1043,8 +1011,8 @@ static char *__ri_get_cert_from_file(const char *file)
        memset(certbuf, 0x00, (int)certlen);
 
        i = 0;
-       while((ch = fgetc(fp_cert)) != EOF) {
-               if(ch != '\n') {
+       while ((ch = fgetc(fp_cert)) != EOF) {
+               if (ch != '\n') {
                        certbuf[i] = ch;
                        i++;
                }
@@ -1055,14 +1023,14 @@ static char *__ri_get_cert_from_file(const char *file)
        endcert = strstr(certbuf, "-----END CERTIFICATE-----");
        certwrite = (int)endcert - (int)startcert;
 
-       cert = (char*)malloc(sizeof(char) * (certwrite+2));
+       cert = (char *)malloc(sizeof(char) * (certwrite + 2));
        if (cert == NULL) {
                _LOGE("[ERR][%s] Fail to allocate memory\n", __func__);
                error = 1;
                goto err;
        }
-       memset(cert, 0x00, certwrite+2);
-       snprintf(cert, certwrite+1, "%s", startcert);
+       memset(cert, 0x00, certwrite + 2);
+       snprintf(cert, certwrite + 1, "%s", startcert);
        _LOGD("Root CA, len=[%d]\n%s", certwrite, cert);
 
 err:
@@ -1078,38 +1046,18 @@ err:
 static int __privilege_func(const char *name, void *user_data)
 {
        int ret = 0;
-       const char *perm[] = {NULL, NULL};
-       perm[0] = name;
-       int apptype = PERM_APP_TYPE_EFL;
-       privilegeinfo *info = (privilegeinfo*)user_data;
-
-       _LOGD("package_id = [%s], privilege = [%s]", info->package_id, name);
-       privilege_list = g_list_append(privilege_list, strdup((char*)name));
-
-       if (info->visibility & CERT_SVC_VISIBILITY_PLATFORM) {
-               _LOGD("VISIBILITY_PLATFORM!");
-               apptype = PERM_APP_TYPE_EFL_PLATFORM;
-       } else if ((info->visibility & CERT_SVC_VISIBILITY_PARTNER) ||
-                       (info->visibility & CERT_SVC_VISIBILITY_PARTNER_OPERATOR) ||
-                       (info->visibility & CERT_SVC_VISIBILITY_PARTNER_MANUFACTURER)) {
-               _LOGD("VISIBILITY_PARTNER!");
-               apptype = PERM_APP_TYPE_EFL_PARTNER;
-       }
+       privilegeinfo *info = (privilegeinfo *) user_data;
 
-       ret = _ri_privilege_enable_permissions((char *)info->package_id, apptype, perm, 1);
-       if(ret < 0) {
-               _LOGE("_ri_privilege_enable_permissions(%s, %d) failed.", (char *)info->package_id, apptype);
-               return -1;
-       }
+       _LOGD("package_id=[%s], privilege=[%s]", info->package_id, name);
+       info->privileges = g_list_append(info->privileges, strdup((char *)name));
 
        if (strcmp(name, EXT_APPDATA_PRIVILEGE_NAME) == 0) {
                _LOGD("it is EXT_APPDATA_PRIVILEGE_NAME");
-               if(_coretpk_installer_make_directory_for_ext((char *)info->package_id) < 0) {
+               if (_coretpk_installer_make_directory_for_ext((char *)info->package_id) < 0) {
                        _LOGE("make_directory_for_ext failed.");
                }
        }
 
-       _LOGD("_ri_privilege_enable_permissions(%s, %d) succeed.", (char *)info->package_id, apptype);
        return ret;
 }
 
@@ -1117,95 +1065,109 @@ char *__strlwr(char *str)
 {
        int i = 0;
 
-       while(*(str+i) != '\0'){
-               if(*(str+i) >= 65 || *(str+i)<= 90) {
-                       *(str+i) = towlower(*(str+i));
+       while (*(str + i) != '\0') {
+               if (*(str + i) >= 65 || *(str + i) <= 90) {
+                       *(str + i) = towlower(*(str + i));
                }
                i++;
        }
        return str;
 }
 
-static int __ri_find_xml(char *pkgid, char **rpm_xml)
+static int __ri_install_fota(const char *pkgid)
 {
-       DIR *dir;
-       struct dirent entry, *result;
        int ret = 0;
-       char buf[BUF_SIZE];
-
-       dir = opendir(USR_SHARE_PACKAGES);
-       if (!dir) {
-               if (strerror_r(errno, buf, sizeof(buf)) == 0)
-                       _LOGE("fota-info : Failed to access the [%s] because %s", USR_SHARE_PACKAGES, buf);
-               return -1;
-       }
-
-       for (ret = readdir_r(dir, &entry, &result);
-                       ret == 0 && result != NULL;
-                       ret = readdir_r(dir, &entry, &result)) {
-               char *manifest = NULL;
+       int visibility = 0;
+       char manifest[BUF_SIZE] = { 0, };
+       char rw_manifest[BUF_SIZE] = { 0, };
+       char root_path[BUF_SIZE] = { 0, };
+       char signature_file[BUF_SIZE] = { 0, };
+       char *fota_version = NULL;
+       char *temp[] = { "fota=true", NULL };
+       pkginfo *db_info = NULL;
+       char *smack_label = NULL;
+
+       _LOGD("fota-info : pkgid[%s] start installation.", pkgid);
+
+       snprintf(manifest, BUF_SIZE, "%s/%s.xml", USR_SHARE_PACKAGES, pkgid);
+       snprintf(rw_manifest, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
+
+       _LOGD("fota-info : pkgid[%s] has manefest[%s].", pkgid, manifest);
+
+       if (access(rw_manifest, F_OK) == 0) {
+               _LOGD("fota-info : [%s] is existed.", rw_manifest);
+
+               // compare version(db, fota)
+               db_info = _rpm_installer_get_pkgname_info(pkgid);
+               if (db_info != NULL) {
+                       __get_version_from_xml(manifest, &fota_version);
+                       if (fota_version != NULL) {
+                               _LOGD("fota-info : dbinfo version=[%s], fota version=[%s]", db_info->version, fota_version);
+
+                               ret = _installer_util_compare_version(db_info->version, fota_version);
+                               if (ret == VERSION_NEW) {
+                                       _LOGD("fota is new version. [%s]", manifest);
+                               } else {
+                                       _LOGD("fota is not new version. [%s]", manifest);
+                               }
+                       } else {
+                               _LOGE("fota-info : __get_version_from_xml(%s) failed.", manifest);
+                       }
 
-               if (entry.d_name[0] == '.') continue;
+                       if(fota_version){
+                               free(fota_version);
+                       }
 
-               manifest = _manifest_to_package(entry.d_name);
-               if (!manifest) {
-                       _LOGE("fota-info : Failed to convert file to xml[%s]", entry.d_name);
-                       continue;
-               }
+                       if (db_info) {
+                               free(db_info);
+                       }
 
-               if (strstr(manifest, __strlwr(pkgid))) {
-                       snprintf(buf, sizeof(buf), "%s/%s", USR_SHARE_PACKAGES, manifest);
-                       _LOGD("fota-info : pkgid[%s] find xml[%s]\n", pkgid, buf);
-                       *rpm_xml = strdup(buf);
-                       closedir(dir);
-                       free(manifest);
-                       return 0;
+                       if (ret != VERSION_NEW) {
+                               _LOGD("fota-info : [%s] installation skip. [version is not new]", pkgid);
+                               return 0;
+                       }
+               } else {
+                       _LOGE("fota-info : _rpm_installer_get_pkgname_info(%s) failed.", pkgid);
                }
-
-               free(manifest);
        }
 
-       _LOGE("fota-info : Failed to find xml for pkgid[%s]", pkgid);
-       closedir(dir);
-       return 0;
-}
-
-static int __ri_install_fota(char *pkgid)
-{
-       int ret = 0;
-       char *manifest = NULL;
-       char *temp[] = {"fota=true", NULL};
-
-       _LOGD("fota-info : pkgid[%s] start installation\n", pkgid);
-
-       /*if pkgid is one of deactivation, it does not need to install*/
-       ret = __ri_check_pkgid_for_deactivation(pkgid);
+       ret = pkgmgr_parser_parse_manifest_for_installation(manifest, temp);
        if (ret < 0) {
-               _LOGE("fota-info : pkgid[%s] for deactivation dont need to install.\n", pkgid);
-               return ret;
-       }
-
-       /*get manifest*/
-       manifest = pkgmgr_parser_get_manifest_file(pkgid);
-       if (manifest == NULL) {
-               _LOGE("fota-info : dont have manefest[pkgid=%s]\n", pkgid);
+               _LOGE("fota-info : installation fail. [manifest=%s]", manifest);
                ret = -1;
                goto end;
        }
-       _LOGD("fota-info : pkgid[%s] has manefest[%s]\n", pkgid, manifest);
+       _LOGD("fota-info : pkgid[%s] installation success.", pkgid);
 
-       ret = pkgmgr_parser_parse_manifest_for_installation(manifest, temp);
-       if (ret < 0) {
-               _LOGE("fota-info : installation fail[manifest=%s]\n", manifest);
-               ret = -1;
-               goto end;
+       snprintf(root_path, BUF_SIZE, "%s/%s", USR_APPS, pkgid);
+       ret = _ri_verify_signatures(root_path, pkgid, false);
+       if (ret == 0) {
+               _LOGD("fota-info : certificate register(%s).", pkgid);
+               _ri_register_cert(pkgid);
        }
-       _LOGD("fota-info : pkgid[%s] installation success\n", pkgid);
 
-       __rpm_apply_smack(pkgid,0);
        __ri_make_directory(pkgid);
 
-       ret = _ri_apply_privilege(pkgid, 0);
+       ret = __get_smack_label_from_db(pkgid, &smack_label);
+       _LOGD("smack_label[%s], ret[%d]\n", smack_label, ret);
+
+       __rpm_apply_smack(pkgid, 0, smack_label);
+
+       snprintf(signature_file, BUF_SIZE, "%s/%s", root_path, SIGNATURE1_XML);
+       if (access(signature_file, F_OK) != 0){
+               _LOGE("[%s] is not present", signature_file);
+       } else {
+               ret = _ri_get_visibility_from_signature_file(signature_file, &visibility, false);
+               if (ret != 0) {
+                       _LOGE("Couldnt get visiblity [%d], ret: %d", visibility, ret);
+                       ret = RPM_INSTALLER_ERR_INTERNAL;
+                       goto end;
+               } else{
+                       _LOGD("visibility : %d", visibility);
+               }
+       }
+
+       ret = _ri_apply_privilege(pkgid, visibility, smack_label);
        if (ret < 0) {
                _LOGE("fota-info : _ri_apply_privilege fail[pkgid=%s]\n", pkgid);
                ret = -1;
@@ -1214,75 +1176,81 @@ static int __ri_install_fota(char *pkgid)
        _LOGD("fota-info : pkgid[%s] apply smack success\n", pkgid);
 
 end:
-       if (manifest){
-               free(manifest);
-               manifest = NULL;
-       }
+       FREE_AND_NULL(smack_label);
 
        return ret;
 }
 
-static int __ri_upgrade_fota(char *pkgid)
+static int __ri_upgrade_fota(const char *pkgid)
 {
-       _LOGD("fota-info : pkgid[%s] start upgrade\n", pkgid);
+       _LOGD("fota-info : pkgid[%s] start upgrade.", pkgid);
 
        int ret = 0;
-       char *manifest = NULL;
-       char *temp[] = {"fota=true", NULL};
+       int visibility = 0;
+       char manifest[BUF_SIZE] = { 0, };
+       char root_path[BUF_SIZE] = { 0, };
+       char signature_file[BUF_SIZE] = { 0, };
+       char *temp[] = { "fota=true", NULL };
+       char *smack_label = NULL;
 
-       /*if pkgid is one of deactivation, it does not need to upgrade*/
-       ret = __ri_check_pkgid_for_deactivation(pkgid);
-       if (ret < 0) {
-               _LOGE("fota-info : pkgid[%s] for deactivation dont need to install.\n", pkgid);
-               return ret;
-       }
+       /* del manifest */
+       snprintf(manifest, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
+       (void)remove(manifest);
+       memset(manifest, '\0', BUF_SIZE);
 
-       /*get manifest*/
-       manifest = pkgmgr_parser_get_manifest_file(pkgid);
-       if (manifest == NULL) {
-               _LOGE("fota-info : dont have manefest[pkgid=%s]\n", pkgid);
+       snprintf(manifest, BUF_SIZE, "%s/%s.xml", USR_SHARE_PACKAGES, pkgid);
+       _LOGD("fota-info : pkgid[%s] has manefest[%s].", pkgid, manifest);
+
+       if (access(manifest, F_OK) != 0) {
+               _LOGE("fota-info : can not access[manifest=%s].", manifest);
                ret = -1;
                goto end;
        }
-       _LOGD("fota-info : pkgid[%s] has manefest[%s]\n", pkgid, manifest);
-       if (access(manifest, F_OK) != 0) {
-               _LOGE("fota-info : can not access[manifest=%s]\n", manifest);
-               free(manifest);
-               manifest = NULL;
-
-               /*find xml*/
-               ret = __ri_find_xml(pkgid, &manifest);
-               if (ret < 0) {
-                       _LOGE("fota-info : can not find xml[pkgid=%s]\n", pkgid);
-                       ret = -1;
-                       goto end;
-               }
-       }
-
-       if (manifest == NULL) {
-               return -1;
-       }
 
        ret = pkgmgr_parser_parse_manifest_for_upgrade(manifest, temp);
        if (ret < 0) {
-               _LOGE("fota-info : upgrade fail[manifest=%s]\n", manifest);
+               _LOGE("fota-info : upgrade fail[manifest=%s].", manifest);
                ret = -1;
                goto end;
        }
 
-       __rpm_apply_smack(pkgid,0);
+       snprintf(root_path, BUF_SIZE, "%s/%s", USR_APPS, pkgid);
+       ret = _ri_verify_signatures(root_path, pkgid, false);
+       if (ret == 0) {
+               _LOGD("fota-info : certificate register(%s).", pkgid);
+               _ri_register_cert(pkgid);
+       }
+
        __ri_make_directory(pkgid);
 
-       ret = _ri_apply_privilege(pkgid, 0);
+       ret = __get_smack_label_from_db(pkgid, &smack_label);
+       _LOGD("smack_label[%s], ret[%d]\n", smack_label, ret);
+
+       __rpm_apply_smack(pkgid, 0, smack_label);
+
+       snprintf(signature_file, BUF_SIZE, "%s/%s", root_path, SIGNATURE1_XML);
+       if (access(signature_file, F_OK) != 0){
+               _LOGE("[%s] is not present", signature_file);
+       } else {
+               ret = _ri_get_visibility_from_signature_file(signature_file, &visibility, false);
+               if (ret != 0) {
+                       _LOGE("Couldnt get visiblity [%d], ret: %d", visibility, ret);
+                       ret = RPM_INSTALLER_ERR_INTERNAL;
+                       goto end;
+               } else{
+                       _LOGD("visibility : %d", visibility);
+               }
+       }
+
+       ret = _ri_apply_privilege(pkgid, visibility, smack_label);
        if (ret < 0) {
-               _LOGE("fota-info : _ri_apply_privilege fail[pkgid=%s]\n", pkgid);
+               _LOGE("fota-info : _ri_apply_privilege fail[pkgid=%s].", pkgid);
        }
 
-       _LOGD("fota-info : pkgid[%s] upgrade success\n", pkgid);
+       _LOGD("fota-info : pkgid[%s] upgrade success.", pkgid);
 
 end:
-       if (manifest)
-               free(manifest);
+       FREE_AND_NULL(smack_label);
 
        return ret;
 }
@@ -1292,27 +1260,55 @@ static int __ri_uninstall_fota(char *pkgid)
        _LOGD("fota-info : pkgid[%s] start uninstallation\n", pkgid);
 
        int ret = 0;
+       char buff[BUF_SIZE] = { '\0' };
+       char *smack_label = NULL;
 
-       ret = _ri_privilege_unregister_package(pkgid);
+       /* del root path dir */
+       snprintf(buff, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgid);
+       if (__is_dir(buff)) {
+               _installer_util_delete_dir(buff);
+       }
+
+       /* del root path dir for ext */
+       char extpath[BUF_SIZE] = { '\0' };
+       snprintf(extpath, BUF_SIZE, "%s/%s", OPT_STORAGE_SDCARD_APP_ROOT, pkgid);
+       if (__is_dir(extpath)) {
+               _installer_util_delete_dir(extpath);
+       }
+
+       /* Unregister cert info */
+       _ri_unregister_cert(pkgid);
+
+       ret = __get_smack_label_from_db(pkgid, &smack_label);
+       _LOGD("smack_label[%s], ret[%d]\n", smack_label, ret);
+
+       ret = _ri_privilege_unregister_package(smack_label);
        if (ret < 0) {
-               _LOGE("fota-info : _ri_privilege_unregister_package fail[pkgid=%s]\n", pkgid);
+               _LOGE("fota-info : _ri_privilege_unregister_package fail[smack_label=%s]\n", smack_label);
        }
 
        ret = pkgmgr_parser_parse_manifest_for_uninstallation(pkgid, NULL);
        if (ret < 0) {
-               _LOGE("fota-info : uninstall fail[manifest=%s]\n", pkgid);
+               _LOGE("fota-info : uninstall fail[pkgid=%s]\n", pkgid);
+       }
+
+       ret = _ri_smack_reload(smack_label, UNINSTALL_REQ);
+       if (ret != 0) {
+               _LOGD("_ri_smack_reload failed.");
        }
 
        _LOGD("fota-info : pkgid[%s] uninstall success\n", pkgid);
 
+       FREE_AND_NULL(smack_label);
+
        return ret;
 }
 
-static char * __getvalue(const char* pBuf, const char* pKey)
+static char *__getvalue(const char *pBuf, const char *pKey)
 {
-       const charp = NULL;
-       const charpStart = NULL;
-       const charpEnd = NULL;
+       const char *p = NULL;
+       const char *pStart = NULL;
+       const char *pEnd = NULL;
 
        p = strstr(pBuf, pKey);
        if (p == NULL)
@@ -1327,8 +1323,8 @@ static char * __getvalue(const char* pBuf, const char* pKey)
        if (len <= 0)
                return NULL;
 
-       char *pRes = (char*)malloc(len + 1);
-       if(pRes == NULL){
+       char *pRes = (char *)malloc(len + 1);
+       if (pRes == NULL) {
                _LOGE("@malloc failed");
                return NULL;
        }
@@ -1338,14 +1334,14 @@ static char * __getvalue(const char* pBuf, const char* pKey)
        return pRes;
 }
 
-static char *__find_rpm_pkgid(const charmanifest)
+static char *__find_rpm_pkgid(const char *manifest)
 {
        FILE *fp = NULL;
-       char buf[BUF_SIZE] = {0};
+       char buf[BUF_SIZE] = { 0 };
        char *pkgid = NULL;
 
        fp = fopen(manifest, "r");
-       if (fp == NULL) {
+       if (fp == NULL) {
                _LOGE("csc-info : Fail get : %s\n", manifest);
                return NULL;
        }
@@ -1353,7 +1349,7 @@ static char *__find_rpm_pkgid(const char* manifest)
        while (fgets(buf, BUF_SIZE, fp) != NULL) {
                __str_trim(buf);
                pkgid = __getvalue(buf, TOKEN_PACKAGE_STR);
-               if (pkgid !=  NULL) {
+               if (pkgid != NULL) {
                        fclose(fp);
                        return pkgid;
                }
@@ -1366,52 +1362,57 @@ static char *__find_rpm_pkgid(const char* manifest)
        return NULL;
 }
 
-static int __copy_file( const char *src_path, const char *dst_path)
+static int __copy_file(const char *src_path, const char *dst_path)
 {
-       FILE            *src, *dst;
-       int             rc = 0;
-       unsigned char   temp_buf[8192] = {'\0',};
-       size_t          size_of_uchar = sizeof( unsigned char);
-       size_t          size_of_temp_buf = sizeof( temp_buf);
-
-    src = fopen(src_path, "r");
-    if( src == NULL) {
-               _LOGE("Failed to open(). path=%s, E:%d(%s)", src_path, errno, strerror(errno));
-        return  -1;
-    }
-
-    dst = fopen(dst_path, "w");
-    if( dst == NULL) {
-               _LOGE("Failed to open dst file. file=%s, E:%d(%s)", dst_path, errno, strerror(errno));
-        fclose(src);
-        return  -1;
-    }
-
-    while(!feof(src)) {
-        rc = fread( temp_buf, size_of_uchar, size_of_temp_buf, src);
-        fwrite( temp_buf, size_of_uchar, rc, dst);
-    }
-
-    fclose( src);
-    fclose( dst);
-    return  0;
-}
-
+       FILE *src, *dst;
+       int rc = 0;
+       unsigned char temp_buf[8192] = { '\0', };
+       size_t size_of_uchar = sizeof(unsigned char);
+       size_t size_of_temp_buf = sizeof(temp_buf);
+       char buf[BUF_SIZE] = { 0, };
+
+       src = fopen(src_path, "r");
+       if (src == NULL) {
+               if( strerror_r(errno, buf, sizeof(buf)) == 0) {
+                       _LOGE("Failed to open(). path=%s, E:%d(%s)", src_path, errno, buf);
+               }
+               return -1;
+       }
+
+       dst = fopen(dst_path, "w");
+       if (dst == NULL) {
+               if( strerror_r(errno, buf, sizeof(buf)) == 0) {
+                       _LOGE("Failed to open dst file. file=%s, E:%d(%s)", dst_path, errno, buf);
+               }
+               fclose(src);
+               return -1;
+       }
+
+       while (!feof(src)) {
+               rc = fread(temp_buf, size_of_uchar, size_of_temp_buf, src);
+               fwrite(temp_buf, size_of_uchar, rc, dst);
+       }
+
+       fclose(src);
+       fclose(dst);
+       return 0;
+}
+
 static int __ri_install_csc(char *path_str, char *remove_str)
 {
        int ret = 0;
-
        char *pkgid = NULL;
        char delims[] = "/";
-       char* token = NULL;
-       char argv[BUF_SIZE] = {'\0'};
-       char xml_name[BUF_SIZE] = {'\0'};
-       char src_file[BUF_SIZE] = {'\0'};
-       char dest_file[BUF_SIZE] = {'\0'};
+       char *token = NULL;
+       char argv[BUF_SIZE] = { '\0' };
+       char xml_name[BUF_SIZE] = { '\0' };
+       char src_file[BUF_SIZE] = { '\0' };
+       char dest_file[BUF_SIZE] = { '\0' };
+       char *save_str = NULL;
 
        snprintf(src_file, sizeof(src_file), "%s", path_str);
 
-       /*get pkgid from path str*/
+       /* get pkgid from path str */
        pkgid = __find_rpm_pkgid(path_str);
        if (pkgid == NULL) {
                _LOGE("csc-info : fail to find pkgid\n");
@@ -1419,21 +1420,16 @@ static int __ri_install_csc(char *path_str, char *remove_str)
        }
        _LOGD("csc-info : find pkgid=[%s] for installation\n", pkgid);
 
-       /*find xml name*/
-       token = strtok(path_str, delims);
-       while(token)
-       {
+       /* find xml name */
+       token = strtok_r(path_str, delims, &save_str);
+       while (token) {
                memset(xml_name, 0x00, sizeof(xml_name));
-               if (strlen(token) < BUF_SIZE)
-                       strcat(xml_name, token);
-               else
-                       _LOGE("tokenized value too long[%s]", token);
-
-               token = strtok(NULL, delims);
+               strncat(xml_name, token, sizeof(xml_name) - 1);
+               token = strtok_r(NULL, delims, &save_str);
        }
        _LOGD("csc-info : xml name = %s\n", xml_name);
 
-       /*copy xml to /opt/share/packages*/
+       /* copy xml to /opt/share/packages */
        snprintf(dest_file, sizeof(dest_file), "%s/%s", OPT_SHARE_PACKAGES, xml_name);
        ret = __copy_file(src_file, dest_file);
        if (ret != 0) {
@@ -1442,7 +1438,7 @@ static int __ri_install_csc(char *path_str, char *remove_str)
                _LOGE("csc-info : xml copy success to [%s] \n", dest_file);
        }
 
-       /*remove old pkg info*/
+       /* remove old pkg info */
        ret = pkgmgr_parser_parse_manifest_for_uninstallation(pkgid, NULL);
        if (ret < 0) {
                _LOGD("csc-info : fail remove old pkg info\n");
@@ -1450,7 +1446,7 @@ static int __ri_install_csc(char *path_str, char *remove_str)
                _LOGD("csc-info : success remove old pkg info\n");
        }
 
-       /*insert new pkg info*/
+       /* insert new pkg info */
        memset(argv, 0x00, sizeof(argv));
        snprintf(argv, sizeof(argv), "%s/%s", OPT_SHARE_PACKAGES, xml_name);
        ret = __ri_init_csc_xml(argv, remove_str);
@@ -1459,7 +1455,7 @@ static int __ri_install_csc(char *path_str, char *remove_str)
        } else {
                _LOGD("csc-info : success xml name = %s\n", xml_name);
        }
-       if(pkgid){
+       if (pkgid) {
                free(pkgid);
                pkgid = NULL;
        }
@@ -1469,7 +1465,7 @@ static int __ri_install_csc(char *path_str, char *remove_str)
 
 static int __ri_uninstall_csc(char *pkgid)
 {
-       /*remove old pkg info*/
+       /* remove old pkg info */
        int ret = pkgmgr_parser_parse_manifest_for_uninstallation(pkgid, NULL);
        if (ret < 0) {
                _LOGD("csc-info : fail remove old pkg info\n");
@@ -1480,7 +1476,6 @@ static int __ri_uninstall_csc(char *pkgid)
        return 0;
 }
 
-
 static int __get_size_from_xml(const char *manifest, int *size)
 {
        const char *val = NULL;
@@ -1488,19 +1483,19 @@ static int __get_size_from_xml(const char *manifest, int *size)
        xmlTextReaderPtr reader;
        int ret = PMINFO_R_OK;
 
-       if(manifest == NULL) {
+       if (manifest == NULL) {
                _LOGE("Input argument is NULL\n");
                return PMINFO_R_ERROR;
        }
 
-       if(size == NULL) {
+       if (size == NULL) {
                _LOGE("Argument supplied to hold return value is NULL\n");
                return PMINFO_R_ERROR;
        }
 
        reader = xmlReaderForFile(manifest, NULL, 0);
 
-       if (reader){
+       if (reader) {
                if (_child_element(reader, -1)) {
                        node = xmlTextReaderConstName(reader);
                        if (!node) {
@@ -1510,19 +1505,19 @@ static int __get_size_from_xml(const char *manifest, int *size)
                        }
 
                        if (!strcmp(ASCII(node), "manifest")) {
-                               ret = _ri_get_attribute(reader,"size",&val);
-                               if(ret != 0){
+                               ret = _ri_get_attribute(reader, "size", &val);
+                               if (ret != 0) {
                                        _LOGE("@Error in getting the attribute value");
                                        ret = PMINFO_R_ERROR;
                                        goto end;
                                }
                                if (val) {
                                        *size = atoi(val);
-                                       free((void*)val);
+                                       free((void *)val);
                                } else {
                                        *size = 0;
                                        _LOGE("package size is not specified\n");
-                                       ret =  PMINFO_R_ERROR;
+                                       ret = PMINFO_R_ERROR;
                                        goto end;
                                }
                        } else {
@@ -1533,36 +1528,35 @@ static int __get_size_from_xml(const char *manifest, int *size)
                }
        } else {
                _LOGE("xmlReaderForFile value is NULL\n");
-               ret =  PMINFO_R_ERROR;
+               ret = PMINFO_R_ERROR;
        }
 
-
 end:
        xmlFreeTextReader(reader);
-       return PMINFO_R_OK;
+       return ret;
 }
 
-static int __get_location_from_xml(const char *manifest, pkgmgrinfo_install_location *location)
+static int __get_location_from_xml(const char *manifest, pkgmgrinfo_install_location * location)
 {
        const char *val = NULL;
        const xmlChar *node;
        xmlTextReaderPtr reader;
        int ret = -1;
 
-       if(manifest == NULL) {
+       if (manifest == NULL) {
                _LOGE("Input argument is NULL\n");
                return PMINFO_R_ERROR;
        }
 
-       if(location == NULL) {
+       if (location == NULL) {
                _LOGE("Argument supplied to hold return value is NULL\n");
                return PMINFO_R_ERROR;
        }
 
        reader = xmlReaderForFile(manifest, NULL, 0);
 
-       if (reader){
-               if ( _child_element(reader, -1)) {
+       if (reader) {
+               if (_child_element(reader, -1)) {
                        node = xmlTextReaderConstName(reader);
                        if (!node) {
                                _LOGE("xmlTextReaderConstName value is NULL\n");
@@ -1571,8 +1565,8 @@ static int __get_location_from_xml(const char *manifest, pkgmgrinfo_install_loca
                        }
 
                        if (!strcmp(ASCII(node), "manifest")) {
-                               ret = _ri_get_attribute(reader,"install-location",&val);
-                               if(ret != 0){
+                               ret = _ri_get_attribute(reader, "install-location", &val);
+                               if (ret != 0) {
                                        _LOGE("@Error in getting the attribute value");
                                        xmlFreeTextReader(reader);
                                        return PMINFO_R_ERROR;
@@ -1585,7 +1579,7 @@ static int __get_location_from_xml(const char *manifest, pkgmgrinfo_install_loca
                                                *location = PMINFO_INSTALL_LOCATION_PREFER_EXTERNAL;
                                        else
                                                *location = PMINFO_INSTALL_LOCATION_AUTO;
-                                       free((void*)val);
+                                       free((void *)val);
                                }
                        } else {
                                _LOGE("Unable to create xml reader\n");
@@ -1606,26 +1600,33 @@ static int __get_location_from_xml(const char *manifest, pkgmgrinfo_install_loca
 static char *__get_pkg_path(const char *pkg_path, const char *pkgid)
 {
        int ret = 0;
-       char buff[BUF_SIZE] = {'\0'};
+       char buff[BUF_SIZE] = { '\0' };
        char *real_path = NULL;
+       char buf[BUF_SIZE] = { 0, };
 
        snprintf(buff, BUF_SIZE, "%s/%s", pkg_path, pkgid);
        do {
-               if (__is_dir(buff)) break;
+               if (__is_dir(buff))
+                       break;
                memset(buff, '\0', BUF_SIZE);
-               snprintf(buff, BUF_SIZE, "%s/%s", USR_APPS,pkgid);
-               if (__is_dir(buff)) break;
+               snprintf(buff, BUF_SIZE, "%s/%s", USR_APPS, pkgid);
+               if (__is_dir(buff))
+                       break;
                memset(buff, '\0', BUF_SIZE);
                snprintf(buff, BUF_SIZE, "/opt/apps/%s", pkgid);
-               if (__is_dir(buff)) break;
+               if (__is_dir(buff))
+                       break;
                memset(buff, '\0', BUF_SIZE);
-               snprintf(buff, BUF_SIZE, "%s/%s", OPT_USR_APPS,pkgid);
-               if (__is_dir(buff)) break;
+               snprintf(buff, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgid);
+               if (__is_dir(buff))
+                       break;
        } while (0);
 
        ret = chdir(buff);
        if (ret != 0) {
-               _LOGE("chdir(%s) failed. [%s]", buff, strerror(errno));
+               if( strerror_r(errno, buf, sizeof(buf)) == 0) {
+                       _LOGE("chdir(%s) failed. [%s]", buff, buf);
+               }
                return NULL;
        }
 
@@ -1640,122 +1641,12 @@ static char *__get_pkg_path(const char *pkg_path, const char *pkgid)
        return real_path;
 }
 
-static int __set_label_for_pkg_dir(const char *dirpath, const char* label)
-{
-       int ret = 0;
-       DIR *dir;
-       struct dirent entry;
-       struct dirent *result;
-       char fullpath[BUF_SIZE] = {'\0'};
-
-       if (access(dirpath, F_OK) != 0) {
-               _LOGE("skip! empty dirpath = [%s]", dirpath);
-               return 0;
-       }
-
-       dir = opendir(dirpath);
-       if (!dir) {
-               _LOGE("opendir(%s) failed. [%d][%s]", dirpath, errno, strerror(errno));
-               return -1;
-       }
-
-       if(lsetxattr(dirpath, "security.SMACK64", label, strlen(label), 0)) {
-               _LOGE("error(%d) in setting smack label",errno);
-       }
-
-       for (ret = readdir_r(dir, &entry, &result); ret == 0 && result != NULL; ret = readdir_r(dir, &entry, &result)){
-               if (!strcmp(entry.d_name, ".") ||
-                       !strcmp(entry.d_name, "..")) {
-                       continue;
-               }
-
-               // sub
-               snprintf(fullpath, BUF_SIZE, "%s/%s", dirpath, entry.d_name);
-               if(lsetxattr(fullpath, "security.SMACK64", label, strlen(label), 0)) {
-                       _LOGE("error(%d) in setting smack label",errno);
-               }
-
-
-               // find next dir
-               if (entry.d_type == DT_DIR) {
-                       ret = __set_label_for_pkg_dir(fullpath,label);
-                       if(ret != 0 ){
-                               _LOGE("_coretpk_installer_apply_directory_policy(%s) failed.", fullpath);
-                       }
-               }
-               memset(fullpath, '\0', BUF_SIZE);
-       }
-
-       closedir(dir);
-
-       return ret;
-
-}
-
-void _ri_soft_reset(char *pkgid)
-{
-       int ret = 0;
-       char dirpath[PKG_STRING_LEN_MAX] = {'\0'};
-
-       //home
-       snprintf(dirpath, PKG_STRING_LEN_MAX, "%s/%s", OPT_USR_APPS, pkgid);
-       if (access(dirpath, F_OK)==0) {
-               __set_label_for_pkg_dir(dirpath, "_");
-       }
-
-       // data
-       memset(dirpath, '\0', PKG_STRING_LEN_MAX);
-       snprintf(dirpath, PKG_STRING_LEN_MAX, "%s/%s/data", OPT_USR_APPS, pkgid);
-       if (access(dirpath, F_OK)==0) {
-               __set_label_for_pkg_dir(dirpath, pkgid);
-       }
-
-       //shared
-       memset(dirpath, '\0', PKG_STRING_LEN_MAX);
-       snprintf(dirpath, PKG_STRING_LEN_MAX, "%s/%s/shared", OPT_USR_APPS, pkgid);
-       if (access(dirpath, F_OK)==0) {
-               __set_label_for_pkg_dir(dirpath, "_");
-       }
-
-       //shared/res
-       memset(dirpath, '\0', PKG_STRING_LEN_MAX);
-       snprintf(dirpath, PKG_STRING_LEN_MAX, "%s/%s/shared/res", OPT_USR_APPS, pkgid);
-       if (access(dirpath, F_OK)==0) {
-               __set_label_for_pkg_dir(dirpath, "_");
-       }
-
-       //shared/data
-       memset(dirpath, '\0', PKG_STRING_LEN_MAX);
-       snprintf(dirpath, PKG_STRING_LEN_MAX, "%s/%s/shared/data", OPT_USR_APPS, pkgid);
-       if (access(dirpath, F_OK)==0) {
-               __set_label_for_pkg_dir(dirpath, "_");
-       }
-
-       //shared/trusted
-       memset(dirpath, '\0', PKG_STRING_LEN_MAX);
-       snprintf(dirpath, PKG_STRING_LEN_MAX, "%s/%s/shared/trusted", OPT_USR_APPS, pkgid);
-       if (access(dirpath, F_OK)==0) {
-               pkgmgrinfo_pkginfo_h handle = NULL;
-               char *groupid = NULL;
-               ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
-               if (ret != PMINFO_R_OK)
-                       return;
-               ret = pkgmgrinfo_pkginfo_get_groupid(handle, &groupid);
-               if (ret == PMINFO_R_OK) {
-                       _ri_privilege_setup_path(pkgid, dirpath, APP_PATH_GROUP_RW, groupid);
-               }
-               pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
-       }
-
-       return;
-}
-
 void _ri_register_cert(const char *pkgid)
 {
        int error = 0;
        pkgmgrinfo_instcertinfo_h handle = NULL;
        int i = 0;
-       /* create Handle*/
+       /* create Handle */
        error = pkgmgrinfo_create_certinfo_set_handle(&handle);
        if (error != 0) {
                _LOGE("Cert handle creation failed. Err:%d", error);
@@ -1778,7 +1669,7 @@ void _ri_register_cert(const char *pkgid)
                        }
                }
        }
-       /* Save the certificates in cert DB*/
+       /* Save the certificates in cert DB */
        error = pkgmgrinfo_save_certinfo(pkgid, handle);
        if (error != 0) {
                _LOGE("pkgmgrinfo_save_certinfo failed. Err:%d", error);
@@ -1793,7 +1684,7 @@ err:
 void _ri_unregister_cert(const char *pkgid)
 {
        int error = 0;
-       /* Delete the certifictes from cert DB*/
+       /* Delete the certifictes from cert DB */
        error = pkgmgrinfo_delete_certinfo(pkgid);
        if (error != 0) {
                _LOGE("pkgmgrinfo_delete_certinfo failed. Err:%d", error);
@@ -1801,13 +1692,159 @@ void _ri_unregister_cert(const char *pkgid)
        }
 }
 
-int _ri_verify_sig_and_cert(const char *sigfile, int *visibility)
+int _ri_get_visibility_from_signature_file(const char *sigfile, int *visibility, bool save_ca_path)
+{
+       char certval[BUF_SIZE] = { '\0' };
+       int err = 0;
+       int i = 0;
+       int j = 0;
+       int ret = RPM_INSTALLER_SUCCESS;
+       signature_x *signx = NULL;
+       struct keyinfo_x *keyinfo = NULL;
+       struct x509data_x *x509data = NULL;
+       CERT_CONTEXT *ctx = NULL;
+       int validity = 0;
+
+       if (sigfile == NULL) {
+               return RPM_INSTALLER_ERR_WRONG_PARAM;
+       }
+
+       ctx = cert_svc_cert_context_init();
+       if (ctx == NULL) {
+               _LOGE("cert_svc_cert_context_init() failed.");
+               return RPM_INSTALLER_ERR_INTERNAL;
+       }
+
+       if (!strstr(sigfile, SIGNATURE1_XML)) {
+               _LOGE("Unsupported signature type! [%s]", sigfile);
+               cert_svc_cert_context_final(ctx);
+               return RPM_INSTALLER_ERR_INTERNAL;
+       }
+
+       signx = _ri_process_signature_xml(sigfile);
+       if (signx == NULL) {
+               _LOGE("_ri_process_signature_xml(%s) failed.", sigfile);
+               ret = RPM_INSTALLER_ERR_INTERNAL;
+               goto end;
+       }
+
+       keyinfo = signx->keyinfo;
+       if ((keyinfo == NULL) || (keyinfo->x509data == NULL)
+               || (keyinfo->x509data->x509certificate == NULL)) {
+               _LOGE("keyinfo is invalid. [%s]", sigfile);
+               ret = RPM_INSTALLER_ERR_CERT_INVALID;
+               goto end;
+       }
+
+       x509data = keyinfo->x509data;
+       x509certificate_x *cert = x509data->x509certificate;
+
+       /* First cert is Signer certificate */
+       if (cert->text != NULL) {
+               for (i = 0; i <= (int)strlen(cert->text); i++) {
+                       if (cert->text[i] != '\n') {
+                               certval[j++] = cert->text[i];
+                       }
+               }
+               certval[j] = '\0';
+
+               err = cert_svc_load_buf_to_context(ctx, (unsigned char *)certval);
+               if (err != 0) {
+                       _LOGE("cert_svc_load_buf_to_context() failed. cert = [%s], err = [%d]", certval, err);
+                       ret = RPM_INSTALLER_ERR_INTERNAL;
+                       goto end;
+               }
+
+               if (save_ca_path) {
+                       err = __ri_create_cert_chain(SIG_DIST1, SIG_SIGNER, certval);
+                       if (err) {
+                               _LOGE("__ri_create_cert_chain() failed. sigtype = [%d], certval = [%s]", SIG_DIST1, certval);
+                               __ri_free_cert_chain();
+                               ret = RPM_INSTALLER_ERR_INTERNAL;
+                               goto end;
+                       }
+               }
+       }
+
+       /* Second cert is Intermediate certificate */
+       cert = cert->next;
+       if ((cert != NULL) && (cert->text != NULL)) {
+               memset(certval, 0x00, BUF_SIZE);
+               j = 0;
+               for (i = 0; i <= (int)strlen(cert->text); i++) {
+                       if (cert->text[i] != '\n') {
+                               certval[j++] = cert->text[i];
+                       }
+               }
+               certval[j] = '\0';
+
+               if (cert->text != NULL) {
+                       err = cert_svc_push_buf_into_context(ctx, (unsigned char *)certval);
+                       if (err != 0) {
+                               _LOGE("cert_svc_push_buf_into_context() failed. cert = [%s], err = [%d]", certval, err);
+                               ret = RPM_INSTALLER_ERR_INTERNAL;
+                               goto end;
+                       }
+               }
+
+               if (save_ca_path) {
+                       err = __ri_create_cert_chain(SIG_DIST1, SIG_INTERMEDIATE, certval);
+                       if (err) {
+                               _LOGE("__ri_create_cert_chain() failed. sigtype = [%d], certval = [%s]", SIG_DIST1, certval);
+                               __ri_free_cert_chain();
+                               ret = RPM_INSTALLER_ERR_INTERNAL;
+                               goto end;
+                       }
+               }
+       } else {
+               _LOGE("Invalid CertChain! (cert->text is NULL.)");
+               ret = RPM_INSTALLER_ERR_CERT_INVALID;
+               goto end;
+       }
+
+       err = cert_svc_verify_certificate(ctx, &validity);
+
+       if (err != 0) {
+               _LOGE("cert_svc_verify_certificate() failed.");
+               ret = RPM_INSTALLER_ERR_INTERNAL;
+               goto end;
+       }
+       if (validity == 0) {
+               _LOGE("Certificate Invalid/Expired (validity == 0)");
+               ret = RPM_INSTALLER_ERR_CERTCHAIN_VERIFICATION_FAILED;
+               goto end;
+       }
+       _LOGD("cert_svc_verify() is done successfully. validity=[%d]", validity);
+
+       err = cert_svc_get_visibility(ctx, visibility);
+       if (err != 0) {
+               _LOGE("cert_svc_get_visibility() failed. err = [%d]", err);
+               ret = RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED;
+               goto end;
+       }
+       ret = 0;
+
+       if (save_ca_path && ctx->fileNames && ctx->fileNames->filename) {
+               FREE_AND_NULL(sig1_capath);
+               sig1_capath = strdup(ctx->fileNames->filename);
+               sig1_visibility = *visibility;
+       }
+
+end:
+       cert_svc_cert_context_final(ctx);
+       ctx = NULL;
+       _ri_free_signature_xml(signx);
+       signx = NULL;
+       return ret;
+}
+
+int _ri_verify_sig_and_cert(const char *sigfile, int *visibility, bool need_verify, char *ca_path)
 {
-       char certval[MAX_BUFF_LEN] = {'\0'};
+       char certval[BUF_SIZE] = { '\0' };
        int err = 0;
        int validity = 0;
        int i = 0;
-       int j= 0;
+       int j = 0;
        int ret = RPM_INSTALLER_SUCCESS;
        char *crt = NULL;
        signature_x *signx = NULL;
@@ -1815,6 +1852,7 @@ int _ri_verify_sig_and_cert(const char *sigfile, int *visibility)
        struct x509data_x *x509data = NULL;
        CERT_CONTEXT *ctx = NULL;
        int sigtype = 0;
+       char *root_ca_path = NULL;
 
        ctx = cert_svc_cert_context_init();
        if (ctx == NULL) {
@@ -1834,10 +1872,16 @@ int _ri_verify_sig_and_cert(const char *sigfile, int *visibility)
                return RPM_INSTALLER_ERR_INTERNAL;
        }
 
+       if (sigtype == SIG_DIST1 && ca_path != NULL && strlen(ca_path) != 0) {
+               root_ca_path = ca_path;
+               *visibility = sig1_visibility;
+               goto verify_sig;
+       }
+
        signx = _ri_process_signature_xml(sigfile);
        if (signx == NULL) {
                _LOGE("_ri_process_signature_xml(%s) failed.", sigfile);
-               ret = RPM_INSTALLER_ERR_INTERNAL;
+               ret = RPM_INSTALLER_ERR_SIG_INVALID;
                goto end;
        }
 
@@ -1851,7 +1895,7 @@ int _ri_verify_sig_and_cert(const char *sigfile, int *visibility)
        x509data = keyinfo->x509data;
        x509certificate_x *cert = x509data->x509certificate;
 
-       // First cert is Signer certificate
+       /* First cert is Signer certificate */
        if (cert->text != NULL) {
                for (i = 0; i <= (int)strlen(cert->text); i++) {
                        if (cert->text[i] != '\n') {
@@ -1860,27 +1904,36 @@ int _ri_verify_sig_and_cert(const char *sigfile, int *visibility)
                }
                certval[j] = '\0';
 
-               err = cert_svc_load_buf_to_context(ctx, (unsigned char*)certval);
+               err = cert_svc_load_buf_to_context(ctx, (unsigned char *)certval);
                if (err != 0) {
                        _LOGE("cert_svc_load_buf_to_context() failed. cert = [%s], err = [%d]", certval, err);
-                       ret = RPM_INSTALLER_ERR_INTERNAL;
+                       ret = RPM_INSTALLER_ERR_CERT_INVALID;
                        goto end;
                }
-               // _LOGD("SIG_SIGNER certval = [%d][%s]", strlen(certval), certval);
 
                err = __ri_create_cert_chain(sigtype, SIG_SIGNER, certval);
                if (err) {
                        _LOGE("__ri_create_cert_chain() failed. sigtype = [%d], certval = [%s]", sigtype, certval);
                        __ri_free_cert_chain();
-                       ret = RPM_INSTALLER_ERR_INTERNAL;
+                       ret = RPM_INSTALLER_ERR_CERT_INVALID;
                        goto end;
                }
+       } else {
+               _LOGE("cert->text is NULL. [Signer]");
+               ret = RPM_INSTALLER_ERR_CERT_INVALID;
+               goto end;
        }
 
-       // Second cert is Intermediate certificate
+       /* Second cert is Intermediate certificate */
        cert = cert->next;
+       if (cert == NULL) {
+               _LOGE("cert is NULL. [Intermediate]");
+               ret = RPM_INSTALLER_ERR_CERT_INVALID;
+               goto end;
+       }
+
        if (cert->text != NULL) {
-               memset(certval, 0x00, MAX_BUFF_LEN);
+               memset(certval, 0x00, BUF_SIZE);
                j = 0;
                for (i = 0; i <= (int)strlen(cert->text); i++) {
                        if (cert->text[i] != '\n') {
@@ -1890,76 +1943,79 @@ int _ri_verify_sig_and_cert(const char *sigfile, int *visibility)
                certval[j] = '\0';
 
                if (cert->text != NULL) {
-                       err = cert_svc_push_buf_into_context(ctx, (unsigned char*)certval);
+                       err = cert_svc_push_buf_into_context(ctx, (unsigned char *)certval);
                        if (err != 0) {
                                _LOGE("cert_svc_push_buf_into_context() failed. cert = [%s], err = [%d]", certval, err);
-                               ret = RPM_INSTALLER_ERR_INTERNAL;
+                               ret = RPM_INSTALLER_ERR_CERT_INVALID;
                                goto end;
                        }
                }
-               // _LOGD("SIG_INTERMEDIATE certval = [%d][%s]", strlen(certval), certval);
 
                err = __ri_create_cert_chain(sigtype, SIG_INTERMEDIATE, certval);
                if (err) {
                        _LOGE("__ri_create_cert_chain() failed. sigtype = [%d], certval = [%s]", sigtype, certval);
                        __ri_free_cert_chain();
-                       ret = RPM_INSTALLER_ERR_INTERNAL;
+                       ret = RPM_INSTALLER_ERR_CERT_INVALID;
                        goto end;
                }
        } else {
-               _LOGE("Invalid CertChain! (cert->text is NULL.)");
+               _LOGE("Invalid CertChain! (cert->text is NULL.) [Intermediate]");
                ret = RPM_INSTALLER_ERR_CERT_INVALID;
                goto end;
        }
 
        err = cert_svc_verify_certificate(ctx, &validity);
-       if (err != 0) {
-               _LOGE("cert_svc_verify_certificate() failed.");
-               ret = RPM_INSTALLER_ERR_INTERNAL;
+       if (err != 0 && (need_verify == true)) {
+               _LOGE("cert_svc_verify_certificate() failed. err=[%d]", err);
+               ret = err;
                goto end;
        }
        _LOGD("cert_svc_verify() is done successfully. validity=[%d]", validity);
 
        if (validity == 0) {
                _LOGE("Certificate Invalid/Expired (validity == 0)");
-               ret = RPM_INSTALLER_ERR_CERTCHAIN_VERIFICATION_FAILED;
+               ret = RPM_INSTALLER_ERR_CERTIFICATE_EXPIRED;
                goto end;
        }
 
-       // verify signature
-       // For reference validation, we should be in TEMP_DIR/usr/apps/<pkgid>
-       if (ctx->fileNames && ctx->fileNames->filename) {
-               _LOGD("Root CA cert path=[%s]", ctx->fileNames->filename);
+       err = cert_svc_get_visibility(ctx, visibility);
+       if (err != 0) {
+               _LOGE("cert_svc_get_visibility() failed. err = [%d]", err);
+               ret = RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED;
+               goto end;
+       }
+       _LOGD("cert_svc_get_visibility() returns visibility=[%d]", *visibility);
 
-               err = __ri_xmlsec_verify_signature(sigfile, ctx->fileNames->filename);
-               if (err < 0) {
+       if (ctx->fileNames == NULL || ctx->fileNames->filename == NULL) {
+               _LOGE("No Root CA cert found. Signature validation failed.");
+               ret = RPM_INSTALLER_ERR_ROOT_CERT_NOT_FOUND;
+               goto end;
+       } else
+               root_ca_path = ctx->fileNames->filename;
+
+verify_sig:
+       /* verify signature
+          For reference validation, we should be in TEMP_DIR/usr/apps/<pkgid> */
+       if (root_ca_path != NULL && strlen(root_ca_path) != 0) {
+               _LOGD("Root CA cert path=[%s]", root_ca_path);
+
+               err = __ri_xmlsec_verify_signature(sigfile, root_ca_path);
+               if (err < 0 && (need_verify == true)) {
                        _LOGE("__ri_xmlsec_verify_signature(%s) failed.", sigfile);
                        ret = RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED;
                        goto end;
                }
 
-               crt = __ri_get_cert_from_file(ctx->fileNames->filename);
+               crt = __ri_get_cert_from_file(root_ca_path);
                err = __ri_create_cert_chain(sigtype, SIG_ROOT, crt);
                if (err) {
                        _LOGE("__ri_create_cert_chain(%d) failed.", sigtype);
                        __ri_free_cert_chain();
-                       ret = RPM_INSTALLER_ERR_INTERNAL;
+                       ret = RPM_INSTALLER_ERR_CERT_INVALID;
                        goto end;
                }
 
-               err = cert_svc_get_visibility(ctx, visibility);
-               if (err != 0) {
-                       _LOGE("cert_svc_get_visibility() failed. err = [%d]", err);
-                       ret = RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED;
-                       goto end;
-               }
-               _LOGD("cert_svc_get_visibility() returns visibility=[%d]", *visibility);
-       } else {
-               _LOGE("No Root CA cert found. Signature validation failed.");
-               ret = RPM_INSTALLER_ERR_ROOT_CERT_NOT_FOUND;
-               goto end;
        }
-
        ret = 0;
 
 end:
@@ -1970,16 +2026,16 @@ end:
        return ret;
 }
 
-int _ri_verify_signatures(const char *root_path, const char *pkgid)
+int _ri_verify_signatures(const char *root_path, const char *pkgid, bool need_verify)
 {
        int ret = 0;
-       char buff[BUF_SIZE] = {'\0'};
+       char buff[BUF_SIZE] = { '\0' };
        char *pkg_path = NULL;
        int visibility = 0;
 
        _LOGD("root_path=[%s], pkgid=[%s]", root_path, pkgid);
 
-       // check for signature and certificate
+       /* check for signature and certificate */
        pkg_path = __get_pkg_path(root_path, pkgid);
        if (pkg_path == NULL) {
                _LOGE("__get_pkg_path(%s, %s) failed.", root_path, pkgid);
@@ -1988,11 +2044,11 @@ int _ri_verify_signatures(const char *root_path, const char *pkgid)
 
        _LOGD("switched to pkg_path=[%s]", pkg_path);
 
-       // author-signature.xml is mandatory
+       /* author-signature.xml is mandatory */
        snprintf(buff, BUF_SIZE, "%s/author-signature.xml", pkg_path);
        if (access(buff, F_OK) == 0) {
                _LOGD("author-signature.xml, path=[%s]", buff);
-               ret = _ri_verify_sig_and_cert(buff, &visibility);
+               ret = _ri_verify_sig_and_cert(buff, &visibility, need_verify, NULL);
                if (ret) {
                        _LOGE("_ri_verify_sig_and_cert(%s) failed.", buff);
                        ret = -1;
@@ -2005,11 +2061,11 @@ int _ri_verify_signatures(const char *root_path, const char *pkgid)
        }
        memset(buff, '\0', BUF_SIZE);
 
-       // signature2.xml is optional
+       /* signature2.xml is optional */
        snprintf(buff, BUF_SIZE, "%s/signature2.xml", pkg_path);
        if (access(buff, F_OK) == 0) {
                _LOGD("signature2.xml found. [%s]", pkg_path);
-               ret = _ri_verify_sig_and_cert(buff, &visibility);
+               ret = _ri_verify_sig_and_cert(buff, &visibility, need_verify, NULL);
                if (ret) {
                        _LOGE("_ri_verify_sig_and_cert(%s) failed.", buff);
                        ret = -1;
@@ -2019,11 +2075,11 @@ int _ri_verify_signatures(const char *root_path, const char *pkgid)
        }
        memset(buff, '\0', BUF_SIZE);
 
-       // signature1.xml is mandatory
+       /* signature1.xml is mandatory */
        snprintf(buff, BUF_SIZE, "%s/signature1.xml", pkg_path);
        if (access(buff, F_OK) == 0) {
                _LOGD("signature1.xml, path=[%s]", buff);
-               ret = _ri_verify_sig_and_cert(buff, &visibility);
+               ret = _ri_verify_sig_and_cert(buff, &visibility, need_verify, NULL);
                if (ret) {
                        _LOGE("_ri_verify_sig_and_cert(%s) failed.", buff);
                        ret = -1;
@@ -2038,8 +2094,8 @@ int _ri_verify_signatures(const char *root_path, const char *pkgid)
 
        ret = 0;
 
-end :
-       if(pkg_path){
+end:
+       if (pkg_path) {
                free(pkg_path);
                pkg_path = NULL;
        }
@@ -2052,43 +2108,54 @@ end :
        return ret;
 }
 
-void _ri_apply_smack(char *pkgname, int flag)
+void _ri_apply_smack(const char *pkgname, int flag, char *smack_label)
 {
-       __rpm_apply_smack(pkgname, flag);
+       __rpm_apply_smack(pkgname, flag, smack_label);
 }
 
-int _ri_apply_privilege(char *pkgid, int visibility)
+int _ri_apply_privilege(const char *pkgid, int visibility, char *smack_label)
 {
        int ret = -1;
        pkgmgrinfo_pkginfo_h handle = NULL;
        privilegeinfo info;
        int apptype = PERM_APP_TYPE_EFL;
+       int i = 0;
+       char *privilege_fota[BUF_SIZE] = { NULL, };
+       char *api_version = NULL;
+       GList *list = NULL;
 
-       if (!pkgid) {
-               _LOGE("pkgid is NULL");
-               return -1;
+       if (smack_label == NULL || strlen(smack_label) == 0) {
+               smack_label = (char *)pkgid;
        }
 
        memset(&info, '\0', sizeof(info));
 
        ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
-       if (ret != PMINFO_R_OK)
+       if (ret != PMINFO_R_OK) {
+               _LOGE("pkgmgrinfo_pkginfo_get_pkginfo(%s) failed.", pkgid);
                return -1;
+       }
 
-       if (strlen(pkgid) < PKG_MAX_LEN)
-               strcpy(info.package_id, pkgid);
-       else {
-               _LOGE("pkgid too long[%s]", pkgid);
-               if (handle)
+       ret = pkgmgrinfo_pkginfo_get_api_version(handle, &api_version);
+       if (ret != PMINFO_R_OK)
+               _LOGE("failed to get api version (%s)", pkgid);
+
+       if (api_version) {
+               ret = _ri_privilege_set_package_version(smack_label, api_version);
+               if (ret != 0) {
+                       _LOGE("failed to set api version for smack_label: %s, ret[%d]", smack_label, ret);
                        pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
-               return -1;
+                       return -1;
+               } else
+                       _LOGD("api-version[%s] fot privilege has done successfully.", api_version);
        }
 
+       strncpy(info.package_id, pkgid, sizeof(info.package_id) - 1);
        info.visibility = visibility;
 
        ret = pkgmgrinfo_pkginfo_foreach_privilege(handle, __privilege_func, (void *)&info);
        if (ret != PMINFO_R_OK) {
-               _LOGE("pkgmgrinfo_pkginfo_get_pkgid failed\n");
+               _LOGE("pkgmgrinfo_pkginfo_foreach_privilege(%s) failed.", pkgid);
                pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
                return -1;
        }
@@ -2098,18 +2165,49 @@ int _ri_apply_privilege(char *pkgid, int visibility)
                _LOGD("VISIBILITY_PLATFORM!");
                apptype = PERM_APP_TYPE_EFL_PLATFORM;
        } else if ((visibility & CERT_SVC_VISIBILITY_PARTNER) ||
-                       (visibility & CERT_SVC_VISIBILITY_PARTNER_OPERATOR) ||
-                       (visibility & CERT_SVC_VISIBILITY_PARTNER_MANUFACTURER)) {
+               (visibility & CERT_SVC_VISIBILITY_PARTNER_OPERATOR) ||
+               (visibility & CERT_SVC_VISIBILITY_PARTNER_MANUFACTURER)) {
                _LOGD("VISIBILITY_PARTNER!");
                apptype = PERM_APP_TYPE_EFL_PARTNER;
        }
 
-       /*reload privilege*/
-       const char *perm[] = {NULL, NULL};
-       ret = _ri_privilege_enable_permissions(pkgid, apptype, perm, 1);
-
-       return 0;
-}
+       list = g_list_first(info.privileges);
+
+       while (list) {
+               privilege_fota[i] = strdup((char *)list->data);
+               i++;
+               list = g_list_next(list);
+       }
+
+       privilege_fota[i] = NULL;
+
+       ret = _ri_privilege_enable_permissions(smack_label, apptype, (const char **)privilege_fota, 1);
+       for (i = 0; i < g_list_length(info.privileges); i++) {
+               if (privilege_fota[i]) {
+                       free(privilege_fota[i]);
+                       privilege_fota[i] = NULL;
+               }
+       }
+
+       if (info.privileges != NULL) {
+               list = g_list_first(info.privileges);
+               while (list) {
+                       if (list->data) {
+                               free(list->data);
+                       }
+                       list = g_list_next(list);
+               }
+               g_list_free(info.privileges);
+               info.privileges = NULL;
+       }
+
+       if (ret < 0) {
+               _LOGE("_ri_privilege_enable_permissions(%s, %d) failed.", smack_label, apptype);
+               return -1;
+       }
+
+       return 0;
+}
 
 int _ri_set_group_id(const char *pkgid, const char *groupid)
 {
@@ -2119,11 +2217,11 @@ int _ri_set_group_id(const char *pkgid, const char *groupid)
        sqlite3 *pkginfo_db = NULL;
        char *query = NULL;
 
-       /*open db*/
+       /* open db */
        ret = db_util_open(PKGMGR_DB, &pkginfo_db, 0);
        retvm_if(ret != SQLITE_OK, PMINFO_R_ERROR, "connect db [%s] failed!", PKGMGR_DB);
 
-       /*Begin transaction*/
+       /* Begin transaction */
        ret = sqlite3_exec(pkginfo_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
        tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Failed to begin transaction\n");
        _LOGD("Transaction Begin\n");
@@ -2133,7 +2231,7 @@ int _ri_set_group_id(const char *pkgid, const char *groupid)
        ret = sqlite3_exec(pkginfo_db, query, NULL, NULL, NULL);
        tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
 
-       /*Commit transaction*/
+       /* Commit transaction */
        ret = sqlite3_exec(pkginfo_db, "COMMIT", NULL, NULL, NULL);
        if (ret != SQLITE_OK) {
                _LOGE("Failed to commit transaction. Rollback now\n");
@@ -2146,6 +2244,7 @@ int _ri_set_group_id(const char *pkgid, const char *groupid)
 catch:
        sqlite3_free(query);
        sqlite3_close(pkginfo_db);
+
        return ret;
 }
 
@@ -2153,44 +2252,48 @@ static int __ri_install_fota_for_rw(char *pkgid)
 {
        int ret = 0;
        int home_dir = 1;
-       char buff[BUF_SIZE] = {'\0'};
+       char buff[BUF_SIZE] = { '\0' };
+       char *smack_label = NULL;
+       int visibility = 0;
+       char signature_file[BUF_SIZE] = {0, };
 
        _LOGD("fota-info : pkgid[%s] start installation\n", pkgid);
 
-       /*unzip pkg path from factoryrest data*/
-       snprintf(buff, BUF_SIZE, "opt/usr/apps/%s/*", pkgid);
+       /* unzip pkg path from factoryrest data */
+       snprintf(buff, BUF_SIZE, "opt/usr/apps/%s/*", pkgid); // relative path
        const char *pkg_argv[] = { "/usr/bin/unzip", "-oX", OPT_ZIP_FILE, buff, "-d", "/", NULL };
        ret = _ri_xsystem(pkg_argv);
        if (ret != 0) {
                _LOGE("fota-info : unzip root path[%s] is fail .\n", buff);
-               return ret;
+       } else {
+               _LOGE("fota-info : unzip root path[%s] is success\n", buff);
        }
 
-       _LOGD("fota-info : unzip root path[%s] is success\n", buff);
-
-       /*unzip manifest from factoryrest data*/
+       /* unzip manifest from factoryrest data */
        memset(buff, '\0', BUF_SIZE);
-       snprintf(buff, BUF_SIZE, "opt/share/packages/%s.xml", pkgid);
+       snprintf(buff, BUF_SIZE, "opt/share/packages/%s.xml", pkgid); // relative path
 
        const char *xml_argv[] = { "/usr/bin/unzip", "-oX", OPT_ZIP_FILE, buff, "-d", "/", NULL };
        ret = _ri_xsystem(xml_argv);
        if (ret != 0) {
                _LOGE("fota-info : xml_argv fail for pkgid[%s] .\n", pkgid);
-               return ret;
+       } else {
+               _LOGE("fota-info : xml_argv[%s] is success\n", pkgid);
        }
 
-       _LOGD("fota-info : xml_argv[%s] is success\n", pkgid);
-
-       /*get updated manifest path*/
+       /* get updated manifest path */
        memset(buff, '\0', BUF_SIZE);
        snprintf(buff, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
        _LOGE("Manifest name is %s\n", buff);
 
-       /*apply smack for manifest*/
-       _ri_privilege_change_smack_label(buff, pkgid, 0);/*0 is SMACK_LABEL_ACCESS*/
+       /* apply smack for manifest */
+       ret = __get_smack_label_from_db(pkgid, &smack_label);
+       _LOGD("smack_label[%s], ret[%d]\n", smack_label, ret);
 
-       /*register manifest*/
-       char* fota_tags[3] = {NULL, };
+       _ri_privilege_change_smack_label(buff, smack_label, 0); /* 0 is SMACK_LABEL_ACCESS */
+
+       /* register manifest */
+       char *fota_tags[3] = { NULL, };
        fota_tags[0] = "removable=true";
        fota_tags[1] = "preload=true";
        fota_tags[2] = NULL;
@@ -2201,29 +2304,46 @@ static int __ri_install_fota_for_rw(char *pkgid)
                ret = -1;
                goto err;
        } else {
-               _LOGD("Parsing Manifest Success\n");
+               _LOGE("Parsing Manifest Success\n");
        }
 
-       /*Register cert info*/
-       _ri_register_cert(pkgid);
-
-       /*apply smack for pkg root path*/
        memset(buff, '\0', BUF_SIZE);
        snprintf(buff, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgid);
-       _ri_privilege_setup_path(pkgid, buff, PERM_APP_PATH_ANY_LABEL, pkgid);
-
-       /*apply smack for defined directory*/
-       __rpm_apply_smack(pkgid, home_dir);
+       ret = _ri_verify_signatures(buff, pkgid, false);
+       if (ret == 0) {
+               _LOGE("fota-info : certificate register(%s).", pkgid);
+               _ri_register_cert(pkgid);
+       }
 
-       /*apply privilege*/
-       ret = _ri_apply_privilege(pkgid, 0);
+       __ri_make_directory(pkgid);
+       ret = _coretpk_installer_apply_smack(pkgid, 0);
        if (ret != 0) {
-               _LOGE("apply perm failed with err(%d)\n", ret);
+               _LOGE("failed to apply the smack.");
+       }
+
+       snprintf(signature_file, BUF_SIZE, "%s/%s", buff, SIGNATURE1_XML);
+       if (access(signature_file, F_OK) != 0){
+               _LOGE("[%s] is not present", signature_file);
+       } else {
+               ret = _ri_get_visibility_from_signature_file(signature_file, &visibility, false);
+               if (ret != 0) {
+                       _LOGE("Couldnt get visiblity [%d], ret: %d", visibility, ret);
+                       ret = RPM_INSTALLER_ERR_INTERNAL;
+                       goto err;
+               } else {
+                       _LOGE("visibility : %d", visibility);
+               }
+       }
+
+       ret = _ri_apply_privilege(pkgid, visibility, smack_label);
+       if (ret < 0) {
+               _LOGE("fota-info : _ri_apply_privilege fail[pkgid=%s]\n", pkgid);
        } else {
-               _LOGD("apply perm success\n");
+               _LOGE("fota-info : pkgid[%s] apply smack success\n", pkgid);
        }
 
 err:
+       FREE_AND_NULL(smack_label);
 
        return ret;
 }
@@ -2232,42 +2352,46 @@ static int __ri_upgrade_fota_for_rw(char *pkgid)
 {
        int ret = 0;
        int home_dir = 1;
-       char buff[BUF_SIZE] = {'\0'};
+       char buff[BUF_SIZE] = { '\0' };
+       int visibility = 0;
+       char signature_file[BUF_SIZE] = {0, };
+       char *smack_label = NULL;
 
        _LOGD("fota-info : pkgid[%s] start upgrade\n", pkgid);
 
-       /*unzip pkg dir from factoryrest data*/
-       snprintf(buff, BUF_SIZE, "opt/usr/apps/%s/*", pkgid);
+       /* unzip pkg dir from factoryrest data */
+       snprintf(buff, BUF_SIZE, "opt/usr/apps/%s/*", pkgid); // relative path
        const char *pkg_argv[] = { "/usr/bin/unzip", "-oX", OPT_ZIP_FILE, buff, "-d", "/", NULL };
        ret = _ri_xsystem(pkg_argv);
        if (ret != 0) {
                _LOGE("fota-info : unzip root path[%s] is fail .\n", buff);
-               return ret;
+       } else {
+               _LOGD("fota-info : unzip root path[%s] is success\n", buff);
        }
 
-       _LOGD("fota-info : unzip root path[%s] is success\n", buff);
-
-       /*unzip manifest from factoryrest data*/
+       /* unzip manifest from factoryrest data */
        memset(buff, '\0', BUF_SIZE);
-       snprintf(buff, BUF_SIZE, "opt/share/packages/%s.xml", pkgid);
+       snprintf(buff, BUF_SIZE, "opt/share/packages/%s.xml", pkgid); // relative path
        const char *xml_argv[] = { "/usr/bin/unzip", "-oX", OPT_ZIP_FILE, buff, "-d", "/", NULL };
        ret = _ri_xsystem(xml_argv);
        if (ret != 0) {
                _LOGE("fota-info : unzip manifest[%s] is fail .\n", buff);
-               return ret;
+       } else {
+               _LOGD("fota-info : unzip manifest[%s] is success\n", buff);
        }
 
-       _LOGD("fota-info : unzip manifest[%s] is success\n", buff);
-
-       /*get updated manifest path*/
+       /* get updated manifest path */
        memset(buff, '\0', BUF_SIZE);
        snprintf(buff, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
        _LOGE("Manifest name is %s\n", buff);
 
-       /*apply smack for manifest*/
-       _ri_privilege_change_smack_label(buff, pkgid, 0);/*0 is SMACK_LABEL_ACCESS*/
+       /* apply smack for manifest */
+       ret = __get_smack_label_from_db(pkgid, &smack_label);
+       _LOGD("smack_label[%s], ret[%d]\n", smack_label, ret);
+
+       _ri_privilege_change_smack_label(buff, smack_label, 0); /* 0 is SMACK_LABEL_ACCESS */
 
-       /*register manifest*/
+       /* register manifest */
        ret = pkgmgr_parser_parse_manifest_for_upgrade(buff, NULL);
        if (ret < 0) {
                _LOGE("Parsing Manifest Failed\n");
@@ -2277,62 +2401,87 @@ static int __ri_upgrade_fota_for_rw(char *pkgid)
                _LOGD("Parsing Manifest Success\n");
        }
 
-       /*Register new cert info*/
-       _ri_unregister_cert(pkgid);
-       _ri_register_cert(pkgid);
-
-       /*apply smack for pkg root path*/
        memset(buff, '\0', BUF_SIZE);
        snprintf(buff, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgid);
-       _ri_privilege_setup_path(pkgid, buff, PERM_APP_PATH_ANY_LABEL, pkgid);
-
-       /*apply smack for defined directory*/
-       __rpm_apply_smack(pkgid, home_dir);
+       ret = _ri_verify_signatures(buff, pkgid, false);
+       if (ret == 0) {
+               _LOGE("fota-info : certificate register(%s).", pkgid);
+               /* Register new cert info */
+               _ri_unregister_cert(pkgid);
+               _ri_register_cert(pkgid);
+       }
 
-       /*apply privilege*/
-       ret = _ri_apply_privilege(pkgid, 0);
+       __ri_make_directory(pkgid);
+       ret = _coretpk_installer_apply_smack(pkgid, 0);
        if (ret != 0) {
-               _LOGE("apply perm failed with err(%d)\n", ret);
+               _LOGE("failed to apply the smack.");
+       }
+
+       snprintf(signature_file, BUF_SIZE, "%s/%s", buff, SIGNATURE1_XML);
+       if (access(signature_file, F_OK) != 0){
+               _LOGE("[%s] is not present", signature_file);
+       } else {
+               ret = _ri_get_visibility_from_signature_file(signature_file, &visibility, false);
+               if (ret != 0) {
+                       _LOGE("Couldnt get visiblity [%d], ret: %d", visibility, ret);
+                       ret = RPM_INSTALLER_ERR_INTERNAL;
+                       goto err;
+               } else {
+                       _LOGE("visibility : %d", visibility);
+               }
+       }
+
+       ret = _ri_apply_privilege(pkgid, visibility, smack_label);
+       if (ret < 0) {
+               _LOGE("fota-info : _ri_apply_privilege fail[pkgid=%s]\n", pkgid);
        } else {
-               _LOGD("apply perm success\n");
+               _LOGE("fota-info : pkgid[%s] apply smack success\n", pkgid);
        }
 
 err:
+       FREE_AND_NULL(smack_label);
+
        return ret;
 }
 
 static int __ri_uninstall_fota_for_rw(char *pkgid)
 {
        int ret = 0;
-       char buff[BUF_SIZE] = {'\0'};
+       char buff[BUF_SIZE] = { '\0' };
+       char *smack_label = NULL;
 
        _LOGD("fota-info : pkgid[%s] start uninstall\n", pkgid);
 
-       /*del root path dir*/
+       ret = __get_smack_label_from_db(pkgid, &smack_label);
+       _LOGD("smack_label[%s], ret[%d]\n", smack_label, ret);
+
+       /* del root path dir */
        snprintf(buff, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgid);
 
        if (__is_dir(buff)) {
-               _rpm_delete_dir(buff);
+               _installer_util_delete_dir(buff);
        }
 
-       /*del manifest*/
+       /* del manifest */
        memset(buff, '\0', BUF_SIZE);
        snprintf(buff, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
        (void)remove(buff);
 
-       /*del db info*/
+       /* del db info */
        ret = pkgmgr_parser_parse_manifest_for_uninstallation(pkgid, NULL);
        if (ret < 0) {
                _LOGE("Parsing Manifest Failed\n");
        }
 
-       /*execute privilege APIs*/
-       _ri_privilege_revoke_permissions(pkgid);
-       _ri_privilege_unregister_package(pkgid);
+       /* execute privilege APIs */
+       _ri_privilege_revoke_permissions(smack_label);
+       _ri_privilege_unregister_package(smack_label);
 
-       /*Unregister cert info*/
+       /* Unregister cert info */
        _ri_unregister_cert(pkgid);
 
+       FREE_AND_NULL(smack_label);
+
        return 0;
 }
 
@@ -2352,6 +2501,9 @@ int __ri_check_running_app(const pkgmgrinfo_appinfo_h handle, void *user_data)
                return ret;
        }
 
+       if (user_data != NULL)
+               *(GList **)user_data = g_list_append(*(GList **)user_data, strdup(appid));
+
        ret = app_manager_is_running(appid, &isRunning);
        if (ret < 0) {
                _LOGE("Failed to execute app_manager_is_running[%d].\n", ret);
@@ -2401,14 +2553,14 @@ int __ri_check_running_app(const pkgmgrinfo_appinfo_h handle, void *user_data)
        return ret;
 }
 
-int __ri_change_dir(char *dirname)
+int __ri_change_dir(const char *dirname)
 {
        int ret = 0;
 
        ret = mkdir(dirname, 0644);
        if (ret < 0) {
                if (access(dirname, F_OK) == 0) {
-                       _rpm_delete_dir(dirname);
+                       _installer_util_delete_dir(dirname);
                        ret = mkdir(dirname, 0644);
                        if (ret < 0) {
                                _LOGE("mkdir(%s) failed\n", dirname);
@@ -2422,7 +2574,10 @@ int __ri_change_dir(char *dirname)
 
        ret = chdir(dirname);
        if (ret != 0) {
-               _LOGE("chdir(%s) failed [%s]\n", dirname, strerror(errno));
+               char buf[BUF_SIZE] = { 0, };
+               if( strerror_r(errno, buf, sizeof(buf)) == 0) {
+                       _LOGE("chdir(%s) failed [%s]\n", dirname, buf);
+               }
                return -1;
        }
        return 0;
@@ -2434,23 +2589,23 @@ int _ri_smack_reload(const char *pkgid, rpm_request_type request_type)
        char *op_type = NULL;
 
        switch (request_type) {
-               case INSTALL_REQ:
-                       op_type = strdup("install");
-                       break;
+       case INSTALL_REQ:
+               op_type = strdup("install");
+               break;
 
-               case UPGRADE_REQ:
-                       op_type = strdup("update");
-                       break;
+       case UPGRADE_REQ:
+               op_type = strdup("update");
+               break;
 
-               case UNINSTALL_REQ:
-                       op_type = strdup("uninstall");
-                       break;
+       case UNINSTALL_REQ:
+               op_type = strdup("uninstall");
+               break;
 
-               default:
-                       break;
+       default:
+               break;
        }
 
-       if(op_type == NULL) {
+       if (op_type == NULL) {
                _LOGE("@Failed to reload smack. request_type not matched[pkgid=%s, op=%s]", pkgid, op_type);
                return -1;
        }
@@ -2462,7 +2617,7 @@ int _ri_smack_reload(const char *pkgid, rpm_request_type request_type)
        } else {
                _LOGD("#success: smack reload[pkgid=%s, op=%s]", pkgid, op_type);
        }
-       if(op_type){
+       if (op_type) {
                free(op_type);
                op_type = NULL;
        }
@@ -2473,7 +2628,7 @@ int _ri_smack_reload_all(void)
 {
        int ret = 0;
 
-       const char *smack_argv[] = { "/usr/bin/smackload-fast", NULL};
+       const char *smack_argv[] = { "/usr/bin/smackload-fast", NULL };
        ret = _ri_xsystem(smack_argv);
        if (ret != 0) {
                _LOGE("@Failed to reload all smack : %d", errno);
@@ -2486,35 +2641,35 @@ int _ri_smack_reload_all(void)
 
 void __ri_remove_updated_dir(const char *pkgid)
 {
-       char path_buf[BUF_SIZE] = {'\0'};
+       char path_buf[BUF_SIZE] = { '\0' };
 
-       // check pkgid
+       /* check pkgid */
        if (pkgid == NULL) {
                _LOGE("pkgid is NULL.");
                return;
        }
 
-       // remove bin dir
+       /* remove bin dir */
        snprintf(path_buf, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, BIN_DIR_STR);
        if (__is_dir(path_buf)) {
                _LOGE("@pkgid[%s] need to clean dir[%s]\n", pkgid, path_buf);
-               _rpm_delete_dir(path_buf);
+               _installer_util_delete_dir(path_buf);
        }
 
-       // remove res dir
+       /* remove res dir */
        memset(path_buf, '\0', BUF_SIZE);
        snprintf(path_buf, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, RES_DIR_STR);
        if (__is_dir(path_buf)) {
                _LOGE("@pkgid[%s] need to clean dir[%s]\n", pkgid, path_buf);
-               _rpm_delete_dir(path_buf);
+               _installer_util_delete_dir(path_buf);
        }
 
-       // remove shared/res dir
+       /* remove shared/res dir */
        memset(path_buf, '\0', BUF_SIZE);
        snprintf(path_buf, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, SHARED_RES_DIR_STR);
        if (__is_dir(path_buf)) {
                _LOGE("@pkgid[%s] need to clean dir[%s]\n", pkgid, path_buf);
-               _rpm_delete_dir(path_buf);
+               _installer_util_delete_dir(path_buf);
        }
 }
 
@@ -2548,11 +2703,11 @@ static int __metadata_func(const char *key, const char *value, void *user_data)
                if (isRunning) {
                        _LOGE("consumer[%s] is already launched \n", (char *)user_data);
                } else {
-                       usleep(100 * 1000); /* 100ms sleep for infomation ready*/
+                       usleep(100 * 1000);     /* 100ms sleep for infomation ready */
                        ret = aul_launch_app((char *)user_data, NULL);
                        if (ret == AUL_R_ERROR) {
                                _LOGE("consumer[%s] launch fail, sleep and retry  launch_app\n", (char *)user_data);
-                               usleep(100 * 1000);     /* 100ms sleep for infomation ready*/
+                               usleep(100 * 1000);     /* 100ms sleep for infomation ready */
                                aul_launch_app((char *)user_data, NULL);
                        }
                        _LOGE("consumer[%s] is launched !!!! \n", (char *)user_data);
@@ -2610,82 +2765,13 @@ static void __ri_launch_consumer(const char *pkgid)
        pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
 }
 
-static int __ail_change_info(int op, const char *appid)
-{
-       void *lib_handle = NULL;
-       int (*ail_desktop_operation) (const char *);
-       char *aop = NULL;
-       int ret = 0;
-
-       if ((lib_handle = dlopen(LIBAIL_PATH, RTLD_LAZY)) == NULL) {
-               _LOGE("dlopen is failed LIBAIL_PATH[%s]\n", LIBAIL_PATH);
-               goto END;
-       }
-
-
-       switch (op) {
-               case 0:
-                       aop  = "ail_desktop_add";
-                       break;
-               case 1:
-                       aop  = "ail_desktop_update";
-                       break;
-               case 2:
-                       aop  = "ail_desktop_remove";
-                       break;
-               case 3:
-                       aop  = "ail_desktop_clean";
-                       break;
-               case 4:
-                       aop  = "ail_desktop_fota";
-                       break;
-               default:
-                       goto END;
-                       break;
-       }
-
-       if ((ail_desktop_operation =
-            dlsym(lib_handle, aop)) == NULL || dlerror() != NULL) {
-               _LOGE("can not find symbol \n");
-
-               goto END;
-       }
-
-       ret = ail_desktop_operation(appid);
-
-END:
-       if (lib_handle)
-               dlclose(lib_handle);
-
-       return ret;
-}
-
-static int __ri_update_ail_info(const pkgmgrinfo_appinfo_h handle, void *user_data)
-{
-       int ret = 0;
-       char *appid = NULL;
-
-       ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
-       if (ret < 0) {
-               _LOGE("Failed to execute pkgmgrinfo_appinfo_get_appid[%d].\n", ret);
-               return ret;
-       }
-
-       ret = __ail_change_info(AIL_INSTALL, appid);
-       if (ret < 0) {
-               _LOGE("Failed to execute __ail_change_info[%s].\n", appid);
-       }
-
-       return ret;
-}
-
-static int __child_list_cb (const pkgmgrinfo_pkginfo_h handle, void *user_data)
+static int __child_list_cb(const pkgmgrinfo_pkginfo_h handle, void *user_data)
 {
        int ret = 0;
        char *pkgid = NULL;
 
        ret = pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid);
-       if(ret < 0) {
+       if (ret < 0) {
                _LOGE("get_pkgid failed\n");
                return ret;
        }
@@ -2693,8 +2779,8 @@ static int __child_list_cb (const pkgmgrinfo_pkginfo_h handle, void *user_data)
        _LOGD("@child pkgid is [%s] for uninstallation", pkgid);
 
        ret = _rpm_uninstall_pkg_with_dbpath(pkgid, 0);
-       if(ret != 0) {
-               _LOGE("uninstall pkg(%s) failed\n",pkgid );
+       if (ret != 0) {
+               _LOGE("uninstall pkg(%s) failed\n", pkgid);
        }
 
        return ret;
@@ -2729,11 +2815,14 @@ end:
 int _rpm_install_pkg_with_dbpath(char *pkgfilepath, char *pkgid, char *clientid)
 {
        int ret = 0;
-       char manifest[BUF_SIZE] = { '\0'};
-       char resultxml[BUF_SIZE] = {'\0'};
-       char cwd[BUF_SIZE] = {'\0'};
+       char manifest[BUF_SIZE] = { '\0' };
+       char resultxml[BUF_SIZE] = { '\0' };
+       char resxml[BUF_SIZE] = { '\0' };
+       char cwd[BUF_SIZE] = { '\0' };
        int home_dir = 0;
        char *temp = NULL;
+       char buf[BUF_SIZE] = { 0, };
+
 #ifdef APP2EXT_ENABLE
        app2ext_handle *handle = NULL;
        GList *dir_list = NULL;
@@ -2741,20 +2830,22 @@ int _rpm_install_pkg_with_dbpath(char *pkgfilepath, char *pkgid, char *clientid)
        int size = -1;
        unsigned long rpm_size = 0;
 #endif
-       /*send event for start*/
-       _ri_broadcast_status_notification(pkgid, "rpm", "start", "install");
+       char *smack_label = NULL;
+
+       /* send event for start */
+       _ri_broadcast_status_notification(pkgid, PKGTYPE_RPM, "start", "install");
        _LOGD("[#]start : _rpm_install_pkg_with_dbpath");
 
-       /*getcwd*/
+       /* getcwd */
        temp = getcwd(cwd, BUF_SIZE);
-       if ((temp == NULL) ||(cwd[0] == '\0')) {
+       if ((temp == NULL) || (cwd[0] == '\0')) {
                _LOGE("@failed to get the current directory info.");
                ret = RPM_INSTALLER_ERR_INTERNAL;
                goto err;
        }
        _LOGD("#current working directory is %s", cwd);
 
-       /*change dir*/
+       /* change dir */
        ret = __ri_change_dir(TEMP_DIR);
        if (ret == -1) {
                _LOGE("@failed to change directory.");
@@ -2763,19 +2854,19 @@ int _rpm_install_pkg_with_dbpath(char *pkgfilepath, char *pkgid, char *clientid)
        }
        _LOGD("#switched to %s", TEMP_DIR);
 
-       /*run cpio script*/
+       /* run cpio script */
        const char *cpio_argv[] = { CPIO_SCRIPT, pkgfilepath, NULL };
        ret = _ri_xsystem(cpio_argv);
 
-       /*get manifext.xml path*/
-       snprintf(manifest, BUF_SIZE, "%s/opt/share/packages/%s.xml", TEMP_DIR, pkgid);
+       /* get manifext.xml path */
+       snprintf(manifest, BUF_SIZE, "%s%s/%s.xml", TEMP_DIR, OPT_SHARE_PACKAGES, pkgid);
        _LOGD("#manifest name is %s", manifest);
 
        if (access(manifest, F_OK)) {
                _LOGD("#there is no RW manifest.xml. check RO manifest.xml.");
 
                memset(manifest, '\0', sizeof(manifest));
-               snprintf(manifest, BUF_SIZE, "%s/usr/share/packages/%s.xml", TEMP_DIR, pkgid);
+               snprintf(manifest, BUF_SIZE, "%s%s/%s.xml", TEMP_DIR, USR_SHARE_PACKAGES, pkgid);
                _LOGD("#manifest name is %s", manifest);
 
                if (access(manifest, F_OK)) {
@@ -2786,10 +2877,11 @@ int _rpm_install_pkg_with_dbpath(char *pkgfilepath, char *pkgid, char *clientid)
                        home_dir = 0;
                }
 
-#if 0  //disable "copy ro-xml to rw-xml", because of some bug
+#if 0
+               /* disable "copy ro-xml to rw-xml", because of some bug */
                snprintf(srcpath, BUF_SIZE, "%s", manifest);
                memset(manifest, '\0', sizeof(manifest));
-               snprintf(manifest, BUF_SIZE, "%s/%s.xml", MANIFEST_RW_DIRECTORY, pkgid);
+               snprintf(manifest, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
 
                const char *xml_update_argv[] = { CPIO_SCRIPT_UPDATE_XML, srcpath, manifest, NULL };
                ret = _ri_xsystem(xml_update_argv);
@@ -2797,34 +2889,52 @@ int _rpm_install_pkg_with_dbpath(char *pkgfilepath, char *pkgid, char *clientid)
        } else {
                home_dir = 1;
        }
-       /*send event for install_percent*/
-       _ri_broadcast_status_notification(pkgid, "rpm", "install_percent", "30");
 
-       /*check manifest.xml validation*/
+       /* send event for install_percent */
+       _ri_broadcast_status_notification(pkgid, PKGTYPE_RPM, "install_percent", "30");
+
+       /* check manifest.xml validation */
        ret = pkgmgr_parser_check_manifest_validation(manifest);
-       if(ret < 0) {
+       if (ret < 0) {
                _LOGE("@invalid manifest");
                ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
                goto err;
        }
 
-       /*check for signature and certificate*/
-       ret = _ri_verify_signatures(TEMP_DIR, pkgid);
-    if (ret < 0) {
-       _LOGE("@signature and certificate failed(%s).", pkgid);
-               ret = RPM_INSTALLER_ERR_SIG_INVALID;
-               goto err;
-    }
-    _LOGD("#_ri_verify_signatures success.");
+       /* check existance of res.xml for resource manager */
+       snprintf(resxml, BUF_SIZE, "%s%s/%s/res/res.xml", TEMP_DIR, USR_APPS, pkgid);
+       _LOGD("#path of res.xml is %s", resxml);
+       if (access(resxml, F_OK) != 0) {
+               _LOGE("file not found. try other paths");
+               memset(resxml, '\0', sizeof(resxml));
+               snprintf(resxml, BUF_SIZE, "%s%s/%s/res/res.xml", TEMP_DIR, OPT_USR_APPS, pkgid);
+               if (access(resxml, F_OK) != 0) {
+                       _LOGE("file not found");
+                       memset(resxml, '\0', sizeof(resxml));
+               }
+       }
+
+       if (resxml[0] != '\0') {
+               if (access(resxml, R_OK) == 0) {
+                       /* validate it */
+                       ret = pkgmgr_resource_parser_check_xml_validation(resxml);
+                       if (ret < 0) {
+                               _LOGE("pkgmgr_resource_parser_check_xml_validation(%s) failed.", resxml);
+                               ret = RPM_INSTALLER_ERR_INTERNAL;
+                               goto err;
+                       }
+               }
+       }
 
-    /*chdir*/
+       /* chdir */
        ret = chdir(cwd);
        if (ret != 0) {
-               _LOGE("@failed to change directory(%s)(%s)", cwd, strerror(errno));
+               if( strerror_r(errno, buf, sizeof(buf)) == 0) {
+                       _LOGE("@failed to change directory(%s)(%s)", cwd, buf);
+               }
                ret = RPM_INSTALLER_ERR_INTERNAL;
                goto err;
        }
-
 #ifdef APP2EXT_ENABLE
        ret = __get_location_from_xml(manifest, &location);
        if (ret < 0) {
@@ -2835,31 +2945,29 @@ int _rpm_install_pkg_with_dbpath(char *pkgfilepath, char *pkgid, char *clientid)
                if (location == PMINFO_INSTALL_LOCATION_PREFER_EXTERNAL) {
                        _LOGD("#Install: external storage location");
 
-                       /*Get the rpm's size from rpm header*/
+                       /* Get the rpm's size from rpm header */
                        rpm_size = _ri_calculate_rpm_size(pkgfilepath);
-                       if(rpm_size != 0){
-                               rpm_size = rpm_size/(1024*1024);      //rpm size in MB
-                               _LOGD("#Rpm file(%s) size is %lu MB",pkgfilepath,rpm_size);
+                       if (rpm_size != 0) {
+                               rpm_size = rpm_size / (1024 * 1024);    /* rpm size in MB */
+                               _LOGD("#Rpm file(%s) size is %lu MB", pkgfilepath, rpm_size);
 
-                               /*Add margin to the rpm size*/
+                               /* Add margin to the rpm size */
                                rpm_size = rpm_size + RPM_SIZE_MARGIN(rpm_size);
-                               _LOGD("#Rpm file (%s) size after margin is %lu MB",pkgfilepath,rpm_size);
-                       }else{
+                               _LOGD("#Rpm file (%s) size after margin is %lu MB", pkgfilepath, rpm_size);
+                       } else {
                                _LOGE("@Failed to get size from rpm header\n");
                                ret = RPM_INSTALLER_ERR_INTERNAL;
                                goto err;
                        }
 
-                       /*
-                       Get the size from the manifest file.
-                       */
+                       /* Get the size from the manifest file. */
                        ret = __get_size_from_xml(manifest, &size);
                        if (ret != PMINFO_R_OK) {
                                size = rpm_size;
-                               _LOGD(" #rpm size is %d MB",size);
-                       }else{
-                               size = size>rpm_size?size:rpm_size;
-                               _LOGD("#rpm size is %d MB",size);
+                               _LOGD(" #rpm size is %d MB", size);
+                       } else {
+                               size = size > rpm_size ? size : rpm_size;
+                               _LOGD("#rpm size is %d MB", size);
                        }
                }
        }
@@ -2871,7 +2979,8 @@ int _rpm_install_pkg_with_dbpath(char *pkgfilepath, char *pkgid, char *clientid)
                        ret = RPM_INSTALLER_ERR_INTERNAL;
                        goto err;
                }
-               if ((&(handle->interface) != NULL) && (handle->interface.pre_install != NULL) && (handle->interface.post_install != NULL)){
+               if ((&(handle->interface) != NULL) && (handle->interface.pre_install != NULL)
+                       && (handle->interface.post_install != NULL)) {
                        dir_list = __rpm_populate_dir_list();
                        if (dir_list == NULL) {
                                _LOGE("@ \nError in populating the directory list\n");
@@ -2882,10 +2991,9 @@ int _rpm_install_pkg_with_dbpath(char *pkgfilepath, char *pkgid, char *clientid)
                        ret = handle->interface.pre_install(gpkgname, dir_list, size);
                        if (ret == APP2EXT_ERROR_MMC_STATUS) {
                                _LOGE("@app2xt MMC is not here, go internal\n");
-                       } else if (ret == APP2EXT_SUCCESS){
+                       } else if (ret == APP2EXT_SUCCESS) {
                                _LOGE("@pre_install done, go internal\n");
-                       }
-                       else {
+                       } else {
                                _LOGE("@app2xt pre install API failed (%d)\n", ret);
                                __rpm_clear_dir_list(dir_list);
                                handle->interface.post_install(gpkgname, APP2EXT_STATUS_FAILED);
@@ -2897,10 +3005,10 @@ int _rpm_install_pkg_with_dbpath(char *pkgfilepath, char *pkgid, char *clientid)
        }
 #endif
 
-
-       /*run script*/
+       /* run script */
        if (home_dir == 0) {
-#if 0  //disable "INSTALL_SCRIPT_WITH_DBPATH_RO", because of some bug
+#if 0
+               /* disable "INSTALL_SCRIPT_WITH_DBPATH_RO", because of some bug */
                const char *argv[] = { INSTALL_SCRIPT_WITH_DBPATH_RO, pkgfilepath, NULL };
                ret = _ri_xsystem(argv);
 #endif
@@ -2913,7 +3021,7 @@ int _rpm_install_pkg_with_dbpath(char *pkgfilepath, char *pkgid, char *clientid)
        if (ret != 0) {
                _LOGE("@failed to install the pkg(%d).", ret);
 #ifdef APP2EXT_ENABLE
-               if ((handle != NULL) && (handle->interface.post_install != NULL)){
+               if ((handle != NULL) && (handle->interface.post_install != NULL)) {
                        __rpm_clear_dir_list(dir_list);
                        handle->interface.post_install(gpkgname, APP2EXT_STATUS_FAILED);
                        app2ext_deinit(handle);
@@ -2923,7 +3031,16 @@ int _rpm_install_pkg_with_dbpath(char *pkgfilepath, char *pkgid, char *clientid)
        }
        _LOGD("#install success.");
 
-       /*write the storeclient-id to manifest.xml*/
+       /* check for signature and certificate */
+       ret = _ri_verify_signatures(TEMP_DIR, pkgid, true);
+       if (ret < 0) {
+               _LOGE("@signature and certificate failed(%s).", pkgid);
+               ret = RPM_INSTALLER_ERR_SIG_INVALID;
+               goto err;
+       }
+       _LOGD("#_ri_verify_signatures success.");
+
+       /* write the storeclient-id to manifest.xml */
        if (clientid != NULL) {
                if (home_dir == 0) {
                        snprintf(resultxml, BUF_SIZE, "%s/%s.xml", USR_SHARE_PACKAGES, pkgid);
@@ -2941,18 +3058,11 @@ int _rpm_install_pkg_with_dbpath(char *pkgfilepath, char *pkgid, char *clientid)
                _LOGD("#client id[%s], input manifest:[%s], dest manifest:[%s]", clientid, manifest, resultxml);
        }
 
-       /*send event for install_percent*/
-       _ri_broadcast_status_notification(pkgid, "rpm", "install_percent", "60");
+       /* send event for install_percent */
+       _ri_broadcast_status_notification(pkgid, PKGTYPE_RPM, "install_percent", "60");
 
-#ifdef APP2EXT_ENABLE
-       if ((handle != NULL) && (handle->interface.post_install != NULL)){
-               __rpm_clear_dir_list(dir_list);
-               handle->interface.post_install(gpkgname, APP2EXT_STATUS_SUCCESS);
-               app2ext_deinit(handle);
-       }
-#endif
-
-       /*Parse the manifest to get install location and size. If installation fails, remove manifest info from DB*/
+       /* Parse the manifest to get install location and size.
+          If installation fails, remove manifest info from DB */
        if (clientid != NULL) {
                ret = pkgmgr_parser_parse_manifest_for_installation(resultxml, NULL);
        } else {
@@ -2965,43 +3075,55 @@ int _rpm_install_pkg_with_dbpath(char *pkgfilepath, char *pkgid, char *clientid)
        }
        _LOGD("#manifest parsing success");
 
-       /*register cert info*/
+#ifdef APP2EXT_ENABLE
+       if ((handle != NULL) && (handle->interface.post_install != NULL)) {
+               __rpm_clear_dir_list(dir_list);
+               handle->interface.post_install(gpkgname, APP2EXT_STATUS_SUCCESS);
+               app2ext_deinit(handle);
+       }
+#endif
+       /* register cert info */
        _ri_register_cert(pkgid);
 
-       /*search_ug_app*/
+       /* search_ug_app */
        _coretpk_installer_search_ui_gadget(pkgid);
 
-       /*apply smack to shared dir*/
-       __rpm_apply_smack(pkgid, 1);
+       /* apply smack to shared dir */
+       ret = __get_smack_label_from_db(pkgid, &smack_label);
+       _LOGD("smack_label[%s], ret[%d]\n", smack_label, ret);
+
+       __rpm_apply_smack(pkgid, 1, smack_label);
 
-       /*apply smack by privilege*/
-       ret = _ri_apply_privilege(pkgid, 0);
+       /* apply smack by privilege */
+       ret = _ri_apply_privilege(pkgid, 0, smack_label);
        if (ret != 0) {
                _LOGE("@failed to apply permission(%d).", ret);
        }
        _LOGD("#permission applying success.");
 
-       /*reload smack*/
+       /* reload smack */
        ret = _ri_smack_reload_all();
        if (ret != 0) {
                _LOGD("@failed to reload_all the smack.");
        }
 
-       /*send event for install_percent*/
-       _ri_broadcast_status_notification(pkgid, "rpm", "install_percent", "100");
+       /* send event for install_percent */
+       _ri_broadcast_status_notification(pkgid, PKGTYPE_RPM, "install_percent", "100");
+
+       ret = RPM_INSTALLER_SUCCESS;
 
 err:
-       _rpm_delete_dir(TEMP_DIR);
-       _rpm_delete_dir(TEMP_DBPATH);
+       _installer_util_delete_dir(TEMP_DIR);
+       _installer_util_delete_dir(TEMP_DBPATH);
 
-        if (ret == RPM_INSTALLER_SUCCESS) {
-                _LOGD("[#]end : _rpm_install_pkg_with_dbpath");
-                __ri_launch_consumer(pkgid);
-               _ri_broadcast_status_notification(pkgid, "rpm", "end", "ok");
+       if (ret == RPM_INSTALLER_SUCCESS) {
+               _LOGD("[#]end : _rpm_install_pkg_with_dbpath");
+               __ri_launch_consumer(pkgid);
+               _ri_broadcast_status_notification(pkgid, PKGTYPE_RPM, "end", "ok");
                _ri_stat_cb(pkgid, "end", "ok");
        } else {
                _LOGE("[@]end : _rpm_install_pkg_with_dbpath");
-               /*remove db info*/
+               /* remove db info */
                ret = _coretpk_installer_remove_db_info(pkgid);
                if (ret < 0) {
                        _LOGE("_coretpk_installer_remove_db_info is failed.");
@@ -3009,22 +3131,24 @@ err:
 
                char *errstr = NULL;
                _ri_error_no_to_string(ret, &errstr);
-               _ri_broadcast_status_notification(pkgid, "rpm", "error", errstr);
+               _ri_broadcast_status_notification(pkgid, PKGTYPE_RPM, "error", errstr);
                _ri_stat_cb(pkgid, "error", errstr);
-               _ri_broadcast_status_notification(pkgid, "rpm", "end", "fail");
+               _ri_broadcast_status_notification(pkgid, PKGTYPE_RPM, "end", "fail");
                sleep(2);
                _ri_stat_cb(pkgid, "end", "fail");
                _LOGE("install failed with err(%d) (%s)\n", ret, errstr);
        }
 
+       FREE_AND_NULL(smack_label);
+
        return ret;
 }
 
 int _rpm_upgrade_pkg_with_dbpath(char *pkgfilepath, char *pkgid)
 {
        int ret = 0;
-       char manifest[BUF_SIZE] = { '\0'};
-       char cwd[BUF_SIZE] = {'\0'};
+       char manifest[BUF_SIZE] = { '\0' };
+       char cwd[BUF_SIZE] = { '\0' };
        int home_dir = 0;
        pkgmgrinfo_pkginfo_h pkghandle;
        char *temp = NULL;
@@ -3035,10 +3159,14 @@ int _rpm_upgrade_pkg_with_dbpath(char *pkgfilepath, char *pkgid)
        int size = -1;
        unsigned long rpm_size = 0;
 #endif
-       _ri_broadcast_status_notification(pkgid, "rpm", "start", "update");
+       char *smack_label = NULL;
+       char resxml[BUF_SIZE] = { '\0' };
+       char buf[BUF_SIZE] = { 0, };
+
+       _ri_broadcast_status_notification(pkgid, PKGTYPE_RPM, "start", "update");
        _LOGD("[#]start : _rpm_upgrade_pkg_with_dbpath");
 
-       /*terminate running app*/
+       /* terminate running app */
        ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkghandle);
        if (ret < 0) {
                _LOGE("@failed to get pkginfo handle");
@@ -3048,16 +3176,16 @@ int _rpm_upgrade_pkg_with_dbpath(char *pkgfilepath, char *pkgid)
        pkgmgrinfo_appinfo_get_list(pkghandle, PM_UI_APP, __ri_check_running_app, NULL);
        pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
 
-       /*getcwd*/
+       /* getcwd */
        temp = getcwd(cwd, BUF_SIZE);
-       if ((temp == NULL) ||(cwd[0] == '\0')) {
+       if ((temp == NULL) || (cwd[0] == '\0')) {
                _LOGE("@getcwd() failed.");
                ret = RPM_INSTALLER_ERR_INTERNAL;
                goto err;
        }
        _LOGD("#current working directory is %s.", cwd);
 
-       /*change dir*/
+       /* change dir */
        ret = __ri_change_dir(TEMP_DIR);
        if (ret == -1) {
                _LOGE("@change dir failed.");
@@ -3066,19 +3194,19 @@ int _rpm_upgrade_pkg_with_dbpath(char *pkgfilepath, char *pkgid)
        }
        _LOGD("#switched to %s", TEMP_DIR);
 
-       /*run cpio script*/
+       /* run cpio script */
        const char *cpio_argv[] = { CPIO_SCRIPT, pkgfilepath, NULL };
        ret = _ri_xsystem(cpio_argv);
 
-       /*get manifext.xml path*/
-       snprintf(manifest, BUF_SIZE, "%s/opt/share/packages/%s.xml", TEMP_DIR, pkgid);
+       /* get manifext.xml path */
+       snprintf(manifest, BUF_SIZE, "%s%s/%s.xml", TEMP_DIR, OPT_SHARE_PACKAGES, pkgid);
        _LOGD("#manifest name is %s.", manifest);
 
        if (access(manifest, F_OK)) {
                _LOGD("#there is no RW manifest.xml. check RO manifest.xml.");
 
                memset(manifest, '\0', sizeof(manifest));
-               snprintf(manifest, BUF_SIZE, "%s/usr/share/packages/%s.xml", TEMP_DIR, pkgid);
+               snprintf(manifest, BUF_SIZE, "%s%s/%s.xml", TEMP_DIR, USR_SHARE_PACKAGES, pkgid);
                _LOGD("#manifest name is %s.", manifest);
 
                if (access(manifest, F_OK)) {
@@ -3089,10 +3217,11 @@ int _rpm_upgrade_pkg_with_dbpath(char *pkgfilepath, char *pkgid)
                        home_dir = 0;
                }
 
-#if 0  //disable "copy ro-xml to rw-xml", because of some bug
+#if 0
+               /* disable "copy ro-xml to rw-xml", because of some bug */
                snprintf(srcpath, BUF_SIZE, "%s", manifest);
                memset(manifest, '\0', sizeof(manifest));
-               snprintf(manifest, BUF_SIZE, "%s/%s.xml", MANIFEST_RW_DIRECTORY, pkgid);
+               snprintf(manifest, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
 
                const char *xml_update_argv[] = { CPIO_SCRIPT_UPDATE_XML, srcpath, manifest, NULL };
                ret = _ri_xsystem(xml_update_argv);
@@ -3101,122 +3230,152 @@ int _rpm_upgrade_pkg_with_dbpath(char *pkgfilepath, char *pkgid)
                home_dir = 1;
        }
 
-       /*send event for install_percent*/
-       _ri_broadcast_status_notification(pkgid, "rpm", "install_percent", "30");
+       /* check existance of res.xml for resource manager */
+       snprintf(resxml, BUF_SIZE, "%s%s/%s/res/res.xml", TEMP_DIR, USR_APPS, pkgid);
+       _LOGD("#path of res.xml is %s", resxml);
+       if (access(resxml, F_OK) != 0) {
+               _LOGE("file not found. try other paths");
+               memset(resxml, '\0', sizeof(resxml));
+               snprintf(resxml, BUF_SIZE, "%s%s/%s/res/res.xml", TEMP_DIR, OPT_USR_APPS, pkgid);
+               if (access(resxml, F_OK) != 0) {
+                       _LOGE("file not found");
+                       memset(resxml, '\0', sizeof(resxml));
+               }
+       }
+
+       if (resxml[0] != '\0') {
+               if (access(resxml, R_OK) == 0) {
+                       /* validate it */
+                       ret = pkgmgr_resource_parser_check_xml_validation(resxml);
+                       if (ret < 0) {
+                               _LOGE("pkgmgr_resource_parser_check_xml_validation(%s) failed.", resxml);
+                               ret = RPM_INSTALLER_ERR_INTERNAL;
+                               goto err;
+                       }
+               }
+       }
+
+       /* send event for install_percent */
+       _ri_broadcast_status_notification(pkgid, PKGTYPE_RPM, "install_percent", "30");
 
-       /*check manifest.xml validation*/
+       /* check manifest.xml validation */
        ret = pkgmgr_parser_check_manifest_validation(manifest);
-       if(ret < 0) {
+       if (ret < 0) {
                _LOGE("@invalid manifest");
                ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
                goto err;
        }
 
-       /*check for signature and certificate*/
-       ret = _ri_verify_signatures(TEMP_DIR, pkgid);
-    if (ret < 0) {
+       /* check for signature and certificate */
+       ret = _ri_verify_signatures(TEMP_DIR, pkgid, true);
+       if (ret < 0) {
                _LOGE("@signature and certificate failed(%s).", pkgid);
                ret = RPM_INSTALLER_ERR_SIG_INVALID;
                goto err;
-    }
-    _LOGD("#_ri_verify_signatures success.");
+       }
+       _LOGD("#_ri_verify_signatures success.");
 
-    /*chdir*/
+       /* chdir */
        ret = chdir(cwd);
        if (ret != 0) {
-               _LOGE("@chdir(%s) failed(%s).", cwd, strerror(errno));
+               if( strerror_r(errno, buf, sizeof(buf)) == 0) {
+                       _LOGE("@chdir(%s) failed(%s).", cwd, buf);
+               }
                ret = RPM_INSTALLER_ERR_INTERNAL;
                goto err;
        }
 
-    /*remove dir for clean*/
+       /* remove dir for clean */
        __ri_remove_updated_dir(pkgid);
 
        _LOGD("#Preserve the smack file");
-       /*Preserve the smack rule file */
-       ret = __ri_copy_smack_rule_file(UPGRADE_REQ,pkgid,0);
-       if(ret != RPM_INSTALLER_SUCCESS)
+       /* Preserve the smack rule file */
+       ret = __ri_copy_smack_rule_file(UPGRADE_REQ, pkgid, 0);
+       if (ret != RPM_INSTALLER_SUCCESS)
                goto err;
+
 #ifdef APP2EXT_ENABLE
        ret = pkgmgrinfo_pkginfo_get_pkginfo(gpkgname, &pkghandle);
        if (ret < 0) {
                _LOGE("Failed to get pkginfo handle\n");
                ret = RPM_INSTALLER_ERR_PKG_NOT_FOUND;
                goto err;
-       } else {
-               ret = pkgmgrinfo_pkginfo_get_installed_storage(pkghandle, &location);
-               if (ret < 0) {
-                       _LOGE("Failed to get install location\n");
-                       pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
+       }
+
+       ret = pkgmgrinfo_pkginfo_get_installed_storage(pkghandle, &location);
+       if (ret < 0) {
+               _LOGE("Failed to get install location\n");
+               pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
+               ret = RPM_INSTALLER_ERR_INTERNAL;
+               goto err;
+       }
+
+       if (location == PMINFO_EXTERNAL_STORAGE) {
+               /* Get the rpm's size from rpm header */
+               rpm_size = _ri_calculate_rpm_size(pkgfilepath);
+               if (rpm_size == 0) {
+                       _LOGE("@Failed to get size from rpm header\n");
                        ret = RPM_INSTALLER_ERR_INTERNAL;
                        goto err;
+               }
+               rpm_size = rpm_size / (1024 * 1024);    /* rpm size in MB */
+               _LOGD("#Rpm file(%s) size is %lu MB", pkgfilepath, rpm_size);
+
+               /* Add margin to the rpm size */
+               rpm_size = rpm_size + RPM_SIZE_MARGIN(rpm_size);
+               _LOGD("#Rpm file (%s) size after margin is %lu MB", pkgfilepath, rpm_size);
+
+               /* Get the size from the manifest file. */
+               ret = __get_size_from_xml(manifest, &size);
+               if (ret != PMINFO_R_OK) {
+                       size = rpm_size;
+                       _LOGD(" #rpm size is %d", size);
                } else {
-                       if (location == PMINFO_EXTERNAL_STORAGE) {
-                               /*Get the rpm's size from rpm header*/
-                               rpm_size = _ri_calculate_rpm_size(pkgfilepath);
-                               if(rpm_size != 0){
-                                       rpm_size = rpm_size/(1024*1024);      //rpm size in MB
-                                       _LOGD("#Rpm file(%s) size is %lu MB",pkgfilepath,rpm_size);
-
-                                       /*Add margin to the rpm size*/
-                                       rpm_size = rpm_size + RPM_SIZE_MARGIN(rpm_size);
-                                       _LOGD("#Rpm file (%s) size after margin is %lu MB",pkgfilepath,rpm_size);
-                               }else{
-                                       _LOGE("@Failed to get size from rpm header\n");
-                                       ret = RPM_INSTALLER_ERR_INTERNAL;
-                                       goto err;
-                               }
+                       size = size > rpm_size ? size : rpm_size;
+                       _LOGD("#rpm size is %d", size);
+               }
+       }
 
-                               /*
-                               Get the size from the manifest file.
-                               */
-                               ret = __get_size_from_xml(manifest, &size);
-                               if (ret != PMINFO_R_OK) {
-                                       size = rpm_size;
-                                       _LOGD(" #rpm size is %d",size);
-                               }else{
-                                       size = size>rpm_size?size:rpm_size;
-                                       _LOGD("#rpm size is %d",size);
-                               }
-                       }
+       pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
+
+       if ((location == PMINFO_EXTERNAL_STORAGE) && size > 0) {
+               handle = app2ext_init(APP2EXT_SD_CARD);
+               if (handle == NULL) {
+                       _LOGE("app2ext init failed\n");
+                       ret = RPM_INSTALLER_ERR_INTERNAL;
+                       goto err;
                }
-               pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
-               if ((location == PMINFO_EXTERNAL_STORAGE) && size > 0) {
-                       handle = app2ext_init(APP2EXT_SD_CARD);
-                       if (handle == NULL) {
-                               _LOGE("app2ext init failed\n");
-                               ret = RPM_INSTALLER_ERR_INTERNAL;
+
+               if ((&(handle->interface) != NULL) && (handle->interface.pre_upgrade != NULL)
+                       && (handle->interface.post_upgrade != NULL)) {
+                       dir_list = __rpm_populate_dir_list();
+                       if (dir_list == NULL) {
+                               _LOGE("\nError in populating the directory list\n");
+                               ret = RPM_INSTALLER_ERR_RPM_SCRIPT_WRONG_ARGS;
+                               app2ext_deinit(handle);
                                goto err;
                        }
-                       if ((&(handle->interface) != NULL) && (handle->interface.pre_upgrade != NULL) && (handle->interface.post_upgrade != NULL)){
-                               dir_list = __rpm_populate_dir_list();
-                               if (dir_list == NULL) {
-                                       _LOGE("\nError in populating the directory list\n");
-                                       ret = RPM_INSTALLER_ERR_RPM_SCRIPT_WRONG_ARGS;
-                                       app2ext_deinit(handle);
-                                       goto err;
-                               }
-                               ret = handle->interface.pre_upgrade(gpkgname, dir_list, size);
-                               if (ret == APP2EXT_ERROR_MMC_STATUS ) {
-                                       _LOGE("app2xt MMC is not here, go internal (%d)\n", ret);
-                               }else if(ret == APP2EXT_SUCCESS){
-                                       _LOGE("pre upgrade done, go internal");
-                               }else {
-                                       _LOGE("app2xt pre upgrade API failed (%d)\n", ret);
-                                       __rpm_clear_dir_list(dir_list);
-                                       handle->interface.post_upgrade(gpkgname, APP2EXT_STATUS_FAILED);
-                                       ret = RPM_INSTALLER_ERR_INTERNAL;
-                                       app2ext_deinit(handle);
-                                       goto err;
-                               }
+                       ret = handle->interface.pre_upgrade(gpkgname, dir_list, size);
+                       if (ret == APP2EXT_ERROR_MMC_STATUS) {
+                               _LOGE("app2xt MMC is not here, go internal (%d)\n", ret);
+                       } else if (ret == APP2EXT_SUCCESS) {
+                               _LOGE("pre upgrade done, go internal");
+                       } else {
+                               _LOGE("app2xt pre upgrade API failed (%d)\n", ret);
+                               __rpm_clear_dir_list(dir_list);
+                               handle->interface.post_upgrade(gpkgname, APP2EXT_STATUS_FAILED);
+                               ret = RPM_INSTALLER_ERR_INTERNAL;
+                               app2ext_deinit(handle);
+                               goto err;
                        }
                }
        }
 #endif
 
-       /*run script*/
+       /* run script */
        if (home_dir == 0) {
-#if 0  //disable "UPGRADE_SCRIPT_WITH_DBPATH_RO", because of some bug
+#if 0
+               /* disable "UPGRADE_SCRIPT_WITH_DBPATH_RO", because of some bug */
                const char *argv[] = { UPGRADE_SCRIPT_WITH_DBPATH_RO, pkgfilepath, NULL };
                ret = _ri_xsystem(argv);
 #endif
@@ -3229,7 +3388,7 @@ int _rpm_upgrade_pkg_with_dbpath(char *pkgfilepath, char *pkgid)
        if (ret != 0) {
                _LOGE("@upgrade complete with error(%d)", ret);
 #ifdef APP2EXT_ENABLE
-               if ((handle != NULL) && (handle->interface.post_upgrade != NULL)){
+               if ((handle != NULL) && (handle->interface.post_upgrade != NULL)) {
                        __rpm_clear_dir_list(dir_list);
                        handle->interface.post_upgrade(gpkgname, APP2EXT_STATUS_FAILED);
                        app2ext_deinit(handle);
@@ -3239,10 +3398,11 @@ int _rpm_upgrade_pkg_with_dbpath(char *pkgfilepath, char *pkgid)
        }
        _LOGD("#upgrade script success.");
 
-       /*send event for install_percent*/
-       _ri_broadcast_status_notification(pkgid, "rpm", "install_percent", "60");
+       /* send event for install_percent */
+       _ri_broadcast_status_notification(pkgid, PKGTYPE_RPM, "install_percent", "60");
 
-       /*Parse the manifest to get install location and size. If fails, remove manifest info from DB.*/
+       /* Parse the manifest to get install location and size.
+          If fails, remove manifest info from DB. */
        ret = pkgmgr_parser_parse_manifest_for_upgrade(manifest, NULL);
        if (ret < 0) {
                _LOGE("@parsing manifest failed.");
@@ -3251,63 +3411,70 @@ int _rpm_upgrade_pkg_with_dbpath(char *pkgfilepath, char *pkgid)
        }
        _LOGD("#parsing manifest success.");
 
-       /*unregister cert info*/
+       /* unregister cert info */
        _ri_unregister_cert(pkgid);
 
-       /*register cert info*/
+       /* register cert info */
        _ri_register_cert(pkgid);
 
 #ifdef APP2EXT_ENABLE
-       if ((handle != NULL) && (handle->interface.post_upgrade != NULL)){
+       if ((handle != NULL) && (handle->interface.post_upgrade != NULL)) {
                __rpm_clear_dir_list(dir_list);
                handle->interface.post_upgrade(gpkgname, APP2EXT_STATUS_SUCCESS);
                app2ext_deinit(handle);
        }
 #endif
 
-       /*search_ug_app*/
+       /* search_ug_app */
        _coretpk_installer_search_ui_gadget(pkgid);
 
-       /*apply smack to shared dir*/
-       __rpm_apply_smack(pkgid, 1);
+       /* apply smack to shared dir */
+       ret = __get_smack_label_from_db(pkgid, &smack_label);
+       _LOGD("smack_label[%s], ret[%d]\n", smack_label, ret);
+
+       __rpm_apply_smack(pkgid, 1, smack_label);
 
-       /*apply smack by privilege*/
-       ret = _ri_apply_privilege(pkgid, 0);
+       /* apply smack by privilege */
+       ret = _ri_apply_privilege(pkgid, 0, smack_label);
        if (ret != 0) {
                _LOGE("@apply perm failed with err(%d)", ret);
        }
        _LOGD("#apply perm success.");
 
-       /*reload smack*/
+       /* reload smack */
        ret = _ri_smack_reload_all();
        if (ret != 0) {
                _LOGD("_ri_smack_reload_all failed.");
        }
 
-       /*send event for install_percent*/
-       _ri_broadcast_status_notification(pkgid, "rpm", "install_percent", "100");
+       /* send event for install_percent */
+       _ri_broadcast_status_notification(pkgid, PKGTYPE_RPM, "install_percent", "100");
+
+       ret = RPM_INSTALLER_SUCCESS;
 
 err:
-       _rpm_delete_dir(TEMP_DIR);
-       _rpm_delete_dir(TEMP_DBPATH);
+       _installer_util_delete_dir(TEMP_DIR);
+       _installer_util_delete_dir(TEMP_DBPATH);
 
-        if (ret == RPM_INSTALLER_SUCCESS) {
-                _LOGD("[#]end : _rpm_upgrade_pkg_with_dbpath");
-                __ri_launch_consumer(pkgid);
-               _ri_broadcast_status_notification(pkgid, "rpm", "end", "ok");
+       if (ret == RPM_INSTALLER_SUCCESS) {
+               _LOGD("[#]end : _rpm_upgrade_pkg_with_dbpath");
+               __ri_launch_consumer(pkgid);
+               _ri_broadcast_status_notification(pkgid, PKGTYPE_RPM, "end", "ok");
                _ri_stat_cb(pkgid, "end", "ok");
        } else {
-                _LOGE("[@]end : _rpm_upgrade_pkg_with_dbpath");
+               _LOGE("[@]end : _rpm_upgrade_pkg_with_dbpath");
                char *errstr = NULL;
                _ri_error_no_to_string(ret, &errstr);
-               _ri_broadcast_status_notification(pkgid, "rpm", "error", errstr);
+               _ri_broadcast_status_notification(pkgid, PKGTYPE_RPM, "error", errstr);
                _ri_stat_cb(pkgid, "error", errstr);
-               _ri_broadcast_status_notification(pkgid, "rpm", "end", "fail");
+               _ri_broadcast_status_notification(pkgid, PKGTYPE_RPM, "end", "fail");
                sleep(2);
                _ri_stat_cb(pkgid, "end", "fail");
                _LOGE("install failed with err(%d) (%s)\n", ret, errstr);
        }
 
+       FREE_AND_NULL(smack_label);
+
        return ret;
 }
 
@@ -3315,45 +3482,59 @@ int _rpm_uninstall_pkg_with_dbpath(const char *pkgid, bool is_system)
 {
        if (pkgid == NULL) {
                _LOGE("pkgid is NULL.");
-               return -1;
+               return RPM_INSTALLER_ERR_WRONG_PARAM;
        }
 
        int ret = 0;
-       char buff[BUF_SIZE] = {'\0'};
-       char tizen_manifest[BUF_SIZE] = {'\0'};
+       int tmp = 0;
+       char buff[BUF_SIZE] = { '\0' };
+       char extpath[BUF_SIZE] = { '\0' };
+       char tizen_manifest[BUF_SIZE] = { '\0' };
        pkgmgrinfo_pkginfo_h pkghandle = NULL;
        bool mother_package = false;
        bool coretpk = false;
+       GList *appid_list = NULL;
 
-       #ifdef APP2EXT_ENABLE
+#ifdef APP2EXT_ENABLE
        app2ext_handle *handle = NULL;
        pkgmgrinfo_installed_storage location = 1;
-       #endif
+#endif
+       char *smack_label = NULL;
 
        _LOGD("pkgid=[%s], is_system=[%d]", pkgid, is_system);
 
        snprintf(tizen_manifest, BUF_SIZE, "%s/%s/tizen-manifest.xml", OPT_USR_APPS, pkgid);
        if (access(tizen_manifest, R_OK) == 0) {
                coretpk = true;
-               _LOGD("[%s] is existed.", tizen_manifest);
+               _LOGD("[%s] is existing.", tizen_manifest);
        }
 
-       // send start event
-       if (is_system)
-               _ri_broadcast_status_notification(pkgid, coretpk ? "coretpk" : "rpm", "start", "update");
-       else
-               _ri_broadcast_status_notification(pkgid, coretpk ? "coretpk" : "rpm", "start", "uninstall");
-
-       // terminate running app
+       /* send start event */
+       if (is_system) {
+               _ri_broadcast_status_notification(pkgid, coretpk ? PKGTYPE_TPK : PKGTYPE_RPM, "start", "update");
+       } else {
+               _ri_broadcast_status_notification(pkgid, coretpk ? PKGTYPE_TPK : PKGTYPE_RPM, "start", "uninstall");
+       }
+
+       /* terminate running app */
        ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkghandle);
        if (ret < 0) {
                _LOGE("pkgmgrinfo_pkginfo_get_pkginfo(%s) failed.", pkgid);
                ret = RPM_INSTALLER_ERR_PKG_NOT_FOUND;
                goto end;
        }
-       pkgmgrinfo_appinfo_get_list(pkghandle, PM_UI_APP, __ri_check_running_app, NULL);
+       pkgmgrinfo_appinfo_get_list(pkghandle, PM_UI_APP, __ri_check_running_app, &appid_list);
+
+       while (appid_list != NULL) {
+               _ri_broadcast_app_uninstall_notification(pkgid, coretpk ? PKGTYPE_TPK : PKGTYPE_RPM, (char *)appid_list->data);
+
+               if (appid_list->next == NULL)
+                       break;
+               else
+                       appid_list = g_list_next(appid_list);
+       }
 
-       // If package is mother package, then uninstall child package
+       /* If package is mother package, then uninstall child package */
        pkgmgrinfo_pkginfo_is_mother_package(pkghandle, &mother_package);
        if (mother_package == true) {
                _LOGD("[%s] is mother package", pkgid);
@@ -3361,37 +3542,32 @@ int _rpm_uninstall_pkg_with_dbpath(const char *pkgid, bool is_system)
        }
        pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
 
-       snprintf(tizen_manifest, BUF_SIZE, "%s/%s/tizen-manifest.xml", OPT_USR_APPS, pkgid);
-       if (access(tizen_manifest, R_OK) == 0) {
-               coretpk = true;
-               _LOGD("[%s] is existing.", tizen_manifest);
-       }
+       ret = __get_smack_label_from_db(pkgid, &smack_label);
+       _LOGD("smack_label[%s], ret[%d]\n", smack_label, ret);
 
-       // del root path dir
+       /* del root path dir */
        snprintf(buff, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgid);
        if (__is_dir(buff)) {
-               _rpm_delete_dir(buff);
+               _installer_util_delete_dir(buff);
        }
 
-       // del root path dir for ext
-       if (_coretpk_installer_get_configuration_value(INI_VALUE_MAKE_EXT_DIRECTORY)) {
-               char extpath[BUF_SIZE] = {'\0'};
-               snprintf(extpath, BUF_SIZE, "%s/%s", OPT_STORAGE_SDCARD_APP_ROOT, pkgid);
-               if (__is_dir(extpath)) {
-                       _rpm_delete_dir(extpath);
-               }
+       /* del root path dir for ext */
+       snprintf(extpath, BUF_SIZE, "%s/%s", OPT_STORAGE_SDCARD_APP_ROOT, pkgid);
+       if (__is_dir(extpath)) {
+               _installer_util_delete_dir(extpath);
        }
 
-       // del manifest
+       /* del manifest */
        memset(buff, '\0', BUF_SIZE);
        snprintf(buff, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
        (void)remove(buff);
 
-       // check system pkg, if pkg is system pkg, need to update xml on USR_SHARE_PACKAGES
+       /* check system pkg, if pkg is system pkg, need to update xml on USR_SHARE_PACKAGES */
        if (is_system) {
                memset(buff, '\0', BUF_SIZE);
                snprintf(buff, BUF_SIZE, "%s/%s.xml", USR_SHARE_PACKAGES, pkgid);
-               _LOGE("manifest for upgrade, path=[%s]",buff);
+               _LOGE("manifest for upgrade, path=[%s]", buff);
+
                ret = pkgmgr_parser_parse_manifest_for_upgrade(buff, NULL);
                if (ret < 0) {
                        _LOGE("parsing manifest failed.");
@@ -3409,76 +3585,96 @@ int _rpm_uninstall_pkg_with_dbpath(const char *pkgid, bool is_system)
                if (ret < 0) {
                        _LOGE("failed to get install location\n");
                        pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
-                       return RPM_INSTALLER_ERR_INTERNAL;
+                       ret = RPM_INSTALLER_ERR_INTERNAL;
+                       goto end;
                }
-
                pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
+
                if (location == PMINFO_EXTERNAL_STORAGE) {
                        handle = app2ext_init(APP2EXT_SD_CARD);
                        if (handle == NULL) {
                                _LOGE("app2ext init failed\n");
-                               return RPM_INSTALLER_ERR_INTERNAL;
+                               ret = RPM_INSTALLER_ERR_INTERNAL;
+                               goto end;
                        }
-                       if ((&(handle->interface) != NULL) && (handle->interface.pre_uninstall != NULL) && (handle->interface.post_uninstall != NULL) &&
-                                       (handle->interface.disable != NULL)){
+                       if ((&(handle->interface) != NULL) && (handle->interface.pre_uninstall != NULL) &&
+                               (handle->interface.post_uninstall != NULL) &&
+                               (handle->interface.disable != NULL)) {
                                ret = handle->interface.disable(pkgid);
                                if (ret != APP2EXT_SUCCESS) {
                                        _LOGE("Unmount ret[%d]", ret);
                                }
                                ret = app2ext_get_app_location(pkgid);
-                               if (ret == APP2EXT_INTERNAL_MEM){
+                               if (ret == APP2EXT_INTERNAL_MEM) {
                                        _LOGE("app2xt APP is not in MMC, go internal (%d)\n", ret);
-                               }
-                               else {
+                               } else {
                                        ret = handle->interface.pre_uninstall(pkgid);
-                                       if (ret == APP2EXT_ERROR_MMC_STATUS ) {
+                                       if (ret == APP2EXT_ERROR_MMC_STATUS) {
                                                _LOGE("app2xt MMC is not here, go internal (%d)\n", ret);
-                                       }else if (ret == APP2EXT_SUCCESS){
+                                       } else if (ret == APP2EXT_SUCCESS) {
                                                _LOGE("pre uninstall done, go to internal");
-                                       }else {
+                                       } else {
                                                _LOGE("app2xt pre uninstall API failed (%d)\n", ret);
                                                handle->interface.post_uninstall(pkgid);
                                                app2ext_deinit(handle);
-                                               return RPM_INSTALLER_ERR_INTERNAL;
+                                               ret = RPM_INSTALLER_ERR_INTERNAL;
+                                               goto end;
                                        }
                                }
                        }
                }
 #endif
 
-               // del db info
+               /* del db info */
                ret = pkgmgr_parser_parse_manifest_for_uninstallation(pkgid, NULL);
                if (ret < 0) {
                        _LOGE("pkgmgr_parser_parse_manifest_for_uninstallation() failed, pkgid=[%s]", pkgid);
                }
 
 #ifdef APP2EXT_ENABLE
-               if ((handle != NULL) && (handle->interface.post_uninstall != NULL)){
+               if ((handle != NULL) && (handle->interface.post_uninstall != NULL)) {
                        handle->interface.post_uninstall(pkgid);
                        app2ext_deinit(handle);
                }
 #endif
-
        }
 
-       // execute privilege APIs
-       _ri_privilege_revoke_permissions(pkgid);
-       _ri_privilege_unregister_package(pkgid);
+       /* execute privilege APIs */
+       _ri_privilege_revoke_permissions(smack_label);
+       _ri_privilege_unregister_package(smack_label);
 
-       // Unregister cert info
+       /* Unregister cert info */
        _ri_unregister_cert(pkgid);
 
+       ret = RPM_INSTALLER_SUCCESS;
+
 end:
-       // Restore the old smack file
+       /* Free appid list */
+       appid_list = g_list_first(appid_list);
+       while (appid_list != NULL) {
+               if (appid_list->data != NULL) {
+                       free(appid_list->data);
+               }
+
+               if (appid_list->next == NULL)
+                       break;
+               else
+                       appid_list = g_list_next(appid_list);
+       }
+       g_list_free(appid_list);
+
+       /* Restore the old smack file */
        if (coretpk == false) {
-               ret = __ri_copy_smack_rule_file(UNINSTALL_REQ,pkgid,is_system);
-               if (ret != RPM_INSTALLER_SUCCESS){
+               tmp = __ri_copy_smack_rule_file(UNINSTALL_REQ, pkgid, is_system);
+               if (tmp != RPM_INSTALLER_SUCCESS) {
                        _LOGD("smack restore failed");
-               }else{
-                       /*reload smack*/
-                       ret = _ri_smack_reload_all();
-                       if (ret != 0) {
+                       ret = tmp;
+               } else {
+                       /*reload smack */
+                       tmp = _ri_smack_reload_all();
+                       if (tmp != 0) {
                                _LOGD("_ri_smack_reload_all failed.");
+                               ret = tmp;
                        }
                }
        }
@@ -3487,34 +3683,39 @@ end:
                _LOGE("failed, ret=[%d]", ret);
                char *errstr = NULL;
                _ri_error_no_to_string(ret, &errstr);
-               _ri_broadcast_status_notification(pkgid, coretpk ? "coretpk" : "rpm", "error",  errstr);
+               _ri_broadcast_status_notification(pkgid, coretpk ? PKGTYPE_TPK : PKGTYPE_RPM, "error", errstr);
                _ri_stat_cb(pkgid, "error", errstr);
                sleep(2);
-               _ri_broadcast_status_notification(pkgid, coretpk ? "coretpk" : "rpm", "end",  "fail");
+               _ri_broadcast_status_notification(pkgid, coretpk ? PKGTYPE_TPK : PKGTYPE_RPM, "end", "fail");
                _ri_stat_cb(pkgid, "end", "fail");
                _LOGE("remove failed with err(%d) (%s)\n", ret, errstr);
        } else {
                _LOGE("success");
-               _ri_broadcast_status_notification(pkgid, coretpk ? "coretpk" : "rpm", "end", "ok");
+               _ri_broadcast_status_notification(pkgid, coretpk ? PKGTYPE_TPK : PKGTYPE_RPM, "end", "ok");
                _ri_stat_cb(pkgid, "end", "ok");
        }
 
+       FREE_AND_NULL(smack_label);
+
        return ret;
 }
 
 int _rpm_uninstall_pkg(char *pkgid)
 {
        int ret = 0;
-       int err = 0;
        bool is_update = 0;
        bool is_system = 0;
        bool is_removable = 0;
-       char buff[BUF_SIZE] = {'\0'};
        pkgmgrinfo_install_location location = 1;
 #ifdef APP2EXT_ENABLE
        app2ext_handle *handle = NULL;
 #endif
+#ifdef PRE_CHECK_FOR_MANIFEST
        char *manifest = NULL;
+       int err = 0;
+#endif
+       char *smack_label = NULL;
+
        pkgmgrinfo_pkginfo_h pkghandle;
        const char *argv[] = { UNINSTALL_SCRIPT, pkgid, NULL };
 
@@ -3525,7 +3726,7 @@ int _rpm_uninstall_pkg(char *pkgid)
                _LOGE("Failed to get pkginfo handle\n");
                return RPM_INSTALLER_ERR_PKG_NOT_FOUND;
        }
-       /*terminate running app*/
+       /* terminate running app */
        pkgmgrinfo_appinfo_get_list(pkghandle, PM_UI_APP, __ri_check_running_app, NULL);
 
        ret = pkgmgrinfo_pkginfo_is_system(pkghandle, &is_system);
@@ -3541,8 +3742,8 @@ int _rpm_uninstall_pkg(char *pkgid)
                }
                if (is_update) {
                        pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
-                       /*updated and system pkg need to "remove-update"*/
-                       _LOGD("Remove Update[%s]",pkgid);
+                       /* updated and system pkg need to "remove-update" */
+                       _LOGD("Remove Update[%s]", pkgid);
                        ret = _rpm_uninstall_pkg_with_dbpath(pkgid, 1);
                        if (ret < 0) {
                                _LOGE("uninstall_pkg_with_dbpath for system, is_update fail\n");
@@ -3552,8 +3753,8 @@ int _rpm_uninstall_pkg(char *pkgid)
        } else {
                pkgmgrinfo_pkginfo_is_removable(pkghandle, &is_removable);
                if (is_removable) {
-                       /*non-system and can be removable,  it should be deleted*/
-                       _LOGD("Delete Package [%s]",pkgid);
+                       /* non-system and can be removable,  it should be deleted */
+                       _LOGD("Delete Package [%s]", pkgid);
                        pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
                        ret = _rpm_uninstall_pkg_with_dbpath(pkgid, 0);
                        if (ret < 0) {
@@ -3563,7 +3764,7 @@ int _rpm_uninstall_pkg(char *pkgid)
                }
        }
 
-       _ri_broadcast_status_notification(pkgid, "rpm", "start", "uninstall");
+       _ri_broadcast_status_notification(pkgid, PKGTYPE_RPM, "start", "uninstall");
 
 #ifdef APP2EXT_ENABLE
        ret = pkgmgrinfo_pkginfo_get_install_location(pkghandle, &location);
@@ -3582,17 +3783,16 @@ int _rpm_uninstall_pkg(char *pkgid)
                        _LOGE("app2ext init failed\n");
                        return RPM_INSTALLER_ERR_INTERNAL;
                }
-               if ((&(handle->interface) != NULL) && (handle->interface.pre_uninstall != NULL) && (handle->interface.post_uninstall != NULL)){
+               if ((&(handle->interface) != NULL) && (handle->interface.pre_uninstall != NULL) &&
+                       (handle->interface.post_uninstall != NULL)) {
                        ret = app2ext_get_app_location(pkgid);
-                       if (ret == APP2EXT_INTERNAL_MEM){
-                                       _LOGE("app2xt APP is not in MMC, go internal (%d)\n", ret);
-                       }
-                       else {
+                       if (ret == APP2EXT_INTERNAL_MEM) {
+                               _LOGE("app2xt APP is not in MMC, go internal (%d)\n", ret);
+                       } else {
                                ret = handle->interface.pre_uninstall(pkgid);
-                               if (ret == APP2EXT_ERROR_MMC_STATUS || ret == APP2EXT_SUCCESS ) {
+                               if (ret == APP2EXT_ERROR_MMC_STATUS || ret == APP2EXT_SUCCESS) {
                                        _LOGE("app2xt MMC is not here, go internal (%d)\n", ret);
-                               }
-                               else {
+                               } else {
                                        _LOGE("app2xt pre uninstall API failed (%d)\n", ret);
                                        handle->interface.post_uninstall(pkgid);
                                        app2ext_deinit(handle);
@@ -3603,13 +3803,17 @@ int _rpm_uninstall_pkg(char *pkgid)
        }
 #endif
 
+       ret = __get_smack_label_from_db(pkgid, &smack_label);
+       _LOGD("smack_label[%s], ret[%d]\n", smack_label, ret);
+
 #ifdef PRE_CHECK_FOR_MANIFEST
-       /*Manifest info should be removed first because after installation manifest
-       file is uninstalled. If uninstallation fails, we need to re-insert manifest info for consistency*/
+       /* Manifest info should be removed first because after installation manifest
+          file is uninstalled. If uninstallation fails, we need to re-insert manifest info for consistency */
        manifest = pkgmgr_parser_get_manifest_file(pkgid);
        if (manifest == NULL) {
                _LOGE("manifest name is NULL\n");
                app2ext_deinit(handle);
+               FREE_AND_NULL(smack_label);
                return RPM_INSTALLER_ERR_NO_MANIFEST;
        }
        _LOGD("manifest name is %s\n", manifest);
@@ -3617,90 +3821,76 @@ int _rpm_uninstall_pkg(char *pkgid)
        if (ret < 0) {
                _LOGE("pkgmgr_parser_parse_manifest_for_uninstallation failed.\n");
        }
-
 #endif
 
        ret = _rpm_xsystem(argv);
        if (ret != 0) {
                _LOGE("uninstall failed with error(%d)\n", ret);
-               #ifdef PRE_CHECK_FOR_MANIFEST
+#ifdef PRE_CHECK_FOR_MANIFEST
                err = pkgmgr_parser_parse_manifest_for_installation(manifest, NULL);
                if (err < 0) {
                        _LOGE("Parsing Manifest Failed\n");
                }
-               if (manifest) {
-                       free(manifest);
-                       manifest = NULL;
-               }
-               #endif
-               #ifdef APP2EXT_ENABLE
-               if ((handle != NULL) && (handle->interface.post_uninstall != NULL)){
+
+               FREE_AND_NULL(manifest);
+#endif
+#ifdef APP2EXT_ENABLE
+               if ((handle != NULL) && (handle->interface.post_uninstall != NULL)) {
                        handle->interface.post_uninstall(pkgid);
                        app2ext_deinit(handle);
                }
-               #endif
+#endif
+
+               FREE_AND_NULL(smack_label);
+
                return ret;
        }
-
 #ifdef APP2EXT_ENABLE
-       if ((handle != NULL) && (handle->interface.post_uninstall != NULL)){
+       if ((handle != NULL) && (handle->interface.post_uninstall != NULL)) {
                handle->interface.post_uninstall(pkgid);
                app2ext_deinit(handle);
        }
 #endif
 
-#ifdef PRE_CHECK_FOR_MANIFEST
-       if (manifest) {
-               free(manifest);
-               manifest = NULL;
-       }
-#endif
-       /* Uninstallation Success. Remove the installation time key from vconf*/
-       snprintf(buff, BUF_SIZE, "db/app-info/%s/installed-time", pkgid);
-       err = vconf_unset(buff);
-       if (err) {
-               _LOGE("unset installation time failed\n");
-       }
-       /*execute privilege APIs*/
-       _ri_privilege_revoke_permissions(pkgid);
-       _ri_privilege_unregister_package(pkgid);
-       /*Unregister cert info*/
+       /* execute privilege APIs */
+       _ri_privilege_revoke_permissions(smack_label);
+       _ri_privilege_unregister_package(smack_label);
+
+       /* Unregister cert info */
        _ri_unregister_cert(gpkgname);
 
+       FREE_AND_NULL(manifest);
+       FREE_AND_NULL(smack_label);
+
        _LOGD("end : _rpm_uninstall_pkg(%d)\n", ret);
+
        return ret;
 }
 
-int _rpm_install_corexml(char *pkgfilepath, char *pkgid)
+int _rpm_install_corexml(const char *pkgfilepath, char *pkgid)
 {
        int ret = 0;
-       /*validate signature and certifictae*/
-       ret = _ri_verify_signatures(USR_APPS, pkgid);
-    if (ret < 0) {
-               _LOGE("_ri_verify_signatures Failed : %s\n", pkgid);
-               ret = RPM_INSTALLER_ERR_SIG_INVALID;
-               goto err;
-    }
 
-       /* check : given pkgid is deactivation*/
-       ret = __ri_check_pkgid_for_deactivation(pkgid);
+       /* validate signature and certifictae */
+       ret = _ri_verify_signatures(USR_APPS, pkgid, true);
        if (ret < 0) {
-               _LOGE("pkgid[%s] for deactivation dont need to install.\n", pkgid);
+               _LOGE("_ri_verify_signatures Failed : %s\n", pkgid);
+               ret = RPM_INSTALLER_ERR_SIG_INVALID;
                goto err;
        }
 
-       /* Parse and insert manifest in DB*/
-    ret = pkgmgr_parser_parse_manifest_for_installation(pkgfilepath, NULL);
-    if (ret < 0) {
+       /* Parse and insert manifest in DB */
+       ret = pkgmgr_parser_parse_manifest_for_installation(pkgfilepath, NULL);
+       if (ret < 0) {
                _LOGD("Installing Manifest Failed : %s\n", pkgfilepath);
                ret = RPM_INSTALLER_ERR_PACKAGE_NOT_INSTALLED;
                goto err;
-    }
+       }
 
-    // _ri_register_cert has __ri_free_cert_chain.
-    _ri_register_cert(pkgid);
+       /* _ri_register_cert has __ri_free_cert_chain. */
+       _ri_register_cert(pkgid);
 
-       /*search_ug_app*/
+       /* search_ug_app */
        _coretpk_installer_search_ui_gadget(pkgid);
 
        ret = RPM_INSTALLER_SUCCESS;
@@ -3711,7 +3901,6 @@ err:
        }
 
        return ret;
-
 }
 
 int _rpm_move_pkg(char *pkgid, int move_type)
@@ -3729,17 +3918,18 @@ int _rpm_move_pkg(char *pkgid, int move_type)
        else
                return RPM_INSTALLER_ERR_WRONG_PARAM;
 
-       ret = pkgmgrinfo_pkginfo_get_pkginfo(gpkgname,&pkghandle);
-       if(ret < 0){
+       ret = pkgmgrinfo_pkginfo_get_pkginfo(gpkgname, &pkghandle);
+       if (ret < 0) {
                _LOGE("@failed to get the pkginfo handle!!");
                ret = RPM_INSTALLER_ERR_PKG_NOT_FOUND;
                return ret;
        }
+
        /* Terminate the running instance of app */
-       pkgmgrinfo_appinfo_get_list(pkghandle,PM_UI_APP,__ri_check_running_app,NULL);
+       pkgmgrinfo_appinfo_get_list(pkghandle, PM_UI_APP, __ri_check_running_app, NULL);
        pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
        hdl = app2ext_init(APP2EXT_SD_CARD);
-       if ((hdl != NULL) && (hdl->interface.move != NULL)){
+       if ((hdl != NULL) && (hdl->interface.move != NULL)) {
                dir_list = __rpm_populate_dir_list();
                if (dir_list == NULL) {
                        _LOGE("\nError in populating the directory list\n");
@@ -3750,20 +3940,6 @@ int _rpm_move_pkg(char *pkgid, int move_type)
                if (ret != 0) {
                        _LOGE("Failed to move app\n");
                        return RPM_INSTALLER_ERR_INTERNAL;
-               }else{
-                       if(move_type == PM_MOVE_TO_INTERNAL){
-                               _LOGD("#updating the installed storage from external to internal");
-                               ret = pkgmgrinfo_pkginfo_set_installed_storage(pkgid, INSTALL_INTERNAL);
-                       }
-                       else{
-                               _LOGD("#updating the installed storage from internal to external");
-                               ret = pkgmgrinfo_pkginfo_set_installed_storage(pkgid, INSTALL_EXTERNAL);
-                       }
-
-                       if(ret != PMINFO_R_OK){
-                               _LOGE("@failed to udpate the installed storage");
-                               return RPM_INSTALLER_ERR_INTERNAL;
-                       }
                }
                app2ext_deinit(hdl);
                return RPM_INSTALLER_SUCCESS;
@@ -3781,58 +3957,58 @@ int _rpm_process_cscxml(char *csc_script)
        char *path_str = NULL;
        char *op_str = NULL;
        char *remove_str = NULL;
-       char csc_str[BUF_SIZE] = {'\0'};
+       char csc_str[BUF_SIZE] = { '\0' };
        snprintf(csc_str, BUF_SIZE - 1, "%s:", csc_script);
 
-       /*get params from csc script*/
-       path_str = __ri_get_str(csc_str, TOKEN_PATH_STR);
-       op_str = __ri_get_str(csc_str, TOKEN_OPERATION_STR);
-       remove_str = __ri_get_str(csc_str, TOKEN_REMOVE_STR);
-       if((path_str == NULL) || (op_str == NULL) || (remove_str == NULL)){
+       /* get params from csc script */
+       path_str = _installer_util_get_str(csc_str, TOKEN_PATH_STR);
+       op_str = _installer_util_get_str(csc_str, TOKEN_OPERATION_STR);
+       remove_str = _installer_util_get_str(csc_str, TOKEN_REMOVE_STR);
+       if ((path_str == NULL) || (op_str == NULL) || (remove_str == NULL)) {
                _LOGE("csc-info : input param is null[%s, %s, %s]\n", path_str, op_str, remove_str);
                goto end;
        }
        _LOGD("csc-info : path=%s, op=%s, remove=%s\n", path_str, op_str, remove_str);
 
-       /*get operation type*/
+       /* get operation type */
        op_type = __ri_get_op_type(op_str);
-       if(op_type < 0){
+       if (op_type < 0) {
                _LOGE("csc-info : operation error[%s, %s]\n", path_str, op_str);
                goto end;
        }
 
        switch (op_type) {
-               case INSTALL_REQ:
-                       ret = __ri_install_csc(path_str, remove_str);
-                       break;
+       case INSTALL_REQ:
+               ret = __ri_install_csc(path_str, remove_str);
+               break;
 
-               case UPGRADE_REQ:
-                       ret = __ri_install_csc(path_str, remove_str);
-                       break;
+       case UPGRADE_REQ:
+               ret = __ri_install_csc(path_str, remove_str);
+               break;
 
-               case UNINSTALL_REQ:
-                       ret = __ri_uninstall_csc(path_str);
-                       break;
+       case UNINSTALL_REQ:
+               ret = __ri_uninstall_csc(path_str);
+               break;
 
-               default:
-                       break;
+       default:
+               break;
        }
 
        if (ret < 0)
-               _LOGE("fota-info : Fota fail [pkgid=%s, operation=%d]\n",path_str, op_type);
+               _LOGE("fota-info : Fota fail [pkgid=%s, operation=%d]\n", path_str, op_type);
 
 end:
-       if(path_str)
+       if (path_str)
                free(path_str);
-       if(op_str)
+       if (op_str)
                free(op_str);
-       if(remove_str)
+       if (remove_str)
                free(remove_str);
 
        return ret;
 }
 
-int _rpm_process_csc_coretpk(char *csc_script)
+int _rpm_process_csc_coretpk(const char *csc_script)
 {
        int ret = 0;
        int op_type = 0;
@@ -3840,52 +4016,52 @@ int _rpm_process_csc_coretpk(char *csc_script)
        char *path_str = NULL;
        char *op_str = NULL;
        char *remove_str = NULL;
-       char csc_str[BUF_SIZE] = {'\0'};
+       char csc_str[BUF_SIZE] = { '\0' };
        snprintf(csc_str, BUF_SIZE - 1, "%s:", csc_script);
 
-       /*get params from csc script*/
-       path_str = __ri_get_str(csc_str, TOKEN_PATH_STR);
-       op_str = __ri_get_str(csc_str, TOKEN_OPERATION_STR);
-       remove_str = __ri_get_str(csc_str, TOKEN_REMOVE_STR);
-       if((path_str == NULL) || (op_str == NULL) || (remove_str == NULL)){
+       /* get params from csc script */
+       path_str = _installer_util_get_str(csc_str, TOKEN_PATH_STR);
+       op_str = _installer_util_get_str(csc_str, TOKEN_OPERATION_STR);
+       remove_str = _installer_util_get_str(csc_str, TOKEN_REMOVE_STR);
+       if ((path_str == NULL) || (op_str == NULL) || (remove_str == NULL)) {
                _LOGE("csc-info : input param is null[%s, %s, %s]\n", path_str, op_str, remove_str);
                goto end;
        }
        _LOGD("csc-info : path=%s, op=%s, remove=%s\n", path_str, op_str, remove_str);
 
-       /*get operation type*/
+       /* get operation type */
        op_type = __ri_get_op_type(op_str);
-       if(op_type < 0){
+       if (op_type < 0) {
                _LOGE("csc-info : operation error[%s, %s]\n", path_str, op_str);
                goto end;
        }
 
        switch (op_type) {
-               case INSTALL_REQ:
-                       ret = _coretpk_installer_csc_install(path_str, remove_str);
-                       break;
+       case INSTALL_REQ:
+               ret = _coretpk_installer_csc_install(path_str, remove_str, csc_script);
+               break;
 
-               case UPGRADE_REQ:
-                       ret = _coretpk_installer_csc_install(path_str, remove_str);
-                       break;
+       case UPGRADE_REQ:
+               ret = _coretpk_installer_csc_upgrade(path_str, remove_str, csc_script);
+               break;
 
-               case UNINSTALL_REQ:
-                       ret = __ri_uninstall_csc(path_str);
-                       break;
+       case UNINSTALL_REQ:
+               ret = _coretpk_installer_csc_uninstall(path_str);
+               break;
 
-               default:
-                       break;
+       default:
+               break;
        }
 
        if (ret < 0)
-               _LOGE("csc-info : csc fail [pkgid=%s, operation=%d]\n",path_str, op_type);
+               _LOGE("csc-info : csc fail [pkgid=%s, operation=%d]\n", path_str, op_type);
 
 end:
-       if(path_str)
+       if (path_str)
                free(path_str);
-       if(op_str)
+       if (op_str)
                free(op_str);
-       if(remove_str)
+       if (remove_str)
                free(remove_str);
 
        return ret;
@@ -3898,49 +4074,49 @@ int _rpm_process_fota(char *fota_script)
        char *pkgid = NULL;
        char *op_str = NULL;
 
-       char csc_str[BUF_SIZE] = {'\0'};
+       char csc_str[BUF_SIZE] = { '\0' };
        snprintf(csc_str, BUF_SIZE - 1, "%s:", fota_script);
 
-       /*get params from fota script*/
-       pkgid = __ri_get_str(csc_str, TOKEN_PATH_STR);
-       op_str = __ri_get_str(csc_str, TOKEN_OPERATION_STR);
-       if((pkgid == NULL) || (op_str == NULL)){
+       /* get params from fota script */
+       pkgid = _installer_util_get_str(csc_str, TOKEN_PATH_STR);
+       op_str = _installer_util_get_str(csc_str, TOKEN_OPERATION_STR);
+       if ((pkgid == NULL) || (op_str == NULL)) {
                _LOGE("fota-info : input param is null[%s, %s]\n", pkgid, op_str);
                goto end;
        }
        _LOGD("fota-info : path=%s, op=%s\n", pkgid, op_str);
 
-       /*get operation type*/
+       /* get operation type */
        op_type = __ri_get_op_type(op_str);
-       if(op_type < 0){
+       if (op_type < 0) {
                _LOGE("fota-info : operation error[%s, %s]\n", pkgid, op_str);
                goto end;
        }
 
        switch (op_type) {
-               case INSTALL_REQ:
-                       ret = __ri_install_fota(pkgid);
-                       break;
+       case INSTALL_REQ:
+               ret = __ri_install_fota(pkgid);
+               break;
 
-               case UPGRADE_REQ:
-                       ret = __ri_upgrade_fota(pkgid);
-                       break;
+       case UPGRADE_REQ:
+               ret = __ri_upgrade_fota(pkgid);
+               break;
 
-               case UNINSTALL_REQ:
-                       ret = __ri_uninstall_fota(pkgid);
-                       break;
+       case UNINSTALL_REQ:
+               ret = __ri_uninstall_fota(pkgid);
+               break;
 
-               default:
-                       break;
+       default:
+               break;
        }
 
        if (ret < 0)
-               _LOGE("fota-info : Fota fail [pkgid=%s, operation=%d]\n",pkgid, op_type);
+               _LOGE("fota-info : Fota fail [pkgid=%s, operation=%d]\n", pkgid, op_type);
 
 end:
-       if(pkgid)
+       if (pkgid)
                free(pkgid);
-       if(op_str)
+       if (op_str)
                free(op_str);
 
        return ret;
@@ -3953,49 +4129,49 @@ int _rpm_process_fota_for_rw(char *fota_script)
        char *pkgid = NULL;
        char *op_str = NULL;
 
-       char fota_str[BUF_SIZE] = {'\0'};
+       char fota_str[BUF_SIZE] = { '\0' };
        snprintf(fota_str, BUF_SIZE - 1, "%s:", fota_script);
 
-       /*get params from fota script*/
-       pkgid = __ri_get_str(fota_str, TOKEN_PATH_STR);
-       op_str = __ri_get_str(fota_str, TOKEN_OPERATION_STR);
-       if((pkgid == NULL) || (op_str == NULL)){
+       /* get params from fota script */
+       pkgid = _installer_util_get_str(fota_str, TOKEN_PATH_STR);
+       op_str = _installer_util_get_str(fota_str, TOKEN_OPERATION_STR);
+       if ((pkgid == NULL) || (op_str == NULL)) {
                _LOGE("fota-info : input param is null[%s, %s]\n", pkgid, op_str);
                goto end;
        }
        _LOGD("fota-info : path=%s, op=%s\n", pkgid, op_str);
 
-       /*get operation type*/
+       /* get operation type */
        op_type = __ri_get_op_type(op_str);
-       if(op_type < 0){
+       if (op_type < 0) {
                _LOGE("fota-info : operation error[%s, %s]\n", pkgid, op_str);
                goto end;
        }
 
        switch (op_type) {
-               case INSTALL_REQ:
-                       ret = __ri_install_fota_for_rw(pkgid);
-                       break;
+       case INSTALL_REQ:
+               ret = __ri_install_fota_for_rw(pkgid);
+               break;
 
-               case UPGRADE_REQ:
-                       ret = __ri_upgrade_fota_for_rw(pkgid);
-                       break;
+       case UPGRADE_REQ:
+               ret = __ri_upgrade_fota_for_rw(pkgid);
+               break;
 
-               case UNINSTALL_REQ:
-                       ret = __ri_uninstall_fota_for_rw(pkgid);
-                       break;
+       case UNINSTALL_REQ:
+               ret = __ri_uninstall_fota_for_rw(pkgid);
+               break;
 
-               default:
-                       break;
+       default:
+               break;
        }
 
        if (ret < 0)
-               _LOGE("fota-info : Fota fail [pkgid=%s, operation=%d]\n",pkgid, op_type);
+               _LOGE("fota-info : Fota fail [pkgid=%s, operation=%d]\n", pkgid, op_type);
 
 end:
-       if(pkgid)
+       if (pkgid)
                free(pkgid);
-       if(op_str)
+       if (op_str)
                free(op_str);
 
        sync();
@@ -4003,246 +4179,230 @@ end:
        return ret;
 }
 
-int _rpm_process_enable(char *pkgid)
+int __ri_copy_smack_rule_file(int op, const char *pkgname, int is_system)
 {
-       int ret = 0;
-       char *manifest = NULL;
-       pkgmgrinfo_pkginfo_h handle;
-       bool is_system = 0;
-
-       _LOGE("start :: pkgid[%s] enable process\n",pkgid);
-
-       ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
-       if ((ret == 0) && (handle != NULL)) {
-               _LOGE("pkg[%s] is already installed.", pkgid);
-               pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
-               return 0;
-       }
+       mode_t mode = DIR_PERMS;
+       int ret = RPM_INSTALLER_SUCCESS;
+       char src[BUF_SIZE] = { 0 };
+       char dest[BUF_SIZE] = { 0 };
+       char buf[BUF_SIZE] = { 0, };
 
-       manifest = pkgmgr_parser_get_manifest_file(pkgid);
-       if (manifest == NULL) {
-               _LOGE("Failed to fetch package manifest file\n");
-               return -1;
-       }
+       switch (op) {
+       case UNINSTALL_REQ:
+               /* For downloadable native app, restore the smack file.
+                  Otherwise, remove the stored smack file. */
+               snprintf(dest, BUF_SIZE - 1, "%s/%s.rule", SMACK_RULES_ALT_PATH, pkgname);
+               snprintf(src, BUF_SIZE - 1, "%s%s.rule", DIR_RPM_WGT_SMACK_RULE_OPT, pkgname);
+               _LOGD("#src:[%s] dest:[%s]", src, dest);
+
+               if (!is_system) {
+                       if (!access(src, F_OK)) {
+                               ret = remove(src);
+                               if (!ret) {
+                                       _LOGD("#File [%s] deleted.", src);
+                               } else {
+                                       if( strerror_r(errno, buf, sizeof(buf)) == 0) {
+                                               _LOGE("@Unable to delete the file [%s], error:(%s)", src, buf);
+                                       }
+                                       ret = RPM_INSTALLER_ERR_INTERNAL;
+                                       goto end;
+                               }
+                       }
+                       if (!access(dest, F_OK)) {
+                               ret = remove(dest);
+                               if (!ret) {
+                                       _LOGD("#File [%s] deleted.", dest);
+                               } else {
+                                       if( strerror_r(errno, buf, sizeof(buf)) == 0) {
+                                               _LOGE("@Unable to delete the file [%s], error:(%s)", dest, buf);
+                                       }
+                                       ret = RPM_INSTALLER_ERR_INTERNAL;
+                                       goto end;
+                               }
+                       }
+               } else {
+                       _LOGD("#Restore smack files for uninstallation [%s]", pkgname);
+                       if (!access(src, F_OK)) {
+                               _LOGD("#Copying [%s] to [%s]", src, dest);
+                               ret = __copy_file(src, dest);
+                               if (!ret) {
+                                       ret = remove(src);
+                                       if (!ret) {
+                                               _LOGD("#File [%s] deleted.", src);
+                                       } else {
+                                               if( strerror_r(errno, buf, sizeof(buf)) == 0) {
+                                                       _LOGE("@Unable to delete the file [%s], error:(%s)", src, buf);
+                                               }
+                                               ret = RPM_INSTALLER_ERR_INTERNAL;
+                                               goto end;
+                                       }
+                               } else {
+                                       _LOGE("@Copy Failed!!");
+                                       ret = RPM_INSTALLER_ERR_INTERNAL;
+                                       goto end;
+                               }
+                       } else {
+                               _LOGE("@ %s.rule file is not preserved", pkgname);
+                       }
+               }
+               break;
+       case UPGRADE_REQ:
+               _LOGD("#Preserve the smack file for upgrade [%s]", pkgname);
 
-       _ri_broadcast_status_notification(pkgid, "rpm", PKGMGR_INSTALLER_START_KEY_STR, PKGMGR_INSTALLER_INSTALL_EVENT_STR);
+               /* Apply the new smack file and preserve the old smack rule file
+                  if it is not preserved. */
+               snprintf(src, BUF_SIZE - 1, "%s/%s.rule", SMACK_RULES_ALT_PATH, pkgname);
+               snprintf(dest, BUF_SIZE - 1, "%s%s.rule", DIR_RPM_WGT_SMACK_RULE_OPT, pkgname);
 
-       ret = pkgmgr_parser_parse_manifest_for_installation(manifest, NULL);
-       free(manifest);
-       if (ret < 0) {
-               _ri_broadcast_status_notification(pkgid, "rpm", PKGMGR_INSTALLER_END_KEY_STR, PKGMGR_INSTALLER_FAIL_EVENT_STR);
-               _LOGE("insert in db failed\n");
-               return -1;
-       }
+               _LOGD("#src[%s] dest[%s]", src, dest);
 
-       ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
-       if (ret < 0) {
-               _LOGE("insert in db failed\n");
-       } else {
-               ret = pkgmgrinfo_pkginfo_is_system(handle, &is_system);
-               if (is_system) {
-                       pkgmgrinfo_appinfo_get_list(handle, PM_UI_APP, __ri_update_ail_info, NULL);
+               /* Create the directory if not exist to preserve the smack files */
+               if (mkdir(DIR_RPM_WGT_SMACK_RULE_OPT, mode) == 0 || errno == EEXIST) {
+                       if ((access(src, F_OK) == 0) && (access(dest, F_OK) != 0)) {
+                               ret = __copy_file(src, dest);
+                       } else {
+                               _LOGD("#Smack file is already preserved");
+                       }
+               } else {
+                       _LOGE("@Temporary folder creation failed");
+                       ret = RPM_INSTALLER_ERR_INTERNAL;
                }
-               pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
+               break;
+       default:
+               _LOGE("@Unsupported Operation\n");
+               ret = RPM_INSTALLER_ERR_INTERNAL;
        }
-
-       /*delete disabled pkg info from backup db table*/
-       pkgmgr_parser_delete_disabled_pkg(pkgid, NULL);
-
-       _ri_broadcast_status_notification(pkgid, "rpm", PKGMGR_INSTALLER_END_KEY_STR, PKGMGR_INSTALLER_OK_EVENT_STR);
-
-       _LOGE("end :: pkgid[%s] enable process\n",pkgid);
-
-       return 0;
+ end:
+       return ret;
 }
 
-int _rpm_process_disable(char *pkgid)
+int __get_smack_label_from_xml(const char *manifest, const char *pkgid, char **label)
 {
-       int ret = 0;
-       pkgmgrinfo_pkginfo_h handle;
-
-       _LOGE("start :: pkgid[%s] disable process\n",pkgid);
+       const char *val = NULL;
+       char pkgpath[BUF_SIZE] = { '\0' };
+       const xmlChar *node;
+       xmlTextReaderPtr reader;
+       int ret = PMINFO_R_OK;
 
-       ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
-       if ((ret < 0) || (handle == NULL)) {
-               _LOGE("pkgid[%s] is already disabled\n", pkgid);
-               return 0;
+       if (label == NULL) {
+               _LOGE("space for label is NULL\n");
+               return PMINFO_R_ERROR;
        }
 
-       _ri_broadcast_status_notification(pkgid, "rpm", PKGMGR_INSTALLER_START_KEY_STR, PKGMGR_INSTALLER_UNINSTALL_EVENT_STR);
-
-       pkgmgrinfo_appinfo_get_list(handle, PM_UI_APP, __ri_check_running_app, NULL);
-       pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
+       *label = NULL;
 
-       ret = pkgmgr_parser_parse_manifest_for_uninstallation(pkgid, NULL);
-       if (ret < 0) {
-               _ri_broadcast_status_notification(pkgid, "rpm", PKGMGR_INSTALLER_END_KEY_STR, PKGMGR_INSTALLER_FAIL_EVENT_STR);
-               _LOGE("pkgmgr_parser_parse_manifest_for_uninstallation failed.\n");
-               return -1;
+       if (pkgid == NULL) {
+               _LOGE("pkgid is NULL\n");
+               return PMINFO_R_ERROR;
        }
 
-       /*save disabled pkg info to backup db table*/
-       pkgmgr_parser_insert_disabled_pkg(pkgid, NULL);
-
-       _ri_broadcast_status_notification(pkgid, "rpm", PKGMGR_INSTALLER_END_KEY_STR, PKGMGR_INSTALLER_OK_EVENT_STR);
-
-       _LOGE("end :: pkgid[%s] disable process\n",pkgid);
-       return 0;
-}
-
-int _rpm_process_enabled_list(const char *enabled_list)
-{
-       char* token = NULL;
-       char delims[] = ":";
-       char pkgid[MAX_BUF_SIZE] = {'\0'};
-       char pkgid_list[MAX_BUF_SIZE] = {'\0'};
-
-       if (enabled_list == NULL)
-               return -1;
-
-       snprintf(pkgid_list, MAX_BUF_SIZE, "%s", enabled_list);
-       token = strtok(pkgid_list, delims);
-
-       while(token)
-       {
-               memset(pkgid, 0x00, sizeof(pkgid));
-               if (strlen(token) < MAX_BUF_SIZE)
-                       strcat(pkgid, token);
-               else
-                       _LOGE("pkgid too long[%s]", token);
-
-
-               _rpm_process_enable(pkgid);
-
-               token = strtok(NULL, delims);
+       if (manifest == NULL) {
+               _LOGE("manifest is NULL\n");
+               *label = strdup(pkgid);
+               return PMINFO_R_ERROR;
        }
 
-       return 0;
-}
+       snprintf(pkgpath, BUF_SIZE, "%s/%s/tizen-manifest.xml", OPT_USR_APPS, pkgid);
+       if (access(pkgpath, R_OK) == 0) {
+               _LOGE("This is a core tpk package");
+               *label = strdup(pkgid);
+               return 0;
+       }
 
-int _rpm_process_disabled_list(const char *disabled_list)
-{
-       char* token = NULL;
-       char delims[] = ":";
-       char pkgid[MAX_BUF_SIZE] = {'\0'};
-       char pkgid_list[MAX_BUF_SIZE] = {'\0'};
+       reader = xmlReaderForFile(manifest, NULL, 0);
 
-       if (disabled_list == NULL)
-               return -1;
+       if (reader) {
+               if (_child_element(reader, -1)) {
+                       node = xmlTextReaderConstName(reader);
+                       if (!node) {
+                               _LOGE("xmlTextReaderConstName value is NULL\n");
+                               ret = PMINFO_R_ERROR;
+                               goto end;
+                       }
 
-       snprintf(pkgid_list, MAX_BUF_SIZE, "%s", disabled_list);
-       token = strtok(pkgid_list, delims);
+                       if (!strcmp(ASCII(node), "manifest")) {
+                               ret = _ri_get_attribute(reader, "smack-label", &val);
+                               if (ret != 0) {
+                                       _LOGE("@Error in getting the attribute value");
+                                       ret = PMINFO_R_ERROR;
+                                       goto end;
+                               }
+                               if (val) {
+                                       *label = strdup(val);
+                                       if (*label == NULL) {
+                                               _LOGE("Out of memeory\n");
+                                               ret = PMINFO_R_ERROR;
+                                       }
+                                       free((void *)val);
+                               } else {
+                                       *label = NULL;
+                                       _LOGE("package smack-label is not specified\n");
+                                       goto end;
+                               }
+                       } else {
+                               _LOGE("Unable to create xml reader\n");
+                               ret = PMINFO_R_ERROR;
+                               goto end;
+                       }
+               }
+       } else {
+               _LOGE("xmlReaderForFile value is NULL\n");
+               ret = PMINFO_R_ERROR;
+       }
 
-       while(token)
-       {
-               memset(pkgid, 0x00, sizeof(pkgid));
-               if (strlen(token) < MAX_BUF_SIZE)
-                       strncat(pkgid, token, strlen(token));
-               else
-                       _LOGE("pkgid too long[%s]", token);
+ end:
 
-               _rpm_process_disable(pkgid);
+       xmlFreeTextReader(reader);
 
-               token = strtok(NULL, delims);
+       if (*label == NULL) {
+               *label = strdup(pkgid);
        }
 
-       return 0;
+       return ret;
 }
 
-int __ri_copy_smack_rule_file(int op, const char *pkgname, int is_system)
+int __get_smack_label_from_db(const char *pkgid, char **label)
 {
+       char *smack_label = NULL;
+       pkgmgrinfo_pkginfo_h handle = NULL;
+       int ret = PMINFO_R_OK;
 
-       mode_t mode = DIR_PERMS;
-       int ret = RPM_INSTALLER_SUCCESS;
-       char src[PATH_MAX]={0};
-       char dest[PATH_MAX]={0};
-
-       switch(op)
-               {
-               case UNINSTALL_REQ:
-                       /*
-                       For downloadable native app, restore the smack file.
-                       Otherwise, remove the stored smack file.
-                       */
-                       snprintf(dest,PATH_MAX-1,"%s%s.rule",SMACK_RULES_ALT_PATH,pkgname);
-                       snprintf(src,PATH_MAX-1,"%s%s.rule",DIR_RPM_WGT_SMACK_RULE_OPT,pkgname);
-                       _LOGD("#src:[%s] dest:[%s]",src,dest);
-
-                       if(!is_system){
-                               if(!access(src,F_OK)){
-                                       ret = remove(src);
-                                       if(!ret){
-                                               _LOGD("#File [%s] deleted.",src);
-                                       }else{
-                                               _LOGE("@Unable to delete the file [%s], error:(%s)",src,strerror(errno));
-                                               ret = RPM_INSTALLER_ERR_INTERNAL;
-                                               goto end;
-                                       }
-                               }
-                               if(!access(dest,F_OK)){
-                                       ret = remove(dest);
-                                       if(!ret){
-                                               _LOGD("#File [%s] deleted.",dest);
-                                       }else{
-                                               _LOGE("@Unable to delete the file [%s], error:(%s)",dest,strerror(errno));
-                                               ret = RPM_INSTALLER_ERR_INTERNAL;
-                                               goto end;
-                                       }
-                               }
-                       }else{
-                               _LOGD("#Restore smack files for uninstallation [%s]",pkgname);
-                               if(!access(src,F_OK)){
-                                       _LOGD("#Copying [%s] to [%s]",src,dest);
-                                       ret = __copy_file(src,dest);
-                                       if(!ret){
-                                               ret = remove(src);
-                                               if(!ret){
-                                                       _LOGD("#File [%s] deleted.",src);
-                                               }else{
-                                                       _LOGE("@Unable to delete the file [%s], error:(%s)",src,strerror(errno));
-                                                       ret = RPM_INSTALLER_ERR_INTERNAL;
-                                                       goto end;
-                                               }
-                                       }else{
-                                               _LOGE("@Copy Failed!!");
-                                               ret = RPM_INSTALLER_ERR_INTERNAL;
-                                               goto end;
-                                       }
-                               }else{
-                                       _LOGE("@ %s.rule file is not preserved",pkgname);
-                               }
-                       }
-                       break;
-
-               case UPGRADE_REQ:
+       if (label == NULL) {
+               _LOGE("space for label is NULL\n");
+               return PMINFO_R_ERROR;
+       }
 
-                       _LOGD("#Preserve the smack file for upgrade [%s]",pkgname);
+       *label = NULL;
 
-                       /* Apply the new smack file and preserve the old smack rule file if it is not preserved.*/
-                       snprintf(src,PATH_MAX-1,"%s%s.rule",SMACK_RULES_ALT_PATH,pkgname);
-                       snprintf(dest,PATH_MAX-1,"%s%s.rule",DIR_RPM_WGT_SMACK_RULE_OPT,pkgname);
+       if (pkgid == NULL) {
+               _LOGE("pkgid is NULL\n");
+               return PMINFO_R_ERROR;
+       }
 
-                       _LOGD("#src[%s] dest[%s]",src,dest);
+       ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
+       if (ret != PMINFO_R_OK) {
+               _LOGE("failed to get pkginfo\n");
+               ret = PMINFO_R_ERROR;
+               goto end;
+       }
+       ret = pkgmgrinfo_pkginfo_get_custom_smack_label(handle, &smack_label);
+       if (ret != PMINFO_R_OK) {
+               _LOGE("failed to get custom smack_label\n");
+               ret = PMINFO_R_ERROR;
+               goto end;
+       }
 
-                       /* Create the directory if not exist to preserve the smack files */
-                       if(mkdir(DIR_RPM_WGT_SMACK_RULE_OPT,mode) == 0 || errno == EEXIST){
-                               if((access(src,F_OK) == 0) && (access(dest,F_OK) != 0)){
-                                       ret = __copy_file(src,dest);
-                               }else{
-                                       _LOGD("#Smack file is already preserved");
-                               }
-                       }else{
-                               _LOGE("@Temporary folder creation failed");
-                               ret = RPM_INSTALLER_ERR_INTERNAL;
-                       }
-                       break;
-               default:
-                       _LOGE("@Unsupported Operation\n");
-                       ret = RPM_INSTALLER_ERR_INTERNAL;
-               }
+       _LOGD("smack_label(%s)\n", smack_label);
+       if (smack_label) {
+               *label = strdup(smack_label);
+       }
 
      end:
-               return ret;
+ end:
+       pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
 
+       if (*label == NULL) {
+               *label = strdup(pkgid);
+       }
 
+       return ret;
 }