Fri Sep 11 13:44:49 2009

Asterisk developer's documentation


astfd.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2009, Digium, Inc.
00005  *
00006  * Tilghman Lesher <tlesher@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Debugging routines for file descriptor leaks
00022  *
00023  * \author Tilghman Lesher <tlesher@digium.com>
00024  */
00025 
00026 #include "asterisk.h"
00027 
00028 #ifdef DEBUG_FD_LEAKS
00029 
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 187301 $")
00031 
00032 #include <stdio.h>
00033 #include <string.h>
00034 #include <stddef.h>
00035 #include <time.h>
00036 #include <sys/time.h>
00037 #include <sys/resource.h>
00038 
00039 #include "asterisk/cli.h"
00040 #include "asterisk/logger.h"
00041 #include "asterisk/options.h"
00042 #include "asterisk/lock.h"
00043 #include "asterisk/strings.h"
00044 #include "asterisk/unaligned.h"
00045 
00046 static struct fdleaks {
00047    char file[40];
00048    int line;
00049    char function[25];
00050    char callname[10];
00051    char callargs[60];
00052    unsigned int isopen:1;
00053 } fdleaks[1024] = { { "", }, };
00054 
00055 #define  COPY(dst, src)                                             \
00056    do {                                                           \
00057       int dlen = sizeof(dst), slen = strlen(src);                \
00058       if (slen + 1 > dlen) {                                     \
00059          char *slash = strrchr(src, '/');                       \
00060          if (slash) {                                           \
00061             ast_copy_string(dst, slash + 1, dlen);             \
00062          } else {                                               \
00063             ast_copy_string(dst, src + slen - dlen + 1, dlen); \
00064          }                                                      \
00065       } else {                                                   \
00066          ast_copy_string(dst, src, dlen);                       \
00067       }                                                          \
00068    } while (0)
00069 
00070 #define STORE_COMMON(offset, name, ...)     \
00071    COPY(fdleaks[offset].file, file);       \
00072    fdleaks[offset].line = line;            \
00073    COPY(fdleaks[offset].function, func);   \
00074    strcpy(fdleaks[offset].callname, name); \
00075    snprintf(fdleaks[offset].callargs, sizeof(fdleaks[offset].callargs), __VA_ARGS__); \
00076    fdleaks[offset].isopen = 1;
00077 
00078 #undef open
00079 int __ast_fdleak_open(const char *file, int line, const char *func, const char *path, int flags, ...)
00080 {
00081    int res;
00082    va_list ap;
00083    int mode;
00084 
00085    if (flags & O_CREAT) {
00086       va_start(ap, flags);
00087       mode = va_arg(ap, int);
00088       va_end(ap);
00089       res = open(path, flags, mode);
00090       if (res > -1 && res < (sizeof(fdleaks) / sizeof(fdleaks[0]))) {
00091          char sflags[80];
00092          snprintf(sflags, sizeof(sflags), "O_CREAT%s%s%s%s%s%s%s%s",
00093             flags & O_APPEND ? "|O_APPEND" : "",
00094             flags & O_EXCL ? "|O_EXCL" : "",
00095             flags & O_NONBLOCK ? "|O_NONBLOCK" : "",
00096             flags & O_TRUNC ? "|O_TRUNC" : "",
00097             flags & O_RDWR ? "|O_RDWR" : "",
00098             flags & O_RDONLY ? "|O_RDONLY" : "",
00099             flags & O_WRONLY ? "|O_WRONLY" : "",
00100             "");
00101          flags |= ~(O_CREAT | O_APPEND | O_EXCL | O_NONBLOCK | O_TRUNC | O_RDWR | O_RDONLY | O_WRONLY);
00102          if (flags) {
00103             STORE_COMMON(res, "open", "\"%s\",%s|%d,%04o", path, sflags, flags, mode);
00104          } else {
00105             STORE_COMMON(res, "open", "\"%s\",%s,%04o", path, sflags, mode);
00106          }
00107       }
00108    } else {
00109       res = open(path, flags);
00110       if (res > -1 && res < (sizeof(fdleaks) / sizeof(fdleaks[0]))) {
00111          STORE_COMMON(res, "open", "\"%s\",%d", path, flags);
00112       }
00113    }
00114    return res;
00115 }
00116 
00117 #undef pipe
00118 int __ast_fdleak_pipe(int *fds, const char *file, int line, const char *func)
00119 {
00120    int i, res = pipe(fds);
00121    if (res) {
00122       return res;
00123    }
00124    for (i = 0; i < 2; i++) {
00125       STORE_COMMON(fds[i], "pipe", "{%d,%d}", fds[0], fds[1]);
00126    }
00127    return 0;
00128 }
00129 
00130 #undef socket
00131 int __ast_fdleak_socket(int domain, int type, int protocol, const char *file, int line, const char *func)
00132 {
00133    char sdomain[20], stype[20], *sproto;
00134    struct protoent *pe;
00135    int res = socket(domain, type, protocol);
00136    if (res < 0 || res > 1023) {
00137       return res;
00138    }
00139 
00140    pe = getprotobynumber(protocol);
00141    sproto = pe->p_name;
00142 
00143    if (domain == PF_UNIX) {
00144       ast_copy_string(sdomain, "PF_UNIX", sizeof(sdomain));
00145    } else if (domain == PF_INET) {
00146       ast_copy_string(sdomain, "PF_INET", sizeof(sdomain));
00147    } else {
00148       snprintf(sdomain, sizeof(sdomain), "%d", domain);
00149    }
00150 
00151    if (type == SOCK_DGRAM) {
00152       ast_copy_string(stype, "SOCK_DGRAM", sizeof(stype));
00153       if (protocol == 0) {
00154          sproto = "udp";
00155       }
00156    } else if (type == SOCK_STREAM) {
00157       ast_copy_string(stype, "SOCK_STREAM", sizeof(stype));
00158       if (protocol == 0) {
00159          sproto = "tcp";
00160       }
00161    } else {
00162       snprintf(stype, sizeof(stype), "%d", type);
00163    }
00164 
00165    STORE_COMMON(res, "socket", "%s,%s,\"%s\"", sdomain, stype, sproto);
00166    return res;
00167 }
00168 
00169 #undef close
00170 int __ast_fdleak_close(int fd)
00171 {
00172    int res = close(fd);
00173    if (!res && fd > -1 && fd < 1024) {
00174       fdleaks[fd].isopen = 0;
00175    }
00176    return res;
00177 }
00178 
00179 #undef fopen
00180 FILE *__ast_fdleak_fopen(const char *path, const char *mode, const char *file, int line, const char *func)
00181 {
00182    FILE *res = fopen(path, mode);
00183    int fd;
00184    if (!res) {
00185       return res;
00186    }
00187    fd = fileno(res);
00188    STORE_COMMON(fd, "fopen", "\"%s\",\"%s\"", path, mode);
00189    return res;
00190 }
00191 
00192 #undef fclose
00193 int __ast_fdleak_fclose(FILE *ptr)
00194 {
00195    int fd, res;
00196    if (!ptr) {
00197       return fclose(ptr);
00198    }
00199 
00200    fd = fileno(ptr);
00201    if ((res = fclose(ptr)) || fd < 0 || fd > 1023) {
00202       return res;
00203    }
00204    fdleaks[fd].isopen = 0;
00205    return res;
00206 }
00207 
00208 #undef dup2
00209 int __ast_fdleak_dup2(int oldfd, int newfd, const char *file, int line, const char *func)
00210 {
00211    int res = dup2(oldfd, newfd);
00212    if (res < 0 || res > 1023) {
00213       return res;
00214    }
00215    STORE_COMMON(res, "dup2", "%d,%d", oldfd, newfd);
00216    return res;
00217 }
00218 
00219 #undef dup
00220 int __ast_fdleak_dup(int oldfd, const char *file, int line, const char *func)
00221 {
00222    int res = dup(oldfd);
00223    if (res < 0 || res > 1023) {
00224       return res;
00225    }
00226    STORE_COMMON(res, "dup2", "%d", oldfd);
00227    return res;
00228 }
00229 
00230 static int handle_show_fd(int fd, int argc, char *argv[])
00231 {
00232    int i;
00233    char line[24];
00234    struct rlimit rl;
00235    getrlimit(RLIMIT_FSIZE, &rl);
00236    if (rl.rlim_cur == RLIM_INFINITY || rl.rlim_max == RLIM_INFINITY) {
00237       ast_copy_string(line, "unlimited", sizeof(line));
00238    } else {
00239       snprintf(line, sizeof(line), "%d/%d", (int) rl.rlim_cur, (int) rl.rlim_max);
00240    }
00241    ast_cli(fd, "Current maxfiles: %s\n", line);
00242    for (i = 0; i < 1024; i++) {
00243       if (fdleaks[i].isopen) {
00244          snprintf(line, sizeof(line), "%d", fdleaks[i].line);
00245          ast_cli(fd, "%5d %15s:%-7.7s (%-25s): %s(%s)\n", i, fdleaks[i].file, line, fdleaks[i].function, fdleaks[i].callname, fdleaks[i].callargs);
00246       }
00247    }
00248    return RESULT_SUCCESS;
00249 }
00250 
00251 static struct ast_cli_entry cli_show_fd =
00252    { { "core", "show", "fd", NULL },
00253    handle_show_fd, "Show open file descriptors" };
00254 
00255 int ast_fd_init(void)
00256 {
00257    return ast_cli_register(&cli_show_fd);
00258 }
00259 
00260 #else  /* !defined(DEBUG_FD_LEAKS) */
00261 int ast_fd_init(void)
00262 {
00263    return 0;
00264 }
00265 #endif /* defined(DEBUG_FD_LEAKS) */
00266 

Generated on Fri Sep 11 13:44:49 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7