]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
Stay in non-blocking state for the entire TLS connection
authorEric Bollengier <eric@baculasystems.com>
Fri, 4 Nov 2022 08:08:40 +0000 (09:08 +0100)
committerEric Bollengier <eric@baculasystems.com>
Thu, 14 Sep 2023 11:57:00 +0000 (13:57 +0200)
bacula/src/lib/bsockcore.c
bacula/src/lib/tls.c

index db1443b56c69e2a434643592788c76f0bbcc6f2b..9b5559107191ddcd8e5522ffc84a463b182741d5 100644 (file)
@@ -466,7 +466,7 @@ bool BSOCKCORE::open(JCR *jcr, const char *name, char *host, char *service,
    m_terminated = false;
    m_suppress_error_msgs = false;
    errors = 0;
-   m_blocking = 0;
+   m_blocking = 1;              /* The socket will block by default */
 
 #ifdef INET6_ADDRSTRLEN
    char info[2*INET6_ADDRSTRLEN+20];
index 355b9c91e98b7cde32f286e0e8a32243bd2354e0..bfdcbc77f40395db5b572db81a050176f4437077 100644 (file)
@@ -719,8 +719,8 @@ static inline bool openssl_bsock_session_start(BSOCK *bsock, bool server)
 {
    TLS_CONNECTION *tls = bsock->tls;
    int err;
-   int flags;
    int stat = true;
+   int flags;
 
    /* Ensure that socket is non-blocking */
    flags = bsock->set_nonblocking();
@@ -768,8 +768,10 @@ static inline bool openssl_bsock_session_start(BSOCK *bsock, bool server)
    }
 
 cleanup:
-   /* Restore saved flags */
-   bsock->restore_blocking(flags);
+   if (!stat) {
+      // Got an error, restore the blocking state
+      bsock->restore_blocking(flags);
+   }
    /* Clear timer */
    bsock->timer_start = 0;
    bsock->set_killable(true);
@@ -866,8 +868,6 @@ static inline int openssl_bsock_readwrite(BSOCK *bsock, char *ptr, int nbytes, b
    while (nleft > 0) {
 
       pthread_mutex_lock(&tls->rwlock);
-      /* Ensure that socket is non-blocking */
-      int flags = bsock->set_nonblocking();
       int ssl_error = SSL_ERROR_NONE;
       while (nleft > 0 && ssl_error == SSL_ERROR_NONE) {
          if (write) {
@@ -884,8 +884,6 @@ static inline int openssl_bsock_readwrite(BSOCK *bsock, char *ptr, int nbytes, b
             ssl_error = SSL_get_error(tls->openssl, nwritten);
          }
       }
-      /* Restore saved flags */
-      bsock->restore_blocking(flags);
       pthread_mutex_unlock(&tls->rwlock);
 
       /* Handle errors */