Mon Jun 27 16:51:21 2011

Asterisk developer's documentation


strcompat.c File Reference

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)


Detailed Description

Compatibility functions for strsep and strtoq missing on Solaris.

Definition in file strcompat.c.


Function Documentation

void closefrom ( int  n  ) 

Definition at line 424 of file strcompat.c.

References errno.

Referenced by ast_close_fds_above_n().

00425 {
00426    long x;
00427    struct rlimit rl;
00428    DIR *dir;
00429    char path[16], *result;
00430    struct dirent *entry;
00431 
00432    snprintf(path, sizeof(path), "/proc/%d/fd", (int) getpid());
00433    if ((dir = opendir(path))) {
00434       while ((entry = readdir(dir))) {
00435          /* Skip . and .. */
00436          if (entry->d_name[0] == '.') {
00437             continue;
00438          }
00439          if ((x = strtol(entry->d_name, &result, 10)) && x >= n) {
00440 #ifdef STRICT_COMPAT
00441             close(x);
00442 #else
00443             /* This isn't strictly compatible, but it's actually faster
00444              * for our purposes to set the CLOEXEC flag than to close
00445              * file descriptors.
00446              */
00447             long flags = fcntl(x, F_GETFD);
00448             if (flags == -1 && errno == EBADF) {
00449                continue;
00450             }
00451             fcntl(x, F_SETFD, flags | FD_CLOEXEC);
00452 #endif
00453          }
00454       }
00455       closedir(dir);
00456    } else {
00457       getrlimit(RLIMIT_NOFILE, &rl);
00458       if (rl.rlim_cur > 65535) {
00459          /* A more reasonable value.  Consider that the primary source of
00460           * file descriptors in Asterisk are UDP sockets, of which we are
00461           * limited to 65,535 per address.  We additionally limit that down
00462           * to about 10,000 sockets per protocol.  While the kernel will
00463           * allow us to set the fileno limit higher (up to 4.2 billion),
00464           * there really is no practical reason for it to be that high.
00465           */
00466          rl.rlim_cur = 65535;
00467       }
00468       for (x = n; x < rl.rlim_cur; x++) {
00469 #ifdef STRICT_COMPAT
00470          close(x);
00471 #else
00472          long flags = fcntl(x, F_GETFD);
00473          if (flags == -1 && errno == EBADF) {
00474             continue;
00475          }
00476          fcntl(x, F_SETFD, flags | FD_CLOEXEC);
00477 #endif
00478       }
00479    }
00480 }

uint64_t htonll ( uint64_t  host64  ) 

Definition at line 385 of file strcompat.c.

Referenced by iax_ie_append_versioned_uint64().

00386 {
00387 #if BYTE_ORDER == BIG_ENDIAN
00388    return host64;
00389 #elif BYTE_ORDER == LITTLE_ENDIAN
00390    union {
00391       unsigned char c[8];
00392       uint64_t u;
00393    } number;
00394    number.u = host64;
00395    return
00396       (((uint64_t) number.c[0]) << 56) |
00397       (((uint64_t) number.c[1]) << 48) |
00398       (((uint64_t) number.c[2]) << 40) |
00399       (((uint64_t) number.c[3]) << 32) |
00400       (((uint64_t) number.c[4]) << 24) |
00401       (((uint64_t) number.c[5]) << 16) |
00402       (((uint64_t) number.c[6]) <<  8) |
00403       (((uint64_t) number.c[7]) <<  0);
00404 #else
00405    #error "Unknown byte order"
00406 #endif
00407 }

uint64_t ntohll ( uint64_t  net64  ) 

Definition at line 359 of file strcompat.c.

Referenced by dump_versioned_codec(), and iax_parse_ies().

00360 {
00361 #if BYTE_ORDER == BIG_ENDIAN
00362    return net64;
00363 #elif BYTE_ORDER == LITTLE_ENDIAN
00364    union {
00365       unsigned char c[8];
00366       uint64_t u;
00367    } number;
00368    number.u = net64;
00369    return
00370       (((uint64_t) number.c[0]) << 56) |
00371       (((uint64_t) number.c[1]) << 48) |
00372       (((uint64_t) number.c[2]) << 40) |
00373       (((uint64_t) number.c[3]) << 32) |
00374       (((uint64_t) number.c[4]) << 24) |
00375       (((uint64_t) number.c[5]) << 16) |
00376       (((uint64_t) number.c[6]) <<  8) |
00377       (((uint64_t) number.c[7]) <<  0);
00378 #else
00379    #error "Unknown byte order"
00380 #endif
00381 }


Generated on Mon Jun 27 16:51:21 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7