Index: channels/chan_iax2.c =================================================================== --- channels/chan_iax2.c (revision 7283) +++ channels/chan_iax2.c (working copy) @@ -1173,7 +1173,7 @@ last++; else last = s; - snprintf(s2, strlen(s) + 100, "/var/tmp/%s-%ld", last, (unsigned long)rand()); + snprintf(s2, strlen(s) + 100, "/var/tmp/%s-%ld", last, (unsigned long)ast_random()); res = stat(s, &stbuf); if (res < 0) { ast_log(LOG_WARNING, "Failed to stat '%s': %s\n", s, strerror(errno)); @@ -3111,7 +3111,7 @@ int res; struct iax_ie_data ied0; struct iax_ie_data ied1; - unsigned int transferid = rand(); + unsigned int transferid = ast_random(); memset(&ied0, 0, sizeof(ied0)); iax_ie_append_addr(&ied0, IAX_IE_APPARENT_ADDR, &iaxs[callno1]->addr); iax_ie_append_short(&ied0, IAX_IE_CALLNO, iaxs[callno1]->peercallno); @@ -3651,9 +3651,9 @@ ms = ast_tvdiff_ms(ast_tvnow(), p->rxcore); #ifdef IAXTESTS if (test_jit) { - if (!test_jitpct || ((100.0 * rand() / (RAND_MAX + 1.0)) < test_jitpct)) { - jit = (int)((float)test_jit * rand() / (RAND_MAX + 1.0)); - if ((int)(2.0 * rand() / (RAND_MAX + 1.0))) + if (!test_jitpct || ((100.0 * ast_random() / (RAND_MAX + 1.0)) < test_jitpct)) { + jit = (int)((float)test_jit * ast_random() / (RAND_MAX + 1.0)); + if ((int)(2.0 * ast_random() / (RAND_MAX + 1.0))) jit = -jit; ms += jit; } @@ -4921,7 +4921,7 @@ memset(&ied, 0, sizeof(ied)); iax_ie_append_short(&ied, IAX_IE_AUTHMETHODS, p->authmethods); if (p->authmethods & (IAX_AUTH_MD5 | IAX_AUTH_RSA)) { - snprintf(p->challenge, sizeof(p->challenge), "%d", rand()); + snprintf(p->challenge, sizeof(p->challenge), "%d", (int)ast_random()); iax_ie_append_str(&ied, IAX_IE_CHALLENGE, p->challenge); } if (p->encmethods) @@ -5727,7 +5727,7 @@ iax_ie_append_short(&ied, IAX_IE_AUTHMETHODS, p->authmethods); if (p->authmethods & (IAX_AUTH_RSA | IAX_AUTH_MD5)) { /* Build the challenge */ - snprintf(iaxs[callno]->challenge, sizeof(iaxs[callno]->challenge), "%d", rand()); + snprintf(iaxs[callno]->challenge, sizeof(iaxs[callno]->challenge), "%d", (int)ast_random()); iax_ie_append_str(&ied, IAX_IE_CHALLENGE, iaxs[callno]->challenge); } iax_ie_append_str(&ied, IAX_IE_USERNAME, name); @@ -6294,7 +6294,7 @@ return 1; } if(test_losspct) { /* simulate random loss condition */ - if( (100.0*rand()/(RAND_MAX+1.0)) < test_losspct) + if( (100.0*ast_random()/(RAND_MAX+1.0)) < test_losspct) return 1; } Index: asterisk.c =================================================================== --- asterisk.c (revision 7283) +++ asterisk.c (working copy) @@ -221,6 +221,8 @@ static int restartnow = 0; static pthread_t consolethread = AST_PTHREADT_NULL; +static char randompool[256]; + #if !defined(LOW_MEMORY) struct file_version { AST_LIST_ENTRY(file_version) list; @@ -2212,7 +2214,7 @@ Asterisk is started */ srand((unsigned int) getpid() + (unsigned int) time(NULL)); - srandom((unsigned int) getpid() + (unsigned int) time(NULL)); + initstate((unsigned int) getpid() + (unsigned int) time(NULL), randompool, sizeof(randompool)); if (init_logger()) { printf(term_quit()); Index: utils.c =================================================================== --- utils.c (revision 7283) +++ utils.c (working copy) @@ -878,6 +878,19 @@ #endif /* linux */ #endif /* !defined(_BSD_SOURCE) */ +/* glibc puts a lock inside random(3), so that the results are thread-safe. + * BSD libc (and others) do not. */ +#ifndef linux +long int ast_random(void) +{ + long int res; + ast_mutex_lock(&randomlock); + res = random(); + ast_mutex_unlock(&randomlock); + return res; +} +#endif + char *ast_process_quotes_and_slashes(char *start, char find, char replace_with) { char *dataPut = start; Index: include/asterisk/utils.h =================================================================== --- include/asterisk/utils.h (revision 7283) +++ include/asterisk/utils.h (working copy) @@ -235,4 +235,10 @@ int getloadavg(double *list, int nelem); #endif +#ifdef linux +#define ast_random random +#else +long int ast_random(void); +#endif + #endif /* _ASTERISK_UTILS_H */