[haiku-commits] r41828 - haiku/trunk/src/add-ons/mail_daemon/inbound_protocols/imap/imap_lib

  • From: clemens.zeidler@xxxxxxxxxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Mon, 30 May 2011 07:46:11 +0200 (CEST)

Author: czeidler
Date: 2011-05-30 07:46:10 +0200 (Mon, 30 May 2011)
New Revision: 41828
Changeset: https://dev.haiku-os.org/changeset/41828
Ticket: https://dev.haiku-os.org/ticket/7574

Modified:
   
haiku/trunk/src/add-ons/mail_daemon/inbound_protocols/imap/imap_lib/ServerConnection.cpp
Log:
SSL should be thread safe (right?) but SSL_library_init function and 
SSL_load_error_strings are not, or they should be called only once...
Remove extra ssl lock, there is already one in ssl.
This fixes #7574. I have seen this or a similar bug before and it was quit 
reproduceable, now it seems to be fixed.



Modified: 
haiku/trunk/src/add-ons/mail_daemon/inbound_protocols/imap/imap_lib/ServerConnection.cpp
===================================================================
--- 
haiku/trunk/src/add-ons/mail_daemon/inbound_protocols/imap/imap_lib/ServerConnection.cpp
    2011-05-30 05:29:13 UTC (rev 41827)
+++ 
haiku/trunk/src/add-ons/mail_daemon/inbound_protocols/imap/imap_lib/ServerConnection.cpp
    2011-05-30 05:46:10 UTC (rev 41828)
@@ -44,6 +44,38 @@
 
 
 #ifdef USE_SSL
+class InitSSL {
+public:
+       InitSSL()
+       {
+               if (SSL_library_init() != 1) {
+                       fInit = false;
+                       return;
+               }
+
+               RAND_seed(this, sizeof(InitSSL));
+               /*--- Because we're an add-on loaded at an unpredictable time, 
all the
+               memory addresses and things contained in ourself are 
esssentially
+               random. */
+               fInit = true;
+               return;
+       };
+
+
+       status_t
+       InitCheck()
+       {
+               return fInit ? B_OK : B_ERROR;
+       }
+
+private:
+                       bool                            fInit;
+};
+
+
+static InitSSL gInitSSL;
+
+
 class SSLConnection : public SocketConnection {
 public:
                                                                SSLConnection();
@@ -60,9 +92,6 @@
                        SSL_CTX*                        fCTX;
                        SSL*                            fSSL;
                        BIO*                            fBIO;
-
-                       // ssl seems to be not thread save
-                       BLocker                         fLocker;
 };
 #endif
 
@@ -255,20 +284,13 @@
        if (fSSL != NULL)
                Disconnect();
 
-       BAutolock _(fLocker);
+       if (gInitSSL.InitCheck() != B_OK)
+               return B_ERROR;
 
        status_t status = SocketConnection::Connect(server, port);
        if (status != B_OK)
                return status;
 
-       if (SSL_library_init() != 1)
-               return B_ERROR;
-       SSL_load_error_strings();
-       RAND_seed(this, sizeof(SSLConnection));
-       /*--- Because we're an add-on loaded at an unpredictable time, all
-       the memory addresses and things contained in ourself are
-       esssentially random. */
-
        fCTX = SSL_CTX_new(SSLv23_method());
        fSSL = SSL_new(fCTX);
        fBIO = BIO_new_socket(fSocket, BIO_NOCLOSE);
@@ -289,7 +311,6 @@
 SSLConnection::Disconnect()
 {
        TRACE("SSLConnection::Disconnect()\n");
-       BAutolock _(fLocker);
 
        if (fSSL)
                SSL_shutdown(fSSL);
@@ -308,14 +329,11 @@
 status_t
 SSLConnection::WaitForData(bigtime_t timeout)
 {
-       fLocker.Lock();
        if (!fSSL)
                return B_ERROR;
        if (SSL_pending(fSSL) > 0) {
-               fLocker.Unlock();
                return B_OK;
        }
-       fLocker.Unlock();
        return SocketConnection::WaitForData(timeout);
 }
 
@@ -323,7 +341,6 @@
 int32
 SSLConnection::Read(char* buffer, uint32 nBytes)
 {
-       BAutolock _(fLocker);
        if (!fSSL)
                return B_ERROR;
        return SSL_read(fSSL, buffer, nBytes);
@@ -333,7 +350,6 @@
 int32
 SSLConnection::Write(const char* buffer, uint32 nBytes)
 {
-       BAutolock _(fLocker);
        if (!fSSL)
                return B_ERROR;
        return SSL_write(fSSL, buffer, nBytes);


Other related posts: