tool_main.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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. #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 HAVE_FCNTL_H
  33. #include <fcntl.h>
  34. #endif
  35. #ifdef USE_NSS
  36. #include <nspr.h>
  37. #include <plarenas.h>
  38. #endif
  39. #define ENABLE_CURLX_PRINTF
  40. /* use our own printf() functions */
  41. #include "curlx.h"
  42. #include "tool_cfgable.h"
  43. #include "tool_doswin.h"
  44. #include "tool_msgs.h"
  45. #include "tool_operate.h"
  46. #include "tool_panykey.h"
  47. #include "tool_vms.h"
  48. #include "tool_main.h"
  49. #include "tool_libinfo.h"
  50. /*
  51. * This is low-level hard-hacking memory leak tracking and similar. Using
  52. * the library level code from this client-side is ugly, but we do this
  53. * anyway for convenience.
  54. */
  55. #include "memdebug.h" /* keep this as LAST include */
  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. #ifdef __MINGW32__
  66. /*
  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
  69. * it off.
  70. */
  71. int _CRT_glob = 0;
  72. #endif /* __MINGW32__ */
  73. /* if we build a static library for unit tests, there is no main() function */
  74. #ifndef UNITTESTS
  75. #if defined(HAVE_PIPE) && defined(HAVE_FCNTL)
  76. /*
  77. * Ensure that file descriptors 0, 1 and 2 (stdin, stdout, stderr) are
  78. * open before starting to run. Otherwise, the first three network
  79. * sockets opened by curl could be used for input sources, downloaded data
  80. * or error logs as they will effectively be stdin, stdout and/or stderr.
  81. */
  82. static int main_checkfds(void)
  83. {
  84. int fd[2];
  85. while(fcntl(STDIN_FILENO, F_GETFD) ||
  86. fcntl(STDOUT_FILENO, F_GETFD) ||
  87. fcntl(STDERR_FILENO, F_GETFD))
  88. if(pipe(fd))
  89. return 1;
  90. return 0;
  91. }
  92. #else
  93. #define main_checkfds() 0
  94. #endif
  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. if(main_checkfds()) {
  236. fprintf(stderr, "curl: out of file descriptors\n");
  237. return CURLE_FAILED_INIT;
  238. }
  239. #if defined(HAVE_SIGNAL) && defined(SIGPIPE)
  240. (void)signal(SIGPIPE, SIG_IGN);
  241. #endif
  242. /* Initialize memory tracking */
  243. memory_tracking_init();
  244. /* Initialize the curl library - do not call any libcurl functions before
  245. this point */
  246. result = main_init(&global);
  247. if(!result) {
  248. /* Start our curl operation */
  249. result = operate(&global, argc, argv);
  250. /* Perform the main cleanup */
  251. main_free(&global);
  252. }
  253. #ifdef WIN32
  254. /* Flush buffers of all streams opened in write or update mode */
  255. fflush(NULL);
  256. #endif
  257. #ifdef __NOVELL_LIBC__
  258. if(!getenv("_IN_NETWARE_BASH_"))
  259. tool_pressanykey();
  260. #endif
  261. #ifdef __VMS
  262. vms_special_exit(result, vms_show);
  263. #else
  264. return (int)result;
  265. #endif
  266. }
  267. #endif /* ndef UNITTESTS */