util.c 6.6 KB

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