1 /***************************************************************************
3 * Project ___| | | | _ \| |
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 */
31 #ifdef HAVE_SYS_SOCKET_H
32 #include <sys/socket.h>
34 #ifdef HAVE_NETINET_IN_H
35 #include <netinet/in.h>
37 #ifdef _XOPEN_SOURCE_EXTENDED
38 /* This define is "almost" required to build on HPUX 11 */
39 #include <arpa/inet.h>
44 #ifdef HAVE_SYS_POLL_H
46 #elif defined(HAVE_POLL_H)
50 #define ENABLE_CURLX_PRINTF
51 /* make the curlx header define all printf() functions to use the curlx_*
53 #include "curlx.h" /* from the private lib dir */
58 #if defined(ENABLE_IPV6) && defined(__MINGW32__)
59 const struct in6_addr in6addr_any
= {{ IN6ADDR_ANY_INIT
}};
62 /* someone else must set this properly */
63 extern const char *serverlogfile
;
65 void logmsg(const char *msg
, ...)
68 char buffer
[512]; /* possible overflow if you pass in a huge string */
77 fprintf(stderr
, "Error: serverlogfile not set\n");
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
);
89 vsprintf(buffer
, msg
, ap
);
92 logfp
= fopen(serverlogfile
, "a");
94 fprintf(logfp
, "%s %s\n", timebuf
, buffer
);
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
);
107 /* use instead of perror() on generic windows */
108 void win32_perror (const char *msg
)
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
);
117 fprintf(stderr
, "%s: ", msg
);
118 fprintf(stderr
, "%s\n", buf
);
123 void win32_init(void)
125 WORD wVersionRequested
;
128 wVersionRequested
= MAKEWORD(USE_WINSOCK
, USE_WINSOCK
);
130 err
= WSAStartup(wVersionRequested
, &wsaData
);
133 perror("Winsock init failed");
134 logmsg("Error initialising winsock -- aborting");
138 if ( LOBYTE( wsaData
.wVersion
) != USE_WINSOCK
||
139 HIBYTE( wsaData
.wVersion
) != USE_WINSOCK
) {
142 perror("Winsock init failed");
143 logmsg("No suitable winsock.dll found -- aborting");
148 void win32_cleanup(void)
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
);
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.
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
;
179 struct timeval initial_tv
;
188 SET_SOCKERRNO(EINVAL
);
193 #elif defined(USE_WINSOCK)
196 pending_ms
= timeout_ms
;
197 initial_tv
= curlx_tvnow();
199 #if defined(HAVE_POLL_FINE)
200 r
= poll(NULL
, 0, pending_ms
);
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 */
211 pending_ms
= timeout_ms
- (int)curlx_tvdiff(curlx_tvnow(), initial_tv
);
215 #endif /* USE_WINSOCK */
221 int write_pidfile(const char *filename
)
226 pid
= (long)getpid();
227 pidfile
= fopen(filename
, "w");
229 logmsg("Couldn't write pid file: %s %s", filename
, strerror(ERRNO
));
232 fprintf(pidfile
, "%ld\n", pid
);
234 logmsg("Wrote pid %ld to %s", pid
, filename
);
235 return 1; /* success */
238 void set_advisor_read_lock(const char *filename
)
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
));
254 res
= fclose(lockfile
);
255 } while(res
&& ((error
= ERRNO
) == EINTR
));
257 logmsg("Error closing lock file %s error: %d %s",
258 filename
, error
, strerror(error
));
261 void clear_advisor_read_lock(const char *filename
)
267 res
= unlink(filename
);
268 } while(res
&& ((error
= ERRNO
) == EINTR
));
270 logmsg("Error removing lock file %s error: %d %s",
271 filename
, error
, strerror(error
));