]> git.ipfire.org Git - people/ms/pakfire.git/commitdiff
cli: daemon: Build more scaffolding
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 21 Oct 2023 10:36:29 +0000 (10:36 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 21 Oct 2023 11:11:51 +0000 (11:11 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/cli/lib/daemon.c [new file with mode: 0644]
src/cli/lib/daemon.h [new file with mode: 0644]
src/cli/pakfire-daemon.c

index d80f50d7ed9fc5291af99718e135149efdaa5033..a301d99bf7d30ff545f5ba5dc09a43fa0ce87521 100644 (file)
@@ -494,6 +494,8 @@ libcli_la_SOURCES = \
        src/cli/lib/command.h \
        src/cli/lib/config.c \
        src/cli/lib/config.h \
+       src/cli/lib/daemon.c \
+       src/cli/lib/daemon.h \
        src/cli/lib/dist.c \
        src/cli/lib/dist.h \
        src/cli/lib/dump.c \
diff --git a/src/cli/lib/daemon.c b/src/cli/lib/daemon.c
new file mode 100644 (file)
index 0000000..a781468
--- /dev/null
@@ -0,0 +1,119 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2023 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <errno.h>
+#include <sys/epoll.h>
+
+#include <pakfire/ctx.h>
+#include <pakfire/httpclient.h>
+#include <pakfire/logging.h>
+
+#include "daemon.h"
+
+#define EPOLL_MAX_EVENTS 8
+
+struct cli_daemon_ctx {
+       struct pakfire_ctx* ctx;
+};
+
+static int cli_daemon_loop(struct pakfire_ctx* ctx) {
+       struct pakfire_httpclient* httpclient = NULL;
+       struct epoll_event events[EPOLL_MAX_EVENTS];
+       struct epoll_event event;
+       int epollfd = -1;
+       int num = 0;
+       int fd = -1;
+       int e;
+       int r;
+
+       // Setup the HTTP client
+       r = pakfire_httpclient_create(&httpclient, ctx);
+       if (r)
+               goto ERROR;
+
+       // Setup epoll()
+       epollfd = epoll_create1(EPOLL_CLOEXEC);
+       if (epollfd < 0) {
+               CTX_ERROR(ctx, "Could not setup main loop: %s\n", strerror(errno));
+               r = -errno;
+               goto ERROR;
+       }
+
+       CTX_DEBUG(ctx, "Entering main loop...\n");
+
+       // Main loop, loop, loop, looooop....
+       for (;;) {
+               num = epoll_wait(epollfd, events, EPOLL_MAX_EVENTS, -1);
+               if (num < 1) {
+                       switch (errno) {
+                               case EINTR:
+                                       break;
+
+                               default:
+                                       CTX_ERROR(ctx, "epoll_wait() failed: %m\n");
+                                       r = -errno;
+                                       break;
+                       }
+                       goto ERROR;
+               }
+
+               // Handle all events
+               for (int i = 0; i < num; i++) {
+                       fd = events[i].data.fd;
+                       e  = events[i].events;
+
+                       // XXX Do the work...
+
+                       // Remove the file descriptor if it has been closed
+                       if (e & EPOLLHUP) {
+                               r = epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, NULL);
+                               if (r) {
+                                       CTX_ERROR(ctx, "Could not remove closed fd %d: %s\n", fd, strerror(errno));
+                                       r = -errno;
+                                       goto ERROR;
+                               }
+                       }
+               }
+       }
+
+ERROR:
+       CTX_DEBUG(ctx, "Exited main loop...\n");
+
+       if (httpclient)
+               pakfire_httpclient_unref(httpclient);
+       if (epollfd >= 0)
+               close(epollfd);
+
+       return r;
+}
+
+int cli_daemon_main(struct pakfire_ctx* ctx) {
+       int r;
+
+       CTX_INFO(ctx, "Pakfire Daemon has been successfully initialized\n");
+
+       // Run the main loop
+       r = cli_daemon_loop(ctx);
+       if (r)
+               goto ERROR;
+
+ERROR:
+       return r;
+}
diff --git a/src/cli/lib/daemon.h b/src/cli/lib/daemon.h
new file mode 100644 (file)
index 0000000..736c762
--- /dev/null
@@ -0,0 +1,28 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2023 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef PAKFIRE_CLI_DAEMON_H
+#define PAKFIRE_CLI_DAEMON_H
+
+#include <pakfire/ctx.h>
+
+int cli_daemon_main(struct pakfire_ctx* ctx);
+
+#endif /* PAKFIRE_CLI_DAEMON_H */
index d9d3181c35fc84d14a579ae38615fb9f1c2b551e..17c3e5a191339431a60c8c4aabc216b275c89e79 100644 (file)
@@ -24,6 +24,7 @@
 #include <pakfire/ctx.h>
 
 #include "lib/command.h"
+#include "lib/daemon.h"
 
 const char* argp_program_version = PACKAGE_VERSION;
 
@@ -67,7 +68,8 @@ int main(int argc, char* argv[]) {
        if (r)
                goto ERROR;
 
-       // XXX Do the work
+       // Do the work...
+       r = cli_daemon_main(ctx);
 
 ERROR:
        if (ctx)