From 9c03e3e728f07c4aa9c401646cdbca3127342944 Mon Sep 17 00:00:00 2001 From: Srivardhan Hebbar Date: Mon, 23 Mar 2015 19:44:03 +0100 Subject: [PATCH] ecore_con: clean up, refactor and unpdate documentation. Summary: 1. Have refactored code in ecore_con_local.c 2. Changed env variable from "TMP" to "TMPDIR". 3. Added check for negetive port number when type is ECORE_CON_LOCAL_USER. 4. Added check to check TMPDIR before assuming /tmp as temp directory. 5. Updated documentation in Ecore_Con.h explaining about local socket and port number. Signed-off-by: Srivardhan Hebbar Reviewers: cedric Reviewed By: cedric Subscribers: cedric Differential Revision: https://phab.enlightenment.org/D2194 Signed-off-by: Cedric BAIL --- src/lib/ecore_con/Ecore_Con.h | 32 ++++++++---- src/lib/ecore_con/ecore_con_local.c | 101 ++++++++++++++++++++---------------- 2 files changed, 79 insertions(+), 54 deletions(-) diff --git a/src/lib/ecore_con/Ecore_Con.h b/src/lib/ecore_con/Ecore_Con.h index fee1889..2c99079 100644 --- a/src/lib/ecore_con/Ecore_Con.h +++ b/src/lib/ecore_con/Ecore_Con.h @@ -1123,9 +1123,16 @@ EAPI void ecore_con_socks_apply_always(Ecore_Con_Socks *ecs); * The socket on which the server listens depends on the connection * type: * @li If @a type is @c ECORE_CON_LOCAL_USER, the server will listen on - * the Unix socket "~/.ecore/[name]/[port]". + * the Unix socket. The path to the socket is taken from XDG_RUNTIME_DIR, + * if that is not set, then from HOME, even if this is not set, then from + * TMPDIR. If none is set, then path would be /tmp. From this path socket + * would be created as "[path]/.ecore/[name]/[port]". If port is negetive, + * then "[path]/.ecore/[name]". * @li If @a type is @c ECORE_CON_LOCAL_SYSTEM, the server will listen - * on Unix socket "/tmp/.ecore_service|[name]|[port]". + * on Unix socket "/tmp/.ecore_service|[name]|[port]". If port is negetive, + * then "/tmp/.ecore_service|[name]". + * @li If @a type is @c ECORE_CON_LOCAL_ABSTRACT, then port number is not + * considered while creating the socket. * @li If @a type is @c ECORE_CON_REMOTE_TCP, the server will listen * on TCP port @c port. * @@ -1154,14 +1161,19 @@ EAPI Ecore_Con_Server *ecore_con_server_add(Ecore_Con_Type type, * @return A new Ecore_Con_Server. * * The socket to which the connection is made depends on the connection type: - * @li If @a type is @c ECORE_CON_LOCAL_USER, the function will - * connect to the server at the Unix socket - * "~/.ecore/[name]/[port]". - * @li If @a type is @c ECORE_CON_LOCAL_SYSTEM, the function will - * connect to the server at the Unix socket - * "/tmp/.ecore_service|[name]|[port]". - * @li If @a type is @c ECORE_CON_REMOTE_TCP, the function will - * connect to the server at the TCP port "[name]:[port]". + * @li If @a type is @c ECORE_CON_LOCAL_USER, the server will conect to + * the Unix socket. The path to the socket is taken from XDG_RUNTIME_DIR, + * if that is not set, then from HOME, even if this is not set, then from + * TMPDIR. If none is set, then path would be /tmp. From this path the + * function would connect to socket at "[path]/.ecore/[name]/[port]". If + * port is negetive, then to socket at "[path]/.ecore/[name]". + * @li If @a type is @c ECORE_CON_LOCAL_SYSTEM, the server will connect to + * Unix socket at "/tmp/.ecore_service|[name]|[port]". If port is negetive, + * then to Unix socket at "/tmp/.ecore_service|[name]". + * @li If @a type is @c ECORE_CON_LOCAL_ABSTRACT, then port number is not + * considered while connecting to socket. + * @li If @a type is @c ECORE_CON_REMOTE_TCP, the server will listen + * on TCP port @c port. * * More information about the @p type can be found at @ref _Ecore_Con_Type. * diff --git a/src/lib/ecore_con/ecore_con_local.c b/src/lib/ecore_con/ecore_con_local.c index 9af5ca5..dc9a78e 100644 --- a/src/lib/ecore_con/ecore_con_local.c +++ b/src/lib/ecore_con/ecore_con_local.c @@ -37,6 +37,24 @@ static int _ecore_con_local_init_count = 0; +static inline const char *_ecore_con_get_tmpdir() +{ + const char *tmpdir = "/tmp"; + const char *dir = getenv("TMPDIR"); + + if (!dir) return tmpdir; + return dir; +} + +static const char *_ecore_con_local_path_get() +{ + const char *homedir = getenv("XDG_RUNTIME_DIR"); + if (!homedir) homedir = getenv("HOME"); + if (!homedir) homedir = _ecore_con_get_tmpdir(); + + return homedir; +} + int ecore_con_local_init(void) { @@ -77,33 +95,23 @@ ecore_con_local_connect(Ecore_Con_Server *obj, #if defined(HAVE_GETUID) && defined(HAVE_GETEUID) if (getuid() == geteuid()) #endif - { - homedir = getenv("XDG_RUNTIME_DIR"); - if (!homedir) - { - 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); - } + homedir = _ecore_con_local_path_get(); #if defined(HAVE_GETUID) && defined(HAVE_GETEUID) else { struct passwd *pw = getpwent(); - if ((!pw) || (!pw->pw_dir)) - snprintf(buf, sizeof(buf), "/tmp/%s/%i", svr->name, - svr->port); - else - snprintf(buf, sizeof(buf), "%s/.ecore/%s/%i", pw->pw_dir, svr->name, - svr->port); + if ((!pw) || (!pw->pw_dir)) homedir = "/tmp"; + else homedir = pw->pw_dir; } #endif + + if (svr->port < 0) + snprintf(buf, sizeof(buf), "%s/.ecore/%s", + homedir, svr->name); + else + snprintf(buf, sizeof(buf), "%s/.ecore/%s/%i", + homedir, svr->name, svr->port); } else if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_SYSTEM) { @@ -115,15 +123,22 @@ ecore_con_local_connect(Ecore_Con_Server *obj, buf[sizeof(buf) - 1] = 0; } else - snprintf(buf, sizeof(buf), "/tmp/.ecore_service|%s", svr->name); + { + homedir = _ecore_con_get_tmpdir(); + snprintf(buf, sizeof(buf), "%s/.ecore_service|%s", + homedir, 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); + { + homedir = _ecore_con_get_tmpdir(); + snprintf(buf, sizeof(buf), "%s/.ecore_service|%s|%i", + homedir, svr->name, svr->port); + } } } else if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_ABSTRACT) @@ -225,18 +240,8 @@ ecore_con_local_listen( #if defined(HAVE_GETUID) && defined(HAVE_GETEUID) if (getuid() == geteuid()) #endif - { - homedir = getenv("XDG_RUNTIME_DIR"); - if (!homedir) - { - homedir = getenv("HOME"); - if (!homedir) - { - homedir = getenv("TMP"); - if (!homedir) homedir = "/tmp"; - } - } - } + homedir = _ecore_con_local_path_get(); + #if defined(HAVE_GETUID) && defined(HAVE_GETEUID) else { @@ -259,12 +264,13 @@ ecore_con_local_listen( if (mkdir(buf, mask) < 0) ERR("mkdir '%s' failed", buf); } - snprintf(buf, - sizeof(buf), - "%s/.ecore/%s/%i", - homedir, - svr->name, - svr->port); + if (svr->port < 0) + snprintf(buf, sizeof(buf), "%s/.ecore/%s", + homedir, svr->name); + else + 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_TYPE) == ECORE_CON_LOCAL_SYSTEM) @@ -278,15 +284,22 @@ ecore_con_local_listen( buf[sizeof(buf) - 1] = 0; } else - snprintf(buf, sizeof(buf), "/tmp/.ecore_service|%s", svr->name); + { + homedir = _ecore_con_get_tmpdir(); + snprintf(buf, sizeof(buf), "%s/.ecore_service|%s", + homedir, 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); + { + homedir = _ecore_con_get_tmpdir(); + snprintf(buf, sizeof(buf), "%s/.ecore_service|%s|%i", + homedir, svr->name, svr->port); + } } } else if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_ABSTRACT) -- 2.7.4