Wed Jan 8 2020 09:49:42

Asterisk developer's documentation


astfd.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2009, Digium, Inc.
5  *
6  * Tilghman Lesher <tlesher@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18 
19 /*! \file
20  *
21  * \brief Debugging routines for file descriptor leaks
22  *
23  * \author Tilghman Lesher <tlesher@digium.com>
24  */
25 
26 /*** MODULEINFO
27  <support_level>core</support_level>
28  ***/
29 
30 #include "asterisk.h"
31 
32 #ifdef DEBUG_FD_LEAKS
33 
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 397106 $")
35 
36 #include <stdio.h>
37 #include <string.h>
38 #include <stddef.h>
39 #include <time.h>
40 #include <sys/time.h>
41 #include <sys/resource.h>
42 
43 #include "asterisk/cli.h"
44 #include "asterisk/logger.h"
45 #include "asterisk/options.h"
46 #include "asterisk/lock.h"
47 #include "asterisk/strings.h"
48 #include "asterisk/unaligned.h"
49 
50 static struct fdleaks {
51  char file[40];
52  int line;
53  char function[25];
54  char callname[10];
55  char callargs[60];
56  unsigned int isopen:1;
57 } fdleaks[1024] = { { "", }, };
58 
59 #define COPY(dst, src) \
60  do { \
61  int dlen = sizeof(dst), slen = strlen(src); \
62  if (slen + 1 > dlen) { \
63  char *slash = strrchr(src, '/'); \
64  if (slash) { \
65  ast_copy_string(dst, slash + 1, dlen); \
66  } else { \
67  ast_copy_string(dst, src + slen - dlen + 1, dlen); \
68  } \
69  } else { \
70  ast_copy_string(dst, src, dlen); \
71  } \
72  } while (0)
73 
74 #define STORE_COMMON(offset, name, ...) \
75  COPY(fdleaks[offset].file, file); \
76  fdleaks[offset].line = line; \
77  COPY(fdleaks[offset].function, func); \
78  strcpy(fdleaks[offset].callname, name); \
79  snprintf(fdleaks[offset].callargs, sizeof(fdleaks[offset].callargs), __VA_ARGS__); \
80  fdleaks[offset].isopen = 1;
81 
82 #undef open
83 int __ast_fdleak_open(const char *file, int line, const char *func, const char *path, int flags, ...)
84 {
85  int res;
86  va_list ap;
87  int mode;
88 
89  if (flags & O_CREAT) {
90  va_start(ap, flags);
91  mode = va_arg(ap, int);
92  va_end(ap);
93  res = open(path, flags, mode);
94  if (res > -1 && res < (sizeof(fdleaks) / sizeof(fdleaks[0]))) {
95  char sflags[80];
96  snprintf(sflags, sizeof(sflags), "O_CREAT%s%s%s%s%s%s%s%s",
97  flags & O_APPEND ? "|O_APPEND" : "",
98  flags & O_EXCL ? "|O_EXCL" : "",
99  flags & O_NONBLOCK ? "|O_NONBLOCK" : "",
100  flags & O_TRUNC ? "|O_TRUNC" : "",
101  flags & O_RDWR ? "|O_RDWR" : "",
102 #if O_RDONLY == 0
103  !(flags & (O_WRONLY | O_RDWR)) ? "|O_RDONLY" : "",
104 #else
105  flags & O_RDONLY ? "|O_RDONLY" : "",
106 #endif
107  flags & O_WRONLY ? "|O_WRONLY" : "",
108  "");
109  flags &= ~(O_CREAT | O_APPEND | O_EXCL | O_NONBLOCK | O_TRUNC | O_RDWR | O_RDONLY | O_WRONLY);
110  if (flags) {
111  STORE_COMMON(res, "open", "\"%s\",%s|%d,%04o", path, sflags, flags, mode);
112  } else {
113  STORE_COMMON(res, "open", "\"%s\",%s,%04o", path, sflags, mode);
114  }
115  }
116  } else {
117  res = open(path, flags);
118  if (res > -1 && res < (sizeof(fdleaks) / sizeof(fdleaks[0]))) {
119  STORE_COMMON(res, "open", "\"%s\",%d", path, flags);
120  }
121  }
122  return res;
123 }
124 
125 #undef pipe
126 int __ast_fdleak_pipe(int *fds, const char *file, int line, const char *func)
127 {
128  int i, res = pipe(fds);
129  if (res) {
130  return res;
131  }
132  for (i = 0; i < 2; i++) {
133  STORE_COMMON(fds[i], "pipe", "{%d,%d}", fds[0], fds[1]);
134  }
135  return 0;
136 }
137 
138 #undef socket
139 int __ast_fdleak_socket(int domain, int type, int protocol, const char *file, int line, const char *func)
140 {
141  char sdomain[20], stype[20], *sproto = NULL;
142  struct protoent *pe;
143  int res = socket(domain, type, protocol);
144  if (res < 0 || res > 1023) {
145  return res;
146  }
147 
148  if ((pe = getprotobynumber(protocol))) {
149  sproto = pe->p_name;
150  }
151 
152  if (domain == PF_UNIX) {
153  ast_copy_string(sdomain, "PF_UNIX", sizeof(sdomain));
154  } else if (domain == PF_INET) {
155  ast_copy_string(sdomain, "PF_INET", sizeof(sdomain));
156  } else {
157  snprintf(sdomain, sizeof(sdomain), "%d", domain);
158  }
159 
160  if (type == SOCK_DGRAM) {
161  ast_copy_string(stype, "SOCK_DGRAM", sizeof(stype));
162  if (protocol == 0) {
163  sproto = "udp";
164  }
165  } else if (type == SOCK_STREAM) {
166  ast_copy_string(stype, "SOCK_STREAM", sizeof(stype));
167  if (protocol == 0) {
168  sproto = "tcp";
169  }
170  } else {
171  snprintf(stype, sizeof(stype), "%d", type);
172  }
173 
174  if (sproto) {
175  STORE_COMMON(res, "socket", "%s,%s,\"%s\"", sdomain, stype, sproto);
176  } else {
177  STORE_COMMON(res, "socket", "%s,%s,\"%d\"", sdomain, stype, protocol);
178  }
179  return res;
180 }
181 
182 #undef close
183 int __ast_fdleak_close(int fd)
184 {
185  int res = close(fd);
186  if (!res && fd > -1 && fd < 1024) {
187  fdleaks[fd].isopen = 0;
188  }
189  return res;
190 }
191 
192 #undef fopen
193 FILE *__ast_fdleak_fopen(const char *path, const char *mode, const char *file, int line, const char *func)
194 {
195  FILE *res = fopen(path, mode);
196  int fd;
197  if (!res) {
198  return res;
199  }
200  fd = fileno(res);
201  STORE_COMMON(fd, "fopen", "\"%s\",\"%s\"", path, mode);
202  return res;
203 }
204 
205 #undef fclose
206 int __ast_fdleak_fclose(FILE *ptr)
207 {
208  int fd, res;
209  if (!ptr) {
210  return fclose(ptr);
211  }
212 
213  fd = fileno(ptr);
214  if ((res = fclose(ptr)) || fd < 0 || fd > 1023) {
215  return res;
216  }
217  fdleaks[fd].isopen = 0;
218  return res;
219 }
220 
221 #undef dup2
222 int __ast_fdleak_dup2(int oldfd, int newfd, const char *file, int line, const char *func)
223 {
224  int res = dup2(oldfd, newfd);
225  if (res < 0 || res > 1023) {
226  return res;
227  }
228  STORE_COMMON(res, "dup2", "%d,%d", oldfd, newfd);
229  return res;
230 }
231 
232 #undef dup
233 int __ast_fdleak_dup(int oldfd, const char *file, int line, const char *func)
234 {
235  int res = dup(oldfd);
236  if (res < 0 || res > 1023) {
237  return res;
238  }
239  STORE_COMMON(res, "dup2", "%d", oldfd);
240  return res;
241 }
242 
243 static char *handle_show_fd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
244 {
245  int i;
246  char line[24];
247  struct rlimit rl;
248  switch (cmd) {
249  case CLI_INIT:
250  e->command = "core show fd";
251  e->usage =
252  "Usage: core show fd\n"
253  " List all file descriptors currently in use and where\n"
254  " each was opened, and with what command.\n";
255  return NULL;
256  case CLI_GENERATE:
257  return NULL;
258  }
259  getrlimit(RLIMIT_FSIZE, &rl);
260  if (rl.rlim_cur == RLIM_INFINITY || rl.rlim_max == RLIM_INFINITY) {
261  ast_copy_string(line, "unlimited", sizeof(line));
262  } else {
263  snprintf(line, sizeof(line), "%d/%d", (int) rl.rlim_cur, (int) rl.rlim_max);
264  }
265  ast_cli(a->fd, "Current maxfiles: %s\n", line);
266  for (i = 0; i < 1024; i++) {
267  if (fdleaks[i].isopen) {
268  snprintf(line, sizeof(line), "%d", fdleaks[i].line);
269  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);
270  }
271  }
272  return CLI_SUCCESS;
273 }
274 
275 static struct ast_cli_entry cli_show_fd = AST_CLI_DEFINE(handle_show_fd, "Show open file descriptors");
276 
277 static void fd_shutdown(void)
278 {
279  ast_cli_unregister(&cli_show_fd);
280 }
281 
282 int ast_fd_init(void)
283 {
284  ast_register_atexit(fd_shutdown);
285  return ast_cli_register(&cli_show_fd);
286 }
287 
288 #else /* !defined(DEBUG_FD_LEAKS) */
289 int ast_fd_init(void)
290 {
291  return 0;
292 }
293 #endif /* defined(DEBUG_FD_LEAKS) */
294 
#define AST_CLI_DEFINE(fn, txt,...)
Definition: cli.h:191
Asterisk locking-related definitions:
Asterisk main include file. File version handling, generic pbx functions.
String manipulation functions.
int ast_cli_register(struct ast_cli_entry *e)
Registers a command or an array of commands.
Definition: cli.c:2159
Time-related functions and macros.
int ast_cli_unregister(struct ast_cli_entry *e)
Unregisters a command or an array of commands.
Definition: cli.c:2153
descriptor for a cli entry.
Definition: cli.h:165
Definition: cli.h:146
void ast_cli(int fd, const char *fmt,...)
Definition: cli.c:105
Handle unaligned data access.
const int fd
Definition: cli.h:153
int ast_register_atexit(void(*func)(void))
Register a function to be executed before Asterisk exits.
Definition: asterisk.c:998
char * command
Definition: cli.h:180
static const char type[]
Definition: chan_nbs.c:57
Support for logging to various files, console and syslog Configuration in file logger.conf.
const char * usage
Definition: cli.h:171
#define CLI_SUCCESS
Definition: cli.h:43
Standard Command Line Interface.
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:223
Options provided by main asterisk program.
int ast_fd_init(void)
Definition: astfd.c:289
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180