1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
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.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
22 #include "server_setup.h"
27 #ifdef HAVE_NETINET_IN_H
28 #include <netinet/in.h>
30 #ifdef _XOPEN_SOURCE_EXTENDED
31 /* This define is "almost" required to build on HPUX 11 */
32 #include <arpa/inet.h>
37 #ifdef HAVE_SYS_POLL_H
39 #elif defined(HAVE_POLL_H)
43 #define ENABLE_CURLX_PRINTF
44 /* make the curlx header define all printf() functions to use the curlx_*
46 #include "curlx.h" /* from the private lib dir */
53 #define EINTR 4 /* errno.h value */
55 #define EINVAL 22 /* errno.h value */
58 #if defined(ENABLE_IPV6) && defined(__MINGW32__)
59 const struct in6_addr in6addr_any = {{ IN6ADDR_ANY_INIT }};
62 /* This function returns a pointer to STATIC memory. It converts the given
63 * binary lump to a hex formatted string usable for output in logs or
66 char *data_to_hex(char *data, size_t len)
68 static char buf[256*3];
76 for(i=0; i < len; i++) {
77 if((data[i] >= 0x20) && (data[i] < 0x7f))
80 snprintf(optr, 4, "%%%02x", *iptr++);
84 *optr=0; /* in case no sprintf was used */
89 void logmsg(const char *msg, ...)
92 char buffer[2048 + 1];
99 static time_t epoch_offset;
100 static int known_offset;
103 fprintf(stderr, "Error: serverlogfile not set\n");
109 epoch_offset = time(NULL) - tv.tv_sec;
112 sec = epoch_offset + tv.tv_sec;
113 now = localtime(&sec); /* not thread safe but we don't care */
115 snprintf(timebuf, sizeof(timebuf), "%02d:%02d:%02d.%06ld",
116 (int)now->tm_hour, (int)now->tm_min, (int)now->tm_sec, (long)tv.tv_usec);
119 vsnprintf(buffer, sizeof(buffer), msg, ap);
122 logfp = fopen(serverlogfile, "ab");
124 fprintf(logfp, "%s %s\n", timebuf, buffer);
129 fprintf(stderr, "fopen() failed with error: %d %s\n",
130 error, strerror(error));
131 fprintf(stderr, "Error opening file: %s\n", serverlogfile);
132 fprintf(stderr, "Msg not logged: %s %s\n", timebuf, buffer);
137 /* use instead of perror() on generic windows */
138 void win32_perror(const char *msg)
141 DWORD err = SOCKERRNO;
143 if(!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
144 LANG_NEUTRAL, buf, sizeof(buf), NULL))
145 snprintf(buf, sizeof(buf), "Unknown error %lu (%#lx)", err, err);
147 fprintf(stderr, "%s: ", msg);
148 fprintf(stderr, "%s\n", buf);
153 void win32_init(void)
155 WORD wVersionRequested;
158 wVersionRequested = MAKEWORD(USE_WINSOCK, USE_WINSOCK);
160 err = WSAStartup(wVersionRequested, &wsaData);
163 perror("Winsock init failed");
164 logmsg("Error initialising winsock -- aborting");
168 if(LOBYTE(wsaData.wVersion) != USE_WINSOCK ||
169 HIBYTE(wsaData.wVersion) != USE_WINSOCK) {
171 perror("Winsock init failed");
172 logmsg("No suitable winsock.dll found -- aborting");
177 void win32_cleanup(void)
181 #endif /* USE_WINSOCK */
183 /* set by the main code to point to where the test dir is */
184 const char *path=".";
186 char *test2file(long testno)
188 static char filename[256];
189 snprintf(filename, sizeof(filename), TEST_DATA_PATH, path, testno);
194 * Portable function used for waiting a specific amount of ms.
195 * Waiting indefinitely with this function is not allowed, a
196 * zero or negative timeout value will return immediately.
199 * -1 = system call error, or invalid timeout value
200 * 0 = specified timeout has elapsed
202 int wait_ms(int timeout_ms)
204 #if !defined(MSDOS) && !defined(USE_WINSOCK)
205 #ifndef HAVE_POLL_FINE
206 struct timeval pending_tv;
208 struct timeval initial_tv;
222 #elif defined(USE_WINSOCK)
225 pending_ms = timeout_ms;
226 initial_tv = curlx_tvnow();
228 #if defined(HAVE_POLL_FINE)
229 r = poll(NULL, 0, pending_ms);
231 pending_tv.tv_sec = pending_ms / 1000;
232 pending_tv.tv_usec = (pending_ms % 1000) * 1000;
233 r = select(0, NULL, NULL, NULL, &pending_tv);
234 #endif /* HAVE_POLL_FINE */
238 if(error && (error != EINTR))
240 pending_ms = timeout_ms - (int)curlx_tvdiff(curlx_tvnow(), initial_tv);
244 #endif /* USE_WINSOCK */
250 int write_pidfile(const char *filename)
255 pid = (long)getpid();
256 pidfile = fopen(filename, "wb");
258 logmsg("Couldn't write pid file: %s %s", filename, strerror(errno));
261 fprintf(pidfile, "%ld\n", pid);
263 logmsg("Wrote pid %ld to %s", pid, filename);
264 return 1; /* success */
267 void set_advisor_read_lock(const char *filename)
274 lockfile = fopen(filename, "wb");
275 } while((lockfile == NULL) && ((error = errno) == EINTR));
276 if(lockfile == NULL) {
277 logmsg("Error creating lock file %s error: %d %s",
278 filename, error, strerror(error));
283 res = fclose(lockfile);
284 } while(res && ((error = errno) == EINTR));
286 logmsg("Error closing lock file %s error: %d %s",
287 filename, error, strerror(error));
290 void clear_advisor_read_lock(const char *filename)
296 ** Log all removal failures. Even those due to file not existing.
297 ** This allows to detect if unexpectedly the file has already been
298 ** removed by a process different than the one that should do this.
302 res = unlink(filename);
303 } while(res && ((error = errno) == EINTR));
305 logmsg("Error removing lock file %s error: %d %s",
306 filename, error, strerror(error));
310 /* Portable, consistent toupper (remember EBCDIC). Do not use toupper() because
311 its behavior is altered by the current locale. */
312 static char raw_toupper(char in)
314 #if !defined(CURL_DOES_CONVERSIONS)
315 if(in >= 'a' && in <= 'z')
316 return (char)('A' + in - 'a');
377 int strncasecompare(const char *first, const char *second, size_t max)
379 while(*first && *second && max) {
380 if(raw_toupper(*first) != raw_toupper(*second)) {
388 return 1; /* they are equal this far */
390 return raw_toupper(*first) == raw_toupper(*second);