• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <signal.h>
33 
34 #ifdef HAVE_FCNTL_H
35 #include <fcntl.h>
36 #endif
37 
38 #include "curlx.h"
39 
40 #include "tool_cfgable.h"
41 #include "tool_doswin.h"
42 #include "tool_msgs.h"
43 #include "tool_operate.h"
44 #include "tool_vms.h"
45 #include "tool_main.h"
46 #include "tool_libinfo.h"
47 #include "tool_stderr.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 #if defined(__AMIGA__)
67 #if defined(__GNUC__)
68 #define CURL_USED __attribute__((used))
69 #else
70 #define CURL_USED
71 #endif
72 static const char CURL_USED min_stack[] = "$STACK:16384";
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 extern int _CRT_glob;
82 int _CRT_glob = 0;
83 #endif /* __MINGW32__ */
84 
85 /* if we build a static library for unit tests, there is no main() function */
86 #ifndef UNITTESTS
87 
88 #if defined(HAVE_PIPE) && defined(HAVE_FCNTL)
89 /*
90  * Ensure that file descriptors 0, 1 and 2 (stdin, stdout, stderr) are
91  * open before starting to run. Otherwise, the first three network
92  * sockets opened by curl could be used for input sources, downloaded data
93  * or error logs as they will effectively be stdin, stdout and/or stderr.
94  *
95  * fcntl's F_GETFD instruction returns -1 if the file descriptor is closed,
96  * otherwise it returns "the file descriptor flags (which typically can only
97  * be FD_CLOEXEC, which is not set here).
98  */
main_checkfds(void)99 static int main_checkfds(void)
100 {
101   int fd[2];
102   while((fcntl(STDIN_FILENO, F_GETFD) == -1) ||
103         (fcntl(STDOUT_FILENO, F_GETFD) == -1) ||
104         (fcntl(STDERR_FILENO, F_GETFD) == -1))
105     if(pipe(fd))
106       return 1;
107   return 0;
108 }
109 #else
110 #define main_checkfds() 0
111 #endif
112 
113 #ifdef CURLDEBUG
memory_tracking_init(void)114 static void memory_tracking_init(void)
115 {
116   char *env;
117   /* if CURL_MEMDEBUG is set, this starts memory tracking message logging */
118   env = curl_getenv("CURL_MEMDEBUG");
119   if(env) {
120     /* use the value as filename */
121     char fname[CURL_MT_LOGFNAME_BUFSIZE];
122     if(strlen(env) >= CURL_MT_LOGFNAME_BUFSIZE)
123       env[CURL_MT_LOGFNAME_BUFSIZE-1] = '\0';
124     strcpy(fname, env);
125     curl_free(env);
126     curl_dbg_memdebug(fname);
127     /* this weird stuff here is to make curl_free() get called before
128        curl_dbg_memdebug() as otherwise memory tracking will log a free()
129        without an alloc! */
130   }
131   /* if CURL_MEMLIMIT is set, this enables fail-on-alloc-number-N feature */
132   env = curl_getenv("CURL_MEMLIMIT");
133   if(env) {
134     char *endptr;
135     long num = strtol(env, &endptr, 10);
136     if((endptr != env) && (endptr == env + strlen(env)) && (num > 0))
137       curl_dbg_memlimit(num);
138     curl_free(env);
139   }
140 }
141 #else
142 #  define memory_tracking_init() Curl_nop_stmt
143 #endif
144 
145 /*
146  * This is the main global constructor for the app. Call this before
147  * _any_ libcurl usage. If this fails, *NO* libcurl functions may be
148  * used, or havoc may be the result.
149  */
main_init(struct GlobalConfig * config)150 static CURLcode main_init(struct GlobalConfig *config)
151 {
152   CURLcode result = CURLE_OK;
153 
154 #ifdef __DJGPP__
155   /* stop stat() wasting time */
156   _djstat_flags |= _STAT_INODE | _STAT_EXEC_MAGIC | _STAT_DIRSIZE;
157 #endif
158 
159   /* Initialise the global config */
160   config->showerror = FALSE;          /* show errors when silent */
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");
180         free(config->first);
181       }
182     }
183     else {
184       errorf(config, "error initializing curl library");
185       free(config->first);
186     }
187   }
188   else {
189     errorf(config, "error initializing curl");
190     result = CURLE_FAILED_INIT;
191   }
192 
193   return result;
194 }
195 
free_globalconfig(struct GlobalConfig * config)196 static void free_globalconfig(struct GlobalConfig *config)
197 {
198   Curl_safefree(config->trace_dump);
199 
200   if(config->trace_fopened && config->trace_stream)
201     fclose(config->trace_stream);
202   config->trace_stream = NULL;
203 
204   Curl_safefree(config->libcurl);
205 }
206 
207 /*
208  * This is the main global destructor for the app. Call this after
209  * _all_ libcurl usage is done.
210  */
main_free(struct GlobalConfig * config)211 static void main_free(struct GlobalConfig *config)
212 {
213   /* Cleanup the easy handle */
214   /* Main cleanup */
215   curl_global_cleanup();
216   free_globalconfig(config);
217 
218   /* Free the config structures */
219   config_free(config->last);
220   config->first = NULL;
221   config->last = NULL;
222 }
223 
224 /*
225 ** curl tool main function.
226 */
227 #ifdef _UNICODE
228 #if defined(__GNUC__) || defined(__clang__)
229 /* GCC does not know about wmain() */
230 #pragma GCC diagnostic push
231 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
232 #pragma GCC diagnostic ignored "-Wmissing-declarations"
233 #endif
wmain(int argc,wchar_t * argv[])234 int wmain(int argc, wchar_t *argv[])
235 #else
236 int main(int argc, char *argv[])
237 #endif
238 {
239   CURLcode result = CURLE_OK;
240   struct GlobalConfig global;
241   memset(&global, 0, sizeof(global));
242 
243   tool_init_stderr();
244 
245 #ifdef _WIN32
246   /* Undocumented diagnostic option to list the full paths of all loaded
247      modules. This is purposely pre-init. */
248   if(argc == 2 && !_tcscmp(argv[1], _T("--dump-module-paths"))) {
249     struct curl_slist *item, *head = GetLoadedModulePaths();
250     for(item = head; item; item = item->next)
251       printf("%s\n", item->data);
252     curl_slist_free_all(head);
253     return head ? 0 : 1;
254   }
255   /* win32_init must be called before other init routines. */
256   result = win32_init();
257   if(result) {
258     errorf(&global, "(%d) Windows-specific init failed", result);
259     return (int)result;
260   }
261 #endif
262 
263   if(main_checkfds()) {
264     errorf(&global, "out of file descriptors");
265     return CURLE_FAILED_INIT;
266   }
267 
268 #if defined(HAVE_SIGNAL) && defined(SIGPIPE)
269   (void)signal(SIGPIPE, SIG_IGN);
270 #endif
271 
272   /* Initialize memory tracking */
273   memory_tracking_init();
274 
275   /* Initialize the curl library - do not call any libcurl functions before
276      this point */
277   result = main_init(&global);
278   if(!result) {
279     /* Start our curl operation */
280     result = operate(&global, argc, argv);
281 
282     /* Perform the main cleanup */
283     main_free(&global);
284   }
285 
286 #ifdef _WIN32
287   /* Flush buffers of all streams opened in write or update mode */
288   fflush(NULL);
289 #endif
290 
291 #ifdef __VMS
292   vms_special_exit(result, vms_show);
293 #else
294   return (int)result;
295 #endif
296 }
297 
298 #ifdef _UNICODE
299 #if defined(__GNUC__) || defined(__clang__)
300 #pragma GCC diagnostic pop
301 #endif
302 #endif
303 
304 #endif /* ndef UNITTESTS */
305