From: Michael Tremer Date: Sat, 21 Oct 2023 11:10:33 +0000 (+0000) Subject: daemon: Move into libpakfire X-Git-Tag: 0.9.30~1395 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a236ec387347adae2cc1fc080ed517031650b18e;p=pakfire.git daemon: Move into libpakfire This is currently not exported which means that the linker will link those functions statically into pakfire-daemon. Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index a301d99bf..1e619a4b2 100644 --- a/Makefile.am +++ b/Makefile.am @@ -216,6 +216,7 @@ libpakfire_la_SOURCES = \ src/libpakfire/compress.c \ src/libpakfire/config.c \ src/libpakfire/ctx.c \ + src/libpakfire/daemon.c \ src/libpakfire/db.c \ src/libpakfire/dependencies.c \ src/libpakfire/digest.c \ @@ -260,6 +261,7 @@ pkginclude_HEADERS += \ src/libpakfire/include/pakfire/config.h \ src/libpakfire/include/pakfire/constants.h \ src/libpakfire/include/pakfire/ctx.h \ + src/libpakfire/include/pakfire/daemon.h \ src/libpakfire/include/pakfire/db.h \ src/libpakfire/include/pakfire/dependencies.h \ src/libpakfire/include/pakfire/digest.h \ diff --git a/src/cli/lib/daemon.c b/src/cli/lib/daemon.c index a78146821..d608457bc 100644 --- a/src/cli/lib/daemon.c +++ b/src/cli/lib/daemon.c @@ -18,102 +18,24 @@ # # #############################################################################*/ -#include -#include - #include -#include -#include +#include #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) { + struct pakfire_daemon* daemon = NULL; int r; - CTX_INFO(ctx, "Pakfire Daemon has been successfully initialized\n"); + // Create the daemon + r = pakfire_daemon_create(&daemon, ctx); + + // Run the daemon + r = pakfire_daemon_main(daemon); - // Run the main loop - r = cli_daemon_loop(ctx); - if (r) - goto ERROR; + // Cleanup + if (daemon) + pakfire_daemon_unref(daemon); -ERROR: return r; } diff --git a/src/libpakfire/daemon.c b/src/libpakfire/daemon.c new file mode 100644 index 000000000..f04eb83b8 --- /dev/null +++ b/src/libpakfire/daemon.c @@ -0,0 +1,158 @@ +/*############################################################################# +# # +# 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 . # +# # +#############################################################################*/ + +#include +#include +#include + +#include +#include +#include + +#define EPOLL_MAX_EVENTS 8 + +struct pakfire_daemon { + struct pakfire_ctx* ctx; + int nrefs; + + // A HTTP Client + struct pakfire_httpclient* httpclient; +}; + +static void pakfire_daemon_free(struct pakfire_daemon* daemon) { + if (daemon->httpclient) + pakfire_httpclient_unref(daemon->httpclient); + if (daemon->ctx) + pakfire_ctx_unref(daemon->ctx); + free(daemon); +} + +int pakfire_daemon_create(struct pakfire_daemon** daemon, struct pakfire_ctx* ctx) { + struct pakfire_daemon* d = NULL; + int r; + + // Allocate some memory + d = calloc(1, sizeof(*d)); + if (!d) + return -errno; + + // Store a reference to the context + d->ctx = pakfire_ctx_ref(ctx); + + // Initialize the reference counter + d->nrefs = 1; + + // Setup the HTTP client + r = pakfire_httpclient_create(&d->httpclient, d->ctx); + if (r) + goto ERROR; + + // Return the pointer + *daemon = d; + + return 0; + +ERROR: + if (d) + pakfire_daemon_unref(d); + + return r; +} + +struct pakfire_daemon* pakfire_daemon_ref(struct pakfire_daemon* daemon) { + ++daemon->nrefs; + + return daemon; +} + +struct pakfire_daemon* pakfire_daemon_unref(struct pakfire_daemon* daemon) { + if (--daemon->nrefs > 0) + return daemon; + + pakfire_daemon_free(daemon); + return NULL; +} + +static int pakfire_daemon_loop(struct pakfire_daemon* daemon) { + 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 epoll() + epollfd = epoll_create1(EPOLL_CLOEXEC); + if (epollfd < 0) { + CTX_ERROR(daemon->ctx, "Could not setup main loop: %s\n", strerror(errno)); + r = -errno; + goto ERROR; + } + + CTX_DEBUG(daemon->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(daemon->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(daemon->ctx, "Could not remove closed fd %d: %s\n", fd, strerror(errno)); + r = -errno; + goto ERROR; + } + } + } + } + +ERROR: + CTX_DEBUG(daemon->ctx, "Exited main loop...\n"); + + if (epollfd >= 0) + close(epollfd); + + return r; +} + +int pakfire_daemon_main(struct pakfire_daemon* daemon) { + return pakfire_daemon_loop(daemon); +} diff --git a/src/libpakfire/include/pakfire/daemon.h b/src/libpakfire/include/pakfire/daemon.h new file mode 100644 index 000000000..ee5bcbad5 --- /dev/null +++ b/src/libpakfire/include/pakfire/daemon.h @@ -0,0 +1,38 @@ +/*############################################################################# +# # +# 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 . # +# # +#############################################################################*/ + +#ifndef PAKFIRE_DAEMON_H +#define PAKFIRE_DAEMON_H + +#ifdef PAKFIRE_PRIVATE + +struct pakfire_daemon; + +#include + +int pakfire_daemon_create(struct pakfire_daemon** daemon, struct pakfire_ctx* ctx); + +struct pakfire_daemon* pakfire_daemon_ref(struct pakfire_daemon* daemon); +struct pakfire_daemon* pakfire_daemon_unref(struct pakfire_daemon* daemon); + +int pakfire_daemon_main(struct pakfire_daemon* daemon); + +#endif /* PAKFIRE_PRIVATE */ +#endif /* PAKFIRE_DAEMON_H */