diff --git a/Makefile b/Makefile index 1547f30a..1120340f 100644 --- a/Makefile +++ b/Makefile @@ -3,10 +3,10 @@ include config.mk -SRC = client.c draw.c event.c layout.c awesome.c tag.c util.c config.c screen.c statusbar.c uicb.c tab.c window.c +SRC = client.c draw.c event.c layout.c awesome.c tag.c util.c config.c screen.c statusbar.c uicb.c tab.c window.c awesome-client-common.c OBJ = ${SRC:.c=.o} ${LAYOUTS:.c=.o} -SRCCLIENT = awesome-client.c util.c +SRCCLIENT = awesome-client.c awesome-client-common.c util.c OBJCLIENT = ${SRCCLIENT:.c=.o} all: options awesome awesome-client diff --git a/awesome-client-common.c b/awesome-client-common.c new file mode 100644 index 00000000..3daa4d22 --- /dev/null +++ b/awesome-client-common.c @@ -0,0 +1,66 @@ +/* + * awesome-client-common.c - awesome client, communicate with socket, common functions + * + * Copyright © 2007 Julien Danjou + * Copyright © 2007 daniel@brinkers.de + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ + +#include +#include + +#include "awesome-client.h" +#include "util.h" + +#define CONTROL_UNIX_SOCKET_PATH ".awesome_so_ctl" + +struct sockaddr_un * +get_client_addr(void) +{ + char *homedir; + ssize_t path_len; + struct sockaddr_un *addr; + + addr = p_new(struct sockaddr_un, 1); + homedir = getenv("HOME"); + path_len = a_strlen(homedir) + a_strlen(CONTROL_UNIX_SOCKET_PATH) + 2; + if(path_len >= ssizeof(addr->sun_path)) + { + fprintf(stderr, "error: path of control UNIX domain socket is too long"); + return NULL; + } + a_strcpy(addr->sun_path, path_len, homedir); + a_strcat(addr->sun_path, path_len, "/"); + a_strcat(addr->sun_path, path_len, CONTROL_UNIX_SOCKET_PATH); + + addr->sun_family = AF_UNIX; + + return addr; +} + +int +get_client_socket(void) +{ + int csfd; + + csfd = socket(AF_UNIX, SOCK_DGRAM, 0); + + if(csfd < 0) + perror("error opening UNIX domain socket"); + + return csfd; +} diff --git a/awesome-client.c b/awesome-client.c index afdd6495..9b72a2aa 100644 --- a/awesome-client.c +++ b/awesome-client.c @@ -23,9 +23,6 @@ #include #include #include -#include -#include -#include #include #include @@ -35,30 +32,18 @@ int main() { - ssize_t path_len; - struct sockaddr_un addr; + struct sockaddr_un *addr; char buf[1024]; - const char *homedir; int csfd; - homedir = getenv("HOME"); - path_len = a_strlen(homedir) + a_strlen(CONTROL_UNIX_SOCKET_PATH) + 2; - if(path_len >= (int)sizeof(addr.sun_path)) - { - fprintf(stderr, "error: path of control UNIX domain socket is too long"); - exit(1); - } - a_strcpy(addr.sun_path, path_len, homedir); - a_strcat(addr.sun_path, path_len, "/"); - a_strcat(addr.sun_path, path_len, CONTROL_UNIX_SOCKET_PATH); - csfd = socket(AF_UNIX, SOCK_DGRAM, 0); - if(csfd < 0) - perror("error opening UNIX domain socket"); - addr.sun_family = AF_UNIX; + csfd = get_client_socket(); + addr = get_client_addr(); + while(fgets(buf, sizeof(buf), stdin)) - { - if(sendto(csfd, buf, strlen(buf), MSG_NOSIGNAL, (struct sockaddr*)&addr, sizeof(addr)) == -1) + if(sendto(csfd, buf, a_strlen(buf), MSG_NOSIGNAL, + (const struct sockaddr *) addr, sizeof(struct sockaddr_un)) == -1) perror("error sending datagram"); - } + + return EXIT_SUCCESS; } // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 diff --git a/awesome-client.h b/awesome-client.h index c8fec5fd..1ea128cb 100644 --- a/awesome-client.h +++ b/awesome-client.h @@ -22,7 +22,8 @@ #ifndef AWESOME_AWESOME_CLIENT_H #define AWESOME_AWESOME_CLIENT_H -#define CONTROL_UNIX_SOCKET_PATH ".awesome_so_ctl" +struct sockaddr_un * get_client_addr(void); +int get_client_socket(void); #endif // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 diff --git a/awesome.c b/awesome.c index c603ec64..5027eb52 100644 --- a/awesome.c +++ b/awesome.c @@ -234,7 +234,7 @@ int main(int argc, char *argv[]) { char buf[1024]; - const char *confpath = NULL, *homedir; + const char *confpath = NULL; int r, xfd, e_dummy, csfd; fd_set rd; XEvent ev; @@ -246,8 +246,7 @@ main(int argc, char *argv[]) Atom netatom[NetLast]; event_handler **handler; Client **clients, **sel; - ssize_t path_len; - struct sockaddr_un addr; + struct sockaddr_un *addr; if(argc >= 2) { @@ -351,35 +350,22 @@ main(int argc, char *argv[]) XSync(dpy, False); - /* construct socket path */ - homedir = getenv("HOME"); - csfd = -1; - path_len = a_strlen(homedir) + a_strlen(CONTROL_UNIX_SOCKET_PATH) + 2; + /* get socket fd */ + csfd = get_client_socket(); + addr = get_client_addr(); - if(path_len <= ssizeof(addr.sun_path)) + if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr))) { - a_strcpy(addr.sun_path, path_len, homedir); - a_strcat(addr.sun_path, path_len, "/"); - a_strcat(addr.sun_path, path_len, CONTROL_UNIX_SOCKET_PATH); - csfd = socket(AF_UNIX, SOCK_DGRAM, 0); - if(csfd < 0) - perror("error opening UNIX domain socket"); - addr.sun_family = AF_UNIX; - if(bind(csfd, (struct sockaddr *) &addr, SUN_LEN(&addr))) + if(errno == EADDRINUSE) { - if(errno == EADDRINUSE) - { - if(unlink(addr.sun_path)) - perror("error unlinking existend file"); - if(bind(csfd, (struct sockaddr *) &addr, SUN_LEN(&addr))) - perror("error binding UNIX domain socket"); - } - else + if(unlink(addr->sun_path)) + perror("error unlinking existing file"); + if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr))) perror("error binding UNIX domain socket"); } + else + perror("error binding UNIX domain socket"); } - else - fprintf(stderr, "error: path of control UNIX domain socket is too long"); /* main event loop, also reads status text from socket */ while(running) @@ -423,7 +409,7 @@ main(int argc, char *argv[]) if(csfd > 0 && close(csfd)) perror("error closing UNIX domain socket"); - if(unlink(addr.sun_path)) + if(unlink(addr->sun_path)) perror("error unlinking UNIX domain socket"); cleanup(awesomeconf);