From a8dc553099ee1a14a3cd7faa257c6a5e6f3bf5b4 Mon Sep 17 00:00:00 2001 From: Tilghman Lesher Date: Thu, 9 Apr 2009 04:31:38 +0000 Subject: [PATCH] Add debugging mode for diagnosing file descriptor leaks. (Related to issue #14625) git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/1.4@187300 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- agi/Makefile | 2 ++ build_tools/cflags.xml | 2 ++ include/asterisk.h | 29 +++++++++++++++++++++++++++++ main/Makefile | 2 +- main/asterisk.c | 1 + main/file.c | 9 ++++++--- utils/Makefile | 2 +- 7 files changed, 42 insertions(+), 5 deletions(-) diff --git a/agi/Makefile b/agi/Makefile index 7a13d22415..67eeb3656b 100644 --- a/agi/Makefile +++ b/agi/Makefile @@ -21,6 +21,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 b027cb017d..21f316fbf3 100644 --- a/build_tools/cflags.xml +++ b/build_tools/cflags.xml @@ -5,6 +5,8 @@ + + diff --git a/include/asterisk.h b/include/asterisk.h index 20f5aa9d4f..a827a23f06 100644 --- a/include/asterisk.h +++ b/include/asterisk.h @@ -35,6 +35,34 @@ #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 + /* provided in asterisk.c */ extern char ast_config_AST_CONFIG_DIR[PATH_MAX]; extern char ast_config_AST_CONFIG_FILE[PATH_MAX]; @@ -73,6 +101,7 @@ int dnsmgr_reload(void); /*!< Provided by dnsmgr.c */ void threadstorage_init(void); /*!< Provided by threadstorage.c */ int astobj2_init(void); /*! Provided by astobj2.c */ void ast_autoservice_init(void); /*!< Provided by autoservice.c */ +int ast_fd_init(void); /*!< Provided by astfd.c */ /* Many headers need 'ast_channel' to be defined */ struct ast_channel; diff --git a/main/Makefile b/main/Makefile index 38d49f34c2..1bd3cd4b1b 100644 --- a/main/Makefile +++ b/main/Makefile @@ -22,7 +22,7 @@ OBJS= 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 eb79e71a18..e0fbcc303e 100644 --- a/main/asterisk.c +++ b/main/asterisk.c @@ -2732,6 +2732,7 @@ int main(int argc, char *argv[]) ast_builtins_init(); ast_utils_init(); tdd_init(); + ast_fd_init(); if (getenv("HOME")) snprintf(filename, sizeof(filename), "%s/.asterisk_history", getenv("HOME")); diff --git a/main/file.c b/main/file.c index faca5a9be9..8e83880141 100644 --- a/main/file.c +++ b/main/file.c @@ -332,8 +332,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) @@ -371,8 +373,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 6886f9859e..ac0619d7ac 100644 --- a/utils/Makefile +++ b/utils/Makefile @@ -75,7 +75,7 @@ md5.c: ../main/md5.c astman: astman.o md5.o astman: LIBS+=$(NEWT_LIB) -astman.o: ASTCFLAGS+=-DNO_MALLOC_DEBUG +astman.o: ASTCFLAGS+=-DNO_MALLOC_DEBUG -DSTANDALONE stereorize: stereorize.o frame.o stereorize: LIBS+=-lm