Merge "Move variables declarations from header to source" into tizen
[sdk/target/sdbd.git] / src / sdb.c
index c31020b..64415ad 100644 (file)
--- a/src/sdb.c
+++ b/src/sdb.c
@@ -44,8 +44,6 @@
 #if !SDB_HOST
 #include <linux/prctl.h>
 #define SDB_PIDPATH "/tmp/.sdbd.pid"
-#else
-#include "usb_vendors.h"
 #endif
 #include <system_info.h>
 #include <vconf.h>
@@ -144,9 +142,6 @@ void handle_sig_term(int sig) {
     if (access(SDB_PIDPATH, F_OK) == 0)
         sdb_unlink(SDB_PIDPATH);
 #endif
-    char * const cmd1_args[] = {"/usr/bin/killall", "/usr/bin/debug_launchpad_preloading_preinitializing_daemon", NULL};
-    spawn("/usr/bin/killall", cmd1_args);
-    sdb_sleep_ms(1000);
 }
 
 static const char *sdb_device_banner = "device";
@@ -173,6 +168,11 @@ void fatal_errno(const char *fmt, ...)
     exit(-1);
 }
 
+static int is_enable_sdbd_log()
+{
+    return (!strncmp(g_capabilities.log_enable, SDBD_CAP_RET_ENABLED, strlen(SDBD_CAP_RET_ENABLED)));
+}
+
 int   sdb_trace_mask;
 
 /* read a comma/space/colum/semi-column separated list of tags
@@ -206,8 +206,12 @@ void  sdb_trace_init(void)
         { NULL, 0 }
     };
 
-    if (p == NULL)
+    if (p == NULL) {
+        if (is_enable_sdbd_log())
+            p = "all";
+        else
             return;
+    }
 
     /* use a comma/column/semi-colum/space separated list */
     while (*p) {
@@ -643,8 +647,7 @@ void handle_packet(apacket *p, atransport *t)
     // Verify pointer p
     int result = access((const char *) p, F_OK);
     if ((result == -1) && (errno == EFAULT)) {
-        D("Invalid apacket = [0x%x]", p);
-        fatal("Invalid apacket = [0x%x]", p);
+        fatal("Invalid apacket = [0x%p]", p);
     }
 
     asocket *s;
@@ -1021,14 +1024,18 @@ void start_logging(void)
 }
 
 #if !SDB_HOST
