From 691b732f1ab74de967372482d0ba42ad668b4a75 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Tue, 17 Mar 2009 20:01:38 +0100 Subject: [PATCH] awesome-client: Don't go into an endless loop if awesome dies This patch makes awesome-client give up after 10 tries when it lost the connection to awesome. Also it now waits for some time between reconnect attempts. I like the behaviour of this, but some of the code seems a little icky... Signed-off-by: Uli Schlachter Signed-off-by: Julien Danjou --- awesome-client.c | 48 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/awesome-client.c b/awesome-client.c index c55f0d775..c7c84fa5d 100644 --- a/awesome-client.c +++ b/awesome-client.c @@ -84,34 +84,54 @@ sockets_reconnect(void) sockets_init(); } +/** Send a message to awesome without handling retries. + * \param msg The message. + * \param msg_len The message length. + * \return The errno of send(). + */ +static int +send_msg_raw(const char *msg, ssize_t msg_len) +{ + #ifndef __FreeBSD__ + return send(csfd, msg, msg_len, MSG_NOSIGNAL | MSG_EOR); + #else + return send(csfd, msg, msg_len, MSG_NOSIGNAL | MSG_EOF); + #endif +} + /** Send a message to awesome. * \param msg The message. * \param msg_len The message length. - * \return The errno of sendto(). + * \return The errno of send(). */ static int send_msg(const char *msg, ssize_t msg_len) { - #ifndef __FreeBSD__ - if(send(csfd, msg, msg_len, MSG_NOSIGNAL | MSG_EOR) == -1) - #else - if(send(csfd, msg, msg_len, MSG_NOSIGNAL | MSG_EOF) == -1) - #endif + int try = 10; + + while (try && send_msg_raw(msg, msg_len) == -1) { switch (errno) { - case ENOENT: - warn("can't write to %s", addr->sun_path); - break; case EPIPE: case ENOTCONN: case ECONNRESET: sockets_reconnect(); - return send_msg(msg, msg_len); + try--; + break; + case ENOENT: + warn("can't write to %s", addr->sun_path); + return errno; default: warn("error sending packet: %s", strerror(errno)); + return errno; } - return errno; + usleep(100000); + } + if(!try) + { + warn("giving up."); + return EXIT_FAILURE; } return EXIT_SUCCESS; @@ -201,12 +221,16 @@ main(int argc, char **argv) while((msg = readline(prompt))) if((msg_len = a_strlen(msg))) { + int result; add_history (msg); p_realloc(&msg, msg_len + 2); msg[msg_len] = '\n'; msg[msg_len + 1] = '\0'; - if(send_msg(msg, msg_len + 2) == EXIT_SUCCESS) + result = send_msg(msg, msg_len + 2); + if(result == EXIT_SUCCESS) recv_msg(); + else if(result == EXIT_FAILURE) + break; p_delete(&msg); } }