Pass it's own uid when calling not 'usr' prefixed api
[platform/core/appfw/pkgmgr-info.git] / parser / pkgmgr_parser_db.c
index 4c010ab..2b36c38 100644 (file)
@@ -20,6 +20,7 @@
  *
  */
 
+#define _GNU_SOURCE
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -170,6 +171,8 @@ sqlite3 *pkgmgr_cert_db;
                                                "app_background_category INTEGER DEFAULT 0, " \
                                                "app_root_path text, " \
                                                "app_api_version text, " \
+                                               "app_effective_appid text, " \
+                                               "app_splash_screen_display text DEFAULT 'true', " \
                                                "FOREIGN KEY(package) " \
                                                "REFERENCES package_info(package) " \
                                                "ON DELETE CASCADE)"
@@ -363,6 +366,17 @@ static int __guestmode_visibility_cb(void *data, int ncols, char **coltxt, char
 static int __pkgmgr_parser_create_db(sqlite3 **db_handle, const char *db_path);
 static int __parserdb_change_perm(const char *db_file, uid_t uid);
 
+#define REGULAR_USER 5000
+static inline uid_t _getuid(void)
+{
+       uid_t uid = getuid();
+
+       if (uid < REGULAR_USER)
+               return tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
+       else
+               return uid;
+}
+
 static int __delete_subpkg_list_cb(void *data, int ncols, char **coltxt, char **colname)
 {
        if (coltxt[0])
@@ -1145,6 +1159,25 @@ static int __convert_background_category(GList *category_list)
        return ret;
 }
 
+static const char *__find_effective_appid(GList *metadata_list)
+{
+       GList *tmp_list;
+       metadata_x *md;
+
+       for (tmp_list = metadata_list; tmp_list; tmp_list = tmp_list->next) {
+               md = (metadata_x *)tmp_list->data;
+               if (md == NULL || md->key == NULL)
+                       continue;
+
+               if (strcmp(md->key, "http://tizen.org/metadata/effective-appid") == 0) {
+                       if (md->value)
+                               return md->value;
+               }
+       }
+
+       return NULL;
+}
+
 /* _PRODUCT_LAUNCHING_ENHANCED_
 *  app->indicatordisplay, app->portraitimg, app->landscapeimg, app->guestmode_appstatus
 */
@@ -1156,6 +1189,7 @@ static int __insert_application_info(manifest_x *mfx)
        int background_value = 0;
        char query[MAX_QUERY_LEN] = {'\0'};
        char *type = NULL;
+       const char *effective_appid;
 
        if (mfx->type)
                type = strdup(mfx->type);
@@ -1173,6 +1207,8 @@ static int __insert_application_info(manifest_x *mfx)
                        background_value = 0;
                }
 
+               effective_appid = __find_effective_appid(app->metadata);
+
                snprintf(query, MAX_QUERY_LEN,
                        "insert into package_app_info(" \
                        "app_id, app_component, app_exec, app_nodisplay, app_type, " \
@@ -1181,7 +1217,8 @@ static int __insert_application_info(manifest_x *mfx)
                        "app_indicatordisplay, app_portraitimg, app_landscapeimg, app_guestmodevisibility, app_permissiontype, " \
                        "app_preload, app_submode, app_submode_mainid, app_installed_storage, app_process_pool, " \
                        "app_launch_mode, app_ui_gadget, app_support_disable, component_type, package, " \
-                       "app_tep_name, app_background_category, app_package_type, app_root_path, app_api_version) " \
+                       "app_tep_name, app_background_category, app_package_type, app_root_path, app_api_version, " \
+                       "app_effective_appid, app_splash_screen_display) " \
                        "values(" \
                        "'%s', '%s', '%s', '%s', '%s', " \
                        "'%s', '%s', '%s', '%s', '%s', " \
@@ -1189,7 +1226,8 @@ static int __insert_application_info(manifest_x *mfx)
                        "'%s', '%s', '%s', '%s', '%s', " \
                        "'%s', '%s', '%s', '%s', '%s', " \
                        "'%s', '%s', '%s', '%s', '%s', " \
-                       "'%s', '%d', '%s', '%s', '%s')", \
+                       "'%s', '%d', '%s', '%s', '%s', " \
+                       "'%s', '%s')", \
                        app->appid, app->component_type, app->exec, app->nodisplay, app->type,
                        app->onboot, app->multiple, app->autorestart, app->taskmanage, app->enabled,
                        app->hwacceleration, app->screenreader, app->mainapp, __get_str(app->recentimage), app->launchcondition,
@@ -1197,7 +1235,8 @@ static int __insert_application_info(manifest_x *mfx)
                        app->guestmode_visibility, app->permission_type,
                        mfx->preload, app->submode, __get_str(app->submode_mainid), mfx->installed_storage, app->process_pool,
                        app->launch_mode, app->ui_gadget, mfx->support_disable, app->component_type, mfx->package,
-                       __get_str(mfx->tep_name), background_value, type, mfx->root_path, __get_str(mfx->api_version));
+                       __get_str(mfx->tep_name), background_value, type, mfx->root_path, __get_str(mfx->api_version),
+                       __get_str(effective_appid), app->splash_screen_display);
 
                ret = __exec_query(query);
                if (ret == -1) {
@@ -1655,7 +1694,8 @@ static int __insert_application_legacy_splashscreen_info(manifest_x *mfx)
                                return -1;
                        }
                        memset(query, '\0', MAX_QUERY_LEN);
