2424#include <ctype.h>
2525#include <string.h>
2626
27+ #include "oauth2/ipc.h"
2728#include "oauth2/log.h"
2829#include "oauth2/mem.h"
2930#include "oauth2/util.h"
4041#include "cfg_int.h"
4142
4243static CURL * _s_curl = NULL ;
43- static oauth2_uint_t _curl_refcount = 0 ;
44+ static oauth2_ipc_mutex_t * _curl_mutex = NULL ;
4445
4546oauth2_log_t * oauth2_init (oauth2_log_level_t level , oauth2_log_sink_t * sink )
4647{
48+ oauth2_log_t * log = NULL ;
4749 ERR_load_crypto_strings ();
4850 OpenSSL_add_all_algorithms ();
4951 // TODO: align flags/call with memory initialization in mem.c
5052 // possibly providing alloc funcs as part of init?
5153 curl_global_init (CURL_GLOBAL_ALL );
52- return oauth2_log_init (level , sink );
54+ log = oauth2_log_init (level , sink );
55+ _curl_mutex = oauth2_ipc_mutex_init (log );
56+ oauth2_ipc_mutex_post_config (log , _curl_mutex );
57+ return log ;
5358}
5459
5560void oauth2_shutdown (oauth2_log_t * log )
@@ -61,6 +66,10 @@ void oauth2_shutdown(oauth2_log_t *log)
6166 curl_easy_cleanup (_s_curl );
6267 _s_curl = NULL ;
6368 }
69+ if (_curl_mutex != NULL ) {
70+ oauth2_ipc_mutex_free (log , _curl_mutex );
71+ _curl_mutex = NULL ;
72+ }
6473 curl_global_cleanup ();
6574 EVP_cleanup ();
6675 ERR_free_strings ();
@@ -243,23 +252,19 @@ int oauth2_strnenvcmp(const char *a, const char *b, int len)
243252
244253static CURL * oauth2_curl_init (oauth2_log_t * log )
245254{
255+ oauth2_ipc_mutex_lock (log , _curl_mutex );
246256 if (_s_curl == NULL ) {
247257 _s_curl = curl_easy_init ();
248258 if (_s_curl == NULL ) {
249259 oauth2_error (log , "curl_easy_init() error" );
250260 }
251261 }
252- _curl_refcount ++ ;
253262 return _s_curl ;
254263}
255264
256- static void oauth2_curl_free (CURL * curl )
265+ static void oauth2_curl_free (oauth2_log_t * log , CURL * curl )
257266{
258- _curl_refcount -- ;
259- if ((_curl_refcount == 0 ) && (_s_curl )) {
260- curl_easy_cleanup (_s_curl );
261- _s_curl = NULL ;
262- }
267+ oauth2_ipc_mutex_unlock (log , _curl_mutex );
263268}
264269
265270char * oauth2_url_encode (oauth2_log_t * log , const char * src )
@@ -290,7 +295,7 @@ char *oauth2_url_encode(oauth2_log_t *log, const char *src)
290295 if (rc )
291296 curl_free (rc );
292297 if (curl )
293- oauth2_curl_free (curl );
298+ oauth2_curl_free (log , curl );
294299
295300 oauth2_debug (log , "leave: %s" , dst );
296301
@@ -340,7 +345,7 @@ char *oauth2_url_decode(oauth2_log_t *log, const char *src)
340345 if (replaced )
341346 oauth2_mem_free (replaced );
342347 if (curl )
343- oauth2_curl_free (curl );
348+ oauth2_curl_free (log , curl );
344349
345350 oauth2_debug (log , "leave: %s" , dst );
346351
0 commit comments