astfd.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <stddef.h>
  33. #include <time.h>
  34. #include <sys/time.h>
  35. #include <sys/resource.h>
  36. #include "asterisk/cli.h"
  37. #include "asterisk/logger.h"
  38. #include "asterisk/options.h"
  39. #include "asterisk/lock.h"
  40. #include "asterisk/strings.h"
  41. #include "asterisk/unaligned.h"
  42. static struct fdleaks {
  43. const char *callname;
  44. int line;
  45. unsigned int isopen:1;
  46. char file[40];
  47. char function[25];
  48. char callargs[100];
  49. } fdleaks[1024] = { { "", }, };
  50. /* COPY does ast_copy_string(dst, src, sizeof(dst)), except:
  51. * - if it doesn't fit, it copies the value after the slash
  52. * (possibly truncated)
  53. * - if there is no slash, it copies the value with the head
  54. * truncated */
  55. #define COPY(dst, src) \
  56. do { \
  57. int dlen = sizeof(dst), slen = strlen(src); \
  58. if (slen + 1 > dlen) { \
  59. const char *slash = strrchr(src, '/'); \
  60. if (slash) { \
  61. ast_copy_string(dst, slash + 1, dlen); \
  62. } else { \
  63. ast_copy_string(dst, src + slen - dlen + 1, dlen); \
  64. } \
  65. } else { \
  66. ast_copy_string(dst, src, dlen); \
  67. } \
  68. } while (0)
  69. #define STORE_COMMON(offset, name, ...) \
  70. do { \
  71. struct fdleaks *tmp = &fdleaks[offset]; \
  72. COPY(tmp->file, file); \
  73. tmp->line = line; \
  74. COPY(tmp->function, func); \
  75. tmp->callname = name; \
  76. snprintf(tmp->callargs, sizeof(tmp->callargs), __VA_ARGS__); \
  77. tmp->isopen = 1; \
  78. } while (0)
  79. #undef open
  80. int __ast_fdleak_open(const char *file, int line, const char *func, const char *path, int flags, ...)
  81. {
  82. int res;
  83. va_list ap;
  84. int mode;
  85. if (flags & O_CREAT) {
  86. va_start(ap, flags);
  87. mode = va_arg(ap, int);
  88. va_end(ap);
  89. res = open(path, flags, mode);
  90. if (res > -1 && res < ARRAY_LEN(fdleaks)) {
  91. char sflags[80];
  92. snprintf(sflags, sizeof(sflags), "O_CREAT%s%s%s%s%s%s%s%s",
  93. flags & O_APPEND ? "|O_APPEND" : "",
  94. flags & O_EXCL ? "|O_EXCL" : "",
  95. flags & O_NONBLOCK ? "|O_NONBLOCK" : "",
  96. flags & O_TRUNC ? "|O_TRUNC" : "",
  97. flags & O_RDWR ? "|O_RDWR" : "",
  98. #if O_RDONLY == 0
  99. !(flags & (O_WRONLY | O_RDWR)) ? "|O_RDONLY" : "",
  100. #else
  101. flags & O_RDONLY ? "|O_RDONLY" : "",
  102. #endif
  103. flags & O_WRONLY ? "|O_WRONLY" : "",
  104. "");
  105. flags &= ~(O_CREAT | O_APPEND | O_EXCL | O_NONBLOCK | O_TRUNC | O_RDWR | O_RDONLY | O_WRONLY);
  106. if (flags) {
  107. STORE_COMMON(res, "open", "\"%s\",%s|%d,%04o", path, sflags, flags, mode);
  108. } else {
  109. STORE_COMMON(res, "open", "\"%s\",%s,%04o", path, sflags, mode);
  110. }
  111. }
  112. } else {
  113. res = open(path, flags);
  114. if (res > -1 && res < ARRAY_LEN(fdleaks)) {
  115. STORE_COMMON(res, "open", "\"%s\",%d", path, flags);
  116. }
  117. }
  118. return res;
  119. }
  120. #undef pipe
  121. int __ast_fdleak_pipe(int *fds, const char *file, int line, const char *func)
  122. {
  123. int i, res = pipe(fds);
  124. if (res) {
  125. return res;
  126. }
  127. for (i = 0; i < 2; i++) {
  128. if (fds[i] > -1 && fds[i] < ARRAY_LEN(fdleaks)) {
  129. STORE_COMMON(fds[i], "pipe", "{%d,%d}", fds[0], fds[1]);
  130. }
  131. }
  132. return 0;
  133. }
  134. #undef socket
  135. int __ast_fdleak_socket(int domain, int type, int protocol, const char *file, int line, const char *func)
  136. {
  137. char sdomain[20], stype[20], *sproto = NULL;
  138. struct protoent *pe;
  139. int res = socket(domain, type, protocol);
  140. if (res < 0 || res >= ARRAY_LEN(fdleaks)) {
  141. return res;
  142. }
  143. if ((pe = getprotobynumber(protocol))) {
  144. sproto = pe->p_name;
  145. }
  146. if (domain == PF_UNIX) {
  147. ast_copy_string(sdomain, "PF_UNIX", sizeof(sdomain));
  148. } else if (domain == PF_INET) {
  149. ast_copy_string(sdomain, "PF_INET", sizeof(sdomain));
  150. } else {
  151. snprintf(sdomain, sizeof(sdomain), "%d", domain);
  152. }
  153. if (type == SOCK_DGRAM) {
  154. ast_copy_string(stype, "SOCK_DGRAM", sizeof(stype));
  155. if (protocol == 0) {
  156. sproto = "udp";
  157. }
  158. } else if (type == SOCK_STREAM) {
  159. ast_copy_string(stype, "SOCK_STREAM", sizeof(stype));
  160. if (protocol == 0) {
  161. sproto = "tcp";
  162. }
  163. } else {
  164. snprintf(stype, sizeof(stype), "%d", type);
  165. }
  166. if (sproto) {
  167. STORE_COMMON(res, "socket", "%s,%s,\"%s\"", sdomain, stype, sproto);
  168. } else {
  169. STORE_COMMON(res, "socket", "%s,%s,\"%d\"", sdomain, stype, protocol);
  170. }
  171. return res;
  172. }
  173. #undef close
  174. int __ast_fdleak_close(int fd)
  175. {
  176. int res = close(fd);
  177. if (!res && fd > -1 && fd < ARRAY_LEN(fdleaks)) {
  178. fdleaks[fd].isopen = 0;
  179. }
  180. return res;
  181. }
  182. #undef fopen
  183. FILE *__ast_fdleak_fopen(const char *path, const char *mode, const char *file, int line, const char *func)
  184. {
  185. FILE *res = fopen(path, mode);
  186. int fd;
  187. if (!res) {
  188. return res;
  189. }
  190. fd = fileno(res);
  191. if (fd > -1 && fd < ARRAY_LEN(fdleaks)) {
  192. STORE_COMMON(fd, "fopen", "\"%s\",\"%s\"", path, mode);
  193. }
  194. return res;
  195. }
  196. #undef fclose
  197. int __ast_fdleak_fclose(FILE *ptr)
  198. {
  199. int fd, res;
  200. if (!ptr) {
  201. return fclose(ptr);
  202. }
  203. fd = fileno(ptr);
  204. if ((res = fclose(ptr)) || fd < 0 || fd >= ARRAY_LEN(fdleaks)) {
  205. return res;
  206. }
  207. fdleaks[fd].isopen = 0;
  208. return res;
  209. }
  210. #undef dup2
  211. int __ast_fdleak_dup2(int oldfd, int newfd, const char *file, int line, const char *func)
  212. {
  213. int res = dup2(oldfd, newfd);
  214. if (res < 0 || res >= ARRAY_LEN(fdleaks)) {
  215. return res;
  216. }
  217. /* On success, newfd will be closed automatically if it was already
  218. * open. We don't need to mention anything about that, we're updating
  219. * the value anway. */
  220. STORE_COMMON(res, "dup2", "%d,%d", oldfd, newfd); /* res == newfd */
  221. return res;
  222. }
  223. #undef dup
  224. int __ast_fdleak_dup(int oldfd, const char *file, int line, const char *func)
  225. {
  226. int res = dup(oldfd);
  227. if (res < 0 || res >= ARRAY_LEN(fdleaks)) {
  228. return res;
  229. }
  230. STORE_COMMON(res, "dup2", "%d", oldfd);
  231. return res;
  232. }
  233. static char *handle_show_fd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  234. {
  235. int i;
  236. char line[24];
  237. struct rlimit rl;
  238. switch (cmd) {
  239. case CLI_INIT:
  240. e->command = "core show fd";
  241. e->usage =
  242. "Usage: core show fd\n"
  243. " List all file descriptors currently in use and where\n"
  244. " each was opened, and with what command.\n";
  245. return NULL;
  246. case CLI_GENERATE:
  247. return NULL;
  248. }
  249. getrlimit(RLIMIT_NOFILE, &rl);
  250. if (rl.rlim_cur == RLIM_INFINITY || rl.rlim_max == RLIM_INFINITY) {
  251. ast_copy_string(line, "unlimited", sizeof(line));
  252. } else {
  253. snprintf(line, sizeof(line), "%d/%d", (int) rl.rlim_cur, (int) rl.rlim_max);
  254. }
  255. ast_cli(a->fd, "Current maxfiles: %s\n", line);
  256. for (i = 0; i < ARRAY_LEN(fdleaks); i++) {
  257. if (fdleaks[i].isopen) {
  258. snprintf(line, sizeof(line), "%d", fdleaks[i].line);
  259. ast_cli(a->fd, "%5d %15s:%-7.7s (%-25s): %s(%s)\n", i, fdleaks[i].file, line, fdleaks[i].function, fdleaks[i].callname, fdleaks[i].callargs);
  260. }
  261. }
  262. return CLI_SUCCESS;
  263. }
  264. static struct ast_cli_entry cli_show_fd = AST_CLI_DEFINE(handle_show_fd, "Show open file descriptors");
  265. static void fd_shutdown(void)
  266. {
  267. ast_cli_unregister(&cli_show_fd);
  268. }
  269. int ast_fd_init(void)
  270. {
  271. ast_register_cleanup(fd_shutdown);
  272. return ast_cli_register(&cli_show_fd);
  273. }
  274. #else /* !defined(DEBUG_FD_LEAKS) */
  275. int ast_fd_init(void)
  276. {
  277. return 0;
  278. }
  279. #endif /* defined(DEBUG_FD_LEAKS) */