]> git.ipfire.org Git - pakfire.git/commitdiff
jail: Bring back helper function to add FDs to epoll()
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 2 Dec 2023 12:57:13 +0000 (12:57 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 2 Dec 2023 12:57:13 +0000 (12:57 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/jail.c

index 00630daf73216cc72d49a541a16f31a4283f46ea..5cc1dd753d366f1e62688e3705ad74073f375389 100644 (file)
@@ -743,9 +743,39 @@ static int pakfire_jail_log(struct pakfire* pakfire, void* data, int priority,
        return 0;
 }
 
+static int pakfire_jail_epoll_add_fd(struct pakfire_jail* jail, int epollfd, int fd, int events) {
+       struct epoll_event event = {
+               .events = events|EPOLLHUP,
+               .data   = {
+                       .fd = fd,
+               },
+       };
+       int r;
+
+       // Read flags
+       int flags = fcntl(fd, F_GETFL, 0);
+
+       // Set modified flags
+       r  = fcntl(fd, F_SETFL, flags|O_NONBLOCK);
+       if (r < 0) {
+               CTX_ERROR(jail->ctx, "Could not set file descriptor %d into non-blocking mode: %s\n",
+                       fd, strerror(errno));
+               return -errno;
+       }
+
+       // Add the file descriptor to the loop
+       r = epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &event);
+       if (r < 0) {
+               ERROR(jail->pakfire, "Could not add file descriptor %d to epoll(): %s\n",
+                       fd, strerror(errno));
+               return -errno;
+       }
+
+       return 0;
+}
+
 static int pakfire_jail_wait(struct pakfire_jail* jail, struct pakfire_jail_exec* ctx) {
        int epollfd = -1;
-       struct epoll_event ev;
        struct epoll_event events[EPOLL_MAX_EVENTS];
        char garbage[8];
        int r = 0;
@@ -767,17 +797,30 @@ static int pakfire_jail_wait(struct pakfire_jail* jail, struct pakfire_jail_exec
 #endif /* ENABLE_DEBUG */
 
        // Make a list of all file descriptors we are interested in
-       const int fds[] = {
-               stdin,
-               stdout,
-               stderr,
-               pidfd,
-               timerfd,
-               log_INFO,
-               log_ERROR,
+       const struct pakfire_wait_fds {
+               const int fd;
+               const int events;
+       } fds[] = {
+               // Standard input/output
+               { stdin,  EPOLLOUT },
+               { stdout, EPOLLIN },
+               { stderr, EPOLLIN },
+
+               // Timer
+               { timerfd, EPOLLIN },
+
+               // Child Process
+               { ctx->pidfd, EPOLLIN },
+
+               // Log Pipes
+               { log_INFO, EPOLLIN },
+               { log_ERROR, EPOLLIN },
 #ifdef ENABLE_DEBUG
-               log_DEBUG,
+               { log_DEBUG, EPOLLIN },
 #endif /* ENABLE_DEBUG */
+
+               // Sentinel
+               { -1, 0 },
        };
 
        // Setup epoll
@@ -789,38 +832,15 @@ static int pakfire_jail_wait(struct pakfire_jail* jail, struct pakfire_jail_exec
        }
 
        // Turn file descriptors into non-blocking mode and add them to epoll()
-       for (unsigned int i = 0; i < sizeof(fds) / sizeof(*fds); i++) {
-               int fd = fds[i];
-
+       for (const struct pakfire_wait_fds* fd = fds; fd->events; fd++) {
                // Skip fds which were not initialized
-               if (fd < 0)
+               if (fd->fd < 0)
                        continue;
 
-               ev.events = EPOLLHUP;
-
-               if (fd == stdin)
-                       ev.events |= EPOLLOUT;
-               else
-                       ev.events |= EPOLLIN;
-
-               // Read flags
-               int flags = fcntl(fd, F_GETFL, 0);
-
-               // Set modified flags
-               if (fcntl(fd, F_SETFL, flags|O_NONBLOCK) < 0) {
-                       ERROR(jail->pakfire,
-                               "Could not set file descriptor %d into non-blocking mode: %m\n", fd);
-                       r = 1;
-                       goto ERROR;
-               }
-
-               ev.data.fd = fd;
-
-               if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) < 0) {
-                       ERROR(jail->pakfire, "Could not add file descriptor %d to epoll(): %m\n", fd);
-                       r = 1;
+               // Add the FD to the event loop
+               r = pakfire_jail_epoll_add_fd(jail, epollfd, fd->fd, fd->events);
+               if (r)
                        goto ERROR;
-               }
        }
 
        int ended = 0;