]> git.ipfire.org Git - pakfire.git/commitdiff
jail: Align processing of file descriptors in the event loop
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 16 Dec 2023 13:19:54 +0000 (13:19 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 16 Dec 2023 13:19:54 +0000 (13:19 +0000)
This is slightly easier to read than checking the event type first and
then handle the rest.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/jail.c
src/libpakfire/pakfire.c

index 5f3884445c04117f8e3365b15983f6d536e393ed..45be6755ec437e226137dd172d1e9ae45de2c82b 100644 (file)
@@ -1204,11 +1204,6 @@ static int pakfire_jail_wait(struct pakfire_jail* jail, struct pakfire_jail_exec
                        int e  = events[i].events;
                        int fd = events[i].data.fd;
 
-                       struct pakfire_log_buffer* buffer = NULL;
-                       pakfire_jail_communicate_out callback = NULL;
-                       void* data = NULL;
-                       int priority;
-
                        // Handle PTY forwarding events
                        if (ctx->pty.master.fd == fd) {
                                if (e & (EPOLLIN|EPOLLHUP))
@@ -1247,12 +1242,10 @@ static int pakfire_jail_wait(struct pakfire_jail* jail, struct pakfire_jail_exec
                                        CTX_ERROR(jail->ctx, "Failed forwarding the PTY: %s\n", strerror(-r));
                                        goto ERROR;
                                }
-                       }
 
-                       // Check if there is any data to be read
-                       if (e & EPOLLIN) {
-                               // Handle any changes to the PIDFD
-                               if (fd == pidfd) {
+                       // Handle any changes to the PIDFD
+                       } else if (pidfd == fd) {
+                               if (e & EPOLLIN) {
                                        // Call waidid() and store the result
                                        r = waitid(P_PIDFD, ctx->pidfd, &ctx->status, WEXITED);
                                        if (r) {
@@ -1263,10 +1256,11 @@ static int pakfire_jail_wait(struct pakfire_jail* jail, struct pakfire_jail_exec
                                        // Mark that we have ended so that we will process the remaining
                                        // events from epoll() now, but won't restart the outer loop.
                                        ended = 1;
-                                       continue;
+                               }
 
-                               // Handle timer events
-                               } else if (fd == timerfd) {
+                       // Handle timer events
+                       } else if (timerfd == fd) {
+                               if (e & EPOLLIN) {
                                        DEBUG(jail->pakfire, "Timer event received\n");
 
                                        // Disarm the timer
@@ -1288,33 +1282,11 @@ static int pakfire_jail_wait(struct pakfire_jail* jail, struct pakfire_jail_exec
                                                        goto ERROR;
                                                }
                                        }
+                               }
 
-                                       // Don't fall through to log processing
-                                       continue;
-
-                               // Handle logging messages
-                               } else if (fd == log_INFO) {
-                                       buffer = &ctx->buffers.log_INFO;
-                                       priority = LOG_INFO;
-
-                                       callback = pakfire_jail_log;
-
-                               } else if (fd == log_ERROR) {
-                                       buffer = &ctx->buffers.log_ERROR;
-                                       priority = LOG_ERR;
-
-                                       callback = pakfire_jail_log;
-
-#ifdef ENABLE_DEBUG
-                               } else if (fd == log_DEBUG) {
-                                       buffer = &ctx->buffers.log_DEBUG;
-                                       priority = LOG_DEBUG;
-
-                                       callback = pakfire_jail_log;
-#endif /* ENABLE_DEBUG */
-
-                               // Handle socket messages
-                               } else if (fd == socket_recv) {
+                       // Handle socket messages
+                       } else if (socket_recv == fd) {
+                               if (e & EPOLLIN) {
                                        // Receive the passed FD
                                        r = pakfire_jail_recv_fd(jail, socket_recv, &fd);
                                        if (r)
@@ -1328,19 +1300,41 @@ static int pakfire_jail_wait(struct pakfire_jail* jail, struct pakfire_jail_exec
                                                        goto ERROR;
                                                }
                                        }
+                               }
 
-                                       // Don't fall through to log processing
-                                       continue;
+                       // Handle log INFO messages
+                       } else if (log_INFO == fd) {
+                               if (e & EPOLLIN) {
+                                       r = pakfire_jail_handle_log(jail, ctx, LOG_INFO, fd,
+                                               &ctx->buffers.log_INFO, pakfire_jail_log, NULL);
+                                       if (r)
+                                               goto ERROR;
+                               }
 
-                               } else {
-                                       DEBUG(jail->pakfire, "Received invalid file descriptor %d\n", fd);
-                                       continue;
+                       // Handle log ERROR messages
+                       } else if (log_ERROR == fd) {
+                               if (e & EPOLLIN) {
+                                       r = pakfire_jail_handle_log(jail, ctx, LOG_ERR, fd,
+                                               &ctx->buffers.log_ERROR, pakfire_jail_log, NULL);
+                                       if (r)
+                                               goto ERROR;
                                }
 
-                               // Handle log event
-                               r = pakfire_jail_handle_log(jail, ctx, priority, fd, buffer, callback, data);
-                               if (r)
-                                       goto ERROR;
+#ifdef ENABLE_DEBUG
+                       // Handle log DEBUG messages
+                       } else if (log_DEBUG == fd) {
+                               if (e & EPOLLIN) {
+                                       r = pakfire_jail_handle_log(jail, ctx, LOG_DEBUG, fd,
+                                               &ctx->buffers.log_DEBUG, pakfire_jail_log, NULL);
+                                       if (r)
+                                               goto ERROR;
+                               }
+#endif /* ENABLE_DEBUG */
+
+                       // Log a message for anything else
+                       } else {
+                               DEBUG(jail->pakfire, "Received invalid file descriptor %d\n", fd);
+                               continue;
                        }
 
                        // Check if any file descriptors have been closed
index 8ad78a580d97df564547d93f17e531a1c0a95259..072ba8f931c48def2abe1400ba79e193f64beb70 100644 (file)
@@ -292,7 +292,7 @@ static int pakfire_populate_pool(struct pakfire* pakfire) {
 
 #ifdef ENABLE_DEBUG
        // Enable debug output
-       pool_setdebuglevel(pool, 3);
+       //pool_setdebuglevel(pool, 3);
 #endif
 
        // Set architecture of the pool