]> granicus.if.org Git - icinga2/commitdiff
Make UnameHelper() efficient 6854/head
authorAlexander A. Klimov <alexander.klimov@icinga.com>
Thu, 20 Dec 2018 14:48:10 +0000 (15:48 +0100)
committerAlexander A. Klimov <alexander.klimov@icinga.com>
Thu, 20 Dec 2018 15:37:04 +0000 (16:37 +0100)
refs #6452

lib/base/utility.cpp

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