[Asterisk-cvs] asterisk asterisk.c,1.186,1.187 utils.c,1.80,1.81

kpfleming kpfleming
Mon Oct 31 16:33:16 CST 2005


Update of /usr/cvsroot/asterisk
In directory mongoose.digium.com:/tmp/cvs-serv5345

Modified Files:
	asterisk.c utils.c 
Log Message:
provide an alternate getloadavg implementation and a fallback for systems that don't have it at all (issue #5549 with minor mods)


Index: asterisk.c
===================================================================
RCS file: /usr/cvsroot/asterisk/asterisk.c,v
retrieving revision 1.186
retrieving revision 1.187
diff -u -d -r1.186 -r1.187
--- asterisk.c	26 Oct 2005 18:54:24 -0000	1.186
+++ asterisk.c	31 Oct 2005 21:25:21 -0000	1.187
@@ -1874,7 +1874,12 @@
 				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;
 			}
 		}

Index: utils.c
===================================================================
RCS file: /usr/cvsroot/asterisk/utils.c,v
retrieving revision 1.80
retrieving revision 1.81
diff -u -d -r1.80 -r1.81
--- utils.c	31 Oct 2005 15:21:12 -0000	1.80
+++ utils.c	31 Oct 2005 21:25:21 -0000	1.81
@@ -481,7 +481,7 @@
 
 #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 @@
 		return NULL;
 	}
 }
-#endif
+#endif /* !HAVE_STRCASESTR */
 
 #ifndef HAVE_STRNLEN
 size_t strnlen(const char *s, size_t n)
@@ -704,7 +704,7 @@
 
 	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 @@
 	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 @@
 
 	return size;
 }
-#endif
+#endif /* !defined(HAVE_VASPRINTF) && !defined(__AST_DEBUG_MALLOC) */
 
 #ifndef HAVE_STRTOQ
 #define LONG_MIN        (-9223372036854775807L-1L)
@@ -835,7 +835,43 @@
 	         *((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)
 {




More information about the svn-commits mailing list