Skip to contentSkip to conversation

Improve signals and avoid losing high priority ones

PATCH In review [email protected]... 5 participants 23 messages Jan 1, 2023 – Jan 23, 2023

lore
Share & tools
lore
#0 lore raw

cover Improve signals and avoid losing high priority ones

From: Selva Nair <[email protected]...>

Back to my long-forgotten signal patch -- cleaned up and rebased. What better to do on a Jan 1st afternoon...

Commits:

1.  Preparing for better signal handling: some code refactoring
2.  Refactor signal handling in openvpn_getaddrinfo
3.  Assign and honour signal priority order
4.  Fix signal handling on Windows
5.  Improve signal handling using POSIX sigaction

Commits 1 to 4 are fairly non-intrusive and should fix trac #639 and #311 (or github #205) for most practical purposes.

Commit 5 rewrites sig.c using sigaction for proper masking of signals during updates. May be too intrusive and too late to get well-tested for 2.6.

#1 lore raw

PATCH 1/5 Preparing for better signal handling: some code refactoring

From: Selva Nair <[email protected]...>

- Do not directly update signal_received: always use register_signal() throw_signal() or signal_reset().
To facilitate this, register_signal() now takes c->sig as an argument instead of the context c itself, and sig_info struct is passed-in to functions that need to set a signal.

- openvpn_getaddrinfo() is updated in a following commit as it could benefit from some logic changes that we may or may not want to do.

No functional changes.

TODO:
(i) update signal handling in openvpn_getaddrinfo
(ii) enforce signal priority
(iii) fix signal handling on Windows
for 2.7?
(iv) replace system-V signal with POSIX sigaction

---
 src/openvpn/dco_win.c | 11 +++++----
 src/openvpn/dco_win.h |  3 ++-
 src/openvpn/forward.c | 34 +++++++++++++--------------
 src/openvpn/init.c    | 13 ++++-------
 src/openvpn/multi.c   |  6 ++---
 src/openvpn/occ.c     |  3 +--
 src/openvpn/ping.c    |  6 ++---
 src/openvpn/proxy.c   |  7 +++---
 src/openvpn/proxy.h   |  2 +-
 src/openvpn/push.c    | 33 +++++++++++++-------------
 src/openvpn/sig.c     | 17 +++++++-------
 src/openvpn/sig.h     | 37 +++++++++++++++--------------
 src/openvpn/socket.c  | 54 +++++++++++++++++++++----------------------
 src/openvpn/socks.c   | 22 ++++++++++--------
 src/openvpn/socks.h   |  4 ++--
 src/openvpn/win32.c   |  3 +--
 16 files changed, 125 insertions(+), 130 deletions(-)
src/openvpn/dco_win.c +6 -5
index 0d0d7946..825b1cd3 100644
@@ -106,8 +106,9 @@ dco_start_tun(struct tuntap *tt)
 }
 
 static void
-dco_connect_wait(HANDLE handle, OVERLAPPED *ov, int timeout, volatile int *signal_received)
+dco_connect_wait(HANDLE handle, OVERLAPPED *ov, int timeout, struct signal_info *sig_info)
 {
+    volatile int *signal_received = &sig_info->signal_received;
     /* GetOverlappedResultEx is available starting from Windows 8 */
     typedef BOOL (*get_overlapped_result_ex_t) (HANDLE, LPOVERLAPPED, LPDWORD, DWORD, BOOL);
     get_overlapped_result_ex_t get_overlapped_result_ex =
@@ -138,7 +139,7 @@ dco_connect_wait(HANDLE handle, OVERLAPPED *ov, int timeout, volatile int *signa
         {
             /* dco reported connection error */
             msg(M_NONFATAL | M_ERRNO, "dco connect error");
-            *signal_received = SIGUSR1;
+            register_signal(sig_info, SIGUSR1, "dco-connect-error");
             return;
         }
 
@@ -153,13 +154,13 @@ dco_connect_wait(HANDLE handle, OVERLAPPED *ov, int timeout, volatile int *signa
 
     /* we end up here when timeout occurs in userspace */
     msg(M_NONFATAL, "dco connect timeout");
-    *signal_received = SIGUSR1;
+    register_signal(sig_info, SIGUSR1, "dco-connect-timeout");
 }
 
 void
 dco_create_socket(HANDLE handle, struct addrinfo *remoteaddr, bool bind_local,
                   struct addrinfo *bind, int timeout,
-                  volatile int *signal_received)
+                  struct signal_info *sig_info)
 {
     msg(D_DCO_DEBUG, "%s", __func__);
 
@@ -240,7 +241,7 @@ dco_create_socket(HANDLE handle, struct addrinfo *remoteaddr, bool bind_local,
         }
         else
         {
-            dco_connect_wait(handle, &ov, timeout, signal_received);
+            dco_connect_wait(handle, &ov, timeout, sig_info);
         }
     }
 }
src/openvpn/dco_win.h +2 -1
index b3cdbbbd..bba7b340 100644
@@ -26,6 +26,7 @@
 
 #include "buffer.h"
 #include "ovpn_dco_win.h"
+#include "sig.h"
 
 typedef OVPN_KEY_SLOT dco_key_slot_t;
 typedef OVPN_CIPHER_ALG dco_cipher_t;
@@ -42,7 +43,7 @@ create_dco_handle(const char *devname, struct gc_arena *gc);
 void
 dco_create_socket(HANDLE handle, struct addrinfo *remoteaddr, bool bind_local,
                   struct addrinfo *bind, int timeout,
-                  volatile int *signal_received);
+                  struct signal_info *sig_info);
 
 void
 dco_start_tun(struct tuntap *tt);
src/openvpn/forward.c +17 -17
index af4ed05d..ae0512fc 100644
@@ -82,13 +82,13 @@ static void
 check_tls_errors_co(struct context *c)
 {
     msg(D_STREAM_ERRORS, "Fatal TLS error (check_tls_errors_co), restarting");
-    register_signal(c, c->c2.tls_exit_signal, "tls-error"); /* SOFT-SIGUSR1 -- TLS error */
+    register_signal(c->sig, c->c2.tls_exit_signal, "tls-error"); /* SOFT-SIGUSR1 -- TLS error */
 }
 
 static void
 check_tls_errors_nco(struct context *c)
 {
-    register_signal(c, c->c2.tls_exit_signal, "tls-error"); /* SOFT-SIGUSR1 -- TLS error */
+    register_signal(c->sig, c->c2.tls_exit_signal, "tls-error"); /* SOFT-SIGUSR1 -- TLS error */
 }
 
 /*
@@ -155,7 +155,7 @@ check_dco_key_status(struct context *c)
     {
         /* Something bad happened. Kill the connection to
          * be able to recover. */
-        register_signal(c, SIGUSR1, "dco update keys error");
+        register_signal(c->sig, SIGUSR1, "dco update keys error");
     }
 }
 
@@ -199,7 +199,7 @@ check_tls(struct context *c)
             }
             else
             {
-                register_signal(c, SIGTERM, "auth-control-exit");
+                register_signal(c->sig, SIGTERM, "auth-control-exit");
             }
         }
 
@@ -351,7 +351,7 @@ check_connection_established(struct context *c)
         {
             if (!do_up(c, false, 0))
             {
-                register_signal(c, SIGUSR1, "connection initialisation failed");
+                register_signal(c->sig, SIGUSR1, "connection initialisation failed");
             }
         }
 
@@ -431,7 +431,7 @@ check_add_routes(struct context *c)
         {
             if (!tun_standby(c->c1.tuntap))
             {
-                register_signal(c, SIGHUP, "ip-fail");
+                register_signal(c->sig, SIGHUP, "ip-fail");
                 c->persist.restart_sleep_seconds = 10;
 #ifdef _WIN32
                 show_routes(M_INFO|M_NOPREFIX);
@@ -455,7 +455,7 @@ static void
 check_inactivity_timeout(struct context *c)
 {
     msg(M_INFO, "Inactivity timeout (--inactive), exiting");
-    register_signal(c, SIGTERM, "inactive");
+    register_signal(c->sig, SIGTERM, "inactive");
 }
 
 int
@@ -474,7 +474,7 @@ check_server_poll_timeout(struct context *c)
     if (!tls_initial_packet_received(c->c2.tls_multi))
     {
         msg(M_INFO, "Server poll timeout, restarting");
-        register_signal(c, SIGUSR1, "server_poll");
+        register_signal(c->sig, SIGUSR1, "server_poll");
         c->persist.restart_sleep_seconds = -1;
     }
 }
@@ -499,7 +499,7 @@ schedule_exit(struct context *c, const int n_seconds, const int signal)
 static void
 check_scheduled_exit(struct context *c)
 {
-    register_signal(c, c->c2.scheduled_exit_signal, "delayed-exit");
+    register_signal(c->sig, c->c2.scheduled_exit_signal, "delayed-exit");
 }
 
 /*
@@ -661,7 +661,7 @@ check_session_timeout(struct context *c)
                                  ETT_DEFAULT))
     {
         msg(M_INFO, "Session timeout, exiting");
-        register_signal(c, SIGTERM, "session-timeout");
+        register_signal(c->sig, SIGTERM, "session-timeout");
     }
 }
 
@@ -902,7 +902,7 @@ read_incoming_link(struct context *c)
             const struct buffer *fbuf = socket_foreign_protocol_head(c->c2.link_socket);
             const int sd = socket_foreign_protocol_sd(c->c2.link_socket);
             port_share_redirect(port_share, fbuf, sd);
-            register_signal(c, SIGTERM, "port-share-redirect");
+            register_signal(c->sig, SIGTERM, "port-share-redirect");
         }
         else
 #endif
@@ -915,7 +915,7 @@ read_incoming_link(struct context *c)
             }
             else
             {
-                register_signal(c, SIGUSR1, "connection-reset"); /* SOFT-SIGUSR1 -- TCP connection reset */
+                register_signal(c->sig, SIGUSR1, "connection-reset"); /* SOFT-SIGUSR1 -- TCP connection reset */
                 msg(D_STREAM_ERRORS, "Connection reset, restarting [%d]", status);
             }
         }
