tool_main.c 9.3 KB

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