| 1 | // Package main provides the Wails desktop shell around the Reasonix kernel. |
| 2 | // |
| 3 | // webkit_compat_linux.go applies WebKit2GTK compatibility workarounds around |
| 4 | // Wails startup and JavaScriptCore initialization. |
| 5 | |
| 6 | package main |
| 7 | |
| 8 | /* |
| 9 | #cgo linux pkg-config: gtk+-3.0 |
| 10 | #cgo !webkit2_41 pkg-config: webkit2gtk-4.0 |
| 11 | #cgo webkit2_41 pkg-config: webkit2gtk-4.1 |
| 12 | |
| 13 | #include <errno.h> |
| 14 | #include <signal.h> |
| 15 | #include <stdio.h> |
| 16 | #include <string.h> |
| 17 | |
| 18 | #include <glib.h> |
| 19 | |
| 20 | static void reasonix_fix_signal(int signum) |
| 21 | { |
| 22 | struct sigaction st; |
| 23 | |
| 24 | if (sigaction(signum, NULL, &st) < 0) { |
| 25 | goto fix_signal_error; |
| 26 | } |
| 27 | st.sa_flags |= SA_ONSTACK; |
| 28 | if (sigaction(signum, &st, NULL) < 0) { |
| 29 | goto fix_signal_error; |
| 30 | } |
| 31 | return; |
| 32 | |
| 33 | fix_signal_error: |
| 34 | fprintf(stderr, "reasonix: error fixing handler for signal %d: %s\n", |
| 35 | signum, strerror(errno)); |
| 36 | } |
| 37 | |
| 38 | static void reasonix_install_signal_handlers(void) |
| 39 | { |
| 40 | #if defined(SIGCHLD) |
| 41 | reasonix_fix_signal(SIGCHLD); |
| 42 | #endif |
| 43 | #if defined(SIGHUP) |
| 44 | reasonix_fix_signal(SIGHUP); |
| 45 | #endif |
| 46 | #if defined(SIGINT) |
| 47 | reasonix_fix_signal(SIGINT); |
| 48 | #endif |
| 49 | #if defined(SIGQUIT) |
| 50 | reasonix_fix_signal(SIGQUIT); |
| 51 | #endif |
| 52 | #if defined(SIGABRT) |
| 53 | reasonix_fix_signal(SIGABRT); |
| 54 | #endif |
| 55 | #if defined(SIGFPE) |
| 56 | reasonix_fix_signal(SIGFPE); |
| 57 | #endif |
| 58 | #if defined(SIGTERM) |
| 59 | reasonix_fix_signal(SIGTERM); |
| 60 | #endif |
| 61 | #if defined(SIGBUS) |
| 62 | reasonix_fix_signal(SIGBUS); |
| 63 | #endif |
| 64 | #if defined(SIGSEGV) |
| 65 | reasonix_fix_signal(SIGSEGV); |
| 66 | #endif |
| 67 | // Do not modify SIGUSR1. JavaScriptCore owns it for conservative GC |
| 68 | // stack scanning after installing its handler. |
| 69 | #if defined(SIGXCPU) |
| 70 | reasonix_fix_signal(SIGXCPU); |
| 71 | #endif |
| 72 | #if defined(SIGXFSZ) |
| 73 | reasonix_fix_signal(SIGXFSZ); |
| 74 | #endif |
| 75 | } |
| 76 | |
| 77 | static gboolean reasonix_install_signal_handlers_timeout(gpointer data) |
| 78 | { |
| 79 | reasonix_install_signal_handlers(); |
| 80 | int *remaining = (int *)data; |
| 81 | (*remaining)--; |
| 82 | return *remaining > 0 ? G_SOURCE_CONTINUE : G_SOURCE_REMOVE; |
| 83 | } |
| 84 | |
| 85 | static void reasonix_schedule_signal_handler_fix(void) |
| 86 | { |
| 87 | int *remaining = (int *)g_malloc(sizeof(int)); |
| 88 | *remaining = 100; |
| 89 | g_timeout_add_full( |
| 90 | G_PRIORITY_DEFAULT, |
| 91 | 50, |
| 92 | reasonix_install_signal_handlers_timeout, |
| 93 | remaining, |
| 94 | g_free |
| 95 | ); |
| 96 | } |
| 97 | */ |
| 98 | import "C" |
| 99 | |
| 100 | // JavaScriptCore installs several signal handlers lazily when JavaScript first |
| 101 | // executes. Those handlers can replace the SA_ONSTACK flag required by Go after |
| 102 | // Wails v2.12's one-shot repair has already run. The bounded GLib timer below |
| 103 | // mirrors the Linux repair shipped by Wails v2.13: it restores SA_ONSTACK every |
| 104 | // 50 ms for the first five seconds of WebKit startup, then domReady performs one |
| 105 | // final deterministic repair. |
| 106 | // |
| 107 | // The timer starts dispatching only after Wails enters GTK's main loop. It fixes |
| 108 | // the verified JavaScriptCore signal-handler race, but is not intended to cover |
| 109 | // failures that occur earlier while WebKit constructs the window. |
| 110 | |
| 111 | // scheduleWebKitSignalHandlerRepair starts the bounded GLib timer immediately |
| 112 | // before wails.Run. The callback begins executing when Wails enters GTK's main |
| 113 | // loop, which anchors the five-second repair window to WebKit startup instead |
| 114 | // of package initialization. |
| 115 | func scheduleWebKitSignalHandlerRepair() { |
| 116 | C.reasonix_schedule_signal_handler_fix() |
| 117 | } |
| 118 | |
| 119 | // repairWebKitSignalHandlers runs once after the DOM is ready, when JSC has |
| 120 | // installed its lazy signal handlers. |
| 121 | func repairWebKitSignalHandlers() { |
| 122 | C.reasonix_install_signal_handlers() |
| 123 | } |
| 124 |