bug-cfengine
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Patch for detecting RHL advanced server release...


From: Phil D'Amore
Subject: Patch for detecting RHL advanced server release...
Date: Mon, 19 Aug 2002 17:56:48 -0400
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.0rc3) Gecko/20020524

Hello...

If anyone has run into the Red Hat Linux Advanced Server product, you have noticed that /etc/redhat-release file format has changed just enough to break cfengine's parsing of the file. Attached are 2 patches, one against 2.0.3, and one against 2.0.4pre, that replace the linux_redhat_version() function. That function changed by one line between releases so I figured I'd make one for each. AFAICT, the main change was to close the file if the function bailed early due to error. My new function opens the file, reads it, and closes it before doing anything else, so that should not be an issue. The new function handles the original Red Hat Linux, the Advanced Server, and the Mandrake variations of the file. For AS, it will add the following classes:

redhat
redhat_as
redhat_as_2
redhat_as_2_1

The original RHL and Mandrake files should cause the same results they always have.

Later,

--
Phil D'Amore                             I have always been here.
Senior System Administrator              For the first time your mind
Red Hat, Inc                             is quiet enough to hear me.
Office: 919.754.3700 x44395 Cell: 919.641.3669
diff -ru cfengine-2.0.3.orig/src/cfagent.c cfengine-2.0.3/src/cfagent.c
--- cfengine-2.0.3.orig/src/cfagent.c   Thu Jun 20 13:22:07 2002
+++ cfengine-2.0.3/src/cfagent.c        Mon Aug 19 17:36:42 2002
@@ -1825,80 +1825,108 @@
 
/*********************************************************************************/
 
 int linux_redhat_version(void)
