Thanks to Andrei I think I managed to merge my patches.
Alex On Thu, Mar 19, 2009 at 8:03 PM, Alex Cornejo <[email protected]> wrote: > Dammit, I just realized there was a bug in my implementation, here is a > patch to fix it. > > PS: I know Julien would want me to do something fancy with Git to create a > new merged patch. However I am clueless in git, if someone tells me how to > merge two commits into one I would be happy to resubmit the patch. > > Cheers, > > Alex > > > > On Thu, Mar 19, 2009 at 7:55 PM, Alex Cornejo <[email protected]> wrote: > >> Here are a couple of patches to implement the described fallback logic. >> >> Right now we query HOME, TMPDIR and /tmp, in that order. However its >> trivial to add more fallbacks (either from environment variables or fixed >> strings). >> >> Cheers, >> >> Alex >> >> >> On Thu, Mar 19, 2009 at 1:09 PM, Julien Danjou <[email protected]>wrote: >> >>> At 1237473819 time_t, Alex Cornejo wrote: >>> > That function would then take care of all the fallback logic and print >>> out >>> > any warning/error messages. What do you think? >>> >>> Seems ok to me. >>> >>> Cheers, >>> -- >>> Julien Danjou >>> // ᐰ <[email protected]> http://julien.danjou.info >>> // 9A0D 5FD9 EB42 22F6 8974 C95C A462 B51E C2FE E5CD >>> // When I get sad, I stop being sad and be awesome instead. True story. >>> >>> -----BEGIN PGP SIGNATURE----- >>> Version: GnuPG v1.4.9 (GNU/Linux) >>> >>> iEYEARECAAYFAknCfEEACgkQpGK1HsL+5c2XvQCdFT5rsskvLs35NIDX/ebPiofr >>> WpwAoKvoLKxh2dGoUeATdo3QKBD00njT >>> =BwEH >>> -----END PGP SIGNATURE----- >>> >>> >> >
From 34bcf162e035a00795f94063701e763f1fcf1103 Mon Sep 17 00:00:00 2001 From: Alex Cornejo <[email protected]> Date: Thu, 19 Mar 2009 20:59:40 -0400 Subject: [PATCH] Changed socket interface to support fallbacks. Now we support a list of fallback targets when opening the socket, and socket binding/connect is done inside socket.* instead of luaa.c or awesome-client.c Signed-off-by: Alex Cornejo <[email protected]> --- common/socket.c | 71 +++++++++++++++++++++++++++++++++++++++++-------------- common/socket.h | 8 +++++- 2 files changed, 60 insertions(+), 19 deletions(-) diff --git a/common/socket.c b/common/socket.c index 3fb9b68..9119e03 100644 --- a/common/socket.c +++ b/common/socket.c @@ -24,6 +24,7 @@ #include <errno.h> #include <sys/socket.h> #include <sys/un.h> +#include <unistd.h> #include <xcb/xcb.h> @@ -32,42 +33,76 @@ #define CONTROL_UNIX_SOCKET_PATH ".awesome_ctl." -/** Get a sockaddr_un struct with information feeded for opening a - * communication to the awesome socket for given display - * \param directory Where socket is created. +/** Open a communication socket with awesome for a given display + * \param csfd the socket file descriptor * \param display the display number - * \return sockaddr_un struct ready to be used or NULL if a problem occured + * \param mode the open mode, either Bind or Connect + * \return sockaddr_un struct ready to be used or NULL if a problem ocurred */ -struct sockaddr_un * -socket_getaddr(const char *directory, const char *display) +struct sockaddr_un *socket_open(int csfd, const char *display, const socket_mode_t mode) { char *host = NULL; - int screenp = 0, displayp = 0; + int screenp = 0, displayp = 0, i=0, status=-1; ssize_t path_len, len; struct sockaddr_un *addr; + const char *fallback[]={":HOME",":TMPDIR","/tmp",NULL}, *directory; addr = p_new(struct sockaddr_un, 1); + addr->sun_family = AF_UNIX; xcb_parse_display(NULL, &host, &displayp, &screenp); - len = a_strlen(host); - /* + 2 for / and . and \0 */ - path_len = snprintf(addr->sun_path, sizeof(addr->sun_path), - "%s/" CONTROL_UNIX_SOCKET_PATH "%s%s%d", - directory, len ? host : "", len ? "." : "", - displayp); + while(fallback[i] != NULL && status == -1) + { + if(fallback[i][0] == ':') + { + directory = getenv(fallback[i]+1); + if(directory == NULL) + { + i++; + continue; + } + } + else + directory = fallback[i]; + i++; + + /* + 2 for / and . and \0 */ + path_len = snprintf(addr->sun_path, sizeof(addr->sun_path), + "%s/" CONTROL_UNIX_SOCKET_PATH "%s%s%d", + directory, len ? host : "", len ? "." : "", + displayp); + if(path_len < ssizeof(addr->sun_path)) + { + if(mode == Bind) + { + if(!bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr))) + status = 0; + else if(errno == EADDRINUSE) + { + if(unlink(addr->sun_path)) + warn("error unlinking existing file: %s", strerror(errno)); + if(!bind(csfd, (const struct sockaddr *)addr, SUN_LEN(addr))) + status = 0; + else break; + } + } + else if(!connect(csfd, (const struct sockaddr *)addr, sizeof(struct sockaddr_un))) + status = 0; + } + else + warn("error: path (using %s) of control UNIX domain socket is too long",directory); + } p_delete(&host); - if(path_len >= ssizeof(addr->sun_path)) + if (status == -1) { - fprintf(stderr, "error: path of control UNIX domain socket is too long"); - return NULL; + p_delete(&addr); + addr=NULL; } - addr->sun_family = AF_UNIX; - return addr; } diff --git a/common/socket.h b/common/socket.h index 55e1127..29159bc 100644 --- a/common/socket.h +++ b/common/socket.h @@ -22,7 +22,13 @@ #ifndef AWESOME_COMMON_SOCKET_H #define AWESOME_COMMON_SOCKET_H -struct sockaddr_un * socket_getaddr(const char *, const char *); +typedef enum +{ + Bind, + Connect +} socket_mode_t; + +struct sockaddr_un *socket_open(const int, const char *, const socket_mode_t); int socket_getclient(void); #endif -- 1.5.6.3
From 8c0755447233d5c28ebef09249aa8c4662eef8de Mon Sep 17 00:00:00 2001 From: Alex Cornejo <[email protected]> Date: Thu, 19 Mar 2009 23:12:07 -0400 Subject: [PATCH] Changed to use new socket_open interface Signed-off-by: Alex Cornejo <[email protected]> --- awesome-client.c | 10 +--------- luaa.c | 21 ++++----------------- 2 files changed, 5 insertions(+), 26 deletions(-) diff --git a/awesome-client.c b/awesome-client.c index b2282a0..fcc7847 100644 --- a/awesome-client.c +++ b/awesome-client.c @@ -56,17 +56,9 @@ sockets_init(void) if((csfd = socket_getclient()) < 0) return false; - if(!(addr = socket_getaddr(getenv("HOME"), display))) + if(!(addr = socket_open(csfd, display, Connect))) return false; - if(connect(csfd, addr, sizeof(struct sockaddr_un)) == -1) - { - if(!(addr = socket_getaddr("/tmp", display))) - return false; - if(connect(csfd, addr, sizeof(struct sockaddr_un)) == -1) - return false; - } - return true; } diff --git a/luaa.c b/luaa.c index 87df9f2..aae7640 100644 --- a/luaa.c +++ b/luaa.c @@ -1115,31 +1115,18 @@ luaA_cs_init(void) if (csfd < 0 || fcntl(csfd, F_SETFD, FD_CLOEXEC) == -1) return; - addr = socket_getaddr(getenv("HOME"), getenv("DISPLAY")); - /* Needed for some OSes like Solaris */ #ifndef SUN_LEN #define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) + strlen ((ptr)->sun_path)) #endif - if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr))) + if(!(addr = socket_open(csfd, getenv("DISPLAY"), Bind))) { - if(errno == EADDRINUSE) - { - if(unlink(addr->sun_path)) - warn("error unlinking existing file: %s", strerror(errno)); - if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr))) - return warn("error binding UNIX domain socket: %s", strerror(errno)); - } - else - { - addr = socket_getaddr("/tmp", getenv("DISPLAY")); - if (bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr))) - return warn("error binding UNIX domain socket: %s", strerror(errno)); - } + warn("error binding UNIX domain socket: %s", strerror(errno)); + return; } - listen(csfd, 10); + listen(csfd, 10); ev_io_init(&csio, &luaA_conn_cb, csfd, EV_READ); ev_io_start(EV_DEFAULT_UC_ &csio); ev_unref(EV_DEFAULT_UC); -- 1.5.6.3
