2005-04-30 19:30:55 -04:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2017-07-28 09:49:36 -04:00
|
|
|
* Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2005-04-30 19:30:55 -04:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2016-02-02 18:19:02 -05:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2005-04-30 19:30:55 -04: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.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
2012-04-10 11:32:06 -04:00
|
|
|
#include "server_setup.h"
|
2005-04-30 19:30:55 -04:00
|
|
|
|
2008-02-27 19:55:06 -05:00
|
|
|
#ifdef HAVE_SIGNAL_H
|
2005-04-30 19:30:55 -04:00
|
|
|
#include <signal.h>
|
2008-02-27 19:55:06 -05:00
|
|
|
#endif
|
2005-04-30 19:30:55 -04: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 02:45:54 -04:00
|
|
|
#ifdef HAVE_POLL_H
|
2008-01-22 09:52:54 -05:00
|
|
|
#include <poll.h>
|
2017-04-12 02:45:54 -04:00
|
|
|
#elif defined(HAVE_SYS_POLL_H)
|
|
|
|
#include <sys/poll.h>
|
2007-07-11 21:07:49 -04:00
|
|
|
#endif
|
2017-04-02 13:22:42 -04:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
#include <w32api.h>
|
|
|
|
#endif
|
2005-04-30 19:30:55 -04:00
|
|
|
|
2005-05-01 08:56:09 -04:00
|
|
|
#define ENABLE_CURLX_PRINTF
|
|
|
|
/* make the curlx header define all printf() functions to use the curlx_*
|
|
|
|
versions instead */
|
2005-04-30 19:30:55 -04:00
|
|
|
#include "curlx.h" /* from the private lib dir */
|
|
|
|
#include "getpart.h"
|
|
|
|
#include "util.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "timeval.h"
|
2005-04-30 19:30:55 -04:00
|
|
|
|
2013-01-09 09:10:23 -05:00
|
|
|
#ifdef USE_WINSOCK
|
|
|
|
#undef EINTR
|
|
|
|
#define EINTR 4 /* errno.h value */
|
|
|
|
#undef EINVAL
|
|
|
|
#define EINVAL 22 /* errno.h value */
|
|
|
|
#endif
|
|
|
|
|
2017-04-02 13:22:42 -04:00
|
|
|
/* MinGW with w32api version < 3.6 declared in6addr_any as extern,
|
|
|
|
but lacked the definition */
|
2006-09-13 09:41:53 -04:00
|
|
|
#if defined(ENABLE_IPV6) && defined(__MINGW32__)
|
2017-04-02 13:22:42 -04:00
|
|
|
#if (__W32API_MAJOR_VERSION < 3) || \
|
|
|
|
((__W32API_MAJOR_VERSION == 3) && (__W32API_MINOR_VERSION < 6))
|
2006-09-13 09:41:53 -04:00
|
|
|
const struct in6_addr in6addr_any = {{ IN6ADDR_ANY_INIT }};
|
2017-04-02 13:22:42 -04:00
|
|
|
#endif /* w32api < 3.6 */
|
|
|
|
#endif /* ENABLE_IPV6 && __MINGW32__*/
|
2006-09-13 09:41:53 -04:00
|
|
|
|
2017-10-23 06:05:49 -04:00
|
|
|
static struct timeval tvnow(void);
|
|
|
|
|
2011-12-17 17:47:22 -05: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-09 17:09:06 -04:00
|
|
|
for(i = 0; i < len; i++) {
|
2011-12-17 17:47:22 -05:00
|
|
|
if((data[i] >= 0x20) && (data[i] < 0x7f))
|
|
|
|
*optr++ = *iptr++;
|
|
|
|
else {
|
2016-04-03 16:35:43 -04:00
|
|
|
snprintf(optr, 4, "%%%02x", *iptr++);
|
2017-09-09 17:09:06 -04:00
|
|
|
optr += 3;
|
2011-12-17 17:47:22 -05:00
|
|
|
}
|
|
|
|
}
|
2017-09-09 17:09:06 -04:00
|
|
|
*optr = 0; /* in case no sprintf was used */
|
2011-12-17 17:47:22 -05:00
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2005-04-30 19:30:55 -04:00
|
|
|
void logmsg(const char *msg, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
2008-09-25 20:17:01 -04:00
|
|
|
char buffer[2048 + 1];
|
2005-04-30 19:30:55 -04:00
|
|
|
FILE *logfp;
|
2007-02-16 14:41:25 -05:00
|
|
|
int error;
|
2017-10-23 06:05:49 -04:00
|
|
|
struct timeval tv;
|
2007-02-16 20:25:32 -05:00
|
|
|
time_t sec;
|
|
|
|
struct tm *now;
|
|
|
|
char timebuf[20];
|
2010-02-25 01:59:04 -05:00
|
|
|
static time_t epoch_offset;
|
|
|
|
static int known_offset;
|
2007-02-16 14:41:25 -05:00
|
|
|
|
2016-04-03 16:35:43 -04:00
|
|
|
if(!serverlogfile) {
|
2007-02-16 14:41:25 -05:00
|
|
|
fprintf(stderr, "Error: serverlogfile not set\n");
|
|
|
|
return;
|
|
|
|
}
|
2005-04-30 19:30:55 -04:00
|
|
|
|
2017-10-23 06:05:49 -04:00
|
|
|
tv = tvnow();
|
2010-02-25 01:59:04 -05:00
|
|
|
if(!known_offset) {
|
|
|
|
epoch_offset = time(NULL) - tv.tv_sec;
|
|
|
|
known_offset = 1;
|
|
|
|
}
|
|
|
|
sec = epoch_offset + tv.tv_sec;
|
|
|
|
now = localtime(&sec); /* not thread safe but we don't care */
|
2005-04-30 19:30:55 -04:00
|
|
|
|
2005-05-25 08:04:52 -04:00
|
|
|
snprintf(timebuf, sizeof(timebuf), "%02d:%02d:%02d.%06ld",
|
2008-09-25 20:17:01 -04:00
|
|
|
(int)now->tm_hour, (int)now->tm_min, (int)now->tm_sec, (long)tv.tv_usec);
|
2005-04-30 19:30:55 -04:00
|
|
|
|
|
|
|
va_start(ap, msg);
|
2008-09-25 20:17:01 -04:00
|
|
|
vsnprintf(buffer, sizeof(buffer), msg, ap);
|
2005-04-30 19:30:55 -04:00
|
|
|
va_end(ap);
|
|
|
|
|
2009-12-16 10:16:06 -05:00
|
|
|
logfp = fopen(serverlogfile, "ab");
|
2005-05-25 08:04:52 -04:00
|
|
|
if(logfp) {
|
|
|
|
fprintf(logfp, "%s %s\n", timebuf, buffer);
|
2005-04-30 19:30:55 -04:00
|
|
|
fclose(logfp);
|
2005-05-25 08:04:52 -04:00
|
|
|
}
|
2007-02-16 14:41:25 -05:00
|
|
|
else {
|
2013-01-09 09:10:23 -05:00
|
|
|
error = errno;
|
2007-02-16 14:41:25 -05: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-04-30 19:30:55 -04:00
|
|
|
}
|
2005-05-17 06:22:22 -04:00
|
|
|
|
2006-10-11 12:01:16 -04:00
|
|
|
#ifdef WIN32
|
2005-05-17 06:22:22 -04:00
|
|
|
/* use instead of perror() on generic windows */
|
2016-12-13 17:34:59 -05:00
|
|
|
void win32_perror(const char *msg)
|
2005-05-17 06:22:22 -04:00
|
|
|
{
|
2007-02-16 14:41:25 -05:00
|
|
|
char buf[512];
|
2007-02-16 13:19:35 -05:00
|
|
|
DWORD err = SOCKERRNO;
|
2005-05-17 06:22:22 -04:00
|
|
|
|
2016-04-03 16:35:43 -04:00
|
|
|
if(!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
|
|
|
|
LANG_NEUTRAL, buf, sizeof(buf), NULL))
|
2005-05-17 06:22:22 -04:00
|
|
|
snprintf(buf, sizeof(buf), "Unknown error %lu (%#lx)", err, err);
|
2016-04-03 16:35:43 -04:00
|
|
|
if(msg)
|
|
|
|
fprintf(stderr, "%s: ", msg);
|
2005-05-17 06:22:22 -04:00
|
|
|
fprintf(stderr, "%s\n", buf);
|
|
|
|
}
|
2006-10-18 17:05:40 -04:00
|
|
|
#endif /* WIN32 */
|
2005-05-17 06:22:22 -04:00
|
|
|
|
2006-10-18 17:05:40 -04:00
|
|
|
#ifdef USE_WINSOCK
|
2005-05-17 06:22:22 -04:00
|
|
|
void win32_init(void)
|
|
|
|
{
|
|
|
|
WORD wVersionRequested;
|
|
|
|
WSADATA wsaData;
|
|
|
|
int err;
|
2006-10-18 17:05:40 -04:00
|
|
|
wVersionRequested = MAKEWORD(USE_WINSOCK, USE_WINSOCK);
|
2005-05-17 06:22:22 -04:00
|
|
|
|
|
|
|
err = WSAStartup(wVersionRequested, &wsaData);
|
|
|
|
|
2016-04-03 16:35:43 -04:00
|
|
|
if(err != 0) {
|
2005-05-17 06:22:22 -04:00
|
|
|
perror("Winsock init failed");
|
2007-02-18 21:03:58 -05:00
|
|
|
logmsg("Error initialising winsock -- aborting");
|
2005-05-17 06:22:22 -04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-04-03 16:35:43 -04:00
|
|
|
if(LOBYTE(wsaData.wVersion) != USE_WINSOCK ||
|
|
|
|
HIBYTE(wsaData.wVersion) != USE_WINSOCK) {
|
2005-05-17 06:22:22 -04:00
|
|
|
WSACleanup();
|
|
|
|
perror("Winsock init failed");
|
2007-02-18 21:03:58 -05:00
|
|
|
logmsg("No suitable winsock.dll found -- aborting");
|
2005-05-17 06:22:22 -04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void win32_cleanup(void)
|
|
|
|
{
|
|
|
|
WSACleanup();
|
|
|
|
}
|
2006-10-18 17:05:40 -04:00
|
|
|
#endif /* USE_WINSOCK */
|
2005-05-17 06:22:22 -04:00
|
|
|
|
2005-09-15 16:22:43 -04:00
|
|
|
/* set by the main code to point to where the test dir is */
|
2017-09-09 17:09:06 -04:00
|
|
|
const char *path = ".";
|
2005-09-15 16:22:43 -04:00
|
|
|
|
|
|
|
char *test2file(long testno)
|
|
|
|
{
|
|
|
|
static char filename[256];
|
|
|
|
snprintf(filename, sizeof(filename), TEST_DATA_PATH, path, testno);
|
|
|
|
return filename;
|
|
|
|
}
|
2007-07-11 21:07:49 -04:00
|
|
|
|
2008-02-18 15:13:30 -05: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-11 21:07:49 -04:00
|
|
|
{
|
2008-02-18 15:13:30 -05:00
|
|
|
#if !defined(MSDOS) && !defined(USE_WINSOCK)
|
|
|
|
#ifndef HAVE_POLL_FINE
|
2017-08-02 05:53:27 -04:00
|
|
|
struct timeval pending_tv;
|
2007-07-11 21:07:49 -04:00
|
|
|
#endif
|
2017-10-23 06:05:49 -04:00
|
|
|
struct timeval initial_tv;
|
2008-02-18 15:13:30 -05:00
|
|
|
int pending_ms;
|
|
|
|
int error;
|
2007-07-11 21:07:49 -04:00
|
|
|
#endif
|
2008-02-18 15:13:30 -05:00
|
|
|
int r = 0;
|
2007-07-11 21:07:49 -04:00
|
|
|
|
2008-02-18 15:13:30 -05:00
|
|
|
if(!timeout_ms)
|
|
|
|
return 0;
|
|
|
|
if(timeout_ms < 0) {
|
2013-01-09 09:10:23 -05:00
|
|
|
errno = EINVAL;
|
2008-02-18 15:13:30 -05:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#if defined(MSDOS)
|
|
|
|
delay(timeout_ms);
|
|
|
|
#elif defined(USE_WINSOCK)
|
|
|
|
Sleep(timeout_ms);
|
|
|
|
#else
|
|
|
|
pending_ms = timeout_ms;
|
2017-10-23 06:05:49 -04:00
|
|
|
initial_tv = tvnow();
|
2008-02-18 15:13:30 -05:00
|
|
|
do {
|
|
|
|
#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 09:10:23 -05:00
|
|
|
error = errno;
|
2009-12-09 13:41:43 -05:00
|
|
|
if(error && (error != EINTR))
|
2008-02-18 15:13:30 -05:00
|
|
|
break;
|
2017-10-23 06:05:49 -04:00
|
|
|
pending_ms = timeout_ms - (int)timediff(tvnow(), initial_tv);
|
2008-02-18 15:13:30 -05:00
|
|
|
if(pending_ms <= 0)
|
|
|
|
break;
|
|
|
|
} while(r == -1);
|
|
|
|
#endif /* USE_WINSOCK */
|
|
|
|
if(r)
|
|
|
|
r = -1;
|
|
|
|
return r;
|
|
|
|
}
|
2007-07-11 21:07:49 -04:00
|
|
|
|
2008-02-28 04:38:32 -05:00
|
|
|
int write_pidfile(const char *filename)
|
2008-02-26 10:06:44 -05:00
|
|
|
{
|
|
|
|
FILE *pidfile;
|
|
|
|
long pid;
|
|
|
|
|
|
|
|
pid = (long)getpid();
|
2009-12-16 10:16:06 -05:00
|
|
|
pidfile = fopen(filename, "wb");
|
2008-02-26 10:06:44 -05:00
|
|
|
if(!pidfile) {
|
2013-01-09 09:10:23 -05:00
|
|
|
logmsg("Couldn't write pid file: %s %s", filename, strerror(errno));
|
2008-02-28 04:38:32 -05:00
|
|
|
return 0; /* fail */
|
2008-02-26 10:06:44 -05:00
|
|
|
}
|
|
|
|
fprintf(pidfile, "%ld\n", pid);
|
|
|
|
fclose(pidfile);
|
|
|
|
logmsg("Wrote pid %ld to %s", pid, filename);
|
2008-02-28 04:38:32 -05:00
|
|
|
return 1; /* success */
|
2008-02-26 10:06:44 -05:00
|
|
|
}
|
2008-04-23 19:55:34 -04:00
|
|
|
|
|
|
|
void set_advisor_read_lock(const char *filename)
|
|
|
|
{
|
|
|
|
FILE *lockfile;
|
2008-10-01 13:34:24 -04:00
|
|
|
int error = 0;
|
2008-04-23 19:55:34 -04:00
|
|
|
int res;
|
|
|
|
|
|
|
|
do {
|
|
|
|
lockfile = fopen(filename, "wb");
|
2013-01-09 09:10:23 -05:00
|
|
|
} while((lockfile == NULL) && ((error = errno) == EINTR));
|
2008-04-23 19:55:34 -04:00
|
|
|
if(lockfile == NULL) {
|
|
|
|
logmsg("Error creating lock file %s error: %d %s",
|
|
|
|
filename, error, strerror(error));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
res = fclose(lockfile);
|
2013-01-09 09:10:23 -05:00
|
|
|
} while(res && ((error = errno) == EINTR));
|
2008-04-23 19:55:34 -04: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-01 13:34:24 -04:00
|
|
|
int error = 0;
|
2008-04-23 19:55:34 -04:00
|
|
|
int res;
|
|
|
|
|
2009-11-26 05:15:08 -05: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-23 19:55:34 -04:00
|
|
|
do {
|
|
|
|
res = unlink(filename);
|
2013-01-09 09:10:23 -05:00
|
|
|
} while(res && ((error = errno) == EINTR));
|
2008-04-23 19:55:34 -04:00
|
|
|
if(res)
|
|
|
|
logmsg("Error removing lock file %s error: %d %s",
|
|
|
|
filename, error, strerror(error));
|
|
|
|
}
|
2016-10-31 18:49:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
/* Portable, consistent toupper (remember EBCDIC). Do not use toupper() because
|
|
|
|
its behavior is altered by the current locale. */
|
|
|
|
static char raw_toupper(char in)
|
|
|
|
{
|
|
|
|
#if !defined(CURL_DOES_CONVERSIONS)
|
|
|
|
if(in >= 'a' && in <= 'z')
|
|
|
|
return (char)('A' + in - 'a');
|
|
|
|
#else
|
2016-12-13 17:34:59 -05:00
|
|
|
switch(in) {
|
2016-10-31 18:49:54 -04:00
|
|
|
case 'a':
|
|
|
|
return 'A';
|
|
|
|
case 'b':
|
|
|
|
return 'B';
|
|
|
|
case 'c':
|
|
|
|
return 'C';
|
|
|
|
case 'd':
|
|
|
|
return 'D';
|
|
|
|
case 'e':
|
|
|
|
return 'E';
|
|
|
|
case 'f':
|
|
|
|
return 'F';
|
|
|
|
case 'g':
|
|
|
|
return 'G';
|
|
|
|
case 'h':
|
|
|
|
return 'H';
|
|
|
|
case 'i':
|
|
|
|
return 'I';
|
|
|
|
case 'j':
|
|
|
|
return 'J';
|
|
|
|
case 'k':
|
|
|
|
return 'K';
|
|
|
|
case 'l':
|
|
|
|
return 'L';
|
|
|
|
case 'm':
|
|
|
|
return 'M';
|
|
|
|
case 'n':
|
|
|
|
return 'N';
|
|
|
|
case 'o':
|
|
|
|
return 'O';
|
|
|
|
case 'p':
|
|
|
|
return 'P';
|
|
|
|
case 'q':
|
|
|
|
return 'Q';
|
|
|
|
case 'r':
|
|
|
|
return 'R';
|
|
|
|
case 's':
|
|
|
|
return 'S';
|
|
|
|
case 't':
|
|
|
|
return 'T';
|
|
|
|
case 'u':
|
|
|
|
return 'U';
|
|
|
|
case 'v':
|
|
|
|
return 'V';
|
|
|
|
case 'w':
|
|
|
|
return 'W';
|
|
|
|
case 'x':
|
|
|
|
return 'X';
|
|
|
|
case 'y':
|
|
|
|
return 'Y';
|
|
|
|
case 'z':
|
|
|
|
return 'Z';
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
int strncasecompare(const char *first, const char *second, size_t max)
|
|
|
|
{
|
|
|
|
while(*first && *second && max) {
|
|
|
|
if(raw_toupper(*first) != raw_toupper(*second)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
max--;
|
|
|
|
first++;
|
|
|
|
second++;
|
|
|
|
}
|
|
|
|
if(0 == max)
|
|
|
|
return 1; /* they are equal this far */
|
|
|
|
|
|
|
|
return raw_toupper(*first) == raw_toupper(*second);
|
|
|
|
}
|
2017-10-23 06:05:49 -04: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;
|
|
|
|
#if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
|
|
|
|
ULONGLONG milliseconds = GetTickCount64();
|
|
|
|
#else
|
|
|
|
DWORD milliseconds = GetTickCount();
|
|
|
|
#endif
|
|
|
|
now.tv_sec = (long)(milliseconds / 1000);
|
|
|
|
now.tv_usec = (milliseconds % 1000) * 1000;
|
|
|
|
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;
|
|
|
|
now.tv_usec = tsnow.tv_nsec / 1000;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
** 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 {
|
|
|
|
now.tv_sec = (long)time(NULL);
|
|
|
|
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;
|
|
|
|
now.tv_sec = (long)time(NULL);
|
|
|
|
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;
|
|
|
|
}
|