blob: 434979dea3de5767f5927de7a3c77331b73bd6d6 [file] [log] [blame]
xf.li6c8fc1e2023-08-12 00:11:09 -07001/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2022, 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#include "tool_setup.h"
25
26#include <sys/stat.h>
27
28#ifdef WIN32
29#include <tchar.h>
30#endif
31
32#ifdef HAVE_SIGNAL_H
33#include <signal.h>
34#endif
35
36#ifdef HAVE_FCNTL_H
37#include <fcntl.h>
38#endif
39
40#ifdef USE_NSS
41#include <nspr.h>
42#include <plarenas.h>
43#endif
44
45#define ENABLE_CURLX_PRINTF
46/* use our own printf() functions */
47#include "curlx.h"
48
49#include "tool_cfgable.h"
50#include "tool_doswin.h"
51#include "tool_msgs.h"
52#include "tool_operate.h"
53#include "tool_panykey.h"
54#include "tool_vms.h"
55#include "tool_main.h"
56#include "tool_libinfo.h"
57
58/*
59 * This is low-level hard-hacking memory leak tracking and similar. Using
60 * the library level code from this client-side is ugly, but we do this
61 * anyway for convenience.
62 */
63#include "memdebug.h" /* keep this as LAST include */
64
65#ifdef __VMS
66/*
67 * vms_show is a global variable, used in main() as parameter for
68 * function vms_special_exit() to allow proper curl tool exiting.
69 * Its value may be set in other tool_*.c source files thanks to
70 * forward declaration present in tool_vms.h
71 */
72int vms_show = 0;
73#endif
74
75#ifdef __MINGW32__
76/*
77 * There seems to be no way to escape "*" in command-line arguments with MinGW
78 * when command-line argument globbing is enabled under the MSYS shell, so turn
79 * it off.
80 */
81int _CRT_glob = 0;
82#endif /* __MINGW32__ */
83
84/* if we build a static library for unit tests, there is no main() function */
85#ifndef UNITTESTS
86
87#if defined(HAVE_PIPE) && defined(HAVE_FCNTL)
88/*
89 * Ensure that file descriptors 0, 1 and 2 (stdin, stdout, stderr) are
90 * open before starting to run. Otherwise, the first three network
91 * sockets opened by curl could be used for input sources, downloaded data
92 * or error logs as they will effectively be stdin, stdout and/or stderr.
93 *
94 * fcntl's F_GETFD instruction returns -1 if the file descriptor is closed,
95 * otherwise it returns "the file descriptor flags (which typically can only
96 * be FD_CLOEXEC, which is not set here).
97 */
98static int main_checkfds(void)
99{
100 int fd[2];
101 while((fcntl(STDIN_FILENO, F_GETFD) == -1) ||
102 (fcntl(STDOUT_FILENO, F_GETFD) == -1) ||
103 (fcntl(STDERR_FILENO, F_GETFD) == -1))
104 if(pipe(fd))
105 return 1;
106 return 0;
107}
108#else
109#define main_checkfds() 0
110#endif
111
112#ifdef CURLDEBUG
113static void memory_tracking_init(void)
114{
115 char *env;
116 /* if CURL_MEMDEBUG is set, this starts memory tracking message logging */
117 env = curlx_getenv("CURL_MEMDEBUG");
118 if(env) {
119 /* use the value as file name */
120 char fname[CURL_MT_LOGFNAME_BUFSIZE];
121 if(strlen(env) >= CURL_MT_LOGFNAME_BUFSIZE)
122 env[CURL_MT_LOGFNAME_BUFSIZE-1] = '\0';
123 strcpy(fname, env);
124 curl_free(env);
125 curl_dbg_memdebug(fname);
126 /* this weird stuff here is to make curl_free() get called before
127 curl_dbg_memdebug() as otherwise memory tracking will log a free()
128 without an alloc! */
129 }
130 /* if CURL_MEMLIMIT is set, this enables fail-on-alloc-number-N feature */
131 env = curlx_getenv("CURL_MEMLIMIT");
132 if(env) {
133 char *endptr;
134 long num = strtol(env, &endptr, 10);
135 if((endptr != env) && (endptr == env + strlen(env)) && (num > 0))
136 curl_dbg_memlimit(num);
137 curl_free(env);
138 }
139}
140#else
141# define memory_tracking_init() Curl_nop_stmt
142#endif
143
144/*
145 * This is the main global constructor for the app. Call this before
146 * _any_ libcurl usage. If this fails, *NO* libcurl functions may be
147 * used, or havoc may be the result.
148 */
149static CURLcode main_init(struct GlobalConfig *config)
150{
151 CURLcode result = CURLE_OK;
152
153#if defined(__DJGPP__) || defined(__GO32__)
154 /* stop stat() wasting time */
155 _djstat_flags |= _STAT_INODE | _STAT_EXEC_MAGIC | _STAT_DIRSIZE;
156#endif
157
158 /* Initialise the global config */
159 config->showerror = -1; /* Will show errors */
160 config->errors = stderr; /* Default errors to stderr */
161 config->styled_output = TRUE; /* enable detection */
162 config->parallel_max = PARALLEL_DEFAULT;
163
164 /* Allocate the initial operate config */
165 config->first = config->last = malloc(sizeof(struct OperationConfig));
166 if(config->first) {
167 /* Perform the libcurl initialization */
168 result = curl_global_init(CURL_GLOBAL_DEFAULT);
169 if(!result) {
170 /* Get information about libcurl */
171 result = get_libcurl_info();
172
173 if(!result) {
174 /* Initialise the config */
175 config_init(config->first);
176 config->first->global = config;
177 }
178 else {
179 errorf(config, "error retrieving curl library information\n");
180 free(config->first);
181 }
182 }
183 else {
184 errorf(config, "error initializing curl library\n");
185 free(config->first);
186 }
187 }
188 else {
189 errorf(config, "error initializing curl\n");
190 result = CURLE_FAILED_INIT;
191 }
192
193 return result;
194}
195
196static void free_globalconfig(struct GlobalConfig *config)
197{
198 Curl_safefree(config->trace_dump);
199
200 if(config->errors_fopened && config->errors)
201 fclose(config->errors);
202 config->errors = NULL;
203
204 if(config->trace_fopened && config->trace_stream)
205 fclose(config->trace_stream);
206 config->trace_stream = NULL;
207
208 Curl_safefree(config->libcurl);
209}
210
211/*
212 * This is the main global destructor for the app. Call this after
213 * _all_ libcurl usage is done.
214 */
215static void main_free(struct GlobalConfig *config)
216{
217 /* Cleanup the easy handle */
218 /* Main cleanup */
219 curl_global_cleanup();
220#ifdef USE_NSS
221 if(PR_Initialized()) {
222 /* prevent valgrind from reporting still reachable mem from NSPR arenas */
223 PL_ArenaFinish();
224 /* prevent valgrind from reporting possibly lost memory (fd cache, ...) */
225 PR_Cleanup();
226 }
227#endif
228 free_globalconfig(config);
229
230 /* Free the config structures */
231 config_free(config->last);
232 config->first = NULL;
233 config->last = NULL;
234}
235
236/*
237** curl tool main function.
238*/
239#ifdef _UNICODE
240int wmain(int argc, wchar_t *argv[])
241#else
242int main(int argc, char *argv[])
243#endif
244{
245 CURLcode result = CURLE_OK;
246 struct GlobalConfig global;
247 memset(&global, 0, sizeof(global));
248
249#ifdef WIN32
250 /* Undocumented diagnostic option to list the full paths of all loaded
251 modules. This is purposely pre-init. */
252 if(argc == 2 && !_tcscmp(argv[1], _T("--dump-module-paths"))) {
253 struct curl_slist *item, *head = GetLoadedModulePaths();
254 for(item = head; item; item = item->next)
255 printf("%s\n", item->data);
256 curl_slist_free_all(head);
257 return head ? 0 : 1;
258 }
259 /* win32_init must be called before other init routines. */
260 result = win32_init();
261 if(result) {
262 fprintf(stderr, "curl: (%d) Windows-specific init failed.\n", result);
263 return result;
264 }
265#endif
266
267 if(main_checkfds()) {
268 fprintf(stderr, "curl: out of file descriptors\n");
269 return CURLE_FAILED_INIT;
270 }
271
272#if defined(HAVE_SIGNAL) && defined(SIGPIPE)
273 (void)signal(SIGPIPE, SIG_IGN);
274#endif
275
276 /* Initialize memory tracking */
277 memory_tracking_init();
278
279 /* Initialize the curl library - do not call any libcurl functions before
280 this point */
281 result = main_init(&global);
282 if(!result) {
283 /* Start our curl operation */
284 result = operate(&global, argc, argv);
285
286 /* Perform the main cleanup */
287 main_free(&global);
288 }
289
290#ifdef WIN32
291 /* Flush buffers of all streams opened in write or update mode */
292 fflush(NULL);
293#endif
294
295#ifdef __NOVELL_LIBC__
296 if(!getenv("_IN_NETWARE_BASH_"))
297 tool_pressanykey();
298#endif
299
300#ifdef __VMS
301 vms_special_exit(result, vms_show);
302#else
303 return (int)result;
304#endif
305}
306
307#endif /* ndef UNITTESTS */