Wed May 16 06:33:22 2012

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: 363215 $")
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 #if O_RDONLY == 0
00099             !(flags & (O_WRONLY | O_RDWR)) ? "|O_RDONLY" : "",
00100 #else
00101             flags & O_RDONLY ? "|O_RDONLY" : "",
00102 #endif
00103             flags & O_WRONLY ? "|O_WRONLY" : "",
00104             "");
00105          flags &= ~(O_CREAT | O_APPEND | O_EXCL | O_NONBLOCK | O_TRUNC | O_RDWR | O_RDONLY | O_WRONLY);
00106          if (flags) {
00107             STORE_COMMON(res, "open", "\"%s\",%s|%d,%04o", path, sflags, flags, mode);
00108          } else {
00109             STORE_COMMON(res, "open", "\"%s\",%s,%04o", path, sflags, mode);
00110          }
00111       }
00112    } else {
00113       res = open(path, flags);
00114       if (res > -1 && res < (sizeof(fdleaks) / sizeof(fdleaks[0]))) {
00115          STORE_COMMON(res, "open", "\"%s\",%d", path, flags);
00116       }
00117    }
00118    return res;
00119 }
00120 
00121 #undef pipe
00122 int __ast_fdleak_pipe(int *fds, const char *file, int line, const char *func)
00123 {
00124    int i, res = pipe(fds);
00125    if (res) {
00126       return res;
00127    }
00128    for (i = 0; i < 2; i++) {
00129       STORE_COMMON(fds[i], "pipe", "{%d,%d}", fds[0], fds[1]);
00130    }
00131    return 0;
00132 }
00133 
00134 #undef socket
00135 int __ast_fdleak_socket(int domain, int type, int protocol, const char *file, int line, const char *func)
00136 {
00137    char sdomain[20], stype[20], *sproto = NULL;
00138    struct protoent *pe;
00139    int res = socket(domain, type, protocol);
00140    if (res < 0 || res > 1023) {
00141       return res;
00142    }
00143 
00144    if ((pe = getprotobynumber(protocol))) {
00145       sproto = pe->p_name;
00146    }
00147 
00148    if (domain == PF_UNIX) {
00149       ast_copy_string(sdomain, "PF_UNIX", sizeof(sdomain));
00150    } else if (domain == PF_INET) {
00151       ast_copy_string(sdomain, "PF_INET", sizeof(sdomain));
00152    } else {
00153       snprintf(sdomain, sizeof(sdomain), "%d", domain);
00154    }
00155 
00156    if (type == SOCK_DGRAM) {
00157       ast_copy_string(stype, "SOCK_DGRAM", sizeof(stype));
00158       if (protocol == 0) {
00159          sproto = "udp";
00160       }
00161    } else if (type == SOCK_STREAM) {
00162       ast_copy_string(stype, "SOCK_STREAM", sizeof(stype));
00163       if (protocol == 0) {
00164          sproto = "tcp";
00165       }
00166    } else {
00167       snprintf(stype, sizeof(stype), "%d", type);
00168    }
00169 
00170    if (sproto) {
00171       STORE_COMMON(res, "socket", "%s,%s,\"%s\"", sdomain, stype, sproto);
00172    } else {
00173       STORE_COMMON(res, "socket", "%s,%s,\"%d\"", sdomain, stype, protocol);
00174    }
00175    return res;
00176 }
00177 
00178 #undef close
00179 int __ast_fdleak_close(int fd)
00180 {
00181    int res = close(fd);
00182    if (!res && fd > -1 && fd < 1024) {
00183       fdleaks[fd].isopen = 0;
00184    }
00185    return res;
00186 }
00187 
00188 #undef fopen
00189 FILE *__ast_fdleak_fopen(const char *path, const char *mode, const char *file, int line, const char *func)
00190 {
00191    FILE *res = fopen(path, mode);
00192    int fd;
00193    if (!res) {
00194       return res;
00195    }
00196    fd = fileno(res);
00197    STORE_COMMON(fd, "fopen", "\"%s\",\"%s\"", path, mode);
00198    return res;
00199 }
00200 
00201 #undef fclose
00202 int __ast_fdleak_fclose(FILE *ptr)
00203 {
00204    int fd, res;
00205    if (!ptr) {
00206       return fclose(ptr);
00207    }
00208 
00209    fd = fileno(ptr);
00210    if ((res = fclose(ptr)) || fd < 0 || fd > 1023) {
00211       return res;
00212    }
00213    fdleaks[fd].isopen = 0;
00214    return res;
00215 }
00216 
00217 #undef dup2
00218 int __ast_fdleak_dup2(int oldfd, int newfd, const char *file, int line, const char *func)
00219 {
00220    int res = dup2(oldfd, newfd);
00221    if (res < 0 || res > 1023) {
00222       return res;
00223    }
00224    STORE_COMMON(res, "dup2", "%d,%d", oldfd, newfd);
00225    return res;
00226 }
00227 
00228 #undef dup
00229 int __ast_fdleak_dup(int oldfd, const char *file, int line, const char *func)
00230 {
00231    int res = dup(oldfd);
00232    if (res < 0 || res > 1023) {
00233       return res;
00234    }
00235    STORE_COMMON(res, "dup2", "%d", oldfd);
00236    return res;
00237 }
00238 
00239 static char *handle_show_fd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00240 {
00241    int i;
00242    char line[24];
00243    struct rlimit rl;
00244    switch (cmd) {
00245    case CLI_INIT:
00246       e->command = "core show fd";
00247       e->usage =
00248          "Usage: core show fd\n"
00249          "       List all file descriptors currently in use and where\n"
00250          "       each was opened, and with what command.\n";
00251       return NULL;
00252    case CLI_GENERATE:
00253       return NULL;
00254    }
00255    getrlimit(RLIMIT_FSIZE, &rl);
00256    if (rl.rlim_cur == RLIM_INFINITY || rl.rlim_max == RLIM_INFINITY) {
00257       ast_copy_string(line, "unlimited", sizeof(line));
00258    } else {
00259       snprintf(line, sizeof(line), "%d/%d", (int) rl.rlim_cur, (int) rl.rlim_max);
00260    }
00261    ast_cli(a->fd, "Current maxfiles: %s\n", line);
00262    for (i = 0; i < 1024; i++) {
00263       if (fdleaks[i].isopen) {
00264          snprintf(line, sizeof(line), "%d", fdleaks[i].line);
00265          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);
00266       }
00267    }
00268    return CLI_SUCCESS;
00269 }
00270 
00271 static struct ast_cli_entry cli_show_fd = AST_CLI_DEFINE(handle_show_fd, "Show open file descriptors");
00272 
00273 int ast_fd_init(void)
00274 {
00275    return ast_cli_register(&cli_show_fd);
00276 }
00277 
00278 #else  /* !defined(DEBUG_FD_LEAKS) */
00279 int ast_fd_init(void)
00280 {
00281    return 0;
00282 }
00283 #endif /* defined(DEBUG_FD_LEAKS) */
00284 

Generated on Wed May 16 06:33:22 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6