[icinga-checkins] icinga.org: icinga-core/mfriedrich/cgis: core: revamp the detection of embedded perl usage directive "# icinga: +epn" ( Andreas Ericsson) #2197

git at icinga.org git at icinga.org
Wed Aug 1 03:00:45 CEST 2012


Module: icinga-core
Branch: mfriedrich/cgis
Commit: 9707f13287e4788ff73d6d9ec212b9a795584acb
URL:    https://git.icinga.org/?p=icinga-core.git;a=commit;h=9707f13287e4788ff73d6d9ec212b9a795584acb

Author: Michael Friedrich <michael.friedrich at univie.ac.at>
Date:   Fri Jul  6 18:33:01 2012 +0200

core: revamp the detection of embedded perl usage directive "# icinga: +epn" (Andreas Ericsson) #2197

patch is revamping the detection of the first 10 lines containing the
"#icinga: +epn" line.

previously, the core would have read only the first 80 characters from
each line via fgets, which could be to less. the inner handling is
changed to 256 chars, which should be sufficient. removing the strtok on
':' is also a good idea, as this can be done via the strstr pointer
match on 'epn' as well, calculating the return value based on the
character ('+' means embedded perl enabled).

refs #2197

---

 Changelog    |    1 +
 base/utils.c |   89 +++++++++++++++++++++++----------------------------------
 2 files changed, 37 insertions(+), 53 deletions(-)

diff --git a/Changelog b/Changelog
index 41b1777..e90697d 100644
--- a/Changelog
+++ b/Changelog
@@ -17,6 +17,7 @@ ENHANCEMENTS
 FIXES
 * core: unify check scheduling replacement logic for new events (Andreas Ericsson) #2676
 * core: get rid of the instame macro usage while logging alerts and states (Andreas Ericsson) #2665
+* core: revamp the detection of embedded perl usage directive "# icinga: +epn" (Andreas Ericsson) #2197
 
 CHANGES
 * icinga.spec: add devel package #2634
diff --git a/base/utils.c b/base/utils.c
index c7acf0a..9ae8304 100644
--- a/base/utils.c
+++ b/base/utils.c
@@ -3581,72 +3581,55 @@ int deinit_embedded_perl(void) {
 
 /* checks to see if we should run a script using the embedded Perl interpreter */
 int file_uses_embedded_perl(char *fname) {
-	int use_epn = FALSE;
-#ifdef EMBEDDEDPERL
+#ifndef EMBEDDEDPERL
+	return FALSE;
+#else
+	int line, use_epn = FALSE;
 	FILE *fp = NULL;
-	char line1[80] = "";
-	char linen[80] = "";
-	int line = 0;
-	char *ptr = NULL;
-	int found_epn_directive = FALSE;
-
-	if (enable_embedded_perl == TRUE) {
-
-		/* open the file, check if its a Perl script and see if we can use epn  */
-		fp = fopen(fname, "r");
-		if (fp != NULL) {
-
-			/* grab the first line - we should see Perl */
-			fgets(line1, 80, fp);
+	char buf[256] = "";
 
-			/* yep, its a Perl script... */
-			if (strstr(line1, "/bin/perl") != NULL) {
-
-				/* epn directives must be found in first ten lines of plugin */
-				for (line = 1; line < 10; line++) {
-
-					if (fgets(linen, 80, fp)) {
-
-						/* line contains Icinga directives - keep Nagios compatibility */
-						if (strstr(linen, "# nagios:") || strstr(linen, "# icinga:")) {
-
-							ptr = strtok(linen, ":");
+	if (enable_embedded_perl != TRUE)
+		return FALSE;
 
-							/* process each directive */
-							for (ptr = strtok(NULL, ","); ptr != NULL; ptr = strtok(NULL, ",")) {
 
-								strip(ptr);
+	/* open the file, check if its a Perl script and see if we can use epn  */
+	fp = fopen(fname, "r");
+	if (fp == NULL)
+		return FALSE;
 
-								if (!strcmp(ptr, "+epn")) {
-									use_epn = TRUE;
-									found_epn_directive = TRUE;
-								} else if (!strcmp(ptr, "-epn")) {
-									use_epn = FALSE;
-									found_epn_directive = TRUE;
-								}
-							}
-						}
+	/* grab the first line - we should see Perl. go home if not */
+	if (fgets(line1, 80, fp) == NULL || strstr(buf, "/bin/perl") == NULL) {
+		fclose(fp);
+	}
 
-						if (found_epn_directive == TRUE)
-							break;
-					}
+	/* epn directives must be found in first ten lines of plugin */
+	for (line = 1; line < 10; line++) {
+		if (fgets(buf, sizeof(buf) - 1, fp) == NULL)
+			break;
 
-					/* EOF */
-					else
-						break;
-				}
+		buf[sizeof(buf) - 1] = '\0';
 
-				/* if the plugin didn't tell us whether or not to use embedded Perl, use implicit value */
-				if (found_epn_directive == FALSE)
-					use_epn = (use_embedded_perl_implicitly == TRUE) ? TRUE : FALSE;
-			}
+		/* line contains Icinga directives - keep Nagios compatibility */
+		if (strstr(linen, "# nagios:") || strstr(linen, "# icinga:")) {
+			char *p;
+			p = strstr(buf + 8, "epn");
+			if (!p)
+				continue;
 
+			/*
+			 * we found it, so close the file and return
+			 * whatever it shows. '+epn' means yes. everything
+			 * else means no.
+			 */
 			fclose(fp);
+			return *(p - 1) == '+' ? TRUE : FALSE;
 		}
 	}
-#endif
 
-	return use_epn;
+	fclose(fp);
+
+	return use_embedded_perl_implicitly;
+#endif
 }
 
 





More information about the icinga-checkins mailing list