Add libsys_is_emulator() and refactor libsys_is_container() 29/273729/2
authorHyotaek Shim <hyotaek.shim@samsung.com>
Wed, 13 Apr 2022 02:26:17 +0000 (11:26 +0900)
committerYoungjae Cho <y0.cho@samsung.com>
Tue, 26 Apr 2022 06:54:24 +0000 (23:54 -0700)
Change-Id: Ic2cce60d76704803cd1c54aa70a9c795b92c9906
Signed-off-by: Hyotaek Shim <hyotaek.shim@samsung.com>
Signed-off-by: Youngjae Cho <y0.cho@samsung.com>
CMakeLists.txt
packaging/libsyscommon.spec
src/.gitignore [deleted file]
src/libcommon/common.c
src/libcommon/common.h

index bebdb2e..2df65ef 100644 (file)
@@ -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}")
index 4ec8ea2..d4bc949 100644 (file)
@@ -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 (file)
index 7ca4570..0000000
+++ /dev/null
@@ -1,2 +0,0 @@
-/Makefile
-/test-*
\ No newline at end of file
index fd3a11b..f2d1831 100644 (file)
  */
 
 #include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
 #include <unistd.h>
+#include <system/system_info.h>
+#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;
 }
index 43a1fae..057bad4 100644 (file)
 
 #include <stdbool.h>
 
-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__ */