Sat Aug 6 00:39:22 2011

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: 228338 $")
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 = NULL;
00134    struct protoent *pe;
00135    int res = socket(domain, type, protocol);
00136    if (res < 0 || res > 1023) {
00137       return res;
00138    }
00139 
00140    if ((pe = getprotobynumber(protocol))) {
00141       sproto = pe->p_name;
00142    }
00143 
00144    if (domain == PF_UNIX) {
00145       ast_copy_string(sdomain, "PF_UNIX", sizeof(sdomain));
00146    } else if (domain == PF_INET) {
00147       ast_copy_string(sdomain, "PF_INET", sizeof(sdomain));
00148    } else {
00149       snprintf(sdomain, sizeof(sdomain), "%d", domain);
00150    }
00151 
00152    if (type == SOCK_DGRAM) {
00153       ast_copy_string(stype, "SOCK_DGRAM", sizeof(stype));
00154       if (protocol == 0) {
00155          sproto = "udp";
00156       }
00157    } else if (type == SOCK_STREAM) {
00158       ast_copy_string(stype, "SOCK_STREAM", sizeof(stype));
00159       if (protocol == 0) {
00160          sproto = "tcp";
00161       }
00162    } else {
00163       snprintf(stype, sizeof(stype), "%d", type);
00164    }
00165 
00166    if (sproto) {
00167       STORE_COMMON(res, "socket", "%s,%s,\"%s\"", sdomain, stype, sproto);
00168    } else {
00169       STORE_COMMON(res, "socket", "%s,%s,\"%d\"", sdomain, stype, protocol);
00170    }
00171    return res;
00172 }
00173 
00174 #undef close
00175 int __ast_fdleak_close(int fd)
00176 {
00177    int res = close(fd);
00178    if (!res && fd > -1 && fd < 1024) {
00179       fdleaks[fd].isopen = 0;
00180    }
00181    return res;
00182 }
00183 
00184 #undef fopen
00185 FILE *__ast_fdleak_fopen(const char *path, const char *mode, const char *file, int line, const char *func)
00186 {
00187    FILE *res = fopen(path, mode);
00188    int fd;
00189    if (!res) {
00190       return res;
00191    }
00192    fd = fileno(res);
00193    STORE_COMMON(fd, "fopen", "\"%s\",\"%s\"", path, mode);
00194    return res;
00195 }
00196 
00197 #undef fclose
00198 int __ast_fdleak_fclose(FILE *ptr)
00199 {
00200    int fd, res;
00201    if (!ptr) {
00202       return fclose(ptr);
00203    }
00204 
00205    fd = fileno(ptr);
00206    if ((res = fclose(ptr)) || fd < 0 || fd > 1023) {
00207       return res;
00208    }
00209    fdleaks[fd].isopen = 0;
00210    return res;
00211 }
00212 
00213 #undef dup2
00214 int __ast_fdleak_dup2(int oldfd, int newfd, const char *file, int line, const char *func)
00215 {
00216    int res = dup2(oldfd, newfd);
00217    if (res < 0 || res > 1023) {
00218       return res;
00219    }
00220    STORE_COMMON(res, "dup2", "%d,%d", oldfd, newfd);
00221    return res;
00222 }
00223 
00224 #undef dup
00225 int __ast_fdleak_dup(int oldfd, const char *file, int line, const char *func)
00226 {
00227    int res = dup(oldfd);
00228    if (res < 0 || res > 1023) {
00229       return res;
00230    }
00231    STORE_COMMON(res, "dup2", "%d", oldfd);
00232    return res;
00233 }
00234 
00235 static int handle_show_fd(int fd, int argc, char *argv[])
00236 {
00237    int i;
00238    char line[24];
00239    struct rlimit rl;
00240    getrlimit(RLIMIT_FSIZE, &rl);
00241    if (rl.rlim_cur == RLIM_INFINITY || rl.rlim_max == RLIM_INFINITY) {
00242       ast_copy_string(line, "unlimited", sizeof(line));
00243    } else {
00244       snprintf(line, sizeof(line), "%d/%d", (int) rl.rlim_cur, (int) rl.rlim_max);
00245    }
00246    ast_cli(fd, "Current maxfiles: %s\n", line);
00247    for (i = 0; i < 1024; i++) {
00248       if (fdleaks[i].isopen) {
00249          snprintf(line, sizeof(line), "%d", fdleaks[i].line);
00250          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);
00251       }
00252    }
00253    return RESULT_SUCCESS;
00254 }
00255 
00256 static struct ast_cli_entry cli_show_fd =
00257    { { "core", "show", "fd", NULL },
00258    handle_show_fd, "Show open file descriptors" };
00259 
00260 int ast_fd_init(void)
00261 {
00262    return ast_cli_register(&cli_show_fd);
00263 }
00264 
00265 #else  /* !defined(DEBUG_FD_LEAKS) */
00266 int ast_fd_init(void)
00267 {
00268    return 0;
00269 }
00270 #endif /* defined(DEBUG_FD_LEAKS) */
00271 

Generated on Sat Aug 6 00:39:22 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7