@@ -1067,7 +1067,7 @@ process_incoming_link_part1(struct context *c, struct link_socket_info *lsi, boo
         if (!decrypt_status && link_socket_connection_oriented(c->c2.link_socket))
         {
             /* decryption errors are fatal in TCP mode */
-            register_signal(c, SIGUSR1, "decryption-error"); /* SOFT-SIGUSR1 -- decryption error in TCP mode */
+            register_signal(c->sig, SIGUSR1, "decryption-error"); /* SOFT-SIGUSR1 -- decryption error in TCP mode */
             msg(D_STREAM_ERRORS, "Fatal decryption error (process_incoming_link), restarting");
         }
     }
@@ -1248,7 +1248,7 @@ read_incoming_tun(struct context *c)
         read_wintun(c->c1.tuntap, &c->c2.buf);
         if (c->c2.buf.len == -1)
         {
-            register_signal(c, SIGHUP, "tun-abort");
+            register_signal(c->sig, SIGHUP, "tun-abort");
             c->persist.restart_sleep_seconds = 1;
             msg(M_INFO, "Wintun read error, restarting");
             perf_pop();
@@ -1277,7 +1277,7 @@ read_incoming_tun(struct context *c)
     /* Was TUN/TAP interface stopped? */
     if (tuntap_stop(c->c2.buf.len))
     {
-        register_signal(c, SIGTERM, "tun-stop");
+        register_signal(c->sig, SIGTERM, "tun-stop");
         msg(M_INFO, "TUN/TAP interface has been stopped, exiting");
         perf_pop();
         return;
@@ -1286,7 +1286,7 @@ read_incoming_tun(struct context *c)
     /* Was TUN/TAP I/O operation aborted? */
     if (tuntap_abort(c->c2.buf.len))
     {
-        register_signal(c, SIGHUP, "tun-abort");
+        register_signal(c->sig, SIGHUP, "tun-abort");
         c->persist.restart_sleep_seconds = 10;
         msg(M_INFO, "TUN/TAP I/O operation aborted, restarting");
         perf_pop();
@@ -1845,7 +1845,7 @@ process_outgoing_link(struct context *c)
             && !tls_initial_packet_received(c->c2.tls_multi) && c->options.mode == MODE_POINT_TO_POINT)
         {
             msg(M_INFO, "Network unreachable, restarting");
-            register_signal(c, SIGUSR1, "network-unreachable");
+            register_signal(c->sig, SIGUSR1, "network-unreachable");
         }
     }
     else
src/openvpn/init.c +5 -8
index 3380ed9e..eec25acf 100644
@@ -2934,13 +2934,13 @@ do_init_crypto_tls_c1(struct context *c)
                 /* Intentional [[fallthrough]]; */
 
                 case AR_NOINTERACT:
-                    c->sig->signal_received = SIGUSR1; /* SOFT-SIGUSR1 -- Password failure error */
+                    /* SOFT-SIGUSR1 -- Password failure error */
+                    register_signal(c->sig, SIGUSR1, "private-key-password-failure");
                     break;
 
                 default:
                     ASSERT(0);
             }
-            c->sig->signal_text = "private-key-password-failure";
             return;
         }
 
@@ -4229,9 +4229,7 @@ init_instance(struct context *c, const struct env_set *env, const unsigned int f
     }
 
     /* signals caught here will abort */
-    c->sig->signal_received = 0;
-    c->sig->signal_text = NULL;
-    c->sig->source = SIG_SOURCE_SOFT;
+    signal_reset(c->sig);
 
     if (c->mode == CM_P2P)
     {
@@ -4733,7 +4731,7 @@ close_context(struct context *c, int sig, unsigned int flags)
 
     if (sig >= 0)
     {
-        c->sig->signal_received = sig;
+        register_signal(c->sig, sig, "close_context");
     }
 
     if (c->sig->signal_received == SIGUSR1)
@@ -4741,8 +4739,7 @@ close_context(struct context *c, int sig, unsigned int flags)
         if ((flags & CC_USR1_TO_HUP)
             || (c->sig->source == SIG_SOURCE_HARD && (flags & CC_HARD_USR1_TO_HUP)))
         {
-            c->sig->signal_received = SIGHUP;
-            c->sig->signal_text = "close_context usr1 to hup";
+            register_signal(c->sig, SIGHUP, "close_context usr1 to hup");
         }
     }
 
src/openvpn/multi.c +3 -3
index 92e63dd2..c2254399 100644
@@ -2364,7 +2364,7 @@ multi_client_generate_tls_keys(struct context *c)
                                           get_link_socket_info(c)))
     {
         msg(D_TLS_ERRORS, "TLS Error: initializing data channel failed");
-        register_signal(c, SIGUSR1, "process-push-msg-failed");
+        register_signal(c->sig, SIGUSR1, "process-push-msg-failed");
         return false;
     }
 
@@ -3828,7 +3828,7 @@ multi_push_restart_schedule_exit(struct multi_context *m, bool next_server)
                        &m->deferred_shutdown_signal.wakeup,
                        compute_wakeup_sigma(&m->deferred_shutdown_signal.wakeup));
 
-    m->top.sig->signal_received = 0;
+    signal_reset(m->top.sig);
 }
 
 /*
@@ -3843,7 +3843,7 @@ multi_process_signal(struct multi_context *m)
         struct status_output *so = status_open(NULL, 0, M_INFO, NULL, 0);
         multi_print_status(m, so, m->status_file_version);
         status_close(so);
-        m->top.sig->signal_received = 0;
+        signal_reset(m->top.sig);
         return false;
     }
     else if (proto_is_dgram(m->top.options.ce.proto)
src/openvpn/occ.c +1 -2
index eb1f2fae..0b291756 100644
@@ -431,8 +431,7 @@ process_received_occ_msg(struct context *c)
 
         case OCC_EXIT:
             dmsg(D_PACKET_CONTENT, "RECEIVED OCC_EXIT");
-            c->sig->signal_received = SIGUSR1;
-            c->sig->signal_text = "remote-exit";
+            register_signal(c->sig, SIGUSR1, "remote-exit");
             break;
     }
     c->c2.buf.len = 0; /* don't pass packet on */
src/openvpn/ping.c +2 -4
index 588723d0..cf1861a6 100644
@@ -55,15 +55,13 @@ trigger_ping_timeout_signal(struct context *c)
         case PING_EXIT:
             msg(M_INFO, "%sInactivity timeout (--ping-exit), exiting",
                 format_common_name(c, &gc));
-            c->sig->signal_received = SIGTERM;
-            c->sig->signal_text = "ping-exit";
+            register_signal(c->sig, SIGTERM, "ping-exit");
             break;
 
         case PING_RESTART:
             msg(M_INFO, "%sInactivity timeout (--ping-restart), restarting",
                 format_common_name(c, &gc));
-            c->sig->signal_received = SIGUSR1; /* SOFT-SIGUSR1 -- Ping Restart */
-            c->sig->signal_text = "ping-restart";
+            register_signal(c->sig, SIGUSR1, "ping-restart");
             break;
 
         default:
src/openvpn/proxy.c +4 -3
index 633caee0..91121f25 100644
@@ -636,7 +636,7 @@ establish_http_proxy_passthru(struct http_proxy_info *p,
                               const char *port,          /* openvpn server port */
                               struct event_timeout *server_poll_timeout,
                               struct buffer *lookahead,
-                              volatile int *signal_received)
+                              struct signal_info *sig_info)
 {
     struct gc_arena gc = gc_new();
     char buf[512];
@@ -646,6 +646,7 @@ establish_http_proxy_passthru(struct http_proxy_info *p,
     int nparms;
     bool ret = false;
     bool processed = false;
+    volatile int *signal_received = &sig_info->signal_received;
 
     /* get user/pass if not previously given */
     if (p->auth_method == HTTP_AUTH_BASIC
@@ -1079,9 +1080,9 @@ done:
     return ret;
 
 error:
-    if (!*signal_received)
+    if (!sig_info->signal_received)
     {
-        *signal_received = SIGUSR1; /* SOFT-SIGUSR1 -- HTTP proxy error */
+        register_signal(sig_info, SIGUSR1, "HTTP proxy error"); /* SOFT-SIGUSR1 -- HTTP proxy error */
     }
     gc_free(&gc);
     return ret;
src/openvpn/proxy.h +1 -1
index 9d719382..4fe2a08f 100644
@@ -86,7 +86,7 @@ bool establish_http_proxy_passthru(struct http_proxy_info *p,
                                    const char *port,          /* openvpn server port */
                                    struct event_timeout *server_poll_timeout,
                                    struct buffer *lookahead,
-                                   volatile int *signal_received);
+                                   struct signal_info *sig_info);
 
 uint8_t *make_base64_string2(const uint8_t *str, int str_len, struct gc_arena *gc);
 
src/openvpn/push.c +16 -17
index e765d2a9..9796da4e 100644
@@ -74,8 +74,7 @@ receive_auth_failed(struct context *c, const struct buffer *buffer)
     if (authfail_extended && buf_string_match_head_str(&buf, "TEMP"))
     {
         parse_auth_failed_temp(&c->options, reason + strlen("TEMP"));
-        c->sig->signal_received = SIGUSR1;
-        c->sig->signal_text = "auth-temp-failure (server temporary reject)";
+        register_signal(c->sig, SIGUSR1, "auth-temp-failure (server temporary reject)");
     }
 
     /* Before checking how to react on AUTH_FAILED, first check if the
@@ -85,8 +84,8 @@ receive_auth_failed(struct context *c, const struct buffer *buffer)
      * identical for this scenario */
     else if (ssl_clean_auth_token())
     {
-        c->sig->signal_received = SIGUSR1;     /* SOFT-SIGUSR1 -- Auth failure error */
-        c->sig->signal_text = "auth-failure (auth-token)";
+        /* SOFT-SIGUSR1 -- Auth failure error */
+        register_signal(c->sig, SIGUSR1, "auth-failure (auth-token)");
         c->options.no_advance = true;
     }
     else
@@ -94,20 +93,21 @@ receive_auth_failed(struct context *c, const struct buffer *buffer)
         switch (auth_retry_get())
         {
             case AR_NONE:
-                c->sig->signal_received = SIGTERM;     /* SOFT-SIGTERM -- Auth failure error */
+                /* SOFT-SIGTERM -- Auth failure error */
+                register_signal(c->sig, SIGTERM, "auth-failure");
                 break;
 
             case AR_INTERACT:
                 ssl_purge_auth(false);
 
             case AR_NOINTERACT:
-                c->sig->signal_received = SIGUSR1;     /* SOFT-SIGUSR1 -- Auth failure error */
+                /* SOFT-SIGTUSR1 -- Auth failure error */
+                register_signal(c->sig, SIGUSR1, "auth-failure");
                 break;
 
             default:
                 ASSERT(0);
         }
-        c->sig->signal_text = "auth-failure";
     }
 #ifdef ENABLE_MANAGEMENT
     if (management)
@@ -171,14 +171,14 @@ server_pushed_signal(struct context *c, const struct buffer *buffer, const bool
         if (restart)
         {
             msg(D_STREAM_ERRORS, "Connection reset command was pushed by server ('%s')", m);
-            c->sig->signal_received = SIGUSR1; /* SOFT-SIGUSR1 -- server-pushed connection reset */
-            c->sig->signal_text = "server-pushed-connection-reset";
+            /* SOFT-SIGUSR1 -- server-pushed connection reset */
+            register_signal(c->sig, SIGUSR1, "server-pushed-connection-reset");
         }
         else
         {
             msg(D_STREAM_ERRORS, "Halt command was pushed by server ('%s')", m);
-            c->sig->signal_received = SIGTERM; /* SOFT-SIGTERM -- server-pushed halt */
-            c->sig->signal_text = "server-pushed-halt";
+            /* SOFT-SIGTERM -- server-pushed halt */
+            register_signal(c->sig, SIGTERM, "server-pushed-halt");
         }
 #ifdef ENABLE_MANAGEMENT
         if (management)
@@ -210,13 +210,12 @@ receive_exit_message(struct context *c)
     }
     else
     {
-        c->sig->signal_received = SIGUSR1;
+        register_signal(c->sig, SIGUSR1, "remote-exit");
     }
-    c->sig->signal_text = "remote-exit";
 #ifdef ENABLE_MANAGEMENT
     if (management)
     {
-        management_notify(management, "info", c->sig->signal_text, "EXIT");
+        management_notify(management, "info", "remote-exit", "EXIT");
     }
 #endif
 }
