2005-05-01 07:30:55 +08:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2022-02-03 20:04:30 +08:00
|
|
|
* Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2005-05-01 07:30:55 +08:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 21:02:01 +08:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2005-05-01 07:30:55 +08:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
2005-05-01 20:56:09 +08:00
|
|
|
* SPDX-License-Identifier: curl
|
2022-05-17 17:16:50 +08:00
|
|
|
*
|
2005-05-01 07:30:55 +08:00
|
|
|
***************************************************************************/
|
2012-04-10 23:32:06 +08:00
|
|
|
#include "server_setup.h"
|
2005-05-01 07:30:55 +08:00
|
|
|
|
2008-02-28 08:55:06 +08:00
|
|
|
#ifdef HAVE_SIGNAL_H
|
2005-05-01 07:30:55 +08:00
|
|
|
#include <signal.h>
|
2008-02-28 08:55:06 +08:00
|
|
|
#endif
|
2005-05-01 07:30:55 +08:00
|
|
|
#ifdef HAVE_NETINET_IN_H
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#endif
|
|
|
|
#ifdef _XOPEN_SOURCE_EXTENDED
|
|
|
|
/* This define is "almost" required to build on HPUX 11 */
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETDB_H
|
|
|
|
#include <netdb.h>
|
|
|
|
#endif
|
2017-04-12 14:45:54 +08:00
|
|
|
#ifdef HAVE_POLL_H
|
2008-01-22 22:52:54 +08:00
|
|
|
#include <poll.h>
|
2017-04-12 14:45:54 +08:00
|
|
|
#elif defined(HAVE_SYS_POLL_H)
|
|
|
|
#include <sys/poll.h>
|
2007-07-12 09:07:49 +08:00
|
|
|
#endif
|
2017-04-03 01:22:42 +08:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
#include <w32api.h>
|
|
|
|
#endif
|
2005-05-01 07:30:55 +08:00
|
|
|
|
2005-05-01 20:56:09 +08:00
|
|
|
#define ENABLE_CURLX_PRINTF
|
|
|
|
/* make the curlx header define all printf() functions to use the curlx_*
|
|
|
|
versions instead */
|
2005-05-01 07:30:55 +08:00
|
|
|
#include "curlx.h" /* from the private lib dir */
|
|
|
|
#include "getpart.h"
|
|
|
|
#include "util.h"
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "timeval.h"
|
2005-05-01 07:30:55 +08:00
|
|
|
|
2013-01-09 22:10:23 +08:00
|
|
|
#ifdef USE_WINSOCK
|
|
|
|
#undef EINTR
|
|
|
|
#define EINTR 4 /* errno.h value */
|
|
|
|
#undef EINVAL
|
|
|
|
#define EINVAL 22 /* errno.h value */
|
|
|
|
#endif
|
|
|
|
|
2017-04-03 01:22:42 +08:00
|
|
|
/* MinGW with w32api version < 3.6 declared in6addr_any as extern,
|
|
|
|
but lacked the definition */
|
2006-09-13 21:41:53 +08:00
|
|
|
#if defined(ENABLE_IPV6) && defined(__MINGW32__)
|
2017-04-03 01:22:42 +08:00
|
|
|
#if (__W32API_MAJOR_VERSION < 3) || \
|
|
|
|
((__W32API_MAJOR_VERSION == 3) && (__W32API_MINOR_VERSION < 6))
|
2006-09-13 21:41:53 +08:00
|
|
|
const struct in6_addr in6addr_any = {{ IN6ADDR_ANY_INIT }};
|
2017-04-03 01:22:42 +08:00
|
|
|
#endif /* w32api < 3.6 */
|
2022-10-31 00:38:16 +08:00
|
|
|
#endif /* ENABLE_IPV6 && __MINGW32__ */
|
2006-09-13 21:41:53 +08:00
|
|
|
|
2017-10-23 18:05:49 +08:00
|
|
|
static struct timeval tvnow(void);
|
|
|
|
|
2011-12-18 06:47:22 +08:00
|
|
|
/* This function returns a pointer to STATIC memory. It converts the given
|
|
|
|
* binary lump to a hex formatted string usable for output in logs or
|
|
|
|
* whatever.
|
|
|
|
*/
|
|
|
|
char *data_to_hex(char *data, size_t len)
|
|
|
|
{
|
|
|
|
static char buf[256*3];
|
|
|
|
size_t i;
|
|
|
|
char *optr = buf;
|
|
|
|
char *iptr = data;
|
|
|
|
|
|
|
|
if(len > 255)
|
|
|
|
len = 255;
|
|
|
|
|
2017-09-10 05:09:06 +08:00
|
|
|
for(i = 0; i < len; i++) {
|
2011-12-18 06:47:22 +08:00
|
|
|
if((data[i] >= 0x20) && (data[i] < 0x7f))
|
|
|
|
*optr++ = *iptr++;
|
|
|
|
else {
|
2018-11-22 16:01:24 +08:00
|
|
|
msnprintf(optr, 4, "%%%02x", *iptr++);
|
2017-09-10 05:09:06 +08:00
|
|
|
optr += 3;
|
2011-12-18 06:47:22 +08:00
|
|
|
}
|
|
|
|
}
|
2017-09-10 05:09:06 +08:00
|
|
|
*optr = 0; /* in case no sprintf was used */
|
2011-12-18 06:47:22 +08:00
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2005-05-01 07:30:55 +08:00
|
|
|
void logmsg(const char *msg, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
2008-09-26 08:17:01 +08:00
|
|
|
char buffer[2048 + 1];
|
2005-05-01 07:30:55 +08:00
|
|
|
FILE *logfp;
|
2017-10-23 18:05:49 +08:00
|
|
|
struct timeval tv;
|
2007-02-17 09:25:32 +08:00
|
|
|
time_t sec;
|
|
|
|
struct tm *now;
|
|
|
|
char timebuf[20];
|
2010-02-25 14:59:04 +08:00
|
|
|
static time_t epoch_offset;
|
|
|
|
static int known_offset;
|
2007-02-17 03:41:25 +08:00
|
|
|
|
2016-04-04 04:35:43 +08:00
|
|
|
if(!serverlogfile) {
|
2007-02-17 03:41:25 +08:00
|
|
|
fprintf(stderr, "Error: serverlogfile not set\n");
|
|
|
|
return;
|
|
|
|
}
|
2005-05-01 07:30:55 +08:00
|
|
|
|
2017-10-23 18:05:49 +08:00
|
|
|
tv = tvnow();
|
2010-02-25 14:59:04 +08:00
|
|
|
if(!known_offset) {
|
|
|
|
epoch_offset = time(NULL) - tv.tv_sec;
|
|
|
|
known_offset = 1;
|
|
|
|
}
|
|
|
|
sec = epoch_offset + tv.tv_sec;
|
2020-07-27 20:28:37 +08:00
|
|
|
/* !checksrc! disable BANNEDFUNC 1 */
|
2010-02-25 14:59:04 +08:00
|
|
|
now = localtime(&sec); /* not thread safe but we don't care */
|
2005-05-01 07:30:55 +08:00
|
|
|
|
2018-11-22 16:01:24 +08:00
|
|
|
msnprintf(timebuf, sizeof(timebuf), "%02d:%02d:%02d.%06ld",
|
|
|
|
(int)now->tm_hour, (int)now->tm_min, (int)now->tm_sec,
|
|
|
|
(long)tv.tv_usec);
|
2005-05-01 07:30:55 +08:00
|
|
|
|
|
|
|
va_start(ap, msg);
|
2018-11-22 16:01:24 +08:00
|
|
|
mvsnprintf(buffer, sizeof(buffer), msg, ap);
|
2005-05-01 07:30:55 +08:00
|
|
|
va_end(ap);
|
|
|
|
|
2009-12-16 23:16:06 +08:00
|
|
|
logfp = fopen(serverlogfile, "ab");
|
2005-05-25 20:04:52 +08:00
|
|
|
if(logfp) {
|
|
|
|
fprintf(logfp, "%s %s\n", timebuf, buffer);
|
2005-05-01 07:30:55 +08:00
|
|
|
fclose(logfp);
|
2005-05-25 20:04:52 +08:00
|
|
|
}
|
2007-02-17 03:41:25 +08:00
|
|
|
else {
|
2018-06-03 04:52:56 +08:00
|
|
|
int error = errno;
|
2007-02-17 03:41:25 +08:00
|
|
|
fprintf(stderr, "fopen() failed with error: %d %s\n",
|
|
|
|
error, strerror(error));
|
|
|
|
fprintf(stderr, "Error opening file: %s\n", serverlogfile);
|
|
|
|
fprintf(stderr, "Msg not logged: %s %s\n", timebuf, buffer);
|
|
|
|
}
|
2005-05-01 07:30:55 +08:00
|
|
|
}
|
2005-05-17 18:22:22 +08:00
|
|
|
|
2006-10-12 00:01:16 +08:00
|
|
|
#ifdef WIN32
|
2005-05-17 18:22:22 +08:00
|
|
|
/* use instead of perror() on generic windows */
|
2016-12-14 06:34:59 +08:00
|
|
|
void win32_perror(const char *msg)
|
2005-05-17 18:22:22 +08:00
|
|
|
{
|
2007-02-17 03:41:25 +08:00
|
|
|
char buf[512];
|
2007-02-17 02:19:35 +08:00
|
|
|
DWORD err = SOCKERRNO;
|
2005-05-17 18:22:22 +08:00
|
|
|
|
2019-11-10 07:07:59 +08:00
|
|
|
if(!FormatMessageA((FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS), NULL, err,
|
2019-04-11 01:26:35 +08:00
|
|
|
LANG_NEUTRAL, buf, sizeof(buf), NULL))
|
2018-11-22 16:01:24 +08:00
|
|
|
msnprintf(buf, sizeof(buf), "Unknown error %lu (%#lx)", err, err);
|
2016-04-04 04:35:43 +08:00
|
|
|
if(msg)
|
|
|
|
fprintf(stderr, "%s: ", msg);
|
2005-05-17 18:22:22 +08:00
|
|
|
fprintf(stderr, "%s\n", buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void win32_init(void)
|
|
|
|
{
|
2022-02-26 21:34:42 +08:00
|
|
|
#ifdef USE_WINSOCK
|
2005-05-17 18:22:22 +08:00
|
|
|
WORD wVersionRequested;
|
|
|
|
WSADATA wsaData;
|
|
|
|
int err;
|
|
|
|
|
2020-08-30 18:21:53 +08:00
|
|
|
wVersionRequested = MAKEWORD(2, 2);
|
2005-05-17 18:22:22 +08:00
|
|
|
err = WSAStartup(wVersionRequested, &wsaData);
|
|
|
|
|
2021-04-19 16:46:11 +08:00
|
|
|
if(err) {
|
2005-05-17 18:22:22 +08:00
|
|
|
perror("Winsock init failed");
|
2007-02-19 10:03:58 +08:00
|
|
|
logmsg("Error initialising winsock -- aborting");
|
2005-05-17 18:22:22 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2020-08-30 18:21:53 +08:00
|
|
|
if(LOBYTE(wsaData.wVersion) != LOBYTE(wVersionRequested) ||
|
|
|
|
HIBYTE(wsaData.wVersion) != HIBYTE(wVersionRequested) ) {
|
2005-05-17 18:22:22 +08:00
|
|
|
WSACleanup();
|
|
|
|
perror("Winsock init failed");
|
2007-02-19 10:03:58 +08:00
|
|
|
logmsg("No suitable winsock.dll found -- aborting");
|
2005-05-17 18:22:22 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
2022-02-26 21:34:42 +08:00
|
|
|
#endif /* USE_WINSOCK */
|
2005-05-17 18:22:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void win32_cleanup(void)
|
|
|
|
{
|
2022-02-26 21:34:42 +08:00
|
|
|
#ifdef USE_WINSOCK
|
2005-05-17 18:22:22 +08:00
|
|
|
WSACleanup();
|
2006-10-19 05:05:40 +08:00
|
|
|
#endif /* USE_WINSOCK */
|
2005-05-17 18:22:22 +08:00
|
|
|
|
2022-02-26 21:34:42 +08:00
|
|
|
/* flush buffers of all streams regardless of their mode */
|
|
|
|
_flushall();
|
|
|
|
}
|
|
|
|
#endif /* WIN32 */
|
|
|
|
|
2005-09-16 04:22:43 +08:00
|
|
|
/* set by the main code to point to where the test dir is */
|
2017-09-10 05:09:06 +08:00
|
|
|
const char *path = ".";
|
2005-09-16 04:22:43 +08:00
|
|
|
|
2020-04-17 15:58:42 +08:00
|
|
|
FILE *test2fopen(long testno)
|
2005-09-16 04:22:43 +08:00
|
|
|
{
|
2020-04-17 15:58:42 +08:00
|
|
|
FILE *stream;
|
|
|
|
char filename[256];
|
|
|
|
/* first try the alternative, preprocessed, file */
|
2020-04-29 19:26:14 +08:00
|
|
|
msnprintf(filename, sizeof(filename), ALTTEST_DATA_PATH, ".", testno);
|
2020-04-17 15:58:42 +08:00
|
|
|
stream = fopen(filename, "rb");
|
|
|
|
if(stream)
|
|
|
|
return stream;
|
|
|
|
|
|
|
|
/* then try the source version */
|
2018-11-22 16:01:24 +08:00
|
|
|
msnprintf(filename, sizeof(filename), TEST_DATA_PATH, path, testno);
|
2020-04-17 15:58:42 +08:00
|
|
|
stream = fopen(filename, "rb");
|
|
|
|
|
|
|
|
return stream;
|
2005-09-16 04:22:43 +08:00
|
|
|
}
|
2007-07-12 09:07:49 +08:00
|
|
|
|
2008-02-19 04:13:30 +08:00
|
|
|
/*
|
|
|
|
* Portable function used for waiting a specific amount of ms.
|
|
|
|
* Waiting indefinitely with this function is not allowed, a
|
|
|
|
* zero or negative timeout value will return immediately.
|
|
|
|
*
|
|
|
|
* Return values:
|
|
|
|
* -1 = system call error, or invalid timeout value
|
|
|
|
* 0 = specified timeout has elapsed
|
|
|
|
*/
|
|
|
|
int wait_ms(int timeout_ms)
|
2007-07-12 09:07:49 +08:00
|
|
|
{
|
2008-02-19 04:13:30 +08:00
|
|
|
#if !defined(MSDOS) && !defined(USE_WINSOCK)
|
|
|
|
#ifndef HAVE_POLL_FINE
|
2017-08-02 17:53:27 +08:00
|
|
|
struct timeval pending_tv;
|
2007-07-12 09:07:49 +08:00
|
|
|
#endif
|
2017-10-23 18:05:49 +08:00
|
|
|
struct timeval initial_tv;
|
2008-02-19 04:13:30 +08:00
|
|
|
int pending_ms;
|
2007-07-12 09:07:49 +08:00
|
|
|
#endif
|
2008-02-19 04:13:30 +08:00
|
|
|
int r = 0;
|
2007-07-12 09:07:49 +08:00
|
|
|
|
2008-02-19 04:13:30 +08:00
|
|
|
if(!timeout_ms)
|
|
|
|
return 0;
|
|
|
|
if(timeout_ms < 0) {
|
2013-01-09 22:10:23 +08:00
|
|
|
errno = EINVAL;
|
2008-02-19 04:13:30 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#if defined(MSDOS)
|
|
|
|
delay(timeout_ms);
|
|
|
|
#elif defined(USE_WINSOCK)
|
|
|
|
Sleep(timeout_ms);
|
|
|
|
#else
|
|
|
|
pending_ms = timeout_ms;
|
2017-10-23 18:05:49 +08:00
|
|
|
initial_tv = tvnow();
|
2008-02-19 04:13:30 +08:00
|
|
|
do {
|
2018-06-03 04:52:56 +08:00
|
|
|
int error;
|
2008-02-19 04:13:30 +08:00
|
|
|
#if defined(HAVE_POLL_FINE)
|
|
|
|
r = poll(NULL, 0, pending_ms);
|
|
|
|
#else
|
|
|
|
pending_tv.tv_sec = pending_ms / 1000;
|
|
|
|
pending_tv.tv_usec = (pending_ms % 1000) * 1000;
|
|
|
|
r = select(0, NULL, NULL, NULL, &pending_tv);
|
|
|
|
#endif /* HAVE_POLL_FINE */
|
|
|
|
if(r != -1)
|
|
|
|
break;
|
2013-01-09 22:10:23 +08:00
|
|
|
error = errno;
|
2009-12-10 02:41:43 +08:00
|
|
|
if(error && (error != EINTR))
|
2008-02-19 04:13:30 +08:00
|
|
|
break;
|
2017-10-23 18:05:49 +08:00
|
|
|
pending_ms = timeout_ms - (int)timediff(tvnow(), initial_tv);
|
2008-02-19 04:13:30 +08:00
|
|
|
if(pending_ms <= 0)
|
|
|
|
break;
|
|
|
|
} while(r == -1);
|
|
|
|
#endif /* USE_WINSOCK */
|
|
|
|
if(r)
|
|
|
|
r = -1;
|
|
|
|
return r;
|
|
|
|
}
|
2007-07-12 09:07:49 +08:00
|
|
|
|
2021-07-24 02:33:40 +08:00
|
|
|
curl_off_t our_getpid(void)
|
2008-02-26 23:06:44 +08:00
|
|
|
{
|
2020-04-12 05:40:45 +08:00
|
|
|
curl_off_t pid;
|
2008-02-26 23:06:44 +08:00
|
|
|
|
2020-04-12 05:40:45 +08:00
|
|
|
pid = (curl_off_t)getpid();
|
2020-04-06 01:18:09 +08:00
|
|
|
#if defined(WIN32) || defined(_WIN32)
|
|
|
|
/* store pid + 65536 to avoid conflict with Cygwin/msys PIDs, see also:
|
|
|
|
* - https://cygwin.com/git/?p=newlib-cygwin.git;a=commit; ↵
|
|
|
|
* h=b5e1003722cb14235c4f166be72c09acdffc62ea
|
|
|
|
* - https://cygwin.com/git/?p=newlib-cygwin.git;a=commit; ↵
|
|
|
|
* h=448cf5aa4b429d5a9cebf92a0da4ab4b5b6d23fe
|
|
|
|
*/
|
|
|
|
pid += 65536;
|
|
|
|
#endif
|
2021-07-24 02:33:40 +08:00
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
int write_pidfile(const char *filename)
|
|
|
|
{
|
|
|
|
FILE *pidfile;
|
|
|
|
curl_off_t pid;
|
|
|
|
|
|
|
|
pid = our_getpid();
|
|
|
|
pidfile = fopen(filename, "wb");
|
|
|
|
if(!pidfile) {
|
|
|
|
logmsg("Couldn't write pid file: %s %s", filename, strerror(errno));
|
|
|
|
return 0; /* fail */
|
|
|
|
}
|
2020-04-12 05:40:45 +08:00
|
|
|
fprintf(pidfile, "%" CURL_FORMAT_CURL_OFF_T "\n", pid);
|
2008-02-26 23:06:44 +08:00
|
|
|
fclose(pidfile);
|
2020-06-06 05:03:04 +08:00
|
|
|
logmsg("Wrote pid %" CURL_FORMAT_CURL_OFF_T " to %s", pid, filename);
|
2008-02-28 17:38:32 +08:00
|
|
|
return 1; /* success */
|
2008-02-26 23:06:44 +08:00
|
|
|
}
|
2008-04-24 07:55:34 +08:00
|
|
|
|
2020-04-14 17:19:12 +08:00
|
|
|
/* store the used port number in a file */
|
|
|
|
int write_portfile(const char *filename, int port)
|
|
|
|
{
|
|
|
|
FILE *portfile = fopen(filename, "wb");
|
|
|
|
if(!portfile) {
|
|
|
|
logmsg("Couldn't write port file: %s %s", filename, strerror(errno));
|
|
|
|
return 0; /* fail */
|
|
|
|
}
|
|
|
|
fprintf(portfile, "%d\n", port);
|
|
|
|
fclose(portfile);
|
|
|
|
logmsg("Wrote port %d to %s", port, filename);
|
|
|
|
return 1; /* success */
|
|
|
|
}
|
|
|
|
|
2008-04-24 07:55:34 +08:00
|
|
|
void set_advisor_read_lock(const char *filename)
|
|
|
|
{
|
|
|
|
FILE *lockfile;
|
2008-10-02 01:34:24 +08:00
|
|
|
int error = 0;
|
2008-04-24 07:55:34 +08:00
|
|
|
int res;
|
|
|
|
|
|
|
|
do {
|
|
|
|
lockfile = fopen(filename, "wb");
|
2021-04-19 16:46:11 +08:00
|
|
|
} while(!lockfile && ((error = errno) == EINTR));
|
|
|
|
if(!lockfile) {
|
2008-04-24 07:55:34 +08:00
|
|
|
logmsg("Error creating lock file %s error: %d %s",
|
|
|
|
filename, error, strerror(error));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
res = fclose(lockfile);
|
2013-01-09 22:10:23 +08:00
|
|
|
} while(res && ((error = errno) == EINTR));
|
2008-04-24 07:55:34 +08:00
|
|
|
if(res)
|
|
|
|
logmsg("Error closing lock file %s error: %d %s",
|
|
|
|
filename, error, strerror(error));
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear_advisor_read_lock(const char *filename)
|
|
|
|
{
|
2008-10-02 01:34:24 +08:00
|
|
|
int error = 0;
|
2008-04-24 07:55:34 +08:00
|
|
|
int res;
|
|
|
|
|
2009-11-26 18:15:08 +08:00
|
|
|
/*
|
|
|
|
** Log all removal failures. Even those due to file not existing.
|
|
|
|
** This allows to detect if unexpectedly the file has already been
|
|
|
|
** removed by a process different than the one that should do this.
|
|
|
|
*/
|
|
|
|
|
2008-04-24 07:55:34 +08:00
|
|
|
do {
|
|
|
|
res = unlink(filename);
|
2013-01-09 22:10:23 +08:00
|
|
|
} while(res && ((error = errno) == EINTR));
|
2008-04-24 07:55:34 +08:00
|
|
|
if(res)
|
|
|
|
logmsg("Error removing lock file %s error: %d %s",
|
|
|
|
filename, error, strerror(error));
|
|
|
|
}
|
2016-11-01 06:49:54 +08:00
|
|
|
|
|
|
|
|
2017-10-23 18:05:49 +08:00
|
|
|
#if defined(WIN32) && !defined(MSDOS)
|
|
|
|
|
|
|
|
static struct timeval tvnow(void)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
** GetTickCount() is available on _all_ Windows versions from W95 up
|
|
|
|
** to nowadays. Returns milliseconds elapsed since last system boot,
|
|
|
|
** increases monotonically and wraps once 49.7 days have elapsed.
|
|
|
|
**
|
|
|
|
** GetTickCount64() is available on Windows version from Windows Vista
|
|
|
|
** and Windows Server 2008 up to nowadays. The resolution of the
|
|
|
|
** function is limited to the resolution of the system timer, which
|
|
|
|
** is typically in the range of 10 milliseconds to 16 milliseconds.
|
|
|
|
*/
|
|
|
|
struct timeval now;
|
2018-10-08 03:46:56 +08:00
|
|
|
#if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600) && \
|
|
|
|
(!defined(__MINGW32__) || defined(__MINGW64_VERSION_MAJOR))
|
2017-10-23 18:05:49 +08:00
|
|
|
ULONGLONG milliseconds = GetTickCount64();
|
|
|
|
#else
|
|
|
|
DWORD milliseconds = GetTickCount();
|
|
|
|
#endif
|
|
|
|
now.tv_sec = (long)(milliseconds / 1000);
|
2019-01-02 01:03:11 +08:00
|
|
|
now.tv_usec = (long)((milliseconds % 1000) * 1000);
|
2017-10-23 18:05:49 +08:00
|
|
|
return now;
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
|
|
|
|
|
|
|
|
static struct timeval tvnow(void)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
** clock_gettime() is granted to be increased monotonically when the
|
|
|
|
** monotonic clock is queried. Time starting point is unspecified, it
|
|
|
|
** could be the system start-up time, the Epoch, or something else,
|
|
|
|
** in any case the time starting point does not change once that the
|
|
|
|
** system has started up.
|
|
|
|
*/
|
|
|
|
struct timeval now;
|
|
|
|
struct timespec tsnow;
|
|
|
|
if(0 == clock_gettime(CLOCK_MONOTONIC, &tsnow)) {
|
|
|
|
now.tv_sec = tsnow.tv_sec;
|
2020-07-17 00:52:03 +08:00
|
|
|
now.tv_usec = (int)(tsnow.tv_nsec / 1000);
|
2017-10-23 18:05:49 +08:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
** Even when the configure process has truly detected monotonic clock
|
|
|
|
** availability, it might happen that it is not actually available at
|
|
|
|
** run-time. When this occurs simply fallback to other time source.
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_GETTIMEOFDAY
|
|
|
|
else
|
|
|
|
(void)gettimeofday(&now, NULL);
|
|
|
|
#else
|
|
|
|
else {
|
2021-07-21 10:23:05 +08:00
|
|
|
now.tv_sec = time(NULL);
|
2017-10-23 18:05:49 +08:00
|
|
|
now.tv_usec = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return now;
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(HAVE_GETTIMEOFDAY)
|
|
|
|
|
|
|
|
static struct timeval tvnow(void)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
** gettimeofday() is not granted to be increased monotonically, due to
|
|
|
|
** clock drifting and external source time synchronization it can jump
|
|
|
|
** forward or backward in time.
|
|
|
|
*/
|
|
|
|
struct timeval now;
|
|
|
|
(void)gettimeofday(&now, NULL);
|
|
|
|
return now;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
static struct timeval tvnow(void)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
** time() returns the value of time in seconds since the Epoch.
|
|
|
|
*/
|
|
|
|
struct timeval now;
|
2021-07-21 10:23:05 +08:00
|
|
|
now.tv_sec = time(NULL);
|
2017-10-23 18:05:49 +08:00
|
|
|
now.tv_usec = 0;
|
|
|
|
return now;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
long timediff(struct timeval newer, struct timeval older)
|
|
|
|
{
|
|
|
|
timediff_t diff = newer.tv_sec-older.tv_sec;
|
|
|
|
if(diff >= (LONG_MAX/1000))
|
|
|
|
return LONG_MAX;
|
|
|
|
else if(diff <= (LONG_MIN/1000))
|
|
|
|
return LONG_MIN;
|
|
|
|
return (long)(newer.tv_sec-older.tv_sec)*1000+
|
|
|
|
(long)(newer.tv_usec-older.tv_usec)/1000;
|
|
|
|
}
|
2020-04-11 14:35:05 +08:00
|
|
|
|
|
|
|
/* vars used to keep around previous signal handlers */
|
|
|
|
|
2021-04-07 17:37:15 +08:00
|
|
|
typedef void (*SIGHANDLER_T)(int);
|
2020-04-11 14:35:05 +08:00
|
|
|
|
|
|
|
#ifdef SIGHUP
|
|
|
|
static SIGHANDLER_T old_sighup_handler = SIG_ERR;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef SIGPIPE
|
|
|
|
static SIGHANDLER_T old_sigpipe_handler = SIG_ERR;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef SIGALRM
|
|
|
|
static SIGHANDLER_T old_sigalrm_handler = SIG_ERR;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef SIGINT
|
|
|
|
static SIGHANDLER_T old_sigint_handler = SIG_ERR;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef SIGTERM
|
|
|
|
static SIGHANDLER_T old_sigterm_handler = SIG_ERR;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(SIGBREAK) && defined(WIN32)
|
|
|
|
static SIGHANDLER_T old_sigbreak_handler = SIG_ERR;
|
|
|
|
#endif
|
|
|
|
|
2020-04-13 00:38:12 +08:00
|
|
|
#ifdef WIN32
|
2022-10-12 06:21:23 +08:00
|
|
|
#ifdef _WIN32_WCE
|
2020-04-13 00:38:12 +08:00
|
|
|
static DWORD thread_main_id = 0;
|
2022-10-12 06:21:23 +08:00
|
|
|
#else
|
|
|
|
static unsigned int thread_main_id = 0;
|
|
|
|
#endif
|
2020-04-13 00:38:12 +08:00
|
|
|
static HANDLE thread_main_window = NULL;
|
|
|
|
static HWND hidden_main_window = NULL;
|
|
|
|
#endif
|
|
|
|
|
2020-04-11 14:35:05 +08:00
|
|
|
/* var which if set indicates that the program should finish execution */
|
|
|
|
volatile int got_exit_signal = 0;
|
|
|
|
|
|
|
|
/* if next is set indicates the first signal handled in exit_signal_handler */
|
|
|
|
volatile int exit_signal = 0;
|
|
|
|
|
2020-04-19 03:51:19 +08:00
|
|
|
#ifdef WIN32
|
|
|
|
/* event which if set indicates that the program should finish */
|
|
|
|
HANDLE exit_event = NULL;
|
|
|
|
#endif
|
|
|
|
|
2020-04-11 14:35:05 +08:00
|
|
|
/* signal handler that will be triggered to indicate that the program
|
|
|
|
* should finish its execution in a controlled manner as soon as possible.
|
|
|
|
* The first time this is called it will set got_exit_signal to one and
|
|
|
|
* store in exit_signal the signal that triggered its execution.
|
|
|
|
*/
|
2021-04-07 17:37:15 +08:00
|
|
|
static void exit_signal_handler(int signum)
|
2020-04-11 14:35:05 +08:00
|
|
|
{
|
|
|
|
int old_errno = errno;
|
|
|
|
logmsg("exit_signal_handler: %d", signum);
|
|
|
|
if(got_exit_signal == 0) {
|
|
|
|
got_exit_signal = 1;
|
|
|
|
exit_signal = signum;
|
2020-04-19 03:51:19 +08:00
|
|
|
#ifdef WIN32
|
|
|
|
if(exit_event)
|
|
|
|
(void)SetEvent(exit_event);
|
|
|
|
#endif
|
2020-04-11 14:35:05 +08:00
|
|
|
}
|
|
|
|
(void)signal(signum, exit_signal_handler);
|
|
|
|
errno = old_errno;
|
|
|
|
}
|
|
|
|
|
2020-04-12 05:31:55 +08:00
|
|
|
#ifdef WIN32
|
|
|
|
/* CTRL event handler for Windows Console applications to simulate
|
|
|
|
* SIGINT, SIGTERM and SIGBREAK on CTRL events and trigger signal handler.
|
|
|
|
*
|
|
|
|
* Background information from MSDN:
|
|
|
|
* SIGINT is not supported for any Win32 application. When a CTRL+C
|
|
|
|
* interrupt occurs, Win32 operating systems generate a new thread
|
|
|
|
* to specifically handle that interrupt. This can cause a single-thread
|
|
|
|
* application, such as one in UNIX, to become multithreaded and cause
|
|
|
|
* unexpected behavior.
|
|
|
|
* [...]
|
|
|
|
* The SIGILL and SIGTERM signals are not generated under Windows.
|
|
|
|
* They are included for ANSI compatibility. Therefore, you can set
|
|
|
|
* signal handlers for these signals by using signal, and you can also
|
|
|
|
* explicitly generate these signals by calling raise. Source:
|
|
|
|
* https://docs.microsoft.com/de-de/cpp/c-runtime-library/reference/signal
|
|
|
|
*/
|
|
|
|
static BOOL WINAPI ctrl_event_handler(DWORD dwCtrlType)
|
|
|
|
{
|
|
|
|
int signum = 0;
|
|
|
|
logmsg("ctrl_event_handler: %d", dwCtrlType);
|
|
|
|
switch(dwCtrlType) {
|
|
|
|
#ifdef SIGINT
|
|
|
|
case CTRL_C_EVENT: signum = SIGINT; break;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGTERM
|
|
|
|
case CTRL_CLOSE_EVENT: signum = SIGTERM; break;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGBREAK
|
|
|
|
case CTRL_BREAK_EVENT: signum = SIGBREAK; break;
|
|
|
|
#endif
|
|
|
|
default: return FALSE;
|
|
|
|
}
|
|
|
|
if(signum) {
|
|
|
|
logmsg("ctrl_event_handler: %d -> %d", dwCtrlType, signum);
|
2020-04-19 03:42:05 +08:00
|
|
|
raise(signum);
|
2020-04-12 05:31:55 +08:00
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
2020-04-13 00:38:12 +08:00
|
|
|
/* Window message handler for Windows applications to add support
|
|
|
|
* for graceful process termination via taskkill (without /f) which
|
|
|
|
* sends WM_CLOSE to all Windows of a process (even hidden ones).
|
|
|
|
*
|
|
|
|
* Therefore we create and run a hidden Window in a separate thread
|
|
|
|
* to receive and handle the WM_CLOSE message as SIGTERM signal.
|
|
|
|
*/
|
|
|
|
static LRESULT CALLBACK main_window_proc(HWND hwnd, UINT uMsg,
|
|
|
|
WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
|
|
|
int signum = 0;
|
|
|
|
if(hwnd == hidden_main_window) {
|
|
|
|
switch(uMsg) {
|
|
|
|
#ifdef SIGTERM
|
|
|
|
case WM_CLOSE: signum = SIGTERM; break;
|
|
|
|
#endif
|
|
|
|
case WM_DESTROY: PostQuitMessage(0); break;
|
|
|
|
}
|
|
|
|
if(signum) {
|
|
|
|
logmsg("main_window_proc: %d -> %d", uMsg, signum);
|
2020-04-19 03:42:05 +08:00
|
|
|
raise(signum);
|
2020-04-13 00:38:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return DefWindowProc(hwnd, uMsg, wParam, lParam);
|
|
|
|
}
|
|
|
|
/* Window message queue loop for hidden main window, details see above.
|
|
|
|
*/
|
2022-10-12 06:21:23 +08:00
|
|
|
#ifdef _WIN32_WCE
|
2020-04-13 00:38:12 +08:00
|
|
|
static DWORD WINAPI main_window_loop(LPVOID lpParameter)
|
2022-10-12 06:21:23 +08:00
|
|
|
#else
|
|
|
|
#include <process.h>
|
|
|
|
static unsigned int WINAPI main_window_loop(void *lpParameter)
|
|
|
|
#endif
|
2020-04-13 00:38:12 +08:00
|
|
|
{
|
|
|
|
WNDCLASS wc;
|
|
|
|
BOOL ret;
|
|
|
|
MSG msg;
|
|
|
|
|
|
|
|
ZeroMemory(&wc, sizeof(wc));
|
|
|
|
wc.lpfnWndProc = (WNDPROC)main_window_proc;
|
|
|
|
wc.hInstance = (HINSTANCE)lpParameter;
|
2020-10-14 02:23:40 +08:00
|
|
|
wc.lpszClassName = TEXT("MainWClass");
|
2020-04-13 00:38:12 +08:00
|
|
|
if(!RegisterClass(&wc)) {
|
|
|
|
perror("RegisterClass failed");
|
|
|
|
return (DWORD)-1;
|
|
|
|
}
|
|
|
|
|
2020-10-14 02:23:40 +08:00
|
|
|
hidden_main_window = CreateWindowEx(0, TEXT("MainWClass"),
|
|
|
|
TEXT("Recv WM_CLOSE msg"),
|
2020-04-13 00:38:12 +08:00
|
|
|
WS_OVERLAPPEDWINDOW,
|
|
|
|
CW_USEDEFAULT, CW_USEDEFAULT,
|
|
|
|
CW_USEDEFAULT, CW_USEDEFAULT,
|
|
|
|
(HWND)NULL, (HMENU)NULL,
|
|
|
|
wc.hInstance, (LPVOID)NULL);
|
|
|
|
if(!hidden_main_window) {
|
|
|
|
perror("CreateWindowEx failed");
|
|
|
|
return (DWORD)-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
ret = GetMessage(&msg, NULL, 0, 0);
|
|
|
|
if(ret == -1) {
|
|
|
|
perror("GetMessage failed");
|
|
|
|
return (DWORD)-1;
|
|
|
|
}
|
|
|
|
else if(ret) {
|
|
|
|
if(msg.message == WM_APP) {
|
|
|
|
DestroyWindow(hidden_main_window);
|
|
|
|
}
|
|
|
|
else if(msg.hwnd && !TranslateMessage(&msg)) {
|
|
|
|
DispatchMessage(&msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while(ret);
|
|
|
|
|
|
|
|
hidden_main_window = NULL;
|
|
|
|
return (DWORD)msg.wParam;
|
|
|
|
}
|
2020-04-12 05:31:55 +08:00
|
|
|
#endif
|
|
|
|
|
2021-01-26 10:10:48 +08:00
|
|
|
static SIGHANDLER_T set_signal(int signum, SIGHANDLER_T handler,
|
|
|
|
bool restartable)
|
|
|
|
{
|
|
|
|
#if defined(HAVE_SIGACTION) && defined(SA_RESTART)
|
|
|
|
struct sigaction sa, oldsa;
|
|
|
|
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
sa.sa_handler = handler;
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sigaddset(&sa.sa_mask, signum);
|
|
|
|
sa.sa_flags = restartable? SA_RESTART: 0;
|
|
|
|
|
|
|
|
if(sigaction(signum, &sa, &oldsa))
|
|
|
|
return SIG_ERR;
|
|
|
|
|
|
|
|
return oldsa.sa_handler;
|
|
|
|
#else
|
|
|
|
SIGHANDLER_T oldhdlr = signal(signum, handler);
|
|
|
|
|
|
|
|
#ifdef HAVE_SIGINTERRUPT
|
|
|
|
if(oldhdlr != SIG_ERR)
|
|
|
|
siginterrupt(signum, (int) restartable);
|
|
|
|
#else
|
|
|
|
(void) restartable;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return oldhdlr;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-04-11 14:35:05 +08:00
|
|
|
void install_signal_handlers(bool keep_sigalrm)
|
|
|
|
{
|
2020-04-19 03:51:19 +08:00
|
|
|
#ifdef WIN32
|
2022-10-12 06:21:23 +08:00
|
|
|
#ifdef _WIN32_WCE
|
|
|
|
typedef HANDLE curl_win_thread_handle_t;
|
|
|
|
#elif defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
|
|
|
|
typedef unsigned long curl_win_thread_handle_t;
|
|
|
|
#else
|
|
|
|
typedef uintptr_t curl_win_thread_handle_t;
|
|
|
|
#endif
|
|
|
|
curl_win_thread_handle_t thread;
|
2020-04-19 03:51:19 +08:00
|
|
|
/* setup windows exit event before any signal can trigger */
|
|
|
|
exit_event = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
if(!exit_event)
|
|
|
|
logmsg("cannot create exit event");
|
|
|
|
#endif
|
2020-04-11 14:35:05 +08:00
|
|
|
#ifdef SIGHUP
|
|
|
|
/* ignore SIGHUP signal */
|
2021-01-26 10:10:48 +08:00
|
|
|
old_sighup_handler = set_signal(SIGHUP, SIG_IGN, FALSE);
|
2020-04-11 14:35:05 +08:00
|
|
|
if(old_sighup_handler == SIG_ERR)
|
|
|
|
logmsg("cannot install SIGHUP handler: %s", strerror(errno));
|
|
|
|
#endif
|
|
|
|
#ifdef SIGPIPE
|
|
|
|
/* ignore SIGPIPE signal */
|
2021-01-26 10:10:48 +08:00
|
|
|
old_sigpipe_handler = set_signal(SIGPIPE, SIG_IGN, FALSE);
|
2020-04-11 14:35:05 +08:00
|
|
|
if(old_sigpipe_handler == SIG_ERR)
|
|
|
|
logmsg("cannot install SIGPIPE handler: %s", strerror(errno));
|
|
|
|
#endif
|
|
|
|
#ifdef SIGALRM
|
|
|
|
if(!keep_sigalrm) {
|
|
|
|
/* ignore SIGALRM signal */
|
2021-01-26 10:10:48 +08:00
|
|
|
old_sigalrm_handler = set_signal(SIGALRM, SIG_IGN, FALSE);
|
2020-04-11 14:35:05 +08:00
|
|
|
if(old_sigalrm_handler == SIG_ERR)
|
|
|
|
logmsg("cannot install SIGALRM handler: %s", strerror(errno));
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
(void)keep_sigalrm;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGINT
|
|
|
|
/* handle SIGINT signal with our exit_signal_handler */
|
2021-01-26 10:10:48 +08:00
|
|
|
old_sigint_handler = set_signal(SIGINT, exit_signal_handler, TRUE);
|
2020-04-11 14:35:05 +08:00
|
|
|
if(old_sigint_handler == SIG_ERR)
|
|
|
|
logmsg("cannot install SIGINT handler: %s", strerror(errno));
|
|
|
|
#endif
|
|
|
|
#ifdef SIGTERM
|
|
|
|
/* handle SIGTERM signal with our exit_signal_handler */
|
2021-01-26 10:10:48 +08:00
|
|
|
old_sigterm_handler = set_signal(SIGTERM, exit_signal_handler, TRUE);
|
2020-04-11 14:35:05 +08:00
|
|
|
if(old_sigterm_handler == SIG_ERR)
|
|
|
|
logmsg("cannot install SIGTERM handler: %s", strerror(errno));
|
|
|
|
#endif
|
|
|
|
#if defined(SIGBREAK) && defined(WIN32)
|
|
|
|
/* handle SIGBREAK signal with our exit_signal_handler */
|
2021-01-26 10:10:48 +08:00
|
|
|
old_sigbreak_handler = set_signal(SIGBREAK, exit_signal_handler, TRUE);
|
2020-04-11 14:35:05 +08:00
|
|
|
if(old_sigbreak_handler == SIG_ERR)
|
|
|
|
logmsg("cannot install SIGBREAK handler: %s", strerror(errno));
|
|
|
|
#endif
|
2020-04-12 05:31:55 +08:00
|
|
|
#ifdef WIN32
|
|
|
|
if(!SetConsoleCtrlHandler(ctrl_event_handler, TRUE))
|
|
|
|
logmsg("cannot install CTRL event handler");
|
2022-10-12 06:21:23 +08:00
|
|
|
#ifdef _WIN32_WCE
|
|
|
|
thread = CreateThread(NULL, 0, &main_window_loop,
|
|
|
|
(LPVOID)GetModuleHandle(NULL), 0, &thread_main_id);
|
|
|
|
#else
|
|
|
|
thread = _beginthreadex(NULL, 0, &main_window_loop,
|
|
|
|
(void *)GetModuleHandle(NULL), 0, &thread_main_id);
|
|
|
|
#endif
|
|
|
|
thread_main_window = (HANDLE)thread;
|
2020-04-13 00:38:12 +08:00
|
|
|
if(!thread_main_window || !thread_main_id)
|
|
|
|
logmsg("cannot start main window loop");
|
2020-04-12 05:31:55 +08:00
|
|
|
#endif
|
2020-04-11 14:35:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void restore_signal_handlers(bool keep_sigalrm)
|
|
|
|
{
|
|
|
|
#ifdef SIGHUP
|
|
|
|
if(SIG_ERR != old_sighup_handler)
|
2021-01-26 10:10:48 +08:00
|
|
|
(void) set_signal(SIGHUP, old_sighup_handler, FALSE);
|
2020-04-11 14:35:05 +08:00
|
|
|
#endif
|
|
|
|
#ifdef SIGPIPE
|
|
|
|
if(SIG_ERR != old_sigpipe_handler)
|
2021-01-26 10:10:48 +08:00
|
|
|
(void) set_signal(SIGPIPE, old_sigpipe_handler, FALSE);
|
2020-04-11 14:35:05 +08:00
|
|
|
#endif
|
|
|
|
#ifdef SIGALRM
|
|
|
|
if(!keep_sigalrm) {
|
|
|
|
if(SIG_ERR != old_sigalrm_handler)
|
2021-01-26 10:10:48 +08:00
|
|
|
(void) set_signal(SIGALRM, old_sigalrm_handler, FALSE);
|
2020-04-11 14:35:05 +08:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
(void)keep_sigalrm;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGINT
|
|
|
|
if(SIG_ERR != old_sigint_handler)
|
2021-01-26 10:10:48 +08:00
|
|
|
(void) set_signal(SIGINT, old_sigint_handler, FALSE);
|
2020-04-11 14:35:05 +08:00
|
|
|
#endif
|
|
|
|
#ifdef SIGTERM
|
|
|
|
if(SIG_ERR != old_sigterm_handler)
|
2021-01-26 10:10:48 +08:00
|
|
|
(void) set_signal(SIGTERM, old_sigterm_handler, FALSE);
|
2020-04-11 14:35:05 +08:00
|
|
|
#endif
|
|
|
|
#if defined(SIGBREAK) && defined(WIN32)
|
|
|
|
if(SIG_ERR != old_sigbreak_handler)
|
2021-01-26 10:10:48 +08:00
|
|
|
(void) set_signal(SIGBREAK, old_sigbreak_handler, FALSE);
|
2020-04-11 14:35:05 +08:00
|
|
|
#endif
|
2020-04-12 05:31:55 +08:00
|
|
|
#ifdef WIN32
|
|
|
|
(void)SetConsoleCtrlHandler(ctrl_event_handler, FALSE);
|
2020-04-13 00:38:12 +08:00
|
|
|
if(thread_main_window && thread_main_id) {
|
2020-04-19 03:46:14 +08:00
|
|
|
if(PostThreadMessage(thread_main_id, WM_APP, 0, 0)) {
|
|
|
|
if(WaitForSingleObjectEx(thread_main_window, INFINITE, TRUE)) {
|
|
|
|
if(CloseHandle(thread_main_window)) {
|
|
|
|
thread_main_window = NULL;
|
|
|
|
thread_main_id = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-19 03:51:19 +08:00
|
|
|
if(exit_event) {
|
|
|
|
if(CloseHandle(exit_event)) {
|
|
|
|
exit_event = NULL;
|
|
|
|
}
|
2020-04-13 00:38:12 +08:00
|
|
|
}
|
2020-04-12 05:31:55 +08:00
|
|
|
#endif
|
2020-04-11 14:35:05 +08:00
|
|
|
}
|
2022-04-17 17:43:36 +08:00
|
|
|
|
|
|
|
#ifdef USE_UNIX_SOCKETS
|
|
|
|
|
|
|
|
int bind_unix_socket(curl_socket_t sock, const char *unix_socket,
|
|
|
|
struct sockaddr_un *sau) {
|
|
|
|
int error;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
memset(sau, 0, sizeof(struct sockaddr_un));
|
|
|
|
sau->sun_family = AF_UNIX;
|
|
|
|
strncpy(sau->sun_path, unix_socket, sizeof(sau->sun_path) - 1);
|
|
|
|
rc = bind(sock, (struct sockaddr*)sau, sizeof(struct sockaddr_un));
|
|
|
|
if(0 != rc && errno == EADDRINUSE) {
|
|
|
|
struct_stat statbuf;
|
|
|
|
/* socket already exists. Perhaps it is stale? */
|
|
|
|
curl_socket_t unixfd = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if(CURL_SOCKET_BAD == unixfd) {
|
|
|
|
error = SOCKERRNO;
|
|
|
|
logmsg("Error binding socket, failed to create socket at %s: (%d) %s",
|
|
|
|
unix_socket, error, strerror(error));
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
/* check whether the server is alive */
|
|
|
|
rc = connect(unixfd, (struct sockaddr*)sau, sizeof(struct sockaddr_un));
|
|
|
|
error = errno;
|
|
|
|
sclose(unixfd);
|
|
|
|
if(ECONNREFUSED != error) {
|
|
|
|
logmsg("Error binding socket, failed to connect to %s: (%d) %s",
|
|
|
|
unix_socket, error, strerror(error));
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
/* socket server is not alive, now check if it was actually a socket. */
|
|
|
|
#ifdef WIN32
|
|
|
|
/* Windows does not have lstat function. */
|
|
|
|
rc = curlx_win32_stat(unix_socket, &statbuf);
|
|
|
|
#else
|
|
|
|
rc = lstat(unix_socket, &statbuf);
|
|
|
|
#endif
|
|
|
|
if(0 != rc) {
|
|
|
|
logmsg("Error binding socket, failed to stat %s: (%d) %s",
|
|
|
|
unix_socket, errno, strerror(errno));
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
#ifdef S_IFSOCK
|
|
|
|
if((statbuf.st_mode & S_IFSOCK) != S_IFSOCK) {
|
|
|
|
logmsg("Error binding socket, failed to stat %s: (%d) %s",
|
|
|
|
unix_socket, error, strerror(error));
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* dead socket, cleanup and retry bind */
|
|
|
|
rc = unlink(unix_socket);
|
|
|
|
if(0 != rc) {
|
|
|
|
logmsg("Error binding socket, failed to unlink %s: (%d) %s",
|
|
|
|
unix_socket, errno, strerror(errno));
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
/* stale socket is gone, retry bind */
|
|
|
|
rc = bind(sock, (struct sockaddr*)sau, sizeof(struct sockaddr_un));
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
#endif
|