1 /*
2 This file is part of libmicrohttpd
3 Copyright (C) 2007-2018 Daniel Pittman and Christian Grothoff
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /**
21 * @file lib/init.c
22 * @brief initialization routines
23 * @author Christian Grothoff
24 */
25 #include "internal.h"
26 #include "init.h"
27
28
29 #ifndef _AUTOINIT_FUNCS_ARE_SUPPORTED
30 /**
31 * Track global initialisation
32 */
33 volatile unsigned int global_init_count = 0;
34
35 #ifdef MHD_MUTEX_STATIC_DEFN_INIT_
36 /**
37 * Global initialisation mutex
38 */
39 MHD_MUTEX_STATIC_DEFN_INIT_ (global_init_mutex_);
40 #endif /* MHD_MUTEX_STATIC_DEFN_INIT_ */
41
42 #endif
43
44 #if defined(_WIN32) && ! defined(__CYGWIN__)
45 /**
46 * Track initialization of winsock
47 */
48 static int mhd_winsock_inited_ = 0;
49 #endif
50
51 /**
52 * Default implementation of the panic function,
53 * prints an error message and aborts.
54 *
55 * @param cls unused
56 * @param file name of the file with the problem
57 * @param line line number with the problem
58 * @param reason error message with details
59 */
60 static void
mhd_panic_std(void * cls,const char * file,unsigned int line,const char * reason)61 mhd_panic_std (void *cls,
62 const char *file,
63 unsigned int line,
64 const char *reason)
65 {
66 (void) cls; /* Mute compiler warning. */
67 #ifdef HAVE_MESSAGES
68 fprintf (stderr,
69 _ ("Fatal error in GNU libmicrohttpd %s:%u: %s\n"),
70 file,
71 line,
72 reason);
73 #else /* ! HAVE_MESSAGES */
74 (void) file; /* Mute compiler warning. */
75 (void) line; /* Mute compiler warning. */
76 (void) reason; /* Mute compiler warning. */
77 #endif
78 abort ();
79 }
80
81
82 /**
83 * Globally initialize library.
84 */
85 void
MHD_init(void)86 MHD_init (void)
87 {
88 #if defined(_WIN32) && ! defined(__CYGWIN__)
89 WSADATA wsd;
90 #endif /* _WIN32 && ! __CYGWIN__ */
91
92 if (NULL == mhd_panic)
93 mhd_panic = &mhd_panic_std;
94
95 #if defined(_WIN32) && ! defined(__CYGWIN__)
96 if (0 != WSAStartup (MAKEWORD (2, 2),
97 &wsd))
98 MHD_PANIC (_ ("Failed to initialize winsock.\n"));
99 mhd_winsock_inited_ = 1;
100 if ( (2 != LOBYTE (wsd.wVersion)) &&
101 (2 != HIBYTE (wsd.wVersion)) )
102 MHD_PANIC (_ ("Winsock version 2.2 is not available.\n"));
103 #endif
104 MHD_monotonic_sec_counter_init ();
105 #ifdef HAVE_FREEBSD_SENDFILE
106 MHD_conn_init_static_ ();
107 #endif /* HAVE_FREEBSD_SENDFILE */
108 }
109
110
111 /**
112 * Global teardown work.
113 */
114 void
MHD_fini(void)115 MHD_fini (void)
116 {
117 #if defined(_WIN32) && ! defined(__CYGWIN__)
118 if (mhd_winsock_inited_)
119 WSACleanup ();
120 #endif
121 MHD_monotonic_sec_counter_finish ();
122 }
123
124
125 #ifdef _AUTOINIT_FUNCS_ARE_SUPPORTED
126
127 _SET_INIT_AND_DEINIT_FUNCS (MHD_init, MHD_fini);
128
129 #else
130
131 /**
132 * Check whether global initialisation was performed
133 * and call initialiser if necessary.
134 */
135 void
MHD_check_global_init_(void)136 MHD_check_global_init_ (void)
137 {
138 #ifdef MHD_MUTEX_STATIC_DEFN_INIT_
139 MHD_mutex_lock_chk_ (&global_init_mutex_);
140 #endif /* MHD_MUTEX_STATIC_DEFN_INIT_ */
141 if (0 == global_init_count++)
142 MHD_init ();
143 #ifdef MHD_MUTEX_STATIC_DEFN_INIT_
144 MHD_mutex_unlock_chk_ (&global_init_mutex_);
145 #endif /* MHD_MUTEX_STATIC_DEFN_INIT_ */
146 }
147
148
149 #endif
150