util: remove/replace unnecessary system(3) call
authorSangho Park <sangho.p@samsung.com>
Mon, 15 Feb 2016 03:38:07 +0000 (12:38 +0900)
committerSeokYeon Hwang <syeon.hwang@samsung.com>
Fri, 19 Feb 2016 07:02:02 +0000 (16:02 +0900)
* Mac OS X
** use uname(3) instead of system("uname")
** remove system("java -version")
* Linux
** Try to read /etc/lsb-release
** Add get_number_of_processors()
** Remove redundant codes

Change-Id: I0a73b27ab35a6ab9dd774455b33182abb15ad481
Signed-off-by: Sangho Park <sangho.p@samsung.com>
tizen/src/util/osutil-darwin.c
tizen/src/util/osutil-linux.c

index cdf492c13b502b787409b02e89dfbafedc402622..b27ed7ca24faf92900b6b4a2c7c865ad496149c9 100644 (file)
@@ -6,6 +6,7 @@
  * Contact:
  * SeokYeon Hwang <syeon.hwang@samsung.com>
  * MunKyu Im <munkyu.im@samsung.com>
+ * Sangho Park <sangho.p@samsung.com>
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -32,6 +33,7 @@
  */
 
 #include <sys/sysctl.h>
+#include <sys/utsname.h>
 
 #include "png.h"
 
@@ -102,15 +104,18 @@ int get_number_of_processors(void)
 
 void print_system_info_os(void)
 {
+    struct utsname utsname;
+
     LOG_INFO("* Mac\n");
     LOG_INFO("* LibPNG Version : %s\n", PNG_LIBPNG_VER_STRING);
 
     /* uname */
     LOG_INFO("* Host machine uname :\n");
-    char const *const uname_cmd = "uname -a";
-    if(system(uname_cmd) < 0) {
-        LOG_INFO("system function command '%s' \
-            returns error !", uname_cmd);
+    if (uname(&utsname) == 0) {
+        LOG_INFO("* Host machine uname : %s %s %s %s %s\n",
+            utsname.sysname, utsname.nodename,
+            utsname.release, utsname.version,
+            utsname.machine);
     }
 
     /* hw information */
@@ -153,16 +158,6 @@ void print_system_info_os(void)
     if (sysctl(mib, 2, &sys_num, &len, NULL, 0) >= 0) {
         LOG_INFO("* Total memory : %llu bytes\n", sys_num);
     }
-
-    /* java version */
-    LOG_INFO("* Java version :\n");
-    char const *const lspci_cmd = "java -version";
-
-    fflush(stdout);
-    if(system(lspci_cmd) < 0) {
-        LOG_INFO("system function command '%s' \
-            returns error !", lspci_cmd);
-    }
 }
 
 bool make_sdcard_lock_os(char *sdcard)
index 9dc506382a6b27e3a66b6723a7cdaa70dd812355..8e432f990bda9f795ca03631b9f5abb70c533316 100644 (file)
@@ -6,6 +6,7 @@
  * Contact:
  * SeokYeon Hwang <syeon.hwang@samsung.com>
  * MunKyu Im <munkyu.im@samsung.com>
+ * Sangho Park <sangho.p@samsung.com>
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
 #include <linux/version.h>
 #include <sys/utsname.h>
 #include <sys/sysinfo.h>
+#include <errno.h>
 
 #include "png.h"
 
 #include "osutil.h"
 #include "emul_state.h"
+#include "qemu/osdep.h"
 
 #ifndef CONFIG_LINUX
 #error
@@ -82,11 +85,48 @@ int get_number_of_processors(void)
     if (num_processors < 1) {
         num_processors = 1;
     }
-    LOG_TRACE("Number of processors : %d\n", num_processors);
+    LOG_INFO("Number of processors : %d\n", num_processors);
 
     return num_processors;
 }
 
+static void _system(const char *buf)
+{
+    if (system(buf) < 0) {
+        LOG_INFO("system function command '%s' \
+                returns error !", buf);
+    }
+}
+
+static void get_lsb(void)
+{
+    int fd;
+
+    /*
+     * lsb_release(1) is defined at LSB spec
+     * while the spec does not define /etc/lsb-release.
+     * At first, try to read from /etc/lsb-release in order to avoid system(3).
+     * If open(2) to the file is failed, try to system(lsb_release).
+     */
+    LOG_INFO("* Linux distribution infomation :\n");
+    fd = qemu_open("/etc/lsb-release", O_RDONLY);
+    if (fd >= 0) {
+        char buf[4096];
+        int ret;
+        do {
+            ret = read(fd, buf, sizeof(buf));
+            if (ret > 0)
+                LOG_INFO("%.*s\n", ret, buf);
+        } while (ret == sizeof(buf));
+        if (ret < 0) {
+            LOG_INFO("Failed to read file for lsb: %s\n", strerror(errno));
+        }
+        qemu_close(fd);
+    } else {
+        _system("lsb_release -d -r -c");
+    }
+}
+
 void print_system_info_os(void)
 {
     LOG_INFO("* Linux\n");
@@ -98,7 +138,7 @@ void print_system_info_os(void)
         (LINUX_VERSION_CODE >> 8) & 0xff,
         LINUX_VERSION_CODE & 0xff);
 
-     /* depends on launching */
+    /* depends on launching */
     struct utsname host_uname_buf;
     if (uname(&host_uname_buf) == 0) {
         LOG_INFO("* Host machine uname : %s %s %s %s %s\n",
@@ -107,6 +147,11 @@ void print_system_info_os(void)
             host_uname_buf.machine);
     }
 
+    /* get linux distribution information */
+    get_lsb();
+
+    get_number_of_processors();
+
     struct sysinfo sys_info;
     if (sysinfo(&sys_info) == 0) {
         LOG_INFO("* Total Ram : %llu kB, Free: %llu kB\n",
@@ -114,37 +159,9 @@ void print_system_info_os(void)
             sys_info.freeram * (unsigned long long)sys_info.mem_unit / 1024);
     }
 
-    /* get linux distribution information */
-    LOG_INFO("* Linux distribution infomation :\n");
-    char const *const lsb_release_cmd = "lsb_release -d -r -c";
-    gchar *buffer = NULL;
-    gint buffer_size = strlen(lsb_release_cmd) + 1;
-
-    buffer = g_malloc(buffer_size);
-
-    g_snprintf(buffer, buffer_size, "%s", lsb_release_cmd);
-
-    if (system(buffer) < 0) {
-        LOG_INFO("system function command '%s' \
-                returns error !", buffer);
-    }
-    g_free(buffer);
-
     /* pci device description */
     LOG_INFO("* Host PCI devices :\n");
-    char const *const lspci_cmd = "lspci";
-    buffer_size = strlen(lspci_cmd) + 1;
-
-    buffer = g_malloc(buffer_size);
-
-    g_snprintf(buffer, buffer_size, "%s", lspci_cmd);
-
-    fflush(stdout);
-    if (system(buffer) < 0) {
-        LOG_INFO("system function command '%s' \
-                returns error !", buffer);
-    }
-    g_free(buffer);
+    _system("lspci");
 }
 
 bool make_sdcard_lock_os(char *sdcard)