That allows us to not keep any references to the modules.
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
sd_event_source* sigterm;
sd_event_source* sigint;
sd_event_source* modules_init;
- sd_event_source* modules_shutdown;
} events;
};
return collecty_modules_init(daemon->ctx, daemon);
}
-static int collecty_daemon_modules_shutdown(sd_event_source* source, void* data) {
- collecty_daemon* daemon = data;
-
- // Shutdown all modules
- return collecty_modules_shutdown(daemon->ctx, daemon);
-}
-
static int collecty_daemon_terminate(sd_event_source* source,
const struct signalfd_siginfo* si, void* data) {
collecty_daemon* self = data;
return r;
}
- // Shutdown all modules when the loop exits
- r = sd_event_add_exit(self->loop, &self->events.modules_shutdown,
- collecty_daemon_modules_shutdown, self);
- if (r < 0) {
- ERROR(self->ctx, "Failed to register module shutdown: %s\n", strerror(-r));
- return r;
- }
-
return 0;
}
static void collecty_daemon_free(collecty_daemon* self) {
- if (self->events.modules_shutdown)
- sd_event_source_unref(self->events.modules_shutdown);
if (self->events.modules_init)
sd_event_source_unref(self->events.modules_init);
if (self->events.sigterm)
return NULL;
}
+sd_event* collecty_daemon_loop(collecty_daemon* self) {
+ return sd_event_ref(self->loop);
+}
+
int collecty_daemon_run(collecty_daemon* self) {
int r;
#ifndef COLLECTY_DAEMON_H
#define COLLECTY_DAEMON_H
+#include <systemd/sd-event.h>
+
typedef struct collecty_daemon collecty_daemon;
#include "ctx.h"
collecty_daemon* collecty_daemon_ref(collecty_daemon* daemon);
collecty_daemon* collecty_daemon_unref(collecty_daemon* daemon);
+sd_event* collecty_daemon_loop(collecty_daemon* self);
+
int collecty_daemon_run(collecty_daemon* self);
#endif /* COLLECTY_DAEMON_H */
#include <stdlib.h>
#include <string.h>
+#include <systemd/sd-event.h>
+
#include "ctx.h"
#include "daemon.h"
#include "module.h"
// Methods
const collecty_module_methods* methods;
+
+ // Event Loop
+ sd_event* loop;
+
+ // Events
+ struct {
+ sd_event_source* shutdown;
+ } events;
};
+static int collecty_daemon_module_shutdown(sd_event_source* source, void* data) {
+ collecty_module* self = data;
+
+ DEBUG(self->ctx, "Shutting down module '%s'\n", collecty_module_name(self));
+
+ // Decrement the reference counter to free the module
+ collecty_module_unref(self);
+
+ return 0;
+}
+
+static int collecty_module_register_shutdown(collecty_module* self) {
+ int r;
+
+ // Call the shutdown function when the loop exits
+ r = sd_event_add_exit(self->loop, &self->events.shutdown,
+ collecty_daemon_module_shutdown, collecty_module_ref(self));
+ if (r < 0) {
+ ERROR(self->ctx, "Failed to register module shutdown: %s\n", strerror(-r));
+ }
+
+ return r;
+}
+
static int collecty_module_init(collecty_module* self) {
int r;
static void collecty_module_free(collecty_module* self) {
if (self->methods->free)
self->methods->free(self->ctx);
-
+ if (self->events.shutdown)
+ sd_event_source_unref(self->events.shutdown);
+ if (self->loop)
+ sd_event_unref(self->loop);
if (self->daemon)
collecty_daemon_unref(self->daemon);
if (self->ctx)
// Store a reference to the daemon
self->daemon = collecty_daemon_ref(daemon);
+ // Fetch a reference to the event loop
+ self->loop = collecty_daemon_loop(daemon);
+
// Store the methods
self->methods = methods;
+ // Register shutdown
+ r = collecty_module_register_shutdown(self);
+ if (r < 0)
+ goto ERROR;
+
// Initialize the module
r = collecty_module_init(self);
if (r < 0)
# #
#############################################################################*/
+#include <errno.h>
#include <stddef.h>
-#include <sys/queue.h>
+#include <stdlib.h>
#include "ctx.h"
#include "daemon.h"
#include "modules/loadavg.h"
// Register all modules
-static const collecty_module_methods* available_modules[] = {
+static const collecty_module_methods* modules[] = {
&loadavg_module,
NULL,
};
-struct module {
- collecty_module* module;
- STAILQ_ENTRY(module) nodes;
-};
-
-STAILQ_HEAD(modules, module) modules;
-
int collecty_modules_init(collecty_ctx* ctx, collecty_daemon* daemon) {
collecty_module* module = NULL;
int r;
- // Initialize the queue
- STAILQ_INIT(&modules);
-
DEBUG(ctx, "Initializing all modules...\n");
// Initialize all modules
- for (const collecty_module_methods** m = available_modules; *m; m++) {
+ for (const collecty_module_methods** m = modules; *m; m++) {
r = collecty_module_create(&module, ctx, daemon, *m);
if (r < 0)
return r;
- }
- return 0;
-}
-
-int collecty_modules_shutdown(collecty_ctx* ctx, collecty_daemon* daemon) {
- int r;
-
- DEBUG(ctx, "Shutting down all modules...\n");
-
-#if 0
- // Shutdown all modules
- for (collecty_module** m = modules; *m; m++) {
- r = collecty_module_shutdown(ctx, *m);
- if (r < 0)
- return r;
+ collecty_module_unref(module);
}
-#endif
return 0;
}
#include "daemon.h"
int collecty_modules_init(collecty_ctx* ctx, collecty_daemon* daemon);
-int collecty_modules_shutdown(collecty_ctx* ctx, collecty_daemon* daemon);
#endif /* COLLECTY_MODULES_H */