From 3ce84f61e407112f1758d8288f097fd80ce44ce2 Mon Sep 17 00:00:00 2001 From: Sooyoung Ha Date: Tue, 16 May 2017 14:19:34 +0900 Subject: [PATCH 01/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 02/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 03/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 04/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 From 981f183b8b4f9cf8aa6b8adc5c76f2589592dc9d Mon Sep 17 00:00:00 2001 From: Sooyoung Ha Date: Mon, 12 Jun 2017 20:17:09 +0900 Subject: [PATCH 05/16] package: update version (3.0.28) Change-Id: I040d55035076328d3317c47a92019f60fa86782b 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 130e95c..7ecd2ff 100644 --- a/packaging/sdbd.spec +++ b/packaging/sdbd.spec @@ -2,7 +2,7 @@ Name: sdbd Summary: SDB daemon -Version: 3.0.27 +Version: 3.0.28 Release: 0 License: Apache-2.0 Summary: SDB daemon -- 2.7.4 From 16774a1a86387718aa971249a464af99fd3e0053 Mon Sep 17 00:00:00 2001 From: Jinhyung Jo Date: Wed, 21 Jun 2017 20:18:48 +0900 Subject: [PATCH 06/16] source: remove build warning change format specifier for 'size_t': %d -> %zu, %x -> %zx remove -Wint-to-pointer-cast, -Wpointer-to-int-cast apply indentation to 'if' statement Change-Id: Id58a213d6ce8a8b43d19c2789327ec7b2716ab93 Signed-off-by: Jinhyung Jo --- src/default_plugin_appcmd.c | 24 ++++++++++++------------ src/sdb.c | 4 ++-- src/services.c | 6 +++--- src/sockets.c | 2 +- src/transport.c | 12 ++++++------ src/transport_local.c | 6 +++--- src/usb_funcfs_client.c | 2 +- src/usb_linux_client.c | 2 +- 8 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/default_plugin_appcmd.c b/src/default_plugin_appcmd.c index 2302386..8ffb4e3 100644 --- a/src/default_plugin_appcmd.c +++ b/src/default_plugin_appcmd.c @@ -68,7 +68,7 @@ static int appcmd_install_gen_shellcmd(appcmd_info* p_info) { int len = sizeof(p_info->shell_cmd); if (p_info->args_cnt != 5) { - D("failed to parse appcmd.(cnt=%d)\n", p_info->args_cnt); + D("failed to parse appcmd.(cnt=%zu)\n", p_info->args_cnt); return -1; } @@ -101,7 +101,7 @@ static int appcmd_uninstall_gen_shellcmd(appcmd_info* p_info) { int len = sizeof(p_info->shell_cmd); if (p_info->args_cnt != 2) { - D("failed to parse appcmd.(cnt=%d)\n", p_info->args_cnt); + D("failed to parse appcmd.(cnt=%zu)\n", p_info->args_cnt); return -1; } @@ -120,7 +120,7 @@ static int appcmd_runapp_gen_shellcmd(appcmd_info* p_info) { int len = sizeof(p_info->shell_cmd); if (p_info->args_cnt != 2) { - D("failed to parse appcmd.(cnt=%d)\n", p_info->args_cnt); + D("failed to parse appcmd.(cnt=%zu)\n", p_info->args_cnt); return -1; } @@ -150,7 +150,7 @@ static int appcmd_rununittestapp_gen_shellcmd(appcmd_info* p_info) { p_service = strtok_r(p_info->raw_command, ":", &ptr); p_appid = strtok_r(NULL, ":", &ptr); if (p_service == NULL || p_appid == NULL) { - D("failed to parse appcmd.(cnt=%d)\n", p_info->args_cnt); + D("failed to parse appcmd.(cnt=%zu)\n", p_info->args_cnt); return -1; } @@ -175,7 +175,7 @@ static int appcmd_killapp_gen_shellcmd(appcmd_info* p_info) { int len = sizeof(p_info->shell_cmd); if (p_info->args_cnt != 2) { - D("failed to parse appcmd.(cnt=%d)\n", p_info->args_cnt); + D("failed to parse appcmd.(cnt=%zu)\n", p_info->args_cnt); return -1; } @@ -194,7 +194,7 @@ static int appcmd_packagelist_gen_shellcmd(appcmd_info* p_info) { int len = sizeof(p_info->shell_cmd); if (p_info->args_cnt != 2) { - D("failed to parse appcmd.(cnt=%d)\n", p_info->args_cnt); + D("failed to parse appcmd.(cnt=%zu)\n", p_info->args_cnt); return -1; } @@ -213,7 +213,7 @@ static int appcmd_debugwebapp_gen_shellcmd(appcmd_info* p_info) { int len = sizeof(p_info->shell_cmd); if (p_info->args_cnt != 2) { - D("failed to parse appcmd.(cnt=%d)\n", p_info->args_cnt); + D("failed to parse appcmd.(cnt=%zu)\n", p_info->args_cnt); return -1; } @@ -236,7 +236,7 @@ static int appcmd_debugnativeapp_gen_shellcmd(appcmd_info* p_info) { int len = sizeof(p_info->shell_cmd); if (p_info->args_cnt != 5) { - D("failed to parse appcmd.(cnt=%d)\n", p_info->args_cnt); + D("failed to parse appcmd.(cnt=%zu)\n", p_info->args_cnt); return -1; } @@ -264,7 +264,7 @@ static int appcmd_appinfo_gen_shellcmd(appcmd_info* p_info) { int len = sizeof(p_info->shell_cmd); if (p_info->args_cnt != 2) { - D("failed to parse appcmd.(cnt=%d)\n", p_info->args_cnt); + D("failed to parse appcmd.(cnt=%zu)\n", p_info->args_cnt); return -1; } @@ -571,7 +571,7 @@ static void run_appcmd_packageinfo(appcmd_info* p_info) { p_info->exitcode = -1; if (p_info->args_cnt != 3) { - D("failed to parse appcmd.(cnt=%d)\n", p_info->args_cnt); + D("failed to parse appcmd.(cnt=%zu)\n", p_info->args_cnt); return; } @@ -596,7 +596,7 @@ static int appcmd_packageinfo_gen_shellcmd(appcmd_info* p_info) { int len = sizeof(p_info->shell_cmd); if (p_info->args_cnt != 2) { - D("failed to parse appcmd.(cnt=%d)\n", p_info->args_cnt); + D("failed to parse appcmd.(cnt=%zu)\n", p_info->args_cnt); return -1; } @@ -714,7 +714,7 @@ int appcmd_service( parameters* in, int out_fd ) { /* appcmd parameter data map * "service name:arg1:arg2:...:argN" */ info.args_cnt = tokenize(command, ":", info.args, MAX_TOKENS); - D("args_cnt=%d\n", info.args_cnt); + D("args_cnt=%zu\n", info.args_cnt); if (info.args_cnt < 1) { D("failed to parse appcmd for install. (%s)\n", command); info.exitcode = -1; diff --git a/src/sdb.c b/src/sdb.c index fd9114c..a83c3f7 100644 --- a/src/sdb.c +++ b/src/sdb.c @@ -791,7 +791,7 @@ static void update_version(atransport *t, int version, size_t payload) #endif t->protocol_version = min(version, A_VERSION); t->max_payload = min(payload, max_payload); - D("update transport version. version=%x, max_payload=%d\n", t->protocol_version, t->max_payload); + D("update transport version. version=%x, max_payload=%zu\n", t->protocol_version, t->max_payload); } void handle_packet(apacket *p, atransport *t) @@ -1398,7 +1398,7 @@ static int sdbd_set_groups(const char *name, int gid, struct group_info default_ D("group list : ngroups = %d\n", ngroups); group_ids = malloc((ngroups + default_groups_size) * sizeof(gid_t)); if (group_ids == NULL) { - D("failed to allocate group_ids(%d)\n", (ngroups + default_groups_size) * sizeof(gid_t)); + D("failed to allocate group_ids(%zu)\n", (ngroups + default_groups_size) * sizeof(gid_t)); return -1; } if (getgrouplist(name, gid, group_ids, &ngroups) == -1) { diff --git a/src/services.c b/src/services.c index c12a8d7..6c9d1d6 100644 --- a/src/services.c +++ b/src/services.c @@ -578,7 +578,7 @@ int create_userprocess(const char *cmd, pid_t *pid, char * const argv[], char * static void subproc_waiter_service(int fd, void *cookie) { - pid_t pid = (pid_t)cookie; + pid_t pid = (pid_t)((intptr_t)cookie); D("entered. fd=%d of pid=%d\n", fd, pid); for (;;) { @@ -820,7 +820,7 @@ static int create_subproc_thread(const char *name, int lines, int columns) sti = malloc(sizeof(stinfo)); if(sti == 0) fatal("cannot allocate stinfo"); sti->func = subproc_waiter_service; - sti->cookie = (void*)pid; + sti->cookie = (void*)((intptr_t)pid); sti->fd = ret_fd; if(sdb_thread_create( &t, service_bootstrap_func, sti)){ @@ -866,7 +866,7 @@ static int create_sync_subprocess(void (*func)(int, void *), void* cookie) { sti = malloc(sizeof(stinfo)); if(sti == 0) fatal("cannot allocate stinfo"); sti->func = subproc_waiter_service; - sti->cookie = (void*)pid; + sti->cookie = (void*)((intptr_t)pid); sti->fd = s[0]; if(sdb_thread_create( &t, service_bootstrap_func, sti)){ diff --git a/src/sockets.c b/src/sockets.c index f27affa..b81d6a3 100644 --- a/src/sockets.c +++ b/src/sockets.c @@ -335,7 +335,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); + D("LS(%d): post sdb_read(fd=%d,...) r=%d (errno=%d) avail=%zu\n", s->id, s->fd, r, r<0?errno:0, avail); if(r > 0 && r <= avail) { avail -= r; x += r; diff --git a/src/transport.c b/src/transport.c index 152a22a..855c02d 100644 --- a/src/transport.c +++ b/src/transport.c @@ -670,8 +670,8 @@ atransport *acquire_one_transport(int state, transport_type ttype, const char* s sdb_mutex_lock(&transport_lock); for (t = transport_list.next; t != &transport_list; t = t->next) { if (t->connection_state == CS_NOPERM) { - if (error_out) - *error_out = "insufficient permissions for device"; + if (error_out) + *error_out = "insufficient permissions for device"; continue; } @@ -875,7 +875,7 @@ int readx(int fd, void *ptr, size_t len) #if SDB_TRACE size_t len0 = len; #endif - D("readx: fd=%d wanted=%d\n", fd, len); + D("readx: fd=%d wanted=%zu\n", fd, len); while(len > 0) { r = sdb_read(fd, p, len); if(r > 0 && r <= len) { @@ -894,7 +894,7 @@ int readx(int fd, void *ptr, size_t len) } #if SDB_TRACE - D("readx: fd=%d wanted=%d got=%d\n", fd, len0, len0 - len); + D("readx: fd=%d wanted=%zu got=%zu\n", fd, len0, len0 - len); dump_hex( ptr, len0 ); #endif return 0; @@ -906,7 +906,7 @@ int writex(int fd, const void *ptr, size_t len) int r; #if SDB_TRACE - D("writex: fd=%d len=%d: ", fd, len); + D("writex: fd=%d len=%zu: ", fd, len); dump_hex( ptr, len ); #endif while(len > 0) { @@ -936,7 +936,7 @@ int check_header(apacket *p, atransport *t) } if(p->msg.data_length > t->max_payload) { - D("check_header(): %d > transport->max_payload(%d)\n", p->msg.data_length, t->max_payload); + D("check_header(): %d > transport->max_payload(%zu)\n", p->msg.data_length, t->max_payload); return -1; } diff --git a/src/transport_local.c b/src/transport_local.c index 01f5c6c..d1b569e 100644 --- a/src/transport_local.c +++ b/src/transport_local.c @@ -146,7 +146,7 @@ static void *server_socket_thread(void * arg) int serverfd, fd; struct sockaddr_in addr; socklen_t alen; - int port = (int)arg; + int port = (intptr_t)arg; D("transport: server_socket_thread() starting\n"); serverfd = -1; @@ -469,7 +469,7 @@ static void* notify_sdbd_startup_thread(void* ptr) { snprintf(request, sizeof request, "host:connect:%s:%d", guest_ip, DEFAULT_SDB_LOCAL_TRANSPORT_PORT); } D("[%s:%d] request:%s \n", __FUNCTION__, __LINE__, request); - snprintf(buffer, sizeof buffer, "%04x%s", strlen(request), request ); + snprintf(buffer, sizeof buffer, "%04zx%s", strlen(request), request ); if (send_msg_to_localhost_from_guest(host_ip, DEFAULT_SDB_PORT, buffer, 0) <0) { D("could not send sdbd noti request. it might sdb server has not been started yet.\n"); @@ -501,7 +501,7 @@ void local_init(int port) D("transport: local %s init\n", HOST ? "client" : "server"); - if(sdb_thread_create(&thr, func, (void *)port)) { + if(sdb_thread_create(&thr, func, (void *)((intptr_t)port))) { fatal_errno("cannot create local socket %s thread", HOST ? "client" : "server"); } diff --git a/src/usb_funcfs_client.c b/src/usb_funcfs_client.c index 241d368..3ca92a3 100644 --- a/src/usb_funcfs_client.c +++ b/src/usb_funcfs_client.c @@ -554,7 +554,7 @@ int ffs_usb_read(usb_handle *h, void *data, size_t len) { int n; - D("%d: about to read (fd=%d, len=%d)\n", getpid(), h->bulk_out, len); + D("%d: about to read (fd=%d, len=%zu)\n", getpid(), h->bulk_out, len); n = bulk_read(h->bulk_out, data, len); if(n != len) { D("ERROR: fd = %d, n = %d, errno = %d\n", diff --git a/src/usb_linux_client.c b/src/usb_linux_client.c index ecea71a..ea9d7e6 100644 --- a/src/usb_linux_client.c +++ b/src/usb_linux_client.c @@ -98,7 +98,7 @@ int linux_usb_write(usb_handle *h, const void *data, int len) int linux_usb_read(usb_handle *h, void *data, size_t len) { - D("about to read (fd=%d, len=%d)\n", h->fd, len); + D("about to read (fd=%d, len=%zu)\n", h->fd, len); while (len > 0) { /* The sdb_read does not support read larger than 4096 bytes at once. Read 4096 byte block repeatedly when reading data is larger than 4096 bytes. */ -- 2.7.4 From 5ae69c8554258b1ffd96ae597111209a6053c40c Mon Sep 17 00:00:00 2001 From: Jinhyung Jo Date: Wed, 21 Jun 2017 20:28:48 +0900 Subject: [PATCH 07/16] source: remove integer underflow Change-Id: Ie6987bb833fa1061409ccd3a8b4abddf4e281cc7 Signed-off-by: Jinhyung Jo --- src/sockets.c | 23 +++++++++++------------ src/transport.c | 17 ++++++++--------- src/usb_linux_client.c | 35 ++++++++++++++++++----------------- 3 files changed, 37 insertions(+), 38 deletions(-) diff --git a/src/sockets.c b/src/sockets.c index b81d6a3..9711613 100644 --- a/src/sockets.c +++ b/src/sockets.c @@ -324,37 +324,36 @@ static void local_socket_event_func(int fd, unsigned ev, void *_s) // sdbd에서 패킷 데이터를 MAX_PAYLOAD - 200로 잡아서 암호화 하게되면 // 최대 MAX_PAYLOAD - 100 크기의 패킷을 생성하게 됨. const size_t max_payload = asock_get_max_payload(s) - 200; - size_t avail = max_payload; #else const size_t max_payload = asock_get_max_payload(s); - size_t avail = max_payload; #endif - + int avail = max_payload; int r = 0; int is_eof = 0; - while(avail > 0) { + while (avail > 0) { r = sdb_read(fd, x, avail); - D("LS(%d): post sdb_read(fd=%d,...) r=%d (errno=%d) avail=%zu\n", s->id, s->fd, r, r<0?errno:0, avail); - if(r > 0 && r <= 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 && r <= avail) { avail -= r; x += r; continue; } - if(r < 0) { - if(errno == EAGAIN) break; - if(errno == EINTR) continue; + if (r < 0) { + if (errno == EAGAIN) break; + if (errno == EINTR) continue; } - /* r = 0 or unhandled error */ + /* r = 0 or unhandled error */ is_eof = 1; break; } 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); - //변경된 최대 패킷 크기로 코드 수정 - if((avail == max_payload) || (s->peer == 0)) { + //변경된 최대 패킷 크기로 코드 수정 + if ((avail == max_payload) || (s->peer == 0)) { put_apacket(p); } else { p->len = max_payload >= avail ? max_payload - avail : 0; diff --git a/src/transport.c b/src/transport.c index 855c02d..7eb9e95 100644 --- a/src/transport.c +++ b/src/transport.c @@ -434,7 +434,7 @@ static int transport_read_action(int fd, struct tmsg* m) { char *p = (char*)m; - size_t len = sizeof(*m); + int len = sizeof(*m); int r; while (len > 0) { @@ -872,14 +872,13 @@ int readx(int fd, void *ptr, size_t len) { char *p = ptr; int r; -#if SDB_TRACE - size_t len0 = len; -#endif + int l = len; + D("readx: fd=%d wanted=%zu\n", fd, len); - while(len > 0) { + while (l > 0) { r = sdb_read(fd, p, len); - if(r > 0 && r <= len) { - len -= r; + if (r > 0 && r <= l) { + l -= r; p += r; } else { if (r < 0) { @@ -894,8 +893,8 @@ int readx(int fd, void *ptr, size_t len) } #if SDB_TRACE - D("readx: fd=%d wanted=%zu got=%zu\n", fd, len0, len0 - len); - dump_hex( ptr, len0 ); + D("readx: fd=%d wanted=%zu got=%zu\n", fd, len, len - l); + dump_hex( ptr, len ); #endif return 0; } diff --git a/src/usb_linux_client.c b/src/usb_linux_client.c index ea9d7e6..a4840c9 100644 --- a/src/usb_linux_client.c +++ b/src/usb_linux_client.c @@ -98,25 +98,26 @@ int linux_usb_write(usb_handle *h, const void *data, int len) int linux_usb_read(usb_handle *h, void *data, size_t len) { - D("about to read (fd=%d, len=%zu)\n", h->fd, len); - while (len > 0) { - /* The sdb_read does not support read larger than 4096 bytes at once. - Read 4096 byte block repeatedly when reading data is larger than 4096 bytes. */ - int bytes_to_read = len < 4096 ? len : 4096; - int n = sdb_read(h->fd, data, bytes_to_read); - if(n < 0) { - if(errno == EINTR) { - continue; - } else { - D("ERROR: fd = %d, n = %d, errno = %d\n", h->fd, n, errno); - return -1; - } + D("about to read (fd=%d, len=%zu)\n", h->fd, len); + int l = len; + while (l > 0) { + /* The sdb_read does not support read larger than 4096 bytes at once. + Read 4096 byte block repeatedly when reading data is larger than 4096 bytes. */ + int bytes_to_read = l < 4096 ? l : 4096; + int n = sdb_read(h->fd, data, bytes_to_read); + if (n < 0) { + if (errno == EINTR) { + continue; + } else { + D("ERROR: fd = %d, n = %d, errno = %d\n", h->fd, n, errno); + return -1; } - len = len >= n ? len - n : 0; - data = ((char*) data) + n; } - D("[ done fd=%d ]\n", h->fd); - return 0; + l = (l >= n) ? l - n : 0; + data = ((char*)data) + n; + } + D("[ done fd=%d ]\n", h->fd); + return 0; } void linux_usb_init() -- 2.7.4 From 112c62399fc25c810de70bce171f54fefe5a0b51 Mon Sep 17 00:00:00 2001 From: Jinhyung Jo Date: Thu, 22 Jun 2017 17:15:06 +0900 Subject: [PATCH 08/16] package: update version (3.0.29) Change-Id: I99d417e1d2a513506f084b59c219466d14b98e4f Signed-off-by: Jinhyung Jo --- packaging/sdbd.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/sdbd.spec b/packaging/sdbd.spec index 7ecd2ff..8f5efed 100644 --- a/packaging/sdbd.spec +++ b/packaging/sdbd.spec @@ -2,7 +2,7 @@ Name: sdbd Summary: SDB daemon -Version: 3.0.28 +Version: 3.0.29 Release: 0 License: Apache-2.0 Summary: SDB daemon -- 2.7.4 From 26a2f3133488b8be6318540e5405a20d1c766ef9 Mon Sep 17 00:00:00 2001 From: Sooyoung Ha Date: Tue, 27 Jun 2017 22:31:06 +0900 Subject: [PATCH 09/16] dbus: add dbus_connection_unref Change-Id: Ic7c085a06da97f40c570b7be116a1236d5b222da Signed-off-by: Sooyoung Ha --- src/sdb.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/sdb.c b/src/sdb.c index a83c3f7..39ea547 100644 --- a/src/sdb.c +++ b/src/sdb.c @@ -1373,6 +1373,7 @@ static void *bootdone_cb(void *x) { g_main_loop_run(mainloop); D("dbus loop exited"); + dbus_connection_unref(bus); return NULL; } -- 2.7.4 From 01531ec45d3292cd266f41e3812e240d02d6c585 Mon Sep 17 00:00:00 2001 From: Sooyoung Ha Date: Tue, 27 Jun 2017 22:32:18 +0900 Subject: [PATCH 10/16] source: align indent I change the tabs to spaces and remove the redundant spaces. Change-Id: Iaed7c815f2d9e04582b1a68bc65d678261824378 Signed-off-by: Sooyoung Ha --- src/TizenConfig.h | 2 +- src/commandline_sdbd.c | 198 ++++++++--------- src/commandline_sdbd.h | 12 +- src/default_plugin_auth.c | 1 - src/fdevent.h | 3 +- src/file_sync_service.h | 4 +- src/init.c | 198 ++++++++--------- src/plugin_encrypt.c | 476 ++++++++++++++++++++--------------------- src/plugin_encrypt.h | 30 +-- src/sdb.c | 394 +++++++++++++++++----------------- src/sdb.h | 6 +- src/services.c | 2 +- src/socket_inaddr_any_server.c | 2 +- src/socket_local.h | 8 +- src/socket_local_server.c | 4 +- src/socket_loopback_server.c | 2 +- src/sockets.c | 10 +- src/sockets.h | 44 ++-- src/transport.c | 24 +-- src/transport_local.c | 8 +- src/transport_usb.c | 2 +- src/usb_funcfs_client.c | 78 +++---- src/utils.c | 152 ++++++------- 23 files changed, 829 insertions(+), 831 deletions(-) diff --git a/src/TizenConfig.h b/src/TizenConfig.h index 81d36f3..7da4f99 100644 --- a/src/TizenConfig.h +++ b/src/TizenConfig.h @@ -15,7 +15,7 @@ */ /* - * Android config -- "CYGWIN_NT-5.1". + * Android config -- "CYGWIN_NT-5.1". * * Cygwin has pthreads, but GDB seems to get confused if you use it to * create threads. By "confused", I mean it freezes up the first time the diff --git a/src/commandline_sdbd.c b/src/commandline_sdbd.c index 63850f3..eee6ac2 100644 --- a/src/commandline_sdbd.c +++ b/src/commandline_sdbd.c @@ -53,115 +53,115 @@ static void print_sdbd_command(FILE *stream, SdbdCommandlineArgs *sdbd_args) { } int parse_sdbd_commandline(SdbdCommandlineArgs *sdbd_args, int argc, char *argv[]) { - int split_retval; - - int opt; - int long_index = 0; - - static struct option long_options[] = { - { ARG_EMULATOR_VM_NAME, required_argument, NULL, ARG_S_EMULATOR_VM_NAME }, - { ARG_SENSORS, required_argument, NULL, ARG_S_SENSORS }, - { ARG_SDB, required_argument, NULL, ARG_S_SDB }, - { ARG_SDBD_LISTEN_PORT, required_argument, NULL, ARG_S_SDBD_LISTEN_PORT }, - { ARG_HELP, no_argument, NULL, ARG_S_HELP }, - { ARG_USAGE, no_argument, NULL, ARG_S_USAGE }, - { NULL, 0, NULL, 0 } - }; - - optind = 1; /* the index of the next element to be processed in argv */ - - while ((opt = getopt_long(argc, argv, "", long_options, &long_index)) != -1) { - switch (opt) { - case ARG_S_EMULATOR_VM_NAME: - split_retval = split_host_port(optarg, - &sdbd_args->emulator.host, - &sdbd_args->emulator.port); - if (split_retval != SDBD_COMMANDLINE_SUCCESS) { - return split_retval; - } - /* if we are on emulator we listen using local transport - * so we should set port to default value but this can - * be overwritten by command line options */ - if (sdbd_args->sdbd_port < 0) { - sdbd_args->sdbd_port = DEFAULT_SDB_LOCAL_TRANSPORT_PORT; - } - print_sdbd_command(stdout, sdbd_args); - break; - case ARG_S_SENSORS: - split_retval = split_host_port(optarg, - &sdbd_args->sensors.host, - &sdbd_args->sensors.port); - if (split_retval != SDBD_COMMANDLINE_SUCCESS) { - return split_retval; - } - print_sdbd_command(stdout, sdbd_args); - break; - case ARG_S_SDB: - split_retval = split_host_port(optarg, - &sdbd_args->sdb.host, - &sdbd_args->sdb.port); - if (split_retval != SDBD_COMMANDLINE_SUCCESS) { - return split_retval; - } - print_sdbd_command(stdout, sdbd_args); - break; - case ARG_S_SDBD_LISTEN_PORT: - if (sscanf(optarg, "%d", &sdbd_args->sdbd_port) < 1) { - return SDBD_COMMANDLINE_FAILURE; - } - print_sdbd_command(stdout, sdbd_args); - break; - case ARG_S_HELP: - return SDBD_COMMANDLINE_HELP; - case ARG_S_USAGE: - return SDBD_COMMANDLINE_USAGE; - case 1: - return SDBD_COMMANDLINE_FAILURE_UNKNOWN_OPT; - case '?': - return SDBD_COMMANDLINE_FAILURE_UNKNOWN_OPT; - default: - return SDBD_COMMANDLINE_FAILURE; - } - } - - print_sdbd_command(stdout, sdbd_args); - - return SDBD_COMMANDLINE_SUCCESS; + int split_retval; + + int opt; + int long_index = 0; + + static struct option long_options[] = { + { ARG_EMULATOR_VM_NAME, required_argument, NULL, ARG_S_EMULATOR_VM_NAME }, + { ARG_SENSORS, required_argument, NULL, ARG_S_SENSORS }, + { ARG_SDB, required_argument, NULL, ARG_S_SDB }, + { ARG_SDBD_LISTEN_PORT, required_argument, NULL, ARG_S_SDBD_LISTEN_PORT }, + { ARG_HELP, no_argument, NULL, ARG_S_HELP }, + { ARG_USAGE, no_argument, NULL, ARG_S_USAGE }, + { NULL, 0, NULL, 0 } + }; + + optind = 1; /* the index of the next element to be processed in argv */ + + while ((opt = getopt_long(argc, argv, "", long_options, &long_index)) != -1) { + switch (opt) { + case ARG_S_EMULATOR_VM_NAME: + split_retval = split_host_port(optarg, + &sdbd_args->emulator.host, + &sdbd_args->emulator.port); + if (split_retval != SDBD_COMMANDLINE_SUCCESS) { + return split_retval; + } + /* if we are on emulator we listen using local transport + * so we should set port to default value but this can + * be overwritten by command line options */ + if (sdbd_args->sdbd_port < 0) { + sdbd_args->sdbd_port = DEFAULT_SDB_LOCAL_TRANSPORT_PORT; + } + print_sdbd_command(stdout, sdbd_args); + break; + case ARG_S_SENSORS: + split_retval = split_host_port(optarg, + &sdbd_args->sensors.host, + &sdbd_args->sensors.port); + if (split_retval != SDBD_COMMANDLINE_SUCCESS) { + return split_retval; + } + print_sdbd_command(stdout, sdbd_args); + break; + case ARG_S_SDB: + split_retval = split_host_port(optarg, + &sdbd_args->sdb.host, + &sdbd_args->sdb.port); + if (split_retval != SDBD_COMMANDLINE_SUCCESS) { + return split_retval; + } + print_sdbd_command(stdout, sdbd_args); + break; + case ARG_S_SDBD_LISTEN_PORT: + if (sscanf(optarg, "%d", &sdbd_args->sdbd_port) < 1) { + return SDBD_COMMANDLINE_FAILURE; + } + print_sdbd_command(stdout, sdbd_args); + break; + case ARG_S_HELP: + return SDBD_COMMANDLINE_HELP; + case ARG_S_USAGE: + return SDBD_COMMANDLINE_USAGE; + case 1: + return SDBD_COMMANDLINE_FAILURE_UNKNOWN_OPT; + case '?': + return SDBD_COMMANDLINE_FAILURE_UNKNOWN_OPT; + default: + return SDBD_COMMANDLINE_FAILURE; + } + } + + print_sdbd_command(stdout, sdbd_args); + + return SDBD_COMMANDLINE_SUCCESS; } void apply_sdbd_commandline_defaults(SdbdCommandlineArgs *sdbd_args) { - sdbd_args->emulator.port = -1; + sdbd_args->emulator.port = -1; - sdbd_args->sensors.host = strdup(QEMU_FORWARD_IP); - sdbd_args->sensors.port = DEFAULT_SENSORS_LOCAL_TRANSPORT_PORT; + sdbd_args->sensors.host = strdup(QEMU_FORWARD_IP); + sdbd_args->sensors.port = DEFAULT_SENSORS_LOCAL_TRANSPORT_PORT; - sdbd_args->sdb.host = strdup(QEMU_FORWARD_IP); - sdbd_args->sdb.port = DEFAULT_SDB_PORT; + sdbd_args->sdb.host = strdup(QEMU_FORWARD_IP); + sdbd_args->sdb.port = DEFAULT_SDB_PORT; - // by default don't listen on local transport - sdbd_args->sdbd_port = -1; + // by default don't listen on local transport + sdbd_args->sdbd_port = -1; } int split_host_port(const char *optarg, char **host, int *port) { - const char *colon = strchr(optarg, ':'); - char *old_val = NULL; - - if (colon) { - old_val = *host; - *host = strndup(optarg, colon - optarg); - if (sscanf(colon + 1, "%d", port) < 1) { - return SDBD_COMMANDLINE_FAILURE; - } - } else { - return SDBD_COMMANDLINE_FAILURE; - } - - if (old_val) { - free(old_val); - } - return SDBD_COMMANDLINE_SUCCESS; + const char *colon = strchr(optarg, ':'); + char *old_val = NULL; + + if (colon) { + old_val = *host; + *host = strndup(optarg, colon - optarg); + if (sscanf(colon + 1, "%d", port) < 1) { + return SDBD_COMMANDLINE_FAILURE; + } + } else { + return SDBD_COMMANDLINE_FAILURE; + } + + if (old_val) { + free(old_val); + } + return SDBD_COMMANDLINE_SUCCESS; } @@ -175,7 +175,7 @@ void clear_sdbd_commandline_args(SdbdCommandlineArgs *sdbd_args) { free(sdbd_args->sensors.host); sdbd_args->sensors.host = NULL; - memset(sdbd_args, 0, sizeof(SdbdCommandlineArgs)); + memset(sdbd_args, 0, sizeof(SdbdCommandlineArgs)); } diff --git a/src/commandline_sdbd.h b/src/commandline_sdbd.h index 598b478..2b026ca 100644 --- a/src/commandline_sdbd.h +++ b/src/commandline_sdbd.h @@ -46,8 +46,8 @@ * @brief A simple host:port tuple */ typedef struct { - char *host; - int port; + char *host; + int port; } HostPort; /*! @@ -55,10 +55,10 @@ typedef struct { * @brief Contains all values, which are read from commandline. */ typedef struct { - HostPort emulator; ///< emulator name and forward port - HostPort sdb; ///< sdb address - HostPort sensors; ///< sensors address - int sdbd_port; ///< Port to listen on in tcp mode + HostPort emulator; ///< emulator name and forward port + HostPort sdb; ///< sdb address + HostPort sensors; ///< sensors address + int sdbd_port; ///< Port to listen on in tcp mode } SdbdCommandlineArgs; #include diff --git a/src/default_plugin_auth.c b/src/default_plugin_auth.c index 8fa743a..103603b 100644 --- a/src/default_plugin_auth.c +++ b/src/default_plugin_auth.c @@ -41,7 +41,6 @@ int auth_support ( parameters* in, parameters* out ) int auth_get_key_file_paths ( parameters* in, parameters* out ) { return PLUGIN_CMD_FAIL; - } int confirm_public_key( parameters* in, int out_fd ) diff --git a/src/fdevent.h b/src/fdevent.h index a6db9ea..d383c1f 100644 --- a/src/fdevent.h +++ b/src/fdevent.h @@ -64,8 +64,7 @@ void fdevent_set_timeout(fdevent *fde, int64_t timeout_ms); */ void fdevent_loop(); -struct fdevent -{ +struct fdevent { fdevent *next; fdevent *prev; diff --git a/src/file_sync_service.h b/src/file_sync_service.h index 94a52b6..089a212 100644 --- a/src/file_sync_service.h +++ b/src/file_sync_service.h @@ -18,7 +18,7 @@ #define _FILE_SYNC_SERVICE_H_ #ifdef HAVE_BIG_ENDIAN -static inline unsigned __swap_uint32(unsigned x) +static inline unsigned __swap_uint32(unsigned x) { return (((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8) @@ -72,7 +72,7 @@ typedef union { struct { unsigned id; unsigned msglen; - } status; + } status; } syncmsg; void init_sdk_sync_permit_rule_regx(void); diff --git a/src/init.c b/src/init.c index bec59f2..7fcc70b 100644 --- a/src/init.c +++ b/src/init.c @@ -50,7 +50,7 @@ char *smack_mnt = NULL; void set_smackmnt(const char *mnt) { - smack_mnt = strdup(mnt); + smack_mnt = strdup(mnt); } /* Verify the mount point for smack file system has a smackfs. @@ -62,126 +62,126 @@ void set_smackmnt(const char *mnt) */ static int verify_smackmnt(const char *mnt) { - struct statfs sfbuf; - int rc; - - do { - rc = statfs(mnt, &sfbuf); - } while (rc < 0 && errno == EINTR); - - if (rc == 0) { - if ((uint32_t)sfbuf.f_type == (uint32_t)SMACK_MAGIC) { - struct statvfs vfsbuf; - rc = statvfs(mnt, &vfsbuf); - if (rc == 0) { - if (!(vfsbuf.f_flag & ST_RDONLY)) { - set_smackmnt(mnt); - } - return 0; - } - } - } - - return -1; + struct statfs sfbuf; + int rc; + + do { + rc = statfs(mnt, &sfbuf); + } while (rc < 0 && errno == EINTR); + + if (rc == 0) { + if ((uint32_t)sfbuf.f_type == (uint32_t)SMACK_MAGIC) { + struct statvfs vfsbuf; + rc = statvfs(mnt, &vfsbuf); + if (rc == 0) { + if (!(vfsbuf.f_flag & ST_RDONLY)) { + set_smackmnt(mnt); + } + return 0; + } + } + } + + return -1; } int smackfs_exists(void) { - int exists = 0; - FILE *fp = NULL; - char *buf = NULL; - size_t len; - ssize_t num; - - fp = fopen("/proc/filesystems", "r"); - if (!fp) - return 1; /* Fail as if it exists */ - - __fsetlocking(fp, FSETLOCKING_BYCALLER); - - num = getline(&buf, &len, fp); - while (num != -1) { - if (strstr(buf, SMACKFS)) { - exists = 1; - break; - } - num = getline(&buf, &len, fp); - } - - free(buf); - fclose(fp); - return exists; + int exists = 0; + FILE *fp = NULL; + char *buf = NULL; + size_t len; + ssize_t num; + + fp = fopen("/proc/filesystems", "r"); + if (!fp) + return 1; /* Fail as if it exists */ + + __fsetlocking(fp, FSETLOCKING_BYCALLER); + + num = getline(&buf, &len, fp); + while (num != -1) { + if (strstr(buf, SMACKFS)) { + exists = 1; + break; + } + num = getline(&buf, &len, fp); + } + + free(buf); + fclose(fp); + return exists; } static void init_smackmnt(void) { - char *buf=NULL, *p; - FILE *fp=NULL; - size_t len; - ssize_t num; - - if (smack_mnt) - return; - - if (verify_smackmnt(SMACKFSMNT) == 0) - return; - - if (verify_smackmnt(OLDSMACKFSMNT) == 0) - return; - - /* Drop back to detecting it the long way. */ - if (!smackfs_exists()) - goto out; - - /* At this point, the usual spot doesn't have an smackfs so - * we look around for it */ - fp = fopen("/proc/mounts", "r"); - if (!fp) - goto out; - - __fsetlocking(fp, FSETLOCKING_BYCALLER); - while ((num = getline(&buf, &len, fp)) != -1) { - char *tmp; - p = strchr(buf, ' '); - if (!p) - goto out; - p++; - - tmp = strchr(p, ' '); - if (!tmp) - goto out; - - if (!strncmp(tmp + 1, SMACKFS" ", strlen(SMACKFS)+1)) { - *tmp = '\0'; - break; - } - } - - /* If we found something, dup it */ - if (num > 0) - verify_smackmnt(p); + char *buf=NULL, *p; + FILE *fp=NULL; + size_t len; + ssize_t num; + + if (smack_mnt) + return; + + if (verify_smackmnt(SMACKFSMNT) == 0) + return; + + if (verify_smackmnt(OLDSMACKFSMNT) == 0) + return; + + /* Drop back to detecting it the long way. */ + if (!smackfs_exists()) + goto out; + + /* At this point, the usual spot doesn't have an smackfs so + * we look around for it */ + fp = fopen("/proc/mounts", "r"); + if (!fp) + goto out; + + __fsetlocking(fp, FSETLOCKING_BYCALLER); + while ((num = getline(&buf, &len, fp)) != -1) { + char *tmp; + p = strchr(buf, ' '); + if (!p) + goto out; + p++; + + tmp = strchr(p, ' '); + if (!tmp) + goto out; + + if (!strncmp(tmp + 1, SMACKFS" ", strlen(SMACKFS)+1)) { + *tmp = '\0'; + break; + } + } + + /* If we found something, dup it */ + if (num > 0) + verify_smackmnt(p); out: - free(buf); - if (fp) - fclose(fp); - return; + free(buf); + if (fp) + fclose(fp); + return; } void fini_smackmnt(void) { - free(smack_mnt); - smack_mnt = NULL; + free(smack_mnt); + smack_mnt = NULL; } static void init_lib(void) __attribute__ ((constructor)); static void init_lib(void) { - init_smackmnt(); + init_smackmnt(); } static void fini_lib(void) __attribute__ ((destructor)); static void fini_lib(void) { - fini_smackmnt(); + fini_smackmnt(); } diff --git a/src/plugin_encrypt.c b/src/plugin_encrypt.c index 51df016..b7fc3ab 100644 --- a/src/plugin_encrypt.c +++ b/src/plugin_encrypt.c @@ -1,238 +1,238 @@ - -#include - -//#define LOG_TAG "SDBD" -//#include -#define TRACE_TAG TRACE_SDB -#include "log.h" - -#include "plugin.h" -#include "plugin_encrypt.h" -#include "parameter.h" -#include "sdbd_plugin.h" - -#define SAKEP_AKE_MSG_RECORD_FIXED_LEN 36 -#define SAKEP_AES_ECB_ADDED_PADDING_SIZE 16 - -// return 1 if success -// return 0 otherwise -int security_init(const int nSessionID, const char* pUserID) -{ - int success = 0; - int ret; - parameters in, out; - - if (pUserID == NULL) { - in.number_of_parameter = 1; - in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) ); - in.array_of_parameter[0].type = type_int32; - in.array_of_parameter[0].v_int32 = nSessionID; - } else { - in.number_of_parameter = 2; - in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) * in.number_of_parameter ); - in.array_of_parameter[0].type = type_int32; - in.array_of_parameter[0].v_int32 = nSessionID; - in.array_of_parameter[1].type = type_string; - in.array_of_parameter[1].v_string.length = strlen(pUserID); - in.array_of_parameter[1].v_string.data = strdup(pUserID); - } - - ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_INIT, &in, &out ); - if ( ret == PLUGIN_CMD_SUCCESS ) { - success = 1; - release_parameters ( &out ); - } - - release_parameters ( &in ); - return success; -} - -// return 1 if success -// return 0 otherwise -int security_deinit(const int nSessionID) -{ - int success = 0; - int ret; - parameters in, out; - - in.number_of_parameter = 1; - in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) ); - in.array_of_parameter[0].type = type_int32; - in.array_of_parameter[0].v_int32 = nSessionID; - - ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_DEINIT, &in, &out ); - if ( ret == PLUGIN_CMD_SUCCESS ) { - success = 1; - release_parameters ( &out ); - } - - release_parameters ( &in ); - return success; -} - -// return 1 if success -// return 0 otherwise -int security_parse_server_hello(const int nSessionID, apacket* pApacket) -{ - int success = 0; - int ret; - parameters in, out; - - in.number_of_parameter = 2; - in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) * in.number_of_parameter ); - in.array_of_parameter[0].type = type_int32; - in.array_of_parameter[0].v_int32 = nSessionID; - in.array_of_parameter[1].type = type_chunk; - in.array_of_parameter[1].v_chunk.size = pApacket->msg.data_length; - in.array_of_parameter[1].v_chunk.data = pApacket->data; - - ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_PARSE_SERVER_HELLO, &in, &out ); - if ( ret == PLUGIN_CMD_SUCCESS ) { - success = 1; - release_parameters ( &out ); - } - - // avoid to free - in.array_of_parameter[1].v_chunk.data = NULL; - release_parameters ( &in ); - return success; -} - -// return 1 if success -// return 0 otherwise -int security_gen_client_hello(const int nSessionID, apacket* pApacket) -{ - int success = 0; - int ret; - parameters in, out; - - in.number_of_parameter = 1; - in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) ); - in.array_of_parameter[0].type = type_int32; - in.array_of_parameter[0].v_int32 = nSessionID; - - ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_GEN_CLIENT_HELLO, &in, &out ); - if ( ret == PLUGIN_CMD_SUCCESS ) { - memcpy(pApacket->data, out.array_of_parameter[0].v_chunk.data, out.array_of_parameter[0].v_chunk.size); - pApacket->msg.data_length = out.array_of_parameter[0].v_chunk.size; - success = 1; - release_parameters ( &out ); - } - - release_parameters ( &in ); - return success; -} - -// return 1 if success -// return 0 otherwise -int security_parse_server_ack(const int nSessionID, apacket* pApacket) -{ - int success = 0; - int ret; - parameters in, out; - - in.number_of_parameter = 2; - in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) * in.number_of_parameter ); - in.array_of_parameter[0].type = type_int32; - in.array_of_parameter[0].v_int32 = nSessionID; - in.array_of_parameter[1].type = type_chunk; - in.array_of_parameter[1].v_chunk.size = pApacket->msg.data_length; - in.array_of_parameter[1].v_chunk.data = pApacket->data; - - ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_PARSE_SERVER_ACK, &in, &out ); - if ( ret == PLUGIN_CMD_SUCCESS ) { - success = 1; - release_parameters ( &out ); - } - - // avoid to free - in.array_of_parameter[1].v_chunk.data = NULL; - release_parameters ( &in ); - return success; -} - -// return 1 if success -// return 0 otherwise -int security_gen_client_ack(const int nSessionID, apacket* pApacket) -{ - int success = 0; - int ret; - parameters in, out; - - in.number_of_parameter = 1; - in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) ); - in.array_of_parameter[0].type = type_int32; - in.array_of_parameter[0].v_int32 = nSessionID; - - ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_GEN_CLIENT_ACK, &in, &out ); - if ( ret == PLUGIN_CMD_SUCCESS ) { - memcpy(pApacket->data, out.array_of_parameter[0].v_chunk.data, out.array_of_parameter[0].v_chunk.size); - pApacket->msg.data_length = out.array_of_parameter[0].v_chunk.size; - success = 1; - release_parameters ( &out ); - } - - release_parameters ( &in ); - return success; -} - -// return 1 if success -// return 0 otherwise -int security_encrypt(const int nSessionID, apacket* pApacket) -{ - int success = 0; - int ret; - parameters in, out; - - in.number_of_parameter = 2; - in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) * in.number_of_parameter ); - in.array_of_parameter[0].type = type_int32; - in.array_of_parameter[0].v_int32 = nSessionID; - in.array_of_parameter[1].type = type_chunk; - in.array_of_parameter[1].v_chunk.size = pApacket->msg.data_length; - in.array_of_parameter[1].v_chunk.data = pApacket->data; - - ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_ENCRYPT, &in, &out ); - if ( ret == PLUGIN_CMD_SUCCESS ) { - memcpy(pApacket->data, out.array_of_parameter[0].v_chunk.data, out.array_of_parameter[0].v_chunk.size); - pApacket->msg.data_length = out.array_of_parameter[0].v_chunk.size; - success = 1; - release_parameters ( &out ); - } - - // avoid to free - in.array_of_parameter[1].v_chunk.data = NULL; - release_parameters ( &in ); - return success; -} - -// return 1 if success -// return 0 otherwise -int security_decrypt(const int nSessionID, apacket* pApacket) -{ - int success = 0; - int ret; - parameters in, out; - - in.number_of_parameter = 2; - in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) * in.number_of_parameter ); - in.array_of_parameter[0].type = type_int32; - in.array_of_parameter[0].v_int32 = nSessionID; - in.array_of_parameter[1].type = type_chunk; - in.array_of_parameter[1].v_chunk.size = pApacket->msg.data_length; - in.array_of_parameter[1].v_chunk.data = pApacket->data; - - ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_DECRYPT, &in, &out ); - if ( ret == PLUGIN_CMD_SUCCESS ) { - memcpy(pApacket->data, out.array_of_parameter[0].v_chunk.data, out.array_of_parameter[0].v_chunk.size); - pApacket->msg.data_length = out.array_of_parameter[0].v_chunk.size; - success = 1; - release_parameters ( &out ); - } - - // avoid to free - in.array_of_parameter[1].v_chunk.data = NULL; - release_parameters ( &in ); - return success; -} - + +#include + +//#define LOG_TAG "SDBD" +//#include +#define TRACE_TAG TRACE_SDB +#include "log.h" + +#include "plugin.h" +#include "plugin_encrypt.h" +#include "parameter.h" +#include "sdbd_plugin.h" + +#define SAKEP_AKE_MSG_RECORD_FIXED_LEN 36 +#define SAKEP_AES_ECB_ADDED_PADDING_SIZE 16 + +// return 1 if success +// return 0 otherwise +int security_init(const int nSessionID, const char* pUserID) +{ + int success = 0; + int ret; + parameters in, out; + + if (pUserID == NULL) { + in.number_of_parameter = 1; + in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) ); + in.array_of_parameter[0].type = type_int32; + in.array_of_parameter[0].v_int32 = nSessionID; + } else { + in.number_of_parameter = 2; + in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) * in.number_of_parameter ); + in.array_of_parameter[0].type = type_int32; + in.array_of_parameter[0].v_int32 = nSessionID; + in.array_of_parameter[1].type = type_string; + in.array_of_parameter[1].v_string.length = strlen(pUserID); + in.array_of_parameter[1].v_string.data = strdup(pUserID); + } + + ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_INIT, &in, &out ); + if ( ret == PLUGIN_CMD_SUCCESS ) { + success = 1; + release_parameters ( &out ); + } + + release_parameters ( &in ); + return success; +} + +// return 1 if success +// return 0 otherwise +int security_deinit(const int nSessionID) +{ + int success = 0; + int ret; + parameters in, out; + + in.number_of_parameter = 1; + in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) ); + in.array_of_parameter[0].type = type_int32; + in.array_of_parameter[0].v_int32 = nSessionID; + + ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_DEINIT, &in, &out ); + if ( ret == PLUGIN_CMD_SUCCESS ) { + success = 1; + release_parameters ( &out ); + } + + release_parameters ( &in ); + return success; +} + +// return 1 if success +// return 0 otherwise +int security_parse_server_hello(const int nSessionID, apacket* pApacket) +{ + int success = 0; + int ret; + parameters in, out; + + in.number_of_parameter = 2; + in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) * in.number_of_parameter ); + in.array_of_parameter[0].type = type_int32; + in.array_of_parameter[0].v_int32 = nSessionID; + in.array_of_parameter[1].type = type_chunk; + in.array_of_parameter[1].v_chunk.size = pApacket->msg.data_length; + in.array_of_parameter[1].v_chunk.data = pApacket->data; + + ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_PARSE_SERVER_HELLO, &in, &out ); + if ( ret == PLUGIN_CMD_SUCCESS ) { + success = 1; + release_parameters ( &out ); + } + + // avoid to free + in.array_of_parameter[1].v_chunk.data = NULL; + release_parameters ( &in ); + return success; +} + +// return 1 if success +// return 0 otherwise +int security_gen_client_hello(const int nSessionID, apacket* pApacket) +{ + int success = 0; + int ret; + parameters in, out; + + in.number_of_parameter = 1; + in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) ); + in.array_of_parameter[0].type = type_int32; + in.array_of_parameter[0].v_int32 = nSessionID; + + ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_GEN_CLIENT_HELLO, &in, &out ); + if ( ret == PLUGIN_CMD_SUCCESS ) { + memcpy(pApacket->data, out.array_of_parameter[0].v_chunk.data, out.array_of_parameter[0].v_chunk.size); + pApacket->msg.data_length = out.array_of_parameter[0].v_chunk.size; + success = 1; + release_parameters ( &out ); + } + + release_parameters ( &in ); + return success; +} + +// return 1 if success +// return 0 otherwise +int security_parse_server_ack(const int nSessionID, apacket* pApacket) +{ + int success = 0; + int ret; + parameters in, out; + + in.number_of_parameter = 2; + in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) * in.number_of_parameter ); + in.array_of_parameter[0].type = type_int32; + in.array_of_parameter[0].v_int32 = nSessionID; + in.array_of_parameter[1].type = type_chunk; + in.array_of_parameter[1].v_chunk.size = pApacket->msg.data_length; + in.array_of_parameter[1].v_chunk.data = pApacket->data; + + ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_PARSE_SERVER_ACK, &in, &out ); + if ( ret == PLUGIN_CMD_SUCCESS ) { + success = 1; + release_parameters ( &out ); + } + + // avoid to free + in.array_of_parameter[1].v_chunk.data = NULL; + release_parameters ( &in ); + return success; +} + +// return 1 if success +// return 0 otherwise +int security_gen_client_ack(const int nSessionID, apacket* pApacket) +{ + int success = 0; + int ret; + parameters in, out; + + in.number_of_parameter = 1; + in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) ); + in.array_of_parameter[0].type = type_int32; + in.array_of_parameter[0].v_int32 = nSessionID; + + ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_GEN_CLIENT_ACK, &in, &out ); + if ( ret == PLUGIN_CMD_SUCCESS ) { + memcpy(pApacket->data, out.array_of_parameter[0].v_chunk.data, out.array_of_parameter[0].v_chunk.size); + pApacket->msg.data_length = out.array_of_parameter[0].v_chunk.size; + success = 1; + release_parameters ( &out ); + } + + release_parameters ( &in ); + return success; +} + +// return 1 if success +// return 0 otherwise +int security_encrypt(const int nSessionID, apacket* pApacket) +{ + int success = 0; + int ret; + parameters in, out; + + in.number_of_parameter = 2; + in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) * in.number_of_parameter ); + in.array_of_parameter[0].type = type_int32; + in.array_of_parameter[0].v_int32 = nSessionID; + in.array_of_parameter[1].type = type_chunk; + in.array_of_parameter[1].v_chunk.size = pApacket->msg.data_length; + in.array_of_parameter[1].v_chunk.data = pApacket->data; + + ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_ENCRYPT, &in, &out ); + if ( ret == PLUGIN_CMD_SUCCESS ) { + memcpy(pApacket->data, out.array_of_parameter[0].v_chunk.data, out.array_of_parameter[0].v_chunk.size); + pApacket->msg.data_length = out.array_of_parameter[0].v_chunk.size; + success = 1; + release_parameters ( &out ); + } + + // avoid to free + in.array_of_parameter[1].v_chunk.data = NULL; + release_parameters ( &in ); + return success; +} + +// return 1 if success +// return 0 otherwise +int security_decrypt(const int nSessionID, apacket* pApacket) +{ + int success = 0; + int ret; + parameters in, out; + + in.number_of_parameter = 2; + in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) * in.number_of_parameter ); + in.array_of_parameter[0].type = type_int32; + in.array_of_parameter[0].v_int32 = nSessionID; + in.array_of_parameter[1].type = type_chunk; + in.array_of_parameter[1].v_chunk.size = pApacket->msg.data_length; + in.array_of_parameter[1].v_chunk.data = pApacket->data; + + ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_DECRYPT, &in, &out ); + if ( ret == PLUGIN_CMD_SUCCESS ) { + memcpy(pApacket->data, out.array_of_parameter[0].v_chunk.data, out.array_of_parameter[0].v_chunk.size); + pApacket->msg.data_length = out.array_of_parameter[0].v_chunk.size; + success = 1; + release_parameters ( &out ); + } + + // avoid to free + in.array_of_parameter[1].v_chunk.data = NULL; + release_parameters ( &in ); + return success; +} + diff --git a/src/plugin_encrypt.h b/src/plugin_encrypt.h index c54ff2b..93d2363 100644 --- a/src/plugin_encrypt.h +++ b/src/plugin_encrypt.h @@ -1,15 +1,15 @@ -#ifndef __TRANSPORT_SECURITY_H__ -#define __TRANSPORT_SECURITY_H__ - -#include "sdb.h" - -int security_init(const int nID, const char* pUserID); -int security_deinit(const int nSessionID); -int security_parse_server_hello(const int nSessionID, apacket* pApacket); -int security_gen_client_hello(const int nSessionID, apacket* pApacket); -int security_parse_server_ack(const int nSessionID, apacket* pApacket); -int security_gen_client_ack(const int nSessionID, apacket* pApacket); -int security_encrypt(const int nID, apacket* pApacket); -int security_decrypt(const int nID, apacket* pApacket); - -#endif +#ifndef __TRANSPORT_SECURITY_H__ +#define __TRANSPORT_SECURITY_H__ + +#include "sdb.h" + +int security_init(const int nID, const char* pUserID); +int security_deinit(const int nSessionID); +int security_parse_server_hello(const int nSessionID, apacket* pApacket); +int security_gen_client_hello(const int nSessionID, apacket* pApacket); +int security_parse_server_ack(const int nSessionID, apacket* pApacket); +int security_gen_client_ack(const int nSessionID, apacket* pApacket); +int security_encrypt(const int nID, apacket* pApacket); +int security_decrypt(const int nID, apacket* pApacket); + +#endif diff --git a/src/sdb.c b/src/sdb.c index 39ea547..21653e3 100644 --- a/src/sdb.c +++ b/src/sdb.c @@ -369,141 +369,141 @@ void print_packet(const char *label, apacket *p) /* desc. : 암호화 실패 메시지 전송 parameter : [in] apacket* p : sdbd로 들어온 메시지 - [in] atransport *t : 현재 연결에 대한 atransport - [in] unsigned failed_value : 실패 값 + [in] atransport *t : 현재 연결에 대한 atransport + [in] unsigned failed_value : 실패 값 */ void send_encr_fail(apacket* p, atransport *t, unsigned failed_value){ - apacket* enc_p; - enc_p = get_apacket(); - enc_p->msg.command = A_ENCR; // 암호화 메시지 - enc_p->msg.arg0 = failed_value; // 실패값 - enc_p->msg.arg1 = p->msg.arg1; - send_packet(enc_p, t); - //put_apacket(enc_p); + apacket* enc_p; + enc_p = get_apacket(); + enc_p->msg.command = A_ENCR; // 암호화 메시지 + enc_p->msg.arg0 = failed_value; // 실패값 + enc_p->msg.arg1 = p->msg.arg1; + send_packet(enc_p, t); + //put_apacket(enc_p); } /* desc. : 암호화 메시지 핸들링 parameter : [in] apacket* p : sdbd로 들어온 메시지 - [in/out] atransport *t : 현재 연결에 대한 atransport + [in/out] atransport *t : 현재 연결에 대한 atransport ret : 0 : 정상적으로 메시지 전송 - -1: 메시지 전송 실패 + -1: 메시지 전송 실패 */ int handle_encr_packet(apacket* p, atransport *t){ - static int sessionID = 0; - int retVal = 0; + static int sessionID = 0; + int retVal = 0; apacket* enc_p = NULL; - if(p->msg.arg0 == ENCR_SET_ON_REQ){ // hello 메시지인 경우 - t->sessionID = sessionID; - if((retVal = security_init(t->sessionID, NULL)) == 1){ // 암호화 handshaking을 위한 init - if(security_parse_server_hello(t->sessionID, p) == 1){ // hello 메시지 파싱 - D("security_parse_server_hello success\n"); + if(p->msg.arg0 == ENCR_SET_ON_REQ){ // hello 메시지인 경우 + t->sessionID = sessionID; + if((retVal = security_init(t->sessionID, NULL)) == 1){ // 암호화 handshaking을 위한 init + if(security_parse_server_hello(t->sessionID, p) == 1){ // hello 메시지 파싱 + D("security_parse_server_hello success\n"); enc_p = get_apacket(); - if(security_gen_client_hello(t->sessionID, enc_p) == 1){ // hello 메시지 생성 - D("security_gen_client_hello success\n"); - enc_p->msg.command = A_ENCR; - enc_p->msg.arg0 = ENCR_SET_ON_REQ; - enc_p->msg.arg1 = p->msg.arg1; - sessionID++; - send_packet(enc_p, t); - } - else { // hello 메시지 생성 실패 - D("security_gen_client_hello error\n"); - send_encr_fail(p, t, ENCR_ON_FAIL); // 암호화 on 실패 메시지 전송 - t->encryption = ENCR_OFF; // 암호화 모드는 off - security_deinit(t->sessionID); - return -1; - } - } - else{ // hello 메시지 파싱 실패 - D("security_parse_server_hello error\n"); - send_encr_fail(p, t, ENCR_ON_FAIL); - t->encryption = ENCR_OFF; - security_deinit(t->sessionID); - - return -1; - } - } else { // init 실패 - D("security_init error\n"); - send_encr_fail(p, t, ENCR_ON_FAIL); - t->encryption = ENCR_OFF; - if (retVal == -1) - { - security_deinit(t->sessionID); - } - //here!! do security_deinit(), but when plugin pointer is null -> not deinit - return -1; - } - } - else if(p->msg.arg0 == ENCR_SET_ON_OK){ // ack 메시지인 경우 - if(security_parse_server_ack(t->sessionID, p) == 1){ // ack 메시지 파싱 + if(security_gen_client_hello(t->sessionID, enc_p) == 1){ // hello 메시지 생성 + D("security_gen_client_hello success\n"); + enc_p->msg.command = A_ENCR; + enc_p->msg.arg0 = ENCR_SET_ON_REQ; + enc_p->msg.arg1 = p->msg.arg1; + sessionID++; + send_packet(enc_p, t); + } + else { // hello 메시지 생성 실패 + D("security_gen_client_hello error\n"); + send_encr_fail(p, t, ENCR_ON_FAIL); // 암호화 on 실패 메시지 전송 + t->encryption = ENCR_OFF; // 암호화 모드는 off + security_deinit(t->sessionID); + return -1; + } + } + else{ // hello 메시지 파싱 실패 + D("security_parse_server_hello error\n"); + send_encr_fail(p, t, ENCR_ON_FAIL); + t->encryption = ENCR_OFF; + security_deinit(t->sessionID); + + return -1; + } + } else { // init 실패 + D("security_init error\n"); + send_encr_fail(p, t, ENCR_ON_FAIL); + t->encryption = ENCR_OFF; + if (retVal == -1) + { + security_deinit(t->sessionID); + } + //here!! do security_deinit(), but when plugin pointer is null -> not deinit + return -1; + } + } + else if(p->msg.arg0 == ENCR_SET_ON_OK){ // ack 메시지인 경우 + if(security_parse_server_ack(t->sessionID, p) == 1){ // ack 메시지 파싱 enc_p = get_apacket(); - if(security_gen_client_ack(t->sessionID, enc_p) == 1){ // ack 메시지 생성 - D("security_gen_client_ack success\n"); - enc_p->msg.command = A_ENCR; - enc_p->msg.arg0 = ENCR_SET_ON_OK; - enc_p->msg.arg1 = p->msg.arg1; - t->encryption = ENCR_ON; - send_packet(enc_p, t); - } - else { // ack 메시지 생성에 실패한 경우 - D("security_gen_client_ack error\n"); - send_encr_fail(p, t, ENCR_ON_FAIL); - t->encryption = ENCR_OFF; - security_deinit(t->sessionID); - return -1; - } - } - else { // ack 메시지 파싱에 실패한 경우 - D("security_parse_server_ack error\n"); - send_encr_fail(p, t, ENCR_ON_FAIL); - t->encryption = ENCR_OFF; - security_deinit(t->sessionID); - return -1; - } - } - else if(p->msg.arg0 == ENCR_SET_OFF){ // 암호화 모드 off 요청 메시지 - if(t->encryption == ENCR_ON && security_deinit(t->sessionID) == 1){ // 현재 암호화 모드가 on 상태인 경우 - enc_p = get_apacket(); - t->encryption = ENCR_OFF; // 현재 연결에 대한 암호화 모드 off - enc_p->msg.command = A_ENCR; - enc_p->msg.arg0 = ENCR_SET_OFF; - enc_p->msg.arg1 = p->msg.arg1; - send_packet(enc_p, t); - } - else { // 암호화 모드 off에 실패한 경우 - D("security_deinit error\n"); - send_encr_fail(p, t, ENCR_OFF_FAIL); // 암호화 모드 off 실패 메시지 전송 - return -1; - } - } - else if(p->msg.arg0 == ENCR_GET){ // 암호화 모드의 상태 요청 메시지인 경우 - enc_p = get_apacket(); - enc_p->msg.command = A_ENCR; - enc_p->msg.arg0 = ENCR_GET; // 암호화 모드 status get메시지 - enc_p->msg.arg1 = p->msg.arg1; - if(t->encryption == ENCR_ON){ // 암호화 모드가 on인 경우 - enc_p->msg.data_length = 13; - strncpy((char*)enc_p->data, "encryption:on", enc_p->msg.data_length); // encryption:on 메시지 전송 - } else if(t->encryption == ENCR_OFF){ // 암호화 모드가 off인 경우 - enc_p->msg.data_length = 14; - strncpy((char*)enc_p->data, "encryption:off", enc_p->msg.data_length); // encryption:off 메시지 전송 - } - send_packet(enc_p, t); - } - else if (p->msg.arg0 == ENCR_ON_FAIL) // 암호화 모드를 on 하는 도중 실패한 경우 받는 메시지 - { - t->encryption = ENCR_OFF; // 암호화 모드를 다시 off - D("encryption on failed\n"); - } - else if (p->msg.arg0 == ENCR_OFF_FAIL) // 암호화 모드를 off하는 도중 실패한 경우 받는 메시지 - { - //t->encryption = ENCR_ON; - D("encryption off failed\n"); - } - //put_apacket(enc_p); - return 0; + if(security_gen_client_ack(t->sessionID, enc_p) == 1){ // ack 메시지 생성 + D("security_gen_client_ack success\n"); + enc_p->msg.command = A_ENCR; + enc_p->msg.arg0 = ENCR_SET_ON_OK; + enc_p->msg.arg1 = p->msg.arg1; + t->encryption = ENCR_ON; + send_packet(enc_p, t); + } + else { // ack 메시지 생성에 실패한 경우 + D("security_gen_client_ack error\n"); + send_encr_fail(p, t, ENCR_ON_FAIL); + t->encryption = ENCR_OFF; + security_deinit(t->sessionID); + return -1; + } + } + else { // ack 메시지 파싱에 실패한 경우 + D("security_parse_server_ack error\n"); + send_encr_fail(p, t, ENCR_ON_FAIL); + t->encryption = ENCR_OFF; + security_deinit(t->sessionID); + return -1; + } + } + else if(p->msg.arg0 == ENCR_SET_OFF){ // 암호화 모드 off 요청 메시지 + if(t->encryption == ENCR_ON && security_deinit(t->sessionID) == 1){ // 현재 암호화 모드가 on 상태인 경우 + enc_p = get_apacket(); + t->encryption = ENCR_OFF; // 현재 연결에 대한 암호화 모드 off + enc_p->msg.command = A_ENCR; + enc_p->msg.arg0 = ENCR_SET_OFF; + enc_p->msg.arg1 = p->msg.arg1; + send_packet(enc_p, t); + } + else { // 암호화 모드 off에 실패한 경우 + D("security_deinit error\n"); + send_encr_fail(p, t, ENCR_OFF_FAIL); // 암호화 모드 off 실패 메시지 전송 + return -1; + } + } + else if(p->msg.arg0 == ENCR_GET){ // 암호화 모드의 상태 요청 메시지인 경우 + enc_p = get_apacket(); + enc_p->msg.command = A_ENCR; + enc_p->msg.arg0 = ENCR_GET; // 암호화 모드 status get메시지 + enc_p->msg.arg1 = p->msg.arg1; + if(t->encryption == ENCR_ON){ // 암호화 모드가 on인 경우 + enc_p->msg.data_length = 13; + strncpy((char*)enc_p->data, "encryption:on", enc_p->msg.data_length); // encryption:on 메시지 전송 + } else if(t->encryption == ENCR_OFF){ // 암호화 모드가 off인 경우 + enc_p->msg.data_length = 14; + strncpy((char*)enc_p->data, "encryption:off", enc_p->msg.data_length); // encryption:off 메시지 전송 + } + send_packet(enc_p, t); + } + else if (p->msg.arg0 == ENCR_ON_FAIL) // 암호화 모드를 on 하는 도중 실패한 경우 받는 메시지 + { + t->encryption = ENCR_OFF; // 암호화 모드를 다시 off + D("encryption on failed\n"); + } + else if (p->msg.arg0 == ENCR_OFF_FAIL) // 암호화 모드를 off하는 도중 실패한 경우 받는 메시지 + { + //t->encryption = ENCR_ON; + D("encryption off failed\n"); + } + //put_apacket(enc_p); + return 0; } #endif @@ -892,11 +892,11 @@ void handle_packet(apacket *p, atransport *t) } break; #ifdef SUPPORT_ENCRYPT - case A_ENCR: // 암호화 메시지인 경우 - if(t->connection_state != CS_OFFLINE) { - handle_encr_packet(p, t); - } - break; + case A_ENCR: // 암호화 메시지인 경우 + if(t->connection_state != CS_OFFLINE) { + handle_encr_packet(p, t); + } + break; #endif default: @@ -1301,91 +1301,91 @@ int should_drop_privileges() { #define SDBD_BOOT_INFO_FILE "/tmp/sdbd_boot_info" static DBusHandlerResult __sdbd_dbus_signal_filter(DBusConnection *conn, - DBusMessage *message, void *user_data) { - D("got dbus message\n"); - const char *interface; - - DBusError error; - dbus_error_init(&error); - - interface = dbus_message_get_interface(message); - if (interface == NULL) { - D("reject by security issue - no interface\n"); - return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; - } - - if (dbus_message_is_signal(message, DEVICED_CORE_INTERFACE, - BOOTING_DONE_SIGNAL)) { - booting_done = 1; - if (access(SDBD_BOOT_INFO_FILE, F_OK) == 0) { - D("booting is done before\n"); - } else { - FILE *f = fopen(SDBD_BOOT_INFO_FILE, "w"); - if (f != NULL) { - fprintf(f, "%d", 1); - fclose(f); - } - } - D("booting is done\n"); - } - - D("handled dbus message\n"); - return DBUS_HANDLER_RESULT_HANDLED; + DBusMessage *message, void *user_data) { + D("got dbus message\n"); + const char *interface; + + DBusError error; + dbus_error_init(&error); + + interface = dbus_message_get_interface(message); + if (interface == NULL) { + D("reject by security issue - no interface\n"); + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + } + + if (dbus_message_is_signal(message, DEVICED_CORE_INTERFACE, + BOOTING_DONE_SIGNAL)) { + booting_done = 1; + if (access(SDBD_BOOT_INFO_FILE, F_OK) == 0) { + D("booting is done before\n"); + } else { + FILE *f = fopen(SDBD_BOOT_INFO_FILE, "w"); + if (f != NULL) { + fprintf(f, "%d", 1); + fclose(f); + } + } + D("booting is done\n"); + } + + D("handled dbus message\n"); + return DBUS_HANDLER_RESULT_HANDLED; } static void *bootdone_cb(void *x) { - int MAX_LOCAL_BUFSZ = 128; - DBusError error; - DBusConnection *bus; - char rule[MAX_LOCAL_BUFSZ]; - GMainLoop *mainloop; + int MAX_LOCAL_BUFSZ = 128; + DBusError error; + DBusConnection *bus; + char rule[MAX_LOCAL_BUFSZ]; + GMainLoop *mainloop; /* g_type_init() is deprecated for glib version 2.35.0 or greater, */ #if !GLIB_CHECK_VERSION(2,35,0) - g_type_init(); + g_type_init(); #endif - dbus_error_init(&error); - bus = dbus_bus_get(DBUS_BUS_SYSTEM, &error); - if (!bus) { - D("Failed to connect to the D-BUS daemon: %s", error.message); - dbus_error_free(&error); - return NULL; - } - dbus_connection_setup_with_g_main(bus, NULL); - - snprintf(rule, MAX_LOCAL_BUFSZ, "type='signal',interface='%s'", - DEVICED_CORE_INTERFACE); - /* listening to messages */ - dbus_bus_add_match(bus, rule, &error); - if (dbus_error_is_set(&error)) { - D("Fail to rule set: %s", error.message); - dbus_error_free(&error); - return NULL; - } - - if (dbus_connection_add_filter(bus, __sdbd_dbus_signal_filter, NULL, NULL) - == FALSE) - return NULL; - - D("booting signal initialized\n"); - mainloop = g_main_loop_new(NULL, FALSE); - g_main_loop_run(mainloop); - - D("dbus loop exited"); + dbus_error_init(&error); + bus = dbus_bus_get(DBUS_BUS_SYSTEM, &error); + if (!bus) { + D("Failed to connect to the D-BUS daemon: %s", error.message); + dbus_error_free(&error); + return NULL; + } + dbus_connection_setup_with_g_main(bus, NULL); + + snprintf(rule, MAX_LOCAL_BUFSZ, "type='signal',interface='%s'", + DEVICED_CORE_INTERFACE); + /* listening to messages */ + dbus_bus_add_match(bus, rule, &error); + if (dbus_error_is_set(&error)) { + D("Fail to rule set: %s", error.message); + dbus_error_free(&error); + return NULL; + } + + if (dbus_connection_add_filter(bus, __sdbd_dbus_signal_filter, NULL, NULL) + == FALSE) + return NULL; + + D("booting signal initialized\n"); + mainloop = g_main_loop_new(NULL, FALSE); + g_main_loop_run(mainloop); + + D("dbus loop exited"); dbus_connection_unref(bus); - return NULL; + return NULL; } void register_bootdone_cb() { - D("registerd bootdone callback\n"); + D("registerd bootdone callback\n"); - sdb_thread_t t; - if (sdb_thread_create(&t, bootdone_cb, NULL)) { - D("cannot create service thread\n"); - return; - } + sdb_thread_t t; + if (sdb_thread_create(&t, bootdone_cb, NULL)) { + D("cannot create service thread\n"); + return; + } } static int sdbd_set_groups(const char *name, int gid, struct group_info default_groups[], int default_groups_size) { diff --git a/src/sdb.h b/src/sdb.h index 12b3105..1516455 100644 --- a/src/sdb.h +++ b/src/sdb.h @@ -146,7 +146,7 @@ struct asocket { /* socket-type-specific extradata */ void *extra; - /* A socket is bound to atransport */ + /* A socket is bound to atransport */ atransport *transport; }; @@ -216,8 +216,8 @@ struct atransport size_t max_payload; #ifdef SUPPORT_ENCRYPT - unsigned encryption; // 해당 연결이 암호화 모드인지 확인하는 flag , 0 = no-encryption / 1 = encryption - int sessionID; // 암호화 세션 ID, 암호화 map에 대한 key + unsigned encryption; // 해당 연결이 암호화 모드인지 확인하는 flag , 0 = no-encryption / 1 = encryption + int sessionID; // 암호화 세션 ID, 암호화 map에 대한 key #endif }; diff --git a/src/services.c b/src/services.c index 6c9d1d6..ea991c2 100644 --- a/src/services.c +++ b/src/services.c @@ -139,7 +139,7 @@ void rootshell_service(int fd, void *cookie) writex(fd, buf, strlen(buf)); } } else { - snprintf(buf, sizeof(buf), "Unknown command option : %s\n", mode); + snprintf(buf, sizeof(buf), "Unknown command option : %s\n", mode); writex(fd, buf, strlen(buf)); } D("set rootshell to %s\n", rootshell_mode == 1 ? "root" : SDK_USER_NAME); diff --git a/src/socket_inaddr_any_server.c b/src/socket_inaddr_any_server.c index 10a84e6..00aa19b 100644 --- a/src/socket_inaddr_any_server.c +++ b/src/socket_inaddr_any_server.c @@ -64,7 +64,7 @@ int socket_inaddr_any_server(int port, int type) if (ret < 0) { close(s); - return -1; + return -1; } } diff --git a/src/socket_local.h b/src/socket_local.h index dc274ac..056d090 100644 --- a/src/socket_local.h +++ b/src/socket_local.h @@ -17,7 +17,7 @@ #ifndef __SOCKET_LOCAL_H #define __SOCKET_LOCAL_H -#define FILESYSTEM_SOCKET_PREFIX "/tmp/" +#define FILESYSTEM_SOCKET_PREFIX "/tmp/" #define ANDROID_RESERVED_SOCKET_PREFIX "/dev/socket/" /* @@ -30,10 +30,10 @@ * socklen_t is set to indicate the final length. This function * will fail if the namespace is invalid (not one of the indicated * constants) or if the name is too long. - * + * * @return 0 on success or -1 on failure - */ -int socket_make_sockaddr_un(const char *name, int namespaceId, + */ +int socket_make_sockaddr_un(const char *name, int namespaceId, struct sockaddr_un *p_addr, socklen_t *alen); #endif diff --git a/src/socket_local_server.c b/src/socket_local_server.c index 8f2ed9f..5cba92d 100644 --- a/src/socket_local_server.c +++ b/src/socket_local_server.c @@ -88,7 +88,7 @@ int socket_local_server_bind(int s, const char *name, int namespaceId) } -/** Open a server-side UNIX domain datagram socket in the Linux non-filesystem +/** Open a server-side UNIX domain datagram socket in the Linux non-filesystem * namespace * * Returns fd on success, -1 on fail @@ -98,7 +98,7 @@ int socket_local_server(const char *name, int namespace, int type) { int err; int s; - + s = socket(AF_LOCAL, type, 0); if (s < 0) return -1; diff --git a/src/socket_loopback_server.c b/src/socket_loopback_server.c index 73e5670..f397a3e 100644 --- a/src/socket_loopback_server.c +++ b/src/socket_loopback_server.c @@ -133,7 +133,7 @@ int socket_loopback_server(int port, int type) if (ret < 0) { sdb_close(s); - return -1; + return -1; } } diff --git a/src/sockets.c b/src/sockets.c index 9711613..12baade 100644 --- a/src/sockets.c +++ b/src/sockets.c @@ -319,11 +319,11 @@ static void local_socket_event_func(int fd, unsigned ev, void *_s) apacket *p = get_apacket(); unsigned char *x = p->data; #ifdef SUPPORT_ENCRYPT - // sdb.c:536에서 sdb server의 패킷은 MAX_PAYLOAD-100으로 정하여서, - // sdb server에서 패킷 데이터의 크기를 MAX_PAYLOAD-100보다 작은 지를 체크함. - // sdbd에서 패킷 데이터를 MAX_PAYLOAD - 200로 잡아서 암호화 하게되면 - // 최대 MAX_PAYLOAD - 100 크기의 패킷을 생성하게 됨. - const size_t max_payload = asock_get_max_payload(s) - 200; + // sdb.c:536에서 sdb server의 패킷은 MAX_PAYLOAD-100으로 정하여서, + // sdb server에서 패킷 데이터의 크기를 MAX_PAYLOAD-100보다 작은 지를 체크함. + // sdbd에서 패킷 데이터를 MAX_PAYLOAD - 200로 잡아서 암호화 하게되면 + // 최대 MAX_PAYLOAD - 100 크기의 패킷을 생성하게 됨. + const size_t max_payload = asock_get_max_payload(s) - 200; #else const size_t max_payload = asock_get_max_payload(s); #endif diff --git a/src/sockets.h b/src/sockets.h index de2269f..e358a19 100644 --- a/src/sockets.h +++ b/src/sockets.h @@ -28,8 +28,8 @@ typedef int socklen_t; #include #endif -#define ANDROID_SOCKET_ENV_PREFIX "ANDROID_SOCKET_" -#define ANDROID_SOCKET_DIR "/dev/socket" +#define ANDROID_SOCKET_ENV_PREFIX "ANDROID_SOCKET_" +#define ANDROID_SOCKET_DIR "/dev/socket" #ifdef __cplusplus extern "C" { @@ -45,32 +45,32 @@ extern "C" { */ static inline int android_get_control_socket(const char *name) { - char key[64] = ANDROID_SOCKET_ENV_PREFIX; - const char *val; - int fd; + char key[64] = ANDROID_SOCKET_ENV_PREFIX; + const char *val; + int fd; - /* build our environment variable, counting cycles like a wolf ... */ + /* build our environment variable, counting cycles like a wolf ... */ #if HAVE_STRLCPY - strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1, - name, - sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX)); -#else /* for the host, which may lack the almightly strncpy ... */ - strncpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1, - name, - sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX)); - key[sizeof(key)-1] = '\0'; + strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1, + name, + sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX)); +#else /* for the host, which may lack the almightly strncpy ... */ + strncpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1, + name, + sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX)); + key[sizeof(key)-1] = '\0'; #endif - val = getenv(key); - if (!val) - return -1; + val = getenv(key); + if (!val) + return -1; - errno = 0; - fd = strtol(val, NULL, 10); - if (errno) - return -1; + errno = 0; + fd = strtol(val, NULL, 10); + if (errno) + return -1; - return fd; + return fd; } /* diff --git a/src/transport.c b/src/transport.c index 7eb9e95..faf5482 100644 --- a/src/transport.c +++ b/src/transport.c @@ -304,10 +304,10 @@ static void *output_thread(void *_t) t->serial); #ifdef SUPPORT_ENCRYPT - if (t->encryption == ENCR_ON && p->msg.command != A_ENCR) // 현재 연결이 암호화 모드이고, 암호화 관련 메시지가 아닌 경우, 메시지 복호화 - { - security_decrypt(t->sessionID, p); - } + if (t->encryption == ENCR_ON && p->msg.command != A_ENCR) // 현재 연결이 암호화 모드이고, 암호화 관련 메시지가 아닌 경우, 메시지 복호화 + { + security_decrypt(t->sessionID, p); + } #endif @@ -382,14 +382,14 @@ static void *input_thread(void *_t) D("%s: transport got packet, sending to remote, state(%s)\n", t->serial, statename(t)); #ifdef SUPPORT_ENCRYPT - if (t->encryption == ENCR_ON && p->msg.command != A_ENCR) // 현재 연결이 암호화 모드이고, 암호화 관련 메시지가 아닌 경우, 메시지를 암호화 - { - security_encrypt(t->sessionID, p); - } - else if(t->encryption == ENCR_OFF) - { + if (t->encryption == ENCR_ON && p->msg.command != A_ENCR) // 현재 연결이 암호화 모드이고, 암호화 관련 메시지가 아닌 경우, 메시지를 암호화 + { + security_encrypt(t->sessionID, p); + } + else if(t->encryption == ENCR_OFF) + { - } + } #endif @@ -812,7 +812,7 @@ void broadcast_transport(apacket *p) apacket* ap = get_apacket(); copy_packet(ap, p); send_packet(ap, t); - + if (ap->msg.command == A_STAT && ap->msg.arg1 == 0) { // lock state message if (ap->msg.arg0 == 0) { diff --git a/src/transport_local.c b/src/transport_local.c index d1b569e..58a07a4 100644 --- a/src/transport_local.c +++ b/src/transport_local.c @@ -41,7 +41,7 @@ #include "plugin.h" #ifdef HAVE_BIG_ENDIAN -#define H4(x) (((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8) | (((x) & 0x0000FF00) << 8) | (((x) & 0x000000FF) << 24) +#define H4(x) (((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8) | (((x) & 0x0000FF00) << 8) | (((x) & 0x000000FF) << 24) static inline void fix_endians(apacket *p) { p->msg.command = H4(p->msg.command); @@ -450,9 +450,9 @@ static void* notify_sdbd_startup_thread(void* ptr) { } if (get_emulator_guestip(guest_ip, sizeof guest_ip) == -1) { - D("failed to get emulator guest ip\n"); - goto sleep_and_continue; - } + D("failed to get emulator guest ip\n"); + goto sleep_and_continue; + } // tell qemu sdbd is just started with udp if (send_msg_to_localhost_from_guest(host_ip, sensors_port, "2\n", 1) < 0) { diff --git a/src/transport_usb.c b/src/transport_usb.c index 32789ec..471aa87 100644 --- a/src/transport_usb.c +++ b/src/transport_usb.c @@ -26,7 +26,7 @@ #include "sdb.h" #ifdef HAVE_BIG_ENDIAN -#define H4(x) (((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8) | (((x) & 0x0000FF00) << 8) | (((x) & 0x000000FF) << 24) +#define H4(x) (((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8) | (((x) & 0x0000FF00) << 8) | (((x) & 0x000000FF) << 24) static inline void fix_endians(apacket *p) { p->msg.command = H4(p->msg.command); diff --git a/src/usb_funcfs_client.c b/src/usb_funcfs_client.c index 3ca92a3..00b25c3 100644 --- a/src/usb_funcfs_client.c +++ b/src/usb_funcfs_client.c @@ -35,8 +35,8 @@ #include "sdb.h" -#define MAX_PACKET_SIZE_FS 64 -#define MAX_PACKET_SIZE_HS 512 +#define MAX_PACKET_SIZE_FS 64 +#define MAX_PACKET_SIZE_HS 512 #define cpu_to_le16(x) htole16(x) #define cpu_to_le32(x) htole32(x) @@ -250,7 +250,7 @@ static void *usb_open_thread(void *x) sdb_mutex_unlock(&usb->lock); D("[ usb_thread - registering device ]\n"); - register_usb_transport(usb, NULL, 1); /* writable transport */ + register_usb_transport(usb, NULL, 1); /* writable transport */ while (1) { /* wait until the USB device needs reset */ @@ -269,7 +269,7 @@ static void *usb_open_thread(void *x) sdb_mutex_unlock(&usb->lock); D("[ usb_thread - registering device ]\n"); - register_usb_transport(usb, NULL, 1); /* writable transport */ + register_usb_transport(usb, NULL, 1); /* writable transport */ } /* never gets here */ @@ -312,34 +312,34 @@ static int read_control(struct usb_handle *usb) } /* dispatch read event */ - switch (read_event.type) { - case FUNCTIONFS_RESUME: - case FUNCTIONFS_ENABLE: - D("FFSEvent %s\n", names[read_event.type]); - sdb_mutex_lock(&usb->lock); - usb->ffs_enabled = 1; - sdb_cond_signal(&usb->notify); - sdb_mutex_unlock(&usb->lock); - break; - - case FUNCTIONFS_SUSPEND: - case FUNCTIONFS_DISABLE: - D("FFSEvent %s\n", names[read_event.type]); - sdb_mutex_lock(&usb->lock); - usb->ffs_enabled = 0; - sdb_mutex_unlock(&usb->lock); - break; - - case FUNCTIONFS_BIND: - case FUNCTIONFS_UNBIND: - case FUNCTIONFS_SETUP: - D("FFSEvent %s\n", names[read_event.type]); - break; - - default: - D("FFSEvent event (type=%d) is unknown -- ignored\n", read_event.type); - break; - } + switch (read_event.type) { + case FUNCTIONFS_RESUME: + case FUNCTIONFS_ENABLE: + D("FFSEvent %s\n", names[read_event.type]); + sdb_mutex_lock(&usb->lock); + usb->ffs_enabled = 1; + sdb_cond_signal(&usb->notify); + sdb_mutex_unlock(&usb->lock); + break; + + case FUNCTIONFS_SUSPEND: + case FUNCTIONFS_DISABLE: + D("FFSEvent %s\n", names[read_event.type]); + sdb_mutex_lock(&usb->lock); + usb->ffs_enabled = 0; + sdb_mutex_unlock(&usb->lock); + break; + + case FUNCTIONFS_BIND: + case FUNCTIONFS_UNBIND: + case FUNCTIONFS_SETUP: + D("FFSEvent %s\n", names[read_event.type]); + break; + + default: + D("FFSEvent event (type=%d) is unknown -- ignored\n", read_event.type); + break; + } return ret; } @@ -431,9 +431,9 @@ static int bulk_read(int bulkout_fd, void *buf, size_t length) if (errno != EINTR) { return ret; } - } else { + } else { count += ret; - } + } } while (count < length); @@ -446,8 +446,8 @@ static int bulk_read(int bulkout_fd, void *buf, size_t length) */ static int ep0_exists() { - struct stat statb; - return stat(ep0_path, &statb) == 0; + struct stat statb; + return stat(ep0_path, &statb) == 0; } @@ -464,9 +464,9 @@ static int autoconfig(struct usb_handle *h) return -ENODEV; } - h->EP0_NAME = ep0_path; - h->EP_OUT_NAME = ep1_path; - h->EP_IN_NAME = ep2_path; + h->EP0_NAME = ep0_path; + h->EP_OUT_NAME = ep1_path; + h->EP_IN_NAME = ep2_path; return 0; } diff --git a/src/utils.c b/src/utils.c index 6f48ba2..3a27e16 100644 --- a/src/utils.c +++ b/src/utils.c @@ -117,41 +117,41 @@ buff_add (char* buff, char* buffEnd, const char* format, ... ) } char *str_trim(char *str) { - size_t len = 0; - char *frontp = str; - char *endp = NULL; - - if (str == NULL) { - return NULL; - } - if (str[0] == '\0') { - return str; - } - - len = strlen(str); - endp = str + len; - - while (isspace(*frontp)) { - ++frontp; - } - if (endp != frontp) { - while (isspace(*(--endp)) && endp != frontp) { - } - } - - if (str + len - 1 != endp) - *(endp + 1) = '\0'; - else if (frontp != str && endp == frontp) - *str = '\0'; - - endp = str; - if (frontp != str) { - while (*frontp) { - *endp++ = *frontp++; - } - *endp = '\0'; - } - return str; + size_t len = 0; + char *frontp = str; + char *endp = NULL; + + if (str == NULL) { + return NULL; + } + if (str[0] == '\0') { + return str; + } + + len = strlen(str); + endp = str + len; + + while (isspace(*frontp)) { + ++frontp; + } + if (endp != frontp) { + while (isspace(*(--endp)) && endp != frontp) { + } + } + + if (str + len - 1 != endp) + *(endp + 1) = '\0'; + else if (frontp != str && endp == frontp) + *str = '\0'; + + endp = str; + if (frontp != str) { + while (*frontp) { + *endp++ = *frontp++; + } + *endp = '\0'; + } + return str; } int spawn(const char* program, char* const arg_list[]) @@ -182,47 +182,47 @@ int spawn(const char* program, char* const arg_list[]) } char** str_split(char* a_str, const char a_delim) { - char** result = 0; - size_t count = 0; - char* tmp = a_str; - char* last_comma = 0; - char delim[2]; - delim[0] = a_delim; - delim[1] = 0; - char *ptr; - - /* Count how many elements will be extracted. */ - while (*tmp) { - if (a_delim == *tmp) { - count++; - last_comma = tmp; - } - tmp++; - } - - /* Add space for trailing token. */ - count += last_comma < (a_str + strlen(a_str) - 1); - - /* Add space for terminating null string so caller - knows where the list of returned strings ends. */ - count++; - - result = malloc(sizeof(char*) * count); - - if (result) { - size_t idx = 0; - char* token = strtok_r(a_str, delim, &ptr); - - while (token) { - //assert(idx < count); - *(result + idx++) = strdup(token); - token = strtok_r(0, delim, &ptr); - } - //assert(idx == count - 1); - *(result + idx) = 0; - } - - return result; + char** result = 0; + size_t count = 0; + char* tmp = a_str; + char* last_comma = 0; + char delim[2]; + delim[0] = a_delim; + delim[1] = 0; + char *ptr; + + /* Count how many elements will be extracted. */ + while (*tmp) { + if (a_delim == *tmp) { + count++; + last_comma = tmp; + } + tmp++; + } + + /* Add space for trailing token. */ + count += last_comma < (a_str + strlen(a_str) - 1); + + /* Add space for terminating null string so caller + knows where the list of returned strings ends. */ + count++; + + result = malloc(sizeof(char*) * count); + + if (result) { + size_t idx = 0; + char* token = strtok_r(a_str, delim, &ptr); + + while (token) { + //assert(idx < count); + *(result + idx++) = strdup(token); + token = strtok_r(0, delim, &ptr); + } + //assert(idx == count - 1); + *(result + idx) = 0; + } + + return result; } int keep_alive(int fd, int onoff, int cnt, int idle, int interval) -- 2.7.4 From 74a263c7743e5f92b5898eace2f85b98b33e566b Mon Sep 17 00:00:00 2001 From: Sooyoung Ha Date: Tue, 27 Jun 2017 22:49:24 +0900 Subject: [PATCH 11/16] package: update version (3.0.30) Change-Id: I29f10364d289fbf07f1811a22d693c71ef30b842 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 8f5efed..a251f8c 100644 --- a/packaging/sdbd.spec +++ b/packaging/sdbd.spec @@ -2,7 +2,7 @@ Name: sdbd Summary: SDB daemon -Version: 3.0.29 +Version: 3.0.30 Release: 0 License: Apache-2.0 Summary: SDB daemon -- 2.7.4 From 6de1b632732c5fec08d29ed715428ff2b110b8e6 Mon Sep 17 00:00:00 2001 From: Sooyoung Ha Date: Wed, 28 Jun 2017 16:51:21 +0900 Subject: [PATCH 12/16] source: fix typo Change-Id: I7c68c0689e3e62361491285ce8107879c24b0209 Signed-off-by: Sooyoung Ha --- src/transport.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transport.c b/src/transport.c index faf5482..d6ecd66 100644 --- a/src/transport.c +++ b/src/transport.c @@ -876,7 +876,7 @@ int readx(int fd, void *ptr, size_t len) D("readx: fd=%d wanted=%zu\n", fd, len); while (l > 0) { - r = sdb_read(fd, p, len); + r = sdb_read(fd, p, l); if (r > 0 && r <= l) { l -= r; p += r; -- 2.7.4 From 1dabf8de4277ef1bb4c5113cac7931b8e7d88642 Mon Sep 17 00:00:00 2001 From: Sooyoung Ha Date: Wed, 28 Jun 2017 16:54:11 +0900 Subject: [PATCH 13/16] package: update version (3.0.31) Change-Id: Ie8d745d492170e326cf86d4c1e20afd12f1def36 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 a251f8c..3bfc0ec 100644 --- a/packaging/sdbd.spec +++ b/packaging/sdbd.spec @@ -2,7 +2,7 @@ Name: sdbd Summary: SDB daemon -Version: 3.0.30 +Version: 3.0.31 Release: 0 License: Apache-2.0 Summary: SDB daemon -- 2.7.4 From f5cd214d92d63433a829ca64f3a6d711c92bc00d Mon Sep 17 00:00:00 2001 From: Sooyoung Ha Date: Fri, 30 Jun 2017 14:11:37 +0900 Subject: [PATCH 14/16] packaging: modify user and group of tv service file The 'sdk' user group causes TV smack denying. This almost revert of commit a16797abdc35b105d38beaf9aaccab12b616933d. Change-Id: Id0a4beb8efc9e986cfd9f7eb56041de0d05b1105 Signed-off-by: Sooyoung Ha --- packaging/sdbd_device_tv.service | 2 -- packaging/sdbd_emulator_tv.service | 2 -- 2 files changed, 4 deletions(-) diff --git a/packaging/sdbd_device_tv.service b/packaging/sdbd_device_tv.service index b683d96..fe3c965 100644 --- a/packaging/sdbd_device_tv.service +++ b/packaging/sdbd_device_tv.service @@ -4,8 +4,6 @@ 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 0590499..4d81fd2 100644 --- a/packaging/sdbd_emulator_tv.service +++ b/packaging/sdbd_emulator_tv.service @@ -5,8 +5,6 @@ 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 ff571418f7992e29f3d5f8f329ea67d4640eca51 Mon Sep 17 00:00:00 2001 From: Sooyoung Ha Date: Fri, 30 Jun 2017 14:15:11 +0900 Subject: [PATCH 15/16] package: update version (3.0.32) Change-Id: I44433704c5f7267dee1aa18573e760bed2cbc56e 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 3bfc0ec..c3cdb7a 100644 --- a/packaging/sdbd.spec +++ b/packaging/sdbd.spec @@ -2,7 +2,7 @@ Name: sdbd Summary: SDB daemon -Version: 3.0.31 +Version: 3.0.32 Release: 0 License: Apache-2.0 Summary: SDB daemon -- 2.7.4 From a3f4720d14b5ff542bea08801d6ae54b2f9a849a Mon Sep 17 00:00:00 2001 From: Slava Barinov Date: Fri, 26 May 2017 12:01:38 +0300 Subject: [PATCH 16/16] package: Add libpthread explicitly for ASan build Change-Id: I39508a5c77a41f5ce3b3dfcfe295cb24bde4a367 Signed-off-by: Slava Barinov --- packaging/sdbd.spec | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packaging/sdbd.spec b/packaging/sdbd.spec index c3cdb7a..5ad9833 100644 --- a/packaging/sdbd.spec +++ b/packaging/sdbd.spec @@ -68,8 +68,7 @@ SDBD plugin API library cp %{SOURCE1003} . %build - -cmake . -DCMAKE_INSTALL_PREFIX=%{_prefix} \ +cmake . -DCMAKE_INSTALL_PREFIX=%{_prefix} %{?asan:-DCMAKE_EXE_LINKER_FLAGS="-pthread"} make %{?jobs:-j%jobs} -- 2.7.4