diff --git a/include/info/linux/device_api_version.h b/include/info/linux/device_api_version.h index d08dbc87d9dd4d2a8b780c58554149f1fa5eae27..5fc4bfeb10c66fe98418729045dfcf7ca343136e 100644 --- a/include/info/linux/device_api_version.h +++ b/include/info/linux/device_api_version.h @@ -21,8 +21,15 @@ extern "C" { #endif /** - * @brief Get the api version number of the device. - * @return The api version number of the device. + * @brief Init the api version number of the device. + * @return void. + */ +void InitDeviceApiVersion(void); + +/** + * @brief Get the api version number of the device, the value is equal to const.ohos.apiversion value. + * If get api version value not equal API_VERSION_DEFAULT, the cached value will be used and not be updated. + * @return The api version number of the device, or 0 on default/failure. */ int get_device_api_version(void); diff --git a/ldso/linux/dynlink.c b/ldso/linux/dynlink.c index 7208a914009fabe5a9d05068feb4be9e41df0503..e0838e8ccbb05cddcb6518e2e6398c73027c9c56 100644 --- a/ldso/linux/dynlink.c +++ b/ldso/linux/dynlink.c @@ -40,6 +40,7 @@ #include "fork_impl.h" #include "strops.h" #include "trace/trace_marker.h" +#include "info/device_api_version.h" #ifdef IS_ASAN #if defined (__arm__) @@ -3034,6 +3035,7 @@ void __dls3(size_t *sp, size_t *auxv, size_t *aux) #endif InitHilogSocketFd(); __init_fdsan(); + InitDeviceApiVersion(); // do nothing when no define OHOS_ENABLE_PARAMETER /* If the main program was already loaded by the kernel, * AT_PHDR will point to some location other than the dynamic * linker's program headers. */ diff --git a/libc-test/src/functionalext/info/device_api_version.c b/libc-test/src/functionalext/info/device_api_version.c index 41af7975193d35adf4d7a8b513b1dc6dad8baf6e..c6668518afa6b447afb781a5228cd3d5a7e7a3e4 100644 --- a/libc-test/src/functionalext/info/device_api_version.c +++ b/libc-test/src/functionalext/info/device_api_version.c @@ -33,8 +33,9 @@ static void get_device_api_version_0010(void) { int APIVersion = get_device_api_version(); - EXPECT_EQ(APIVersion, API_VERSION_DEFAULT); + EXPECT_EQ(API_VERSION_DEFAULT, APIVersion < 0); } + int main(void) { get_device_api_version_0010(); diff --git a/src/info/linux/device_api_version.c b/src/info/linux/device_api_version.c index 81efaa0980dc024cf905f50fcada2caec90b6dd3..ead4e1f6572495e7af435d9e70fc51f4d2cad215 100644 --- a/src/info/linux/device_api_version.c +++ b/src/info/linux/device_api_version.c @@ -13,13 +13,46 @@ * limitations under the License. */ +#include +#include #include +#ifdef OHOS_ENABLE_PARAMETER +#include "sys_param.h" +#endif + #define API_VERSION_DEFAULT 0 +#define API_VERSION_NAME_LEN 256 -int get_device_api_version(void) +static int device_api_level = API_VERSION_DEFAULT; + +void InitDeviceApiVersion(void) { - // depend subsystem of syspara support the interface of get system property +#ifdef OHOS_ENABLE_PARAMETER + const char para_name[API_VERSION_NAME_LEN] = "const.ohos.apiversion"; + CachedHandle para_handler = CachedParameterCreate(para_name, "0"); + if (para_handler == NULL) { + device_api_level = API_VERSION_DEFAULT; + return; + } + const char *para_value = CachedParameterGet(para_handler); + if (para_value == NULL) { + device_api_level = API_VERSION_DEFAULT; + return; + } + int api_level = atoi(para_value); + CachedParameterDestroy(para_handler); + device_api_level = (api_level > 0 ) ? api_level : API_VERSION_DEFAULT; +#endif // OHOS_ENABLE_PARAMETER +} - return API_VERSION_DEFAULT; +int get_device_api_version(void) +{ +#ifdef OHOS_ENABLE_PARAMETER + if (device_api_level == API_VERSION_DEFAULT) { + // Api version may not init by InitDeviceApiVersion in dls3, try again. + InitDeviceApiVersion(); + } +#endif // OHOS_ENABLE_PARAMETER + return device_api_level; }