| | 46 | #if (NGX_THREADS) && defined(OPENSSL_THREADS) |
| | 47 | static ngx_mutex_t* g_locks[CRYPTO_NUM_LOCKS] = {}; |
| | 48 | |
| | 49 | static int ngx_install_openssl_locks(ngx_log_t *log); |
| | 50 | static int ngx_install_openssl_callbacks(ngx_log_t *log); |
| | 51 | static unsigned long ngx_openssl_thread_id(void); |
| | 52 | static void ngx_openssl_lock_fn(int mode, int idx, const char* file, int line); |
| | 53 | static int ngx_remove_openssl_callbacks(void); |
| | 54 | static int ngx_remove_openssl_locks(void); |
| | 55 | #endif |
| | 56 | |
| | 110 | |
| | 111 | #if (NGX_THREADS) && defined(OPENSSL_THREADS) |
| | 112 | if(ngx_install_openssl_locks(log) != NGX_OK) |
| | 113 | { |
| | 114 | ngx_ssl_error(NGX_LOG_EMERG, log, 0, |
| | 115 | "ngx_install_openssl_locks() failed"); |
| | 116 | return NGX_ERROR; |
| | 117 | } |
| | 118 | |
| | 119 | if(ngx_install_openssl_callbacks(log) != NGX_OK) |
| | 120 | { |
| | 121 | ngx_ssl_error(NGX_LOG_EMERG, log, 0, |
| | 122 | "ngx_install_openssl_callbacks() failed"); |
| | 123 | return NGX_ERROR; |
| | 124 | } |
| | 125 | #endif |
| | 2598 | |
| | 2599 | |
| | 2600 | #if (NGX_THREADS) && defined(OPENSSL_THREADS) |
| | 2601 | int |
| | 2602 | ngx_install_openssl_locks(ngx_log_t *log) |
| | 2603 | { |
| | 2604 | if(CRYPTO_num_locks() != CRYPTO_NUM_LOCKS) |
| | 2605 | { |
| | 2606 | ngx_ssl_error(NGX_LOG_EMERG, log, 0, |
| | 2607 | "CRYPTO_num_locks() is not CRYPTO_NUM_LOCKS"); |
| | 2608 | return NGX_ERROR; |
| | 2609 | } |
| | 2610 | |
| | 2611 | for(unsigned n = 0; n < CRYPTO_NUM_LOCKS; n++) |
| | 2612 | { |
| | 2613 | g_locks[n] = ngx_mutex_init(log, 0); |
| | 2614 | if (g_locks[n] == NULL) { |
| | 2615 | return NGX_ERROR; |
| | 2616 | } |
| | 2617 | } |
| | 2618 | |
| | 2619 | return NGX_OK; |
| | 2620 | } |
| | 2621 | |
| | 2622 | |
| | 2623 | static int |
| | 2624 | ngx_install_openssl_callbacks(ngx_log_t *log) |
| | 2625 | { |
| | 2626 | CRYPTO_set_id_callback(ngx_openssl_thread_id); |
| | 2627 | CRYPTO_set_locking_callback(ngx_openssl_lock_fn); |
| | 2628 | |
| | 2629 | return NGX_OK; |
| | 2630 | } |
| | 2631 | |
| | 2632 | |
| | 2633 | unsigned long |
| | 2634 | ngx_openssl_thread_id(void) |
| | 2635 | { |
| | 2636 | return (unsigned long)ngx_thread_self(); |
| | 2637 | } |
| | 2638 | |
| | 2639 | |
| | 2640 | void |
| | 2641 | ngx_openssl_lock_fn(int mode, int idx, const char* file, int line) |
| | 2642 | { |
| | 2643 | if (!(idx < 0 || idx > CRYPTO_NUM_LOCKS)) |
| | 2644 | return; |
| | 2645 | |
| | 2646 | if ((mode & CRYPTO_LOCK) == CRYPTO_LOCK) |
| | 2647 | { |
| | 2648 | ngx_mutex_lock(g_locks[idx]); |
| | 2649 | } |
| | 2650 | else if ((mode & CRYPTO_UNLOCK) == CRYPTO_UNLOCK) |
| | 2651 | { |
| | 2652 | ngx_mutex_unlock(g_locks[idx]); |
| | 2653 | } |
| | 2654 | |
| | 2655 | /* Hmm... void functions suck */ |
| | 2656 | } |
| | 2657 | |
| | 2658 | |
| | 2659 | int |
| | 2660 | ngx_remove_openssl_callbacks(void) |
| | 2661 | { |
| | 2662 | CRYPTO_set_id_callback(NULL); |
| | 2663 | CRYPTO_set_locking_callback(NULL); |
| | 2664 | |
| | 2665 | return NGX_OK; |
| | 2666 | } |
| | 2667 | |
| | 2668 | |
| | 2669 | int |
| | 2670 | ngx_remove_openssl_locks(void) |
| | 2671 | { |
| | 2672 | for(unsigned n = 0; n < CRYPTO_NUM_LOCKS; n++) |
| | 2673 | { |
| | 2674 | ngx_mutex_destroy(g_locks[n]); |
| | 2675 | g_locks[n] = NULL; |
| | 2676 | } |
| | 2677 | |
| | 2678 | return NGX_OK; |
| | 2679 | } |
| | 2680 | #endif |