Merge pull request #6854 from Icinga/bugfix/unamehelper-inefficient-6452

Make UnameHelper() efficient
This commit is contained in:
Michael Friedrich 2019-02-11 16:57:17 +01:00 committed by GitHub
commit 7a865aefd9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -49,6 +49,7 @@
#ifndef _WIN32 #ifndef _WIN32
# include <sys/types.h> # include <sys/types.h>
# include <sys/utsname.h>
# include <pwd.h> # include <pwd.h>
# include <grp.h> # include <grp.h>
# include <errno.h> # include <errno.h>
@ -1463,28 +1464,23 @@ String Utility::UnescapeString(const String& s)
#ifndef _WIN32 #ifndef _WIN32
static String UnameHelper(char type) static String UnameHelper(char type)
{ {
/* Unfortunately the uname() system call doesn't support some of the struct utsname name;
* query types we're interested in - so we're using popen() instead. */ uname(&name);
char cmd[] = "uname -X 2>&1"; switch (type) {
cmd[7] = type; case 'm':
return (char*)name.machine;
FILE *fp = popen(cmd, "r"); case 'n':
return (char*)name.nodename;
if (!fp) case 'r':
return "Unknown"; return (char*)name.release;
case 's':
char line[1024]; return (char*)name.sysname;
std::ostringstream msgbuf; case 'v':
return (char*)name.version;
while (fgets(line, sizeof(line), fp)) default:
msgbuf << line; VERIFY(!"Invalid uname query.");
}
pclose(fp);
String result = msgbuf.str();
return result.Trim();
} }
#endif /* _WIN32 */ #endif /* _WIN32 */
static bool ReleaseHelper(String *platformName, String *platformVersion) static bool ReleaseHelper(String *platformName, String *platformVersion)