main: Implement a graceful shutdown

Gracefully stop the main loop and release used drivers upon receiving a
termination signal.

Without this, iio kernel drivers are not released, and modules cannot
be unloaded after iio-sensor-proxy termination.
This commit is contained in:
Yauhen Kharuzhy
2024-01-01 23:11:19 +02:00
parent 1ecb9fbb48
commit 408f75ff29

View File

@@ -17,6 +17,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <glib-unix.h>
#include <gio/gio.h>
#include <gudev/gudev.h>
#include <polkit/polkit.h>
@@ -981,6 +982,17 @@ sensor_changes (GUdevClient *client,
}
}
gboolean
termination_signal_handler (gpointer user_data)
{
SensorData *data = user_data;
g_debug ("Shutting down");
g_main_loop_quit (data->loop);
return G_SOURCE_REMOVE;
}
int main (int argc, char **argv)
{
SensorData *data;
@@ -1022,6 +1034,10 @@ int main (int argc, char **argv)
data->auth = polkit_authority_get_sync (NULL, NULL);
data->loop = g_main_loop_new (NULL, TRUE);
g_unix_signal_add (SIGINT, (GSourceFunc) termination_signal_handler, data);
g_unix_signal_add (SIGTERM, (GSourceFunc) termination_signal_handler, data);
g_main_loop_run (data->loop);
ret = data->ret;
free_sensor_data (data);