astfd.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2009, Digium, Inc.
  5. *
  6. * Tilghman Lesher <tlesher@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Debugging routines for file descriptor leaks
  21. *
  22. * \author Tilghman Lesher \verbatim <tlesher@digium.com> \endverbatim
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. #ifdef DEBUG_FD_LEAKS
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <stddef.h>
  32. #include <time.h>
  33. #include <sys/time.h>
  34. #include <sys/resource.h>
  35. #include "asterisk/cli.h"
  36. #include "asterisk/logger.h"
  37. #include "asterisk/options.h"
  38. #include "asterisk/lock.h"
  39. #include "asterisk/strings.h"
  40. #include "asterisk/unaligned.h"
  41. #include "asterisk/localtime.h"
  42. #include "asterisk/time.h"
  43. static struct fdleaks {
  44. const char *callname;
  45. int line;
  46. unsigned int isopen:1;
  47. char file[40];
  48. char function[25];
  49. char callargs[100];
  50. struct timeval now;
  51. } fdleaks[1024] = { { "", }, };
  52. /* COPY does ast_copy_string(dst, src, sizeof(dst)), except:
  53. * - if it doesn't fit, it copies the value after the slash
  54. * (possibly truncated)
  55. * - if there is no slash, it copies the value with the head
  56. * truncated */
  57. #define COPY(dst, src) \
  58. do { \
  59. int dlen = sizeof(dst), slen = strlen(src); \
  60. if (slen + 1 > dlen) { \
  61. const char *slash = strrchr(src, '/'); \
  62. if (slash) { \
  63. ast_copy_string(dst, slash + 1, dlen); \
  64. } else { \
  65. ast_copy_string(dst, src + slen - dlen + 1, dlen); \
  66. } \
  67. } else { \
  68. ast_copy_string(dst, src, dlen); \
  69. } \
  70. } while (0)
  71. #define STORE_COMMON(offset, name, ...) \
  72. do { \
  73. struct fdleaks *tmp = &fdleaks[offset]; \
  74. tmp->now = ast_tvnow(); \
  75. COPY(tmp->file, file); \
  76. tmp->line = line; \
  77. COPY(tmp->function, func); \
  78. tmp->callname = name; \
  79. snprintf(tmp->callargs, sizeof(tmp->callargs), __VA_ARGS__); \
  80. tmp->isopen = 1; \
  81. } while (0)
  82. #undef open
  83. int __ast_fdleak_open(const char *file, int line, const char *func, const char *path, int flags, ...)
  84. {
  85. int res;
  86. va_list ap;
  87. int mode;
  88. if (flags & O_CREAT) {
  89. va_start(ap, flags);
  90. mode = va_arg(ap, int);
  91. va_end(ap);
  92. res = open(path, flags, mode);
  93. if (res > -1 && res < ARRAY_LEN(fdleaks)) {
  94. char sflags[80];
  95. snprintf(sflags, sizeof(sflags), "O_CREAT%s%s%s%s%s%s%s%s",
  96. flags & O_APPEND ? "|O_APPEND" : "",
  97. flags & O_EXCL ? "|O_EXCL" : "",
  98. flags & O_NONBLOCK ? "|O_NONBLOCK" : "",
  99. flags & O_TRUNC ? "|O_TRUNC" : "",
  100. flags & O_RDWR ? "|O_RDWR" : "",
  101. #if O_RDONLY == 0
  102. !(flags & (O_WRONLY | O_RDWR)) ? "|O_RDONLY" : "",
  103. #else
  104. flags & O_RDONLY ? "|O_RDONLY" : "",
  105. #endif
  106. flags & O_WRONLY ? "|O_WRONLY" : "",
  107. "");
  108. flags &= ~(O_CREAT | O_APPEND | O_EXCL | O_NONBLOCK | O_TRUNC | O_RDWR | O_RDONLY | O_WRONLY);
  109. if (flags) {
  110. STORE_COMMON(res, "open", "\"%s\",%s|%d,%04o", path, sflags, flags, mode);
  111. } else {
  112. STORE_COMMON(res, "open", "\"%s\",%s,%04o", path, sflags, mode);
  113. }
  114. }
  115. } else {
  116. res = open(path, flags);
  117. if (res > -1 && res < ARRAY_LEN(fdleaks)) {
  118. STORE_COMMON(res, "open", "\"%s\",%d", path, flags);
  119. }
  120. }
  121. return res;
  122. }
  123. #undef accept
  124. int __ast_fdleak_accept(int socket, struct sockaddr *address, socklen_t *address_len,
  125. const char *file, int line, const char *func)
  126. {
  127. int res = accept(socket, address, address_len);
  128. if (res >= 0) {
  129. STORE_COMMON(res, "accept", "{%d}", socket);
  130. }
  131. return res;
  132. }
  133. #undef pipe
  134. int __ast_fdleak_pipe(int *fds, const char *file, int line, const char *func)
  135. {
  136. int i, res = pipe(fds);
  137. if (res) {
  138. return res;
  139. }
  140. for (i = 0; i < 2; i++) {
  141. if (fds[i] > -1 && fds[i] < ARRAY_LEN(fdleaks)) {
  142. STORE_COMMON(fds[i], "pipe", "{%d,%d}", fds[0], fds[1]);
  143. }
  144. }
  145. return 0;
  146. }
  147. #undef socketpair
  148. int __ast_fdleak_socketpair(int domain, int type, int protocol, int sv[2],
  149. const char *file, int line, const char *func)
  150. {
  151. int i, res = socketpair(domain, type, protocol, sv);
  152. if (res) {
  153. return res;
  154. }
  155. for (i = 0; i < 2; i++) {
  156. if (sv[i] > -1 && sv[i] < ARRAY_LEN(fdleaks)) {
  157. STORE_COMMON(sv[i], "socketpair", "{%d,%d}", sv[0], sv[1]);
  158. }
  159. }
  160. return 0;
  161. }
  162. #if defined(HAVE_EVENTFD)
  163. #undef eventfd
  164. #include <sys/eventfd.h>
  165. int __ast_fdleak_eventfd(unsigned int initval, int flags, const char *file, int line, const char *func)
  166. {
  167. int res = eventfd(initval, flags);
  168. if (res >= 0) {
  169. STORE_COMMON(res, "eventfd", "{%d}", res);
  170. }
  171. return res;
  172. }
  173. #endif
  174. #if defined(HAVE_TIMERFD)
  175. #undef timerfd_create
  176. #include <sys/timerfd.h>
  177. int __ast_fdleak_timerfd_create(int clockid, int flags, const char *file, int line, const char *func)
  178. {
  179. int res = timerfd_create(clockid, flags);
  180. if (res >= 0) {
  181. STORE_COMMON(res, "timerfd_create", "{%d}", res);
  182. }
  183. return res;
  184. }
  185. #endif
  186. #undef socket
  187. int __ast_fdleak_socket(int domain, int type, int protocol, const char *file, int line, const char *func)
  188. {
  189. char sdomain[20], stype[20], *sproto = NULL;
  190. struct protoent *pe;
  191. int res = socket(domain, type, protocol);
  192. if (res < 0 || res >= ARRAY_LEN(fdleaks)) {
  193. return res;
  194. }
  195. if ((pe = getprotobynumber(protocol))) {
  196. sproto = pe->p_name;
  197. }
  198. if (domain == PF_UNIX) {
  199. ast_copy_string(sdomain, "PF_UNIX", sizeof(sdomain));
  200. } else if (domain == PF_INET) {
  201. ast_copy_string(sdomain, "PF_INET", sizeof(sdomain));
  202. } else {
  203. snprintf(sdomain, sizeof(sdomain), "%d", domain);
  204. }
  205. if (type == SOCK_DGRAM) {
  206. ast_copy_string(stype, "SOCK_DGRAM", sizeof(stype));
  207. if (protocol == 0) {
  208. sproto = "udp";
  209. }
  210. } else if (type == SOCK_STREAM) {
  211. ast_copy_string(stype, "SOCK_STREAM", sizeof(stype));
  212. if (protocol == 0) {
  213. sproto = "tcp";
  214. }
  215. } else {
  216. snprintf(stype, sizeof(stype), "%d", type);
  217. }
  218. if (sproto) {
  219. STORE_COMMON(res, "socket", "%s,%s,\"%s\"", sdomain, stype, sproto);
  220. } else {
  221. STORE_COMMON(res, "socket", "%s,%s,\"%d\"", sdomain, stype, protocol);
  222. }
  223. return res;
  224. }
  225. #undef close
  226. int __ast_fdleak_close(int fd)
  227. {
  228. int res = close(fd);
  229. if (!res && fd > -1 && fd < ARRAY_LEN(fdleaks)) {
  230. fdleaks[fd].isopen = 0;
  231. }
  232. return res;
  233. }
  234. #undef fopen
  235. FILE *__ast_fdleak_fopen(const char *path, const char *mode, const char *file, int line, const char *func)
  236. {
  237. FILE *res = fopen(path, mode);
  238. int fd;
  239. if (!res) {
  240. return res;
  241. }
  242. fd = fileno(res);
  243. if (fd > -1 && fd < ARRAY_LEN(fdleaks)) {
  244. STORE_COMMON(fd, "fopen", "\"%s\",\"%s\"", path, mode);
  245. }
  246. return res;
  247. }
  248. #undef fclose
  249. int __ast_fdleak_fclose(FILE *ptr)
  250. {
  251. int fd, res;
  252. if (!ptr) {
  253. errno = EINVAL;
  254. return -1;
  255. }
  256. fd = fileno(ptr);
  257. if ((res = fclose(ptr)) || fd < 0 || fd >= ARRAY_LEN(fdleaks)) {
  258. return res;
  259. }
  260. fdleaks[fd].isopen = 0;
  261. return res;
  262. }
  263. #undef dup2
  264. int __ast_fdleak_dup2(int oldfd, int newfd, const char *file, int line, const char *func)
  265. {
  266. int res = dup2(oldfd, newfd);
  267. if (res < 0 || res >= ARRAY_LEN(fdleaks)) {
  268. return res;
  269. }
  270. /* On success, newfd will be closed automatically if it was already
  271. * open. We don't need to mention anything about that, we're updating
  272. * the value anyway. */
  273. STORE_COMMON(res, "dup2", "%d,%d", oldfd, newfd); /* res == newfd */
  274. return res;
  275. }
  276. #undef dup
  277. int __ast_fdleak_dup(int oldfd, const char *file, int line, const char *func)
  278. {
  279. int res = dup(oldfd);
  280. if (res < 0 || res >= ARRAY_LEN(fdleaks)) {
  281. return res;
  282. }
  283. STORE_COMMON(res, "dup2", "%d", oldfd);
  284. return res;
  285. }
  286. static char *handle_show_fd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  287. {
  288. int i;
  289. char line[24];
  290. struct rlimit rl;
  291. switch (cmd) {
  292. case CLI_INIT:
  293. e->command = "core show fd";
  294. e->usage =
  295. "Usage: core show fd\n"
  296. " List all file descriptors currently in use and where\n"
  297. " each was opened, and with what command.\n";
  298. return NULL;
  299. case CLI_GENERATE:
  300. return NULL;
  301. }
  302. getrlimit(RLIMIT_NOFILE, &rl);
  303. if (rl.rlim_cur == RLIM_INFINITY) {
  304. ast_copy_string(line, "unlimited", sizeof(line));
  305. } else {
  306. snprintf(line, sizeof(line), "%d", (int) rl.rlim_cur);
  307. }
  308. ast_cli(a->fd, "Current maxfiles: %s\n", line);
  309. for (i = 0; i < ARRAY_LEN(fdleaks); i++) {
  310. if (fdleaks[i].isopen) {
  311. struct ast_tm tm = {0};
  312. char datestring[256];
  313. ast_localtime(&fdleaks[i].now, &tm, NULL);
  314. ast_strftime(datestring, sizeof(datestring), "%F %T", &tm);
  315. snprintf(line, sizeof(line), "%d", fdleaks[i].line);
  316. ast_cli(a->fd, "%5d [%s] %22s:%-7.7s (%-25s): %s(%s)\n", i, datestring, fdleaks[i].file, line, fdleaks[i].function, fdleaks[i].callname, fdleaks[i].callargs);
  317. }
  318. }
  319. return CLI_SUCCESS;
  320. }
  321. static struct ast_cli_entry cli_show_fd = AST_CLI_DEFINE(handle_show_fd, "Show open file descriptors");
  322. static void fd_shutdown(void)
  323. {
  324. ast_cli_unregister(&cli_show_fd);
  325. }
  326. int ast_fd_init(void)
  327. {
  328. ast_register_cleanup(fd_shutdown);
  329. return ast_cli_register(&cli_show_fd);
  330. }
  331. #else /* !defined(DEBUG_FD_LEAKS) */
  332. int ast_fd_init(void)
  333. {
  334. return 0;
  335. }
  336. #endif /* defined(DEBUG_FD_LEAKS) */