2004-05-09 08:22:15 +00:00
|
|
|
/*
|
2005-09-14 20:46:50 +00:00
|
|
|
* Asterisk -- An open source telephony toolkit.
|
2004-05-09 08:22:15 +00:00
|
|
|
*
|
2006-01-04 21:54:09 +00:00
|
|
|
* Copyright (C) 1999 - 2006, Digium, Inc.
|
2004-05-09 08:22:15 +00:00
|
|
|
*
|
2005-09-14 20:46:50 +00:00
|
|
|
* See http://www.asterisk.org for more information about
|
|
|
|
* the Asterisk project. Please do not directly contact
|
|
|
|
* any of the maintainers of this project for assistance;
|
|
|
|
* the project provides a web site, mailing lists and IRC
|
|
|
|
* channels for your use.
|
2004-05-09 08:22:15 +00:00
|
|
|
*
|
|
|
|
* This program is free software, distributed under the terms of
|
2005-09-14 20:46:50 +00:00
|
|
|
* the GNU General Public License Version 2. See the LICENSE file
|
|
|
|
* at the top of the source tree.
|
|
|
|
*/
|
|
|
|
|
2005-10-24 20:12:06 +00:00
|
|
|
/*! \file
|
2005-09-14 20:46:50 +00:00
|
|
|
*
|
2005-10-24 20:12:06 +00:00
|
|
|
* \brief Utility functions
|
2005-09-14 20:46:50 +00:00
|
|
|
*
|
2005-10-24 20:12:06 +00:00
|
|
|
* \note These are important for portability and security,
|
|
|
|
* so please use them in favour of other routines.
|
|
|
|
* Please consult the CODING GUIDELINES for more information.
|
2004-05-09 08:22:15 +00:00
|
|
|
*/
|
|
|
|
|
2012-06-15 16:20:16 +00:00
|
|
|
/*** MODULEINFO
|
|
|
|
<support_level>core</support_level>
|
|
|
|
***/
|
|
|
|
|
2006-06-07 18:54:56 +00:00
|
|
|
#include "asterisk.h"
|
|
|
|
|
|
|
|
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
|
|
|
|
2004-05-11 18:41:09 +00:00
|
|
|
#include <ctype.h>
|
2007-06-22 04:35:12 +00:00
|
|
|
#include <sys/stat.h>
|
2010-12-18 00:08:13 +00:00
|
|
|
#include <sys/stat.h>
|
2005-04-22 13:11:34 +00:00
|
|
|
|
2007-07-21 14:39:52 +00:00
|
|
|
#ifdef HAVE_DEV_URANDOM
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
|
2010-12-12 03:58:33 +00:00
|
|
|
#include <sys/syscall.h>
|
|
|
|
#if defined(__APPLE__)
|
|
|
|
#include <mach/mach.h>
|
|
|
|
#elif defined(HAVE_SYS_THR_H)
|
|
|
|
#include <sys/thr.h>
|
|
|
|
#endif
|
|
|
|
|
2007-11-17 14:11:53 +00:00
|
|
|
#include "asterisk/network.h"
|
|
|
|
|
2006-03-30 23:26:22 +00:00
|
|
|
#define AST_API_MODULE /* ensure that inlinable API functions will be built in lock.h if required */
|
2005-04-21 06:02:45 +00:00
|
|
|
#include "asterisk/lock.h"
|
|
|
|
#include "asterisk/io.h"
|
|
|
|
#include "asterisk/md5.h"
|
2006-02-03 22:37:29 +00:00
|
|
|
#include "asterisk/sha1.h"
|
2007-08-03 19:41:42 +00:00
|
|
|
#include "asterisk/cli.h"
|
|
|
|
#include "asterisk/linkedlists.h"
|
2004-05-09 08:22:15 +00:00
|
|
|
|
2005-06-24 22:45:15 +00:00
|
|
|
#define AST_API_MODULE /* ensure that inlinable API functions will be built in this module if required */
|
|
|
|
#include "asterisk/strings.h"
|
|
|
|
|
|
|
|
#define AST_API_MODULE /* ensure that inlinable API functions will be built in this module if required */
|
|
|
|
#include "asterisk/time.h"
|
|
|
|
|
2006-01-04 21:54:09 +00:00
|
|
|
#define AST_API_MODULE /* ensure that inlinable API functions will be built in this module if required */
|
|
|
|
#include "asterisk/stringfields.h"
|
|
|
|
|
2005-06-23 23:07:55 +00:00
|
|
|
#define AST_API_MODULE /* ensure that inlinable API functions will be built in this module if required */
|
|
|
|
#include "asterisk/utils.h"
|
|
|
|
|
Merge team/russell/ast_verbose_threadstorage
- instead of defining a free() wrapper in a bunch of files, define it as
ast_free() in utils.h and remove the copies from all the files.
- centralize and abstract the code used for doing thread storage. The code
lives in threadstorage.h, with one function being implemented in utils.c.
This new API includes generic thread storage as well as special functions
for handling thread local dynamic length string buffers.
- update ast_inet_ntoa() to use the new threadstorage API
- update ast_state2str() to use the new threadstorage API
- update ast_cli() to use the new threadstorage API
- Modify manager_event() to use thread storage. Instead of using a buffer of
4096 characters as the workspace for building the manager event, use a thread
local dynamic string. Now there is no length limitation on the length of the
body of a manager event.
- Significantly simplify the handling of ast_verbose() ...
- Instead of using a static char buffer and a lock to make sure only one
thread can be using ast_verbose() at a time, use a thread local dynamic
string as the workspace for preparing the verbose message. Instead of
locking around the entire function, the only locking done now is when the
message has been built and is being deliviered to the list of registered
verbose message handlers.
- This function was doing a strdup() on every message passed to it and
keeping a queue of the last 200 messages in memory. This has been
completely removed. The only place this was used was that if there were
any messages in the verbose queue when a verbose handler was registered,
all of the messages in the queue would be fed to it. So, I just made sure
that the console verbose handler and the network verbose handler (for
remote asterisk consoles) were registered before any verbose messages.
pbx_gtkconsole and pbx_kdeconsole will now lose a few verbose messages at
startup, but I didn't feel the performance hit of this message queue was
worth saving the initial verbose output for these very rarely used modules.
- I have removed the last three arguments to the verbose handlers, leaving
only the string itself because they aren't needed anymore. For example,
ast_verbose had some logic for telling the verbose handler to add
a newline if the buffer was completely full. Now that the buffer can grow
as needed, this doesn't matter anymore.
- remove unused function, ast_verbose_dmesg() which was to dispatch the
message queue
- Convert the list of verbose handlers to use the linked list macros.
- add missing newline characters to a few ast_verbose() calls
- convert the list of log channels to use the linked list macros in logger.c
- fix close_logger() to close all of the files it opened for logging
- update ast_log() to use a thread local dynamic string for its workspace
for preparing log messages instead of a buffer of size BUFSIZ (8kB on my
system) allocated on the stack. The dynamic string in this case is limited
to only growing to a maximum size of BUFSIZ.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@39272 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2006-08-08 06:32:04 +00:00
|
|
|
#define AST_API_MODULE
|
|
|
|
#include "asterisk/threadstorage.h"
|
|
|
|
|
2008-06-09 22:51:59 +00:00
|
|
|
#define AST_API_MODULE
|
|
|
|
#include "asterisk/config.h"
|
|
|
|
|
2004-06-25 04:07:52 +00:00
|
|
|
static char base64[64];
|
|
|
|
static char b2a[256];
|
|
|
|
|
2006-10-19 01:00:57 +00:00
|
|
|
AST_THREADSTORAGE(inet_ntoa_buf);
|
2006-07-27 01:49:57 +00:00
|
|
|
|
2007-08-01 17:27:09 +00:00
|
|
|
#if !defined(HAVE_GETHOSTBYNAME_R_5) && !defined(HAVE_GETHOSTBYNAME_R_6)
|
2004-05-09 08:22:15 +00:00
|
|
|
|
2006-03-07 17:55:33 +00:00
|
|
|
#define ERANGE 34 /*!< duh? ERANGE value copied from web... */
|
2004-05-09 08:22:15 +00:00
|
|
|
#undef gethostbyname
|
|
|
|
|
2004-06-09 01:45:08 +00:00
|
|
|
AST_MUTEX_DEFINE_STATIC(__mutex);
|
|
|
|
|
2006-06-01 14:18:12 +00:00
|
|
|
/*! \brief Reentrant replacement for gethostbyname for BSD-based systems.
|
2006-03-07 17:55:33 +00:00
|
|
|
\note This
|
2012-03-22 19:51:16 +00:00
|
|
|
routine is derived from code originally written and placed in the public
|
2005-05-09 13:50:38 +00:00
|
|
|
domain by Enzo Michelangeli <em@em.no-ip.com> */
|
|
|
|
|
2004-06-22 17:42:14 +00:00
|
|
|
static int gethostbyname_r (const char *name, struct hostent *ret, char *buf,
|
2012-03-22 19:51:16 +00:00
|
|
|
size_t buflen, struct hostent **result,
|
|
|
|
int *h_errnop)
|
2004-05-09 08:22:15 +00:00
|
|
|
{
|
|
|
|
int hsave;
|
|
|
|
struct hostent *ph;
|
|
|
|
ast_mutex_lock(&__mutex); /* begin critical area */
|
|
|
|
hsave = h_errno;
|
|
|
|
|
|
|
|
ph = gethostbyname(name);
|
|
|
|
*h_errnop = h_errno; /* copy h_errno to *h_herrnop */
|
|
|
|
if (ph == NULL) {
|
|
|
|
*result = NULL;
|
|
|
|
} else {
|
|
|
|
char **p, **q;
|
|
|
|
char *pbuf;
|
2008-02-11 18:27:47 +00:00
|
|
|
int nbytes = 0;
|
|
|
|
int naddr = 0, naliases = 0;
|
2004-05-09 08:22:15 +00:00
|
|
|
/* determine if we have enough space in buf */
|
|
|
|
|
|
|
|
/* count how many addresses */
|
|
|
|
for (p = ph->h_addr_list; *p != 0; p++) {
|
|
|
|
nbytes += ph->h_length; /* addresses */
|
|
|
|
nbytes += sizeof(*p); /* pointers */
|
|
|
|
naddr++;
|
|
|
|
}
|
|
|
|
nbytes += sizeof(*p); /* one more for the terminating NULL */
|
|
|
|
|
|
|
|
/* count how many aliases, and total length of strings */
|
|
|
|
for (p = ph->h_aliases; *p != 0; p++) {
|
|
|
|
nbytes += (strlen(*p)+1); /* aliases */
|
|
|
|
nbytes += sizeof(*p); /* pointers */
|
|
|
|
naliases++;
|
|
|
|
}
|
|
|
|
nbytes += sizeof(*p); /* one more for the terminating NULL */
|
|
|
|
|
|
|
|
/* here nbytes is the number of bytes required in buffer */
|
|
|
|
/* as a terminator must be there, the minimum value is ph->h_length */
|
2006-07-10 19:05:48 +00:00
|
|
|
if (nbytes > buflen) {
|
2004-05-09 08:22:15 +00:00
|
|
|
*result = NULL;
|
|
|
|
ast_mutex_unlock(&__mutex); /* end critical area */
|
|
|
|
return ERANGE; /* not enough space in buf!! */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* There is enough space. Now we need to do a deep copy! */
|
|
|
|
/* Allocation in buffer:
|
|
|
|
from [0] to [(naddr-1) * sizeof(*p)]:
|
|
|
|
pointers to addresses
|
|
|
|
at [naddr * sizeof(*p)]:
|
|
|
|
NULL
|
|
|
|
from [(naddr+1) * sizeof(*p)] to [(naddr+naliases) * sizeof(*p)] :
|
|
|
|
pointers to aliases
|
|
|
|
at [(naddr+naliases+1) * sizeof(*p)]:
|
|
|
|
NULL
|
|
|
|
then naddr addresses (fixed length), and naliases aliases (asciiz).
|
|
|
|
*/
|
|
|
|
|
|
|
|
*ret = *ph; /* copy whole structure (not its address!) */
|
|
|
|
|
|
|
|
/* copy addresses */
|
|
|
|
q = (char **)buf; /* pointer to pointers area (type: char **) */
|
|
|
|
ret->h_addr_list = q; /* update pointer to address list */
|
2006-07-10 19:05:48 +00:00
|
|
|
pbuf = buf + ((naddr + naliases + 2) * sizeof(*p)); /* skip that area */
|
2004-05-09 08:22:15 +00:00
|
|
|
for (p = ph->h_addr_list; *p != 0; p++) {
|
|
|
|
memcpy(pbuf, *p, ph->h_length); /* copy address bytes */
|
|
|
|
*q++ = pbuf; /* the pointer is the one inside buf... */
|
|
|
|
pbuf += ph->h_length; /* advance pbuf */
|
|
|
|
}
|
|
|
|
*q++ = NULL; /* address list terminator */
|
|
|
|
|
|
|
|
/* copy aliases */
|
|
|
|
ret->h_aliases = q; /* update pointer to aliases list */
|
|
|
|
for (p = ph->h_aliases; *p != 0; p++) {
|
|
|
|
strcpy(pbuf, *p); /* copy alias strings */
|
|
|
|
*q++ = pbuf; /* the pointer is the one inside buf... */
|
|
|
|
pbuf += strlen(*p); /* advance pbuf */
|
|
|
|
*pbuf++ = 0; /* string terminator */
|
|
|
|
}
|
|
|
|
*q++ = NULL; /* terminator */
|
|
|
|
|
|
|
|
strcpy(pbuf, ph->h_name); /* copy alias strings */
|
|
|
|
ret->h_name = pbuf;
|
|
|
|
pbuf += strlen(ph->h_name); /* advance pbuf */
|
|
|
|
*pbuf++ = 0; /* string terminator */
|
|
|
|
|
|
|
|
*result = ret; /* and let *result point to structure */
|
|
|
|
|
|
|
|
}
|
|
|
|
h_errno = hsave; /* restore h_errno */
|
|
|
|
ast_mutex_unlock(&__mutex); /* end critical area */
|
|
|
|
|
2004-05-15 05:02:42 +00:00
|
|
|
return (*result == NULL); /* return 0 on success, non-zero on error */
|
2004-05-09 08:22:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2012-03-22 19:51:16 +00:00
|
|
|
/*! \brief Re-entrant (thread safe) version of gethostbyname that replaces the
|
2005-04-06 21:12:32 +00:00
|
|
|
standard gethostbyname (which is not thread safe)
|
2004-06-29 17:54:25 +00:00
|
|
|
*/
|
2004-05-09 08:22:15 +00:00
|
|
|
struct hostent *ast_gethostbyname(const char *host, struct ast_hostent *hp)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
int herrno;
|
2008-02-11 18:27:47 +00:00
|
|
|
int dots = 0;
|
2004-05-11 18:41:09 +00:00
|
|
|
const char *s;
|
2004-05-09 08:22:15 +00:00
|
|
|
struct hostent *result = NULL;
|
2004-05-11 18:41:09 +00:00
|
|
|
/* Although it is perfectly legitimate to lookup a pure integer, for
|
|
|
|
the sake of the sanity of people who like to name their peers as
|
|
|
|
integers, we break with tradition and refuse to look up a
|
|
|
|
pure integer */
|
|
|
|
s = host;
|
2005-03-17 23:12:15 +00:00
|
|
|
res = 0;
|
2007-01-23 00:11:32 +00:00
|
|
|
while (s && *s) {
|
2005-10-21 04:09:23 +00:00
|
|
|
if (*s == '.')
|
|
|
|
dots++;
|
|
|
|
else if (!isdigit(*s))
|
2004-05-11 18:41:09 +00:00
|
|
|
break;
|
|
|
|
s++;
|
|
|
|
}
|
2005-10-21 04:09:23 +00:00
|
|
|
if (!s || !*s) {
|
|
|
|
/* Forge a reply for IP's to avoid octal IP's being interpreted as octal */
|
|
|
|
if (dots != 3)
|
|
|
|
return NULL;
|
2005-10-21 14:09:03 +00:00
|
|
|
memset(hp, 0, sizeof(struct ast_hostent));
|
2006-10-31 06:21:40 +00:00
|
|
|
hp->hp.h_addrtype = AF_INET;
|
2005-10-31 15:21:12 +00:00
|
|
|
hp->hp.h_addr_list = (void *) hp->buf;
|
2005-10-21 14:09:03 +00:00
|
|
|
hp->hp.h_addr = hp->buf + sizeof(void *);
|
2011-01-19 17:59:18 +00:00
|
|
|
/* For AF_INET, this will always be 4 */
|
|
|
|
hp->hp.h_length = 4;
|
2005-10-21 04:09:23 +00:00
|
|
|
if (inet_pton(AF_INET, host, hp->hp.h_addr) > 0)
|
|
|
|
return &hp->hp;
|
2004-05-11 18:41:09 +00:00
|
|
|
return NULL;
|
2012-03-22 19:51:16 +00:00
|
|
|
|
2005-10-21 04:09:23 +00:00
|
|
|
}
|
2007-08-01 17:53:09 +00:00
|
|
|
#ifdef HAVE_GETHOSTBYNAME_R_5
|
2004-12-14 23:36:30 +00:00
|
|
|
result = gethostbyname_r(host, &hp->hp, hp->buf, sizeof(hp->buf), &herrno);
|
|
|
|
|
|
|
|
if (!result || !hp->hp.h_addr_list || !hp->hp.h_addr_list[0])
|
|
|
|
return NULL;
|
|
|
|
#else
|
2004-05-09 08:22:15 +00:00
|
|
|
res = gethostbyname_r(host, &hp->hp, hp->buf, sizeof(hp->buf), &result, &herrno);
|
|
|
|
|
2004-05-13 21:54:30 +00:00
|
|
|
if (res || !result || !hp->hp.h_addr_list || !hp->hp.h_addr_list[0])
|
2004-05-09 08:22:15 +00:00
|
|
|
return NULL;
|
2004-12-14 23:36:30 +00:00
|
|
|
#endif
|
2004-05-09 08:22:15 +00:00
|
|
|
return &hp->hp;
|
|
|
|
}
|
2004-06-22 17:42:14 +00:00
|
|
|
|
2006-03-07 17:55:33 +00:00
|
|
|
/*! \brief Produce 32 char MD5 hash of value. */
|
2009-05-21 21:13:09 +00:00
|
|
|
void ast_md5_hash(char *output, const char *input)
|
2005-02-18 05:29:31 +00:00
|
|
|
{
|
2005-05-04 04:05:34 +00:00
|
|
|
struct MD5Context md5;
|
|
|
|
unsigned char digest[16];
|
|
|
|
char *ptr;
|
|
|
|
int x;
|
|
|
|
|
|
|
|
MD5Init(&md5);
|
2009-05-21 21:13:09 +00:00
|
|
|
MD5Update(&md5, (const unsigned char *) input, strlen(input));
|
2005-05-04 04:05:34 +00:00
|
|
|
MD5Final(digest, &md5);
|
|
|
|
ptr = output;
|
2006-07-10 19:05:48 +00:00
|
|
|
for (x = 0; x < 16; x++)
|
2005-05-04 04:05:34 +00:00
|
|
|
ptr += sprintf(ptr, "%2.2x", digest[x]);
|
2005-02-18 05:29:31 +00:00
|
|
|
}
|
|
|
|
|
2006-03-07 17:55:33 +00:00
|
|
|
/*! \brief Produce 40 char SHA1 hash of value. */
|
2009-05-21 21:13:09 +00:00
|
|
|
void ast_sha1_hash(char *output, const char *input)
|
2006-02-03 22:37:29 +00:00
|
|
|
{
|
|
|
|
struct SHA1Context sha;
|
|
|
|
char *ptr;
|
|
|
|
int x;
|
|
|
|
uint8_t Message_Digest[20];
|
|
|
|
|
|
|
|
SHA1Reset(&sha);
|
2012-03-22 19:51:16 +00:00
|
|
|
|
2006-02-03 22:37:29 +00:00
|
|
|
SHA1Input(&sha, (const unsigned char *) input, strlen(input));
|
|
|
|
|
|
|
|
SHA1Result(&sha, Message_Digest);
|
|
|
|
ptr = output;
|
|
|
|
for (x = 0; x < 20; x++)
|
|
|
|
ptr += sprintf(ptr, "%2.2x", Message_Digest[x]);
|
|
|
|
}
|
|
|
|
|
2012-06-02 21:13:36 +00:00
|
|
|
/*! \brief Produce a 20 byte SHA1 hash of value. */
|
|
|
|
void ast_sha1_hash_uint(uint8_t *digest, const char *input)
|
|
|
|
{
|
|
|
|
struct SHA1Context sha;
|
|
|
|
|
|
|
|
SHA1Reset(&sha);
|
|
|
|
|
|
|
|
SHA1Input(&sha, (const unsigned char *) input, strlen(input));
|
|
|
|
|
|
|
|
SHA1Result(&sha, digest);
|
|
|
|
}
|
|
|
|
|
2006-03-07 17:55:33 +00:00
|
|
|
/*! \brief decode BASE64 encoded text */
|
2005-10-16 03:10:33 +00:00
|
|
|
int ast_base64decode(unsigned char *dst, const char *src, int max)
|
2004-06-25 04:07:52 +00:00
|
|
|
{
|
|
|
|
int cnt = 0;
|
|
|
|
unsigned int byte = 0;
|
|
|
|
unsigned int bits = 0;
|
|
|
|
int incnt = 0;
|
2009-11-06 19:47:11 +00:00
|
|
|
while(*src && *src != '=' && (cnt < max)) {
|
2004-06-25 04:07:52 +00:00
|
|
|
/* Shift in 6 bits of input */
|
|
|
|
byte <<= 6;
|
|
|
|
byte |= (b2a[(int)(*src)]) & 0x3f;
|
|
|
|
bits += 6;
|
|
|
|
src++;
|
|
|
|
incnt++;
|
2012-03-22 19:51:16 +00:00
|
|
|
/* If we have at least 8 bits left over, take that character
|
2004-06-25 04:07:52 +00:00
|
|
|
off the top */
|
|
|
|
if (bits >= 8) {
|
|
|
|
bits -= 8;
|
|
|
|
*dst = (byte >> bits) & 0xff;
|
|
|
|
dst++;
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
}
|
2010-07-02 15:57:02 +00:00
|
|
|
/* Don't worry about left over bits, they're extra anyway */
|
2004-06-25 04:07:52 +00:00
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
2006-03-07 17:55:33 +00:00
|
|
|
/*! \brief encode text to BASE64 coding */
|
2006-06-01 19:05:45 +00:00
|
|
|
int ast_base64encode_full(char *dst, const unsigned char *src, int srclen, int max, int linebreaks)
|
2004-06-25 04:07:52 +00:00
|
|
|
{
|
|
|
|
int cnt = 0;
|
2006-06-01 19:05:45 +00:00
|
|
|
int col = 0;
|
2004-06-25 04:07:52 +00:00
|
|
|
unsigned int byte = 0;
|
|
|
|
int bits = 0;
|
|
|
|
int cntin = 0;
|
2006-06-01 19:05:45 +00:00
|
|
|
/* Reserve space for null byte at end of string */
|
2004-06-25 04:07:52 +00:00
|
|
|
max--;
|
2006-07-10 19:05:48 +00:00
|
|
|
while ((cntin < srclen) && (cnt < max)) {
|
2004-06-25 04:07:52 +00:00
|
|
|
byte <<= 8;
|
|
|
|
byte |= *(src++);
|
|
|
|
bits += 8;
|
|
|
|
cntin++;
|
2006-08-25 17:48:34 +00:00
|
|
|
if ((bits == 24) && (cnt + 4 <= max)) {
|
2006-06-01 19:05:45 +00:00
|
|
|
*dst++ = base64[(byte >> 18) & 0x3f];
|
|
|
|
*dst++ = base64[(byte >> 12) & 0x3f];
|
|
|
|
*dst++ = base64[(byte >> 6) & 0x3f];
|
|
|
|
*dst++ = base64[byte & 0x3f];
|
|
|
|
cnt += 4;
|
|
|
|
col += 4;
|
|
|
|
bits = 0;
|
|
|
|
byte = 0;
|
|
|
|
}
|
|
|
|
if (linebreaks && (cnt < max) && (col == 64)) {
|
|
|
|
*dst++ = '\n';
|
2004-06-25 04:07:52 +00:00
|
|
|
cnt++;
|
2006-06-01 19:05:45 +00:00
|
|
|
col = 0;
|
2004-06-25 04:07:52 +00:00
|
|
|
}
|
|
|
|
}
|
2006-08-25 17:48:34 +00:00
|
|
|
if (bits && (cnt + 4 <= max)) {
|
2012-03-22 19:51:16 +00:00
|
|
|
/* Add one last character for the remaining bits,
|
2004-06-25 04:07:52 +00:00
|
|
|
padding the rest with 0 */
|
2006-06-01 19:05:45 +00:00
|
|
|
byte <<= 24 - bits;
|
|
|
|
*dst++ = base64[(byte >> 18) & 0x3f];
|
|
|
|
*dst++ = base64[(byte >> 12) & 0x3f];
|
|
|
|
if (bits == 16)
|
|
|
|
*dst++ = base64[(byte >> 6) & 0x3f];
|
|
|
|
else
|
|
|
|
*dst++ = '=';
|
|
|
|
*dst++ = '=';
|
|
|
|
cnt += 4;
|
|
|
|
}
|
|
|
|
if (linebreaks && (cnt < max)) {
|
|
|
|
*dst++ = '\n';
|
2004-06-25 04:07:52 +00:00
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
*dst = '\0';
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
2006-06-01 19:05:45 +00:00
|
|
|
int ast_base64encode(char *dst, const unsigned char *src, int srclen, int max)
|
|
|
|
{
|
|
|
|
return ast_base64encode_full(dst, src, srclen, max, 0);
|
|
|
|
}
|
|
|
|
|
2004-06-25 04:07:52 +00:00
|
|
|
static void base64_init(void)
|
|
|
|
{
|
|
|
|
int x;
|
|
|
|
memset(b2a, -1, sizeof(b2a));
|
|
|
|
/* Initialize base-64 Conversion table */
|
2006-07-10 19:05:48 +00:00
|
|
|
for (x = 0; x < 26; x++) {
|
2004-06-25 04:07:52 +00:00
|
|
|
/* A-Z */
|
|
|
|
base64[x] = 'A' + x;
|
|
|
|
b2a['A' + x] = x;
|
|
|
|
/* a-z */
|
|
|
|
base64[x + 26] = 'a' + x;
|
|
|
|
b2a['a' + x] = x + 26;
|
|
|
|
/* 0-9 */
|
|
|
|
if (x < 10) {
|
|
|
|
base64[x + 52] = '0' + x;
|
|
|
|
b2a['0' + x] = x + 52;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
base64[62] = '+';
|
|
|
|
base64[63] = '/';
|
|
|
|
b2a[(int)'+'] = 62;
|
|
|
|
b2a[(int)'/'] = 63;
|
|
|
|
}
|
|
|
|
|
2011-01-24 18:59:22 +00:00
|
|
|
const struct ast_flags ast_uri_http = {AST_URI_UNRESERVED};
|
|
|
|
const struct ast_flags ast_uri_http_legacy = {AST_URI_LEGACY_SPACE | AST_URI_UNRESERVED};
|
|
|
|
const struct ast_flags ast_uri_sip_user = {AST_URI_UNRESERVED | AST_URI_SIP_USER_UNRESERVED};
|
|
|
|
|
|
|
|
char *ast_uri_encode(const char *string, char *outbuf, int buflen, struct ast_flags spec)
|
2005-08-29 22:39:39 +00:00
|
|
|
{
|
RFC compliant uri and display-name encode/decode
1. URI Encoding
This patch changes ast_uri_encode()'s behavior when doreserved is enabled.
Previously when doreserved was enabled only a small set of reserved
characters were encoded. This set was comprised primarily of the reserved
characters defined in RFC3261 section 25.1, but contained other characters as
well. Rather than only escaping the reserved set, doreserved now escapes
all characters not within the unreserved set as defined by RFC 3261 and
RFC 2396. Also, the 'doreserved' variable has been renamed to 'do_special_char'
in attempts to avoid confusion.
When doreserve is not enabled, the previous logic of only encoding the
characters <= 0X1F and > 0X7f remains, except for the '%' character, which
must always be encoded as it signifies a HEX escaped character during the decode
process.
2. URI Decoding: Break up URI before decode.
In chan_sip.c ast_uri_decode is called on the entire URI instead of it's
individual parts after it is parsed. This is not good as ast_uri_decode
can introduce special characters back into the URI which can mess up parsing.
This patch resolves this by not decoding a URI until parsing is completely
done. There are many instances where we check to see if pedantic checking
is enabled before we decode a URI. In these cases a new macro,
SIP_PEDANTIC_DECODE, is used on the individual parsed segments of the URI
rather than constantly putting if (pedantic) { decode() } checks everywhere
in the code. In the areas where ast_uri_decode is not dependent upon
pedantic checking this macro is not used, but decoding is still moved to
each individual part of the URI. The only behavior that should change from
this patch is the time at which decoding occurs.
Since I had to look over every place URI parsing occurs to create this
patch, I found several places where we use duplicate code for parsing.
To consolidate the code, those areas have updated to use the parse_uri()
function where possible.
3. SIP display-name decoding according to RFC3261 section 25.
To properly decode the display-name portion of a FROM header, chan_sip's
get_calleridname() function required a complete re-write. More information
about this change can be found in the comments at the beginning of this function.
4. Unit Tests.
Unit tests for ast_uri_encode, ast_uri_decode, and get_calleridname() have been
written. This involved the addition of the test_utils.c file for testing the
utils api.
(closes issue #16299)
Reported by: wdoekes
Patches:
astsvn-16299-get_calleridname.diff uploaded by wdoekes (license 717)
get_calleridname_rewrite.diff uploaded by dvossel (license 671)
Tested by: wdoekes, dvossel, Nick_Lewis
Review: https://reviewboard.asterisk.org/r/469/
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@243200 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2010-01-26 16:30:08 +00:00
|
|
|
const char *ptr = string; /* Start with the string */
|
2011-01-11 18:55:16 +00:00
|
|
|
char *out = outbuf;
|
RFC compliant uri and display-name encode/decode
1. URI Encoding
This patch changes ast_uri_encode()'s behavior when doreserved is enabled.
Previously when doreserved was enabled only a small set of reserved
characters were encoded. This set was comprised primarily of the reserved
characters defined in RFC3261 section 25.1, but contained other characters as
well. Rather than only escaping the reserved set, doreserved now escapes
all characters not within the unreserved set as defined by RFC 3261 and
RFC 2396. Also, the 'doreserved' variable has been renamed to 'do_special_char'
in attempts to avoid confusion.
When doreserve is not enabled, the previous logic of only encoding the
characters <= 0X1F and > 0X7f remains, except for the '%' character, which
must always be encoded as it signifies a HEX escaped character during the decode
process.
2. URI Decoding: Break up URI before decode.
In chan_sip.c ast_uri_decode is called on the entire URI instead of it's
individual parts after it is parsed. This is not good as ast_uri_decode
can introduce special characters back into the URI which can mess up parsing.
This patch resolves this by not decoding a URI until parsing is completely
done. There are many instances where we check to see if pedantic checking
is enabled before we decode a URI. In these cases a new macro,
SIP_PEDANTIC_DECODE, is used on the individual parsed segments of the URI
rather than constantly putting if (pedantic) { decode() } checks everywhere
in the code. In the areas where ast_uri_decode is not dependent upon
pedantic checking this macro is not used, but decoding is still moved to
each individual part of the URI. The only behavior that should change from
this patch is the time at which decoding occurs.
Since I had to look over every place URI parsing occurs to create this
patch, I found several places where we use duplicate code for parsing.
To consolidate the code, those areas have updated to use the parse_uri()
function where possible.
3. SIP display-name decoding according to RFC3261 section 25.
To properly decode the display-name portion of a FROM header, chan_sip's
get_calleridname() function required a complete re-write. More information
about this change can be found in the comments at the beginning of this function.
4. Unit Tests.
Unit tests for ast_uri_encode, ast_uri_decode, and get_calleridname() have been
written. This involved the addition of the test_utils.c file for testing the
utils api.
(closes issue #16299)
Reported by: wdoekes
Patches:
astsvn-16299-get_calleridname.diff uploaded by wdoekes (license 717)
get_calleridname_rewrite.diff uploaded by dvossel (license 671)
Tested by: wdoekes, dvossel, Nick_Lewis
Review: https://reviewboard.asterisk.org/r/469/
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@243200 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2010-01-26 16:30:08 +00:00
|
|
|
const char *mark = "-_.!~*'()"; /* no encode set, RFC 2396 section 2.3, RFC 3261 sec 25 */
|
2011-01-24 18:59:22 +00:00
|
|
|
const char *user_unreserved = "&=+$,;?/"; /* user-unreserved set, RFC 3261 sec 25 */
|
2005-08-29 22:39:39 +00:00
|
|
|
|
2011-01-11 18:55:16 +00:00
|
|
|
while (*ptr && out - outbuf < buflen - 1) {
|
2011-01-24 18:59:22 +00:00
|
|
|
if (ast_test_flag(&spec, AST_URI_LEGACY_SPACE) && *ptr == ' ') {
|
|
|
|
/* for legacy encoding, encode spaces as '+' */
|
|
|
|
*out = '+';
|
|
|
|
out++;
|
|
|
|
} else if (!(ast_test_flag(&spec, AST_URI_MARK)
|
|
|
|
&& strchr(mark, *ptr))
|
|
|
|
&& !(ast_test_flag(&spec, AST_URI_ALPHANUM)
|
|
|
|
&& ((*ptr >= '0' && *ptr <= '9')
|
|
|
|
|| (*ptr >= 'A' && *ptr <= 'Z')
|
|
|
|
|| (*ptr >= 'a' && *ptr <= 'z')))
|
|
|
|
&& !(ast_test_flag(&spec, AST_URI_SIP_USER_UNRESERVED)
|
|
|
|
&& strchr(user_unreserved, *ptr))) {
|
|
|
|
|
2011-01-11 18:55:16 +00:00
|
|
|
if (out - outbuf >= buflen - 3) {
|
|
|
|
break;
|
2005-08-29 22:39:39 +00:00
|
|
|
}
|
RFC compliant uri and display-name encode/decode
1. URI Encoding
This patch changes ast_uri_encode()'s behavior when doreserved is enabled.
Previously when doreserved was enabled only a small set of reserved
characters were encoded. This set was comprised primarily of the reserved
characters defined in RFC3261 section 25.1, but contained other characters as
well. Rather than only escaping the reserved set, doreserved now escapes
all characters not within the unreserved set as defined by RFC 3261 and
RFC 2396. Also, the 'doreserved' variable has been renamed to 'do_special_char'
in attempts to avoid confusion.
When doreserve is not enabled, the previous logic of only encoding the
characters <= 0X1F and > 0X7f remains, except for the '%' character, which
must always be encoded as it signifies a HEX escaped character during the decode
process.
2. URI Decoding: Break up URI before decode.
In chan_sip.c ast_uri_decode is called on the entire URI instead of it's
individual parts after it is parsed. This is not good as ast_uri_decode
can introduce special characters back into the URI which can mess up parsing.
This patch resolves this by not decoding a URI until parsing is completely
done. There are many instances where we check to see if pedantic checking
is enabled before we decode a URI. In these cases a new macro,
SIP_PEDANTIC_DECODE, is used on the individual parsed segments of the URI
rather than constantly putting if (pedantic) { decode() } checks everywhere
in the code. In the areas where ast_uri_decode is not dependent upon
pedantic checking this macro is not used, but decoding is still moved to
each individual part of the URI. The only behavior that should change from
this patch is the time at which decoding occurs.
Since I had to look over every place URI parsing occurs to create this
patch, I found several places where we use duplicate code for parsing.
To consolidate the code, those areas have updated to use the parse_uri()
function where possible.
3. SIP display-name decoding according to RFC3261 section 25.
To properly decode the display-name portion of a FROM header, chan_sip's
get_calleridname() function required a complete re-write. More information
about this change can be found in the comments at the beginning of this function.
4. Unit Tests.
Unit tests for ast_uri_encode, ast_uri_decode, and get_calleridname() have been
written. This involved the addition of the test_utils.c file for testing the
utils api.
(closes issue #16299)
Reported by: wdoekes
Patches:
astsvn-16299-get_calleridname.diff uploaded by wdoekes (license 717)
get_calleridname_rewrite.diff uploaded by dvossel (license 671)
Tested by: wdoekes, dvossel, Nick_Lewis
Review: https://reviewboard.asterisk.org/r/469/
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@243200 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2010-01-26 16:30:08 +00:00
|
|
|
out += sprintf(out, "%%%02X", (unsigned char) *ptr);
|
2011-01-11 18:55:16 +00:00
|
|
|
} else {
|
2005-08-29 22:39:39 +00:00
|
|
|
*out = *ptr; /* Continue copying the string */
|
|
|
|
out++;
|
RFC compliant uri and display-name encode/decode
1. URI Encoding
This patch changes ast_uri_encode()'s behavior when doreserved is enabled.
Previously when doreserved was enabled only a small set of reserved
characters were encoded. This set was comprised primarily of the reserved
characters defined in RFC3261 section 25.1, but contained other characters as
well. Rather than only escaping the reserved set, doreserved now escapes
all characters not within the unreserved set as defined by RFC 3261 and
RFC 2396. Also, the 'doreserved' variable has been renamed to 'do_special_char'
in attempts to avoid confusion.
When doreserve is not enabled, the previous logic of only encoding the
characters <= 0X1F and > 0X7f remains, except for the '%' character, which
must always be encoded as it signifies a HEX escaped character during the decode
process.
2. URI Decoding: Break up URI before decode.
In chan_sip.c ast_uri_decode is called on the entire URI instead of it's
individual parts after it is parsed. This is not good as ast_uri_decode
can introduce special characters back into the URI which can mess up parsing.
This patch resolves this by not decoding a URI until parsing is completely
done. There are many instances where we check to see if pedantic checking
is enabled before we decode a URI. In these cases a new macro,
SIP_PEDANTIC_DECODE, is used on the individual parsed segments of the URI
rather than constantly putting if (pedantic) { decode() } checks everywhere
in the code. In the areas where ast_uri_decode is not dependent upon
pedantic checking this macro is not used, but decoding is still moved to
each individual part of the URI. The only behavior that should change from
this patch is the time at which decoding occurs.
Since I had to look over every place URI parsing occurs to create this
patch, I found several places where we use duplicate code for parsing.
To consolidate the code, those areas have updated to use the parse_uri()
function where possible.
3. SIP display-name decoding according to RFC3261 section 25.
To properly decode the display-name portion of a FROM header, chan_sip's
get_calleridname() function required a complete re-write. More information
about this change can be found in the comments at the beginning of this function.
4. Unit Tests.
Unit tests for ast_uri_encode, ast_uri_decode, and get_calleridname() have been
written. This involved the addition of the test_utils.c file for testing the
utils api.
(closes issue #16299)
Reported by: wdoekes
Patches:
astsvn-16299-get_calleridname.diff uploaded by wdoekes (license 717)
get_calleridname_rewrite.diff uploaded by dvossel (license 671)
Tested by: wdoekes, dvossel, Nick_Lewis
Review: https://reviewboard.asterisk.org/r/469/
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@243200 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2010-01-26 16:30:08 +00:00
|
|
|
}
|
2005-08-29 22:39:39 +00:00
|
|
|
ptr++;
|
|
|
|
}
|
2011-01-11 18:55:16 +00:00
|
|
|
|
|
|
|
if (buflen) {
|
2005-08-29 22:39:39 +00:00
|
|
|
*out = '\0';
|
2011-01-11 18:55:16 +00:00
|
|
|
}
|
|
|
|
|
2005-08-29 22:39:39 +00:00
|
|
|
return outbuf;
|
|
|
|
}
|
|
|
|
|
2011-01-24 18:59:22 +00:00
|
|
|
void ast_uri_decode(char *s, struct ast_flags spec)
|
2005-08-29 22:39:39 +00:00
|
|
|
{
|
|
|
|
char *o;
|
|
|
|
unsigned int tmp;
|
|
|
|
|
|
|
|
for (o = s; *s; s++, o++) {
|
2011-01-24 18:59:22 +00:00
|
|
|
if (ast_test_flag(&spec, AST_URI_LEGACY_SPACE) && *s == '+') {
|
|
|
|
/* legacy mode, decode '+' as space */
|
|
|
|
*o = ' ';
|
|
|
|
} else if (*s == '%' && s[1] != '\0' && s[2] != '\0' && sscanf(s + 1, "%2x", &tmp) == 1) {
|
2005-08-29 22:39:39 +00:00
|
|
|
/* have '%', two chars and correct parsing */
|
|
|
|
*o = tmp;
|
|
|
|
s += 2; /* Will be incremented once more when we break out */
|
|
|
|
} else /* all other cases, just copy */
|
|
|
|
*o = *s;
|
|
|
|
}
|
|
|
|
*o = '\0';
|
|
|
|
}
|
|
|
|
|
2011-01-24 18:59:22 +00:00
|
|
|
char *ast_escape_quoted(const char *string, char *outbuf, int buflen)
|
|
|
|
{
|
|
|
|
const char *ptr = string;
|
|
|
|
char *out = outbuf;
|
|
|
|
char *allow = "\t\v !"; /* allow LWS (minus \r and \n) and "!" */
|
|
|
|
|
|
|
|
while (*ptr && out - outbuf < buflen - 1) {
|
|
|
|
if (!(strchr(allow, *ptr))
|
|
|
|
&& !(*ptr >= '#' && *ptr <= '[') /* %x23 - %x5b */
|
|
|
|
&& !(*ptr >= ']' && *ptr <= '~') /* %x5d - %x7e */
|
2011-01-25 15:52:42 +00:00
|
|
|
&& !((unsigned char) *ptr > 0x7f)) { /* UTF8-nonascii */
|
2011-01-24 18:59:22 +00:00
|
|
|
|
|
|
|
if (out - outbuf >= buflen - 2) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
out += sprintf(out, "\\%c", (unsigned char) *ptr);
|
|
|
|
} else {
|
|
|
|
*out = *ptr;
|
|
|
|
out++;
|
|
|
|
}
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buflen) {
|
|
|
|
*out = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return outbuf;
|
|
|
|
}
|
2005-10-26 13:03:17 +00:00
|
|
|
/*! \brief ast_inet_ntoa: Recursive thread safe replacement of inet_ntoa */
|
2006-07-21 17:31:28 +00:00
|
|
|
const char *ast_inet_ntoa(struct in_addr ia)
|
2004-06-29 12:56:46 +00:00
|
|
|
{
|
2006-07-21 17:31:28 +00:00
|
|
|
char *buf;
|
|
|
|
|
Merge team/russell/ast_verbose_threadstorage
- instead of defining a free() wrapper in a bunch of files, define it as
ast_free() in utils.h and remove the copies from all the files.
- centralize and abstract the code used for doing thread storage. The code
lives in threadstorage.h, with one function being implemented in utils.c.
This new API includes generic thread storage as well as special functions
for handling thread local dynamic length string buffers.
- update ast_inet_ntoa() to use the new threadstorage API
- update ast_state2str() to use the new threadstorage API
- update ast_cli() to use the new threadstorage API
- Modify manager_event() to use thread storage. Instead of using a buffer of
4096 characters as the workspace for building the manager event, use a thread
local dynamic string. Now there is no length limitation on the length of the
body of a manager event.
- Significantly simplify the handling of ast_verbose() ...
- Instead of using a static char buffer and a lock to make sure only one
thread can be using ast_verbose() at a time, use a thread local dynamic
string as the workspace for preparing the verbose message. Instead of
locking around the entire function, the only locking done now is when the
message has been built and is being deliviered to the list of registered
verbose message handlers.
- This function was doing a strdup() on every message passed to it and
keeping a queue of the last 200 messages in memory. This has been
completely removed. The only place this was used was that if there were
any messages in the verbose queue when a verbose handler was registered,
all of the messages in the queue would be fed to it. So, I just made sure
that the console verbose handler and the network verbose handler (for
remote asterisk consoles) were registered before any verbose messages.
pbx_gtkconsole and pbx_kdeconsole will now lose a few verbose messages at
startup, but I didn't feel the performance hit of this message queue was
worth saving the initial verbose output for these very rarely used modules.
- I have removed the last three arguments to the verbose handlers, leaving
only the string itself because they aren't needed anymore. For example,
ast_verbose had some logic for telling the verbose handler to add
a newline if the buffer was completely full. Now that the buffer can grow
as needed, this doesn't matter anymore.
- remove unused function, ast_verbose_dmesg() which was to dispatch the
message queue
- Convert the list of verbose handlers to use the linked list macros.
- add missing newline characters to a few ast_verbose() calls
- convert the list of log channels to use the linked list macros in logger.c
- fix close_logger() to close all of the files it opened for logging
- update ast_log() to use a thread local dynamic string for its workspace
for preparing log messages instead of a buffer of size BUFSIZ (8kB on my
system) allocated on the stack. The dynamic string in this case is limited
to only growing to a maximum size of BUFSIZ.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@39272 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2006-08-08 06:32:04 +00:00
|
|
|
if (!(buf = ast_threadstorage_get(&inet_ntoa_buf, INET_ADDRSTRLEN)))
|
|
|
|
return "";
|
2006-07-21 17:31:28 +00:00
|
|
|
|
|
|
|
return inet_ntop(AF_INET, &ia, buf, INET_ADDRSTRLEN);
|
2004-06-29 12:56:46 +00:00
|
|
|
}
|
2004-06-25 04:07:52 +00:00
|
|
|
|
2007-07-21 14:39:52 +00:00
|
|
|
#ifdef HAVE_DEV_URANDOM
|
|
|
|
static int dev_urandom_fd;
|
|
|
|
#endif
|
|
|
|
|
2005-03-08 20:47:30 +00:00
|
|
|
#ifndef __linux__
|
|
|
|
#undef pthread_create /* For ast_pthread_create function only */
|
2005-10-31 21:25:21 +00:00
|
|
|
#endif /* !__linux__ */
|
2005-08-29 22:39:39 +00:00
|
|
|
|
2006-10-04 21:10:36 +00:00
|
|
|
#if !defined(LOW_MEMORY)
|
2007-08-03 19:41:42 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_THREADS
|
|
|
|
|
|
|
|
/*! \brief A reasonable maximum number of locks a thread would be holding ... */
|
2008-01-08 20:56:38 +00:00
|
|
|
#define AST_MAX_LOCKS 64
|
2007-08-03 19:41:42 +00:00
|
|
|
|
|
|
|
/* Allow direct use of pthread_mutex_t and friends */
|
|
|
|
#undef pthread_mutex_t
|
|
|
|
#undef pthread_mutex_lock
|
|
|
|
#undef pthread_mutex_unlock
|
|
|
|
#undef pthread_mutex_init
|
|
|
|
#undef pthread_mutex_destroy
|
|
|
|
|
2012-03-22 19:51:16 +00:00
|
|
|
/*!
|
|
|
|
* \brief Keep track of which locks a thread holds
|
2007-08-03 19:41:42 +00:00
|
|
|
*
|
|
|
|
* There is an instance of this struct for every active thread
|
|
|
|
*/
|
|
|
|
struct thr_lock_info {
|
|
|
|
/*! The thread's ID */
|
|
|
|
pthread_t thread_id;
|
|
|
|
/*! The thread name which includes where the thread was started */
|
|
|
|
const char *thread_name;
|
|
|
|
/*! This is the actual container of info for what locks this thread holds */
|
|
|
|
struct {
|
|
|
|
const char *file;
|
|
|
|
int line_num;
|
|
|
|
const char *func;
|
|
|
|
const char *lock_name;
|
|
|
|
void *lock_addr;
|
|
|
|
int times_locked;
|
2007-10-01 21:15:57 +00:00
|
|
|
enum ast_lock_type type;
|
2007-08-03 19:41:42 +00:00
|
|
|
/*! This thread is waiting on this lock */
|
2007-10-09 22:21:49 +00:00
|
|
|
int pending:2;
|
2008-05-23 22:35:50 +00:00
|
|
|
#ifdef HAVE_BKTR
|
|
|
|
struct ast_bt *backtrace;
|
|
|
|
#endif
|
2007-08-03 19:41:42 +00:00
|
|
|
} locks[AST_MAX_LOCKS];
|
|
|
|
/*! This is the number of locks currently held by this thread.
|
|
|
|
* The index (num_locks - 1) has the info on the last one in the
|
|
|
|
* locks member */
|
|
|
|
unsigned int num_locks;
|
2012-03-22 19:51:16 +00:00
|
|
|
/*! Protects the contents of the locks member
|
2007-08-03 19:41:42 +00:00
|
|
|
* Intentionally not ast_mutex_t */
|
|
|
|
pthread_mutex_t lock;
|
|
|
|
AST_LIST_ENTRY(thr_lock_info) entry;
|
|
|
|
};
|
|
|
|
|
2012-03-22 19:51:16 +00:00
|
|
|
/*!
|
|
|
|
* \brief Locked when accessing the lock_infos list
|
2007-08-03 19:41:42 +00:00
|
|
|
*/
|
|
|
|
AST_MUTEX_DEFINE_STATIC(lock_infos_lock);
|
|
|
|
/*!
|
2012-03-22 19:51:16 +00:00
|
|
|
* \brief A list of each thread's lock info
|
2007-08-03 19:41:42 +00:00
|
|
|
*/
|
|
|
|
static AST_LIST_HEAD_NOLOCK_STATIC(lock_infos, thr_lock_info);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Destroy a thread's lock info
|
|
|
|
*
|
|
|
|
* This gets called automatically when the thread stops
|
|
|
|
*/
|
|
|
|
static void lock_info_destroy(void *data)
|
|
|
|
{
|
|
|
|
struct thr_lock_info *lock_info = data;
|
2008-06-19 19:48:26 +00:00
|
|
|
int i;
|
2007-08-03 19:41:42 +00:00
|
|
|
|
|
|
|
pthread_mutex_lock(&lock_infos_lock.mutex);
|
|
|
|
AST_LIST_REMOVE(&lock_infos, lock_info, entry);
|
|
|
|
pthread_mutex_unlock(&lock_infos_lock.mutex);
|
|
|
|
|
2008-06-19 19:48:26 +00:00
|
|
|
|
|
|
|
for (i = 0; i < lock_info->num_locks; i++) {
|
2008-12-16 21:39:15 +00:00
|
|
|
if (lock_info->locks[i].pending == -1) {
|
|
|
|
/* This just means that the last lock this thread went for was by
|
|
|
|
* using trylock, and it failed. This is fine. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-03-22 19:51:16 +00:00
|
|
|
ast_log(LOG_ERROR,
|
|
|
|
"Thread '%s' still has a lock! - '%s' (%p) from '%s' in %s:%d!\n",
|
2008-06-19 19:48:26 +00:00
|
|
|
lock_info->thread_name,
|
|
|
|
lock_info->locks[i].lock_name,
|
|
|
|
lock_info->locks[i].lock_addr,
|
|
|
|
lock_info->locks[i].func,
|
|
|
|
lock_info->locks[i].file,
|
|
|
|
lock_info->locks[i].line_num
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2007-08-03 19:41:42 +00:00
|
|
|
pthread_mutex_destroy(&lock_info->lock);
|
2007-11-01 06:12:08 +00:00
|
|
|
if (lock_info->thread_name)
|
|
|
|
free((void *) lock_info->thread_name);
|
2007-08-03 19:41:42 +00:00
|
|
|
free(lock_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief The thread storage key for per-thread lock info
|
|
|
|
*/
|
|
|
|
AST_THREADSTORAGE_CUSTOM(thread_lock_info, NULL, lock_info_destroy);
|
2008-05-23 22:35:50 +00:00
|
|
|
#ifdef HAVE_BKTR
|
|
|
|
void ast_store_lock_info(enum ast_lock_type type, const char *filename,
|
|
|
|
int line_num, const char *func, const char *lock_name, void *lock_addr, struct ast_bt *bt)
|
|
|
|
#else
|
2007-10-01 21:15:57 +00:00
|
|
|
void ast_store_lock_info(enum ast_lock_type type, const char *filename,
|
|
|
|
int line_num, const char *func, const char *lock_name, void *lock_addr)
|
2008-05-23 22:35:50 +00:00
|
|
|
#endif
|
2007-08-03 19:41:42 +00:00
|
|
|
{
|
|
|
|
struct thr_lock_info *lock_info;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!(lock_info = ast_threadstorage_get(&thread_lock_info, sizeof(*lock_info))))
|
|
|
|
return;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&lock_info->lock);
|
|
|
|
|
|
|
|
for (i = 0; i < lock_info->num_locks; i++) {
|
|
|
|
if (lock_info->locks[i].lock_addr == lock_addr) {
|
|
|
|
lock_info->locks[i].times_locked++;
|
2008-05-23 22:35:50 +00:00
|
|
|
#ifdef HAVE_BKTR
|
|
|
|
lock_info->locks[i].backtrace = bt;
|
|
|
|
#endif
|
2007-08-03 19:41:42 +00:00
|
|
|
pthread_mutex_unlock(&lock_info->lock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lock_info->num_locks == AST_MAX_LOCKS) {
|
|
|
|
/* Can't use ast_log here, because it will cause infinite recursion */
|
|
|
|
fprintf(stderr, "XXX ERROR XXX A thread holds more locks than '%d'."
|
|
|
|
" Increase AST_MAX_LOCKS!\n", AST_MAX_LOCKS);
|
|
|
|
pthread_mutex_unlock(&lock_info->lock);
|
|
|
|
return;
|
|
|
|
}
|
2008-02-25 23:19:31 +00:00
|
|
|
|
2008-03-19 04:06:31 +00:00
|
|
|
if (i && lock_info->locks[i - 1].pending == -1) {
|
2008-02-25 23:19:31 +00:00
|
|
|
/* The last lock on the list was one that this thread tried to lock but
|
|
|
|
* failed at doing so. It has now moved on to something else, so remove
|
|
|
|
* the old lock from the list. */
|
|
|
|
i--;
|
|
|
|
lock_info->num_locks--;
|
|
|
|
memset(&lock_info->locks[i], 0, sizeof(lock_info->locks[0]));
|
|
|
|
}
|
|
|
|
|
2007-08-03 19:41:42 +00:00
|
|
|
lock_info->locks[i].file = filename;
|
|
|
|
lock_info->locks[i].line_num = line_num;
|
|
|
|
lock_info->locks[i].func = func;
|
|
|
|
lock_info->locks[i].lock_name = lock_name;
|
|
|
|
lock_info->locks[i].lock_addr = lock_addr;
|
|
|
|
lock_info->locks[i].times_locked = 1;
|
2007-10-01 21:15:57 +00:00
|
|
|
lock_info->locks[i].type = type;
|
2007-08-03 19:41:42 +00:00
|
|
|
lock_info->locks[i].pending = 1;
|
2008-05-23 22:35:50 +00:00
|
|
|
#ifdef HAVE_BKTR
|
|
|
|
lock_info->locks[i].backtrace = bt;
|
|
|
|
#endif
|
2007-08-03 19:41:42 +00:00
|
|
|
lock_info->num_locks++;
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&lock_info->lock);
|
|
|
|
}
|
|
|
|
|
2008-02-28 22:39:26 +00:00
|
|
|
void ast_mark_lock_acquired(void *lock_addr)
|
2007-08-03 19:41:42 +00:00
|
|
|
{
|
|
|
|
struct thr_lock_info *lock_info;
|
|
|
|
|
|
|
|
if (!(lock_info = ast_threadstorage_get(&thread_lock_info, sizeof(*lock_info))))
|
|
|
|
return;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&lock_info->lock);
|
2008-02-28 22:39:26 +00:00
|
|
|
if (lock_info->locks[lock_info->num_locks - 1].lock_addr == lock_addr) {
|
|
|
|
lock_info->locks[lock_info->num_locks - 1].pending = 0;
|
|
|
|
}
|
2007-08-03 19:41:42 +00:00
|
|
|
pthread_mutex_unlock(&lock_info->lock);
|
|
|
|
}
|
|
|
|
|
2008-02-28 22:39:26 +00:00
|
|
|
void ast_mark_lock_failed(void *lock_addr)
|
2007-10-09 22:21:49 +00:00
|
|
|
{
|
|
|
|
struct thr_lock_info *lock_info;
|
|
|
|
|
|
|
|
if (!(lock_info = ast_threadstorage_get(&thread_lock_info, sizeof(*lock_info))))
|
|
|
|
return;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&lock_info->lock);
|
2008-02-28 22:39:26 +00:00
|
|
|
if (lock_info->locks[lock_info->num_locks - 1].lock_addr == lock_addr) {
|
|
|
|
lock_info->locks[lock_info->num_locks - 1].pending = -1;
|
|
|
|
lock_info->locks[lock_info->num_locks - 1].times_locked--;
|
|
|
|
}
|
2007-10-09 22:21:49 +00:00
|
|
|
pthread_mutex_unlock(&lock_info->lock);
|
|
|
|
}
|
2008-05-30 21:51:17 +00:00
|
|
|
|
2008-06-27 13:54:13 +00:00
|
|
|
int ast_find_lock_info(void *lock_addr, char *filename, size_t filename_size, int *lineno, char *func, size_t func_size, char *mutex_name, size_t mutex_name_size)
|
2008-05-29 17:35:19 +00:00
|
|
|
{
|
|
|
|
struct thr_lock_info *lock_info;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
if (!(lock_info = ast_threadstorage_get(&thread_lock_info, sizeof(*lock_info))))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&lock_info->lock);
|
|
|
|
|
|
|
|
for (i = lock_info->num_locks - 1; i >= 0; i--) {
|
|
|
|
if (lock_info->locks[i].lock_addr == lock_addr)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == -1) {
|
|
|
|
/* Lock not found :( */
|
|
|
|
pthread_mutex_unlock(&lock_info->lock);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-06-27 13:54:13 +00:00
|
|
|
ast_copy_string(filename, lock_info->locks[i].file, filename_size);
|
2008-05-29 17:35:19 +00:00
|
|
|
*lineno = lock_info->locks[i].line_num;
|
2008-06-27 13:54:13 +00:00
|
|
|
ast_copy_string(func, lock_info->locks[i].func, func_size);
|
|
|
|
ast_copy_string(mutex_name, lock_info->locks[i].lock_name, mutex_name_size);
|
2008-06-26 23:04:18 +00:00
|
|
|
|
|
|
|
pthread_mutex_unlock(&lock_info->lock);
|
|
|
|
|
2008-05-29 17:35:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-05-30 21:51:17 +00:00
|
|
|
#ifdef HAVE_BKTR
|
|
|
|
void ast_remove_lock_info(void *lock_addr, struct ast_bt *bt)
|
|
|
|
#else
|
2007-08-03 19:41:42 +00:00
|
|
|
void ast_remove_lock_info(void *lock_addr)
|
2008-05-23 22:35:50 +00:00
|
|
|
#endif
|
2007-08-03 19:41:42 +00:00
|
|
|
{
|
|
|
|
struct thr_lock_info *lock_info;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
if (!(lock_info = ast_threadstorage_get(&thread_lock_info, sizeof(*lock_info))))
|
|
|
|
return;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&lock_info->lock);
|
|
|
|
|
|
|
|
for (i = lock_info->num_locks - 1; i >= 0; i--) {
|
|
|
|
if (lock_info->locks[i].lock_addr == lock_addr)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == -1) {
|
|
|
|
/* Lock not found :( */
|
|
|
|
pthread_mutex_unlock(&lock_info->lock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lock_info->locks[i].times_locked > 1) {
|
|
|
|
lock_info->locks[i].times_locked--;
|
2008-05-23 22:35:50 +00:00
|
|
|
#ifdef HAVE_BKTR
|
|
|
|
lock_info->locks[i].backtrace = bt;
|
|
|
|
#endif
|
2007-08-03 19:41:42 +00:00
|
|
|
pthread_mutex_unlock(&lock_info->lock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i < lock_info->num_locks - 1) {
|
|
|
|
/* Not the last one ... *should* be rare! */
|
2012-03-22 19:51:16 +00:00
|
|
|
memmove(&lock_info->locks[i], &lock_info->locks[i + 1],
|
2007-08-03 19:41:42 +00:00
|
|
|
(lock_info->num_locks - (i + 1)) * sizeof(lock_info->locks[0]));
|
|
|
|
}
|
|
|
|
|
|
|
|
lock_info->num_locks--;
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&lock_info->lock);
|
|
|
|
}
|
|
|
|
|
2007-10-01 21:15:57 +00:00
|
|
|
static const char *locktype2str(enum ast_lock_type type)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case AST_MUTEX:
|
|
|
|
return "MUTEX";
|
|
|
|
case AST_RDLOCK:
|
|
|
|
return "RDLOCK";
|
|
|
|
case AST_WRLOCK:
|
|
|
|
return "WRLOCK";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "UNKNOWN";
|
|
|
|
}
|
|
|
|
|
2008-05-23 22:35:50 +00:00
|
|
|
#ifdef HAVE_BKTR
|
|
|
|
static void append_backtrace_information(struct ast_str **str, struct ast_bt *bt)
|
|
|
|
{
|
|
|
|
char **symbols;
|
2012-08-20 14:45:07 +00:00
|
|
|
int num_frames;
|
2008-05-23 22:35:50 +00:00
|
|
|
|
|
|
|
if (!bt) {
|
|
|
|
ast_str_append(str, 0, "\tNo backtrace to print\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-27 14:13:44 +00:00
|
|
|
/* store frame count locally to avoid the memory corruption that
|
|
|
|
* sometimes happens on virtualized CentOS 6.x systems */
|
2012-08-20 15:01:08 +00:00
|
|
|
num_frames = bt->num_frames;
|
2012-08-20 14:45:07 +00:00
|
|
|
if ((symbols = ast_bt_get_symbols(bt->addresses, num_frames))) {
|
2008-05-23 22:35:50 +00:00
|
|
|
int frame_iterator;
|
2012-03-22 19:51:16 +00:00
|
|
|
|
2012-08-20 14:45:07 +00:00
|
|
|
for (frame_iterator = 0; frame_iterator < num_frames; ++frame_iterator) {
|
2008-05-23 22:35:50 +00:00
|
|
|
ast_str_append(str, 0, "\t%s\n", symbols[frame_iterator]);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(symbols);
|
|
|
|
} else {
|
|
|
|
ast_str_append(str, 0, "\tCouldn't retrieve backtrace symbols\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-04-22 14:38:46 +00:00
|
|
|
static void append_lock_information(struct ast_str **str, struct thr_lock_info *lock_info, int i)
|
|
|
|
{
|
|
|
|
int j;
|
|
|
|
ast_mutex_t *lock;
|
2008-06-03 18:26:51 +00:00
|
|
|
struct ast_lock_track *lt;
|
2012-03-22 19:51:16 +00:00
|
|
|
|
|
|
|
ast_str_append(str, 0, "=== ---> %sLock #%d (%s): %s %d %s %s %p (%d)\n",
|
|
|
|
lock_info->locks[i].pending > 0 ? "Waiting for " :
|
2008-04-22 14:38:46 +00:00
|
|
|
lock_info->locks[i].pending < 0 ? "Tried and failed to get " : "", i,
|
2012-03-22 19:51:16 +00:00
|
|
|
lock_info->locks[i].file,
|
2008-04-22 14:38:46 +00:00
|
|
|
locktype2str(lock_info->locks[i].type),
|
|
|
|
lock_info->locks[i].line_num,
|
|
|
|
lock_info->locks[i].func, lock_info->locks[i].lock_name,
|
2012-03-22 19:51:16 +00:00
|
|
|
lock_info->locks[i].lock_addr,
|
2008-04-22 14:38:46 +00:00
|
|
|
lock_info->locks[i].times_locked);
|
2008-05-23 22:35:50 +00:00
|
|
|
#ifdef HAVE_BKTR
|
|
|
|
append_backtrace_information(str, lock_info->locks[i].backtrace);
|
|
|
|
#endif
|
2012-03-22 19:51:16 +00:00
|
|
|
|
2008-04-22 14:38:46 +00:00
|
|
|
if (!lock_info->locks[i].pending || lock_info->locks[i].pending == -1)
|
|
|
|
return;
|
2012-03-22 19:51:16 +00:00
|
|
|
|
2008-04-22 14:38:46 +00:00
|
|
|
/* We only have further details for mutexes right now */
|
|
|
|
if (lock_info->locks[i].type != AST_MUTEX)
|
|
|
|
return;
|
2012-03-22 19:51:16 +00:00
|
|
|
|
2008-04-22 14:38:46 +00:00
|
|
|
lock = lock_info->locks[i].lock_addr;
|
2011-01-31 06:50:49 +00:00
|
|
|
lt = lock->track;
|
2008-06-03 18:26:51 +00:00
|
|
|
ast_reentrancy_lock(lt);
|
|
|
|
for (j = 0; *str && j < lt->reentrancy; j++) {
|
2008-04-22 14:38:46 +00:00
|
|
|
ast_str_append(str, 0, "=== --- ---> Locked Here: %s line %d (%s)\n",
|
2008-06-03 18:26:51 +00:00
|
|
|
lt->file[j], lt->lineno[j], lt->func[j]);
|
2008-04-22 14:38:46 +00:00
|
|
|
}
|
2012-03-22 19:51:16 +00:00
|
|
|
ast_reentrancy_unlock(lt);
|
2008-04-22 14:38:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-22 19:51:16 +00:00
|
|
|
/*! This function can help you find highly temporal locks; locks that happen for a
|
2008-04-22 14:38:46 +00:00
|
|
|
short time, but at unexpected times, usually at times that create a deadlock,
|
|
|
|
Why is this thing locked right then? Who is locking it? Who am I fighting
|
2012-03-22 19:51:16 +00:00
|
|
|
with for this lock?
|
2008-04-22 14:38:46 +00:00
|
|
|
|
|
|
|
To answer such questions, just call this routine before you would normally try
|
|
|
|
to aquire a lock. It doesn't do anything if the lock is not acquired. If the
|
|
|
|
lock is taken, it will publish a line or two to the console via ast_log().
|
|
|
|
|
|
|
|
Sometimes, the lock message is pretty uninformative. For instance, you might
|
|
|
|
find that the lock is being aquired deep within the astobj2 code; this tells
|
|
|
|
you little about higher level routines that call the astobj2 routines.
|
|
|
|
But, using gdb, you can set a break at the ast_log below, and for that
|
|
|
|
breakpoint, you can set the commands:
|
|
|
|
where
|
|
|
|
cont
|
|
|
|
which will give a stack trace and continue. -- that aught to do the job!
|
|
|
|
|
|
|
|
*/
|
2008-04-16 20:54:41 +00:00
|
|
|
void log_show_lock(void *this_lock_addr)
|
|
|
|
{
|
|
|
|
struct thr_lock_info *lock_info;
|
2008-04-22 14:38:46 +00:00
|
|
|
struct ast_str *str;
|
|
|
|
|
|
|
|
if (!(str = ast_str_create(4096))) {
|
|
|
|
ast_log(LOG_NOTICE,"Could not create str\n");
|
|
|
|
return;
|
|
|
|
}
|
2012-03-22 19:51:16 +00:00
|
|
|
|
2008-04-16 20:54:41 +00:00
|
|
|
|
|
|
|
pthread_mutex_lock(&lock_infos_lock.mutex);
|
|
|
|
AST_LIST_TRAVERSE(&lock_infos, lock_info, entry) {
|
|
|
|
int i;
|
|
|
|
pthread_mutex_lock(&lock_info->lock);
|
|
|
|
for (i = 0; str && i < lock_info->num_locks; i++) {
|
2008-04-22 14:38:46 +00:00
|
|
|
/* ONLY show info about this particular lock, if
|
|
|
|
it's acquired... */
|
2008-04-16 20:54:41 +00:00
|
|
|
if (lock_info->locks[i].lock_addr == this_lock_addr) {
|
2008-04-22 14:38:46 +00:00
|
|
|
append_lock_information(&str, lock_info, i);
|
2008-12-15 15:21:23 +00:00
|
|
|
ast_log(LOG_NOTICE, "%s", ast_str_buffer(str));
|
2008-04-22 14:38:46 +00:00
|
|
|
break;
|
2008-04-16 20:54:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&lock_info->lock);
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&lock_infos_lock.mutex);
|
2008-04-22 14:38:46 +00:00
|
|
|
ast_free(str);
|
2008-04-16 20:54:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-03 19:41:42 +00:00
|
|
|
static char *handle_show_locks(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
|
|
|
|
{
|
|
|
|
struct thr_lock_info *lock_info;
|
2007-10-15 19:19:01 +00:00
|
|
|
struct ast_str *str;
|
|
|
|
|
|
|
|
if (!(str = ast_str_create(4096)))
|
|
|
|
return CLI_FAILURE;
|
2007-08-03 19:41:42 +00:00
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case CLI_INIT:
|
|
|
|
e->command = "core show locks";
|
|
|
|
e->usage =
|
|
|
|
"Usage: core show locks\n"
|
|
|
|
" This command is for lock debugging. It prints out which locks\n"
|
|
|
|
"are owned by each active thread.\n";
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
case CLI_GENERATE:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-22 19:51:16 +00:00
|
|
|
ast_str_append(&str, 0, "\n"
|
2007-08-03 19:41:42 +00:00
|
|
|
"=======================================================================\n"
|
|
|
|
"=== Currently Held Locks ==============================================\n"
|
|
|
|
"=======================================================================\n"
|
|
|
|
"===\n"
|
2008-04-16 20:54:41 +00:00
|
|
|
"=== <pending> <lock#> (<file>): <lock type> <line num> <function> <lock name> <lock addr> (times locked)\n"
|
2007-08-03 19:41:42 +00:00
|
|
|
"===\n");
|
|
|
|
|
2007-10-15 19:25:56 +00:00
|
|
|
if (!str)
|
|
|
|
return CLI_FAILURE;
|
|
|
|
|
2007-08-03 19:41:42 +00:00
|
|
|
pthread_mutex_lock(&lock_infos_lock.mutex);
|
|
|
|
AST_LIST_TRAVERSE(&lock_infos, lock_info, entry) {
|
|
|
|
int i;
|
2008-05-12 17:55:08 +00:00
|
|
|
if (lock_info->num_locks) {
|
2011-03-15 01:36:26 +00:00
|
|
|
ast_str_append(&str, 0, "=== Thread ID: 0x%lx (%s)\n", (long) lock_info->thread_id,
|
2008-05-12 17:55:08 +00:00
|
|
|
lock_info->thread_name);
|
|
|
|
pthread_mutex_lock(&lock_info->lock);
|
|
|
|
for (i = 0; str && i < lock_info->num_locks; i++) {
|
|
|
|
append_lock_information(&str, lock_info, i);
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&lock_info->lock);
|
|
|
|
if (!str)
|
|
|
|
break;
|
|
|
|
ast_str_append(&str, 0, "=== -------------------------------------------------------------------\n"
|
|
|
|
"===\n");
|
|
|
|
if (!str)
|
|
|
|
break;
|
2007-08-03 19:41:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&lock_infos_lock.mutex);
|
|
|
|
|
2007-10-15 19:25:56 +00:00
|
|
|
if (!str)
|
|
|
|
return CLI_FAILURE;
|
|
|
|
|
2007-10-15 19:19:01 +00:00
|
|
|
ast_str_append(&str, 0, "=======================================================================\n"
|
2007-08-03 19:41:42 +00:00
|
|
|
"\n");
|
|
|
|
|
2007-10-15 19:25:56 +00:00
|
|
|
if (!str)
|
|
|
|
return CLI_FAILURE;
|
|
|
|
|
2008-12-15 15:21:23 +00:00
|
|
|
ast_cli(a->fd, "%s", ast_str_buffer(str));
|
2007-10-15 19:19:01 +00:00
|
|
|
|
2007-12-12 20:05:13 +00:00
|
|
|
ast_free(str);
|
2007-10-15 19:19:01 +00:00
|
|
|
|
2007-10-15 19:25:56 +00:00
|
|
|
return CLI_SUCCESS;
|
2007-08-03 19:41:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct ast_cli_entry utils_cli[] = {
|
2007-10-22 20:05:18 +00:00
|
|
|
AST_CLI_DEFINE(handle_show_locks, "Show which locks are held by which thread"),
|
2007-08-03 19:41:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* DEBUG_THREADS */
|
|
|
|
|
2006-04-12 20:40:46 +00:00
|
|
|
/*
|
|
|
|
* support for 'show threads'. The start routine is wrapped by
|
|
|
|
* dummy_start(), so that ast_register_thread() and
|
|
|
|
* ast_unregister_thread() know the thread identifier.
|
|
|
|
*/
|
|
|
|
struct thr_arg {
|
|
|
|
void *(*start_routine)(void *);
|
|
|
|
void *data;
|
|
|
|
char *name;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* on OS/X, pthread_cleanup_push() and pthread_cleanup_pop()
|
|
|
|
* are odd macros which start and end a block, so they _must_ be
|
|
|
|
* used in pairs (the latter with a '1' argument to call the
|
|
|
|
* handler on exit.
|
2006-10-04 19:51:38 +00:00
|
|
|
* On BSD we don't need this, but we keep it for compatibility.
|
2006-04-12 20:40:46 +00:00
|
|
|
*/
|
|
|
|
static void *dummy_start(void *data)
|
2004-08-08 17:15:02 +00:00
|
|
|
{
|
2006-04-12 20:40:46 +00:00
|
|
|
void *ret;
|
2006-10-04 19:51:38 +00:00
|
|
|
struct thr_arg a = *((struct thr_arg *) data); /* make a local copy */
|
2007-08-03 19:41:42 +00:00
|
|
|
#ifdef DEBUG_THREADS
|
|
|
|
struct thr_lock_info *lock_info;
|
2007-12-07 21:25:03 +00:00
|
|
|
pthread_mutexattr_t mutex_attr;
|
2007-08-03 19:41:42 +00:00
|
|
|
|
|
|
|
if (!(lock_info = ast_threadstorage_get(&thread_lock_info, sizeof(*lock_info))))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
lock_info->thread_id = pthread_self();
|
|
|
|
lock_info->thread_name = strdup(a.name);
|
2007-12-07 21:25:03 +00:00
|
|
|
|
|
|
|
pthread_mutexattr_init(&mutex_attr);
|
|
|
|
pthread_mutexattr_settype(&mutex_attr, AST_MUTEX_KIND);
|
|
|
|
pthread_mutex_init(&lock_info->lock, &mutex_attr);
|
|
|
|
pthread_mutexattr_destroy(&mutex_attr);
|
2007-08-03 19:41:42 +00:00
|
|
|
|
|
|
|
pthread_mutex_lock(&lock_infos_lock.mutex); /* Intentionally not the wrapper */
|
|
|
|
AST_LIST_INSERT_TAIL(&lock_infos, lock_info, entry);
|
|
|
|
pthread_mutex_unlock(&lock_infos_lock.mutex); /* Intentionally not the wrapper */
|
|
|
|
#endif /* DEBUG_THREADS */
|
|
|
|
|
2012-11-18 20:27:45 +00:00
|
|
|
/* note that even though data->name is a pointer to allocated memory,
|
|
|
|
we are not freeing it here because ast_register_thread is going to
|
|
|
|
keep a copy of the pointer and then ast_unregister_thread will
|
|
|
|
free the memory
|
|
|
|
*/
|
|
|
|
ast_free(data);
|
|
|
|
ast_register_thread(a.name);
|
|
|
|
pthread_cleanup_push(ast_unregister_thread, (void *) pthread_self());
|
|
|
|
|
2006-04-12 20:40:46 +00:00
|
|
|
ret = a.start_routine(a.data);
|
2007-08-03 19:41:42 +00:00
|
|
|
|
2006-04-12 20:40:46 +00:00
|
|
|
pthread_cleanup_pop(1);
|
2006-10-04 19:51:38 +00:00
|
|
|
|
2006-04-12 20:40:46 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-10-04 21:10:36 +00:00
|
|
|
#endif /* !LOW_MEMORY */
|
|
|
|
|
2006-10-04 19:51:38 +00:00
|
|
|
int ast_pthread_create_stack(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *),
|
|
|
|
void *data, size_t stacksize, const char *file, const char *caller,
|
|
|
|
int line, const char *start_fn)
|
2006-04-12 20:40:46 +00:00
|
|
|
{
|
2006-10-04 21:10:36 +00:00
|
|
|
#if !defined(LOW_MEMORY)
|
2006-04-12 20:40:46 +00:00
|
|
|
struct thr_arg *a;
|
2006-10-04 21:10:36 +00:00
|
|
|
#endif
|
2006-04-12 20:40:46 +00:00
|
|
|
|
2004-08-08 17:15:02 +00:00
|
|
|
if (!attr) {
|
2012-07-31 20:21:43 +00:00
|
|
|
attr = ast_alloca(sizeof(*attr));
|
2006-10-04 19:51:38 +00:00
|
|
|
pthread_attr_init(attr);
|
2004-08-08 17:15:02 +00:00
|
|
|
}
|
2006-10-04 19:51:38 +00:00
|
|
|
|
2005-09-29 04:08:18 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
/* On Linux, pthread_attr_init() defaults to PTHREAD_EXPLICIT_SCHED,
|
|
|
|
which is kind of useless. Change this here to
|
|
|
|
PTHREAD_INHERIT_SCHED; that way the -p option to set realtime
|
|
|
|
priority will propagate down to new threads by default.
|
|
|
|
This does mean that callers cannot set a different priority using
|
|
|
|
PTHREAD_EXPLICIT_SCHED in the attr argument; instead they must set
|
|
|
|
the priority afterwards with pthread_setschedparam(). */
|
2006-10-04 19:51:38 +00:00
|
|
|
if ((errno = pthread_attr_setinheritsched(attr, PTHREAD_INHERIT_SCHED)))
|
|
|
|
ast_log(LOG_WARNING, "pthread_attr_setinheritsched: %s\n", strerror(errno));
|
2005-09-29 04:08:18 +00:00
|
|
|
#endif
|
|
|
|
|
2005-03-04 00:01:41 +00:00
|
|
|
if (!stacksize)
|
|
|
|
stacksize = AST_STACKSIZE;
|
2006-10-04 19:51:38 +00:00
|
|
|
|
|
|
|
if ((errno = pthread_attr_setstacksize(attr, stacksize ? stacksize : AST_STACKSIZE)))
|
|
|
|
ast_log(LOG_WARNING, "pthread_attr_setstacksize: %s\n", strerror(errno));
|
|
|
|
|
2006-10-04 21:10:36 +00:00
|
|
|
#if !defined(LOW_MEMORY)
|
2006-10-04 19:51:38 +00:00
|
|
|
if ((a = ast_malloc(sizeof(*a)))) {
|
2006-04-12 20:40:46 +00:00
|
|
|
a->start_routine = start_routine;
|
|
|
|
a->data = data;
|
|
|
|
start_routine = dummy_start;
|
2012-08-21 21:01:11 +00:00
|
|
|
if (ast_asprintf(&a->name, "%-20s started at [%5d] %s %s()",
|
2008-11-02 18:52:13 +00:00
|
|
|
start_fn, line, file, caller) < 0) {
|
|
|
|
a->name = NULL;
|
|
|
|
}
|
2006-04-12 20:40:46 +00:00
|
|
|
data = a;
|
|
|
|
}
|
2006-10-04 21:10:36 +00:00
|
|
|
#endif /* !LOW_MEMORY */
|
2006-10-04 19:51:38 +00:00
|
|
|
|
2004-08-08 17:15:02 +00:00
|
|
|
return pthread_create(thread, attr, start_routine, data); /* We're in ast_pthread_create, so it's okay */
|
|
|
|
}
|
2004-08-27 04:21:09 +00:00
|
|
|
|
2007-05-24 18:30:19 +00:00
|
|
|
|
|
|
|
int ast_pthread_create_detached_stack(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *),
|
|
|
|
void *data, size_t stacksize, const char *file, const char *caller,
|
|
|
|
int line, const char *start_fn)
|
|
|
|
{
|
|
|
|
unsigned char attr_destroy = 0;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
if (!attr) {
|
2012-07-31 20:21:43 +00:00
|
|
|
attr = ast_alloca(sizeof(*attr));
|
2007-05-24 18:30:19 +00:00
|
|
|
pthread_attr_init(attr);
|
|
|
|
attr_destroy = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((errno = pthread_attr_setdetachstate(attr, PTHREAD_CREATE_DETACHED)))
|
|
|
|
ast_log(LOG_WARNING, "pthread_attr_setdetachstate: %s\n", strerror(errno));
|
|
|
|
|
2012-03-22 19:51:16 +00:00
|
|
|
res = ast_pthread_create_stack(thread, attr, start_routine, data,
|
2007-05-24 18:30:19 +00:00
|
|
|
stacksize, file, caller, line, start_fn);
|
|
|
|
|
|
|
|
if (attr_destroy)
|
|
|
|
pthread_attr_destroy(attr);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2005-03-05 03:20:55 +00:00
|
|
|
int ast_wait_for_input(int fd, int ms)
|
|
|
|
{
|
|
|
|
struct pollfd pfd[1];
|
|
|
|
memset(pfd, 0, sizeof(pfd));
|
|
|
|
pfd[0].fd = fd;
|
|
|
|
pfd[0].events = POLLIN|POLLPRI;
|
Merged revisions 182810 via svnmerge from
https://origsvn.digium.com/svn/asterisk/branches/1.4
........
r182810 | russell | 2009-03-17 21:09:13 -0500 (Tue, 17 Mar 2009) | 44 lines
Fix cases where the internal poll() was not being used when it needed to be.
We have seen a number of problems caused by poll() not working properly on
Mac OSX. If you search around, you'll find a number of references to using
select() instead of poll() to work around these issues. In Asterisk, we've
had poll.c which implements poll() using select() internally. However, we
were still getting reports of problems.
vadim investigated a bit and realized that at least on his system, even
though we were compiling in poll.o, the system poll() was still being used.
So, the primary purpose of this patch is to ensure that we're using the
internal poll() when we want it to be used.
The changes are:
1) Remove logic for when internal poll should be used from the Makefile.
Instead, put it in the configure script. The logic in the configure
script is the same as it was in the Makefile. Ideally, we would have
a functionality test for the problem, but that's not actually possible,
since we would have to be able to run an application on the _target_
system to test poll() behavior.
2) Always include poll.o in the build, but it will be empty if AST_POLL_COMPAT
is not defined.
3) Change uses of poll() throughout the source tree to ast_poll(). I feel
that it is good practice to give the API call a new name when we are
changing its behavior and not using the system version directly in all cases.
So, normally, ast_poll() is just redefined to poll(). On systems where
AST_POLL_COMPAT is defined, ast_poll() is redefined to ast_internal_poll().
4) Change poll() in main/poll.c to be ast_internal_poll().
It's worth noting that any code that still uses poll() directly will work fine
(if they worked fine before). So, for example, out of tree modules that are
using poll() will not stop working or anything. However, for modules to work
properly on Mac OSX, ast_poll() needs to be used.
(closes issue #13404)
Reported by: agalbraith
Tested by: russell, vadim
http://reviewboard.digium.com/r/198/
........
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@182847 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2009-03-18 02:28:55 +00:00
|
|
|
return ast_poll(pfd, 1, ms);
|
2005-03-05 03:20:55 +00:00
|
|
|
}
|
|
|
|
|
2008-12-22 17:09:36 +00:00
|
|
|
static int ast_wait_for_output(int fd, int timeoutms)
|
|
|
|
{
|
|
|
|
struct pollfd pfd = {
|
|
|
|
.fd = fd,
|
|
|
|
.events = POLLOUT,
|
|
|
|
};
|
|
|
|
int res;
|
2008-12-22 17:29:10 +00:00
|
|
|
struct timeval start = ast_tvnow();
|
|
|
|
int elapsed = 0;
|
2008-12-22 17:09:36 +00:00
|
|
|
|
|
|
|
/* poll() until the fd is writable without blocking */
|
Merged revisions 182810 via svnmerge from
https://origsvn.digium.com/svn/asterisk/branches/1.4
........
r182810 | russell | 2009-03-17 21:09:13 -0500 (Tue, 17 Mar 2009) | 44 lines
Fix cases where the internal poll() was not being used when it needed to be.
We have seen a number of problems caused by poll() not working properly on
Mac OSX. If you search around, you'll find a number of references to using
select() instead of poll() to work around these issues. In Asterisk, we've
had poll.c which implements poll() using select() internally. However, we
were still getting reports of problems.
vadim investigated a bit and realized that at least on his system, even
though we were compiling in poll.o, the system poll() was still being used.
So, the primary purpose of this patch is to ensure that we're using the
internal poll() when we want it to be used.
The changes are:
1) Remove logic for when internal poll should be used from the Makefile.
Instead, put it in the configure script. The logic in the configure
script is the same as it was in the Makefile. Ideally, we would have
a functionality test for the problem, but that's not actually possible,
since we would have to be able to run an application on the _target_
system to test poll() behavior.
2) Always include poll.o in the build, but it will be empty if AST_POLL_COMPAT
is not defined.
3) Change uses of poll() throughout the source tree to ast_poll(). I feel
that it is good practice to give the API call a new name when we are
changing its behavior and not using the system version directly in all cases.
So, normally, ast_poll() is just redefined to poll(). On systems where
AST_POLL_COMPAT is defined, ast_poll() is redefined to ast_internal_poll().
4) Change poll() in main/poll.c to be ast_internal_poll().
It's worth noting that any code that still uses poll() directly will work fine
(if they worked fine before). So, for example, out of tree modules that are
using poll() will not stop working or anything. However, for modules to work
properly on Mac OSX, ast_poll() needs to be used.
(closes issue #13404)
Reported by: agalbraith
Tested by: russell, vadim
http://reviewboard.digium.com/r/198/
........
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@182847 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2009-03-18 02:28:55 +00:00
|
|
|
while ((res = ast_poll(&pfd, 1, timeoutms - elapsed)) <= 0) {
|
2008-12-22 17:09:36 +00:00
|
|
|
if (res == 0) {
|
|
|
|
/* timed out. */
|
2010-01-05 17:26:50 +00:00
|
|
|
#ifndef STANDALONE
|
2010-01-05 17:16:01 +00:00
|
|
|
ast_debug(1, "Timed out trying to write\n");
|
2010-01-05 17:26:50 +00:00
|
|
|
#endif
|
2008-12-22 17:09:36 +00:00
|
|
|
return -1;
|
|
|
|
} else if (res == -1) {
|
|
|
|
/* poll() returned an error, check to see if it was fatal */
|
|
|
|
|
|
|
|
if (errno == EINTR || errno == EAGAIN) {
|
2008-12-22 17:29:10 +00:00
|
|
|
elapsed = ast_tvdiff_ms(ast_tvnow(), start);
|
|
|
|
if (elapsed >= timeoutms) {
|
|
|
|
return -1;
|
|
|
|
}
|
2008-12-22 17:09:36 +00:00
|
|
|
/* This was an acceptable error, go back into poll() */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fatal error, bail. */
|
|
|
|
ast_log(LOG_ERROR, "poll returned error: %s\n", strerror(errno));
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2008-12-22 17:29:10 +00:00
|
|
|
elapsed = ast_tvdiff_ms(ast_tvnow(), start);
|
|
|
|
if (elapsed >= timeoutms) {
|
|
|
|
return -1;
|
|
|
|
}
|
2008-12-22 17:09:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-11-07 08:34:26 +00:00
|
|
|
/*!
|
|
|
|
* Try to write string, but wait no more than ms milliseconds before timing out.
|
|
|
|
*
|
|
|
|
* \note The code assumes that the file descriptor has NONBLOCK set,
|
|
|
|
* so there is only one system call made to do a write, unless we actually
|
|
|
|
* have a need to wait. This way, we get better performance.
|
|
|
|
* If the descriptor is blocking, all assumptions on the guaranteed
|
|
|
|
* detail do not apply anymore.
|
|
|
|
*/
|
2012-03-22 19:51:16 +00:00
|
|
|
int ast_carefulwrite(int fd, char *s, int len, int timeoutms)
|
2006-05-05 21:01:39 +00:00
|
|
|
{
|
2008-12-22 17:29:10 +00:00
|
|
|
struct timeval start = ast_tvnow();
|
2006-05-05 21:01:39 +00:00
|
|
|
int res = 0;
|
2008-12-22 17:29:10 +00:00
|
|
|
int elapsed = 0;
|
2008-12-18 21:44:47 +00:00
|
|
|
|
2006-05-05 21:01:39 +00:00
|
|
|
while (len) {
|
2008-12-22 17:29:10 +00:00
|
|
|
if (ast_wait_for_output(fd, timeoutms - elapsed)) {
|
2008-12-22 17:09:36 +00:00
|
|
|
return -1;
|
2008-12-18 21:44:47 +00:00
|
|
|
}
|
|
|
|
|
2006-05-05 21:01:39 +00:00
|
|
|
res = write(fd, s, len);
|
2008-12-18 21:44:47 +00:00
|
|
|
|
|
|
|
if (res < 0 && errno != EAGAIN && errno != EINTR) {
|
|
|
|
/* fatal error from write() */
|
|
|
|
ast_log(LOG_ERROR, "write() returned error: %s\n", strerror(errno));
|
2006-05-05 21:01:39 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2008-12-18 21:44:47 +00:00
|
|
|
|
|
|
|
if (res < 0) {
|
|
|
|
/* It was an acceptable error */
|
2006-05-05 21:01:39 +00:00
|
|
|
res = 0;
|
2008-12-18 21:44:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Update how much data we have left to write */
|
2006-05-05 21:01:39 +00:00
|
|
|
len -= res;
|
|
|
|
s += res;
|
|
|
|
res = 0;
|
2008-12-22 17:29:10 +00:00
|
|
|
|
|
|
|
elapsed = ast_tvdiff_ms(ast_tvnow(), start);
|
|
|
|
if (elapsed >= timeoutms) {
|
2012-03-22 19:51:16 +00:00
|
|
|
/* We've taken too long to write
|
2008-12-22 17:29:10 +00:00
|
|
|
* This is only an error condition if we haven't finished writing. */
|
|
|
|
res = len ? -1 : 0;
|
|
|
|
break;
|
|
|
|
}
|
2006-05-05 21:01:39 +00:00
|
|
|
}
|
2008-12-18 21:44:47 +00:00
|
|
|
|
2006-05-05 21:01:39 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2008-12-22 17:09:36 +00:00
|
|
|
int ast_careful_fwrite(FILE *f, int fd, const char *src, size_t len, int timeoutms)
|
|
|
|
{
|
|
|
|
struct timeval start = ast_tvnow();
|
|
|
|
int n = 0;
|
2008-12-22 17:29:10 +00:00
|
|
|
int elapsed = 0;
|
2008-12-22 17:09:36 +00:00
|
|
|
|
|
|
|
while (len) {
|
2008-12-22 17:29:10 +00:00
|
|
|
if (ast_wait_for_output(fd, timeoutms - elapsed)) {
|
2008-12-22 17:09:36 +00:00
|
|
|
/* poll returned a fatal error, so bail out immediately. */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear any errors from a previous write */
|
|
|
|
clearerr(f);
|
|
|
|
|
|
|
|
n = fwrite(src, 1, len, f);
|
|
|
|
|
|
|
|
if (ferror(f) && errno != EINTR && errno != EAGAIN) {
|
|
|
|
/* fatal error from fwrite() */
|
|
|
|
if (!feof(f)) {
|
|
|
|
/* Don't spam the logs if it was just that the connection is closed. */
|
|
|
|
ast_log(LOG_ERROR, "fwrite() returned error: %s\n", strerror(errno));
|
|
|
|
}
|
|
|
|
n = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update for data already written to the socket */
|
|
|
|
len -= n;
|
|
|
|
src += n;
|
|
|
|
|
|
|
|
elapsed = ast_tvdiff_ms(ast_tvnow(), start);
|
2008-12-22 17:29:10 +00:00
|
|
|
if (elapsed >= timeoutms) {
|
2012-03-22 19:51:16 +00:00
|
|
|
/* We've taken too long to write
|
2008-12-22 17:09:36 +00:00
|
|
|
* This is only an error condition if we haven't finished writing. */
|
|
|
|
n = len ? -1 : 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (fflush(f)) {
|
|
|
|
if (errno == EAGAIN || errno == EINTR) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!feof(f)) {
|
|
|
|
/* Don't spam the logs if it was just that the connection is closed. */
|
|
|
|
ast_log(LOG_ERROR, "fflush() returned error: %s\n", strerror(errno));
|
|
|
|
}
|
|
|
|
n = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n < 0 ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
2005-06-24 22:45:15 +00:00
|
|
|
char *ast_strip_quoted(char *s, const char *beg_quotes, const char *end_quotes)
|
|
|
|
{
|
|
|
|
char *e;
|
|
|
|
char *q;
|
|
|
|
|
|
|
|
s = ast_strip(s);
|
2007-01-17 20:57:51 +00:00
|
|
|
if ((q = strchr(beg_quotes, *s)) && *q != '\0') {
|
2005-06-24 22:45:15 +00:00
|
|
|
e = s + strlen(s) - 1;
|
|
|
|
if (*e == *(end_quotes + (q - beg_quotes))) {
|
|
|
|
s++;
|
|
|
|
*e = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2007-08-17 19:13:25 +00:00
|
|
|
char *ast_unescape_semicolon(char *s)
|
|
|
|
{
|
|
|
|
char *e;
|
|
|
|
char *work = s;
|
|
|
|
|
|
|
|
while ((e = strchr(work, ';'))) {
|
|
|
|
if ((e > work) && (*(e-1) == '\\')) {
|
|
|
|
memmove(e - 1, e, strlen(e) + 1);
|
|
|
|
work = e;
|
2007-10-29 14:29:18 +00:00
|
|
|
} else {
|
|
|
|
work = e + 1;
|
2007-08-17 19:13:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2007-12-19 17:09:01 +00:00
|
|
|
/* !\brief unescape some C sequences in place, return pointer to the original string.
|
|
|
|
*/
|
|
|
|
char *ast_unescape_c(char *src)
|
|
|
|
{
|
|
|
|
char c, *ret, *dst;
|
|
|
|
|
|
|
|
if (src == NULL)
|
|
|
|
return NULL;
|
|
|
|
for (ret = dst = src; (c = *src++); *dst++ = c ) {
|
|
|
|
if (c != '\\')
|
|
|
|
continue; /* copy char at the end of the loop */
|
|
|
|
switch ((c = *src++)) {
|
|
|
|
case '\0': /* special, trailing '\' */
|
|
|
|
c = '\\';
|
|
|
|
break;
|
|
|
|
case 'b': /* backspace */
|
|
|
|
c = '\b';
|
|
|
|
break;
|
|
|
|
case 'f': /* form feed */
|
|
|
|
c = '\f';
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
c = '\n';
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
c = '\r';
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
c = '\t';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* default, use the char literally */
|
|
|
|
}
|
|
|
|
*dst = '\0';
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-10-01 15:41:27 +00:00
|
|
|
int ast_build_string_va(char **buffer, size_t *space, const char *fmt, va_list ap)
|
2005-05-15 22:33:02 +00:00
|
|
|
{
|
|
|
|
int result;
|
|
|
|
|
|
|
|
if (!buffer || !*buffer || !space || !*space)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
result = vsnprintf(*buffer, *space, fmt, ap);
|
|
|
|
|
|
|
|
if (result < 0)
|
|
|
|
return -1;
|
|
|
|
else if (result > *space)
|
|
|
|
result = *space;
|
|
|
|
|
|
|
|
*buffer += result;
|
|
|
|
*space -= result;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-10-01 15:41:27 +00:00
|
|
|
int ast_build_string(char **buffer, size_t *space, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
result = ast_build_string_va(buffer, space, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-06-08 21:08:17 +00:00
|
|
|
int ast_regex_string_to_regex_pattern(const char *regex_string, struct ast_str **regex_pattern)
|
2012-04-03 19:31:25 +00:00
|
|
|
{
|
|
|
|
int regex_len = strlen(regex_string);
|
|
|
|
int ret = 3;
|
|
|
|
|
|
|
|
/* Chop off the leading / if there is one */
|
|
|
|
if ((regex_len >= 1) && (regex_string[0] == '/')) {
|
2012-06-08 21:08:17 +00:00
|
|
|
ast_str_set(regex_pattern, 0, "%s", regex_string + 1);
|
2012-04-03 19:31:25 +00:00
|
|
|
ret -= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Chop off the ending / if there is one */
|
|
|
|
if ((regex_len > 1) && (regex_string[regex_len - 1] == '/')) {
|
2012-06-08 21:08:17 +00:00
|
|
|
ast_str_truncate(*regex_pattern, -1);
|
2012-04-03 19:31:25 +00:00
|
|
|
ret -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-06-24 22:45:15 +00:00
|
|
|
int ast_true(const char *s)
|
|
|
|
{
|
2005-10-26 18:54:24 +00:00
|
|
|
if (ast_strlen_zero(s))
|
2005-06-24 22:45:15 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Determine if this is a true value */
|
|
|
|
if (!strcasecmp(s, "yes") ||
|
|
|
|
!strcasecmp(s, "true") ||
|
|
|
|
!strcasecmp(s, "y") ||
|
|
|
|
!strcasecmp(s, "t") ||
|
|
|
|
!strcasecmp(s, "1") ||
|
|
|
|
!strcasecmp(s, "on"))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ast_false(const char *s)
|
|
|
|
{
|
2005-10-26 18:54:24 +00:00
|
|
|
if (ast_strlen_zero(s))
|
2005-06-24 22:45:15 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Determine if this is a false value */
|
|
|
|
if (!strcasecmp(s, "no") ||
|
|
|
|
!strcasecmp(s, "false") ||
|
|
|
|
!strcasecmp(s, "n") ||
|
|
|
|
!strcasecmp(s, "f") ||
|
|
|
|
!strcasecmp(s, "0") ||
|
|
|
|
!strcasecmp(s, "off"))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-07-15 23:00:47 +00:00
|
|
|
#define ONE_MILLION 1000000
|
|
|
|
/*
|
|
|
|
* put timeval in a valid range. usec is 0..999999
|
|
|
|
* negative values are not allowed and truncated.
|
|
|
|
*/
|
|
|
|
static struct timeval tvfix(struct timeval a)
|
|
|
|
{
|
|
|
|
if (a.tv_usec >= ONE_MILLION) {
|
2005-08-23 17:55:59 +00:00
|
|
|
ast_log(LOG_WARNING, "warning too large timestamp %ld.%ld\n",
|
2008-05-15 10:56:29 +00:00
|
|
|
(long)a.tv_sec, (long int) a.tv_usec);
|
2005-12-13 16:07:19 +00:00
|
|
|
a.tv_sec += a.tv_usec / ONE_MILLION;
|
2005-07-15 23:00:47 +00:00
|
|
|
a.tv_usec %= ONE_MILLION;
|
|
|
|
} else if (a.tv_usec < 0) {
|
2005-07-19 15:32:03 +00:00
|
|
|
ast_log(LOG_WARNING, "warning negative timestamp %ld.%ld\n",
|
2008-05-15 10:56:29 +00:00
|
|
|
(long)a.tv_sec, (long int) a.tv_usec);
|
2005-07-15 23:00:47 +00:00
|
|
|
a.tv_usec = 0;
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct timeval ast_tvadd(struct timeval a, struct timeval b)
|
|
|
|
{
|
|
|
|
/* consistency checks to guarantee usec in 0..999999 */
|
|
|
|
a = tvfix(a);
|
|
|
|
b = tvfix(b);
|
|
|
|
a.tv_sec += b.tv_sec;
|
|
|
|
a.tv_usec += b.tv_usec;
|
|
|
|
if (a.tv_usec >= ONE_MILLION) {
|
|
|
|
a.tv_sec++;
|
|
|
|
a.tv_usec -= ONE_MILLION;
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct timeval ast_tvsub(struct timeval a, struct timeval b)
|
|
|
|
{
|
|
|
|
/* consistency checks to guarantee usec in 0..999999 */
|
|
|
|
a = tvfix(a);
|
|
|
|
b = tvfix(b);
|
|
|
|
a.tv_sec -= b.tv_sec;
|
|
|
|
a.tv_usec -= b.tv_usec;
|
|
|
|
if (a.tv_usec < 0) {
|
|
|
|
a.tv_sec-- ;
|
|
|
|
a.tv_usec += ONE_MILLION;
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}
|
2012-11-07 19:15:26 +00:00
|
|
|
|
|
|
|
int ast_remaining_ms(struct timeval start, int max_ms)
|
|
|
|
{
|
|
|
|
int ms;
|
|
|
|
|
|
|
|
if (max_ms < 0) {
|
|
|
|
ms = max_ms;
|
|
|
|
} else {
|
|
|
|
ms = max_ms - ast_tvdiff_ms(ast_tvnow(), start);
|
|
|
|
if (ms < 0) {
|
|
|
|
ms = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ms;
|
|
|
|
}
|
|
|
|
|
2005-07-15 23:00:47 +00:00
|
|
|
#undef ONE_MILLION
|
|
|
|
|
2007-07-21 14:39:52 +00:00
|
|
|
#ifndef linux
|
2006-01-10 08:33:52 +00:00
|
|
|
AST_MUTEX_DEFINE_STATIC(randomlock);
|
2007-07-21 14:39:52 +00:00
|
|
|
#endif
|
2006-01-10 08:33:52 +00:00
|
|
|
|
2006-01-10 00:55:45 +00:00
|
|
|
long int ast_random(void)
|
|
|
|
{
|
|
|
|
long int res;
|
2007-07-21 14:39:52 +00:00
|
|
|
#ifdef HAVE_DEV_URANDOM
|
|
|
|
if (dev_urandom_fd >= 0) {
|
|
|
|
int read_res = read(dev_urandom_fd, &res, sizeof(res));
|
2007-11-26 15:28:13 +00:00
|
|
|
if (read_res > 0) {
|
2007-11-27 17:01:19 +00:00
|
|
|
long int rm = RAND_MAX;
|
2007-11-26 15:28:13 +00:00
|
|
|
res = res < 0 ? ~res : res;
|
2007-11-27 17:01:19 +00:00
|
|
|
rm++;
|
|
|
|
return res % rm;
|
2007-11-26 15:28:13 +00:00
|
|
|
}
|
2007-07-21 14:39:52 +00:00
|
|
|
}
|
|
|
|
#endif
|
2012-11-29 17:16:50 +00:00
|
|
|
/* XXX - Thread safety really depends on the libc, not the OS.
|
|
|
|
*
|
|
|
|
* But... popular Linux libc's (uClibc, glibc, eglibc), all have a
|
|
|
|
* somewhat thread safe random(3) (results are random, but not
|
|
|
|
* reproducible). The libc's for other systems (BSD, et al.), not so
|
|
|
|
* much.
|
|
|
|
*/
|
2007-07-21 14:39:52 +00:00
|
|
|
#ifdef linux
|
|
|
|
res = random();
|
|
|
|
#else
|
2006-01-10 00:55:45 +00:00
|
|
|
ast_mutex_lock(&randomlock);
|
|
|
|
res = random();
|
|
|
|
ast_mutex_unlock(&randomlock);
|
2007-07-21 14:39:52 +00:00
|
|
|
#endif
|
2006-01-10 00:55:45 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2012-03-22 21:25:22 +00:00
|
|
|
void ast_replace_subargument_delimiter(char *s)
|
|
|
|
{
|
|
|
|
for (; *s; s++) {
|
|
|
|
if (*s == '^') {
|
|
|
|
*s = ',';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-23 02:57:14 +00:00
|
|
|
char *ast_process_quotes_and_slashes(char *start, char find, char replace_with)
|
|
|
|
{
|
2012-03-22 19:51:16 +00:00
|
|
|
char *dataPut = start;
|
2005-09-23 02:57:14 +00:00
|
|
|
int inEscape = 0;
|
|
|
|
int inQuotes = 0;
|
|
|
|
|
|
|
|
for (; *start; start++) {
|
|
|
|
if (inEscape) {
|
|
|
|
*dataPut++ = *start; /* Always goes verbatim */
|
|
|
|
inEscape = 0;
|
2006-07-10 19:05:48 +00:00
|
|
|
} else {
|
2005-09-23 02:57:14 +00:00
|
|
|
if (*start == '\\') {
|
|
|
|
inEscape = 1; /* Do not copy \ into the data */
|
|
|
|
} else if (*start == '\'') {
|
2006-07-10 19:05:48 +00:00
|
|
|
inQuotes = 1 - inQuotes; /* Do not copy ' into the data */
|
2005-09-23 02:57:14 +00:00
|
|
|
} else {
|
|
|
|
/* Replace , with |, unless in quotes */
|
2006-07-10 19:05:48 +00:00
|
|
|
*dataPut++ = inQuotes ? *start : ((*start == find) ? replace_with : *start);
|
2005-09-23 02:57:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (start != dataPut)
|
|
|
|
*dataPut = 0;
|
|
|
|
return dataPut;
|
|
|
|
}
|
2005-12-20 20:20:04 +00:00
|
|
|
|
2009-05-21 21:13:09 +00:00
|
|
|
void ast_join(char *s, size_t len, const char * const w[])
|
2005-12-20 20:20:04 +00:00
|
|
|
{
|
|
|
|
int x, ofs = 0;
|
|
|
|
const char *src;
|
|
|
|
|
|
|
|
/* Join words into a string */
|
|
|
|
if (!s)
|
|
|
|
return;
|
2006-07-10 19:05:48 +00:00
|
|
|
for (x = 0; ofs < len && w[x]; x++) {
|
2005-12-20 20:20:04 +00:00
|
|
|
if (x > 0)
|
|
|
|
s[ofs++] = ' ';
|
|
|
|
for (src = w[x]; *src && ofs < len; src++)
|
|
|
|
s[ofs++] = *src;
|
|
|
|
}
|
|
|
|
if (ofs == len)
|
|
|
|
ofs--;
|
|
|
|
s[ofs] = '\0';
|
|
|
|
}
|
2006-01-04 21:54:09 +00:00
|
|
|
|
2007-11-04 19:44:31 +00:00
|
|
|
/*
|
|
|
|
* stringfields support routines.
|
|
|
|
*/
|
2006-01-04 21:54:09 +00:00
|
|
|
|
2009-03-31 21:29:50 +00:00
|
|
|
/* this is a little complex... string fields are stored with their
|
|
|
|
allocated size in the bytes preceding the string; even the
|
|
|
|
constant 'empty' string has to be this way, so the code that
|
|
|
|
checks to see if there is enough room for a new string doesn't
|
|
|
|
have to have any special case checks
|
|
|
|
*/
|
|
|
|
|
|
|
|
static const struct {
|
|
|
|
ast_string_field_allocation allocation;
|
|
|
|
char string[1];
|
|
|
|
} __ast_string_field_empty_buffer;
|
|
|
|
|
|
|
|
ast_string_field __ast_string_field_empty = __ast_string_field_empty_buffer.string;
|
|
|
|
|
|
|
|
#define ALLOCATOR_OVERHEAD 48
|
|
|
|
|
|
|
|
static size_t optimal_alloc_size(size_t size)
|
|
|
|
{
|
|
|
|
unsigned int count;
|
|
|
|
|
|
|
|
size += ALLOCATOR_OVERHEAD;
|
|
|
|
|
|
|
|
for (count = 1; size; size >>= 1, count++);
|
|
|
|
|
|
|
|
return (1 << count) - ALLOCATOR_OVERHEAD;
|
|
|
|
}
|
2007-11-04 19:44:31 +00:00
|
|
|
|
|
|
|
/*! \brief add a new block to the pool.
|
|
|
|
* We can only allocate from the topmost pool, so the
|
|
|
|
* fields in *mgr reflect the size of that only.
|
|
|
|
*/
|
2009-05-05 08:51:06 +00:00
|
|
|
static int add_string_pool(struct ast_string_field_mgr *mgr, struct ast_string_field_pool **pool_head,
|
|
|
|
size_t size, const char *file, int lineno, const char *func)
|
2006-01-26 04:00:05 +00:00
|
|
|
{
|
|
|
|
struct ast_string_field_pool *pool;
|
2009-03-31 21:29:50 +00:00
|
|
|
size_t alloc_size = optimal_alloc_size(sizeof(*pool) + size);
|
2006-01-26 04:00:05 +00:00
|
|
|
|
2009-05-05 08:51:06 +00:00
|
|
|
#if defined(__AST_DEBUG_MALLOC)
|
|
|
|
if (!(pool = __ast_calloc(1, alloc_size, file, lineno, func))) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#else
|
2009-03-31 21:29:50 +00:00
|
|
|
if (!(pool = ast_calloc(1, alloc_size))) {
|
2006-01-26 04:00:05 +00:00
|
|
|
return -1;
|
2009-03-31 21:29:50 +00:00
|
|
|
}
|
2009-05-05 08:51:06 +00:00
|
|
|
#endif
|
2009-03-31 21:29:50 +00:00
|
|
|
|
2007-11-04 19:44:31 +00:00
|
|
|
pool->prev = *pool_head;
|
2009-03-31 21:29:50 +00:00
|
|
|
pool->size = alloc_size - sizeof(*pool);
|
2007-11-04 19:44:31 +00:00
|
|
|
*pool_head = pool;
|
2008-07-23 16:30:18 +00:00
|
|
|
mgr->last_alloc = NULL;
|
2006-01-26 04:00:05 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-11-04 19:44:31 +00:00
|
|
|
/*
|
|
|
|
* This is an internal API, code should not use it directly.
|
|
|
|
* It initializes all fields as empty, then uses 'size' for 3 functions:
|
|
|
|
* size > 0 means initialize the pool list with a pool of given size.
|
|
|
|
* This must be called right after allocating the object.
|
|
|
|
* size = 0 means release all pools except the most recent one.
|
2009-05-05 14:17:18 +00:00
|
|
|
* If the first pool was allocated via embedding in another
|
|
|
|
* object, that pool will be preserved instead.
|
2007-11-04 19:44:31 +00:00
|
|
|
* This is useful to e.g. reset an object to the initial value.
|
|
|
|
* size < 0 means release all pools.
|
|
|
|
* This must be done before destroying the object.
|
|
|
|
*/
|
2009-05-05 08:51:06 +00:00
|
|
|
int __ast_string_field_init(struct ast_string_field_mgr *mgr, struct ast_string_field_pool **pool_head,
|
|
|
|
int needed, const char *file, int lineno, const char *func)
|
2006-01-04 21:54:09 +00:00
|
|
|
{
|
2008-07-23 16:30:18 +00:00
|
|
|
const char **p = (const char **) pool_head + 1;
|
2009-05-05 08:51:06 +00:00
|
|
|
struct ast_string_field_pool *cur = NULL;
|
|
|
|
struct ast_string_field_pool *preserve = NULL;
|
2007-11-04 19:44:31 +00:00
|
|
|
|
|
|
|
/* clear fields - this is always necessary */
|
2009-03-31 21:29:50 +00:00
|
|
|
while ((struct ast_string_field_mgr *) p != mgr) {
|
2007-11-04 19:44:31 +00:00
|
|
|
*p++ = __ast_string_field_empty;
|
2009-03-31 21:29:50 +00:00
|
|
|
}
|
|
|
|
|
2008-07-23 16:30:18 +00:00
|
|
|
mgr->last_alloc = NULL;
|
2009-05-05 08:51:06 +00:00
|
|
|
#if defined(__AST_DEBUG_MALLOC)
|
|
|
|
mgr->owner_file = file;
|
|
|
|
mgr->owner_func = func;
|
|
|
|
mgr->owner_line = lineno;
|
|
|
|
#endif
|
|
|
|
if (needed > 0) { /* allocate the initial pool */
|
2007-11-04 19:44:31 +00:00
|
|
|
*pool_head = NULL;
|
2010-03-10 23:15:55 +00:00
|
|
|
mgr->embedded_pool = NULL;
|
2009-05-05 08:51:06 +00:00
|
|
|
return add_string_pool(mgr, pool_head, needed, file, lineno, func);
|
2007-11-04 19:44:31 +00:00
|
|
|
}
|
2009-03-31 21:29:50 +00:00
|
|
|
|
2009-05-28 20:36:49 +00:00
|
|
|
/* if there is an embedded pool, we can't actually release *all*
|
|
|
|
* pools, we must keep the embedded one. if the caller is about
|
|
|
|
* to free the structure that contains the stringfield manager
|
|
|
|
* and embedded pool anyway, it will be freed as part of that
|
|
|
|
* operation.
|
|
|
|
*/
|
|
|
|
if ((needed < 0) && mgr->embedded_pool) {
|
|
|
|
needed = 0;
|
|
|
|
}
|
|
|
|
|
2009-05-05 08:51:06 +00:00
|
|
|
if (needed < 0) { /* reset all pools */
|
2009-05-28 14:51:43 +00:00
|
|
|
cur = *pool_head;
|
2009-05-05 14:17:18 +00:00
|
|
|
} else if (mgr->embedded_pool) { /* preserve the embedded pool */
|
|
|
|
preserve = mgr->embedded_pool;
|
|
|
|
cur = *pool_head;
|
2009-05-05 08:51:06 +00:00
|
|
|
} else { /* preserve the last pool */
|
|
|
|
if (*pool_head == NULL) {
|
2007-11-04 19:44:31 +00:00
|
|
|
ast_log(LOG_WARNING, "trying to reset empty pool\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2009-05-05 08:51:06 +00:00
|
|
|
preserve = *pool_head;
|
|
|
|
cur = preserve->prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preserve) {
|
|
|
|
preserve->prev = NULL;
|
|
|
|
preserve->used = preserve->active = 0;
|
2007-11-04 19:44:31 +00:00
|
|
|
}
|
2008-07-23 16:30:18 +00:00
|
|
|
|
2007-11-04 19:44:31 +00:00
|
|
|
while (cur) {
|
|
|
|
struct ast_string_field_pool *prev = cur->prev;
|
2008-07-23 16:30:18 +00:00
|
|
|
|
2009-05-05 08:51:06 +00:00
|
|
|
if (cur != preserve) {
|
|
|
|
ast_free(cur);
|
|
|
|
}
|
2007-11-04 19:44:31 +00:00
|
|
|
cur = prev;
|
|
|
|
}
|
2008-07-23 16:30:18 +00:00
|
|
|
|
2009-05-05 08:51:06 +00:00
|
|
|
*pool_head = preserve;
|
|
|
|
|
2006-01-26 04:00:05 +00:00
|
|
|
return 0;
|
2006-01-04 21:54:09 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 19:44:31 +00:00
|
|
|
ast_string_field __ast_string_field_alloc_space(struct ast_string_field_mgr *mgr,
|
2008-07-23 16:30:18 +00:00
|
|
|
struct ast_string_field_pool **pool_head, size_t needed)
|
2006-01-04 21:54:09 +00:00
|
|
|
{
|
|
|
|
char *result = NULL;
|
2009-03-31 21:29:50 +00:00
|
|
|
size_t space = (*pool_head)->size - (*pool_head)->used;
|
2011-11-02 22:02:07 +00:00
|
|
|
size_t to_alloc;
|
2006-01-04 21:54:09 +00:00
|
|
|
|
2011-11-02 22:02:07 +00:00
|
|
|
/* Make room for ast_string_field_allocation and make it a multiple of that. */
|
2011-11-11 22:00:14 +00:00
|
|
|
to_alloc = ast_make_room_for(needed, ast_string_field_allocation);
|
|
|
|
ast_assert(to_alloc % ast_alignof(ast_string_field_allocation) == 0);
|
2011-11-02 22:02:07 +00:00
|
|
|
|
|
|
|
if (__builtin_expect(to_alloc > space, 0)) {
|
2009-03-31 21:29:50 +00:00
|
|
|
size_t new_size = (*pool_head)->size;
|
2006-01-04 21:54:09 +00:00
|
|
|
|
2009-03-31 21:29:50 +00:00
|
|
|
while (new_size < to_alloc) {
|
2006-01-04 21:54:09 +00:00
|
|
|
new_size *= 2;
|
2009-03-31 21:29:50 +00:00
|
|
|
}
|
2006-01-04 21:54:09 +00:00
|
|
|
|
2009-05-05 08:51:06 +00:00
|
|
|
#if defined(__AST_DEBUG_MALLOC)
|
|
|
|
if (add_string_pool(mgr, pool_head, new_size, mgr->owner_file, mgr->owner_line, mgr->owner_func))
|
2006-01-04 21:54:09 +00:00
|
|
|
return NULL;
|
2009-05-05 08:51:06 +00:00
|
|
|
#else
|
2009-05-05 13:18:21 +00:00
|
|
|
if (add_string_pool(mgr, pool_head, new_size, __FILE__, __LINE__, __FUNCTION__))
|
2009-05-05 08:51:06 +00:00
|
|
|
return NULL;
|
|
|
|
#endif
|
2006-01-04 21:54:09 +00:00
|
|
|
}
|
|
|
|
|
2011-11-02 22:02:07 +00:00
|
|
|
/* pool->base is always aligned (gcc aligned attribute). We ensure that
|
2011-11-11 22:00:14 +00:00
|
|
|
* to_alloc is also a multiple of ast_alignof(ast_string_field_allocation)
|
2011-11-02 22:02:07 +00:00
|
|
|
* causing result to always be aligned as well; which in turn fixes that
|
|
|
|
* AST_STRING_FIELD_ALLOCATION(result) is aligned. */
|
2009-03-31 21:29:50 +00:00
|
|
|
result = (*pool_head)->base + (*pool_head)->used;
|
|
|
|
(*pool_head)->used += to_alloc;
|
|
|
|
(*pool_head)->active += needed;
|
2011-11-11 22:00:14 +00:00
|
|
|
result += ast_alignof(ast_string_field_allocation);
|
2009-03-31 21:29:50 +00:00
|
|
|
AST_STRING_FIELD_ALLOCATION(result) = needed;
|
2008-07-23 16:30:18 +00:00
|
|
|
mgr->last_alloc = result;
|
2009-03-31 21:29:50 +00:00
|
|
|
|
2006-01-04 21:54:09 +00:00
|
|
|
return result;
|
|
|
|
}
|
2006-01-06 19:19:17 +00:00
|
|
|
|
2009-03-31 21:29:50 +00:00
|
|
|
int __ast_string_field_ptr_grow(struct ast_string_field_mgr *mgr,
|
|
|
|
struct ast_string_field_pool **pool_head, size_t needed,
|
2008-07-23 16:30:18 +00:00
|
|
|
const ast_string_field *ptr)
|
|
|
|
{
|
2009-03-31 21:29:50 +00:00
|
|
|
ssize_t grow = needed - AST_STRING_FIELD_ALLOCATION(*ptr);
|
|
|
|
size_t space = (*pool_head)->size - (*pool_head)->used;
|
2008-07-23 16:30:18 +00:00
|
|
|
|
|
|
|
if (*ptr != mgr->last_alloc) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (space < grow) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-03-31 21:29:50 +00:00
|
|
|
(*pool_head)->used += grow;
|
|
|
|
(*pool_head)->active += grow;
|
|
|
|
AST_STRING_FIELD_ALLOCATION(*ptr) += grow;
|
2008-07-23 16:30:18 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-03-31 21:29:50 +00:00
|
|
|
void __ast_string_field_release_active(struct ast_string_field_pool *pool_head,
|
|
|
|
const ast_string_field ptr)
|
|
|
|
{
|
|
|
|
struct ast_string_field_pool *pool, *prev;
|
|
|
|
|
|
|
|
if (ptr == __ast_string_field_empty) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (pool = pool_head, prev = NULL; pool; prev = pool, pool = pool->prev) {
|
|
|
|
if ((ptr >= pool->base) && (ptr <= (pool->base + pool->size))) {
|
|
|
|
pool->active -= AST_STRING_FIELD_ALLOCATION(ptr);
|
|
|
|
if ((pool->active == 0) && prev) {
|
|
|
|
prev->prev = pool->prev;
|
|
|
|
ast_free(pool);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-04 19:44:31 +00:00
|
|
|
void __ast_string_field_ptr_build_va(struct ast_string_field_mgr *mgr,
|
2008-07-23 16:30:18 +00:00
|
|
|
struct ast_string_field_pool **pool_head,
|
2011-05-26 15:55:22 +00:00
|
|
|
ast_string_field *ptr, const char *format, va_list ap)
|
2006-01-06 19:19:17 +00:00
|
|
|
{
|
|
|
|
size_t needed;
|
2009-02-16 21:45:54 +00:00
|
|
|
size_t available;
|
2009-03-31 21:29:50 +00:00
|
|
|
size_t space = (*pool_head)->size - (*pool_head)->used;
|
|
|
|
ssize_t grow;
|
2009-02-16 21:45:54 +00:00
|
|
|
char *target;
|
2011-05-26 15:55:22 +00:00
|
|
|
va_list ap2;
|
2006-01-06 19:19:17 +00:00
|
|
|
|
2009-02-16 21:45:54 +00:00
|
|
|
/* if the field already has space allocated, try to reuse it;
|
2009-03-31 21:29:50 +00:00
|
|
|
otherwise, try to use the empty space at the end of the current
|
2009-02-16 21:45:54 +00:00
|
|
|
pool
|
|
|
|
*/
|
2009-03-31 21:29:50 +00:00
|
|
|
if (*ptr != __ast_string_field_empty) {
|
2009-02-16 21:45:54 +00:00
|
|
|
target = (char *) *ptr;
|
2009-03-31 21:29:50 +00:00
|
|
|
available = AST_STRING_FIELD_ALLOCATION(*ptr);
|
|
|
|
if (*ptr == mgr->last_alloc) {
|
|
|
|
available += space;
|
|
|
|
}
|
2009-02-16 21:45:54 +00:00
|
|
|
} else {
|
2011-11-11 22:00:14 +00:00
|
|
|
/* pool->used is always a multiple of ast_alignof(ast_string_field_allocation)
|
2011-11-02 22:02:07 +00:00
|
|
|
* so we don't need to re-align anything here.
|
|
|
|
*/
|
2011-11-11 22:00:14 +00:00
|
|
|
target = (*pool_head)->base + (*pool_head)->used + ast_alignof(ast_string_field_allocation);
|
|
|
|
available = space - ast_alignof(ast_string_field_allocation);
|
2009-02-16 21:45:54 +00:00
|
|
|
}
|
2006-01-06 19:19:17 +00:00
|
|
|
|
2011-05-26 15:55:22 +00:00
|
|
|
va_copy(ap2, ap);
|
|
|
|
needed = vsnprintf(target, available, format, ap2) + 1;
|
|
|
|
va_end(ap2);
|
2006-01-26 04:00:05 +00:00
|
|
|
|
2009-02-16 21:45:54 +00:00
|
|
|
if (needed > available) {
|
2009-03-31 21:29:50 +00:00
|
|
|
/* the allocation could not be satisfied using the field's current allocation
|
|
|
|
(if it has one), or the space available in the pool (if it does not). allocate
|
|
|
|
space for it, adding a new string pool if necessary.
|
2009-02-16 21:45:54 +00:00
|
|
|
*/
|
2009-03-31 21:29:50 +00:00
|
|
|
if (!(target = (char *) __ast_string_field_alloc_space(mgr, pool_head, needed))) {
|
|
|
|
return;
|
2009-02-16 21:45:54 +00:00
|
|
|
}
|
2011-05-26 15:55:22 +00:00
|
|
|
vsprintf(target, format, ap);
|
2012-02-29 20:41:38 +00:00
|
|
|
va_end(ap); /* XXX va_end without va_start? */
|
2009-03-31 21:29:50 +00:00
|
|
|
__ast_string_field_release_active(*pool_head, *ptr);
|
|
|
|
*ptr = target;
|
|
|
|
} else if (*ptr != target) {
|
|
|
|
/* the allocation was satisfied using available space in the pool, but not
|
|
|
|
using the space already allocated to the field
|
|
|
|
*/
|
|
|
|
__ast_string_field_release_active(*pool_head, *ptr);
|
2009-02-16 21:45:54 +00:00
|
|
|
mgr->last_alloc = *ptr = target;
|
2009-03-31 21:29:50 +00:00
|
|
|
AST_STRING_FIELD_ALLOCATION(target) = needed;
|
2011-11-11 22:00:14 +00:00
|
|
|
(*pool_head)->used += ast_make_room_for(needed, ast_string_field_allocation);
|
2009-03-31 21:29:50 +00:00
|
|
|
(*pool_head)->active += needed;
|
|
|
|
} else if ((grow = (needed - AST_STRING_FIELD_ALLOCATION(*ptr))) > 0) {
|
|
|
|
/* the allocation was satisfied by using available space in the pool *and*
|
|
|
|
the field was the last allocated field from the pool, so it grew
|
|
|
|
*/
|
|
|
|
AST_STRING_FIELD_ALLOCATION(*ptr) += grow;
|
2011-11-11 22:00:14 +00:00
|
|
|
(*pool_head)->used += ast_align_for(grow, ast_string_field_allocation);
|
2011-11-02 22:02:07 +00:00
|
|
|
(*pool_head)->active += grow;
|
2009-02-16 21:45:54 +00:00
|
|
|
}
|
2006-11-07 21:47:49 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 19:44:31 +00:00
|
|
|
void __ast_string_field_ptr_build(struct ast_string_field_mgr *mgr,
|
2008-07-23 16:30:18 +00:00
|
|
|
struct ast_string_field_pool **pool_head,
|
2009-02-16 21:45:54 +00:00
|
|
|
ast_string_field *ptr, const char *format, ...)
|
2006-11-07 21:47:49 +00:00
|
|
|
{
|
2011-05-26 15:55:22 +00:00
|
|
|
va_list ap;
|
2006-11-07 21:47:49 +00:00
|
|
|
|
2011-05-26 15:55:22 +00:00
|
|
|
va_start(ap, format);
|
|
|
|
__ast_string_field_ptr_build_va(mgr, pool_head, ptr, format, ap);
|
|
|
|
va_end(ap);
|
2006-01-06 19:19:17 +00:00
|
|
|
}
|
2009-05-05 14:17:18 +00:00
|
|
|
|
|
|
|
void *__ast_calloc_with_stringfields(unsigned int num_structs, size_t struct_size, size_t field_mgr_offset,
|
|
|
|
size_t field_mgr_pool_offset, size_t pool_size, const char *file,
|
|
|
|
int lineno, const char *func)
|
|
|
|
{
|
|
|
|
struct ast_string_field_mgr *mgr;
|
|
|
|
struct ast_string_field_pool *pool;
|
|
|
|
struct ast_string_field_pool **pool_head;
|
|
|
|
size_t pool_size_needed = sizeof(*pool) + pool_size;
|
|
|
|
size_t size_to_alloc = optimal_alloc_size(struct_size + pool_size_needed);
|
|
|
|
void *allocation;
|
|
|
|
unsigned int x;
|
|
|
|
|
2012-03-22 19:51:16 +00:00
|
|
|
#if defined(__AST_DEBUG_MALLOC)
|
2009-05-05 14:17:18 +00:00
|
|
|
if (!(allocation = __ast_calloc(num_structs, size_to_alloc, file, lineno, func))) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (!(allocation = ast_calloc(num_structs, size_to_alloc))) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for (x = 0; x < num_structs; x++) {
|
|
|
|
void *base = allocation + (size_to_alloc * x);
|
|
|
|
const char **p;
|
|
|
|
|
|
|
|
mgr = base + field_mgr_offset;
|
|
|
|
pool_head = base + field_mgr_pool_offset;
|
|
|
|
pool = base + struct_size;
|
|
|
|
|
|
|
|
p = (const char **) pool_head + 1;
|
|
|
|
while ((struct ast_string_field_mgr *) p != mgr) {
|
|
|
|
*p++ = __ast_string_field_empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
mgr->embedded_pool = pool;
|
|
|
|
*pool_head = pool;
|
|
|
|
pool->size = size_to_alloc - struct_size - sizeof(*pool);
|
2010-01-15 20:58:24 +00:00
|
|
|
#if defined(__AST_DEBUG_MALLOC)
|
|
|
|
mgr->owner_file = file;
|
|
|
|
mgr->owner_func = func;
|
|
|
|
mgr->owner_line = lineno;
|
|
|
|
#endif
|
2009-05-05 14:17:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return allocation;
|
|
|
|
}
|
|
|
|
|
2007-11-04 19:44:31 +00:00
|
|
|
/* end of stringfields support */
|
2006-02-15 00:24:24 +00:00
|
|
|
|
2006-03-30 23:26:22 +00:00
|
|
|
AST_MUTEX_DEFINE_STATIC(fetchadd_m); /* used for all fetc&add ops */
|
2006-07-10 19:05:48 +00:00
|
|
|
|
2006-03-30 23:26:22 +00:00
|
|
|
int ast_atomic_fetchadd_int_slow(volatile int *p, int v)
|
|
|
|
{
|
2008-03-04 23:04:29 +00:00
|
|
|
int ret;
|
|
|
|
ast_mutex_lock(&fetchadd_m);
|
|
|
|
ret = *p;
|
|
|
|
*p += v;
|
|
|
|
ast_mutex_unlock(&fetchadd_m);
|
|
|
|
return ret;
|
2006-03-30 23:26:22 +00:00
|
|
|
}
|
2006-02-15 00:24:24 +00:00
|
|
|
|
2007-07-18 19:47:20 +00:00
|
|
|
/*! \brief
|
|
|
|
* get values from config variables.
|
|
|
|
*/
|
|
|
|
int ast_get_timeval(const char *src, struct timeval *dst, struct timeval _default, int *consumed)
|
|
|
|
{
|
|
|
|
long double dtv = 0.0;
|
|
|
|
int scanned;
|
|
|
|
|
|
|
|
if (dst == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
*dst = _default;
|
|
|
|
|
|
|
|
if (ast_strlen_zero(src))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* only integer at the moment, but one day we could accept more formats */
|
2009-08-10 19:20:57 +00:00
|
|
|
if (sscanf(src, "%30Lf%n", &dtv, &scanned) > 0) {
|
2007-07-18 19:47:20 +00:00
|
|
|
dst->tv_sec = dtv;
|
|
|
|
dst->tv_usec = (dtv - dst->tv_sec) * 1000000.0;
|
|
|
|
if (consumed)
|
|
|
|
*consumed = scanned;
|
|
|
|
return 0;
|
|
|
|
} else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-03-07 17:55:33 +00:00
|
|
|
/*! \brief
|
2006-02-15 00:24:24 +00:00
|
|
|
* get values from config variables.
|
|
|
|
*/
|
2006-02-23 17:13:57 +00:00
|
|
|
int ast_get_time_t(const char *src, time_t *dst, time_t _default, int *consumed)
|
2006-02-15 00:24:24 +00:00
|
|
|
{
|
|
|
|
long t;
|
2006-02-23 17:13:57 +00:00
|
|
|
int scanned;
|
2006-02-15 00:24:24 +00:00
|
|
|
|
|
|
|
if (dst == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
*dst = _default;
|
|
|
|
|
|
|
|
if (ast_strlen_zero(src))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* only integer at the moment, but one day we could accept more formats */
|
2009-08-10 19:20:57 +00:00
|
|
|
if (sscanf(src, "%30ld%n", &t, &scanned) == 1) {
|
2006-02-15 00:24:24 +00:00
|
|
|
*dst = t;
|
2006-02-23 17:13:57 +00:00
|
|
|
if (consumed)
|
|
|
|
*consumed = scanned;
|
2006-02-15 00:24:24 +00:00
|
|
|
return 0;
|
|
|
|
} else
|
|
|
|
return -1;
|
|
|
|
}
|
2006-07-10 19:05:48 +00:00
|
|
|
|
2006-10-12 18:43:52 +00:00
|
|
|
void ast_enable_packet_fragmentation(int sock)
|
|
|
|
{
|
2006-10-13 15:47:42 +00:00
|
|
|
#if defined(HAVE_IP_MTU_DISCOVER)
|
2006-10-12 18:43:52 +00:00
|
|
|
int val = IP_PMTUDISC_DONT;
|
2012-03-22 19:51:16 +00:00
|
|
|
|
2006-10-12 18:43:52 +00:00
|
|
|
if (setsockopt(sock, IPPROTO_IP, IP_MTU_DISCOVER, &val, sizeof(val)))
|
|
|
|
ast_log(LOG_WARNING, "Unable to disable PMTU discovery. Large UDP packets may fail to be delivered when sent from this socket.\n");
|
2006-10-13 15:47:42 +00:00
|
|
|
#endif /* HAVE_IP_MTU_DISCOVER */
|
2006-10-12 18:43:52 +00:00
|
|
|
}
|
2007-06-22 04:35:12 +00:00
|
|
|
|
|
|
|
int ast_mkdir(const char *path, int mode)
|
|
|
|
{
|
|
|
|
char *ptr;
|
|
|
|
int len = strlen(path), count = 0, x, piececount = 0;
|
|
|
|
char *tmp = ast_strdupa(path);
|
|
|
|
char **pieces;
|
2012-07-31 20:21:43 +00:00
|
|
|
char *fullpath = ast_alloca(len + 1);
|
2007-06-22 04:35:12 +00:00
|
|
|
int res = 0;
|
|
|
|
|
|
|
|
for (ptr = tmp; *ptr; ptr++) {
|
|
|
|
if (*ptr == '/')
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Count the components to the directory path */
|
2012-07-31 20:21:43 +00:00
|
|
|
pieces = ast_alloca(count * sizeof(*pieces));
|
2007-06-22 04:35:12 +00:00
|
|
|
for (ptr = tmp; *ptr; ptr++) {
|
|
|
|
if (*ptr == '/') {
|
|
|
|
*ptr = '\0';
|
|
|
|
pieces[piececount++] = ptr + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*fullpath = '\0';
|
|
|
|
for (x = 0; x < piececount; x++) {
|
|
|
|
/* This looks funky, but the buffer is always ideally-sized, so it's fine. */
|
|
|
|
strcat(fullpath, "/");
|
|
|
|
strcat(fullpath, pieces[x]);
|
|
|
|
res = mkdir(fullpath, mode);
|
|
|
|
if (res && errno != EEXIST)
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-08-03 19:41:42 +00:00
|
|
|
int ast_utils_init(void)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_DEV_URANDOM
|
|
|
|
dev_urandom_fd = open("/dev/urandom", O_RDONLY);
|
|
|
|
#endif
|
|
|
|
base64_init();
|
|
|
|
#ifdef DEBUG_THREADS
|
2008-04-10 22:02:32 +00:00
|
|
|
#if !defined(LOW_MEMORY)
|
2008-07-11 18:09:35 +00:00
|
|
|
ast_cli_register_multiple(utils_cli, ARRAY_LEN(utils_cli));
|
2008-04-10 22:02:32 +00:00
|
|
|
#endif
|
2007-08-03 19:41:42 +00:00
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
2008-05-23 12:37:31 +00:00
|
|
|
|
2009-04-23 20:36:35 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
*\brief Parse digest authorization header.
|
|
|
|
*\return Returns -1 if we have no auth or something wrong with digest.
|
|
|
|
*\note This function may be used for Digest request and responce header.
|
|
|
|
* request arg is set to nonzero, if we parse Digest Request.
|
|
|
|
* pedantic arg can be set to nonzero if we need to do addition Digest check.
|
|
|
|
*/
|
|
|
|
int ast_parse_digest(const char *digest, struct ast_http_digest *d, int request, int pedantic) {
|
2012-03-15 19:11:03 +00:00
|
|
|
char *c;
|
2009-04-23 20:36:35 +00:00
|
|
|
struct ast_str *str = ast_str_create(16);
|
|
|
|
|
2012-03-15 19:11:03 +00:00
|
|
|
/* table of recognised keywords, and places where they should be copied */
|
|
|
|
const struct x {
|
|
|
|
const char *key;
|
|
|
|
const ast_string_field *field;
|
|
|
|
} *i, keys[] = {
|
|
|
|
{ "username=", &d->username },
|
|
|
|
{ "realm=", &d->realm },
|
|
|
|
{ "nonce=", &d->nonce },
|
|
|
|
{ "uri=", &d->uri },
|
|
|
|
{ "domain=", &d->domain },
|
|
|
|
{ "response=", &d->response },
|
|
|
|
{ "cnonce=", &d->cnonce },
|
|
|
|
{ "opaque=", &d->opaque },
|
|
|
|
/* Special cases that cannot be directly copied */
|
|
|
|
{ "algorithm=", NULL },
|
|
|
|
{ "qop=", NULL },
|
|
|
|
{ "nc=", NULL },
|
|
|
|
{ NULL, 0 },
|
|
|
|
};
|
|
|
|
|
2009-04-23 20:36:35 +00:00
|
|
|
if (ast_strlen_zero(digest) || !d || !str) {
|
|
|
|
ast_free(str);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ast_str_set(&str, 0, "%s", digest);
|
|
|
|
|
|
|
|
c = ast_skip_blanks(ast_str_buffer(str));
|
|
|
|
|
2011-05-05 02:34:29 +00:00
|
|
|
if (strncasecmp(c, "Digest ", strlen("Digest "))) {
|
2009-04-23 20:36:35 +00:00
|
|
|
ast_log(LOG_WARNING, "Missing Digest.\n");
|
|
|
|
ast_free(str);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
c += strlen("Digest ");
|
|
|
|
|
|
|
|
/* lookup for keys/value pair */
|
2012-03-15 19:11:03 +00:00
|
|
|
while (c && *c && *(c = ast_skip_blanks(c))) {
|
2009-04-23 20:36:35 +00:00
|
|
|
/* find key */
|
2012-03-15 19:11:03 +00:00
|
|
|
for (i = keys; i->key != NULL; i++) {
|
|
|
|
char *src, *separator;
|
|
|
|
int unescape = 0;
|
|
|
|
if (strncasecmp(c, i->key, strlen(i->key)) != 0) {
|
|
|
|
continue;
|
2009-04-23 20:36:35 +00:00
|
|
|
}
|
|
|
|
|
2012-03-15 19:11:03 +00:00
|
|
|
/* Found. Skip keyword, take text in quotes or up to the separator. */
|
|
|
|
c += strlen(i->key);
|
|
|
|
if (*c == '"') {
|
|
|
|
src = ++c;
|
|
|
|
separator = "\"";
|
|
|
|
unescape = 1;
|
|
|
|
} else {
|
|
|
|
src = c;
|
|
|
|
separator = ",";
|
2009-04-23 20:36:35 +00:00
|
|
|
}
|
2012-03-15 19:11:03 +00:00
|
|
|
strsep(&c, separator); /* clear separator and move ptr */
|
|
|
|
if (unescape) {
|
|
|
|
ast_unescape_c(src);
|
|
|
|
}
|
|
|
|
if (i->field) {
|
|
|
|
ast_string_field_ptr_set(d, i->field, src);
|
|
|
|
} else {
|
|
|
|
/* Special cases that require additional procesing */
|
|
|
|
if (!strcasecmp(i->key, "algorithm=")) {
|
|
|
|
if (strcasecmp(src, "MD5")) {
|
|
|
|
ast_log(LOG_WARNING, "Digest algorithm: \"%s\" not supported.\n", src);
|
|
|
|
ast_free(str);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else if (!strcasecmp(i->key, "qop=") && !strcasecmp(src, "auth")) {
|
|
|
|
d->qop = 1;
|
|
|
|
} else if (!strcasecmp(i->key, "nc=")) {
|
|
|
|
unsigned long u;
|
|
|
|
if (sscanf(src, "%30lx", &u) != 1) {
|
|
|
|
ast_log(LOG_WARNING, "Incorrect Digest nc value: \"%s\".\n", src);
|
|
|
|
ast_free(str);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ast_string_field_set(d, nc, src);
|
|
|
|
}
|
2009-04-23 20:36:35 +00:00
|
|
|
}
|
2012-03-15 19:11:03 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i->key == NULL) { /* not found, try ',' */
|
|
|
|
strsep(&c, ",");
|
2009-04-23 20:36:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ast_free(str);
|
|
|
|
|
|
|
|
/* Digest checkout */
|
|
|
|
if (ast_strlen_zero(d->realm) || ast_strlen_zero(d->nonce)) {
|
|
|
|
/* "realm" and "nonce" MUST be always exist */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!request) {
|
|
|
|
/* Additional check for Digest response */
|
|
|
|
if (ast_strlen_zero(d->username) || ast_strlen_zero(d->uri) || ast_strlen_zero(d->response)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pedantic && d->qop && (ast_strlen_zero(d->cnonce) || ast_strlen_zero(d->nc))) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-05-23 12:37:31 +00:00
|
|
|
#ifndef __AST_DEBUG_MALLOC
|
|
|
|
int _ast_asprintf(char **ret, const char *file, int lineno, const char *func, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
if ((res = vasprintf(ret, fmt, ap)) == -1) {
|
|
|
|
MALLOC_FAILURE_MSG;
|
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
#endif
|
2010-12-12 03:58:33 +00:00
|
|
|
|
|
|
|
int ast_get_tid(void)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
#if defined (__linux) && defined(SYS_gettid)
|
|
|
|
ret = syscall(SYS_gettid); /* available since Linux 1.4.11 */
|
|
|
|
#elif defined(__sun)
|
|
|
|
ret = pthread_self();
|
|
|
|
#elif defined(__APPLE__)
|
|
|
|
ret = mach_thread_self();
|
|
|
|
mach_port_deallocate(mach_task_self(), ret);
|
|
|
|
#elif defined(__FreeBSD__) && defined(HAVE_SYS_THR_H)
|
|
|
|
long lwpid;
|
|
|
|
thr_self(&lwpid); /* available since sys/thr.h creation 2003 */
|
|
|
|
ret = lwpid;
|
|
|
|
#endif
|
|
|
|
return ret;
|
|
|
|
}
|
2010-12-18 00:08:13 +00:00
|
|
|
|
|
|
|
char *ast_utils_which(const char *binary, char *fullpath, size_t fullpath_size)
|
|
|
|
{
|
|
|
|
const char *envPATH = getenv("PATH");
|
|
|
|
char *tpath, *path;
|
|
|
|
struct stat unused;
|
|
|
|
if (!envPATH) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
tpath = ast_strdupa(envPATH);
|
|
|
|
while ((path = strsep(&tpath, ":"))) {
|
|
|
|
snprintf(fullpath, fullpath_size, "%s/%s", path, binary);
|
|
|
|
if (!stat(fullpath, &unused)) {
|
|
|
|
return fullpath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-11-08 17:38:31 +00:00
|
|
|
void ast_do_crash(void)
|
|
|
|
{
|
|
|
|
#if defined(DO_CRASH)
|
|
|
|
abort();
|
|
|
|
/*
|
|
|
|
* Just in case abort() doesn't work or something else super
|
|
|
|
* silly, and for Qwell's amusement.
|
|
|
|
*/
|
|
|
|
*((int *) 0) = 0;
|
|
|
|
#endif /* defined(DO_CRASH) */
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(AST_DEVMODE)
|
|
|
|
void __ast_assert_failed(int condition, const char *condition_str, const char *file, int line, const char *function)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Attempt to put it into the logger, but hope that at least
|
|
|
|
* someone saw the message on stderr ...
|
|
|
|
*/
|
|
|
|
ast_log(__LOG_ERROR, file, line, function, "FRACK!, Failed assertion %s (%d)\n",
|
|
|
|
condition_str, condition);
|
|
|
|
fprintf(stderr, "FRACK!, Failed assertion %s (%d) at line %d in %s of %s\n",
|
|
|
|
condition_str, condition, line, function, file);
|
|
|
|
/*
|
|
|
|
* Give the logger a chance to get the message out, just in case
|
|
|
|
* we abort(), or Asterisk crashes due to whatever problem just
|
|
|
|
* happened after we exit ast_assert().
|
|
|
|
*/
|
|
|
|
usleep(1);
|
|
|
|
ast_do_crash();
|
|
|
|
}
|
|
|
|
#endif /* defined(AST_DEVMODE) */
|