util.c 7.0 KB

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