diff options
author | tilghman <tilghman@f38db490-d61c-443f-a65b-d21fe96a405b> | 2009-04-09 04:32:40 +0000 |
---|---|---|
committer | tilghman <tilghman@f38db490-d61c-443f-a65b-d21fe96a405b> | 2009-04-09 04:32:40 +0000 |
commit | 3e658ba1694dfecea45182861877b0ed91a735f6 (patch) | |
tree | 63d112b18eb45b721de15642e78f43c2d44f96c3 /main | |
parent | c23a82df3d893f525a52da48a638fcf30dd51abe (diff) |
Oops, missed this file in the last commit.
git-svn-id: http://svn.digium.com/svn/asterisk/branches/1.4@187301 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'main')
-rw-r--r-- | main/astfd.c | 266 |
1 files changed, 266 insertions, 0 deletions
diff --git a/main/astfd.c b/main/astfd.c new file mode 100644 index 000000000..9880c6b7c --- /dev/null +++ b/main/astfd.c @@ -0,0 +1,266 @@ +/* + * Asterisk -- An open source telephony toolkit. + * + * Copyright (C) 2009, Digium, Inc. + * + * Tilghman Lesher <tlesher@digium.com> + * + * 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 <tlesher@digium.com> + */ + +#include "asterisk.h" + +#ifdef DEBUG_FD_LEAKS + +ASTERISK_FILE_VERSION(__FILE__, "$Revision$") + +#include <stdio.h> +#include <string.h> +#include <stddef.h> +#include <time.h> +#include <sys/time.h> +#include <sys/resource.h> + +#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 int handle_show_fd(int fd, int argc, char *argv[]) +{ + int i; + char line[24]; + struct rlimit rl; + 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(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(fd, "%5d %15s:%-7.7s (%-25s): %s(%s)\n", i, fdleaks[i].file, line, fdleaks[i].function, fdleaks[i].callname, fdleaks[i].callargs); + } + } + return RESULT_SUCCESS; +} + +static struct ast_cli_entry cli_show_fd = + { { "core", "show", "fd", NULL }, + 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) */ + |