compositor: Use sigaction to trap SIGINT 24/28024/1
authorMarius Vlad <marius.vlad@collabora.com>
Wed, 21 Sep 2022 14:29:39 +0000 (17:29 +0300)
committerMarius Vlad <marius.vlad@collabora.com>
Fri, 23 Sep 2022 15:15:11 +0000 (18:15 +0300)
signalfd interacts badly with gdb's signal trapping - when hitting
ctrl-c in a debugger attached to weston, weston will receive the
signal. This results in weston exiting cleanly when the intent
was to use gdb to interfere with its operation.

Trapping SIGINT was introduced in commit 50dc6989 which ensured we
would call wl_display_terminate() on SIGINT or SIGTERM to clean
up our socket.

Killing weston with SIGINT is quite common for several developers,
so it's important to preserve this clean shutdown behaviour, so
we can't naively stop trapping SIGINT entirely.

Instead, use the sigaction() function to trap SIGINT, and have
the SIGINT handler send weston SIGUSR2 (SIGUSR1 is already
used by xwayland). SIGUSR2 can be trapped in the proper wayland
way via wl_event_loop_add_signal(). This way we can properly
break our event loop and clean up on SIGINT, but we can also
have gdb intercept SIGINT.

There are other ways around this, but I'm hoping this one allows
people to continue using ctrl-c to stop weston, and doesn't
require additional project specific gdb knowledge.

Bug-AGL: SPEC-4570

Signed-off-by: Derek Foreman <derek.foreman@collabora.com>
Signed-off-by: Marius Vlad <marius.vlad@collabora.com>
Change-Id: I42fc186296856861c8b6aee54a302c7c0574a88d

src/compositor.c

index add78af..564153b 100644 (file)
@@ -73,6 +73,12 @@ to_ivi_compositor(struct weston_compositor *ec)
        return weston_compositor_get_user_data(ec);
 }
 
+static void
+sigint_helper(int sig)
+{
+       raise(SIGUSR2);
+}
+
 static void
 handle_output_destroy(struct wl_listener *listener, void *data)
 {
@@ -1625,6 +1631,7 @@ int wet_main(int argc, char *argv[], const struct weston_testsuite_data *test_da
        struct weston_log_subscriber *logger;
        int ret = EXIT_FAILURE;
        bool xwayland = false;
+       struct sigaction action;
 
        const struct weston_option core_options[] = {
                { WESTON_OPTION_STRING, "backend", 'B', &backend },
@@ -1707,11 +1714,26 @@ int wet_main(int argc, char *argv[], const struct weston_testsuite_data *test_da
 
        signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
                                              display);
-       signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
+       signals[1] = wl_event_loop_add_signal(loop, SIGUSR2, on_term_signal,
                                              display);
        signals[2] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
                                              display);
 
+       /* When debugging the compositor, if use wl_event_loop_add_signal() to
+        * catch SIGINT, the debugger can't catch it, and attempting to stop
+        * the compositor from within the debugger results in weston exiting
+        * cleanly.
+        *
+        * Instead, use the sigaction() function, which sets up the signal in a
+        * way that gdb can successfully catch, but have the handler for SIGINT
+        * send SIGUSR2 (xwayland uses SIGUSR1), which we catch via
+        * wl_event_loop_add_signal().
+        */
+       action.sa_handler = sigint_helper;
+       sigemptyset(&action.sa_mask);
+       action.sa_flags = 0;
+       sigaction(SIGINT, &action, NULL);
+
        for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
                if (!signals[i])
                        goto error_signals;