~singpolyma/asterisk

ref: 51af79b42e4ae7d1831603633d73bf0df179720e asterisk/main/astfd.c -rw-r--r-- 9.4 KiB
51af79b4Christopher Vollick Content Created By Initiator 2: Electric Boogaloo 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
 * 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 \verbatim <tlesher@digium.com> \endverbatim
 */

/*** MODULEINFO
	<support_level>core</support_level>
 ***/

#include "asterisk.h"

#ifdef DEBUG_FD_LEAKS

#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"
#include "asterisk/localtime.h"
#include "asterisk/time.h"

static struct fdleaks {
	const char *callname;
	int line;
	unsigned int isopen:1;
	char file[40];
	char function[25];
	char callargs[100];
	struct timeval now;
} fdleaks[1024] = { { "", }, };

/* COPY does ast_copy_string(dst, src, sizeof(dst)), except:
 * - if it doesn't fit, it copies the value after the slash
 *   (possibly truncated)
 * - if there is no slash, it copies the value with the head
 *   truncated */
#define	COPY(dst, src)                                             \
	do {                                                           \
		int dlen = sizeof(dst), slen = strlen(src);                \
		if (slen + 1 > dlen) {                                     \
			const 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, ...)     \
	do { \
		struct fdleaks *tmp = &fdleaks[offset]; \
		tmp->now = ast_tvnow(); \
		COPY(tmp->file, file);      \
		tmp->line = line;           \
		COPY(tmp->function, func);  \
		tmp->callname = name;       \
		snprintf(tmp->callargs, sizeof(tmp->callargs), __VA_ARGS__); \
		tmp->isopen = 1;            \
	} while (0)

#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 < ARRAY_LEN(fdleaks)) {
			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" : "",
#if O_RDONLY == 0
				!(flags & (O_WRONLY | O_RDWR)) ? "|O_RDONLY" : "",
#else
				flags & O_RDONLY ? "|O_RDONLY" : "",
#endif
				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 < ARRAY_LEN(fdleaks)) {
			STORE_COMMON(res, "open", "\"%s\",%d", path, flags);
		}
	}
	return res;
}

#undef accept
int __ast_fdleak_accept(int socket, struct sockaddr *address, socklen_t *address_len,
	const char *file, int line, const char *func)
{
	int res = accept(socket, address, address_len);

	if (res >= 0) {
		STORE_COMMON(res, "accept", "{%d}", socket);
	}

	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++) {
		if (fds[i] > -1 && fds[i] < ARRAY_LEN(fdleaks)) {
			STORE_COMMON(fds[i], "pipe", "{%d,%d}", fds[0], fds[1]);
		}
	}
	return 0;
}

#undef socketpair
int __ast_fdleak_socketpair(int domain, int type, int protocol, int sv[2],
	const char *file, int line, const char *func)
{
	int i, res = socketpair(domain, type, protocol, sv);
	if (res) {
		return res;
	}
	for (i = 0; i < 2; i++) {
		if (sv[i] > -1 && sv[i] < ARRAY_LEN(fdleaks)) {
			STORE_COMMON(sv[i], "socketpair", "{%d,%d}", sv[0], sv[1]);
		}
	}
	return 0;
}

#if defined(HAVE_EVENTFD)
#undef eventfd
#include <sys/eventfd.h>
int __ast_fdleak_eventfd(unsigned int initval, int flags, const char *file, int line, const char *func)
{
	int res = eventfd(initval, flags);

	if (res >= 0) {
		STORE_COMMON(res, "eventfd", "{%d}", res);
	}

	return res;
}
#endif

#if defined(HAVE_TIMERFD)
#undef timerfd_create
#include <sys/timerfd.h>
int __ast_fdleak_timerfd_create(int clockid, int flags, const char *file, int line, const char *func)
{
	int res = timerfd_create(clockid, flags);

	if (res >= 0) {
		STORE_COMMON(res, "timerfd_create", "{%d}", res);
	}

	return res;
}
#endif

#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 = NULL;
	struct protoent *pe;
	int res = socket(domain, type, protocol);
	if (res < 0 || res >= ARRAY_LEN(fdleaks)) {
		return res;
	}

	if ((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);
	}

	if (sproto) {
		STORE_COMMON(res, "socket", "%s,%s,\"%s\"", sdomain, stype, sproto);
	} else {
		STORE_COMMON(res, "socket", "%s,%s,\"%d\"", sdomain, stype, protocol);
	}
	return res;
}

#undef close
int __ast_fdleak_close(int fd)
{
	int res = close(fd);
	if (!res && fd > -1 && fd < ARRAY_LEN(fdleaks)) {
		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);
	if (fd > -1 && fd < ARRAY_LEN(fdleaks)) {
		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 >= ARRAY_LEN(fdleaks)) {
		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 >= ARRAY_LEN(fdleaks)) {
		return res;
	}
	/* On success, newfd will be closed automatically if it was already
	 * open. We don't need to mention anything about that, we're updating
	 * the value anway. */
	STORE_COMMON(res, "dup2", "%d,%d", oldfd, newfd); /* res == 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 >= ARRAY_LEN(fdleaks)) {
		return res;
	}
	STORE_COMMON(res, "dup2", "%d", oldfd);
	return res;
}

static char *handle_show_fd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
	int i;
	char line[24];
	struct rlimit rl;
	switch (cmd) {
	case CLI_INIT:
		e->command = "core show fd";
		e->usage =
			"Usage: core show fd\n"
			"       List all file descriptors currently in use and where\n"
			"       each was opened, and with what command.\n";
		return NULL;
	case CLI_GENERATE:
		return NULL;
	}
	getrlimit(RLIMIT_NOFILE, &rl);
	if (rl.rlim_cur == RLIM_INFINITY) {
		ast_copy_string(line, "unlimited", sizeof(line));
	} else {
		snprintf(line, sizeof(line), "%d", (int) rl.rlim_cur);
	}
	ast_cli(a->fd, "Current maxfiles: %s\n", line);
	for (i = 0; i < ARRAY_LEN(fdleaks); i++) {
		if (fdleaks[i].isopen) {
			struct ast_tm tm = {0};
			char datestring[256];

			ast_localtime(&fdleaks[i].now, &tm, NULL);
			ast_strftime(datestring, sizeof(datestring), "%F %T", &tm);
			snprintf(line, sizeof(line), "%d", fdleaks[i].line);
			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);
		}
	}
	return CLI_SUCCESS;
}

static struct ast_cli_entry cli_show_fd = AST_CLI_DEFINE(handle_show_fd, "Show open file descriptors");

static void fd_shutdown(void)
{
	ast_cli_unregister(&cli_show_fd);
}

int ast_fd_init(void)
{
	ast_register_cleanup(fd_shutdown);
	return ast_cli_register(&cli_show_fd);
}

#else  /* !defined(DEBUG_FD_LEAKS) */
int ast_fd_init(void)
{
	return 0;
}
#endif /* defined(DEBUG_FD_LEAKS) */