1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2010, 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.
22 ***************************************************************************/
24 #define CURL_NO_OLDIES
26 #include "setup.h" /* portability help from the lib directory */
34 #ifdef HAVE_SYS_SOCKET_H
35 #include <sys/socket.h>
37 #ifdef HAVE_NETINET_IN_H
38 #include <netinet/in.h>
40 #ifdef _XOPEN_SOURCE_EXTENDED
41 /* This define is "almost" required to build on HPUX 11 */
42 #include <arpa/inet.h>
47 #ifdef HAVE_SYS_POLL_H
49 #elif defined(HAVE_POLL_H)
53 #define ENABLE_CURLX_PRINTF
54 /* make the curlx header define all printf() functions to use the curlx_*
56 #include "curlx.h" /* from the private lib dir */
61 #if defined(ENABLE_IPV6) && defined(__MINGW32__)
62 const struct in6_addr in6addr_any = {{ IN6ADDR_ANY_INIT }};
65 void logmsg(const char *msg, ...)
68 char buffer[2048 + 1];
75 static time_t epoch_offset;
76 static int known_offset;
79 fprintf(stderr, "Error: serverlogfile not set\n");
85 epoch_offset = time(NULL) - tv.tv_sec;
88 sec = epoch_offset + tv.tv_sec;
89 now = localtime(&sec); /* not thread safe but we don't care */
91 snprintf(timebuf, sizeof(timebuf), "%02d:%02d:%02d.%06ld",
92 (int)now->tm_hour, (int)now->tm_min, (int)now->tm_sec, (long)tv.tv_usec);
95 vsnprintf(buffer, sizeof(buffer), msg, ap);
98 logfp = fopen(serverlogfile, "ab");
100 fprintf(logfp, "%s %s\n", timebuf, buffer);
105 fprintf(stderr, "fopen() failed with error: %d %s\n",
106 error, strerror(error));
107 fprintf(stderr, "Error opening file: %s\n", serverlogfile);
108 fprintf(stderr, "Msg not logged: %s %s\n", timebuf, buffer);
113 /* use instead of perror() on generic windows */
114 void win32_perror (const char *msg)
117 DWORD err = SOCKERRNO;
119 if (!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
120 LANG_NEUTRAL, buf, sizeof(buf), NULL))
121 snprintf(buf, sizeof(buf), "Unknown error %lu (%#lx)", err, err);
123 fprintf(stderr, "%s: ", msg);
124 fprintf(stderr, "%s\n", buf);
129 void win32_init(void)
131 WORD wVersionRequested;
134 wVersionRequested = MAKEWORD(USE_WINSOCK, USE_WINSOCK);
136 err = WSAStartup(wVersionRequested, &wsaData);
139 perror("Winsock init failed");
140 logmsg("Error initialising winsock -- aborting");
144 if ( LOBYTE( wsaData.wVersion ) != USE_WINSOCK ||
145 HIBYTE( wsaData.wVersion ) != USE_WINSOCK ) {
148 perror("Winsock init failed");
149 logmsg("No suitable winsock.dll found -- aborting");
154 void win32_cleanup(void)
158 #endif /* USE_WINSOCK */
160 /* set by the main code to point to where the test dir is */
161 const char *path=".";
163 char *test2file(long testno)
165 static char filename[256];
166 snprintf(filename, sizeof(filename), TEST_DATA_PATH, path, testno);
171 * Portable function used for waiting a specific amount of ms.
172 * Waiting indefinitely with this function is not allowed, a
173 * zero or negative timeout value will return immediately.
176 * -1 = system call error, or invalid timeout value
177 * 0 = specified timeout has elapsed
179 int wait_ms(int timeout_ms)
181 #if !defined(MSDOS) && !defined(USE_WINSOCK)
182 #ifndef HAVE_POLL_FINE
183 struct timeval pending_tv;
185 struct timeval initial_tv;
194 SET_SOCKERRNO(EINVAL);
199 #elif defined(USE_WINSOCK)
202 pending_ms = timeout_ms;
203 initial_tv = curlx_tvnow();
205 #if defined(HAVE_POLL_FINE)
206 r = poll(NULL, 0, pending_ms);
208 pending_tv.tv_sec = pending_ms / 1000;
209 pending_tv.tv_usec = (pending_ms % 1000) * 1000;
210 r = select(0, NULL, NULL, NULL, &pending_tv);
211 #endif /* HAVE_POLL_FINE */
215 if(error && (error != EINTR))
217 pending_ms = timeout_ms - (int)curlx_tvdiff(curlx_tvnow(), initial_tv);
221 #endif /* USE_WINSOCK */
227 int write_pidfile(const char *filename)
232 pid = (long)getpid();
233 pidfile = fopen(filename, "wb");
235 logmsg("Couldn't write pid file: %s %s", filename, strerror(ERRNO));
238 fprintf(pidfile, "%ld\n", pid);
240 logmsg("Wrote pid %ld to %s", pid, filename);
241 return 1; /* success */
244 void set_advisor_read_lock(const char *filename)
251 lockfile = fopen(filename, "wb");
252 } while((lockfile == NULL) && ((error = ERRNO) == EINTR));
253 if(lockfile == NULL) {
254 logmsg("Error creating lock file %s error: %d %s",
255 filename, error, strerror(error));
260 res = fclose(lockfile);
261 } while(res && ((error = ERRNO) == EINTR));
263 logmsg("Error closing lock file %s error: %d %s",
264 filename, error, strerror(error));
267 void clear_advisor_read_lock(const char *filename)
273 ** Log all removal failures. Even those due to file not existing.
274 ** This allows to detect if unexpectedly the file has already been
275 ** removed by a process different than the one that should do this.
279 res = unlink(filename);
280 } while(res && ((error = ERRNO) == EINTR));
282 logmsg("Error removing lock file %s error: %d %s",
283 filename, error, strerror(error));