1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2019, 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.haxx.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 ***************************************************************************/
22 #include "tool_setup.h"
23
24 #include <sys/stat.h>
25
26 #ifdef HAVE_SIGNAL_H
27 #include <signal.h>
28 #endif
29
30 #ifdef USE_NSS
31 #include <nspr.h>
32 #include <plarenas.h>
33 #endif
34
35 #define ENABLE_CURLX_PRINTF
36 /* use our own printf() functions */
37 #include "curlx.h"
38
39 #include "tool_cfgable.h"
40 #include "tool_convert.h"
41 #include "tool_doswin.h"
42 #include "tool_msgs.h"
43 #include "tool_operate.h"
44 #include "tool_panykey.h"
45 #include "tool_vms.h"
46 #include "tool_main.h"
47 #include "tool_libinfo.h"
48
49 /*
50 * This is low-level hard-hacking memory leak tracking and similar. Using
51 * the library level code from this client-side is ugly, but we do this
52 * anyway for convenience.
53 */
54 #include "memdebug.h" /* keep this as LAST include */
55
56 #ifdef __VMS
57 /*
58 * vms_show is a global variable, used in main() as parameter for
59 * function vms_special_exit() to allow proper curl tool exiting.
60 * Its value may be set in other tool_*.c source files thanks to
61 * forward declaration present in tool_vms.h
62 */
63 int vms_show = 0;
64 #endif
65
66 #ifdef __MINGW32__
67 /*
68 * There seems to be no way to escape "*" in command-line arguments with MinGW
69 * when command-line argument globbing is enabled under the MSYS shell, so turn
70 * it off.
71 */
72 int _CRT_glob = 0;
73 #endif /* __MINGW32__ */
74
75 /* if we build a static library for unit tests, there is no main() function */
76 #ifndef UNITTESTS
77
78 /*
79 * Ensure that file descriptors 0, 1 and 2 (stdin, stdout, stderr) are
80 * open before starting to run. Otherwise, the first three network
81 * sockets opened by curl could be used for input sources, downloaded data
82 * or error logs as they will effectively be stdin, stdout and/or stderr.
83 */
main_checkfds(void)84 static void main_checkfds(void)
85 {
86 #ifdef HAVE_PIPE
87 int fd[2] = { STDIN_FILENO, STDIN_FILENO };
88 while(fd[0] == STDIN_FILENO ||
89 fd[0] == STDOUT_FILENO ||
90 fd[0] == STDERR_FILENO ||
91 fd[1] == STDIN_FILENO ||
92 fd[1] == STDOUT_FILENO ||
93 fd[1] == STDERR_FILENO)
94 if(pipe(fd) < 0)
95 return; /* Out of handles. This isn't really a big problem now, but
96 will be when we try to create a socket later. */
97 close(fd[0]);
98 close(fd[1]);
99 #endif
100 }
101
102 #ifdef CURLDEBUG
memory_tracking_init(void)103 static void memory_tracking_init(void)
104 {
105 char *env;
106 /* if CURL_MEMDEBUG is set, this starts memory tracking message logging */
107 env = curlx_getenv("CURL_MEMDEBUG");
108 if(env) {
109 /* use the value as file name */
110 char fname[CURL_MT_LOGFNAME_BUFSIZE];
111 if(strlen(env) >= CURL_MT_LOGFNAME_BUFSIZE)
112 env[CURL_MT_LOGFNAME_BUFSIZE-1] = '\0';
113 strcpy(fname, env);
114 curl_free(env);
115 curl_dbg_memdebug(fname);
116 /* this weird stuff here is to make curl_free() get called
117 before curl_memdebug() as otherwise memory tracking will
118 log a free() without an alloc! */
119 }
120 /* if CURL_MEMLIMIT is set, this enables fail-on-alloc-number-N feature */
121 env = curlx_getenv("CURL_MEMLIMIT");
122 if(env) {
123 char *endptr;
124 long num = strtol(env, &endptr, 10);
125 if((endptr != env) && (endptr == env + strlen(env)) && (num > 0))
126 curl_dbg_memlimit(num);
127 curl_free(env);
128 }
129 }
130 #else
131 # define memory_tracking_init() Curl_nop_stmt
132 #endif
133
134 /*
135 * This is the main global constructor for the app. Call this before
136 * _any_ libcurl usage. If this fails, *NO* libcurl functions may be
137 * used, or havoc may be the result.
138 */
main_init(struct GlobalConfig * config)139 static CURLcode main_init(struct GlobalConfig *config)
140 {
141 CURLcode result = CURLE_OK;
142
143 #if defined(__DJGPP__) || defined(__GO32__)
144 /* stop stat() wasting time */
145 _djstat_flags |= _STAT_INODE | _STAT_EXEC_MAGIC | _STAT_DIRSIZE;
146 #endif
147
148 /* Initialise the global config */
149 config->showerror = -1; /* Will show errors */
150 config->errors = stderr; /* Default errors to stderr */
151 config->styled_output = TRUE; /* enable detection */
152
153 /* Allocate the initial operate config */
154 config->first = config->last = malloc(sizeof(struct OperationConfig));
155 if(config->first) {
156 /* Perform the libcurl initialization */
157 result = curl_global_init(CURL_GLOBAL_DEFAULT);
158 if(!result) {
159 /* Get information about libcurl */
160 result = get_libcurl_info();
161
162 if(!result) {
163 /* Get a curl handle to use for all forthcoming curl transfers */
164 config->easy = curl_easy_init();
165 if(config->easy) {
166 /* Initialise the config */
167 config_init(config->first);
168 config->first->easy = config->easy;
169 config->first->global = config;
170 }
171 else {
172 helpf(stderr, "error initializing curl easy handle\n");
173 result = CURLE_FAILED_INIT;
174 free(config->first);
175 }
176 }
177 else {
178 helpf(stderr, "error retrieving curl library information\n");
179 free(config->first);
180 }
181 }
182 else {
183 helpf(stderr, "error initializing curl library\n");
184 free(config->first);
185 }
186 }
187 else {
188 helpf(stderr, "error initializing curl\n");
189 result = CURLE_FAILED_INIT;
190 }
191
192 return result;
193 }
194
free_globalconfig(struct GlobalConfig * config)195 static void free_globalconfig(struct GlobalConfig *config)
196 {
197 Curl_safefree(config->trace_dump);
198
199 if(config->errors_fopened && config->errors)
200 fclose(config->errors);
201 config->errors = NULL;
202
203 if(config->trace_fopened && config->trace_stream)
204 fclose(config->trace_stream);
205 config->trace_stream = NULL;
206
207 Curl_safefree(config->libcurl);
208 }
209
210 /*
211 * This is the main global destructor for the app. Call this after
212 * _all_ libcurl usage is done.
213 */
main_free(struct GlobalConfig * config)214 static void main_free(struct GlobalConfig *config)
215 {
216 /* Cleanup the easy handle */
217 curl_easy_cleanup(config->easy);
218 config->easy = NULL;
219
220 /* Main cleanup */
221 curl_global_cleanup();
222 convert_cleanup();
223 metalink_cleanup();
224 #ifdef USE_NSS
225 if(PR_Initialized()) {
226 /* prevent valgrind from reporting still reachable mem from NSRP arenas */
227 PL_ArenaFinish();
228 /* prevent valgrind from reporting possibly lost memory (fd cache, ...) */
229 PR_Cleanup();
230 }
231 #endif
232 free_globalconfig(config);
233
234 /* Free the config structures */
235 config_free(config->last);
236 config->first = NULL;
237 config->last = NULL;
238 }
239
240 #ifdef _WIN32
241 /* TerminalSettings for Windows */
242 static struct TerminalSettings {
243 HANDLE hStdOut;
244 DWORD dwOutputMode;
245 } TerminalSettings;
246
configure_terminal(void)247 static void configure_terminal(void)
248 {
249 /*
250 * If we're running Windows, enable VT output.
251 * Note: VT mode flag can be set on any version of Windows, but VT
252 * processing only performed on Win10 >= Creators Update)
253 */
254
255 /* Define the VT flags in case we're building with an older SDK */
256 #ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
257 #define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
258 #endif
259
260 memset(&TerminalSettings, 0, sizeof(TerminalSettings));
261
262 /* Enable VT output */
263 TerminalSettings.hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
264 if((TerminalSettings.hStdOut != INVALID_HANDLE_VALUE)
265 && (GetConsoleMode(TerminalSettings.hStdOut,
266 &TerminalSettings.dwOutputMode))) {
267 SetConsoleMode(TerminalSettings.hStdOut,
268 TerminalSettings.dwOutputMode
269 | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
270 }
271 }
272 #else
273 #define configure_terminal()
274 #endif
275
restore_terminal(void)276 static void restore_terminal(void)
277 {
278 #ifdef _WIN32
279 /* Restore Console output mode and codepage to whatever they were
280 * when Curl started */
281 SetConsoleMode(TerminalSettings.hStdOut, TerminalSettings.dwOutputMode);
282 #endif
283 }
284
285 /*
286 ** curl tool main function.
287 */
main(int argc,char * argv[])288 int main(int argc, char *argv[])
289 {
290 CURLcode result = CURLE_OK;
291 struct GlobalConfig global;
292 memset(&global, 0, sizeof(global));
293
294 /* Perform any platform-specific terminal configuration */
295 configure_terminal();
296
297 main_checkfds();
298
299 #if defined(HAVE_SIGNAL) && defined(SIGPIPE)
300 (void)signal(SIGPIPE, SIG_IGN);
301 #endif
302
303 /* Initialize memory tracking */
304 memory_tracking_init();
305
306 /* Initialize the curl library - do not call any libcurl functions before
307 this point */
308 result = main_init(&global);
309
310 #ifdef WIN32
311 /* Undocumented diagnostic option to list the full paths of all loaded
312 modules, regardless of whether or not initialization succeeded. */
313 if(argc == 2 && !strcmp(argv[1], "--dump-module-paths")) {
314 struct curl_slist *item, *head = GetLoadedModulePaths();
315 for(item = head; item; item = item->next) {
316 printf("%s\n", item->data);
317 }
318 curl_slist_free_all(head);
319 if(!result)
320 main_free(&global);
321 }
322 else
323 #endif /* WIN32 */
324 if(!result) {
325 /* Start our curl operation */
326 result = operate(&global, argc, argv);
327
328 #ifdef __SYMBIAN32__
329 if(global.showerror)
330 tool_pressanykey();
331 #endif
332
333 /* Perform the main cleanup */
334 main_free(&global);
335 }
336
337 /* Return the terminal to its original state */
338 restore_terminal();
339
340 #ifdef __NOVELL_LIBC__
341 if(getenv("_IN_NETWARE_BASH_") == NULL)
342 tool_pressanykey();
343 #endif
344
345 #ifdef __VMS
346 vms_special_exit(result, vms_show);
347 #else
348 return (int)result;
349 #endif
350 }
351
352 #endif /* ndef UNITTESTS */
353