[gupnp/wip/phako/cleanup: 12/21] tests: Use g_unix_signal_add




commit 6b0bdf2a48fd60b37ff2bd98daf18688da2d865e
Author: Jens Georg <mail jensge org>
Date:   Sun May 30 11:49:08 2021 +0200

    tests: Use g_unix_signal_add
    
    We were using illegal functions in the Unix signal handler
    
    For windows, keep on using sigint for now. Perhaps moving to
    SetCtrlHandler might be an option:
    
    https://docs.microsoft.com/en-us/windows/console/registering-a-control-handler-function

 meson.build                |  2 ++
 tests/meson.build          |  2 +-
 tests/test-browsing.c      | 29 +++++++++++++++++++++--------
 tests/test-introspection.c | 44 +++++++++++++++++++++++++++-----------------
 tests/test-proxy.c         | 30 ++++++++++++++++++++++--------
 tests/test-server.c        | 32 ++++++++++++++++++++++++--------
 tests/test-white-list.c    | 29 +++++++++++++++++++++--------
 7 files changed, 118 insertions(+), 50 deletions(-)
---
diff --git a/meson.build b/meson.build
index 27ddf2b..8f6540e 100644
--- a/meson.build
+++ b/meson.build
@@ -25,6 +25,8 @@ guul = subproject('guul', default_options : ['default_library=static'])
 
 gssdp_dep = dependency('gssdp-1.2', version : '>= 1.2.3', default_options: ['sniffer=false'])
 
+gio_unix = dependency('gio-unix-2.0', version: '>= 2.44', required: host_machine.system() != 'windows')
+
 dependencies = [
     dependency('glib-2.0', version : '>= ' + glib_version),
     dependency('gio-2.0', version : '>= ' + glib_version),
diff --git a/tests/meson.build b/tests/meson.build
index 61b91bc..c27a3f5 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -2,7 +2,7 @@ foreach program : ['browsing', 'proxy', 'server', 'introspection', 'white-list']
     executable(
         'test-' + program,
         sources : 'test-@0@.c'.format(program),
-        dependencies : gupnp
+        dependencies : [gupnp, gio_unix]
     )
 endforeach
 
diff --git a/tests/test-browsing.c b/tests/test-browsing.c
index 89b6169..1dd4560 100644
--- a/tests/test-browsing.c
+++ b/tests/test-browsing.c
@@ -28,12 +28,31 @@
 
 GMainLoop *main_loop;
 
+#ifndef G_OS_WIN32
+#include <glib-unix.h>
+#endif
+
+static gboolean
+unix_signal_handler (gpointer user_data)
+{
+        g_main_loop_quit (main_loop);
+
+        return G_SOURCE_REMOVE;
+}
+
+
+#ifdef G_OS_WIN32
+/*
+ *  Since in Windows this is basically called from
+ *  the ConsoleCtrlHandler which does not share the restrictions of Unix signals
+ */
 static void
 interrupt_signal_handler (G_GNUC_UNUSED int signum)
 {
-        g_main_loop_quit (main_loop);
+        unix_signal_handler (NULL);
 }
 
+#endif
 static void
 device_proxy_available_cb (G_GNUC_UNUSED GUPnPControlPoint *cp,
                            GUPnPDeviceProxy                *proxy)
@@ -96,9 +115,6 @@ main (G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv)
         GError *error;
         GUPnPContext *context;
         GUPnPControlPoint *cp;
-#ifndef G_OS_WIN32
-        struct sigaction sig_action;
-#endif /* G_OS_WIN32 */
 
         setlocale (LC_ALL, "");
 
@@ -137,10 +153,7 @@ main (G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv)
         main_loop = g_main_loop_new (NULL, FALSE);
 
 #ifndef G_OS_WIN32
-        /* Hook the handler for SIGTERM */
-        memset (&sig_action, 0, sizeof (sig_action));
-        sig_action.sa_handler = interrupt_signal_handler;
-        sigaction (SIGINT, &sig_action, NULL);
+        g_unix_signal_add (SIGINT, unix_signal_handler, NULL);
 #else
         signal(SIGINT, interrupt_signal_handler);
 #endif /* G_OS_WIN32 */
diff --git a/tests/test-introspection.c b/tests/test-introspection.c
index bd24869..1512317 100644
--- a/tests/test-introspection.c
+++ b/tests/test-introspection.c
@@ -28,22 +28,42 @@
 #include <signal.h>
 #include <glib.h>
 
+#ifndef G_OS_WIN32
+#include <glib-unix.h>
+#endif
+
 GMainLoop *main_loop;
 
 static GCancellable *cancellable;
 
-static void
-interrupt_signal_handler (G_GNUC_UNUSED int signum)
+static gboolean
+unix_signal_handler (gpointer user_data)
 {
         if (g_cancellable_is_cancelled (cancellable)) {
                 g_main_loop_quit (main_loop);
+
+                return G_SOURCE_REMOVE;
         } else {
                 g_print ("Canceling all introspection calls. "
-                         "Press ^C again to quit.\n");
+                         "Press ^C again to force quit.\n");
                 g_cancellable_cancel (cancellable);
+                return G_SOURCE_CONTINUE;
         }
 }
 