@@ -527,7 +526,7 @@ incoming_push_message(struct context *c, const struct buffer *buffer)
     goto cleanup;
 
 error:
-    register_signal(c, SIGUSR1, "process-push-msg-failed");
+    register_signal(c->sig, SIGUSR1, "process-push-msg-failed");
 cleanup:
     gc_free(&gc);
 }
@@ -555,8 +554,8 @@ send_push_request(struct context *c)
     {
         msg(D_STREAM_ERRORS, "No reply from server to push requests in %ds",
             (int)(now - ks->established));
-        c->sig->signal_received = SIGUSR1; /* SOFT-SIGUSR1 -- server-pushed connection reset */
-        c->sig->signal_text = "no-push-reply";
+        /* SOFT-SIGUSR1 -- server-pushed connection reset */
+        register_signal(c->sig, SIGUSR1, "no-push-reply");
         return false;
     }
 }
src/openvpn/sig.c +8 -9
index 65cd25c6..e462b93e 100644
@@ -115,7 +115,7 @@ throw_signal_soft(const int signum, const char *signal_text)
     siginfo_static.signal_text = signal_text;
 }
 
-static void
+void
 signal_reset(struct signal_info *si)
 {
     if (si)
@@ -374,8 +374,7 @@ process_explicit_exit_notification_timer_wakeup(struct context *c)
         if (now >= c->c2.explicit_exit_notification_time_wait + c->options.ce.explicit_exit_notification)
         {
             event_timeout_clear(&c->c2.explicit_exit_notification_interval);
-            c->sig->signal_received = SIGTERM;
-            c->sig->signal_text = "exit-with-notification";
+            register_signal(c->sig, SIGTERM, "exit-with-notification");
         }
         else if (!cc_exit_notify_enabled(c))
         {
@@ -393,7 +392,7 @@ remap_signal(struct context *c)
 {
     if (c->sig->signal_received == SIGUSR1 && c->options.remap_sigusr1)
     {
-        c->sig->signal_received = c->options.remap_sigusr1;
+        register_signal(c->sig, c->options.remap_sigusr1, c->sig->signal_text);
     }
 }
 
@@ -442,7 +441,7 @@ ignore_restart_signals(struct context *c)
         {
             msg(M_INFO, "Converting soft %s received during exit notification to SIGTERM",
                 signal_name(c->sig->signal_received, true));
-            register_signal(c, SIGTERM, "exit-with-notification");
+            register_signal(c->sig, SIGTERM, "exit-with-notification");
             ret = false;
         }
     }
@@ -471,11 +470,11 @@ process_signal(struct context *c)
 }
 
 void
-register_signal(struct context *c, int sig, const char *text)
+register_signal(struct signal_info *si, int sig, const char *text)
 {
-    if (c->sig->signal_received != SIGTERM)
+    if (si->signal_received != SIGTERM)
     {
-        c->sig->signal_received = sig;
+        si->signal_received = sig;
     }
-    c->sig->signal_text = text;
+    si->signal_text = text;
 }
src/openvpn/sig.h +18 -19
index 091f16b3..83adc543 100644
@@ -27,8 +27,6 @@
 #include "status.h"
 #include "win32.h"
 
-
-
 #define SIG_SOURCE_SOFT 0
 #define SIG_SOURCE_HARD 1
 /* CONNECTION_FAILED is also a "soft" status,
@@ -79,41 +77,42 @@ void signal_restart_status(const struct signal_info *si);
 
 bool process_signal(struct context *c);
 
-void register_signal(struct context *c, int sig, const char *text);
+void register_signal(struct signal_info *si, int sig, const char *text);
 
 void process_explicit_exit_notification_timer_wakeup(struct context *c);
 
-#ifdef _WIN32
-
-static inline void
-get_signal(volatile int *sig)
-{
-    *sig = win32_signal_get(&win32_signal);
-}
+void signal_reset(struct signal_info *si);
 
 static inline void
 halt_non_edge_triggered_signals(void)
 {
+#ifdef _WIN32
     win32_signal_close(&win32_signal);
+#endif
 }
 
-#else  /* ifdef _WIN32 */
+/**
+ * Copy the global signal_received (if non-zero) to the passed-in argument sig.
+ * As the former is volatile, do not assign if sig and &signal_received are the
+ * same.  Even on windows signal_received is really volatile as it can change if
+ * a ctrl-C or ctrl-break is delivered. So use the same logic as above.
+ *
+ * Also, on windows always call win32_signal_get to pickup any signals simulated by
+ * key-board short cuts or the exit event.
+ */
 
 static inline void
 get_signal(volatile int *sig)
 {
+#ifdef _WIN32
+    const int i = win32_signal_get(&win32_signal);
+#else
     const int i = siginfo_static.signal_received;
-    if (i)
+#endif
+    if (i && sig != &siginfo_static.signal_received)
     {
         *sig = i;
     }
 }
 
-static inline void
-halt_non_edge_triggered_signals(void)
-{
-}
-
-#endif /* ifdef _WIN32 */
-
 #endif /* ifndef SIG_H */
src/openvpn/socket.c +27 -27

175 lines not on this page · raw message

src/openvpn/socks.c +12 -10
index ef178a35..b2ca3744 100644
@@ -448,12 +448,12 @@ establish_socks_proxy_passthru(struct socks_proxy_info *p,
                                socket_descriptor_t sd,  /* already open to proxy */
                                const char *host,        /* openvpn server remote */
                                const char *servname,    /* openvpn server port */
-                               volatile int *signal_received)
+                               struct signal_info *sig_info)
 {
     char buf[270];
     size_t len;
 
-    if (!socks_handshake(p, sd, signal_received))
+    if (!socks_handshake(p, sd, &sig_info->signal_received))
     {
         goto error;
     }
@@ -491,7 +491,7 @@ establish_socks_proxy_passthru(struct socks_proxy_info *p,
 
 
     /* receive reply from Socks proxy and discard */
-    if (!recv_socks_reply(sd, NULL, signal_received))
+    if (!recv_socks_reply(sd, NULL, &sig_info->signal_received))
     {
         goto error;
     }
@@ -499,9 +499,10 @@ establish_socks_proxy_passthru(struct socks_proxy_info *p,
     return;
 
 error:
-    if (!*signal_received)
+    if (!sig_info->signal_received)
     {
-        *signal_received = SIGUSR1; /* SOFT-SIGUSR1 -- socks error */
+        /* SOFT-SIGUSR1 -- socks error */
+        register_signal(sig_info, SIGUSR1, "socks-error");
     }
     return;
 }
@@ -511,9 +512,9 @@ establish_socks_proxy_udpassoc(struct socks_proxy_info *p,
                                socket_descriptor_t ctrl_sd,  /* already open to proxy */
                                socket_descriptor_t udp_sd,
                                struct openvpn_sockaddr *relay_addr,
-                               volatile int *signal_received)
+                               struct signal_info *sig_info)
 {
-    if (!socks_handshake(p, ctrl_sd, signal_received))
+    if (!socks_handshake(p, ctrl_sd, &sig_info->signal_received))
     {
         goto error;
     }
@@ -534,7 +535,7 @@ establish_socks_proxy_udpassoc(struct socks_proxy_info *p,
 
     /* receive reply from Socks proxy */
     CLEAR(*relay_addr);
-    if (!recv_socks_reply(ctrl_sd, relay_addr, signal_received))
+    if (!recv_socks_reply(ctrl_sd, relay_addr, &sig_info->signal_received))
     {
         goto error;
     }
@@ -542,9 +543,10 @@ establish_socks_proxy_udpassoc(struct socks_proxy_info *p,
     return;
 
 error:
-    if (!*signal_received)
+    if (!sig_info->signal_received)
     {
-        *signal_received = SIGUSR1; /* SOFT-SIGUSR1 -- socks error */
+        /* SOFT-SIGUSR1 -- socks error */
+        register_signal(sig_info, SIGUSR1, "socks-error");
     }
     return;
 }
src/openvpn/socks.h +2 -2
index 47cdac10..55c75c60 100644
@@ -52,13 +52,13 @@ void establish_socks_proxy_passthru(struct socks_proxy_info *p,
                                     socket_descriptor_t sd,  /* already open to proxy */
                                     const char *host,        /* openvpn server remote */
                                     const char *servname,          /* openvpn server port */
-                                    volatile int *signal_received);
+                                    struct signal_info *sig_info);
 
 void establish_socks_proxy_udpassoc(struct socks_proxy_info *p,
                                     socket_descriptor_t ctrl_sd,  /* already open to proxy */
                                     socket_descriptor_t udp_sd,
                                     struct openvpn_sockaddr *relay_addr,
-                                    volatile int *signal_received);
+                                    struct signal_info *sig_info);
 
 void socks_process_incoming_udp(struct buffer *buf,
                                 struct link_socket_actual *from);
src/openvpn/win32.c +1 -2
index cfe4dbde..c3520bca 100644
@@ -682,8 +682,7 @@ win32_signal_get(struct win32_signal *ws)
         }
         if (ret)
         {
-            siginfo_static.signal_received = ret;
-            siginfo_static.source = SIG_SOURCE_HARD;
+            throw_signal(ret); /* this will update signinfo_static.signal received */
         }
     }
     return ret;
