From: Youngjae Cho Date: Wed, 23 Jun 2021 08:39:15 +0000 (+0900) Subject: Support database search for HAL system-info X-Git-Tag: submit/tizen/20210628.062452~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=bcb32a6548cf467f968ddf3426f5469782b1f363;p=platform%2Fcore%2Fapi%2Fsystem-info.git Support database search for HAL system-info In addition to platform system-info database(/etc/system_info_db), system-info of HAL(/hal/system_info_db) can be searched for a requested key. If a key exists only in one database, use it. If a key exists in both of the databases, - if value of key is type of bool, the two value are ANDed. - if value of key is type of other than bool, the value of HAL is used. Change-Id: If36a4b7a4143107ec550bb1311dafb470f492167 Signed-off-by: Youngjae Cho --- diff --git a/CMakeLists.txt b/CMakeLists.txt index a336011..dd5466e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,6 +36,7 @@ ADD_DEFINITIONS("-DTIZEN_ID_PATH=\"${TIZEN_ID_PATH}\"") ADD_DEFINITIONS("-DLIBPATH=\"${LIB_INSTALL_DIR}\"") ADD_DEFINITIONS("-DSYSTEM_INFO_DB_RO_PATH=\"${DB_RO_PATH}\"") ADD_DEFINITIONS("-DSYSTEM_INFO_DB_RW_PATH=\"${DB_RW_PATH}\"") +ADD_DEFINITIONS("-DSYSTEM_INFO_HAL_DB_RO_PATH=\"${HAL_DB_RO_PATH}\"") ADD_DEFINITIONS("-DPLUGIN_DIR=\"${PLUGIN_DIR}\"") ADD_DEFINITIONS(-D_GNU_SOURCE) # to make scandirat() available diff --git a/packaging/capi-system-info.spec b/packaging/capi-system-info.spec index 4b08b80..9729006 100644 --- a/packaging/capi-system-info.spec +++ b/packaging/capi-system-info.spec @@ -52,6 +52,7 @@ cp %{SOURCE1001} . %define model_config_ro_path %{_sysconfdir}/config/model-config.xml %define db_ro_path %{TZ_SYS_RO_ETC}/system_info_db +%define hal_db_ro_path %{_hal_prefix}/system_info_db %define model_config_rw_dir /opt/system/model-config %define db_rw_path %{model_config_rw_dir}/system_info_db @@ -67,8 +68,9 @@ MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'` -DTIZEN_ID_PATH=%{tizen_id_path} \ -DDB_RO_PATH=%{db_ro_path} \ -DDB_RW_PATH=%{db_rw_path} \ + -DHAL_DB_RO_PATH=%{hal_db_ro_path} \ -DPLUGIN_DIR=%{plugin_dir} \ - -DGCOV=%{?gcov:1}%{!?gcov:0} + -DGCOV=%{?gcov:1}%{!?gcov:0} %__make %{?_smp_mflags} diff --git a/src/system_info.c b/src/system_info.c index 78b3a50..5309c54 100644 --- a/src/system_info.c +++ b/src/system_info.c @@ -74,14 +74,73 @@ static void destroy_key_value(gpointer data) } //LCOV_EXCL_STOP +static int db_join_value(const char *value_platform, const char *value_hal, const char *type, char *out, int len) +{ + if (!value_platform || !value_hal || !type) + return SYSTEM_INFO_ERROR_INVALID_PARAMETER; + + /* If the type is other than bool type, then + * the value_hal takes precedence over the value_platform */ + if (strncmp(type, BOOL_TYPE, sizeof(BOOL_TYPE)) != 0) { + strncpy(out, value_hal, len); + return SYSTEM_INFO_ERROR_NONE; + } + + /* If the type is bool type, then + * the result is AND of those two value */ + if (value_platform[0] == 'F') + strncpy(out, value_platform, len); + else if (value_hal[0] == 'F') + strncpy(out, value_hal, len); + else + strncpy(out, value_platform, len); + + return SYSTEM_INFO_ERROR_NONE; +} + +static int db_search_value(const char *db_path, char *key_internal, char *value, int len) +{ + FILE *fp = NULL; + int key_internal_len; + char buf[PATH_MAX]; // buffer size should be larger than KEY_MAX + size_t value_internal_len; + char *temp; + + if (!db_path || !key_internal || !value) + return SYSTEM_INFO_ERROR_INVALID_PARAMETER; + + snprintf(buf, sizeof(buf), "%s/%lu", db_path, simple_hash(key_internal)); + + fp = fopen(buf, "r"); + if (!fp) { + _E("fopen for %s failed (%d)", buf, errno); //LCOV_EXCL_LINE + return SYSTEM_INFO_ERROR_IO_ERROR; + } + + key_internal_len = strlen(key_internal); + while ((temp = fgets(buf, sizeof(buf), fp))) { + if (!strncmp(buf, key_internal, key_internal_len) && buf[key_internal_len] == ' ') { + value_internal_len = strcspn(buf + key_internal_len + 1, "\n") + 1; + snprintf(value, len < value_internal_len ? len : value_internal_len, + "%s", buf + key_internal_len + 1); + break; + } + } + fclose(fp); + + if (!temp) { + _D("Failed to find key=%s in DB=%s", key_internal, db_path); + return SYSTEM_INFO_ERROR_IO_ERROR; + } + + _D("key=%s in DB=%s, found value=%s", key_internal, db_path, value); + return SYSTEM_INFO_ERROR_NONE; +} + static int db_get_value(enum tag_type tag, const char *key, const char *type, char *value, size_t len) { char key_internal[KEY_MAX]; - size_t key_internal_len; - char buf[PATH_MAX]; // buffer size should be larger than KEY_MAX - size_t value_internal_len; - FILE *fp = NULL; int ret; char *tag_s; char *temp; @@ -117,44 +176,47 @@ static int db_get_value(enum tag_type tag, const char *key, } } - if (access(SYSTEM_INFO_DB_RW_PATH, R_OK) == 0) - snprintf(buf, sizeof(buf), SYSTEM_INFO_DB_RW_PATH"/%lu", simple_hash(key_internal)); - else - snprintf(buf, sizeof(buf), SYSTEM_INFO_DB_RO_PATH"/%lu", simple_hash(key_internal)); - - fp = fopen(buf, "r"); - if (!fp) { - if (errno == ENOENT) - _D("Failed to find key in DB (%s, %s)", key, type); - else - _E("fopen for %s failed (%d)", buf, errno); //LCOV_EXCL_LINE - ret = SYSTEM_INFO_ERROR_IO_ERROR; //LCOV_EXCL_LINE - goto out; - } + if (access(SYSTEM_INFO_DB_RW_PATH, R_OK) == 0) { + ret = db_search_value(SYSTEM_INFO_DB_RW_PATH, key_internal, value, len); + } else { + int retval1; + int retval2; + char *buffer1; + char *buffer2; + + buffer1 = (char *) calloc(len, sizeof(char)); + buffer2 = (char *) calloc(len, sizeof(char)); + if (!buffer1 || !buffer2) { + free(buffer1); + free(buffer2); + pthread_mutex_unlock(&fmutex); + return SYSTEM_INFO_ERROR_OUT_OF_MEMORY; + } - key_internal_len = strlen(key_internal); - while ((temp = fgets(buf, sizeof(buf), fp))) { - if (!strncmp(buf, key_internal, key_internal_len) && buf[key_internal_len] == ' ') { - value_internal_len = strcspn(buf + key_internal_len + 1, "\n") + 1; - snprintf(value, len < value_internal_len ? len : value_internal_len, - "%s", buf + key_internal_len + 1); - break; + retval1 = db_search_value(SYSTEM_INFO_DB_RO_PATH, key_internal, buffer1, len); + retval2 = db_search_value(SYSTEM_INFO_HAL_DB_RO_PATH, key_internal, buffer2, len); + + if (retval1 == SYSTEM_INFO_ERROR_NONE && retval2 == SYSTEM_INFO_ERROR_NONE) { + ret = db_join_value(buffer1, buffer2, type, value, len); + } else if (retval1 == SYSTEM_INFO_ERROR_NONE) { + ret = SYSTEM_INFO_ERROR_NONE; + strncpy(value, buffer1, len); + } else if (retval2 == SYSTEM_INFO_ERROR_NONE) { + ret = SYSTEM_INFO_ERROR_NONE; + strncpy(value, buffer2, len); + } else { + ret = SYSTEM_INFO_ERROR_IO_ERROR; } - } - if (!temp) { - _D("Failed to find key in DB (%s)", key_internal); - ret = SYSTEM_INFO_ERROR_IO_ERROR; - goto out; + free(buffer1); + free(buffer2); } - ret = SYSTEM_INFO_ERROR_NONE; + if (ret == SYSTEM_INFO_ERROR_NONE) + g_hash_table_insert(hashtable, strdup(key_internal), strdup(value)); - g_hash_table_insert(hashtable, strdup(key_internal), strdup(value)); -out: - if (fp) - fclose(fp); pthread_mutex_unlock(&fmutex); + return ret; }