-
-/* Contrib Pekka Savola */
-
 {
-#define FMT_SIZ 15
-#define REDHAT_ID "Red Hat"
+#define REDHAT_ID "Red Hat Linux"
+#define REDHAT_AS_ID "Red Hat Linux Advanced Server"
 #define MANDRAKE_ID "Linux Mandrake"
-#define RAWHIDE_ID "Raw Hide"
 
-/* Linux Mandrake release 7.1 (helium)
+#define RELEASE_FLAG "release "
+
+/* We are looking for one of the following strings...
+ *
  * Red Hat Linux release 6.2 (Zoot)
- * Raw Hide 20000526
+ * Red Hat Linux Advanced Server release 2.1AS (Pensacola)
+ * Linux Mandrake release 7.1 (helium)
  */
- 
-#define RH_REL_STR "%s %s"                     /* "Red Hat", "Linux Mandrake" 
*/
-#define RH_REL_STR_REDHAT "%*s %*s %u.%u %*s"  /* "Linux release 6.1 
(Cartman)" */
-#define RH_REL_STR_MANDRAKE "%*s %u.%u %*s"    /* "release 7.0 (Whatever)" */
+
 #define RH_REL_FILENAME "/etc/redhat-release"
 
 FILE *fp;
  
-char fmt1[maxvarsize]; 
-char fmt2[maxvarsize];
-char fmt[2*FMT_SIZ];
- 
-char *distro="";
+/* The full string red in from redhat-release */
+char relstring[maxvarsize]; 
+char classbuf[maxvarsize];
+
+/* Red Hat, Mandrake */
+char *vendor="";
+/* as (Advanced Server) */
+char *edition="";
+/* Where the numerical release will be found */
+char *release=NULL;
  
 int major = -1; 
-int release = -1; 
+char strmajor[maxvarsize];
+int minor = -1; 
+char strminor[maxvarsize];
 
+/* Grab the first line from the file and then close it. */
  if ((fp = fopen(RH_REL_FILENAME,"r")) == NULL)
     {
     return 1;
     }
+ fgets(relstring, sizeof(relstring), fp);
+ fclose(fp);
 
  Verbose("Looking for redhat linux info...\n");
- 
-/* Get necessary strings to distinguish between different distros using the 
same 
-   file name */
- 
- fscanf(fp, RH_REL_STR, fmt1, fmt2);
- 
-/* A few sanity checks */
- if (strlen(fmt1) >= FMT_SIZ || strlen(fmt2) >= FMT_SIZ )
-    {
-    Verbose("Your %s is corrupted, either one of it's first words is longer 
than %i chars\n", RH_REL_FILENAME, FMT_SIZ); 
-    return 2;
-    }
- 
- snprintf(fmt,maxvarsize,"%s %s", fmt1, fmt2);
- 
- if (!strncmp(fmt, REDHAT_ID, sizeof(fmt)))
-    {
-    fscanf(fp, RH_REL_STR_REDHAT, &major, &release );
-    Verbose("This appears to be a Redhat %u.%u system..", major, release);
-    distro="redhat";
-    }
- else if (!strncmp(fmt, MANDRAKE_ID, sizeof(fmt)))
-    {
-    fscanf(fp, RH_REL_STR_MANDRAKE, &major, &release );
-    Verbose("This appears to be a Mandrake %u.%u system..", major, release);
-    distro="mandrake";
-    }
- 
- fclose(fp);
 
- if ( major != -1 && release != -1 && distro != "" )
-    {
-    snprintf(fmt1,maxvarsize,"%s_%u", distro, major);
-    snprintf(fmt2,maxvarsize,"%s_%u_%u", distro, major, release);
-    
-    AddClassToHeap(fmt1);
-    AddClassToHeap(fmt2);
-    }
+ /* First, try to grok the vendor and the edition (if any) */
+ if(!strncmp(relstring, REDHAT_AS_ID, strlen(REDHAT_AS_ID)))
+ {
+         vendor = "redhat";
+         edition = "as";
+ }
+ else if(!strncmp(relstring, REDHAT_ID, strlen(REDHAT_ID)))
+ {
+         vendor = "redhat";
+ }
+ else if(!strncmp(relstring, MANDRAKE_ID, strlen(MANDRAKE_ID)))
+ {
+         vendor = "mandrake";
+ }
+ else
+ {
+         Verbose("Could not identify OS distro from %s\n", RH_REL_FILENAME);
+         return 2;
+ }
+
+ /* Now, grok the release.  For AS, we neglect the AS at the end of the
+  * numerical release because we already figured out that it *is* AS
+  * from the infomation above.  We assume that all the strings will
+  * have the word 'release' before the numerical release.
+  */
+ release = strstr(relstring, RELEASE_FLAG);
+ if(release == NULL)
+ {
+         Verbose("Could not find a numeric OS release in %s\n",
+                         RH_REL_FILENAME);
+         return 2;
+ }
+ else
+ {
+         release += strlen(RELEASE_FLAG);
+         sscanf(release, "%d.%d", &major, &minor);
+         sprintf(strmajor, "%d", major);
+         sprintf(strminor, "%d", minor);
+ }
+ 
+ if(major != -1 && minor != -1 && vendor != "")
+ {
+        classbuf[0] = '\0';
+         strcat(classbuf, vendor);
+         AddClassToHeap(classbuf);
+         strcat(classbuf, "_");
+         if(edition != "")
+         {
+                 strcat(classbuf, edition);
+                 AddClassToHeap(classbuf);
+                 strcat(classbuf, "_");
+         }
+         strcat(classbuf, strmajor);
+         AddClassToHeap(classbuf);
+         strcat(classbuf, "_");
+         strcat(classbuf, strminor);
+         AddClassToHeap(classbuf);
+ }
  return 0;
 }
 
diff -ru cfengine-2.0.4pre.orig/src/cfagent.c cfengine-2.0.4pre/src/cfagent.c
--- cfengine-2.0.4pre.orig/src/cfagent.c        Thu Aug  8 17:45:10 2002
+++ cfengine-2.0.4pre/src/cfagent.c     Mon Aug 19 17:37:14 2002
@@ -1855,81 +1855,108 @@
 
/*********************************************************************************/
 
 int linux_redhat_version(void)
-
-/* Contrib Pekka Savola */
-
 {
-#define FMT_SIZ 15
-#define REDHAT_ID "Red Hat"
+#define REDHAT_ID "Red Hat Linux"
+#define REDHAT_AS_ID "Red Hat Linux Advanced Server"
 #define MANDRAKE_ID "Linux Mandrake"
-#define RAWHIDE_ID "Raw Hide"
 
-/* Linux Mandrake release 7.1 (helium)
+#define RELEASE_FLAG "release "
+
+/* We are looking for one of the following strings...
+ *
  * Red Hat Linux release 6.2 (Zoot)
- * Raw Hide 20000526
+ * Red Hat Linux Advanced Server release 2.1AS (Pensacola)
+ * Linux Mandrake release 7.1 (helium)
  */
- 
-#define RH_REL_STR "%s %s"                     /* "Red Hat", "Linux Mandrake" 
*/
-#define RH_REL_STR_REDHAT "%*s %*s %u.%u %*s"  /* "Linux release 6.1 
(Cartman)" */
-#define RH_REL_STR_MANDRAKE "%*s %u.%u %*s"    /* "release 7.0 (Whatever)" */
+
 #define RH_REL_FILENAME "/etc/redhat-release"
 
 FILE *fp;
- 
-char fmt1[maxvarsize]; 
-char fmt2[maxvarsize];
-char fmt[2*FMT_SIZ];
- 
-char *distro="";
- 
-int major = -1; 
-int release = -1; 
 
+/* The full string red in from redhat-release */
+char relstring[maxvarsize];
+char classbuf[maxvarsize];
+
+/* Red Hat, Mandrake */
+char *vendor="";
+/* as (Advanced Server) */
+char *edition="";
+/* Where the numerical release will be found */
+char *release=NULL;
+
+int major = -1;
+char strmajor[maxvarsize];
+int minor = -1;
+char strminor[maxvarsize];
+
+/* Grab the first line from the file and then close it. */
  if ((fp = fopen(RH_REL_FILENAME,"r")) == NULL)
     {
     return 1;
     }
+ fgets(relstring, sizeof(relstring), fp);
+ fclose(fp);
 
  Verbose("Looking for redhat linux info...\n");
- 
-/* Get necessary strings to distinguish between different distros using the 
same 
-   file name */
- 
- fscanf(fp, RH_REL_STR, fmt1, fmt2);
- 
-/* A few sanity checks */
- if (strlen(fmt1) >= FMT_SIZ || strlen(fmt2) >= FMT_SIZ )
-    {
-    Verbose("Your %s is corrupted, either one of it's first words is longer 
than %i chars\n", RH_REL_FILENAME, FMT_SIZ);
-    fclose(fp);
-    return 2;
-    }
- 
- snprintf(fmt,maxvarsize,"%s %s", fmt1, fmt2);
- 
- if (!strncmp(fmt, REDHAT_ID, sizeof(fmt)))
-    {
-    fscanf(fp, RH_REL_STR_REDHAT, &major, &release );
-    Verbose("This appears to be a Redhat %u.%u system..", major, release);
-    distro="redhat";
-    }
- else if (!strncmp(fmt, MANDRAKE_ID, sizeof(fmt)))
-    {
-    fscanf(fp, RH_REL_STR_MANDRAKE, &major, &release );
-    Verbose("This appears to be a Mandrake %u.%u system..", major, release);
-    distro="mandrake";
-    }
- 
- fclose(fp);
 
- if ( major != -1 && release != -1 && distro != "" )
-    {
-    snprintf(fmt1,maxvarsize,"%s_%u", distro, major);
-    snprintf(fmt2,maxvarsize,"%s_%u_%u", distro, major, release);
-    
-    AddClassToHeap(fmt1);
-    AddClassToHeap(fmt2);
-    }
+ /* First, try to grok the vendor and the edition (if any) */
+ if(!strncmp(relstring, REDHAT_AS_ID, strlen(REDHAT_AS_ID)))
+ {
+         vendor = "redhat";
+         edition = "as";
+ }
+ else if(!strncmp(relstring, REDHAT_ID, strlen(REDHAT_ID)))
+ {
+         vendor = "redhat";
+ }
+ else if(!strncmp(relstring, MANDRAKE_ID, strlen(MANDRAKE_ID)))
+ {
+         vendor = "mandrake";
+ }
+ else
+ {
+         Verbose("Could not identify OS distro from %s\n", RH_REL_FILENAME);
+         return 2;
+ }
+
+ /* Now, grok the release.  For AS, we neglect the AS at the end of the
+  * numerical release because we already figured out that it *is* AS
+  * from the infomation above.  We assume that all the strings will
+  * have the word 'release' before the numerical release.
+  */
+ release = strstr(relstring, RELEASE_FLAG);
+ if(release == NULL)
+ {
+         Verbose("Could not find a numeric OS release in %s\n",
+                         RH_REL_FILENAME);
+         return 2;
+ }
+ else
+ {
+         release += strlen(RELEASE_FLAG);
+         sscanf(release, "%d.%d", &major, &minor);
+         sprintf(strmajor, "%d", major);
+         sprintf(strminor, "%d", minor);
+ }
+
+ if(major != -1 && minor != -1 && vendor != "")
+ {
+         classbuf[0] = '\0';
+         strcat(classbuf, vendor);
+         AddClassToHeap(classbuf);
+         strcat(classbuf, "_");
+         if(edition != "")
+         {
+                 strcat(classbuf, edition);
+                 AddClassToHeap(classbuf);
+                 strcat(classbuf, "_");
+         }
+         strcat(classbuf, strmajor);
+         AddClassToHeap(classbuf);
+         strcat(classbuf, "_");
+         strcat(classbuf, strminor);
+         AddClassToHeap(classbuf);
+ }
  return 0;
 }
 

reply via email to

[Prev in Thread] Current Thread [Next in Thread]