From aec772a0cdec34611b9ccf391a9e94872676bd44 Mon Sep 17 00:00:00 2001 From: tilghman Date: Thu, 9 Apr 2009 05:12:07 +0000 Subject: Merged revisions 187302 via svnmerge from https://origsvn.digium.com/svn/asterisk/trunk ................ r187302 | tilghman | 2009-04-08 23:59:05 -0500 (Wed, 08 Apr 2009) | 14 lines Merged revisions 187300-187301 via svnmerge from https://origsvn.digium.com/svn/asterisk/branches/1.4 ........ r187300 | tilghman | 2009-04-08 23:31:38 -0500 (Wed, 08 Apr 2009) | 3 lines Add debugging mode for diagnosing file descriptor leaks. (Related to issue #14625) ........ r187301 | tilghman | 2009-04-08 23:32:40 -0500 (Wed, 08 Apr 2009) | 2 lines Oops, missed this file in the last commit. ........ ................ git-svn-id: http://svn.digium.com/svn/asterisk/branches/1.6.2@187303 f38db490-d61c-443f-a65b-d21fe96a405b --- agi/Makefile | 2 + build_tools/cflags.xml | 2 + include/asterisk.h | 29 ++++++ main/Makefile | 2 +- main/asterisk.c | 1 + main/astfd.c | 275 +++++++++++++++++++++++++++++++++++++++++++++++++ main/file.c | 9 +- utils/Makefile | 4 +- 8 files changed, 319 insertions(+), 5 deletions(-) create mode 100644 main/astfd.c diff --git a/agi/Makefile b/agi/Makefile index e8e57a9b9..89bf7bbec 100644 --- a/agi/Makefile +++ b/agi/Makefile @@ -25,6 +25,8 @@ endif include $(ASTTOPDIR)/Makefile.rules +ASTCFLAGS+=-DSTANDALONE + all: $(AGIS) strcompat.c: ../main/strcompat.c diff --git a/build_tools/cflags.xml b/build_tools/cflags.xml index 6279c50a9..4567360d4 100644 --- a/build_tools/cflags.xml +++ b/build_tools/cflags.xml @@ -8,6 +8,8 @@ yes + + diff --git a/include/asterisk.h b/include/asterisk.h index f159cf375..30a20ee60 100644 --- a/include/asterisk.h +++ b/include/asterisk.h @@ -43,7 +43,36 @@ #define setpriority __PLEASE_USE_ast_set_priority_INSTEAD_OF_setpriority__ #define sched_setscheduler __PLEASE_USE_ast_set_priority_INSTEAD_OF_sched_setscheduler__ +#if defined(DEBUG_FD_LEAKS) && !defined(STANDALONE) && !defined(STANDALONE_AEL) +/* These includes are all about ordering */ +#include +#include +#include +#include +#include +#include + +#define open(a,...) __ast_fdleak_open(__FILE__,__LINE__,__PRETTY_FUNCTION__, a, __VA_ARGS__) +#define pipe(a) __ast_fdleak_pipe(a, __FILE__,__LINE__,__PRETTY_FUNCTION__) +#define socket(a,b,c) __ast_fdleak_socket(a, b, c, __FILE__,__LINE__,__PRETTY_FUNCTION__) +#define close(a) __ast_fdleak_close(a) +#define fopen(a,b) __ast_fdleak_fopen(a, b, __FILE__,__LINE__,__PRETTY_FUNCTION__) +#define fclose(a) __ast_fdleak_fclose(a) +#define dup2(a,b) __ast_fdleak_dup2(a, b, __FILE__,__LINE__,__PRETTY_FUNCTION__) +#define dup(a) __ast_fdleak_dup(a, __FILE__,__LINE__,__PRETTY_FUNCTION__) + +int __ast_fdleak_open(const char *file, int line, const char *func, const char *path, int flags, ...); +int __ast_fdleak_pipe(int *fds, const char *file, int line, const char *func); +int __ast_fdleak_socket(int domain, int type, int protocol, const char *file, int line, const char *func); +int __ast_fdleak_close(int fd); +FILE *__ast_fdleak_fopen(const char *path, const char *mode, const char *file, int line, const char *func); +int __ast_fdleak_fclose(FILE *ptr); +int __ast_fdleak_dup2(int oldfd, int newfd, const char *file, int line, const char *func); +int __ast_fdleak_dup(int oldfd, const char *file, int line, const char *func); +#endif + int ast_set_priority(int); /*!< Provided by asterisk.c */ +int ast_fd_init(void); /*!< Provided by astfd.c */ /*! * \brief Register a function to be executed before Asterisk exits. diff --git a/main/Makefile b/main/Makefile index 3030cfd0b..6495f478e 100644 --- a/main/Makefile +++ b/main/Makefile @@ -22,7 +22,7 @@ OBJS= tcptls.o io.o sched.o logger.o frame.o loader.o config.o channel.o \ ulaw.o alaw.o callerid.o fskmodem.o image.o app.o \ cdr.o tdd.o acl.o rtp.o udptl.o manager.o asterisk.o \ dsp.o chanvars.o indications.o autoservice.o db.o privacy.o \ - astmm.o enum.o srv.o dns.o aescrypt.o aestab.o aeskey.o \ + astmm.o astfd.o enum.o srv.o dns.o aescrypt.o aestab.o aeskey.o \ utils.o plc.o jitterbuf.o dnsmgr.o devicestate.o \ netsock.o slinfactory.o ast_expr2.o ast_expr2f.o \ cryptostub.o sha1.o http.o fixedjitterbuf.o abstract_jb.o \ diff --git a/main/asterisk.c b/main/asterisk.c index fdef5e156..e2ecac913 100644 --- a/main/asterisk.c +++ b/main/asterisk.c @@ -3136,6 +3136,7 @@ int main(int argc, char *argv[]) ast_utils_init(); tdd_init(); ast_tps_init(); + ast_fd_init(); if (getenv("HOME")) snprintf(filename, sizeof(filename), "%s/.asterisk_history", getenv("HOME")); diff --git a/main/astfd.c b/main/astfd.c new file mode 100644 index 000000000..b0871b6bc --- /dev/null +++ b/main/astfd.c @@ -0,0 +1,275 @@ +/* + * Asterisk -- An open source telephony toolkit. + * + * Copyright (C) 2009, Digium, Inc. + * + * Tilghman Lesher + * + * See http://www.asterisk.org for more information about + * the Asterisk project. Please do not directly contact + * any of the maintainers of this project for assistance; + * the project provides a web site, mailing lists and IRC + * channels for your use. + * + * This program is free software, distributed under the terms of + * the GNU General Public License Version 2. See the LICENSE file + * at the top of the source tree. + */ + +/*! \file + * + * \brief Debugging routines for file descriptor leaks + * + * \author Tilghman Lesher + */ + +#include "asterisk.h" + +#ifdef DEBUG_FD_LEAKS + +ASTERISK_FILE_VERSION(__FILE__, "$Revision$") + +#include +#include +#include +#include +#include +#include + +#include "asterisk/cli.h" +#include "asterisk/logger.h" +#include "asterisk/options.h" +#include "asterisk/lock.h" +#include "asterisk/strings.h" +#include "asterisk/unaligned.h" + +static struct fdleaks { + char file[40]; + int line; + char function[25]; + char callname[10]; + char callargs[60]; + unsigned int isopen:1; +} fdleaks[1024] = { { "", }, }; + +#define COPY(dst, src) \ + do { \ + int dlen = sizeof(dst), slen = strlen(src); \ + if (slen + 1 > dlen) { \ + char *slash = strrchr(src, '/'); \ + if (slash) { \ + ast_copy_string(dst, slash + 1, dlen); \ + } else { \ + ast_copy_string(dst, src + slen - dlen + 1, dlen); \ + } \ + } else { \ + ast_copy_string(dst, src, dlen); \ + } \ + } while (0) + +#define STORE_COMMON(offset, name, ...) \ + COPY(fdleaks[offset].file, file); \ + fdleaks[offset].line = line; \ + COPY(fdleaks[offset].function, func); \ + strcpy(fdleaks[offset].callname, name); \ + snprintf(fdleaks[offset].callargs, sizeof(fdleaks[offset].callargs), __VA_ARGS__); \ + fdleaks[offset].isopen = 1; + +#undef open +int __ast_fdleak_open(const char *file, int line, const char *func, const char *path, int flags, ...) +{ + int res; + va_list ap; + int mode; + + if (flags & O_CREAT) { + va_start(ap, flags); + mode = va_arg(ap, int); + va_end(ap); + res = open(path, flags, mode); + if (res > -1 && res < (sizeof(fdleaks) / sizeof(fdleaks[0]))) { + char sflags[80]; + snprintf(sflags, sizeof(sflags), "O_CREAT%s%s%s%s%s%s%s%s", + flags & O_APPEND ? "|O_APPEND" : "", + flags & O_EXCL ? "|O_EXCL" : "", + flags & O_NONBLOCK ? "|O_NONBLOCK" : "", + flags & O_TRUNC ? "|O_TRUNC" : "", + flags & O_RDWR ? "|O_RDWR" : "", + flags & O_RDONLY ? "|O_RDONLY" : "", + flags & O_WRONLY ? "|O_WRONLY" : "", + ""); + flags |= ~(O_CREAT | O_APPEND | O_EXCL | O_NONBLOCK | O_TRUNC | O_RDWR | O_RDONLY | O_WRONLY); + if (flags) { + STORE_COMMON(res, "open", "\"%s\",%s|%d,%04o", path, sflags, flags, mode); + } else { + STORE_COMMON(res, "open", "\"%s\",%s,%04o", path, sflags, mode); + } + } + } else { + res = open(path, flags); + if (res > -1 && res < (sizeof(fdleaks) / sizeof(fdleaks[0]))) { + STORE_COMMON(res, "open", "\"%s\",%d", path, flags); + } + } + return res; +} + +#undef pipe +int __ast_fdleak_pipe(int *fds, const char *file, int line, const char *func) +{ + int i, res = pipe(fds); + if (res) { + return res; + } + for (i = 0; i < 2; i++) { + STORE_COMMON(fds[i], "pipe", "{%d,%d}", fds[0], fds[1]); + } + return 0; +} + +#undef socket +int __ast_fdleak_socket(int domain, int type, int protocol, const char *file, int line, const char *func) +{ + char sdomain[20], stype[20], *sproto; + struct protoent *pe; + int res = socket(domain, type, protocol); + if (res < 0 || res > 1023) { + return res; + } + + pe = getprotobynumber(protocol); + sproto = pe->p_name; + + if (domain == PF_UNIX) { + ast_copy_string(sdomain, "PF_UNIX", sizeof(sdomain)); + } else if (domain == PF_INET) { + ast_copy_string(sdomain, "PF_INET", sizeof(sdomain)); + } else { + snprintf(sdomain, sizeof(sdomain), "%d", domain); + } + + if (type == SOCK_DGRAM) { + ast_copy_string(stype, "SOCK_DGRAM", sizeof(stype)); + if (protocol == 0) { + sproto = "udp"; + } + } else if (type == SOCK_STREAM) { + ast_copy_string(stype, "SOCK_STREAM", sizeof(stype)); + if (protocol == 0) { + sproto = "tcp"; + } + } else { + snprintf(stype, sizeof(stype), "%d", type); + } + + STORE_COMMON(res, "socket", "%s,%s,\"%s\"", sdomain, stype, sproto); + return res; +} + +#undef close +int __ast_fdleak_close(int fd) +{ + int res = close(fd); + if (!res && fd > -1 && fd < 1024) { + fdleaks[fd].isopen = 0; + } + return res; +} + +#undef fopen +FILE *__ast_fdleak_fopen(const char *path, const char *mode, const char *file, int line, const char *func) +{ + FILE *res = fopen(path, mode); + int fd; + if (!res) { + return res; + } + fd = fileno(res); + STORE_COMMON(fd, "fopen", "\"%s\",\"%s\"", path, mode); + return res; +} + +#undef fclose +int __ast_fdleak_fclose(FILE *ptr) +{ + int fd, res; + if (!ptr) { + return fclose(ptr); + } + + fd = fileno(ptr); + if ((res = fclose(ptr)) || fd < 0 || fd > 1023) { + return res; + } + fdleaks[fd].isopen = 0; + return res; +} + +#undef dup2 +int __ast_fdleak_dup2(int oldfd, int newfd, const char *file, int line, const char *func) +{ + int res = dup2(oldfd, newfd); + if (res < 0 || res > 1023) { + return res; + } + STORE_COMMON(res, "dup2", "%d,%d", oldfd, newfd); + return res; +} + +#undef dup +int __ast_fdleak_dup(int oldfd, const char *file, int line, const char *func) +{ + int res = dup(oldfd); + if (res < 0 || res > 1023) { + return res; + } + STORE_COMMON(res, "dup2", "%d", oldfd); + return res; +} + +static char *handle_show_fd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) +{ + int i; + char line[24]; + struct rlimit rl; + switch (cmd) { + case CLI_INIT: + e->command = "core show fd"; + e->usage = + "Usage: core show fd\n" + " List all file descriptors currently in use and where\n" + " each was opened, and with what command.\n"; + return NULL; + case CLI_GENERATE: + return NULL; + } + getrlimit(RLIMIT_FSIZE, &rl); + if (rl.rlim_cur == RLIM_INFINITY || rl.rlim_max == RLIM_INFINITY) { + ast_copy_string(line, "unlimited", sizeof(line)); + } else { + snprintf(line, sizeof(line), "%d/%d", (int) rl.rlim_cur, (int) rl.rlim_max); + } + ast_cli(a->fd, "Current maxfiles: %s\n", line); + for (i = 0; i < 1024; i++) { + if (fdleaks[i].isopen) { + snprintf(line, sizeof(line), "%d", fdleaks[i].line); + 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); + } + } + return CLI_SUCCESS; +} + +static struct ast_cli_entry cli_show_fd = AST_CLI_DEFINE(handle_show_fd, "Show open file descriptors"); + +int ast_fd_init(void) +{ + return ast_cli_register(&cli_show_fd); +} + +#else /* !defined(DEBUG_FD_LEAKS) */ +int ast_fd_init(void) +{ + return 0; +} +#endif /* defined(DEBUG_FD_LEAKS) */ + diff --git a/main/file.c b/main/file.c index 05f6ea6f6..ac47b2b51 100644 --- a/main/file.c +++ b/main/file.c @@ -314,8 +314,10 @@ static void filestream_destructor(void *arg) free(f->filename); if (f->realfilename) free(f->realfilename); - if (f->fmt->close) - f->fmt->close(f); + if (f->fmt->close) { + void (*closefn)(struct ast_filestream *) = f->fmt->close; + closefn(f); + } if (f->f) fclose(f->f); if (f->vfs) @@ -353,8 +355,9 @@ static int fn_wrapper(struct ast_filestream *s, const char *comment, enum wrap_f { struct ast_format *f = s->fmt; int ret = -1; + int (*openfn)(struct ast_filestream *s); - if (mode == WRAP_OPEN && f->open && f->open(s)) + if (mode == WRAP_OPEN && (openfn = f->open) && openfn(s)) ast_log(LOG_WARNING, "Unable to open format %s\n", f->name); else if (mode == WRAP_REWRITE && f->rewrite && f->rewrite(s, comment)) ast_log(LOG_WARNING, "Unable to rewrite format %s\n", f->name); diff --git a/utils/Makefile b/utils/Makefile index 70fe13c1b..2f5fc027a 100644 --- a/utils/Makefile +++ b/utils/Makefile @@ -95,7 +95,7 @@ md5.o: ASTCFLAGS+=-DSTANDALONE astman: astman.o md5.o astman: LIBS+=$(NEWT_LIB) -astman.o: ASTCFLAGS+=-DSTANDALONE +astman.o: ASTCFLAGS+=-DNO_MALLOC_DEBUG -DSTANDALONE stereorize: stereorize.o frame.o stereorize: LIBS+=-lm @@ -210,6 +210,8 @@ check_expr2: $(ASTTOPDIR)/main/ast_expr2f.c $(ASTTOPDIR)/main/ast_expr2.c $(ASTT rm ast_expr2z.o ast_expr2fz.o ./check_expr2 expr2.testinput +smsq.o: ASTCFLAGS+=-DSTANDALONE + smsq: smsq.o strcompat.o smsq: LIBS+=$(POPT_LIB) -- cgit v1.2.3