42abeed8743a8eae501d3caea80eec95e3b94b8a
[platform/upstream/curl.git] / tests / server / util.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2010, 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 http://curl.haxx.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  * $Id$
22  ***************************************************************************/
23
24 #define CURL_NO_OLDIES
25
26 #include "setup.h" /* portability help from the lib directory */
27
28 #ifdef HAVE_SIGNAL_H
29 #include <signal.h>
30 #endif
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34 #ifdef HAVE_SYS_SOCKET_H
35 #include <sys/socket.h>
36 #endif
37 #ifdef HAVE_NETINET_IN_H
38 #include <netinet/in.h>
39 #endif
40 #ifdef _XOPEN_SOURCE_EXTENDED
41 /* This define is "almost" required to build on HPUX 11 */
42 #include <arpa/inet.h>
43 #endif
44 #ifdef HAVE_NETDB_H
45 #include <netdb.h>
46 #endif
47 #ifdef HAVE_SYS_POLL_H
48 #include <sys/poll.h>
49 #elif defined(HAVE_POLL_H)
50 #include <poll.h>
51 #endif
52
53 #define ENABLE_CURLX_PRINTF
54 /* make the curlx header define all printf() functions to use the curlx_*
55    versions instead */
56 #include "curlx.h" /* from the private lib dir */
57 #include "getpart.h"
58 #include "util.h"
59 #include "timeval.h"
60
61 #if defined(ENABLE_IPV6) && defined(__MINGW32__)
62 const struct in6_addr in6addr_any = {{ IN6ADDR_ANY_INIT }};
63 #endif
64
65 void logmsg(const char *msg, ...)
66 {
67   va_list ap;
68   char buffer[2048 + 1];
69   FILE *logfp;
70   int error;
71   struct timeval tv;
72   time_t sec;
73   struct tm *now;
74   char timebuf[20];
75   static time_t epoch_offset;
76   static int    known_offset;
77
78   if (!serverlogfile) {
79     fprintf(stderr, "Error: serverlogfile not set\n");
80     return;
81   }
82
83   tv = curlx_tvnow();
84   if(!known_offset) {
85     epoch_offset = time(NULL) - tv.tv_sec;
86     known_offset = 1;
87   }
88   sec = epoch_offset + tv.tv_sec;
89   now = localtime(&sec); /* not thread safe but we don't care */
90
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);
93
94   va_start(ap, msg);
95   vsnprintf(buffer, sizeof(buffer), msg, ap);
96   va_end(ap);
97
98   logfp = fopen(serverlogfile, "ab");
99   if(logfp) {
100     fprintf(logfp, "%s %s\n", timebuf, buffer);
101     fclose(logfp);
102   }
103   else {
104     error = ERRNO;
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);
109   }
110 }
111
112 #ifdef WIN32
113 /* use instead of perror() on generic windows */
114 void win32_perror (const char *msg)
115 {
116   char buf[512];
117   DWORD err = SOCKERRNO;
118
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);
122   if (msg)
123      fprintf(stderr, "%s: ", msg);
124   fprintf(stderr, "%s\n", buf);
125 }
126 #endif  /* WIN32 */
127
128 #ifdef USE_WINSOCK
129 void win32_init(void)
130 {
131   WORD wVersionRequested;
132   WSADATA wsaData;
133   int err;
134   wVersionRequested = MAKEWORD(USE_WINSOCK, USE_WINSOCK);
135
136   err = WSAStartup(wVersionRequested, &wsaData);
137
138   if (err != 0) {
139     perror("Winsock init failed");
140     logmsg("Error initialising winsock -- aborting");
141     exit(1);
142   }
143
144   if ( LOBYTE( wsaData.wVersion ) != USE_WINSOCK ||
145        HIBYTE( wsaData.wVersion ) != USE_WINSOCK ) {
146
147     WSACleanup();
148     perror("Winsock init failed");
149     logmsg("No suitable winsock.dll found -- aborting");
150     exit(1);
151   }
152 }
153
154 void win32_cleanup(void)
155 {
156   WSACleanup();
157 }
158 #endif  /* USE_WINSOCK */
159
160 /* set by the main code to point to where the test dir is */
161 const char *path=".";
162
163 char *test2file(long testno)
164 {
165   static char filename[256];
166   snprintf(filename, sizeof(filename), TEST_DATA_PATH, path, testno);
167   return filename;
168 }
169
170 /*
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.
174  *
175  * Return values:
176  *   -1 = system call error, or invalid timeout value
177  *    0 = specified timeout has elapsed
178  */
179 int wait_ms(int timeout_ms)
180 {
181 #if !defined(MSDOS) && !defined(USE_WINSOCK)
182 #ifndef HAVE_POLL_FINE
183   struct timeval pending_tv;
184 #endif
185   struct timeval initial_tv;
186   int pending_ms;
187   int error;
188 #endif
189   int r = 0;
190
191   if(!timeout_ms)
192     return 0;
193   if(timeout_ms < 0) {
194     SET_SOCKERRNO(EINVAL);
195     return -1;
196   }
197 #if defined(MSDOS)
198   delay(timeout_ms);
199 #elif defined(USE_WINSOCK)
200   Sleep(timeout_ms);
201 #else
202   pending_ms = timeout_ms;
203   initial_tv = curlx_tvnow();
204   do {
205 #if defined(HAVE_POLL_FINE)
206     r = poll(NULL, 0, pending_ms);
207 #else
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 */
212     if(r != -1)
213       break;
214     error = SOCKERRNO;
215     if(error && (error != EINTR))
216       break;
217     pending_ms = timeout_ms - (int)curlx_tvdiff(curlx_tvnow(), initial_tv);
218     if(pending_ms <= 0)
219       break;
220   } while(r == -1);
221 #endif /* USE_WINSOCK */
222   if(r)
223     r = -1;
224   return r;
225 }
226
227 int write_pidfile(const char *filename)
228 {
229   FILE *pidfile;
230   long pid;
231
232   pid = (long)getpid();
233   pidfile = fopen(filename, "wb");
234   if(!pidfile) {
235     logmsg("Couldn't write pid file: %s %s", filename, strerror(ERRNO));
236     return 0; /* fail */
237   }
238   fprintf(pidfile, "%ld\n", pid);
239   fclose(pidfile);
240   logmsg("Wrote pid %ld to %s", pid, filename);
241   return 1; /* success */
242 }
243
244 void set_advisor_read_lock(const char *filename)
245 {
246   FILE *lockfile;
247   int error = 0;
248   int res;
249
250   do {
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));
256     return;
257   }
258
259   do {
260     res = fclose(lockfile);
261   } while(res && ((error = ERRNO) == EINTR));
262   if(res)
263     logmsg("Error closing lock file %s error: %d %s",
264            filename, error, strerror(error));
265 }
266
267 void clear_advisor_read_lock(const char *filename)
268 {
269   int error = 0;
270   int res;
271
272   /*
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.
276   */
277
278   do {
279     res = unlink(filename);
280   } while(res && ((error = ERRNO) == EINTR));
281   if(res)
282     logmsg("Error removing lock file %s error: %d %s",
283            filename, error, strerror(error));
284 }