1 /* init.c - Various initializations
2 * Copyright (C) 2007 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * This file is free software; you can redistribute it and/or modify
7 * it under the terms of either
9 * - the GNU Lesser General Public License as published by the Free
10 * Software Foundation; either version 3 of the License, or (at
11 * your option) any later version.
15 * - the GNU General Public License as published by the Free
16 * Software Foundation; either version 2 of the License, or (at
17 * your option) any later version.
19 * or both in parallel, as here.
21 * This file is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, see <https://www.gnu.org/licenses/>.
32 #ifdef HAVE_W32_SYSTEM
33 # ifdef HAVE_WINSOCK2_H
34 # include <winsock2.h>
38 #ifdef HAVE_W32CE_SYSTEM
39 # include <assuan.h> /* For _assuan_w32ce_finish_pipe. */
47 /* This object is used to register memory cleanup functions.
48 Technically they are not needed but they can avoid frequent
49 questions about un-released memory. Note that we use the system
50 malloc and not any wrappers. */
51 struct mem_cleanup_item_s;
52 typedef struct mem_cleanup_item_s *mem_cleanup_item_t;
54 struct mem_cleanup_item_s
56 mem_cleanup_item_t next;
60 static mem_cleanup_item_t mem_cleanup_list;
63 /* The default error source of the application. This is different
64 from GPG_ERR_SOURCE_DEFAULT in that it does not depend on the
65 source file and thus is usable in code shared by applications.
66 Note that we need to initialize it because otherwise some linkers
67 (OS X at least) won't find the symbol when linking the t-*.c
69 gpg_err_source_t default_errsource = 0;
72 #ifdef HAVE_W32CE_SYSTEM
73 static void parse_std_file_handles (int *argcp, char ***argvp);
77 /* The sshd on CE swallows some of the command output. Sleeping a
78 while usually helps. */
81 #endif /*HAVE_W32CE_SYSTEM*/
84 static void prepare_w32_commandline (int *argcp, char ***argvp);
85 #endif /*HAVE_W32_SYSTEM*/
90 run_mem_cleanup (void)
92 mem_cleanup_item_t next;
94 while (mem_cleanup_list)
96 next = mem_cleanup_list->next;
97 mem_cleanup_list->func ();
98 free (mem_cleanup_list);
99 mem_cleanup_list = next;
105 register_mem_cleanup_func (void (*func)(void))
107 mem_cleanup_item_t item;
109 for (item = mem_cleanup_list; item; item = item->next)
110 if (item->func == func)
111 return; /* Function has already been registered. */
113 item = malloc (sizeof *item);
117 item->next = mem_cleanup_list;
118 mem_cleanup_list = item;
123 /* If STRING is not NULL write string to es_stdout or es_stderr. MODE
124 must be 1 or 2. If STRING is NULL flush the respective stream. */
126 writestring_via_estream (int mode, const char *string)
128 if (mode == 1 || mode == 2)
131 return es_fputs (string, mode == 1? es_stdout : es_stderr);
133 return es_fflush (mode == 1? es_stdout : es_stderr);
140 /* This function should be the first called after main. */
142 early_system_init (void)
147 /* This function is to be used early at program startup to make sure
148 that some subsystems are initialized. This is in particular
149 important for W32 to initialize the sockets so that our socket
150 emulation code used directly as well as in libassuan may be used.
151 It should best be called before any I/O is done so that setup
152 required for logging is ready. ARGCP and ARGVP are the addresses
153 of the parameters given to main. This function may modify them.
155 This function should be called only via the macro
156 init_common_subsystems.
158 CAUTION: This might be called while running suid(root). */
160 _init_common_subsystems (gpg_err_source_t errsource, int *argcp, char ***argvp)
162 /* Store the error source in a global variable. */
163 default_errsource = errsource;
165 atexit (run_mem_cleanup);
167 /* Try to auto set the character set. */
168 set_native_charset (NULL);
170 #ifdef HAVE_W32_SYSTEM
171 /* For W32 we need to initialize the socket layer. This is because
172 we use recv and send in libassuan as well as at some other
177 WSAStartup (0x202, &wsadat);
181 #ifdef HAVE_W32CE_SYSTEM
182 /* Register the sleep exit function before the estream init so that
183 the sleep will be called after the estream registered atexit
184 function which flushes the left open estream streams and in
185 particular es_stdout. */
186 atexit (sleep_on_exit);
189 if (!gcry_check_version (NEED_LIBGCRYPT_VERSION))
191 log_fatal (_("%s is too old (need %s, have %s)\n"), "libgcrypt",
192 NEED_LIBGCRYPT_VERSION, gcry_check_version (NULL));
195 /* Initialize the Estream library. */
197 gpgrt_set_alloc_func (gcry_realloc);
199 #ifdef HAVE_W32CE_SYSTEM
200 /* Special hack for Windows CE: We extract some options from arg
201 to setup the standard handles. */
202 parse_std_file_handles (argcp, argvp);
205 #ifdef HAVE_W32_SYSTEM
206 /* We want gettext to always output UTF-8 and we put the console in
208 gettext_use_utf8 (1);
209 if (!SetConsoleCP (CP_UTF8) || !SetConsoleOutputCP (CP_UTF8))
211 log_info ("SetConsoleCP failed: %s\n", w32_strerror (-1));
212 log_info ("Warning: Garbled console data possible\n");
216 /* Access the standard estreams as early as possible. If we don't
217 do this the original stdio streams may have been closed when
218 _es_get_std_stream is first use and in turn it would connect to
222 for (i=0; i < 3; i++)
223 (void)_gpgrt_get_std_stream (i);
226 /* --version et al shall use estream as well. */
227 gpgrt_set_usage_outfnc (writestring_via_estream);
229 /* Register our string mapper with gpgrt. */
230 gpgrt_set_fixed_string_mapper (map_static_macro_string);
232 /* Logging shall use the standard socket directory as fallback. */
233 log_set_socket_dir_cb (gnupg_socketdir);
236 /* For Standard Windows we use our own parser for the command line
237 * so that we can return an array of utf-8 encoded strings. */
238 prepare_w32_commandline (argcp, argvp);
248 /* WindowsCE uses a very strange way of handling the standard streams.
249 There is a function SetStdioPath to associate a standard stream
250 with a file or a device but what we really want is to use pipes as
251 standard streams. Despite that we implement pipes using a device,
252 we would have some limitations on the number of open pipes due to
253 the 3 character limit of device file name. Thus we don't take this
254 path. Another option would be to install a file system driver with
255 support for pipes; this would allow us to get rid of the device
256 name length limitation. However, with GnuPG we can get away be
257 redefining the standard streams and passing the handles to be used
258 on the command line. This has also the advantage that it makes
259 creating a process much easier and does not require the
260 SetStdioPath set and restore game. The caller needs to pass the
261 rendezvous ids using up to three options:
263 -&S0=<rvid> -&S1=<rvid> -&S2=<rvid>
265 They are all optional but they must be the first arguments on the
266 command line. Parsing stops as soon as an invalid option is found.
267 These rendezvous ids are then used to finish the pipe creation.*/
268 #ifdef HAVE_W32CE_SYSTEM
270 parse_std_file_handles (int *argcp, char ***argvp)
273 char **argv = *argvp;
282 for (argc--, argv++; argc; argc--, argv++)
285 if (*s == '-' && s[1] == '&' && s[2] == 'S'
286 && (s[3] == '0' || s[3] == '1' || s[3] == '2')
288 && (strchr ("-01234567890", s[5]) || !strcmp (s+5, "null")))
291 fd = ASSUAN_INVALID_FD;
293 fd = _assuan_w32ce_finish_pipe (atoi (s+5), s[3] != '0');
294 _es_set_std_fd (s[3] - '0', (int)fd);
308 for (i=1; i < argc; i++)
309 argv[i] = argv[i + fixup];
310 for (; i < argc + fixup; i++)
316 #endif /*HAVE_W32CE_SYSTEM*/
319 /* For Windows we need to parse the command line so that we can
320 * provide an UTF-8 encoded argv. If there is any Unicode character
321 * we return a new array but if there is no Unicode character we do
323 #ifdef HAVE_W32_SYSTEM
325 prepare_w32_commandline (int *r_argc, char ***r_argv)
327 const wchar_t *wcmdline, *ws;
332 int i, globing, itemsalloced;
334 s = gpgrt_strusage (95);
335 globing = (s && *s == '1');
337 wcmdline = GetCommandLineW ();
340 log_error ("GetCommandLineW failed\n");
346 /* If globbing is not enabled we use our own parser only if
347 * there are any non-ASCII characters. */
348 for (ws=wcmdline; *ws; ws++)
352 return; /* No Unicode - return directly. */
355 cmdline = wchar_to_utf8 (wcmdline);
358 log_error ("parsing command line failed: %s\n", strerror (errno));
361 gpgrt_annotate_leaked_object (cmdline);
363 argv = w32_parse_commandline (cmdline, globing, &argc, &itemsalloced);
366 log_error ("parsing command line failed: %s\n", "internal error");
369 gpgrt_annotate_leaked_object (argv);
372 for (i=0; i < argc; i++)
373 gpgrt_annotate_leaked_object (argv[i]);
378 #endif /*HAVE_W32_SYSTEM*/