2
0

opensslthreadlock.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. /* Example source code to show one way to set the necessary OpenSSL locking
  23. * callbacks if you want to do multi-threaded transfers with HTTPS/FTPS with
  24. * libcurl built to use OpenSSL.
  25. *
  26. * This is not a complete stand-alone example.
  27. *
  28. * Author: Jeremy Brown
  29. */
  30. #include <stdio.h>
  31. #include <pthread.h>
  32. #include <openssl/err.h>
  33. #define MUTEX_TYPE pthread_mutex_t
  34. #define MUTEX_SETUP(x) pthread_mutex_init(&(x), NULL)
  35. #define MUTEX_CLEANUP(x) pthread_mutex_destroy(&(x))
  36. #define MUTEX_LOCK(x) pthread_mutex_lock(&(x))
  37. #define MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x))
  38. #define THREAD_ID pthread_self( )
  39. void handle_error(const char *file, int lineno, const char *msg){
  40. fprintf(stderr, "** %s:%d %s\n", file, lineno, msg);
  41. ERR_print_errors_fp(stderr);
  42. /* exit(-1); */
  43. }
  44. /* This array will store all of the mutexes available to OpenSSL. */
  45. static MUTEX_TYPE *mutex_buf= NULL;
  46. static void locking_function(int mode, int n, const char * file, int line)
  47. {
  48. if (mode & CRYPTO_LOCK)
  49. MUTEX_LOCK(mutex_buf[n]);
  50. else
  51. MUTEX_UNLOCK(mutex_buf[n]);
  52. }
  53. static unsigned long id_function(void)
  54. {
  55. return ((unsigned long)THREAD_ID);
  56. }
  57. int thread_setup(void)
  58. {
  59. int i;
  60. mutex_buf = malloc(CRYPTO_num_locks( ) * sizeof(MUTEX_TYPE));
  61. if (!mutex_buf)
  62. return 0;
  63. for (i = 0; i < CRYPTO_num_locks( ); i++)
  64. MUTEX_SETUP(mutex_buf[i]);
  65. CRYPTO_set_id_callback(id_function);
  66. CRYPTO_set_locking_callback(locking_function);
  67. return 1;
  68. }
  69. int thread_cleanup(void)
  70. {
  71. int i;
  72. if (!mutex_buf)
  73. return 0;
  74. CRYPTO_set_id_callback(NULL);
  75. CRYPTO_set_locking_callback(NULL);
  76. for (i = 0; i < CRYPTO_num_locks( ); i++)
  77. MUTEX_CLEANUP(mutex_buf[i]);
  78. free(mutex_buf);
  79. mutex_buf = NULL;
  80. return 1;
  81. }