KiCad PCB EDA Suite
kicad_curl.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2015 Mark Roszko <mark.roszko@gmail.com>
5  * Copyright (C) 2016 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
6  * Copyright (C) 2015 KiCad Developers, see CHANGELOG.TXT for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 // kicad_curl.h must be included before wx headers, to avoid
27 // conflicts for some defines, at least on Windows
28 #include <kicad_curl/kicad_curl.h>
29 
30 #include <ki_mutex.h> // MUTEX and MUTLOCK
31 #include <ki_exception.h> // THROW_IO_ERROR
32 
33 
34 
35 // These are even more private than class members, and since there is only
36 // one instance of KICAD_CURL ever, these statics are hidden here to simplify the
37 // client (API) header file.
38 static volatile bool s_initialized;
39 
40 static MUTEX s_lock; // for s_initialized
41 
42 // Assume that on these platforms libcurl uses OpenSSL
43 #if defined(__linux__) || defined(__MINGW32__)
44 
45 #include <openssl/crypto.h>
46 
47 static MUTEX* s_crypto_locks;
48 
49 static void lock_callback( int mode, int type, const char* file, int line )
50 {
51  (void)file;
52  (void)line;
53 
54  wxASSERT( s_crypto_locks && unsigned( type ) < unsigned( CRYPTO_num_locks() ) );
55 
56  if( mode & CRYPTO_LOCK )
57  {
58  s_crypto_locks[ type ].lock();
59  }
60  else
61  {
62  s_crypto_locks[ type ].unlock();
63  }
64 }
65 
66 
67 static void init_locks()
68 {
69  s_crypto_locks = new MUTEX[ CRYPTO_num_locks() ];
70 
71  // From http://linux.die.net/man/3/crypto_set_id_callback:
72 
73  /*
74 
75  OpenSSL can safely be used in multi-threaded applications provided that at
76  least two callback functions are set, locking_function and threadid_func.
77 
78  locking_function(int mode, int n, const char *file, int line) is needed to
79  perform locking on shared data structures. (Note that OpenSSL uses a number
80  of global data structures that will be implicitly shared whenever multiple
81  threads use OpenSSL.) Multi-threaded applications will crash at random if it
82  is not set.
83 
84  threadid_func( CRYPTO_THREADID *id) is needed to record the
85  currently-executing thread's identifier into id. The implementation of this
86  callback should not fill in id directly, but should use
87  CRYPTO_THREADID_set_numeric() if thread IDs are numeric, or
88  CRYPTO_THREADID_set_pointer() if they are pointer-based. If the application
89  does not register such a callback using CRYPTO_THREADID_set_callback(), then
90  a default implementation is used - on Windows and BeOS this uses the
91  system's default thread identifying APIs, and on all other platforms it uses
92  the address of errno. The latter is satisfactory for thread-safety if and
93  only if the platform has a thread-local error number facility.
94 
95  Dick: "sounds like CRYPTO_THREADID_set_callback() is not mandatory on our
96  2 OpenSSL platforms."
97 
98  */
99 
100  CRYPTO_set_locking_callback( &lock_callback );
101 }
102 
103 
104 static void kill_locks()
105 {
106  CRYPTO_set_locking_callback( NULL );
107 
108  delete[] s_crypto_locks;
109 
110  s_crypto_locks = NULL;
111 }
112 
113 #else
114 
115 inline void init_locks() { /* dummy */ }
116 inline void kill_locks() { /* dummy */ }
117 
118 #endif
119 
122 static void at_terminate()
123 {
125 }
126 
127 
129 {
130  // We test s_initialized twice in an effort to avoid
131  // unnecessarily locking s_lock. This understands that the common case
132  // will not need to lock.
133  if( !s_initialized )
134  {
135  MUTLOCK lock( s_lock );
136 
137  if( !s_initialized )
138  {
139  if( curl_global_init( CURL_GLOBAL_ALL ) != CURLE_OK )
140  {
141  THROW_IO_ERROR( "curl_global_init() failed." );
142  }
143 
144  init_locks();
145 
146  //wxLogDebug( "Using %s", GetVersion() );
147 
148  s_initialized = true;
149  }
150  }
151 }
152 
153 
155 {
156  /*
157 
158  Calling MUTLOCK() from a static destructor will typically be bad, since the
159  s_lock may already have been statically destroyed itself leading to a boost
160  exception. (Remember C++ does not provide certain sequencing of static
161  destructor invocation.)
162 
163  To prevent this we test s_initialized twice, which ensures that the MUTLOCK
164  is only instantiated on the first call, which should be from
165  PGM_BASE::destroy() which is first called earlier than static destruction.
166  Then when called again from the actual PGM_BASE::~PGM_BASE() function,
167  MUTLOCK will not be instantiated because s_initialized will be false.
168 
169  */
170 
171  if( s_initialized )
172  {
173  MUTLOCK lock( s_lock );
174 
175  if( s_initialized )
176  {
177  curl_global_cleanup();
178 
179  kill_locks();
180 
181  atexit( &at_terminate );
182 
183  s_initialized = false;
184  }
185  }
186 }
187 
188 
190 {
191  if( !s_initialized )
192  Init();
193 
194  curl_version_info_data* info = curl_version_info( CURLVERSION_NOW );
195 
196  std::string res;
197 
198  if( info->version )
199  {
200  res += "libcurl version: " + std::string( info->version );
201  }
202 
203  res += " (";
204 
205  if( info->features & CURL_VERSION_SSL )
206  {
207  res += "with SSL - ";
208  res += std::string( info->ssl_version );
209  }
210  else
211  {
212  res += "without SSL";
213  }
214  res += ")";
215 
216  return res;
217 }
218 
219 std::string GetKicadCurlVersion()
220 {
221  return KICAD_CURL::GetVersion();
222 }
223 
224 std::string GetCurlLibVersion()
225 {
226  return LIBCURL_VERSION;
227 }
static void Cleanup()
Function Cleanup calls curl_global_cleanup for the application.
Definition: kicad_curl.cpp:154
static void Init()
Function Init calls curl_global_init for the application.
Definition: kicad_curl.cpp:128
boost::interprocess::interprocess_mutex MUTEX
Establish KiCad MUTEX choices here in this file: typedef MUTEX and typedef MUTLOCK.
Definition: ki_mutex.h:42
static void at_terminate()
At process termination, using atexit() keeps the CURL stuff out of the singletops and PGM_BASE...
Definition: kicad_curl.cpp:122
std::string GetKicadCurlVersion()
Definition: kicad_curl.cpp:219
std::string GetCurlLibVersion()
Definition: kicad_curl.cpp:224
static volatile bool s_initialized
Definition: kicad_curl.cpp:38
void kill_locks()
Definition: kicad_curl.cpp:116
boost::interprocess::scoped_lock< MUTEX > MUTLOCK
Definition: ki_mutex.h:43
static const char * GetVersion()
Function GetVersion wrapper for curl_version().
Definition: kicad_curl.h:93
void init_locks()
Definition: kicad_curl.cpp:115
static MUTEX s_lock
Definition: kicad_curl.cpp:40
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38
static std::string GetSimpleVersion()
Function GetSimpleVersion Reports back curl version only and SSL library support. ...
Definition: kicad_curl.cpp:189