tool_main.c 8.1 KB

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