# #
#############################################################################*/
-#include <errno.h>
-#include <sys/epoll.h>
-
#include <pakfire/ctx.h>
-#include <pakfire/httpclient.h>
-#include <pakfire/logging.h>
+#include <pakfire/daemon.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) {
+ 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;
}