utils.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * Copyright (C) 2008-2015 Tobias Brunner
  3. * Copyright (C) 2005-2008 Martin Willi
  4. * HSR Hochschule fuer Technik Rapperswil
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  13. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * for more details.
  15. */
  16. #include "utils.h"
  17. #include <sys/types.h>
  18. #include <unistd.h>
  19. #include <limits.h>
  20. #include <ctype.h>
  21. #include <errno.h>
  22. #ifndef WIN32
  23. # include <signal.h>
  24. #endif
  25. #ifndef HAVE_CLOSEFROM
  26. #if defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)
  27. # include <sys/stat.h>
  28. # include <fcntl.h>
  29. # include <sys/syscall.h>
  30. /* This is from the kernel sources. We limit the length of directory names to
  31. * 256 as we only use it to enumerate FDs. */
  32. struct linux_dirent64 {
  33. uint64_t d_ino;
  34. int64_t d_off;
  35. unsigned short d_reclen;
  36. unsigned char d_type;
  37. char d_name[256];
  38. };
  39. #else /* !defined(__linux__) || !defined(HAVE_SYS_SYSCALL_H) */
  40. # include <dirent.h>
  41. #endif /* defined(__linux__) && defined(HAVE_SYS_SYSCALL_H) */
  42. #endif
  43. #include <library.h>
  44. #include <collections/enumerator.h>
  45. #define FD_DIR "/proc/self/fd"
  46. #ifdef WIN32
  47. #include <threading/mutex.h>
  48. #include <threading/condvar.h>
  49. /**
  50. * Flag to indicate signaled wait_sigint()
  51. */
  52. static bool sigint_signaled = FALSE;
  53. /**
  54. * Condvar to wait in wait_sigint()
  55. */
  56. static condvar_t *sigint_cond;
  57. /**
  58. * Mutex to check signaling()
  59. */
  60. static mutex_t *sigint_mutex;
  61. /**
  62. * Control handler to catch ^C
  63. */
  64. static BOOL WINAPI handler(DWORD dwCtrlType)
  65. {
  66. switch (dwCtrlType)
  67. {
  68. case CTRL_C_EVENT:
  69. case CTRL_BREAK_EVENT:
  70. case CTRL_CLOSE_EVENT:
  71. sigint_mutex->lock(sigint_mutex);
  72. sigint_signaled = TRUE;
  73. sigint_cond->signal(sigint_cond);
  74. sigint_mutex->unlock(sigint_mutex);
  75. return TRUE;
  76. default:
  77. return FALSE;
  78. }
  79. }
  80. /**
  81. * Windows variant
  82. */
  83. void wait_sigint()
  84. {
  85. SetConsoleCtrlHandler(handler, TRUE);
  86. sigint_mutex = mutex_create(MUTEX_TYPE_DEFAULT);
  87. sigint_cond = condvar_create(CONDVAR_TYPE_DEFAULT);
  88. sigint_mutex->lock(sigint_mutex);
  89. while (!sigint_signaled)
  90. {
  91. sigint_cond->wait(sigint_cond, sigint_mutex);
  92. }
  93. sigint_mutex->unlock(sigint_mutex);
  94. sigint_mutex->destroy(sigint_mutex);
  95. sigint_cond->destroy(sigint_cond);
  96. }
  97. #else /* !WIN32 */
  98. /**
  99. * Unix variant
  100. */
  101. void wait_sigint()
  102. {
  103. sigset_t set;
  104. sigemptyset(&set);
  105. sigaddset(&set, SIGINT);
  106. sigaddset(&set, SIGTERM);
  107. sigprocmask(SIG_BLOCK, &set, NULL);
  108. while (sigwaitinfo(&set, NULL) == -1 && errno == EINTR)
  109. {
  110. /* wait for signal */
  111. }
  112. }
  113. #ifndef HAVE_SIGWAITINFO
  114. int sigwaitinfo(const sigset_t *set, void *info)
  115. {
  116. int sig, err;
  117. if (info)
  118. { /* we don't replicate siginfo_t, fail if anybody tries to use it */
  119. errno = EINVAL;
  120. return -1;
  121. }
  122. err = sigwait(set, &sig);
  123. if (err != 0)
  124. {
  125. errno = err;
  126. sig = -1;
  127. }
  128. return sig;
  129. }
  130. #endif /* HAVE_SIGWAITINFO */
  131. #endif /* WIN32 */
  132. #ifndef HAVE_CLOSEFROM
  133. /**
  134. * Described in header.
  135. */
  136. void closefrom(int low_fd)
  137. {
  138. int max_fd, dir_fd, fd;
  139. /* try to close only open file descriptors on Linux... */
  140. #if defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)
  141. /* By directly using a syscall we avoid any calls that might be unsafe after
  142. * fork() (e.g. malloc()). */
  143. char buffer[sizeof(struct linux_dirent64)];
  144. struct linux_dirent64 *entry;
  145. int offset, len;
  146. dir_fd = open("/proc/self/fd", O_RDONLY);
  147. if (dir_fd != -1)
  148. {
  149. while ((len = syscall(__NR_getdents64, dir_fd, buffer,
  150. sizeof(buffer))) > 0)
  151. {
  152. for (offset = 0; offset < len; offset += entry->d_reclen)
  153. {
  154. entry = (struct linux_dirent64*)(buffer + offset);
  155. if (!isdigit(entry->d_name[0]))
  156. {
  157. continue;
  158. }
  159. fd = atoi(entry->d_name);
  160. if (fd != dir_fd && fd >= low_fd)
  161. {
  162. close(fd);
  163. }
  164. }
  165. }
  166. close(dir_fd);
  167. return;
  168. }
  169. #else /* !defined(__linux__) || !defined(HAVE_SYS_SYSCALL_H) */
  170. /* This is potentially unsafe when called after fork() in multi-threaded
  171. * applications. In particular opendir() will require an allocation.
  172. * Depends on how the malloc() implementation handles such situations. */
  173. DIR *dir;
  174. struct dirent *entry;
  175. #ifndef HAVE_DIRFD
  176. /* if we don't have dirfd() lets close the lowest FD and hope it gets reused
  177. * by opendir() */
  178. close(low_fd);
  179. dir_fd = low_fd++;
  180. #endif
  181. dir = opendir(FD_DIR);
  182. if (dir)
  183. {
  184. #ifdef HAVE_DIRFD
  185. dir_fd = dirfd(dir);
  186. #endif
  187. while ((entry = readdir(dir)))
  188. {
  189. if (!isdigit(entry->d_name[0]))
  190. {
  191. continue;
  192. }
  193. fd = atoi(entry->d_name);
  194. if (fd != dir_fd && fd >= low_fd)
  195. {
  196. close(fd);
  197. }
  198. }
  199. closedir(dir);
  200. return;
  201. }
  202. #endif /* defined(__linux__) && defined(HAVE_SYS_SYSCALL_H) */
  203. /* ...fall back to closing all fds otherwise */
  204. #ifdef WIN32
  205. max_fd = _getmaxstdio();
  206. #else
  207. max_fd = (int)sysconf(_SC_OPEN_MAX);
  208. #endif
  209. if (max_fd < 0)
  210. {
  211. max_fd = 256;
  212. }
  213. for (fd = low_fd; fd < max_fd; fd++)
  214. {
  215. close(fd);
  216. }
  217. }
  218. #endif /* HAVE_CLOSEFROM */
  219. /**
  220. * return null
  221. */
  222. void *return_null()
  223. {
  224. return NULL;
  225. }
  226. /**
  227. * returns TRUE
  228. */
  229. bool return_true()
  230. {
  231. return TRUE;
  232. }
  233. /**
  234. * returns FALSE
  235. */
  236. bool return_false()
  237. {
  238. return FALSE;
  239. }
  240. /**
  241. * nop operation
  242. */
  243. void nop()
  244. {
  245. }
  246. /**
  247. * See header
  248. */
  249. void utils_init()
  250. {
  251. #ifdef WIN32
  252. windows_init();
  253. #endif
  254. atomics_init();
  255. strerror_init();
  256. }
  257. /**
  258. * See header
  259. */
  260. void utils_deinit()
  261. {
  262. #ifdef WIN32
  263. windows_deinit();
  264. #endif
  265. atomics_deinit();
  266. strerror_deinit();
  267. }