aboutsummaryrefslogtreecommitdiff
path: root/src/lib/init.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/init.c')
-rw-r--r--src/lib/init.c156
1 files changed, 156 insertions, 0 deletions
diff --git a/src/lib/init.c b/src/lib/init.c
new file mode 100644
index 00000000..d7a6faa1
--- /dev/null
+++ b/src/lib/init.c
@@ -0,0 +1,156 @@
1#include "init.h"
2
3
4#ifdef MHD_HTTPS_REQUIRE_GRYPT
5#if defined(HTTPS_SUPPORT) && GCRYPT_VERSION_NUMBER < 0x010600
6#if defined(MHD_USE_POSIX_THREADS)
7GCRY_THREAD_OPTION_PTHREAD_IMPL;
8#elif defined(MHD_W32_MUTEX_)
9
10static int
11gcry_w32_mutex_init (void **ppmtx)
12{
13 *ppmtx = malloc (sizeof (MHD_mutex_));
14
15 if (NULL == *ppmtx)
16 return ENOMEM;
17 if (!MHD_mutex_init_ ((MHD_mutex_*)*ppmtx))
18 {
19 free (*ppmtx);
20 *ppmtx = NULL;
21 return EPERM;
22 }
23
24 return 0;
25}
26
27
28static int
29gcry_w32_mutex_destroy (void **ppmtx)
30{
31 int res = (MHD_mutex_destroy_ ((MHD_mutex_*)*ppmtx)) ? 0 : EINVAL;
32 free (*ppmtx);
33 return res;
34}
35
36
37static int
38gcry_w32_mutex_lock (void **ppmtx)
39{
40 return MHD_mutex_lock_ ((MHD_mutex_*)*ppmtx) ? 0 : EINVAL;
41}
42
43
44static int
45gcry_w32_mutex_unlock (void **ppmtx)
46{
47 return MHD_mutex_unlock_ ((MHD_mutex_*)*ppmtx) ? 0 : EINVAL;
48}
49
50
51static struct gcry_thread_cbs gcry_threads_w32 = {
52 (GCRY_THREAD_OPTION_USER | (GCRY_THREAD_OPTION_VERSION << 8)),
53 NULL, gcry_w32_mutex_init, gcry_w32_mutex_destroy,
54 gcry_w32_mutex_lock, gcry_w32_mutex_unlock,
55 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
56
57#endif /* defined(MHD_W32_MUTEX_) */
58#endif /* HTTPS_SUPPORT && GCRYPT_VERSION_NUMBER < 0x010600 */
59#endif /* MHD_HTTPS_REQUIRE_GRYPT */
60
61
62#ifndef _AUTOINIT_FUNCS_ARE_SUPPORTED
63
64/**
65 * Track global initialisation
66 */
67volatile int global_init_count = 0;
68#ifdef MHD_MUTEX_STATIC_DEFN_INIT_
69/**
70 * Global initialisation mutex
71 */
72MHD_MUTEX_STATIC_DEFN_INIT_(global_init_mutex_);
73#endif /* MHD_MUTEX_STATIC_DEFN_INIT_ */
74
75
76/**
77 * Check whether global initialisation was performed
78 * and call initialiser if necessary.
79 */
80void
81MHD_check_global_init_ (void)
82{
83#ifdef MHD_MUTEX_STATIC_DEFN_INIT_
84 MHD_mutex_lock_chk_(&global_init_mutex_);
85#endif /* MHD_MUTEX_STATIC_DEFN_INIT_ */
86 if (0 == global_init_count++)
87 MHD_init ();
88#ifdef MHD_MUTEX_STATIC_DEFN_INIT_
89 MHD_mutex_unlock_chk_(&global_init_mutex_);
90#endif /* MHD_MUTEX_STATIC_DEFN_INIT_ */
91}
92
93
94/**
95 * Initialize do setup work.
96 */
97void
98MHD_init(void)
99{
100#if defined(_WIN32) && ! defined(__CYGWIN__)
101 WSADATA wsd;
102#endif /* _WIN32 && ! __CYGWIN__ */
103
104 if (NULL == mhd_panic)
105 mhd_panic = &mhd_panic_std;
106
107#if defined(_WIN32) && ! defined(__CYGWIN__)
108 if (0 != WSAStartup(MAKEWORD(2, 2), &wsd))
109 MHD_PANIC (_("Failed to initialize winsock\n"));
110 mhd_winsock_inited_ = 1;
111 if (2 != LOBYTE(wsd.wVersion) && 2 != HIBYTE(wsd.wVersion))
112 MHD_PANIC (_("Winsock version 2.2 is not available\n"));
113#endif
114#ifdef HTTPS_SUPPORT
115#ifdef MHD_HTTPS_REQUIRE_GRYPT
116#if GCRYPT_VERSION_NUMBER < 0x010600
117#if defined(MHD_USE_POSIX_THREADS)
118 if (0 != gcry_control (GCRYCTL_SET_THREAD_CBS,
119 &gcry_threads_pthread))
120 MHD_PANIC (_("Failed to initialise multithreading in libgcrypt\n"));
121#elif defined(MHD_W32_MUTEX_)
122 if (0 != gcry_control (GCRYCTL_SET_THREAD_CBS,
123 &gcry_threads_w32))
124 MHD_PANIC (_("Failed to initialise multithreading in libgcrypt\n"));
125#endif /* defined(MHD_W32_MUTEX_) */
126 gcry_check_version (NULL);
127#else
128 if (NULL == gcry_check_version ("1.6.0"))
129 MHD_PANIC (_("libgcrypt is too old. MHD was compiled for libgcrypt 1.6.0 or newer\n"));
130#endif
131#endif /* MHD_HTTPS_REQUIRE_GRYPT */
132 gnutls_global_init ();
133#endif /* HTTPS_SUPPORT */
134 MHD_monotonic_sec_counter_init();
135#ifdef HAVE_FREEBSD_SENDFILE
136 MHD_conn_init_static_ ();
137#endif /* HAVE_FREEBSD_SENDFILE */
138}
139
140
141void
142MHD_fini(void)
143{
144#ifdef HTTPS_SUPPORT
145 gnutls_global_deinit ();
146#endif /* HTTPS_SUPPORT */
147#if defined(_WIN32) && ! defined(__CYGWIN__)
148 if (mhd_winsock_inited_)
149 WSACleanup();
150#endif
151 MHD_monotonic_sec_counter_finish();
152}
153
154#ifdef _AUTOINIT_FUNCS_ARE_SUPPORTED
155_SET_INIT_AND_DEINIT_FUNCS(MHD_init, MHD_fini);
156#endif /* _AUTOINIT_FUNCS_ARE_SUPPORTED */