Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / tests / server / util.c
bloba089ccb80c7d64554cf84155c899e22554d11ec2
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2008, 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 * $Id: util.c,v 1.1.1.1 2008-09-23 16:32:06 hoffman Exp $
22 ***************************************************************************/
23 #include "setup.h" /* portability help from the lib directory */
25 #ifdef HAVE_SIGNAL_H
26 #include <signal.h>
27 #endif
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31 #ifdef HAVE_SYS_SOCKET_H
32 #include <sys/socket.h>
33 #endif
34 #ifdef HAVE_NETINET_IN_H
35 #include <netinet/in.h>
36 #endif
37 #ifdef _XOPEN_SOURCE_EXTENDED
38 /* This define is "almost" required to build on HPUX 11 */
39 #include <arpa/inet.h>
40 #endif
41 #ifdef HAVE_NETDB_H
42 #include <netdb.h>
43 #endif
44 #ifdef HAVE_SYS_POLL_H
45 #include <sys/poll.h>
46 #elif defined(HAVE_POLL_H)
47 #include <poll.h>
48 #endif
50 #define ENABLE_CURLX_PRINTF
51 /* make the curlx header define all printf() functions to use the curlx_*
52 versions instead */
53 #include "curlx.h" /* from the private lib dir */
54 #include "getpart.h"
55 #include "util.h"
56 #include "timeval.h"
58 #if defined(ENABLE_IPV6) && defined(__MINGW32__)
59 const struct in6_addr in6addr_any = {{ IN6ADDR_ANY_INIT }};
60 #endif
62 /* someone else must set this properly */
63 extern const char *serverlogfile;
65 void logmsg(const char *msg, ...)
67 va_list ap;
68 char buffer[512]; /* possible overflow if you pass in a huge string */
69 FILE *logfp;
70 int error;
71 struct timeval tv;
72 time_t sec;
73 struct tm *now;
74 char timebuf[20];
76 if (!serverlogfile) {
77 fprintf(stderr, "Error: serverlogfile not set\n");
78 return;
81 tv = curlx_tvnow();
82 sec = tv.tv_sec;
83 now = localtime(&sec); /* not multithread safe but we don't care */
85 snprintf(timebuf, sizeof(timebuf), "%02d:%02d:%02d.%06ld",
86 now->tm_hour, now->tm_min, now->tm_sec, tv.tv_usec);
88 va_start(ap, msg);
89 vsprintf(buffer, msg, ap);
90 va_end(ap);
92 logfp = fopen(serverlogfile, "a");
93 if(logfp) {
94 fprintf(logfp, "%s %s\n", timebuf, buffer);
95 fclose(logfp);
97 else {
98 error = ERRNO;
99 fprintf(stderr, "fopen() failed with error: %d %s\n",
100 error, strerror(error));
101 fprintf(stderr, "Error opening file: %s\n", serverlogfile);
102 fprintf(stderr, "Msg not logged: %s %s\n", timebuf, buffer);
106 #ifdef WIN32
107 /* use instead of perror() on generic windows */
108 void win32_perror (const char *msg)
110 char buf[512];
111 DWORD err = SOCKERRNO;
113 if (!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
114 LANG_NEUTRAL, buf, sizeof(buf), NULL))
115 snprintf(buf, sizeof(buf), "Unknown error %lu (%#lx)", err, err);
116 if (msg)
117 fprintf(stderr, "%s: ", msg);
118 fprintf(stderr, "%s\n", buf);
120 #endif /* WIN32 */
122 #ifdef USE_WINSOCK
123 void win32_init(void)
125 WORD wVersionRequested;
126 WSADATA wsaData;
127 int err;
128 wVersionRequested = MAKEWORD(USE_WINSOCK, USE_WINSOCK);
130 err = WSAStartup(wVersionRequested, &wsaData);
132 if (err != 0) {
133 perror("Winsock init failed");
134 logmsg("Error initialising winsock -- aborting");
135 exit(1);
138 if ( LOBYTE( wsaData.wVersion ) != USE_WINSOCK ||
139 HIBYTE( wsaData.wVersion ) != USE_WINSOCK ) {
141 WSACleanup();
142 perror("Winsock init failed");
143 logmsg("No suitable winsock.dll found -- aborting");
144 exit(1);
148 void win32_cleanup(void)
150 WSACleanup();
152 #endif /* USE_WINSOCK */
154 /* set by the main code to point to where the test dir is */
155 const char *path=".";
157 char *test2file(long testno)
159 static char filename[256];
160 snprintf(filename, sizeof(filename), TEST_DATA_PATH, path, testno);
161 return filename;
165 * Portable function used for waiting a specific amount of ms.
166 * Waiting indefinitely with this function is not allowed, a
167 * zero or negative timeout value will return immediately.
169 * Return values:
170 * -1 = system call error, or invalid timeout value
171 * 0 = specified timeout has elapsed
173 int wait_ms(int timeout_ms)
175 #if !defined(MSDOS) && !defined(USE_WINSOCK)
176 #ifndef HAVE_POLL_FINE
177 struct timeval pending_tv;
178 #endif
179 struct timeval initial_tv;
180 int pending_ms;
181 int error;
182 #endif
183 int r = 0;
185 if(!timeout_ms)
186 return 0;
187 if(timeout_ms < 0) {
188 SET_SOCKERRNO(EINVAL);
189 return -1;
191 #if defined(MSDOS)
192 delay(timeout_ms);
193 #elif defined(USE_WINSOCK)
194 Sleep(timeout_ms);
195 #else
196 pending_ms = timeout_ms;
197 initial_tv = curlx_tvnow();
198 do {
199 #if defined(HAVE_POLL_FINE)
200 r = poll(NULL, 0, pending_ms);
201 #else
202 pending_tv.tv_sec = pending_ms / 1000;
203 pending_tv.tv_usec = (pending_ms % 1000) * 1000;
204 r = select(0, NULL, NULL, NULL, &pending_tv);
205 #endif /* HAVE_POLL_FINE */
206 if(r != -1)
207 break;
208 error = SOCKERRNO;
209 if(error == EINVAL)
210 break;
211 pending_ms = timeout_ms - (int)curlx_tvdiff(curlx_tvnow(), initial_tv);
212 if(pending_ms <= 0)
213 break;
214 } while(r == -1);
215 #endif /* USE_WINSOCK */
216 if(r)
217 r = -1;
218 return r;
221 int write_pidfile(const char *filename)
223 FILE *pidfile;
224 long pid;
226 pid = (long)getpid();
227 pidfile = fopen(filename, "w");
228 if(!pidfile) {
229 logmsg("Couldn't write pid file: %s %s", filename, strerror(ERRNO));
230 return 0; /* fail */
232 fprintf(pidfile, "%ld\n", pid);
233 fclose(pidfile);
234 logmsg("Wrote pid %ld to %s", pid, filename);
235 return 1; /* success */
238 void set_advisor_read_lock(const char *filename)
240 FILE *lockfile;
241 int error;
242 int res;
244 do {
245 lockfile = fopen(filename, "wb");
246 } while((lockfile == NULL) && ((error = ERRNO) == EINTR));
247 if(lockfile == NULL) {
248 logmsg("Error creating lock file %s error: %d %s",
249 filename, error, strerror(error));
250 return;
253 do {
254 res = fclose(lockfile);
255 } while(res && ((error = ERRNO) == EINTR));
256 if(res)
257 logmsg("Error closing lock file %s error: %d %s",
258 filename, error, strerror(error));
261 void clear_advisor_read_lock(const char *filename)
263 int error;
264 int res;
266 do {
267 res = unlink(filename);
268 } while(res && ((error = ERRNO) == EINTR));
269 if(res)
270 logmsg("Error removing lock file %s error: %d %s",
271 filename, error, strerror(error));