2 * Copyright (c) 2000 - 2017 Samsung Electronics Co., Ltd. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include <sys/types.h>
24 #include <sys/smack.h>
25 #include <sys/sysmacros.h>
26 #include <linux/limits.h>
33 #include <tzplatform_config.h>
34 #include <system_info.h>
36 #include "pkgmgr-info.h"
37 #include "pkgmgrinfo_basic.h"
38 #include "pkgmgr_parser.h"
39 #include "pkgmgr_parser_debug.h"
40 #include "pkgmgr_parser_internal.h"
41 #include "manager/pkginfo_manager.h"
42 #include "pkgmgr_query_index.h"
48 #define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)
51 #define APPFW_USER "app_fw"
60 #define XXHDPI "xxhdpi"
70 #define XXHDPI_MIN 481
71 #define XXHDPI_MAX 600
73 #define MAX_UID_LENGTH 64
75 /* app background category value */
76 #define APP_BG_CATEGORY_USER_DISABLE_FALSE_VAL 0x00000
77 #define APP_BG_CATEGORY_USER_DISABLE_TRUE_VAL 0x00001
78 #define APP_BG_CATEGORY_MEDIA_VAL 0x00002
79 #define APP_BG_CATEGORY_DOWNLOAD_VAL 0x00004
80 #define APP_BG_CATEGORY_BGNETWORK_VAL 0x00008
81 #define APP_BG_CATEGORY_LOCATION_VAL 0x00010
82 #define APP_BG_CATEGORY_SENSOR_VAL 0x00020
83 #define APP_BG_CATEGORY_IOTCOMM_VAL 0x00040
84 #define APP_BG_CATEGORY_SYSTEM_VAL 0x00080
86 #define APP_BG_CATEGORY_USER_DISABLE_FALSE_STR "enable"
87 #define APP_BG_CATEGORY_USER_DISABLE_TRUE_STR "disable"
88 #define APP_BG_CATEGORY_MEDIA_STR "media"
89 #define APP_BG_CATEGORY_DOWNLOAD_STR "download"
90 #define APP_BG_CATEGORY_BGNETWORK_STR "background-network"
91 #define APP_BG_CATEGORY_LOCATION_STR "location"
92 #define APP_BG_CATEGORY_SENSOR_STR "sensor"
93 #define APP_BG_CATEGORY_IOTCOMM_STR "iot-communication"
94 #define APP_BG_CATEGORY_SYSTEM "system"
96 #define REGULAR_USER 5000
97 static inline uid_t __getuid(void)
101 if (uid < REGULAR_USER)
102 return tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
107 API int pkgmgr_parser_create_and_initialize_db(uid_t uid)
111 struct passwd *result;
114 ret = getpwnam_r(APPFW_USER, &pwd, buf, sizeof(buf), &result);
115 if (result == NULL) {
117 _LOGE("no such user: %s", APPFW_USER);
119 _LOGE("getpwnam_r failed: %d", errno);
120 return PM_PARSER_R_ERROR;
123 if (getuid() != OWNER_ROOT && getuid() != pwd.pw_uid) {
124 _LOGE("Only root or app_fw user is allowed");
125 return PM_PARSER_R_EINVAL;
128 if (_parser_create_and_initialize_db(uid))
129 return PM_PARSER_R_ERROR;
131 return PM_PARSER_R_OK;
134 /* TODO: move to installer */
136 /* TODO: refactor inserting localized info */
138 static int __insert_package_plugin_execution_info(
139 manifest_x *mfx, uid_t uid)
146 int ret = PM_PARSER_R_ERROR;
149 return PM_PARSER_R_OK;
151 int len = g_list_length(mfx->plugin);
153 args = calloc(len, sizeof(char **));
155 _LOGE("Out of memory");
156 return PM_PARSER_R_ERROR;
159 for (i = 0; i < len; i++) {
160 args[i] = calloc(4, sizeof(char *));
161 if (args[i] == NULL) {
162 _LOGE("Out of memory");
167 for (tmp = mfx->plugin; tmp; tmp = tmp->next) {
168 plugin = (plugin_x *)tmp->data;
172 args[idx][0] = plugin->pkgid;
173 args[idx][1] = plugin->appid;
174 args[idx][2] = plugin->plugin_type;
175 args[idx][3] = plugin->plugin_name;
179 if (_parser_execute_write_queries(QUERY_INDEX_INSERT_PACKAGE_PLUGIN_EXECUTION_INFO,
180 args, 4, idx, uid) < 0) {
181 _LOGE("Fail to write to db");
183 return PM_PARSER_R_ERROR;
186 ret = PM_PARSER_R_OK;
188 for (i = 0; i < len; i++)
196 static int __delete_package_plugin_execution_info(const char *pkgid, uid_t uid)
198 const char *args[1] = { pkgid };
200 if (_parser_execute_write_query(QUERY_INDEX_DELETE_PACKAGE_PLUGIN_EXECUTION_INFO,
202 _LOGE("Fail to write to db");
203 return PM_PARSER_R_ERROR;
206 return PM_PARSER_R_OK;
209 API int pkgmgr_parser_insert_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid)
211 if (_parser_insert_manifest_info(mfx, uid) < 0) {
212 _LOGE("Fail to insert manifest uid[%d]", uid);
213 return PM_PARSER_R_ERROR;
216 return PM_PARSER_R_OK;
219 API int pkgmgr_parser_insert_manifest_info_in_db(manifest_x *mfx)
221 return pkgmgr_parser_insert_manifest_info_in_usr_db(mfx, __getuid());
224 API int pkgmgr_parser_delete_manifest_info_from_usr_db(manifest_x *mfx,
227 if (_parser_delete_manifest_info(mfx, uid) < 0) {
228 _LOGE("Fail to update manifest uid[%d]", uid);
229 return PM_PARSER_R_ERROR;
232 return PM_PARSER_R_OK;
235 API int pkgmgr_parser_delete_manifest_info_from_db(manifest_x *mfx)
237 return pkgmgr_parser_delete_manifest_info_from_usr_db(mfx, __getuid());
240 API int pkgmgr_parser_update_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid)
242 if (_parser_update_manifest_info(mfx, uid) < 0) {
243 _LOGE("Fail to update manifest uid[%d]", uid);
244 return PM_PARSER_R_ERROR;
247 return PM_PARSER_R_OK;
250 API int pkgmgr_parser_update_manifest_info_in_db(manifest_x *mfx)
252 return pkgmgr_parser_update_manifest_info_in_usr_db(mfx, __getuid());
255 API int pkgmgr_parser_update_global_app_disable_for_uid_info_in_db(
256 const char *appid, uid_t uid, int is_disable)
258 char uid_str[MAX_UID_LENGTH] = { 0, };
262 r = snprintf(uid_str, sizeof(uid_str), "%d", uid);
263 if (r < 0 || r >= sizeof(uid_str))
264 return PM_PARSER_R_ERROR;
267 args[2] = is_disable ? "true" : "false";
270 if (_parser_execute_write_query(QUERY_INDEX_UPDATE_GLOBAL_APP_DISABLE,
271 args, 4, GLOBAL_USER) < 0) {
272 _LOGE("Fail to write to db");
273 return PM_PARSER_R_ERROR;
276 return PM_PARSER_R_OK;
279 API int pkgmgr_parser_update_app_disable_info_in_usr_db(const char *appid,
280 uid_t uid, int is_disable)
283 args[0] = is_disable ? "true" : "false";
286 if (_parser_execute_write_query(QUERY_INDEX_UPDATE_APP_DISABLE_INFO,
288 _LOGE("Fail to write to db");
289 return PM_PARSER_R_ERROR;
292 return PM_PARSER_R_OK;
295 API int pkgmgr_parser_update_app_disable_info_in_db(const char *appid,
298 return pkgmgr_parser_update_app_disable_info_in_usr_db(appid,
299 __getuid(), is_disable);
302 API int pkgmgr_parser_update_pkg_disable_info_in_usr_db(const char *pkgid,
303 uid_t uid, int is_disable)
306 args[0] = is_disable ? "true" : "false";
309 if (_parser_execute_write_query(QUERY_INDEX_UPDATE_PKG_DISABLE_INFO,
311 _LOGE("Fail to write to db");
312 return PM_PARSER_R_ERROR;
315 return PM_PARSER_R_OK;
318 API int pkgmgr_parser_update_pkg_disable_info_in_db(const char *pkgid,
321 return pkgmgr_parser_update_pkg_disable_info_in_usr_db(pkgid,
322 __getuid(), is_disable);
325 API int pkgmgr_parser_update_global_app_splash_screen_display_info_in_usr_db(
326 const char *appid, uid_t uid, int flag)
329 char uid_str[MAX_UID_LENGTH] = { 0, };
332 r = snprintf(uid_str, sizeof(uid_str), "%d", uid);
333 if (r < 0 || r >= sizeof(uid_str))
334 return PM_PARSER_R_ERROR;
337 args[2] = (flag == 1 ? "true" : "false");
339 if (_parser_execute_write_query(
340 QUERY_INDEX_UPDATE_GLOBAL_APP_SPLASH_SCREEN_DISPLAY_INFO,
342 _LOGE("Fail to write to db");
343 return PM_PARSER_R_ERROR;
346 return PM_PARSER_R_OK;
349 API int pkgmgr_parser_update_app_splash_screen_display_info_in_usr_db(
350 const char *appid, uid_t uid, int flag)
354 args[0] = (flag == 1 ? "true" : "false");
357 if (_parser_execute_write_query(
358 QUERY_INDEX_UPDATE_APP_SPLASH_SCREEN_DISPLAY_INFO,
360 _LOGE("Fail to write to db");
361 return PM_PARSER_R_ERROR;
364 return PM_PARSER_R_OK;
367 API int pkgmgr_parser_update_app_splash_screen_display_info_in_db(
368 const char *appid, int flag)
370 return pkgmgr_parser_update_app_splash_screen_display_info_in_usr_db(
371 appid, __getuid(), flag);
374 API int pkgmgr_parser_update_app_label_info_in_usr_db(const char *appid,
375 uid_t uid, const char *label)
377 const char *args[2] = { label, appid };
379 if (_parser_execute_write_query(QUERY_INDEX_UPDATE_APP_LABEL_INFO,
381 _LOGE("Fail to write to db");
382 return PM_PARSER_R_ERROR;
385 return PM_PARSER_R_OK;
388 API int pkgmgr_parser_update_app_label_info_in_db(const char *appid,
391 return pkgmgr_parser_update_app_label_info_in_usr_db(appid, __getuid(),
395 API int pkgmgr_parser_update_app_icon_info_in_usr_db(const char *appid,
396 uid_t uid, const char *icon_path)
398 const char *args[2] = { icon_path, appid };
400 if (_parser_execute_write_query(QUERY_INDEX_UPDATE_APP_ICON_INFO,
402 _LOGE("Fail to write to db");
403 return PM_PARSER_R_ERROR;
406 return PM_PARSER_R_OK;
409 API int pkgmgr_parser_update_app_icon_info_in_db(const char *appid,
410 const char *icon_path)
412 return pkgmgr_parser_update_app_icon_info_in_usr_db(appid, __getuid(),
416 API int pkgmgr_parser_update_tep_info_in_usr_db(const char *pkgid,
417 const char *tep_path, uid_t uid)
419 const char *args[2] = { tep_path, pkgid };
421 if (_parser_execute_write_query(QUERY_INDEX_UPDATE_TEP_INFO,
423 _LOGE("Fail to write to db");
424 return PM_PARSER_R_ERROR;
427 return PM_PARSER_R_OK;
430 API int pkgmgr_parser_update_tep_info_in_db(const char *pkgid,
431 const char *tep_path)
433 return pkgmgr_parser_update_tep_info_in_usr_db(pkgid, tep_path,
437 static int __convert_update_type(pkgmgrinfo_updateinfo_update_type type,
438 const char **update_type)
440 if (type == PMINFO_UPDATEINFO_NONE)
441 *update_type = PMINFO_UPDATEINFO_TYPE_NONE;
442 else if (type == PMINFO_UPDATEINFO_FORCE)
443 *update_type = PMINFO_UPDATEINFO_TYPE_FORCE;
444 else if (type == PMINFO_UPDATEINFO_OPTIONAL)
445 *update_type = PMINFO_UPDATEINFO_TYPE_OPTIONAL;
451 static int __register_pkg_update_info(updateinfo_x *info,
452 const char *update_type, uid_t uid)
454 const char *args[3] = { info->version, update_type, info->pkgid };
456 if (_parser_execute_write_query(QUERY_INDEX_REGISTER_PKG_UPDATE_INFO,
458 _LOGE("Fail to write to db");
459 return PM_PARSER_R_ERROR;
462 return PM_PARSER_R_OK;
465 API int pkgmgr_parser_register_pkg_update_info_in_usr_db(
466 pkgmgrinfo_updateinfo_h handle, uid_t uid)
469 updateinfo_x *update_info;
470 updateinfo_x *prev_update_info;
471 pkgmgrinfo_updateinfo_h prev_update_handle;
472 pkgmgrinfo_pkginfo_h pkginfo;
473 pkgmgrinfo_version_compare_type compare_result;
475 const char *update_type;
477 if (handle == NULL) {
478 _LOGE("invalid parameter");
479 return PM_PARSER_R_EINVAL;
482 update_info = (updateinfo_x *)handle;
483 if (update_info->pkgid == NULL || update_info->version == NULL)
484 return PM_PARSER_R_EINVAL;
485 if (__convert_update_type(update_info->type, &update_type) != 0)
486 return PM_PARSER_R_EINVAL;
488 ret = pkgmgrinfo_updateinfo_get_usr_updateinfo(update_info->pkgid,
489 &prev_update_handle, uid);
490 if (ret != PMINFO_R_OK)
491 return PM_PARSER_R_ERROR;
493 prev_update_info = (updateinfo_x *)prev_update_handle;
494 ret = pkgmgrinfo_compare_package_version(update_info->version,
495 prev_update_info->version, &compare_result);
496 if (ret != PMINFO_R_OK) {
497 pkgmgrinfo_updateinfo_destroy(prev_update_handle);
498 return PM_PARSER_R_ERROR;
501 if (compare_result == PMINFO_VERSION_SAME &&
502 prev_update_info->type == PMINFO_UPDATEINFO_NONE) {
503 _LOGI("Given update info version[%s] of pkgid[%s] "
505 update_info->version, update_info->pkgid);
506 pkgmgrinfo_updateinfo_destroy(prev_update_handle);
507 return PM_PARSER_R_OK;
509 pkgmgrinfo_updateinfo_destroy(prev_update_handle);
511 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(update_info->pkgid, uid,
513 if (ret != PMINFO_R_OK)
514 return PM_PARSER_R_ERROR;
516 ret = pkgmgrinfo_pkginfo_is_global(pkginfo, &is_global_pkg);
517 if (ret != PMINFO_R_OK) {
518 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
519 return PM_PARSER_R_ERROR;
521 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
523 return __register_pkg_update_info(update_info, update_type, uid);
526 API int pkgmgr_parser_register_pkg_update_info_in_db(
527 pkgmgrinfo_updateinfo_h handle)
529 return pkgmgr_parser_register_pkg_update_info_in_usr_db(handle,
533 static int __unregister_pkg_update_info(const char *pkgid, uid_t uid)
535 const char *args[1] = { pkgid };
537 if (_parser_execute_write_query(QUERY_INDEX_UNREGISTER_PKG_UPDATE_INFO,
539 _LOGE("Fail to write to db");
540 return PM_PARSER_R_ERROR;
543 return PM_PARSER_R_OK;
546 API int pkgmgr_parser_unregister_pkg_update_info_in_usr_db(const char *pkgid,
550 pkgmgrinfo_pkginfo_h pkginfo;
555 _LOGE("invalid parameter");
556 return PM_PARSER_R_EINVAL;
559 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &pkginfo);
560 if (ret != PMINFO_R_OK)
561 return PM_PARSER_R_EINVAL;
563 ret = pkgmgrinfo_pkginfo_is_global(pkginfo, &is_global_pkg);
564 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
565 if (ret != PMINFO_R_OK)
566 return PM_PARSER_R_ERROR;
568 target_uid = is_global_pkg ? GLOBAL_USER : uid;
570 // TODO: what we do with is_global_pkg?
571 return __unregister_pkg_update_info(pkgid, target_uid);
574 API int pkgmgr_parser_unregister_pkg_update_info_in_db(const char *pkgid)
576 return pkgmgr_parser_unregister_pkg_update_info_in_usr_db(pkgid,
580 API int pkgmgr_parser_unregister_all_pkg_update_info_in_usr_db(uid_t uid)
582 if (_parser_execute_write_query(QUERY_INDEX_UNREGISTER_ALL_PKG_UPDATE_INFO,
584 _LOGE("Fail to write to db");
585 return PM_PARSER_R_ERROR;
588 return PM_PARSER_R_OK;
591 API int pkgmgr_parser_unregister_all_pkg_update_info_in_db(void)
593 return pkgmgr_parser_unregister_all_pkg_update_info_in_usr_db(
597 API int pkgmgr_parser_register_pkg_plugin_info_in_usr_db(
598 manifest_x *mfx, uid_t uid)
600 return __insert_package_plugin_execution_info(mfx, uid);
603 API int pkgmgr_parser_register_pkg_plugin_info_in_db(manifest_x *mfx)
605 return pkgmgr_parser_register_pkg_plugin_info_in_usr_db(mfx, __getuid());
608 API int pkgmgr_parser_update_pkg_plugin_info_in_usr_db(
609 manifest_x *mfx, uid_t uid)
611 // TODO: construct query and send to server
614 ret = __delete_package_plugin_execution_info(mfx->package, uid);
615 if (ret != PM_PARSER_R_OK)
617 ret = __insert_package_plugin_execution_info(mfx, uid);
618 if (ret != PM_PARSER_R_OK)
621 return PM_PARSER_R_OK;
624 API int pkgmgr_parser_update_pkg_plugin_info_in_db(manifest_x *mfx)
626 return pkgmgr_parser_update_pkg_plugin_info_in_usr_db(mfx, __getuid());
629 API int pkgmgr_parser_unregister_pkg_plugin_info_in_usr_db(
630 const char *pkgid, uid_t uid)
632 return __delete_package_plugin_execution_info(pkgid, uid);
635 API int pkgmgr_parser_unregister_pkg_plugin_info_in_db(const char *pkgid)
637 return pkgmgr_parser_unregister_pkg_plugin_info_in_usr_db(pkgid, __getuid());
640 API int pkgmgr_parser_clear_cache_usr_memory_db(uid_t uid)
642 return _parser_clear_cache_memory_db(uid);
645 API int pkgmgr_parser_clear_cache_memory_db(void)
647 return pkgmgr_parser_clear_cache_usr_memory_db(__getuid());
650 API int pkgmgr_parser_update_pending_cache(const char *pkgid)
652 return _parser_update_pending_cache(pkgid);