diff options
Diffstat (limited to 'curl/curl/tool_main.c')
-rw-r--r-- | curl/curl/tool_main.c | 60 |
1 files changed, 33 insertions, 27 deletions
diff --git a/curl/curl/tool_main.c b/curl/curl/tool_main.c index 913f8d6..0eb4e6d 100644 --- a/curl/curl/tool_main.c +++ b/curl/curl/tool_main.c @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al. + * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -18,6 +18,8 @@ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY * KIND, either express or implied. * + * SPDX-License-Identifier: curl + * ***************************************************************************/ #include "tool_setup.h" @@ -31,6 +33,10 @@ #include <signal.h> #endif +#ifdef HAVE_FCNTL_H +#include <fcntl.h> +#endif + #ifdef USE_NSS #include <nspr.h> #include <plarenas.h> @@ -45,11 +51,9 @@ #include "curlx.h" #include "tool_cfgable.h" -#include "tool_convert.h" #include "tool_doswin.h" #include "tool_msgs.h" #include "tool_operate.h" -#include "tool_panykey.h" #include "tool_vms.h" #include "tool_main.h" #include "tool_libinfo.h" @@ -83,29 +87,30 @@ int _CRT_glob = 0; /* if we build a static library for unit tests, there is no main() function */ #ifndef UNITTESTS +#if defined(HAVE_PIPE) && defined(HAVE_FCNTL) /* * Ensure that file descriptors 0, 1 and 2 (stdin, stdout, stderr) are * open before starting to run. Otherwise, the first three network * sockets opened by curl could be used for input sources, downloaded data * or error logs as they will effectively be stdin, stdout and/or stderr. + * + * fcntl's F_GETFD instruction returns -1 if the file descriptor is closed, + * otherwise it returns "the file descriptor flags (which typically can only + * be FD_CLOEXEC, which is not set here). */ -static void main_checkfds(void) +static int main_checkfds(void) { -#ifdef HAVE_PIPE - int fd[2] = { STDIN_FILENO, STDIN_FILENO }; - while(fd[0] == STDIN_FILENO || - fd[0] == STDOUT_FILENO || - fd[0] == STDERR_FILENO || - fd[1] == STDIN_FILENO || - fd[1] == STDOUT_FILENO || - fd[1] == STDERR_FILENO) - if(pipe(fd) < 0) - return; /* Out of handles. This isn't really a big problem now, but - will be when we try to create a socket later. */ - close(fd[0]); - close(fd[1]); -#endif + int fd[2]; + while((fcntl(STDIN_FILENO, F_GETFD) == -1) || + (fcntl(STDOUT_FILENO, F_GETFD) == -1) || + (fcntl(STDERR_FILENO, F_GETFD) == -1)) + if(pipe(fd)) + return 1; + return 0; } +#else +#define main_checkfds() 0 +#endif #ifdef CURLDEBUG static void memory_tracking_init(void) @@ -122,7 +127,7 @@ static void memory_tracking_init(void) curl_free(env); curl_dbg_memdebug(fname); /* this weird stuff here is to make curl_free() get called before - curl_gdb_memdebug() as otherwise memory tracking will log a free() + curl_dbg_memdebug() as otherwise memory tracking will log a free() without an alloc! */ } /* if CURL_MEMLIMIT is set, this enables fail-on-alloc-number-N feature */ @@ -189,7 +194,7 @@ static CURLcode main_init(struct GlobalConfig *config) #endif /* Initialise the global config */ - config->showerror = -1; /* Will show errors */ + config->showerror = FALSE; /* show errors when silent */ config->errors = stderr; /* Default errors to stderr */ config->styled_output = TRUE; /* enable detection */ config->parallel_max = PARALLEL_DEFAULT; @@ -250,11 +255,9 @@ static void main_free(struct GlobalConfig *config) /* Cleanup the easy handle */ /* Main cleanup */ curl_global_cleanup(); - convert_cleanup(); - metalink_cleanup(); #ifdef USE_NSS if(PR_Initialized()) { - /* prevent valgrind from reporting still reachable mem from NSRP arenas */ + /* prevent valgrind from reporting still reachable mem from NSPR arenas */ PL_ArenaFinish(); /* prevent valgrind from reporting possibly lost memory (fd cache, ...) */ PR_Cleanup(); @@ -299,7 +302,10 @@ int main(int argc, char *argv[]) } #endif - main_checkfds(); + if(main_checkfds()) { + fprintf(stderr, "curl: out of file descriptors\n"); + return CURLE_FAILED_INIT; + } #if defined(HAVE_SIGNAL) && defined(SIGPIPE) (void)signal(SIGPIPE, SIG_IGN); @@ -319,9 +325,9 @@ int main(int argc, char *argv[]) main_free(&global); } -#ifdef __NOVELL_LIBC__ - if(getenv("_IN_NETWARE_BASH_") == NULL) - tool_pressanykey(); +#ifdef WIN32 + /* Flush buffers of all streams opened in write or update mode */ + fflush(NULL); #endif #ifdef __VMS |