From 7382ed2ef0eb3de36631957c0cee0cbfaf8a8d5b Mon Sep 17 00:00:00 2001 From: Hyotaek Shim Date: Wed, 13 Apr 2022 11:26:17 +0900 Subject: [PATCH] Add libsys_is_emulator() and refactor libsys_is_container() Change-Id: Ic2cce60d76704803cd1c54aa70a9c795b92c9906 Signed-off-by: Hyotaek Shim Signed-off-by: Youngjae Cho --- CMakeLists.txt | 3 ++- packaging/libsyscommon.spec | 10 ++++---- src/.gitignore | 2 -- src/libcommon/common.c | 47 +++++++++++++++++++++++++++++++++---- src/libcommon/common.h | 14 ++++++++++- 5 files changed, 63 insertions(+), 13 deletions(-) delete mode 100644 src/.gitignore diff --git a/CMakeLists.txt b/CMakeLists.txt index bebdb2e..2df65ef 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,8 +33,9 @@ INCLUDE(FindPkgConfig) pkg_check_modules(syscommon REQUIRED glib-2.0 gio-2.0 + gio-unix-2.0 dlog - gio-unix-2.0) + capi-system-info) FOREACH(flag ${syscommon_CFLAGS}) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") diff --git a/packaging/libsyscommon.spec b/packaging/libsyscommon.spec index 4ec8ea2..d4bc949 100644 --- a/packaging/libsyscommon.spec +++ b/packaging/libsyscommon.spec @@ -7,16 +7,16 @@ Group: System/Libraries Source: %{name}-%{version}.tar.gz Source1001: %{name}.manifest +BuildRequires: cmake +BuildRequires: pkgconfig(cmocka) BuildRequires: pkgconfig(glib-2.0) >= 2.44 BuildRequires: pkgconfig(gio-2.0) >= 2.44 BuildRequires: pkgconfig(gio-unix-2.0) BuildRequires: pkgconfig(dlog) -BuildRequires: pkgconfig(cmocka) -BuildRequires: cmake - -Requires: /bin/cp +BuildRequires: pkgconfig(capi-system-info) -Requires(post): /sbin/ldconfig +Requires: /bin/cp +Requires(post): /sbin/ldconfig Requires(postun): /sbin/ldconfig %description diff --git a/src/.gitignore b/src/.gitignore deleted file mode 100644 index 7ca4570..0000000 --- a/src/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -/Makefile -/test-* \ No newline at end of file diff --git a/src/libcommon/common.c b/src/libcommon/common.c index fd3a11b..f2d1831 100644 --- a/src/libcommon/common.c +++ b/src/libcommon/common.c @@ -15,15 +15,54 @@ */ #include +#include +#include #include +#include +#include "shared/log.h" #include "common.h" -#define CONTAINER_FILE_PATH "/run/systemd/container" +#define FEATURE_MODEL_NAME "http://tizen.org/system/model_name" +#define FEATURE_MODEL_NAME_EMULATOR "Emulator" +#define CONTAINER_FILE_PATH "/run/systemd/container" -bool is_container() +bool libsys_is_emulator(void) { + int ret = 0; + char *model_name = NULL; + static bool is_emul = false; + static bool is_cached = false; + + if (is_cached) + return is_emul; + + ret = system_info_get_platform_string(FEATURE_MODEL_NAME, &model_name); + if (ret < 0) { + _E("Cannot get model name: %d, libsys_is_emulator() returns false on operation failure", ret); + return false; + } + + if (!strncmp(FEATURE_MODEL_NAME_EMULATOR, model_name, strlen(model_name) + 1)) + is_emul = true; + + is_cached = true; + free(model_name); + + return is_emul; +} + +bool libsys_is_container(void) +{ + static bool is_container = false; + static bool is_cached = false; + + if (is_cached) + return is_container; + if (access(CONTAINER_FILE_PATH, F_OK) == 0) - return true; + is_container = true; + + is_cached = true; - return false; + return is_container; } diff --git a/src/libcommon/common.h b/src/libcommon/common.h index 43a1fae..057bad4 100644 --- a/src/libcommon/common.h +++ b/src/libcommon/common.h @@ -19,6 +19,18 @@ #include -bool is_container(); +/** + * @brief Check if running on emulator + * + * @return true if running on emulator, otherwise false even on operation failure + */ +bool libsys_is_emulator(void); + +/** + * @brief Check if running on container + * + * @return true if running on container, otherwise false even on operation failure + */ +bool libsys_is_container(void); #endif /* __LIBCOMMON_COMMON_H__ */ -- 2.34.1