| 1 | /*************************************************************************** | 
|---|
| 2 | *                                  _   _ ____  _ | 
|---|
| 3 | *  Project                     ___| | | |  _ \| | | 
|---|
| 4 | *                             / __| | | | |_) | | | 
|---|
| 5 | *                            | (__| |_| |  _ <| |___ | 
|---|
| 6 | *                             \___|\___/|_| \_\_____| | 
|---|
| 7 | * | 
|---|
| 8 | * Copyright (C) 1998 - 2021, 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 | ***************************************************************************/ | 
|---|
| 22 | #include "server_setup.h" | 
|---|
| 23 |  | 
|---|
| 24 | #ifdef HAVE_SIGNAL_H | 
|---|
| 25 | #include <signal.h> | 
|---|
| 26 | #endif | 
|---|
| 27 | #ifdef HAVE_NETINET_IN_H | 
|---|
| 28 | #include <netinet/in.h> | 
|---|
| 29 | #endif | 
|---|
| 30 | #ifdef _XOPEN_SOURCE_EXTENDED | 
|---|
| 31 | /* This define is "almost" required to build on HPUX 11 */ | 
|---|
| 32 | #include <arpa/inet.h> | 
|---|
| 33 | #endif | 
|---|
| 34 | #ifdef HAVE_NETDB_H | 
|---|
| 35 | #include <netdb.h> | 
|---|
| 36 | #endif | 
|---|
| 37 | #ifdef HAVE_POLL_H | 
|---|
| 38 | #include <poll.h> | 
|---|
| 39 | #elif defined(HAVE_SYS_POLL_H) | 
|---|
| 40 | #include <sys/poll.h> | 
|---|
| 41 | #endif | 
|---|
| 42 | #ifdef __MINGW32__ | 
|---|
| 43 | #include <w32api.h> | 
|---|
| 44 | #endif | 
|---|
| 45 |  | 
|---|
| 46 | #define ENABLE_CURLX_PRINTF | 
|---|
| 47 | /* make the curlx header define all printf() functions to use the curlx_* | 
|---|
| 48 | versions instead */ | 
|---|
| 49 | #include "curlx.h" /* from the private lib dir */ | 
|---|
| 50 | #include "getpart.h" | 
|---|
| 51 | #include "util.h" | 
|---|
| 52 | #include "timeval.h" | 
|---|
| 53 |  | 
|---|
| 54 | #ifdef USE_WINSOCK | 
|---|
| 55 | #undef  EINTR | 
|---|
| 56 | #define EINTR    4 /* errno.h value */ | 
|---|
| 57 | #undef  EINVAL | 
|---|
| 58 | #define EINVAL  22 /* errno.h value */ | 
|---|
| 59 | #endif | 
|---|
| 60 |  | 
|---|
| 61 | /* MinGW with w32api version < 3.6 declared in6addr_any as extern, | 
|---|
| 62 | but lacked the definition */ | 
|---|
| 63 | #if defined(ENABLE_IPV6) && defined(__MINGW32__) | 
|---|
| 64 | #if (__W32API_MAJOR_VERSION < 3) || \ | 
|---|
| 65 | ((__W32API_MAJOR_VERSION == 3) && (__W32API_MINOR_VERSION < 6)) | 
|---|
| 66 | const struct in6_addr in6addr_any = {{ IN6ADDR_ANY_INIT }}; | 
|---|
| 67 | #endif /* w32api < 3.6 */ | 
|---|
| 68 | #endif /* ENABLE_IPV6 && __MINGW32__*/ | 
|---|
| 69 |  | 
|---|
| 70 | static struct timeval tvnow(void); | 
|---|
| 71 |  | 
|---|
| 72 | /* This function returns a pointer to STATIC memory. It converts the given | 
|---|
| 73 | * binary lump to a hex formatted string usable for output in logs or | 
|---|
| 74 | * whatever. | 
|---|
| 75 | */ | 
|---|
| 76 | char *data_to_hex(char *data, size_t len) | 
|---|
| 77 | { | 
|---|
| 78 | static char buf[256*3]; | 
|---|
| 79 | size_t i; | 
|---|
| 80 | char *optr = buf; | 
|---|
| 81 | char *iptr = data; | 
|---|
| 82 |  | 
|---|
| 83 | if(len > 255) | 
|---|
| 84 | len = 255; | 
|---|
| 85 |  | 
|---|
| 86 | for(i = 0; i < len; i++) { | 
|---|
| 87 | if((data[i] >= 0x20) && (data[i] < 0x7f)) | 
|---|
| 88 | *optr++ = *iptr++; | 
|---|
| 89 | else { | 
|---|
| 90 | msnprintf(optr, 4, "%%%02x", *iptr++); | 
|---|
| 91 | optr += 3; | 
|---|
| 92 | } | 
|---|
| 93 | } | 
|---|
| 94 | *optr = 0; /* in case no sprintf was used */ | 
|---|
| 95 |  | 
|---|
| 96 | return buf; | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | void logmsg(const char *msg, ...) | 
|---|
| 100 | { | 
|---|
| 101 | va_list ap; | 
|---|
| 102 | char buffer[2048 + 1]; | 
|---|
| 103 | FILE *logfp; | 
|---|
| 104 | struct timeval tv; | 
|---|
| 105 | time_t sec; | 
|---|
| 106 | struct tm *now; | 
|---|
| 107 | char timebuf[20]; | 
|---|
| 108 | static time_t epoch_offset; | 
|---|
| 109 | static int    known_offset; | 
|---|
| 110 |  | 
|---|
| 111 | if(!serverlogfile) { | 
|---|
| 112 | fprintf(stderr, "Error: serverlogfile not set\n"); | 
|---|
| 113 | return; | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | tv = tvnow(); | 
|---|
| 117 | if(!known_offset) { | 
|---|
| 118 | epoch_offset = time(NULL) - tv.tv_sec; | 
|---|
| 119 | known_offset = 1; | 
|---|
| 120 | } | 
|---|
| 121 | sec = epoch_offset + tv.tv_sec; | 
|---|
| 122 | /* !checksrc! disable BANNEDFUNC 1 */ | 
|---|
| 123 | now = localtime(&sec); /* not thread safe but we don't care */ | 
|---|
| 124 |  | 
|---|
| 125 | msnprintf(timebuf, sizeof(timebuf), "%02d:%02d:%02d.%06ld", | 
|---|
| 126 | (int)now->tm_hour, (int)now->tm_min, (int)now->tm_sec, | 
|---|
| 127 | (long)tv.tv_usec); | 
|---|
| 128 |  | 
|---|
| 129 | va_start(ap, msg); | 
|---|
| 130 | mvsnprintf(buffer, sizeof(buffer), msg, ap); | 
|---|
| 131 | va_end(ap); | 
|---|
| 132 |  | 
|---|
| 133 | logfp = fopen(serverlogfile, "ab"); | 
|---|
| 134 | if(logfp) { | 
|---|
| 135 | fprintf(logfp, "%s %s\n", timebuf, buffer); | 
|---|
| 136 | fclose(logfp); | 
|---|
| 137 | } | 
|---|
| 138 | else { | 
|---|
| 139 | int error = errno; | 
|---|
| 140 | fprintf(stderr, "fopen() failed with error: %d %s\n", | 
|---|
| 141 | error, strerror(error)); | 
|---|
| 142 | fprintf(stderr, "Error opening file: %s\n", serverlogfile); | 
|---|
| 143 | fprintf(stderr, "Msg not logged: %s %s\n", timebuf, buffer); | 
|---|
| 144 | } | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | #ifdef WIN32 | 
|---|
| 148 | /* use instead of perror() on generic windows */ | 
|---|
| 149 | void win32_perror(const char *msg) | 
|---|
| 150 | { | 
|---|
| 151 | char buf[512]; | 
|---|
| 152 | DWORD err = SOCKERRNO; | 
|---|
| 153 |  | 
|---|
| 154 | if(!FormatMessageA((FORMAT_MESSAGE_FROM_SYSTEM | | 
|---|
| 155 | FORMAT_MESSAGE_IGNORE_INSERTS), NULL, err, | 
|---|
| 156 | LANG_NEUTRAL, buf, sizeof(buf), NULL)) | 
|---|
| 157 | msnprintf(buf, sizeof(buf), "Unknown error %lu (%#lx)", err, err); | 
|---|
| 158 | if(msg) | 
|---|
| 159 | fprintf(stderr, "%s: ", msg); | 
|---|
| 160 | fprintf(stderr, "%s\n", buf); | 
|---|
| 161 | } | 
|---|
| 162 | #endif  /* WIN32 */ | 
|---|
| 163 |  | 
|---|
| 164 | #ifdef USE_WINSOCK | 
|---|
| 165 | void win32_init(void) | 
|---|
| 166 | { | 
|---|
| 167 | WORD wVersionRequested; | 
|---|
| 168 | WSADATA wsaData; | 
|---|
| 169 | int err; | 
|---|
| 170 |  | 
|---|
| 171 | wVersionRequested = MAKEWORD(2, 2); | 
|---|
| 172 | err = WSAStartup(wVersionRequested, &wsaData); | 
|---|
| 173 |  | 
|---|
| 174 | if(err) { | 
|---|
| 175 | perror( "Winsock init failed"); | 
|---|
| 176 | logmsg( "Error initialising winsock -- aborting"); | 
|---|
| 177 | exit(1); | 
|---|
| 178 | } | 
|---|
| 179 |  | 
|---|
| 180 | if(LOBYTE(wsaData.wVersion) != LOBYTE(wVersionRequested) || | 
|---|
| 181 | HIBYTE(wsaData.wVersion) != HIBYTE(wVersionRequested) ) { | 
|---|
| 182 | WSACleanup(); | 
|---|
| 183 | perror( "Winsock init failed"); | 
|---|
| 184 | logmsg( "No suitable winsock.dll found -- aborting"); | 
|---|
| 185 | exit(1); | 
|---|
| 186 | } | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 | void win32_cleanup(void) | 
|---|
| 190 | { | 
|---|
| 191 | WSACleanup(); | 
|---|
| 192 | } | 
|---|
| 193 | #endif  /* USE_WINSOCK */ | 
|---|
| 194 |  | 
|---|
| 195 | /* set by the main code to point to where the test dir is */ | 
|---|
| 196 | const char *path = "."; | 
|---|
| 197 |  | 
|---|
| 198 | FILE *test2fopen(long testno) | 
|---|
| 199 | { | 
|---|
| 200 | FILE *stream; | 
|---|
| 201 | char filename[256]; | 
|---|
| 202 | /* first try the alternative, preprocessed, file */ | 
|---|
| 203 | msnprintf(filename, sizeof(filename), ALTTEST_DATA_PATH, ".", testno); | 
|---|
| 204 | stream = fopen(filename, "rb"); | 
|---|
| 205 | if(stream) | 
|---|
| 206 | return stream; | 
|---|
| 207 |  | 
|---|
| 208 | /* then try the source version */ | 
|---|
| 209 | msnprintf(filename, sizeof(filename), TEST_DATA_PATH, path, testno); | 
|---|
| 210 | stream = fopen(filename, "rb"); | 
|---|
| 211 |  | 
|---|
| 212 | return stream; | 
|---|
| 213 | } | 
|---|
| 214 |  | 
|---|
| 215 | /* | 
|---|
| 216 | * Portable function used for waiting a specific amount of ms. | 
|---|
| 217 | * Waiting indefinitely with this function is not allowed, a | 
|---|
| 218 | * zero or negative timeout value will return immediately. | 
|---|
| 219 | * | 
|---|
| 220 | * Return values: | 
|---|
| 221 | *   -1 = system call error, or invalid timeout value | 
|---|
| 222 | *    0 = specified timeout has elapsed | 
|---|
| 223 | */ | 
|---|
| 224 | int wait_ms(int timeout_ms) | 
|---|
| 225 | { | 
|---|
| 226 | #if !defined(MSDOS) && !defined(USE_WINSOCK) | 
|---|
| 227 | #ifndef HAVE_POLL_FINE | 
|---|
| 228 | struct timeval pending_tv; | 
|---|
| 229 | #endif | 
|---|
| 230 | struct timeval initial_tv; | 
|---|
| 231 | int pending_ms; | 
|---|
| 232 | #endif | 
|---|
| 233 | int r = 0; | 
|---|
| 234 |  | 
|---|
| 235 | if(!timeout_ms) | 
|---|
| 236 | return 0; | 
|---|
| 237 | if(timeout_ms < 0) { | 
|---|
| 238 | errno = EINVAL; | 
|---|
| 239 | return -1; | 
|---|
| 240 | } | 
|---|
| 241 | #if defined(MSDOS) | 
|---|
| 242 | delay(timeout_ms); | 
|---|
| 243 | #elif defined(USE_WINSOCK) | 
|---|
| 244 | Sleep(timeout_ms); | 
|---|
| 245 | #else | 
|---|
| 246 | pending_ms = timeout_ms; | 
|---|
| 247 | initial_tv = tvnow(); | 
|---|
| 248 | do { | 
|---|
| 249 | int error; | 
|---|
| 250 | #if defined(HAVE_POLL_FINE) | 
|---|
| 251 | r = poll(NULL, 0, pending_ms); | 
|---|
| 252 | #else | 
|---|
| 253 | pending_tv.tv_sec = pending_ms / 1000; | 
|---|
| 254 | pending_tv.tv_usec = (pending_ms % 1000) * 1000; | 
|---|
| 255 | r = select(0, NULL, NULL, NULL, &pending_tv); | 
|---|
| 256 | #endif /* HAVE_POLL_FINE */ | 
|---|
| 257 | if(r != -1) | 
|---|
| 258 | break; | 
|---|
| 259 | error = errno; | 
|---|
| 260 | if(error && (error != EINTR)) | 
|---|
| 261 | break; | 
|---|
| 262 | pending_ms = timeout_ms - (int)timediff(tvnow(), initial_tv); | 
|---|
| 263 | if(pending_ms <= 0) | 
|---|
| 264 | break; | 
|---|
| 265 | } while(r == -1); | 
|---|
| 266 | #endif /* USE_WINSOCK */ | 
|---|
| 267 | if(r) | 
|---|
| 268 | r = -1; | 
|---|
| 269 | return r; | 
|---|
| 270 | } | 
|---|
| 271 |  | 
|---|
| 272 | curl_off_t our_getpid(void) | 
|---|
| 273 | { | 
|---|
| 274 | curl_off_t pid; | 
|---|
| 275 |  | 
|---|
| 276 | pid = (curl_off_t)getpid(); | 
|---|
| 277 | #if defined(WIN32) || defined(_WIN32) | 
|---|
| 278 | /* store pid + 65536 to avoid conflict with Cygwin/msys PIDs, see also: | 
|---|
| 279 | * - https://cygwin.com/git/?p=newlib-cygwin.git;a=commit; ↵ | 
|---|
| 280 | *   h=b5e1003722cb14235c4f166be72c09acdffc62ea | 
|---|
| 281 | * - https://cygwin.com/git/?p=newlib-cygwin.git;a=commit; ↵ | 
|---|
| 282 | *   h=448cf5aa4b429d5a9cebf92a0da4ab4b5b6d23fe | 
|---|
| 283 | */ | 
|---|
| 284 | pid += 65536; | 
|---|
| 285 | #endif | 
|---|
| 286 | return pid; | 
|---|
| 287 | } | 
|---|
| 288 |  | 
|---|
| 289 | int write_pidfile(const char *filename) | 
|---|
| 290 | { | 
|---|
| 291 | FILE *pidfile; | 
|---|
| 292 | curl_off_t pid; | 
|---|
| 293 |  | 
|---|
| 294 | pid = our_getpid(); | 
|---|
| 295 | pidfile = fopen(filename, "wb"); | 
|---|
| 296 | if(!pidfile) { | 
|---|
| 297 | logmsg( "Couldn't write pid file: %s %s", filename, strerror(errno)); | 
|---|
| 298 | return 0; /* fail */ | 
|---|
| 299 | } | 
|---|
| 300 | fprintf(pidfile, "%"CURL_FORMAT_CURL_OFF_T "\n", pid); | 
|---|
| 301 | fclose(pidfile); | 
|---|
| 302 | logmsg( "Wrote pid %"CURL_FORMAT_CURL_OFF_T " to %s", pid, filename); | 
|---|
| 303 | return 1; /* success */ | 
|---|
| 304 | } | 
|---|
| 305 |  | 
|---|
| 306 | /* store the used port number in a file */ | 
|---|
| 307 | int write_portfile(const char *filename, int port) | 
|---|
| 308 | { | 
|---|
| 309 | FILE *portfile = fopen(filename, "wb"); | 
|---|
| 310 | if(!portfile) { | 
|---|
| 311 | logmsg( "Couldn't write port file: %s %s", filename, strerror(errno)); | 
|---|
| 312 | return 0; /* fail */ | 
|---|
| 313 | } | 
|---|
| 314 | fprintf(portfile, "%d\n", port); | 
|---|
| 315 | fclose(portfile); | 
|---|
| 316 | logmsg( "Wrote port %d to %s", port, filename); | 
|---|
| 317 | return 1; /* success */ | 
|---|
| 318 | } | 
|---|
| 319 |  | 
|---|
| 320 | void set_advisor_read_lock(const char *filename) | 
|---|
| 321 | { | 
|---|
| 322 | FILE *lockfile; | 
|---|
| 323 | int error = 0; | 
|---|
| 324 | int res; | 
|---|
| 325 |  | 
|---|
| 326 | do { | 
|---|
| 327 | lockfile = fopen(filename, "wb"); | 
|---|
| 328 | } while(!lockfile && ((error = errno) == EINTR)); | 
|---|
| 329 | if(!lockfile) { | 
|---|
| 330 | logmsg( "Error creating lock file %s error: %d %s", | 
|---|
| 331 | filename, error, strerror(error)); | 
|---|
| 332 | return; | 
|---|
| 333 | } | 
|---|
| 334 |  | 
|---|
| 335 | do { | 
|---|
| 336 | res = fclose(lockfile); | 
|---|
| 337 | } while(res && ((error = errno) == EINTR)); | 
|---|
| 338 | if(res) | 
|---|
| 339 | logmsg( "Error closing lock file %s error: %d %s", | 
|---|
| 340 | filename, error, strerror(error)); | 
|---|
| 341 | } | 
|---|
| 342 |  | 
|---|
| 343 | void clear_advisor_read_lock(const char *filename) | 
|---|
| 344 | { | 
|---|
| 345 | int error = 0; | 
|---|
| 346 | int res; | 
|---|
| 347 |  | 
|---|
| 348 | /* | 
|---|
| 349 | ** Log all removal failures. Even those due to file not existing. | 
|---|
| 350 | ** This allows to detect if unexpectedly the file has already been | 
|---|
| 351 | ** removed by a process different than the one that should do this. | 
|---|
| 352 | */ | 
|---|
| 353 |  | 
|---|
| 354 | do { | 
|---|
| 355 | res = unlink(filename); | 
|---|
| 356 | } while(res && ((error = errno) == EINTR)); | 
|---|
| 357 | if(res) | 
|---|
| 358 | logmsg( "Error removing lock file %s error: %d %s", | 
|---|
| 359 | filename, error, strerror(error)); | 
|---|
| 360 | } | 
|---|
| 361 |  | 
|---|
| 362 |  | 
|---|
| 363 | /* Portable, consistent toupper (remember EBCDIC). Do not use toupper() because | 
|---|
| 364 | its behavior is altered by the current locale. */ | 
|---|
| 365 | static char raw_toupper(char in) | 
|---|
| 366 | { | 
|---|
| 367 | #if !defined(CURL_DOES_CONVERSIONS) | 
|---|
| 368 | if(in >= 'a' && in <= 'z') | 
|---|
| 369 | return (char)('A' + in - 'a'); | 
|---|
| 370 | #else | 
|---|
| 371 | switch(in) { | 
|---|
| 372 | case 'a': | 
|---|
| 373 | return 'A'; | 
|---|
| 374 | case 'b': | 
|---|
| 375 | return 'B'; | 
|---|
| 376 | case 'c': | 
|---|
| 377 | return 'C'; | 
|---|
| 378 | case 'd': | 
|---|
| 379 | return 'D'; | 
|---|
| 380 | case 'e': | 
|---|
| 381 | return 'E'; | 
|---|
| 382 | case 'f': | 
|---|
| 383 | return 'F'; | 
|---|
| 384 | case 'g': | 
|---|
| 385 | return 'G'; | 
|---|
| 386 | case 'h': | 
|---|
| 387 | return 'H'; | 
|---|
| 388 | case 'i': | 
|---|
| 389 | return 'I'; | 
|---|
| 390 | case 'j': | 
|---|
| 391 | return 'J'; | 
|---|
| 392 | case 'k': | 
|---|
| 393 | return 'K'; | 
|---|
| 394 | case 'l': | 
|---|
| 395 | return 'L'; | 
|---|
| 396 | case 'm': | 
|---|
| 397 | return 'M'; | 
|---|
| 398 | case 'n': | 
|---|
| 399 | return 'N'; | 
|---|
| 400 | case 'o': | 
|---|
| 401 | return 'O'; | 
|---|
| 402 | case 'p': | 
|---|
| 403 | return 'P'; | 
|---|
| 404 | case 'q': | 
|---|
| 405 | return 'Q'; | 
|---|
| 406 | case 'r': | 
|---|
| 407 | return 'R'; | 
|---|
| 408 | case 's': | 
|---|
| 409 | return 'S'; | 
|---|
| 410 | case 't': | 
|---|
| 411 | return 'T'; | 
|---|
| 412 | case 'u': | 
|---|
| 413 | return 'U'; | 
|---|
| 414 | case 'v': | 
|---|
| 415 | return 'V'; | 
|---|
| 416 | case 'w': | 
|---|
| 417 | return 'W'; | 
|---|
| 418 | case 'x': | 
|---|
| 419 | return 'X'; | 
|---|
| 420 | case 'y': | 
|---|
| 421 | return 'Y'; | 
|---|
| 422 | case 'z': | 
|---|
| 423 | return 'Z'; | 
|---|
| 424 | } | 
|---|
| 425 | #endif | 
|---|
| 426 |  | 
|---|
| 427 | return in; | 
|---|
| 428 | } | 
|---|
| 429 |  | 
|---|
| 430 | int strncasecompare(const char *first, const char *second, size_t max) | 
|---|
| 431 | { | 
|---|
| 432 | while(*first && *second && max) { | 
|---|
| 433 | if(raw_toupper(*first) != raw_toupper(*second)) { | 
|---|
| 434 | break; | 
|---|
| 435 | } | 
|---|
| 436 | max--; | 
|---|
| 437 | first++; | 
|---|
| 438 | second++; | 
|---|
| 439 | } | 
|---|
| 440 | if(0 == max) | 
|---|
| 441 | return 1; /* they are equal this far */ | 
|---|
| 442 |  | 
|---|
| 443 | return raw_toupper(*first) == raw_toupper(*second); | 
|---|
| 444 | } | 
|---|
| 445 |  | 
|---|
| 446 | #if defined(WIN32) && !defined(MSDOS) | 
|---|
| 447 |  | 
|---|
| 448 | static struct timeval tvnow(void) | 
|---|
| 449 | { | 
|---|
| 450 | /* | 
|---|
| 451 | ** GetTickCount() is available on _all_ Windows versions from W95 up | 
|---|
| 452 | ** to nowadays. Returns milliseconds elapsed since last system boot, | 
|---|
| 453 | ** increases monotonically and wraps once 49.7 days have elapsed. | 
|---|
| 454 | ** | 
|---|
| 455 | ** GetTickCount64() is available on Windows version from Windows Vista | 
|---|
| 456 | ** and Windows Server 2008 up to nowadays. The resolution of the | 
|---|
| 457 | ** function is limited to the resolution of the system timer, which | 
|---|
| 458 | ** is typically in the range of 10 milliseconds to 16 milliseconds. | 
|---|
| 459 | */ | 
|---|
| 460 | struct timeval now; | 
|---|
| 461 | #if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600) && \ | 
|---|
| 462 | (!defined(__MINGW32__) || defined(__MINGW64_VERSION_MAJOR)) | 
|---|
| 463 | ULONGLONG milliseconds = GetTickCount64(); | 
|---|
| 464 | #else | 
|---|
| 465 | DWORD milliseconds = GetTickCount(); | 
|---|
| 466 | #endif | 
|---|
| 467 | now.tv_sec = (long)(milliseconds / 1000); | 
|---|
| 468 | now.tv_usec = (long)((milliseconds % 1000) * 1000); | 
|---|
| 469 | return now; | 
|---|
| 470 | } | 
|---|
| 471 |  | 
|---|
| 472 | #elif defined(HAVE_CLOCK_GETTIME_MONOTONIC) | 
|---|
| 473 |  | 
|---|
| 474 | static struct timeval tvnow(void) | 
|---|
| 475 | { | 
|---|
| 476 | /* | 
|---|
| 477 | ** clock_gettime() is granted to be increased monotonically when the | 
|---|
| 478 | ** monotonic clock is queried. Time starting point is unspecified, it | 
|---|
| 479 | ** could be the system start-up time, the Epoch, or something else, | 
|---|
| 480 | ** in any case the time starting point does not change once that the | 
|---|
| 481 | ** system has started up. | 
|---|
| 482 | */ | 
|---|
| 483 | struct timeval now; | 
|---|
| 484 | struct timespec tsnow; | 
|---|
| 485 | if(0 == clock_gettime(CLOCK_MONOTONIC, &tsnow)) { | 
|---|
| 486 | now.tv_sec = tsnow.tv_sec; | 
|---|
| 487 | now.tv_usec = (int)(tsnow.tv_nsec / 1000); | 
|---|
| 488 | } | 
|---|
| 489 | /* | 
|---|
| 490 | ** Even when the configure process has truly detected monotonic clock | 
|---|
| 491 | ** availability, it might happen that it is not actually available at | 
|---|
| 492 | ** run-time. When this occurs simply fallback to other time source. | 
|---|
| 493 | */ | 
|---|
| 494 | #ifdef HAVE_GETTIMEOFDAY | 
|---|
| 495 | else | 
|---|
| 496 | (void)gettimeofday(&now, NULL); | 
|---|
| 497 | #else | 
|---|
| 498 | else { | 
|---|
| 499 | now.tv_sec = time(NULL); | 
|---|
| 500 | now.tv_usec = 0; | 
|---|
| 501 | } | 
|---|
| 502 | #endif | 
|---|
| 503 | return now; | 
|---|
| 504 | } | 
|---|
| 505 |  | 
|---|
| 506 | #elif defined(HAVE_GETTIMEOFDAY) | 
|---|
| 507 |  | 
|---|
| 508 | static struct timeval tvnow(void) | 
|---|
| 509 | { | 
|---|
| 510 | /* | 
|---|
| 511 | ** gettimeofday() is not granted to be increased monotonically, due to | 
|---|
| 512 | ** clock drifting and external source time synchronization it can jump | 
|---|
| 513 | ** forward or backward in time. | 
|---|
| 514 | */ | 
|---|
| 515 | struct timeval now; | 
|---|
| 516 | (void)gettimeofday(&now, NULL); | 
|---|
| 517 | return now; | 
|---|
| 518 | } | 
|---|
| 519 |  | 
|---|
| 520 | #else | 
|---|
| 521 |  | 
|---|
| 522 | static struct timeval tvnow(void) | 
|---|
| 523 | { | 
|---|
| 524 | /* | 
|---|
| 525 | ** time() returns the value of time in seconds since the Epoch. | 
|---|
| 526 | */ | 
|---|
| 527 | struct timeval now; | 
|---|
| 528 | now.tv_sec = time(NULL); | 
|---|
| 529 | now.tv_usec = 0; | 
|---|
| 530 | return now; | 
|---|
| 531 | } | 
|---|
| 532 |  | 
|---|
| 533 | #endif | 
|---|
| 534 |  | 
|---|
| 535 | long timediff(struct timeval newer, struct timeval older) | 
|---|
| 536 | { | 
|---|
| 537 | timediff_t diff = newer.tv_sec-older.tv_sec; | 
|---|
| 538 | if(diff >= (LONG_MAX/1000)) | 
|---|
| 539 | return LONG_MAX; | 
|---|
| 540 | else if(diff <= (LONG_MIN/1000)) | 
|---|
| 541 | return LONG_MIN; | 
|---|
| 542 | return (long)(newer.tv_sec-older.tv_sec)*1000+ | 
|---|
| 543 | (long)(newer.tv_usec-older.tv_usec)/1000; | 
|---|
| 544 | } | 
|---|
| 545 |  | 
|---|
| 546 | /* vars used to keep around previous signal handlers */ | 
|---|
| 547 |  | 
|---|
| 548 | typedef void (*SIGHANDLER_T)(int); | 
|---|
| 549 |  | 
|---|
| 550 | #ifdef SIGHUP | 
|---|
| 551 | static SIGHANDLER_T old_sighup_handler  = SIG_ERR; | 
|---|
| 552 | #endif | 
|---|
| 553 |  | 
|---|
| 554 | #ifdef SIGPIPE | 
|---|
| 555 | static SIGHANDLER_T old_sigpipe_handler = SIG_ERR; | 
|---|
| 556 | #endif | 
|---|
| 557 |  | 
|---|
| 558 | #ifdef SIGALRM | 
|---|
| 559 | static SIGHANDLER_T old_sigalrm_handler = SIG_ERR; | 
|---|
| 560 | #endif | 
|---|
| 561 |  | 
|---|
| 562 | #ifdef SIGINT | 
|---|
| 563 | static SIGHANDLER_T old_sigint_handler  = SIG_ERR; | 
|---|
| 564 | #endif | 
|---|
| 565 |  | 
|---|
| 566 | #ifdef SIGTERM | 
|---|
| 567 | static SIGHANDLER_T old_sigterm_handler = SIG_ERR; | 
|---|
| 568 | #endif | 
|---|
| 569 |  | 
|---|
| 570 | #if defined(SIGBREAK) && defined(WIN32) | 
|---|
| 571 | static SIGHANDLER_T old_sigbreak_handler = SIG_ERR; | 
|---|
| 572 | #endif | 
|---|
| 573 |  | 
|---|
| 574 | #ifdef WIN32 | 
|---|
| 575 | static DWORD thread_main_id = 0; | 
|---|
| 576 | static HANDLE thread_main_window = NULL; | 
|---|
| 577 | static HWND hidden_main_window = NULL; | 
|---|
| 578 | #endif | 
|---|
| 579 |  | 
|---|
| 580 | /* var which if set indicates that the program should finish execution */ | 
|---|
| 581 | volatile int got_exit_signal = 0; | 
|---|
| 582 |  | 
|---|
| 583 | /* if next is set indicates the first signal handled in exit_signal_handler */ | 
|---|
| 584 | volatile int exit_signal = 0; | 
|---|
| 585 |  | 
|---|
| 586 | #ifdef WIN32 | 
|---|
| 587 | /* event which if set indicates that the program should finish */ | 
|---|
| 588 | HANDLE exit_event = NULL; | 
|---|
| 589 | #endif | 
|---|
| 590 |  | 
|---|
| 591 | /* signal handler that will be triggered to indicate that the program | 
|---|
| 592 | * should finish its execution in a controlled manner as soon as possible. | 
|---|
| 593 | * The first time this is called it will set got_exit_signal to one and | 
|---|
| 594 | * store in exit_signal the signal that triggered its execution. | 
|---|
| 595 | */ | 
|---|
| 596 | static void exit_signal_handler(int signum) | 
|---|
| 597 | { | 
|---|
| 598 | int old_errno = errno; | 
|---|
| 599 | logmsg( "exit_signal_handler: %d", signum); | 
|---|
| 600 | if(got_exit_signal == 0) { | 
|---|
| 601 | got_exit_signal = 1; | 
|---|
| 602 | exit_signal = signum; | 
|---|
| 603 | #ifdef WIN32 | 
|---|
| 604 | if(exit_event) | 
|---|
| 605 | (void)SetEvent(exit_event); | 
|---|
| 606 | #endif | 
|---|
| 607 | } | 
|---|
| 608 | (void)signal(signum, exit_signal_handler); | 
|---|
| 609 | errno = old_errno; | 
|---|
| 610 | } | 
|---|
| 611 |  | 
|---|
| 612 | #ifdef WIN32 | 
|---|
| 613 | /* CTRL event handler for Windows Console applications to simulate | 
|---|
| 614 | * SIGINT, SIGTERM and SIGBREAK on CTRL events and trigger signal handler. | 
|---|
| 615 | * | 
|---|
| 616 | * Background information from MSDN: | 
|---|
| 617 | * SIGINT is not supported for any Win32 application. When a CTRL+C | 
|---|
| 618 | * interrupt occurs, Win32 operating systems generate a new thread | 
|---|
| 619 | * to specifically handle that interrupt. This can cause a single-thread | 
|---|
| 620 | * application, such as one in UNIX, to become multithreaded and cause | 
|---|
| 621 | * unexpected behavior. | 
|---|
| 622 | * [...] | 
|---|
| 623 | * The SIGILL and SIGTERM signals are not generated under Windows. | 
|---|
| 624 | * They are included for ANSI compatibility. Therefore, you can set | 
|---|
| 625 | * signal handlers for these signals by using signal, and you can also | 
|---|
| 626 | * explicitly generate these signals by calling raise. Source: | 
|---|
| 627 | * https://docs.microsoft.com/de-de/cpp/c-runtime-library/reference/signal | 
|---|
| 628 | */ | 
|---|
| 629 | static BOOL WINAPI ctrl_event_handler(DWORD dwCtrlType) | 
|---|
| 630 | { | 
|---|
| 631 | int signum = 0; | 
|---|
| 632 | logmsg( "ctrl_event_handler: %d", dwCtrlType); | 
|---|
| 633 | switch(dwCtrlType) { | 
|---|
| 634 | #ifdef SIGINT | 
|---|
| 635 | case CTRL_C_EVENT: signum = SIGINT; break; | 
|---|
| 636 | #endif | 
|---|
| 637 | #ifdef SIGTERM | 
|---|
| 638 | case CTRL_CLOSE_EVENT: signum = SIGTERM; break; | 
|---|
| 639 | #endif | 
|---|
| 640 | #ifdef SIGBREAK | 
|---|
| 641 | case CTRL_BREAK_EVENT: signum = SIGBREAK; break; | 
|---|
| 642 | #endif | 
|---|
| 643 | default: return FALSE; | 
|---|
| 644 | } | 
|---|
| 645 | if(signum) { | 
|---|
| 646 | logmsg( "ctrl_event_handler: %d -> %d", dwCtrlType, signum); | 
|---|
| 647 | raise(signum); | 
|---|
| 648 | } | 
|---|
| 649 | return TRUE; | 
|---|
| 650 | } | 
|---|
| 651 | /* Window message handler for Windows applications to add support | 
|---|
| 652 | * for graceful process termination via taskkill (without /f) which | 
|---|
| 653 | * sends WM_CLOSE to all Windows of a process (even hidden ones). | 
|---|
| 654 | * | 
|---|
| 655 | * Therefore we create and run a hidden Window in a separate thread | 
|---|
| 656 | * to receive and handle the WM_CLOSE message as SIGTERM signal. | 
|---|
| 657 | */ | 
|---|
| 658 | static LRESULT CALLBACK main_window_proc(HWND hwnd, UINT uMsg, | 
|---|
| 659 | WPARAM wParam, LPARAM lParam) | 
|---|
| 660 | { | 
|---|
| 661 | int signum = 0; | 
|---|
| 662 | if(hwnd == hidden_main_window) { | 
|---|
| 663 | switch(uMsg) { | 
|---|
| 664 | #ifdef SIGTERM | 
|---|
| 665 | case WM_CLOSE: signum = SIGTERM; break; | 
|---|
| 666 | #endif | 
|---|
| 667 | case WM_DESTROY: PostQuitMessage(0); break; | 
|---|
| 668 | } | 
|---|
| 669 | if(signum) { | 
|---|
| 670 | logmsg( "main_window_proc: %d -> %d", uMsg, signum); | 
|---|
| 671 | raise(signum); | 
|---|
| 672 | } | 
|---|
| 673 | } | 
|---|
| 674 | return DefWindowProc(hwnd, uMsg, wParam, lParam); | 
|---|
| 675 | } | 
|---|
| 676 | /* Window message queue loop for hidden main window, details see above. | 
|---|
| 677 | */ | 
|---|
| 678 | static DWORD WINAPI main_window_loop(LPVOID lpParameter) | 
|---|
| 679 | { | 
|---|
| 680 | WNDCLASS wc; | 
|---|
| 681 | BOOL ret; | 
|---|
| 682 | MSG msg; | 
|---|
| 683 |  | 
|---|
| 684 | ZeroMemory(&wc, sizeof(wc)); | 
|---|
| 685 | wc.lpfnWndProc = (WNDPROC)main_window_proc; | 
|---|
| 686 | wc.hInstance = (HINSTANCE)lpParameter; | 
|---|
| 687 | wc.lpszClassName = TEXT( "MainWClass"); | 
|---|
| 688 | if(!RegisterClass(&wc)) { | 
|---|
| 689 | perror( "RegisterClass failed"); | 
|---|
| 690 | return (DWORD)-1; | 
|---|
| 691 | } | 
|---|
| 692 |  | 
|---|
| 693 | hidden_main_window = CreateWindowEx(0, TEXT( "MainWClass"), | 
|---|
| 694 | TEXT( "Recv WM_CLOSE msg"), | 
|---|
| 695 | WS_OVERLAPPEDWINDOW, | 
|---|
| 696 | CW_USEDEFAULT, CW_USEDEFAULT, | 
|---|
| 697 | CW_USEDEFAULT, CW_USEDEFAULT, | 
|---|
| 698 | (HWND)NULL, (HMENU)NULL, | 
|---|
| 699 | wc.hInstance, (LPVOID)NULL); | 
|---|
| 700 | if(!hidden_main_window) { | 
|---|
| 701 | perror( "CreateWindowEx failed"); | 
|---|
| 702 | return (DWORD)-1; | 
|---|
| 703 | } | 
|---|
| 704 |  | 
|---|
| 705 | do { | 
|---|
| 706 | ret = GetMessage(&msg, NULL, 0, 0); | 
|---|
| 707 | if(ret == -1) { | 
|---|
| 708 | perror( "GetMessage failed"); | 
|---|
| 709 | return (DWORD)-1; | 
|---|
| 710 | } | 
|---|
| 711 | else if(ret) { | 
|---|
| 712 | if(msg.message == WM_APP) { | 
|---|
| 713 | DestroyWindow(hidden_main_window); | 
|---|
| 714 | } | 
|---|
| 715 | else if(msg.hwnd && !TranslateMessage(&msg)) { | 
|---|
| 716 | DispatchMessage(&msg); | 
|---|
| 717 | } | 
|---|
| 718 | } | 
|---|
| 719 | } while(ret); | 
|---|
| 720 |  | 
|---|
| 721 | hidden_main_window = NULL; | 
|---|
| 722 | return (DWORD)msg.wParam; | 
|---|
| 723 | } | 
|---|
| 724 | #endif | 
|---|
| 725 |  | 
|---|
| 726 | static SIGHANDLER_T set_signal(int signum, SIGHANDLER_T handler, | 
|---|
| 727 | bool restartable) | 
|---|
| 728 | { | 
|---|
| 729 | #if defined(HAVE_SIGACTION) && defined(SA_RESTART) | 
|---|
| 730 | struct sigaction sa, oldsa; | 
|---|
| 731 |  | 
|---|
| 732 | memset(&sa, 0, sizeof(sa)); | 
|---|
| 733 | sa.sa_handler = handler; | 
|---|
| 734 | sigemptyset(&sa.sa_mask); | 
|---|
| 735 | sigaddset(&sa.sa_mask, signum); | 
|---|
| 736 | sa.sa_flags = restartable? SA_RESTART: 0; | 
|---|
| 737 |  | 
|---|
| 738 | if(sigaction(signum, &sa, &oldsa)) | 
|---|
| 739 | return SIG_ERR; | 
|---|
| 740 |  | 
|---|
| 741 | return oldsa.sa_handler; | 
|---|
| 742 | #else | 
|---|
| 743 | SIGHANDLER_T oldhdlr = signal(signum, handler); | 
|---|
| 744 |  | 
|---|
| 745 | #ifdef HAVE_SIGINTERRUPT | 
|---|
| 746 | if(oldhdlr != SIG_ERR) | 
|---|
| 747 | siginterrupt(signum, (int) restartable); | 
|---|
| 748 | #else | 
|---|
| 749 | (void) restartable; | 
|---|
| 750 | #endif | 
|---|
| 751 |  | 
|---|
| 752 | return oldhdlr; | 
|---|
| 753 | #endif | 
|---|
| 754 | } | 
|---|
| 755 |  | 
|---|
| 756 | void install_signal_handlers(bool keep_sigalrm) | 
|---|
| 757 | { | 
|---|
| 758 | #ifdef WIN32 | 
|---|
| 759 | /* setup windows exit event before any signal can trigger */ | 
|---|
| 760 | exit_event = CreateEvent(NULL, TRUE, FALSE, NULL); | 
|---|
| 761 | if(!exit_event) | 
|---|
| 762 | logmsg( "cannot create exit event"); | 
|---|
| 763 | #endif | 
|---|
| 764 | #ifdef SIGHUP | 
|---|
| 765 | /* ignore SIGHUP signal */ | 
|---|
| 766 | old_sighup_handler = set_signal(SIGHUP, SIG_IGN, FALSE); | 
|---|
| 767 | if(old_sighup_handler == SIG_ERR) | 
|---|
| 768 | logmsg( "cannot install SIGHUP handler: %s", strerror(errno)); | 
|---|
| 769 | #endif | 
|---|
| 770 | #ifdef SIGPIPE | 
|---|
| 771 | /* ignore SIGPIPE signal */ | 
|---|
| 772 | old_sigpipe_handler = set_signal(SIGPIPE, SIG_IGN, FALSE); | 
|---|
| 773 | if(old_sigpipe_handler == SIG_ERR) | 
|---|
| 774 | logmsg( "cannot install SIGPIPE handler: %s", strerror(errno)); | 
|---|
| 775 | #endif | 
|---|
| 776 | #ifdef SIGALRM | 
|---|
| 777 | if(!keep_sigalrm) { | 
|---|
| 778 | /* ignore SIGALRM signal */ | 
|---|
| 779 | old_sigalrm_handler = set_signal(SIGALRM, SIG_IGN, FALSE); | 
|---|
| 780 | if(old_sigalrm_handler == SIG_ERR) | 
|---|
| 781 | logmsg( "cannot install SIGALRM handler: %s", strerror(errno)); | 
|---|
| 782 | } | 
|---|
| 783 | #else | 
|---|
| 784 | (void)keep_sigalrm; | 
|---|
| 785 | #endif | 
|---|
| 786 | #ifdef SIGINT | 
|---|
| 787 | /* handle SIGINT signal with our exit_signal_handler */ | 
|---|
| 788 | old_sigint_handler = set_signal(SIGINT, exit_signal_handler, TRUE); | 
|---|
| 789 | if(old_sigint_handler == SIG_ERR) | 
|---|
| 790 | logmsg( "cannot install SIGINT handler: %s", strerror(errno)); | 
|---|
| 791 | #endif | 
|---|
| 792 | #ifdef SIGTERM | 
|---|
| 793 | /* handle SIGTERM signal with our exit_signal_handler */ | 
|---|
| 794 | old_sigterm_handler = set_signal(SIGTERM, exit_signal_handler, TRUE); | 
|---|
| 795 | if(old_sigterm_handler == SIG_ERR) | 
|---|
| 796 | logmsg( "cannot install SIGTERM handler: %s", strerror(errno)); | 
|---|
| 797 | #endif | 
|---|
| 798 | #if defined(SIGBREAK) && defined(WIN32) | 
|---|
| 799 | /* handle SIGBREAK signal with our exit_signal_handler */ | 
|---|
| 800 | old_sigbreak_handler = set_signal(SIGBREAK, exit_signal_handler, TRUE); | 
|---|
| 801 | if(old_sigbreak_handler == SIG_ERR) | 
|---|
| 802 | logmsg( "cannot install SIGBREAK handler: %s", strerror(errno)); | 
|---|
| 803 | #endif | 
|---|
| 804 | #ifdef WIN32 | 
|---|
| 805 | if(!SetConsoleCtrlHandler(ctrl_event_handler, TRUE)) | 
|---|
| 806 | logmsg( "cannot install CTRL event handler"); | 
|---|
| 807 | thread_main_window = CreateThread(NULL, 0, | 
|---|
| 808 | &main_window_loop, | 
|---|
| 809 | (LPVOID)GetModuleHandle(NULL), | 
|---|
| 810 | 0, &thread_main_id); | 
|---|
| 811 | if(!thread_main_window || !thread_main_id) | 
|---|
| 812 | logmsg( "cannot start main window loop"); | 
|---|
| 813 | #endif | 
|---|
| 814 | } | 
|---|
| 815 |  | 
|---|
| 816 | void restore_signal_handlers(bool keep_sigalrm) | 
|---|
| 817 | { | 
|---|
| 818 | #ifdef SIGHUP | 
|---|
| 819 | if(SIG_ERR != old_sighup_handler) | 
|---|
| 820 | (void) set_signal(SIGHUP, old_sighup_handler, FALSE); | 
|---|
| 821 | #endif | 
|---|
| 822 | #ifdef SIGPIPE | 
|---|
| 823 | if(SIG_ERR != old_sigpipe_handler) | 
|---|
| 824 | (void) set_signal(SIGPIPE, old_sigpipe_handler, FALSE); | 
|---|
| 825 | #endif | 
|---|
| 826 | #ifdef SIGALRM | 
|---|
| 827 | if(!keep_sigalrm) { | 
|---|
| 828 | if(SIG_ERR != old_sigalrm_handler) | 
|---|
| 829 | (void) set_signal(SIGALRM, old_sigalrm_handler, FALSE); | 
|---|
| 830 | } | 
|---|
| 831 | #else | 
|---|
| 832 | (void)keep_sigalrm; | 
|---|
| 833 | #endif | 
|---|
| 834 | #ifdef SIGINT | 
|---|
| 835 | if(SIG_ERR != old_sigint_handler) | 
|---|
| 836 | (void) set_signal(SIGINT, old_sigint_handler, FALSE); | 
|---|
| 837 | #endif | 
|---|
| 838 | #ifdef SIGTERM | 
|---|
| 839 | if(SIG_ERR != old_sigterm_handler) | 
|---|
| 840 | (void) set_signal(SIGTERM, old_sigterm_handler, FALSE); | 
|---|
| 841 | #endif | 
|---|
| 842 | #if defined(SIGBREAK) && defined(WIN32) | 
|---|
| 843 | if(SIG_ERR != old_sigbreak_handler) | 
|---|
| 844 | (void) set_signal(SIGBREAK, old_sigbreak_handler, FALSE); | 
|---|
| 845 | #endif | 
|---|
| 846 | #ifdef WIN32 | 
|---|
| 847 | (void)SetConsoleCtrlHandler(ctrl_event_handler, FALSE); | 
|---|
| 848 | if(thread_main_window && thread_main_id) { | 
|---|
| 849 | if(PostThreadMessage(thread_main_id, WM_APP, 0, 0)) { | 
|---|
| 850 | if(WaitForSingleObjectEx(thread_main_window, INFINITE, TRUE)) { | 
|---|
| 851 | if(CloseHandle(thread_main_window)) { | 
|---|
| 852 | thread_main_window = NULL; | 
|---|
| 853 | thread_main_id = 0; | 
|---|
| 854 | } | 
|---|
| 855 | } | 
|---|
| 856 | } | 
|---|
| 857 | } | 
|---|
| 858 | if(exit_event) { | 
|---|
| 859 | if(CloseHandle(exit_event)) { | 
|---|
| 860 | exit_event = NULL; | 
|---|
| 861 | } | 
|---|
| 862 | } | 
|---|
| 863 | #endif | 
|---|
| 864 | } | 
|---|
| 865 |  | 
|---|