Signature · 1 line
2.34.1
Acked-by on v1 1/5
#2 lore raw

[Openvpn-devel] [PATCH applied] Re: Preparing for better signal handling: some code refactoring

(Lightly) tested on Linux, FreeBSD - namely, does it compile, pass t_client tests (which send SIGTERM), pass t_server tests (which uses per-instance SIGUSR1/SIGTERM). Plus "push to github, does it break windows?".

Stared at the code for a long time - most of it is really straightforward replacement, but the "easy" ones tend to create oversights. I'm not sure I fully understand the nuances why we went from "c" to "c->sig", but I assume this is due to "definition of 'c' not available in all the places that need the new code". But even if the latter could be made possible (opaque struct pointer etc), having "just the signal stuff" passed to register_signal() sounds more clean anyway.

The continued existance of "volatile int *signal_received" in dco_connect_wait() confused me at first, but is needed for get_signal() (and that is needed for "do not set signal if there already is one", which should be covered much more nicely by 4/5... not there yet).

Generally speaking, using signal_reset() and register_signal() everywhere is much nicer, especially (old) code like this

-            c->sig->signal_received = SIGUSR1;
-            c->sig->signal_text = "remote-exit";
+            register_signal(c->sig, SIGUSR1, "remote-exit");

.. should have never been accepted in the first place...

The /* */ comment here is arguably wrong, and was wrong before...