+#ifdef G_OS_WIN32
+/*
+ *  Since in Windows this is basically called from
+ *  the ConsoleCtrlHandler which does not share the restrictions of Unix signals
+ */
+static void
+interrupt_signal_handler (G_GNUC_UNUSED int signum)
+{
+        unix_signal_handler (NULL);
+}
+
+#endif
+
 static void
 print_action_arguments (GList *argument_list)
 {
@@ -101,7 +121,7 @@ print_state_variables (GUPnPServiceIntrospection *introspection)
                 g_print ("state variables:\n");
                 for (iter = variables; iter; iter = iter->next) {
                         GUPnPServiceStateVariableInfo *variable;
-                        GValue default_value;
+                        GValue default_value = G_VALUE_INIT;
                         const char * default_value_str;
 
                         variable = (GUPnPServiceStateVariableInfo *) iter->data;
@@ -113,7 +133,6 @@ print_state_variables (GUPnPServiceIntrospection *introspection)
                                  g_type_name (variable->type),
                                  variable->send_events? "yes": "no");
 
-                        memset (&default_value, 0, sizeof (GValue));
                         g_value_init (&default_value, G_TYPE_STRING);
                         g_value_transform (&variable->default_value,
                                            &default_value);
@@ -125,11 +144,7 @@ print_state_variables (GUPnPServiceIntrospection *introspection)
                         g_value_unset (&default_value);
 
                         if (variable->is_numeric) {
-                                GValue min, max, step;
-
-                                memset (&min, 0, sizeof (GValue));
-                                memset (&max, 0, sizeof (GValue));
-                                memset (&step, 0, sizeof (GValue));
+                                GValue min = G_VALUE_INIT, max = G_VALUE_INIT, step = G_VALUE_INIT;
 
                                 g_value_init (&min, G_TYPE_STRING);
                                 g_value_init (&max, G_TYPE_STRING);
@@ -225,9 +240,6 @@ main (int argc, char **argv)
         GError *error = NULL;
         GUPnPContext *context;
         GUPnPControlPoint *cp;
-#ifndef G_OS_WIN32
-        struct sigaction sig_action;
-#endif /* G_OS_WIN32 */
 
         error = NULL;
         context = g_initable_new (GUPNP_TYPE_CONTEXT, NULL, &error, NULL);
@@ -257,11 +269,9 @@ main (int argc, char **argv)
 
         main_loop = g_main_loop_new (NULL, FALSE);
 
-        /* Hook the handler for SIGTERM */
+        /* Hook the handler for SIGINT */
 #ifndef G_OS_WIN32
-        memset (&sig_action, 0, sizeof (sig_action));
-        sig_action.sa_handler = interrupt_signal_handler;
-        sigaction (SIGINT, &sig_action, NULL);
+        g_unix_signal_add (SIGINT, unix_signal_handler, NULL);
 #else
         signal(SIGINT,interrupt_signal_handler);
 #endif /* G_OS_WIN32 */
diff --git a/tests/test-proxy.c b/tests/test-proxy.c
index 77530e7..bbf44d6 100644
--- a/tests/test-proxy.c
+++ b/tests/test-proxy.c
@@ -28,12 +28,31 @@
 
 GMainLoop *main_loop;
 
+#ifndef G_OS_WIN32
+#include <glib-unix.h>
+#endif
+
+static gboolean
+unix_signal_handler (gpointer user_data)
+{
+        g_main_loop_quit (main_loop);
+
+        return G_SOURCE_REMOVE;
+}
+
+#ifdef G_OS_WIN32
+/*
+ *  Since in Windows this is basically called from
+ *  the ConsoleCtrlHandler which does not share the restrictions of Unix signals
+ */
 static void
 interrupt_signal_handler (G_GNUC_UNUSED int signum)
 {
-        g_main_loop_quit (main_loop);
+        unix_signal_handler (NULL);
 }
 
+#endif
+
 static void
 subscription_lost_cb (G_GNUC_UNUSED GUPnPServiceProxy *proxy,
                       const GError                    *reason,
@@ -166,9 +185,6 @@ main (G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv)
         GError *error;
         GUPnPContext *context;
         GUPnPControlPoint *cp;
-#ifndef G_OS_WIN32
-        struct sigaction sig_action;
-#endif /* G_OS_WIN32 */
 
         setlocale (LC_ALL, "");
 
@@ -201,11 +217,9 @@ main (G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv)
 
         /* Hook the handler for SIGTERM */
 #ifndef G_OS_WIN32
-        memset (&sig_action, 0, sizeof (sig_action));
-        sig_action.sa_handler = interrupt_signal_handler;
-        sigaction (SIGINT, &sig_action, NULL);
+        g_unix_signal_add (SIGINT, unix_signal_handler, NULL);
 #else
-        signal(SIGINT,interrupt_signal_handler);
+        signal(SIGINT, interrupt_signal_handler);
 #endif /* G_OS_WIN32 */
 
         g_main_loop_run (main_loop);
diff --git a/tests/test-server.c b/tests/test-server.c
index 53d704b..940f8d9 100644
--- a/tests/test-server.c
+++ b/tests/test-server.c
@@ -26,14 +26,35 @@
 #include <string.h>
 #include <signal.h>
 
+
+#ifndef G_OS_WIN32
+#include <glib-unix.h>
+#endif
+
 GMainLoop *main_loop;
 
+static gboolean
+unix_signal_handler (gpointer user_data)
+{
+        g_main_loop_quit (main_loop);
+
+        return G_SOURCE_REMOVE;
+}
+
+
+#ifdef G_OS_WIN32
+/*
+ *  Since in Windows this is basically called from
+ *  the ConsoleCtrlHandler which does not share the restrictions of Unix signals
+ */
 static void
 interrupt_signal_handler (G_GNUC_UNUSED int signum)
 {
-        g_main_loop_quit (main_loop);
+        unix_signal_handler (NULL);
 }
 
+#endif
+
 static void
 notify_failed_cb (G_GNUC_UNUSED GUPnPService *service,
                   G_GNUC_UNUSED const GList  *callback_urls,
@@ -62,9 +83,6 @@ main (int argc, char **argv)
         GUPnPContext *context;
         GUPnPRootDevice *dev;
         GUPnPServiceInfo *content_dir;
-#ifndef G_OS_WIN32
-        struct sigaction sig_action;
-#endif /* G_OS_WIN32 */
 
         if (argc < 2) {
                 g_printerr ("Usage: %s DESCRIPTION_FILE\n", argv[0]);
@@ -126,11 +144,9 @@ main (int argc, char **argv)
 
         main_loop = g_main_loop_new (NULL, FALSE);
 
-        /* Hook the handler for SIGTERM */
+        /* Hook the handler for SIGINT */
 #ifndef G_OS_WIN32
-        memset (&sig_action, 0, sizeof (sig_action));
-        sig_action.sa_handler = interrupt_signal_handler;
-        sigaction (SIGINT, &sig_action, NULL);
+        g_unix_signal_add (SIGINT, unix_signal_handler, NULL);
 #else
         signal(SIGINT, interrupt_signal_handler);
 #endif /* G_OS_WIN32 */
diff --git a/tests/test-white-list.c b/tests/test-white-list.c
index 037ca1c..7e8624a 100644
--- a/tests/test-white-list.c
+++ b/tests/test-white-list.c
@@ -29,12 +29,31 @@
 
 GMainLoop *main_loop;
 
+#ifndef G_OS_WIN32
+#include <glib-unix.h>
+#endif
+
+static gboolean
+unix_signal_handler (gpointer user_data)
+{
+        g_main_loop_quit (main_loop);
+
+        return G_SOURCE_REMOVE;
+}
+
+
+#ifdef G_OS_WIN32
+/*
+ *  Since in Windows this is basically called from
+ *  the ConsoleCtrlHandler which does not share the restrictions of Unix signals
+ */
 static void
 interrupt_signal_handler (G_GNUC_UNUSED int signum)
 {
-        g_main_loop_quit (main_loop);
+        unix_signal_handler (NULL);
 }
 
+#endif
 static void
 device_proxy_available_cb (G_GNUC_UNUSED GUPnPControlPoint *cp,
                            GUPnPDeviceProxy                *proxy)
@@ -246,9 +265,6 @@ main (G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv)
 {
         GUPnPContextManager *cm;
         guint id;
-#ifndef G_OS_WIN32
-        struct sigaction sig_action;
-#endif /* G_OS_WIN32 */
 
         setlocale (LC_ALL, "");
 
@@ -269,10 +285,7 @@ main (G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv)
         id = g_timeout_add_seconds (5, change_white_list, cm);
 
 #ifndef G_OS_WIN32
-        /* Hook the handler for SIGTERM */
-        memset (&sig_action, 0, sizeof (sig_action));
-        sig_action.sa_handler = interrupt_signal_handler;
-        sigaction (SIGINT, &sig_action, NULL);
+        g_unix_signal_add (SIGINT, unix_signal_handler, NULL);
 #else
         signal(SIGINT, interrupt_signal_handler);
 #endif /* G_OS_WIN32 */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]