xref: /curl/lib/dllmain.c (revision 7860f575)
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 #ifdef USE_OPENSSL
28 #include <openssl/crypto.h>
29 #endif
30 
31 /* The fourth-to-last include */
32 #ifdef __CYGWIN__
33 #define WIN32_LEAN_AND_MEAN
34 #include <windows.h>
35 #ifdef _WIN32
36 #undef _WIN32
37 #endif
38 #endif
39 
40 /* The last 3 #include files should be in this order */
41 #include "curl_printf.h"
42 #include "curl_memory.h"
43 #include "memdebug.h"
44 
45 /* DllMain() must only be defined for Windows and Cygwin DLL builds. */
46 #if (defined(_WIN32) || defined(__CYGWIN__)) && !defined(CURL_STATICLIB)
47 
48 #if defined(USE_OPENSSL) && \
49     !defined(OPENSSL_IS_AWSLC) && \
50     !defined(OPENSSL_IS_BORINGSSL) && \
51     !defined(LIBRESSL_VERSION_NUMBER) && \
52     (OPENSSL_VERSION_NUMBER >= 0x10100000L)
53 #define PREVENT_OPENSSL_MEMLEAK
54 #endif
55 
56 #ifdef PREVENT_OPENSSL_MEMLEAK
57 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved);
DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)58 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
59 {
60   (void)hinstDLL;
61   (void)lpvReserved;
62 
63   switch(fdwReason) {
64   case DLL_PROCESS_ATTACH:
65     break;
66   case DLL_PROCESS_DETACH:
67     break;
68   case DLL_THREAD_ATTACH:
69     break;
70   case DLL_THREAD_DETACH:
71     /* Call OPENSSL_thread_stop to prevent a memory leak in case OpenSSL is
72        linked statically.
73        https://github.com/curl/curl/issues/12327#issuecomment-1826405944 */
74     OPENSSL_thread_stop();
75     break;
76   }
77   return TRUE;
78 }
79 #endif /* OpenSSL */
80 
81 #endif /* DLL build */
82