astfd.c 7.4 KB

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