From 7b448ecab8fb3bb1e3d8b10aa1a3540b5693487d Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Wed, 25 May 2016 21:31:33 +0900 Subject: [PATCH] Add socket timeout feature The socket timeout period is 5.2s. If the socket timeout file exists, the caller process uses the system socket timeout period. This feature is enabled in the tv profile. - Add internal APIs aul_sock_set_sock_option() aul_sock_get_rcv_timeval() Change-Id: I56a9580b221165b4277144d7b8716e7f76f85b65 Signed-off-by: Hwankyu Jhun --- CMakeLists.txt | 4 +++ include/aul_sock.h | 11 +++++++ packaging/aul.spec | 6 ++++ src/aul_sock.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 101 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 088b6d0..acca6f9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,6 +23,10 @@ ENDIF (with_x11) IF(TIZEN_FEATURE_DEFAULT_USER) ADD_DEFINITIONS("-DTIZEN_FEATURE_DEFAULT_USER") ENDIF(TIZEN_FEATURE_DEFAULT_USER) +IF(TIZEN_FEATURE_SOCKET_TIMEOUT) + ADD_DEFINITIONS("-DTIZEN_FEATURE_SOCKET_TIMEOUT") +ENDIF(TIZEN_FEATURE_SOCKET_TIMEOUT) + FOREACH(flag ${libpkgs_CFLAGS}) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") ENDFOREACH(flag) diff --git a/include/aul_sock.h b/include/aul_sock.h index 372d17a..14b3831 100644 --- a/include/aul_sock.h +++ b/include/aul_sock.h @@ -91,3 +91,14 @@ int aul_sock_recv_reply_sock_fd(int fd, int *ret_fd, int num_of_ret_fd); * This API is only for Appfw internally. */ int aul_sock_recv_reply_pkt(int fd, app_pkt_t **pkt); + +/* + * This API is only for Appfw internally. + */ +int aul_sock_set_sock_option(int fd, int cli); + +/* + * This API is only for Appfw internally. + */ +struct timeval aul_sock_get_rcv_timeval(void); + diff --git a/packaging/aul.spec b/packaging/aul.spec index ea5db72..76cbbfe 100644 --- a/packaging/aul.spec +++ b/packaging/aul.spec @@ -40,8 +40,10 @@ BuildRequires: pkgconfig(libxml-2.0) %if "%{?profile}" == "tv" %define tizen_feature_default_user 1 +%define tizen_feature_socket_timeout 1 %else %define tizen_feature_default_user 0 +%define tizen_feature_socket_timeout 0 %endif %description @@ -76,6 +78,9 @@ CFLAGS="%{optflags} -D__emul__"; export CFLAGS %if 0%{?tizen_feature_default_user} TIZEN_FEATURE_DEFAULT_USER=ON %endif +%if 0%{?tizen_feature_socket_timeout} +TIZEN_FEATURE_SOCKET_TIMEOUT=ON +%endif MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'` %cmake -DFULLVER=%{version} \ @@ -87,6 +92,7 @@ MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'` -Dwith_x11=TRUE \ %endif -DTIZEN_FEATURE_DEFAULT_USER:BOOL=${TIZEN_FEATURE_DEFAULT_USER} \ + -DTIZEN_FEATURE_SOCKET_TIMEOUT:BOOL=${TIZEN_FEATURE_SOCKET_TIMEOUT} \ . %__make %{?_smp_mflags} diff --git a/src/aul_sock.c b/src/aul_sock.c index 9ec6e86..145fd2b 100644 --- a/src/aul_sock.c +++ b/src/aul_sock.c @@ -26,6 +26,10 @@ #ifdef TIZEN_FEATURE_DEFAULT_USER #include #endif +#ifdef TIZEN_FEATURE_SOCKET_TIMEOUT +#include +#include +#endif #include "aul_api.h" #include "aul_sock.h" @@ -39,20 +43,89 @@ static uid_t default_uid; static int default_uid_initialized; #endif +#ifdef TIZEN_FEATURE_SOCKET_TIMEOUT +static int socket_timeout_initialized; +#endif + +static struct timeval tv = { 5, 200 * 1000 }; /* 5.2 */ static int __connect_client_sock(int sockfd, const struct sockaddr *saptr, socklen_t salen, int nsec); -static inline void __set_sock_option(int fd, int cli) +#ifdef TIZEN_FEATURE_SOCKET_TIMEOUT +static void __set_timeval(double sec) +{ + char buf[12]; + gchar *ptr = NULL; + + snprintf(buf, sizeof(buf), "%.3f", sec); + tv.tv_sec = g_ascii_strtoull(buf, &ptr, 10); + tv.tv_usec = g_ascii_strtoull(ptr + 1, &ptr, 10) * 1000; + _D("tv_sec: %d, tv_usec: %d", tv.tv_sec, tv.tv_usec); +} + +static void __socket_timeout_vconf_cb(keynode_t *key, void *data) +{ + const char *name; + double sec; + + name = vconf_keynode_get_name(key); + if (name && strcmp(name, VCONFKEY_AUL_SOCKET_TIMEOUT) == 0) { + sec = vconf_keynode_get_dbl(key); + __set_timeval(sec); + } +} + +static void __init_socket_timeout(void) +{ + int r; + double sec = 5.2f; + + r = access("/run/aul/.socket_timeout", F_OK); + if (r < 0) { + socket_timeout_initialized = 1; + return; + } + + r = vconf_get_dbl(VCONFKEY_AUL_SOCKET_TIMEOUT, &sec); + if (r < 0) + _D("Failed to get vconf: %s", VCONFKEY_AUL_SOCKET_TIMEOUT); + + r = vconf_notify_key_changed(VCONFKEY_AUL_SOCKET_TIMEOUT, + __socket_timeout_vconf_cb, NULL); + if (r < 0) { + _E("Failed to register callback for %s", + VCONFKEY_AUL_SOCKET_TIMEOUT); + return; + } + + __set_timeval(sec); + socket_timeout_initialized = 1; +} +#endif + +API struct timeval aul_sock_get_rcv_timeval(void) +{ + return tv; +} + +API int aul_sock_set_sock_option(int fd, int cli) { int size; - struct timeval tv = { 5, 200 * 1000 }; /* 5.2 sec */ size = AUL_SOCK_MAXBUFF; setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size)); setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size)); - if (cli) + + if (cli) { +#ifdef TIZEN_FEATURE_SOCKET_TIMEOUT + if (!socket_timeout_initialized) + __init_socket_timeout(); +#endif setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); + } + + return 0; } API int aul_sock_create_server(int pid, uid_t uid) @@ -130,7 +203,7 @@ API int aul_sock_create_server(int pid, uid_t uid) return -1; } - __set_sock_option(fd, 0); + aul_sock_set_sock_option(fd, 0); if (listen(fd, 128) == -1) { _E("listen error"); @@ -215,7 +288,7 @@ retry_con: return -1; } - __set_sock_option(fd, 1); + aul_sock_set_sock_option(fd, 1); return fd; } @@ -436,7 +509,7 @@ API app_pkt_t *aul_sock_recv_pkt(int fd, int *clifd, struct ucred *cr) return NULL; } - __set_sock_option(*clifd, 1); + aul_sock_set_sock_option(*clifd, 1); retry_recv: /* receive header(cmd, datalen) */ @@ -717,7 +790,7 @@ retry_con: return -1; } - __set_sock_option(fd, 1); + aul_sock_set_sock_option(fd, 1); return fd; } -- 2.7.4