Asterisk - The Open Source Telephony Project  18.5.0
Macros | Functions
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"
Include dependency graph for strcompat.c:

Go to the source code of this file.

Macros

#define ASTMM_LIBC   ASTMM_IGNORE
 
#define POLL_SIZE   1024
 

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.

.. and lots of other functions too.

Definition in file strcompat.c.

Macro Definition Documentation

◆ ASTMM_LIBC

#define ASTMM_LIBC   ASTMM_IGNORE

Definition at line 28 of file strcompat.c.

◆ POLL_SIZE

#define POLL_SIZE   1024

Definition at line 41 of file strcompat.c.

Referenced by closefrom().

Function Documentation

◆ closefrom()

void closefrom ( int  n)

Definition at line 429 of file strcompat.c.

References ast_random(), errno, len(), mkdtemp(), NULL, POLL_SIZE, and roundf().

Referenced by ast_close_fds_above_n().

430 {
431  int maxfd;
432 #ifndef _SC_OPEN_MAX
433  struct rlimit rl;
434 #endif
435  struct pollfd fds[POLL_SIZE];
436  int fd=n, loopmax, i;
437 #ifndef STRICT_COMPAT
438  long flags;
439 #endif
440 
441 #ifndef _SC_OPEN_MAX
442  if (getrlimit(RLIMIT_NOFILE, &rl) == -1) {
443  maxfd = -1;
444  } else {
445  maxfd = rl.rlim_cur;
446  }
447 #else
448  maxfd = sysconf (_SC_OPEN_MAX);
449 #endif
450 
451  if (maxfd == -1 || maxfd > 65536) {
452  /* A more reasonable value. Consider that the primary source of
453  * file descriptors in Asterisk are UDP sockets, of which we are
454  * limited to 65,535 per address. We additionally limit that down
455  * to about 10,000 sockets per protocol. While the kernel will
456  * allow us to set the fileno limit higher (up to 4.2 billion),
457  * there really is no practical reason for it to be that high.
458  *
459  * sysconf as well as getrlimit can return -1 on error. Let's set
460  * maxfd to the mentioned reasonable value of 65,535 in this case.
461  */
462  maxfd = 65536;
463  }
464 
465  while (fd < maxfd) {
466  loopmax = maxfd - fd;
467  if (loopmax > POLL_SIZE) {
468  loopmax = POLL_SIZE;
469  }
470  for (i = 0; i < loopmax; i++) {
471  fds[i].fd = fd+i;
472  fds[i].events = 0;
473  }
474  poll(fds, loopmax, 0);
475  for (i = 0; i < loopmax; i++) {
476  if (fds[i].revents == POLLNVAL) {
477  continue;
478  }
479 #ifdef STRICT_COMPAT
480  close(fds[i].fd);
481 #else
482  /* This isn't strictly compatible, but it's actually faster
483  * for our purposes to set the CLOEXEC flag than to close
484  * file descriptors.
485  */
486  flags = fcntl(fds[i].fd, F_GETFD);
487  if (flags == -1 && errno == EBADF) {
488  continue;
489  }
490  fcntl(fds[i].fd, F_SETFD, flags | FD_CLOEXEC);
491 #endif
492  }
493  fd += loopmax;
494  }
495 }
#define POLL_SIZE
Definition: strcompat.c:41
int errno

◆ htonll()

uint64_t htonll ( uint64_t  host64)

Definition at line 390 of file strcompat.c.

References c, and ffsll().

Referenced by ast_websocket_write(), and iax_ie_append_versioned_uint64().

391 {
392 #if BYTE_ORDER == BIG_ENDIAN
393  return host64;
394 #elif BYTE_ORDER == LITTLE_ENDIAN
395  union {
396  unsigned char c[8];
397  uint64_t u;
398  } number;
399  number.u = host64;
400  return
401  (((uint64_t) number.c[0]) << 56) |
402  (((uint64_t) number.c[1]) << 48) |
403  (((uint64_t) number.c[2]) << 40) |
404  (((uint64_t) number.c[3]) << 32) |
405  (((uint64_t) number.c[4]) << 24) |
406  (((uint64_t) number.c[5]) << 16) |
407  (((uint64_t) number.c[6]) << 8) |
408  (((uint64_t) number.c[7]) << 0);
409 #else
410  #error "Unknown byte order"
411 #endif
412 }
static struct test_val c
Number structure.
Definition: app_followme.c:154

◆ ntohll()

uint64_t ntohll ( uint64_t  net64)

Definition at line 364 of file strcompat.c.

References c.

Referenced by ast_websocket_read(), dump_versioned_codec(), and iax_parse_ies().

365 {
366 #if BYTE_ORDER == BIG_ENDIAN
367  return net64;
368 #elif BYTE_ORDER == LITTLE_ENDIAN
369  union {
370  unsigned char c[8];
371  uint64_t u;
372  } number;
373  number.u = net64;
374  return
375  (((uint64_t) number.c[0]) << 56) |
376  (((uint64_t) number.c[1]) << 48) |
377  (((uint64_t) number.c[2]) << 40) |
378  (((uint64_t) number.c[3]) << 32) |
379  (((uint64_t) number.c[4]) << 24) |
380  (((uint64_t) number.c[5]) << 16) |
381  (((uint64_t) number.c[6]) << 8) |
382  (((uint64_t) number.c[7]) << 0);
383 #else
384  #error "Unknown byte order"
385 #endif
386 }
static struct test_val c
Number structure.
Definition: app_followme.c:154