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 <psychon@znc.in>
Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Uli Schlachter 2009-03-17 20:01:38 +01:00 committed by Julien Danjou
parent 7f7aac4583
commit 691b732f1a
1 changed files with 36 additions and 12 deletions

View File

@ -84,35 +84,55 @@ sockets_reconnect(void)
sockets_init(); 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. /** Send a message to awesome.
* \param msg The message. * \param msg The message.
* \param msg_len The message length. * \param msg_len The message length.
* \return The errno of sendto(). * \return The errno of send().
*/ */
static int static int
send_msg(const char *msg, ssize_t msg_len) send_msg(const char *msg, ssize_t msg_len)
{ {
#ifndef __FreeBSD__ int try = 10;
if(send(csfd, msg, msg_len, MSG_NOSIGNAL | MSG_EOR) == -1)
#else while (try && send_msg_raw(msg, msg_len) == -1)
if(send(csfd, msg, msg_len, MSG_NOSIGNAL | MSG_EOF) == -1)
#endif
{ {
switch (errno) switch (errno)
{ {
case ENOENT:
warn("can't write to %s", addr->sun_path);
break;
case EPIPE: case EPIPE:
case ENOTCONN: case ENOTCONN:
case ECONNRESET: case ECONNRESET:
sockets_reconnect(); sockets_reconnect();
return send_msg(msg, msg_len); try--;
break;
case ENOENT:
warn("can't write to %s", addr->sun_path);
return errno;
default: default:
warn("error sending packet: %s", strerror(errno)); warn("error sending packet: %s", strerror(errno));
}
return errno; return errno;
} }
usleep(100000);
}
if(!try)
{
warn("giving up.");
return EXIT_FAILURE;
}
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
@ -201,12 +221,16 @@ main(int argc, char **argv)
while((msg = readline(prompt))) while((msg = readline(prompt)))
if((msg_len = a_strlen(msg))) if((msg_len = a_strlen(msg)))
{ {
int result;
add_history (msg); add_history (msg);
p_realloc(&msg, msg_len + 2); p_realloc(&msg, msg_len + 2);
msg[msg_len] = '\n'; msg[msg_len] = '\n';
msg[msg_len + 1] = '\0'; 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(); recv_msg();
else if(result == EXIT_FAILURE)
break;
p_delete(&msg); p_delete(&msg);
} }
} }