+
 void start_device_log(void)
 {
     int fd;
-    char    path[PATH_MAX];
+    char    path[PATH_MAX] = {0, };
+    char    path_folder[PATH_MAX] = {0, };
+    char    path_file[PATH_MAX] = {0, };
     struct tm now;
     time_t t;
 //    char value[PROPERTY_VALUE_MAX];
-    const char* p = getenv("SDB_TRACE");
+    const char* p_trace = getenv("SDB_TRACE");
+    const char* p_path = getenv("SDBD_LOG_PATH");
     // read the trace mask from persistent property persist.sdb.trace_mask
     // give up if the property is not set or cannot be parsed
 #if 0 /* tizen specific */
@@ -1037,16 +1044,28 @@ void start_device_log(void)
         return;
 #endif
 
-    if (p == NULL) {
+    if ((p_trace == NULL ) && !is_enable_sdbd_log()) {
         return;
     }
+
+    if (p_path)
+        snprintf(path_folder, sizeof(path_folder), "%s", p_path);
+    else if (g_capabilities.log_path[0] != '\0')
+        snprintf(path_folder, sizeof(path_folder), "%s", g_capabilities.log_path);
+    else
+        return;
+
     tzset();
     time(&t);
     localtime_r(&t, &now);
-    strftime(path, sizeof(path),
-                "/tmp/sdbd-%Y-%m-%d-%H-%M-%S.txt",
-                &now);
-    fd = unix_open(path, O_WRONLY | O_CREAT | O_TRUNC, 0640);
+
+    strftime(path_file, sizeof(path_file),
+                    "sdbd-%Y-%m-%d-%H-%M-%S.txt",
+                    &now);
+
+    snprintf(path, sizeof(path), "%s/%s", path_folder, path_file);
+
+    fd = unix_open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
     if (fd < 0) {
         return;
     }
@@ -1068,9 +1087,6 @@ void start_device_log(void)
 
 int daemonize(void) {
 
-    // set file creation mask to 0
-    umask(0);
-
     switch (fork()) {
     case -1:
         return -1;
@@ -1079,6 +1095,11 @@ int daemonize(void) {
     default:
         _exit(0);
     }
+
+    // 2016-08-25 : modified umask to 022 from 000 because of security reason
+       // there is a problem that the file created by sdb shell command could be written by 'others'
+       umask(SDBD_UMASK);
+
 #ifdef SDB_PIDPATH
     FILE *f = fopen(SDB_PIDPATH, "w");
 
@@ -1541,12 +1562,6 @@ int set_sdk_user_privileges() {
     return 0;
 }
 
-static void execute_required_process() {
-    char * const cmd_args[] = {"/usr/bin/debug_launchpad_preloading_preinitializing_daemon",NULL};
-
-    spawn("/usr/bin/debug_launchpad_preloading_preinitializing_daemon", cmd_args);
-}
-
 /* default plugin proc */
 static int get_plugin_capability(const char* in_buf, sdbd_plugin_param out) {
     int ret = SDBD_PLUGIN_RET_NOT_SUPPORT;
@@ -1589,12 +1604,22 @@ static int get_plugin_capability(const char* in_buf, sdbd_plugin_param out) {
         } else {
             snprintf(out.data, out.len, "%s", SDBD_CAP_RET_DISABLED);
         }
+        ret = SDBD_PLUGIN_RET_SUCCESS;
+    } else if (SDBD_CMP_CAP(in_buf, CANLAUNCH)) {
+        snprintf(out.data, out.len, "%s", UNKNOWN);
+        ret = SDBD_PLUGIN_RET_SUCCESS;
     } else if (SDBD_CMP_CAP(in_buf, PLUGIN_VER)) {
         snprintf(out.data, out.len, "%s", UNKNOWN);
         ret = SDBD_PLUGIN_RET_SUCCESS;
     } else if (SDBD_CMP_CAP(in_buf, PRODUCT_VER)) {
         snprintf(out.data, out.len, "%s", UNKNOWN);
         ret = SDBD_PLUGIN_RET_SUCCESS;
+    } else if (SDBD_CMP_CAP(in_buf, LOG_ENABLE)) {
+        snprintf(out.data, out.len, "%s", SDBD_CAP_RET_DISABLED);
+        ret = SDBD_PLUGIN_RET_SUCCESS;
+    } else if (SDBD_CMP_CAP(in_buf, LOG_PATH)) {
+        snprintf(out.data, out.len, "%s", "/tmp");
+        ret = SDBD_PLUGIN_RET_SUCCESS;
     }
 
     return ret;
@@ -1671,6 +1696,11 @@ static int verify_root_cmd(const char* in_buf, sdbd_plugin_param out) {
     return ret;
 }
 
+static int get_shell_env(const char* in_buf, sdbd_plugin_param out) {
+    snprintf(out.data, out.len, "%s", "");
+    return SDBD_PLUGIN_RET_SUCCESS;
+}
+
 int default_cmd_proc(const char* cmd,
                     const char* in_buf, sdbd_plugin_param out) {
     int ret = SDBD_PLUGIN_RET_NOT_SUPPORT;
@@ -1696,6 +1726,8 @@ int default_cmd_proc(const char* cmd,
         ret = verify_sdbd_launch(in_buf, out);
     } else if (SDBD_CMP_CMD(cmd, VERIFY_ROOTCMD)) {
         ret = verify_root_cmd(in_buf, out);
+    } else if (SDBD_CMP_CMD(cmd, SHELL_ENVVAR)) {
+        ret = get_shell_env(in_buf, out);
     } else {
         D("Not supported command : %s\n", cmd);
         ret = SDBD_PLUGIN_RET_NOT_SUPPORT;
@@ -1885,8 +1917,6 @@ static void init_sdk_requirements() {
         }
     }
 
-    execute_required_process();
-
     if (is_emulator()) {
         register_bootdone_cb();
     }
@@ -2072,6 +2102,16 @@ static void init_capabilities(void) {
     }
 
 
+    // Target name of the launch possible
+    if(!request_plugin_cmd(SDBD_CMD_PLUGIN_CAP, SDBD_CAP_TYPE_CANLAUNCH,
+                            g_capabilities.can_launch,
+                            sizeof(g_capabilities.can_launch))) {
+        D("failed to request. (%s:%s) \n", SDBD_CMD_PLUGIN_CAP, SDBD_CAP_TYPE_CANLAUNCH);
+        snprintf(g_capabilities.can_launch, sizeof(g_capabilities.can_launch),
+                    "%s", UNKNOWN);
+    }
+
+
     // Platform version
     ret = system_info_get_platform_string("http://tizen.org/feature/platform.version", &value);
     if (ret != SYSTEM_INFO_ERROR_NONE) {
@@ -2110,6 +2150,30 @@ static void init_capabilities(void) {
         snprintf(g_capabilities.sdbd_plugin_version, sizeof(g_capabilities.sdbd_plugin_version),
                     "%s", UNKNOWN);
     }
+
+
+    // sdbd log enable
+    if(!request_plugin_cmd(SDBD_CMD_PLUGIN_CAP, SDBD_CAP_TYPE_LOG_ENABLE,
+                               g_capabilities.log_enable,
+                               sizeof(g_capabilities.log_enable))) {
+           D("failed to request. (%s:%s) \n", SDBD_CMD_PLUGIN_CAP, SDBD_CAP_TYPE_LOG_ENABLE);
+           snprintf(g_capabilities.log_enable, sizeof(g_capabilities.log_enable),
+                       "%s", DISABLED);
+    }
+
+    // sdbd log path
+    if(!request_plugin_cmd(SDBD_CMD_PLUGIN_CAP, SDBD_CAP_TYPE_LOG_PATH,
+                               g_capabilities.log_path,
+                               sizeof(g_capabilities.log_path))) {
+           D("failed to request. (%s:%s) \n", SDBD_CMD_PLUGIN_CAP, SDBD_CAP_TYPE_LOG_PATH);
+           snprintf(g_capabilities.log_path, sizeof(g_capabilities.log_path),
+                       "%s", UNKNOWN);
+    }
+
+
+    // Capability version
+    snprintf(g_capabilities.sdbd_cap_version, sizeof(g_capabilities.sdbd_cap_version),
+                "%d.%d", SDBD_CAP_VERSION_MAJOR, SDBD_CAP_VERSION_MINOR);
 }
 
 static int is_support_usbproto()
@@ -2152,14 +2216,15 @@ int sdb_main(int is_daemon, int server_port)
     load_sdbd_plugin();
     init_capabilities();
 
+    sdb_trace_init();
+    start_device_log();
+
     init_drop_privileges();
     init_sdk_requirements();
     if (!request_plugin_verification(SDBD_CMD_VERIFY_LAUNCH, NULL)) {
         D("sdbd should be launched in develop mode.\n");
         return -1;
     }
-
-    umask(000);
 #endif
 
     atexit(sdb_cleanup);
@@ -2175,7 +2240,6 @@ int sdb_main(int is_daemon, int server_port)
 
 #if SDB_HOST
     HOST = 1;
-    usb_vendors_init();
     usb_init();
     local_init(DEFAULT_SDB_LOCAL_TRANSPORT_PORT);
 
@@ -2618,7 +2682,6 @@ int recovery_mode = 0;
 
 int main(int argc, char **argv)
 {
-    sdb_trace_init(); /* tizen specific */
 #if SDB_HOST
     sdb_sysdeps_init();
     sdb_trace_init();
@@ -2657,7 +2720,6 @@ int main(int argc, char **argv)
         fatal("daemonize() failed: errno:%d", errno);
 #endif
 
-    start_device_log();
     D("Handling main()\n");
 
     //sdbd will never die on emulator!