psl.c 3.2 KB

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