From c3f9388b2399a5db44ee464d5c20276763aa75f1 Mon Sep 17 00:00:00 2001 From: "Kevin P. Fleming" Date: Mon, 31 Oct 2005 21:25:21 +0000 Subject: 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 --- utils.c | 48 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 6 deletions(-) (limited to 'utils.c') diff --git a/utils.c b/utils.c index 203b098d8..054258ca6 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) { -- cgit v1.2.3