1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2013, 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 http://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 sprintf(optr, "%%%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;
102 if (!serverlogfile) {
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 ) {
172 perror("Winsock init failed");
173 logmsg("No suitable winsock.dll found -- aborting");
178 void win32_cleanup(void)
182 #endif /* USE_WINSOCK */
184 /* set by the main code to point to where the test dir is */
185 const char *path=".";
187 char *test2file(long testno)
189 static char filename[256];
190 snprintf(filename, sizeof(filename), TEST_DATA_PATH, path, testno);
195 * Portable function used for waiting a specific amount of ms.
196 * Waiting indefinitely with this function is not allowed, a
197 * zero or negative timeout value will return immediately.
200 * -1 = system call error, or invalid timeout value
201 * 0 = specified timeout has elapsed
203 int wait_ms(int timeout_ms)
205 #if !defined(MSDOS) && !defined(USE_WINSOCK)
206 #ifndef HAVE_POLL_FINE
207 struct timeval pending_tv;
209 struct timeval initial_tv;
223 #elif defined(USE_WINSOCK)
226 pending_ms = timeout_ms;
227 initial_tv = curlx_tvnow();
229 #if defined(HAVE_POLL_FINE)
230 r = poll(NULL, 0, pending_ms);
232 pending_tv.tv_sec = pending_ms / 1000;
233 pending_tv.tv_usec = (pending_ms % 1000) * 1000;
234 r = select(0, NULL, NULL, NULL, &pending_tv);
235 #endif /* HAVE_POLL_FINE */
239 if(error && (error != EINTR))
241 pending_ms = timeout_ms - (int)curlx_tvdiff(curlx_tvnow(), initial_tv);
245 #endif /* USE_WINSOCK */
251 int write_pidfile(const char *filename)
256 pid = (long)getpid();
257 pidfile = fopen(filename, "wb");
259 logmsg("Couldn't write pid file: %s %s", filename, strerror(errno));
262 fprintf(pidfile, "%ld\n", pid);
264 logmsg("Wrote pid %ld to %s", pid, filename);
265 return 1; /* success */
268 void set_advisor_read_lock(const char *filename)
275 lockfile = fopen(filename, "wb");
276 } while((lockfile == NULL) && ((error = errno) == EINTR));
277 if(lockfile == NULL) {
278 logmsg("Error creating lock file %s error: %d %s",
279 filename, error, strerror(error));
284 res = fclose(lockfile);
285 } while(res && ((error = errno) == EINTR));
287 logmsg("Error closing lock file %s error: %d %s",
288 filename, error, strerror(error));
291 void clear_advisor_read_lock(const char *filename)
297 ** Log all removal failures. Even those due to file not existing.
298 ** This allows to detect if unexpectedly the file has already been
299 ** removed by a process different than the one that should do this.
303 res = unlink(filename);
304 } while(res && ((error = errno) == EINTR));
306 logmsg("Error removing lock file %s error: %d %s",
307 filename, error, strerror(error));