From: caro Date: Mon, 4 Jan 2010 19:35:21 +0000 (+0000) Subject: * put local code in its own file. It will be better X-Git-Tag: build/2012-07-04.173327~2326 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ae33d1e6a9681d1d9846dd07e3db6de3cc88b279;p=profile%2Fivi%2Fecore.git * put local code in its own file. It will be better when i'll add the Windows XP code, rather than plenty #ifdef in te code * remove some extra EAPI in code and useless Ecore_Data.h I've tested and it seems to work. If you see problems with local connections, please report in that thread git-svn-id: http://svn.enlightenment.org/svn/e/trunk/ecore@44888 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33 --- diff --git a/src/lib/ecore_con/Makefile.am b/src/lib/ecore_con/Makefile.am index 69e6684..2b85f7b 100644 --- a/src/lib/ecore_con/Makefile.am +++ b/src/lib/ecore_con/Makefile.am @@ -17,7 +17,8 @@ libecore_con_la_SOURCES = \ ecore_con.c \ ecore_con_dns.c \ ecore_con_ssl.c \ -ecore_con_url.c +ecore_con_url.c \ +ecore_con_local.c if HAVE_CARES libecore_con_la_SOURCES += ecore_con_ares.c diff --git a/src/lib/ecore_con/ecore_con.c b/src/lib/ecore_con/ecore_con.c index 562c515..47dbb74 100644 --- a/src/lib/ecore_con/ecore_con.c +++ b/src/lib/ecore_con/ecore_con.c @@ -28,9 +28,6 @@ #ifdef HAVE_NETINET_IN_H # include #endif -#ifdef HAVE_WINSOCK2_H -# include -#endif static void _ecore_con_cb_tcp_connect(void *data, Ecore_Con_Info *info); static void _ecore_con_cb_udp_connect(void *data, Ecore_Con_Info *info); @@ -66,8 +63,6 @@ EAPI int ECORE_CON_EVENT_SERVER_DATA = 0; static Eina_List *servers = NULL; static int _ecore_con_init_count = 0; int _ecore_con_log_dom = -1; -#define LENGTH_OF_SOCKADDR_UN(s) (strlen((s)->sun_path) + (size_t)(((struct sockaddr_un *)NULL)->sun_path)) -#define LENGTH_OF_ABSTRACT_SOCKADDR_UN(s, path) (strlen(path) + 1 + (size_t)(((struct sockaddr_un *)NULL)->sun_path)) /** * @defgroup Ecore_Con_Lib_Group Ecore Connection Library Functions @@ -176,10 +171,6 @@ ecore_con_server_add(Ecore_Con_Type compl_type, const char *name, int port, { Ecore_Con_Server *svr; Ecore_Con_Type type; - struct sockaddr_un socket_unix; - struct linger lin; - char buf[4096]; - mode_t pmode; if (port < 0) return NULL; /* local user socket: FILE: ~/.ecore/[name]/[port] */ @@ -205,87 +196,8 @@ ecore_con_server_add(Ecore_Con_Type compl_type, const char *name, int port, if ((type == ECORE_CON_LOCAL_USER) || (type == ECORE_CON_LOCAL_SYSTEM) || (type == ECORE_CON_LOCAL_ABSTRACT)) { - const char *homedir; - struct stat st; - mode_t mask; - int socket_unix_len; - - if (!name) goto error; - mask = S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH; - - if (type == ECORE_CON_LOCAL_USER) - { - homedir = getenv("HOME"); - if (!homedir) homedir = getenv("TMP"); - if (!homedir) homedir = "/tmp"; - mask = S_IRUSR | S_IWUSR | S_IXUSR; - snprintf(buf, sizeof(buf), "%s/.ecore", homedir); - if (stat(buf, &st) < 0) mkdir(buf, mask); - snprintf(buf, sizeof(buf), "%s/.ecore/%s", homedir, name); - if (stat(buf, &st) < 0) mkdir(buf, mask); - snprintf(buf, sizeof(buf), "%s/.ecore/%s/%i", homedir, name, port); - mask = S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH; - } - else if (type == ECORE_CON_LOCAL_SYSTEM) - { - mask = 0; - if (name[0] == '/') - { - if (port >= 0) - snprintf(buf, sizeof(buf), "%s|%i", name, port); - else - snprintf(buf, sizeof(buf), "%s", name); - } - else - snprintf(buf, sizeof(buf), "/tmp/.ecore_service|%s|%i", name, port); - } - else if (type == ECORE_CON_LOCAL_ABSTRACT) - strncpy(buf, name, sizeof(buf)); - pmode = umask(mask); - start: - svr->fd = socket(AF_UNIX, SOCK_STREAM, 0); - if (svr->fd < 0) goto error_umask; - if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) goto error_umask; - if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) goto error_umask; - lin.l_onoff = 1; - lin.l_linger = 0; - if (setsockopt(svr->fd, SOL_SOCKET, SO_LINGER, &lin, sizeof(struct linger)) < 0) goto error_umask; - socket_unix.sun_family = AF_UNIX; - if (type == ECORE_CON_LOCAL_ABSTRACT) - { -#ifdef HAVE_ABSTRACT_SOCKETS - /* . is a placeholder */ - snprintf(socket_unix.sun_path, sizeof(socket_unix.sun_path), ".%s", name); - /* first char null indicates abstract namespace */ - socket_unix.sun_path[0] = '\0'; - socket_unix_len = LENGTH_OF_ABSTRACT_SOCKADDR_UN(&socket_unix, name); -#else - ERR("Your system does not support abstract sockets!"); - goto error_umask; -#endif - } - else - { - strncpy(socket_unix.sun_path, buf, sizeof(socket_unix.sun_path)); - socket_unix_len = LENGTH_OF_SOCKADDR_UN(&socket_unix); - } - if (bind(svr->fd, (struct sockaddr *)&socket_unix, socket_unix_len) < 0) - { - if (((type == ECORE_CON_LOCAL_USER) || (type == ECORE_CON_LOCAL_SYSTEM)) && - (connect(svr->fd, (struct sockaddr *)&socket_unix, socket_unix_len) < 0) && - (unlink(buf) >= 0)) - goto start; - else - goto error_umask; - } - if (listen(svr->fd, 4096) < 0) goto error_umask; - svr->path = strdup(buf); - if (!svr->path) goto error_umask; - svr->fd_handler = - ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ, - _ecore_con_svr_handler, svr, NULL, NULL); - umask(pmode); - if (!svr->fd_handler) goto error; + /* Local */ + if (!ecore_con_local_listen(svr, _ecore_con_svr_handler, svr)) goto error; } if (type == ECORE_CON_REMOTE_TCP || type == ECORE_CON_REMOTE_NODELAY) @@ -304,8 +216,6 @@ ecore_con_server_add(Ecore_Con_Type compl_type, const char *name, int port, return svr; - error_umask: - umask(pmode); error: if (svr->name) free(svr->name); if (svr->path) free(svr->path); @@ -351,9 +261,6 @@ ecore_con_server_connect(Ecore_Con_Type compl_type, const char *name, int port, { Ecore_Con_Server *svr; Ecore_Con_Type type; - struct sockaddr_un socket_unix; - int curstate = 0; - char buf[4096]; if (!name) return NULL; /* local user socket: FILE: ~/.ecore/[name]/[port] */ @@ -381,90 +288,8 @@ ecore_con_server_connect(Ecore_Con_Type compl_type, const char *name, int port, if ((type == ECORE_CON_LOCAL_USER) || (type == ECORE_CON_LOCAL_SYSTEM) || (type == ECORE_CON_LOCAL_ABSTRACT)) { - const char *homedir; - int socket_unix_len; - - if (type == ECORE_CON_LOCAL_USER) - { - homedir = getenv("HOME"); - if (!homedir) homedir = getenv("TMP"); - if (!homedir) homedir = "/tmp"; - snprintf(buf, sizeof(buf), "%s/.ecore/%s/%i", homedir, name, port); - } - else if (type == ECORE_CON_LOCAL_SYSTEM) - { - if (port < 0) - { - if (name[0] == '/') - strncpy(buf, name, sizeof(buf)); - else - snprintf(buf, sizeof(buf), "/tmp/.ecore_service|%s", name); - } - else - { - if (name[0] == '/') - snprintf(buf, sizeof(buf), "%s|%i", name, port); - else - snprintf(buf, sizeof(buf), "/tmp/.ecore_service|%s|%i", name, port); - } - } - else if (type == ECORE_CON_LOCAL_ABSTRACT) - strncpy(buf, name, sizeof(buf)); - - svr->fd = socket(AF_UNIX, SOCK_STREAM, 0); - if (svr->fd < 0) goto error; - if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) goto error; - if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) goto error; - if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, &curstate, sizeof(curstate)) < 0) goto error; - socket_unix.sun_family = AF_UNIX; - - if (type == ECORE_CON_LOCAL_ABSTRACT) - { -#ifdef HAVE_ABSTRACT_SOCKETS - /* copy name insto sun_path, prefixed by null to indicate abstract namespace */ - snprintf(socket_unix.sun_path, sizeof(socket_unix.sun_path), ".%s", name); - socket_unix.sun_path[0] = '\0'; - socket_unix_len = LENGTH_OF_ABSTRACT_SOCKADDR_UN(&socket_unix, name); -#else - WRN("Your system does not support abstract sockets!"); - goto error; -#endif - } - else - { - strncpy(socket_unix.sun_path, buf, sizeof(socket_unix.sun_path)); - socket_unix_len = LENGTH_OF_SOCKADDR_UN(&socket_unix); - } - - if (connect(svr->fd, (struct sockaddr *)&socket_unix, socket_unix_len) < 0) - { - goto error; - } - svr->path = strdup(buf); - if (!svr->path) goto error; - - if (svr->type & ECORE_CON_SSL) - ecore_con_ssl_server_init(svr); - - svr->fd_handler = - ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ, - _ecore_con_cl_handler, svr, NULL, NULL); - if (!svr->fd_handler) goto error; - - if (!svr->delete_me) - { - /* we got our server! */ - Ecore_Con_Event_Server_Add *e; - - e = calloc(1, sizeof(Ecore_Con_Event_Server_Add)); - if (e) - { - svr->event_count++; - e->server = svr; - ecore_event_add(ECORE_CON_EVENT_SERVER_ADD, e, - _ecore_con_event_server_add_free, NULL); - } - } + /* Local */ + if (!ecore_con_local_connect(svr, _ecore_con_cl_handler, svr, _ecore_con_event_server_add_free)) goto error; } if (type == ECORE_CON_REMOTE_TCP || type == ECORE_CON_REMOTE_NODELAY) diff --git a/src/lib/ecore_con/ecore_con_ares.c b/src/lib/ecore_con/ecore_con_ares.c index 0a660ef..a6f2430 100644 --- a/src/lib/ecore_con/ecore_con_ares.c +++ b/src/lib/ecore_con/ecore_con_ares.c @@ -56,7 +56,7 @@ static int _ecore_con_info_cares_fd_cb(void *data, Ecore_Fd_Handler *fd_handler) static int _ecore_con_info_cares_timeout_cb(void *data); static void _ecore_con_info_cares_clean(void); -EAPI int +int ecore_con_info_init(void) { if (info_init == 0) @@ -74,7 +74,7 @@ ecore_con_info_init(void) return info_init; } -EAPI int +int ecore_con_info_shutdown(void) { info_init--; diff --git a/src/lib/ecore_con/ecore_con_info.c b/src/lib/ecore_con/ecore_con_info.c index 7bc9822..7d9ce93 100644 --- a/src/lib/ecore_con/ecore_con_info.c +++ b/src/lib/ecore_con/ecore_con_info.c @@ -69,14 +69,14 @@ static int _ecore_con_info_exit_handler(void *data, int type __UNUSED__, void *e static int info_init = 0; static CB_Data *info_slaves = NULL; -EAPI int +int ecore_con_info_init(void) { info_init++; return info_init; } -EAPI int +int ecore_con_info_shutdown(void) { info_init--; diff --git a/src/lib/ecore_con/ecore_con_local.c b/src/lib/ecore_con/ecore_con_local.c new file mode 100644 index 0000000..b685f04 --- /dev/null +++ b/src/lib/ecore_con/ecore_con_local.c @@ -0,0 +1,238 @@ +/* + * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2 + */ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "Ecore_Con.h" +#include "ecore_con_private.h" + + +#define LENGTH_OF_SOCKADDR_UN(s) (strlen((s)->sun_path) + (size_t)(((struct sockaddr_un *)NULL)->sun_path)) +#define LENGTH_OF_ABSTRACT_SOCKADDR_UN(s, path) (strlen(path) + 1 + (size_t)(((struct sockaddr_un *)NULL)->sun_path)) + + +static int _ecore_con_local_init_count = 0; + +int +ecore_con_local_init(void) +{ + if (++_ecore_con_local_init_count != 1) + return _ecore_con_local_init_count; + + return _ecore_con_local_init_count; +} + +int +ecore_con_local_shutdown(void) +{ + if (--_ecore_con_local_init_count != 0) + return _ecore_con_local_init_count; + + return _ecore_con_local_init_count; +} + +int +ecore_con_local_connect(Ecore_Con_Server *svr, + int (*cb_done)(void *data, Ecore_Fd_Handler *fd_handler), + void *data, + void (*cb_free)(void *data, void *ev)) +{ + char buf[4096]; + struct sockaddr_un socket_unix; + int curstate = 0; + const char *homedir; + int socket_unix_len; + + if (svr->type == ECORE_CON_LOCAL_USER) + { + homedir = getenv("HOME"); + if (!homedir) homedir = getenv("TMP"); + if (!homedir) homedir = "/tmp"; + snprintf(buf, sizeof(buf), "%s/.ecore/%s/%i", homedir, svr->name, svr->port); + } + else if (svr->type == ECORE_CON_LOCAL_SYSTEM) + { + if (svr->port < 0) + { + if (svr->name[0] == '/') + strncpy(buf, svr->name, sizeof(buf)); + else + snprintf(buf, sizeof(buf), "/tmp/.ecore_service|%s", svr->name); + } + else + { + if (svr->name[0] == '/') + snprintf(buf, sizeof(buf), "%s|%i", svr->name, svr->port); + else + snprintf(buf, sizeof(buf), "/tmp/.ecore_service|%s|%i", svr->name, svr->port); + } + } + else if (svr->type == ECORE_CON_LOCAL_ABSTRACT) + strncpy(buf, svr->name, sizeof(buf)); + + svr->fd = socket(AF_UNIX, SOCK_STREAM, 0); + if (svr->fd < 0) return 0; + if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) return 0; + if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) return 0; + if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, &curstate, sizeof(curstate)) < 0) return 0; + socket_unix.sun_family = AF_UNIX; + + if (svr->type == ECORE_CON_LOCAL_ABSTRACT) + { +#ifdef HAVE_ABSTRACT_SOCKETS + /* copy name insto sun_path, prefixed by null to indicate abstract namespace */ + snprintf(socket_unix.sun_path, sizeof(socket_unix.sun_path), ".%s", svr->name); + socket_unix.sun_path[0] = '\0'; + socket_unix_len = LENGTH_OF_ABSTRACT_SOCKADDR_UN(&socket_unix, svr->name); +#else + WRN("Your system does not support abstract sockets!"); + return 0; +#endif + } + else + { + strncpy(socket_unix.sun_path, buf, sizeof(socket_unix.sun_path)); + socket_unix_len = LENGTH_OF_SOCKADDR_UN(&socket_unix); + } + + if (connect(svr->fd, (struct sockaddr *)&socket_unix, socket_unix_len) < 0) + return 0; + svr->path = strdup(buf); + if (!svr->path) return 0; + + if (svr->type & ECORE_CON_SSL) + ecore_con_ssl_server_init(svr); + + svr->fd_handler = + ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ, + cb_done, svr, NULL, NULL); + if (!svr->fd_handler) return 0; + + if (!svr->delete_me) + { + /* we got our server! */ + Ecore_Con_Event_Server_Add *e; + + e = calloc(1, sizeof(Ecore_Con_Event_Server_Add)); + if (e) + { + svr->event_count++; + e->server = svr; + ecore_event_add(ECORE_CON_EVENT_SERVER_ADD, e, + cb_free, NULL); + } + } + + return 1; +} + +int +ecore_con_local_listen(Ecore_Con_Server *svr, + int (*cb_listen)(void *data, Ecore_Fd_Handler *fd_handler), + void *data) +{ + char buf[4096]; + struct sockaddr_un socket_unix; + struct linger lin; + mode_t pmode; + const char *homedir; + struct stat st; + mode_t mask; + int socket_unix_len; + + mask = S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH; + + if (svr->type == ECORE_CON_LOCAL_USER) + { + homedir = getenv("HOME"); + if (!homedir) homedir = getenv("TMP"); + if (!homedir) homedir = "/tmp"; + mask = S_IRUSR | S_IWUSR | S_IXUSR; + snprintf(buf, sizeof(buf), "%s/.ecore", homedir); + if (stat(buf, &st) < 0) mkdir(buf, mask); + snprintf(buf, sizeof(buf), "%s/.ecore/%s", homedir, svr->name); + if (stat(buf, &st) < 0) mkdir(buf, mask); + snprintf(buf, sizeof(buf), "%s/.ecore/%s/%i", homedir, svr->name, svr->port); + mask = S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH; + } + else if (svr->type == ECORE_CON_LOCAL_SYSTEM) + { + mask = 0; + if (svr->name[0] == '/') + { + if (svr->port >= 0) + snprintf(buf, sizeof(buf), "%s|%i", svr->name, svr->port); + else + snprintf(buf, sizeof(buf), "%s", svr->name); + } + else + snprintf(buf, sizeof(buf), "/tmp/.ecore_service|%s|%i", svr->name, svr->port); + } + else if (svr->type == ECORE_CON_LOCAL_ABSTRACT) + strncpy(buf, svr->name, sizeof(buf)); + pmode = umask(mask); + start: + svr->fd = socket(AF_UNIX, SOCK_STREAM, 0); + if (svr->fd < 0) goto error_umask; + if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) goto error_umask; + if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) goto error_umask; + lin.l_onoff = 1; + lin.l_linger = 0; + if (setsockopt(svr->fd, SOL_SOCKET, SO_LINGER, &lin, sizeof(struct linger)) < 0) goto error_umask; + socket_unix.sun_family = AF_UNIX; + if (svr->type == ECORE_CON_LOCAL_ABSTRACT) + { +#ifdef HAVE_ABSTRACT_SOCKETS + /* . is a placeholder */ + snprintf(socket_unix.sun_path, sizeof(socket_unix.sun_path), ".%s", svr->name); + /* first char null indicates abstract namespace */ + socket_unix.sun_path[0] = '\0'; + socket_unix_len = LENGTH_OF_ABSTRACT_SOCKADDR_UN(&socket_unix, svr->name); +#else + ERR("Your system does not support abstract sockets!"); + goto error_umask; +#endif + } + else + { + strncpy(socket_unix.sun_path, buf, sizeof(socket_unix.sun_path)); + socket_unix_len = LENGTH_OF_SOCKADDR_UN(&socket_unix); + } + if (bind(svr->fd, (struct sockaddr *)&socket_unix, socket_unix_len) < 0) + { + if (((svr->type == ECORE_CON_LOCAL_USER) || (svr->type == ECORE_CON_LOCAL_SYSTEM)) && + (connect(svr->fd, (struct sockaddr *)&socket_unix, socket_unix_len) < 0) && + (unlink(buf) >= 0)) + goto start; + else + goto error_umask; + } + if (listen(svr->fd, 4096) < 0) goto error_umask; + svr->path = strdup(buf); + if (!svr->path) goto error_umask; + svr->fd_handler = + ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ, + cb_listen, svr, NULL, NULL); + umask(pmode); + if (!svr->fd_handler) goto error; + + return 1; + + error_umask: + umask(pmode); + error: + return 0; +} diff --git a/src/lib/ecore_con/ecore_con_private.h b/src/lib/ecore_con/ecore_con_private.h index 17aebb2..86fb340 100644 --- a/src/lib/ecore_con/ecore_con_private.h +++ b/src/lib/ecore_con/ecore_con_private.h @@ -3,7 +3,6 @@ #include "ecore_private.h" #include "Ecore_Con.h" -#include "Ecore_Data.h" #define ECORE_MAGIC_CON_SERVER 0x77665544 #define ECORE_MAGIC_CON_CLIENT 0x77556677 @@ -159,6 +158,11 @@ struct _Ecore_Con_Info /* from ecore_con_dns.c */ int ecore_con_dns_init(void); int ecore_con_dns_shutdown(void); +/* from ecore_local.c */ +int ecore_con_local_init(void); +int ecore_con_local_shutdown(void); +int ecore_con_local_connect(Ecore_Con_Server *svr, int (*cb_done)(void *data, Ecore_Fd_Handler *fd_handler), void *data, void (*cb_free)(void *data, void *ev)); +int ecore_con_local_listen(Ecore_Con_Server *svr, int (*cb_listen)(void *data, Ecore_Fd_Handler *fd_handler), void *data); /* from ecore_con_info.c */ int ecore_con_info_init(void); int ecore_con_info_shutdown(void);