2
0

psl.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2018, 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 https://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. #include "curl_setup.h"
  23. #include <curl/curl.h>
  24. #ifdef USE_LIBPSL
  25. #include "psl.h"
  26. #include "share.h"
  27. /* The last 3 #include files should be in this order */
  28. #include "curl_printf.h"
  29. #include "curl_memory.h"
  30. #include "memdebug.h"
  31. void Curl_psl_destroy(struct PslCache *pslcache)
  32. {
  33. if(pslcache->psl) {
  34. if(pslcache->dynamic)
  35. psl_free((psl_ctx_t *) pslcache->psl);
  36. pslcache->psl = NULL;
  37. pslcache->dynamic = FALSE;
  38. }
  39. }
  40. static time_t now_seconds(void)
  41. {
  42. struct curltime now = Curl_now();
  43. return now.tv_sec;
  44. }
  45. const psl_ctx_t *Curl_psl_use(struct Curl_easy *easy)
  46. {
  47. struct PslCache *pslcache = easy->psl;
  48. const psl_ctx_t *psl;
  49. time_t now;
  50. if(!pslcache)
  51. return NULL;
  52. Curl_share_lock(easy, CURL_LOCK_DATA_PSL, CURL_LOCK_ACCESS_SHARED);
  53. now = now_seconds();
  54. if(!pslcache->psl || pslcache->expires <= now) {
  55. /* Let a chance to other threads to do the job: avoids deadlock. */
  56. Curl_share_unlock(easy, CURL_LOCK_DATA_PSL);
  57. /* Update cache: this needs an exclusive lock. */
  58. Curl_share_lock(easy, CURL_LOCK_DATA_PSL, CURL_LOCK_ACCESS_SINGLE);
  59. /* Recheck in case another thread did the job. */
  60. now = now_seconds();
  61. if(!pslcache->psl || pslcache->expires <= now) {
  62. bool dynamic = FALSE;
  63. time_t expires = TIME_T_MAX;
  64. #if defined(PSL_VERSION_NUMBER) && PSL_VERSION_NUMBER >= 0x001000
  65. psl = psl_latest(NULL);
  66. dynamic = psl != NULL;
  67. /* Take care of possible time computation overflow. */
  68. expires = now < TIME_T_MAX - PSL_TTL? now + PSL_TTL: TIME_T_MAX;
  69. /* Only get the built-in PSL if we do not already have the "latest". */
  70. if(!psl && !pslcache->dynamic)
  71. #endif
  72. psl = psl_builtin();
  73. if(psl) {
  74. Curl_psl_destroy(pslcache);
  75. pslcache->psl = psl;
  76. pslcache->dynamic = dynamic;
  77. pslcache->expires = expires;
  78. }
  79. }
  80. Curl_share_unlock(easy, CURL_LOCK_DATA_PSL); /* Release exclusive lock. */
  81. Curl_share_lock(easy, CURL_LOCK_DATA_PSL, CURL_LOCK_ACCESS_SHARED);
  82. }
  83. psl = pslcache->psl;
  84. if(!psl)
  85. Curl_share_unlock(easy, CURL_LOCK_DATA_PSL);
  86. return psl;
  87. }
  88. void Curl_psl_release(struct Curl_easy *easy)
  89. {
  90. Curl_share_unlock(easy, CURL_LOCK_DATA_PSL);
  91. }
  92. #endif /* USE_LIBPSL */