tool_main.c 8.1 KB

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