]> git.meshlink.io Git - meshlink/commitdiff
Add warning messages when checking for bad fds after select() returns an error.
authorGuus Sliepen <guus@meshlink.io>
Sun, 21 Feb 2021 21:11:52 +0000 (22:11 +0100)
committerGuus Sliepen <guus@meshlink.io>
Sun, 21 Feb 2021 21:11:52 +0000 (22:11 +0100)
src/event.c
src/event.h
src/net.c

index 82133eb471826ce1d9921d0f4a3fbac83df9387a..f8ebe8b842047695c92cfccd98441673b8af452f 100644 (file)
@@ -21,6 +21,8 @@
 
 #include "dropin.h"
 #include "event.h"
+#include "logger.h"
+#include "meshlink.h"
 #include "net.h"
 #include "splay_tree.h"
 #include "utils.h"
@@ -282,7 +284,7 @@ void idle_set(event_loop_t *loop, idle_cb_t cb, void *data) {
        loop->idle_data = data;
 }
 
-static void check_bad_fds(event_loop_t *loop) {
+static void check_bad_fds(event_loop_t *loop, meshlink_handle_t *mesh) {
        // Just call all registered callbacks and have them check their fds
 
        do {
@@ -309,6 +311,11 @@ static void check_bad_fds(event_loop_t *loop) {
 
        // Rebuild the fdsets
 
+       fd_set old_readfds;
+       fd_set old_writefds;
+       memcpy(&old_readfds, &loop->readfds, sizeof(old_readfds));
+       memcpy(&old_writefds, &loop->writefds, sizeof(old_writefds));
+
        memset(&loop->readfds, 0, sizeof(loop->readfds));
        memset(&loop->writefds, 0, sizeof(loop->writefds));
 
@@ -322,12 +329,19 @@ static void check_bad_fds(event_loop_t *loop) {
                        FD_SET(io->fd, &loop->writefds);
                        io->cb(loop, io->data, IO_WRITE);
                }
+       }
+
+       if(memcmp(&old_readfds, &loop->readfds, sizeof(old_readfds))) {
+               logger(mesh, MESHLINK_WARNING, "Incorrect readfds fixed");
+       }
 
+       if(memcmp(&old_writefds, &loop->writefds, sizeof(old_writefds))) {
+               logger(mesh, MESHLINK_WARNING, "Incorrect writefds fixed");
        }
 }
 
-bool event_loop_run(event_loop_t *loop, pthread_mutex_t *mutex) {
-       assert(mutex);
+bool event_loop_run(event_loop_t *loop, meshlink_handle_t *mesh) {
+       assert(mesh);
 
        fd_set readable;
        fd_set writable;
@@ -368,7 +382,7 @@ bool event_loop_run(event_loop_t *loop, pthread_mutex_t *mutex) {
                }
 
                // release mesh mutex during select
-               pthread_mutex_unlock(mutex);
+               pthread_mutex_unlock(&mesh->mutex);
 
 #ifdef HAVE_PSELECT
                int n = pselect(fds, &readable, &writable, NULL, &ts, NULL);
@@ -377,7 +391,7 @@ bool event_loop_run(event_loop_t *loop, pthread_mutex_t *mutex) {
                int n = select(fds, &readable, &writable, NULL, (struct timeval *)&tv);
 #endif
 
-               if(pthread_mutex_lock(mutex) != 0) {
+               if(pthread_mutex_lock(&mesh->mutex) != 0) {
                        abort();
                }
 
@@ -390,10 +404,12 @@ bool event_loop_run(event_loop_t *loop, pthread_mutex_t *mutex) {
                                errors++;
 
                                if(errors > 10) {
+                                       logger(mesh, MESHLINK_ERROR, "Unrecoverable error from select(): %s", strerror(errno));
                                        return false;
                                }
 
-                               check_bad_fds(loop);
+                               logger(mesh, MESHLINK_WARNING, "Error from select(), checking for bad fds: %s", strerror(errno));
+                               check_bad_fds(loop, mesh);
                                continue;
                        }
                }
index bec32045e0bf501bd9c39985870b6d9d0268b82f..6b53d9192797d500781fb319233e561394556242 100644 (file)
@@ -28,6 +28,7 @@
 #define IO_WRITE 2
 
 typedef struct event_loop_t event_loop_t;
+struct meshlink_handle;
 
 typedef void (*io_cb_t)(event_loop_t *loop, void *data, int flags);
 typedef void (*timeout_cb_t)(event_loop_t *loop, void *data);
@@ -96,7 +97,7 @@ void idle_set(event_loop_t *loop, idle_cb_t cb, void *data);
 
 void event_loop_init(event_loop_t *loop);
 void event_loop_exit(event_loop_t *loop);
-bool event_loop_run(event_loop_t *loop, pthread_mutex_t *mutex) __attribute__((__warn_unused_result__));
+bool event_loop_run(event_loop_t *loop, struct meshlink_handle *mesh) __attribute__((__warn_unused_result__));
 void event_loop_flush_output(event_loop_t *loop);
 void event_loop_start(event_loop_t *loop);
 void event_loop_stop(event_loop_t *loop);
index 22f8287ff70a232acc2ab8c7f7091308c47f0304..99c29c943290b1ea7636b7b01dd9ed072a965665 100644 (file)
--- a/src/net.c
+++ b/src/net.c
@@ -735,7 +735,7 @@ void main_loop(meshlink_handle_t *mesh) {
        mesh->datafromapp.signum = 0;
        signal_add(&mesh->loop, &mesh->datafromapp, meshlink_send_from_queue, mesh, mesh->datafromapp.signum);
 
-       if(!event_loop_run(&mesh->loop, &mesh->mutex)) {
+       if(!event_loop_run(&mesh->loop, mesh)) {
                logger(mesh, MESHLINK_ERROR, "Error while waiting for input: %s", strerror(errno));
                call_error_cb(mesh, MESHLINK_ENETWORK);
        }