From 408f75ff294e42cb343d48093749526eb48ad04a Mon Sep 17 00:00:00 2001 From: Yauhen Kharuzhy Date: Mon, 1 Jan 2024 23:11:19 +0200 Subject: [PATCH] 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. --- src/iio-sensor-proxy.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/iio-sensor-proxy.c b/src/iio-sensor-proxy.c index 836e742..3f27a53 100644 --- a/src/iio-sensor-proxy.c +++ b/src/iio-sensor-proxy.c @@ -17,6 +17,7 @@ #include #include +#include #include #include #include @@ -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);