diff --git a/src/bspwm.c b/src/bspwm.c index cda9482..88c48e9 100644 --- a/src/bspwm.c +++ b/src/bspwm.c @@ -215,6 +215,8 @@ int main(int argc, char *argv[]) if (!check_connection(dpy)) { running = false; } + + prune_dead_subscribers(); } if (restart) { diff --git a/src/subscribe.c b/src/subscribe.c index ebf121a..90a4bd5 100644 --- a/src/subscribe.c +++ b/src/subscribe.c @@ -163,3 +163,20 @@ void put_status(subscriber_mask_t mask, ...) sb = next; } } + +void prune_dead_subscribers(void) +{ + subscriber_list_t *sb = subscribe_head; + while (sb != NULL) { + subscriber_list_t *next = sb->next; + // To check if a subscriber's stream is still open and writable call + // write with an empty buffer and check the returned value. If the + // stream is not writable anymore (i.e. it has been closed because the + // process associated to this subscriber no longer exists) then write() + // will return -1. + if (write(fileno(sb->stream), 0, 0) == -1) { + remove_subscriber(sb); + } + sb = next; + } +} diff --git a/src/subscribe.h b/src/subscribe.h index cb90a86..e3e5ba8 100644 --- a/src/subscribe.h +++ b/src/subscribe.h @@ -68,4 +68,8 @@ void add_subscriber(subscriber_list_t *sb); int print_report(FILE *stream); void put_status(subscriber_mask_t mask, ...); +/* Remove any subscriber for which the stream has been closed and is no longer + * writable. */ +void prune_dead_subscribers(void); + #endif