determine version for various Linux releases

This commit is contained in:
Andreas Steffen
2011-09-10 22:39:55 +02:00
parent d883495418
commit 733ac2287f
2 changed files with 87 additions and 42 deletions
+1 -1
View File
@@ -33,7 +33,7 @@ INSERT INTO products (
INSERT INTO products ( INSERT INTO products (
name name
) VALUES ( ) VALUES (
'Gentoo UML i686' 'Gentoo Base System release 1.12.11.1 i686'
); );
/* Files */ /* Files */
+74 -29
View File
@@ -391,68 +391,113 @@ METHOD(pts_t, destroy, void,
static char* extract_platform_info(void) static char* extract_platform_info(void)
{ {
FILE *file; FILE *file;
const char description[] = "Description:"; char buf[BUF_LEN], *pos, *value = NULL;
char buf[BUF_LEN], *pos, *value; int i, len, value_len;
int value_len;
/* open a pipe stream for reading the output of the lsb_release commmand */ /* Linux/Unix distribution release info (from http://linuxmafia.com) */
file = popen("/usr/bin/lsb_release -d 2> /dev/null" , "r"); const char* releases[] = {
"/etc/lsb-release", "/etc/debian_version",
"/etc/SuSE-release", "/etc/novell-release",
"/etc/sles-release", "/etc/redhat-release",
"/etc/fedora-release", "/etc/gentoo-release",
"/etc/slackware-version", "/etc/annvix-release",
"/etc/arch-release", "/etc/arklinux-release",
"/etc/aurox-release", "/etc/blackcat-release",
"/etc/cobalt-release", "/etc/conectiva-release",
"/etc/debian_release", "/etc/immunix-release",
"/etc/lfs-release", "/etc/linuxppc-release",
"/etc/mandrake-release", "/etc/mandriva-release",
"/etc/mandrakelinux-release", "/etc/mklinux-release",
"/etc/pld-release", "/etc/redhat_version",
"/etc/slackware-release", "/etc/e-smith-release",
"/etc/release", "/etc/sun-release",
"/etc/tinysofa-release", "/etc/turbolinux-release",
"/etc/ultrapenguin-release", "/etc/UnitedLinux-release",
"/etc/va-release", "/etc/yellowdog-release"
};
const char description[] = "DISTRIB_DESCRIPTION=\"";
for (i = 0; i < countof(releases); i++)
{
file = fopen(releases[i], "r");
if (!file) if (!file)
{ {
DBG2(DBG_IMC, "popen failed for lsb_release command"); continue;
return NULL;
} }
fseek(file, 0, SEEK_END);
/* read the output the lsb_release command */ len = min(ftell(file), sizeof(buf)-1);
if (!fgets(buf, BUF_LEN-1, file)) rewind(file);
buf[len] = '\0';
if (fread(buf, 1, len, file) != len)
{ {
DBG2(DBG_IMC, "failed to read output of lsb_release command"); DBG1(DBG_IMC, "failed to read file '%s'", releases[i]);
pclose(file); fclose(file);
return NULL; return NULL;
} }
pclose(file); fclose(file);
if (i == 0) /* LSB release */
{
pos = strstr(buf, description); pos = strstr(buf, description);
if (!pos) if (!pos)
{ {
DBG2(DBG_IMC, "failed to find lsb_release description field"); DBG1(DBG_IMC, "failed to find begin of lsb-release "
"DESCRIPTION field");
return NULL; return NULL;
} }
value = pos + strlen(description); value = pos + strlen(description);
pos = strchr(value, '"');
/* eat whitespace */ if (!pos)
while (*value == ' ' || *value == '\t')
{ {
value++; DBG1(DBG_IMC, "failed to find end of lsb-release "
"DESCRIPTION field");
return NULL;
}
}
else
{
value = buf;
pos = strchr(value, '\n');
if (!pos)
{
DBG1(DBG_IMC, "failed to find end of release string");
return NULL;
}
}
value_len = pos - value;
value[value_len] = ' ';
break;
}
if (!value)
{
DBG1(DBG_IMC, "no distribution release file found");
return NULL;
} }
/* remove newline at the end and move value to the front of the buffer */
value_len = strlen(value) - 1;
memcpy(buf, value, value_len);
buf[value_len] = ' ';
/* open a pipe stream for reading the output of the arch commmand */ /* open a pipe stream for reading the output of the arch commmand */
file = popen("/usr/bin/arch 2> /dev/null" , "r"); file = popen("/usr/bin/arch 2> /dev/null" , "r");
if (!file) if (!file)
{ {
DBG2(DBG_IMC, "popen failed for arch command"); DBG1(DBG_IMC, "popen failed for arch command");
return NULL; return NULL;
} }
/* read the output the arch command */ /* read the output the arch command */
if (!fgets(buf + value_len + 1, BUF_LEN - value_len - 2, file)) len = BUF_LEN - (value - buf) - value_len - 2;
if (!fgets(value + value_len + 1, len, file))
{ {
DBG2(DBG_IMC, "failed to read output of arch command"); DBG1(DBG_IMC, "failed to read output of arch command");
pclose(file); pclose(file);
return NULL; return NULL;
} }
pclose(file); pclose(file);
/* remove newline at the end */ /* remove newline at the end */
buf[strlen(buf)-1] = '\0'; value[strlen(value)-1] = '\0';
DBG1(DBG_IMV, "platform is '%s'", buf); DBG1(DBG_IMV, "platform is '%s'", value);
return strdup(buf); return strdup(value);
} }
/** /**