util.c 6.6 KB

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