From fc5348227888e54fd4999bc4e22a6c14c5c8a8c2 Mon Sep 17 00:00:00 2001 From: Sooyoung Ha Date: Wed, 12 Apr 2017 00:14:50 +0900 Subject: [PATCH 01/16] package: update version (3.0.21) Change-Id: Iab27716caef546f4b0fd3382828dfb8b87d26b0f Signed-off-by: Sooyoung Ha --- packaging/sdbd.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/sdbd.spec b/packaging/sdbd.spec index ac5f44f..cb64740 100644 --- a/packaging/sdbd.spec +++ b/packaging/sdbd.spec @@ -2,7 +2,7 @@ Name: sdbd Summary: SDB daemon -Version: 3.0.20 +Version: 3.0.21 Release: 0 License: Apache-2.0 Summary: SDB daemon -- 2.7.4 From 317db5e54e8b2c16b8ccd85b2f55369876bbe2b5 Mon Sep 17 00:00:00 2001 From: Sooyoung Ha Date: Thu, 13 Apr 2017 02:02:20 +0900 Subject: [PATCH 02/16] file_sync_service: fix stack corruption Change-Id: I1e85ba0bc7f772e8f8446030ea4ea71cda2b48d5 Signed-off-by: Sooyoung Ha --- src/file_sync_service.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/file_sync_service.c b/src/file_sync_service.c index 8b0b73c..d4d35eb 100644 --- a/src/file_sync_service.c +++ b/src/file_sync_service.c @@ -220,7 +220,7 @@ static int do_list(int s, const char *path) continue; } - s_strncpy(fname, de->d_name, sizeof tmp); + s_strncpy(fname, de->d_name, len); if(lstat(tmp, &st) == 0) { msg.dent.mode = htoll(st.st_mode); msg.dent.size = htoll(st.st_size); -- 2.7.4 From 5f8698b41647756d5a4386c704f0d446217d1fd7 Mon Sep 17 00:00:00 2001 From: Sooyoung Ha Date: Thu, 13 Apr 2017 02:11:45 +0900 Subject: [PATCH 03/16] package: update version (3.0.22) Change-Id: I687caf67d1d79df25e096dd6d2365f2b2d16a057 Signed-off-by: Sooyoung Ha --- packaging/sdbd.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/sdbd.spec b/packaging/sdbd.spec index cb64740..f5830f5 100644 --- a/packaging/sdbd.spec +++ b/packaging/sdbd.spec @@ -2,7 +2,7 @@ Name: sdbd Summary: SDB daemon -Version: 3.0.21 +Version: 3.0.22 Release: 0 License: Apache-2.0 Summary: SDB daemon -- 2.7.4 From 9f8960351494588c5a41b8ef100c890e620864ea Mon Sep 17 00:00:00 2001 From: Sooyoung Ha Date: Thu, 13 Apr 2017 15:30:30 +0900 Subject: [PATCH 04/16] packaging: remove redundant file copy code Change-Id: I9964684b1de3bd44cad0b85c356eea69c4abd1d6 Signed-off-by: Sooyoung Ha --- packaging/sdbd.spec | 3 --- 1 file changed, 3 deletions(-) diff --git a/packaging/sdbd.spec b/packaging/sdbd.spec index f5830f5..d353b13 100644 --- a/packaging/sdbd.spec +++ b/packaging/sdbd.spec @@ -74,8 +74,6 @@ cmake . -DCMAKE_INSTALL_PREFIX=%{_prefix} \ make %{?jobs:-j%jobs} %install -mkdir -p %{buildroot}/usr/share/license -cp LICENSE %{buildroot}/usr/share/license/%{name} mkdir -p %{buildroot}%{_unitdir} # extension-*-emulator @@ -135,7 +133,6 @@ chsmack -e "User::Shell" /sbin/sdbd-user %{_unitdir}/sdbd_tcp.service %{_unitdir}/multi-user.target.wants/sdbd.service %{_prefix}/lib/udev/rules.d/99-sdbd.rules -/usr/share/license/%{name} %{TZ_SYS_BIN}/profile_command %ifarch %{ix86} x86_64 -- 2.7.4 From ab7e3e8750f26c4a9b538762d7764ff3fecae20e Mon Sep 17 00:00:00 2001 From: Munkyu Im Date: Tue, 11 Apr 2017 23:34:35 +0900 Subject: [PATCH 05/16] misc: avoid potential buffer overflows Change-Id: I9c0c8c389cb502719d531a8495f4e35539606ffa Signed-off-by: Munkyu Im (cherry picked from commit 31061870b2693168025f6d6717c1ea8e8ae8c3e6) --- src/default_plugin_appcmd.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/default_plugin_appcmd.c b/src/default_plugin_appcmd.c index 643fb77..62a0217 100644 --- a/src/default_plugin_appcmd.c +++ b/src/default_plugin_appcmd.c @@ -300,7 +300,7 @@ static void appcmd_receiver_debugwebapp(int fd_in, int fd_out) D("debug webapp output : %s\n", buf); sub_str = strstr(buf, "port: "); - if (sub_str != NULL && sscanf(sub_str, "port: %s", port_str) == 1) { + if (sub_str != NULL && sscanf(sub_str, "port: %31s", port_str) == 1) { snprintf(out_buf, sizeof(out_buf), "\n%s:%s\n", MESSAGE_PREFIX_APPCMD_RETURN, port_str); writex(fd_out, out_buf, strlen(out_buf)+1); break; @@ -411,13 +411,13 @@ static void appcmd_receiver_appinfo(int fd_in, int fd_out) if (!strncmp(buf, "Appid: ", 7)) { memset(appid, 0, sizeof(appid)); - sscanf(buf, "Appid: %s", appid); + sscanf(buf, "Appid: %127s", appid); snprintf(out_buf+out_ptr, sizeof(out_buf)-out_ptr, ":%s", appid); out_ptr += strlen(appid)+1; } else if (!strncmp(buf, "Apptype: ", 9)) { memset(apptype, 0, sizeof(apptype)); - sscanf(buf, "Apptype: %s", apptype); + sscanf(buf, "Apptype: %127s", apptype); snprintf(out_buf+out_ptr, sizeof(out_buf)-out_ptr, ":%s", apptype); out_ptr += strlen(apptype)+1; -- 2.7.4 From dfb2d193c82a54bbb9647be30be03b49586061c5 Mon Sep 17 00:00:00 2001 From: Sooyoung Ha Date: Thu, 20 Apr 2017 20:28:00 +0900 Subject: [PATCH 06/16] package: update version (3.0.23) Change-Id: I2ed26fbaf8c2fa13936e1a7efb9a0b1c263ca63b Signed-off-by: Sooyoung Ha --- packaging/sdbd.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/sdbd.spec b/packaging/sdbd.spec index d353b13..e594ed7 100644 --- a/packaging/sdbd.spec +++ b/packaging/sdbd.spec @@ -2,7 +2,7 @@ Name: sdbd Summary: SDB daemon -Version: 3.0.22 +Version: 3.0.23 Release: 0 License: Apache-2.0 Summary: SDB daemon -- 2.7.4 From 59ee84abdf93edd551fd2c274bdf53ada93310ad Mon Sep 17 00:00:00 2001 From: Munkyu Im Date: Mon, 24 Apr 2017 18:20:57 +0900 Subject: [PATCH 07/16] sync: fix excluded files issue when execute "sdb pull" command need 1 length more to handle null terminated. Change-Id: I0da67c726cc2b68d636faac42c46c860c2ec892b Signed-off-by: Munkyu Im --- src/file_sync_service.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/file_sync_service.c b/src/file_sync_service.c index d4d35eb..f2da4c1 100644 --- a/src/file_sync_service.c +++ b/src/file_sync_service.c @@ -220,7 +220,7 @@ static int do_list(int s, const char *path) continue; } - s_strncpy(fname, de->d_name, len); + s_strncpy(fname, de->d_name, len + 1); if(lstat(tmp, &st) == 0) { msg.dent.mode = htoll(st.st_mode); msg.dent.size = htoll(st.st_size); -- 2.7.4 From e6c3665bd4b4b73706b5a6e3ab7d7e0a1b52b82f Mon Sep 17 00:00:00 2001 From: Sooyoung Ha Date: Mon, 24 Apr 2017 23:31:23 +0900 Subject: [PATCH 08/16] package: update version (3.0.24) Change-Id: I17e56843a94725176eee0982414e7f2435ed4795 Signed-off-by: Sooyoung Ha --- packaging/sdbd.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/sdbd.spec b/packaging/sdbd.spec index e594ed7..16f2830 100644 --- a/packaging/sdbd.spec +++ b/packaging/sdbd.spec @@ -2,7 +2,7 @@ Name: sdbd Summary: SDB daemon -Version: 3.0.23 +Version: 3.0.24 Release: 0 License: Apache-2.0 Summary: SDB daemon -- 2.7.4 From 4ac7e0127bc9ee267835430b26bfd887cf386e26 Mon Sep 17 00:00:00 2001 From: Jinhyung Choi Date: Thu, 20 Apr 2017 15:08:44 +0900 Subject: [PATCH 09/16] misc: avoid potential buffer overflow Change-Id: Ib87bfb9ee6a3f1a0b3b3cd3d65f460e0ceea3338 Signed-off-by: Jinhyung Choi (cherry picked from commit efe0ed6319e6f273dd78aadcac445a99524691ba) --- src/sdktools.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sdktools.c b/src/sdktools.c index ac7d21e..755625a 100644 --- a/src/sdktools.c +++ b/src/sdktools.c @@ -90,7 +90,7 @@ static int get_application_install_path(char* pkg_path) { while(ret_str[--len]=='\n'); ret_str[len + 1] = '\0'; - if (sscanf(ret_str, "Tizen Application Installation Path: %s", pkg_path) != 1) { + if (sscanf(ret_str, "Tizen Application Installation Path: %4095s", pkg_path) != 1) { D("failed : parsing fail (str:%s)\n", ret_str); return 0; } -- 2.7.4 From d2d4514fb32cc7dca021ac4162d0dc22b09e6c13 Mon Sep 17 00:00:00 2001 From: Jinhyung Choi Date: Wed, 10 May 2017 14:04:37 +0900 Subject: [PATCH 10/16] package: update version (3.0.25) Change-Id: I40f480f3279054c663d46c0bc2f5507ff4e549c2 Signed-off-by: Jinhyung Choi --- packaging/sdbd.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/sdbd.spec b/packaging/sdbd.spec index 16f2830..fe45995 100644 --- a/packaging/sdbd.spec +++ b/packaging/sdbd.spec @@ -2,7 +2,7 @@ Name: sdbd Summary: SDB daemon -Version: 3.0.24 +Version: 3.0.25 Release: 0 License: Apache-2.0 Summary: SDB daemon -- 2.7.4 From a16797abdc35b105d38beaf9aaccab12b616933d Mon Sep 17 00:00:00 2001 From: Sooyoung Ha Date: Wed, 24 May 2017 17:08:05 +0900 Subject: [PATCH 11/16] packaging: modify user and group of service Change-Id: I674a6fe5ff4311b991f83d5e89b3da0b982b123f Signed-off-by: Sooyoung Ha --- packaging/sdbd.spec | 4 ++-- packaging/sdbd_device_tv.service | 2 ++ packaging/sdbd_emulator_tv.service | 2 ++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packaging/sdbd.spec b/packaging/sdbd.spec index fe45995..de1b554 100644 --- a/packaging/sdbd.spec +++ b/packaging/sdbd.spec @@ -156,11 +156,11 @@ mv %{_unitdir}/sdbd.service %{_unitdir}/sdbd.service.tv.emulator %endif // ifarch %{ix86} x86_64 %post profile_tv -pushd {%_unitdir} +pushd %{_unitdir} mv sdbd.service.tv sdbd.service popd %preun profile_tv -pushd {%_unitdir} +pushd %{_unitdir} cp sdbd.service sdbd.service.tv echo For correct bahavior of %{name} after uninstalling %{name}-profile_tv, you need to reinstall %{name} popd diff --git a/packaging/sdbd_device_tv.service b/packaging/sdbd_device_tv.service index fe3c965..b683d96 100644 --- a/packaging/sdbd_device_tv.service +++ b/packaging/sdbd_device_tv.service @@ -4,6 +4,8 @@ Requires=tizen-system-env.service After=tmp.mount [Service] +User=sdk +Group=sdk Type=forking #location of SDBD log file #Environment=SDBD_LOG_PATH=/tmp diff --git a/packaging/sdbd_emulator_tv.service b/packaging/sdbd_emulator_tv.service index 4d81fd2..0590499 100644 --- a/packaging/sdbd_emulator_tv.service +++ b/packaging/sdbd_emulator_tv.service @@ -5,6 +5,8 @@ After=tmp.mount dbus.service #DefaultDependencies=false [Service] +User=sdk +Group=sdk Type=forking #location of SDBD log file #Environment=SDBD_LOG_PATH=/tmp -- 2.7.4 From 95a2525d56d17d482213c80ab9fe914dbc1a4b93 Mon Sep 17 00:00:00 2001 From: Sooyoung Ha Date: Wed, 24 May 2017 17:09:08 +0900 Subject: [PATCH 12/16] package: update version (3.0.26) Change-Id: If6b898bb10d5b82717a0da8fcbe82ed080eab424 Signed-off-by: Sooyoung Ha --- packaging/sdbd.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/sdbd.spec b/packaging/sdbd.spec index de1b554..f1177e7 100644 --- a/packaging/sdbd.spec +++ b/packaging/sdbd.spec @@ -2,7 +2,7 @@ Name: sdbd Summary: SDB daemon -Version: 3.0.25 +Version: 3.0.26 Release: 0 License: Apache-2.0 Summary: SDB daemon -- 2.7.4 From 3ce84f61e407112f1758d8288f097fd80ce44ce2 Mon Sep 17 00:00:00 2001 From: Sooyoung Ha Date: Tue, 16 May 2017 14:19:34 +0900 Subject: [PATCH 13/16] source: remove redundant codes There were so many legacy sdb-server source codes on sdb-daemon repository. These could disturb a developer in code reading. Change-Id: Ida03e0eee34081246c917d044acd7d75e45441e4 Signed-off-by: Sooyoung Ha --- src/log.h | 5 - src/mutex_list.h | 3 - src/sdb.c | 438 ------------------------------------------- src/sdb.h | 46 +---- src/services.c | 120 +----------- src/socket_loopback_server.c | 2 - src/sockets.c | 102 ---------- src/transport.c | 261 -------------------------- src/transport_local.c | 200 -------------------- src/transport_usb.c | 23 --- 10 files changed, 3 insertions(+), 1197 deletions(-) diff --git a/src/log.h b/src/log.h index f237e15..e7aff88 100644 --- a/src/log.h +++ b/src/log.h @@ -47,21 +47,16 @@ typedef enum { #if SDB_TRACE -#if !SDB_HOST /* * When running inside the emulator, guest's sdbd can connect to 'sdb-debug' * qemud service that can display sdb trace messages (on condition that emulator * has been started with '-debug sdb' option). */ - /* Delivers a trace message to the emulator via QEMU pipe. */ void sdb_qemu_trace(const char* fmt, ...); /* Macro to use to send SDB trace messages to the emulator. */ #define DQ(...) sdb_qemu_trace(__VA_ARGS__) -#else -#define DQ(...) ((void)0) -#endif /* !SDB_HOST */ extern int sdb_trace_mask; extern unsigned char sdb_trace_output_count; diff --git a/src/mutex_list.h b/src/mutex_list.h index 3e55e9f..aaa6588 100644 --- a/src/mutex_list.h +++ b/src/mutex_list.h @@ -24,9 +24,6 @@ SDB_MUTEX(dns_lock) SDB_MUTEX(socket_list_lock) SDB_MUTEX(transport_lock) -#if SDB_HOST -SDB_MUTEX(local_transports_lock) -#endif SDB_MUTEX(usb_lock) // Sadly logging to /data/sdb/sdb-... is not thread safe. diff --git a/src/sdb.c b/src/sdb.c index 5c6b2d5..fd9114c 100644 --- a/src/sdb.c +++ b/src/sdb.c @@ -37,9 +37,7 @@ #include "log.h" #include "sdb.h" #include "strutils.h" -#if !SDB_HOST #include "commandline_sdbd.h" -#endif #include "utils.h" #include "sdktools.h" #include "plugin.h" @@ -49,10 +47,8 @@ #include "plugin_encrypt.h" #endif -#if !SDB_HOST #include #define SDB_PIDPATH "/tmp/.sdbd.pid" -#endif #include #include #include @@ -107,9 +103,7 @@ struct group_info g_default_groups[] = { int is_init_sdk_userinfo = 0; int is_pwlocked = 0; // 0 if unlocked, 1 otherwise -#if !SDB_HOST SdbdCommandlineArgs sdbd_commandline_args; -#endif static int is_support_usbproto(); static int is_support_sockproto(); @@ -123,9 +117,6 @@ void (*usb_kick)(usb_handle *h) = NULL; int g_is_emulator = -1; int is_emulator(void) { -#if SDB_HOST - return 0; -#else if (g_is_emulator >= 0) { return g_is_emulator; } else { @@ -133,7 +124,6 @@ int is_emulator(void) { } return sdbd_commandline_args.emulator.host != NULL; -#endif } int is_appid2pid_supported(void) { @@ -283,7 +273,6 @@ void sdb_trace_init(void) } } -#if !SDB_HOST /* * Implements SDB tracing inside the emulator. */ @@ -308,36 +297,6 @@ void sdb_trace_init(void) /* A handle to sdb-debug qemud service in the emulator. */ int sdb_debug_qemu = -1; -/* Initializes connection with the sdb-debug qemud service in the emulator. */ -#if 0 /* doen't support in Tizen */ -static int sdb_qemu_trace_init(void) -{ - char con_name[32]; - - if (sdb_debug_qemu >= 0) { - return 0; - } - - /* sdb debugging QEMUD service connection request. */ - snprintf(con_name, sizeof(con_name), "qemud:sdb-debug"); - sdb_debug_qemu = qemu_pipe_open(con_name); - return (sdb_debug_qemu >= 0) ? 0 : -1; -} - -void sdb_qemu_trace(const char* fmt, ...) -{ - va_list args; - va_start(args, fmt); - char msg[1024]; - - if (sdb_debug_qemu >= 0) { - vsnprintf(msg, sizeof(msg), fmt, args); - sdb_write(sdb_debug_qemu, msg, strlen(msg)); - } -} -#endif -#endif /* !SDB_HOST */ - apacket *get_apacket(void) { apacket *p = malloc(sizeof(apacket)); @@ -604,11 +563,6 @@ static void send_connect(atransport *t) cp->msg.data_length = strlen((char*) cp->data) + 1; send_packet(cp, t); -#if SDB_HOST - /* XXX why sleep here? */ - // allow the device some time to respond to the connect message - sdb_sleep_ms(1000); -#endif } void send_device_status() @@ -1228,8 +1182,6 @@ void start_logging(void) #endif } -#if !SDB_HOST - void start_device_log(void) { int fd; @@ -1314,144 +1266,6 @@ int daemonize(void) { return 0; } -#endif - -#if SDB_HOST -int launch_server(int server_port) -{ -#ifdef HAVE_WIN32_PROC - /* we need to start the server in the background */ - /* we create a PIPE that will be used to wait for the server's "OK" */ - /* message since the pipe handles must be inheritable, we use a */ - /* security attribute */ - HANDLE pipe_read, pipe_write; - SECURITY_ATTRIBUTES sa; - STARTUPINFO startup; - PROCESS_INFORMATION pinfo; - char program_path[ MAX_PATH ]; - int ret; - - sa.nLength = sizeof(sa); - sa.lpSecurityDescriptor = NULL; - sa.bInheritHandle = TRUE; - - /* create pipe, and ensure its read handle isn't inheritable */ - ret = CreatePipe( &pipe_read, &pipe_write, &sa, 0 ); - if (!ret) { - fprintf(stderr, "CreatePipe() failure, error %ld\n", GetLastError() ); - return -1; - } - - SetHandleInformation( pipe_read, HANDLE_FLAG_INHERIT, 0 ); - - ZeroMemory( &startup, sizeof(startup) ); - startup.cb = sizeof(startup); - startup.hStdInput = GetStdHandle( STD_INPUT_HANDLE ); - startup.hStdOutput = pipe_write; - startup.hStdError = GetStdHandle( STD_ERROR_HANDLE ); - startup.dwFlags = STARTF_USESTDHANDLES; - - ZeroMemory( &pinfo, sizeof(pinfo) ); - - /* get path of current program */ - GetModuleFileName( NULL, program_path, sizeof(program_path) ); - - ret = CreateProcess( - program_path, /* program path */ - "sdb fork-server server", - /* the fork-server argument will set the - debug = 2 in the child */ - NULL, /* process handle is not inheritable */ - NULL, /* thread handle is not inheritable */ - TRUE, /* yes, inherit some handles */ - DETACHED_PROCESS, /* the new process doesn't have a console */ - NULL, /* use parent's environment block */ - NULL, /* use parent's starting directory */ - &startup, /* startup info, i.e. std handles */ - &pinfo ); - - CloseHandle( pipe_write ); - - if (!ret) { - fprintf(stderr, "CreateProcess failure, error %ld\n", GetLastError() ); - CloseHandle( pipe_read ); - return -1; - } - - CloseHandle( pinfo.hProcess ); - CloseHandle( pinfo.hThread ); - - /* wait for the "OK\n" message */ - { - char temp[3]; - DWORD count; - - ret = ReadFile( pipe_read, temp, 3, &count, NULL ); - CloseHandle( pipe_read ); - if ( !ret ) { - fprintf(stderr, "could not read ok from SDB Server, error = %ld\n", GetLastError() ); - return -1; - } - if (count != 3 || temp[0] != 'O' || temp[1] != 'K' || temp[2] != '\n') { - fprintf(stderr, "SDB server didn't ACK\n" ); - return -1; - } - } -#elif defined(HAVE_FORKEXEC) - char path[PATH_MAX]; - int fd[2]; - - // set up a pipe so the child can tell us when it is ready. - // fd[0] will be parent's end, and fd[1] will get mapped to stderr in the child. - if (pipe(fd)) { - fprintf(stderr, "pipe failed in launch_server, errno: %d\n", errno); - return -1; - } - get_my_path(path, PATH_MAX); - pid_t pid = fork(); - if(pid < 0) return -1; - - if (pid == 0) { - // child side of the fork - - // redirect stderr to the pipe - // we use stderr instead of stdout due to stdout's buffering behavior. - sdb_close(fd[0]); - dup2(fd[1], STDERR_FILENO); - sdb_close(fd[1]); - - // child process - int result = execl(path, "sdb", "fork-server", "server", NULL); - // this should not return - fprintf(stderr, "OOPS! execl returned %d, errno: %d\n", result, errno); - } else { - // parent side of the fork - - char temp[3]; - - temp[0] = 'A'; temp[1] = 'B'; temp[2] = 'C'; - // wait for the "OK\n" message - sdb_close(fd[1]); - int ret = sdb_read(fd[0], temp, 3); - int saved_errno = errno; - sdb_close(fd[0]); - if (ret < 0) { - fprintf(stderr, "could not read ok from SDB Server, errno = %d\n", saved_errno); - return -1; - } - if (ret != 3 || temp[0] != 'O' || temp[1] != 'K' || temp[2] != '\n') { - fprintf(stderr, "SDB server didn't ACK\n" ); - return -1; - } - - setsid(); - } -#else -#error "cannot implement background server start on this platform" -#endif - return 0; -} -#endif /* Constructs a local name of form tcp:port. * target_str points to the target string, it's content will be overwritten. @@ -1463,7 +1277,6 @@ void build_local_name(char* target_str, size_t target_size, int server_port) snprintf(target_str, target_size, "tcp:%d", server_port); } -#if !SDB_HOST static void init_drop_privileges() { #ifdef _DROP_PRIVILEGE rootshell_mode = 0; @@ -1898,7 +1711,6 @@ static void init_sdk_requirements() { register_bootdone_cb(); } } -#endif /* !SDB_HOST */ static void init_capabilities(void) { int ret = -1; @@ -2187,7 +1999,6 @@ static void fork_child_handler(void) int sdb_main(int is_daemon, int server_port) { -#if !SDB_HOST check_emulator_or_device(); load_sdbd_plugin(); @@ -2207,7 +2018,6 @@ int sdb_main(int is_daemon, int server_port) umask(000); pthread_atfork(fork_prepare_handler, fork_parent_handler, fork_child_handler); -#endif atexit(sdb_cleanup); #ifdef HAVE_WIN32_PROC @@ -2219,18 +2029,6 @@ int sdb_main(int is_daemon, int server_port) init_transport_registration(); - -#if SDB_HOST - HOST = 1; - usb_init(); - local_init(DEFAULT_SDB_LOCAL_TRANSPORT_PORT); - - char local_name[30]; - build_local_name(local_name, sizeof(local_name), server_port); - if(install_listener(local_name, "*smartsocket*", NULL)) { - exit(1); - } -#else /* don't listen on a port (default 5037) if running in secure mode */ /* don't run as root if we are running in secure mode */ @@ -2313,7 +2111,6 @@ int sdb_main(int is_daemon, int server_port) init_jdwp(); D("sdb_main(): post init_jdwp()\n"); #endif -#endif if (is_daemon) { @@ -2338,105 +2135,6 @@ int sdb_main(int is_daemon, int server_port) return 0; } -#if SDB_HOST -void connect_device(char* host, char* buffer, int buffer_size) -{ - int port, fd; - char* portstr = strchr(host, ':'); - char hostbuf[100]; - char serial[100]; - - s_strncpy(hostbuf, host, sizeof(hostbuf) - 1); - if (portstr) { - if (portstr - host >= sizeof(hostbuf)) { - snprintf(buffer, buffer_size, "bad host name %s", host); - return; - } - // zero terminate the host at the point we found the colon - hostbuf[portstr - host] = 0; - if (sscanf(portstr + 1, "%d", &port) == 0) { - snprintf(buffer, buffer_size, "bad port number %s", portstr); - return; - } - } else { - port = DEFAULT_SDB_LOCAL_TRANSPORT_PORT; - } - - snprintf(serial, sizeof(serial), "%s:%d", hostbuf, port); - if (find_transport(serial)) { - snprintf(buffer, buffer_size, "already connected to %s", serial); - return; - } - - fd = socket_network_client(hostbuf, port, SOCK_STREAM); - if (fd < 0) { - snprintf(buffer, buffer_size, "unable to connect to %s", host); - return; - } - - D("client: connected on remote on fd %d\n", fd); - close_on_exec(fd); - disable_tcp_nagle(fd); - register_socket_transport(fd, serial, port, 0, NULL); - snprintf(buffer, buffer_size, "connected to %s", serial); -} - -void connect_emulator(char* port_spec, char* buffer, int buffer_size) -{ - char* port_separator = strchr(port_spec, ','); - if (!port_separator) { - snprintf(buffer, buffer_size, - "unable to parse '%s' as ,", - port_spec); - return; - } - - // Zero-terminate console port and make port_separator point to 2nd port. - *port_separator++ = 0; - int console_port = strtol(port_spec, NULL, 0); - int sdb_port = strtol(port_separator, NULL, 0); - if (!(console_port > 0 && sdb_port > 0)) { - *(port_separator - 1) = ','; - snprintf(buffer, buffer_size, - "Invalid port numbers: Expected positive numbers, got '%s'", - port_spec); - return; - } - - /* Check if the emulator is already known. - * Note: There's a small but harmless race condition here: An emulator not - * present just yet could be registered by another invocation right - * after doing this check here. However, local_connect protects - * against double-registration too. From here, a better error message - * can be produced. In the case of the race condition, the very specific - * error message won't be shown, but the data doesn't get corrupted. */ - atransport* known_emulator = find_emulator_transport_by_sdb_port(sdb_port); - if (known_emulator != NULL) { - snprintf(buffer, buffer_size, - "Emulator on port %d already registered.", sdb_port); - return; - } - - /* Check if more emulators can be registered. Similar unproblematic - * race condition as above. */ - int candidate_slot = get_available_local_transport_index(); - if (candidate_slot < 0) { - snprintf(buffer, buffer_size, "Cannot accept more emulators."); - return; - } - - /* Preconditions met, try to connect to the emulator. */ - if (!local_connect_arbitrary_ports(console_port, sdb_port, NULL)) { - snprintf(buffer, buffer_size, - "Connected to emulator on ports %d,%d", console_port, sdb_port); - } else { - snprintf(buffer, buffer_size, - "Could not connect to emulator on ports %d,%d", - console_port, sdb_port); - } -} -#endif - int copy_packet(apacket* dest, apacket* src) { if(dest == NULL) { @@ -2477,132 +2175,6 @@ int handle_host_request(char *service, transport_type ttype, char* serial, int r exit(0); } -#if SDB_HOST - // "transport:" is used for switching transport with a specified serial number - // "transport-usb:" is used for switching transport to the only USB transport - // "transport-local:" is used for switching transport to the only local transport - // "transport-any:" is used for switching transport to the only transport - if (!strncmp(service, "transport", strlen("transport"))) { - char* error_string = "unknown failure"; - transport_type type = kTransportAny; - - if (!strncmp(service, "transport-usb", strlen("transport-usb"))) { - type = kTransportUsb; - } else if (!strncmp(service, "transport-local", strlen("transport-local"))) { - type = kTransportLocal; - } else if (!strncmp(service, "transport-any", strlen("transport-any"))) { - type = kTransportAny; - } else if (!strncmp(service, "transport:", strlen("transport:"))) { - service += strlen("transport:"); - serial = service; - } - - transport = acquire_one_transport(CS_ANY, type, serial, &error_string); - - if (transport) { - s->transport = transport; - sdb_write(reply_fd, "OKAY", 4); - } else { - sendfailmsg(reply_fd, error_string); - } - return 1; - } - - // return a list of all connected devices - if (!strcmp(service, "devices")) { - char buffer[4096]; - memset(buf, 0, sizeof(buf)); - memset(buffer, 0, sizeof(buffer)); - D("Getting device list \n"); - list_transports(buffer, sizeof(buffer)); - snprintf(buf, sizeof(buf), "OKAY%04x%s",(unsigned)strlen(buffer),buffer); - D("Wrote device list \n"); - writex(reply_fd, buf, strlen(buf)); - return 0; - } - - // add a new TCP transport, device or emulator - if (!strncmp(service, "connect:", 8)) { - char buffer[4096]; - char* host = service + 8; - if (!strncmp(host, "emu:", 4)) { - connect_emulator(host + 4, buffer, sizeof(buffer)); - } else { - connect_device(host, buffer, sizeof(buffer)); - } - // Send response for emulator and device - snprintf(buf, sizeof(buf), "OKAY%04x%s",(unsigned)strlen(buffer), buffer); - writex(reply_fd, buf, strlen(buf)); - return 0; - } - - // remove TCP transport - if (!strncmp(service, "disconnect:", 11)) { - char buffer[4096]; - memset(buffer, 0, sizeof(buffer)); - char* serial = service + 11; - if (serial[0] == 0) { - // disconnect from all TCP devices - unregister_all_tcp_transports(); - } else { - char hostbuf[100]; - // assume port 26101 if no port is specified - if (!strchr(serial, ':')) { - snprintf(hostbuf, sizeof(hostbuf) - 1, "%s:26101", serial); - serial = hostbuf; - } - atransport *t = find_transport(serial); - - if (t) { - unregister_transport(t); - } else { - snprintf(buffer, sizeof(buffer), "No such device %s", serial); - } - } - - snprintf(buf, sizeof(buf), "OKAY%04x%s",(unsigned)strlen(buffer), buffer); - writex(reply_fd, buf, strlen(buf)); - return 0; - } - - // returns our value for SDB_SERVER_VERSION - if (!strcmp(service, "version")) { - char version[12]; - snprintf(version, sizeof version, "%04x", SDB_SERVER_VERSION); - snprintf(buf, sizeof buf, "OKAY%04x%s", (unsigned)strlen(version), version); - writex(reply_fd, buf, strlen(buf)); - return 0; - } - - if(!strncmp(service,"get-serialno",strlen("get-serialno"))) { - char *out = "unknown"; - transport = acquire_one_transport(CS_ANY, ttype, serial, NULL); - if (transport && transport->serial) { - out = transport->serial; - } - snprintf(buf, sizeof buf, "OKAY%04x%s",(unsigned)strlen(out),out); - writex(reply_fd, buf, strlen(buf)); - return 0; - } - // indicates a new emulator instance has started - if (!strncmp(service,"emulator:",9)) { /* tizen specific */ - char *tmp = strtok(service+9, DEVICEMAP_SEPARATOR); - int port = 0; - - if (tmp == NULL) { - port = atoi(service+9); - } else { - port = atoi(tmp); - tmp = strtok(NULL, DEVICEMAP_SEPARATOR); - if (tmp != NULL) { - local_connect(port, tmp); - } - } - local_connect(port, NULL); - return 0; - } -#endif // SDB_HOST - if(!strncmp(service,"forward:",8) || !strncmp(service,"killforward:",12)) { char *local, *remote, *err; int r; @@ -2658,17 +2230,10 @@ int handle_host_request(char *service, transport_type ttype, char* serial, int r return -1; } -#if !SDB_HOST int recovery_mode = 0; -#endif int main(int argc, char **argv) { -#if SDB_HOST - sdb_sysdeps_init(); - sdb_trace_init(); - return sdb_commandline(argc - 1, argv + 1); -#else /* If sdbd runs inside the emulator this will enable sdb tracing via * sdb-debug qemud service in the emulator. */ #if 0 /* tizen specific */ @@ -2697,16 +2262,13 @@ int main(int argc, char **argv) return EXIT_FAILURE; } -#if !SDB_HOST if (daemonize() < 0) fatal("daemonize() failed: errno:%d", errno); -#endif D("Handling main()\n"); //sdbd will never die on emulator! signal(SIGTERM, handle_sig_term); /* tizen specific */ return sdb_main(0, DEFAULT_SDB_PORT); -#endif } diff --git a/src/sdb.h b/src/sdb.h index 0fe7005..12b3105 100644 --- a/src/sdb.h +++ b/src/sdb.h @@ -23,9 +23,7 @@ #include "transport.h" /* readx(), writex() */ #include "fdevent.h" -#if !SDB_HOST #include "commandline_sdbd.h" -#endif #include #define MAX_PAYLOAD_V1 (4*1024) @@ -351,9 +349,6 @@ void run_transport_disconnects( atransport* t ); void kick_transport( atransport* t ); /* initialize a transport object's func pointers and state */ -#if SDB_HOST -int get_available_local_transport_index(); -#endif int init_socket_transport(atransport *t, int s, int port, int local); void init_usb_transport(atransport *t, usb_handle *usb, int state); @@ -373,23 +368,14 @@ void register_usb_transport(usb_handle *h, const char *serial, unsigned writeabl void unregister_usb_transport(usb_handle *usb); atransport *find_transport(const char *serial); -#if SDB_HOST -atransport* find_emulator_transport_by_sdb_port(int sdb_port); -#endif int service_to_fd(const char *name); -#if SDB_HOST -asocket *host_service_to_socket(const char* name, const char *serial); -#endif -#if !SDB_HOST int init_jdwp(void); asocket* create_jdwp_service_socket(); asocket* create_jdwp_tracker_service_socket(); int create_jdwp_connection_fd(int jdwp_pid); -#endif -#if !SDB_HOST typedef enum { BACKUP, RESTORE @@ -429,8 +415,6 @@ extern gid_t g_root_group_id; extern char* g_root_home_dir; extern char* g_root_home_dir_env; -#endif - int should_drop_privileges(void); void send_device_status(); int set_sdk_user_privileges(int is_drop_capability_after_fork); @@ -477,15 +461,6 @@ int local_connect(int port, const char *device_name); int local_connect_arbitrary_ports(int console_port, int sdb_port, const char *device_name); /* usb host/client interface */ -#if SDB_HOST -void usb_init(); -void usb_cleanup(); -int usb_write(usb_handle *h, const void *data, int len); -int usb_read(usb_handle *h, void *data, size_t len); -int usb_close(usb_handle *h); -void usb_kick(usb_handle *h); -#else - extern void (*usb_init)(); extern void (*usb_cleanup)(); extern int (*usb_write)(usb_handle *h, const void *data, int len); @@ -509,13 +484,6 @@ int linux_usb_read(usb_handle *h, void *data, size_t len); int linux_usb_close(usb_handle *h); void linux_usb_kick(usb_handle *h); -#endif - -/* used for USB device detection */ -#if SDB_HOST -int is_sdb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol); -#endif - unsigned host_to_le32(unsigned n); int sdb_commandline(int argc, char **argv); @@ -533,9 +501,7 @@ int connection_state(atransport *t); extern int HOST; extern int SHELL_EXIT_NOTIFY_FD; -#if !SDB_HOST extern SdbdCommandlineArgs sdbd_commandline_args; -#endif #define CHUNK_SIZE (64*1024) #define SDBD_SHELL_CMD_MAX 4096 @@ -547,17 +513,6 @@ int copy_packet(apacket* dest, apacket* src); int is_emulator(void); #define DEFAULT_DEVICENAME "unknown" -#if SDB_HOST /* tizen-specific */ -#define DEVICEMAP_SEPARATOR ":" -#define DEVICENAME_MAX 256 -#define VMS_PATH OS_PATH_SEPARATOR_STR "vms" OS_PATH_SEPARATOR_STR // should include sysdeps.h above - -void register_device_name(const char *device_type, const char *device_name, int port); -int get_devicename_from_shdmem(int port, char *device_name); -int read_line(const int fd, char* ptr, const size_t maxlen); -#endif -#endif - #define USB_FUNCFS_SDB_PATH "/dev/usbgadget/sdb" #define USB_NODE_FILE "/dev/samsung_sdb" int create_subprocess(const char *cmd, pid_t *pid, char * const argv[], char * const envp[]); @@ -566,3 +521,4 @@ void get_env(char *key, char **env); #define RESERVE_CAPABILITIES_AFTER_FORK 0 #define DROP_CAPABILITIES_AFTER_FORK 1 +#endif diff --git a/src/services.c b/src/services.c index 1daaa43..2f4f0f6 100644 --- a/src/services.c +++ b/src/services.c @@ -29,16 +29,8 @@ #include "sdb.h" #include "file_sync_service.h" -#if SDB_HOST -# ifndef HAVE_WINSOCK -# include -# include -# include -# endif -#else -# include -# include "sdktools.h" -#endif +#include +#include "sdktools.h" #include #include @@ -75,28 +67,6 @@ void *service_bootstrap_func(void *x) return 0; } -#if SDB_HOST -SDB_MUTEX_DEFINE( dns_lock ); - -static void dns_service(int fd, void *cookie) -{ - char *hostname = cookie; - struct hostent *hp; - unsigned zero = 0; - - sdb_mutex_lock(&dns_lock); - hp = gethostbyname(hostname); - free(cookie); - if(hp == 0) { - writex(fd, &zero, 4); - } else { - writex(fd, hp->h_addr, 4); - } - sdb_mutex_unlock(&dns_lock); - sdb_close(fd); -} -#else - static int is_support_interactive_shell() { return (!strncmp(g_capabilities.intershell_support, PLUGIN_RET_ENABLED, strlen(PLUGIN_RET_ENABLED))); @@ -238,7 +208,6 @@ void reboot_service(int fd, void *arg) #endif } -#if !SDB_HOST #define EVENT_SIZE ( sizeof (struct inotify_event) ) #define BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) ) #define CS_PATH tzplatform_getenv(TZ_SYS_CRASH) @@ -304,8 +273,6 @@ done: sdb_close(fd); D( "inoti_service end\n"); } -#endif -#endif #if 0 static void echo_service(int fd, void *cookie) @@ -370,8 +337,6 @@ static int create_service_thread(void (*func)(int, void *), void *cookie) return s[0]; } -#if !SDB_HOST - static void redirect_and_exec(int pts, const char *cmd, char * const argv[], char * const envp[]) { dup2(pts, 0); @@ -604,14 +569,12 @@ int create_userprocess(const char *cmd, pid_t *pid, char * const argv[], char * return ptm; } } -#endif /* !SDB_HOST */ #define USER_DAEMON_COMMAND "/usr/sbin/sdbd-user" #define LOGIN_COMMAND "/bin/login" #define SUPER_USER "root" #define LOGIN_CONFIG "/etc/login.defs" -#if !SDB_HOST static void subproc_waiter_service(int fd, void *cookie) { pid_t pid = (pid_t)cookie; @@ -932,7 +895,6 @@ static int create_syncproc_thread() return ret_fd; } -#endif static void get_platforminfo(int fd, void *cookie) { pinfo sysinfo; @@ -1184,13 +1146,7 @@ int service_to_fd(const char *name) disable_tcp_nagle(ret); } } else { -#if SDB_HOST - sdb_mutex_lock(&dns_lock); - ret = socket_network_client(name + 1, port, SOCK_STREAM); - sdb_mutex_unlock(&dns_lock); -#else return -1; -#endif } #ifndef HAVE_WINSOCK /* winsock doesn't implement unix domain sockets */ } else if(!strncmp(name, "local:", 6)) { @@ -1206,12 +1162,6 @@ int service_to_fd(const char *name) ret = socket_local_client(name + 16, ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM); #endif -#if SDB_HOST - } else if(!strncmp("dns:", name, 4)){ - char *n = strdup(name + 4); - if(n == 0) return -1; - ret = create_service_thread(dns_service, n); -#else /* !SDB_HOST */ }/* else if(!strncmp("dev:", name, 4)) {// tizen specific ret = unix_open(name + 4, O_RDWR); } else if(!strncmp(name, "framebuffer:", 12)) { @@ -1257,7 +1207,6 @@ int service_to_fd(const char *name) ret = create_service_thread(rootshell_service, (void *)(service_name)); } else if(!strncmp(name, "cs:", 5)) { ret = create_service_thread(inoti_service, NULL); -#endif } else if(!strncmp(name, "sysinfo:", 8)){ ret = create_service_thread(get_platforminfo, 0); } else if(!strncmp(name, "capability:", 11)){ @@ -1285,68 +1234,3 @@ int service_to_fd(const char *name) } return ret; } - -#if SDB_HOST -struct state_info { - transport_type transport; - char* serial; - int state; -}; - -static void wait_for_state(int fd, void* cookie) -{ - struct state_info* sinfo = cookie; - char* err = "unknown error"; - - D("wait_for_state %d\n", sinfo->state); - - atransport *t = acquire_one_transport(sinfo->state, sinfo->transport, sinfo->serial, &err); - if(t != 0) { - writex(fd, "OKAY", 4); - } else { - sendfailmsg(fd, err); - } - - if (sinfo->serial) - free(sinfo->serial); - free(sinfo); - sdb_close(fd); - D("wait_for_state is done\n"); -} -#endif - -#if SDB_HOST -asocket* host_service_to_socket(const char* name, const char *serial) -{ - if (!strcmp(name,"track-devices")) { - return create_device_tracker(); - } else if (!strncmp(name, "wait-for-", strlen("wait-for-"))) { - struct state_info* sinfo = malloc(sizeof(struct state_info)); - - if (serial) - sinfo->serial = strdup(serial); - else - sinfo->serial = NULL; - - name += strlen("wait-for-"); - - if (!strncmp(name, "local", strlen("local"))) { - sinfo->transport = kTransportLocal; - sinfo->state = CS_DEVICE; - } else if (!strncmp(name, "usb", strlen("usb"))) { - sinfo->transport = kTransportUsb; - sinfo->state = CS_DEVICE; - } else if (!strncmp(name, "any", strlen("any"))) { - sinfo->transport = kTransportAny; - sinfo->state = CS_DEVICE; - } else { - free(sinfo); - return NULL; - } - - int fd = create_service_thread(wait_for_state, sinfo); - return create_local_socket(fd); - } - return NULL; -} -#endif /* SDB_HOST */ diff --git a/src/socket_loopback_server.c b/src/socket_loopback_server.c index b6c186a..73e5670 100644 --- a/src/socket_loopback_server.c +++ b/src/socket_loopback_server.c @@ -91,7 +91,6 @@ int socket_loopback_server(int port, int type) int cnt_max = 30; /* tizen specific */ -#if !SDB_HOST // check the loopback interface has been up in 30 sec while(cnt_max > 0) { if(get_loopback_status() == LOOPBACK_DOWN) { @@ -102,7 +101,6 @@ int socket_loopback_server(int port, int type) break; } } -#endif memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(port); diff --git a/src/sockets.c b/src/sockets.c index 71d646d..5496eb0 100644 --- a/src/sockets.c +++ b/src/sockets.c @@ -430,7 +430,6 @@ asocket *create_local_service_socket(const char *name) int fd; #if 0 /* not support in tizen */ -#if !SDB_HOST if (!strcmp(name,"jdwp")) { return create_jdwp_service_socket(); } @@ -438,7 +437,6 @@ asocket *create_local_service_socket(const char *name) return create_jdwp_tracker_service_socket(); } #endif -#endif fd = service_to_fd(name); if(fd < 0) return 0; @@ -448,22 +446,6 @@ asocket *create_local_service_socket(const char *name) return s; } -#if SDB_HOST -static asocket *create_host_service_socket(const char *name, const char* serial) -{ - asocket *s; - - s = host_service_to_socket(name, serial); - - if (s != NULL) { - D("LS(%d) bound to '%s'\n", s->id, name); - return s; - } - - return s; -} -#endif /* SDB_HOST */ - /* a Remote socket is used to send/receive data to/from a given transport object ** it needs to be closed when the transport is forcibly destroyed by the user */ @@ -647,11 +629,6 @@ char *skip_host_serial(char *service) { static int smart_socket_enqueue(asocket *s, apacket *p) { unsigned len; -#if SDB_HOST - char *service = NULL; - char* serial = NULL; - transport_type ttype = kTransportAny; -#endif D("SS(%d): enqueue %d\n", s->id, p->len); @@ -693,84 +670,6 @@ static int smart_socket_enqueue(asocket *s, apacket *p) D("SS(%d): '%s'\n", s->id, (char*) (p->data + 4)); -#if SDB_HOST - service = (char *)p->data + 4; - if(!strncmp(service, "host-serial:", strlen("host-serial:"))) { - char* serial_end; - service += strlen("host-serial:"); - - // serial number should follow "host:" and could be a host:port string. - serial_end = skip_host_serial(service); - if (serial_end) { - *serial_end = 0; // terminate string - serial = service; - service = serial_end + 1; - } - } else if (!strncmp(service, "host-usb:", strlen("host-usb:"))) { - ttype = kTransportUsb; - service += strlen("host-usb:"); - } else if (!strncmp(service, "host-local:", strlen("host-local:"))) { - ttype = kTransportLocal; - service += strlen("host-local:"); - } else if (!strncmp(service, "host:", strlen("host:"))) { - ttype = kTransportAny; - service += strlen("host:"); - } else { - service = NULL; - } - - if (service) { - asocket *s2; - - /* some requests are handled immediately -- in that - ** case the handle_host_request() routine has sent - ** the OKAY or FAIL message and all we have to do - ** is clean up. - */ - if(handle_host_request(service, ttype, serial, s->peer->fd, s) == 0) { - /* XXX fail message? */ - D( "SS(%d): handled host service '%s'\n", s->id, service ); - goto fail; - } - if (!strncmp(service, "transport", strlen("transport"))) { - D( "SS(%d): okay transport\n", s->id ); - p->len = 0; - return 0; - } - - /* try to find a local service with this name. - ** if no such service exists, we'll fail out - ** and tear down here. - */ - s2 = create_host_service_socket(service, serial); - if(s2 == 0) { - D( "SS(%d): couldn't create host service '%s'\n", s->id, service ); - sendfailmsg(s->peer->fd, "unknown host service"); - goto fail; - } - - /* we've connected to a local host service, - ** so we make our peer back into a regular - ** local socket and bind it to the new local - ** service socket, acknowledge the successful - ** connection, and close this smart socket now - ** that its work is done. - */ - sdb_write(s->peer->fd, "OKAY", 4); - - s->peer->ready = local_socket_ready; - s->peer->close = local_socket_close; - s->peer->peer = s2; - s2->peer = s->peer; - s->peer = 0; - D( "SS(%d): okay\n", s->id ); - s->close(s); - - /* initial state is "ready" */ - s2->ready(s2); - return 0; - } -#else /* !SDB_HOST */ if (s->transport == NULL) { char* error_string = "unknown failure"; s->transport = acquire_one_transport (CS_ANY, @@ -781,7 +680,6 @@ static int smart_socket_enqueue(asocket *s, apacket *p) goto fail; } } -#endif if(!(s->transport) || (s->transport->connection_state == CS_OFFLINE)) { /* if there's no remote we fail the connection diff --git a/src/transport.c b/src/transport.c index 8d889e3..f8ee0e6 100644 --- a/src/transport.c +++ b/src/transport.c @@ -418,153 +418,10 @@ static int transport_registration_recv = -1; static fdevent transport_registration_fde; -#if SDB_HOST -static int list_transports_msg(char* buffer, size_t bufferlen) -{ - char head[5]; - int len; - - len = list_transports(buffer+4, bufferlen-4); - snprintf(head, sizeof(head), "%04x", len); - memcpy(buffer, head, 4); - len += 4; - return len; -} - -/* this adds support required by the 'track-devices' service. - * this is used to send the content of "list_transport" to any - * number of client connections that want it through a single - * live TCP connection - */ -typedef struct device_tracker device_tracker; -struct device_tracker { - asocket socket; - int update_needed; - device_tracker* next; -}; - -/* linked list of all device trackers */ -static device_tracker* device_tracker_list; - -static void -device_tracker_remove( device_tracker* tracker ) -{ - device_tracker** pnode = &device_tracker_list; - device_tracker* node = *pnode; - - sdb_mutex_lock( &transport_lock ); - while (node) { - if (node == tracker) { - *pnode = node->next; - break; - } - pnode = &node->next; - node = *pnode; - } - sdb_mutex_unlock( &transport_lock ); -} - -static void -device_tracker_close( asocket* socket ) -{ - device_tracker* tracker = (device_tracker*) socket; - asocket* peer = socket->peer; - - D( "device tracker %p removed\n", tracker); - if (peer) { - peer->peer = NULL; - peer->close(peer); - } - device_tracker_remove(tracker); - free(tracker); -} - -static int -device_tracker_enqueue( asocket* socket, apacket* p ) -{ - /* you can't read from a device tracker, close immediately */ - put_apacket(p); - device_tracker_close(socket); - return -1; -} - -static int -device_tracker_send( device_tracker* tracker, - const char* buffer, - int len ) -{ - apacket* p = get_apacket(); - asocket* peer = tracker->socket.peer; - - memcpy(p->data, buffer, len); - p->len = len; - return peer->enqueue( peer, p ); -} - - -static void -device_tracker_ready( asocket* socket ) -{ - device_tracker* tracker = (device_tracker*) socket; - - /* we want to send the device list when the tracker connects - * for the first time, even if no update occured */ - if (tracker->update_needed > 0) { - char buffer[1024]; - int len; - - tracker->update_needed = 0; - - len = list_transports_msg(buffer, sizeof(buffer)); - device_tracker_send(tracker, buffer, len); - } -} - - -asocket* -create_device_tracker(void) -{ - device_tracker* tracker = calloc(1,sizeof(*tracker)); - - if(tracker == 0) fatal("cannot allocate device tracker"); - - D( "device tracker %p created\n", tracker); - - tracker->socket.enqueue = device_tracker_enqueue; - tracker->socket.ready = device_tracker_ready; - tracker->socket.close = device_tracker_close; - tracker->update_needed = 1; - - tracker->next = device_tracker_list; - device_tracker_list = tracker; - - return &tracker->socket; -} - - -/* call this function each time the transport list has changed */ -void update_transports(void) -{ - char buffer[1024]; - int len; - device_tracker* tracker; - - len = list_transports_msg(buffer, sizeof(buffer)); - - tracker = device_tracker_list; - while (tracker != NULL) { - device_tracker* next = tracker->next; - /* note: this may destroy the tracker if the connection is closed */ - device_tracker_send(tracker, buffer, len); - tracker = next; - } -} -#else void update_transports(void) { // nothing to do on the device side } -#endif // SDB_HOST typedef struct tmsg tmsg; struct tmsg @@ -878,51 +735,6 @@ atransport *acquire_one_transport(int state, transport_type ttype, const char* s return result; } -#if SDB_HOST -int list_transports(char *buf, size_t bufsize) -{ - char* p = buf; - char* end = buf + bufsize; - int len; - atransport *t; - - /* XXX OVERRUN PROBLEMS XXX */ - sdb_mutex_lock(&transport_lock); - for(t = transport_list.next; t != &transport_list; t = t->next) { - const char* serial = t->serial; - const char* devicename = (t->device_name == NULL) ? DEFAULT_DEVICENAME : t->device_name; /* tizen specific */ - if (!serial || !serial[0]) - serial = "????????????"; - len = snprintf(p, end - p, "%s\t%s\t%s\n", serial, statename(t), devicename); - - if (p + len >= end) { - /* discard last line if buffer is too short */ - break; - } - p += len; - } - p[0] = 0; - sdb_mutex_unlock(&transport_lock); - return p - buf; -} - - -/* hack for osx */ -void close_usb_devices() -{ - atransport *t; - - sdb_mutex_lock(&transport_lock); - for(t = transport_list.next; t != &transport_list; t = t->next) { - if ( !t->kicked ) { - t->kicked = 1; - t->kick(t); - } - } - sdb_mutex_unlock(&transport_lock); -} -#endif // SDB_HOST - void register_socket_transport(int s, const char *serial, int port, int local, const char *device_name) { atransport *t = calloc(1, sizeof(atransport)); @@ -940,14 +752,6 @@ void register_socket_transport(int s, const char *serial, int port, int local, c if ( init_socket_transport(t, s, port, local) < 0 ) { sdb_close(s); free(t); -#if SDB_HOST /* tizen specific */ - atransport *old_t = find_transport(serial); - if (old_t) { - unregister_transport(old_t); - } else { - D("No such device %s", serial); - } -#endif return; } @@ -979,74 +783,9 @@ void register_socket_transport(int s, const char *serial, int port, int local, c t->serial = strdup(serial); sdb_mutex_unlock(&transport_lock); -#if SDB_HOST /* tizen specific */ - if (device_name) {/* tizen specific */ - t->device_name = strdup(device_name); - } else { // device_name could be null when sdb server was forked before qemu has sent the connect message. - char device_name[DEVICENAME_MAX]; - if (get_devicename_from_shdmem(port, device_name) == 0) { - t->device_name = strdup(device_name); - } - } -#endif register_transport(t); } -#if SDB_HOST -atransport *find_transport(const char *serial) -{ - atransport *t; - - sdb_mutex_lock(&transport_lock); - for(t = transport_list.next; t != &transport_list; t = t->next) { - if (t->serial && !strcmp(serial, t->serial)) { - break; - } - } - sdb_mutex_unlock(&transport_lock); - - if (t != &transport_list) - return t; - else - return 0; -} - -void unregister_transport(atransport *t) -{ - sdb_mutex_lock(&transport_lock); - t->next->prev = t->prev; - t->prev->next = t->next; - sdb_mutex_unlock(&transport_lock); - - kick_transport(t); - transport_unref(t); -} - -// unregisters all non-emulator TCP transports -void unregister_all_tcp_transports() -{ - atransport *t, *next; - sdb_mutex_lock(&transport_lock); - for (t = transport_list.next; t != &transport_list; t = next) { - next = t->next; - if (t->type == kTransportLocal && t->sdb_port == 0) { - t->next->prev = t->prev; - t->prev->next = next; - // we cannot call kick_transport when holding transport_lock - if (!t->kicked) - { - t->kicked = 1; - t->kick(t); - } - transport_unref_locked(t); - } - } - - sdb_mutex_unlock(&transport_lock); -} - -#endif - int get_connected_count(transport_type type) /* tizen specific */ { int cnt = 0; diff --git a/src/transport_local.c b/src/transport_local.c index cae3d04..01f5c6c 100644 --- a/src/transport_local.c +++ b/src/transport_local.c @@ -35,9 +35,7 @@ #include "sdb.h" #include "strutils.h" -#if !SDB_HOST #include "commandline_sdbd.h" -#endif #include "utils.h" #include "sdbd_plugin.h" #include "plugin.h" @@ -57,18 +55,6 @@ static inline void fix_endians(apacket *p) #define fix_endians(p) do {} while (0) #endif -#if SDB_HOST -/* we keep a list of opened transports. The atransport struct knows to which - * local transport it is connected. The list is used to detect when we're - * trying to connect twice to a given local transport. - */ -#define SDB_LOCAL_TRANSPORT_MAX 16 - -SDB_MUTEX_DEFINE( local_transports_lock ); - -static atransport* local_transports[ SDB_LOCAL_TRANSPORT_MAX ]; -#endif /* SDB_HOST */ - SDB_MUTEX_DEFINE( register_noti_lock ); #ifndef _WIN32 static pthread_cond_t noti_cond = PTHREAD_COND_INITIALIZER; @@ -133,12 +119,6 @@ int local_connect_arbitrary_ports(int console_port, int sdb_port, const char *de char buf[64]; int fd = -1; -#if SDB_HOST - const char *host = getenv("SDBHOST"); - if (host) { - fd = socket_network_client(host, sdb_port, SOCK_STREAM); - } -#endif if (fd < 0) { fd = socket_loopback_client(sdb_port, SOCK_STREAM); } @@ -156,102 +136,8 @@ int local_connect_arbitrary_ports(int console_port, int sdb_port, const char *de return -1; } -#if SDB_HOST /* tizen specific */ -int get_devicename_from_shdmem(int port, char *device_name) -{ - char *vms = NULL; -#ifndef HAVE_WIN32_IPC - int shm_id; - void *shared_memory = (void *)0; - - shm_id = shmget( (key_t)port-1, 0, 0); - if (shm_id == -1) - return -1; - - shared_memory = shmat(shm_id, (void *)0, SHM_RDONLY); - - if (shared_memory == (void *)-1) - { - D("faild to get shdmem key (%d) : %s\n", port, strerror(errno)); - return -1; - } - - vms = strstr((char*)shared_memory, VMS_PATH); - if (vms != NULL) - s_strncpy(device_name, vms+strlen(VMS_PATH), DEVICENAME_MAX); - else - s_strncpy(device_name, DEFAULT_DEVICENAME, DEVICENAME_MAX); - -#else /* _WIN32*/ - HANDLE hMapFile; - char s_port[5]; - char* pBuf; - - sprintf(s_port, "%d", port-1); - hMapFile = OpenFileMapping(FILE_MAP_READ, TRUE, s_port); - - if(hMapFile == NULL) { - D("faild to get shdmem key (%ld) : %s\n", port, GetLastError() ); - return -1; - } - pBuf = (char*)MapViewOfFile(hMapFile, - FILE_MAP_READ, - 0, - 0, - 50); - if (pBuf == NULL) { - D("Could not map view of file (%ld)\n", GetLastError()); - CloseHandle(hMapFile); - return -1; - } - - vms = strstr((char*)pBuf, VMS_PATH); - if (vms != NULL) - s_strncpy(device_name, vms+strlen(VMS_PATH), DEVICENAME_MAX); - else - s_strncpy(device_name, DEFAULT_DEVICENAME, DEVICENAME_MAX); - CloseHandle(hMapFile); -#endif - D("init device name %s on port %d\n", device_name, port); - - return 0; -} - -int read_line(const int fd, char* ptr, size_t maxlen) -{ - unsigned int n = 0; - char c[2]; - int rc; - - while(n != maxlen) { - if((rc = sdb_read(fd, c, 1)) != 1) - return -1; // eof or read err - - if(*c == '\n') { - ptr[n] = 0; - return n; - } - ptr[n++] = *c; - } - return -1; // no space -} -#endif - static void *client_socket_thread(void *x) { -#if SDB_HOST - int port = DEFAULT_SDB_LOCAL_TRANSPORT_PORT; - int count = SDB_LOCAL_TRANSPORT_MAX; - - D("transport: client_socket_thread() starting\n"); - - /* try to connect to any number of running emulator instances */ - /* this is only done when SDB starts up. later, each new emulator */ - /* will send a message to SDB to indicate that is is starting up */ - for ( ; count > 0; count--, port += 10 ) { /* tizen specific */ - (void) local_connect(port, NULL); - } -#endif return 0; } @@ -320,7 +206,6 @@ static void *server_socket_thread(void * arg) } /* This is relevant only for SDB daemon running inside the emulator. */ -#if !SDB_HOST /* * Redefine open and write for qemu_pipe.h that contains inlined references * to those routines. We will redifine them back after qemu_pipe.h inclusion. @@ -429,7 +314,6 @@ static const char _ok_resp[] = "ok"; D("transport: qemu_socket_thread() exiting\n"); return 0; } -#endif // !SDB_HOST #endif int connect_nonb(int sockfd, const struct sockaddr *saptr, socklen_t salen, @@ -607,16 +491,12 @@ void local_init(int port) if(HOST) { func = client_socket_thread; } else { -#if SDB_HOST - func = server_socket_thread; -#else /* For the sdbd daemon in the system image we need to distinguish * between the device, and the emulator. */ { /* Running inside the device: use TCP socket as the transport. */ func = server_socket_thread; } -#endif // !SDB_HOST } D("transport: local %s init\n", HOST ? "client" : "server"); @@ -650,19 +530,6 @@ static void remote_kick(atransport *t) sdb_shutdown(fd); sdb_close(fd); -#if SDB_HOST - if(HOST) { - int nn; - sdb_mutex_lock( &local_transports_lock ); - for (nn = 0; nn < SDB_LOCAL_TRANSPORT_MAX; nn++) { - if (local_transports[nn] == t) { - local_transports[nn] = NULL; - break; - } - } - sdb_mutex_unlock( &local_transports_lock ); - } -#endif } static void remote_close(atransport *t) @@ -670,49 +537,6 @@ static void remote_close(atransport *t) sdb_close(t->fd); } - -#if SDB_HOST -/* Only call this function if you already hold local_transports_lock. */ -atransport* find_emulator_transport_by_sdb_port_locked(int sdb_port) -{ - int i; - for (i = 0; i < SDB_LOCAL_TRANSPORT_MAX; i++) { - if (local_transports[i] && local_transports[i]->sdb_port == sdb_port) { - return local_transports[i]; - } - } - return NULL; -} - -atransport* find_emulator_transport_by_sdb_port(int sdb_port) -{ - sdb_mutex_lock( &local_transports_lock ); - atransport* result = find_emulator_transport_by_sdb_port_locked(sdb_port); - sdb_mutex_unlock( &local_transports_lock ); - return result; -} - -/* Only call this function if you already hold local_transports_lock. */ -int get_available_local_transport_index_locked() -{ - int i; - for (i = 0; i < SDB_LOCAL_TRANSPORT_MAX; i++) { - if (local_transports[i] == NULL) { - return i; - } - } - return -1; -} - -int get_available_local_transport_index() -{ - sdb_mutex_lock( &local_transports_lock ); - int result = get_available_local_transport_index_locked(); - sdb_mutex_unlock( &local_transports_lock ); - return result; -} -#endif - int init_socket_transport(atransport *t, int s, int sdb_port, int local) { int fail = 0; @@ -727,29 +551,5 @@ int init_socket_transport(atransport *t, int s, int sdb_port, int local) t->type = kTransportLocal; t->sdb_port = 0; -#if SDB_HOST - if (HOST && local) { - sdb_mutex_lock( &local_transports_lock ); - { - t->sdb_port = sdb_port; - atransport* existing_transport = - find_emulator_transport_by_sdb_port_locked(sdb_port); - int index = get_available_local_transport_index_locked(); - if (existing_transport != NULL) { - D("local transport for port %d already registered (%p)?\n", - sdb_port, existing_transport); - fail = -1; - } else if (index < 0) { - // Too many emulators. - D("cannot register more emulators. Maximum is %d\n", - SDB_LOCAL_TRANSPORT_MAX); - fail = -1; - } else { - local_transports[index] = t; - } - } - sdb_mutex_unlock( &local_transports_lock ); - } -#endif return fail; } diff --git a/src/transport_usb.c b/src/transport_usb.c index 96905bf..32789ec 100644 --- a/src/transport_usb.c +++ b/src/transport_usb.c @@ -119,28 +119,5 @@ void init_usb_transport(atransport *t, usb_handle *h, int state) t->type = kTransportUsb; t->usb = h; -#if SDB_HOST - HOST = 1; -#else HOST = 0; -#endif -} - -#if SDB_HOST -int is_sdb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol) -{ - unsigned i; - for (i = 0; i < vendorIdCount; i++) { - if (vid == vendorIds[i]) { - if (usb_class == SDB_CLASS && usb_subclass == SDB_SUBCLASS && - usb_protocol == SDB_PROTOCOL) { - return 1; - } - - return 0; - } - } - - return 0; } -#endif -- 2.7.4 From 9fc6d59ab1489742a56194f6fe5d9059a849c183 Mon Sep 17 00:00:00 2001 From: Sooyoung Ha Date: Fri, 2 Jun 2017 15:28:56 +0900 Subject: [PATCH 14/16] package: update version (3.0.27) Change-Id: I8d832462f0f0e600da4b143ec16252567e2d7177 Signed-off-by: Sooyoung Ha --- packaging/sdbd.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/sdbd.spec b/packaging/sdbd.spec index f1177e7..130e95c 100644 --- a/packaging/sdbd.spec +++ b/packaging/sdbd.spec @@ -2,7 +2,7 @@ Name: sdbd Summary: SDB daemon -Version: 3.0.26 +Version: 3.0.27 Release: 0 License: Apache-2.0 Summary: SDB daemon -- 2.7.4 From 27536534686cbe24901a8e2d13f7269bb733c866 Mon Sep 17 00:00:00 2001 From: Sooyoung Ha Date: Fri, 2 Jun 2017 15:27:57 +0900 Subject: [PATCH 15/16] source: add some defensive codes Change-Id: I41c728f258b75b0d8d03f0bd8526fda73103d901 Signed-off-by: Sooyoung Ha --- src/default_plugin_appcmd.c | 4 +++- src/services.c | 3 ++- src/sockets.c | 11 ++--------- src/transport.c | 6 +++--- src/usb_linux_client.c | 2 +- 5 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/default_plugin_appcmd.c b/src/default_plugin_appcmd.c index 62a0217..7c0405e 100644 --- a/src/default_plugin_appcmd.c +++ b/src/default_plugin_appcmd.c @@ -362,9 +362,11 @@ static void appcmd_receiver_packagelist(int fd_in, int fd_out) if (sub1 == NULL) { continue; } - sub1 = strstr(sub1, "[")+1; + sub1 = strstr(sub1, "["); if (sub1 == NULL) { continue; + } else { + sub1++; } sub2 = strstr(sub1, "]"); if (sub2 == NULL) { diff --git a/src/services.c b/src/services.c index 2f4f0f6..c12a8d7 100644 --- a/src/services.c +++ b/src/services.c @@ -243,7 +243,8 @@ void inoti_service(int fd, void *arg) D( "inoti read failed\n"); goto done; } - while (i >= 0 && i <= (length - EVENT_SIZE)) { + int payload = length >= EVENT_SIZE ? length - EVENT_SIZE : 0; + while (i >= 0 && i <= payload) { struct inotify_event *event = (struct inotify_event *) &buffer[i]; if (event->len) { if (event->mask & IN_CREATE) { diff --git a/src/sockets.c b/src/sockets.c index 5496eb0..f27affa 100644 --- a/src/sockets.c +++ b/src/sockets.c @@ -336,7 +336,7 @@ static void local_socket_event_func(int fd, unsigned ev, void *_s) while(avail > 0) { r = sdb_read(fd, x, avail); D("LS(%d): post sdb_read(fd=%d,...) r=%d (errno=%d) avail=%d\n", s->id, s->fd, r, r<0?errno:0, avail); - if(r > 0) { + if(r > 0 && r <= avail) { avail -= r; x += r; continue; @@ -353,18 +353,11 @@ static void local_socket_event_func(int fd, unsigned ev, void *_s) D("LS(%d): fd=%d post avail loop. r=%d is_eof=%d forced_eof=%d\n", s->id, s->fd, r, is_eof, s->fde.force_eof); -#ifdef SUPPORT_ENCRYPT //변경된 최대 패킷 크기로 코드 수정 if((avail == max_payload) || (s->peer == 0)) { - put_apacket(p); - } else { - p->len = max_payload - avail; -#else - if((avail == max_payload) || (s->peer == 0)) { put_apacket(p); } else { - p->len = max_payload - avail; -#endif + p->len = max_payload >= avail ? max_payload - avail : 0; r = s->peer->enqueue(s->peer, p); D("LS(%d): fd=%d post peer->enqueue(). r=%d\n", s->id, s->fd, r); diff --git a/src/transport.c b/src/transport.c index f8ee0e6..152a22a 100644 --- a/src/transport.c +++ b/src/transport.c @@ -437,9 +437,9 @@ transport_read_action(int fd, struct tmsg* m) size_t len = sizeof(*m); int r; - while(len > 0) { + while (len > 0) { r = sdb_read(fd, p, len); - if(r > 0) { + if (r > 0 && r <= len) { len -= r; p += r; } else { @@ -878,7 +878,7 @@ int readx(int fd, void *ptr, size_t len) D("readx: fd=%d wanted=%d\n", fd, len); while(len > 0) { r = sdb_read(fd, p, len); - if(r > 0) { + if(r > 0 && r <= len) { len -= r; p += r; } else { diff --git a/src/usb_linux_client.c b/src/usb_linux_client.c index efeff2c..ecea71a 100644 --- a/src/usb_linux_client.c +++ b/src/usb_linux_client.c @@ -112,7 +112,7 @@ int linux_usb_read(usb_handle *h, void *data, size_t len) return -1; } } - len -= n; + len = len >= n ? len - n : 0; data = ((char*) data) + n; } D("[ done fd=%d ]\n", h->fd); -- 2.7.4 From 186bea594d970b4d1dda03a8ca288df26fbc76d3 Mon Sep 17 00:00:00 2001 From: Sooyoung Ha Date: Mon, 12 Jun 2017 19:50:04 +0900 Subject: [PATCH 16/16] source: avoid potential buffer overflow of scanf Change-Id: I853dac8153041dc1786bb58f2058f31cba0a56e3 Signed-off-by: Sooyoung Ha --- src/default_plugin_appcmd.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/default_plugin_appcmd.c b/src/default_plugin_appcmd.c index 7c0405e..2302386 100644 --- a/src/default_plugin_appcmd.c +++ b/src/default_plugin_appcmd.c @@ -632,9 +632,9 @@ static void appcmd_receiver_packageinfo(int fd_in, int fd_out) } if (!strncmp(buf, "mainappid : ", 12)) { - sscanf(buf, "mainappid : %s", mainapp_id); + sscanf(buf, "mainappid : %127s", mainapp_id); } else if (!strncmp(buf, "Type: ", 6)) { - sscanf(buf, "Type: %s", type); + sscanf(buf, "Type: %127s", type); } else if (!strncmp(buf, "Removable: ", 11)) { sscanf(buf, "Removable: %d", &is_removable); } else if (strstr(buf, " is Running") != NULL) { -- 2.7.4