#include "asterisk.h"
#include <ctype.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <fcntl.h>
#include "asterisk/utils.h"
Go to the source code of this file.
Functions | |
void | closefrom (int n) |
uint64_t | htonll (uint64_t host64) |
uint64_t | ntohll (uint64_t net64) |
Definition in file strcompat.c.
void closefrom | ( | int | n | ) |
Definition at line 428 of file strcompat.c.
References errno.
Referenced by ast_close_fds_above_n().
00429 { 00430 long x; 00431 struct rlimit rl; 00432 DIR *dir; 00433 char path[16], *result; 00434 struct dirent *entry; 00435 00436 snprintf(path, sizeof(path), "/proc/%d/fd", (int) getpid()); 00437 if ((dir = opendir(path))) { 00438 while ((entry = readdir(dir))) { 00439 /* Skip . and .. */ 00440 if (entry->d_name[0] == '.') { 00441 continue; 00442 } 00443 if ((x = strtol(entry->d_name, &result, 10)) && x >= n) { 00444 #ifdef STRICT_COMPAT 00445 close(x); 00446 #else 00447 /* This isn't strictly compatible, but it's actually faster 00448 * for our purposes to set the CLOEXEC flag than to close 00449 * file descriptors. 00450 */ 00451 long flags = fcntl(x, F_GETFD); 00452 if (flags == -1 && errno == EBADF) { 00453 continue; 00454 } 00455 fcntl(x, F_SETFD, flags | FD_CLOEXEC); 00456 #endif 00457 } 00458 } 00459 closedir(dir); 00460 } else { 00461 getrlimit(RLIMIT_NOFILE, &rl); 00462 if (rl.rlim_cur > 65535) { 00463 /* A more reasonable value. Consider that the primary source of 00464 * file descriptors in Asterisk are UDP sockets, of which we are 00465 * limited to 65,535 per address. We additionally limit that down 00466 * to about 10,000 sockets per protocol. While the kernel will 00467 * allow us to set the fileno limit higher (up to 4.2 billion), 00468 * there really is no practical reason for it to be that high. 00469 */ 00470 rl.rlim_cur = 65535; 00471 } 00472 for (x = n; x < rl.rlim_cur; x++) { 00473 #ifdef STRICT_COMPAT 00474 close(x); 00475 #else 00476 long flags = fcntl(x, F_GETFD); 00477 if (flags == -1 && errno == EBADF) { 00478 continue; 00479 } 00480 fcntl(x, F_SETFD, flags | FD_CLOEXEC); 00481 #endif 00482 } 00483 } 00484 }
uint64_t htonll | ( | uint64_t | host64 | ) |
Definition at line 389 of file strcompat.c.
Referenced by iax_ie_append_versioned_uint64().
00390 { 00391 #if BYTE_ORDER == BIG_ENDIAN 00392 return host64; 00393 #elif BYTE_ORDER == LITTLE_ENDIAN 00394 union { 00395 unsigned char c[8]; 00396 uint64_t u; 00397 } number; 00398 number.u = host64; 00399 return 00400 (((uint64_t) number.c[0]) << 56) | 00401 (((uint64_t) number.c[1]) << 48) | 00402 (((uint64_t) number.c[2]) << 40) | 00403 (((uint64_t) number.c[3]) << 32) | 00404 (((uint64_t) number.c[4]) << 24) | 00405 (((uint64_t) number.c[5]) << 16) | 00406 (((uint64_t) number.c[6]) << 8) | 00407 (((uint64_t) number.c[7]) << 0); 00408 #else 00409 #error "Unknown byte order" 00410 #endif 00411 }
uint64_t ntohll | ( | uint64_t | net64 | ) |
Definition at line 363 of file strcompat.c.
Referenced by dump_versioned_codec(), and iax_parse_ies().
00364 { 00365 #if BYTE_ORDER == BIG_ENDIAN 00366 return net64; 00367 #elif BYTE_ORDER == LITTLE_ENDIAN 00368 union { 00369 unsigned char c[8]; 00370 uint64_t u; 00371 } number; 00372 number.u = net64; 00373 return 00374 (((uint64_t) number.c[0]) << 56) | 00375 (((uint64_t) number.c[1]) << 48) | 00376 (((uint64_t) number.c[2]) << 40) | 00377 (((uint64_t) number.c[3]) << 32) | 00378 (((uint64_t) number.c[4]) << 24) | 00379 (((uint64_t) number.c[5]) << 16) | 00380 (((uint64_t) number.c[6]) << 8) | 00381 (((uint64_t) number.c[7]) << 0); 00382 #else 00383 #error "Unknown byte order" 00384 #endif 00385 }