util.c 7.4 KB

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