• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
25 #include "curl_setup.h"
26 
27 #include <curl/curl.h>
28 
29 #ifdef USE_LIBPSL
30 
31 #include "psl.h"
32 #include "share.h"
33 
34 /* The last 3 #include files should be in this order */
35 #include "curl_printf.h"
36 #include "curl_memory.h"
37 #include "memdebug.h"
38 
Curl_psl_destroy(struct PslCache * pslcache)39 void Curl_psl_destroy(struct PslCache *pslcache)
40 {
41   if(pslcache->psl) {
42     if(pslcache->dynamic)
43       psl_free((psl_ctx_t *) pslcache->psl);
44     pslcache->psl = NULL;
45     pslcache->dynamic = FALSE;
46   }
47 }
48 
now_seconds(void)49 static time_t now_seconds(void)
50 {
51   struct curltime now = Curl_now();
52 
53   return now.tv_sec;
54 }
55 
Curl_psl_use(struct Curl_easy * easy)56 const psl_ctx_t *Curl_psl_use(struct Curl_easy *easy)
57 {
58   struct PslCache *pslcache = easy->psl;
59   const psl_ctx_t *psl;
60   time_t now;
61 
62   if(!pslcache)
63     return NULL;
64 
65   Curl_share_lock(easy, CURL_LOCK_DATA_PSL, CURL_LOCK_ACCESS_SHARED);
66   now = now_seconds();
67   if(!pslcache->psl || pslcache->expires <= now) {
68     /* Let a chance to other threads to do the job: avoids deadlock. */
69     Curl_share_unlock(easy, CURL_LOCK_DATA_PSL);
70 
71     /* Update cache: this needs an exclusive lock. */
72     Curl_share_lock(easy, CURL_LOCK_DATA_PSL, CURL_LOCK_ACCESS_SINGLE);
73 
74     /* Recheck in case another thread did the job. */
75     now = now_seconds();
76     if(!pslcache->psl || pslcache->expires <= now) {
77       bool dynamic = FALSE;
78       time_t expires = TIME_T_MAX;
79 
80 #if defined(PSL_VERSION_NUMBER) && PSL_VERSION_NUMBER >= 0x001000
81       psl = psl_latest(NULL);
82       dynamic = psl != NULL;
83       /* Take care of possible time computation overflow. */
84       expires = now < TIME_T_MAX - PSL_TTL? now + PSL_TTL: TIME_T_MAX;
85 
86       /* Only get the built-in PSL if we do not already have the "latest". */
87       if(!psl && !pslcache->dynamic)
88 #endif
89 
90         psl = psl_builtin();
91 
92       if(psl) {
93         Curl_psl_destroy(pslcache);
94         pslcache->psl = psl;
95         pslcache->dynamic = dynamic;
96         pslcache->expires = expires;
97       }
98     }
99     Curl_share_unlock(easy, CURL_LOCK_DATA_PSL);  /* Release exclusive lock. */
100     Curl_share_lock(easy, CURL_LOCK_DATA_PSL, CURL_LOCK_ACCESS_SHARED);
101   }
102   psl = pslcache->psl;
103   if(!psl)
104     Curl_share_unlock(easy, CURL_LOCK_DATA_PSL);
105   return psl;
106 }
107 
Curl_psl_release(struct Curl_easy * easy)108 void Curl_psl_release(struct Curl_easy *easy)
109 {
110   Curl_share_unlock(easy, CURL_LOCK_DATA_PSL);
111 }
112 
113 #endif /* USE_LIBPSL */
114