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>
64 #include <app_manager_extension.h>
67 #define APP2EXT_ENABLE
69 #include <app2ext_interface.h>
72 #include "rpm-installer-util.h"
73 #include "rpm-installer-signature.h"
74 #include "rpm-installer.h"
75 #include "rpm-frontend.h"
76 #include "rpm-installer-type.h"
77 #include "coretpk-installer-internal.h"
86 enum rpm_sig_sub_type {
92 typedef struct cert_chain_t {
97 cert_chain list[MAX_CERT_NUM];
98 GList *privilege_list;
100 #define PKGMGR_DB "/opt/dbspace/.pkgmgr_parser.db"
101 #define EXT_APPDATA_PRIVILEGE_NAME "http://tizen.org/privilege/externalstorage.appdata"
103 extern char *gpkgname;
104 extern int sig_enable;
106 static void __rpm_process_line(char *line);
107 static void __rpm_perform_read(int fd);
108 static int __ri_xmlsec_verify_signature(const char *sigxmlfile, char *rootca);
109 static xmlSecKeysMngrPtr __ri_load_trusted_certs(char *files, int files_size);
110 static int __ri_verify_file(xmlSecKeysMngrPtr mngr, const char *sigxmlfile);
111 static int __ri_create_cert_chain(int sigtype, int sigsubtype, char *value);
112 static void __ri_free_cert_chain(void);
113 static char *__ri_get_cert_from_file(const char *file);
114 static int __privilege_func(const char *name, void *user_data);
115 static char *__ri_get_str(const char* str, const char* pKey);
116 static void __ri_xmlsec_debug_print(const char* file, int line, const char* func,
117 const char* errorObject, const char* errorSubject, int reason, const char* msg);
119 int _ri_set_group_id(const char *pkgid, const char *groupid);
121 static void __str_trim(char *input)
123 char *trim_str = input;
128 while (*input != 0) {
129 if (!isspace(*input)) {
140 static int __ri_check_pkgid_for_deactivation(const char *pkgid)
143 char deactivation_str[FILENAME_MAX] = { 0 };
144 char *deactivation_pkgid = NULL;
145 char *deactivation_state = NULL;
147 fp = fopen(DEACTIVATION_PKGID_LIST, "r");
149 _LOGE("fopen fail\n");
153 while (fgets(deactivation_str, sizeof(deactivation_str), fp) != NULL) {
154 __str_trim(deactivation_str);
156 deactivation_pkgid = __ri_get_str(deactivation_str, TOKEN_PKGID_STR);
157 if(deactivation_pkgid == NULL)
160 deactivation_state = __ri_get_str(deactivation_str, TOKEN_STATE_STR);
161 if(deactivation_state == NULL) {
162 free(deactivation_pkgid);
166 if ((strcmp(deactivation_pkgid, pkgid) == 0) && (strcmp(deactivation_state, "off") == 0)) {
168 free(deactivation_pkgid);
169 free(deactivation_state);
170 _LOGE("Find pkgid[%s] form deactivation list.\n", pkgid);
174 free(deactivation_pkgid);
175 free(deactivation_state);
176 memset(deactivation_str, 0x00, sizeof(deactivation_str));
186 static int __ri_get_op_type(char *op_str)
188 if (strcmp(op_str,"install")==0)
190 else if (strcmp(op_str,"update")==0)
192 else if (strcmp(op_str,"uninstall")==0)
193 return UNINSTALL_REQ;
198 static char *__ri_get_str(const char* str, const char* pKey)
200 const char* p = NULL;
201 const char* pStart = NULL;
202 const char* pEnd = NULL;
207 char *pBuf = strdup(str);
209 _LOGE("Malloc failed !");
213 p = strstr(pBuf, pKey);
222 pStart = p + strlen(pKey);
223 pEnd = strchr(pStart, SEPERATOR_END);
231 size_t len = pEnd - pStart;
239 char *pRes = (char*)malloc(len + 1);
245 _LOGE("Malloc failed!");
248 strncpy(pRes, pStart, len);
259 static int __ri_init_csc_xml(char *xml_path, char *removable)
262 char* csc_tags[3] = {NULL, };
264 if (strcmp(removable,"true")==0)
265 csc_tags[0] = "removable=true";
267 csc_tags[0] = "removable=false";
269 csc_tags[1] = "preload=true";
272 ret = pkgmgr_parser_parse_manifest_for_installation(xml_path, csc_tags);
277 static int __ri_create_cert_chain(int sigtype, int sigsubtype, char *value)
281 // _LOGD("Push in list [%d] [%d] [%s]", sigtype, sigsubtype, value);
284 switch (sigsubtype) {
286 list[PMINFO_SET_AUTHOR_SIGNER_CERT].cert_type = PMINFO_SET_AUTHOR_SIGNER_CERT;
287 list[PMINFO_SET_AUTHOR_SIGNER_CERT].cert_value = strdup(value);
289 case SIG_INTERMEDIATE:
290 list[PMINFO_SET_AUTHOR_INTERMEDIATE_CERT].cert_type = PMINFO_SET_AUTHOR_INTERMEDIATE_CERT;
291 list[PMINFO_SET_AUTHOR_INTERMEDIATE_CERT].cert_value = strdup(value);
294 /*value is already a mallocd pointer*/
295 list[PMINFO_SET_AUTHOR_ROOT_CERT].cert_type = PMINFO_SET_AUTHOR_ROOT_CERT;
296 list[PMINFO_SET_AUTHOR_ROOT_CERT].cert_value = value;
303 switch (sigsubtype) {
305 list[PMINFO_SET_DISTRIBUTOR_SIGNER_CERT].cert_type = PMINFO_SET_DISTRIBUTOR_SIGNER_CERT;
306 list[PMINFO_SET_DISTRIBUTOR_SIGNER_CERT].cert_value = strdup(value);
308 case SIG_INTERMEDIATE:
309 list[PMINFO_SET_DISTRIBUTOR_INTERMEDIATE_CERT].cert_type = PMINFO_SET_DISTRIBUTOR_INTERMEDIATE_CERT;
310 list[PMINFO_SET_DISTRIBUTOR_INTERMEDIATE_CERT].cert_value = strdup(value);
313 /*value is already a mallocd pointer*/
314 list[PMINFO_SET_DISTRIBUTOR_ROOT_CERT].cert_type = PMINFO_SET_DISTRIBUTOR_ROOT_CERT;
315 list[PMINFO_SET_DISTRIBUTOR_ROOT_CERT].cert_value = value;
322 switch (sigsubtype) {
324 list[PMINFO_SET_DISTRIBUTOR2_SIGNER_CERT].cert_type = PMINFO_SET_DISTRIBUTOR2_SIGNER_CERT;
325 list[PMINFO_SET_DISTRIBUTOR2_SIGNER_CERT].cert_value = strdup(value);
327 case SIG_INTERMEDIATE:
328 list[PMINFO_SET_DISTRIBUTOR2_INTERMEDIATE_CERT].cert_type = PMINFO_SET_DISTRIBUTOR2_INTERMEDIATE_CERT;
329 list[PMINFO_SET_DISTRIBUTOR2_INTERMEDIATE_CERT].cert_value = strdup(value);
332 /*value is already a mallocd pointer*/
333 list[PMINFO_SET_DISTRIBUTOR2_ROOT_CERT].cert_type = PMINFO_SET_DISTRIBUTOR2_ROOT_CERT;
334 list[PMINFO_SET_DISTRIBUTOR2_ROOT_CERT].cert_value = value;
346 static void __ri_free_cert_chain()
349 for (i = 0; i < MAX_CERT_NUM; i++) {
350 if (list[i].cert_value)
351 free(list[i].cert_value);
355 static void __ri_xmlsec_debug_print(const char* file, int line, const char* func,
356 const char* errorObject, const char* errorSubject, int reason, const char* msg)
358 char total[BUF_SIZE];
359 snprintf(total, sizeof(total), "[%s(%d)] : [%s] : [%s] : [%s]", func, line, errorObject, errorSubject, msg);
361 fprintf(stderr, "## [validate error]: %s\n", total);
368 static int __ri_verify_file(xmlSecKeysMngrPtr sec_key_mngr, const char *sigxmlfile)
370 xmlDocPtr doc = NULL;
371 xmlNodePtr node = NULL;
372 xmlSecDSigCtxPtr dsigCtx = NULL;
374 if (sigxmlfile == NULL)
376 if (sec_key_mngr == NULL)
379 /* set error callback to xmlsec1 */
380 xmlSecErrorsSetCallback(__ri_xmlsec_debug_print);
383 doc = xmlParseFile(sigxmlfile);
384 if ((doc == NULL) || (xmlDocGetRootElement(doc) == NULL)) {
385 _LOGE("unable to parse file \"%s\"\n", sigxmlfile);
388 /* find start node */
389 node = xmlSecFindNode(xmlDocGetRootElement(doc), xmlSecNodeSignature, xmlSecDSigNs);
391 _LOGE("start node not found in \"%s\"\n", sigxmlfile);
394 /* create signature context */
395 dsigCtx = xmlSecDSigCtxCreate(sec_key_mngr);
396 if (dsigCtx == NULL) {
397 _LOGE("failed to create signature context\n");
400 /* Verify signature */
401 if (xmlSecDSigCtxVerify(dsigCtx, node) < 0) {
402 _LOGE("failed to verify signature\n");
405 /* print verification result to stdout */
406 if (dsigCtx->status == xmlSecDSigStatusSucceeded) {
408 _LOGD("valid signature");
411 _LOGD("invalid signature");
416 if(dsigCtx != NULL) {
417 xmlSecDSigCtxDestroy(dsigCtx);
425 static xmlSecKeysMngrPtr __ri_load_trusted_certs(char *files, int files_size)
427 xmlSecKeysMngrPtr sec_key_mngr;
432 sec_key_mngr = xmlSecKeysMngrCreate();
433 if (sec_key_mngr == NULL) {
434 _LOGE("failed to create keys manager.\n");
437 if (xmlSecCryptoAppDefaultKeysMngrInit(sec_key_mngr) < 0) {
438 _LOGE("failed to initialize keys manager.\n");
439 xmlSecKeysMngrDestroy(sec_key_mngr);
442 /* load trusted cert */
443 if (xmlSecCryptoAppKeysMngrCertLoad(sec_key_mngr, files, xmlSecKeyDataFormatPem, xmlSecKeyDataTypeTrusted) < 0) {
444 _LOGE("failed to load pem certificate from \"%s\"\n", files);
445 xmlSecKeysMngrDestroy(sec_key_mngr);
451 static int __ri_xmlsec_verify_signature(const char *sigxmlfile, char *rootca)
454 xmlSecKeysMngrPtr sec_key_mngr = NULL;
455 xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
456 xmlSubstituteEntitiesDefault(1);
458 #ifndef XMLSEC_NO_XSLT
459 xmlIndentTreeOutput = 1;
460 xsltSecurityPrefsPtr sec_prefs = xsltNewSecurityPrefs();
461 xsltSetSecurityPrefs(sec_prefs, XSLT_SECPREF_WRITE_FILE, xsltSecurityForbid);
462 xsltSetSecurityPrefs(sec_prefs, XSLT_SECPREF_READ_FILE, xsltSecurityForbid);
463 xsltSetSecurityPrefs(sec_prefs, XSLT_SECPREF_CREATE_DIRECTORY, xsltSecurityForbid);
464 xsltSetSecurityPrefs(sec_prefs, XSLT_SECPREF_WRITE_NETWORK, xsltSecurityForbid);
465 xsltSetSecurityPrefs(sec_prefs, XSLT_SECPREF_READ_NETWORK, xsltSecurityForbid);
466 xsltSetDefaultSecurityPrefs(sec_prefs);
471 _LOGE("xmlsec initialization failed [%d]\n", ret);
474 ret = xmlSecCheckVersion();
476 _LOGE("Incompatible version of loaded xmlsec library [%d]\n", ret);
479 #ifdef XMLSEC_CRYPTO_DYNAMIC_LOADING
480 ret = xmlSecCryptoDLLoadLibrary(BAD_CAST "openssl");
482 _LOGE("unable to load openssl library [%d]\n", ret);
487 ret = xmlSecCryptoAppInit(NULL);
489 _LOGE("crypto initialization failed [%d]\n", ret);
492 ret = xmlSecCryptoInit();
494 _LOGE("xmlsec-crypto initialization failed [%d]\n", ret);
498 sec_key_mngr = __ri_load_trusted_certs(rootca, 1);
499 if (sec_key_mngr == NULL) {
500 _LOGE("loading of trusted certs failed\n");
505 if (__ri_verify_file(sec_key_mngr, sigxmlfile) < 0) {
511 xmlSecKeysMngrDestroy(sec_key_mngr);
512 xmlSecCryptoShutdown();
513 xmlSecCryptoAppShutdown();
515 #ifndef XMLSEC_NO_XSLT
516 xsltFreeSecurityPrefs(sec_prefs);
517 xsltCleanupGlobals();
522 int _rpm_installer_get_group_id(char *pkgid, char **result)
525 const char *value = NULL;
526 char author_signature[BUF_SIZE] = {'\0'};
527 char *e_rootcert = NULL;
528 char *d_rootcert = NULL;
530 unsigned char hashout[BUF_SIZE] = {'\0'};
531 unsigned int h_size = 0;
534 pkgmgrinfo_certinfo_h handle = NULL;
536 snprintf(author_signature, BUF_SIZE, "%s/%s/%s", USR_APPS, pkgid, AUTHOR_SIGNATURE_XML);
537 if (access(author_signature, F_OK) != 0) {
538 _LOGE("[%s] isn't existed.", author_signature);
542 ret = pkgmgrinfo_pkginfo_create_certinfo(&handle);
544 _LOGE("pkgmgrinfo_pkginfo_create_certinfo(%s) failed.", pkgid);
548 ret = pkgmgrinfo_pkginfo_load_certinfo(pkgid, handle, getuid());
550 _LOGE("pkgmgrinfo_pkginfo_load_certinfo(%s) failed.", pkgid);
554 // get root certificate
555 ret = pkgmgrinfo_pkginfo_get_cert_value(handle, PMINFO_AUTHOR_SIGNER_CERT, &value);
556 if (ret < 0 || value == NULL) {
557 _LOGE("pkgmgrinfo_pkginfo_get_cert_value(%s) failed. [%d]", pkgid, ret);
562 d_rootcert = (char *)g_base64_decode(value, &d_size);
563 if (d_rootcert == NULL) {
564 _LOGE("g_base64_decode() failed.");
567 _LOGD("g_base64_decode() succeed, d_size=[%d]", d_size);
570 EVP_Digest(d_rootcert, d_size, hashout, &h_size, EVP_sha1(), NULL);
572 _LOGE("EVP_Digest(hash) failed.");
575 _LOGD("EVP_Digest() succeed, h_size=[%d]", h_size);
578 e_rootcert = g_base64_encode((const guchar *)hashout, h_size);
579 if (e_rootcert == NULL) {
580 _LOGE("g_base64_encode() failed.");
583 e_size = strlen(e_rootcert);
584 _LOGD("g_base64_encode() succeed, e_size=[%d]", e_size);
587 for (length = e_size; length >= 0; --length) {
588 if (e_rootcert[length] == '/') {
589 e_rootcert[length] = '#';
593 *result = e_rootcert;
602 pkgmgrinfo_pkginfo_destroy_certinfo(handle);
608 void __rpm_apply_smack(char *pkgname, int flag)
612 char dirpath[BUF_SIZE] = {'\0'};
613 char* groupid = NULL;
614 char *shared_data_label = NULL;
616 /*execute privilege APIs. The APIs should not fail*/
617 _ri_privilege_register_package(pkgname);
619 /*home dir. Dont setup path but change smack access to "_" */
620 snprintf(dirpath, BUF_SIZE, "%s/%s", USR_APPS, pkgname);
621 if (__is_dir(dirpath))
622 _ri_privilege_change_smack_label(dirpath, "_", 0);/*0 is SMACK_LABEL_ACCESS*/
623 memset(dirpath, '\0', BUF_SIZE);
624 snprintf(dirpath, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgname);
625 if (__is_dir(dirpath))
626 _ri_privilege_change_smack_label(dirpath, "_", 0);/*0 is SMACK_LABEL_ACCESS*/
629 memset(dirpath, '\0', BUF_SIZE);
630 snprintf(dirpath, BUF_SIZE, "%s/%s/data", OPT_USR_APPS, pkgname);
631 if (__is_dir(dirpath))
632 _ri_privilege_setup_path(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
635 memset(dirpath, '\0', BUF_SIZE);
636 snprintf(dirpath, BUF_SIZE, "%s/%s/cache", OPT_USR_APPS, pkgname);
637 if (!__is_dir(dirpath)) {
638 ret = mkdir(dirpath, DIRECTORY_PERMISSION_755);
640 _LOGE("directory making is failed.\n");
643 ret = chown(dirpath, APP_OWNER_ID, APP_GROUP_ID);
645 _LOGE("chown failed!! [%s]",strerror(errno));
647 _ri_privilege_setup_path(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
650 memset(dirpath, '\0', BUF_SIZE);
651 snprintf(dirpath, BUF_SIZE, "%s/%s/shared", USR_APPS, pkgname);
652 if (__is_dir(dirpath))
653 _ri_privilege_change_smack_label(dirpath, "_", 0);/*0 is SMACK_LABEL_ACCESS*/
654 memset(dirpath, '\0', BUF_SIZE);
655 snprintf(dirpath, BUF_SIZE, "%s/%s/shared", OPT_USR_APPS, pkgname);
656 if (!__is_dir(dirpath))
657 ret = mkdir(dirpath, DIRECTORY_PERMISSION_755);
659 _LOGE("directory making is failed.\n");
661 _ri_privilege_change_smack_label(dirpath, "_", 0);/*0 is SMACK_LABEL_ACCESS*/
662 memset(dirpath, '\0', BUF_SIZE);
665 snprintf(dirpath, BUF_SIZE, "%s/%s/shared/res", USR_APPS, pkgname);
666 if (__is_dir(dirpath))
667 _ri_privilege_setup_path(pkgname, dirpath, PERM_APP_PATH_ANY_LABEL, "_");
668 memset(dirpath, '\0', BUF_SIZE);
670 snprintf(dirpath, BUF_SIZE, "%s/%s/shared/res", OPT_USR_APPS, pkgname);
671 if (__is_dir(dirpath))
672 _ri_privilege_setup_path(pkgname, dirpath, PERM_APP_PATH_ANY_LABEL, "_");
673 memset(dirpath, '\0', BUF_SIZE);
676 snprintf(dirpath, BUF_SIZE, "%s/%s/shared/data", USR_APPS, pkgname);
677 if (__is_dir(dirpath)) {
678 ret = chown(dirpath, APP_OWNER_ID, APP_GROUP_ID);
680 _LOGE("chown failed!! [%s]",strerror(errno));
682 _ri_privilege_setup_path(pkgname, dirpath, PERM_APP_PATH_PUBLIC, NULL);
684 memset(dirpath, '\0', BUF_SIZE);
686 snprintf(dirpath, BUF_SIZE, "%s/%s/shared/data", OPT_USR_APPS, pkgname);
687 if (__is_dir(dirpath)) {
688 ret = chown(dirpath, APP_OWNER_ID, APP_GROUP_ID);
690 _LOGE("chown failed!! [%s]",strerror(errno));
692 _ri_privilege_setup_path(pkgname, dirpath, PERM_APP_PATH_PUBLIC, NULL);
696 ret = _coretpk_installer_get_smack_label_access(dirpath, &shared_data_label);
698 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", dirpath, ret);
700 memset(dirpath, '\0', BUF_SIZE);
701 snprintf(dirpath, BUF_SIZE, "%s/%s/shared/cache", OPT_USR_APPS, pkgname);
702 if (!__is_dir(dirpath)) {
703 ret = mkdir(dirpath, DIRECTORY_PERMISSION_755);
705 _LOGE("directory making is failed.\n");
708 ret = chown(dirpath, APP_OWNER_ID, APP_GROUP_ID);
710 _LOGE("chown failed!! [%s]",strerror(errno));
712 ret = _coretpk_installer_set_smack_label_access(dirpath, shared_data_label);
714 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", dirpath, ret);
716 ret = _coretpk_installer_set_smack_label_transmute(dirpath, "1");
718 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", dirpath, ret);
722 memset(dirpath, '\0', BUF_SIZE);
723 snprintf(dirpath, BUF_SIZE, "%s/%s/shared/trusted", USR_APPS, pkgname);
724 if (__is_dir(dirpath)) {
725 ret = chown(dirpath, APP_OWNER_ID, APP_GROUP_ID);
727 _LOGE("chown failed!! [%s]",strerror(errno));
730 ret = _rpm_installer_get_group_id(pkgname, &groupid);
732 _LOGD("group id for trusted directory is [%s]", groupid);
733 _ri_privilege_setup_path(pkgname, dirpath, APP_PATH_GROUP_RW, groupid);
734 FREE_AND_NULL(groupid);
737 memset(dirpath, '\0', BUF_SIZE);
739 snprintf(dirpath, BUF_SIZE, "%s/%s/shared/trusted", OPT_USR_APPS, pkgname);
741 _LOGD("dirpath [%s]", dirpath);
743 ret = _rpm_installer_get_group_id(pkgname, &groupid);
745 if (__is_dir(dirpath) != 1) {
746 _LOGE("dont have [%s]", dirpath);
748 ret = mkdir(dirpath, DIRECTORY_PERMISSION_755);
749 if (ret == -1 && errno != EEXIST) {
750 _LOGE("mkdir failed!! [%s]",strerror(errno));
754 ret = chown(dirpath, APP_OWNER_ID, APP_GROUP_ID);
756 _LOGE("chown failed!! [%s]",strerror(errno));
759 _LOGD("group id for trusted directory is [%s]", groupid);
760 _ri_privilege_setup_path(pkgname, dirpath, APP_PATH_GROUP_RW, groupid);
761 _ri_set_group_id(pkgname, groupid);
763 FREE_AND_NULL(groupid);
768 int __is_dir(char *dirname)
770 struct stat stFileInfo;
771 if(dirname == NULL) {
772 _LOGE("dirname is null\n");
776 (void)stat(dirname, &stFileInfo);
778 if (S_ISDIR(stFileInfo.st_mode)) {
784 static void __rpm_process_line(char *line)
787 tok = strtok(line, " ");
789 if (!strncmp(tok, "%%", 2)) {
790 tok = strtok(NULL, " ");
792 _LOGD("Install percentage is %s\n",
794 _ri_broadcast_status_notification(gpkgname,
798 _ri_stat_cb(gpkgname, "install_percent", tok);
806 static void __rpm_perform_read(int fd)
808 char *buf_ptr = NULL;
809 char *tmp_ptr = NULL;
811 static char buffer[BUF_SIZE] = { 0, };
812 static int buffer_position;
814 size = read(fd, &buffer[buffer_position],
815 sizeof(buffer) - buffer_position);
816 buffer_position += size;
820 /* Process each line of the recieved buffer */
821 buf_ptr = tmp_ptr = buffer;
822 while ((tmp_ptr = (char *)memchr(buf_ptr, '\n',
823 buffer + buffer_position - buf_ptr)) !=
826 __rpm_process_line(buf_ptr);
827 /* move to next line and continue */
828 buf_ptr = tmp_ptr + 1;
831 /*move the remaining bits at the start of the buffer
832 and update the buffer position */
833 buf_ptr = (char *)memrchr(buffer, 0, buffer_position);
837 /* we have processed till the last \n which has now become
838 0x0. So we increase the pointer to next position */
841 memmove(buffer, buf_ptr, buf_ptr - buffer);
842 buffer_position = buffer + buffer_position - buf_ptr;
846 int _rpm_xsystem(const char *argv[])
855 if (pipe(pipefd) == -1) {
856 _LOGE("pipe creation failed\n");
859 /*Read progress info via pipe */
864 _LOGE("fork failed\n");
874 _LOGE("dup failed\n");
878 result = dup(pipefd[1]);
880 _LOGE("dup failed\n");
884 if (execvp(argv[0], (char *const *)argv) == -1) {
885 _LOGE("execvp failed\n");
896 while ((err = waitpid(pid, &status, WNOHANG)) != pid) {
900 _LOGE("waitpid failed\n");
909 FD_SET(pipefd[0], &rfds);
913 pselect(pipefd[0] + 1, &rfds, NULL, NULL, &tv, NULL);
917 else if (select_ret < 0 && errno == EINTR)
919 else if (select_ret < 0) {
920 _LOGE("select() returned error\n");
923 if (FD_ISSET(pipefd[0], &rfds))
924 __rpm_perform_read(pipefd[0]);
928 /* Check for an error code. */
929 if (WIFEXITED(status) == 0 || WEXITSTATUS(status) != 0) {
931 if (WIFSIGNALED(status) != 0 && WTERMSIG(status) == SIGSEGV) {
933 ("Sub-process %s received a segmentation fault. \n",
935 } else if (WIFEXITED(status) != 0) {
936 printf("Sub-process %s returned an error code (%u)\n",
937 argv[0], WEXITSTATUS(status));
939 printf("Sub-process %s exited unexpectedly\n", argv[0]);
942 return WEXITSTATUS(status);
945 void __rpm_clear_dir_list(GList* dir_list)
948 app2ext_dir_details* dir_detail = NULL;
950 list = g_list_first(dir_list);
952 dir_detail = (app2ext_dir_details *)list->data;
953 if (dir_detail && dir_detail->name) {
954 free(dir_detail->name);
957 list = g_list_next(list);
959 g_list_free(dir_list);
963 GList * __rpm_populate_dir_list()
965 GList *dir_list = NULL;
967 app2ext_dir_details* dir_detail = NULL;
969 char pkg_ro_content_rpm[3][5] = { "bin", "res", "lib" };
972 for (i=0; i<3; i++) {
973 dir_detail = (app2ext_dir_details*) calloc(1, sizeof(app2ext_dir_details));
974 if (dir_detail == NULL) {
975 printf("\nMemory allocation failed\n");
978 dir_detail->name = (char*) calloc(1, sizeof(char)*(strlen(pkg_ro_content_rpm[i])+2));
979 if (dir_detail->name == NULL) {
980 printf("\nMemory allocation failed\n");
984 snprintf(dir_detail->name, (strlen(pkg_ro_content_rpm[i])+1), "%s", pkg_ro_content_rpm[i]);
985 dir_detail->type = APP2EXT_DIR_RO;
986 dir_list = g_list_append(dir_list, dir_detail);
989 list = g_list_first(dir_list);
991 dir_detail = (app2ext_dir_details *)list->data;
992 list = g_list_next(list);
998 list = g_list_first(dir_list);
1000 dir_detail = (app2ext_dir_details *)list->data;
1001 if (dir_detail && dir_detail->name) {
1002 free(dir_detail->name);
1005 list = g_list_next(list);
1007 g_list_free(dir_list);
1012 static char *__ri_get_cert_from_file(const char *file)
1014 FILE *fp_cert = NULL;
1016 char *certbuf = NULL;
1017 char *startcert = NULL;
1018 char *endcert = NULL;
1025 if(!(fp_cert = fopen(file, "r"))) {
1026 _LOGE("[ERR][%s] Fail to open file, [%s]\n", __func__, file);
1030 fseek(fp_cert, 0L, SEEK_END);
1032 if(ftell(fp_cert) < 0) {
1033 _LOGE("[ERR][%s] Fail to find EOF\n", __func__);
1038 certlen = ftell(fp_cert);
1039 fseek(fp_cert, 0L, SEEK_SET);
1041 if(!(certbuf = (char*)malloc(sizeof(char) * (int)certlen))) {
1042 _LOGE("[ERR][%s] Fail to allocate memory\n", __func__);
1046 memset(certbuf, 0x00, (int)certlen);
1049 while((ch = fgetc(fp_cert)) != EOF) {
1057 startcert = strstr(certbuf, "-----BEGIN CERTIFICATE-----") + strlen("-----BEGIN CERTIFICATE-----");
1058 endcert = strstr(certbuf, "-----END CERTIFICATE-----");
1059 certwrite = (int)endcert - (int)startcert;
1061 cert = (char*)malloc(sizeof(char) * (certwrite+2));
1063 _LOGE("[ERR][%s] Fail to allocate memory\n", __func__);
1067 memset(cert, 0x00, certwrite+2);
1068 snprintf(cert, certwrite+1, "%s", startcert);
1069 _LOGD("Root CA, len=[%d]\n%s", certwrite, cert);
1081 static int __privilege_func(const char *name, void *user_data)
1085 const char *perm[] = {NULL, NULL};
1087 int apptype = PERM_APP_TYPE_EFL;
1088 privilegeinfo *info = (privilegeinfo*)user_data;
1090 _LOGD("package_id = [%s], privilege = [%s]", info->package_id, name);
1091 privilege_list = g_list_append(privilege_list, strdup((char*)name));
1093 if (info->visibility & CERT_SVC_VISIBILITY_PLATFORM) {
1094 _LOGD("VISIBILITY_PLATFORM!");
1095 apptype = PERM_APP_TYPE_EFL_PLATFORM;
1096 } else if ((info->visibility & CERT_SVC_VISIBILITY_PARTNER) ||
1097 (info->visibility & CERT_SVC_VISIBILITY_PARTNER_OPERATOR) ||
1098 (info->visibility & CERT_SVC_VISIBILITY_PARTNER_MANUFACTURER)) {
1099 _LOGD("VISIBILITY_PARTNER!");
1100 apptype = PERM_APP_TYPE_EFL_PARTNER;
1103 ret = _ri_privilege_enable_permissions((char *)info->package_id, apptype, perm, 1);
1105 _LOGE("_ri_privilege_enable_permissions(%s, %d) failed.", (char *)info->package_id, apptype);
1109 if (strcmp(name, EXT_APPDATA_PRIVILEGE_NAME) == 0) {
1110 _LOGD("it is EXT_APPDATA_PRIVILEGE_NAME");
1111 if(_coretpk_installer_make_directory_for_ext((char *)info->package_id) < 0) {
1112 _LOGE("make_directory_for_ext failed.");
1116 _LOGD("_ri_privilege_enable_permissions(%s, %d) succeed.", (char *)info->package_id, apptype);
1122 char *__strlwr(char *str)
1126 while(*(str+i) != '\0'){
1127 if(*(str+i) >= 65 || *(str+i)<= 90) {
1128 *(str+i) = towlower(*(str+i));
1135 static int __ri_find_xml(char *pkgid, char **rpm_xml)
1138 struct dirent entry, *result;
1142 dir = opendir(USR_SHARE_PACKAGES);
1144 if (strerror_r(errno, buf, sizeof(buf)) == 0)
1145 _LOGE("fota-info : Failed to access the [%s] because %s", USR_SHARE_PACKAGES, buf);
1149 for (ret = readdir_r(dir, &entry, &result);
1150 ret == 0 && result != NULL;
1151 ret = readdir_r(dir, &entry, &result)) {
1152 char *manifest = NULL;
1154 if (entry.d_name[0] == '.') continue;
1156 manifest = _manifest_to_package(entry.d_name);
1158 _LOGE("fota-info : Failed to convert file to xml[%s]", entry.d_name);
1162 if (strstr(manifest, __strlwr(pkgid))) {
1163 snprintf(buf, sizeof(buf), "%s/%s", USR_SHARE_PACKAGES, manifest);
1164 _LOGD("fota-info : pkgid[%s] find xml[%s]\n", pkgid, buf);
1165 *rpm_xml = strdup(buf);
1174 _LOGE("fota-info : Failed to find xml for pkgid[%s]", pkgid);
1179 static int __ri_install_fota(char *pkgid)
1182 char *manifest = NULL;
1183 char *temp[] = {"fota=true", NULL};
1185 _LOGD("fota-info : pkgid[%s] start installation\n", pkgid);
1187 /*if pkgid is one of deactivation, it does not need to install*/
1188 ret = __ri_check_pkgid_for_deactivation(pkgid);
1190 _LOGE("fota-info : pkgid[%s] for deactivation dont need to install.\n", pkgid);
1195 manifest = pkgmgr_parser_get_manifest_file(pkgid);
1196 if (manifest == NULL) {
1197 _LOGE("fota-info : dont have manefest[pkgid=%s]\n", pkgid);
1201 _LOGD("fota-info : pkgid[%s] has manefest[%s]\n", pkgid, manifest);
1203 ret = pkgmgr_parser_parse_manifest_for_installation(manifest, temp);
1205 _LOGE("fota-info : installation fail[manifest=%s]\n", manifest);
1209 _LOGD("fota-info : pkgid[%s] installation success\n", pkgid);
1211 __rpm_apply_smack(pkgid,0);
1212 __ri_make_directory(pkgid);
1214 ret = _ri_apply_privilege(pkgid, 0);
1216 _LOGE("fota-info : _ri_apply_privilege fail[pkgid=%s]\n", pkgid);
1220 _LOGD("fota-info : pkgid[%s] apply smack success\n", pkgid);
1231 static int __ri_upgrade_fota(char *pkgid)
1233 _LOGD("fota-info : pkgid[%s] start upgrade\n", pkgid);
1236 char *manifest = NULL;
1237 char *temp[] = {"fota=true", NULL};
1239 /*if pkgid is one of deactivation, it does not need to upgrade*/
1240 ret = __ri_check_pkgid_for_deactivation(pkgid);
1242 _LOGE("fota-info : pkgid[%s] for deactivation dont need to install.\n", pkgid);
1247 manifest = pkgmgr_parser_get_manifest_file(pkgid);
1248 if (manifest == NULL) {
1249 _LOGE("fota-info : dont have manefest[pkgid=%s]\n", pkgid);
1253 _LOGD("fota-info : pkgid[%s] has manefest[%s]\n", pkgid, manifest);
1254 if (access(manifest, F_OK) != 0) {
1255 _LOGE("fota-info : can not access[manifest=%s]\n", manifest);
1260 ret = __ri_find_xml(pkgid, &manifest);
1262 _LOGE("fota-info : can not find xml[pkgid=%s]\n", pkgid);
1268 if (manifest == NULL) {
1272 ret = pkgmgr_parser_parse_manifest_for_upgrade(manifest, temp);
1274 _LOGE("fota-info : upgrade fail[manifest=%s]\n", manifest);
1279 __rpm_apply_smack(pkgid,0);
1280 __ri_make_directory(pkgid);
1282 ret = _ri_apply_privilege(pkgid, 0);
1284 _LOGE("fota-info : _ri_apply_privilege fail[pkgid=%s]\n", pkgid);
1287 _LOGD("fota-info : pkgid[%s] upgrade success\n", pkgid);
1296 static int __ri_uninstall_fota(char *pkgid)
1298 _LOGD("fota-info : pkgid[%s] start uninstallation\n", pkgid);
1302 ret = _ri_privilege_unregister_package(pkgid);
1304 _LOGE("fota-info : _ri_privilege_unregister_package fail[pkgid=%s]\n", pkgid);
1307 ret = pkgmgr_parser_parse_manifest_for_uninstallation(pkgid, NULL);
1309 _LOGE("fota-info : uninstall fail[manifest=%s]\n", pkgid);
1312 _LOGD("fota-info : pkgid[%s] uninstall success\n", pkgid);
1317 static char * __getvalue(const char* pBuf, const char* pKey)
1319 const char* p = NULL;
1320 const char* pStart = NULL;
1321 const char* pEnd = NULL;
1323 p = strstr(pBuf, pKey);
1327 pStart = p + strlen(pKey) + 1;
1328 pEnd = strchr(pStart, SEPERATOR_START);
1332 size_t len = pEnd - pStart;
1336 char *pRes = (char*)malloc(len + 1);
1338 _LOGE("@malloc failed");
1341 strncpy(pRes, pStart, len);
1347 static char *__find_rpm_pkgid(const char* manifest)
1350 char buf[BUF_SIZE] = {0};
1353 fp = fopen(manifest, "r");
1355 _LOGE("csc-info : Fail get : %s\n", manifest);
1359 while (fgets(buf, BUF_SIZE, fp) != NULL) {
1361 pkgid = __getvalue(buf, TOKEN_PACKAGE_STR);
1362 if (pkgid != NULL) {
1366 memset(buf, 0x00, BUF_SIZE);
1375 static int __copy_file( const char *src_path, const char *dst_path)
1379 unsigned char temp_buf[8192] = {'\0',};
1380 size_t size_of_uchar = sizeof( unsigned char);
1381 size_t size_of_temp_buf = sizeof( temp_buf);
1383 src = fopen(src_path, "r");
1385 _LOGE("Failed to open(). path=%s, E:%d(%s)", src_path, errno, strerror(errno));
1389 dst = fopen(dst_path, "w");
1391 _LOGE("Failed to open dst file. file=%s, E:%d(%s)", dst_path, errno, strerror(errno));
1397 rc = fread( temp_buf, size_of_uchar, size_of_temp_buf, src);
1398 fwrite( temp_buf, size_of_uchar, rc, dst);
1406 static int __ri_install_csc(char *path_str, char *remove_str)
1411 char delims[] = "/";
1413 char argv[BUF_SIZE] = {'\0'};
1414 char xml_name[BUF_SIZE] = {'\0'};
1415 char src_file[BUF_SIZE] = {'\0'};
1416 char dest_file[BUF_SIZE] = {'\0'};
1418 snprintf(src_file, sizeof(src_file), "%s", path_str);
1420 /*get pkgid from path str*/
1421 pkgid = __find_rpm_pkgid(path_str);
1422 if (pkgid == NULL) {
1423 _LOGE("csc-info : fail to find pkgid\n");
1426 _LOGD("csc-info : find pkgid=[%s] for installation\n", pkgid);
1429 token = strtok(path_str, delims);
1432 memset(xml_name, 0x00, sizeof(xml_name));
1433 strncat(xml_name, token, strlen(token));
1434 token = strtok(NULL, delims);
1436 _LOGD("csc-info : xml name = %s\n", xml_name);
1438 /*copy xml to /opt/share/packages*/
1439 snprintf(dest_file, sizeof(dest_file), "%s/%s", OPT_SHARE_PACKAGES, xml_name);
1440 ret = __copy_file(src_file, dest_file);
1442 _LOGE("csc-info : xml copy fail(%d)\n", ret);
1444 _LOGE("csc-info : xml copy success to [%s] \n", dest_file);
1447 /*remove old pkg info*/
1448 ret = pkgmgr_parser_parse_manifest_for_uninstallation(pkgid, NULL);
1450 _LOGD("csc-info : fail remove old pkg info\n");
1452 _LOGD("csc-info : success remove old pkg info\n");
1455 /*insert new pkg info*/
1456 memset(argv, 0x00, sizeof(argv));
1457 snprintf(argv, sizeof(argv), "%s/%s", OPT_SHARE_PACKAGES, xml_name);
1458 ret = __ri_init_csc_xml(argv, remove_str);
1460 _LOGD("csc-info : fail insert db\n");
1462 _LOGD("csc-info : success xml name = %s\n", xml_name);
1472 static int __ri_uninstall_csc(char *pkgid)
1474 /*remove old pkg info*/
1475 int ret = pkgmgr_parser_parse_manifest_for_uninstallation(pkgid, NULL);
1477 _LOGD("csc-info : fail remove old pkg info\n");
1479 _LOGD("csc-info : success remove old pkg info\n");
1486 static int __get_size_from_xml(const char *manifest, int *size)
1488 const char *val = NULL;
1489 const xmlChar *node;
1490 xmlTextReaderPtr reader;
1491 int ret = PMINFO_R_OK;
1493 if(manifest == NULL) {
1494 _LOGE("Input argument is NULL\n");
1495 return PMINFO_R_ERROR;
1499 _LOGE("Argument supplied to hold return value is NULL\n");
1500 return PMINFO_R_ERROR;
1503 reader = xmlReaderForFile(manifest, NULL, 0);
1506 if (_child_element(reader, -1)) {
1507 node = xmlTextReaderConstName(reader);
1509 _LOGE("xmlTextReaderConstName value is NULL\n");
1510 ret = PMINFO_R_ERROR;
1514 if (!strcmp(ASCII(node), "manifest")) {
1515 ret = _ri_get_attribute(reader,"size",&val);
1517 _LOGE("@Error in getting the attribute value");
1518 ret = PMINFO_R_ERROR;
1526 _LOGE("package size is not specified\n");
1527 ret = PMINFO_R_ERROR;
1531 _LOGE("Unable to create xml reader\n");
1532 ret = PMINFO_R_ERROR;
1537 _LOGE("xmlReaderForFile value is NULL\n");
1538 ret = PMINFO_R_ERROR;
1543 xmlFreeTextReader(reader);
1547 static int __get_location_from_xml(const char *manifest, pkgmgrinfo_install_location *location)
1549 const char *val = NULL;
1550 const xmlChar *node;
1551 xmlTextReaderPtr reader;
1554 if(manifest == NULL) {
1555 _LOGE("Input argument is NULL\n");
1556 return PMINFO_R_ERROR;
1559 if(location == NULL) {
1560 _LOGE("Argument supplied to hold return value is NULL\n");
1561 return PMINFO_R_ERROR;
1564 reader = xmlReaderForFile(manifest, NULL, 0);
1567 if ( _child_element(reader, -1)) {
1568 node = xmlTextReaderConstName(reader);
1570 _LOGE("xmlTextReaderConstName value is NULL\n");
1571 xmlFreeTextReader(reader);
1572 return PMINFO_R_ERROR;
1575 if (!strcmp(ASCII(node), "manifest")) {
1576 ret = _ri_get_attribute(reader,"install-location",&val);
1578 _LOGE("@Error in getting the attribute value");
1579 xmlFreeTextReader(reader);
1580 return PMINFO_R_ERROR;
1584 if (strcmp(val, "internal-only") == 0)
1585 *location = PMINFO_INSTALL_LOCATION_INTERNAL_ONLY;
1586 else if (strcmp(val, "prefer-external") == 0)
1587 *location = PMINFO_INSTALL_LOCATION_PREFER_EXTERNAL;
1589 *location = PMINFO_INSTALL_LOCATION_AUTO;
1593 _LOGE("Unable to create xml reader\n");
1594 xmlFreeTextReader(reader);
1595 return PMINFO_R_ERROR;
1599 _LOGE("xmlReaderForFile value is NULL\n");
1600 return PMINFO_R_ERROR;
1603 xmlFreeTextReader(reader);
1608 static char *__get_pkg_path(const char *pkg_path, const char *pkgid)
1611 char buff[BUF_SIZE] = {'\0'};
1612 char *real_path = NULL;
1614 snprintf(buff, BUF_SIZE, "%s/%s", pkg_path, pkgid);
1616 if (__is_dir(buff)) break;
1617 memset(buff, '\0', BUF_SIZE);
1618 snprintf(buff, BUF_SIZE, "%s/%s", USR_APPS,pkgid);
1619 if (__is_dir(buff)) break;
1620 memset(buff, '\0', BUF_SIZE);
1621 snprintf(buff, BUF_SIZE, "/opt/apps/%s", pkgid);
1622 if (__is_dir(buff)) break;
1623 memset(buff, '\0', BUF_SIZE);
1624 snprintf(buff, BUF_SIZE, "%s/%s", OPT_USR_APPS,pkgid);
1625 if (__is_dir(buff)) break;
1630 _LOGE("chdir(%s) failed. [%s]", buff, strerror(errno));
1634 real_path = (char *)malloc(strlen(buff) + 1);
1635 if (real_path == NULL) {
1636 _LOGE("malloc() failed.");
1639 memset(real_path, '\0', strlen(buff) + 1);
1640 memcpy(real_path, buff, strlen(buff));
1645 static int __set_label_for_pkg_dir(const char *dirpath, const char* label)
1649 struct dirent entry;
1650 struct dirent *result;
1651 char fullpath[BUF_SIZE] = {'\0'};
1653 if (access(dirpath, F_OK) != 0) {
1654 _LOGE("skip! empty dirpath = [%s]", dirpath);
1658 dir = opendir(dirpath);
1660 _LOGE("opendir(%s) failed. [%d][%s]", dirpath, errno, strerror(errno));
1664 if(lsetxattr(dirpath, "security.SMACK64", label, strlen(label), 0)) {
1665 _LOGE("error(%d) in setting smack label",errno);
1668 for (ret = readdir_r(dir, &entry, &result); ret == 0 && result != NULL; ret = readdir_r(dir, &entry, &result)){
1669 if (!strcmp(entry.d_name, ".") ||
1670 !strcmp(entry.d_name, "..")) {
1675 snprintf(fullpath, BUF_SIZE, "%s/%s", dirpath, entry.d_name);
1676 if(lsetxattr(fullpath, "security.SMACK64", label, strlen(label), 0)) {
1677 _LOGE("error(%d) in setting smack label",errno);
1682 if (entry.d_type == DT_DIR) {
1683 ret = __set_label_for_pkg_dir(fullpath,label);
1685 _LOGE("_coretpk_installer_apply_directory_policy(%s) failed.", fullpath);
1688 memset(fullpath, '\0', BUF_SIZE);
1697 void _ri_soft_reset(char *pkgid)
1700 char dirpath[PKG_STRING_LEN_MAX] = {'\0'};
1703 snprintf(dirpath, PKG_STRING_LEN_MAX, "%s/%s", OPT_USR_APPS, pkgid);
1704 if (access(dirpath, F_OK)==0) {
1705 __set_label_for_pkg_dir(dirpath, "_");
1709 memset(dirpath, '\0', PKG_STRING_LEN_MAX);
1710 snprintf(dirpath, PKG_STRING_LEN_MAX, "%s/%s/data", OPT_USR_APPS, pkgid);
1711 if (access(dirpath, F_OK)==0) {
1712 __set_label_for_pkg_dir(dirpath, pkgid);
1716 memset(dirpath, '\0', PKG_STRING_LEN_MAX);
1717 snprintf(dirpath, PKG_STRING_LEN_MAX, "%s/%s/shared", OPT_USR_APPS, pkgid);
1718 if (access(dirpath, F_OK)==0) {
1719 __set_label_for_pkg_dir(dirpath, "_");
1723 memset(dirpath, '\0', PKG_STRING_LEN_MAX);
1724 snprintf(dirpath, PKG_STRING_LEN_MAX, "%s/%s/shared/res", OPT_USR_APPS, pkgid);
1725 if (access(dirpath, F_OK)==0) {
1726 __set_label_for_pkg_dir(dirpath, "_");
1730 memset(dirpath, '\0', PKG_STRING_LEN_MAX);
1731 snprintf(dirpath, PKG_STRING_LEN_MAX, "%s/%s/shared/data", OPT_USR_APPS, pkgid);
1732 if (access(dirpath, F_OK)==0) {
1733 __set_label_for_pkg_dir(dirpath, "_");
1737 memset(dirpath, '\0', PKG_STRING_LEN_MAX);
1738 snprintf(dirpath, PKG_STRING_LEN_MAX, "%s/%s/shared/trusted", OPT_USR_APPS, pkgid);
1739 if (access(dirpath, F_OK)==0) {
1740 pkgmgrinfo_pkginfo_h handle = NULL;
1741 char *groupid = NULL;
1742 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
1743 if (ret != PMINFO_R_OK)
1745 /* ret = pkgmgrinfo_pkginfo_get_groupid(handle, &groupid);
1746 if (ret == PMINFO_R_OK) {
1747 _ri_privilege_setup_path(pkgid, dirpath, APP_PATH_GROUP_RW, groupid);
1749 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
1755 void _ri_register_cert(const char *pkgid)
1758 pkgmgrinfo_instcertinfo_h handle = NULL;
1761 error = pkgmgrinfo_create_certinfo_set_handle(&handle);
1763 _LOGE("Cert handle creation failed. Err:%d", error);
1764 __ri_free_cert_chain();
1768 if (list[SIG_AUTH].cert_value == NULL) {
1769 _LOGE("pkgid[%s] dont have SIG_AUTH.cert_value ", pkgid);
1773 for (i = 0; i < MAX_CERT_NUM; i++) {
1775 if (list[i].cert_value) {
1776 error = pkgmgrinfo_set_cert_value(handle, list[i].cert_type, list[i].cert_value);
1778 _LOGE("pkgmgrinfo_set_cert_value failed. cert type:%d. Err:%d", list[i].cert_type, error);
1783 /* Save the certificates in cert DB*/
1784 error = pkgmgrinfo_save_certinfo(pkgid, handle, getuid());
1786 _LOGE("pkgmgrinfo_save_certinfo failed. Err:%d", error);
1791 pkgmgrinfo_destroy_certinfo_set_handle(handle);
1792 __ri_free_cert_chain();
1795 void _ri_unregister_cert(const char *pkgid)
1798 /* Delete the certifictes from cert DB*/
1799 error = pkgmgrinfo_delete_usr_certinfo(pkgid, getuid());
1801 _LOGE("pkgmgrinfo_delete_certinfo failed. Err:%d", error);
1806 int _ri_verify_sig_and_cert(const char *sigfile, int *visibility)
1808 char certval[MAX_BUFF_LEN] = {'\0'};
1813 int ret = RPM_INSTALLER_SUCCESS;
1815 signature_x *signx = NULL;
1816 struct keyinfo_x *keyinfo = NULL;
1817 struct x509data_x *x509data = NULL;
1818 CERT_CONTEXT *ctx = NULL;
1821 ctx = cert_svc_cert_context_init();
1823 _LOGE("cert_svc_cert_context_init() failed.");
1824 return RPM_INSTALLER_ERR_INTERNAL;
1827 if (strstr(sigfile, AUTHOR_SIGNATURE_XML))
1829 else if (strstr(sigfile, SIGNATURE1_XML))
1830 sigtype = SIG_DIST1;
1831 else if (strstr(sigfile, SIGNATURE2_XML))
1832 sigtype = SIG_DIST2;
1834 _LOGE("Unsupported signature type! [%s]", sigfile);
1835 cert_svc_cert_context_final(ctx);
1836 return RPM_INSTALLER_ERR_INTERNAL;
1839 signx = _ri_process_signature_xml(sigfile);
1840 if (signx == NULL) {
1841 _LOGE("_ri_process_signature_xml(%s) failed.", sigfile);
1842 ret = RPM_INSTALLER_ERR_INTERNAL;
1846 keyinfo = signx->keyinfo;
1847 if ((keyinfo == NULL) || (keyinfo->x509data == NULL) || (keyinfo->x509data->x509certificate == NULL)) {
1848 _LOGE("keyinfo is invalid. [%s]", sigfile);
1849 ret = RPM_INSTALLER_ERR_CERT_INVALID;
1853 x509data = keyinfo->x509data;
1854 x509certificate_x *cert = x509data->x509certificate;
1856 // First cert is Signer certificate
1857 if (cert->text != NULL) {
1858 for (i = 0; i <= (int)strlen(cert->text); i++) {
1859 if (cert->text[i] != '\n') {
1860 certval[j++] = cert->text[i];
1865 err = cert_svc_load_buf_to_context(ctx, (unsigned char*)certval);
1867 _LOGE("cert_svc_load_buf_to_context() failed. cert = [%s], err = [%d]", certval, err);
1868 ret = RPM_INSTALLER_ERR_INTERNAL;
1871 // _LOGD("SIG_SIGNER certval = [%d][%s]", strlen(certval), certval);
1873 err = __ri_create_cert_chain(sigtype, SIG_SIGNER, certval);
1875 _LOGE("__ri_create_cert_chain() failed. sigtype = [%d], certval = [%s]", sigtype, certval);
1876 __ri_free_cert_chain();
1877 ret = RPM_INSTALLER_ERR_INTERNAL;
1882 // Second cert is Intermediate certificate
1884 if (cert->text != NULL) {
1885 memset(certval, 0x00, MAX_BUFF_LEN);
1887 for (i = 0; i <= (int)strlen(cert->text); i++) {
1888 if (cert->text[i] != '\n') {
1889 certval[j++] = cert->text[i];
1894 if (cert->text != NULL) {
1895 err = cert_svc_push_buf_into_context(ctx, (unsigned char*)certval);
1897 _LOGE("cert_svc_push_buf_into_context() failed. cert = [%s], err = [%d]", certval, err);
1898 ret = RPM_INSTALLER_ERR_INTERNAL;
1902 // _LOGD("SIG_INTERMEDIATE certval = [%d][%s]", strlen(certval), certval);
1904 err = __ri_create_cert_chain(sigtype, SIG_INTERMEDIATE, certval);
1906 _LOGE("__ri_create_cert_chain() failed. sigtype = [%d], certval = [%s]", sigtype, certval);
1907 __ri_free_cert_chain();
1908 ret = RPM_INSTALLER_ERR_INTERNAL;
1912 _LOGE("Invalid CertChain! (cert->text is NULL.)");
1913 ret = RPM_INSTALLER_ERR_CERT_INVALID;
1917 err = cert_svc_verify_certificate(ctx, &validity);
1919 _LOGE("cert_svc_verify_certificate() failed.");
1920 ret = RPM_INSTALLER_ERR_INTERNAL;
1923 _LOGD("cert_svc_verify() is done successfully. validity=[%d]", validity);
1925 if (validity == 0) {
1926 _LOGE("Certificate Invalid/Expired (validity == 0)");
1927 ret = RPM_INSTALLER_ERR_CERTCHAIN_VERIFICATION_FAILED;
1932 // For reference validation, we should be in TEMP_DIR/usr/apps/<pkgid>
1933 if (ctx->fileNames && ctx->fileNames->filename) {
1934 _LOGD("Root CA cert path=[%s]", ctx->fileNames->filename);
1936 err = __ri_xmlsec_verify_signature(sigfile, ctx->fileNames->filename);
1938 _LOGE("__ri_xmlsec_verify_signature(%s) failed.", sigfile);
1939 ret = RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED;
1943 crt = __ri_get_cert_from_file(ctx->fileNames->filename);
1944 err = __ri_create_cert_chain(sigtype, SIG_ROOT, crt);
1946 _LOGE("__ri_create_cert_chain(%d) failed.", sigtype);
1947 __ri_free_cert_chain();
1948 ret = RPM_INSTALLER_ERR_INTERNAL;
1952 err = cert_svc_get_visibility(ctx, visibility);
1954 _LOGE("cert_svc_get_visibility() failed. err = [%d]", err);
1955 ret = RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED;
1958 _LOGD("cert_svc_get_visibility() returns visibility=[%d]", *visibility);*/
1960 _LOGE("No Root CA cert found. Signature validation failed.");
1961 ret = RPM_INSTALLER_ERR_ROOT_CERT_NOT_FOUND;
1968 cert_svc_cert_context_final(ctx);
1970 _ri_free_signature_xml(signx);
1975 int _ri_verify_signatures(const char *root_path, const char *pkgid)
1978 char buff[BUF_SIZE] = {'\0'};
1979 char *pkg_path = NULL;
1982 _LOGD("root_path=[%s], pkgid=[%s]", root_path, pkgid);
1984 // check for signature and certificate
1985 pkg_path = __get_pkg_path(root_path, pkgid);
1986 if (pkg_path == NULL) {
1987 _LOGE("__get_pkg_path(%s, %s) failed.", root_path, pkgid);
1991 _LOGD("switched to pkg_path=[%s]", pkg_path);
1993 // author-signature.xml is mandatory
1994 snprintf(buff, BUF_SIZE, "%s/author-signature.xml", pkg_path);
1995 if (access(buff, F_OK) == 0) {
1996 _LOGD("author-signature.xml, path=[%s]", buff);
1997 ret = _ri_verify_sig_and_cert(buff, &visibility);
1999 _LOGE("_ri_verify_sig_and_cert(%s) failed.", buff);
2003 _LOGD("------------------------------------------------------");
2004 _LOGD("signature is verified successfully");
2005 _LOGD("path=[%s]", buff);
2006 _LOGD("------------------------------------------------------");
2008 memset(buff, '\0', BUF_SIZE);
2010 // signature2.xml is optional
2011 snprintf(buff, BUF_SIZE, "%s/signature2.xml", pkg_path);
2012 if (access(buff, F_OK) == 0) {
2013 _LOGD("signature2.xml found. [%s]", pkg_path);
2014 ret = _ri_verify_sig_and_cert(buff, &visibility);
2016 _LOGE("_ri_verify_sig_and_cert(%s) failed.", buff);
2020 _LOGD("_ri_verify_sig_and_cert(%s) succeed.", buff);
2022 memset(buff, '\0', BUF_SIZE);
2024 // signature1.xml is mandatory
2025 snprintf(buff, BUF_SIZE, "%s/signature1.xml", pkg_path);
2026 if (access(buff, F_OK) == 0) {
2027 _LOGD("signature1.xml, path=[%s]", buff);
2028 ret = _ri_verify_sig_and_cert(buff, &visibility);
2030 _LOGE("_ri_verify_sig_and_cert(%s) failed.", buff);
2034 _LOGD("------------------------------------------------------");
2035 _LOGD("signature is verified successfully");
2036 _LOGD("path=[%s]", buff);
2037 _LOGD("------------------------------------------------------");
2039 memset(buff, '\0', BUF_SIZE);
2049 if ((ret != 0) && (sig_enable == 0)) {
2050 _LOGD("_ri_verify_signatures(%s, %s) failed, but it's ok for config.", root_path, pkgid);
2057 void _ri_apply_smack(char *pkgname, int flag)
2059 __rpm_apply_smack(pkgname, flag);
2062 int _ri_apply_privilege(char *pkgid, int visibility)
2066 pkgmgrinfo_pkginfo_h handle = NULL;
2068 int apptype = PERM_APP_TYPE_EFL;
2070 memset(&info, '\0', sizeof(info));
2072 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
2073 if (ret != PMINFO_R_OK)
2076 strncpy(info.package_id, pkgid, strlen(pkgid));
2077 info.visibility = visibility;
2079 ret = pkgmgrinfo_pkginfo_foreach_privilege(handle, __privilege_func, (void *)&info);
2080 if (ret != PMINFO_R_OK) {
2081 _LOGE("pkgmgrinfo_pkginfo_get_pkgid failed\n");
2082 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
2085 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
2087 if (visibility & CERT_SVC_VISIBILITY_PLATFORM) {
2088 _LOGD("VISIBILITY_PLATFORM!");
2089 apptype = PERM_APP_TYPE_EFL_PLATFORM;
2090 } else if ((visibility & CERT_SVC_VISIBILITY_PARTNER) ||
2091 (visibility & CERT_SVC_VISIBILITY_PARTNER_OPERATOR) ||
2092 (visibility & CERT_SVC_VISIBILITY_PARTNER_MANUFACTURER)) {
2093 _LOGD("VISIBILITY_PARTNER!");
2094 apptype = PERM_APP_TYPE_EFL_PARTNER;
2097 /*reload privilege*/
2098 const char *perm[] = {NULL, NULL};
2099 ret = _ri_privilege_enable_permissions(pkgid, apptype, perm, 1);
2104 int _ri_set_group_id(const char *pkgid, const char *groupid)
2106 retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "pkgid is NULL\n");
2107 retvm_if(groupid == NULL, PMINFO_R_EINVAL, "groupid is NULL\n");
2109 sqlite3 *pkginfo_db = NULL;
2113 ret = db_util_open(PKGMGR_DB, &pkginfo_db, 0);
2114 retvm_if(ret != SQLITE_OK, PMINFO_R_ERROR, "connect db [%s] failed!", PKGMGR_DB);
2116 /*Begin transaction*/
2117 ret = sqlite3_exec(pkginfo_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2118 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Failed to begin transaction\n");
2119 _LOGD("Transaction Begin\n");
2121 query = sqlite3_mprintf("update package_info set package_reserve3=%Q where package=%Q", groupid, pkgid);
2123 ret = sqlite3_exec(pkginfo_db, query, NULL, NULL, NULL);
2124 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
2126 /*Commit transaction*/
2127 ret = sqlite3_exec(pkginfo_db, "COMMIT", NULL, NULL, NULL);
2128 if (ret != SQLITE_OK) {
2129 _LOGE("Failed to commit transaction. Rollback now\n");
2130 ret = sqlite3_exec(pkginfo_db, "ROLLBACK", NULL, NULL, NULL);
2131 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
2133 _LOGD("Transaction Commit and End\n");
2137 sqlite3_free(query);
2138 sqlite3_close(pkginfo_db);
2142 static int __ri_install_fota_for_rw(char *pkgid)
2147 char buff[BUF_SIZE] = {'\0'};
2149 _LOGD("fota-info : pkgid[%s] start installation\n", pkgid);
2151 /*unzip pkg path from factoryrest data*/
2152 snprintf(buff, BUF_SIZE, "opt/usr/apps/%s/*", pkgid);
2153 const char *pkg_argv[] = { "/usr/bin/unzip", "-oX", OPT_ZIP_FILE, buff, "-d", "/", NULL };
2154 ret = _ri_xsystem(pkg_argv);
2156 _LOGE("fota-info : unzip root path[%s] is fail .\n", buff);
2160 _LOGD("fota-info : unzip root path[%s] is success\n", buff);
2162 /*unzip manifest from factoryrest data*/
2163 memset(buff, '\0', BUF_SIZE);
2164 snprintf(buff, BUF_SIZE, "opt/share/packages/%s.xml", pkgid);
2166 const char *xml_argv[] = { "/usr/bin/unzip", "-oX", OPT_ZIP_FILE, buff, "-d", "/", NULL };
2167 ret = _ri_xsystem(xml_argv);
2169 _LOGE("fota-info : xml_argv fail for pkgid[%s] .\n", pkgid);
2173 _LOGD("fota-info : xml_argv[%s] is success\n", pkgid);
2175 /*get updated manifest path*/
2176 memset(buff, '\0', BUF_SIZE);
2177 snprintf(buff, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
2178 _LOGE("Manifest name is %s\n", buff);
2180 /*apply smack for manifest*/
2181 _ri_privilege_change_smack_label(buff, pkgid, 0);/*0 is SMACK_LABEL_ACCESS*/
2183 /*register manifest*/
2184 char* fota_tags[3] = {NULL, };
2185 fota_tags[0] = "removable=true";
2186 fota_tags[1] = "preload=true";
2187 fota_tags[2] = NULL;
2189 ret = pkgmgr_parser_parse_manifest_for_installation(buff, fota_tags);
2191 _LOGE("Parsing Manifest Failed\n");
2195 _LOGD("Parsing Manifest Success\n");
2198 /*Register cert info*/
2199 _ri_register_cert(pkgid);
2201 /*apply smack for pkg root path*/
2202 memset(buff, '\0', BUF_SIZE);
2203 snprintf(buff, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgid);
2204 _ri_privilege_setup_path(pkgid, buff, PERM_APP_PATH_ANY_LABEL, pkgid);
2206 /*apply smack for defined directory*/
2207 __rpm_apply_smack(pkgid, home_dir);
2210 ret = _ri_apply_privilege(pkgid, 0);
2212 _LOGE("apply perm failed with err(%d)\n", ret);
2214 _LOGD("apply perm success\n");
2223 static int __ri_upgrade_fota_for_rw(char *pkgid)
2228 char buff[BUF_SIZE] = {'\0'};
2230 _LOGD("fota-info : pkgid[%s] start upgrade\n", pkgid);
2232 /*unzip pkg dir from factoryrest data*/
2233 snprintf(buff, BUF_SIZE, "opt/usr/apps/%s/*", pkgid);
2234 const char *pkg_argv[] = { "/usr/bin/unzip", "-oX", OPT_ZIP_FILE, buff, "-d", "/", NULL };
2235 ret = _ri_xsystem(pkg_argv);
2237 _LOGE("fota-info : unzip root path[%s] is fail .\n", buff);
2241 _LOGD("fota-info : unzip root path[%s] is success\n", buff);
2243 /*unzip manifest from factoryrest data*/
2244 memset(buff, '\0', BUF_SIZE);
2245 snprintf(buff, BUF_SIZE, "opt/share/packages/%s.xml", pkgid);
2246 const char *xml_argv[] = { "/usr/bin/unzip", "-oX", OPT_ZIP_FILE, buff, "-d", "/", NULL };
2247 ret = _ri_xsystem(xml_argv);
2249 _LOGE("fota-info : unzip manifest[%s] is fail .\n", buff);
2253 _LOGD("fota-info : unzip manifest[%s] is success\n", buff);
2255 /*get updated manifest path*/
2256 memset(buff, '\0', BUF_SIZE);
2257 snprintf(buff, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
2258 _LOGE("Manifest name is %s\n", buff);
2260 /*apply smack for manifest*/
2261 _ri_privilege_change_smack_label(buff, pkgid, 0);/*0 is SMACK_LABEL_ACCESS*/
2263 /*register manifest*/
2264 ret = pkgmgr_parser_parse_manifest_for_upgrade(buff, NULL);
2266 _LOGE("Parsing Manifest Failed\n");
2270 _LOGD("Parsing Manifest Success\n");
2273 /*Register new cert info*/
2274 _ri_unregister_cert(pkgid);
2275 _ri_register_cert(pkgid);
2277 /*apply smack for pkg root path*/
2278 memset(buff, '\0', BUF_SIZE);
2279 snprintf(buff, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgid);
2280 _ri_privilege_setup_path(pkgid, buff, PERM_APP_PATH_ANY_LABEL, pkgid);
2282 /*apply smack for defined directory*/
2283 __rpm_apply_smack(pkgid, home_dir);
2286 ret = _ri_apply_privilege(pkgid, 0);
2288 _LOGE("apply perm failed with err(%d)\n", ret);
2290 _LOGD("apply perm success\n");
2299 static int __ri_uninstall_fota_for_rw(char *pkgid)
2302 char buff[BUF_SIZE] = {'\0'};
2304 _LOGD("fota-info : pkgid[%s] start uninstall\n", pkgid);
2306 /*del root path dir*/
2307 snprintf(buff, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgid);
2309 if (__is_dir(buff)) {
2310 _rpm_delete_dir(buff);
2314 memset(buff, '\0', BUF_SIZE);
2315 snprintf(buff, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
2319 ret = pkgmgr_parser_parse_manifest_for_uninstallation(pkgid, NULL);
2321 _LOGE("Parsing Manifest Failed\n");
2324 /*execute privilege APIs*/
2325 _ri_privilege_revoke_permissions(pkgid);
2326 _ri_privilege_unregister_package(pkgid);
2328 /*Unregister cert info*/
2329 _ri_unregister_cert(pkgid);
2335 * callback for the pkgmgrinfo_appinfo_get_list used in _rpm_uninstall_pkg()
2337 int __ri_check_running_app(const pkgmgrinfo_appinfo_h handle, void *user_data)
2342 app_context_h appCtx = NULL;
2344 ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
2346 _LOGE("Failed to execute pkgmgrinfo_appinfo_get_appid[%d].\n", ret);
2350 ret = app_manager_is_running(appid, &isRunning);
2352 _LOGE("Failed to execute app_manager_is_running[%d].\n", ret);
2355 _LOGE("app[%s] , running state[%d].\n", appid, isRunning);
2358 ret = app_manager_get_app_context(appid, &appCtx);
2360 _LOGE("Failed to execute app_manager_get_app_context[%d].\n", ret);
2364 ret = app_manager_terminate_app(appCtx);
2366 _LOGE("Failed to execute app_manager_terminate_app[%d].\n", ret);
2367 app_context_destroy(appCtx);
2372 for (i = 0; i < TERMINATE_RETRY_COUNT; i++) {
2373 ret = app_manager_is_running(appid, &isRunning);
2375 _LOGE("Failed to execute app_manager_is_running[%d].\n", ret);
2376 app_context_destroy(appCtx);
2381 _LOGD("App(%s) is terminated.\n", appid);
2384 _LOGD("App(%s) is not terminated yet. wait count=[%d].\n", appid, i);
2389 ret = app_context_destroy(appCtx);
2391 _LOGE("Failed to execute app_context_destroy[%d].\n", ret);
2399 int __ri_change_dir(char *dirname)
2403 ret = mkdir(dirname, 0644);
2405 if (access(dirname, F_OK) == 0) {
2406 _rpm_delete_dir(dirname);
2407 ret = mkdir(dirname, 0644);
2409 _LOGE("mkdir(%s) failed\n", dirname);
2413 _LOGE("can not access[%s]\n", dirname);
2418 ret = chdir(dirname);
2420 _LOGE("chdir(%s) failed [%s]\n", dirname, strerror(errno));
2426 int _ri_smack_reload(const char *pkgid, rpm_request_type request_type)
2429 char *op_type = NULL;
2431 switch (request_type) {
2433 op_type = strdup("install");
2437 op_type = strdup("update");
2441 op_type = strdup("uninstall");
2448 if(op_type == NULL) {
2449 _LOGE("@Failed to reload smack. request_type not matched[pkgid=%s, op=%s]", pkgid, op_type);
2453 const char *smack_argv[] = { "/usr/bin/smack_reload.sh", op_type, pkgid, NULL };
2454 ret = _ri_xsystem(smack_argv);
2456 _LOGE("@Failed to reload smack[pkgid=%s, op=%s].", pkgid, op_type);
2458 _LOGD("#success: smack reload[pkgid=%s, op=%s]", pkgid, op_type);
2467 int _ri_smack_reload_all(void)
2471 const char *smack_argv[] = { "/usr/bin/smackload-fast", NULL};
2472 ret = _ri_xsystem(smack_argv);
2474 _LOGE("@Failed to reload all smack : %d", errno);
2476 _LOGD("#success: smack reload all");
2482 void __ri_remove_updated_dir(const char *pkgid)
2484 char path_buf[BUF_SIZE] = {'\0'};
2487 if (pkgid == NULL) {
2488 _LOGE("pkgid is NULL.");
2493 snprintf(path_buf, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, BIN_DIR_STR);
2494 if (__is_dir(path_buf)) {
2495 _LOGE("@pkgid[%s] need to clean dir[%s]\n", pkgid, path_buf);
2496 _rpm_delete_dir(path_buf);
2500 memset(path_buf, '\0', BUF_SIZE);
2501 snprintf(path_buf, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, RES_DIR_STR);
2502 if (__is_dir(path_buf)) {
2503 _LOGE("@pkgid[%s] need to clean dir[%s]\n", pkgid, path_buf);
2504 _rpm_delete_dir(path_buf);
2507 // remove shared/res dir
2508 memset(path_buf, '\0', BUF_SIZE);
2509 snprintf(path_buf, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, SHARED_RES_DIR_STR);
2510 if (__is_dir(path_buf)) {
2511 _LOGE("@pkgid[%s] need to clean dir[%s]\n", pkgid, path_buf);
2512 _rpm_delete_dir(path_buf);
2516 static int __metadata_func(const char *key, const char *value, void *user_data)
2522 _LOGE("key is null\n");
2525 if (value == NULL) {
2526 _LOGE("value is null\n");
2529 if (user_data == NULL) {
2530 _LOGE("user_data is null\n");
2534 if ((strcmp(key, "launch-on-attach") == 0) && (strcmp(value, "true") == 0)) {
2535 _LOGE("consumer[%s] : launch-on-attach is true \n", (char *)user_data);
2537 ret = app_manager_is_running((char *)user_data, &isRunning);
2539 _LOGE("Failed to execute app_manager_is_running[%s].\n", (char *)user_data);
2544 _LOGE("consumer[%s] is already launched \n", (char *)user_data);
2546 usleep(100 * 1000); /* 100ms sleep for infomation ready*/
2547 ret = aul_launch_app((char *)user_data, NULL);
2548 if (ret == AUL_R_ERROR) {
2549 _LOGE("consumer[%s] launch fail, sleep and retry launch_app\n", (char *)user_data);
2550 usleep(100 * 1000); /* 100ms sleep for infomation ready*/
2551 aul_launch_app((char *)user_data, NULL);
2553 _LOGE("consumer[%s] is launched !!!! \n", (char *)user_data);
2559 static int __ri_find_svcapp(const pkgmgrinfo_appinfo_h handle, void *user_data)
2563 char *component_type = NULL;
2565 ret = pkgmgrinfo_appinfo_get_component_type(handle, &component_type);
2566 if (ret != PMINFO_R_OK) {
2567 _LOGE("@Failed to get component_type\n");
2571 if (strcmp(component_type, "svcapp") == 0) {
2572 ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
2573 if (ret != PMINFO_R_OK) {
2574 _LOGE("@Failed to get appid\n");
2577 _LOGE("@find consumer[%s], check metadata for launch\n", appid);
2579 ret = pkgmgrinfo_appinfo_foreach_metadata(handle, __metadata_func, (void *)appid);
2580 if (ret != PMINFO_R_OK) {
2581 _LOGE("@Failed to get foreach_metadata\n");
2589 static void __ri_launch_consumer(const char *pkgid)
2592 pkgmgrinfo_pkginfo_h pkghandle = NULL;
2594 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkghandle);
2596 _LOGE("@Failed to get pkginfo handle [%s]\n", pkgid);
2600 ret = pkgmgrinfo_appinfo_get_list(pkghandle, PM_UI_APP, __ri_find_svcapp, NULL);
2602 _LOGE("@Failed to get appinfo_get_list [%s]\n", pkgid);
2605 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
2608 static int __ail_change_info(int op, const char *appid)
2610 void *lib_handle = NULL;
2611 int (*ail_desktop_operation) (const char *);
2615 if ((lib_handle = dlopen(LIBAIL_PATH, RTLD_LAZY)) == NULL) {
2616 _LOGE("dlopen is failed LIBAIL_PATH[%s]\n", LIBAIL_PATH);
2623 aop = "ail_desktop_add";
2626 aop = "ail_desktop_update";
2629 aop = "ail_desktop_remove";
2632 aop = "ail_desktop_clean";
2635 aop = "ail_desktop_fota";
2642 if ((ail_desktop_operation =
2643 dlsym(lib_handle, aop)) == NULL || dlerror() != NULL) {
2644 _LOGE("can not find symbol \n");
2649 ret = ail_desktop_operation(appid);
2653 dlclose(lib_handle);
2658 static int __ri_update_ail_info(const pkgmgrinfo_appinfo_h handle, void *user_data)
2664 ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
2666 _LOGE("Failed to execute pkgmgrinfo_appinfo_get_appid[%d].\n", ret);
2670 ret = __ail_change_info(AIL_INSTALL, appid);
2672 _LOGE("Failed to execute __ail_change_info[%s].\n", appid);
2680 static int __child_list_cb (const pkgmgrinfo_pkginfo_h handle, void *user_data)
2685 ret = pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid);
2687 _LOGE("get_pkgid failed\n");
2691 _LOGD("@child pkgid is [%s] for uninstallation", pkgid);
2693 ret = _rpm_uninstall_pkg_with_dbpath(pkgid, 0);
2695 _LOGE("uninstall pkg(%s) failed\n",pkgid );
2701 static void __uninstall_child_package_by_mother_pkgid(const char *pkgid)
2705 pkgmgrinfo_pkginfo_filter_h handle = NULL;
2707 ret = pkgmgrinfo_pkginfo_filter_create(&handle);
2709 _LOGE("filter_create failed for (%s)\n", pkgid);
2713 ret = pkgmgrinfo_pkginfo_filter_add_string(handle, PMINFO_PKGINFO_PROP_PACKAGE_STORECLIENT_ID, pkgid);
2715 _LOGE("PMINFO_PKGINFO_PROP_PACKAGE_STORECLIENT_ID add failed\n");
2719 ret = pkgmgrinfo_pkginfo_filter_foreach_pkginfo(handle, __child_list_cb, NULL);
2721 _LOGE("foreach_pkginfo failed\n");
2725 pkgmgrinfo_pkginfo_filter_destroy(handle);
2729 int _rpm_install_pkg_with_dbpath(char *pkgfilepath, char *pkgid, char *clientid)
2732 char manifest[BUF_SIZE] = { '\0'};
2733 char resultxml[BUF_SIZE] = {'\0'};
2734 char cwd[BUF_SIZE] = {'\0'};
2737 #ifdef APP2EXT_ENABLE
2738 app2ext_handle *handle = NULL;
2739 GList *dir_list = NULL;
2740 pkgmgrinfo_install_location location = 1;
2742 unsigned long rpm_size = 0;
2744 /*send event for start*/
2745 _ri_broadcast_status_notification(pkgid, "rpm", "start", "install");
2746 _LOGD("[#]start : _rpm_install_pkg_with_dbpath");
2749 temp = getcwd(cwd, BUF_SIZE);
2750 if ((temp == NULL) ||(cwd[0] == '\0')) {
2751 _LOGE("@failed to get the current directory info.");
2752 ret = RPM_INSTALLER_ERR_INTERNAL;
2755 _LOGD("#current working directory is %s", cwd);
2758 ret = __ri_change_dir(TEMP_DIR);
2760 _LOGE("@failed to change directory.");
2761 ret = RPM_INSTALLER_ERR_INTERNAL;
2764 _LOGD("#switched to %s", TEMP_DIR);
2767 const char *cpio_argv[] = { CPIO_SCRIPT, pkgfilepath, NULL };
2768 ret = _ri_xsystem(cpio_argv);
2770 /*get manifext.xml path*/
2771 snprintf(manifest, BUF_SIZE, "%s/opt/share/packages/%s.xml", TEMP_DIR, pkgid);
2772 _LOGD("#manifest name is %s", manifest);
2774 if (access(manifest, F_OK)) {
2775 _LOGD("#there is no RW manifest.xml. check RO manifest.xml.");
2777 memset(manifest, '\0', sizeof(manifest));
2778 snprintf(manifest, BUF_SIZE, "%s/usr/share/packages/%s.xml", TEMP_DIR, pkgid);
2779 _LOGD("#manifest name is %s", manifest);
2781 if (access(manifest, F_OK)) {
2782 _LOGE("@can not find manifest.xml in the pkg.");
2783 ret = RPM_INSTALLER_ERR_NO_MANIFEST;
2789 #if 0 //disable "copy ro-xml to rw-xml", because of some bug
2790 snprintf(srcpath, BUF_SIZE, "%s", manifest);
2791 memset(manifest, '\0', sizeof(manifest));
2792 snprintf(manifest, BUF_SIZE, "%s/%s.xml", MANIFEST_RW_DIRECTORY, pkgid);
2794 const char *xml_update_argv[] = { CPIO_SCRIPT_UPDATE_XML, srcpath, manifest, NULL };
2795 ret = _ri_xsystem(xml_update_argv);
2800 /*send event for install_percent*/
2801 _ri_broadcast_status_notification(pkgid, "rpm", "install_percent", "30");
2803 /*check manifest.xml validation*/
2804 ret = pkgmgr_parser_check_manifest_validation(manifest);
2806 _LOGE("@invalid manifest");
2807 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
2811 /*check for signature and certificate*/
2812 ret = _ri_verify_signatures(TEMP_DIR, pkgid);
2814 _LOGE("@signature and certificate failed(%s).", pkgid);
2815 ret = RPM_INSTALLER_ERR_SIG_INVALID;
2818 _LOGD("#_ri_verify_signatures success.");
2823 _LOGE("@failed to change directory(%s)(%s)", cwd, strerror(errno));
2824 ret = RPM_INSTALLER_ERR_INTERNAL;
2828 #ifdef APP2EXT_ENABLE
2829 ret = __get_location_from_xml(manifest, &location);
2831 _LOGE("@Failed to get install location\n");
2832 ret = RPM_INSTALLER_ERR_INTERNAL;
2835 if (location == PMINFO_INSTALL_LOCATION_PREFER_EXTERNAL) {
2836 _LOGD("#Install: external storage location");
2838 /*Get the rpm's size from rpm header*/
2839 rpm_size = _ri_calculate_rpm_size(pkgfilepath);
2841 rpm_size = rpm_size/(1024*1024); //rpm size in MB
2842 _LOGD("#Rpm file(%s) size is %lu MB",pkgfilepath,rpm_size);
2844 /*Add margin to the rpm size*/
2845 rpm_size = rpm_size + RPM_SIZE_MARGIN(rpm_size);
2846 _LOGD("#Rpm file (%s) size after margin is %lu MB",pkgfilepath,rpm_size);
2848 _LOGE("@Failed to get size from rpm header\n");
2849 ret = RPM_INSTALLER_ERR_INTERNAL;
2854 Get the size from the manifest file.
2856 ret = __get_size_from_xml(manifest, &size);
2857 if (ret != PMINFO_R_OK) {
2859 _LOGD(" #rpm size is %d MB",size);
2861 size = size>rpm_size?size:rpm_size;
2862 _LOGD("#rpm size is %d MB",size);
2867 if ((location == PMINFO_INSTALL_LOCATION_PREFER_EXTERNAL) && size > 0) {
2868 handle = app2ext_init(APP2EXT_SD_CARD);
2869 if (handle == NULL) {
2870 _LOGE("@app2ext init failed\n");
2871 ret = RPM_INSTALLER_ERR_INTERNAL;
2874 if ((&(handle->interface) != NULL) && (handle->interface.pre_install != NULL) && (handle->interface.post_install != NULL)){
2875 dir_list = __rpm_populate_dir_list();
2876 if (dir_list == NULL) {
2877 _LOGE("@ \nError in populating the directory list\n");
2878 app2ext_deinit(handle);
2879 ret = RPM_INSTALLER_ERR_RPM_SCRIPT_WRONG_ARGS;
2882 ret = handle->interface.pre_install(gpkgname, dir_list, size);
2883 if (ret == APP2EXT_ERROR_MMC_STATUS) {
2884 _LOGE("@app2xt MMC is not here, go internal\n");
2885 } else if (ret == APP2EXT_SUCCESS){
2886 _LOGE("@pre_install done, go internal\n");
2889 _LOGE("@app2xt pre install API failed (%d)\n", ret);
2890 __rpm_clear_dir_list(dir_list);
2891 handle->interface.post_install(gpkgname, APP2EXT_STATUS_FAILED);
2892 app2ext_deinit(handle);
2893 ret = RPM_INSTALLER_ERR_INTERNAL;
2902 if (home_dir == 0) {
2903 #if 0 //disable "INSTALL_SCRIPT_WITH_DBPATH_RO", because of some bug
2904 const char *argv[] = { INSTALL_SCRIPT_WITH_DBPATH_RO, pkgfilepath, NULL };
2905 ret = _ri_xsystem(argv);
2907 const char *argv[] = { INSTALL_SCRIPT, pkgfilepath, NULL };
2908 ret = _ri_xsystem(argv);
2910 const char *argv[] = { INSTALL_SCRIPT_WITH_DBPATH_RW, pkgfilepath, NULL };
2911 ret = _ri_xsystem(argv);
2914 _LOGE("@failed to install the pkg(%d).", ret);
2915 #ifdef APP2EXT_ENABLE
2916 if ((handle != NULL) && (handle->interface.post_install != NULL)){
2917 __rpm_clear_dir_list(dir_list);
2918 handle->interface.post_install(gpkgname, APP2EXT_STATUS_FAILED);
2919 app2ext_deinit(handle);
2924 _LOGD("#install success.");
2926 /*write the storeclient-id to manifest.xml*/
2927 if (clientid != NULL) {
2928 if (home_dir == 0) {
2929 snprintf(resultxml, BUF_SIZE, "%s/%s.xml", USR_SHARE_PACKAGES, pkgid);
2931 snprintf(resultxml, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
2934 const char *convert_argv[] = { RPM_UPDATE_XML, manifest, clientid, resultxml, NULL };
2935 ret = _ri_xsystem(convert_argv);
2937 _LOGE("@Failed to convert the manifest.xml");
2941 _LOGD("#client id[%s], input manifest:[%s], dest manifest:[%s]", clientid, manifest, resultxml);
2944 /*send event for install_percent*/
2945 _ri_broadcast_status_notification(pkgid, "rpm", "install_percent", "60");
2947 #ifdef APP2EXT_ENABLE
2948 if ((handle != NULL) && (handle->interface.post_install != NULL)){
2949 __rpm_clear_dir_list(dir_list);
2950 handle->interface.post_install(gpkgname, APP2EXT_STATUS_SUCCESS);
2951 app2ext_deinit(handle);
2955 /*Parse the manifest to get install location and size. If installation fails, remove manifest info from DB*/
2956 if (clientid != NULL) {
2957 ret = pkgmgr_parser_parse_manifest_for_installation(resultxml, NULL);
2959 ret = pkgmgr_parser_parse_manifest_for_installation(manifest, NULL);
2962 _LOGE("@failed to parse the manifest.");
2963 ret = RPM_INSTALLER_ERR_INTERNAL;
2966 _LOGD("#manifest parsing success");
2968 /*register cert info*/
2969 _ri_register_cert(pkgid);
2972 _coretpk_installer_search_ui_gadget(pkgid);
2974 /*apply smack to shared dir*/
2975 __rpm_apply_smack(pkgid, 1);
2977 /*apply smack by privilege*/
2978 ret = _ri_apply_privilege(pkgid, 0);
2980 _LOGE("@failed to apply permission(%d).", ret);
2982 _LOGD("#permission applying success.");
2985 ret = _ri_smack_reload_all();
2987 _LOGD("@failed to reload_all the smack.");
2990 /*send event for install_percent*/
2991 _ri_broadcast_status_notification(pkgid, "rpm", "install_percent", "100");
2994 _rpm_delete_dir(TEMP_DIR);
2995 _rpm_delete_dir(TEMP_DBPATH);
2997 if (ret == RPM_INSTALLER_SUCCESS) {
2998 _LOGD("[#]end : _rpm_install_pkg_with_dbpath");
2999 __ri_launch_consumer(pkgid);
3000 _ri_broadcast_status_notification(pkgid, "rpm", "end", "ok");
3001 _ri_stat_cb(pkgid, "end", "ok");
3003 _LOGE("[@]end : _rpm_install_pkg_with_dbpath");
3005 ret = _coretpk_installer_remove_db_info(pkgid);
3007 _LOGE("_coretpk_installer_remove_db_info is failed.");
3010 char *errstr = NULL;
3011 _ri_error_no_to_string(ret, &errstr);
3012 _ri_broadcast_status_notification(pkgid, "rpm", "error", errstr);
3013 _ri_stat_cb(pkgid, "error", errstr);
3014 _ri_broadcast_status_notification(pkgid, "rpm", "end", "fail");
3016 _ri_stat_cb(pkgid, "end", "fail");
3017 _LOGE("install failed with err(%d) (%s)\n", ret, errstr);
3023 int _rpm_upgrade_pkg_with_dbpath(char *pkgfilepath, char *pkgid)
3026 char manifest[BUF_SIZE] = { '\0'};
3027 char cwd[BUF_SIZE] = {'\0'};
3029 pkgmgrinfo_pkginfo_h pkghandle;
3031 #ifdef APP2EXT_ENABLE
3032 app2ext_handle *handle = NULL;
3033 GList *dir_list = NULL;
3034 pkgmgrinfo_installed_storage location = 1;
3036 unsigned long rpm_size = 0;
3038 _ri_broadcast_status_notification(pkgid, "rpm", "start", "update");
3039 _LOGD("[#]start : _rpm_upgrade_pkg_with_dbpath");
3041 /*terminate running app*/
3042 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkghandle);
3044 _LOGE("@failed to get pkginfo handle");
3045 ret = RPM_INSTALLER_ERR_PKG_NOT_FOUND;
3048 pkgmgrinfo_appinfo_get_list(pkghandle, PM_UI_APP, __ri_check_running_app, NULL);
3049 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
3052 temp = getcwd(cwd, BUF_SIZE);
3053 if ((temp == NULL) ||(cwd[0] == '\0')) {
3054 _LOGE("@getcwd() failed.");
3055 ret = RPM_INSTALLER_ERR_INTERNAL;
3058 _LOGD("#current working directory is %s.", cwd);
3061 ret = __ri_change_dir(TEMP_DIR);
3063 _LOGE("@change dir failed.");
3064 ret = RPM_INSTALLER_ERR_INTERNAL;
3067 _LOGD("#switched to %s", TEMP_DIR);
3070 const char *cpio_argv[] = { CPIO_SCRIPT, pkgfilepath, NULL };
3071 ret = _ri_xsystem(cpio_argv);
3073 /*get manifext.xml path*/
3074 snprintf(manifest, BUF_SIZE, "%s/opt/share/packages/%s.xml", TEMP_DIR, pkgid);
3075 _LOGD("#manifest name is %s.", manifest);
3077 if (access(manifest, F_OK)) {
3078 _LOGD("#there is no RW manifest.xml. check RO manifest.xml.");
3080 memset(manifest, '\0', sizeof(manifest));
3081 snprintf(manifest, BUF_SIZE, "%s/usr/share/packages/%s.xml", TEMP_DIR, pkgid);
3082 _LOGD("#manifest name is %s.", manifest);
3084 if (access(manifest, F_OK)) {
3085 _LOGE("@can not find manifest.xml in the pkg.");
3086 ret = RPM_INSTALLER_ERR_NO_MANIFEST;
3092 #if 0 //disable "copy ro-xml to rw-xml", because of some bug
3093 snprintf(srcpath, BUF_SIZE, "%s", manifest);
3094 memset(manifest, '\0', sizeof(manifest));
3095 snprintf(manifest, BUF_SIZE, "%s/%s.xml", MANIFEST_RW_DIRECTORY, pkgid);
3097 const char *xml_update_argv[] = { CPIO_SCRIPT_UPDATE_XML, srcpath, manifest, NULL };
3098 ret = _ri_xsystem(xml_update_argv);
3104 /*send event for install_percent*/
3105 _ri_broadcast_status_notification(pkgid, "rpm", "install_percent", "30");
3107 /*check manifest.xml validation*/
3108 ret = pkgmgr_parser_check_manifest_validation(manifest);
3110 _LOGE("@invalid manifest");
3111 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
3115 /*check for signature and certificate*/
3116 ret = _ri_verify_signatures(TEMP_DIR, pkgid);
3118 _LOGE("@signature and certificate failed(%s).", pkgid);
3119 ret = RPM_INSTALLER_ERR_SIG_INVALID;
3122 _LOGD("#_ri_verify_signatures success.");
3127 _LOGE("@chdir(%s) failed(%s).", cwd, strerror(errno));
3128 ret = RPM_INSTALLER_ERR_INTERNAL;
3132 /*remove dir for clean*/
3133 __ri_remove_updated_dir(pkgid);
3135 _LOGD("#Preserve the smack file");
3136 /*Preserve the smack rule file */
3137 ret = __ri_copy_smack_rule_file(UPGRADE_REQ,pkgid,0);
3138 if(ret != RPM_INSTALLER_SUCCESS)
3140 #ifdef APP2EXT_ENABLE
3141 ret = pkgmgrinfo_pkginfo_get_pkginfo(gpkgname, &pkghandle);
3143 _LOGE("Failed to get pkginfo handle\n");
3144 ret = RPM_INSTALLER_ERR_PKG_NOT_FOUND;
3147 ret = pkgmgrinfo_pkginfo_get_installed_storage(pkghandle, &location);
3149 _LOGE("Failed to get install location\n");
3150 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
3151 ret = RPM_INSTALLER_ERR_INTERNAL;
3154 if (location == PMINFO_EXTERNAL_STORAGE) {
3155 /*Get the rpm's size from rpm header*/
3156 rpm_size = _ri_calculate_rpm_size(pkgfilepath);
3158 rpm_size = rpm_size/(1024*1024); //rpm size in MB
3159 _LOGD("#Rpm file(%s) size is %lu MB",pkgfilepath,rpm_size);
3161 /*Add margin to the rpm size*/
3162 rpm_size = rpm_size + RPM_SIZE_MARGIN(rpm_size);
3163 _LOGD("#Rpm file (%s) size after margin is %lu MB",pkgfilepath,rpm_size);
3165 _LOGE("@Failed to get size from rpm header\n");
3166 ret = RPM_INSTALLER_ERR_INTERNAL;
3171 Get the size from the manifest file.
3173 ret = __get_size_from_xml(manifest, &size);
3174 if (ret != PMINFO_R_OK) {
3176 _LOGD(" #rpm size is %d",size);
3178 size = size>rpm_size?size:rpm_size;
3179 _LOGD("#rpm size is %d",size);
3183 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
3184 if ((location == PMINFO_EXTERNAL_STORAGE) && size > 0) {
3185 handle = app2ext_init(APP2EXT_SD_CARD);
3186 if (handle == NULL) {
3187 _LOGE("app2ext init failed\n");
3188 ret = RPM_INSTALLER_ERR_INTERNAL;
3191 if ((&(handle->interface) != NULL) && (handle->interface.pre_upgrade != NULL) && (handle->interface.post_upgrade != NULL)){
3192 dir_list = __rpm_populate_dir_list();
3193 if (dir_list == NULL) {
3194 _LOGE("\nError in populating the directory list\n");
3195 ret = RPM_INSTALLER_ERR_RPM_SCRIPT_WRONG_ARGS;
3196 app2ext_deinit(handle);
3199 ret = handle->interface.pre_upgrade(gpkgname, dir_list, size);
3200 if (ret == APP2EXT_ERROR_MMC_STATUS ) {
3201 _LOGE("app2xt MMC is not here, go internal (%d)\n", ret);
3202 }else if(ret == APP2EXT_SUCCESS){
3203 _LOGE("pre upgrade done, go internal");
3205 _LOGE("app2xt pre upgrade API failed (%d)\n", ret);
3206 __rpm_clear_dir_list(dir_list);
3207 handle->interface.post_upgrade(gpkgname, APP2EXT_STATUS_FAILED);
3208 ret = RPM_INSTALLER_ERR_INTERNAL;
3209 app2ext_deinit(handle);
3218 if (home_dir == 0) {
3219 #if 0 //disable "UPGRADE_SCRIPT_WITH_DBPATH_RO", because of some bug
3220 const char *argv[] = { UPGRADE_SCRIPT_WITH_DBPATH_RO, pkgfilepath, NULL };
3221 ret = _ri_xsystem(argv);
3223 const char *argv[] = { UPGRADE_SCRIPT, pkgfilepath, NULL };
3224 ret = _ri_xsystem(argv);
3226 const char *argv[] = { UPGRADE_SCRIPT_WITH_DBPATH_RW, pkgfilepath, NULL };
3227 ret = _ri_xsystem(argv);
3230 _LOGE("@upgrade complete with error(%d)", ret);
3231 #ifdef APP2EXT_ENABLE
3232 if ((handle != NULL) && (handle->interface.post_upgrade != NULL)){
3233 __rpm_clear_dir_list(dir_list);
3234 handle->interface.post_upgrade(gpkgname, APP2EXT_STATUS_FAILED);
3235 app2ext_deinit(handle);
3240 _LOGD("#upgrade script success.");
3242 /*send event for install_percent*/
3243 _ri_broadcast_status_notification(pkgid, "rpm", "install_percent", "60");
3245 /*Parse the manifest to get install location and size. If fails, remove manifest info from DB.*/
3246 ret = pkgmgr_parser_parse_manifest_for_upgrade(manifest, NULL);
3248 _LOGE("@parsing manifest failed.");
3249 ret = RPM_INSTALLER_ERR_INTERNAL;
3252 _LOGD("#parsing manifest success.");
3254 /*unregister cert info*/
3255 _ri_unregister_cert(pkgid);
3257 /*register cert info*/
3258 _ri_register_cert(pkgid);
3260 #ifdef APP2EXT_ENABLE
3261 if ((handle != NULL) && (handle->interface.post_upgrade != NULL)){
3262 __rpm_clear_dir_list(dir_list);
3263 handle->interface.post_upgrade(gpkgname, APP2EXT_STATUS_SUCCESS);
3264 app2ext_deinit(handle);
3269 _coretpk_installer_search_ui_gadget(pkgid);
3271 /*apply smack to shared dir*/
3272 __rpm_apply_smack(pkgid, 1);
3274 /*apply smack by privilege*/
3275 ret = _ri_apply_privilege(pkgid, 0);
3277 _LOGE("@apply perm failed with err(%d)", ret);
3279 _LOGD("#apply perm success.");
3282 ret = _ri_smack_reload_all();
3284 _LOGD("_ri_smack_reload_all failed.");
3287 /*send event for install_percent*/
3288 _ri_broadcast_status_notification(pkgid, "rpm", "install_percent", "100");
3291 _rpm_delete_dir(TEMP_DIR);
3292 _rpm_delete_dir(TEMP_DBPATH);
3294 if (ret == RPM_INSTALLER_SUCCESS) {
3295 _LOGD("[#]end : _rpm_upgrade_pkg_with_dbpath");
3296 __ri_launch_consumer(pkgid);
3297 _ri_broadcast_status_notification(pkgid, "rpm", "end", "ok");
3298 _ri_stat_cb(pkgid, "end", "ok");
3300 _LOGE("[@]end : _rpm_upgrade_pkg_with_dbpath");
3301 char *errstr = NULL;
3302 _ri_error_no_to_string(ret, &errstr);
3303 _ri_broadcast_status_notification(pkgid, "rpm", "error", errstr);
3304 _ri_stat_cb(pkgid, "error", errstr);
3305 _ri_broadcast_status_notification(pkgid, "rpm", "end", "fail");
3307 _ri_stat_cb(pkgid, "end", "fail");
3308 _LOGE("install failed with err(%d) (%s)\n", ret, errstr);
3314 int _rpm_uninstall_pkg_with_dbpath(const char *pkgid, bool is_system)
3316 if (pkgid == NULL) {
3317 _LOGE("pkgid is NULL.");
3322 char buff[BUF_SIZE] = {'\0'};
3323 char tizen_manifest[BUF_SIZE] = {'\0'};
3324 pkgmgrinfo_pkginfo_h pkghandle = NULL;
3325 bool mother_package = false;
3326 bool coretpk = false;
3328 _LOGD("pkgid=[%s], is_system=[%d]", pkgid, is_system);
3330 snprintf(tizen_manifest, BUF_SIZE, "%s/%s/tizen-manifest.xml", OPT_USR_APPS, pkgid);
3331 if (access(tizen_manifest, R_OK) == 0) {
3333 _LOGD("[%s] is existed.", tizen_manifest);
3338 _ri_broadcast_status_notification(pkgid, coretpk ? "coretpk" : "rpm", "start", "update");
3340 _ri_broadcast_status_notification(pkgid, coretpk ? "coretpk" : "rpm", "start", "uninstall");
3342 // terminate running app
3343 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, getuid(), &pkghandle);
3345 _LOGE("pkgmgrinfo_pkginfo_get_pkginfo(%s) failed.", pkgid);
3346 ret = RPM_INSTALLER_ERR_PKG_NOT_FOUND;
3349 pkgmgrinfo_appinfo_get_usr_list(pkghandle, PM_UI_APP, __ri_check_running_app, NULL, getuid());
3351 // If package is mother package, then uninstall child package
3352 /* pkgmgrinfo_pkginfo_is_mother_package(pkghandle, &mother_package);
3353 if (mother_package == true) {
3354 _LOGD("[%s] is mother package", pkgid);
3355 __uninstall_child_package_by_mother_pkgid(pkgid);
3357 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
3359 snprintf(tizen_manifest, BUF_SIZE, "%s/%s/tizen-manifest.xml", OPT_USR_APPS, pkgid);
3360 if (access(tizen_manifest, R_OK) == 0) {
3362 _LOGD("[%s] is existing.", tizen_manifest);
3365 // del root path dir
3366 snprintf(buff, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgid);
3367 if (__is_dir(buff)) {
3368 _rpm_delete_dir(buff);
3371 // del root path dir for ext
3372 if (_coretpk_installer_get_configuration_value(INI_VALUE_MAKE_EXT_DIRECTORY)) {
3373 char extpath[BUF_SIZE] = {'\0'};
3374 snprintf(extpath, BUF_SIZE, "%s/%s", OPT_STORAGE_SDCARD_APP_ROOT, pkgid);
3375 if (__is_dir(extpath)) {
3376 _rpm_delete_dir(extpath);
3380 // check system pkg, if pkg is system pkg, need to update xml on USR_SHARE_PACKAGES
3382 memset(buff, '\0', BUF_SIZE);
3383 snprintf(buff, BUF_SIZE, "%s/%s.xml", USR_SHARE_PACKAGES, pkgid);
3384 _LOGE("manifest for upgrade, path=[%s]",buff);
3385 ret = pkgmgr_parser_parse_manifest_for_upgrade(buff, NULL);
3387 _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;