1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
22 #include "tool_setup.h"
35 #define ENABLE_CURLX_PRINTF
36 /* use our own printf() functions */
39 #include "tool_cfgable.h"
40 #include "tool_convert.h"
41 #include "tool_msgs.h"
42 #include "tool_operate.h"
43 #include "tool_panykey.h"
45 #include "tool_main.h"
46 #include "tool_libinfo.h"
49 * This is low-level hard-hacking memory leak tracking and similar. Using
50 * the library level code from this client-side is ugly, but we do this
51 * anyway for convenience.
53 #include "memdebug.h" /* keep this as LAST include */
57 * vms_show is a global variable, used in main() as parameter for
58 * function vms_special_exit() to allow proper curl tool exiting.
59 * Its value may be set in other tool_*.c source files thanks to
60 * forward declaration present in tool_vms.h
67 * There seems to be no way to escape "*" in command-line arguments with MinGW
68 * when command-line argument globbing is enabled under the MSYS shell, so turn
72 #endif /* __MINGW32__ */
74 /* if we build a static library for unit tests, there is no main() function */
78 * Ensure that file descriptors 0, 1 and 2 (stdin, stdout, stderr) are
79 * open before starting to run. Otherwise, the first three network
80 * sockets opened by curl could be used for input sources, downloaded data
81 * or error logs as they will effectively be stdin, stdout and/or stderr.
83 static void main_checkfds(void)
86 int fd[2] = { STDIN_FILENO, STDIN_FILENO };
87 while(fd[0] == STDIN_FILENO ||
88 fd[0] == STDOUT_FILENO ||
89 fd[0] == STDERR_FILENO ||
90 fd[1] == STDIN_FILENO ||
91 fd[1] == STDOUT_FILENO ||
92 fd[1] == STDERR_FILENO)
94 return; /* Out of handles. This isn't really a big problem now, but
95 will be when we try to create a socket later. */
102 static void memory_tracking_init(void)
105 /* if CURL_MEMDEBUG is set, this starts memory tracking message logging */
106 env = curlx_getenv("CURL_MEMDEBUG");
108 /* use the value as file name */
109 char fname[CURL_MT_LOGFNAME_BUFSIZE];
110 if(strlen(env) >= CURL_MT_LOGFNAME_BUFSIZE)
111 env[CURL_MT_LOGFNAME_BUFSIZE-1] = '\0';
114 curl_memdebug(fname);
115 /* this weird stuff here is to make curl_free() get called
116 before curl_memdebug() as otherwise memory tracking will
117 log a free() without an alloc! */
119 /* if CURL_MEMLIMIT is set, this enables fail-on-alloc-number-N feature */
120 env = curlx_getenv("CURL_MEMLIMIT");
123 long num = strtol(env, &endptr, 10);
124 if((endptr != env) && (endptr == env + strlen(env)) && (num > 0))
130 # define memory_tracking_init() Curl_nop_stmt
134 * This is the main global constructor for the app. Call this before
135 * _any_ libcurl usage. If this fails, *NO* libcurl functions may be
136 * used, or havoc may be the result.
138 static CURLcode main_init(struct GlobalConfig *config)
140 CURLcode result = CURLE_OK;
142 #if defined(__DJGPP__) || defined(__GO32__)
143 /* stop stat() wasting time */
144 _djstat_flags |= _STAT_INODE | _STAT_EXEC_MAGIC | _STAT_DIRSIZE;
147 /* Initialise the global config */
148 config->showerror = -1; /* Will show errors */
149 config->errors = stderr; /* Default errors to stderr */
151 /* Allocate the initial operate config */
152 config->first = config->last = malloc(sizeof(struct OperationConfig));
154 /* Perform the libcurl initialization */
155 result = curl_global_init(CURL_GLOBAL_DEFAULT);
157 /* Get information about libcurl */
158 result = get_libcurl_info();
161 /* Get a curl handle to use for all forthcoming curl transfers */
162 config->easy = curl_easy_init();
164 /* Initialise the config */
165 config_init(config->first);
166 config->first->easy = config->easy;
167 config->first->global = config;
170 helpf(stderr, "error initializing curl easy handle\n");
171 result = CURLE_FAILED_INIT;
176 helpf(stderr, "error retrieving curl library information\n");
181 helpf(stderr, "error initializing curl library\n");
186 helpf(stderr, "error initializing curl\n");
187 result = CURLE_FAILED_INIT;
193 static void free_config_fields(struct GlobalConfig *config)
195 Curl_safefree(config->trace_dump);
197 if(config->errors_fopened && config->errors)
198 fclose(config->errors);
199 config->errors = NULL;
201 if(config->trace_fopened && config->trace_stream)
202 fclose(config->trace_stream);
203 config->trace_stream = NULL;
205 Curl_safefree(config->libcurl);
209 * This is the main global destructor for the app. Call this after
210 * _all_ libcurl usage is done.
212 static void main_free(struct GlobalConfig *config)
214 /* Cleanup the easy handle */
215 curl_easy_cleanup(config->easy);
219 curl_global_cleanup();
223 if(PR_Initialized()) {
224 /* prevent valgrind from reporting still reachable mem from NSRP arenas */
226 /* prevent valgrind from reporting possibly lost memory (fd cache, ...) */
230 free_config_fields(config);
232 /* Free the config structures */
233 config_free(config->last);
234 config->first = NULL;
239 ** curl tool main function.
241 int main(int argc, char *argv[])
243 CURLcode result = CURLE_OK;
244 struct GlobalConfig global;
245 memset(&global, 0, sizeof(global));
249 #if defined(HAVE_SIGNAL) && defined(SIGPIPE)
250 (void)signal(SIGPIPE, SIG_IGN);
253 /* Initialize memory tracking */
254 memory_tracking_init();
256 /* Initialize the curl library - do not call any libcurl functions before
258 result = main_init(&global);
260 /* Start our curl operation */
261 result = operate(&global, argc, argv);
268 /* Perform the main cleanup */
272 #ifdef __NOVELL_LIBC__
273 if(getenv("_IN_NETWARE_BASH_") == NULL)
278 vms_special_exit(result, vms_show);
284 #endif /* ndef UNITTESTS */