File size: 3,602 Bytes
fd49381
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "xmlrpc_config.h"
#include <assert.h>
#include <pthread.h>
#include <openssl/crypto.h>

#include "mallocvar.h"

#include "xmlrpc-c/string_int.h"

#include "xmlrpc-c/openssl_thread.h"


/* These ugly global variables are necessary and appropriate because our
   purpose is to manipulate OpenSSL's ugly global state.
*/

static pthread_mutex_t * opensslMutex;
    /* Dynamically allocated array with an entry for each lock OpenSSL
       has created.  opensslMutex[N] is the mutex for the lock known by
       OpenSSL with ID N.
    */
      
static unsigned int maxLockCt;
 


static void
lock(int          const mode,
     int          const lockId,
     const char * const fileName ATTR_UNUSED,
     int          const lineNum ATTR_UNUSED) {
/*----------------------------------------------------------------------------
   This is a locking function for OpenSSL to call when it wants to lock or
   unlock a lock.
-----------------------------------------------------------------------------*/
    assert(lockId >= 0);
    assert((unsigned int)lockId < maxLockCt);

    pthread_mutex_t * const mutexP = &opensslMutex[lockId];

    if (mode & CRYPTO_LOCK)
        pthread_mutex_lock(mutexP);
    else
        pthread_mutex_unlock(mutexP);
}



static unsigned long
id(void) {
/*----------------------------------------------------------------------------
   This is an ID function for OpenSSL to call when it wants a unique
   identifier of the executing thread.
-----------------------------------------------------------------------------*/
    return ((unsigned long)pthread_self());
}



void
xmlrpc_openssl_thread_setup(const char ** const errorP) {
/*----------------------------------------------------------------------------
   Set up the OpenSSL library to handle being called by multiple threads
   concurrently.

   This setup is process-global, so this must be called exactly once in the
   program (ergo a subroutine library cannot normally call this under the
   covers; if a subroutine library want to use OpenSSL in a thread-safe
   manner, it must depend upon its user to call this).

   This needs to be called as the program starts up, when it is only one
   thread.

   (But this need not be called at all in a program that will never call
   OpenSSL functions from multiple threads).
-----------------------------------------------------------------------------*/
    maxLockCt = CRYPTO_num_locks();

    MALLOCARRAY(opensslMutex, maxLockCt);

    if (!opensslMutex)
        xmlrpc_asprintf(errorP, "Failed to allocate an array for %u "
                        "potential OpenSSL locks", maxLockCt);
    else {
        *errorP = NULL;
        
        unsigned int i;

        for (i = 0;  i < maxLockCt;  ++i)
            pthread_mutex_init(&opensslMutex[i], NULL);

        CRYPTO_set_id_callback(id);

        CRYPTO_set_locking_callback(lock);
    }
}



void
xmlrpc_openssl_thread_cleanup() {
/*----------------------------------------------------------------------------
   Undo 'xmlrpc_openssl_thread_setup'.

   This should be called at the end of a program, when there is only one
   thread left.

   (As a practical matter, there's really no need to call this at all; if the
   program is just going to exit afterward, any cleanup we do is moot).
-----------------------------------------------------------------------------*/
    CRYPTO_set_locking_callback(NULL);

    CRYPTO_set_id_callback(NULL);

    {
        unsigned int i;

        for (i = 0;  i < maxLockCt; ++i)
            pthread_mutex_destroy(&opensslMutex[i]);
    }
    free(opensslMutex);
}