Also introduces a new system for interacting with WMI via external
scripts. A new source module, util.c, contains the method
exec_and_capture_output(char*, char**). It takes as input a command line
to execute and a pointer to a buffer to be allocated and filled with the
output from the command.
Signed-off-by: Darryl L. Pierce <dpierce(a)redhat.com>
---
src/Makefile.am | 7 ++-
src/processor.cpp | 12 ++++++
src/util.c | 76 +++++++++++++++++++++++++++++++++++++
src/util.h | 37 ++++++++++++++++++
src/windows/win_get_cpu_model.vbs | 30 ++++++++++++++
5 files changed, 160 insertions(+), 2 deletions(-)
create mode 100644 src/util.c
create mode 100644 src/util.h
create mode 100644 src/windows/win_get_cpu_model.vbs
diff --git a/src/Makefile.am b/src/Makefile.am
index c56a900..0910d86 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -28,10 +28,13 @@ matahari_SOURCES = \
main.cpp \
processor.cpp \
processor.h \
+ util.c \
+ util.h \
qmf/hostagent.cpp \
qmf/hostagent.h \
qmf/processoragent.cpp \
- qmf/processoragent.h
+ qmf/processoragent.h \
+ windows/win_get_cpu_model.vbs
SCHEMAS = \
schema-host.xml
@@ -39,7 +42,7 @@ SCHEMAS = \
$(generated_file_list): $(SCHEMAS)
qmf-gen -o ./qmf $(SCHEMAS)
-BUILT_SOURCES = $(generated_file_list)
+BUILT_SOURCES = $(generated_file_list) $(windows_scripts)
CLEANFILES = $(generated_file_list)
matahari_CPPFLAGS = -fno-strict-aliasing
diff --git a/src/processor.cpp b/src/processor.cpp
index 65de27e..9a3c25e 100644
--- a/src/processor.cpp
+++ b/src/processor.cpp
@@ -25,6 +25,7 @@
#include "processor.h"
#include <stdexcept>
#include <string>
+#include "util.h"
using namespace std;
@@ -162,6 +163,17 @@ cpu_get_details()
free(buffer);
buffer = NULL;
}
+
+ // get the processor model
+ char* model = NULL;
+
+ if(!exec_and_capture_text("cscript.exe /nologo win_get_cpu_model.vbs",
+ model)
+ {
+ cpuinfo.model = string(model);
+ }
+
+ free(model);
}
#endif
}
diff --git a/src/util.c b/src/util.c
new file mode 100644
index 0000000..6be88de
--- /dev/null
+++ b/src/util.c
@@ -0,0 +1,76 @@
+/* util.c - Copyright (C) 2010 Red Hat, Inc.
+ * Written by Darryl L. Pierce <dpierce(a)redhat.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA. A copy of the GNU General Public License is
+ * also available at
http://www.gnu.org/copyleft/gpl.html.
+ */
+
+#include "config.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include "util.h"
+
+#ifdef __linux__
+
+#include <string.h>
+
+#define POPEN_MODE "r"
+
+#elif defined WIN32
+
+#include <memory.h>
+
+#define popen(fname, mode) _popen(fname, mode)
+#define pclose(x) _pclose(x)
+
+#define POPEN_MODE "rt"
+
+#endif
+
+int
+exec_and_capture_text(char* cmdline, char** output)
+{
+ int result = 1;
+
+ // initial the output
+ *output = NULL;
+
+ if(cmdline != NULL)
+ {
+ FILE* input;
+ int offset = 0;
+
+ if((input = popen(cmdline, POPEN_MODE)) != NULL)
+ {
+ while(!feof(input))
+ {
+ char buffer[256];
+ int bytesread;
+
+ memset(buffer, 256, 0);
+ bytesread = fread(buffer, 1, 256, input);
+ *output = (char*)realloc(*output, offset + bytesread + 1);
+ memcpy((*output) + offset, buffer, bytesread);
+ offset += bytesread;
+ ((char*)(*output))[offset] = '\0';
+ }
+
+ pclose(input);
+ result = 0;
+ }
+ }
+
+ return result;
+}
diff --git a/src/util.h b/src/util.h
new file mode 100644
index 0000000..df21bc7
--- /dev/null
+++ b/src/util.h
@@ -0,0 +1,37 @@
+#ifndef __UTIL_H
+#define __UTIL_H
+
+/* util.h - Copyright (C) 2010 Red Hat, Inc.
+ * Written by Darryl L. Pierce <dpierce(a)redhat.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA. A copy of the GNU General Public License is
+ * also available at
http://www.gnu.org/copyleft/gpl.html.
+ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+ /* Executes an external program and captures the output,
+ returning the captured text. The output buffer is allocated
+ by this function and the caller is responsible for releasing
+ the memory. */
+ int exec_and_capture_text(char* cmdline, char** output);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/src/windows/win_get_cpu_model.vbs b/src/windows/win_get_cpu_model.vbs
new file mode 100644
index 0000000..5fe1030
--- /dev/null
+++ b/src/windows/win_get_cpu_model.vbs
@@ -0,0 +1,30 @@
+' ---------------------------------------------------------------------
+' win_get_cpu_model.vbs - Copyright (C) 2010, Red Hat.
+' Written by Darryl L. Pierce <dpierce(a)redhat.com>
+'
+' This program is free software: you can redistribute it and/or modify
+' it under the terms of the GNU General Public License as published by
+' the Free Software Foundation, either version 3 of the License, or
+' (at your option) any later version.
+'
+' This program is distributed in the hope that it will be useful,
+' but WITHOUT ANY WARRANTY; without even the implied warranty of
+' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+' GNU General Public License for more details.
+'
+' You should have received a copy of the GNU General Public License
+' along with this program. If not, see <
http://www.gnu.org/licenses/>.
+' ---------------------------------------------------------------------
+
+strComputer = "."
+Set objWMIService = GetObject("winmgmts:\\" & strComputer &
"\root\cimv2")
+Set processors = objWMIService.ExecQuery("select * from Win32_Processor")
+
+displayed = false
+
+For Each processor in processors
+ if Not displayed Then
+ Wscript.echo "NAME=" & processor.Name
+ displayed = true
+ end if
+Next
--
1.7.2.1