db: add db update command for Emulator 94/80594/7 accepted/tizen/common/20160804.174405 accepted/tizen/ivi/20160804.081138 accepted/tizen/mobile/20160804.081209 accepted/tizen/tv/20160804.081058 accepted/tizen/wearable/20160804.081122 submit/tizen/20160803.081834
authortaeyoung <ty317.kim@samsung.com>
Tue, 19 Jul 2016 04:26:43 +0000 (13:26 +0900)
committertaeyoung <ty317.kim@samsung.com>
Wed, 3 Aug 2016 03:55:58 +0000 (12:55 +0900)
- In the emulator environment, the display resolution
  can be changed before the first boot. Thus the command
  system_info_update_db is used to update the system info
  values before the first boot.

Change-Id: I06fcef9eeef60630b33f6bce9c2f054473eb8525
Signed-off-by: taeyoung <ty317.kim@samsung.com>
CMakeLists.txt
packaging/capi-system-info.spec
src/init_db/system_info_db_init.c
src/update_db/CMakeLists.txt [new file with mode: 0755]
src/update_db/system_info_db_update.c [new file with mode: 0644]

index 64b0000160f55ca77ecf5329e19ce644e50dc801..1dac3e987041be2327e681631d08814d67510b0d 100644 (file)
@@ -79,6 +79,9 @@ INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/system_info_intf.h DESTINATION
 
 ADD_SUBDIRECTORY(src/tizenid)
 ADD_SUBDIRECTORY(src/init_db)
+IF("${EMULATOR}" STREQUAL "on")
+       ADD_SUBDIRECTORY(src/update_db)
+ENDIF("${EMULATOR}" STREQUAL "on")
 
 IF(UNIX)
 
index 342ed80d36778859de47c3b2ebf32cbc765301f3..86f13a0cb2afbb1be51ff5045653612a1bd4b1b4 100644 (file)
@@ -1,3 +1,5 @@
+%bcond_with emulator
+
 Name:           capi-system-info
 Version:        0.2.0
 Release:        0
@@ -15,6 +17,9 @@ BuildRequires:  pkgconfig(openssl)
 BuildRequires:  pkgconfig(glib-2.0)
 BuildRequires:  pkgconfig(libtzplatform-config)
 BuildRequires:  gdbm-devel
+%if %{with emulator}
+BuildRequires:  glibc-devel-static
+%endif
 
 %description
 
@@ -31,6 +36,12 @@ Requires: %{name} = %{version}-%{release}
 %setup -q
 cp %{SOURCE1001} .
 
+%if %{with emulator}
+%define EMULATOR on
+%else
+%define EMULATOR off
+%endif
+
 %define config_file_path /etc/config/model-config.xml
 %define info_file_path /etc/info.ini
 %define sysinfo_shared_path %{TZ_SYS_ETC}/sysinfo
@@ -44,6 +55,7 @@ MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'`
                 -DINFO_FILE_PATH=%{info_file_path} \
                 -DMAJORVER=${MAJORVER} \
                 -DFULLVER=%{version} \
+                -DEMULATOR=%{EMULATOR} \
                 -DTIZEN_ID_PATH=%{tizen_id_path} \
                 -DDB_PATH=%{db_path}
 
@@ -70,6 +82,9 @@ chsmack -a "System::Shared" -t %{sysinfo_shared_path}
 %{_libdir}/libcapi-system-info.so.*
 %attr(0744,root,-) /etc/make_info_file.sh
 %{_bindir}/system_info_init_db
+%if %{with emulator}
+%{_bindir}/system_info_update_db
+%endif
 
 #tizenid
 %dir %{sysinfo_shared_path}
index ce953dce98b22f49cb29ce5f5e5e80b8a638d7b3..5fd5b0d7f759fda37928894fa348423bd4c1a0e4 100644 (file)
@@ -20,6 +20,7 @@
 #include <string.h>
 #include <unistd.h>
 #include <ctype.h>
+#include <getopt.h>
 #include <gdbm.h>
 #include <fcntl.h>
 #include <sys/stat.h>
@@ -213,7 +214,7 @@ static int system_info_get_values_ini(GDBM_FILE *db)
        return 0;
 }
 
-int main(int argc, char *argv[])
+static int system_info_create_db(void)
 {
        int ret;
        GDBM_FILE db;
@@ -236,3 +237,107 @@ int main(int argc, char *argv[])
 
        return 0;
 }
