On Fri, 21 Jan 2011 16:20:02 -0500
Adam Stokes <astokes(a)fedoraproject.org> wrote:
- to match against HWADDR in order to properly insert
the windows friendlyName.
---
src/CMakeLists.txt | 36 +++++++++++++++++++
src/os/win32/sigar_os.h | 29 +++++++++++----
src/os/win32/win32_sigar.c | 83
++++++++++++++++++++++++++++++++-----------
src/sigar_format.c | 2 +- tests/t_sigar_fs.c |
4 +- tests/t_sigar_proc.c | 4 +-
6 files changed, 124 insertions(+), 34 deletions(-)
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 83548d1..0ef3a4d 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -4,6 +4,29 @@ MESSAGE(STATUS "CMAKE_SYSTEM_NAME is
${CMAKE_SYSTEM_NAME}")
INCLUDE_DIRECTORIES(../include/)
+INCLUDE(CheckCSourceCompiles)
+
+MACRO (CHECK_STRUCT_MEMBER _STRUCT _MEMBER _HEADER _RESULT)
+ SET(_INCLUDE_FILES)
+ FOREACH (it ${_HEADER})
+ SET(_INCLUDE_FILES "${_INCLUDE_FILES}#include <${it}>\n")
+ ENDFOREACH (it)
+
+ SET(_CHECK_STRUCT_MEMBER_SOURCE_CODE "
+${_INCLUDE_FILES}
+int main()
+{
+ static ${_STRUCT} tmp;
+ if (sizeof(tmp.${_MEMBER}))
+ return 0;
+ return 0;
+}
+")
+ CHECK_C_SOURCE_COMPILES("${_CHECK_STRUCT_MEMBER_SOURCE_CODE}"
${_RESULT}) +
+ENDMACRO (CHECK_STRUCT_MEMBER)
+
+
## linux
IF(CMAKE_SYSTEM_NAME STREQUAL "Linux")
SET(SIGAR_SRC os/linux/linux_sigar.c)
@@ -60,6 +83,8 @@ IF(WIN32)
ADD_DEFINITIONS(-DSIGAR_SHARED)
SET(SIGAR_SRC os/win32/peb.c os/win32/win32_sigar.c)
INCLUDE_DIRECTORIES(os/win32)
+ CHECK_STRUCT_MEMBER(MIB_IPADDRROW wType "windows.h;iphlpapi.h"
wType_in_MIB_IPADDRROW)
+
add_definitions(-DHAVE_MIB_IPADDRROW_WTYPE=${wType_in_MIB_IPADDRROW})
ENDIF(WIN32)
SET(SIGAR_SRC ${SIGAR_SRC}
@@ -77,6 +102,17 @@ ADD_LIBRARY(sigar SHARED ${SIGAR_SRC})
IF(WIN32)
TARGET_LINK_LIBRARIES(sigar ws2_32 netapi32 version)
ENDIF(WIN32)
+
+#glib
+include(FindPkgConfig)
+pkg_check_modules(glib REQUIRED glib-2.0)
+if(glib_FOUND)
+ include_directories(${glib_INCLUDE_DIRS})
+ target_link_libraries(sigar ${glib_LIBRARIES})
+else(glib_FOUND)
+ message(FATAL_ERROR "Glib2 header/library not found.")
+endif(glib_FOUND)
+
IF(SIGAR_LINK_FLAGS)
SET_TARGET_PROPERTIES(sigar PROPERTIES LINK_FLAGS
"${SIGAR_LINK_FLAGS}") ENDIF(SIGAR_LINK_FLAGS)
diff --git a/src/os/win32/sigar_os.h b/src/os/win32/sigar_os.h
index ff2de7c..cd3c7e8 100755
--- a/src/os/win32/sigar_os.h
+++ b/src/os/win32/sigar_os.h
@@ -19,11 +19,15 @@
#ifndef SIGAR_OS_H
#define SIGAR_OS_H
+#ifdef MSVC
#if _MSC_VER <= 1200
#define SIGAR_USING_MSC6 /* Visual Studio version 6 */
#endif
+#else
+/* Cross compiling */
+#define _WIN32_WINNT 0x0501
+#endif
-#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winreg.h>
#include <winperf.h>
@@ -38,7 +42,22 @@
#include "sigar_util.h"
-#define INT64_C(val) val##i64
+#ifdef MSVC
+# define INT64_C(val) val##i64
+# define SIGAR_DLLFUNC(api, name) \
+ struct { \
+ const char *name; \
+ ##api##_##name func; \
+ } ##name
+#else
+/* The GCC compiler doesn't require/accept the ## prefix */
+# define INT64_C(val) val##L
+# define SIGAR_DLLFUNC(api, name) \
+ struct { \
+ const char *name; \
+ api##_##name func; \
+ } name
+#endif
/* see apr/include/arch/win32/atime.h */
#define EPOCH_DELTA INT64_C(11644473600000000)
@@ -457,12 +476,6 @@ typedef BOOL (CALLBACK
*mpr_get_net_connection)(LPCTSTR, LPTSTR,
LPDWORD);
-#define SIGAR_DLLFUNC(api, name) \
- struct { \
- const char *name; \
- ##api##_##name func; \
- } ##name
-
typedef struct {
sigar_dll_handle_t handle;
diff --git a/src/os/win32/win32_sigar.c b/src/os/win32/win32_sigar.c
index b9a5669..aca9758 100755
--- a/src/os/win32/win32_sigar.c
+++ b/src/os/win32/win32_sigar.c
@@ -23,6 +23,8 @@
#include "sigar_util.h"
#include "sigar_format.h"
#include <shellapi.h>
+#include <iphlpapi.h>
+#include <glib.h>
#define USING_WIDE_S(s) (s)->using_wide
#define USING_WIDE() USING_WIDE_S(sigar)
@@ -124,6 +126,9 @@ typedef enum {
#define MS_LOOPBACK_ADAPTER "Microsoft Loopback Adapter"
#define NETIF_LA "la"
+static int get_proc_info(sigar_t *sigar, sigar_pid_t pid);
+static int netif_hash(char *s);
+
sigar_uint64_t sigar_FileTimeToTime(FILETIME *ft)
{
sigar_uint64_t time;
@@ -374,6 +379,7 @@ static sigar_mpr_t sigar_mpr = {
{ NULL, NULL }
};
+#ifdef MSVC
#define DLLMOD_COPY(name) \
memcpy(&(sigar->##name), &sigar_##name, sizeof(sigar_##name))
@@ -382,6 +388,18 @@ static sigar_mpr_t sigar_mpr = {
#define DLLMOD_FREE(name) \
sigar_dllmod_free((sigar_dll_module_t *)&(sigar->##name))
+#else
+/* The GCC compiler doesn't require/accept the ## prefix */
+#define DLLMOD_COPY(name) \
+ memcpy(&(sigar->name), &sigar_##name, sizeof(sigar_##name))
+
+#define DLLMOD_INIT(name, all) \
+ sigar_dllmod_init(sigar, (sigar_dll_module_t *)&(sigar->name),
all) +
+#define DLLMOD_FREE(name) \
+ sigar_dllmod_free((sigar_dll_module_t *)&(sigar->name))
+#endif
+
static void sigar_dllmod_free(sigar_dll_module_t *module)
{
@@ -1470,7 +1488,9 @@ static int sigar_remote_proc_args_get(sigar_t
*sigar, sigar_pid_t pid, }
/* likely we are 32-bit, pid process is 64-bit */
+#ifdef MSVC
status = sigar_proc_args_wmi_get(sigar, pid, procargs);
+#endif
if (status == ERROR_NOT_FOUND) {
status = SIGAR_NO_SUCH_PROCESS;
}
@@ -1507,7 +1527,7 @@ static int sigar_proc_env_parse(UCHAR *ptr,
sigar_proc_env_t *procenv, break; /*XXX*/
}
- klen = val - ptr;
+ klen = val - (char*)ptr;
SIGAR_SSTRCPY(key, ptr);
key[klen] = '\0';
++val;
@@ -1640,6 +1660,7 @@ SIGAR_DECLARE(int) sigar_proc_exe_get(sigar_t
*sigar, sigar_pid_t pid, }
status = sigar_proc_exe_peb_get(sigar, proc, procexe);
+#ifdef MSVC
if (procexe->name[0] == '\0') {
/* likely we are 32-bit, pid process is 64-bit */
/* procexe->cwd[0] = XXX where else can we try? */
@@ -1648,7 +1669,7 @@ SIGAR_DECLARE(int) sigar_proc_exe_get(sigar_t
*sigar, sigar_pid_t pid, status = SIGAR_NO_SUCH_PROCESS;
}
}
-
+#endif
if (procexe->cwd[0] != '\0') {
/* strip trailing '\' */
int len = strlen(procexe->cwd);
@@ -2394,10 +2415,10 @@ static int sigar_get_netif_ipaddr(sigar_t
*sigar, MIB_IPADDRROW *row = &mib->table[i];
short type;
-#ifdef SIGAR_USING_MSC6
- type = row->unused2;
-#else
+#if HAVE_MIB_IPADDRROW_WTYPE
type = row->wType;
+#else
+ type = row->unused2;
#endif
if (!(type & MIB_IPADDR_PRIMARY)) {
continue;
@@ -2682,24 +2703,44 @@ sigar_net_interface_list_get(sigar_t *sigar,
for (i=0; i<ift->dwNumEntries; i++) {
char name[16];
- int key;
+ char wName[256];
+ int key, r;
MIB_IFROW *ifr = ift->table + i;
+ PIP_ADAPTER_ADDRESSES aa, addrs;
sigar_cache_entry_t *entry;
-
- if (strEQ(ifr->bDescr, MS_LOOPBACK_ADAPTER)) {
- /* special-case */
- sprintf(name, NETIF_LA "%d", la++);
- }
- else if (ifr->dwType == MIB_IF_TYPE_LOOPBACK) {
- sprintf(name, "lo%d", lo++);
- }
- else if ((ifr->dwType == MIB_IF_TYPE_ETHERNET) ||
- (ifr->dwType == IF_TYPE_IEEE80211))
- {
- sprintf(name, "eth%d", eth++);
- }
- else {
- continue; /*XXX*/
+ char *mac, *mac_cmp;
+
+ mac = g_strdup_printf("%02X%02X%02X%02X%02X%02X",
+ ifr->bPhysAddr[0],
+ ifr->bPhysAddr[1],
+ ifr->bPhysAddr[2],
+ ifr->bPhysAddr[3],
+ ifr->bPhysAddr[4],
+ ifr->bPhysAddr[5]);
+ r = sigar_get_adapters_addresses(sigar, AF_INET,
GAA_FLAG_INCLUDE_PREFIX, &aa);
+ if(r != SIGAR_OK)
+ return r;
+
+ for (addrs = aa; addrs; addrs = addrs->Next) {
+ mac_cmp = g_strdup_printf("%02X%02X%02X%02X%02X%02X",
+ addrs->PhysicalAddress[0],
+ addrs->PhysicalAddress[1],
+ addrs->PhysicalAddress[2],
+ addrs->PhysicalAddress[3],
+ addrs->PhysicalAddress[4],
+ addrs->PhysicalAddress[5]);
+ if(strcmp(mac, mac_cmp) != 0)
+ continue;
+ r = WideCharToMultiByte(CP_ACP,
+ 0,
+ addrs->FriendlyName,
+ -1,
+ wName,
+ sizeof(wName),
+ NULL,
+ NULL);
+ memset(name, 0, 256);
+ g_strlcpy(name, wName, 256);
}
if (iflist) {
diff --git a/src/sigar_format.c b/src/sigar_format.c
index 0ef9518..b1d6a0e 100644
--- a/src/sigar_format.c
+++ b/src/sigar_format.c
@@ -374,7 +374,7 @@ static char *sigar_inet_ntop(int af, const void
*src, char *dst, int cnt) return dst;
}
}
-#define sigar_inet_ntop_errno GetLastError()
+#define sigar_inet_ntop_errno SIGAR_ENOTIMPL
#else
#define sigar_inet_ntop inet_ntop
#define sigar_inet_ntop_errno errno
diff --git a/tests/t_sigar_fs.c b/tests/t_sigar_fs.c
index f298a08..7a554de 100644
--- a/tests/t_sigar_fs.c
+++ b/tests/t_sigar_fs.c
@@ -39,7 +39,7 @@
#include <stdio.h>
#include <string.h>
#include <errno.h>
-#if defined(_WIN32)
+#if defined(MSVC)
#include <WinError.h>
#endif
@@ -85,7 +85,7 @@ TEST(test_sigar_file_system_list_get) {
} else {
switch (ret) {
/* track the expected error code */
-#if defined(_WIN32)
+#if defined(MSVC)
case ERROR_NOT_READY:
break;
#endif
diff --git a/tests/t_sigar_proc.c b/tests/t_sigar_proc.c
index 88a9ac7..334bae3 100644
--- a/tests/t_sigar_proc.c
+++ b/tests/t_sigar_proc.c
@@ -39,7 +39,7 @@
#include <stdio.h>
#include <string.h>
#include <errno.h>
-#if defined(_WIN32)
+#if defined(MSVC)
#include <WinError.h>
#endif
@@ -127,7 +127,7 @@ TEST(test_sigar_proc_list_get) {
switch (ret) {
case EPERM:
case ESRCH:
-#if (defined(_WIN32))
+#if (defined(MSVC))
/* OpenProcess() may return
ERROR_ACCESS_DENIED */ case ERROR_ACCESS_DENIED:
#endif
So this includes the sigar mingw32 port along with my changes to the
network routines.
However, I'm still getting access violations and I believe it is
because of the way sigar caches its entries before applying to the
network structure