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, getuid());
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)
611 char dirpath[BUF_SIZE] = {'\0'};
612 char* groupid = NULL;
613 char *shared_data_label = NULL;
615 /*execute privilege APIs. The APIs should not fail*/
616 _ri_privilege_register_package(pkgname);
618 /*home dir. Dont setup path but change smack access to "_" */
619 snprintf(dirpath, BUF_SIZE, "%s/%s", USR_APPS, pkgname);
620 if (__is_dir(dirpath))
621 _ri_privilege_change_smack_label(dirpath, "_", 0);/*0 is SMACK_LABEL_ACCESS*/
622 memset(dirpath, '\0', BUF_SIZE);
623 snprintf(dirpath, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgname);
624 if (__is_dir(dirpath))
625 _ri_privilege_change_smack_label(dirpath, "_", 0);/*0 is SMACK_LABEL_ACCESS*/
628 memset(dirpath, '\0', BUF_SIZE);
629 snprintf(dirpath, BUF_SIZE, "%s/%s/data", OPT_USR_APPS, pkgname);
630 if (__is_dir(dirpath))
631 _ri_privilege_setup_path(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
634 memset(dirpath, '\0', BUF_SIZE);
635 snprintf(dirpath, BUF_SIZE, "%s/%s/cache", OPT_USR_APPS, pkgname);
636 if (!__is_dir(dirpath)) {
637 ret = mkdir(dirpath, DIRECTORY_PERMISSION_755);
639 _LOGE("directory making is failed.\n");
642 ret = chown(dirpath, APP_OWNER_ID, APP_GROUP_ID);
644 _LOGE("chown failed!! [%s]",strerror(errno));
646 _ri_privilege_setup_path(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
649 memset(dirpath, '\0', BUF_SIZE);
650 snprintf(dirpath, BUF_SIZE, "%s/%s/shared", USR_APPS, pkgname);
651 if (__is_dir(dirpath))
652 _ri_privilege_change_smack_label(dirpath, "_", 0);/*0 is SMACK_LABEL_ACCESS*/
653 memset(dirpath, '\0', BUF_SIZE);
654 snprintf(dirpath, BUF_SIZE, "%s/%s/shared", OPT_USR_APPS, pkgname);
655 if (!__is_dir(dirpath))
656 ret = mkdir(dirpath, DIRECTORY_PERMISSION_755);
658 _LOGE("directory making is failed.\n");
660 _ri_privilege_change_smack_label(dirpath, "_", 0);/*0 is SMACK_LABEL_ACCESS*/
661 memset(dirpath, '\0', BUF_SIZE);
664 snprintf(dirpath, BUF_SIZE, "%s/%s/shared/res", USR_APPS, pkgname);
665 if (__is_dir(dirpath))
666 _ri_privilege_setup_path(pkgname, dirpath, PERM_APP_PATH_ANY_LABEL, "_");
667 memset(dirpath, '\0', BUF_SIZE);
669 snprintf(dirpath, BUF_SIZE, "%s/%s/shared/res", OPT_USR_APPS, pkgname);
670 if (__is_dir(dirpath))
671 _ri_privilege_setup_path(pkgname, dirpath, PERM_APP_PATH_ANY_LABEL, "_");
672 memset(dirpath, '\0', BUF_SIZE);
675 snprintf(dirpath, BUF_SIZE, "%s/%s/shared/data", USR_APPS, pkgname);
676 if (__is_dir(dirpath)) {
677 ret = chown(dirpath, APP_OWNER_ID, APP_GROUP_ID);
679 _LOGE("chown failed!! [%s]",strerror(errno));
681 _ri_privilege_setup_path(pkgname, dirpath, PERM_APP_PATH_PUBLIC, NULL);
683 memset(dirpath, '\0', BUF_SIZE);
685 snprintf(dirpath, BUF_SIZE, "%s/%s/shared/data", OPT_USR_APPS, pkgname);
686 if (__is_dir(dirpath)) {
687 ret = chown(dirpath, APP_OWNER_ID, APP_GROUP_ID);
689 _LOGE("chown failed!! [%s]",strerror(errno));
691 _ri_privilege_setup_path(pkgname, dirpath, PERM_APP_PATH_PUBLIC, NULL);
695 ret = _coretpk_installer_get_smack_label_access(dirpath, &shared_data_label);
697 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", dirpath, ret);
699 memset(dirpath, '\0', BUF_SIZE);
700 snprintf(dirpath, BUF_SIZE, "%s/%s/shared/cache", OPT_USR_APPS, pkgname);
701 if (!__is_dir(dirpath)) {
702 ret = mkdir(dirpath, DIRECTORY_PERMISSION_755);
704 _LOGE("directory making is failed.\n");
707 ret = chown(dirpath, APP_OWNER_ID, APP_GROUP_ID);
709 _LOGE("chown failed!! [%s]",strerror(errno));
711 ret = _coretpk_installer_set_smack_label_access(dirpath, shared_data_label);
713 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", dirpath, ret);
715 ret = _coretpk_installer_set_smack_label_transmute(dirpath, "1");
717 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", dirpath, ret);
721 memset(dirpath, '\0', BUF_SIZE);
722 snprintf(dirpath, BUF_SIZE, "%s/%s/shared/trusted", USR_APPS, pkgname);
723 if (__is_dir(dirpath)) {
724 ret = chown(dirpath, APP_OWNER_ID, APP_GROUP_ID);
726 _LOGE("chown failed!! [%s]",strerror(errno));
729 ret = _rpm_installer_get_group_id(pkgname, &groupid);
731 _LOGD("group id for trusted directory is [%s]", groupid);
732 _ri_privilege_setup_path(pkgname, dirpath, APP_PATH_GROUP_RW, groupid);
733 FREE_AND_NULL(groupid);
736 memset(dirpath, '\0', BUF_SIZE);
738 snprintf(dirpath, BUF_SIZE, "%s/%s/shared/trusted", OPT_USR_APPS, pkgname);
740 _LOGD("dirpath [%s]", dirpath);
742 ret = _rpm_installer_get_group_id(pkgname, &groupid);
744 if (__is_dir(dirpath) != 1) {
745 _LOGE("dont have [%s]", dirpath);
747 ret = mkdir(dirpath, DIRECTORY_PERMISSION_755);
748 if (ret == -1 && errno != EEXIST) {
749 _LOGE("mkdir failed!! [%s]",strerror(errno));
753 ret = chown(dirpath, APP_OWNER_ID, APP_GROUP_ID);
755 _LOGE("chown failed!! [%s]",strerror(errno));
758 _LOGD("group id for trusted directory is [%s]", groupid);
759 _ri_privilege_setup_path(pkgname, dirpath, APP_PATH_GROUP_RW, groupid);
760 _ri_set_group_id(pkgname, groupid);
762 FREE_AND_NULL(groupid);
767 int __is_dir(char *dirname)
769 struct stat stFileInfo;
770 if(dirname == NULL) {
771 _LOGE("dirname is null\n");
775 (void)stat(dirname, &stFileInfo);
777 if (S_ISDIR(stFileInfo.st_mode)) {
783 static void __rpm_process_line(char *line)
786 tok = strtok(line, " ");
788 if (!strncmp(tok, "%%", 2)) {
789 tok = strtok(NULL, " ");
791 _LOGD("Install percentage is %s\n",
793 _ri_broadcast_status_notification(gpkgname,
797 _ri_stat_cb(gpkgname, "install_percent", tok);
805 static void __rpm_perform_read(int fd)
807 char *buf_ptr = NULL;
808 char *tmp_ptr = NULL;
810 static char buffer[BUF_SIZE] = { 0, };
811 static int buffer_position;
813 size = read(fd, &buffer[buffer_position],
814 sizeof(buffer) - buffer_position);
815 buffer_position += size;
819 /* Process each line of the recieved buffer */
820 buf_ptr = tmp_ptr = buffer;
821 while ((tmp_ptr = (char *)memchr(buf_ptr, '\n',
822 buffer + buffer_position - buf_ptr)) !=
825 __rpm_process_line(buf_ptr);
826 /* move to next line and continue */
827 buf_ptr = tmp_ptr + 1;
830 /*move the remaining bits at the start of the buffer
831 and update the buffer position */
832 buf_ptr = (char *)memrchr(buffer, 0, buffer_position);
836 /* we have processed till the last \n which has now become
837 0x0. So we increase the pointer to next position */
840 memmove(buffer, buf_ptr, buf_ptr - buffer);
841 buffer_position = buffer + buffer_position - buf_ptr;
845 int _rpm_xsystem(const char *argv[])
854 if (pipe(pipefd) == -1) {
855 _LOGE("pipe creation failed\n");
858 /*Read progress info via pipe */
863 _LOGE("fork failed\n");
873 _LOGE("dup failed\n");
877 result = dup(pipefd[1]);
879 _LOGE("dup failed\n");
883 if (execvp(argv[0], (char *const *)argv) == -1) {
884 _LOGE("execvp failed\n");
895 while ((err = waitpid(pid, &status, WNOHANG)) != pid) {
899 _LOGE("waitpid failed\n");
908 FD_SET(pipefd[0], &rfds);
912 pselect(pipefd[0] + 1, &rfds, NULL, NULL, &tv, NULL);
916 else if (select_ret < 0 && errno == EINTR)
918 else if (select_ret < 0) {
919 _LOGE("select() returned error\n");
922 if (FD_ISSET(pipefd[0], &rfds))
923 __rpm_perform_read(pipefd[0]);
927 /* Check for an error code. */
928 if (WIFEXITED(status) == 0 || WEXITSTATUS(status) != 0) {
930 if (WIFSIGNALED(status) != 0 && WTERMSIG(status) == SIGSEGV) {
932 ("Sub-process %s received a segmentation fault. \n",
934 } else if (WIFEXITED(status) != 0) {
935 printf("Sub-process %s returned an error code (%u)\n",
936 argv[0], WEXITSTATUS(status));
938 printf("Sub-process %s exited unexpectedly\n", argv[0]);
941 return WEXITSTATUS(status);
944 void __rpm_clear_dir_list(GList* dir_list)
947 app2ext_dir_details* dir_detail = NULL;
949 list = g_list_first(dir_list);
951 dir_detail = (app2ext_dir_details *)list->data;
952 if (dir_detail && dir_detail->name) {
953 free(dir_detail->name);
956 list = g_list_next(list);
958 g_list_free(dir_list);
962 GList * __rpm_populate_dir_list()
964 GList *dir_list = NULL;
966 app2ext_dir_details* dir_detail = NULL;
968 char pkg_ro_content_rpm[3][5] = { "bin", "res", "lib" };
971 for (i=0; i<3; i++) {
972 dir_detail = (app2ext_dir_details*) calloc(1, sizeof(app2ext_dir_details));
973 if (dir_detail == NULL) {
974 printf("\nMemory allocation failed\n");
977 dir_detail->name = (char*) calloc(1, sizeof(char)*(strlen(pkg_ro_content_rpm[i])+2));
978 if (dir_detail->name == NULL) {
979 printf("\nMemory allocation failed\n");
983 snprintf(dir_detail->name, (strlen(pkg_ro_content_rpm[i])+1), "%s", pkg_ro_content_rpm[i]);
984 dir_detail->type = APP2EXT_DIR_RO;
985 dir_list = g_list_append(dir_list, dir_detail);
988 list = g_list_first(dir_list);
990 dir_detail = (app2ext_dir_details *)list->data;
991 list = g_list_next(list);
997 list = g_list_first(dir_list);
999 dir_detail = (app2ext_dir_details *)list->data;
1000 if (dir_detail && dir_detail->name) {
1001 free(dir_detail->name);
1004 list = g_list_next(list);
1006 g_list_free(dir_list);
1011 static char *__ri_get_cert_from_file(const char *file)
1013 FILE *fp_cert = NULL;
1015 char *certbuf = NULL;
1016 char *startcert = NULL;
1017 char *endcert = NULL;
1024 if(!(fp_cert = fopen(file, "r"))) {
1025 _LOGE("[ERR][%s] Fail to open file, [%s]\n", __func__, file);
1029 fseek(fp_cert, 0L, SEEK_END);
1031 if(ftell(fp_cert) < 0) {
1032 _LOGE("[ERR][%s] Fail to find EOF\n", __func__);
1037 certlen = ftell(fp_cert);
1038 fseek(fp_cert, 0L, SEEK_SET);
1040 if(!(certbuf = (char*)malloc(sizeof(char) * (int)certlen))) {
1041 _LOGE("[ERR][%s] Fail to allocate memory\n", __func__);
1045 memset(certbuf, 0x00, (int)certlen);
1048 while((ch = fgetc(fp_cert)) != EOF) {
1056 startcert = strstr(certbuf, "-----BEGIN CERTIFICATE-----") + strlen("-----BEGIN CERTIFICATE-----");
1057 endcert = strstr(certbuf, "-----END CERTIFICATE-----");
1058 certwrite = (int)endcert - (int)startcert;
1060 cert = (char*)malloc(sizeof(char) * (certwrite+2));
1062 _LOGE("[ERR][%s] Fail to allocate memory\n", __func__);
1066 memset(cert, 0x00, certwrite+2);
1067 snprintf(cert, certwrite+1, "%s", startcert);
1068 _LOGD("Root CA, len=[%d]\n%s", certwrite, cert);
1080 static int __privilege_func(const char *name, void *user_data)
1084 const char *perm[] = {NULL, NULL};
1086 int apptype = PERM_APP_TYPE_EFL;
1087 privilegeinfo *info = (privilegeinfo*)user_data;
1089 _LOGD("package_id = [%s], privilege = [%s]", info->package_id, name);
1090 privilege_list = g_list_append(privilege_list, strdup((char*)name));
1092 if (info->visibility & CERT_SVC_VISIBILITY_PLATFORM) {
1093 _LOGD("VISIBILITY_PLATFORM!");
1094 apptype = PERM_APP_TYPE_EFL_PLATFORM;
1095 } else if ((info->visibility & CERT_SVC_VISIBILITY_PARTNER) ||
1096 (info->visibility & CERT_SVC_VISIBILITY_PARTNER_OPERATOR) ||
1097 (info->visibility & CERT_SVC_VISIBILITY_PARTNER_MANUFACTURER)) {
1098 _LOGD("VISIBILITY_PARTNER!");
1099 apptype = PERM_APP_TYPE_EFL_PARTNER;
1102 ret = _ri_privilege_enable_permissions((char *)info->package_id, apptype, perm, 1);
1104 _LOGE("_ri_privilege_enable_permissions(%s, %d) failed.", (char *)info->package_id, apptype);
1108 if (strcmp(name, EXT_APPDATA_PRIVILEGE_NAME) == 0) {
1109 _LOGD("it is EXT_APPDATA_PRIVILEGE_NAME");
1110 if(_coretpk_installer_make_directory_for_ext((char *)info->package_id) < 0) {
1111 _LOGE("make_directory_for_ext failed.");
1115 _LOGD("_ri_privilege_enable_permissions(%s, %d) succeed.", (char *)info->package_id, apptype);
1121 char *__strlwr(char *str)
1125 while(*(str+i) != '\0'){
1126 if(*(str+i) >= 65 || *(str+i)<= 90) {
1127 *(str+i) = towlower(*(str+i));
1134 static int __ri_find_xml(char *pkgid, char **rpm_xml)
1137 struct dirent entry, *result;
1141 dir = opendir(USR_SHARE_PACKAGES);
1143 if (strerror_r(errno, buf, sizeof(buf)) == 0)
1144 _LOGE("fota-info : Failed to access the [%s] because %s", USR_SHARE_PACKAGES, buf);
1148 for (ret = readdir_r(dir, &entry, &result);
1149 ret == 0 && result != NULL;
1150 ret = readdir_r(dir, &entry, &result)) {
1151 char *manifest = NULL;
1153 if (entry.d_name[0] == '.') continue;
1155 manifest = _manifest_to_package(entry.d_name);
1157 _LOGE("fota-info : Failed to convert file to xml[%s]", entry.d_name);
1161 if (strstr(manifest, __strlwr(pkgid))) {
1162 snprintf(buf, sizeof(buf), "%s/%s", USR_SHARE_PACKAGES, manifest);
1163 _LOGD("fota-info : pkgid[%s] find xml[%s]\n", pkgid, buf);
1164 *rpm_xml = strdup(buf);
1173 _LOGE("fota-info : Failed to find xml for pkgid[%s]", pkgid);
1178 static int __ri_install_fota(char *pkgid)
1181 char *manifest = NULL;
1182 char *temp[] = {"fota=true", NULL};
1184 _LOGD("fota-info : pkgid[%s] start installation\n", pkgid);
1186 /*if pkgid is one of deactivation, it does not need to install*/
1187 ret = __ri_check_pkgid_for_deactivation(pkgid);
1189 _LOGE("fota-info : pkgid[%s] for deactivation dont need to install.\n", pkgid);
1194 manifest = pkgmgr_parser_get_manifest_file(pkgid);
1195 if (manifest == NULL) {
1196 _LOGE("fota-info : dont have manefest[pkgid=%s]\n", pkgid);
1200 _LOGD("fota-info : pkgid[%s] has manefest[%s]\n", pkgid, manifest);
1202 ret = pkgmgr_parser_parse_manifest_for_installation(manifest, temp);
1204 _LOGE("fota-info : installation fail[manifest=%s]\n", manifest);
1208 _LOGD("fota-info : pkgid[%s] installation success\n", pkgid);
1210 __rpm_apply_smack(pkgid,0);
1211 __ri_make_directory(pkgid);
1213 ret = _ri_apply_privilege(pkgid, 0);
1215 _LOGE("fota-info : _ri_apply_privilege fail[pkgid=%s]\n", pkgid);
1219 _LOGD("fota-info : pkgid[%s] apply smack success\n", pkgid);
1230 static int __ri_upgrade_fota(char *pkgid)
1232 _LOGD("fota-info : pkgid[%s] start upgrade\n", pkgid);
1235 char *manifest = NULL;
1236 char *temp[] = {"fota=true", NULL};
1238 /*if pkgid is one of deactivation, it does not need to upgrade*/
1239 ret = __ri_check_pkgid_for_deactivation(pkgid);
1241 _LOGE("fota-info : pkgid[%s] for deactivation dont need to install.\n", pkgid);
1246 manifest = pkgmgr_parser_get_manifest_file(pkgid);
1247 if (manifest == NULL) {
1248 _LOGE("fota-info : dont have manefest[pkgid=%s]\n", pkgid);
1252 _LOGD("fota-info : pkgid[%s] has manefest[%s]\n", pkgid, manifest);
1253 if (access(manifest, F_OK) != 0) {
1254 _LOGE("fota-info : can not access[manifest=%s]\n", manifest);
1259 ret = __ri_find_xml(pkgid, &manifest);
1261 _LOGE("fota-info : can not find xml[pkgid=%s]\n", pkgid);
1267 if (manifest == NULL) {
1271 ret = pkgmgr_parser_parse_manifest_for_upgrade(manifest, temp);
1273 _LOGE("fota-info : upgrade fail[manifest=%s]\n", manifest);
1278 __rpm_apply_smack(pkgid,0);
1279 __ri_make_directory(pkgid);
1281 ret = _ri_apply_privilege(pkgid, 0);
1283 _LOGE("fota-info : _ri_apply_privilege fail[pkgid=%s]\n", pkgid);
1286 _LOGD("fota-info : pkgid[%s] upgrade success\n", pkgid);
1295 static int __ri_uninstall_fota(char *pkgid)
1297 _LOGD("fota-info : pkgid[%s] start uninstallation\n", pkgid);
1301 ret = _ri_privilege_unregister_package(pkgid);
1303 _LOGE("fota-info : _ri_privilege_unregister_package fail[pkgid=%s]\n", pkgid);
1306 ret = pkgmgr_parser_parse_manifest_for_uninstallation(pkgid, NULL);
1308 _LOGE("fota-info : uninstall fail[manifest=%s]\n", pkgid);
1311 _LOGD("fota-info : pkgid[%s] uninstall success\n", pkgid);
1316 static char * __getvalue(const char* pBuf, const char* pKey)
1318 const char* p = NULL;
1319 const char* pStart = NULL;
1320 const char* pEnd = NULL;
1322 p = strstr(pBuf, pKey);
1326 pStart = p + strlen(pKey) + 1;
1327 pEnd = strchr(pStart, SEPERATOR_START);
1331 size_t len = pEnd - pStart;
1335 char *pRes = (char*)malloc(len + 1);
1337 _LOGE("@malloc failed");
1340 strncpy(pRes, pStart, len);
1346 static char *__find_rpm_pkgid(const char* manifest)
1349 char buf[BUF_SIZE] = {0};
1352 fp = fopen(manifest, "r");
1354 _LOGE("csc-info : Fail get : %s\n", manifest);
1358 while (fgets(buf, BUF_SIZE, fp) != NULL) {
1360 pkgid = __getvalue(buf, TOKEN_PACKAGE_STR);
1361 if (pkgid != NULL) {
1365 memset(buf, 0x00, BUF_SIZE);
1374 static int __copy_file( const char *src_path, const char *dst_path)
1378 unsigned char temp_buf[8192] = {'\0',};
1379 size_t size_of_uchar = sizeof( unsigned char);
1380 size_t size_of_temp_buf = sizeof( temp_buf);
1382 src = fopen(src_path, "r");
1384 _LOGE("Failed to open(). path=%s, E:%d(%s)", src_path, errno, strerror(errno));
1388 dst = fopen(dst_path, "w");
1390 _LOGE("Failed to open dst file. file=%s, E:%d(%s)", dst_path, errno, strerror(errno));
1396 rc = fread( temp_buf, size_of_uchar, size_of_temp_buf, src);
1397 fwrite( temp_buf, size_of_uchar, rc, dst);
1405 static int __ri_install_csc(char *path_str, char *remove_str)
1410 char delims[] = "/";
1412 char argv[BUF_SIZE] = {'\0'};
1413 char xml_name[BUF_SIZE] = {'\0'};
1414 char src_file[BUF_SIZE] = {'\0'};
1415 char dest_file[BUF_SIZE] = {'\0'};
1417 snprintf(src_file, sizeof(src_file), "%s", path_str);
1419 /*get pkgid from path str*/
1420 pkgid = __find_rpm_pkgid(path_str);
1421 if (pkgid == NULL) {
1422 _LOGE("csc-info : fail to find pkgid\n");
1425 _LOGD("csc-info : find pkgid=[%s] for installation\n", pkgid);
1428 token = strtok(path_str, delims);
1431 memset(xml_name, 0x00, sizeof(xml_name));
1432 strncat(xml_name, token, strlen(token));
1433 token = strtok(NULL, delims);
1435 _LOGD("csc-info : xml name = %s\n", xml_name);
1437 /*copy xml to /opt/share/packages*/
1438 snprintf(dest_file, sizeof(dest_file), "%s/%s", OPT_SHARE_PACKAGES, xml_name);
1439 ret = __copy_file(src_file, dest_file);
1441 _LOGE("csc-info : xml copy fail(%d)\n", ret);
1443 _LOGE("csc-info : xml copy success to [%s] \n", dest_file);
1446 /*remove old pkg info*/
1447 ret = pkgmgr_parser_parse_manifest_for_uninstallation(pkgid, NULL);
1449 _LOGD("csc-info : fail remove old pkg info\n");
1451 _LOGD("csc-info : success remove old pkg info\n");
1454 /*insert new pkg info*/
1455 memset(argv, 0x00, sizeof(argv));
1456 snprintf(argv, sizeof(argv), "%s/%s", OPT_SHARE_PACKAGES, xml_name);
1457 ret = __ri_init_csc_xml(argv, remove_str);
1459 _LOGD("csc-info : fail insert db\n");
1461 _LOGD("csc-info : success xml name = %s\n", xml_name);
1471 static int __ri_uninstall_csc(char *pkgid)
1473 /*remove old pkg info*/
1474 int ret = pkgmgr_parser_parse_manifest_for_uninstallation(pkgid, NULL);
1476 _LOGD("csc-info : fail remove old pkg info\n");
1478 _LOGD("csc-info : success remove old pkg info\n");
1485 static int __get_size_from_xml(const char *manifest, int *size)
1487 const char *val = NULL;
1488 const xmlChar *node;
1489 xmlTextReaderPtr reader;
1490 int ret = PMINFO_R_OK;
1492 if(manifest == NULL) {
1493 _LOGE("Input argument is NULL\n");
1494 return PMINFO_R_ERROR;
1498 _LOGE("Argument supplied to hold return value is NULL\n");
1499 return PMINFO_R_ERROR;
1502 reader = xmlReaderForFile(manifest, NULL, 0);
1505 if (_child_element(reader, -1)) {
1506 node = xmlTextReaderConstName(reader);
1508 _LOGE("xmlTextReaderConstName value is NULL\n");
1509 ret = PMINFO_R_ERROR;
1513 if (!strcmp(ASCII(node), "manifest")) {
1514 ret = _ri_get_attribute(reader,"size",&val);
1516 _LOGE("@Error in getting the attribute value");
1517 ret = PMINFO_R_ERROR;
1525 _LOGE("package size is not specified\n");
1526 ret = PMINFO_R_ERROR;
1530 _LOGE("Unable to create xml reader\n");
1531 ret = PMINFO_R_ERROR;
1536 _LOGE("xmlReaderForFile value is NULL\n");
1537 ret = PMINFO_R_ERROR;
1542 xmlFreeTextReader(reader);
1546 static int __get_location_from_xml(const char *manifest, pkgmgrinfo_install_location *location)
1548 const char *val = NULL;
1549 const xmlChar *node;
1550 xmlTextReaderPtr reader;
1553 if(manifest == NULL) {
1554 _LOGE("Input argument is NULL\n");
1555 return PMINFO_R_ERROR;
1558 if(location == NULL) {
1559 _LOGE("Argument supplied to hold return value is NULL\n");
1560 return PMINFO_R_ERROR;
1563 reader = xmlReaderForFile(manifest, NULL, 0);
1566 if ( _child_element(reader, -1)) {
1567 node = xmlTextReaderConstName(reader);
1569 _LOGE("xmlTextReaderConstName value is NULL\n");
1570 xmlFreeTextReader(reader);
1571 return PMINFO_R_ERROR;
1574 if (!strcmp(ASCII(node), "manifest")) {
1575 ret = _ri_get_attribute(reader,"install-location",&val);
1577 _LOGE("@Error in getting the attribute value");
1578 xmlFreeTextReader(reader);
1579 return PMINFO_R_ERROR;
1583 if (strcmp(val, "internal-only") == 0)
1584 *location = PMINFO_INSTALL_LOCATION_INTERNAL_ONLY;
1585 else if (strcmp(val, "prefer-external") == 0)
1586 *location = PMINFO_INSTALL_LOCATION_PREFER_EXTERNAL;
1588 *location = PMINFO_INSTALL_LOCATION_AUTO;
1592 _LOGE("Unable to create xml reader\n");
1593 xmlFreeTextReader(reader);
1594 return PMINFO_R_ERROR;
1598 _LOGE("xmlReaderForFile value is NULL\n");
1599 return PMINFO_R_ERROR;
1602 xmlFreeTextReader(reader);
1607 static char *__get_pkg_path(const char *pkg_path, const char *pkgid)
1610 char buff[BUF_SIZE] = {'\0'};
1611 char *real_path = NULL;
1613 snprintf(buff, BUF_SIZE, "%s/%s", pkg_path, pkgid);
1615 if (__is_dir(buff)) break;
1616 memset(buff, '\0', BUF_SIZE);
1617 snprintf(buff, BUF_SIZE, "%s/%s", USR_APPS,pkgid);
1618 if (__is_dir(buff)) break;
1619 memset(buff, '\0', BUF_SIZE);
1620 snprintf(buff, BUF_SIZE, "/opt/apps/%s", pkgid);
1621 if (__is_dir(buff)) break;
1622 memset(buff, '\0', BUF_SIZE);
1623 snprintf(buff, BUF_SIZE, "%s/%s", OPT_USR_APPS,pkgid);
1624 if (__is_dir(buff)) break;
1629 _LOGE("chdir(%s) failed. [%s]", buff, strerror(errno));
1633 real_path = (char *)malloc(strlen(buff) + 1);
1634 if (real_path == NULL) {
1635 _LOGE("malloc() failed.");
1638 memset(real_path, '\0', strlen(buff) + 1);
1639 memcpy(real_path, buff, strlen(buff));
1644 static int __set_label_for_pkg_dir(const char *dirpath, const char* label)
1648 struct dirent entry;
1649 struct dirent *result;
1650 char fullpath[BUF_SIZE] = {'\0'};
1652 if (access(dirpath, F_OK) != 0) {
1653 _LOGE("skip! empty dirpath = [%s]", dirpath);
1657 dir = opendir(dirpath);
1659 _LOGE("opendir(%s) failed. [%d][%s]", dirpath, errno, strerror(errno));
1663 if(lsetxattr(dirpath, "security.SMACK64", label, strlen(label), 0)) {
1664 _LOGE("error(%d) in setting smack label",errno);
1667 for (ret = readdir_r(dir, &entry, &result); ret == 0 && result != NULL; ret = readdir_r(dir, &entry, &result)){
1668 if (!strcmp(entry.d_name, ".") ||
1669 !strcmp(entry.d_name, "..")) {
1674 snprintf(fullpath, BUF_SIZE, "%s/%s", dirpath, entry.d_name);
1675 if(lsetxattr(fullpath, "security.SMACK64", label, strlen(label), 0)) {
1676 _LOGE("error(%d) in setting smack label",errno);
1681 if (entry.d_type == DT_DIR) {
1682 ret = __set_label_for_pkg_dir(fullpath,label);
1684 _LOGE("_coretpk_installer_apply_directory_policy(%s) failed.", fullpath);
1687 memset(fullpath, '\0', BUF_SIZE);
1696 void _ri_soft_reset(char *pkgid)
1699 char dirpath[PKG_STRING_LEN_MAX] = {'\0'};
1702 snprintf(dirpath, PKG_STRING_LEN_MAX, "%s/%s", OPT_USR_APPS, pkgid);
1703 if (access(dirpath, F_OK)==0) {
1704 __set_label_for_pkg_dir(dirpath, "_");
1708 memset(dirpath, '\0', PKG_STRING_LEN_MAX);
1709 snprintf(dirpath, PKG_STRING_LEN_MAX, "%s/%s/data", OPT_USR_APPS, pkgid);
1710 if (access(dirpath, F_OK)==0) {
1711 __set_label_for_pkg_dir(dirpath, pkgid);
1715 memset(dirpath, '\0', PKG_STRING_LEN_MAX);
1716 snprintf(dirpath, PKG_STRING_LEN_MAX, "%s/%s/shared", OPT_USR_APPS, pkgid);
1717 if (access(dirpath, F_OK)==0) {
1718 __set_label_for_pkg_dir(dirpath, "_");
1722 memset(dirpath, '\0', PKG_STRING_LEN_MAX);
1723 snprintf(dirpath, PKG_STRING_LEN_MAX, "%s/%s/shared/res", OPT_USR_APPS, pkgid);
1724 if (access(dirpath, F_OK)==0) {
1725 __set_label_for_pkg_dir(dirpath, "_");
1729 memset(dirpath, '\0', PKG_STRING_LEN_MAX);
1730 snprintf(dirpath, PKG_STRING_LEN_MAX, "%s/%s/shared/data", OPT_USR_APPS, pkgid);
1731 if (access(dirpath, F_OK)==0) {
1732 __set_label_for_pkg_dir(dirpath, "_");
1736 memset(dirpath, '\0', PKG_STRING_LEN_MAX);
1737 snprintf(dirpath, PKG_STRING_LEN_MAX, "%s/%s/shared/trusted", OPT_USR_APPS, pkgid);
1738 if (access(dirpath, F_OK)==0) {
1739 pkgmgrinfo_pkginfo_h handle = NULL;
1740 char *groupid = NULL;
1741 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
1742 if (ret != PMINFO_R_OK)
1744 /* ret = pkgmgrinfo_pkginfo_get_groupid(handle, &groupid);
1745 if (ret == PMINFO_R_OK) {
1746 _ri_privilege_setup_path(pkgid, dirpath, APP_PATH_GROUP_RW, groupid);
1748 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
1754 void _ri_register_cert(const char *pkgid)
1757 pkgmgrinfo_instcertinfo_h handle = NULL;
1760 error = pkgmgrinfo_create_certinfo_set_handle(&handle);
1762 _LOGE("Cert handle creation failed. Err:%d", error);
1763 __ri_free_cert_chain();
1767 if (list[SIG_AUTH].cert_value == NULL) {
1768 _LOGE("pkgid[%s] dont have SIG_AUTH.cert_value ", pkgid);
1772 for (i = 0; i < MAX_CERT_NUM; i++) {
1774 if (list[i].cert_value) {
1775 error = pkgmgrinfo_set_cert_value(handle, list[i].cert_type, list[i].cert_value);
1777 _LOGE("pkgmgrinfo_set_cert_value failed. cert type:%d. Err:%d", list[i].cert_type, error);
1782 /* Save the certificates in cert DB*/
1783 error = pkgmgrinfo_save_certinfo(pkgid, handle, getuid());
1785 _LOGE("pkgmgrinfo_save_certinfo failed. Err:%d", error);
1790 pkgmgrinfo_destroy_certinfo_set_handle(handle);
1791 __ri_free_cert_chain();
1794 void _ri_unregister_cert(const char *pkgid)
1797 /* Delete the certifictes from cert DB*/
1798 error = pkgmgrinfo_delete_usr_certinfo(pkgid, getuid());
1800 _LOGE("pkgmgrinfo_delete_certinfo failed. Err:%d", error);
1805 int _ri_verify_sig_and_cert(const char *sigfile, int *visibility)
1807 char certval[MAX_BUFF_LEN] = {'\0'};
1812 int ret = RPM_INSTALLER_SUCCESS;
1814 signature_x *signx = NULL;
1815 struct keyinfo_x *keyinfo = NULL;
1816 struct x509data_x *x509data = NULL;
1817 CERT_CONTEXT *ctx = NULL;
1820 ctx = cert_svc_cert_context_init();
1822 _LOGE("cert_svc_cert_context_init() failed.");
1823 return RPM_INSTALLER_ERR_INTERNAL;
1826 if (strstr(sigfile, AUTHOR_SIGNATURE_XML))
1828 else if (strstr(sigfile, SIGNATURE1_XML))
1829 sigtype = SIG_DIST1;
1830 else if (strstr(sigfile, SIGNATURE2_XML))
1831 sigtype = SIG_DIST2;
1833 _LOGE("Unsupported signature type! [%s]", sigfile);
1834 cert_svc_cert_context_final(ctx);
1835 return RPM_INSTALLER_ERR_INTERNAL;
1838 signx = _ri_process_signature_xml(sigfile);
1839 if (signx == NULL) {
1840 _LOGE("_ri_process_signature_xml(%s) failed.", sigfile);
1841 ret = RPM_INSTALLER_ERR_INTERNAL;
1845 keyinfo = signx->keyinfo;
1846 if ((keyinfo == NULL) || (keyinfo->x509data == NULL) || (keyinfo->x509data->x509certificate == NULL)) {
1847 _LOGE("keyinfo is invalid. [%s]", sigfile);
1848 ret = RPM_INSTALLER_ERR_CERT_INVALID;
1852 x509data = keyinfo->x509data;
1853 x509certificate_x *cert = x509data->x509certificate;
1855 // First cert is Signer certificate
1856 if (cert->text != NULL) {
1857 for (i = 0; i <= (int)strlen(cert->text); i++) {
1858 if (cert->text[i] != '\n') {
1859 certval[j++] = cert->text[i];
1864 err = cert_svc_load_buf_to_context(ctx, (unsigned char*)certval);
1866 _LOGE("cert_svc_load_buf_to_context() failed. cert = [%s], err = [%d]", certval, err);
1867 ret = RPM_INSTALLER_ERR_INTERNAL;
1870 // _LOGD("SIG_SIGNER certval = [%d][%s]", strlen(certval), certval);
1872 err = __ri_create_cert_chain(sigtype, SIG_SIGNER, certval);
1874 _LOGE("__ri_create_cert_chain() failed. sigtype = [%d], certval = [%s]", sigtype, certval);
1875 __ri_free_cert_chain();
1876 ret = RPM_INSTALLER_ERR_INTERNAL;
1881 // Second cert is Intermediate certificate
1883 if (cert->text != NULL) {
1884 memset(certval, 0x00, MAX_BUFF_LEN);
1886 for (i = 0; i <= (int)strlen(cert->text); i++) {
1887 if (cert->text[i] != '\n') {
1888 certval[j++] = cert->text[i];
1893 if (cert->text != NULL) {
1894 err = cert_svc_push_buf_into_context(ctx, (unsigned char*)certval);
1896 _LOGE("cert_svc_push_buf_into_context() failed. cert = [%s], err = [%d]", certval, err);
1897 ret = RPM_INSTALLER_ERR_INTERNAL;
1901 // _LOGD("SIG_INTERMEDIATE certval = [%d][%s]", strlen(certval), certval);
1903 err = __ri_create_cert_chain(sigtype, SIG_INTERMEDIATE, certval);
1905 _LOGE("__ri_create_cert_chain() failed. sigtype = [%d], certval = [%s]", sigtype, certval);
1906 __ri_free_cert_chain();
1907 ret = RPM_INSTALLER_ERR_INTERNAL;
1911 _LOGE("Invalid CertChain! (cert->text is NULL.)");
1912 ret = RPM_INSTALLER_ERR_CERT_INVALID;
1916 err = cert_svc_verify_certificate(ctx, &validity);
1918 _LOGE("cert_svc_verify_certificate() failed.");
1919 ret = RPM_INSTALLER_ERR_INTERNAL;
1922 _LOGD("cert_svc_verify() is done successfully. validity=[%d]", validity);
1924 if (validity == 0) {
1925 _LOGE("Certificate Invalid/Expired (validity == 0)");
1926 ret = RPM_INSTALLER_ERR_CERTCHAIN_VERIFICATION_FAILED;
1931 // For reference validation, we should be in TEMP_DIR/usr/apps/<pkgid>
1932 if (ctx->fileNames && ctx->fileNames->filename) {
1933 _LOGD("Root CA cert path=[%s]", ctx->fileNames->filename);
1935 err = __ri_xmlsec_verify_signature(sigfile, ctx->fileNames->filename);
1937 _LOGE("__ri_xmlsec_verify_signature(%s) failed.", sigfile);
1938 ret = RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED;
1942 crt = __ri_get_cert_from_file(ctx->fileNames->filename);
1943 err = __ri_create_cert_chain(sigtype, SIG_ROOT, crt);
1945 _LOGE("__ri_create_cert_chain(%d) failed.", sigtype);
1946 __ri_free_cert_chain();
1947 ret = RPM_INSTALLER_ERR_INTERNAL;
1951 err = cert_svc_get_visibility(ctx, visibility);
1953 _LOGE("cert_svc_get_visibility() failed. err = [%d]", err);
1954 ret = RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED;
1957 _LOGD("cert_svc_get_visibility() returns visibility=[%d]", *visibility);*/
1959 _LOGE("No Root CA cert found. Signature validation failed.");
1960 ret = RPM_INSTALLER_ERR_ROOT_CERT_NOT_FOUND;
1967 cert_svc_cert_context_final(ctx);
1969 _ri_free_signature_xml(signx);
1974 int _ri_verify_signatures(const char *root_path, const char *pkgid)
1977 char buff[BUF_SIZE] = {'\0'};
1978 char *pkg_path = NULL;
1981 _LOGD("root_path=[%s], pkgid=[%s]", root_path, pkgid);
1983 // check for signature and certificate
1984 pkg_path = __get_pkg_path(root_path, pkgid);
1985 if (pkg_path == NULL) {
1986 _LOGE("__get_pkg_path(%s, %s) failed.", root_path, pkgid);
1990 _LOGD("switched to pkg_path=[%s]", pkg_path);
1992 // author-signature.xml is mandatory
1993 snprintf(buff, BUF_SIZE, "%s/author-signature.xml", pkg_path);
1994 if (access(buff, F_OK) == 0) {
1995 _LOGD("author-signature.xml, path=[%s]", buff);
1996 ret = _ri_verify_sig_and_cert(buff, &visibility);
1998 _LOGE("_ri_verify_sig_and_cert(%s) failed.", buff);
2002 _LOGD("------------------------------------------------------");
2003 _LOGD("signature is verified successfully");
2004 _LOGD("path=[%s]", buff);
2005 _LOGD("------------------------------------------------------");
2007 memset(buff, '\0', BUF_SIZE);
2009 // signature2.xml is optional
2010 snprintf(buff, BUF_SIZE, "%s/signature2.xml", pkg_path);
2011 if (access(buff, F_OK) == 0) {
2012 _LOGD("signature2.xml found. [%s]", pkg_path);
2013 ret = _ri_verify_sig_and_cert(buff, &visibility);
2015 _LOGE("_ri_verify_sig_and_cert(%s) failed.", buff);
2019 _LOGD("_ri_verify_sig_and_cert(%s) succeed.", buff);
2021 memset(buff, '\0', BUF_SIZE);
2023 // signature1.xml is mandatory
2024 snprintf(buff, BUF_SIZE, "%s/signature1.xml", pkg_path);
2025 if (access(buff, F_OK) == 0) {
2026 _LOGD("signature1.xml, path=[%s]", buff);
2027 ret = _ri_verify_sig_and_cert(buff, &visibility);
2029 _LOGE("_ri_verify_sig_and_cert(%s) failed.", buff);
2033 _LOGD("------------------------------------------------------");
2034 _LOGD("signature is verified successfully");
2035 _LOGD("path=[%s]", buff);
2036 _LOGD("------------------------------------------------------");
2038 memset(buff, '\0', BUF_SIZE);
2048 if ((ret != 0) && (sig_enable == 0)) {
2049 _LOGD("_ri_verify_signatures(%s, %s) failed, but it's ok for config.", root_path, pkgid);
2056 void _ri_apply_smack(char *pkgname, int flag)
2058 __rpm_apply_smack(pkgname, flag);
2061 int _ri_apply_privilege(char *pkgid, int visibility)
2065 pkgmgrinfo_pkginfo_h handle = NULL;
2067 int apptype = PERM_APP_TYPE_EFL;
2069 memset(&info, '\0', sizeof(info));
2071 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
2072 if (ret != PMINFO_R_OK)
2075 strncpy(info.package_id, pkgid, strlen(pkgid));
2076 info.visibility = visibility;
2078 ret = pkgmgrinfo_pkginfo_foreach_privilege(handle, __privilege_func, (void *)&info);
2079 if (ret != PMINFO_R_OK) {
2080 _LOGE("pkgmgrinfo_pkginfo_get_pkgid failed\n");
2081 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
2084 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
2086 if (visibility & CERT_SVC_VISIBILITY_PLATFORM) {
2087 _LOGD("VISIBILITY_PLATFORM!");
2088 apptype = PERM_APP_TYPE_EFL_PLATFORM;
2089 } else if ((visibility & CERT_SVC_VISIBILITY_PARTNER) ||
2090 (visibility & CERT_SVC_VISIBILITY_PARTNER_OPERATOR) ||
2091 (visibility & CERT_SVC_VISIBILITY_PARTNER_MANUFACTURER)) {
2092 _LOGD("VISIBILITY_PARTNER!");
2093 apptype = PERM_APP_TYPE_EFL_PARTNER;
2096 /*reload privilege*/
2097 const char *perm[] = {NULL, NULL};
2098 ret = _ri_privilege_enable_permissions(pkgid, apptype, perm, 1);
2103 int _ri_set_group_id(const char *pkgid, const char *groupid)
2105 retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "pkgid is NULL\n");
2106 retvm_if(groupid == NULL, PMINFO_R_EINVAL, "groupid is NULL\n");
2108 sqlite3 *pkginfo_db = NULL;
2112 ret = db_util_open(PKGMGR_DB, &pkginfo_db, 0);
2113 retvm_if(ret != SQLITE_OK, PMINFO_R_ERROR, "connect db [%s] failed!", PKGMGR_DB);
2115 /*Begin transaction*/
2116 ret = sqlite3_exec(pkginfo_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2117 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Failed to begin transaction\n");
2118 _LOGD("Transaction Begin\n");
2120 query = sqlite3_mprintf("update package_info set package_reserve3=%Q where package=%Q", groupid, pkgid);
2122 ret = sqlite3_exec(pkginfo_db, query, NULL, NULL, NULL);
2123 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
2125 /*Commit transaction*/
2126 ret = sqlite3_exec(pkginfo_db, "COMMIT", NULL, NULL, NULL);
2127 if (ret != SQLITE_OK) {
2128 _LOGE("Failed to commit transaction. Rollback now\n");
2129 ret = sqlite3_exec(pkginfo_db, "ROLLBACK", NULL, NULL, NULL);
2130 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
2132 _LOGD("Transaction Commit and End\n");
2136 sqlite3_free(query);
2137 sqlite3_close(pkginfo_db);
2141 static int __ri_install_fota_for_rw(char *pkgid)
2146 char buff[BUF_SIZE] = {'\0'};
2148 _LOGD("fota-info : pkgid[%s] start installation\n", pkgid);
2150 /*unzip pkg path from factoryrest data*/
2151 snprintf(buff, BUF_SIZE, "opt/usr/apps/%s/*", pkgid);
2152 const char *pkg_argv[] = { "/usr/bin/unzip", "-oX", OPT_ZIP_FILE, buff, "-d", "/", NULL };
2153 ret = _ri_xsystem(pkg_argv);
2155 _LOGE("fota-info : unzip root path[%s] is fail .\n", buff);
2159 _LOGD("fota-info : unzip root path[%s] is success\n", buff);
2161 /*unzip manifest from factoryrest data*/
2162 memset(buff, '\0', BUF_SIZE);
2163 snprintf(buff, BUF_SIZE, "opt/share/packages/%s.xml", pkgid);
2165 const char *xml_argv[] = { "/usr/bin/unzip", "-oX", OPT_ZIP_FILE, buff, "-d", "/", NULL };
2166 ret = _ri_xsystem(xml_argv);
2168 _LOGE("fota-info : xml_argv fail for pkgid[%s] .\n", pkgid);
2172 _LOGD("fota-info : xml_argv[%s] is success\n", pkgid);
2174 /*get updated manifest path*/
2175 memset(buff, '\0', BUF_SIZE);
2176 snprintf(buff, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
2177 _LOGE("Manifest name is %s\n", buff);
2179 /*apply smack for manifest*/
2180 _ri_privilege_change_smack_label(buff, pkgid, 0);/*0 is SMACK_LABEL_ACCESS*/
2182 /*register manifest*/
2183 char* fota_tags[3] = {NULL, };
2184 fota_tags[0] = "removable=true";
2185 fota_tags[1] = "preload=true";
2186 fota_tags[2] = NULL;
2188 ret = pkgmgr_parser_parse_manifest_for_installation(buff, fota_tags);
2190 _LOGE("Parsing Manifest Failed\n");
2194 _LOGD("Parsing Manifest Success\n");
2197 /*Register cert info*/
2198 _ri_register_cert(pkgid);
2200 /*apply smack for pkg root path*/
2201 memset(buff, '\0', BUF_SIZE);
2202 snprintf(buff, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgid);
2203 _ri_privilege_setup_path(pkgid, buff, PERM_APP_PATH_ANY_LABEL, pkgid);
2205 /*apply smack for defined directory*/
2206 __rpm_apply_smack(pkgid, home_dir);
2209 ret = _ri_apply_privilege(pkgid, 0);
2211 _LOGE("apply perm failed with err(%d)\n", ret);
2213 _LOGD("apply perm success\n");
2222 static int __ri_upgrade_fota_for_rw(char *pkgid)
2227 char buff[BUF_SIZE] = {'\0'};
2229 _LOGD("fota-info : pkgid[%s] start upgrade\n", pkgid);
2231 /*unzip pkg dir from factoryrest data*/
2232 snprintf(buff, BUF_SIZE, "opt/usr/apps/%s/*", pkgid);
2233 const char *pkg_argv[] = { "/usr/bin/unzip", "-oX", OPT_ZIP_FILE, buff, "-d", "/", NULL };
2234 ret = _ri_xsystem(pkg_argv);
2236 _LOGE("fota-info : unzip root path[%s] is fail .\n", buff);
2240 _LOGD("fota-info : unzip root path[%s] is success\n", buff);
2242 /*unzip manifest from factoryrest data*/
2243 memset(buff, '\0', BUF_SIZE);
2244 snprintf(buff, BUF_SIZE, "opt/share/packages/%s.xml", pkgid);
2245 const char *xml_argv[] = { "/usr/bin/unzip", "-oX", OPT_ZIP_FILE, buff, "-d", "/", NULL };
2246 ret = _ri_xsystem(xml_argv);
2248 _LOGE("fota-info : unzip manifest[%s] is fail .\n", buff);
2252 _LOGD("fota-info : unzip manifest[%s] is success\n", buff);
2254 /*get updated manifest path*/
2255 memset(buff, '\0', BUF_SIZE);
2256 snprintf(buff, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
2257 _LOGE("Manifest name is %s\n", buff);
2259 /*apply smack for manifest*/
2260 _ri_privilege_change_smack_label(buff, pkgid, 0);/*0 is SMACK_LABEL_ACCESS*/
2262 /*register manifest*/
2263 ret = pkgmgr_parser_parse_manifest_for_upgrade(buff, NULL);
2265 _LOGE("Parsing Manifest Failed\n");
2269 _LOGD("Parsing Manifest Success\n");
2272 /*Register new cert info*/
2273 _ri_unregister_cert(pkgid);
2274 _ri_register_cert(pkgid);
2276 /*apply smack for pkg root path*/
2277 memset(buff, '\0', BUF_SIZE);
2278 snprintf(buff, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgid);
2279 _ri_privilege_setup_path(pkgid, buff, PERM_APP_PATH_ANY_LABEL, pkgid);
2281 /*apply smack for defined directory*/
2282 __rpm_apply_smack(pkgid, home_dir);
2285 ret = _ri_apply_privilege(pkgid, 0);
2287 _LOGE("apply perm failed with err(%d)\n", ret);
2289 _LOGD("apply perm success\n");
2298 static int __ri_uninstall_fota_for_rw(char *pkgid)
2301 char buff[BUF_SIZE] = {'\0'};
2303 _LOGD("fota-info : pkgid[%s] start uninstall\n", pkgid);
2305 /*del root path dir*/
2306 snprintf(buff, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgid);
2308 if (__is_dir(buff)) {
2309 _rpm_delete_dir(buff);
2313 memset(buff, '\0', BUF_SIZE);
2314 snprintf(buff, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
2318 ret = pkgmgr_parser_parse_manifest_for_uninstallation(pkgid, NULL);
2320 _LOGE("Parsing Manifest Failed\n");
2323 /*execute privilege APIs*/
2324 _ri_privilege_revoke_permissions(pkgid);
2325 _ri_privilege_unregister_package(pkgid);
2327 /*Unregister cert info*/
2328 _ri_unregister_cert(pkgid);
2334 * callback for the pkgmgrinfo_appinfo_get_list used in _rpm_uninstall_pkg()
2336 int __ri_check_running_app(const pkgmgrinfo_appinfo_h handle, void *user_data)
2341 app_context_h appCtx = NULL;
2343 ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
2345 _LOGE("Failed to execute pkgmgrinfo_appinfo_get_appid[%d].\n", ret);
2349 ret = app_manager_is_running(appid, &isRunning);
2351 _LOGE("Failed to execute app_manager_is_running[%d].\n", ret);
2354 _LOGE("app[%s] , running state[%d].\n", appid, isRunning);
2357 ret = app_manager_get_app_context(appid, &appCtx);
2359 _LOGE("Failed to execute app_manager_get_app_context[%d].\n", ret);
2363 ret = app_manager_terminate_app(appCtx);
2365 _LOGE("Failed to execute app_manager_terminate_app[%d].\n", ret);
2366 app_context_destroy(appCtx);
2371 for (i = 0; i < TERMINATE_RETRY_COUNT; i++) {
2372 ret = app_manager_is_running(appid, &isRunning);
2374 _LOGE("Failed to execute app_manager_is_running[%d].\n", ret);
2375 app_context_destroy(appCtx);
2380 _LOGD("App(%s) is terminated.\n", appid);
2383 _LOGD("App(%s) is not terminated yet. wait count=[%d].\n", appid, i);
2388 ret = app_context_destroy(appCtx);
2390 _LOGE("Failed to execute app_context_destroy[%d].\n", ret);
2398 int __ri_change_dir(char *dirname)
2402 ret = mkdir(dirname, 0644);
2404 if (access(dirname, F_OK) == 0) {
2405 _rpm_delete_dir(dirname);
2406 ret = mkdir(dirname, 0644);
2408 _LOGE("mkdir(%s) failed\n", dirname);
2412 _LOGE("can not access[%s]\n", dirname);
2417 ret = chdir(dirname);
2419 _LOGE("chdir(%s) failed [%s]\n", dirname, strerror(errno));
2425 int _ri_smack_reload(const char *pkgid, rpm_request_type request_type)
2428 char *op_type = NULL;
2430 switch (request_type) {
2432 op_type = strdup("install");
2436 op_type = strdup("update");
2440 op_type = strdup("uninstall");
2447 if(op_type == NULL) {
2448 _LOGE("@Failed to reload smack. request_type not matched[pkgid=%s, op=%s]", pkgid, op_type);
2452 const char *smack_argv[] = { "/usr/bin/smack_reload.sh", op_type, pkgid, NULL };
2453 ret = _ri_xsystem(smack_argv);
2455 _LOGE("@Failed to reload smack[pkgid=%s, op=%s].", pkgid, op_type);
2457 _LOGD("#success: smack reload[pkgid=%s, op=%s]", pkgid, op_type);
2466 int _ri_smack_reload_all(void)
2470 const char *smack_argv[] = { "/usr/bin/smackload-fast", NULL};
2471 ret = _ri_xsystem(smack_argv);
2473 _LOGE("@Failed to reload all smack : %d", errno);
2475 _LOGD("#success: smack reload all");
2481 void __ri_remove_updated_dir(const char *pkgid)
2483 char path_buf[BUF_SIZE] = {'\0'};
2486 if (pkgid == NULL) {
2487 _LOGE("pkgid is NULL.");
2492 snprintf(path_buf, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, BIN_DIR_STR);
2493 if (__is_dir(path_buf)) {
2494 _LOGE("@pkgid[%s] need to clean dir[%s]\n", pkgid, path_buf);
2495 _rpm_delete_dir(path_buf);
2499 memset(path_buf, '\0', BUF_SIZE);
2500 snprintf(path_buf, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, RES_DIR_STR);
2501 if (__is_dir(path_buf)) {
2502 _LOGE("@pkgid[%s] need to clean dir[%s]\n", pkgid, path_buf);
2503 _rpm_delete_dir(path_buf);
2506 // remove shared/res dir
2507 memset(path_buf, '\0', BUF_SIZE);
2508 snprintf(path_buf, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, SHARED_RES_DIR_STR);
2509 if (__is_dir(path_buf)) {
2510 _LOGE("@pkgid[%s] need to clean dir[%s]\n", pkgid, path_buf);
2511 _rpm_delete_dir(path_buf);
2515 static int __metadata_func(const char *key, const char *value, void *user_data)
2521 _LOGE("key is null\n");
2524 if (value == NULL) {
2525 _LOGE("value is null\n");
2528 if (user_data == NULL) {
2529 _LOGE("user_data is null\n");
2533 if ((strcmp(key, "launch-on-attach") == 0) && (strcmp(value, "true") == 0)) {
2534 _LOGE("consumer[%s] : launch-on-attach is true \n", (char *)user_data);
2536 ret = app_manager_is_running((char *)user_data, &isRunning);
2538 _LOGE("Failed to execute app_manager_is_running[%s].\n", (char *)user_data);
2543 _LOGE("consumer[%s] is already launched \n", (char *)user_data);
2545 usleep(100 * 1000); /* 100ms sleep for infomation ready*/
2546 ret = aul_launch_app((char *)user_data, NULL);
2547 if (ret == AUL_R_ERROR) {
2548 _LOGE("consumer[%s] launch fail, sleep and retry launch_app\n", (char *)user_data);
2549 usleep(100 * 1000); /* 100ms sleep for infomation ready*/
2550 aul_launch_app((char *)user_data, NULL);
2552 _LOGE("consumer[%s] is launched !!!! \n", (char *)user_data);
2558 static int __ri_find_svcapp(const pkgmgrinfo_appinfo_h handle, void *user_data)
2562 char *component_type = NULL;
2564 ret = pkgmgrinfo_appinfo_get_component_type(handle, &component_type);
2565 if (ret != PMINFO_R_OK) {
2566 _LOGE("@Failed to get component_type\n");
2570 if (strcmp(component_type, "svcapp") == 0) {
2571 ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
2572 if (ret != PMINFO_R_OK) {
2573 _LOGE("@Failed to get appid\n");
2576 _LOGE("@find consumer[%s], check metadata for launch\n", appid);
2578 ret = pkgmgrinfo_appinfo_foreach_metadata(handle, __metadata_func, (void *)appid);
2579 if (ret != PMINFO_R_OK) {
2580 _LOGE("@Failed to get foreach_metadata\n");
2588 static void __ri_launch_consumer(const char *pkgid)
2591 pkgmgrinfo_pkginfo_h pkghandle = NULL;
2593 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkghandle);
2595 _LOGE("@Failed to get pkginfo handle [%s]\n", pkgid);
2599 ret = pkgmgrinfo_appinfo_get_list(pkghandle, PM_UI_APP, __ri_find_svcapp, NULL);
2601 _LOGE("@Failed to get appinfo_get_list [%s]\n", pkgid);
2604 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
2607 static int __ail_change_info(int op, const char *appid)
2609 void *lib_handle = NULL;
2610 int (*ail_desktop_operation) (const char *);
2614 if ((lib_handle = dlopen(LIBAIL_PATH, RTLD_LAZY)) == NULL) {
2615 _LOGE("dlopen is failed LIBAIL_PATH[%s]\n", LIBAIL_PATH);
2622 aop = "ail_desktop_add";
2625 aop = "ail_desktop_update";
2628 aop = "ail_desktop_remove";
2631 aop = "ail_desktop_clean";
2634 aop = "ail_desktop_fota";
2641 if ((ail_desktop_operation =
2642 dlsym(lib_handle, aop)) == NULL || dlerror() != NULL) {
2643 _LOGE("can not find symbol \n");
2648 ret = ail_desktop_operation(appid);
2652 dlclose(lib_handle);
2657 static int __ri_update_ail_info(const pkgmgrinfo_appinfo_h handle, void *user_data)
2663 ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
2665 _LOGE("Failed to execute pkgmgrinfo_appinfo_get_appid[%d].\n", ret);
2669 ret = __ail_change_info(AIL_INSTALL, appid);
2671 _LOGE("Failed to execute __ail_change_info[%s].\n", appid);
2679 static int __child_list_cb (const pkgmgrinfo_pkginfo_h handle, void *user_data)
2684 ret = pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid);
2686 _LOGE("get_pkgid failed\n");
2690 _LOGD("@child pkgid is [%s] for uninstallation", pkgid);
2692 ret = _rpm_uninstall_pkg_with_dbpath(pkgid, 0);
2694 _LOGE("uninstall pkg(%s) failed\n",pkgid );
2700 static void __uninstall_child_package_by_mother_pkgid(const char *pkgid)
2704 pkgmgrinfo_pkginfo_filter_h handle = NULL;
2706 ret = pkgmgrinfo_pkginfo_filter_create(&handle);
2708 _LOGE("filter_create failed for (%s)\n", pkgid);
2712 ret = pkgmgrinfo_pkginfo_filter_add_string(handle, PMINFO_PKGINFO_PROP_PACKAGE_STORECLIENT_ID, pkgid);
2714 _LOGE("PMINFO_PKGINFO_PROP_PACKAGE_STORECLIENT_ID add failed\n");
2718 ret = pkgmgrinfo_pkginfo_filter_foreach_pkginfo(handle, __child_list_cb, NULL);
2720 _LOGE("foreach_pkginfo failed\n");
2724 pkgmgrinfo_pkginfo_filter_destroy(handle);
2728 int _rpm_install_pkg_with_dbpath(char *pkgfilepath, char *pkgid, char *clientid)
2731 char manifest[BUF_SIZE] = { '\0'};
2732 char resultxml[BUF_SIZE] = {'\0'};
2733 char cwd[BUF_SIZE] = {'\0'};
2736 #ifdef APP2EXT_ENABLE
2737 app2ext_handle *handle = NULL;
2738 GList *dir_list = NULL;
2739 pkgmgrinfo_install_location location = 1;
2741 unsigned long rpm_size = 0;
2743 /*send event for start*/
2744 _ri_broadcast_status_notification(pkgid, "rpm", "start", "install");
2745 _LOGD("[#]start : _rpm_install_pkg_with_dbpath");
2748 temp = getcwd(cwd, BUF_SIZE);
2749 if ((temp == NULL) ||(cwd[0] == '\0')) {
2750 _LOGE("@failed to get the current directory info.");
2751 ret = RPM_INSTALLER_ERR_INTERNAL;
2754 _LOGD("#current working directory is %s", cwd);
2757 ret = __ri_change_dir(TEMP_DIR);
2759 _LOGE("@failed to change directory.");
2760 ret = RPM_INSTALLER_ERR_INTERNAL;
2763 _LOGD("#switched to %s", TEMP_DIR);
2766 const char *cpio_argv[] = { CPIO_SCRIPT, pkgfilepath, NULL };
2767 ret = _ri_xsystem(cpio_argv);
2769 /*get manifext.xml path*/
2770 snprintf(manifest, BUF_SIZE, "%s/opt/share/packages/%s.xml", TEMP_DIR, pkgid);
2771 _LOGD("#manifest name is %s", manifest);
2773 if (access(manifest, F_OK)) {
2774 _LOGD("#there is no RW manifest.xml. check RO manifest.xml.");
2776 memset(manifest, '\0', sizeof(manifest));
2777 snprintf(manifest, BUF_SIZE, "%s/usr/share/packages/%s.xml", TEMP_DIR, pkgid);
2778 _LOGD("#manifest name is %s", manifest);
2780 if (access(manifest, F_OK)) {
2781 _LOGE("@can not find manifest.xml in the pkg.");
2782 ret = RPM_INSTALLER_ERR_NO_MANIFEST;
2788 #if 0 //disable "copy ro-xml to rw-xml", because of some bug
2789 snprintf(srcpath, BUF_SIZE, "%s", manifest);
2790 memset(manifest, '\0', sizeof(manifest));
2791 snprintf(manifest, BUF_SIZE, "%s/%s.xml", MANIFEST_RW_DIRECTORY, pkgid);
2793 const char *xml_update_argv[] = { CPIO_SCRIPT_UPDATE_XML, srcpath, manifest, NULL };
2794 ret = _ri_xsystem(xml_update_argv);
2799 /*send event for install_percent*/
2800 _ri_broadcast_status_notification(pkgid, "rpm", "install_percent", "30");
2802 /*check manifest.xml validation*/
2803 ret = pkgmgr_parser_check_manifest_validation(manifest);
2805 _LOGE("@invalid manifest");
2806 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
2810 /*check for signature and certificate*/
2811 ret = _ri_verify_signatures(TEMP_DIR, pkgid);
2813 _LOGE("@signature and certificate failed(%s).", pkgid);
2814 ret = RPM_INSTALLER_ERR_SIG_INVALID;
2817 _LOGD("#_ri_verify_signatures success.");
2822 _LOGE("@failed to change directory(%s)(%s)", cwd, strerror(errno));
2823 ret = RPM_INSTALLER_ERR_INTERNAL;
2827 #ifdef APP2EXT_ENABLE
2828 ret = __get_location_from_xml(manifest, &location);
2830 _LOGE("@Failed to get install location\n");
2831 ret = RPM_INSTALLER_ERR_INTERNAL;
2834 if (location == PMINFO_INSTALL_LOCATION_PREFER_EXTERNAL) {
2835 _LOGD("#Install: external storage location");
2837 /*Get the rpm's size from rpm header*/
2838 rpm_size = _ri_calculate_rpm_size(pkgfilepath);
2840 rpm_size = rpm_size/(1024*1024); //rpm size in MB
2841 _LOGD("#Rpm file(%s) size is %lu MB",pkgfilepath,rpm_size);
2843 /*Add margin to the rpm size*/
2844 rpm_size = rpm_size + RPM_SIZE_MARGIN(rpm_size);
2845 _LOGD("#Rpm file (%s) size after margin is %lu MB",pkgfilepath,rpm_size);
2847 _LOGE("@Failed to get size from rpm header\n");
2848 ret = RPM_INSTALLER_ERR_INTERNAL;
2853 Get the size from the manifest file.
2855 ret = __get_size_from_xml(manifest, &size);
2856 if (ret != PMINFO_R_OK) {
2858 _LOGD(" #rpm size is %d MB",size);
2860 size = size>rpm_size?size:rpm_size;
2861 _LOGD("#rpm size is %d MB",size);
2866 if ((location == PMINFO_INSTALL_LOCATION_PREFER_EXTERNAL) && size > 0) {
2867 handle = app2ext_init(APP2EXT_SD_CARD);
2868 if (handle == NULL) {
2869 _LOGE("@app2ext init failed\n");
2870 ret = RPM_INSTALLER_ERR_INTERNAL;
2873 if ((&(handle->interface) != NULL) && (handle->interface.pre_install != NULL) && (handle->interface.post_install != NULL)){
2874 dir_list = __rpm_populate_dir_list();
2875 if (dir_list == NULL) {
2876 _LOGE("@ \nError in populating the directory list\n");
2877 app2ext_deinit(handle);
2878 ret = RPM_INSTALLER_ERR_RPM_SCRIPT_WRONG_ARGS;
2881 ret = handle->interface.pre_install(gpkgname, dir_list, size);
2882 if (ret == APP2EXT_ERROR_MMC_STATUS) {
2883 _LOGE("@app2xt MMC is not here, go internal\n");
2884 } else if (ret == APP2EXT_SUCCESS){
2885 _LOGE("@pre_install done, go internal\n");
2888 _LOGE("@app2xt pre install API failed (%d)\n", ret);
2889 __rpm_clear_dir_list(dir_list);
2890 handle->interface.post_install(gpkgname, APP2EXT_STATUS_FAILED);
2891 app2ext_deinit(handle);
2892 ret = RPM_INSTALLER_ERR_INTERNAL;
2901 if (home_dir == 0) {
2902 #if 0 //disable "INSTALL_SCRIPT_WITH_DBPATH_RO", because of some bug
2903 const char *argv[] = { INSTALL_SCRIPT_WITH_DBPATH_RO, pkgfilepath, NULL };
2904 ret = _ri_xsystem(argv);
2906 const char *argv[] = { INSTALL_SCRIPT, pkgfilepath, NULL };
2907 ret = _ri_xsystem(argv);
2909 const char *argv[] = { INSTALL_SCRIPT_WITH_DBPATH_RW, pkgfilepath, NULL };
2910 ret = _ri_xsystem(argv);
2913 _LOGE("@failed to install the pkg(%d).", ret);
2914 #ifdef APP2EXT_ENABLE
2915 if ((handle != NULL) && (handle->interface.post_install != NULL)){
2916 __rpm_clear_dir_list(dir_list);
2917 handle->interface.post_install(gpkgname, APP2EXT_STATUS_FAILED);
2918 app2ext_deinit(handle);
2923 _LOGD("#install success.");
2925 /*write the storeclient-id to manifest.xml*/
2926 if (clientid != NULL) {
2927 if (home_dir == 0) {
2928 snprintf(resultxml, BUF_SIZE, "%s/%s.xml", USR_SHARE_PACKAGES, pkgid);
2930 snprintf(resultxml, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
2933 const char *convert_argv[] = { RPM_UPDATE_XML, manifest, clientid, resultxml, NULL };
2934 ret = _ri_xsystem(convert_argv);
2936 _LOGE("@Failed to convert the manifest.xml");
2940 _LOGD("#client id[%s], input manifest:[%s], dest manifest:[%s]", clientid, manifest, resultxml);
2943 /*send event for install_percent*/
2944 _ri_broadcast_status_notification(pkgid, "rpm", "install_percent", "60");
2946 #ifdef APP2EXT_ENABLE
2947 if ((handle != NULL) && (handle->interface.post_install != NULL)){
2948 __rpm_clear_dir_list(dir_list);
2949 handle->interface.post_install(gpkgname, APP2EXT_STATUS_SUCCESS);
2950 app2ext_deinit(handle);
2954 /*Parse the manifest to get install location and size. If installation fails, remove manifest info from DB*/
2955 if (clientid != NULL) {
2956 ret = pkgmgr_parser_parse_manifest_for_installation(resultxml, NULL);
2958 ret = pkgmgr_parser_parse_manifest_for_installation(manifest, NULL);
2961 _LOGE("@failed to parse the manifest.");
2962 ret = RPM_INSTALLER_ERR_INTERNAL;
2965 _LOGD("#manifest parsing success");
2967 /*register cert info*/
2968 _ri_register_cert(pkgid);
2971 _coretpk_installer_search_ui_gadget(pkgid);
2973 /*apply smack to shared dir*/
2974 __rpm_apply_smack(pkgid, 1);
2976 /*apply smack by privilege*/
2977 ret = _ri_apply_privilege(pkgid, 0);
2979 _LOGE("@failed to apply permission(%d).", ret);
2981 _LOGD("#permission applying success.");
2984 ret = _ri_smack_reload_all();
2986 _LOGD("@failed to reload_all the smack.");
2989 /*send event for install_percent*/
2990 _ri_broadcast_status_notification(pkgid, "rpm", "install_percent", "100");
2993 _rpm_delete_dir(TEMP_DIR);
2994 _rpm_delete_dir(TEMP_DBPATH);
2996 if (ret == RPM_INSTALLER_SUCCESS) {
2997 _LOGD("[#]end : _rpm_install_pkg_with_dbpath");
2998 __ri_launch_consumer(pkgid);
2999 _ri_broadcast_status_notification(pkgid, "rpm", "end", "ok");
3000 _ri_stat_cb(pkgid, "end", "ok");
3002 _LOGE("[@]end : _rpm_install_pkg_with_dbpath");
3004 ret = _coretpk_installer_remove_db_info(pkgid);
3006 _LOGE("_coretpk_installer_remove_db_info is failed.");
3009 char *errstr = NULL;
3010 _ri_error_no_to_string(ret, &errstr);
3011 _ri_broadcast_status_notification(pkgid, "rpm", "error", errstr);
3012 _ri_stat_cb(pkgid, "error", errstr);
3013 _ri_broadcast_status_notification(pkgid, "rpm", "end", "fail");
3015 _ri_stat_cb(pkgid, "end", "fail");
3016 _LOGE("install failed with err(%d) (%s)\n", ret, errstr);
3022 int _rpm_upgrade_pkg_with_dbpath(char *pkgfilepath, char *pkgid)
3025 char manifest[BUF_SIZE] = { '\0'};
3026 char cwd[BUF_SIZE] = {'\0'};
3028 pkgmgrinfo_pkginfo_h pkghandle;
3030 #ifdef APP2EXT_ENABLE
3031 app2ext_handle *handle = NULL;
3032 GList *dir_list = NULL;
3033 pkgmgrinfo_installed_storage location = 1;
3035 unsigned long rpm_size = 0;
3037 _ri_broadcast_status_notification(pkgid, "rpm", "start", "update");
3038 _LOGD("[#]start : _rpm_upgrade_pkg_with_dbpath");
3040 /*terminate running app*/
3041 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkghandle);
3043 _LOGE("@failed to get pkginfo handle");
3044 ret = RPM_INSTALLER_ERR_PKG_NOT_FOUND;
3047 pkgmgrinfo_appinfo_get_list(pkghandle, PM_UI_APP, __ri_check_running_app, NULL);
3048 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
3051 temp = getcwd(cwd, BUF_SIZE);
3052 if ((temp == NULL) ||(cwd[0] == '\0')) {
3053 _LOGE("@getcwd() failed.");
3054 ret = RPM_INSTALLER_ERR_INTERNAL;
3057 _LOGD("#current working directory is %s.", cwd);
3060 ret = __ri_change_dir(TEMP_DIR);
3062 _LOGE("@change dir failed.");
3063 ret = RPM_INSTALLER_ERR_INTERNAL;
3066 _LOGD("#switched to %s", TEMP_DIR);
3069 const char *cpio_argv[] = { CPIO_SCRIPT, pkgfilepath, NULL };
3070 ret = _ri_xsystem(cpio_argv);
3072 /*get manifext.xml path*/
3073 snprintf(manifest, BUF_SIZE, "%s/opt/share/packages/%s.xml", TEMP_DIR, pkgid);
3074 _LOGD("#manifest name is %s.", manifest);
3076 if (access(manifest, F_OK)) {
3077 _LOGD("#there is no RW manifest.xml. check RO manifest.xml.");
3079 memset(manifest, '\0', sizeof(manifest));
3080 snprintf(manifest, BUF_SIZE, "%s/usr/share/packages/%s.xml", TEMP_DIR, pkgid);
3081 _LOGD("#manifest name is %s.", manifest);
3083 if (access(manifest, F_OK)) {
3084 _LOGE("@can not find manifest.xml in the pkg.");
3085 ret = RPM_INSTALLER_ERR_NO_MANIFEST;
3091 #if 0 //disable "copy ro-xml to rw-xml", because of some bug
3092 snprintf(srcpath, BUF_SIZE, "%s", manifest);
3093 memset(manifest, '\0', sizeof(manifest));
3094 snprintf(manifest, BUF_SIZE, "%s/%s.xml", MANIFEST_RW_DIRECTORY, pkgid);
3096 const char *xml_update_argv[] = { CPIO_SCRIPT_UPDATE_XML, srcpath, manifest, NULL };
3097 ret = _ri_xsystem(xml_update_argv);
3103 /*send event for install_percent*/
3104 _ri_broadcast_status_notification(pkgid, "rpm", "install_percent", "30");
3106 /*check manifest.xml validation*/
3107 ret = pkgmgr_parser_check_manifest_validation(manifest);
3109 _LOGE("@invalid manifest");
3110 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
3114 /*check for signature and certificate*/
3115 ret = _ri_verify_signatures(TEMP_DIR, pkgid);
3117 _LOGE("@signature and certificate failed(%s).", pkgid);
3118 ret = RPM_INSTALLER_ERR_SIG_INVALID;
3121 _LOGD("#_ri_verify_signatures success.");
3126 _LOGE("@chdir(%s) failed(%s).", cwd, strerror(errno));
3127 ret = RPM_INSTALLER_ERR_INTERNAL;
3131 /*remove dir for clean*/
3132 __ri_remove_updated_dir(pkgid);
3134 _LOGD("#Preserve the smack file");
3135 /*Preserve the smack rule file */
3136 ret = __ri_copy_smack_rule_file(UPGRADE_REQ,pkgid,0);
3137 if(ret != RPM_INSTALLER_SUCCESS)
3139 #ifdef APP2EXT_ENABLE
3140 ret = pkgmgrinfo_pkginfo_get_pkginfo(gpkgname, &pkghandle);
3142 _LOGE("Failed to get pkginfo handle\n");
3143 ret = RPM_INSTALLER_ERR_PKG_NOT_FOUND;
3146 ret = pkgmgrinfo_pkginfo_get_installed_storage(pkghandle, &location);
3148 _LOGE("Failed to get install location\n");
3149 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
3150 ret = RPM_INSTALLER_ERR_INTERNAL;
3153 if (location == PMINFO_EXTERNAL_STORAGE) {
3154 /*Get the rpm's size from rpm header*/
3155 rpm_size = _ri_calculate_rpm_size(pkgfilepath);
3157 rpm_size = rpm_size/(1024*1024); //rpm size in MB
3158 _LOGD("#Rpm file(%s) size is %lu MB",pkgfilepath,rpm_size);
3160 /*Add margin to the rpm size*/
3161 rpm_size = rpm_size + RPM_SIZE_MARGIN(rpm_size);
3162 _LOGD("#Rpm file (%s) size after margin is %lu MB",pkgfilepath,rpm_size);
3164 _LOGE("@Failed to get size from rpm header\n");
3165 ret = RPM_INSTALLER_ERR_INTERNAL;
3170 Get the size from the manifest file.
3172 ret = __get_size_from_xml(manifest, &size);
3173 if (ret != PMINFO_R_OK) {
3175 _LOGD(" #rpm size is %d",size);
3177 size = size>rpm_size?size:rpm_size;
3178 _LOGD("#rpm size is %d",size);
3182 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
3183 if ((location == PMINFO_EXTERNAL_STORAGE) && size > 0) {
3184 handle = app2ext_init(APP2EXT_SD_CARD);
3185 if (handle == NULL) {
3186 _LOGE("app2ext init failed\n");
3187 ret = RPM_INSTALLER_ERR_INTERNAL;
3190 if ((&(handle->interface) != NULL) && (handle->interface.pre_upgrade != NULL) && (handle->interface.post_upgrade != NULL)){
3191 dir_list = __rpm_populate_dir_list();
3192 if (dir_list == NULL) {
3193 _LOGE("\nError in populating the directory list\n");
3194 ret = RPM_INSTALLER_ERR_RPM_SCRIPT_WRONG_ARGS;
3195 app2ext_deinit(handle);
3198 ret = handle->interface.pre_upgrade(gpkgname, dir_list, size);
3199 if (ret == APP2EXT_ERROR_MMC_STATUS ) {
3200 _LOGE("app2xt MMC is not here, go internal (%d)\n", ret);
3201 }else if(ret == APP2EXT_SUCCESS){
3202 _LOGE("pre upgrade done, go internal");
3204 _LOGE("app2xt pre upgrade API failed (%d)\n", ret);
3205 __rpm_clear_dir_list(dir_list);
3206 handle->interface.post_upgrade(gpkgname, APP2EXT_STATUS_FAILED);
3207 ret = RPM_INSTALLER_ERR_INTERNAL;
3208 app2ext_deinit(handle);
3217 if (home_dir == 0) {
3218 #if 0 //disable "UPGRADE_SCRIPT_WITH_DBPATH_RO", because of some bug
3219 const char *argv[] = { UPGRADE_SCRIPT_WITH_DBPATH_RO, pkgfilepath, NULL };
3220 ret = _ri_xsystem(argv);
3222 const char *argv[] = { UPGRADE_SCRIPT, pkgfilepath, NULL };
3223 ret = _ri_xsystem(argv);
3225 const char *argv[] = { UPGRADE_SCRIPT_WITH_DBPATH_RW, pkgfilepath, NULL };
3226 ret = _ri_xsystem(argv);
3229 _LOGE("@upgrade complete with error(%d)", ret);
3230 #ifdef APP2EXT_ENABLE
3231 if ((handle != NULL) && (handle->interface.post_upgrade != NULL)){
3232 __rpm_clear_dir_list(dir_list);
3233 handle->interface.post_upgrade(gpkgname, APP2EXT_STATUS_FAILED);
3234 app2ext_deinit(handle);
3239 _LOGD("#upgrade script success.");
3241 /*send event for install_percent*/
3242 _ri_broadcast_status_notification(pkgid, "rpm", "install_percent", "60");
3244 /*Parse the manifest to get install location and size. If fails, remove manifest info from DB.*/
3245 ret = pkgmgr_parser_parse_manifest_for_upgrade(manifest, NULL);
3247 _LOGE("@parsing manifest failed.");
3248 ret = RPM_INSTALLER_ERR_INTERNAL;
3251 _LOGD("#parsing manifest success.");
3253 /*unregister cert info*/
3254 _ri_unregister_cert(pkgid);
3256 /*register cert info*/
3257 _ri_register_cert(pkgid);
3259 #ifdef APP2EXT_ENABLE
3260 if ((handle != NULL) && (handle->interface.post_upgrade != NULL)){
3261 __rpm_clear_dir_list(dir_list);
3262 handle->interface.post_upgrade(gpkgname, APP2EXT_STATUS_SUCCESS);
3263 app2ext_deinit(handle);
3268 _coretpk_installer_search_ui_gadget(pkgid);
3270 /*apply smack to shared dir*/
3271 __rpm_apply_smack(pkgid, 1);
3273 /*apply smack by privilege*/
3274 ret = _ri_apply_privilege(pkgid, 0);
3276 _LOGE("@apply perm failed with err(%d)", ret);
3278 _LOGD("#apply perm success.");
3281 ret = _ri_smack_reload_all();
3283 _LOGD("_ri_smack_reload_all failed.");
3286 /*send event for install_percent*/
3287 _ri_broadcast_status_notification(pkgid, "rpm", "install_percent", "100");
3290 _rpm_delete_dir(TEMP_DIR);
3291 _rpm_delete_dir(TEMP_DBPATH);
3293 if (ret == RPM_INSTALLER_SUCCESS) {
3294 _LOGD("[#]end : _rpm_upgrade_pkg_with_dbpath");
3295 __ri_launch_consumer(pkgid);
3296 _ri_broadcast_status_notification(pkgid, "rpm", "end", "ok");
3297 _ri_stat_cb(pkgid, "end", "ok");
3299 _LOGE("[@]end : _rpm_upgrade_pkg_with_dbpath");
3300 char *errstr = NULL;
3301 _ri_error_no_to_string(ret, &errstr);
3302 _ri_broadcast_status_notification(pkgid, "rpm", "error", errstr);
3303 _ri_stat_cb(pkgid, "error", errstr);
3304 _ri_broadcast_status_notification(pkgid, "rpm", "end", "fail");
3306 _ri_stat_cb(pkgid, "end", "fail");
3307 _LOGE("install failed with err(%d) (%s)\n", ret, errstr);
3313 int _rpm_uninstall_pkg_with_dbpath(const char *pkgid, bool is_system)
3315 if (pkgid == NULL) {
3316 _LOGE("pkgid is NULL.");
3321 char buff[BUF_SIZE] = {'\0'};
3322 char tizen_manifest[BUF_SIZE] = {'\0'};
3323 pkgmgrinfo_pkginfo_h pkghandle = NULL;
3324 bool mother_package = false;
3325 bool coretpk = false;
3327 _LOGD("pkgid=[%s], is_system=[%d]", pkgid, is_system);
3329 snprintf(tizen_manifest, BUF_SIZE, "%s/%s/tizen-manifest.xml", OPT_USR_APPS, pkgid);
3330 if (access(tizen_manifest, R_OK) == 0) {
3332 _LOGD("[%s] is existed.", tizen_manifest);
3337 _ri_broadcast_status_notification(pkgid, coretpk ? "coretpk" : "rpm", "start", "update");
3339 _ri_broadcast_status_notification(pkgid, coretpk ? "coretpk" : "rpm", "start", "uninstall");
3341 // terminate running app
3342 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, getuid(), &pkghandle);
3344 _LOGE("pkgmgrinfo_pkginfo_get_pkginfo(%s) failed.", pkgid);
3345 ret = RPM_INSTALLER_ERR_PKG_NOT_FOUND;
3348 pkgmgrinfo_appinfo_get_usr_list(pkghandle, PM_UI_APP, __ri_check_running_app, NULL, getuid());
3350 // If package is mother package, then uninstall child package
3351 /* pkgmgrinfo_pkginfo_is_mother_package(pkghandle, &mother_package);
3352 if (mother_package == true) {
3353 _LOGD("[%s] is mother package", pkgid);
3354 __uninstall_child_package_by_mother_pkgid(pkgid);
3356 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
3358 snprintf(tizen_manifest, BUF_SIZE, "%s/%s/tizen-manifest.xml", OPT_USR_APPS, pkgid);
3359 if (access(tizen_manifest, R_OK) == 0) {
3361 _LOGD("[%s] is existing.", tizen_manifest);
3364 // del root path dir
3365 snprintf(buff, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgid);
3366 if (__is_dir(buff)) {
3367 _rpm_delete_dir(buff);
3370 // del root path dir for ext
3371 if (_coretpk_installer_get_configuration_value(INI_VALUE_MAKE_EXT_DIRECTORY)) {
3372 char extpath[BUF_SIZE] = {'\0'};
3373 snprintf(extpath, BUF_SIZE, "%s/%s", OPT_STORAGE_SDCARD_APP_ROOT, pkgid);
3374 if (__is_dir(extpath)) {
3375 _rpm_delete_dir(extpath);
3379 // check system pkg, if pkg is system pkg, need to update xml on USR_SHARE_PACKAGES
3381 memset(buff, '\0', BUF_SIZE);
3382 snprintf(buff, BUF_SIZE, "%s/%s.xml", USR_SHARE_PACKAGES, pkgid);
3383 _LOGE("manifest for upgrade, path=[%s]",buff);
3384 ret = pkgmgr_parser_parse_manifest_for_upgrade(buff, NULL);
3386 _LOGE("parsing manifest failed.");
3391 memset(buff, '\0', BUF_SIZE);
3392 snprintf(buff, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
3395 ret = pkgmgr_parser_parse_usr_manifest_for_uninstallation(buff, getuid(), NULL);
3397 _LOGE("pkgmgr_parser_parse_manifest_for_uninstallation() failed, pkgid=[%s]", pkgid);
3404 // execute privilege APIs
3405 // _ri_privilege_revoke_permissions(pkgid);
3406 // _ri_privilege_unregister_package(pkgid);
3408 // Unregister cert info
3409 _ri_unregister_cert(pkgid);
3412 // Restore the old smack file
3413 if (coretpk == false) {
3414 ret = __ri_copy_smack_rule_file(UNINSTALL_REQ,pkgid,is_system);
3415 if (ret != RPM_INSTALLER_SUCCESS){
3416 _LOGD("smack restore failed");
3419 ret = _ri_smack_reload_all();
3421 _LOGD("_ri_smack_reload_all failed.");
3427 _LOGE("failed, ret=[%d]", ret);
3428 char *errstr = NULL;
3429 _ri_error_no_to_string(ret, &errstr);
3430 _ri_broadcast_status_notification(pkgid, coretpk ? "coretpk" : "rpm", "error", errstr);
3431 _ri_stat_cb(pkgid, "error", errstr);
3433 _ri_broadcast_status_notification(pkgid, coretpk ? "coretpk" : "rpm", "end", "fail");
3434 _ri_stat_cb(pkgid, "end", "fail");
3435 _LOGE("remove failed with err(%d) (%s)\n", ret, errstr);
3438 _ri_broadcast_status_notification(pkgid, coretpk ? "coretpk" : "rpm", "end", "ok");
3439 _ri_stat_cb(pkgid, "end", "ok");
3445 int _rpm_uninstall_pkg(char *pkgid)
3451 bool is_removable = 0;
3452 char buff[BUF_SIZE] = {'\0'};
3453 pkgmgrinfo_install_location location = 1;
3454 #ifdef APP2EXT_ENABLE
3455 app2ext_handle *handle = NULL;
3457 char *manifest = NULL;
3458 pkgmgrinfo_pkginfo_h pkghandle;
3459 const char *argv[] = { UNINSTALL_SCRIPT, pkgid, NULL };
3461 _LOGD("start : _rpm_uninstall_pkg\n");
3463 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkghandle);
3465 _LOGE("Failed to get pkginfo handle\n");
3466 return RPM_INSTALLER_ERR_PKG_NOT_FOUND;
3468 /*terminate running app*/
3469 pkgmgrinfo_appinfo_get_list(pkghandle, PM_UI_APP, __ri_check_running_app, NULL);
3471 ret = pkgmgrinfo_pkginfo_is_system(pkghandle, &is_system);
3473 _LOGE("pkgmgrinfo_pkginfo_is_system failed.\n");
3474 return RPM_INSTALLER_ERR_INTERNAL;
3477 ret = pkgmgrinfo_pkginfo_is_update(pkghandle, &is_update);
3479 _LOGE("pkgmgrinfo_pkginfo_is_system failed.\n");
3480 return RPM_INSTALLER_ERR_INTERNAL;
3483 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
3484 /*updated and system pkg need to "remove-update"*/
3485 _LOGD("Remove Update[%s]",pkgid);
3486 ret = _rpm_uninstall_pkg_with_dbpath(pkgid, 1);
3488 _LOGE("uninstall_pkg_with_dbpath for system, is_update fail\n");
3493 pkgmgrinfo_pkginfo_is_removable(pkghandle, &is_removable);
3495 /*non-system and can be removable, it should be deleted*/
3496 _LOGD("Delete Package [%s]",pkgid);
3497 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
3498 ret = _rpm_uninstall_pkg_with_dbpath(pkgid, 0);
3500 _LOGE("uninstall_pkg_with_dbpath for non-system, is_remove fail\n");
3506 _ri_broadcast_status_notification(pkgid, "rpm", "start", "uninstall");
3508 #ifdef APP2EXT_ENABLE
3509 ret = pkgmgrinfo_pkginfo_get_install_location(pkghandle, &location);
3511 _LOGE("Failed to get install location\n");
3512 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
3513 return RPM_INSTALLER_ERR_INTERNAL;
3516 pkgmgrinfo_appinfo_get_list(pkghandle, PM_UI_APP, __ri_check_running_app, NULL);
3518 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
3519 if (location == PMINFO_INSTALL_LOCATION_PREFER_EXTERNAL) {
3520 handle = app2ext_init(APP2EXT_SD_CARD);
3521 if (handle == NULL) {
3522 _LOGE("app2ext init failed\n");
3523 return RPM_INSTALLER_ERR_INTERNAL;
3525 if ((&(handle->interface) != NULL) && (handle->interface.pre_uninstall != NULL) && (handle->interface.post_uninstall != NULL)){
3526 ret = app2ext_get_app_location(pkgid);
3527 if (ret == APP2EXT_INTERNAL_MEM){
3528 _LOGE("app2xt APP is not in MMC, go internal (%d)\n", ret);
3531 ret = handle->interface.pre_uninstall(pkgid);
3532 if (ret == APP2EXT_ERROR_MMC_STATUS || ret == APP2EXT_SUCCESS ) {
3533 _LOGE("app2xt MMC is not here, go internal (%d)\n", ret);
3536 _LOGE("app2xt pre uninstall API failed (%d)\n", ret);
3537 handle->interface.post_uninstall(pkgid);
3538 app2ext_deinit(handle);
3539 return RPM_INSTALLER_ERR_INTERNAL;
3546 #ifdef PRE_CHECK_FOR_MANIFEST
3547 /*Manifest info should be removed first because after installation manifest
3548 file is uninstalled. If uninstallation fails, we need to re-insert manifest info for consistency*/
3549 manifest = pkgmgr_parser_get_manifest_file(pkgid);
3550 if (manifest == NULL) {
3551 _LOGE("manifest name is NULL\n");
3552 app2ext_deinit(handle);
3553 return RPM_INSTALLER_ERR_NO_MANIFEST;
3555 _LOGD("manifest name is %s\n", manifest);
3556 ret = pkgmgr_parser_parse_manifest_for_uninstallation(manifest, NULL);
3558 _LOGE("pkgmgr_parser_parse_manifest_for_uninstallation failed.\n");
3563 ret = _rpm_xsystem(argv);
3565 _LOGE("uninstall failed with error(%d)\n", ret);
3566 #ifdef PRE_CHECK_FOR_MANIFEST
3567 err = pkgmgr_parser_parse_manifest_for_installation(manifest, NULL);
3569 _LOGE("Parsing Manifest Failed\n");
3576 #ifdef APP2EXT_ENABLE
3577 if ((handle != NULL) && (handle->interface.post_uninstall != NULL)){
3578 handle->interface.post_uninstall(pkgid);
3579 app2ext_deinit(handle);
3585 #ifdef APP2EXT_ENABLE
3586 if ((handle != NULL) && (handle->interface.post_uninstall != NULL)){
3587 handle->interface.post_uninstall(pkgid);
3588 app2ext_deinit(handle);
3592 #ifdef PRE_CHECK_FOR_MANIFEST
3598 /* Uninstallation Success. Remove the installation time key from vconf*/
3599 snprintf(buff, BUF_SIZE, "db/app-info/%s/installed-time", pkgid);
3600 err = vconf_unset(buff);
3602 _LOGE("unset installation time failed\n");
3604 /*execute privilege APIs*/
3605 _ri_privilege_revoke_permissions(pkgid);
3606 _ri_privilege_unregister_package(pkgid);
3607 /*Unregister cert info*/
3608 _ri_unregister_cert(gpkgname);
3610 _LOGD("end : _rpm_uninstall_pkg(%d)\n", ret);
3614 int _rpm_install_corexml(char *pkgfilepath, char *pkgid)
3617 /*validate signature and certifictae*/
3618 ret = _ri_verify_signatures(USR_APPS, pkgid);
3620 _LOGE("_ri_verify_signatures Failed : %s\n", pkgid);
3621 ret = RPM_INSTALLER_ERR_SIG_INVALID;
3625 /* check : given pkgid is deactivation*/
3626 ret = __ri_check_pkgid_for_deactivation(pkgid);
3628 _LOGE("pkgid[%s] for deactivation dont need to install.\n", pkgid);
3632 /* Parse and insert manifest in DB*/
3633 ret = pkgmgr_parser_parse_manifest_for_installation(pkgfilepath, NULL);
3635 _LOGD("Installing Manifest Failed : %s\n", pkgfilepath);
3636 ret = RPM_INSTALLER_ERR_PACKAGE_NOT_INSTALLED;
3640 // _ri_register_cert has __ri_free_cert_chain.
3641 _ri_register_cert(pkgid);
3644 _coretpk_installer_search_ui_gadget(pkgid);
3646 ret = RPM_INSTALLER_SUCCESS;
3650 __ri_free_cert_chain();
3657 int _rpm_move_pkg(char *pkgid, int move_type)
3660 app2ext_handle *hdl = NULL;
3663 GList *dir_list = NULL;
3664 pkgmgrinfo_pkginfo_h pkghandle = NULL;
3666 if (move_type == PM_MOVE_TO_INTERNAL)
3667 movetype = APP2EXT_MOVE_TO_PHONE;
3668 else if (move_type == PM_MOVE_TO_SDCARD)
3669 movetype = APP2EXT_MOVE_TO_EXT;
3671 return RPM_INSTALLER_ERR_WRONG_PARAM;
3673 ret = pkgmgrinfo_pkginfo_get_pkginfo(gpkgname,&pkghandle);
3675 _LOGE("@failed to get the pkginfo handle!!");
3676 ret = RPM_INSTALLER_ERR_PKG_NOT_FOUND;
3679 /* Terminate the running instance of app */
3680 pkgmgrinfo_appinfo_get_list(pkghandle,PM_UI_APP,__ri_check_running_app,NULL);
3681 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
3682 hdl = app2ext_init(APP2EXT_SD_CARD);
3683 if ((hdl != NULL) && (hdl->interface.move != NULL)){
3684 dir_list = __rpm_populate_dir_list();
3685 if (dir_list == NULL) {
3686 _LOGE("\nError in populating the directory list\n");
3687 return RPM_INSTALLER_ERR_RPM_SCRIPT_WRONG_ARGS;
3689 ret = hdl->interface.move(pkgid, dir_list, movetype);
3690 __rpm_clear_dir_list(dir_list);
3692 _LOGE("Failed to move app\n");
3693 return RPM_INSTALLER_ERR_INTERNAL;
3695 if(move_type == PM_MOVE_TO_INTERNAL){
3696 _LOGD("#updating the installed storage from external to internal");
3697 ret = pkgmgrinfo_pkginfo_set_installed_storage(pkgid, INSTALL_INTERNAL);
3700 _LOGD("#updating the installed storage from internal to external");
3701 ret = pkgmgrinfo_pkginfo_set_installed_storage(pkgid, INSTALL_EXTERNAL);
3704 if(ret != PMINFO_R_OK){
3705 _LOGE("@failed to udpate the installed storage");
3706 return RPM_INSTALLER_ERR_INTERNAL;
3709 app2ext_deinit(hdl);
3710 return RPM_INSTALLER_SUCCESS;
3712 _LOGE("Failed to get app2ext handle\n");
3713 return RPM_INSTALLER_ERR_INTERNAL;
3719 int _rpm_process_cscxml(char *csc_script)
3724 char *path_str = NULL;
3725 char *op_str = NULL;
3726 char *remove_str = NULL;
3727 char csc_str[BUF_SIZE] = {'\0'};
3728 snprintf(csc_str, BUF_SIZE - 1, "%s:", csc_script);
3730 /*get params from csc script*/
3731 path_str = __ri_get_str(csc_str, TOKEN_PATH_STR);
3732 op_str = __ri_get_str(csc_str, TOKEN_OPERATION_STR);
3733 remove_str = __ri_get_str(csc_str, TOKEN_REMOVE_STR);
3734 if((path_str == NULL) || (op_str == NULL) || (remove_str == NULL)){
3735 _LOGE("csc-info : input param is null[%s, %s, %s]\n", path_str, op_str, remove_str);
3738 _LOGD("csc-info : path=%s, op=%s, remove=%s\n", path_str, op_str, remove_str);
3740 /*get operation type*/
3741 op_type = __ri_get_op_type(op_str);
3743 _LOGE("csc-info : operation error[%s, %s]\n", path_str, op_str);
3749 ret = __ri_install_csc(path_str, remove_str);
3753 ret = __ri_install_csc(path_str, remove_str);
3757 ret = __ri_uninstall_csc(path_str);
3765 _LOGE("fota-info : Fota fail [pkgid=%s, operation=%d]\n",path_str, op_type);
3778 int _rpm_process_csc_coretpk(char *csc_script)
3783 char *path_str = NULL;
3784 char *op_str = NULL;
3785 char *remove_str = NULL;
3786 char csc_str[BUF_SIZE] = {'\0'};
3787 snprintf(csc_str, BUF_SIZE - 1, "%s:", csc_script);
3789 /*get params from csc script*/
3790 path_str = __ri_get_str(csc_str, TOKEN_PATH_STR);
3791 op_str = __ri_get_str(csc_str, TOKEN_OPERATION_STR);
3792 remove_str = __ri_get_str(csc_str, TOKEN_REMOVE_STR);
3793 if((path_str == NULL) || (op_str == NULL) || (remove_str == NULL)){
3794 _LOGE("csc-info : input param is null[%s, %s, %s]\n", path_str, op_str, remove_str);
3797 _LOGD("csc-info : path=%s, op=%s, remove=%s\n", path_str, op_str, remove_str);
3799 /*get operation type*/
3800 op_type = __ri_get_op_type(op_str);
3802 _LOGE("csc-info : operation error[%s, %s]\n", path_str, op_str);
3808 ret = _coretpk_installer_csc_install(path_str, remove_str);
3812 ret = _coretpk_installer_csc_install(path_str, remove_str);
3816 ret = __ri_uninstall_csc(path_str);
3824 _LOGE("csc-info : csc fail [pkgid=%s, operation=%d]\n",path_str, op_type);
3837 int _rpm_process_fota(char *fota_script)
3842 char *op_str = NULL;
3844 char csc_str[BUF_SIZE] = {'\0'};
3845 snprintf(csc_str, BUF_SIZE - 1, "%s:", fota_script);
3847 /*get params from fota script*/
3848 pkgid = __ri_get_str(csc_str, TOKEN_PATH_STR);
3849 op_str = __ri_get_str(csc_str, TOKEN_OPERATION_STR);
3850 if((pkgid == NULL) || (op_str == NULL)){
3851 _LOGE("fota-info : input param is null[%s, %s]\n", pkgid, op_str);
3854 _LOGD("fota-info : path=%s, op=%s\n", pkgid, op_str);
3856 /*get operation type*/
3857 op_type = __ri_get_op_type(op_str);
3859 _LOGE("fota-info : operation error[%s, %s]\n", pkgid, op_str);
3865 ret = __ri_install_fota(pkgid);
3869 ret = __ri_upgrade_fota(pkgid);
3873 ret = __ri_uninstall_fota(pkgid);
3881 _LOGE("fota-info : Fota fail [pkgid=%s, operation=%d]\n",pkgid, op_type);
3892 int _rpm_process_fota_for_rw(char *fota_script)
3897 char *op_str = NULL;
3899 char fota_str[BUF_SIZE] = {'\0'};
3900 snprintf(fota_str, BUF_SIZE - 1, "%s:", fota_script);
3902 /*get params from fota script*/
3903 pkgid = __ri_get_str(fota_str, TOKEN_PATH_STR);
3904 op_str = __ri_get_str(fota_str, TOKEN_OPERATION_STR);
3905 if((pkgid == NULL) || (op_str == NULL)){
3906 _LOGE("fota-info : input param is null[%s, %s]\n", pkgid, op_str);
3909 _LOGD("fota-info : path=%s, op=%s\n", pkgid, op_str);
3911 /*get operation type*/
3912 op_type = __ri_get_op_type(op_str);
3914 _LOGE("fota-info : operation error[%s, %s]\n", pkgid, op_str);
3920 ret = __ri_install_fota_for_rw(pkgid);
3924 ret = __ri_upgrade_fota_for_rw(pkgid);
3928 ret = __ri_uninstall_fota_for_rw(pkgid);
3936 _LOGE("fota-info : Fota fail [pkgid=%s, operation=%d]\n",pkgid, op_type);
3949 int _rpm_process_enable(char *pkgid)
3952 char *manifest = NULL;
3953 pkgmgrinfo_pkginfo_h handle;
3956 _LOGE("start :: pkgid[%s] enable process\n",pkgid);
3958 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
3959 if ((ret == 0) && (handle != NULL)) {
3960 _LOGE("pkg[%s] is already installed.", pkgid);
3961 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
3965 manifest = pkgmgr_parser_get_manifest_file(pkgid);
3966 if (manifest == NULL) {
3967 _LOGE("Failed to fetch package manifest file\n");
3971 _ri_broadcast_status_notification(pkgid, "rpm", PKGMGR_INSTALLER_START_KEY_STR, PKGMGR_INSTALLER_INSTALL_EVENT_STR);
3973 ret = pkgmgr_parser_parse_manifest_for_installation(manifest, NULL);
3976 _ri_broadcast_status_notification(pkgid, "rpm", PKGMGR_INSTALLER_END_KEY_STR, PKGMGR_INSTALLER_FAIL_EVENT_STR);
3977 _LOGE("insert in db failed\n");
3981 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
3983 _LOGE("insert in db failed\n");
3985 ret = pkgmgrinfo_pkginfo_is_system(handle, &is_system);
3987 pkgmgrinfo_appinfo_get_list(handle, PM_UI_APP, __ri_update_ail_info, NULL);
3989 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
3992 /*delete disabled pkg info from backup db table*/
3993 // pkgmgr_parser_delete_disabled_pkg(pkgid, NULL);
3995 _ri_broadcast_status_notification(pkgid, "rpm", PKGMGR_INSTALLER_END_KEY_STR, PKGMGR_INSTALLER_OK_EVENT_STR);
3997 _LOGE("end :: pkgid[%s] enable process\n",pkgid);
4002 int _rpm_process_disable(char *pkgid)
4005 pkgmgrinfo_pkginfo_h handle;
4007 _LOGE("start :: pkgid[%s] disable process\n",pkgid);
4009 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
4010 if ((ret < 0) || (handle == NULL)) {
4011 _LOGE("pkgid[%s] is already disabled\n", pkgid);
4015 _ri_broadcast_status_notification(pkgid, "rpm", PKGMGR_INSTALLER_START_KEY_STR, PKGMGR_INSTALLER_UNINSTALL_EVENT_STR);
4017 pkgmgrinfo_appinfo_get_list(handle, PM_UI_APP, __ri_check_running_app, NULL);
4018 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
4020 ret = pkgmgr_parser_parse_manifest_for_uninstallation(pkgid, NULL);
4022 _ri_broadcast_status_notification(pkgid, "rpm", PKGMGR_INSTALLER_END_KEY_STR, PKGMGR_INSTALLER_FAIL_EVENT_STR);
4023 _LOGE("pkgmgr_parser_parse_manifest_for_uninstallation failed.\n");
4027 /*save disabled pkg info to backup db table*/
4028 // pkgmgr_parser_insert_disabled_pkg(pkgid, NULL);
4030 _ri_broadcast_status_notification(pkgid, "rpm", PKGMGR_INSTALLER_END_KEY_STR, PKGMGR_INSTALLER_OK_EVENT_STR);
4032 _LOGE("end :: pkgid[%s] disable process\n",pkgid);
4036 int _rpm_process_enabled_list(const char *enabled_list)
4039 char delims[] = ":";
4040 char pkgid[MAX_BUF_SIZE] = {'\0'};
4041 char pkgid_list[MAX_BUF_SIZE] = {'\0'};
4043 if (enabled_list == NULL)
4046 snprintf(pkgid_list, MAX_BUF_SIZE, "%s", enabled_list);
4047 token = strtok(pkgid_list, delims);
4051 memset(pkgid, 0x00, sizeof(pkgid));
4052 strncat(pkgid, token, strlen(token));
4054 _rpm_process_enable(pkgid);
4056 token = strtok(NULL, delims);
4062 int _rpm_process_disabled_list(const char *disabled_list)
4065 char delims[] = ":";
4066 char pkgid[MAX_BUF_SIZE] = {'\0'};
4067 char pkgid_list[MAX_BUF_SIZE] = {'\0'};
4069 if (disabled_list == NULL)
4072 snprintf(pkgid_list, MAX_BUF_SIZE, "%s", disabled_list);
4073 token = strtok(pkgid_list, delims);
4077 memset(pkgid, 0x00, sizeof(pkgid));
4078 strncat(pkgid, token, strlen(token));
4080 _rpm_process_disable(pkgid);
4082 token = strtok(NULL, delims);
4088 int __ri_copy_smack_rule_file(int op, const char *pkgname, int is_system)
4091 mode_t mode = DIR_PERMS;
4092 int ret = RPM_INSTALLER_SUCCESS;
4093 char src[PATH_MAX]={0};
4094 char dest[PATH_MAX]={0};
4100 For downloadable native app, restore the smack file.
4101 Otherwise, remove the stored smack file.
4103 snprintf(dest,PATH_MAX-1,"%s%s.rule",SMACK_RULES_ALT_PATH,pkgname);
4104 snprintf(src,PATH_MAX-1,"%s%s.rule",DIR_RPM_WGT_SMACK_RULE_OPT,pkgname);
4105 _LOGD("#src:[%s] dest:[%s]",src,dest);
4108 if(!access(src,F_OK)){
4111 _LOGD("#File [%s] deleted.",src);
4113 _LOGE("@Unable to delete the file [%s], error:(%s)",src,strerror(errno));
4114 ret = RPM_INSTALLER_ERR_INTERNAL;
4118 if(!access(dest,F_OK)){
4121 _LOGD("#File [%s] deleted.",dest);
4123 _LOGE("@Unable to delete the file [%s], error:(%s)",dest,strerror(errno));
4124 ret = RPM_INSTALLER_ERR_INTERNAL;
4129 _LOGD("#Restore smack files for uninstallation [%s]",pkgname);
4130 if(!access(src,F_OK)){
4131 _LOGD("#Copying [%s] to [%s]",src,dest);
4132 ret = __copy_file(src,dest);
4136 _LOGD("#File [%s] deleted.",src);
4138 _LOGE("@Unable to delete the file [%s], error:(%s)",src,strerror(errno));
4139 ret = RPM_INSTALLER_ERR_INTERNAL;
4143 _LOGE("@Copy Failed!!");
4144 ret = RPM_INSTALLER_ERR_INTERNAL;
4148 _LOGE("@ %s.rule file is not preserved",pkgname);
4155 _LOGD("#Preserve the smack file for upgrade [%s]",pkgname);
4157 /* Apply the new smack file and preserve the old smack rule file if it is not preserved.*/
4158 snprintf(src,PATH_MAX-1,"%s%s.rule",SMACK_RULES_ALT_PATH,pkgname);
4159 snprintf(dest,PATH_MAX-1,"%s%s.rule",DIR_RPM_WGT_SMACK_RULE_OPT,pkgname);
4161 _LOGD("#src[%s] dest[%s]",src,dest);
4163 /* Create the directory if not exist to preserve the smack files */
4164 if(mkdir(DIR_RPM_WGT_SMACK_RULE_OPT,mode) == 0 || errno == EEXIST){
4165 if((access(src,F_OK) == 0) && (access(dest,F_OK) != 0)){
4166 ret = __copy_file(src,dest);
4168 _LOGD("#Smack file is already preserved");
4171 _LOGE("@Temporary folder creation failed");
4172 ret = RPM_INSTALLER_ERR_INTERNAL;
4176 _LOGE("@Unsupported Operation\n");
4177 ret = RPM_INSTALLER_ERR_INTERNAL;