diff --git a/modules/backlight.c b/modules/backlight.c index 292b067..af58280 100644 --- a/modules/backlight.c +++ b/modules/backlight.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -178,15 +179,24 @@ run(struct module *mod) bar->refresh(bar); + int ret = 1; while (true) { struct pollfd fds[] = { {.fd = mod->abort_fd, .events = POLLIN}, {.fd = udev_monitor_get_fd(mon), .events = POLLIN}, }; - poll(fds, 2, -1); + if (poll(fds, sizeof(fds) / sizeof(fds[0]), -1) < 0) { + if (errno == EINTR) + continue; - if (fds[0].revents & POLLIN) + LOG_ERRNO("failed to poll"); break; + } + + if (fds[0].revents & POLLIN) { + ret = 0; + break; + } struct udev_device *dev = udev_monitor_receive_device(mon); if (dev == NULL) @@ -209,7 +219,7 @@ run(struct module *mod) udev_unref(udev); close(current_fd); - return 0; + return ret; } static struct module * diff --git a/modules/battery.c b/modules/battery.c index e6526f5..0833310 100644 --- a/modules/battery.c +++ b/modules/battery.c @@ -428,7 +428,15 @@ run(struct module *mod) {.fd = mod->abort_fd, .events = POLLIN}, {.fd = udev_monitor_get_fd(mon), .events = POLLIN}, }; - poll(fds, 2, m->poll_interval > 0 ? m->poll_interval * 1000 : -1); + if (poll(fds, sizeof(fds) / sizeof(fds[0]), + m->poll_interval > 0 ? m->poll_interval * 1000 : -1) < 0) + { + if (errno == EINTR) + continue; + + LOG_ERRNO("failed to poll"); + break; + } if (fds[0].revents & POLLIN) { ret = 0; diff --git a/modules/clock.c b/modules/clock.c index 2956a40..15392aa 100644 --- a/modules/clock.c +++ b/modules/clock.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -67,7 +68,6 @@ content(struct module *mod) return exposable; } -#include static int run(struct module *mod) { @@ -75,6 +75,8 @@ run(struct module *mod) const struct bar *bar = mod->bar; bar->refresh(bar); + int ret = 1; + while (true) { struct timespec _now; clock_gettime(CLOCK_REALTIME, &_now); @@ -120,19 +122,28 @@ run(struct module *mod) now.tv_sec, now.tv_usec, timeout_ms); struct pollfd fds[] = {{.fd = mod->abort_fd, .events = POLLIN}}; - poll(fds, 1, timeout_ms); + if (poll(fds, 1, timeout_ms) < 0) { + if (errno == EINTR) + continue; - if (fds[0].revents & POLLIN) + LOG_ERRNO("failed to poll"); break; + } + + if (fds[0].revents & POLLIN) { + ret = 0; + break; + } bar->refresh(bar); } - return 0; + return ret; } static struct module * -clock_new(struct particle *label, const char *date_format, const char *time_format, bool utc) +clock_new(struct particle *label, const char *date_format, + const char *time_format, bool utc) { struct private *m = calloc(1, sizeof(*m)); m->label = label; diff --git a/modules/mpd.c b/modules/mpd.c index 274fdc8..b2e9e6e 100644 --- a/modules/mpd.c +++ b/modules/mpd.c @@ -251,7 +251,13 @@ wait_for_socket_create(const struct module *mod) {.fd = fd, .events = POLLIN} }; - poll(fds, 2, -1); + if (poll(fds, sizeof(fds) / sizeof(fds[0]), -1) < 0) { + if (errno == EINTR) + continue; + + LOG_ERRNO("failed to poll"); + break; + } if (fds[0].revents & POLLIN) { ret = true; @@ -377,8 +383,9 @@ run(struct module *mod) struct private *m = mod->private; bool aborted = false; + int ret = 0; - while (!aborted) { + while (!aborted && ret == 0) { if (m->conn != NULL) { mpd_connection_free(m->conn); @@ -396,7 +403,7 @@ run(struct module *mod) mtx_unlock(&mod->lock); /* Keep trying to connect, until we succeed */ - while (!aborted) { + while (!aborted && ret == 0) { if (m->port == 0) { /* Use inotify to watch for socket creation */ aborted = wait_for_socket_create(mod); @@ -414,16 +421,27 @@ run(struct module *mod) * host), wait for a while until we try to re-connect * again. */ - struct pollfd fds[] = {{.fd = mod->abort_fd, .events = POLLIN}}; - int res = poll(fds, 1, 10 * 1000); + while (!aborted) { + struct pollfd fds[] = {{.fd = mod->abort_fd, .events = POLLIN}}; + int res = poll(fds, sizeof(fds) / sizeof(fds[0]), 10 * 1000); - if (res == 1) { - assert(fds[0].revents & POLLIN); - aborted = true; + if (res < 0) { + if (errno == EINTR) + continue; + + LOG_ERRNO("failed to poll"); + ret = 1; + break; + } + + if (res == 1) { + assert(fds[0].revents & POLLIN); + aborted = true; + } } } - if (aborted) + if (aborted || ret != 0) break; /* Initial state (after establishing a connection) */ @@ -446,7 +464,14 @@ run(struct module *mod) break; } - poll(fds, 2, -1); + if (poll(fds, sizeof(fds) / sizeof(fds[0]), -1) < 0) { + if (errno == EINTR) + continue; + + LOG_ERRNO("failed to poll"); + ret = 1; + break; + } if (fds[0].revents & POLLIN) { aborted = true; @@ -477,7 +502,7 @@ run(struct module *mod) m->conn = NULL; } - return 0; + return aborted ? 0 : ret; } struct refresh_context { diff --git a/modules/removables.c b/modules/removables.c index 3a54379..736317f 100644 --- a/modules/removables.c +++ b/modules/removables.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -547,16 +548,26 @@ run(struct module *mod) * mount/unmount operations */ int mount_info_fd = open("/proc/self/mountinfo", O_RDONLY); + int ret = 1; + while (true) { struct pollfd fds[] = { {.fd = mod->abort_fd, .events = POLLIN}, {.fd = udev_monitor_get_fd(dev_mon), .events = POLLIN}, {.fd = mount_info_fd, .events = POLLPRI}, }; - poll(fds, 3, -1); + if (poll(fds, sizeof(fds) / sizeof(fds[0]), -1) < 0) { + if (errno == EINTR) + continue; - if (fds[0].revents & POLLIN) + LOG_ERRNO("failed to poll"); break; + } + + if (fds[0].revents & POLLIN) { + ret = 0; + break; + } bool update = false; @@ -587,7 +598,7 @@ run(struct module *mod) udev_monitor_unref(dev_mon); udev_unref(udev); - return 0; + return ret; } static struct module * diff --git a/modules/xkb.c b/modules/xkb.c index 5c2c1f9..965664d 100644 --- a/modules/xkb.c +++ b/modules/xkb.c @@ -399,7 +399,14 @@ event_loop(struct module *mod, xcb_connection_t *conn, int xkb_event_base) }; /* Use poll() since xcb_wait_for_events() doesn't return on signals */ - poll(pfds, sizeof(pfds) / sizeof(pfds[0]), -1); + if (poll(pfds, sizeof(pfds) / sizeof(pfds[0]), -1) < 0) { + if (errno == EINTR) + continue; + + LOG_ERRNO("failed to poll"); + break; + } + if (pfds[0].revents & POLLIN) { ret = true; break; diff --git a/modules/xwindow.c b/modules/xwindow.c index 3925f4f..baf1239 100644 --- a/modules/xwindow.c +++ b/modules/xwindow.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -247,14 +248,24 @@ run(struct module *mod) update_title(mod); mod->bar->refresh(mod->bar); + int ret = 1; + int xcb_fd = xcb_get_file_descriptor(m->conn); while (true) { struct pollfd fds[] = {{.fd = mod->abort_fd, .events = POLLIN}, {.fd = xcb_fd, .events = POLLIN}}; - poll(fds, 2, -1); + if (poll(fds, sizeof(fds) / sizeof(fds[0]), -1) < 0) { + if (errno == EINTR) + continue; - if (fds[0].revents & POLLIN) + LOG_ERRNO("failed to poll"); break; + } + + if (fds[0].revents & POLLIN) { + ret = 0; + break; + } for (xcb_generic_event_t *_e = xcb_wait_for_event(m->conn); _e != NULL; @@ -293,7 +304,7 @@ run(struct module *mod) xcb_destroy_window(m->conn, m->monitor_win); xcb_disconnect(m->conn); - return 0; + return ret; } static struct exposable *