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