tool_main.c 8.0 KB

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