-               } else if (app->landscapeimg) {
+               }
+               if (app->landscapeimg) {
                        orientation = "landscape";
                        snprintf(query, sizeof(query),
                                        "insert into package_app_splash_screen" \
@@ -1674,6 +1714,115 @@ static int __insert_application_legacy_splashscreen_info(manifest_x *mfx)
        return 0;
 }
 
+static int __insert_application_metadata_splashscreen_info(manifest_x *mfx)
+{
+       GList *app_tmp;
+       application_x *app;
+       GList *md_tmp;
+       metadata_x *md;
+       int ret;
+       char query[MAX_QUERY_LEN];
+       char *token;
+       char *tmpptr = NULL;
+       const char *operation;
+       const char *portraitimg;
+       const char *landscapeimg;
+       const char *indicatordisplay;
+       const char *orientation;
+       const char *image_type;
+
+       for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
+               app = (application_x *)app_tmp->data;
+               if (app == NULL)
+                       continue;
+
+               for (md_tmp = app->metadata; md_tmp; md_tmp = md_tmp->next) {
+                       md = (metadata_x *)md_tmp->data;
+                       if (md == NULL || md->key == NULL || md->value == NULL)
+                               continue;
+
+                       if (strcasestr(md->key, "operation_effect=")) {
+                               operation = index(md->key, '=');
+                               if ((operation + 1) != NULL)
+                                       operation++;
+                               else
+                                       operation = NULL;
+                       } else if (strcasestr(md->key, "launch_effect")) {
+                               operation = NULL;
+                       } else {
+                               continue;
+                       }
+
+                       portraitimg = NULL;
+                       landscapeimg = NULL;
+                       indicatordisplay = "true"; /* default */
+                       token = strtok_r(md->value, "|", &tmpptr);
+                       while (token != NULL) {
+                               if (strcasestr(token, "portrait-effectimage=")) {
+                                       portraitimg = index(token, '=');
+                                       if ((portraitimg + 1) != NULL)
+                                               portraitimg++;
+                                       else
+                                               portraitimg = NULL;
+                               } else if (strcasestr(token, "landscape-effectimage=")) {
+                                       landscapeimg = index(token, '=');
+                                       if ((landscapeimg + 1) != NULL)
+                                               landscapeimg++;
+                                       else
+                                               landscapeimg = NULL;
+                               } else if (strcasestr(token, "indicatordisplay=")) {
+                                       indicatordisplay = index(token, '=');
+                                       if ((indicatordisplay + 1) != NULL)
+                                               indicatordisplay++;
+                                       else
+                                               indicatordisplay = "true";
+                               }
+
+                               token = strtok_r(NULL, "|", &tmpptr);
+                       }
+
+                       if (portraitimg) {
+                               orientation = "portrait";
+                               image_type = "img";
+                               if (strcasestr(portraitimg, "edj"))
+                                       image_type = "edj";
+                               snprintf(query, sizeof(query),
+                                       "insert into package_app_splash_screen" \
+                                       "(app_id, src, type, orientation, indicatordisplay, operation) " \
+                                       "values('%s', '%s', '%s', '%s', '%s', '%s')",
+                                       app->appid, portraitimg, image_type,
+                                       orientation, indicatordisplay, __get_str(operation));
+                               ret = __exec_query(query);
+                               if (ret == -1) {
+                                       _LOGD("Package UiApp Splash Screen DB Insert Failed");
+                                       return -1;
+                               }
+                               memset(query, '\0', MAX_QUERY_LEN);
+                       }
+                       if (landscapeimg) {
+                               orientation = "landscape";
+                               image_type = "img";
+                               if (strcasestr(landscapeimg, "edj"))
+                                       image_type = "edj";
+                               snprintf(query, sizeof(query),
+                                       "insert into package_app_splash_screen" \
+                                       "(app_id, src, type, orientation, indicatordisplay, operation) " \
+                                       "values('%s', '%s', '%s', '%s', '%s', '%s')",
+                                       app->appid, landscapeimg, image_type,
+                                       orientation, indicatordisplay, __get_str(operation));
+                               ret = __exec_query(query);
+                               if (ret == -1) {
+                                       _LOGD("Package UiApp Splash Screen DB Insert Failed");
+                                       return -1;
+                               }
+                               memset(query, '\0', MAX_QUERY_LEN);
+                       }
+               }
+       }
+
+       return 0;
+}
+
 static int __insert_manifest_info_in_db(manifest_x *mfx, uid_t uid)
 {
        GList *tmp;
@@ -1845,11 +1994,16 @@ static int __insert_manifest_info_in_db(manifest_x *mfx, uid_t uid)
        if (ret == -1)
                return -1;
 
-       /*Insert in the package_app_splash_screen DB*/
+       /*Insert in the package_app_splash_screen DB (backward compatibility)*/
        ret = __insert_application_legacy_splashscreen_info(mfx);
        if (ret == -1)
                return -1;
 
+       /*Insert in the package_app_splash_screen DB (backward compatibility)*/
+       ret = __insert_application_metadata_splashscreen_info(mfx);
+       if (ret == -1)
+               return -1;
+
        /*Insert in the package_app_splash_screen DB*/
        ret = __insert_application_splashscreen_info(mfx);
        if (ret == -1)
@@ -2238,10 +2392,11 @@ API int pkgmgr_parser_initialize_db(uid_t uid)
 static int __parserdb_change_perm(const char *db_file, uid_t uid)
 {
        char buf[BUFSIZE];
+       char pwuid_buf[1024];
        char journal_file[BUFSIZE];
        char *files[3];
        int ret, i;
-       struct passwd *userinfo = NULL;
+       struct passwd userinfo, *result = NULL;
        files[0] = (char *)db_file;
        files[1] = journal_file;
        files[2] = NULL;
@@ -2255,20 +2410,20 @@ static int __parserdb_change_perm(const char *db_file, uid_t uid)
        snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal");
        if (uid == OWNER_ROOT)
                uid = GLOBAL_USER;
-       userinfo = getpwuid(uid);
-       if (!userinfo) {
+       ret = getpwuid_r(uid, &userinfo, pwuid_buf, sizeof(pwuid_buf), &result);
+       if (ret != 0 || result == NULL) {
                _LOGE("FAIL: user %d doesn't exist", uid);
                return -1;
        }
        snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal");
 
        for (i = 0; files[i]; i++) {
-               ret = chown(files[i], uid, userinfo->pw_gid);
+               ret = chown(files[i], uid, userinfo.pw_gid);
                if (ret == -1) {
                        if (strerror_r(errno, buf, sizeof(buf)))
                                strncpy(buf, "", BUFSIZE - 1);
                        _LOGD("FAIL : chown %s %d.%d : %s", files[i], uid,
-                                       userinfo->pw_gid, buf);
+                                       userinfo.pw_gid, buf);
                        return -1;
                }
 
@@ -2435,7 +2590,7 @@ err:
 
 API int pkgmgr_parser_update_tep_info_in_db(const char *pkgid, const char *tep_path)
 {
-       return pkgmgr_parser_update_tep_info_in_usr_db(pkgid, tep_path, GLOBAL_USER);
+       return pkgmgr_parser_update_tep_info_in_usr_db(pkgid, tep_path, _getuid());
 }
 
 API int pkgmgr_parser_update_tep_info_in_usr_db(const char *pkgid, const char *tep_path, uid_t uid)
@@ -2567,7 +2722,7 @@ err:
 
 API int pkgmgr_parser_update_manifest_info_in_db(manifest_x *mfx)
 {
-       return pkgmgr_parser_update_manifest_info_in_usr_db(mfx, GLOBAL_USER);
+       return pkgmgr_parser_update_manifest_info_in_usr_db(mfx, _getuid());
 }
 
 API int pkgmgr_parser_delete_manifest_info_from_usr_db(manifest_x *mfx, uid_t uid)
@@ -2612,7 +2767,7 @@ err:
 
 API int pkgmgr_parser_delete_manifest_info_from_db(manifest_x *mfx)
 {
-       return pkgmgr_parser_delete_manifest_info_from_usr_db(mfx, GLOBAL_USER);
+       return pkgmgr_parser_delete_manifest_info_from_usr_db(mfx, _getuid());
 }
 
 API int pkgmgr_parser_update_preload_info_in_db()
@@ -2731,7 +2886,7 @@ err:
 
 API int pkgmgr_parser_update_app_disable_info_in_db(const char *appid, int is_disable)
 {
-       return pkgmgr_parser_update_app_disable_info_in_usr_db(appid, GLOBAL_USER, is_disable);
+       return pkgmgr_parser_update_app_disable_info_in_usr_db(appid, _getuid(), is_disable);
 }
 
 API int pkgmgr_parser_update_app_disable_info_in_usr_db(const char *appid, uid_t uid, int is_disable)