diff --git a/asterisk.c b/asterisk.c index 026654e867a33f8dc7d05b575b42f808c36633b8..43cc189fef1e95345ca974baad5367b2addae0ba 100755 --- a/asterisk.c +++ b/asterisk.c @@ -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; } } diff --git a/utils.c b/utils.c index 203b098d89ac1a9bc01ffece7ab08e6a4c417ab7..054258ca625333c26891b1c6a799c112ab2fc0c8 100755 --- a/utils.c +++ b/utils.c @@ -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) {