@@ -555,8 +554,8 @@ send_push_request(struct context *c)
..
msg(D_STREAM_ERRORS, "No reply from server to push requests in %ds",
..
+ /* SOFT-SIGUSR1 -- server-pushed connection reset */
+ register_signal(c->sig, SIGUSR1, "no-push-reply");

I've left this on the list for a few days so people had the chance to stand up and complain "this is all the wrong way to do it" - nobody came up, so I consider this as "no objection".

Your patch has been applied to the master and release/2.6 branch.

commit 05715485b45816e18b52ffb9b47ca22a55abb334 (master) commit 264ce74c409018f42b178ba2cab544bdcecb1767 (release/2.6)

Author: Selva Nair
Date:   Sun Jan 1 16:51:05 2023 -0500
     Preparing for better signal handling: some code refactoring

     Signed-off-by: Selva Nair <[email protected]...>
     Acked-by: Gert Doering <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg25874.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#3 lore raw

PATCH 2/5 Refactor signal handling in openvpn_getaddrinfo

In reply to [email protected]...

From: Selva Nair <[email protected]...>

Pass in sig_info struct to use register signal instead of modifying signal_received.

No functional changes though some may be warranted. Questions:
- Why are we overwriting SIGUSR1 in this function?
- Why the special interrupted syscall treatment for getaddrinfo? Its not a syscall, is it?

---
 src/openvpn/socket.c | 31 ++++++++++++++++---------------
 src/openvpn/socket.h |  4 ++--
 2 files changed, 18 insertions(+), 17 deletions(-)
src/openvpn/socket.c +16 -15
index 273f378e..faaa2748 100644
@@ -67,7 +67,7 @@ sf2gaf(const unsigned int getaddr_flags,
 static int
 get_addr_generic(sa_family_t af, unsigned int flags, const char *hostname,
                  void *network, unsigned int *netbits,
-                 int resolve_retry_seconds, volatile int *signal_received,
+                 int resolve_retry_seconds, struct signal_info *sig_info,
                  int msglevel)
 {
     char *endp, *sep, *var_host = NULL;
@@ -130,7 +130,7 @@ get_addr_generic(sa_family_t af, unsigned int flags, const char *hostname,
     }
 
     ret = openvpn_getaddrinfo(flags & ~GETADDR_HOST_ORDER, var_host, NULL,
-                              resolve_retry_seconds, signal_received, af, &ai);
+                              resolve_retry_seconds, sig_info, af, &ai);
     if ((ret == 0) && network)
     {
         struct in6_addr *ip6;
@@ -183,13 +183,13 @@ getaddr(unsigned int flags,
         const char *hostname,
         int resolve_retry_seconds,
         bool *succeeded,
-        volatile int *signal_received)
+        struct signal_info *sig_info)
 {
     in_addr_t addr;
     int status;
 
     status = get_addr_generic(AF_INET, flags, hostname, &addr, NULL,
-                              resolve_retry_seconds, signal_received,
+                              resolve_retry_seconds, sig_info,
                               M_WARN);
     if (status==0)
     {
@@ -432,13 +432,13 @@ openvpn_getaddrinfo(unsigned int flags,
                     const char *hostname,
                     const char *servname,
                     int resolve_retry_seconds,
-                    volatile int *signal_received,
+                    struct signal_info *sig_info,
                     int ai_family,
                     struct addrinfo **res)
 {
     struct addrinfo hints;
     int status;
-    int sigrec = 0;
+    struct signal_info sigrec = {0};
     int msglevel = (flags & GETADDR_FATAL) ? M_FATAL : D_RESOLVE_ERRORS;
     struct gc_arena gc = gc_new();
     const char *print_hostname;
@@ -464,9 +464,9 @@ openvpn_getaddrinfo(unsigned int flags,
     }
 
     if ((flags & (GETADDR_FATAL_ON_SIGNAL|GETADDR_WARN_ON_SIGNAL))
-        && !signal_received)
+        && !sig_info)
     {
-        signal_received = &sigrec;
+        sig_info = &sigrec;
     }
 
     /* try numeric ipv6 addr first */
@@ -561,17 +561,18 @@ openvpn_getaddrinfo(unsigned int flags,
                  flags, hints.ai_family, hints.ai_socktype);
             status = getaddrinfo(hostname, servname, &hints, res);
 
-            if (signal_received)
+            if (sig_info)
             {
-                get_signal(signal_received);
-                if (*signal_received) /* were we interrupted by a signal? */
+                get_signal(&sig_info->signal_received);
+                if (sig_info->signal_received) /* were we interrupted by a signal? */
                 {
-                    if (*signal_received == SIGUSR1) /* ignore SIGUSR1 */
+                    /* why are we overwriting SIGUSR1 ? */
+                    if (sig_info->signal_received == SIGUSR1) /* ignore SIGUSR1 */
                     {
                         msg(level,
                             "RESOLVE: Ignored SIGUSR1 signal received during "
                             "DNS resolution attempt");
-                        *signal_received = 0;
+                        signal_reset(sig_info);
                     }
                     else
                     {
@@ -638,7 +639,7 @@ openvpn_getaddrinfo(unsigned int flags,
     }
 
 done:
-    if (signal_received && *signal_received)
+    if (sig_info && sig_info->signal_received)
     {
         int level = 0;
         if (flags & GETADDR_FATAL_ON_SIGNAL)
@@ -1759,7 +1760,7 @@ resolve_remote(struct link_socket *sock,
             if (status)
             {
                 status = openvpn_getaddrinfo(flags, sock->remote_host, sock->remote_port,
-                                             retry, signal_received, sock->info.af, &ai);
+                                             retry, sig_info, sock->info.af, &ai);
             }
 
             if (status == 0)
src/openvpn/socket.h +2 -2
index 05c31b10..92f1af77 100644
@@ -526,7 +526,7 @@ in_addr_t getaddr(unsigned int flags,
                   const char *hostname,
                   int resolve_retry_seconds,
                   bool *succeeded,
-                  volatile int *signal_received);
+                  struct signal_info *sig_info);
 
 /**
  * Translate an IPv6 addr or hostname from string form to in6_addr
@@ -538,7 +538,7 @@ int openvpn_getaddrinfo(unsigned int flags,
                         const char *hostname,
                         const char *servname,
                         int resolve_retry_seconds,
-                        volatile int *signal_received,
+                        struct signal_info *sig_info,
                         int ai_family,
                         struct addrinfo **res);
 
Signature · 1 line
2.34.1
Acked-by on v1 2/5
#4 lore raw

[Openvpn-devel] [PATCH applied] Re: Refactor signal handling in openvpn_getaddrinfo

This is basically the same thing as 1/5, just for the getaddrinfo() related functions. And no, I have no idea why they are treated "special", and getaddrinfo() is not a syscall. OTOH, getaddrinfo() spends most of its time in "waiting for DNS packets", which might result in visible behaviour similar to a restarted/restartable syscall on signals coming in... the "let's rewrite that thing" part would be 2.7 material, though.

The code changes look reasonable ("straightforward as 1/5"), the test rigs still works I do not have specific "test signal handling / timeout in DNS" test cases today, though.

Your patch has been applied to the master and release/2.6 branch.

commit eff95d500481c7927c5a9edd6b5c0dfa056a0cbb (master) commit 1ff87267f819b512c34a202b9a023c76e7031af4 (release/2.6)

Author: Selva Nair
Date:   Sun Jan 1 16:51:06 2023 -0500
     Refactor signal handling in openvpn_getaddrinfo

     Signed-off-by: Selva Nair <[email protected]...>
     Acked-by: Gert Doering <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg25872.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#5 lore raw

PATCH 3/5 Assign and honour signal priority order

In reply to [email protected]...

From: Selva Nair <[email protected]...>

Signals are ordered as SIGUSR2, SIGUSR1, SIGHUP, SIGTERM, SIGINT in increasing priority. Lower priority signals are not allowed to overwrite higher ones.

This should fix Trac #311, #639 -- SIGTER/SIGINT lost during dns resolution. (except for the Windows-specific bug handled in next commit)

On sending SIGTERM during dns resolution, it still takes several seconds to terminate as the signal will get processed only after getaddrinfo times out twice (in phase1 and phase2 inits).

Github: fixes OpenVPN/openvpn#205
Note: one has to still wait for address resolution to time out as getaddrinfo() is no interruptible. But a single ctrl-C (and some patience) is enough.

---
 src/openvpn/proxy.c  |  5 +----
 src/openvpn/sig.c    | 45 ++++++++++++++++++++++++++++++++------------
 src/openvpn/socket.c |  8 ++++++--
 src/openvpn/socks.c  | 14 ++++----------
 4 files changed, 44 insertions(+), 28 deletions(-)
src/openvpn/proxy.c +1 -4
index 91121f25..120ba85e 100644
@@ -1080,10 +1080,7 @@ done:
     return ret;
 
 error:
-    if (!sig_info->signal_received)
-    {
-        register_signal(sig_info, SIGUSR1, "HTTP proxy error"); /* SOFT-SIGUSR1 -- HTTP proxy error */
-    }
+    register_signal(sig_info, SIGUSR1, "HTTP proxy error"); /* SOFT-SIGUSR1 -- HTTP proxy error */
     gc_free(&gc);
     return ret;
 }
src/openvpn/sig.c +33 -12

80 lines not on this page · raw message

src/openvpn/socket.c +6 -2
index faaa2748..59d89352 100644
@@ -2277,8 +2277,12 @@ link_socket_init_phase2(struct context *c)
 done:
     if (sig_save.signal_received)
     {
-        /* This can potentially lose a saved high priority signal -- to be fixed */
-        if (!sig_info->signal_received)
+        /* Always restore the saved signal -- register/throw_signal will handle priority */
+        if (sig_save.source == SIG_SOURCE_HARD && sig_info == &siginfo_static)
+        {
+            throw_signal(sig_save.signal_received);
+        }
+        else
         {
             register_signal(sig_info, sig_save.signal_received, sig_save.signal_text);
         }
src/openvpn/socks.c +4 -10
index b2ca3744..8f2ae226 100644
@@ -499,11 +499,8 @@ establish_socks_proxy_passthru(struct socks_proxy_info *p,
     return;
 
 error:
-    if (!sig_info->signal_received)
-    {
-        /* SOFT-SIGUSR1 -- socks error */
-        register_signal(sig_info, SIGUSR1, "socks-error");
-    }
+    /* SOFT-SIGUSR1 -- socks error */
+    register_signal(sig_info, SIGUSR1, "socks-error");
     return;
 }
 
@@ -543,11 +540,8 @@ establish_socks_proxy_udpassoc(struct socks_proxy_info *p,
     return;
 
 error:
-    if (!sig_info->signal_received)
-    {
-        /* SOFT-SIGUSR1 -- socks error */
-        register_signal(sig_info, SIGUSR1, "socks-error");
-    }
+    /* SOFT-SIGUSR1 -- socks error */
+    register_signal(sig_info, SIGUSR1, "socks-error");
     return;
 }
 
Signature · 1 line
2.34.1
Acked-by on v1 3/5
#6 lore raw

Re: [Openvpn-devel] [PATCH 3/5] Assign and honour signal priority order

Am 01.01.23 um 22:51 schrieb [email protected]...:

Quoted 9 lines

From: Selva Nair <[email protected]...>

Signals are ordered as SIGUSR2, SIGUSR1, SIGHUP, SIGTERM, SIGINT in increasing priority. Lower priority signals are not allowed to overwrite higher ones.

This should fix Trac #311, #639 -- SIGTER/SIGINT lost during dns resolution. (except for the Windows-specific bug handled in next commit)

Quoted 7 lines

On sending SIGTERM during dns resolution, it still takes several seconds to terminate as the signal will get processed only after getaddrinfo times out twice (in phase1 and phase2 inits).

Github: fixes OpenVPN/openvpn#205
Note: one has to still wait for address resolution to time out as getaddrinfo() is no interruptible. But a single ctrl-C (and some patience) is enough.

Thanks. This looks like a good approach. I know there is some other similar bug somewhere else that has been forever on my todo list of my Android app and also the problem with signal ordering.

While reviewing the patch, I noticed that SIG_SOURCE_SOFT/SIG_SOURCE_HARD seems to be purely informational and have no real other meaning.

#7 lore raw

[Openvpn-devel] [PATCH applied] Re: Assign and honour signal priority order

In reply to [email protected]...

I've stared at the code for quite a bit, and this looks good, and cleans up quite a few of the odd corner cases ("do not set signal if some other signal is there already") that have grown over time, like the one in proxy.c.

I have not manually tested this to see if I could break it, or reproduce one of the "known issues" - I might go back and do so, but today I am lacking time. I *have* fed this to the full set of client/server tests, FreeBSD, Linux, Linux with DCO, which do excercise the SIGUSR1/SIGTERM on the p2mp server a bit and SIGUSR1/SIGTERM on the client, and all is nicely well-behaved.

Your patch has been applied to the master and release/2.6 branch.

commit 3a7257925791a086c1ac88362a8eb422be518b14 (master) commit b3b1436955b9db8e557fc58b7e37ba3a881109a6 (release/2.6)

Author: Selva Nair
Date:   Sun Jan 1 16:51:07 2023 -0500
     Assign and honour signal priority order

     Signed-off-by: Selva Nair <[email protected]...>
     Acked-by: Arne Schwabe <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg25871.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#8 lore raw

PATCH 4/5 Fix signal handling on Windows

In reply to [email protected]...

From: Selva Nair <[email protected]...>

- In win32_signal_get() re-order the check so that Windows signals are picked up even if signal_received is non-zero

- When management is not active, management_sleep() becomes sleep() but it is not interruptible by signals on Windows. Fix this by periodically checking for signal.

Fixes Trac #311 #639 (windows specific part)

Github: Fixes OpenVPN/openvpn#205 (windows specific part)
Note: if stuck in address resolution, press ctrl-C and wait for getaddrinfo() to timeout.

---
 src/openvpn/manage.c |  4 ++
 src/openvpn/win32.c  | 98 +++++++++++++++++++++++++++++---------------
 2 files changed, 68 insertions(+), 34 deletions(-)
src/openvpn/manage.c +4 -0
index 5465b7e9..ac37e557 100644
@@ -4091,10 +4091,14 @@ man_persist_client_stats(struct management *man, struct context *c)
 void
 management_sleep(const int n)
 {
+#ifdef WIN32
+    win32_sleep(n);
+#else
     if (n > 0)
     {
         sleep(n);
     }
+#endif /* ifdef WIN32 */
 }
 
 #endif /* ENABLE_MANAGEMENT */
src/openvpn/win32.c +64 -34

118 lines not on this page · raw message

Signature · 1 line
2.34.1
#9 lore raw

PATCH v2 4/5 Fix signal handling on Windows

From: Selva Nair <[email protected]...>

- In win32_signal_get() re-order the check so that Windows signals are picked up even if signal_received is non-zero

- When management is not active, management_sleep() becomes sleep() but it is not interruptible by signals on Windows. Fix this by periodically checking for signal.

Fixes Trac #311 #639 (windows specific part)

Github: Fixes OpenVPN/openvpn#205 (windows specific part)
Note: if stuck in address resolution, press ctrl-C and wait for getaddrinfo() to timeout.

v2: WIN32 --> _WIN32
add a chunk in management_sleep that was missed by sloppy conflict-resolution

---
 src/openvpn/manage.c | 16 +++++++-
 src/openvpn/win32.c  | 98 +++++++++++++++++++++++++++++---------------
 src/openvpn/win32.h  |  3 ++
 3 files changed, 81 insertions(+), 36 deletions(-)
src/openvpn/manage.c +14 -2
index 5465b7e9..9bd5ef4e 100644
@@ -4045,9 +4045,16 @@ management_sleep(const int n)
     {
         management_event_loop_n_seconds(management, n);
     }
-    else if (n > 0)
+    else
     {
-        sleep(n);
+#ifdef _WIN32
+        win32_sleep(n);
+#else
+        if (n > 0)
+        {
+            sleep(n);
+        }
+#endif
     }
 }
 
@@ -4088,13 +4095,18 @@ man_persist_client_stats(struct management *man, struct context *c)
 
 #else  /* ifdef ENABLE_MANAGEMENT */
 
+#include "win32.h"
 void
 management_sleep(const int n)
 {
+#ifdef _WIN32
+    win32_sleep(n);
+#else
     if (n > 0)
     {
         sleep(n);
     }
+#endif /* ifdef _WIN32 */
 }
 
 #endif /* ENABLE_MANAGEMENT */
src/openvpn/win32.c +64 -34

118 lines not on this page · raw message

src/openvpn/win32.h +3 -0
index b1371999..26d5ef8f 100644
@@ -330,5 +330,8 @@ openvpn_execve(const struct argv *a, const struct env_set *es, const unsigned in
 bool
 openvpn_swprintf(wchar_t *const str, const size_t size, const wchar_t *const format, ...);
 
+/* Sleep that can be interrupted by signals and exit event */
+void win32_sleep(const int n);
+
 #endif /* ifndef OPENVPN_WIN32_H */
 #endif /* ifdef _WIN32 */
Signature · 1 line
2.34.1
#10 lore raw

Re: [Openvpn-devel] [PATCH v2 4/5] Fix signal handling on Windows

Hi,

src/openvpn/manage.c management_sleep(const int n) · line 4055

Quoted 10 lines
src/openvpn/manage.c +7 -2
-    else if (n > 0)
+    else
     {
-        sleep(n);
+#ifdef _WIN32
+        win32_sleep(n);
+#else
+        if (n > 0)
+        {
+            sleep(n);

My understanding is that we want to have interruptible sleep. In this case, what is the point of calling win32_sleep(0) ? I understand that management_sleep(0)
is required to service possible management commands even if we don't need a delay,
but shouldn't it be a no-op without management?

src/openvpn/win32.c set_openssl_env_vars() · line 1606

Quoted 7 lines
src/openvpn/win32.c +7 -0
+void
+win32_sleep(const int n)
+{
+    if (n < 0)
+    {
+        return;
+    }

Why not <= 0 ?

src/openvpn/win32.c set_openssl_env_vars() · line 1610

Quoted 3 lines
src/openvpn/win32.c +3 -0
+    /* Sleep() is not interruptible. Use a WAIT_OBJECT to catch signal */
+    if (HANDLE_DEFINED(win32_signal.in.read))
+    {

I would reverse this condition, and do just

    Sleep(n*1000);
    return;

This way we get rid of the indentation level.

src/openvpn/win32.c set_openssl_env_vars() · line 1613

Quoted 3 lines
src/openvpn/win32.c +3 -0
+        time_t expire = 0;
+        update_time();
+        expire = now + n;

I would declare and initialize time_t expire after update_time().

src/openvpn/win32.c set_openssl_env_vars() · line 1620

Quoted 5 lines
src/openvpn/win32.c +5 -0
+            DWORD status = WaitForSingleObject(win32_signal.in.read, (expire - now)*1000);
+            if (win32_signal_get(&win32_signal) || status == WAIT_TIMEOUT)
+            {
+                return;
+            }

Shouldn't we call win32_signal_get() only if status == WAIT_OBJECT_0 ?

Under what circumstances are we supposed to do the waiting again? If we get a signal, we bail out.
If the wait times out, we bail out. If wait fails, we do Sleep (although at this point we probably have a bigger issue).

-Lev

author
#11 lore raw

Re: [Openvpn-devel] [PATCH v2 4/5] Fix signal handling on Windows

Hi,

Thanks for the careful review

Quoted 16 lines · Lev Stipakov

Hi,

src/openvpn/manage.c management_sleep(const int n) · line 4055

Quoted 10 lines
src/openvpn/manage.c +7 -2
-    else if (n > 0)
+    else
     {
-        sleep(n);
+#ifdef _WIN32
+        win32_sleep(n);
+#else
+        if (n > 0)
+        {
+            sleep(n);

My understanding is that we want to have interruptible sleep. In this case, what is the point of calling win32_sleep(0) ?

I understand that

Quoted 5 lines

management_sleep(0)
is required to service possible management commands even if we don't need a delay,
but shouldn't it be a no-op without management?

Right, I'm going beyond the original which does nothing for management_sleep(0) if management is not active or enabled.

I want to err on the side of checking windows signal rather than missing it. In case of unix/linux OS signals do get delivered out of band into the global signal_received. In the case of Windows, it has to be picked up (except for the obscure ctrl-Break).

Note that management_sleep(0) when management is enabled does call get_signal()==win32_signal_get and picks up Windows signals, not just management commands. win32_sleep(0) also calls win32_signal_get() even if there is no time to pause.

Quoted 11 lines

src/openvpn/win32.c set_openssl_env_vars() · line 1606

Quoted 8 lines
src/openvpn/win32.c +7 -0
+void
+win32_sleep(const int n)
+{
+    if (n < 0)
+    {
+        return;
+    }

Why not <= 0 ?

See above.

Quoted 22 lines
Quoted 2 lines

+ /* Sleep() is not interruptible. Use a WAIT_OBJECT to catch signal

*/

Review

Quoted 2 lines
(quoted diff) +2 -0
+    if (HANDLE_DEFINED(win32_signal.in.read))
+    {

I would reverse this condition, and do just

    Sleep(n*1000);
    return;

This way we get rid of the indentation level.

src/openvpn/win32.c set_openssl_env_vars() · line 1613

Quoted 3 lines
src/openvpn/win32.c +3 -0
+        time_t expire = 0;
+        update_time();
+        expire = now + n;

I would declare and initialize time_t expire after update_time().

Quoted 1 line

+ DWORD status = WaitForSingleObject(win32_signal.in.read,

Quoted 9 lines

(expire - now)*1000);

Quoted 1 line

+ if (win32_signal_get(&win32_signal) || status ==

WAIT_TIMEOUT)

src/openvpn/win32.c set_openssl_env_vars() · line 1606

Quoted 3 lines
src/openvpn/win32.c +3 -0
+            {
+                return;
+            }

Shouldn't we call win32_signal_get() only if status == WAIT_OBJECT_0 ?

To ensure the signal is checked at least once in all cases including n = 0. Not sure whether status will be WAIT_TIMEOUT or WAIT_OBJECT_0 in case n = 0 and there is a signal to pickup.

Quoted 6 lines

Under what circumstances are we supposed to do the waiting again? If we get a signal, we bail out.
If the wait times out, we bail out. If wait fails, we do Sleep (although at this point we probably have a bigger issue).

Probably never -- there is a WAIT_ABANDONED which may not apply here. But, sometimes windows functions do exit with error codes not listed in the docs. As the loop is slowed down by a Sleep(1000), this can't hurt, can it?

The common thread in all of the above is that I'm trying to err on the side of checking for signals an extra round as opposed to missing it. Can do a v3 if you think it's warranted.

Thanks,

Selva

author
#12 lore raw

Re: [Openvpn-devel] [PATCH v2 4/5] Fix signal handling on Windows

Hi

Quoted 8 lines
Quoted 7 lines

Under what circumstances are we supposed to do the waiting again? If we get a signal, we bail out.
If the wait times out, we bail out. If wait fails, we do Sleep (although at this point we probably have a bigger issue).

Quoted 4 lines

Probably never -- there is a WAIT_ABANDONED which may not apply here. But, sometimes windows functions do exit with error codes not listed in the docs. As the loop is slowed down by a Sleep(1000), this can't hurt, can it?

I see your point now :) I had a different logic earlier where it always slept for a second before trying to wait again, but not so any longer. v3 is coming!

Selva

Quoted 1 line
#13 lore raw

PATCH v3 4/5 Fix signal handling on Windows

In reply to [email protected]...

From: Selva Nair <[email protected]...>

- In win32_signal_get() re-order the check so that Windows signals are picked up even if signal_received is non-zero

- When management is not active, management_sleep() becomes sleep() but it is not interruptible by signals on Windows. Fix this by periodically checking for signal.

Fixes Trac #311 #639 (windows specific part)

Github: Fixes OpenVPN/openvpn#205 (windows specific part)
Note: if stuck in address resolution, press ctrl-C and wait for getaddrinfo() to timeout.

v2: WIN32 --> _WIN32
add a chunk in management_sleep that was missed by sloppy conflict-resolution

v3: following review by Lev Stipakov <[email protected]...>
win32_sleep()
- Early fallback to Sleep() if no wait handles -- less indentation
- Check signal only if wait-object triggered
- Exit the while loop if not safe to continue
Behaviour of win32_sleep(0) checking signal is retained though may be redundant

---
More on why the while loop is required for the wait:
The wait handle in.read can trigger at any key-board input. Even with ctrl-C, it first triggers for the ctrl keypress and only on a second round when keypress of "C" triggers it again we get it parsed as SIGINT/SIGTERM by win32_signal_get().

To make this safer, and not overload the process with repeated waits, we now filter out any status other than WAIT_OBJECT_0, not just WAIT_FAILED. That should be safe enough.

 src/openvpn/manage.c | 16 +++++++-
 src/openvpn/win32.c  | 98 +++++++++++++++++++++++++++++---------------
 src/openvpn/win32.h  |  3 ++
 3 files changed, 81 insertions(+), 36 deletions(-)
src/openvpn/manage.c +14 -2
index 5465b7e9..9bd5ef4e 100644
@@ -4045,9 +4045,16 @@ management_sleep(const int n)
     {
         management_event_loop_n_seconds(management, n);
     }
-    else if (n > 0)
+    else
     {
-        sleep(n);
+#ifdef _WIN32
+        win32_sleep(n);
+#else
+        if (n > 0)
+        {
+            sleep(n);
+        }
+#endif
     }
 }
 
@@ -4088,13 +4095,18 @@ man_persist_client_stats(struct management *man, struct context *c)
 
 #else  /* ifdef ENABLE_MANAGEMENT */
 
+#include "win32.h"
 void
 management_sleep(const int n)
 {
+#ifdef _WIN32
+    win32_sleep(n);
+#else
     if (n > 0)
     {
         sleep(n);
     }
+#endif /* ifdef _WIN32 */
 }
 
 #endif /* ENABLE_MANAGEMENT */
src/openvpn/win32.c +64 -34

118 lines not on this page · raw message

src/openvpn/win32.h +3 -0
index b1371999..26d5ef8f 100644
@@ -330,5 +330,8 @@ openvpn_execve(const struct argv *a, const struct env_set *es, const unsigned in
 bool
 openvpn_swprintf(wchar_t *const str, const size_t size, const wchar_t *const format, ...);
 
+/* Sleep that can be interrupted by signals and exit event */
+void win32_sleep(const int n);
+
 #endif /* ifndef OPENVPN_WIN32_H */
 #endif /* ifdef _WIN32 */
Signature · 1 line
2.34.1
#14 lore raw

PATCH v4 4/5 Fix signal handling on Windows

From: Selva Nair <[email protected]...>

- In win32_signal_get() re-order the check so that Windows signals are picked up even if signal_received is non-zero

- When management is not active, management_sleep() becomes sleep() but it is not interruptible by signals on Windows. Fix this by periodically checking for signal.

Fixes Trac #311 #639 (windows specific part)

Github: Fixes OpenVPN/openvpn#205 (windows specific part)
Note: if stuck in address resolution, press ctrl-C and wait for getaddrinfo() to timeout.

v2: WIN32 --> _WIN32
add a chunk in management_sleep that was missed by sloppy conflict-resolution

v3: following review by Lev Stipakov <[email protected]...>
win32_sleep()
- Early fallback to Sleep() if no wait handles -- less indentation
- Check signal only if wait-object triggered
- Exit the while loop if not safe to continue
Behaviour of win32_sleep(0) checking signal is retained though may be redundant

v4: Avoid Sleep(0) and never loop back to wait again if wait-failed

---
More on why the while loop is required for the wait:
The wait handle in.read can trigger at any key-board input. Even with ctrl-C, it first triggers for the ctrl key-press and only on a second round when key-press of "C" triggers it again we get it parsed as SIGINT/SIGTERM by win32_signal_get().

To make this safer, and not overload the process with repeated waits, we now filter out any status other than WAIT_OBJECT_0, not just WAIT_FAILED. That should be safe enough.

 src/openvpn/manage.c |  16 ++++++-
 src/openvpn/win32.c  | 105 +++++++++++++++++++++++++++++--------------
 src/openvpn/win32.h  |   3 ++
 3 files changed, 88 insertions(+), 36 deletions(-)
src/openvpn/manage.c +14 -2
index 5465b7e9..9bd5ef4e 100644
@@ -4045,9 +4045,16 @@ management_sleep(const int n)
     {
         management_event_loop_n_seconds(management, n);
     }
-    else if (n > 0)
+    else
     {
-        sleep(n);
+#ifdef _WIN32
+        win32_sleep(n);
+#else
+        if (n > 0)
+        {
+            sleep(n);
+        }
+#endif
     }
 }
 
@@ -4088,13 +4095,18 @@ man_persist_client_stats(struct management *man, struct context *c)
 
 #else  /* ifdef ENABLE_MANAGEMENT */
 
+#include "win32.h"
 void
 management_sleep(const int n)
 {
+#ifdef _WIN32
+    win32_sleep(n);
+#else
     if (n > 0)
     {
         sleep(n);
     }
+#endif /* ifdef _WIN32 */
 }
 
 #endif /* ENABLE_MANAGEMENT */
src/openvpn/win32.c +71 -34

125 lines not on this page · raw message

src/openvpn/win32.h +3 -0
index b1371999..26d5ef8f 100644
@@ -330,5 +330,8 @@ openvpn_execve(const struct argv *a, const struct env_set *es, const unsigned in
 bool
 openvpn_swprintf(wchar_t *const str, const size_t size, const wchar_t *const format, ...);
 
+/* Sleep that can be interrupted by signals and exit event */
+void win32_sleep(const int n);
+
 #endif /* ifndef OPENVPN_WIN32_H */
 #endif /* ifdef _WIN32 */
Signature · 1 line
2.34.1
Acked-by on v4 4/5
#15 lore raw

Re: [Openvpn-devel] [PATCH v4 4/5] Fix signal handling on Windows

Looks good,

Tested with an unresolvable hostname - I was able to break during restart pause, unlike before.

2023-01-06 13:49:53 us=46000 SIGUSR1[soft,Could not determine
IPv4/IPv6 protocol] received, process restarting
2023-01-06 13:49:53 us=46000 Restart pause, 16 second(s)

<pressed F4> C:\Users\lev\Projects\openvpn-build\src\openvpn\ARM64-Output\Debug\openvpn.exe (process 11476) exited with code 0.

#16 lore raw

[Openvpn-devel] [PATCH applied] Re: Fix signal handling on Windows

In reply to [email protected]...

I've stared at the code for a bit ("git show -w" helps), but not having deep understanding of all windows/management intricacies, I can only say "it looks reasonable".

I have also built a test binary (Ubuntu/MinGW) which behaved properly when called from the CLI. Tested F1, F2, F3, F4 and ctrl-C.

I have also recreated the test Lev has done, connect to a non-working server with "--connect-retry 30" and try to press ctrl-c when sleeping - did not work before, works now.

Your patch has been applied to the master and release/2.6 branch.

commit 22977577ed128ac953e7ebfe30f839bcf651b334 (master) commit dec9c03d0f6e184fb6cce16b554491d62622425c (release/2.6)

Author: Selva Nair
Date:   Thu Jan 5 19:54:38 2023 -0500
     Fix signal handling on Windows

     Signed-off-by: Selva Nair <[email protected]...>
     Acked-by: Lev Stipakov <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg25895.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#17 lore raw

PATCH 5/5 Improve signal handling using POSIX sigaction

In reply to [email protected]...

From: Selva Nair <[email protected]...>

Currently we use the old signal API which follows system-V or BSD semantics depending on the platform and/or feature-set macros. Further, signal has many weaknesses which makes proper masking (blocking) of signals during update not possible.

Improve this:

- Use sigaction to properly mask signals when modifying.

- Change signal_reset() to read the current value and reset in one operation. This is required to avoid change of signal state between check and reset-operations.
This also allows us to eliminate resetting signal to 0 in init_instance() which can potentially lose signals. Instead, the signal is reset at the end of the SIGUSR1 and SIGHUP loops where their values are checked.

Notes:

SIG_SOURCE_CONNECTION_FAILED is retained in a hackish way. This value has the same meaning as SIG_SOURCE_SOFT everywhere except where the signal is printed. Looks cosmetic --- could be eliminated?

SIGUSR1 during dns resolution is ignored and reset to zero in the original and that behaviour is retained. Not sure why this is needed. Special handling of signals in openvpn_getaddrinfo() as if it is a syscall is retained but looks superfluous.

In pre_init_signal_catch() we ignore some unix signals, but the same signals from management are not ignored though both are treated as "HARD" signals. For example, during auth-user-pass query, "kill -SIGUSR1 <pid>" will be ignored, but "signal SIGUSR1" from management interface will cause M_FATAL and exit. This is the current behaviour, but could be improved?

---
 src/openvpn/init.c    |   3 -
 src/openvpn/multi.c   |   5 +-
 src/openvpn/openvpn.c |   4 +-
 src/openvpn/sig.c     | 294 ++++++++++++++++++++++++++++++++----------
 src/openvpn/sig.h     |   7 +-
 src/openvpn/socket.c  |  12 +-
 6 files changed, 239 insertions(+), 86 deletions(-)
src/openvpn/init.c +0 -3
index eec25acf..eabc8ea1 100644
@@ -4228,9 +4228,6 @@ init_instance(struct context *c, const struct env_set *env, const unsigned int f
         do_inherit_env(c, env);
     }
 
-    /* signals caught here will abort */
-    signal_reset(c->sig);
-
     if (c->mode == CM_P2P)
     {
         init_management_callback_p2p(c);
src/openvpn/multi.c +2 -3
index c2254399..1e7e76f2 100644
@@ -3828,7 +3828,7 @@ multi_push_restart_schedule_exit(struct multi_context *m, bool next_server)
                        &m->deferred_shutdown_signal.wakeup,
                        compute_wakeup_sigma(&m->deferred_shutdown_signal.wakeup));
 
-    signal_reset(m->top.sig);
+    signal_reset(m->top.sig, 0);
 }
 
 /*
@@ -3838,12 +3838,11 @@ multi_push_restart_schedule_exit(struct multi_context *m, bool next_server)
 bool
 multi_process_signal(struct multi_context *m)
 {
-    if (m->top.sig->signal_received == SIGUSR2)
+    if (signal_reset(m->top.sig, SIGUSR2) == SIGUSR2)
     {
         struct status_output *so = status_open(NULL, 0, M_INFO, NULL, 0);
         multi_print_status(m, so, m->status_file_version);
         status_close(so);
-        signal_reset(m->top.sig);
         return false;
     }
     else if (proto_is_dgram(m->top.options.ce.proto)
src/openvpn/openvpn.c +2 -2
index 413a750b..d590b166 100644
@@ -333,14 +333,14 @@ openvpn_main(int argc, char *argv[])
                 /* pass restart status to management subsystem */
                 signal_restart_status(c.sig);
             }
-            while (c.sig->signal_received == SIGUSR1);
+            while (signal_reset(c.sig, SIGUSR1) == SIGUSR1);
 
             env_set_destroy(c.es);
             uninit_options(&c.options);
             gc_reset(&c.gc);
             uninit_early(&c);
         }
-        while (c.sig->signal_received == SIGHUP);
+        while (signal_reset(c.sig, SIGHUP) == SIGHUP);
     }
 
     context_gc_free(&c);
src/openvpn/sig.c +226 -68

394 lines not on this page · raw message

src/openvpn/sig.h +6 -1
index 83adc543..b2845d97 100644
@@ -81,7 +81,12 @@ void register_signal(struct signal_info *si, int sig, const char *text);
 
 void process_explicit_exit_notification_timer_wakeup(struct context *c);
 
-void signal_reset(struct signal_info *si);
+/**
+ * Clear the signal if its current value equals signum. If signum is
+ * zero the signal is cleared independent of its current value.
+ * @returns the current value of the signal.
+ */
+int signal_reset(struct signal_info *si, int signum);
 
 static inline void
 halt_non_edge_triggered_signals(void)
src/openvpn/socket.c +3 -9
index 59d89352..61971906 100644
@@ -567,12 +567,11 @@ openvpn_getaddrinfo(unsigned int flags,
                 if (sig_info->signal_received) /* were we interrupted by a signal? */
                 {
                     /* why are we overwriting SIGUSR1 ? */
-                    if (sig_info->signal_received == SIGUSR1) /* ignore SIGUSR1 */
+                    if (signal_reset(sig_info, SIGUSR1) == SIGUSR1) /* ignore SIGUSR1 */
                     {
                         msg(level,
                             "RESOLVE: Ignored SIGUSR1 signal received during "
                             "DNS resolution attempt");
-                        signal_reset(sig_info);
                     }
                     else
                     {
@@ -1588,7 +1587,6 @@ socket_connect(socket_descriptor_t *sd,
         openvpn_close_socket(*sd);
         *sd = SOCKET_UNDEFINED;
         register_signal(sig_info, SIGUSR1, "connection-failed");
-        sig_info->source = SIG_SOURCE_CONNECTION_FAILED;
     }
     else
     {
@@ -1782,11 +1780,7 @@ resolve_remote(struct link_socket *sock,
             }
             if (status!=0)
             {
-                if (signal_received)
-                {
-                    /* potential overwrite of signal */
-                    register_signal(sig_info, SIGUSR1, "socks-resolve-failure");
-                }
+                register_signal(sig_info, SIGUSR1, "socks-resolve-failure");
                 goto done;
             }
         }
@@ -2177,7 +2171,7 @@ link_socket_init_phase2(struct context *c)
     if (sig_info->signal_received)
     {
         sig_save = *sig_info;
-        signal_reset(sig_info);
+        sig_save.signal_received = signal_reset(sig_info, 0);
     }
 
     /* initialize buffers */
Signature · 1 line
2.34.1
#18 lore raw

PATCH v2 5/5 Improve signal handling using POSIX sigaction

From: Selva Nair <[email protected]...>

Currently we use the old signal API which follows system-V or BSD semantics depending on the platform and/or feature-set macros. Further, signal has many weaknesses which makes proper masking (blocking) of signals during update not possible.

Improve this:

- Use sigaction to properly mask signals when modifying.

- Change signal_reset() to read the current value and reset in one operation. This is required to avoid change of signal state between check and reset-operations.
This also allows us to eliminate resetting signal to 0 in init_instance() which can potentially lose signals. Instead, the signal is reset at the end of the SIGUSR1 and SIGHUP loops where their values are checked.

Notes:

SIG_SOURCE_CONNECTION_FAILED is retained in a hackish way. This value has the same meaning as SIG_SOURCE_SOFT everywhere except where the signal is printed. Looks cosmetic --- could be eliminated?

SIGUSR1 during dns resolution is ignored and reset to zero in the original and that behaviour is retained. Not sure why this is needed. Special handling of signals in openvpn_getaddrinfo() as if it is a syscall is retained but looks superfluous.

In pre_init_signal_catch() we ignore some unix signals, but the same signals from management are not ignored though both are treated as "HARD" signals. For example, during auth-user-pass query, "kill -SIGUSR1 <pid>" will be ignored, but "signal SIGUSR1" from management interface will cause M_FATAL and exit. This is the current behaviour, but could be improved?

v2: A buggy version was submitted as v1 -- NULL check missing around line 1785 in socket.c
Fixed.

---
 src/openvpn/init.c    |   3 -
 src/openvpn/multi.c   |   5 +-
 src/openvpn/openvpn.c |   4 +-
 src/openvpn/sig.c     | 294 ++++++++++++++++++++++++++++++++----------
 src/openvpn/sig.h     |   7 +-
 src/openvpn/socket.c  |   7 +-
 6 files changed, 238 insertions(+), 82 deletions(-)
src/openvpn/init.c +0 -3
index eec25acf..eabc8ea1 100644
@@ -4228,9 +4228,6 @@ init_instance(struct context *c, const struct env_set *env, const unsigned int f
         do_inherit_env(c, env);
     }
 
-    /* signals caught here will abort */
-    signal_reset(c->sig);
-
     if (c->mode == CM_P2P)
     {
         init_management_callback_p2p(c);
src/openvpn/multi.c +2 -3
index c2254399..1e7e76f2 100644
@@ -3828,7 +3828,7 @@ multi_push_restart_schedule_exit(struct multi_context *m, bool next_server)
                        &m->deferred_shutdown_signal.wakeup,
                        compute_wakeup_sigma(&m->deferred_shutdown_signal.wakeup));
 
-    signal_reset(m->top.sig);
+    signal_reset(m->top.sig, 0);
 }
 
 /*
@@ -3838,12 +3838,11 @@ multi_push_restart_schedule_exit(struct multi_context *m, bool next_server)
 bool
 multi_process_signal(struct multi_context *m)
 {
-    if (m->top.sig->signal_received == SIGUSR2)
+    if (signal_reset(m->top.sig, SIGUSR2) == SIGUSR2)
     {
         struct status_output *so = status_open(NULL, 0, M_INFO, NULL, 0);
         multi_print_status(m, so, m->status_file_version);
         status_close(so);
-        signal_reset(m->top.sig);
         return false;
     }
     else if (proto_is_dgram(m->top.options.ce.proto)
src/openvpn/openvpn.c +2 -2
index 413a750b..d590b166 100644
@@ -333,14 +333,14 @@ openvpn_main(int argc, char *argv[])
                 /* pass restart status to management subsystem */
                 signal_restart_status(c.sig);
             }
-            while (c.sig->signal_received == SIGUSR1);
+            while (signal_reset(c.sig, SIGUSR1) == SIGUSR1);
 
             env_set_destroy(c.es);
             uninit_options(&c.options);
             gc_reset(&c.gc);
             uninit_early(&c);
         }
-        while (c.sig->signal_received == SIGHUP);
+        while (signal_reset(c.sig, SIGHUP) == SIGHUP);
     }
 
     context_gc_free(&c);
src/openvpn/sig.c +226 -68

394 lines not on this page · raw message

src/openvpn/sig.h +6 -1
index 83adc543..b2845d97 100644
@@ -81,7 +81,12 @@ void register_signal(struct signal_info *si, int sig, const char *text);
 
 void process_explicit_exit_notification_timer_wakeup(struct context *c);
 
-void signal_reset(struct signal_info *si);
+/**
+ * Clear the signal if its current value equals signum. If signum is
+ * zero the signal is cleared independent of its current value.
+ * @returns the current value of the signal.
+ */
+int signal_reset(struct signal_info *si, int signum);
 
 static inline void
 halt_non_edge_triggered_signals(void)
src/openvpn/socket.c +2 -5
index 59d89352..65de7456 100644
@@ -567,12 +567,11 @@ openvpn_getaddrinfo(unsigned int flags,
                 if (sig_info->signal_received) /* were we interrupted by a signal? */
                 {
                     /* why are we overwriting SIGUSR1 ? */
-                    if (sig_info->signal_received == SIGUSR1) /* ignore SIGUSR1 */
+                    if (signal_reset(sig_info, SIGUSR1) == SIGUSR1) /* ignore SIGUSR1 */
                     {
                         msg(level,
                             "RESOLVE: Ignored SIGUSR1 signal received during "
                             "DNS resolution attempt");
-                        signal_reset(sig_info);
                     }
                     else
                     {
@@ -1588,7 +1587,6 @@ socket_connect(socket_descriptor_t *sd,
         openvpn_close_socket(*sd);
         *sd = SOCKET_UNDEFINED;
         register_signal(sig_info, SIGUSR1, "connection-failed");
-        sig_info->source = SIG_SOURCE_CONNECTION_FAILED;
     }
     else
     {
@@ -1784,7 +1782,6 @@ resolve_remote(struct link_socket *sock,
             {
                 if (signal_received)
                 {
-                    /* potential overwrite of signal */
                     register_signal(sig_info, SIGUSR1, "socks-resolve-failure");
                 }
                 goto done;
@@ -2177,7 +2174,7 @@ link_socket_init_phase2(struct context *c)
     if (sig_info->signal_received)
     {
         sig_save = *sig_info;
-        signal_reset(sig_info);
+        sig_save.signal_received = signal_reset(sig_info, 0);
     }
 
     /* initialize buffers */
Signature · 1 line
2.34.1
#19 lore raw

Re: [Openvpn-devel] [PATCH v2 5/5] Improve signal handling using POSIX sigaction

Hi,

Quoted 6 lines · On Mon, Jan 02, 2023 at 02:43:22PM -0500, [email protected]... wrote:

From: Selva Nair <[email protected]...>

Currently we use the old signal API which follows system-V or BSD semantics depending on the platform and/or feature-set macros. Further, signal has many weaknesses which makes proper masking (blocking) of signals during update not possible.

Just for the record - as discussed before, this is considered too intrusive ("needs more testing") for 2.6.0, and can go in early in the 2.7 phase.

Even if it gets an ACK before 2.6.0 release, I'll still postpone it, because having a git master almost identical to release/2.6 makes testing "last minute 2.6 stuff" much easier for me.

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
author
#20 lore raw

Re: [Openvpn-devel] [PATCH v2 5/5] Improve signal handling using POSIX sigaction

Quoted 5 lines · Gert Doering

Hi,

Quoted 2 lines · On Mon, Jan 02, 2023 at 02:43:22PM -0500, [email protected]... wrote:

From: Selva Nair <[email protected]...>

Quoted 9 lines
Quoted 4 lines

Currently we use the old signal API which follows system-V or BSD semantics depending on the platform and/or feature-set macros. Further, signal has many weaknesses which makes proper masking (blocking) of signals during update not possible.

Just for the record - as discussed before, this is considered too intrusive ("needs more testing") for 2.6.0, and can go in early in the 2.7 phase.

if no one has started reviewing 5/5, I would like to split this into two

1. signal -> sigaction affecting sig.c only
2. changes to the way signal reset is done

#2 involves some decisions, affects the rest of the code, and may elicit more comments.

Otherwise, will wait and see.

Selva

#21 lore raw

Re: [Openvpn-devel] [PATCH v2 5/5] Improve signal handling using POSIX sigaction

Quoted 19 lines · Selva Nair
Quoted 5 lines · Gert Doering

Hi,

Quoted 2 lines · On Mon, Jan 02, 2023 at 02:43:22PM -0500, [email protected]... wrote:

From: Selva Nair <[email protected]...>

Quoted 9 lines
Quoted 4 lines

Currently we use the old signal API which follows system-V or BSD semantics depending on the platform and/or feature-set macros. Further, signal has many weaknesses which makes proper masking (blocking) of signals during update not possible.

Just for the record - as discussed before, this is considered too intrusive ("needs more testing") for 2.6.0, and can go in early in the 2.7 phase.

if no one has started reviewing 5/5, I would like to split this into two

Quoted 7 lines

1. signal -> sigaction affecting sig.c only
2. changes to the way signal reset is done

#2 involves some decisions, affects the rest of the code, and may elicit more comments.

Otherwise, will wait and see.

I think it unlikely anyone has invested much time into a patch designated for 2.7 at this point in time. And even if, splitting up patches might still be worthwile, if it reduces the individual complexity.

So I would say: go ahead.

Regards,

Signature · 1 line
  Frank Lichtenheld
author
#22 lore raw
In reply to [email protected]...

Please use v2 for 4/5 and 5/5 -- sorry about the noise.

Quoted 20 lines · On Sun, Jan 1, 2023 at 4:51 PM <[email protected]...> wrote:

From: Selva Nair <[email protected]...>

Back to my long-forgotten signal patch -- cleaned up and rebased. What better to do on a Jan 1st afternoon...

Commits:

1.  Preparing for better signal handling: some code refactoring
2.  Refactor signal handling in openvpn_getaddrinfo
3.  Assign and honour signal priority order
4.  Fix signal handling on Windows
5.  Improve signal handling using POSIX sigaction

Commits 1 to 4 are fairly non-intrusive and should fix trac #639 and #311 (or github #205) for most practical purposes.

Commit 5 rewrites sig.c using sigaction for proper masking of signals during
updates. May be too intrusive and too late to get well-tested for 2.6.