util.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /* Copyright 2003 Roger Dingledine */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "../or/or.h"
  5. #ifdef MS_WINDOWS
  6. #include <io.h>
  7. #include <limits.h>
  8. #include <process.h>
  9. #endif
  10. #include "util.h"
  11. #include "log.h"
  12. /*
  13. * Memory
  14. */
  15. void *tor_malloc(size_t size) {
  16. void *result;
  17. result = malloc(size);
  18. if(!result) {
  19. log_fn(LOG_ERR, "Out of memory. Dying.");
  20. exit(1);
  21. }
  22. return result;
  23. }
  24. /*
  25. * Time
  26. */
  27. void
  28. my_gettimeofday(struct timeval *timeval)
  29. {
  30. #ifdef HAVE_GETTIMEOFDAY
  31. if (gettimeofday(timeval, NULL)) {
  32. log_fn(LOG_ERR, "gettimeofday failed.");
  33. /* If gettimeofday dies, we have either given a bad timezone (we didn't),
  34. or segfaulted.*/
  35. exit(1);
  36. }
  37. #elif defined(HAVE_FTIME)
  38. ftime(timeval);
  39. #else
  40. #error "No way to get time."
  41. #endif
  42. return;
  43. }
  44. long
  45. tv_udiff(struct timeval *start, struct timeval *end)
  46. {
  47. long udiff;
  48. long end_usec = end->tv_usec;
  49. long secdiff = end->tv_sec - start->tv_sec;
  50. if (secdiff+1 > LONG_MAX/1000000) {
  51. log_fn(LOG_NOTICE, "comparing times too far apart.");
  52. return LONG_MAX;
  53. }
  54. udiff = secdiff*1000000L + (end_usec - start->tv_usec);
  55. if(udiff < 0) {
  56. log_fn(LOG_NOTICE, "start is after end. Returning 0.");
  57. return 0;
  58. }
  59. return udiff;
  60. }
  61. int tv_cmp(struct timeval *a, struct timeval *b) {
  62. if (a->tv_sec > b->tv_sec)
  63. return 1;
  64. if (a->tv_sec < b->tv_sec)
  65. return -1;
  66. if (a->tv_usec > b->tv_usec)
  67. return 1;
  68. if (a->tv_usec < b->tv_usec)
  69. return -1;
  70. return 0;
  71. }
  72. void tv_add(struct timeval *a, struct timeval *b) {
  73. a->tv_usec += b->tv_usec;
  74. a->tv_sec += b->tv_sec + (a->tv_usec / 1000000);
  75. a->tv_usec %= 1000000;
  76. }
  77. void tv_addms(struct timeval *a, long ms) {
  78. a->tv_usec += (ms * 1000) % 1000000;
  79. a->tv_sec += ((ms * 1000) / 1000000) + (a->tv_usec / 1000000);
  80. a->tv_usec %= 1000000;
  81. }
  82. /*
  83. * Low-level I/O.
  84. */
  85. /* a wrapper for write(2) that makes sure to write all count bytes.
  86. * Only use if fd is a blocking socket. */
  87. int write_all(int fd, const void *buf, size_t count) {
  88. int written = 0;
  89. int result;
  90. while(written != count) {
  91. result = write(fd, buf+written, count-written);
  92. if(result<0)
  93. return -1;
  94. written += result;
  95. }
  96. return count;
  97. }
  98. /* a wrapper for read(2) that makes sure to read all count bytes.
  99. * Only use if fd is a blocking socket. */
  100. int read_all(int fd, void *buf, size_t count) {
  101. int numread = 0;
  102. int result;
  103. while(numread != count) {
  104. result = read(fd, buf+numread, count-numread);
  105. if(result<=0)
  106. return -1;
  107. numread += result;
  108. }
  109. return count;
  110. }
  111. void set_socket_nonblocking(int socket)
  112. {
  113. #ifdef MS_WINDOWS
  114. /* Yes means no and no means yes. Do you not want to be nonblocking? */
  115. int nonblocking = 0;
  116. ioctlsocket(socket, FIONBIO, (unsigned long*) &nonblocking);
  117. #else
  118. fcntl(socket, F_SETFL, O_NONBLOCK);
  119. #endif
  120. }
  121. /*
  122. * Process control
  123. */
  124. int spawn_func(int (*func)(void *), void *data)
  125. {
  126. #ifdef MS_WINDOWS
  127. int rv;
  128. rv = _beginthread(func, 0, data);
  129. if (rv == (unsigned long) -1)
  130. return -1;
  131. return 0;
  132. #else
  133. pid_t pid;
  134. pid = fork();
  135. if (pid<0)
  136. return -1;
  137. if (pid==0) {
  138. /* Child */
  139. func(data);
  140. assert(0); /* Should never reach here. */
  141. return 0; /* suppress "control-reaches-end-of-non-void" warning. */
  142. } else {
  143. /* Parent */
  144. return 0;
  145. }
  146. #endif
  147. }
  148. void spawn_exit()
  149. {
  150. #ifdef MS_WINDOWS
  151. _endthread();
  152. #else
  153. exit(0);
  154. #endif
  155. }
  156. /*
  157. * Windows compatibility.
  158. */
  159. int
  160. tor_socketpair(int family, int type, int protocol, int fd[2])
  161. {
  162. #ifdef HAVE_SOCKETPAIR_XXXX
  163. /* For testing purposes, we never fall back to real socketpairs. */
  164. return socketpair(family, type, protocol, fd);
  165. #else
  166. int listener = -1;
  167. int connector = -1;
  168. int acceptor = -1;
  169. struct sockaddr_in listen_addr;
  170. struct sockaddr_in connect_addr;
  171. size_t size;
  172. if (protocol
  173. #ifdef AF_UNIX
  174. || family != AF_UNIX
  175. #endif
  176. ) {
  177. #ifdef MS_WINDOWS
  178. errno = WSAEAFNOSUPPORT;
  179. #else
  180. errno = EAFNOSUPPORT;
  181. #endif
  182. return -1;
  183. }
  184. if (!fd) {
  185. errno = EINVAL;
  186. return -1;
  187. }
  188. listener = socket(AF_INET, type, 0);
  189. if (listener == -1)
  190. return -1;
  191. memset (&listen_addr, 0, sizeof (listen_addr));
  192. listen_addr.sin_family = AF_INET;
  193. listen_addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
  194. listen_addr.sin_port = 0; /* kernel choses port. */
  195. if (bind(listener, (struct sockaddr *) &listen_addr, sizeof (listen_addr))
  196. == -1)
  197. goto tidy_up_and_fail;
  198. if (listen(listener, 1) == -1)
  199. goto tidy_up_and_fail;
  200. connector = socket(AF_INET, type, 0);
  201. if (connector == -1)
  202. goto tidy_up_and_fail;
  203. /* We want to find out the port number to connect to. */
  204. size = sizeof (connect_addr);
  205. if (getsockname(listener, (struct sockaddr *) &connect_addr, &size) == -1)
  206. goto tidy_up_and_fail;
  207. if (size != sizeof (connect_addr))
  208. goto abort_tidy_up_and_fail;
  209. if (connect(connector, (struct sockaddr *) &connect_addr,
  210. sizeof (connect_addr)) == -1)
  211. goto tidy_up_and_fail;
  212. size = sizeof (listen_addr);
  213. acceptor = accept(listener, (struct sockaddr *) &listen_addr, &size);
  214. if (acceptor == -1)
  215. goto tidy_up_and_fail;
  216. if (size != sizeof(listen_addr))
  217. goto abort_tidy_up_and_fail;
  218. close(listener);
  219. /* Now check we are talking to ourself by matching port and host on the
  220. two sockets. */
  221. if (getsockname(connector, (struct sockaddr *) &connect_addr, &size) == -1)
  222. goto tidy_up_and_fail;
  223. if (size != sizeof (connect_addr)
  224. || listen_addr.sin_family != connect_addr.sin_family
  225. || listen_addr.sin_addr.s_addr != connect_addr.sin_addr.s_addr
  226. || listen_addr.sin_port != connect_addr.sin_port) {
  227. goto abort_tidy_up_and_fail;
  228. }
  229. fd[0] = connector;
  230. fd[1] = acceptor;
  231. return 0;
  232. abort_tidy_up_and_fail:
  233. #ifdef MS_WINDOWS
  234. errno = WSAECONNABORTED;
  235. #else
  236. errno = ECONNABORTED; /* I hope this is portable and appropriate. */
  237. #endif
  238. tidy_up_and_fail:
  239. {
  240. int save_errno = errno;
  241. if (listener != -1)
  242. close(listener);
  243. if (connector != -1)
  244. close(connector);
  245. if (acceptor != -1)
  246. close(acceptor);
  247. errno = save_errno;
  248. return -1;
  249. }
  250. #endif
  251. }
  252. #ifdef MS_WINDOWS
  253. int correct_socket_errno(int s)
  254. {
  255. int optval, optvallen=sizeof(optval);
  256. assert(errno == WSAEWOULDBLOCK);
  257. if (getsockopt(s, SOL_SOCKET, SO_ERROR, (void*)&optval, &optvallen))
  258. return errno;
  259. if (optval)
  260. return optval;
  261. return WSAEWOULDBLOCK;
  262. }
  263. #endif
  264. /*
  265. * Filesystem operations.
  266. */
  267. file_status_t file_status(const char *fname)
  268. {
  269. struct stat st;
  270. if (stat(fname, &st)) {
  271. if (errno == ENOENT) {
  272. return FN_NOENT;
  273. }
  274. return FN_ERROR;
  275. }
  276. if (st.st_mode & S_IFDIR)
  277. return FN_DIR;
  278. else if (st.st_mode & S_IFREG)
  279. return FN_FILE;
  280. else
  281. return FN_ERROR;
  282. }
  283. int check_private_dir(const char *dirname, int create)
  284. {
  285. struct stat st;
  286. if (stat(dirname, &st)) {
  287. if (errno != ENOENT) {
  288. log(LOG_ERR, "Directory %s cannot be read: %s", dirname,
  289. strerror(errno));
  290. return -1;
  291. }
  292. if (!create) {
  293. log(LOG_ERR, "Directory %s does not exist.", dirname);
  294. return -1;
  295. }
  296. log(LOG_INFO, "Creating directory %s", dirname);
  297. if (mkdir(dirname, 0700)) {
  298. log(LOG_ERR, "Error creating directory %s: %s", dirname,
  299. strerror(errno));
  300. return -1;
  301. } else {
  302. return 0;
  303. }
  304. }
  305. if (!(st.st_mode & S_IFDIR)) {
  306. log(LOG_ERR, "%s is not a directory", dirname);
  307. return -1;
  308. }
  309. if (st.st_uid != getuid()) {
  310. log(LOG_ERR, "%s is not owned by this UID (%d)", dirname, getuid());
  311. return -1;
  312. }
  313. if (st.st_mode & 0077) {
  314. log(LOG_WARNING, "Fixing permissions on directory %s", dirname);
  315. if (chmod(dirname, 0700)) {
  316. log(LOG_ERR, "Could not chmod directory %s: %s", dirname,
  317. strerror(errno));
  318. return -1;
  319. } else {
  320. return 0;
  321. }
  322. }
  323. return 0;
  324. }
  325. int
  326. write_str_to_file(const char *fname, const char *str)
  327. {
  328. char tempname[1024];
  329. int fd;
  330. FILE *file;
  331. if (strlen(fname) > 1000) {
  332. log(LOG_ERR, "Filename %s is too long.", fname);
  333. return -1;
  334. }
  335. strcpy(tempname,fname);
  336. strcat(tempname,".tmp");
  337. if ((fd = open(tempname, O_WRONLY|O_CREAT|O_TRUNC, 0600)) < 0) {
  338. log(LOG_ERR, "Couldn't open %s for writing: %s", tempname,
  339. strerror(errno));
  340. return -1;
  341. }
  342. if (!(file = fdopen(fd, "w"))) {
  343. log(LOG_ERR, "Couldn't fdopen %s for writing: %s", tempname,
  344. strerror(errno));
  345. close(fd); return -1;
  346. }
  347. if (fputs(str,file) == EOF) {
  348. log(LOG_ERR, "Error writing to %s: %s", tempname, strerror(errno));
  349. fclose(file); return -1;
  350. }
  351. fclose(file);
  352. if (rename(tempname, fname)) {
  353. log(LOG_ERR, "Error replacing %s: %s", fname, strerror(errno));
  354. return -1;
  355. }
  356. return 0;
  357. }