4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>,
7 * Jaeho Lee <jaeho81.lee@samsung.com>, Shobhit Srivastava <shobhit.s@samsung.com>
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
28 #include <sys/types.h>
38 #include <ctype.h> /* for isspace () */
39 #include <wctype.h> /* for towlower() */
41 #include <cert-service.h>
42 #include <libxml/parser.h>
43 #include <libxml/tree.h>
44 #include <libxml/xmlmemory.h>
47 #include <sys/xattr.h>
49 #ifndef XMLSEC_NO_XSLT
50 #include <libxslt/xslt.h>
51 #include <libxslt/security.h>
54 #include <xmlsec/xmlsec.h>
55 #include <xmlsec/xmltree.h>
56 #include <xmlsec/xmldsig.h>
57 #include <xmlsec/crypto.h>
58 #include <xmlsec/errors.h>
59 #include <pkgmgr-info.h>
60 #include <pkgmgr_parser.h>
61 #include <package-manager.h>
62 #include <privilege-control.h>
63 #include <app_manager.h>
66 #define APP2EXT_ENABLE
68 #include <app2ext_interface.h>
71 #include "rpm-installer-util.h"
72 #include "rpm-installer-signature.h"
73 #include "rpm-installer.h"
74 #include "rpm-frontend.h"
75 #include "rpm-installer-type.h"
76 #include "coretpk-installer-internal.h"
85 enum rpm_sig_sub_type {
91 typedef struct cert_chain_t {
96 cert_chain list[MAX_CERT_NUM];
97 GList *privilege_list;
99 #define PKGMGR_DB "/opt/dbspace/.pkgmgr_parser.db"
100 #define EXT_APPDATA_PRIVILEGE_NAME "http://tizen.org/privilege/externalstorage.appdata"
102 extern char *gpkgname;
103 extern int sig_enable;
105 static void __rpm_process_line(char *line);
106 static void __rpm_perform_read(int fd);
107 static int __ri_xmlsec_verify_signature(const char *sigxmlfile, char *rootca);
108 static xmlSecKeysMngrPtr __ri_load_trusted_certs(char *files, int files_size);
109 static int __ri_verify_file(xmlSecKeysMngrPtr mngr, const char *sigxmlfile);
110 static int __ri_create_cert_chain(int sigtype, int sigsubtype, char *value);
111 static void __ri_free_cert_chain(void);
112 static char *__ri_get_cert_from_file(const char *file);
113 static int __privilege_func(const char *name, void *user_data);
114 static char *__ri_get_str(const char* str, const char* pKey);
115 static void __ri_xmlsec_debug_print(const char* file, int line, const char* func,
116 const char* errorObject, const char* errorSubject, int reason, const char* msg);
118 int _ri_set_group_id(const char *pkgid, const char *groupid);
120 static void __str_trim(char *input)
122 char *trim_str = input;
127 while (*input != 0) {
128 if (!isspace(*input)) {
139 static int __ri_check_pkgid_for_deactivation(const char *pkgid)
142 char deactivation_str[FILENAME_MAX] = { 0 };
143 char *deactivation_pkgid = NULL;
144 char *deactivation_state = NULL;
146 fp = fopen(DEACTIVATION_PKGID_LIST, "r");
148 _LOGE("fopen fail\n");
152 while (fgets(deactivation_str, sizeof(deactivation_str), fp) != NULL) {
153 __str_trim(deactivation_str);
155 deactivation_pkgid = __ri_get_str(deactivation_str, TOKEN_PKGID_STR);
156 if(deactivation_pkgid == NULL)
159 deactivation_state = __ri_get_str(deactivation_str, TOKEN_STATE_STR);
160 if(deactivation_state == NULL) {
161 free(deactivation_pkgid);
165 if ((strcmp(deactivation_pkgid, pkgid) == 0) && (strcmp(deactivation_state, "off") == 0)) {
167 free(deactivation_pkgid);
168 free(deactivation_state);
169 _LOGE("Find pkgid[%s] form deactivation list.\n", pkgid);
173 free(deactivation_pkgid);
174 free(deactivation_state);
175 memset(deactivation_str, 0x00, sizeof(deactivation_str));
185 static int __ri_get_op_type(char *op_str)
187 if (strcmp(op_str,"install")==0)
189 else if (strcmp(op_str,"update")==0)
191 else if (strcmp(op_str,"uninstall")==0)
192 return UNINSTALL_REQ;
197 static char *__ri_get_str(const char* str, const char* pKey)
199 const char* p = NULL;
200 const char* pStart = NULL;
201 const char* pEnd = NULL;
206 char *pBuf = strdup(str);
208 _LOGE("Malloc failed !");
212 p = strstr(pBuf, pKey);
221 pStart = p + strlen(pKey);
222 pEnd = strchr(pStart, SEPERATOR_END);
230 size_t len = pEnd - pStart;
238 char *pRes = (char*)malloc(len + 1);
244 _LOGE("Malloc failed!");
247 strncpy(pRes, pStart, len);
258 static int __ri_init_csc_xml(char *xml_path, char *removable)
261 char* csc_tags[3] = {NULL, };
263 if (strcmp(removable,"true")==0)
264 csc_tags[0] = "removable=true";
266 csc_tags[0] = "removable=false";
268 csc_tags[1] = "preload=true";
271 ret = pkgmgr_parser_parse_manifest_for_installation(xml_path, csc_tags);
276 static int __ri_create_cert_chain(int sigtype, int sigsubtype, char *value)
280 // _LOGD("Push in list [%d] [%d] [%s]", sigtype, sigsubtype, value);
283 switch (sigsubtype) {
285 list[PMINFO_SET_AUTHOR_SIGNER_CERT].cert_type = PMINFO_SET_AUTHOR_SIGNER_CERT;
286 list[PMINFO_SET_AUTHOR_SIGNER_CERT].cert_value = strdup(value);
288 case SIG_INTERMEDIATE:
289 list[PMINFO_SET_AUTHOR_INTERMEDIATE_CERT].cert_type = PMINFO_SET_AUTHOR_INTERMEDIATE_CERT;
290 list[PMINFO_SET_AUTHOR_INTERMEDIATE_CERT].cert_value = strdup(value);
293 /*value is already a mallocd pointer*/
294 list[PMINFO_SET_AUTHOR_ROOT_CERT].cert_type = PMINFO_SET_AUTHOR_ROOT_CERT;
295 list[PMINFO_SET_AUTHOR_ROOT_CERT].cert_value = value;
302 switch (sigsubtype) {
304 list[PMINFO_SET_DISTRIBUTOR_SIGNER_CERT].cert_type = PMINFO_SET_DISTRIBUTOR_SIGNER_CERT;
305 list[PMINFO_SET_DISTRIBUTOR_SIGNER_CERT].cert_value = strdup(value);
307 case SIG_INTERMEDIATE:
308 list[PMINFO_SET_DISTRIBUTOR_INTERMEDIATE_CERT].cert_type = PMINFO_SET_DISTRIBUTOR_INTERMEDIATE_CERT;
309 list[PMINFO_SET_DISTRIBUTOR_INTERMEDIATE_CERT].cert_value = strdup(value);
312 /*value is already a mallocd pointer*/
313 list[PMINFO_SET_DISTRIBUTOR_ROOT_CERT].cert_type = PMINFO_SET_DISTRIBUTOR_ROOT_CERT;
314 list[PMINFO_SET_DISTRIBUTOR_ROOT_CERT].cert_value = value;
321 switch (sigsubtype) {
323 list[PMINFO_SET_DISTRIBUTOR2_SIGNER_CERT].cert_type = PMINFO_SET_DISTRIBUTOR2_SIGNER_CERT;
324 list[PMINFO_SET_DISTRIBUTOR2_SIGNER_CERT].cert_value = strdup(value);
326 case SIG_INTERMEDIATE:
327 list[PMINFO_SET_DISTRIBUTOR2_INTERMEDIATE_CERT].cert_type = PMINFO_SET_DISTRIBUTOR2_INTERMEDIATE_CERT;
328 list[PMINFO_SET_DISTRIBUTOR2_INTERMEDIATE_CERT].cert_value = strdup(value);
331 /*value is already a mallocd pointer*/
332 list[PMINFO_SET_DISTRIBUTOR2_ROOT_CERT].cert_type = PMINFO_SET_DISTRIBUTOR2_ROOT_CERT;
333 list[PMINFO_SET_DISTRIBUTOR2_ROOT_CERT].cert_value = value;
345 static void __ri_free_cert_chain()
348 for (i = 0; i < MAX_CERT_NUM; i++) {
349 if (list[i].cert_value)
350 free(list[i].cert_value);
354 static void __ri_xmlsec_debug_print(const char* file, int line, const char* func,
355 const char* errorObject, const char* errorSubject, int reason, const char* msg)
357 char total[BUF_SIZE];
358 snprintf(total, sizeof(total), "[%s(%d)] : [%s] : [%s] : [%s]", func, line, errorObject, errorSubject, msg);
360 fprintf(stderr, "## [validate error]: %s\n", total);
367 static int __ri_verify_file(xmlSecKeysMngrPtr sec_key_mngr, const char *sigxmlfile)
369 xmlDocPtr doc = NULL;
370 xmlNodePtr node = NULL;
371 xmlSecDSigCtxPtr dsigCtx = NULL;
373 if (sigxmlfile == NULL)
375 if (sec_key_mngr == NULL)
378 /* set error callback to xmlsec1 */
379 xmlSecErrorsSetCallback(__ri_xmlsec_debug_print);
382 doc = xmlParseFile(sigxmlfile);
383 if ((doc == NULL) || (xmlDocGetRootElement(doc) == NULL)) {
384 _LOGE("unable to parse file \"%s\"\n", sigxmlfile);
387 /* find start node */
388 node = xmlSecFindNode(xmlDocGetRootElement(doc), xmlSecNodeSignature, xmlSecDSigNs);
390 _LOGE("start node not found in \"%s\"\n", sigxmlfile);
393 /* create signature context */
394 dsigCtx = xmlSecDSigCtxCreate(sec_key_mngr);
395 if (dsigCtx == NULL) {
396 _LOGE("failed to create signature context\n");
399 /* Verify signature */
400 if (xmlSecDSigCtxVerify(dsigCtx, node) < 0) {
401 _LOGE("failed to verify signature\n");
404 /* print verification result to stdout */
405 if (dsigCtx->status == xmlSecDSigStatusSucceeded) {
407 _LOGD("valid signature");
410 _LOGD("invalid signature");
415 if(dsigCtx != NULL) {
416 xmlSecDSigCtxDestroy(dsigCtx);
424 static xmlSecKeysMngrPtr __ri_load_trusted_certs(char *files, int files_size)
426 xmlSecKeysMngrPtr sec_key_mngr;
431 sec_key_mngr = xmlSecKeysMngrCreate();
432 if (sec_key_mngr == NULL) {
433 _LOGE("failed to create keys manager.\n");
436 if (xmlSecCryptoAppDefaultKeysMngrInit(sec_key_mngr) < 0) {
437 _LOGE("failed to initialize keys manager.\n");
438 xmlSecKeysMngrDestroy(sec_key_mngr);
441 /* load trusted cert */
442 if (xmlSecCryptoAppKeysMngrCertLoad(sec_key_mngr, files, xmlSecKeyDataFormatPem, xmlSecKeyDataTypeTrusted) < 0) {
443 _LOGE("failed to load pem certificate from \"%s\"\n", files);
444 xmlSecKeysMngrDestroy(sec_key_mngr);
450 static int __ri_xmlsec_verify_signature(const char *sigxmlfile, char *rootca)
453 xmlSecKeysMngrPtr sec_key_mngr = NULL;
454 xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
455 xmlSubstituteEntitiesDefault(1);
457 #ifndef XMLSEC_NO_XSLT
458 xmlIndentTreeOutput = 1;
459 xsltSecurityPrefsPtr sec_prefs = xsltNewSecurityPrefs();
460 xsltSetSecurityPrefs(sec_prefs, XSLT_SECPREF_WRITE_FILE, xsltSecurityForbid);
461 xsltSetSecurityPrefs(sec_prefs, XSLT_SECPREF_READ_FILE, xsltSecurityForbid);
462 xsltSetSecurityPrefs(sec_prefs, XSLT_SECPREF_CREATE_DIRECTORY, xsltSecurityForbid);
463 xsltSetSecurityPrefs(sec_prefs, XSLT_SECPREF_WRITE_NETWORK, xsltSecurityForbid);
464 xsltSetSecurityPrefs(sec_prefs, XSLT_SECPREF_READ_NETWORK, xsltSecurityForbid);
465 xsltSetDefaultSecurityPrefs(sec_prefs);
470 _LOGE("xmlsec initialization failed [%d]\n", ret);
473 ret = xmlSecCheckVersion();
475 _LOGE("Incompatible version of loaded xmlsec library [%d]\n", ret);
478 #ifdef XMLSEC_CRYPTO_DYNAMIC_LOADING
479 ret = xmlSecCryptoDLLoadLibrary(BAD_CAST "openssl");
481 _LOGE("unable to load openssl library [%d]\n", ret);
486 ret = xmlSecCryptoAppInit(NULL);
488 _LOGE("crypto initialization failed [%d]\n", ret);
491 ret = xmlSecCryptoInit();
493 _LOGE("xmlsec-crypto initialization failed [%d]\n", ret);
497 sec_key_mngr = __ri_load_trusted_certs(rootca, 1);
498 if (sec_key_mngr == NULL) {
499 _LOGE("loading of trusted certs failed\n");
504 if (__ri_verify_file(sec_key_mngr, sigxmlfile) < 0) {
510 xmlSecKeysMngrDestroy(sec_key_mngr);
511 xmlSecCryptoShutdown();
512 xmlSecCryptoAppShutdown();
514 #ifndef XMLSEC_NO_XSLT
515 xsltFreeSecurityPrefs(sec_prefs);
516 xsltCleanupGlobals();
521 int _rpm_installer_get_group_id(char *pkgid, char **result)
524 const char *value = NULL;
525 char author_signature[BUF_SIZE] = {'\0'};
526 char *e_rootcert = NULL;
527 char *d_rootcert = NULL;
529 unsigned char hashout[BUF_SIZE] = {'\0'};
530 unsigned int h_size = 0;
533 pkgmgrinfo_certinfo_h handle = NULL;
535 snprintf(author_signature, BUF_SIZE, "%s/%s/%s", USR_APPS, pkgid, AUTHOR_SIGNATURE_XML);
536 if (access(author_signature, F_OK) != 0) {
537 _LOGE("[%s] isn't existed.", author_signature);
541 ret = pkgmgrinfo_pkginfo_create_certinfo(&handle);
543 _LOGE("pkgmgrinfo_pkginfo_create_certinfo(%s) failed.", pkgid);
547 ret = pkgmgrinfo_pkginfo_load_certinfo(pkgid, handle);
549 _LOGE("pkgmgrinfo_pkginfo_load_certinfo(%s) failed.", pkgid);
553 // get root certificate
554 ret = pkgmgrinfo_pkginfo_get_cert_value(handle, PMINFO_AUTHOR_SIGNER_CERT, &value);
555 if (ret < 0 || value == NULL) {
556 _LOGE("pkgmgrinfo_pkginfo_get_cert_value(%s) failed. [%d]", pkgid, ret);
561 d_rootcert = (char *)g_base64_decode(value, &d_size);
562 if (d_rootcert == NULL) {
563 _LOGE("g_base64_decode() failed.");
566 _LOGD("g_base64_decode() succeed, d_size=[%d]", d_size);
569 EVP_Digest(d_rootcert, d_size, hashout, &h_size, EVP_sha1(), NULL);
571 _LOGE("EVP_Digest(hash) failed.");
574 _LOGD("EVP_Digest() succeed, h_size=[%d]", h_size);
577 e_rootcert = g_base64_encode((const guchar *)hashout, h_size);
578 if (e_rootcert == NULL) {
579 _LOGE("g_base64_encode() failed.");
582 e_size = strlen(e_rootcert);
583 _LOGD("g_base64_encode() succeed, e_size=[%d]", e_size);
586 for (length = e_size; length >= 0; --length) {
587 if (e_rootcert[length] == '/') {
588 e_rootcert[length] = '#';
592 *result = e_rootcert;
601 pkgmgrinfo_pkginfo_destroy_certinfo(handle);
607 void __rpm_apply_smack(char *pkgname, int flag)
610 char dirpath[BUF_SIZE] = {'\0'};
611 char* groupid = NULL;
612 char *shared_data_label = NULL;
614 /*execute privilege APIs. The APIs should not fail*/
615 _ri_privilege_register_package(pkgname);
617 /*home dir. Dont setup path but change smack access to "_" */
618 snprintf(dirpath, BUF_SIZE, "%s/%s", USR_APPS, pkgname);
619 if (__is_dir(dirpath))
620 _ri_privilege_change_smack_label(dirpath, "_", 0);/*0 is SMACK_LABEL_ACCESS*/
621 memset(dirpath, '\0', BUF_SIZE);
622 snprintf(dirpath, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgname);
623 if (__is_dir(dirpath))
624 _ri_privilege_change_smack_label(dirpath, "_", 0);/*0 is SMACK_LABEL_ACCESS*/
627 memset(dirpath, '\0', BUF_SIZE);
628 snprintf(dirpath, BUF_SIZE, "%s/%s/data", OPT_USR_APPS, pkgname);
629 if (__is_dir(dirpath))
630 _ri_privilege_setup_path(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
633 memset(dirpath, '\0', BUF_SIZE);
634 snprintf(dirpath, BUF_SIZE, "%s/%s/cache", OPT_USR_APPS, pkgname);
635 if (!__is_dir(dirpath)) {
636 ret = mkdir(dirpath, DIRECTORY_PERMISSION_755);
638 _LOGE("directory making is failed.\n");
641 ret = chown(dirpath, APP_OWNER_ID, APP_GROUP_ID);
643 _LOGE("chown failed!! [%s]",strerror(errno));
645 _ri_privilege_setup_path(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
648 memset(dirpath, '\0', BUF_SIZE);
649 snprintf(dirpath, BUF_SIZE, "%s/%s/shared", USR_APPS, pkgname);
650 if (__is_dir(dirpath))
651 _ri_privilege_change_smack_label(dirpath, "_", 0);/*0 is SMACK_LABEL_ACCESS*/
652 memset(dirpath, '\0', BUF_SIZE);
653 snprintf(dirpath, BUF_SIZE, "%s/%s/shared", OPT_USR_APPS, pkgname);
654 if (!__is_dir(dirpath))
655 ret = mkdir(dirpath, DIRECTORY_PERMISSION_755);
657 _LOGE("directory making is failed.\n");
659 _ri_privilege_change_smack_label(dirpath, "_", 0);/*0 is SMACK_LABEL_ACCESS*/
660 memset(dirpath, '\0', BUF_SIZE);
663 snprintf(dirpath, BUF_SIZE, "%s/%s/shared/res", USR_APPS, pkgname);
664 if (__is_dir(dirpath))
665 _ri_privilege_setup_path(pkgname, dirpath, PERM_APP_PATH_ANY_LABEL, "_");
666 memset(dirpath, '\0', BUF_SIZE);
668 snprintf(dirpath, BUF_SIZE, "%s/%s/shared/res", OPT_USR_APPS, pkgname);
669 if (__is_dir(dirpath))
670 _ri_privilege_setup_path(pkgname, dirpath, PERM_APP_PATH_ANY_LABEL, "_");
671 memset(dirpath, '\0', BUF_SIZE);
674 snprintf(dirpath, BUF_SIZE, "%s/%s/shared/data", USR_APPS, pkgname);
675 if (__is_dir(dirpath)) {
676 ret = chown(dirpath, APP_OWNER_ID, APP_GROUP_ID);
678 _LOGE("chown failed!! [%s]",strerror(errno));
680 _ri_privilege_setup_path(pkgname, dirpath, PERM_APP_PATH_PUBLIC, NULL);
682 memset(dirpath, '\0', BUF_SIZE);
684 snprintf(dirpath, BUF_SIZE, "%s/%s/shared/data", OPT_USR_APPS, pkgname);
685 if (__is_dir(dirpath)) {
686 ret = chown(dirpath, APP_OWNER_ID, APP_GROUP_ID);
688 _LOGE("chown failed!! [%s]",strerror(errno));
690 _ri_privilege_setup_path(pkgname, dirpath, PERM_APP_PATH_PUBLIC, NULL);
694 ret = _coretpk_installer_get_smack_label_access(dirpath, &shared_data_label);
696 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", dirpath, ret);
698 memset(dirpath, '\0', BUF_SIZE);
699 snprintf(dirpath, BUF_SIZE, "%s/%s/shared/cache", OPT_USR_APPS, pkgname);
700 if (!__is_dir(dirpath)) {
701 ret = mkdir(dirpath, DIRECTORY_PERMISSION_755);
703 _LOGE("directory making is failed.\n");
706 ret = chown(dirpath, APP_OWNER_ID, APP_GROUP_ID);
708 _LOGE("chown failed!! [%s]",strerror(errno));
710 ret = _coretpk_installer_set_smack_label_access(dirpath, shared_data_label);
712 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", dirpath, ret);
714 ret = _coretpk_installer_set_smack_label_transmute(dirpath, "1");
716 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", dirpath, ret);
720 memset(dirpath, '\0', BUF_SIZE);
721 snprintf(dirpath, BUF_SIZE, "%s/%s/shared/trusted", USR_APPS, pkgname);
722 if (__is_dir(dirpath)) {
723 ret = chown(dirpath, APP_OWNER_ID, APP_GROUP_ID);
725 _LOGE("chown failed!! [%s]",strerror(errno));
728 ret = _rpm_installer_get_group_id(pkgname, &groupid);
730 _LOGD("group id for trusted directory is [%s]", groupid);
731 _ri_privilege_setup_path(pkgname, dirpath, APP_PATH_GROUP_RW, groupid);
732 FREE_AND_NULL(groupid);
735 memset(dirpath, '\0', BUF_SIZE);
737 snprintf(dirpath, BUF_SIZE, "%s/%s/shared/trusted", OPT_USR_APPS, pkgname);
739 _LOGD("dirpath [%s]", dirpath);
741 ret = _rpm_installer_get_group_id(pkgname, &groupid);
743 if (__is_dir(dirpath) != 1) {
744 _LOGE("dont have [%s]", dirpath);
746 ret = mkdir(dirpath, DIRECTORY_PERMISSION_755);
747 if (ret == -1 && errno != EEXIST) {
748 _LOGE("mkdir failed!! [%s]",strerror(errno));
752 ret = chown(dirpath, APP_OWNER_ID, APP_GROUP_ID);
754 _LOGE("chown failed!! [%s]",strerror(errno));
757 _LOGD("group id for trusted directory is [%s]", groupid);
758 _ri_privilege_setup_path(pkgname, dirpath, APP_PATH_GROUP_RW, groupid);
759 _ri_set_group_id(pkgname, groupid);
761 FREE_AND_NULL(groupid);
765 int __is_dir(char *dirname)
767 struct stat stFileInfo;
768 if(dirname == NULL) {
769 _LOGE("dirname is null\n");
773 (void)stat(dirname, &stFileInfo);
775 if (S_ISDIR(stFileInfo.st_mode)) {
781 static void __rpm_process_line(char *line)
784 tok = strtok(line, " ");
786 if (!strncmp(tok, "%%", 2)) {
787 tok = strtok(NULL, " ");
789 _LOGD("Install percentage is %s\n",
791 _ri_broadcast_status_notification(gpkgname,
795 _ri_stat_cb(gpkgname, "install_percent", tok);
803 static void __rpm_perform_read(int fd)
805 char *buf_ptr = NULL;
806 char *tmp_ptr = NULL;
808 static char buffer[BUF_SIZE] = { 0, };
809 static int buffer_position;
811 size = read(fd, &buffer[buffer_position],
812 sizeof(buffer) - buffer_position);
813 buffer_position += size;
817 /* Process each line of the recieved buffer */
818 buf_ptr = tmp_ptr = buffer;
819 while ((tmp_ptr = (char *)memchr(buf_ptr, '\n',
820 buffer + buffer_position - buf_ptr)) !=
823 __rpm_process_line(buf_ptr);
824 /* move to next line and continue */
825 buf_ptr = tmp_ptr + 1;
828 /*move the remaining bits at the start of the buffer
829 and update the buffer position */
830 buf_ptr = (char *)memrchr(buffer, 0, buffer_position);
834 /* we have processed till the last \n which has now become
835 0x0. So we increase the pointer to next position */
838 memmove(buffer, buf_ptr, buf_ptr - buffer);
839 buffer_position = buffer + buffer_position - buf_ptr;
843 int _rpm_xsystem(const char *argv[])
852 if (pipe(pipefd) == -1) {
853 _LOGE("pipe creation failed\n");
856 /*Read progress info via pipe */
861 _LOGE("fork failed\n");
871 _LOGE("dup failed\n");
875 result = dup(pipefd[1]);
877 _LOGE("dup failed\n");
881 if (execvp(argv[0], (char *const *)argv) == -1) {
882 _LOGE("execvp failed\n");
893 while ((err = waitpid(pid, &status, WNOHANG)) != pid) {
897 _LOGE("waitpid failed\n");
906 FD_SET(pipefd[0], &rfds);
910 pselect(pipefd[0] + 1, &rfds, NULL, NULL, &tv, NULL);
914 else if (select_ret < 0 && errno == EINTR)
916 else if (select_ret < 0) {
917 _LOGE("select() returned error\n");
920 if (FD_ISSET(pipefd[0], &rfds))
921 __rpm_perform_read(pipefd[0]);
925 /* Check for an error code. */
926 if (WIFEXITED(status) == 0 || WEXITSTATUS(status) != 0) {
928 if (WIFSIGNALED(status) != 0 && WTERMSIG(status) == SIGSEGV) {
930 ("Sub-process %s received a segmentation fault. \n",
932 } else if (WIFEXITED(status) != 0) {
933 printf("Sub-process %s returned an error code (%u)\n",
934 argv[0], WEXITSTATUS(status));
936 printf("Sub-process %s exited unexpectedly\n", argv[0]);
939 return WEXITSTATUS(status);
942 void __rpm_clear_dir_list(GList* dir_list)
945 app2ext_dir_details* dir_detail = NULL;
947 list = g_list_first(dir_list);
949 dir_detail = (app2ext_dir_details *)list->data;
950 if (dir_detail && dir_detail->name) {
951 free(dir_detail->name);
954 list = g_list_next(list);
956 g_list_free(dir_list);
960 GList * __rpm_populate_dir_list()
962 GList *dir_list = NULL;
964 app2ext_dir_details* dir_detail = NULL;
966 char pkg_ro_content_rpm[3][5] = { "bin", "res", "lib" };
969 for (i=0; i<3; i++) {
970 dir_detail = (app2ext_dir_details*) calloc(1, sizeof(app2ext_dir_details));
971 if (dir_detail == NULL) {
972 printf("\nMemory allocation failed\n");
975 dir_detail->name = (char*) calloc(1, sizeof(char)*(strlen(pkg_ro_content_rpm[i])+2));
976 if (dir_detail->name == NULL) {
977 printf("\nMemory allocation failed\n");
981 snprintf(dir_detail->name, (strlen(pkg_ro_content_rpm[i])+1), "%s", pkg_ro_content_rpm[i]);
982 dir_detail->type = APP2EXT_DIR_RO;
983 dir_list = g_list_append(dir_list, dir_detail);
986 list = g_list_first(dir_list);
988 dir_detail = (app2ext_dir_details *)list->data;
989 list = g_list_next(list);
995 list = g_list_first(dir_list);
997 dir_detail = (app2ext_dir_details *)list->data;
998 if (dir_detail && dir_detail->name) {
999 free(dir_detail->name);
1002 list = g_list_next(list);
1004 g_list_free(dir_list);
1009 static char *__ri_get_cert_from_file(const char *file)
1011 FILE *fp_cert = NULL;
1013 char *certbuf = NULL;
1014 char *startcert = NULL;
1015 char *endcert = NULL;
1022 if(!(fp_cert = fopen(file, "r"))) {
1023 _LOGE("[ERR][%s] Fail to open file, [%s]\n", __func__, file);
1027 fseek(fp_cert, 0L, SEEK_END);
1029 certlen = ftell(fp_cert);
1031 _LOGE("[ERR][%s] Fail to find EOF\n", __func__);
1036 fseek(fp_cert, 0L, SEEK_SET);
1038 if(!(certbuf = (char*)malloc(sizeof(char) * (int)certlen))) {
1039 _LOGE("[ERR][%s] Fail to allocate memory\n", __func__);
1043 memset(certbuf, 0x00, (int)certlen);
1046 while((ch = fgetc(fp_cert)) != EOF) {
1054 startcert = strstr(certbuf, "-----BEGIN CERTIFICATE-----") + strlen("-----BEGIN CERTIFICATE-----");
1055 endcert = strstr(certbuf, "-----END CERTIFICATE-----");
1056 certwrite = (int)endcert - (int)startcert;
1058 cert = (char*)malloc(sizeof(char) * (certwrite+2));
1060 _LOGE("[ERR][%s] Fail to allocate memory\n", __func__);
1064 memset(cert, 0x00, certwrite+2);
1065 snprintf(cert, certwrite+1, "%s", startcert);
1066 _LOGD("Root CA, len=[%d]\n%s", certwrite, cert);
1078 static int __privilege_func(const char *name, void *user_data)
1081 const char *perm[] = {NULL, NULL};
1083 int apptype = PERM_APP_TYPE_EFL;
1084 privilegeinfo *info = (privilegeinfo*)user_data;
1086 _LOGD("package_id = [%s], privilege = [%s]", info->package_id, name);
1087 privilege_list = g_list_append(privilege_list, strdup((char*)name));
1089 if (info->visibility & CERT_SVC_VISIBILITY_PLATFORM) {
1090 _LOGD("VISIBILITY_PLATFORM!");
1091 apptype = PERM_APP_TYPE_EFL_PLATFORM;
1092 } else if ((info->visibility & CERT_SVC_VISIBILITY_PARTNER) ||
1093 (info->visibility & CERT_SVC_VISIBILITY_PARTNER_OPERATOR) ||
1094 (info->visibility & CERT_SVC_VISIBILITY_PARTNER_MANUFACTURER)) {
1095 _LOGD("VISIBILITY_PARTNER!");
1096 apptype = PERM_APP_TYPE_EFL_PARTNER;
1099 ret = _ri_privilege_enable_permissions((char *)info->package_id, apptype, perm, 1);
1101 _LOGE("_ri_privilege_enable_permissions(%s, %d) failed.", (char *)info->package_id, apptype);
1105 if (strcmp(name, EXT_APPDATA_PRIVILEGE_NAME) == 0) {
1106 _LOGD("it is EXT_APPDATA_PRIVILEGE_NAME");
1107 if(_coretpk_installer_make_directory_for_ext((char *)info->package_id) < 0) {
1108 _LOGE("make_directory_for_ext failed.");
1112 _LOGD("_ri_privilege_enable_permissions(%s, %d) succeed.", (char *)info->package_id, apptype);
1116 char *__strlwr(char *str)
1120 while(*(str+i) != '\0'){
1121 if(*(str+i) >= 65 || *(str+i)<= 90) {
1122 *(str+i) = towlower(*(str+i));
1129 static int __ri_find_xml(char *pkgid, char **rpm_xml)
1132 struct dirent entry, *result;
1136 dir = opendir(USR_SHARE_PACKAGES);
1138 if (strerror_r(errno, buf, sizeof(buf)) == 0)
1139 _LOGE("fota-info : Failed to access the [%s] because %s", USR_SHARE_PACKAGES, buf);
1143 for (ret = readdir_r(dir, &entry, &result);
1144 ret == 0 && result != NULL;
1145 ret = readdir_r(dir, &entry, &result)) {
1146 char *manifest = NULL;
1148 if (entry.d_name[0] == '.') continue;
1150 manifest = _manifest_to_package(entry.d_name);
1152 _LOGE("fota-info : Failed to convert file to xml[%s]", entry.d_name);
1156 if (strstr(manifest, __strlwr(pkgid))) {
1157 snprintf(buf, sizeof(buf), "%s/%s", USR_SHARE_PACKAGES, manifest);
1158 _LOGD("fota-info : pkgid[%s] find xml[%s]\n", pkgid, buf);
1159 *rpm_xml = strdup(buf);
1168 _LOGE("fota-info : Failed to find xml for pkgid[%s]", pkgid);
1173 static int __ri_install_fota(char *pkgid)
1176 char *manifest = NULL;
1177 char *temp[] = {"fota=true", NULL};
1179 _LOGD("fota-info : pkgid[%s] start installation\n", pkgid);
1181 /*if pkgid is one of deactivation, it does not need to install*/
1182 ret = __ri_check_pkgid_for_deactivation(pkgid);
1184 _LOGE("fota-info : pkgid[%s] for deactivation dont need to install.\n", pkgid);
1189 manifest = pkgmgr_parser_get_manifest_file(pkgid);
1190 if (manifest == NULL) {
1191 _LOGE("fota-info : dont have manefest[pkgid=%s]\n", pkgid);
1195 _LOGD("fota-info : pkgid[%s] has manefest[%s]\n", pkgid, manifest);
1197 ret = pkgmgr_parser_parse_manifest_for_installation(manifest, temp);
1199 _LOGE("fota-info : installation fail[manifest=%s]\n", manifest);
1203 _LOGD("fota-info : pkgid[%s] installation success\n", pkgid);
1205 __rpm_apply_smack(pkgid,0);
1206 __ri_make_directory(pkgid);
1208 ret = _ri_apply_privilege(pkgid, 0);
1210 _LOGE("fota-info : _ri_apply_privilege fail[pkgid=%s]\n", pkgid);
1214 _LOGD("fota-info : pkgid[%s] apply smack success\n", pkgid);
1225 static int __ri_upgrade_fota(char *pkgid)
1227 _LOGD("fota-info : pkgid[%s] start upgrade\n", pkgid);
1230 char *manifest = NULL;
1231 char *temp[] = {"fota=true", NULL};
1233 /*if pkgid is one of deactivation, it does not need to upgrade*/
1234 ret = __ri_check_pkgid_for_deactivation(pkgid);
1236 _LOGE("fota-info : pkgid[%s] for deactivation dont need to install.\n", pkgid);
1241 manifest = pkgmgr_parser_get_manifest_file(pkgid);
1242 if (manifest == NULL) {
1243 _LOGE("fota-info : dont have manefest[pkgid=%s]\n", pkgid);
1247 _LOGD("fota-info : pkgid[%s] has manefest[%s]\n", pkgid, manifest);
1248 if (access(manifest, F_OK) != 0) {
1249 _LOGE("fota-info : can not access[manifest=%s]\n", manifest);
1254 ret = __ri_find_xml(pkgid, &manifest);
1256 _LOGE("fota-info : can not find xml[pkgid=%s]\n", pkgid);
1262 if (manifest == NULL) {
1266 ret = pkgmgr_parser_parse_manifest_for_upgrade(manifest, temp);
1268 _LOGE("fota-info : upgrade fail[manifest=%s]\n", manifest);
1273 __rpm_apply_smack(pkgid,0);
1274 __ri_make_directory(pkgid);
1276 ret = _ri_apply_privilege(pkgid, 0);
1278 _LOGE("fota-info : _ri_apply_privilege fail[pkgid=%s]\n", pkgid);
1281 _LOGD("fota-info : pkgid[%s] upgrade success\n", pkgid);
1290 static int __ri_uninstall_fota(char *pkgid)
1292 _LOGD("fota-info : pkgid[%s] start uninstallation\n", pkgid);
1296 ret = _ri_privilege_unregister_package(pkgid);
1298 _LOGE("fota-info : _ri_privilege_unregister_package fail[pkgid=%s]\n", pkgid);
1301 ret = pkgmgr_parser_parse_manifest_for_uninstallation(pkgid, NULL);
1303 _LOGE("fota-info : uninstall fail[manifest=%s]\n", pkgid);
1306 _LOGD("fota-info : pkgid[%s] uninstall success\n", pkgid);
1311 static char * __getvalue(const char* pBuf, const char* pKey)
1313 const char* p = NULL;
1314 const char* pStart = NULL;
1315 const char* pEnd = NULL;
1317 p = strstr(pBuf, pKey);
1321 pStart = p + strlen(pKey) + 1;
1322 pEnd = strchr(pStart, SEPERATOR_START);
1326 size_t len = pEnd - pStart;
1330 char *pRes = (char*)malloc(len + 1);
1332 _LOGE("@malloc failed");
1335 strncpy(pRes, pStart, len);
1341 static char *__find_rpm_pkgid(const char* manifest)
1344 char buf[BUF_SIZE] = {0};
1347 fp = fopen(manifest, "r");
1349 _LOGE("csc-info : Fail get : %s\n", manifest);
1353 while (fgets(buf, BUF_SIZE, fp) != NULL) {
1355 pkgid = __getvalue(buf, TOKEN_PACKAGE_STR);
1356 if (pkgid != NULL) {
1360 memset(buf, 0x00, BUF_SIZE);
1369 static int __copy_file( const char *src_path, const char *dst_path)
1373 unsigned char temp_buf[8192] = {'\0',};
1374 size_t size_of_uchar = sizeof( unsigned char);
1375 size_t size_of_temp_buf = sizeof( temp_buf);
1377 src = fopen(src_path, "r");
1379 _LOGE("Failed to open(). path=%s, E:%d(%s)", src_path, errno, strerror(errno));
1383 dst = fopen(dst_path, "w");
1385 _LOGE("Failed to open dst file. file=%s, E:%d(%s)", dst_path, errno, strerror(errno));
1391 rc = fread( temp_buf, size_of_uchar, size_of_temp_buf, src);
1392 fwrite( temp_buf, size_of_uchar, rc, dst);
1400 static int __ri_install_csc(char *path_str, char *remove_str)
1405 char delims[] = "/";
1407 char argv[BUF_SIZE] = {'\0'};
1408 char xml_name[BUF_SIZE] = {'\0'};
1409 char src_file[BUF_SIZE] = {'\0'};
1410 char dest_file[BUF_SIZE] = {'\0'};
1412 snprintf(src_file, sizeof(src_file), "%s", path_str);
1414 /*get pkgid from path str*/
1415 pkgid = __find_rpm_pkgid(path_str);
1416 if (pkgid == NULL) {
1417 _LOGE("csc-info : fail to find pkgid\n");
1420 _LOGD("csc-info : find pkgid=[%s] for installation\n", pkgid);
1423 token = strtok(path_str, delims);
1426 memset(xml_name, 0x00, sizeof(xml_name));
1427 if (strlen(token) < BUF_SIZE)
1428 strcat(xml_name, token);
1430 _LOGE("tokenized value too long[%s]", token);
1432 token = strtok(NULL, delims);
1434 _LOGD("csc-info : xml name = %s\n", xml_name);
1436 /*copy xml to /opt/share/packages*/
1437 snprintf(dest_file, sizeof(dest_file), "%s/%s", OPT_SHARE_PACKAGES, xml_name);
1438 ret = __copy_file(src_file, dest_file);
1440 _LOGE("csc-info : xml copy fail(%d)\n", ret);
1442 _LOGE("csc-info : xml copy success to [%s] \n", dest_file);
1445 /*remove old pkg info*/
1446 ret = pkgmgr_parser_parse_manifest_for_uninstallation(pkgid, NULL);
1448 _LOGD("csc-info : fail remove old pkg info\n");
1450 _LOGD("csc-info : success remove old pkg info\n");
1453 /*insert new pkg info*/
1454 memset(argv, 0x00, sizeof(argv));
1455 snprintf(argv, sizeof(argv), "%s/%s", OPT_SHARE_PACKAGES, xml_name);
1456 ret = __ri_init_csc_xml(argv, remove_str);
1458 _LOGD("csc-info : fail insert db\n");
1460 _LOGD("csc-info : success xml name = %s\n", xml_name);
1470 static int __ri_uninstall_csc(char *pkgid)
1472 /*remove old pkg info*/
1473 int ret = pkgmgr_parser_parse_manifest_for_uninstallation(pkgid, NULL);
1475 _LOGD("csc-info : fail remove old pkg info\n");
1477 _LOGD("csc-info : success remove old pkg info\n");
1484 static int __get_size_from_xml(const char *manifest, int *size)
1486 const char *val = NULL;
1487 const xmlChar *node;
1488 xmlTextReaderPtr reader;
1489 int ret = PMINFO_R_OK;
1491 if(manifest == NULL) {
1492 _LOGE("Input argument is NULL\n");
1493 return PMINFO_R_ERROR;
1497 _LOGE("Argument supplied to hold return value is NULL\n");
1498 return PMINFO_R_ERROR;
1501 reader = xmlReaderForFile(manifest, NULL, 0);
1504 if (_child_element(reader, -1)) {
1505 node = xmlTextReaderConstName(reader);
1507 _LOGE("xmlTextReaderConstName value is NULL\n");
1508 ret = PMINFO_R_ERROR;
1512 if (!strcmp(ASCII(node), "manifest")) {
1513 ret = _ri_get_attribute(reader,"size",&val);
1515 _LOGE("@Error in getting the attribute value");
1516 ret = PMINFO_R_ERROR;
1524 _LOGE("package size is not specified\n");
1525 ret = PMINFO_R_ERROR;
1529 _LOGE("Unable to create xml reader\n");
1530 ret = PMINFO_R_ERROR;
1535 _LOGE("xmlReaderForFile value is NULL\n");
1536 ret = PMINFO_R_ERROR;
1541 xmlFreeTextReader(reader);
1545 static int __get_location_from_xml(const char *manifest, pkgmgrinfo_install_location *location)
1547 const char *val = NULL;
1548 const xmlChar *node;
1549 xmlTextReaderPtr reader;
1552 if(manifest == NULL) {
1553 _LOGE("Input argument is NULL\n");
1554 return PMINFO_R_ERROR;
1557 if(location == NULL) {
1558 _LOGE("Argument supplied to hold return value is NULL\n");
1559 return PMINFO_R_ERROR;
1562 reader = xmlReaderForFile(manifest, NULL, 0);
1565 if ( _child_element(reader, -1)) {
1566 node = xmlTextReaderConstName(reader);
1568 _LOGE("xmlTextReaderConstName value is NULL\n");
1569 xmlFreeTextReader(reader);
1570 return PMINFO_R_ERROR;
1573 if (!strcmp(ASCII(node), "manifest")) {
1574 ret = _ri_get_attribute(reader,"install-location",&val);
1576 _LOGE("@Error in getting the attribute value");
1577 xmlFreeTextReader(reader);
1578 return PMINFO_R_ERROR;
1582 if (strcmp(val, "internal-only") == 0)
1583 *location = PMINFO_INSTALL_LOCATION_INTERNAL_ONLY;
1584 else if (strcmp(val, "prefer-external") == 0)
1585 *location = PMINFO_INSTALL_LOCATION_PREFER_EXTERNAL;
1587 *location = PMINFO_INSTALL_LOCATION_AUTO;
1591 _LOGE("Unable to create xml reader\n");
1592 xmlFreeTextReader(reader);
1593 return PMINFO_R_ERROR;
1597 _LOGE("xmlReaderForFile value is NULL\n");
1598 return PMINFO_R_ERROR;
1601 xmlFreeTextReader(reader);
1606 static char *__get_pkg_path(const char *pkg_path, const char *pkgid)
1609 char buff[BUF_SIZE] = {'\0'};
1610 char *real_path = NULL;
1612 snprintf(buff, BUF_SIZE, "%s/%s", pkg_path, pkgid);
1614 if (__is_dir(buff)) break;
1615 memset(buff, '\0', BUF_SIZE);
1616 snprintf(buff, BUF_SIZE, "%s/%s", USR_APPS,pkgid);
1617 if (__is_dir(buff)) break;
1618 memset(buff, '\0', BUF_SIZE);
1619 snprintf(buff, BUF_SIZE, "/opt/apps/%s", pkgid);
1620 if (__is_dir(buff)) break;
1621 memset(buff, '\0', BUF_SIZE);
1622 snprintf(buff, BUF_SIZE, "%s/%s", OPT_USR_APPS,pkgid);
1623 if (__is_dir(buff)) break;
1628 _LOGE("chdir(%s) failed. [%s]", buff, strerror(errno));
1632 real_path = (char *)malloc(strlen(buff) + 1);
1633 if (real_path == NULL) {
1634 _LOGE("malloc() failed.");
1637 memset(real_path, '\0', strlen(buff) + 1);
1638 memcpy(real_path, buff, strlen(buff));
1643 static int __set_label_for_pkg_dir(const char *dirpath, const char* label)
1647 struct dirent entry;
1648 struct dirent *result;
1649 char fullpath[BUF_SIZE] = {'\0'};
1651 if (access(dirpath, F_OK) != 0) {
1652 _LOGE("skip! empty dirpath = [%s]", dirpath);
1656 dir = opendir(dirpath);
1658 _LOGE("opendir(%s) failed. [%d][%s]", dirpath, errno, strerror(errno));
1662 if(lsetxattr(dirpath, "security.SMACK64", label, strlen(label), 0)) {
1663 _LOGE("error(%d) in setting smack label",errno);
1666 for (ret = readdir_r(dir, &entry, &result); ret == 0 && result != NULL; ret = readdir_r(dir, &entry, &result)){
1667 if (!strcmp(entry.d_name, ".") ||
1668 !strcmp(entry.d_name, "..")) {
1673 snprintf(fullpath, BUF_SIZE, "%s/%s", dirpath, entry.d_name);
1674 if(lsetxattr(fullpath, "security.SMACK64", label, strlen(label), 0)) {
1675 _LOGE("error(%d) in setting smack label",errno);
1680 if (entry.d_type == DT_DIR) {
1681 ret = __set_label_for_pkg_dir(fullpath,label);
1683 _LOGE("_coretpk_installer_apply_directory_policy(%s) failed.", fullpath);
1686 memset(fullpath, '\0', BUF_SIZE);
1695 void _ri_soft_reset(char *pkgid)
1698 char dirpath[PKG_STRING_LEN_MAX] = {'\0'};
1701 snprintf(dirpath, PKG_STRING_LEN_MAX, "%s/%s", OPT_USR_APPS, pkgid);
1702 if (access(dirpath, F_OK)==0) {
1703 __set_label_for_pkg_dir(dirpath, "_");
1707 memset(dirpath, '\0', PKG_STRING_LEN_MAX);
1708 snprintf(dirpath, PKG_STRING_LEN_MAX, "%s/%s/data", OPT_USR_APPS, pkgid);
1709 if (access(dirpath, F_OK)==0) {
1710 __set_label_for_pkg_dir(dirpath, pkgid);
1714 memset(dirpath, '\0', PKG_STRING_LEN_MAX);
1715 snprintf(dirpath, PKG_STRING_LEN_MAX, "%s/%s/shared", OPT_USR_APPS, pkgid);
1716 if (access(dirpath, F_OK)==0) {
1717 __set_label_for_pkg_dir(dirpath, "_");
1721 memset(dirpath, '\0', PKG_STRING_LEN_MAX);
1722 snprintf(dirpath, PKG_STRING_LEN_MAX, "%s/%s/shared/res", OPT_USR_APPS, pkgid);
1723 if (access(dirpath, F_OK)==0) {
1724 __set_label_for_pkg_dir(dirpath, "_");
1728 memset(dirpath, '\0', PKG_STRING_LEN_MAX);
1729 snprintf(dirpath, PKG_STRING_LEN_MAX, "%s/%s/shared/data", OPT_USR_APPS, pkgid);
1730 if (access(dirpath, F_OK)==0) {
1731 __set_label_for_pkg_dir(dirpath, "_");
1735 memset(dirpath, '\0', PKG_STRING_LEN_MAX);
1736 snprintf(dirpath, PKG_STRING_LEN_MAX, "%s/%s/shared/trusted", OPT_USR_APPS, pkgid);
1737 if (access(dirpath, F_OK)==0) {
1738 pkgmgrinfo_pkginfo_h handle = NULL;
1739 char *groupid = NULL;
1740 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
1741 if (ret != PMINFO_R_OK)
1743 ret = pkgmgrinfo_pkginfo_get_groupid(handle, &groupid);
1744 if (ret == PMINFO_R_OK) {
1745 _ri_privilege_setup_path(pkgid, dirpath, APP_PATH_GROUP_RW, groupid);
1747 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
1753 void _ri_register_cert(const char *pkgid)
1756 pkgmgrinfo_instcertinfo_h handle = NULL;
1759 error = pkgmgrinfo_create_certinfo_set_handle(&handle);
1761 _LOGE("Cert handle creation failed. Err:%d", error);
1762 __ri_free_cert_chain();
1766 if (list[SIG_AUTH].cert_value == NULL) {
1767 _LOGE("pkgid[%s] dont have SIG_AUTH.cert_value ", pkgid);
1771 for (i = 0; i < MAX_CERT_NUM; i++) {
1773 if (list[i].cert_value) {
1774 error = pkgmgrinfo_set_cert_value(handle, list[i].cert_type, list[i].cert_value);
1776 _LOGE("pkgmgrinfo_set_cert_value failed. cert type:%d. Err:%d", list[i].cert_type, error);
1781 /* Save the certificates in cert DB*/
1782 error = pkgmgrinfo_save_certinfo(pkgid, handle);
1784 _LOGE("pkgmgrinfo_save_certinfo failed. Err:%d", error);
1789 pkgmgrinfo_destroy_certinfo_set_handle(handle);
1790 __ri_free_cert_chain();
1793 void _ri_unregister_cert(const char *pkgid)
1796 /* Delete the certifictes from cert DB*/
1797 error = pkgmgrinfo_delete_certinfo(pkgid);
1799 _LOGE("pkgmgrinfo_delete_certinfo failed. Err:%d", error);
1804 int _ri_verify_sig_and_cert(const char *sigfile, int *visibility)
1806 char certval[MAX_BUFF_LEN] = {'\0'};
1811 int ret = RPM_INSTALLER_SUCCESS;
1813 signature_x *signx = NULL;
1814 struct keyinfo_x *keyinfo = NULL;
1815 struct x509data_x *x509data = NULL;
1816 CERT_CONTEXT *ctx = NULL;
1819 ctx = cert_svc_cert_context_init();
1821 _LOGE("cert_svc_cert_context_init() failed.");
1822 return RPM_INSTALLER_ERR_INTERNAL;
1825 if (strstr(sigfile, AUTHOR_SIGNATURE_XML))
1827 else if (strstr(sigfile, SIGNATURE1_XML))
1828 sigtype = SIG_DIST1;
1829 else if (strstr(sigfile, SIGNATURE2_XML))
1830 sigtype = SIG_DIST2;
1832 _LOGE("Unsupported signature type! [%s]", sigfile);
1833 cert_svc_cert_context_final(ctx);
1834 return RPM_INSTALLER_ERR_INTERNAL;
1837 signx = _ri_process_signature_xml(sigfile);
1838 if (signx == NULL) {
1839 _LOGE("_ri_process_signature_xml(%s) failed.", sigfile);
1840 ret = RPM_INSTALLER_ERR_INTERNAL;
1844 keyinfo = signx->keyinfo;
1845 if ((keyinfo == NULL) || (keyinfo->x509data == NULL) || (keyinfo->x509data->x509certificate == NULL)) {
1846 _LOGE("keyinfo is invalid. [%s]", sigfile);
1847 ret = RPM_INSTALLER_ERR_CERT_INVALID;
1851 x509data = keyinfo->x509data;
1852 x509certificate_x *cert = x509data->x509certificate;
1854 // First cert is Signer certificate
1855 if (cert->text != NULL) {
1856 for (i = 0; i <= (int)strlen(cert->text); i++) {
1857 if (cert->text[i] != '\n') {
1858 certval[j++] = cert->text[i];
1863 err = cert_svc_load_buf_to_context(ctx, (unsigned char*)certval);
1865 _LOGE("cert_svc_load_buf_to_context() failed. cert = [%s], err = [%d]", certval, err);
1866 ret = RPM_INSTALLER_ERR_INTERNAL;
1869 // _LOGD("SIG_SIGNER certval = [%d][%s]", strlen(certval), certval);
1871 err = __ri_create_cert_chain(sigtype, SIG_SIGNER, certval);
1873 _LOGE("__ri_create_cert_chain() failed. sigtype = [%d], certval = [%s]", sigtype, certval);
1874 __ri_free_cert_chain();
1875 ret = RPM_INSTALLER_ERR_INTERNAL;
1880 // Second cert is Intermediate certificate
1882 if (cert->text != NULL) {
1883 memset(certval, 0x00, MAX_BUFF_LEN);
1885 for (i = 0; i <= (int)strlen(cert->text); i++) {
1886 if (cert->text[i] != '\n') {
1887 certval[j++] = cert->text[i];
1892 if (cert->text != NULL) {
1893 err = cert_svc_push_buf_into_context(ctx, (unsigned char*)certval);
1895 _LOGE("cert_svc_push_buf_into_context() failed. cert = [%s], err = [%d]", certval, err);
1896 ret = RPM_INSTALLER_ERR_INTERNAL;
1900 // _LOGD("SIG_INTERMEDIATE certval = [%d][%s]", strlen(certval), certval);
1902 err = __ri_create_cert_chain(sigtype, SIG_INTERMEDIATE, certval);
1904 _LOGE("__ri_create_cert_chain() failed. sigtype = [%d], certval = [%s]", sigtype, certval);
1905 __ri_free_cert_chain();
1906 ret = RPM_INSTALLER_ERR_INTERNAL;
1910 _LOGE("Invalid CertChain! (cert->text is NULL.)");
1911 ret = RPM_INSTALLER_ERR_CERT_INVALID;
1915 err = cert_svc_verify_certificate(ctx, &validity);
1917 _LOGE("cert_svc_verify_certificate() failed.");
1918 ret = RPM_INSTALLER_ERR_INTERNAL;
1921 _LOGD("cert_svc_verify() is done successfully. validity=[%d]", validity);
1923 if (validity == 0) {
1924 _LOGE("Certificate Invalid/Expired (validity == 0)");
1925 ret = RPM_INSTALLER_ERR_CERTCHAIN_VERIFICATION_FAILED;
1930 // For reference validation, we should be in TEMP_DIR/usr/apps/<pkgid>
1931 if (ctx->fileNames && ctx->fileNames->filename) {
1932 _LOGD("Root CA cert path=[%s]", ctx->fileNames->filename);
1934 err = __ri_xmlsec_verify_signature(sigfile, ctx->fileNames->filename);
1936 _LOGE("__ri_xmlsec_verify_signature(%s) failed.", sigfile);
1937 ret = RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED;
1941 crt = __ri_get_cert_from_file(ctx->fileNames->filename);
1942 err = __ri_create_cert_chain(sigtype, SIG_ROOT, crt);
1944 _LOGE("__ri_create_cert_chain(%d) failed.", sigtype);
1945 __ri_free_cert_chain();
1946 ret = RPM_INSTALLER_ERR_INTERNAL;
1950 err = cert_svc_get_visibility(ctx, visibility);
1952 _LOGE("cert_svc_get_visibility() failed. err = [%d]", err);
1953 ret = RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED;
1956 _LOGD("cert_svc_get_visibility() returns visibility=[%d]", *visibility);
1958 _LOGE("No Root CA cert found. Signature validation failed.");
1959 ret = RPM_INSTALLER_ERR_ROOT_CERT_NOT_FOUND;
1966 cert_svc_cert_context_final(ctx);
1968 _ri_free_signature_xml(signx);
1973 int _ri_verify_signatures(const char *root_path, const char *pkgid)
1976 char buff[BUF_SIZE] = {'\0'};
1977 char *pkg_path = NULL;
1980 _LOGD("root_path=[%s], pkgid=[%s]", root_path, pkgid);
1982 // check for signature and certificate
1983 pkg_path = __get_pkg_path(root_path, pkgid);
1984 if (pkg_path == NULL) {
1985 _LOGE("__get_pkg_path(%s, %s) failed.", root_path, pkgid);
1989 _LOGD("switched to pkg_path=[%s]", pkg_path);
1991 // author-signature.xml is mandatory
1992 snprintf(buff, BUF_SIZE, "%s/author-signature.xml", pkg_path);
1993 if (access(buff, F_OK) == 0) {
1994 _LOGD("author-signature.xml, path=[%s]", buff);
1995 ret = _ri_verify_sig_and_cert(buff, &visibility);
1997 _LOGE("_ri_verify_sig_and_cert(%s) failed.", buff);
2001 _LOGD("------------------------------------------------------");
2002 _LOGD("signature is verified successfully");
2003 _LOGD("path=[%s]", buff);
2004 _LOGD("------------------------------------------------------");
2006 memset(buff, '\0', BUF_SIZE);
2008 // signature2.xml is optional
2009 snprintf(buff, BUF_SIZE, "%s/signature2.xml", pkg_path);
2010 if (access(buff, F_OK) == 0) {
2011 _LOGD("signature2.xml found. [%s]", pkg_path);
2012 ret = _ri_verify_sig_and_cert(buff, &visibility);
2014 _LOGE("_ri_verify_sig_and_cert(%s) failed.", buff);
2018 _LOGD("_ri_verify_sig_and_cert(%s) succeed.", buff);
2020 memset(buff, '\0', BUF_SIZE);
2022 // signature1.xml is mandatory
2023 snprintf(buff, BUF_SIZE, "%s/signature1.xml", pkg_path);
2024 if (access(buff, F_OK) == 0) {
2025 _LOGD("signature1.xml, path=[%s]", buff);
2026 ret = _ri_verify_sig_and_cert(buff, &visibility);
2028 _LOGE("_ri_verify_sig_and_cert(%s) failed.", buff);
2032 _LOGD("------------------------------------------------------");
2033 _LOGD("signature is verified successfully");
2034 _LOGD("path=[%s]", buff);
2035 _LOGD("------------------------------------------------------");
2037 memset(buff, '\0', BUF_SIZE);
2047 if ((ret != 0) && (sig_enable == 0)) {
2048 _LOGD("_ri_verify_signatures(%s, %s) failed, but it's ok for config.", root_path, pkgid);
2055 void _ri_apply_smack(char *pkgname, int flag)
2057 __rpm_apply_smack(pkgname, flag);
2060 int _ri_apply_privilege(char *pkgid, int visibility)
2063 pkgmgrinfo_pkginfo_h handle = NULL;
2065 int apptype = PERM_APP_TYPE_EFL;
2068 _LOGE("pkgid is NULL");
2072 memset(&info, '\0', sizeof(info));
2074 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
2075 if (ret != PMINFO_R_OK)
2078 if (strlen(pkgid) < PKG_MAX_LEN)
2079 strcpy(info.package_id, pkgid);
2081 _LOGE("pkgid too long[%s]", pkgid);
2083 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
2087 info.visibility = visibility;
2089 ret = pkgmgrinfo_pkginfo_foreach_privilege(handle, __privilege_func, (void *)&info);
2090 if (ret != PMINFO_R_OK) {
2091 _LOGE("pkgmgrinfo_pkginfo_get_pkgid failed\n");
2092 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
2095 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
2097 if (visibility & CERT_SVC_VISIBILITY_PLATFORM) {
2098 _LOGD("VISIBILITY_PLATFORM!");
2099 apptype = PERM_APP_TYPE_EFL_PLATFORM;
2100 } else if ((visibility & CERT_SVC_VISIBILITY_PARTNER) ||
2101 (visibility & CERT_SVC_VISIBILITY_PARTNER_OPERATOR) ||
2102 (visibility & CERT_SVC_VISIBILITY_PARTNER_MANUFACTURER)) {
2103 _LOGD("VISIBILITY_PARTNER!");
2104 apptype = PERM_APP_TYPE_EFL_PARTNER;
2107 /*reload privilege*/
2108 const char *perm[] = {NULL, NULL};
2109 ret = _ri_privilege_enable_permissions(pkgid, apptype, perm, 1);
2114 int _ri_set_group_id(const char *pkgid, const char *groupid)
2116 retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "pkgid is NULL\n");
2117 retvm_if(groupid == NULL, PMINFO_R_EINVAL, "groupid is NULL\n");
2119 sqlite3 *pkginfo_db = NULL;
2123 ret = db_util_open(PKGMGR_DB, &pkginfo_db, 0);
2124 retvm_if(ret != SQLITE_OK, PMINFO_R_ERROR, "connect db [%s] failed!", PKGMGR_DB);
2126 /*Begin transaction*/
2127 ret = sqlite3_exec(pkginfo_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2128 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Failed to begin transaction\n");
2129 _LOGD("Transaction Begin\n");
2131 query = sqlite3_mprintf("update package_info set package_reserve3=%Q where package=%Q", groupid, pkgid);
2133 ret = sqlite3_exec(pkginfo_db, query, NULL, NULL, NULL);
2134 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
2136 /*Commit transaction*/
2137 ret = sqlite3_exec(pkginfo_db, "COMMIT", NULL, NULL, NULL);
2138 if (ret != SQLITE_OK) {
2139 _LOGE("Failed to commit transaction. Rollback now\n");
2140 ret = sqlite3_exec(pkginfo_db, "ROLLBACK", NULL, NULL, NULL);
2141 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
2143 _LOGD("Transaction Commit and End\n");
2147 sqlite3_free(query);
2148 sqlite3_close(pkginfo_db);
2152 static int __ri_install_fota_for_rw(char *pkgid)
2156 char buff[BUF_SIZE] = {'\0'};
2158 _LOGD("fota-info : pkgid[%s] start installation\n", pkgid);
2160 /*unzip pkg path from factoryrest data*/
2161 snprintf(buff, BUF_SIZE, "opt/usr/apps/%s/*", pkgid);
2162 const char *pkg_argv[] = { "/usr/bin/unzip", "-oX", OPT_ZIP_FILE, buff, "-d", "/", NULL };
2163 ret = _ri_xsystem(pkg_argv);
2165 _LOGE("fota-info : unzip root path[%s] is fail .\n", buff);
2169 _LOGD("fota-info : unzip root path[%s] is success\n", buff);
2171 /*unzip manifest from factoryrest data*/
2172 memset(buff, '\0', BUF_SIZE);
2173 snprintf(buff, BUF_SIZE, "opt/share/packages/%s.xml", pkgid);
2175 const char *xml_argv[] = { "/usr/bin/unzip", "-oX", OPT_ZIP_FILE, buff, "-d", "/", NULL };
2176 ret = _ri_xsystem(xml_argv);
2178 _LOGE("fota-info : xml_argv fail for pkgid[%s] .\n", pkgid);
2182 _LOGD("fota-info : xml_argv[%s] is success\n", pkgid);
2184 /*get updated manifest path*/
2185 memset(buff, '\0', BUF_SIZE);
2186 snprintf(buff, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
2187 _LOGE("Manifest name is %s\n", buff);
2189 /*apply smack for manifest*/
2190 _ri_privilege_change_smack_label(buff, pkgid, 0);/*0 is SMACK_LABEL_ACCESS*/
2192 /*register manifest*/
2193 char* fota_tags[3] = {NULL, };
2194 fota_tags[0] = "removable=true";
2195 fota_tags[1] = "preload=true";
2196 fota_tags[2] = NULL;
2198 ret = pkgmgr_parser_parse_manifest_for_installation(buff, fota_tags);
2200 _LOGE("Parsing Manifest Failed\n");
2204 _LOGD("Parsing Manifest Success\n");
2207 /*Register cert info*/
2208 _ri_register_cert(pkgid);
2210 /*apply smack for pkg root path*/
2211 memset(buff, '\0', BUF_SIZE);
2212 snprintf(buff, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgid);
2213 _ri_privilege_setup_path(pkgid, buff, PERM_APP_PATH_ANY_LABEL, pkgid);
2215 /*apply smack for defined directory*/
2216 __rpm_apply_smack(pkgid, home_dir);
2219 ret = _ri_apply_privilege(pkgid, 0);
2221 _LOGE("apply perm failed with err(%d)\n", ret);
2223 _LOGD("apply perm success\n");
2231 static int __ri_upgrade_fota_for_rw(char *pkgid)
2235 char buff[BUF_SIZE] = {'\0'};
2237 _LOGD("fota-info : pkgid[%s] start upgrade\n", pkgid);
2239 /*unzip pkg dir from factoryrest data*/
2240 snprintf(buff, BUF_SIZE, "opt/usr/apps/%s/*", pkgid);
2241 const char *pkg_argv[] = { "/usr/bin/unzip", "-oX", OPT_ZIP_FILE, buff, "-d", "/", NULL };
2242 ret = _ri_xsystem(pkg_argv);
2244 _LOGE("fota-info : unzip root path[%s] is fail .\n", buff);
2248 _LOGD("fota-info : unzip root path[%s] is success\n", buff);
2250 /*unzip manifest from factoryrest data*/
2251 memset(buff, '\0', BUF_SIZE);
2252 snprintf(buff, BUF_SIZE, "opt/share/packages/%s.xml", pkgid);
2253 const char *xml_argv[] = { "/usr/bin/unzip", "-oX", OPT_ZIP_FILE, buff, "-d", "/", NULL };
2254 ret = _ri_xsystem(xml_argv);
2256 _LOGE("fota-info : unzip manifest[%s] is fail .\n", buff);
2260 _LOGD("fota-info : unzip manifest[%s] is success\n", buff);
2262 /*get updated manifest path*/
2263 memset(buff, '\0', BUF_SIZE);
2264 snprintf(buff, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
2265 _LOGE("Manifest name is %s\n", buff);
2267 /*apply smack for manifest*/
2268 _ri_privilege_change_smack_label(buff, pkgid, 0);/*0 is SMACK_LABEL_ACCESS*/
2270 /*register manifest*/
2271 ret = pkgmgr_parser_parse_manifest_for_upgrade(buff, NULL);
2273 _LOGE("Parsing Manifest Failed\n");
2277 _LOGD("Parsing Manifest Success\n");
2280 /*Register new cert info*/
2281 _ri_unregister_cert(pkgid);
2282 _ri_register_cert(pkgid);
2284 /*apply smack for pkg root path*/
2285 memset(buff, '\0', BUF_SIZE);
2286 snprintf(buff, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgid);
2287 _ri_privilege_setup_path(pkgid, buff, PERM_APP_PATH_ANY_LABEL, pkgid);
2289 /*apply smack for defined directory*/
2290 __rpm_apply_smack(pkgid, home_dir);
2293 ret = _ri_apply_privilege(pkgid, 0);
2295 _LOGE("apply perm failed with err(%d)\n", ret);
2297 _LOGD("apply perm success\n");
2304 static int __ri_uninstall_fota_for_rw(char *pkgid)
2307 char buff[BUF_SIZE] = {'\0'};
2309 _LOGD("fota-info : pkgid[%s] start uninstall\n", pkgid);
2311 /*del root path dir*/
2312 snprintf(buff, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgid);
2314 if (__is_dir(buff)) {
2315 _rpm_delete_dir(buff);
2319 memset(buff, '\0', BUF_SIZE);
2320 snprintf(buff, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
2324 ret = pkgmgr_parser_parse_manifest_for_uninstallation(pkgid, NULL);
2326 _LOGE("Parsing Manifest Failed\n");
2329 /*execute privilege APIs*/
2330 _ri_privilege_revoke_permissions(pkgid);
2331 _ri_privilege_unregister_package(pkgid);
2333 /*Unregister cert info*/
2334 _ri_unregister_cert(pkgid);
2340 * callback for the pkgmgrinfo_appinfo_get_list used in _rpm_uninstall_pkg()
2342 int __ri_check_running_app(const pkgmgrinfo_appinfo_h handle, void *user_data)
2347 app_context_h appCtx = NULL;
2349 ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
2351 _LOGE("Failed to execute pkgmgrinfo_appinfo_get_appid[%d].\n", ret);
2355 ret = app_manager_is_running(appid, &isRunning);
2357 _LOGE("Failed to execute app_manager_is_running[%d].\n", ret);
2360 _LOGE("app[%s] , running state[%d].\n", appid, isRunning);
2363 ret = app_manager_get_app_context(appid, &appCtx);
2365 _LOGE("Failed to execute app_manager_get_app_context[%d].\n", ret);
2369 ret = app_manager_terminate_app(appCtx);
2371 _LOGE("Failed to execute app_manager_terminate_app[%d].\n", ret);
2372 app_context_destroy(appCtx);
2377 for (i = 0; i < TERMINATE_RETRY_COUNT; i++) {
2378 ret = app_manager_is_running(appid, &isRunning);
2380 _LOGE("Failed to execute app_manager_is_running[%d].\n", ret);
2381 app_context_destroy(appCtx);
2386 _LOGD("App(%s) is terminated.\n", appid);
2389 _LOGD("App(%s) is not terminated yet. wait count=[%d].\n", appid, i);
2394 ret = app_context_destroy(appCtx);
2396 _LOGE("Failed to execute app_context_destroy[%d].\n", ret);
2404 int __ri_change_dir(char *dirname)
2408 ret = mkdir(dirname, 0644);
2410 if (access(dirname, F_OK) == 0) {
2411 _rpm_delete_dir(dirname);
2412 ret = mkdir(dirname, 0644);
2414 _LOGE("mkdir(%s) failed\n", dirname);
2418 _LOGE("can not access[%s]\n", dirname);
2423 ret = chdir(dirname);
2425 _LOGE("chdir(%s) failed [%s]\n", dirname, strerror(errno));
2431 int _ri_smack_reload(const char *pkgid, rpm_request_type request_type)
2434 char *op_type = NULL;
2436 switch (request_type) {
2438 op_type = strdup("install");
2442 op_type = strdup("update");
2446 op_type = strdup("uninstall");
2453 if(op_type == NULL) {
2454 _LOGE("@Failed to reload smack. request_type not matched[pkgid=%s, op=%s]", pkgid, op_type);
2458 const char *smack_argv[] = { "/usr/bin/smack_reload.sh", op_type, pkgid, NULL };
2459 ret = _ri_xsystem(smack_argv);
2461 _LOGE("@Failed to reload smack[pkgid=%s, op=%s].", pkgid, op_type);
2463 _LOGD("#success: smack reload[pkgid=%s, op=%s]", pkgid, op_type);
2472 int _ri_smack_reload_all(void)
2476 const char *smack_argv[] = { "/usr/bin/smackload-fast", NULL};
2477 ret = _ri_xsystem(smack_argv);
2479 _LOGE("@Failed to reload all smack : %d", errno);
2481 _LOGD("#success: smack reload all");
2487 void __ri_remove_updated_dir(const char *pkgid)
2489 char path_buf[BUF_SIZE] = {'\0'};
2492 if (pkgid == NULL) {
2493 _LOGE("pkgid is NULL.");
2498 snprintf(path_buf, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, BIN_DIR_STR);
2499 if (__is_dir(path_buf)) {
2500 _LOGE("@pkgid[%s] need to clean dir[%s]\n", pkgid, path_buf);
2501 _rpm_delete_dir(path_buf);
2505 memset(path_buf, '\0', BUF_SIZE);
2506 snprintf(path_buf, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, RES_DIR_STR);
2507 if (__is_dir(path_buf)) {
2508 _LOGE("@pkgid[%s] need to clean dir[%s]\n", pkgid, path_buf);
2509 _rpm_delete_dir(path_buf);
2512 // remove shared/res dir
2513 memset(path_buf, '\0', BUF_SIZE);
2514 snprintf(path_buf, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, SHARED_RES_DIR_STR);
2515 if (__is_dir(path_buf)) {
2516 _LOGE("@pkgid[%s] need to clean dir[%s]\n", pkgid, path_buf);
2517 _rpm_delete_dir(path_buf);
2521 static int __metadata_func(const char *key, const char *value, void *user_data)
2527 _LOGE("key is null\n");
2530 if (value == NULL) {
2531 _LOGE("value is null\n");
2534 if (user_data == NULL) {
2535 _LOGE("user_data is null\n");
2539 if ((strcmp(key, "launch-on-attach") == 0) && (strcmp(value, "true") == 0)) {
2540 _LOGE("consumer[%s] : launch-on-attach is true \n", (char *)user_data);
2542 ret = app_manager_is_running((char *)user_data, &isRunning);
2544 _LOGE("Failed to execute app_manager_is_running[%s].\n", (char *)user_data);
2549 _LOGE("consumer[%s] is already launched \n", (char *)user_data);
2551 usleep(100 * 1000); /* 100ms sleep for infomation ready*/
2552 ret = aul_launch_app((char *)user_data, NULL);
2553 if (ret == AUL_R_ERROR) {
2554 _LOGE("consumer[%s] launch fail, sleep and retry launch_app\n", (char *)user_data);
2555 usleep(100 * 1000); /* 100ms sleep for infomation ready*/
2556 aul_launch_app((char *)user_data, NULL);
2558 _LOGE("consumer[%s] is launched !!!! \n", (char *)user_data);
2564 static int __ri_find_svcapp(const pkgmgrinfo_appinfo_h handle, void *user_data)
2568 char *component_type = NULL;
2570 ret = pkgmgrinfo_appinfo_get_component_type(handle, &component_type);
2571 if (ret != PMINFO_R_OK) {
2572 _LOGE("@Failed to get component_type\n");
2576 if (strcmp(component_type, "svcapp") == 0) {
2577 ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
2578 if (ret != PMINFO_R_OK) {
2579 _LOGE("@Failed to get appid\n");
2582 _LOGE("@find consumer[%s], check metadata for launch\n", appid);
2584 ret = pkgmgrinfo_appinfo_foreach_metadata(handle, __metadata_func, (void *)appid);
2585 if (ret != PMINFO_R_OK) {
2586 _LOGE("@Failed to get foreach_metadata\n");
2594 static void __ri_launch_consumer(const char *pkgid)
2597 pkgmgrinfo_pkginfo_h pkghandle = NULL;
2599 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkghandle);
2601 _LOGE("@Failed to get pkginfo handle [%s]\n", pkgid);
2605 ret = pkgmgrinfo_appinfo_get_list(pkghandle, PM_UI_APP, __ri_find_svcapp, NULL);
2607 _LOGE("@Failed to get appinfo_get_list [%s]\n", pkgid);
2610 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
2613 static int __ail_change_info(int op, const char *appid)
2615 void *lib_handle = NULL;
2616 int (*ail_desktop_operation) (const char *);
2620 if ((lib_handle = dlopen(LIBAIL_PATH, RTLD_LAZY)) == NULL) {
2621 _LOGE("dlopen is failed LIBAIL_PATH[%s]\n", LIBAIL_PATH);
2628 aop = "ail_desktop_add";
2631 aop = "ail_desktop_update";
2634 aop = "ail_desktop_remove";
2637 aop = "ail_desktop_clean";
2640 aop = "ail_desktop_fota";
2647 if ((ail_desktop_operation =
2648 dlsym(lib_handle, aop)) == NULL || dlerror() != NULL) {
2649 _LOGE("can not find symbol \n");
2654 ret = ail_desktop_operation(appid);
2658 dlclose(lib_handle);
2663 static int __ri_update_ail_info(const pkgmgrinfo_appinfo_h handle, void *user_data)
2668 ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
2670 _LOGE("Failed to execute pkgmgrinfo_appinfo_get_appid[%d].\n", ret);
2674 ret = __ail_change_info(AIL_INSTALL, appid);
2676 _LOGE("Failed to execute __ail_change_info[%s].\n", appid);
2682 static int __child_list_cb (const pkgmgrinfo_pkginfo_h handle, void *user_data)
2687 ret = pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid);
2689 _LOGE("get_pkgid failed\n");
2693 _LOGD("@child pkgid is [%s] for uninstallation", pkgid);
2695 ret = _rpm_uninstall_pkg_with_dbpath(pkgid, 0);
2697 _LOGE("uninstall pkg(%s) failed\n",pkgid );
2703 static void __uninstall_child_package_by_mother_pkgid(const char *pkgid)
2706 pkgmgrinfo_pkginfo_filter_h handle = NULL;
2708 ret = pkgmgrinfo_pkginfo_filter_create(&handle);
2710 _LOGE("filter_create failed for (%s)\n", pkgid);
2714 ret = pkgmgrinfo_pkginfo_filter_add_string(handle, PMINFO_PKGINFO_PROP_PACKAGE_STORECLIENT_ID, pkgid);
2716 _LOGE("PMINFO_PKGINFO_PROP_PACKAGE_STORECLIENT_ID add failed\n");
2720 ret = pkgmgrinfo_pkginfo_filter_foreach_pkginfo(handle, __child_list_cb, NULL);
2722 _LOGE("foreach_pkginfo failed\n");
2726 pkgmgrinfo_pkginfo_filter_destroy(handle);
2729 int _rpm_install_pkg_with_dbpath(char *pkgfilepath, char *pkgid, char *clientid)
2732 char manifest[BUF_SIZE] = { '\0'};
2733 char resultxml[BUF_SIZE] = {'\0'};
2734 char cwd[BUF_SIZE] = {'\0'};
2737 #ifdef APP2EXT_ENABLE
2738 app2ext_handle *handle = NULL;
2739 GList *dir_list = NULL;
2740 pkgmgrinfo_install_location location = 1;
2742 unsigned long rpm_size = 0;
2744 /*send event for start*/
2745 _ri_broadcast_status_notification(pkgid, "rpm", "start", "install");
2746 _LOGD("[#]start : _rpm_install_pkg_with_dbpath");
2749 temp = getcwd(cwd, BUF_SIZE);
2750 if ((temp == NULL) ||(cwd[0] == '\0')) {
2751 _LOGE("@failed to get the current directory info.");
2752 ret = RPM_INSTALLER_ERR_INTERNAL;
2755 _LOGD("#current working directory is %s", cwd);
2758 ret = __ri_change_dir(TEMP_DIR);
2760 _LOGE("@failed to change directory.");
2761 ret = RPM_INSTALLER_ERR_INTERNAL;
2764 _LOGD("#switched to %s", TEMP_DIR);
2767 const char *cpio_argv[] = { CPIO_SCRIPT, pkgfilepath, NULL };
2768 ret = _ri_xsystem(cpio_argv);
2770 /*get manifext.xml path*/
2771 snprintf(manifest, BUF_SIZE, "%s/opt/share/packages/%s.xml", TEMP_DIR, pkgid);
2772 _LOGD("#manifest name is %s", manifest);
2774 if (access(manifest, F_OK)) {
2775 _LOGD("#there is no RW manifest.xml. check RO manifest.xml.");
2777 memset(manifest, '\0', sizeof(manifest));
2778 snprintf(manifest, BUF_SIZE, "%s/usr/share/packages/%s.xml", TEMP_DIR, pkgid);
2779 _LOGD("#manifest name is %s", manifest);
2781 if (access(manifest, F_OK)) {
2782 _LOGE("@can not find manifest.xml in the pkg.");
2783 ret = RPM_INSTALLER_ERR_NO_MANIFEST;
2789 #if 0 //disable "copy ro-xml to rw-xml", because of some bug
2790 snprintf(srcpath, BUF_SIZE, "%s", manifest);
2791 memset(manifest, '\0', sizeof(manifest));
2792 snprintf(manifest, BUF_SIZE, "%s/%s.xml", MANIFEST_RW_DIRECTORY, pkgid);
2794 const char *xml_update_argv[] = { CPIO_SCRIPT_UPDATE_XML, srcpath, manifest, NULL };
2795 ret = _ri_xsystem(xml_update_argv);
2800 /*send event for install_percent*/
2801 _ri_broadcast_status_notification(pkgid, "rpm", "install_percent", "30");
2803 /*check manifest.xml validation*/
2804 ret = pkgmgr_parser_check_manifest_validation(manifest);
2806 _LOGE("@invalid manifest");
2807 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
2811 /*check for signature and certificate*/
2812 ret = _ri_verify_signatures(TEMP_DIR, pkgid);
2814 _LOGE("@signature and certificate failed(%s).", pkgid);
2815 ret = RPM_INSTALLER_ERR_SIG_INVALID;
2818 _LOGD("#_ri_verify_signatures success.");
2823 _LOGE("@failed to change directory(%s)(%s)", cwd, strerror(errno));
2824 ret = RPM_INSTALLER_ERR_INTERNAL;
2828 #ifdef APP2EXT_ENABLE
2829 ret = __get_location_from_xml(manifest, &location);
2831 _LOGE("@Failed to get install location\n");
2832 ret = RPM_INSTALLER_ERR_INTERNAL;
2835 if (location == PMINFO_INSTALL_LOCATION_PREFER_EXTERNAL) {
2836 _LOGD("#Install: external storage location");
2838 /*Get the rpm's size from rpm header*/
2839 rpm_size = _ri_calculate_rpm_size(pkgfilepath);
2841 rpm_size = rpm_size/(1024*1024); //rpm size in MB
2842 _LOGD("#Rpm file(%s) size is %lu MB",pkgfilepath,rpm_size);
2844 /*Add margin to the rpm size*/
2845 rpm_size = rpm_size + RPM_SIZE_MARGIN(rpm_size);
2846 _LOGD("#Rpm file (%s) size after margin is %lu MB",pkgfilepath,rpm_size);
2848 _LOGE("@Failed to get size from rpm header\n");
2849 ret = RPM_INSTALLER_ERR_INTERNAL;
2854 Get the size from the manifest file.
2856 ret = __get_size_from_xml(manifest, &size);
2857 if (ret != PMINFO_R_OK) {
2859 _LOGD(" #rpm size is %d MB",size);
2861 size = size>rpm_size?size:rpm_size;
2862 _LOGD("#rpm size is %d MB",size);
2867 if ((location == PMINFO_INSTALL_LOCATION_PREFER_EXTERNAL) && size > 0) {
2868 handle = app2ext_init(APP2EXT_SD_CARD);
2869 if (handle == NULL) {
2870 _LOGE("@app2ext init failed\n");
2871 ret = RPM_INSTALLER_ERR_INTERNAL;
2874 if ((&(handle->interface) != NULL) && (handle->interface.pre_install != NULL) && (handle->interface.post_install != NULL)){
2875 dir_list = __rpm_populate_dir_list();
2876 if (dir_list == NULL) {
2877 _LOGE("@ \nError in populating the directory list\n");
2878 app2ext_deinit(handle);
2879 ret = RPM_INSTALLER_ERR_RPM_SCRIPT_WRONG_ARGS;
2882 ret = handle->interface.pre_install(gpkgname, dir_list, size);
2883 if (ret == APP2EXT_ERROR_MMC_STATUS) {
2884 _LOGE("@app2xt MMC is not here, go internal\n");
2885 } else if (ret == APP2EXT_SUCCESS){
2886 _LOGE("@pre_install done, go internal\n");
2889 _LOGE("@app2xt pre install API failed (%d)\n", ret);
2890 __rpm_clear_dir_list(dir_list);
2891 handle->interface.post_install(gpkgname, APP2EXT_STATUS_FAILED);
2892 app2ext_deinit(handle);
2893 ret = RPM_INSTALLER_ERR_INTERNAL;
2902 if (home_dir == 0) {
2903 #if 0 //disable "INSTALL_SCRIPT_WITH_DBPATH_RO", because of some bug
2904 const char *argv[] = { INSTALL_SCRIPT_WITH_DBPATH_RO, pkgfilepath, NULL };
2905 ret = _ri_xsystem(argv);
2907 const char *argv[] = { INSTALL_SCRIPT, pkgfilepath, NULL };
2908 ret = _ri_xsystem(argv);
2910 const char *argv[] = { INSTALL_SCRIPT_WITH_DBPATH_RW, pkgfilepath, NULL };
2911 ret = _ri_xsystem(argv);
2914 _LOGE("@failed to install the pkg(%d).", ret);
2915 #ifdef APP2EXT_ENABLE
2916 if ((handle != NULL) && (handle->interface.post_install != NULL)){
2917 __rpm_clear_dir_list(dir_list);
2918 handle->interface.post_install(gpkgname, APP2EXT_STATUS_FAILED);
2919 app2ext_deinit(handle);
2924 _LOGD("#install success.");
2926 /*write the storeclient-id to manifest.xml*/
2927 if (clientid != NULL) {
2928 if (home_dir == 0) {
2929 snprintf(resultxml, BUF_SIZE, "%s/%s.xml", USR_SHARE_PACKAGES, pkgid);
2931 snprintf(resultxml, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
2934 const char *convert_argv[] = { RPM_UPDATE_XML, manifest, clientid, resultxml, NULL };
2935 ret = _ri_xsystem(convert_argv);
2937 _LOGE("@Failed to convert the manifest.xml");
2941 _LOGD("#client id[%s], input manifest:[%s], dest manifest:[%s]", clientid, manifest, resultxml);
2944 /*send event for install_percent*/
2945 _ri_broadcast_status_notification(pkgid, "rpm", "install_percent", "60");
2947 #ifdef APP2EXT_ENABLE
2948 if ((handle != NULL) && (handle->interface.post_install != NULL)){
2949 __rpm_clear_dir_list(dir_list);
2950 handle->interface.post_install(gpkgname, APP2EXT_STATUS_SUCCESS);
2951 app2ext_deinit(handle);
2955 /*Parse the manifest to get install location and size. If installation fails, remove manifest info from DB*/
2956 if (clientid != NULL) {
2957 ret = pkgmgr_parser_parse_manifest_for_installation(resultxml, NULL);
2959 ret = pkgmgr_parser_parse_manifest_for_installation(manifest, NULL);
2962 _LOGE("@failed to parse the manifest.");
2963 ret = RPM_INSTALLER_ERR_INTERNAL;
2966 _LOGD("#manifest parsing success");
2968 /*register cert info*/
2969 _ri_register_cert(pkgid);
2972 _coretpk_installer_search_ui_gadget(pkgid);
2974 /*apply smack to shared dir*/
2975 __rpm_apply_smack(pkgid, 1);
2977 /*apply smack by privilege*/
2978 ret = _ri_apply_privilege(pkgid, 0);
2980 _LOGE("@failed to apply permission(%d).", ret);
2982 _LOGD("#permission applying success.");
2985 ret = _ri_smack_reload_all();
2987 _LOGD("@failed to reload_all the smack.");
2990 /*send event for install_percent*/
2991 _ri_broadcast_status_notification(pkgid, "rpm", "install_percent", "100");
2994 _rpm_delete_dir(TEMP_DIR);
2995 _rpm_delete_dir(TEMP_DBPATH);
2997 if (ret == RPM_INSTALLER_SUCCESS) {
2998 _LOGD("[#]end : _rpm_install_pkg_with_dbpath");
2999 __ri_launch_consumer(pkgid);
3000 _ri_broadcast_status_notification(pkgid, "rpm", "end", "ok");
3001 _ri_stat_cb(pkgid, "end", "ok");
3003 _LOGE("[@]end : _rpm_install_pkg_with_dbpath");
3005 ret = _coretpk_installer_remove_db_info(pkgid);
3007 _LOGE("_coretpk_installer_remove_db_info is failed.");
3010 char *errstr = NULL;
3011 _ri_error_no_to_string(ret, &errstr);
3012 _ri_broadcast_status_notification(pkgid, "rpm", "error", errstr);
3013 _ri_stat_cb(pkgid, "error", errstr);
3014 _ri_broadcast_status_notification(pkgid, "rpm", "end", "fail");
3016 _ri_stat_cb(pkgid, "end", "fail");
3017 _LOGE("install failed with err(%d) (%s)\n", ret, errstr);
3023 int _rpm_upgrade_pkg_with_dbpath(char *pkgfilepath, char *pkgid)
3026 char manifest[BUF_SIZE] = { '\0'};
3027 char cwd[BUF_SIZE] = {'\0'};
3029 pkgmgrinfo_pkginfo_h pkghandle;
3031 #ifdef APP2EXT_ENABLE
3032 app2ext_handle *handle = NULL;
3033 GList *dir_list = NULL;
3034 pkgmgrinfo_installed_storage location = 1;
3036 unsigned long rpm_size = 0;
3038 _ri_broadcast_status_notification(pkgid, "rpm", "start", "update");
3039 _LOGD("[#]start : _rpm_upgrade_pkg_with_dbpath");
3041 /*terminate running app*/
3042 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkghandle);
3044 _LOGE("@failed to get pkginfo handle");
3045 ret = RPM_INSTALLER_ERR_PKG_NOT_FOUND;
3048 pkgmgrinfo_appinfo_get_list(pkghandle, PM_UI_APP, __ri_check_running_app, NULL);
3049 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
3052 temp = getcwd(cwd, BUF_SIZE);
3053 if ((temp == NULL) ||(cwd[0] == '\0')) {
3054 _LOGE("@getcwd() failed.");
3055 ret = RPM_INSTALLER_ERR_INTERNAL;
3058 _LOGD("#current working directory is %s.", cwd);
3061 ret = __ri_change_dir(TEMP_DIR);
3063 _LOGE("@change dir failed.");
3064 ret = RPM_INSTALLER_ERR_INTERNAL;
3067 _LOGD("#switched to %s", TEMP_DIR);
3070 const char *cpio_argv[] = { CPIO_SCRIPT, pkgfilepath, NULL };
3071 ret = _ri_xsystem(cpio_argv);
3073 /*get manifext.xml path*/
3074 snprintf(manifest, BUF_SIZE, "%s/opt/share/packages/%s.xml", TEMP_DIR, pkgid);
3075 _LOGD("#manifest name is %s.", manifest);
3077 if (access(manifest, F_OK)) {
3078 _LOGD("#there is no RW manifest.xml. check RO manifest.xml.");
3080 memset(manifest, '\0', sizeof(manifest));
3081 snprintf(manifest, BUF_SIZE, "%s/usr/share/packages/%s.xml", TEMP_DIR, pkgid);
3082 _LOGD("#manifest name is %s.", manifest);
3084 if (access(manifest, F_OK)) {
3085 _LOGE("@can not find manifest.xml in the pkg.");
3086 ret = RPM_INSTALLER_ERR_NO_MANIFEST;
3092 #if 0 //disable "copy ro-xml to rw-xml", because of some bug
3093 snprintf(srcpath, BUF_SIZE, "%s", manifest);
3094 memset(manifest, '\0', sizeof(manifest));
3095 snprintf(manifest, BUF_SIZE, "%s/%s.xml", MANIFEST_RW_DIRECTORY, pkgid);
3097 const char *xml_update_argv[] = { CPIO_SCRIPT_UPDATE_XML, srcpath, manifest, NULL };
3098 ret = _ri_xsystem(xml_update_argv);
3104 /*send event for install_percent*/
3105 _ri_broadcast_status_notification(pkgid, "rpm", "install_percent", "30");
3107 /*check manifest.xml validation*/
3108 ret = pkgmgr_parser_check_manifest_validation(manifest);
3110 _LOGE("@invalid manifest");
3111 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
3115 /*check for signature and certificate*/
3116 ret = _ri_verify_signatures(TEMP_DIR, pkgid);
3118 _LOGE("@signature and certificate failed(%s).", pkgid);
3119 ret = RPM_INSTALLER_ERR_SIG_INVALID;
3122 _LOGD("#_ri_verify_signatures success.");
3127 _LOGE("@chdir(%s) failed(%s).", cwd, strerror(errno));
3128 ret = RPM_INSTALLER_ERR_INTERNAL;
3132 /*remove dir for clean*/
3133 __ri_remove_updated_dir(pkgid);
3135 _LOGD("#Preserve the smack file");
3136 /*Preserve the smack rule file */
3137 ret = __ri_copy_smack_rule_file(UPGRADE_REQ,pkgid,0);
3138 if(ret != RPM_INSTALLER_SUCCESS)
3140 #ifdef APP2EXT_ENABLE
3141 ret = pkgmgrinfo_pkginfo_get_pkginfo(gpkgname, &pkghandle);
3143 _LOGE("Failed to get pkginfo handle\n");
3144 ret = RPM_INSTALLER_ERR_PKG_NOT_FOUND;
3147 ret = pkgmgrinfo_pkginfo_get_installed_storage(pkghandle, &location);
3149 _LOGE("Failed to get install location\n");
3150 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
3151 ret = RPM_INSTALLER_ERR_INTERNAL;
3154 if (location == PMINFO_EXTERNAL_STORAGE) {
3155 /*Get the rpm's size from rpm header*/
3156 rpm_size = _ri_calculate_rpm_size(pkgfilepath);
3158 rpm_size = rpm_size/(1024*1024); //rpm size in MB
3159 _LOGD("#Rpm file(%s) size is %lu MB",pkgfilepath,rpm_size);
3161 /*Add margin to the rpm size*/
3162 rpm_size = rpm_size + RPM_SIZE_MARGIN(rpm_size);
3163 _LOGD("#Rpm file (%s) size after margin is %lu MB",pkgfilepath,rpm_size);
3165 _LOGE("@Failed to get size from rpm header\n");
3166 ret = RPM_INSTALLER_ERR_INTERNAL;
3171 Get the size from the manifest file.
3173 ret = __get_size_from_xml(manifest, &size);
3174 if (ret != PMINFO_R_OK) {
3176 _LOGD(" #rpm size is %d",size);
3178 size = size>rpm_size?size:rpm_size;
3179 _LOGD("#rpm size is %d",size);
3183 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
3184 if ((location == PMINFO_EXTERNAL_STORAGE) && size > 0) {
3185 handle = app2ext_init(APP2EXT_SD_CARD);
3186 if (handle == NULL) {
3187 _LOGE("app2ext init failed\n");
3188 ret = RPM_INSTALLER_ERR_INTERNAL;
3191 if ((&(handle->interface) != NULL) && (handle->interface.pre_upgrade != NULL) && (handle->interface.post_upgrade != NULL)){
3192 dir_list = __rpm_populate_dir_list();
3193 if (dir_list == NULL) {
3194 _LOGE("\nError in populating the directory list\n");
3195 ret = RPM_INSTALLER_ERR_RPM_SCRIPT_WRONG_ARGS;
3196 app2ext_deinit(handle);
3199 ret = handle->interface.pre_upgrade(gpkgname, dir_list, size);
3200 if (ret == APP2EXT_ERROR_MMC_STATUS ) {
3201 _LOGE("app2xt MMC is not here, go internal (%d)\n", ret);
3202 }else if(ret == APP2EXT_SUCCESS){
3203 _LOGE("pre upgrade done, go internal");
3205 _LOGE("app2xt pre upgrade API failed (%d)\n", ret);
3206 __rpm_clear_dir_list(dir_list);
3207 handle->interface.post_upgrade(gpkgname, APP2EXT_STATUS_FAILED);
3208 ret = RPM_INSTALLER_ERR_INTERNAL;
3209 app2ext_deinit(handle);
3218 if (home_dir == 0) {
3219 #if 0 //disable "UPGRADE_SCRIPT_WITH_DBPATH_RO", because of some bug
3220 const char *argv[] = { UPGRADE_SCRIPT_WITH_DBPATH_RO, pkgfilepath, NULL };
3221 ret = _ri_xsystem(argv);
3223 const char *argv[] = { UPGRADE_SCRIPT, pkgfilepath, NULL };
3224 ret = _ri_xsystem(argv);
3226 const char *argv[] = { UPGRADE_SCRIPT_WITH_DBPATH_RW, pkgfilepath, NULL };
3227 ret = _ri_xsystem(argv);
3230 _LOGE("@upgrade complete with error(%d)", ret);
3231 #ifdef APP2EXT_ENABLE
3232 if ((handle != NULL) && (handle->interface.post_upgrade != NULL)){
3233 __rpm_clear_dir_list(dir_list);
3234 handle->interface.post_upgrade(gpkgname, APP2EXT_STATUS_FAILED);
3235 app2ext_deinit(handle);
3240 _LOGD("#upgrade script success.");
3242 /*send event for install_percent*/
3243 _ri_broadcast_status_notification(pkgid, "rpm", "install_percent", "60");
3245 /*Parse the manifest to get install location and size. If fails, remove manifest info from DB.*/
3246 ret = pkgmgr_parser_parse_manifest_for_upgrade(manifest, NULL);
3248 _LOGE("@parsing manifest failed.");
3249 ret = RPM_INSTALLER_ERR_INTERNAL;
3252 _LOGD("#parsing manifest success.");
3254 /*unregister cert info*/
3255 _ri_unregister_cert(pkgid);
3257 /*register cert info*/
3258 _ri_register_cert(pkgid);
3260 #ifdef APP2EXT_ENABLE
3261 if ((handle != NULL) && (handle->interface.post_upgrade != NULL)){
3262 __rpm_clear_dir_list(dir_list);
3263 handle->interface.post_upgrade(gpkgname, APP2EXT_STATUS_SUCCESS);
3264 app2ext_deinit(handle);
3269 _coretpk_installer_search_ui_gadget(pkgid);
3271 /*apply smack to shared dir*/
3272 __rpm_apply_smack(pkgid, 1);
3274 /*apply smack by privilege*/
3275 ret = _ri_apply_privilege(pkgid, 0);
3277 _LOGE("@apply perm failed with err(%d)", ret);
3279 _LOGD("#apply perm success.");
3282 ret = _ri_smack_reload_all();
3284 _LOGD("_ri_smack_reload_all failed.");
3287 /*send event for install_percent*/
3288 _ri_broadcast_status_notification(pkgid, "rpm", "install_percent", "100");
3291 _rpm_delete_dir(TEMP_DIR);
3292 _rpm_delete_dir(TEMP_DBPATH);
3294 if (ret == RPM_INSTALLER_SUCCESS) {
3295 _LOGD("[#]end : _rpm_upgrade_pkg_with_dbpath");
3296 __ri_launch_consumer(pkgid);
3297 _ri_broadcast_status_notification(pkgid, "rpm", "end", "ok");
3298 _ri_stat_cb(pkgid, "end", "ok");
3300 _LOGE("[@]end : _rpm_upgrade_pkg_with_dbpath");
3301 char *errstr = NULL;
3302 _ri_error_no_to_string(ret, &errstr);
3303 _ri_broadcast_status_notification(pkgid, "rpm", "error", errstr);
3304 _ri_stat_cb(pkgid, "error", errstr);
3305 _ri_broadcast_status_notification(pkgid, "rpm", "end", "fail");
3307 _ri_stat_cb(pkgid, "end", "fail");
3308 _LOGE("install failed with err(%d) (%s)\n", ret, errstr);
3314 int _rpm_uninstall_pkg_with_dbpath(const char *pkgid, bool is_system)
3316 if (pkgid == NULL) {
3317 _LOGE("pkgid is NULL.");
3322 char buff[BUF_SIZE] = {'\0'};
3323 char tizen_manifest[BUF_SIZE] = {'\0'};
3324 pkgmgrinfo_pkginfo_h pkghandle = NULL;
3325 bool mother_package = false;
3326 bool coretpk = false;
3328 #ifdef APP2EXT_ENABLE
3329 app2ext_handle *handle = NULL;
3330 pkgmgrinfo_installed_storage location = 1;
3333 _LOGD("pkgid=[%s], is_system=[%d]", pkgid, is_system);
3335 snprintf(tizen_manifest, BUF_SIZE, "%s/%s/tizen-manifest.xml", OPT_USR_APPS, pkgid);
3336 if (access(tizen_manifest, R_OK) == 0) {
3338 _LOGD("[%s] is existed.", tizen_manifest);
3343 _ri_broadcast_status_notification(pkgid, coretpk ? "coretpk" : "rpm", "start", "update");
3345 _ri_broadcast_status_notification(pkgid, coretpk ? "coretpk" : "rpm", "start", "uninstall");
3347 // terminate running app
3348 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkghandle);
3350 _LOGE("pkgmgrinfo_pkginfo_get_pkginfo(%s) failed.", pkgid);
3351 ret = RPM_INSTALLER_ERR_PKG_NOT_FOUND;
3354 pkgmgrinfo_appinfo_get_list(pkghandle, PM_UI_APP, __ri_check_running_app, NULL);
3356 // If package is mother package, then uninstall child package
3357 pkgmgrinfo_pkginfo_is_mother_package(pkghandle, &mother_package);
3358 if (mother_package == true) {
3359 _LOGD("[%s] is mother package", pkgid);
3360 __uninstall_child_package_by_mother_pkgid(pkgid);
3362 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
3364 snprintf(tizen_manifest, BUF_SIZE, "%s/%s/tizen-manifest.xml", OPT_USR_APPS, pkgid);
3365 if (access(tizen_manifest, R_OK) == 0) {
3367 _LOGD("[%s] is existing.", tizen_manifest);
3370 // del root path dir
3371 snprintf(buff, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgid);
3372 if (__is_dir(buff)) {
3373 _rpm_delete_dir(buff);
3376 // del root path dir for ext
3377 if (_coretpk_installer_get_configuration_value(INI_VALUE_MAKE_EXT_DIRECTORY)) {
3378 char extpath[BUF_SIZE] = {'\0'};
3379 snprintf(extpath, BUF_SIZE, "%s/%s", OPT_STORAGE_SDCARD_APP_ROOT, pkgid);
3380 if (__is_dir(extpath)) {
3381 _rpm_delete_dir(extpath);
3386 memset(buff, '\0', BUF_SIZE);
3387 snprintf(buff, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
3390 // check system pkg, if pkg is system pkg, need to update xml on USR_SHARE_PACKAGES
3392 memset(buff, '\0', BUF_SIZE);
3393 snprintf(buff, BUF_SIZE, "%s/%s.xml", USR_SHARE_PACKAGES, pkgid);
3394 _LOGE("manifest for upgrade, path=[%s]",buff);
3395 ret = pkgmgr_parser_parse_manifest_for_upgrade(buff, NULL);
3397 _LOGE("parsing manifest failed.");
3401 #ifdef APP2EXT_ENABLE
3402 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkghandle);
3404 _LOGE("failed to get pkginfo handle");
3405 ret = RPM_INSTALLER_ERR_PKG_NOT_FOUND;
3408 ret = pkgmgrinfo_pkginfo_get_installed_storage(pkghandle, &location);
3410 _LOGE("failed to get install location\n");
3411 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
3412 return RPM_INSTALLER_ERR_INTERNAL;
3415 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
3416 if (location == PMINFO_EXTERNAL_STORAGE) {
3417 handle = app2ext_init(APP2EXT_SD_CARD);
3418 if (handle == NULL) {
3419 _LOGE("app2ext init failed\n");
3420 return RPM_INSTALLER_ERR_INTERNAL;
3422 if ((&(handle->interface) != NULL) && (handle->interface.pre_uninstall != NULL) && (handle->interface.post_uninstall != NULL) &&
3423 (handle->interface.disable != NULL)){
3424 ret = handle->interface.disable(pkgid);
3425 if (ret != APP2EXT_SUCCESS) {
3426 _LOGE("Unmount ret[%d]", ret);
3428 ret = app2ext_get_app_location(pkgid);
3429 if (ret == APP2EXT_INTERNAL_MEM){
3430 _LOGE("app2xt APP is not in MMC, go internal (%d)\n", ret);
3433 ret = handle->interface.pre_uninstall(pkgid);
3434 if (ret == APP2EXT_ERROR_MMC_STATUS ) {
3435 _LOGE("app2xt MMC is not here, go internal (%d)\n", ret);
3436 }else if (ret == APP2EXT_SUCCESS){
3437 _LOGE("pre uninstall done, go to internal");
3439 _LOGE("app2xt pre uninstall API failed (%d)\n", ret);
3440 handle->interface.post_uninstall(pkgid);
3441 app2ext_deinit(handle);
3442 return RPM_INSTALLER_ERR_INTERNAL;
3450 ret = pkgmgr_parser_parse_manifest_for_uninstallation(pkgid, NULL);
3452 _LOGE("pkgmgr_parser_parse_manifest_for_uninstallation() failed, pkgid=[%s]", pkgid);
3455 #ifdef APP2EXT_ENABLE
3456 if ((handle != NULL) && (handle->interface.post_uninstall != NULL)){
3457 handle->interface.post_uninstall(pkgid);
3458 app2ext_deinit(handle);
3464 // execute privilege APIs
3465 _ri_privilege_revoke_permissions(pkgid);
3466 _ri_privilege_unregister_package(pkgid);
3468 // Unregister cert info
3469 _ri_unregister_cert(pkgid);
3472 // Restore the old smack file
3473 if (coretpk == false) {
3474 ret = __ri_copy_smack_rule_file(UNINSTALL_REQ,pkgid,is_system);
3475 if (ret != RPM_INSTALLER_SUCCESS){
3476 _LOGD("smack restore failed");
3479 ret = _ri_smack_reload_all();
3481 _LOGD("_ri_smack_reload_all failed.");
3487 _LOGE("failed, ret=[%d]", ret);
3488 char *errstr = NULL;
3489 _ri_error_no_to_string(ret, &errstr);
3490 _ri_broadcast_status_notification(pkgid, coretpk ? "coretpk" : "rpm", "error", errstr);
3491 _ri_stat_cb(pkgid, "error", errstr);
3493 _ri_broadcast_status_notification(pkgid, coretpk ? "coretpk" : "rpm", "end", "fail");
3494 _ri_stat_cb(pkgid, "end", "fail");
3495 _LOGE("remove failed with err(%d) (%s)\n", ret, errstr);
3498 _ri_broadcast_status_notification(pkgid, coretpk ? "coretpk" : "rpm", "end", "ok");
3499 _ri_stat_cb(pkgid, "end", "ok");
3505 int _rpm_uninstall_pkg(char *pkgid)
3511 bool is_removable = 0;
3512 char buff[BUF_SIZE] = {'\0'};
3513 pkgmgrinfo_install_location location = 1;
3514 #ifdef APP2EXT_ENABLE
3515 app2ext_handle *handle = NULL;
3517 char *manifest = NULL;
3518 pkgmgrinfo_pkginfo_h pkghandle;
3519 const char *argv[] = { UNINSTALL_SCRIPT, pkgid, NULL };
3521 _LOGD("start : _rpm_uninstall_pkg\n");
3523 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkghandle);
3525 _LOGE("Failed to get pkginfo handle\n");
3526 return RPM_INSTALLER_ERR_PKG_NOT_FOUND;
3528 /*terminate running app*/
3529 pkgmgrinfo_appinfo_get_list(pkghandle, PM_UI_APP, __ri_check_running_app, NULL);
3531 ret = pkgmgrinfo_pkginfo_is_system(pkghandle, &is_system);
3533 _LOGE("pkgmgrinfo_pkginfo_is_system failed.\n");
3534 return RPM_INSTALLER_ERR_INTERNAL;
3537 ret = pkgmgrinfo_pkginfo_is_update(pkghandle, &is_update);
3539 _LOGE("pkgmgrinfo_pkginfo_is_system failed.\n");
3540 return RPM_INSTALLER_ERR_INTERNAL;
3543 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
3544 /*updated and system pkg need to "remove-update"*/
3545 _LOGD("Remove Update[%s]",pkgid);
3546 ret = _rpm_uninstall_pkg_with_dbpath(pkgid, 1);
3548 _LOGE("uninstall_pkg_with_dbpath for system, is_update fail\n");
3553 pkgmgrinfo_pkginfo_is_removable(pkghandle, &is_removable);
3555 /*non-system and can be removable, it should be deleted*/
3556 _LOGD("Delete Package [%s]",pkgid);
3557 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
3558 ret = _rpm_uninstall_pkg_with_dbpath(pkgid, 0);
3560 _LOGE("uninstall_pkg_with_dbpath for non-system, is_remove fail\n");
3566 _ri_broadcast_status_notification(pkgid, "rpm", "start", "uninstall");
3568 #ifdef APP2EXT_ENABLE
3569 ret = pkgmgrinfo_pkginfo_get_install_location(pkghandle, &location);
3571 _LOGE("Failed to get install location\n");
3572 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
3573 return RPM_INSTALLER_ERR_INTERNAL;
3576 pkgmgrinfo_appinfo_get_list(pkghandle, PM_UI_APP, __ri_check_running_app, NULL);
3578 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
3579 if (location == PMINFO_INSTALL_LOCATION_PREFER_EXTERNAL) {
3580 handle = app2ext_init(APP2EXT_SD_CARD);
3581 if (handle == NULL) {
3582 _LOGE("app2ext init failed\n");
3583 return RPM_INSTALLER_ERR_INTERNAL;
3585 if ((&(handle->interface) != NULL) && (handle->interface.pre_uninstall != NULL) && (handle->interface.post_uninstall != NULL)){
3586 ret = app2ext_get_app_location(pkgid);
3587 if (ret == APP2EXT_INTERNAL_MEM){
3588 _LOGE("app2xt APP is not in MMC, go internal (%d)\n", ret);
3591 ret = handle->interface.pre_uninstall(pkgid);
3592 if (ret == APP2EXT_ERROR_MMC_STATUS || ret == APP2EXT_SUCCESS ) {
3593 _LOGE("app2xt MMC is not here, go internal (%d)\n", ret);
3596 _LOGE("app2xt pre uninstall API failed (%d)\n", ret);
3597 handle->interface.post_uninstall(pkgid);
3598 app2ext_deinit(handle);
3599 return RPM_INSTALLER_ERR_INTERNAL;
3606 #ifdef PRE_CHECK_FOR_MANIFEST
3607 /*Manifest info should be removed first because after installation manifest
3608 file is uninstalled. If uninstallation fails, we need to re-insert manifest info for consistency*/
3609 manifest = pkgmgr_parser_get_manifest_file(pkgid);
3610 if (manifest == NULL) {
3611 _LOGE("manifest name is NULL\n");
3612 app2ext_deinit(handle);
3613 return RPM_INSTALLER_ERR_NO_MANIFEST;
3615 _LOGD("manifest name is %s\n", manifest);
3616 ret = pkgmgr_parser_parse_manifest_for_uninstallation(manifest, NULL);
3618 _LOGE("pkgmgr_parser_parse_manifest_for_uninstallation failed.\n");
3623 ret = _rpm_xsystem(argv);
3625 _LOGE("uninstall failed with error(%d)\n", ret);
3626 #ifdef PRE_CHECK_FOR_MANIFEST
3627 err = pkgmgr_parser_parse_manifest_for_installation(manifest, NULL);
3629 _LOGE("Parsing Manifest Failed\n");
3636 #ifdef APP2EXT_ENABLE
3637 if ((handle != NULL) && (handle->interface.post_uninstall != NULL)){
3638 handle->interface.post_uninstall(pkgid);
3639 app2ext_deinit(handle);
3645 #ifdef APP2EXT_ENABLE
3646 if ((handle != NULL) && (handle->interface.post_uninstall != NULL)){
3647 handle->interface.post_uninstall(pkgid);
3648 app2ext_deinit(handle);
3652 #ifdef PRE_CHECK_FOR_MANIFEST
3658 /* Uninstallation Success. Remove the installation time key from vconf*/
3659 snprintf(buff, BUF_SIZE, "db/app-info/%s/installed-time", pkgid);
3660 err = vconf_unset(buff);
3662 _LOGE("unset installation time failed\n");
3664 /*execute privilege APIs*/
3665 _ri_privilege_revoke_permissions(pkgid);
3666 _ri_privilege_unregister_package(pkgid);
3667 /*Unregister cert info*/
3668 _ri_unregister_cert(gpkgname);
3670 _LOGD("end : _rpm_uninstall_pkg(%d)\n", ret);
3674 int _rpm_install_corexml(char *pkgfilepath, char *pkgid)
3677 /*validate signature and certifictae*/
3678 ret = _ri_verify_signatures(USR_APPS, pkgid);
3680 _LOGE("_ri_verify_signatures Failed : %s\n", pkgid);
3681 ret = RPM_INSTALLER_ERR_SIG_INVALID;
3685 /* check : given pkgid is deactivation*/
3686 ret = __ri_check_pkgid_for_deactivation(pkgid);
3688 _LOGE("pkgid[%s] for deactivation dont need to install.\n", pkgid);
3692 /* Parse and insert manifest in DB*/
3693 ret = pkgmgr_parser_parse_manifest_for_installation(pkgfilepath, NULL);
3695 _LOGD("Installing Manifest Failed : %s\n", pkgfilepath);
3696 ret = RPM_INSTALLER_ERR_PACKAGE_NOT_INSTALLED;
3700 // _ri_register_cert has __ri_free_cert_chain.
3701 _ri_register_cert(pkgid);
3704 _coretpk_installer_search_ui_gadget(pkgid);
3706 ret = RPM_INSTALLER_SUCCESS;
3710 __ri_free_cert_chain();
3717 int _rpm_move_pkg(char *pkgid, int move_type)
3719 app2ext_handle *hdl = NULL;
3722 GList *dir_list = NULL;
3723 pkgmgrinfo_pkginfo_h pkghandle = NULL;
3725 if (move_type == PM_MOVE_TO_INTERNAL)
3726 movetype = APP2EXT_MOVE_TO_PHONE;
3727 else if (move_type == PM_MOVE_TO_SDCARD)
3728 movetype = APP2EXT_MOVE_TO_EXT;
3730 return RPM_INSTALLER_ERR_WRONG_PARAM;
3732 ret = pkgmgrinfo_pkginfo_get_pkginfo(gpkgname,&pkghandle);
3734 _LOGE("@failed to get the pkginfo handle!!");
3735 ret = RPM_INSTALLER_ERR_PKG_NOT_FOUND;
3738 /* Terminate the running instance of app */
3739 pkgmgrinfo_appinfo_get_list(pkghandle,PM_UI_APP,__ri_check_running_app,NULL);
3740 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
3741 hdl = app2ext_init(APP2EXT_SD_CARD);
3742 if ((hdl != NULL) && (hdl->interface.move != NULL)){
3743 dir_list = __rpm_populate_dir_list();
3744 if (dir_list == NULL) {
3745 _LOGE("\nError in populating the directory list\n");
3746 return RPM_INSTALLER_ERR_RPM_SCRIPT_WRONG_ARGS;
3748 ret = hdl->interface.move(pkgid, dir_list, movetype);
3749 __rpm_clear_dir_list(dir_list);
3751 _LOGE("Failed to move app\n");
3752 return RPM_INSTALLER_ERR_INTERNAL;
3754 if(move_type == PM_MOVE_TO_INTERNAL){
3755 _LOGD("#updating the installed storage from external to internal");
3756 ret = pkgmgrinfo_pkginfo_set_installed_storage(pkgid, INSTALL_INTERNAL);
3759 _LOGD("#updating the installed storage from internal to external");
3760 ret = pkgmgrinfo_pkginfo_set_installed_storage(pkgid, INSTALL_EXTERNAL);
3763 if(ret != PMINFO_R_OK){
3764 _LOGE("@failed to udpate the installed storage");
3765 return RPM_INSTALLER_ERR_INTERNAL;
3768 app2ext_deinit(hdl);
3769 return RPM_INSTALLER_SUCCESS;
3771 _LOGE("Failed to get app2ext handle\n");
3772 return RPM_INSTALLER_ERR_INTERNAL;
3776 int _rpm_process_cscxml(char *csc_script)
3781 char *path_str = NULL;
3782 char *op_str = NULL;
3783 char *remove_str = NULL;
3784 char csc_str[BUF_SIZE] = {'\0'};
3785 snprintf(csc_str, BUF_SIZE - 1, "%s:", csc_script);
3787 /*get params from csc script*/
3788 path_str = __ri_get_str(csc_str, TOKEN_PATH_STR);
3789 op_str = __ri_get_str(csc_str, TOKEN_OPERATION_STR);
3790 remove_str = __ri_get_str(csc_str, TOKEN_REMOVE_STR);
3791 if((path_str == NULL) || (op_str == NULL) || (remove_str == NULL)){
3792 _LOGE("csc-info : input param is null[%s, %s, %s]\n", path_str, op_str, remove_str);
3795 _LOGD("csc-info : path=%s, op=%s, remove=%s\n", path_str, op_str, remove_str);
3797 /*get operation type*/
3798 op_type = __ri_get_op_type(op_str);
3800 _LOGE("csc-info : operation error[%s, %s]\n", path_str, op_str);
3806 ret = __ri_install_csc(path_str, remove_str);
3810 ret = __ri_install_csc(path_str, remove_str);
3814 ret = __ri_uninstall_csc(path_str);
3822 _LOGE("fota-info : Fota fail [pkgid=%s, operation=%d]\n",path_str, op_type);
3835 int _rpm_process_csc_coretpk(char *csc_script)
3840 char *path_str = NULL;
3841 char *op_str = NULL;
3842 char *remove_str = NULL;
3843 char csc_str[BUF_SIZE] = {'\0'};
3844 snprintf(csc_str, BUF_SIZE - 1, "%s:", csc_script);
3846 /*get params from csc script*/
3847 path_str = __ri_get_str(csc_str, TOKEN_PATH_STR);
3848 op_str = __ri_get_str(csc_str, TOKEN_OPERATION_STR);
3849 remove_str = __ri_get_str(csc_str, TOKEN_REMOVE_STR);
3850 if((path_str == NULL) || (op_str == NULL) || (remove_str == NULL)){
3851 _LOGE("csc-info : input param is null[%s, %s, %s]\n", path_str, op_str, remove_str);
3854 _LOGD("csc-info : path=%s, op=%s, remove=%s\n", path_str, op_str, remove_str);
3856 /*get operation type*/
3857 op_type = __ri_get_op_type(op_str);
3859 _LOGE("csc-info : operation error[%s, %s]\n", path_str, op_str);
3865 ret = _coretpk_installer_csc_install(path_str, remove_str);
3869 ret = _coretpk_installer_csc_install(path_str, remove_str);
3873 ret = __ri_uninstall_csc(path_str);
3881 _LOGE("csc-info : csc fail [pkgid=%s, operation=%d]\n",path_str, op_type);
3894 int _rpm_process_fota(char *fota_script)
3899 char *op_str = NULL;
3901 char csc_str[BUF_SIZE] = {'\0'};
3902 snprintf(csc_str, BUF_SIZE - 1, "%s:", fota_script);
3904 /*get params from fota script*/
3905 pkgid = __ri_get_str(csc_str, TOKEN_PATH_STR);
3906 op_str = __ri_get_str(csc_str, TOKEN_OPERATION_STR);
3907 if((pkgid == NULL) || (op_str == NULL)){
3908 _LOGE("fota-info : input param is null[%s, %s]\n", pkgid, op_str);
3911 _LOGD("fota-info : path=%s, op=%s\n", pkgid, op_str);
3913 /*get operation type*/
3914 op_type = __ri_get_op_type(op_str);
3916 _LOGE("fota-info : operation error[%s, %s]\n", pkgid, op_str);
3922 ret = __ri_install_fota(pkgid);
3926 ret = __ri_upgrade_fota(pkgid);
3930 ret = __ri_uninstall_fota(pkgid);
3938 _LOGE("fota-info : Fota fail [pkgid=%s, operation=%d]\n",pkgid, op_type);
3949 int _rpm_process_fota_for_rw(char *fota_script)
3954 char *op_str = NULL;
3956 char fota_str[BUF_SIZE] = {'\0'};
3957 snprintf(fota_str, BUF_SIZE - 1, "%s:", fota_script);
3959 /*get params from fota script*/
3960 pkgid = __ri_get_str(fota_str, TOKEN_PATH_STR);
3961 op_str = __ri_get_str(fota_str, TOKEN_OPERATION_STR);
3962 if((pkgid == NULL) || (op_str == NULL)){
3963 _LOGE("fota-info : input param is null[%s, %s]\n", pkgid, op_str);
3966 _LOGD("fota-info : path=%s, op=%s\n", pkgid, op_str);
3968 /*get operation type*/
3969 op_type = __ri_get_op_type(op_str);
3971 _LOGE("fota-info : operation error[%s, %s]\n", pkgid, op_str);
3977 ret = __ri_install_fota_for_rw(pkgid);
3981 ret = __ri_upgrade_fota_for_rw(pkgid);
3985 ret = __ri_uninstall_fota_for_rw(pkgid);
3993 _LOGE("fota-info : Fota fail [pkgid=%s, operation=%d]\n",pkgid, op_type);
4006 int _rpm_process_enable(char *pkgid)
4009 char *manifest = NULL;
4010 pkgmgrinfo_pkginfo_h handle;
4013 _LOGE("start :: pkgid[%s] enable process\n",pkgid);
4015 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
4016 if ((ret == 0) && (handle != NULL)) {
4017 _LOGE("pkg[%s] is already installed.", pkgid);
4018 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
4022 manifest = pkgmgr_parser_get_manifest_file(pkgid);
4023 if (manifest == NULL) {
4024 _LOGE("Failed to fetch package manifest file\n");
4028 _ri_broadcast_status_notification(pkgid, "rpm", PKGMGR_INSTALLER_START_KEY_STR, PKGMGR_INSTALLER_INSTALL_EVENT_STR);
4030 ret = pkgmgr_parser_parse_manifest_for_installation(manifest, NULL);
4033 _ri_broadcast_status_notification(pkgid, "rpm", PKGMGR_INSTALLER_END_KEY_STR, PKGMGR_INSTALLER_FAIL_EVENT_STR);
4034 _LOGE("insert in db failed\n");
4038 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
4040 _LOGE("insert in db failed\n");
4042 ret = pkgmgrinfo_pkginfo_is_system(handle, &is_system);
4044 pkgmgrinfo_appinfo_get_list(handle, PM_UI_APP, __ri_update_ail_info, NULL);
4046 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
4049 /*delete disabled pkg info from backup db table*/
4050 pkgmgr_parser_delete_disabled_pkg(pkgid, NULL);
4052 _ri_broadcast_status_notification(pkgid, "rpm", PKGMGR_INSTALLER_END_KEY_STR, PKGMGR_INSTALLER_OK_EVENT_STR);
4054 _LOGE("end :: pkgid[%s] enable process\n",pkgid);
4059 int _rpm_process_disable(char *pkgid)
4062 pkgmgrinfo_pkginfo_h handle;
4064 _LOGE("start :: pkgid[%s] disable process\n",pkgid);
4066 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
4067 if ((ret < 0) || (handle == NULL)) {
4068 _LOGE("pkgid[%s] is already disabled\n", pkgid);
4072 _ri_broadcast_status_notification(pkgid, "rpm", PKGMGR_INSTALLER_START_KEY_STR, PKGMGR_INSTALLER_UNINSTALL_EVENT_STR);
4074 pkgmgrinfo_appinfo_get_list(handle, PM_UI_APP, __ri_check_running_app, NULL);
4075 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
4077 ret = pkgmgr_parser_parse_manifest_for_uninstallation(pkgid, NULL);
4079 _ri_broadcast_status_notification(pkgid, "rpm", PKGMGR_INSTALLER_END_KEY_STR, PKGMGR_INSTALLER_FAIL_EVENT_STR);
4080 _LOGE("pkgmgr_parser_parse_manifest_for_uninstallation failed.\n");
4084 /*save disabled pkg info to backup db table*/
4085 pkgmgr_parser_insert_disabled_pkg(pkgid, NULL);
4087 _ri_broadcast_status_notification(pkgid, "rpm", PKGMGR_INSTALLER_END_KEY_STR, PKGMGR_INSTALLER_OK_EVENT_STR);
4089 _LOGE("end :: pkgid[%s] disable process\n",pkgid);
4093 int _rpm_process_enabled_list(const char *enabled_list)
4096 char delims[] = ":";
4097 char pkgid[MAX_BUF_SIZE] = {'\0'};
4098 char pkgid_list[MAX_BUF_SIZE] = {'\0'};
4100 if (enabled_list == NULL)
4103 snprintf(pkgid_list, MAX_BUF_SIZE, "%s", enabled_list);
4104 token = strtok(pkgid_list, delims);
4108 memset(pkgid, 0x00, sizeof(pkgid));
4109 if (strlen(token) < MAX_BUF_SIZE)
4110 strcat(pkgid, token);
4112 _LOGE("pkgid too long[%s]", token);
4115 _rpm_process_enable(pkgid);
4117 token = strtok(NULL, delims);
4123 int _rpm_process_disabled_list(const char *disabled_list)
4126 char delims[] = ":";
4127 char pkgid[MAX_BUF_SIZE] = {'\0'};
4128 char pkgid_list[MAX_BUF_SIZE] = {'\0'};
4130 if (disabled_list == NULL)
4133 snprintf(pkgid_list, MAX_BUF_SIZE, "%s", disabled_list);
4134 token = strtok(pkgid_list, delims);
4138 memset(pkgid, 0x00, sizeof(pkgid));
4139 if (strlen(token) < MAX_BUF_SIZE)
4140 strncat(pkgid, token, strlen(token));
4142 _LOGE("pkgid too long[%s]", token);
4144 _rpm_process_disable(pkgid);
4146 token = strtok(NULL, delims);
4152 int __ri_copy_smack_rule_file(int op, const char *pkgname, int is_system)
4155 mode_t mode = DIR_PERMS;
4156 int ret = RPM_INSTALLER_SUCCESS;
4157 char src[PATH_MAX]={0};
4158 char dest[PATH_MAX]={0};
4164 For downloadable native app, restore the smack file.
4165 Otherwise, remove the stored smack file.
4167 snprintf(dest,PATH_MAX-1,"%s%s.rule",SMACK_RULES_ALT_PATH,pkgname);
4168 snprintf(src,PATH_MAX-1,"%s%s.rule",DIR_RPM_WGT_SMACK_RULE_OPT,pkgname);
4169 _LOGD("#src:[%s] dest:[%s]",src,dest);
4172 if(!access(src,F_OK)){
4175 _LOGD("#File [%s] deleted.",src);
4177 _LOGE("@Unable to delete the file [%s], error:(%s)",src,strerror(errno));
4178 ret = RPM_INSTALLER_ERR_INTERNAL;
4182 if(!access(dest,F_OK)){
4185 _LOGD("#File [%s] deleted.",dest);
4187 _LOGE("@Unable to delete the file [%s], error:(%s)",dest,strerror(errno));
4188 ret = RPM_INSTALLER_ERR_INTERNAL;
4193 _LOGD("#Restore smack files for uninstallation [%s]",pkgname);
4194 if(!access(src,F_OK)){
4195 _LOGD("#Copying [%s] to [%s]",src,dest);
4196 ret = __copy_file(src,dest);
4200 _LOGD("#File [%s] deleted.",src);
4202 _LOGE("@Unable to delete the file [%s], error:(%s)",src,strerror(errno));
4203 ret = RPM_INSTALLER_ERR_INTERNAL;
4207 _LOGE("@Copy Failed!!");
4208 ret = RPM_INSTALLER_ERR_INTERNAL;
4212 _LOGE("@ %s.rule file is not preserved",pkgname);
4219 _LOGD("#Preserve the smack file for upgrade [%s]",pkgname);
4221 /* Apply the new smack file and preserve the old smack rule file if it is not preserved.*/
4222 snprintf(src,PATH_MAX-1,"%s%s.rule",SMACK_RULES_ALT_PATH,pkgname);
4223 snprintf(dest,PATH_MAX-1,"%s%s.rule",DIR_RPM_WGT_SMACK_RULE_OPT,pkgname);
4225 _LOGD("#src[%s] dest[%s]",src,dest);
4227 /* Create the directory if not exist to preserve the smack files */
4228 if(mkdir(DIR_RPM_WGT_SMACK_RULE_OPT,mode) == 0 || errno == EEXIST){
4229 if((access(src,F_OK) == 0) && (access(dest,F_OK) != 0)){
4230 ret = __copy_file(src,dest);
4232 _LOGD("#Smack file is already preserved");
4235 _LOGE("@Temporary folder creation failed");
4236 ret = RPM_INSTALLER_ERR_INTERNAL;
4240 _LOGE("@Unsupported Operation\n");
4241 ret = RPM_INSTALLER_ERR_INTERNAL;