1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 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_vms.h"
54 #include "tool_main.h"
55 #include "tool_libinfo.h"
56 #include "tool_stderr.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 */
72 int 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 */
81 int _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 */
main_checkfds(void)98 static 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
memory_tracking_init(void)113 static 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 */
main_init(struct GlobalConfig * config)149 static 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 = FALSE; /* show errors when silent */
160 config->styled_output = TRUE; /* enable detection */
161 config->parallel_max = PARALLEL_DEFAULT;
162
163 /* Allocate the initial operate config */
164 config->first = config->last = malloc(sizeof(struct OperationConfig));
165 if(config->first) {
166 /* Perform the libcurl initialization */
167 result = curl_global_init(CURL_GLOBAL_DEFAULT);
168 if(!result) {
169 /* Get information about libcurl */
170 result = get_libcurl_info();
171
172 if(!result) {
173 /* Initialise the config */
174 config_init(config->first);
175 config->first->global = config;
176 }
177 else {
178 errorf(config, "error retrieving curl library information\n");
179 free(config->first);
180 }
181 }
182 else {
183 errorf(config, "error initializing curl library\n");
184 free(config->first);
185 }
186 }
187 else {
188 errorf(config, "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->trace_fopened && config->trace_stream)
200 fclose(config->trace_stream);
201 config->trace_stream = NULL;
202
203 Curl_safefree(config->libcurl);
204 }
205
206 /*
207 * This is the main global destructor for the app. Call this after
208 * _all_ libcurl usage is done.
209 */
main_free(struct GlobalConfig * config)210 static void main_free(struct GlobalConfig *config)
211 {
212 /* Cleanup the easy handle */
213 /* Main cleanup */
214 curl_global_cleanup();
215 #ifdef USE_NSS
216 if(PR_Initialized()) {
217 /* prevent valgrind from reporting still reachable mem from NSPR arenas */
218 PL_ArenaFinish();
219 /* prevent valgrind from reporting possibly lost memory (fd cache, ...) */
220 PR_Cleanup();
221 }
222 #endif
223 free_globalconfig(config);
224
225 /* Free the config structures */
226 config_free(config->last);
227 config->first = NULL;
228 config->last = NULL;
229 }
230
231 /*
232 ** curl tool main function.
233 */
234 #ifdef _UNICODE
235 #if defined(__GNUC__)
236 /* GCC doesn't know about wmain() */
237 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
238 #pragma GCC diagnostic ignored "-Wmissing-declarations"
239 #endif
wmain(int argc,wchar_t * argv[])240 int wmain(int argc, wchar_t *argv[])
241 #else
242 int 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 tool_init_stderr();
250
251 #ifdef WIN32
252 /* Undocumented diagnostic option to list the full paths of all loaded
253 modules. This is purposely pre-init. */
254 if(argc == 2 && !_tcscmp(argv[1], _T("--dump-module-paths"))) {
255 struct curl_slist *item, *head = GetLoadedModulePaths();
256 for(item = head; item; item = item->next)
257 printf("%s\n", item->data);
258 curl_slist_free_all(head);
259 return head ? 0 : 1;
260 }
261 /* win32_init must be called before other init routines. */
262 result = win32_init();
263 if(result) {
264 fprintf(stderr, "curl: (%d) Windows-specific init failed.\n", result);
265 return result;
266 }
267 #endif
268
269 if(main_checkfds()) {
270 fprintf(stderr, "curl: out of file descriptors\n");
271 return CURLE_FAILED_INIT;
272 }
273
274 #if defined(HAVE_SIGNAL) && defined(SIGPIPE)
275 (void)signal(SIGPIPE, SIG_IGN);
276 #endif
277
278 /* Initialize memory tracking */
279 memory_tracking_init();
280
281 /* Initialize the curl library - do not call any libcurl functions before
282 this point */
283 result = main_init(&global);
284 if(!result) {
285 /* Start our curl operation */
286 result = operate(&global, argc, argv);
287
288 /* Perform the main cleanup */
289 main_free(&global);
290 }
291
292 #ifdef WIN32
293 /* Flush buffers of all streams opened in write or update mode */
294 fflush(NULL);
295 #endif
296
297 #ifdef __VMS
298 vms_special_exit(result, vms_show);
299 #else
300 return (int)result;
301 #endif
302 }
303
304 #endif /* ndef UNITTESTS */
305