static int pakfire_daemon_terminate(sd_event_source* source,
const struct signalfd_siginfo* si, void* data) {
- struct pakfire_daemon* daemon = data;
+ struct pakfire_daemon* self = data;
- DEBUG(daemon->ctx, "Received signal to terminate...\n");
+ DEBUG(self->ctx, "Received signal to terminate...\n");
// Terminate all jobs
- pakfire_builder_terminate_jobs(daemon->builder);
+ pakfire_builder_terminate_jobs(self->builder);
return sd_event_exit(sd_event_source_get_event(source), 0);
}
/*
* Prevents that the system can be shut down when there are jobs running...
*/
-static int pakfire_daemon_inhibit_shutdown(struct pakfire_daemon* daemon) {
+static int pakfire_daemon_inhibit_shutdown(struct pakfire_daemon* self) {
sd_bus_error error = SD_BUS_ERROR_NULL;
sd_bus_message* reply = NULL;
int r;
// Don't do this again if we are already holding the lock
- if (daemon->inhibitfd >= 0)
+ if (self->inhibitfd >= 0)
return 0;
// Call the Inhibit method
r = sd_bus_call_method(
- daemon->bus,
+ self->bus,
// Destination, Path & Interface
"org.freedesktop.login1",
"block"
);
if (r < 0) {
- ERROR(daemon->ctx, "Failed to inhibit shutdown: %s\n", error.message);
+ ERROR(self->ctx, "Failed to inhibit shutdown: %s\n", error.message);
goto ERROR;
}
// Read the file descriptor from the reply
- r = sd_bus_message_read(reply, "h", &daemon->inhibitfd);
+ r = sd_bus_message_read(reply, "h", &self->inhibitfd);
if (r < 0) {
- ERROR(daemon->ctx, "Failed to parse response message: %s\n", strerror(-r));
+ ERROR(self->ctx, "Failed to parse response message: %s\n", strerror(-r));
goto ERROR;
}
- DEBUG(daemon->ctx, "Successfully inhibited shutdown\n");
+ DEBUG(self->ctx, "Successfully inhibited shutdown\n");
ERROR:
sd_bus_error_free(&error);
return r;
}
-static int pakfire_daemon_release_inhibit_shutdown(struct pakfire_daemon* daemon) {
+static int pakfire_daemon_release_inhibit_shutdown(struct pakfire_daemon* self) {
// If we don't hold the lock, we cannot release it
- if (daemon->inhibitfd >= 0) {
- close(daemon->inhibitfd);
- daemon->inhibitfd = -EBADF;
+ if (self->inhibitfd >= 0) {
+ close(self->inhibitfd);
+ self->inhibitfd = -EBADF;
- DEBUG(daemon->ctx, "Released shutdown inhibition\n");
+ DEBUG(self->ctx, "Released shutdown inhibition\n");
}
return 0;
static int pakfire_daemon_prepare_for_shutdown(
sd_bus_message* message, void* data, sd_bus_error* error) {
- struct pakfire_daemon* daemon = data;
+ struct pakfire_daemon* self = data;
int shutdown;
int r;
// Read the boolean value from the signal message
r = sd_bus_message_read(message, "b", &shutdown);
if (r < 0) {
- ERROR(daemon->ctx, "Failed to parse PrepareForShutdown signal: %s\n", strerror(-r));
+ ERROR(self->ctx, "Failed to parse PrepareForShutdown signal: %s\n", strerror(-r));
return r;
}
// If shutdown is true, the system is going to shut down soon.
// If false, the shutdown has been canceled.
if (shutdown) {
- DEBUG(daemon->ctx, "Expecting the system to shut down soon...\n");
+ DEBUG(self->ctx, "Expecting the system to shut down soon...\n");
// XXX TODO
} else {
- DEBUG(daemon->ctx, "Shutdown has been canceled\n");
+ DEBUG(self->ctx, "Shutdown has been canceled\n");
// XXX TODO
}
return 0;
}
-static int pakfire_daemon_setup_bus(struct pakfire_daemon* daemon) {
+static int pakfire_daemon_setup_bus(struct pakfire_daemon* self) {
int r;
// Connect to the system bus
- r = sd_bus_open_system(&daemon->bus);
+ r = sd_bus_open_system(&self->bus);
if (r < 0) {
- ERROR(daemon->ctx, "Could not connect to system bus: %s\n", strerror(-r));
+ ERROR(self->ctx, "Could not connect to system bus: %s\n", strerror(-r));
return r;
}
// Attach the bus to the event loop
- r = sd_bus_attach_event(daemon->bus, daemon->loop, SD_EVENT_PRIORITY_NORMAL);
+ r = sd_bus_attach_event(self->bus, self->loop, SD_EVENT_PRIORITY_NORMAL);
if (r < 0) {
- ERROR(daemon->ctx, "Could not attach the bus to the event loop: %s\n", strerror(-r));
+ ERROR(self->ctx, "Could not attach the bus to the event loop: %s\n", strerror(-r));
return r;
}
// Add a signal match
r = sd_bus_match_signal(
- daemon->bus,
+ self->bus,
NULL,
// Destination, Path & Interface
daemon
);
if (r < 0) {
- ERROR(daemon->ctx, "Could not register PrepareForShutdown signal: %s\n", strerror(-r));
+ ERROR(self->ctx, "Could not register PrepareForShutdown signal: %s\n", strerror(-r));
return r;
}
return 0;
}
-static int pakfire_daemon_setup_loop(struct pakfire_daemon* daemon) {
+static int pakfire_daemon_setup_loop(struct pakfire_daemon* self) {
int r;
// Fetch a reference to the context's event loop
- r = pakfire_ctx_loop(daemon->ctx, &daemon->loop);
+ r = pakfire_ctx_loop(self->ctx, &self->loop);
if (r < 0) {
- ERROR(daemon->ctx, "Could not setup event loop: %s\n", strerror(-r));
+ ERROR(self->ctx, "Could not setup event loop: %s\n", strerror(-r));
return r;
}
// Enable the watchdog
- r = sd_event_set_watchdog(daemon->loop, 1);
+ r = sd_event_set_watchdog(self->loop, 1);
if (r < 0) {
- ERROR(daemon->ctx, "Could not activate watchdog: %s\n", strerror(-r));
+ ERROR(self->ctx, "Could not activate watchdog: %s\n", strerror(-r));
return r;
}
// Listen for SIGTERM
- r = sd_event_add_signal(daemon->loop, NULL, SIGTERM|SD_EVENT_SIGNAL_PROCMASK,
+ r = sd_event_add_signal(self->loop, NULL, SIGTERM|SD_EVENT_SIGNAL_PROCMASK,
pakfire_daemon_terminate, daemon);
if (r < 0) {
- ERROR(daemon->ctx, "Could not register handling SIGTERM: %s\n", strerror(-r));
+ ERROR(self->ctx, "Could not register handling SIGTERM: %s\n", strerror(-r));
return r;
}
// Listen for SIGINT
- r = sd_event_add_signal(daemon->loop, NULL, SIGINT|SD_EVENT_SIGNAL_PROCMASK,
+ r = sd_event_add_signal(self->loop, NULL, SIGINT|SD_EVENT_SIGNAL_PROCMASK,
pakfire_daemon_terminate, daemon);
if (r < 0) {
- ERROR(daemon->ctx, "Could not register handling SIGINT: %s\n", strerror(-r));
+ ERROR(self->ctx, "Could not register handling SIGINT: %s\n", strerror(-r));
return r;
}
// Listen for SIGCHLD
- r = sd_event_add_signal(daemon->loop, NULL, SIGCHLD|SD_EVENT_SIGNAL_PROCMASK,
+ r = sd_event_add_signal(self->loop, NULL, SIGCHLD|SD_EVENT_SIGNAL_PROCMASK,
pakfire_daemon_SIGCHLD, daemon);
if (r < 0) {
- ERROR(daemon->ctx, "Could not register handling SIGCHLD: %s\n", strerror(-r));
+ ERROR(self->ctx, "Could not register handling SIGCHLD: %s\n", strerror(-r));
return r;
}
return r;
}
-static void pakfire_daemon_free(struct pakfire_daemon* daemon) {
+static void pakfire_daemon_free(struct pakfire_daemon* self) {
// Release shutdown inhibition
- pakfire_daemon_release_inhibit_shutdown(daemon);
-
- if (daemon->builder)
- pakfire_builder_unref(daemon->builder);
- if (daemon->client)
- pakfire_client_unref(daemon->client);
- if (daemon->cgroup)
- pakfire_cgroup_unref(daemon->cgroup);
- if (daemon->loop)
- sd_event_unref(daemon->loop);
- if (daemon->bus)
- sd_bus_unref(daemon->bus);
- if (daemon->ctx)
- pakfire_ctx_unref(daemon->ctx);
- free(daemon);
+ pakfire_daemon_release_inhibit_shutdown(self);
+
+ if (self->builder)
+ pakfire_builder_unref(self->builder);
+ if (self->client)
+ pakfire_client_unref(self->client);
+ if (self->cgroup)
+ pakfire_cgroup_unref(self->cgroup);
+ if (self->loop)
+ sd_event_unref(self->loop);
+ if (self->bus)
+ sd_bus_unref(self->bus);
+ if (self->ctx)
+ pakfire_ctx_unref(self->ctx);
+ free(self);
}
int pakfire_daemon_create(struct pakfire_daemon** daemon, struct pakfire_ctx* ctx) {
- struct pakfire_daemon* d = NULL;
+ struct pakfire_daemon* self = NULL;
int r;
// Allocate some memory
- d = calloc(1, sizeof(*d));
- if (!d)
+ self = calloc(1, sizeof(*self));
+ if (!self)
return -errno;
// Store a reference to the context
- d->ctx = pakfire_ctx_ref(ctx);
+ self->ctx = pakfire_ctx_ref(ctx);
// Initialize the reference counter
- d->nrefs = 1;
+ self->nrefs = 1;
// Initialize file descriptors
- d->inhibitfd = -EBADF;
+ self->inhibitfd = -EBADF;
// Setup the event loop
- r = pakfire_daemon_setup_loop(d);
- if (r)
+ r = pakfire_daemon_setup_loop(self);
+ if (r < 0)
goto ERROR;
// Setup dbus
- r = pakfire_daemon_setup_bus(d);
+ r = pakfire_daemon_setup_bus(self);
if (r < 0)
goto ERROR;
// Setup client
- r = pakfire_daemon_setup_client(d);
+ r = pakfire_daemon_setup_client(self);
if (r < 0)
goto ERROR;
// Create builder
- r = pakfire_client_builder(&d->builder, d->client);
+ r = pakfire_client_builder(&self->builder, self->client);
if (r < 0)
goto ERROR;
// Create the cgroup
- r = pakfire_cgroup_create(&d->cgroup, d->ctx, NULL, "pakfire-daemon", 0);
+ r = pakfire_cgroup_create(&self->cgroup, self->ctx, NULL, "pakfire-daemon", 0);
if (r < 0)
goto ERROR;
// Return the pointer
- *daemon = d;
+ *daemon = self;
return 0;
ERROR:
- if (d)
- pakfire_daemon_unref(d);
+ if (self)
+ pakfire_daemon_unref(self);
return r;
}
-struct pakfire_daemon* pakfire_daemon_ref(struct pakfire_daemon* daemon) {
- ++daemon->nrefs;
+struct pakfire_daemon* pakfire_daemon_ref(struct pakfire_daemon* self) {
+ ++self->nrefs;
- return daemon;
+ return self;
}
-struct pakfire_daemon* pakfire_daemon_unref(struct pakfire_daemon* daemon) {
- if (--daemon->nrefs > 0)
- return daemon;
+struct pakfire_daemon* pakfire_daemon_unref(struct pakfire_daemon* self) {
+ if (--self->nrefs > 0)
+ return self;
- pakfire_daemon_free(daemon);
+ pakfire_daemon_free(self);
return NULL;
}
-int pakfire_daemon_main(struct pakfire_daemon* daemon) {
+int pakfire_daemon_main(struct pakfire_daemon* self) {
int r;
// We are now ready
sd_notify(0, "READY=1\n" "STATUS=Ready...");
// Launch the event loop
- r = sd_event_loop(daemon->loop);
+ r = sd_event_loop(self->loop);
if (r < 0) {
- ERROR(daemon->ctx, "Could not run the event loop: %s\n", strerror(-r));
+ ERROR(self->ctx, "Could not run the event loop: %s\n", strerror(-r));
goto ERROR;
}