| 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 |
#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 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 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 |
errno = EINVAL; |
| 284 |
return -1; |
| 285 |
} |
| 286 |
|
| 287 |
fd = fileno(ptr); |
| 288 |
if ((res = fclose(ptr)) || fd < 0 || fd >= ARRAY_LEN(fdleaks)) { |
| 289 |
return res; |
| 290 |
} |
| 291 |
fdleaks[fd].isopen = 0; |
| 292 |
return res; |
| 293 |
} |
| 294 |
|
| 295 |
#undef dup2 |
| 296 |
int __ast_fdleak_dup2(int oldfd, int newfd, const char *file, int line, const char *func) |
| 297 |
{ |
| 298 |
int res = dup2(oldfd, newfd); |
| 299 |
if (res < 0 || res >= ARRAY_LEN(fdleaks)) { |
| 300 |
return res; |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
STORE_COMMON(res, "dup2", "%d,%d", oldfd, newfd); |
| 306 |
return res; |
| 307 |
} |
| 308 |
|
| 309 |
#undef dup |
| 310 |
int __ast_fdleak_dup(int oldfd, const char *file, int line, const char *func) |
| 311 |
{ |
| 312 |
int res = dup(oldfd); |
| 313 |
if (res < 0 || res >= ARRAY_LEN(fdleaks)) { |
| 314 |
return res; |
| 315 |
} |
| 316 |
STORE_COMMON(res, "dup2", "%d", oldfd); |
| 317 |
return res; |
| 318 |
} |
| 319 |
|
| 320 |
static char *handle_show_fd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 321 |
{ |
| 322 |
int i; |
| 323 |
char line[24]; |
| 324 |
struct rlimit rl; |
| 325 |
switch (cmd) { |
| 326 |
case CLI_INIT: |
| 327 |
e->command = "core show fd"; |
| 328 |
e->usage = |
| 329 |
"Usage: core show fd\n" |
| 330 |
" List all file descriptors currently in use and where\n" |
| 331 |
" each was opened, and with what command.\n"; |
| 332 |
return NULL; |
| 333 |
case CLI_GENERATE: |
| 334 |
return NULL; |
| 335 |
} |
| 336 |
getrlimit(RLIMIT_NOFILE, &rl); |
| 337 |
if (rl.rlim_cur == RLIM_INFINITY) { |
| 338 |
ast_copy_string(line, "unlimited", sizeof(line)); |
| 339 |
} else { |
| 340 |
snprintf(line, sizeof(line), "%d", (int) rl.rlim_cur); |
| 341 |
} |
| 342 |
ast_cli(a->fd, "Current maxfiles: %s\n", line); |
| 343 |
for (i = 0; i < ARRAY_LEN(fdleaks); i++) { |
| 344 |
if (fdleaks[i].isopen) { |
| 345 |
struct ast_tm tm = {0}; |
| 346 |
char datestring[256]; |
| 347 |
|
| 348 |
ast_localtime(&fdleaks[i].now, &tm, NULL); |
| 349 |
ast_strftime(datestring, sizeof(datestring), "%F %T", &tm); |
| 350 |
snprintf(line, sizeof(line), "%d", fdleaks[i].line); |
| 351 |
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); |
| 352 |
} |
| 353 |
} |
| 354 |
return CLI_SUCCESS; |
| 355 |
} |
| 356 |
|
| 357 |
static struct ast_cli_entry cli_show_fd = AST_CLI_DEFINE(handle_show_fd, "Show open file descriptors"); |
| 358 |
|
| 359 |
static void fd_shutdown(void) |
| 360 |
{ |
| 361 |
ast_cli_unregister(&cli_show_fd); |
| 362 |
} |
| 363 |
|
| 364 |
int ast_fd_init(void) |
| 365 |
{ |
| 366 |
ast_register_cleanup(fd_shutdown); |
| 367 |
return ast_cli_register(&cli_show_fd); |
| 368 |
} |
| 369 |
|
| 370 |
#else |
| 371 |
int ast_fd_init(void) |
| 372 |
{ |
| 373 |
return 0; |
| 374 |
} |
| 375 |
#endif |