+
+static void show_help(void)
+{
+       printf("system_info_init_db [OPTIONS]\n");
+       printf("  -h --help         Show this help\n");
+       printf("  -k --key=KEY      System info key to update\n");
+       printf("  -t --type=TYPE    System info type to update (int/bool/double/string)\n");
+       printf("  -g --tag=TAGE     System info tag to update (platform/custom)\n");
+       printf("  -v --value=VALUE  System info value to update\n");
+}
+
+static int system_info_update_db(int argc, char *argv[])
+{
+       int ret;
+       GDBM_FILE db;
+       int opt;
+       bool failed = false;
+       char key[KEY_MAX] = { 0, };
+       char type[KEY_MAX] = { 0, };
+       char tag[KEY_MAX] = { 0, };
+       char value[KEY_MAX] = { 0, };
+       struct option long_options[] = {
+               { "key",   required_argument, 0, 0 },
+               { "type",  required_argument, 0, 0 },
+               { "tag",   required_argument, 0, 0 },
+               { "value", required_argument, 0, 0 },
+               { "help",  no_argument,       0, 0 },
+               { 0,       0,                 0, 0 },
+       };
+
+       while (1) {
+               opt = getopt_long(argc, argv, "k:t:g:v:h",
+                               long_options, NULL);
+               if (opt < 0)
+                       break;
+               switch (opt) {
+               case 'k':
+                       snprintf(key, sizeof(key), "%s", optarg);
+                       break;
+               case 't':
+                       snprintf(type, sizeof(type), "%s", optarg);
+                       break;
+               case 'g':
+                       snprintf(tag, sizeof(tag), "%s", optarg);
+                       break;
+               case 'v':
+                       snprintf(value, sizeof(value), "%s", optarg);
+                       break;
+               case 'h':
+               default:
+                       show_help();
+                       return 0;
+               }
+       }
+
+       failed = false;
+       if (key[0] == '\0') {
+               printf("Invalid Parameter: no key\n");
+               failed = true;
+       }
+       if (type[0] == '\0') {
+               printf("Invalid Parameter: no type\n");
+               failed = true;
+       }
+       if (tag[0] == '\0') {
+               printf("Invalid Parameter: no tag\n");
+               failed = true;
+       }
+       if (value[0] == '\0') {
+               printf("Invalid Parameter: no value\n");
+               failed = true;
+       }
+
+       if (failed)
+               return -EINVAL;
+
+       _I("Request to update: key(%s), type(%s), tag(%s), value(%s)",
+                       key, type, tag, value);
+
+       /* http://www.gnu.org.ua/software/gdbm/manual/html_node/Open.html
+        *   If flags is set to ‘GDBM_WRITER’,
+        *   the user wants both read and write access to the database
+        *   and requires exclusive access */
+       db = gdbm_open(SYSTEM_INFO_DB_PATH, 0, GDBM_WRITER, S_IRUSR | S_IRGRP | S_IROTH, NULL);
+       if (!db) {
+               _E("Failed to open db (%d, %s)", gdbm_errno, gdbm_strerror(gdbm_errno));
+               return -ENOENT;
+       }
+
+       ret = db_set_value(&db, tag, key, type, value);
+       if (ret != 0)
+               _E("Failed to set value (%d)", ret);
+
+       gdbm_close(db);
+       return ret;
+}
+
+int main(int argc, char *argv[])
+{
+       if (argc == 1)
+               return system_info_create_db();
+
+       return system_info_update_db(argc, argv);
+}
diff --git a/src/update_db/CMakeLists.txt b/src/update_db/CMakeLists.txt
new file mode 100755 (executable)
index 0000000..c80cd30
--- /dev/null
@@ -0,0 +1,14 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+SET(UPDATE_DB "system_info_update_db")
+
+SET(SRCS ${CMAKE_SOURCE_DIR}/src/update_db/system_info_db_update.c)
+INCLUDE_DIRECTORIES(include)
+
+SET(BUILD_SHARED_LIBRARIES OFF)
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -g")
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}")
+SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -static")
+
+ADD_EXECUTABLE(${UPDATE_DB} ${SRCS})
+TARGET_LINK_LIBRARIES(${UPDATE_DB})
+INSTALL(TARGETS ${UPDATE_DB} DESTINATION bin)
diff --git a/src/update_db/system_info_db_update.c b/src/update_db/system_info_db_update.c
new file mode 100644 (file)
index 0000000..98a5764
--- /dev/null
@@ -0,0 +1,169 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+#include <stdio.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <ctype.h>
+#include <getopt.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <errno.h>
+
+#define KEY_MAX 256
+
+static const char *db_update_arg[] = {
+       "/usr/bin/system_info_init_db",
+       "-k", NULL,
+       "-t", NULL,
+       "-g", NULL,
+       "-v", NULL,
+       NULL,
+};
+
+static void show_help(void)
+{
+       printf("system_info_update_db [OPTIONS]\n");
+       printf("  -h --help         Show this help\n");
+       printf("  -r --root=ROOT    Root path for chroot operation\n");
+       printf("  -k --key=KEY      System info key to update\n");
+       printf("  -t --type=TYPE    System info type to update (int/bool/double/string)\n");
+       printf("  -g --tag=TAGE     System info tag to update (platform/custom)\n");
+       printf("  -v --value=VALUE  System info value to update\n");
+}
+
+int main(int argc, char *argv[])
+{
+       pid_t pid;
+       int status, ret;
+       int opt;
+       bool failed;
+       char root[KEY_MAX] = { 0, };
+       char key[KEY_MAX] = { 0, };
+       char type[KEY_MAX] = { 0, };
+       char tag[KEY_MAX] = { 0, };
+       char value[KEY_MAX] = { 0, };
+       struct option long_options[] = {
+               { "root",  required_argument, 0, 0 },
+               { "key",   required_argument, 0, 0 },
+               { "type",  required_argument, 0, 0 },
+               { "tag",   required_argument, 0, 0 },
+               { "value", required_argument, 0, 0 },
+               { "help",  no_argument,       0, 0 },
+               { 0,       0,                 0, 0 },
+       };
+
+       while (1) {
+               opt = getopt_long(argc, argv, "r:k:t:g:v:h",
+                               long_options, NULL);
+               if (opt < 0)
+                       break;
+               switch (opt) {
+               case 'r':
+                       snprintf(root, sizeof(root), "%s", optarg);
+                       break;
+               case 'k':
+                       snprintf(key, sizeof(key), "%s", optarg);
+                       break;
+               case 't':
+                       snprintf(type, sizeof(type), "%s", optarg);
+                       break;
+               case 'g':
+                       snprintf(tag, sizeof(tag), "%s", optarg);
+                       break;
+               case 'v':
+                       snprintf(value, sizeof(value), "%s", optarg);
+                       break;
+               case 'h':
+               default:
+                       show_help();
+                       return 0;
+               }
+       }
+
+       failed = false;
+       if (root[0] == '\0') {
+               printf("Invalid Parameter: no root path\n");
+               failed = true;
+       }
+       if (key[0] == '\0') {
+               printf("Invalid Parameter: no key\n");
+               failed = true;
+       }
+       if (type[0] == '\0') {
+               printf("Invalid Parameter: no type\n");
+               failed = true;
+       }
+       if (tag[0] == '\0') {
+               printf("Invalid Parameter: no tag\n");
+               failed = true;
+       }
+       if (value[0] == '\0') {
+               printf("Invalid Parameter: no value\n");
+               failed = true;
+       }
+
+       if (failed)
+               return -EINVAL;
+
+       printf("Root Path(%s)\n", root);
+       printf("Request to update: key(%s), type(%s), tag(%s), value(%s)\n",
+                       key, type, tag, value);
+
+       db_update_arg[2] = key;
+       db_update_arg[4] = type;
+       db_update_arg[6] = tag;
+       db_update_arg[8] = value;
+
+       ret = chroot(root);
+       if (ret < 0) {
+               printf("Failed to change root dir (%s, errno:%d)\n", root, errno);
+               return -errno;
+       }
+
+       pid = fork();
+       if (pid < 0) {
+               printf("Failed to update db (%d)\n", errno);
+               return -errno;
+       } else if (pid == 0) {
+               ret = execv(db_update_arg[0], (char **)db_update_arg);
+               if (ret < 0)
+                       exit(EXIT_FAILURE);
+               return ret;
+       } else {
+               ret = waitpid(pid, &status, 0);
+               if (ret == -1) {
+                       printf("%d waitpid() failed : %d\n", pid, errno);
+                       return -EAGAIN;
+               }
+
+               if (WIFEXITED(status)) {
+                       printf("%d terminated by exit(%d)\n", pid, WEXITSTATUS(status));
+                       return WEXITSTATUS(status);
+               }
+
+               if (WIFSIGNALED(status))
+                       printf("%d terminated by signal %d\n", pid, WTERMSIG(status));
+               else if (WIFSTOPPED(status))
+                       printf("%d stopped by signal %d\n", pid, WSTOPSIG(status));
+               return -EAGAIN;
+       }
+}