Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #62 from spiiroin/iowatch
Add error handling to glib io watches
  • Loading branch information
spiiroin committed Mar 14, 2014
2 parents fdb153c + f89a393 commit bb9515f
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 11 deletions.
12 changes: 9 additions & 3 deletions dsme/dsmesock.c
Expand Up @@ -106,7 +106,8 @@ int dsmesock_listen(dsmesock_callback* read_and_queue)
if (!(as_chan = g_io_channel_unix_new(fd))) {
goto close_and_fail;
}
if (!g_io_add_watch(as_chan, G_IO_IN, accept_client, as_chan)) {
if (!g_io_add_watch(as_chan, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
accept_client, as_chan)) {
goto close_channel_and_fail;
}

Expand Down Expand Up @@ -135,6 +136,11 @@ static gboolean accept_client(GIOChannel* source,
int newfd;
dsmesock_connection_t* newconn = 0;

if( condition & (G_IO_ERR | G_IO_HUP | G_IO_NVAL) ) {
dsme_log(LOG_CRIT, "disabling client connect watcher");
return FALSE;
}

newfd = accept(g_io_channel_unix_get_fd(source), 0, 0);
if(newfd == -1) return TRUE;

Expand Down Expand Up @@ -162,7 +168,7 @@ static gboolean accept_client(GIOChannel* source,

if (!(newconn->channel = g_io_channel_unix_new(newfd)) ||
!g_io_add_watch(newconn->channel,
(G_IO_IN | G_IO_ERR | G_IO_HUP),
G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
handle_client,
newconn))
{
Expand All @@ -188,7 +194,7 @@ static gboolean handle_client(GIOChannel* source,
keep_connection = false;
}
}
if (condition & (G_IO_ERR | G_IO_HUP)) {
if (condition & (G_IO_ERR | G_IO_HUP | G_IO_NVAL)) {
keep_connection = false;
}

Expand Down
3 changes: 2 additions & 1 deletion dsme/mainloop.c
Expand Up @@ -74,7 +74,8 @@ static bool set_up_signal_pipe(void)
if (!(chan = g_io_channel_unix_new(signal_pipe[0]))) {
goto close_and_fail;
}
watch = g_io_add_watch(chan, G_IO_IN, handle_signal, 0);
watch = g_io_add_watch(chan, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
handle_signal, 0);
g_io_channel_unref(chan);
if (!watch) {
goto close_and_fail;
Expand Down
6 changes: 3 additions & 3 deletions modules/heartbeat.c
Expand Up @@ -42,9 +42,9 @@ static gboolean emit_heartbeat_message(GIOChannel* source,
gpointer data)
{
// handle errors
if (condition & (G_IO_ERR | G_IO_HUP)) {
if (condition & (G_IO_ERR | G_IO_HUP | G_IO_NVAL)) {
// the wd process has probably died; remove the watch & quit
dsme_log(LOG_DEBUG, "heartbeat: I/O error or HUP");
dsme_log(LOG_CRIT, "heartbeat: I/O error or HUP, terminating");
dsme_main_loop_quit(EXIT_FAILURE);
return false;
}
Expand Down Expand Up @@ -89,7 +89,7 @@ static bool start_heartbeat(void)
goto fail;
}
if (!(watch = g_io_add_watch(chan,
(G_IO_IN | G_IO_ERR | G_IO_HUP),
G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
emit_heartbeat_message,
0)))
{
Expand Down
12 changes: 11 additions & 1 deletion modules/iphb.c
Expand Up @@ -2406,6 +2406,14 @@ static gboolean epollfd_iowatch_cb(GIOChannel* source,
struct epoll_event events[DSME_MAX_EPOLL_EVENTS];
int nfds;

/* Abandon watch if we get abnorman conditions from glib */
if (condition & ~(G_IO_IN | G_IO_PRI))
{
dsme_log(LOG_ERR, PFIX"epoll waiting I/O error reported");
dsme_log(LOG_CRIT, PFIX"epoll waiting disabled");
return FALSE;
}

dsme_log(LOG_DEBUG, PFIX"epollfd readable");

nfds = epoll_wait(epollfd, events, DSME_MAX_EPOLL_EVENTS, 0);
Expand Down Expand Up @@ -2491,7 +2499,9 @@ static bool epollfd_init(void)
goto cleanup;
}

if( !(epoll_watch = g_io_add_watch(chan, G_IO_IN, epollfd_iowatch_cb, 0)) ) {
if( !(epoll_watch = g_io_add_watch(chan,
G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL,
epollfd_iowatch_cb, 0)) ) {
goto cleanup;
}

Expand Down
4 changes: 3 additions & 1 deletion modules/pwrkeymonitor.c
Expand Up @@ -420,7 +420,9 @@ probe_evdev_device(const char *path)
}
g_io_channel_set_buffered(chan, false);

if( !(watch = g_io_add_watch(chan, G_IO_IN, process_kbevent, 0)) )
if( !(watch = g_io_add_watch(chan,
G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
process_kbevent, 0)) )
{
dsme_log(LOG_ERR, PFIX"%s: unable to add io channel watch", path);
goto EXIT;
Expand Down
4 changes: 2 additions & 2 deletions modules/thermalsensor_battery.c
Expand Up @@ -181,7 +181,7 @@ static void connect_bme()

if (!(chan = g_io_channel_unix_new(request_fd)) ||
!g_io_add_watch(chan,
(G_IO_IN | G_IO_ERR | G_IO_HUP),
G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
handle_battery_temperature_response,
0))
{
Expand Down Expand Up @@ -252,7 +252,7 @@ static gboolean handle_battery_temperature_response(GIOChannel* source,
got_status = false;
}
}
if (condition & (G_IO_ERR | G_IO_HUP)) {
if (condition & (G_IO_ERR | G_IO_HUP | G_IO_NVAL)) {
dsme_log(LOG_DEBUG, "bme connection ERR or HUP");
keep_connection = false;
}
Expand Down

0 comments on commit bb9515f

Please sign in to comment.