Asterisk - The Open Source Telephony Project  18.5.0
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 <[email protected]>
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 \verbatim <[email protected]> \endverbatim
24  */
25 
26 /*** MODULEINFO
27  <support_level>core</support_level>
28  ***/
29 
30 #include "asterisk.h"
31 
32 #ifdef DEBUG_FD_LEAKS
33 
34 #include <stdio.h>
35 #include <string.h>
36 #include <stddef.h>
37 #include <time.h>
38 #include <sys/time.h>
39 #include <sys/resource.h>
40 
41 #include "asterisk/cli.h"
42 #include "asterisk/logger.h"
43 #include "asterisk/options.h"
44 #include "asterisk/lock.h"
45 #include "asterisk/strings.h"
46 #include "asterisk/unaligned.h"
47 #include "asterisk/localtime.h"
48 #include "asterisk/time.h"
49 
50 static struct fdleaks {
51  const char *callname;
52  int line;
53  unsigned int isopen:1;
54  char file[40];
55  char function[25];
56  char callargs[100];
57  struct timeval now;
58 } fdleaks[1024] = { { "", }, };
59 
60 /* COPY does ast_copy_string(dst, src, sizeof(dst)), except:
61  * - if it doesn't fit, it copies the value after the slash
62  * (possibly truncated)
63  * - if there is no slash, it copies the value with the head
64  * truncated */
65 #define COPY(dst, src) \
66  do { \
67  int dlen = sizeof(dst), slen = strlen(src); \
68  if (slen + 1 > dlen) { \
69  const char *slash = strrchr(src, '/'); \
70  if (slash) { \
71  ast_copy_string(dst, slash + 1, dlen); \
72  } else { \
73  ast_copy_string(dst, src + slen - dlen + 1, dlen); \
74  } \
75  } else { \
76  ast_copy_string(dst, src, dlen); \
77  } \
78  } while (0)
79 
80 #define STORE_COMMON(offset, name, ...) \
81  do { \
82  struct fdleaks *tmp = &fdleaks[offset]; \
83  tmp->now = ast_tvnow(); \
84  COPY(tmp->file, file); \
85  tmp->line = line; \
86  COPY(tmp->function, func); \
87  tmp->callname = name; \
88  snprintf(tmp->callargs, sizeof(tmp->callargs), __VA_ARGS__); \
89  tmp->isopen = 1; \
90  } while (0)
91 
92 #undef open
93 int __ast_fdleak_open(const char *file, int line, const char *func, const char *path, int flags, ...)
94 {
95  int res;
96  va_list ap;
97  int mode;
98 
99  if (flags & O_CREAT) {
100  va_start(ap, flags);
101  mode = va_arg(ap, int);
102  va_end(ap);
103  res = open(path, flags, mode);
104  if (res > -1 && res < ARRAY_LEN(fdleaks)) {
105  char sflags[80];
106  snprintf(sflags, sizeof(sflags), "O_CREAT%s%s%s%s%s%s%s%s",
107  flags & O_APPEND ? "|O_APPEND" : "",
108  flags & O_EXCL ? "|O_EXCL" : "",
109  flags & O_NONBLOCK ? "|O_NONBLOCK" : "",
110  flags & O_TRUNC ? "|O_TRUNC" : "",
111  flags & O_RDWR ? "|O_RDWR" : "",
112 #if O_RDONLY == 0
113  !(flags & (O_WRONLY | O_RDWR)) ? "|O_RDONLY" : "",
114 #else
115  flags & O_RDONLY ? "|O_RDONLY" : "",
116 #endif
117  flags & O_WRONLY ? "|O_WRONLY" : "",
118  "");
119  flags &= ~(O_CREAT | O_APPEND | O_EXCL | O_NONBLOCK | O_TRUNC | O_RDWR | O_RDONLY | O_WRONLY);
120  if (flags) {
121  STORE_COMMON(res, "open", "\"%s\",%s|%d,%04o", path, sflags, flags, mode);
122  } else {
123  STORE_COMMON(res, "open", "\"%s\",%s,%04o", path, sflags, mode);
124  }
125  }
126  } else {
127  res = open(path, flags);
128  if (res > -1 && res < ARRAY_LEN(fdleaks)) {
129  STORE_COMMON(res, "open", "\"%s\",%d", path, flags);
130  }
131  }
132  return res;
133 }
134 
135 #undef accept
136 int __ast_fdleak_accept(int socket, struct sockaddr *address, socklen_t *address_len,
137  const char *file, int line, const char *func)
138 {
139  int res = accept(socket, address, address_len);
140 
141  if (res >= 0) {
142  STORE_COMMON(res, "accept", "{%d}", socket);
143  }
144 
145  return res;
146 }
147 
148 #undef pipe
149 int __ast_fdleak_pipe(int *fds, const char *file, int line, const char *func)
150 {
151  int i, res = pipe(fds);
152  if (res) {
153  return res;
154  }
155  for (i = 0; i < 2; i++) {
156  if (fds[i] > -1 && fds[i] < ARRAY_LEN(fdleaks)) {
157  STORE_COMMON(fds[i], "pipe", "{%d,%d}", fds[0], fds[1]);
158  }
159  }
160  return 0;
161 }
162 
163 #undef socketpair
164 int __ast_fdleak_socketpair(int domain, int type, int protocol, int sv[2],
165  const char *file, int line, const char *func)
166 {
167  int i, res = socketpair(domain, type, protocol, sv);
168  if (res) {
169  return res;
170  }
171  for (i = 0; i < 2; i++) {
172  if (sv[i] > -1 && sv[i] < ARRAY_LEN(fdleaks)) {
173  STORE_COMMON(sv[i], "socketpair", "{%d,%d}", sv[0], sv[1]);
174  }
175  }
176  return 0;
177 }
178 
179 #if defined(HAVE_EVENTFD)
180 #undef eventfd
181 #include <sys/eventfd.h>
182 int __ast_fdleak_eventfd(unsigned int initval, int flags, const char *file, int line, const char *func)
183 {
184  int res = eventfd(initval, flags);
185 
186  if (res >= 0) {
187  STORE_COMMON(res, "eventfd", "{%d}", res);
188  }
189 
190  return res;
191 }
192 #endif
193 
194 #if defined(HAVE_TIMERFD)
195 #undef timerfd_create
196 #include <sys/timerfd.h>
197 int __ast_fdleak_timerfd_create(int clockid, int flags, const char *file, int line, const char *func)
198 {
199  int res = timerfd_create(clockid, flags);
200 
201  if (res >= 0) {
202  STORE_COMMON(res, "timerfd_create", "{%d}", res);
203  }
204 
205  return res;
206 }
207 #endif
208 
209 #undef socket
210 int __ast_fdleak_socket(int domain, int type, int protocol, const char *file, int line, const char *func)
211 {
212  char sdomain[20], stype[20], *sproto = NULL;
213  struct protoent *pe;
214  int res = socket(domain, type, protocol);
215  if (res < 0 || res >= ARRAY_LEN(fdleaks)) {
216  return res;
217  }
218 
219  if ((pe = getprotobynumber(protocol))) {
220  sproto = pe->p_name;
221  }
222 
223  if (domain == PF_UNIX) {
224  ast_copy_string(sdomain, "PF_UNIX", sizeof(sdomain));
225  } else if (domain == PF_INET) {
226  ast_copy_string(sdomain, "PF_INET", sizeof(sdomain));
227  } else {
228  snprintf(sdomain, sizeof(sdomain), "%d", domain);
229  }
230 
231  if (type == SOCK_DGRAM) {
232  ast_copy_string(stype, "SOCK_DGRAM", sizeof(stype));
233  if (protocol == 0) {
234  sproto = "udp";
235  }
236  } else if (type == SOCK_STREAM) {
237  ast_copy_string(stype, "SOCK_STREAM", sizeof(stype));
238  if (protocol == 0) {
239  sproto = "tcp";
240  }
241  } else {
242  snprintf(stype, sizeof(stype), "%d", type);
243  }
244 
245  if (sproto) {
246  STORE_COMMON(res, "socket", "%s,%s,\"%s\"", sdomain, stype, sproto);
247  } else {
248  STORE_COMMON(res, "socket", "%s,%s,\"%d\"", sdomain, stype, protocol);
249  }
250  return res;
251 }
252 
253 #undef close
254 int __ast_fdleak_close(int fd)
255 {
256  int res = close(fd);
257  if (!res && fd > -1 && fd < ARRAY_LEN(fdleaks)) {
258  fdleaks[fd].isopen = 0;
259  }
260  return res;
261 }
262 
263 #undef fopen
264 FILE *__ast_fdleak_fopen(const char *path, const char *mode, const char *file, int line, const char *func)
265 {
266  FILE *res = fopen(path, mode);
267  int fd;
268  if (!res) {
269  return res;
270  }
271  fd = fileno(res);
272  if (fd > -1 && fd < ARRAY_LEN(fdleaks)) {
273  STORE_COMMON(fd, "fopen", "\"%s\",\"%s\"", path, mode);
274  }
275  return res;
276 }
277 
278 #undef fclose
279 int __ast_fdleak_fclose(FILE *ptr)
280 {
281  int fd, res;
282  if (!ptr) {
283  return fclose(ptr);
284  }
285 
286  fd = fileno(ptr);
287  if ((res = fclose(ptr)) || fd < 0 || fd >= ARRAY_LEN(fdleaks)) {
288  return res;
289  }
290  fdleaks[fd].isopen = 0;
291  return res;
292 }
293 
294 #undef dup2
295 int __ast_fdleak_dup2(int oldfd, int newfd, const char *file, int line, const char *func)
296 {
297  int res = dup2(oldfd, newfd);
298  if (res < 0 || res >= ARRAY_LEN(fdleaks)) {
299  return res;
300  }
301  /* On success, newfd will be closed automatically if it was already
302  * open. We don't need to mention anything about that, we're updating
303  * the value anway. */
304  STORE_COMMON(res, "dup2", "%d,%d", oldfd, newfd); /* res == newfd */
305  return res;
306 }
307 
308 #undef dup
309 int __ast_fdleak_dup(int oldfd, const char *file, int line, const char *func)
310 {
311  int res = dup(oldfd);
312  if (res < 0 || res >= ARRAY_LEN(fdleaks)) {
313  return res;
314  }
315  STORE_COMMON(res, "dup2", "%d", oldfd);
316  return res;
317 }
318 
319 static char *handle_show_fd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
320 {
321  int i;
322  char line[24];
323  struct rlimit rl;
324  switch (cmd) {
325  case CLI_INIT:
326  e->command = "core show fd";
327  e->usage =
328  "Usage: core show fd\n"
329  " List all file descriptors currently in use and where\n"
330  " each was opened, and with what command.\n";
331  return NULL;
332  case CLI_GENERATE:
333  return NULL;
334  }
335  getrlimit(RLIMIT_NOFILE, &rl);
336  if (rl.rlim_cur == RLIM_INFINITY) {
337  ast_copy_string(line, "unlimited", sizeof(line));
338  } else {
339  snprintf(line, sizeof(line), "%d", (int) rl.rlim_cur);
340  }
341  ast_cli(a->fd, "Current maxfiles: %s\n", line);
342  for (i = 0; i < ARRAY_LEN(fdleaks); i++) {
343  if (fdleaks[i].isopen) {
344  struct ast_tm tm = {0};
345  char datestring[256];
346 
347  ast_localtime(&fdleaks[i].now, &tm, NULL);
348  ast_strftime(datestring, sizeof(datestring), "%F %T", &tm);
349  snprintf(line, sizeof(line), "%d", fdleaks[i].line);
350  ast_cli(a->fd, "%5d [%s] %22s:%-7.7s (%-25s): %s(%s)\n", i, datestring, fdleaks[i].file, line, fdleaks[i].function, fdleaks[i].callname, fdleaks[i].callargs);
351  }
352  }
353  return CLI_SUCCESS;
354 }
355 
356 static struct ast_cli_entry cli_show_fd = AST_CLI_DEFINE(handle_show_fd, "Show open file descriptors");
357 
358 static void fd_shutdown(void)
359 {
360  ast_cli_unregister(&cli_show_fd);
361 }
362 
363 int ast_fd_init(void)
364 {
365  ast_register_cleanup(fd_shutdown);
366  return ast_cli_register(&cli_show_fd);
367 }
368 
369 #else /* !defined(DEBUG_FD_LEAKS) */
370 int ast_fd_init(void)
371 {
372  return 0;
373 }
374 #endif /* defined(DEBUG_FD_LEAKS) */
static const char type[]
Definition: chan_ooh323.c:109
#define AST_CLI_DEFINE(fn, txt,...)
Definition: cli.h:197
Asterisk locking-related definitions:
Asterisk main include file. File version handling, generic pbx functions.
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
String manipulation functions.
Time-related functions and macros.
char * address
Definition: f2c.h:59
int ast_cli_unregister(struct ast_cli_entry *e)
Unregisters a command or an array of commands.
Definition: main/cli.c:2397
descriptor for a cli entry.
Definition: cli.h:171
struct ast_tm * ast_localtime(const struct timeval *timep, struct ast_tm *p_tm, const char *zone)
Timezone-independent version of localtime_r(3).
Definition: localtime.c:1739
Definition: cli.h:152
#define NULL
Definition: resample.c:96
Domain data structure.
Definition: sip.h:888
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
#define ast_cli_register(e)
Registers a command or an array of commands.
Definition: cli.h:256
Custom localtime functions for multiple timezones.
Handle unaligned data access.
int ast_register_cleanup(void(*func)(void))
Register a function to be executed before Asterisk gracefully exits.
Definition: clicompat.c:19
const int fd
Definition: cli.h:159
char * command
Definition: cli.h:186
int ast_strftime(char *buf, size_t len, const char *format, const struct ast_tm *tm)
Special version of strftime(3) that handles fractions of a second. Takes the same arguments as strfti...
Definition: localtime.c:2524
Support for logging to various files, console and syslog Configuration in file logger.conf.
const char * usage
Definition: cli.h:177
#define CLI_SUCCESS
Definition: cli.h:44
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:401
int ast_fd_init(void)
Definition: astfd.c:370
Options provided by main asterisk program.
static struct test_val a