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