From 47e38d695a9a361de82f5ec5761424fffa95d5ea Mon Sep 17 00:00:00 2001 From: greatim Date: Wed, 14 Dec 2016 17:28:38 +0900 Subject: [PATCH] fix potential bugs remove unnecessary files and code (properties.h and properties.c) fix double close problem of socket pair in file_sync_service.c remove unnecessary manifest file Change-Id: Ic61ca8ffab9e1ee31f3a432acf0e9c138fc26fbe Signed-off-by: greatim --- CMakeLists.txt | 1 - sdbd.manifest | 44 ----- src/file_sync_service.c | 33 ++-- src/properties.c | 467 ------------------------------------------------ src/properties.h | 69 ------- src/sdb.c | 5 - src/services.c | 54 ------ src/sysdeps.h | 1 - src/transport_local.c | 8 - 9 files changed, 18 insertions(+), 664 deletions(-) delete mode 100644 sdbd.manifest delete mode 100644 src/properties.c delete mode 100644 src/properties.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 83b0a0a..e16df2d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,7 +43,6 @@ SET(SDBD_SRCS src/socket_loopback_client.c src/socket_loopback_server.c src/socket_network_client.c - src/properties.c src/sdktools.c src/strutils.c src/init.c diff --git a/sdbd.manifest b/sdbd.manifest deleted file mode 100644 index 1effc20..0000000 --- a/sdbd.manifest +++ /dev/null @@ -1,44 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/file_sync_service.c b/src/file_sync_service.c index 6c418a5..9377078 100644 --- a/src/file_sync_service.c +++ b/src/file_sync_service.c @@ -637,27 +637,30 @@ void file_sync_service(int fd, void *cookie) D("cannot create service socket pair\n"); exit(-1); } - char *buffer = malloc(SYNC_DATA_MAX); - if(buffer == 0) { - goto fail; - } - - FD_ZERO(&set); /* clear the set */ - FD_SET(fd, &set); /* add our file descriptor to the set */ - - timeout.tv_sec = SYNC_TIMEOUT; - timeout.tv_usec = 0; pid_t pid = fork(); if (pid == 0) { sdb_close(s[0]); //close the parent fd sync_read_label_notify(s[1]); + return; } else if (pid > 0) { sdb_close(s[1]); + + char *buffer = malloc(SYNC_DATA_MAX); + if(buffer == NULL) { + goto fail; + } + for(;;) { D("sync: waiting for command for %d sec\n", SYNC_TIMEOUT); + FD_ZERO(&set); /* clear the set */ + FD_SET(fd, &set); /* add our file descriptor to the set */ + + timeout.tv_sec = SYNC_TIMEOUT; + timeout.tv_usec = 0; + rv = select(fd + 1, &set, NULL, NULL, &timeout); if (rv == -1) { D("sync file descriptor select failed\n"); @@ -713,15 +716,15 @@ void file_sync_service(int fd, void *cookie) goto fail; } } + +fail: + if(buffer != NULL) { + free(buffer); + } } else { sdb_close(s[1]); } - -fail: - if(buffer != 0) { - free(buffer); - } D("sync: done\n"); sync_send_label_notify(s[0], name, 0); sdb_close(s[0]); diff --git a/src/properties.c b/src/properties.c deleted file mode 100644 index aa14fe7..0000000 --- a/src/properties.c +++ /dev/null @@ -1,467 +0,0 @@ -/* - * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the License); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an AS IS BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - - -#include -#include -#include -#include "sockets.h" -#include -#include - -#include "properties.h" -//#include "loghack.h" -#include "sysdeps.h" -#define TRACE_TAG TRACE_PROPERTIES -#include "log.h" - -#include "sdb.h" - -#define HAVE_TIZEN_PROPERTY - -#ifdef HAVE_TIZEN_PROPERTY - -#define HAVE_PTHREADS -#include -#include "strutils.h" -#include "threads.h" - -static mutex_t env_lock = MUTEX_INITIALIZER; - -#define TIZEN_PROPERTY_FILE "/tmp/.sdb.conf" /* tizen specific*/ -#define PROPERTY_SEPARATOR "=" - -struct config_node { - char *key; - char value[PROPERTY_VALUE_MAX]; -} sdbd_config[] = { - { "service.sdb.tcp.port", "0" }, - { NULL, "" } -}; -int sdbd_config_element_cnt = sizeof(sdbd_config) / sizeof(sdbd_config[0]); - -void property_save(); - -static void property_init(void) -{ - int fd; - int i = 0; - char buffer[PROPERTY_KEY_MAX+PROPERTY_VALUE_MAX+1]; - char *tok = NULL; - char *ptr; - - fd = unix_open(TIZEN_PROPERTY_FILE, O_RDONLY); - if (fd < 0) - return; - for(;;) { - if(read_line(fd, buffer, PROPERTY_KEY_MAX+PROPERTY_VALUE_MAX+1) < 0) - break; - tok = strtok_r(buffer, PROPERTY_SEPARATOR, &ptr); - if(tok) { - for (i = 0; i < sdbd_config_element_cnt && sdbd_config[i].key ; i++) { - if (!strcmp(tok, sdbd_config[i].key)) { - tok = strtok_r(NULL, PROPERTY_SEPARATOR, &ptr); - if(tok) { - snprintf(sdbd_config[i].value, PROPERTY_VALUE_MAX, "%s", tok); - D("property init key=%s, value=%s\n", sdbd_config[i].key, tok); - } - } - } - } - } - sdb_close(fd); - D("called property_init\n"); -} - -void property_save() -{ - int fd; - int i = 0; - char buffer[PROPERTY_KEY_MAX+PROPERTY_VALUE_MAX+1]; - - mutex_lock(&env_lock); - if (access(TIZEN_PROPERTY_FILE, F_OK) == 0) // if exist - sdb_unlink(TIZEN_PROPERTY_FILE); - - fd = unix_open(TIZEN_PROPERTY_FILE, O_WRONLY | O_CREAT | O_APPEND, 0640); - if (fd <0 ) { - mutex_unlock(&env_lock); - return; - } - - for (i = 0; i < sdbd_config_element_cnt && sdbd_config[i].key; i++) { - snprintf(buffer, sizeof(buffer), "%s%s%s\n", sdbd_config[i].key, PROPERTY_SEPARATOR, sdbd_config[i].value); - sdb_write(fd, buffer, strlen(buffer)); - } - sdb_close(fd); - mutex_unlock(&env_lock); -} - -int property_set(const char *key, const char *value) -{ - int i = 0; - - mutex_lock(&env_lock); - - for (i = 0; i < sdbd_config_element_cnt && sdbd_config[i].key; i++) { - if (!strcmp(key,sdbd_config[i].key)) { - snprintf(sdbd_config[i].value, PROPERTY_VALUE_MAX, "%s", value); - D("property set key=%s, value=%s\n", key, value); - break; - } - } - mutex_unlock(&env_lock); - property_save(); - return -1; -} - -int property_get(const char *key, char *value, const char *default_value) -{ - int len = 0; - int i = 0; - - property_init(); - mutex_lock(&env_lock); - - for (i = 0; i < sdbd_config_element_cnt && sdbd_config[i].key; i++) { - if (!strcmp(key,sdbd_config[i].key)) { - len = strlen(sdbd_config[i].value); - memcpy(value, sdbd_config[i].value, len + 1); - D("property get key=%s, value=%s\n", key, value); - mutex_unlock(&env_lock); - return len; - } - } - - if(default_value) { - len = strlen(default_value); - memcpy(value, default_value, len + 1); - D("by default, property get key=%s, value=%s\n", key, value); - } - mutex_unlock(&env_lock); - return len; -} - -int property_list(void (*propfn)(const char *key, const char *value, void *cookie), - void *cookie) -{ - return 0; -} - -#elif defined(HAVE_LIBC_SYSTEM_PROPERTIES) - -#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ -//#include - -int property_set(const char *key, const char *value) -{ - return __system_property_set(key, value); -} - -int property_get(const char *key, char *value, const char *default_value) -{ - int len; - - len = __system_property_get(key, value); - if(len > 0) { - return len; - } - - if(default_value) { - len = strlen(default_value); - memcpy(value, default_value, len + 1); - } - return len; -} - -int property_list(void (*propfn)(const char *key, const char *value, void *cookie), - void *cookie) -{ - char name[PROP_NAME_MAX]; - char value[PROP_VALUE_MAX]; - const prop_info *pi; - unsigned n; - - for(n = 0; (pi = __system_property_find_nth(n)); n++) { - __system_property_read(pi, name, value); - propfn(name, value, cookie); - } - return 0; -} - -#elif defined(HAVE_SYSTEM_PROPERTY_SERVER) - -/* - * The Linux simulator provides a "system property server" that uses IPC - * to set/get/list properties. The file descriptor is shared by all - * threads in the process, so we use a mutex to ensure that requests - * from multiple threads don't get interleaved. - */ -#include -#include -#include -#include -#include - -static pthread_once_t gInitOnce = PTHREAD_ONCE_INIT; -static pthread_mutex_t gPropertyFdLock = PTHREAD_MUTEX_INITIALIZER; -static int gPropFd = -1; - -/* - * Connect to the properties server. - * - * Returns the socket descriptor on success. - */ -static int connectToServer(const char* fileName) -{ - int sock = -1; - int cc; - - struct sockaddr_un addr; - - sock = socket(AF_UNIX, SOCK_STREAM, 0); - if (sock < 0) { - D("UNIX domain socket create failed (errno=%d)\n", errno); - return -1; - } - - /* connect to socket; fails if file doesn't exist */ - strcpy(addr.sun_path, fileName); // max 108 bytes - addr.sun_family = AF_UNIX; - cc = connect(sock, (struct sockaddr*) &addr, SUN_LEN(&addr)); - if (cc < 0) { - // ENOENT means socket file doesn't exist - // ECONNREFUSED means socket exists but nobody is listening - D("AF_UNIX connect failed for '%s': errno:%d\n", - fileName, errno); - sdb_close(sock); - return -1; - } - - return sock; -} - -/* - * Perform one-time initialization. - */ -static void init(void) -{ - assert(gPropFd == -1); - - gPropFd = connectToServer(SYSTEM_PROPERTY_PIPE_NAME); - if (gPropFd < 0) { - D("not connected to system property server\n"); - } else { - D("Connected to system property server\n"); - } -} - -int property_get(const char *key, char *value, const char *default_value) -{ - char sendBuf[1+PROPERTY_KEY_MAX]; - char recvBuf[1+PROPERTY_VALUE_MAX]; - int len = -1; - - D("PROPERTY GET [%s]\n", key); - - pthread_once(&gInitOnce, init); - if (gPropFd < 0) { - /* this mimics the behavior of the device implementation */ - if (default_value != NULL) { - strcpy(value, default_value); - len = strlen(value); - } - return len; - } - - if (strlen(key) >= PROPERTY_KEY_MAX) return -1; - - memset(sendBuf, 0xdd, sizeof(sendBuf)); // placate valgrind - - sendBuf[0] = (char) kSystemPropertyGet; - strcpy(sendBuf+1, key); - - pthread_mutex_lock(&gPropertyFdLock); - if (sdb_write(gPropFd, sendBuf, sizeof(sendBuf)) != sizeof(sendBuf)) { - pthread_mutex_unlock(&gPropertyFdLock); - return -1; - } - if (sdb_read(gPropFd, recvBuf, sizeof(recvBuf)) != sizeof(recvBuf)) { - pthread_mutex_unlock(&gPropertyFdLock); - return -1; - } - pthread_mutex_unlock(&gPropertyFdLock); - - /* first byte is 0 if value not defined, 1 if found */ - if (recvBuf[0] == 0) { - if (default_value != NULL) { - strcpy(value, default_value); - len = strlen(value); - } else { - /* - * If the value isn't defined, hand back an empty string and - * a zero length, rather than a failure. This seems wrong, - * since you can't tell the difference between "undefined" and - * "defined but empty", but it's what the device does. - */ - value[0] = '\0'; - len = 0; - } - } else if (recvBuf[0] == 1) { - strcpy(value, recvBuf+1); - len = strlen(value); - } else { - D("Got strange response to property_get request (%d)\n", - recvBuf[0]); - assert(0); - return -1; - } - D("PROP [found=%d def='%s'] (%d) [%s]: [%s]\n", - recvBuf[0], default_value, len, key, value); - - return len; -} - - -int property_set(const char *key, const char *value) -{ - char sendBuf[1+PROPERTY_KEY_MAX+PROPERTY_VALUE_MAX]; - char recvBuf[1]; - int result = -1; - - D("PROPERTY SET [%s]: [%s]\n", key, value); - - pthread_once(&gInitOnce, init); - if (gPropFd < 0) - return -1; - - if (strlen(key) >= PROPERTY_KEY_MAX) return -1; - if (strlen(value) >= PROPERTY_VALUE_MAX) return -1; - - memset(sendBuf, 0xdd, sizeof(sendBuf)); // placate valgrind - - sendBuf[0] = (char) kSystemPropertySet; - strcpy(sendBuf+1, key); - strcpy(sendBuf+1+PROPERTY_KEY_MAX, value); - - pthread_mutex_lock(&gPropertyFdLock); - if (sdb_write(gPropFd, sendBuf, sizeof(sendBuf)) != sizeof(sendBuf)) { - pthread_mutex_unlock(&gPropertyFdLock); - return -1; - } - if (sdb_read(gPropFd, recvBuf, sizeof(recvBuf)) != sizeof(recvBuf)) { - pthread_mutex_unlock(&gPropertyFdLock); - return -1; - } - pthread_mutex_unlock(&gPropertyFdLock); - - if (recvBuf[0] != 1) - return -1; - return 0; -} - -int property_list(void (*propfn)(const char *key, const char *value, void *cookie), - void *cookie) -{ - D("PROPERTY LIST\n"); - pthread_once(&gInitOnce, init); - if (gPropFd < 0) - return -1; - - return 0; -} - -#else - -/* SUPER-cheesy place-holder implementation for Win32 */ -#define HAVE_PTHREADS /*tizen specific */ -#include -#include "threads.h" - -static mutex_t env_lock = MUTEX_INITIALIZER; - -int property_get(const char *key, char *value, const char *default_value) -{ - char ename[PROPERTY_KEY_MAX + 6]; - char *p; - int len; - - len = strlen(key); - if(len >= PROPERTY_KEY_MAX) return -1; - memcpy(ename, "PROP_", 5); - memcpy(ename + 5, key, len + 1); - - mutex_lock(&env_lock); - - p = getenv(ename); - if(p == 0) p = ""; - len = strlen(p); - if(len >= PROPERTY_VALUE_MAX) { - len = PROPERTY_VALUE_MAX - 1; - } - - if((len == 0) && default_value) { - len = strlen(default_value); - memcpy(value, default_value, len + 1); - } else { - memcpy(value, p, len); - value[len] = 0; - } - - mutex_unlock(&env_lock); - D("get [key=%s value='%s:%s']\n", key, ename, value); - return len; -} - - -int property_set(const char *key, const char *value) -{ - char ename[PROPERTY_KEY_MAX + 6]; - char *p; - int len; - int r; - - if(strlen(value) >= PROPERTY_VALUE_MAX) return -1; - - len = strlen(key); - if(len >= PROPERTY_KEY_MAX) return -1; - memcpy(ename, "PROP_", 5); - memcpy(ename + 5, key, len + 1); - - mutex_lock(&env_lock); -#ifdef HAVE_MS_C_RUNTIME - { - char temp[256]; - snprintf( temp, sizeof(temp), "%s=%s", ename, value); - putenv(temp); - r = 0; - } -#else - r = setenv(ename, value, 1); -#endif - mutex_unlock(&env_lock); - D("set [key=%s value='%s%s']\n", key, ename, value); - return r; -} - -int property_list(void (*propfn)(const char *key, const char *value, void *cookie), - void *cookie) -{ - return 0; -} - -#endif diff --git a/src/properties.h b/src/properties.h deleted file mode 100644 index 08f9d18..0000000 --- a/src/properties.h +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the License); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an AS IS BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __CUTILS_PROPERTIES_H -#define __CUTILS_PROPERTIES_H - -#ifdef __cplusplus -extern "C" { -#endif - -/* System properties are *small* name value pairs managed by the -** property service. If your data doesn't fit in the provided -** space it is not appropriate for a system property. -** -** WARNING: system/bionic/include/sys/system_properties.h also defines -** these, but with different names. (TODO: fix that) -*/ -#define PROPERTY_KEY_MAX 32 -#define PROPERTY_VALUE_MAX 92 - -/* property_get: returns the length of the value which will never be -** greater than PROPERTY_VALUE_MAX - 1 and will always be zero terminated. -** (the length does not include the terminating zero). -** -** If the property read fails or returns an empty value, the default -** value is used (if nonnull). -*/ -int property_get(const char *key, char *value, const char *default_value); - -/* property_set: returns 0 on success, < 0 on failure -*/ -int property_set(const char *key, const char *value); - -int property_list(void (*propfn)(const char *key, const char *value, void *cookie), void *cookie); - -#ifdef HAVE_SYSTEM_PROPERTY_SERVER -/* - * We have an external property server instead of built-in libc support. - * Used by the simulator. - */ -#define SYSTEM_PROPERTY_PIPE_NAME "/tmp/sdb-sysporp" - -enum { - kSystemPropertyUnknown = 0, - kSystemPropertyGet, - kSystemPropertySet, - kSystemPropertyList -}; -#endif /*HAVE_SYSTEM_PROPERTY_SERVER*/ - - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/sdb.c b/src/sdb.c index ac7f573..4eb7389 100644 --- a/src/sdb.c +++ b/src/sdb.c @@ -1196,11 +1196,6 @@ void start_device_log(void) const char* p_path = getenv("SDBD_LOG_PATH"); // read the trace mask from persistent property persist.sdb.trace_mask // give up if the property is not set or cannot be parsed -#if 0 /* tizen specific */ - property_get("persist.sdb.trace_mask", value, ""); - if (sscanf(value, "%x", &sdb_trace_mask) != 1) - return; -#endif if ((p_trace == NULL ) && !is_enable_sdbd_log()) { return; diff --git a/src/services.c b/src/services.c index 4395a1d..b0f2e08 100644 --- a/src/services.c +++ b/src/services.c @@ -133,52 +133,8 @@ static void recover_service(int s, void *cookie) sdb_close(fd); } -void restart_root_service(int fd, void *cookie) -{ - char buf[100]; - char value[PROPERTY_VALUE_MAX]; - - if (getuid() == 0) { - snprintf(buf, sizeof(buf), "sdbd is already running as root\n"); - writex(fd, buf, strlen(buf)); - sdb_close(fd); - } else { - property_get("ro.debuggable", value, ""); - if (strcmp(value, "1") != 0) { - snprintf(buf, sizeof(buf), "sdbd cannot run as root in production builds\n"); - writex(fd, buf, strlen(buf)); - sdb_close(fd); - return; - } - - property_set("service.sdb.root", "1"); - snprintf(buf, sizeof(buf), "restarting sdbd as root\n"); - writex(fd, buf, strlen(buf)); - sdb_close(fd); - } -} #endif -void restart_tcp_service(int fd, void *cookie) -{ - char buf[100]; - char value[PROPERTY_VALUE_MAX]; - int port = (int)cookie; - - if (port <= 0) { - snprintf(buf, sizeof(buf), "invalid port\n"); - writex(fd, buf, strlen(buf)); - sdb_close(fd); - return; - } - - snprintf(value, sizeof(value), "%d", port); - property_set("service.sdb.tcp.port", value); - snprintf(buf, sizeof(buf), "restarting in TCP mode port: %d\n", port); - writex(fd, buf, strlen(buf)); - sdb_close(fd); -} - static int is_support_rootonoff() { return (!strncmp(g_capabilities.rootonoff_support, PLUGIN_RET_ENABLED, strlen(PLUGIN_RET_ENABLED))); @@ -254,16 +210,6 @@ void get_tzplatform_env(int fd, void *cookie) { sdb_close(fd); } -void restart_usb_service(int fd, void *cookie) -{ - char buf[100]; - - property_set("service.sdb.tcp.port", "0"); - snprintf(buf, sizeof(buf), "restarting in USB mode\n"); - writex(fd, buf, strlen(buf)); - sdb_close(fd); -} - void reboot_service(int fd, void *arg) { #if 0 diff --git a/src/sysdeps.h b/src/sysdeps.h index d99a2d4..a72e798 100644 --- a/src/sysdeps.h +++ b/src/sysdeps.h @@ -278,7 +278,6 @@ static __inline__ int sdb_is_absolute_host_path( const char* path ) #include "fdevent.h" #include "sockets.h" -#include "properties.h" // tizen specific #include #include #include diff --git a/src/transport_local.c b/src/transport_local.c index a6adb0b..ed61ec1 100644 --- a/src/transport_local.c +++ b/src/transport_local.c @@ -612,14 +612,6 @@ void local_init(int port) #else /* For the sdbd daemon in the system image we need to distinguish * between the device, and the emulator. */ -#if 0 /* tizen specific */ - char is_qemu[PROPERTY_VALUE_MAX]; - property_get("ro.kernel.qemu", is_qemu, ""); - if (!strcmp(is_qemu, "1")) { - /* Running inside the emulator: use QEMUD pipe as the transport. */ - func = qemu_socket_thread; - } else -#endif { /* Running inside the device: use TCP socket as the transport. */ func = server_socket_thread; -- 2.7.4