]>
git.ipfire.org Git - people/ms/pakfire.git/blob - src/cli/lib/daemon.c
1 /*#############################################################################
3 # Pakfire - The IPFire package management system #
4 # Copyright (C) 2023 Pakfire development team #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 #############################################################################*/
22 #include <sys/epoll.h>
24 #include <pakfire/ctx.h>
25 #include <pakfire/httpclient.h>
26 #include <pakfire/logging.h>
30 #define EPOLL_MAX_EVENTS 8
32 struct cli_daemon_ctx
{
33 struct pakfire_ctx
* ctx
;
36 static int cli_daemon_loop(struct pakfire_ctx
* ctx
) {
37 struct pakfire_httpclient
* httpclient
= NULL
;
38 struct epoll_event events
[EPOLL_MAX_EVENTS
];
39 struct epoll_event event
;
46 // Setup the HTTP client
47 r
= pakfire_httpclient_create(&httpclient
, ctx
);
52 epollfd
= epoll_create1(EPOLL_CLOEXEC
);
54 CTX_ERROR(ctx
, "Could not setup main loop: %s\n", strerror(errno
));
59 CTX_DEBUG(ctx
, "Entering main loop...\n");
61 // Main loop, loop, loop, looooop....
63 num
= epoll_wait(epollfd
, events
, EPOLL_MAX_EVENTS
, -1);
70 CTX_ERROR(ctx
, "epoll_wait() failed: %m\n");
78 for (int i
= 0; i
< num
; i
++) {
79 fd
= events
[i
].data
.fd
;
84 // Remove the file descriptor if it has been closed
86 r
= epoll_ctl(epollfd
, EPOLL_CTL_DEL
, fd
, NULL
);
88 CTX_ERROR(ctx
, "Could not remove closed fd %d: %s\n", fd
, strerror(errno
));
97 CTX_DEBUG(ctx
, "Exited main loop...\n");
100 pakfire_httpclient_unref(httpclient
);
107 int cli_daemon_main(struct pakfire_ctx
* ctx
) {
110 CTX_INFO(ctx
, "Pakfire Daemon has been successfully initialized\n");
113 r
= cli_daemon_loop(ctx
);