Wed Jan 8 2020 09:50:21

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.

.. and lots of other functions too.

Definition in file strcompat.c.

Function Documentation

void closefrom ( int  n)

Definition at line 426 of file strcompat.c.

References errno.

Referenced by ast_close_fds_above_n().

427 {
428  long x;
429  struct rlimit rl;
430  DIR *dir;
431  char path[16], *result;
432  struct dirent *entry;
433 
434  snprintf(path, sizeof(path), "/proc/%d/fd", (int) getpid());
435  if ((dir = opendir(path))) {
436  while ((entry = readdir(dir))) {
437  /* Skip . and .. */
438  if (entry->d_name[0] == '.') {
439  continue;
440  }
441  if ((x = strtol(entry->d_name, &result, 10)) && x >= n) {
442 #ifdef STRICT_COMPAT
443  close(x);
444 #else
445  /* This isn't strictly compatible, but it's actually faster
446  * for our purposes to set the CLOEXEC flag than to close
447  * file descriptors.
448  */
449  long flags = fcntl(x, F_GETFD);
450  if (flags == -1 && errno == EBADF) {
451  continue;
452  }
453  fcntl(x, F_SETFD, flags | FD_CLOEXEC);
454 #endif
455  }
456  }
457  closedir(dir);
458  } else {
459  getrlimit(RLIMIT_NOFILE, &rl);
460  if (rl.rlim_cur > 65535) {
461  /* A more reasonable value. Consider that the primary source of
462  * file descriptors in Asterisk are UDP sockets, of which we are
463  * limited to 65,535 per address. We additionally limit that down
464  * to about 10,000 sockets per protocol. While the kernel will
465  * allow us to set the fileno limit higher (up to 4.2 billion),
466  * there really is no practical reason for it to be that high.
467  */
468  rl.rlim_cur = 65535;
469  }
470  for (x = n; x < rl.rlim_cur; x++) {
471 #ifdef STRICT_COMPAT
472  close(x);
473 #else
474  long flags = fcntl(x, F_GETFD);
475  if (flags == -1 && errno == EBADF) {
476  continue;
477  }
478  fcntl(x, F_SETFD, flags | FD_CLOEXEC);
479 #endif
480  }
481  }
482 }
int errno
uint64_t htonll ( uint64_t  host64)

Definition at line 387 of file strcompat.c.

Referenced by iax_ie_append_versioned_uint64().

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

Definition at line 361 of file strcompat.c.

Referenced by dump_versioned_codec(), and iax_parse_ies().

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