provide an alternate getloadavg implementation and a fallback for systems that don't have it at all (issue #5549 with minor mods)

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@6903 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Kevin P. Fleming 2005-10-31 21:25:21 +00:00
parent f9c8bb8087
commit c3f9388b23
2 changed files with 48 additions and 7 deletions

View File

@ -1874,7 +1874,12 @@ static void ast_readconfig(void) {
option_maxcalls = 0;
}
} else if (!strcasecmp(v->name, "maxload")) {
if ((sscanf(v->value, "%lf", &option_maxload) != 1) || (option_maxload < 0.0)) {
double test[1];
if (getloadavg(test, 1) == -1) {
ast_log(LOG_ERROR, "Cannot obtain load average on this system. 'maxload' option disabled.\n");
option_maxload = 0.0;
} else if ((sscanf(v->value, "%lf", &option_maxload) != 1) || (option_maxload < 0.0)) {
option_maxload = 0.0;
}
}

48
utils.c
View File

@ -481,7 +481,7 @@ int ast_utils_init(void)
#ifndef __linux__
#undef pthread_create /* For ast_pthread_create function only */
#endif /* ! LINUX */
#endif /* !__linux__ */
int ast_pthread_create_stack(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *data, size_t stacksize)
{
@ -691,7 +691,7 @@ char *strcasestr(const char *haystack, const char *needle)
return NULL;
}
}
#endif
#endif /* !HAVE_STRCASESTR */
#ifndef HAVE_STRNLEN
size_t strnlen(const char *s, size_t n)
@ -704,7 +704,7 @@ size_t strnlen(const char *s, size_t n)
return len;
}
#endif
#endif /* !HAVE_STRNLEN */
#if !defined(HAVE_STRNDUP) && !defined(__AST_DEBUG_MALLOC)
char *strndup(const char *s, size_t n)
@ -718,7 +718,7 @@ char *strndup(const char *s, size_t n)
new[len] = '\0';
return memcpy(new, s, len);
}
#endif
#endif /* !defined(HAVE_STRNDUP) && !defined(__AST_DEBUG_MALLOC) */
#if !defined(HAVE_VASPRINTF) && !defined(__AST_DEBUG_MALLOC)
int vasprintf(char **strp, const char *fmt, va_list ap)
@ -738,7 +738,7 @@ int vasprintf(char **strp, const char *fmt, va_list ap)
return size;
}
#endif
#endif /* !defined(HAVE_VASPRINTF) && !defined(__AST_DEBUG_MALLOC) */
#ifndef HAVE_STRTOQ
#define LONG_MIN (-9223372036854775807L-1L)
@ -835,7 +835,43 @@ uint64_t strtoq(const char *nptr, char **endptr, int base)
*((const char **)endptr) = any ? s - 1 : nptr;
return acc;
}
#endif
#endif /* !HAVE_STRTOQ */
#if (!defined(getloadavg))
#ifdef linux
/* Alternative method of getting load avg on Linux only */
int getloadavg(double *list, int nelem)
{
FILE *LOADAVG;
double avg[3] = { 0.0, 0.0, 0.0 };
int i, res = -1;
if ((LOADAVG = fopen("/proc/loadavg", "r"))) {
fscanf(LOADAVG, "%lf %lf %lf", &avg[0], &avg[1], &avg[2]);
res = 0;
fclose(LOADAVG);
}
for (i = 0; (i < nelem) && (i < 3); i++) {
list[i] = avg[i];
}
return res;
}
#else /* !linux */
/* Return something that won't cancel the call, but still return -1, in case
* we correct the implementation to check return value */
int getloadavg(double *list, int nelem)
{
int i;
for (i = 0; i < nelem; i++) {
list[i] = 0.1;
}
return -1;
}
#endif /* linux */
#endif /* !defined(getloadavg) */
char *ast_process_quotes_and_slashes(char *start, char find, char replace_with)
{