go bindings

statically build go with sigar using visual studio 2015 IDE
This commit is contained in:
ilanuriel 2016-01-04 12:14:32 +02:00
parent 7a6aefc7fb
commit 0f0dbe34ce
88 changed files with 17394 additions and 0 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
go_bindings/Sigar/Sigar.sdf Normal file

Binary file not shown.

View File

@ -0,0 +1,28 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.24720.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Sigar", "Sigar\Sigar.vcxproj", "{BB39C6EE-CA93-4F77-9372-4767EAE8838C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{BB39C6EE-CA93-4F77-9372-4767EAE8838C}.Debug|x64.ActiveCfg = Debug|x64
{BB39C6EE-CA93-4F77-9372-4767EAE8838C}.Debug|x64.Build.0 = Debug|x64
{BB39C6EE-CA93-4F77-9372-4767EAE8838C}.Debug|x86.ActiveCfg = Debug|Win32
{BB39C6EE-CA93-4F77-9372-4767EAE8838C}.Debug|x86.Build.0 = Debug|Win32
{BB39C6EE-CA93-4F77-9372-4767EAE8838C}.Release|x64.ActiveCfg = Release|x64
{BB39C6EE-CA93-4F77-9372-4767EAE8838C}.Release|x64.Build.0 = Release|x64
{BB39C6EE-CA93-4F77-9372-4767EAE8838C}.Release|x86.ActiveCfg = Release|Win32
{BB39C6EE-CA93-4F77-9372-4767EAE8838C}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,7 @@
 peb.c
wmi.cpp
c:\users\iuriel\documents\visual studio 2015\projects\sigar\src\wmi.cpp(202): warning C4244: 'argument': conversion from 'sigar_pid_t' to 'DWORD', possible loss of data
c:\users\iuriel\documents\visual studio 2015\projects\sigar\src\wmi.cpp(228): warning C4244: 'argument': conversion from 'sigar_pid_t' to 'DWORD', possible loss of data
Creating library c:\users\iuriel\documents\visual studio 2015\Projects\Sigar\Debug\Sigar.lib and object c:\users\iuriel\documents\visual studio 2015\Projects\Sigar\Debug\Sigar.exp
Sigar.vcxproj -> c:\users\iuriel\documents\visual studio 2015\Projects\Sigar\Debug\Sigar.dll
Sigar.vcxproj -> c:\users\iuriel\documents\visual studio 2015\Projects\Sigar\Debug\Sigar.pdb (Partial PDB)

View File

@ -0,0 +1,2 @@
#TargetFrameworkVersion=v4.0:PlatformToolSet=v140:EnableManagedIncrementalBuild=false:VCToolArchitecture=Native32Bit:WindowsTargetPlatformVersion=8.1
Debug|Win32|c:\users\iuriel\documents\visual studio 2015\Projects\Sigar\|

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,168 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{BB39C6EE-CA93-4F77-9372-4767EAE8838C}</ProjectGuid>
<RootNamespace>Sigar</RootNamespace>
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<TargetName>sigar-amd64-winnt</TargetName>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<TargetName>sigar-amd64-winnt</TargetName>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<TargetName>sigar-amd64-winnt</TargetName>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<TargetName>sigar-amd64-winnt</TargetName>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>false</SDLCheck>
<AdditionalIncludeDirectories>C:\Users\iuriel\Documents\Visual Studio 2015\Projects\Sigar\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>WIN32;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<AdditionalDependencies>ws2_32.lib;version.lib;netapi32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>false</SDLCheck>
<AdditionalIncludeDirectories>C:\Users\iuriel\Documents\Visual Studio 2015\Projects\Sigar\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>WIN32;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<AdditionalDependencies>ws2_32.lib;version.lib;netapi32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>false</SDLCheck>
<AdditionalIncludeDirectories>C:\Users\iuriel\Documents\Visual Studio 2015\Projects\Sigar\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>WIN32;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>ws2_32.lib;version.lib;netapi32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>false</SDLCheck>
<AdditionalIncludeDirectories>C:\Users\iuriel\Documents\Visual Studio 2015\Projects\Sigar\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>WIN32;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>ws2_32.lib;version.lib;netapi32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="..\include\sigar.h" />
<ClInclude Include="..\include\sigar_fileinfo.h" />
<ClInclude Include="..\include\sigar_format.h" />
<ClInclude Include="..\include\sigar_getline.h" />
<ClInclude Include="..\include\sigar_log.h" />
<ClInclude Include="..\include\sigar_os.h" />
<ClInclude Include="..\include\sigar_private.h" />
<ClInclude Include="..\include\sigar_ptql.h" />
<ClInclude Include="..\include\sigar_util.h" />
<ClInclude Include="..\src\os\win32\sigar_pdh.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\src\os\win32\win32_sigar.c" />
<ClCompile Include="..\src\peb.c" />
<ClCompile Include="..\src\sigar.c" />
<ClCompile Include="..\src\sigar_cache.c" />
<ClCompile Include="..\src\sigar_fileinfo.c" />
<ClCompile Include="..\src\sigar_format.c" />
<ClCompile Include="..\src\sigar_getline.c" />
<ClCompile Include="..\src\sigar_ptql.c" />
<ClCompile Include="..\src\sigar_signal.c" />
<ClCompile Include="..\src\sigar_util.c" />
<ClCompile Include="..\src\wmi.cpp" />
<ClCompile Include="sigar_version.c" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@ -0,0 +1,87 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\include\sigar.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\include\sigar_fileinfo.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\include\sigar_format.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\include\sigar_getline.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\include\sigar_log.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\include\sigar_private.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\include\sigar_ptql.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\include\sigar_util.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\src\os\win32\sigar_pdh.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\include\sigar_os.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\src\sigar.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\sigar_cache.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\sigar_fileinfo.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\sigar_format.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\sigar_getline.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\sigar_ptql.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\sigar_signal.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\sigar_util.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\os\win32\win32_sigar.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\peb.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\wmi.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="sigar_version.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
</Project>

View File

@ -0,0 +1,21 @@
#include "sigar.h"
static sigar_version_t sigar_version = {
"2004-2011",
"1",
"7",
"0",
"0",
"sigar",
"1.66",
2,
17122014,
1,
6
};
SIGAR_DECLARE(sigar_version_t *) sigar_version_get(void)
{
return &sigar_version;
}

View File

@ -0,0 +1,30 @@
 sigar.c
c:\users\iuriel\documents\visual studio 2015\projects\sigar\src\sigar.c(52): warning C4996: 'getenv': This function or variable may be unsafe. Consider using _dupenv_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
c:\program files (x86)\windows kits\10\include\10.0.10240.0\ucrt\stdlib.h(1183): note: see declaration of 'getenv'
c:\users\iuriel\documents\visual studio 2015\projects\sigar\src\sigar.c(114): warning C4013: 'getpid' undefined; assuming extern returning int
c:\users\iuriel\documents\visual studio 2015\projects\sigar\src\sigar.c(210): warning C4244: '=': conversion from 'double' to 'sigar_uint64_t', possible loss of data
c:\users\iuriel\documents\visual studio 2015\projects\sigar\src\sigar.c(299): warning C4018: '<': signed/unsigned mismatch
c:\users\iuriel\documents\visual studio 2015\projects\sigar\src\sigar.c(598): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
c:\program files (x86)\windows kits\10\include\10.0.10240.0\ucrt\string.h(119): note: see declaration of 'strcpy'
c:\users\iuriel\documents\visual studio 2015\projects\sigar\src\sigar.c(1249): warning C4311: 'type cast': pointer truncation from 'MEMORY_BASIC_INFORMATION *' to 'DWORD'
c:\users\iuriel\documents\visual studio 2015\projects\sigar\src\sigar.c(1249): warning C4311: 'type cast': pointer truncation from 'PVOID' to 'DWORD'
c:\users\iuriel\documents\visual studio 2015\projects\sigar\src\sigar.c(1251): warning C4311: 'type cast': pointer truncation from 'PVOID' to 'DWORD'
c:\users\iuriel\documents\visual studio 2015\projects\sigar\src\sigar.c(1252): warning C4311: 'type cast': pointer truncation from 'PVOID' to 'DWORD'
c:\users\iuriel\documents\visual studio 2015\projects\sigar\src\sigar.c(1978): warning C4018: '<': signed/unsigned mismatch
c:\users\iuriel\documents\visual studio 2015\projects\sigar\src\sigar.c(2064): warning C4996: 'gethostbyname': Use getaddrinfo() or GetAddrInfoW() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings
c:\program files (x86)\windows kits\8.1\include\um\winsock2.h(2238): note: see declaration of 'gethostbyname'
c:\users\iuriel\documents\visual studio 2015\projects\sigar\src\sigar.c(2096): warning C4996: 'gethostbyaddr': Use getnameinfo() or GetNameInfoW() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings
c:\program files (x86)\windows kits\8.1\include\um\winsock2.h(2216): note: see declaration of 'gethostbyaddr'
c:\users\iuriel\documents\visual studio 2015\projects\sigar\src\sigar.c(2155): warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
c:\program files (x86)\windows kits\10\include\10.0.10240.0\ucrt\string.h(346): note: see declaration of 'strncpy'
c:\users\iuriel\documents\visual studio 2015\projects\sigar\src\sigar.c(2172): warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
c:\program files (x86)\windows kits\10\include\10.0.10240.0\ucrt\string.h(346): note: see declaration of 'strncpy'
c:\users\iuriel\documents\visual studio 2015\projects\sigar\src\sigar.c(2219): warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
c:\program files (x86)\windows kits\10\include\10.0.10240.0\ucrt\string.h(346): note: see declaration of 'strncpy'
c:\users\iuriel\documents\visual studio 2015\projects\sigar\src\sigar.c(2235): warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
c:\program files (x86)\windows kits\10\include\10.0.10240.0\ucrt\string.h(346): note: see declaration of 'strncpy'
c:\users\iuriel\documents\visual studio 2015\projects\sigar\src\sigar.c(2117): warning C4101: 'domain': unreferenced local variable
c:\users\iuriel\documents\visual studio 2015\projects\sigar\src\sigar.c(2301): warning C4013: '_getch' undefined; assuming extern returning int
Creating library C:\Users\iuriel\documents\visual studio 2015\Projects\Sigar\x64\Debug\sigar-amd64-winnt.lib and object C:\Users\iuriel\documents\visual studio 2015\Projects\Sigar\x64\Debug\sigar-amd64-winnt.exp
Sigar.vcxproj -> C:\Users\iuriel\documents\visual studio 2015\Projects\Sigar\x64\Debug\sigar-amd64-winnt.dll
Sigar.vcxproj -> C:\Users\iuriel\documents\visual studio 2015\Projects\Sigar\x64\Debug\sigar-amd64-winnt.pdb (Partial PDB)

View File

@ -0,0 +1,2 @@
#TargetFrameworkVersion=v4.0:PlatformToolSet=v140:EnableManagedIncrementalBuild=false:VCToolArchitecture=Native32Bit:WindowsTargetPlatformVersion=8.1
Debug|x64|C:\Users\iuriel\documents\visual studio 2015\Projects\Sigar\|

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,985 @@
/*
* Copyright (c) 2004-2008 Hyperic, Inc.
* Copyright (c) 2009 SpringSource, Inc.
* Copyright (c) 2009-2010 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef SIGAR_H
#define SIGAR_H
/* System Information Gatherer And Reporter */
#include <limits.h>
#ifndef MAX_INTERFACE_NAME_LEN
#define MAX_INTERFACE_NAME_LEN 256
#endif
#ifdef __cplusplus
extern "C" {
#endif
#if defined(_LP64) || \
defined(__LP64__) || \
defined(__64BIT__) || \
defined(__powerpc64__) || \
defined(__osf__)
#define SIGAR_64BIT
#endif
/* for printf sigar_uint64_t */
#ifdef SIGAR_64BIT
# define SIGAR_F_U64 "%lu"
#else
# define SIGAR_F_U64 "%Lu"
#endif
#if defined(WIN32)
typedef unsigned __int32 sigar_uint32_t;
typedef unsigned __int64 sigar_uint64_t;
typedef __int32 sigar_int32_t;
typedef __int64 sigar_int64_t;
#elif ULONG_MAX > 4294967295UL
typedef unsigned int sigar_uint32_t;
typedef unsigned long sigar_uint64_t;
typedef int sigar_int32_t;
typedef long sigar_int64_t;
#else
typedef unsigned int sigar_uint32_t;
typedef unsigned long long sigar_uint64_t;
typedef int sigar_int32_t;
typedef long long sigar_int64_t;
#endif
#define SIGAR_FIELD_NOTIMPL -1
#define SIGAR_OK 0
#define SIGAR_START_ERROR 20000
#define SIGAR_ENOTIMPL (SIGAR_START_ERROR + 1)
#define SIGAR_OS_START_ERROR (SIGAR_START_ERROR*2)
#ifdef WIN32
# define SIGAR_ENOENT ERROR_FILE_NOT_FOUND
# define SIGAR_EACCES ERROR_ACCESS_DENIED
# define SIGAR_ENXIO ERROR_BAD_DRIVER_LEVEL
#else
# define SIGAR_ENOENT ENOENT
# define SIGAR_EACCES EACCES
# define SIGAR_ENXIO ENXIO
#endif
#ifdef WIN32
# define SIGAR_DECLARE(type) \
__declspec(dllexport) type __stdcall
#else
# define SIGAR_DECLARE(type) type
#endif
#if defined(PATH_MAX)
# define SIGAR_PATH_MAX PATH_MAX
#elif defined(MAXPATHLEN)
# define SIGAR_PATH_MAX MAXPATHLEN
#else
# define SIGAR_PATH_MAX 4096
#endif
#ifdef WIN32
typedef sigar_uint64_t sigar_pid_t;
typedef unsigned long sigar_uid_t;
typedef unsigned long sigar_gid_t;
#else
#include <sys/types.h>
typedef pid_t sigar_pid_t;
typedef uid_t sigar_uid_t;
typedef gid_t sigar_gid_t;
#endif
typedef struct sigar_t sigar_t;
SIGAR_DECLARE(int) sigar_open(sigar_t **sigar);
SIGAR_DECLARE(int) sigar_close(sigar_t *sigar);
SIGAR_DECLARE(sigar_pid_t) sigar_pid_get(sigar_t *sigar);
SIGAR_DECLARE(int) sigar_proc_kill(sigar_pid_t pid, int signum);
SIGAR_DECLARE(int) sigar_signum_get(char *name);
SIGAR_DECLARE(char *) sigar_strerror(sigar_t *sigar, int err);
/* system memory info */
typedef struct {
sigar_uint64_t
ram,
total,
used,
free,
actual_used,
actual_free;
double used_percent;
double free_percent;
} sigar_mem_t;
SIGAR_DECLARE(int) sigar_mem_get(sigar_t *sigar, sigar_mem_t *mem);
typedef struct {
sigar_uint64_t
total,
used,
free,
page_in,
page_out;
} sigar_swap_t;
SIGAR_DECLARE(int) sigar_swap_get(sigar_t *sigar, sigar_swap_t *swap);
typedef struct {
sigar_uint64_t
user,
sys,
nice,
idle,
wait,
irq,
soft_irq,
stolen,
total;
} sigar_cpu_t;
SIGAR_DECLARE(int) sigar_cpu_get(sigar_t *sigar, sigar_cpu_t *cpu);
typedef struct {
unsigned long number;
unsigned long size;
sigar_cpu_t *data;
} sigar_cpu_list_t;
SIGAR_DECLARE(int) sigar_cpu_list_get(sigar_t *sigar, sigar_cpu_list_t *cpulist);
SIGAR_DECLARE(int) sigar_cpu_list_destroy(sigar_t *sigar,
sigar_cpu_list_t *cpulist);
typedef struct {
char vendor[128];
char model[128];
int mhz;
int mhz_max;
int mhz_min;
sigar_uint64_t cache_size;
int total_sockets;
int total_cores;
int cores_per_socket;
} sigar_cpu_info_t;
typedef struct {
unsigned long number;
unsigned long size;
sigar_cpu_info_t *data;
} sigar_cpu_info_list_t;
SIGAR_DECLARE(int)
sigar_cpu_info_list_get(sigar_t *sigar,
sigar_cpu_info_list_t *cpu_infos);
SIGAR_DECLARE(int)
sigar_cpu_info_list_destroy(sigar_t *sigar,
sigar_cpu_info_list_t *cpu_infos);
typedef struct {
double uptime;
} sigar_uptime_t;
SIGAR_DECLARE(int) sigar_uptime_get(sigar_t *sigar,
sigar_uptime_t *uptime);
typedef struct {
double loadavg[3];
} sigar_loadavg_t;
SIGAR_DECLARE(int) sigar_loadavg_get(sigar_t *sigar,
sigar_loadavg_t *loadavg);
typedef struct {
unsigned long number;
unsigned long size;
sigar_pid_t *data;
} sigar_proc_list_t;
typedef struct {
/* RLIMIT_CPU */
sigar_uint64_t cpu_cur, cpu_max;
/* RLIMIT_FSIZE */
sigar_uint64_t file_size_cur, file_size_max;
/* PIPE_BUF */
sigar_uint64_t pipe_size_cur, pipe_size_max;
/* RLIMIT_DATA */
sigar_uint64_t data_cur, data_max;
/* RLIMIT_STACK */
sigar_uint64_t stack_cur, stack_max;
/* RLIMIT_CORE */
sigar_uint64_t core_cur, core_max;
/* RLIMIT_RSS */
sigar_uint64_t memory_cur, memory_max;
/* RLIMIT_NPROC */
sigar_uint64_t processes_cur, processes_max;
/* RLIMIT_NOFILE */
sigar_uint64_t open_files_cur, open_files_max;
/* RLIMIT_AS */
sigar_uint64_t virtual_memory_cur, virtual_memory_max;
} sigar_resource_limit_t;
SIGAR_DECLARE(int) sigar_resource_limit_get(sigar_t *sigar,
sigar_resource_limit_t *rlimit);
SIGAR_DECLARE(int) sigar_proc_list_get(sigar_t *sigar,
sigar_proc_list_t *proclist);
SIGAR_DECLARE(int) sigar_proc_list_destroy(sigar_t *sigar,
sigar_proc_list_t *proclist);
typedef struct {
sigar_uint64_t total;
sigar_uint64_t sleeping;
sigar_uint64_t running;
sigar_uint64_t zombie;
sigar_uint64_t stopped;
sigar_uint64_t idle;
sigar_uint64_t threads;
} sigar_proc_stat_t;
SIGAR_DECLARE(int) sigar_proc_stat_get(sigar_t *sigar,
sigar_proc_stat_t *procstat);
typedef struct {
sigar_uint64_t
size,
resident,
share,
minor_faults,
major_faults,
page_faults;
} sigar_proc_mem_t;
SIGAR_DECLARE(int) sigar_proc_mem_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_mem_t *procmem);
typedef struct {
sigar_uint64_t
bytes_read,
bytes_written,
bytes_total;
} sigar_proc_disk_io_t;
SIGAR_DECLARE(int) sigar_proc_disk_io_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_disk_io_t *proc_disk_io);
typedef struct {
sigar_uint64_t
bytes_read,
bytes_written,
bytes_total;
sigar_uint64_t last_time;
sigar_uint64_t
bytes_read_diff,
bytes_written_diff,
bytes_total_diff;
} sigar_cached_proc_disk_io_t;
typedef struct {
sigar_uint64_t
bytes_read,
bytes_written,
bytes_total;
} sigar_proc_cumulative_disk_io_t;
SIGAR_DECLARE(int) sigar_proc_cumulative_disk_io_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_cumulative_disk_io_t *proc_cumulative_disk_io);
typedef struct {
sigar_uint64_t dummy;
}sigar_dump_pid_cache_t;
SIGAR_DECLARE(int) sigar_dump_pid_cache_get(sigar_t *sigar, sigar_dump_pid_cache_t *info);
typedef struct {
sigar_uid_t uid;
sigar_gid_t gid;
sigar_uid_t euid;
sigar_gid_t egid;
} sigar_proc_cred_t;
SIGAR_DECLARE(int) sigar_proc_cred_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_cred_t *proccred);
#define SIGAR_CRED_NAME_MAX 512
typedef struct {
char user[SIGAR_CRED_NAME_MAX];
char group[SIGAR_CRED_NAME_MAX];
} sigar_proc_cred_name_t;
SIGAR_DECLARE(int)
sigar_proc_cred_name_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_cred_name_t *proccredname);
typedef struct {
sigar_uint64_t
start_time,
user,
sys,
total;
} sigar_proc_time_t;
SIGAR_DECLARE(int) sigar_proc_time_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_time_t *proctime);
typedef struct {
/* must match sigar_proc_time_t fields */
sigar_uint64_t
start_time,
user,
sys,
total;
sigar_uint64_t last_time;
double percent;
} sigar_proc_cpu_t;
SIGAR_DECLARE(int) sigar_proc_cpu_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_cpu_t *proccpu);
#define SIGAR_PROC_STATE_SLEEP 'S'
#define SIGAR_PROC_STATE_RUN 'R'
#define SIGAR_PROC_STATE_STOP 'T'
#define SIGAR_PROC_STATE_ZOMBIE 'Z'
#define SIGAR_PROC_STATE_IDLE 'D'
#define SIGAR_PROC_NAME_LEN 128
typedef struct {
char name[SIGAR_PROC_NAME_LEN];
char state;
sigar_pid_t ppid;
int tty;
int priority;
int nice;
int processor;
sigar_uint64_t threads;
} sigar_proc_state_t;
SIGAR_DECLARE(int) sigar_proc_state_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_state_t *procstate);
typedef struct {
unsigned long number;
unsigned long size;
char **data;
} sigar_proc_args_t;
SIGAR_DECLARE(int) sigar_proc_args_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_args_t *procargs);
SIGAR_DECLARE(int) sigar_proc_args_destroy(sigar_t *sigar,
sigar_proc_args_t *procargs);
typedef struct {
void *data; /* user data */
enum {
SIGAR_PROC_ENV_ALL,
SIGAR_PROC_ENV_KEY
} type;
/* used for SIGAR_PROC_ENV_KEY */
const char *key;
int klen;
int (*env_getter)(void *, const char *, int, char *, int);
} sigar_proc_env_t;
SIGAR_DECLARE(int) sigar_proc_env_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_env_t *procenv);
typedef struct {
sigar_uint64_t total;
/* XXX - which are files, sockets, etc. */
} sigar_proc_fd_t;
SIGAR_DECLARE(int) sigar_proc_fd_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_fd_t *procfd);
typedef struct {
char name[SIGAR_PATH_MAX+1];
char cwd[SIGAR_PATH_MAX+1];
char root[SIGAR_PATH_MAX+1];
} sigar_proc_exe_t;
SIGAR_DECLARE(int) sigar_proc_exe_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_exe_t *procexe);
typedef struct {
void *data; /* user data */
int (*module_getter)(void *, char *, int);
} sigar_proc_modules_t;
SIGAR_DECLARE(int) sigar_proc_modules_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_modules_t *procmods);
typedef struct {
sigar_uint64_t user;
sigar_uint64_t sys;
sigar_uint64_t total;
} sigar_thread_cpu_t;
SIGAR_DECLARE(int) sigar_thread_cpu_get(sigar_t *sigar,
sigar_uint64_t id,
sigar_thread_cpu_t *cpu);
typedef enum {
SIGAR_FSTYPE_UNKNOWN,
SIGAR_FSTYPE_NONE,
SIGAR_FSTYPE_LOCAL_DISK,
SIGAR_FSTYPE_NETWORK,
SIGAR_FSTYPE_RAM_DISK,
SIGAR_FSTYPE_CDROM,
SIGAR_FSTYPE_SWAP,
SIGAR_FSTYPE_MAX
} sigar_file_system_type_e;
#define SIGAR_FS_NAME_LEN SIGAR_PATH_MAX
#define SIGAR_FS_INFO_LEN 256
typedef struct {
char dir_name[SIGAR_FS_NAME_LEN];
char dev_name[SIGAR_FS_NAME_LEN];
char type_name[SIGAR_FS_INFO_LEN]; /* e.g. "local" */
char sys_type_name[SIGAR_FS_INFO_LEN]; /* e.g. "ext3" */
char options[SIGAR_FS_INFO_LEN];
sigar_file_system_type_e type;
unsigned long flags;
} sigar_file_system_t;
typedef struct {
unsigned long number;
unsigned long size;
sigar_file_system_t *data;
} sigar_file_system_list_t;
SIGAR_DECLARE(int)
sigar_file_system_list_get(sigar_t *sigar,
sigar_file_system_list_t *fslist);
SIGAR_DECLARE(int)
sigar_file_system_list_destroy(sigar_t *sigar,
sigar_file_system_list_t *fslist);
typedef struct {
sigar_uint64_t reads;
sigar_uint64_t writes;
sigar_uint64_t write_bytes;
sigar_uint64_t read_bytes;
sigar_uint64_t rtime;
sigar_uint64_t wtime;
sigar_uint64_t qtime;
sigar_uint64_t time;
sigar_uint64_t snaptime;
double service_time;
double queue;
} sigar_disk_usage_t;
/* XXX for sigar_file_system_usage_t compat */
#define disk_reads disk.reads
#define disk_writes disk.writes
#define disk_write_bytes disk.write_bytes
#define disk_read_bytes disk.read_bytes
#define disk_queue disk.queue
#define disk_service_time disk.service_time
typedef struct {
sigar_disk_usage_t disk;
double use_percent;
sigar_uint64_t total;
sigar_uint64_t free;
sigar_uint64_t used;
sigar_uint64_t avail;
sigar_uint64_t files;
sigar_uint64_t free_files;
} sigar_file_system_usage_t;
#undef SIGAR_DISK_USAGE_T
SIGAR_DECLARE(int)
sigar_file_system_usage_get(sigar_t *sigar,
const char *dirname,
sigar_file_system_usage_t *fsusage);
SIGAR_DECLARE(int) sigar_disk_usage_get(sigar_t *sigar,
const char *name,
sigar_disk_usage_t *disk);
SIGAR_DECLARE(int)
sigar_file_system_ping(sigar_t *sigar,
sigar_file_system_t *fs);
typedef struct {
enum {
SIGAR_AF_UNSPEC,
SIGAR_AF_INET,
SIGAR_AF_INET6,
SIGAR_AF_LINK
} family;
union {
sigar_uint32_t in;
sigar_uint32_t in6[4];
unsigned char mac[8];
} addr;
} sigar_net_address_t;
#define SIGAR_INET6_ADDRSTRLEN 46
#define SIGAR_MAXDOMAINNAMELEN 256
#define SIGAR_MAXHOSTNAMELEN 256
typedef struct {
char default_gateway[SIGAR_INET6_ADDRSTRLEN];
char default_gateway_interface[MAX_INTERFACE_NAME_LEN];
char host_name[SIGAR_MAXHOSTNAMELEN];
char domain_name[SIGAR_MAXDOMAINNAMELEN];
char primary_dns[SIGAR_INET6_ADDRSTRLEN];
char secondary_dns[SIGAR_INET6_ADDRSTRLEN];
} sigar_net_info_t;
SIGAR_DECLARE(int)
sigar_net_info_get(sigar_t *sigar,
sigar_net_info_t *netinfo);
#define SIGAR_RTF_UP 0x1
#define SIGAR_RTF_GATEWAY 0x2
#define SIGAR_RTF_HOST 0x4
typedef struct {
sigar_net_address_t destination;
sigar_net_address_t gateway;
sigar_net_address_t mask;
sigar_uint64_t
flags,
refcnt,
use,
metric,
mtu,
window,
irtt;
char ifname[MAX_INTERFACE_NAME_LEN];
} sigar_net_route_t;
typedef struct {
unsigned long number;
unsigned long size;
sigar_net_route_t *data;
} sigar_net_route_list_t;
SIGAR_DECLARE(int) sigar_net_route_list_get(sigar_t *sigar,
sigar_net_route_list_t *routelist);
SIGAR_DECLARE(int) sigar_net_route_list_destroy(sigar_t *sigar,
sigar_net_route_list_t *routelist);
/*
* platforms define most of these "standard" flags,
* but of course, with different values in some cases.
*/
#define SIGAR_IFF_UP 0x1
#define SIGAR_IFF_BROADCAST 0x2
#define SIGAR_IFF_DEBUG 0x4
#define SIGAR_IFF_LOOPBACK 0x8
#define SIGAR_IFF_POINTOPOINT 0x10
#define SIGAR_IFF_NOTRAILERS 0x20
#define SIGAR_IFF_RUNNING 0x40
#define SIGAR_IFF_NOARP 0x80
#define SIGAR_IFF_PROMISC 0x100
#define SIGAR_IFF_ALLMULTI 0x200
#define SIGAR_IFF_MULTICAST 0x800
#define SIGAR_IFF_SLAVE 0x1000
#define SIGAR_IFF_MASTER 0x2000
#define SIGAR_IFF_DYNAMIC 0x4000
#define SIGAR_NULL_HWADDR "00:00:00:00:00:00"
/* scope values from linux-2.6/include/net/ipv6.h */
#define SIGAR_IPV6_ADDR_ANY 0x0000
#define SIGAR_IPV6_ADDR_UNICAST 0x0001
#define SIGAR_IPV6_ADDR_MULTICAST 0x0002
#define SIGAR_IPV6_ADDR_LOOPBACK 0x0010
#define SIGAR_IPV6_ADDR_LINKLOCAL 0x0020
#define SIGAR_IPV6_ADDR_SITELOCAL 0x0040
#define SIGAR_IPV6_ADDR_COMPATv4 0x0080
typedef struct {
char name[MAX_INTERFACE_NAME_LEN];
char type[64];
char description[256];
sigar_net_address_t hwaddr;
sigar_net_address_t address;
sigar_net_address_t destination;
sigar_net_address_t broadcast;
sigar_net_address_t netmask;
sigar_net_address_t address6;
int prefix6_length;
int scope6;
sigar_uint64_t
flags,
mtu,
metric;
int tx_queue_len;
} sigar_net_interface_config_t;
SIGAR_DECLARE(int)
sigar_net_interface_config_get(sigar_t *sigar,
const char *name,
sigar_net_interface_config_t *ifconfig);
SIGAR_DECLARE(int)
sigar_net_interface_config_primary_get(sigar_t *sigar,
sigar_net_interface_config_t *ifconfig);
typedef struct {
sigar_uint64_t
/* received */
rx_packets,
rx_bytes,
rx_errors,
rx_dropped,
rx_overruns,
rx_frame,
/* transmitted */
tx_packets,
tx_bytes,
tx_errors,
tx_dropped,
tx_overruns,
tx_collisions,
tx_carrier,
speed;
} sigar_net_interface_stat_t;
SIGAR_DECLARE(int)
sigar_net_interface_stat_get(sigar_t *sigar,
const char *name,
sigar_net_interface_stat_t *ifstat);
typedef struct {
unsigned long number;
unsigned long size;
char **data;
} sigar_net_interface_list_t;
SIGAR_DECLARE(int)
sigar_net_interface_list_get(sigar_t *sigar,
sigar_net_interface_list_t *iflist);
SIGAR_DECLARE(int)
sigar_net_interface_list_destroy(sigar_t *sigar,
sigar_net_interface_list_t *iflist);
#define SIGAR_NETCONN_CLIENT 0x01
#define SIGAR_NETCONN_SERVER 0x02
#define SIGAR_NETCONN_TCP 0x10
#define SIGAR_NETCONN_UDP 0x20
#define SIGAR_NETCONN_RAW 0x40
#define SIGAR_NETCONN_UNIX 0x80
enum {
SIGAR_TCP_ESTABLISHED = 1,
SIGAR_TCP_SYN_SENT,
SIGAR_TCP_SYN_RECV,
SIGAR_TCP_FIN_WAIT1,
SIGAR_TCP_FIN_WAIT2,
SIGAR_TCP_TIME_WAIT,
SIGAR_TCP_CLOSE,
SIGAR_TCP_CLOSE_WAIT,
SIGAR_TCP_LAST_ACK,
SIGAR_TCP_LISTEN,
SIGAR_TCP_CLOSING,
SIGAR_TCP_IDLE,
SIGAR_TCP_BOUND,
SIGAR_TCP_UNKNOWN
};
typedef struct {
unsigned long local_port;
sigar_net_address_t local_address;
unsigned long remote_port;
sigar_net_address_t remote_address;
sigar_uid_t uid;
unsigned long inode;
int type;
int state;
unsigned long send_queue;
unsigned long receive_queue;
} sigar_net_connection_t;
typedef struct {
unsigned long number;
unsigned long size;
sigar_net_connection_t *data;
} sigar_net_connection_list_t;
SIGAR_DECLARE(int)
sigar_net_connection_list_get(sigar_t *sigar,
sigar_net_connection_list_t *connlist,
int flags);
SIGAR_DECLARE(int)
sigar_net_connection_list_destroy(sigar_t *sigar,
sigar_net_connection_list_t *connlist);
typedef struct sigar_net_connection_walker_t sigar_net_connection_walker_t;
/* alternative to sigar_net_connection_list_get */
struct sigar_net_connection_walker_t {
sigar_t *sigar;
int flags;
void *data; /* user data */
int (*add_connection)(sigar_net_connection_walker_t *walker,
sigar_net_connection_t *connection);
};
SIGAR_DECLARE(int)
sigar_net_connection_walk(sigar_net_connection_walker_t *walker);
typedef struct {
int tcp_states[SIGAR_TCP_UNKNOWN];
sigar_uint32_t tcp_inbound_total;
sigar_uint32_t tcp_outbound_total;
sigar_uint32_t all_inbound_total;
sigar_uint32_t all_outbound_total;
} sigar_net_stat_t;
SIGAR_DECLARE(int)
sigar_net_stat_get(sigar_t *sigar,
sigar_net_stat_t *netstat,
int flags);
SIGAR_DECLARE(int)
sigar_net_stat_port_get(sigar_t *sigar,
sigar_net_stat_t *netstat,
int flags,
sigar_net_address_t *address,
unsigned long port);
/* TCP-MIB */
typedef struct {
sigar_uint64_t active_opens;
sigar_uint64_t passive_opens;
sigar_uint64_t attempt_fails;
sigar_uint64_t estab_resets;
sigar_uint64_t curr_estab;
sigar_uint64_t in_segs;
sigar_uint64_t out_segs;
sigar_uint64_t retrans_segs;
sigar_uint64_t in_errs;
sigar_uint64_t out_rsts;
} sigar_tcp_t;
SIGAR_DECLARE(int)
sigar_tcp_get(sigar_t *sigar,
sigar_tcp_t *tcp);
typedef struct {
sigar_uint64_t null;
sigar_uint64_t getattr;
sigar_uint64_t setattr;
sigar_uint64_t root;
sigar_uint64_t lookup;
sigar_uint64_t readlink;
sigar_uint64_t read;
sigar_uint64_t writecache;
sigar_uint64_t write;
sigar_uint64_t create;
sigar_uint64_t remove;
sigar_uint64_t rename;
sigar_uint64_t link;
sigar_uint64_t symlink;
sigar_uint64_t mkdir;
sigar_uint64_t rmdir;
sigar_uint64_t readdir;
sigar_uint64_t fsstat;
} sigar_nfs_v2_t;
typedef sigar_nfs_v2_t sigar_nfs_client_v2_t;
typedef sigar_nfs_v2_t sigar_nfs_server_v2_t;
SIGAR_DECLARE(int)
sigar_nfs_client_v2_get(sigar_t *sigar,
sigar_nfs_client_v2_t *nfs);
SIGAR_DECLARE(int)
sigar_nfs_server_v2_get(sigar_t *sigar,
sigar_nfs_server_v2_t *nfs);
typedef struct {
sigar_uint64_t null;
sigar_uint64_t getattr;
sigar_uint64_t setattr;
sigar_uint64_t lookup;
sigar_uint64_t access;
sigar_uint64_t readlink;
sigar_uint64_t read;
sigar_uint64_t write;
sigar_uint64_t create;
sigar_uint64_t mkdir;
sigar_uint64_t symlink;
sigar_uint64_t mknod;
sigar_uint64_t remove;
sigar_uint64_t rmdir;
sigar_uint64_t rename;
sigar_uint64_t link;
sigar_uint64_t readdir;
sigar_uint64_t readdirplus;
sigar_uint64_t fsstat;
sigar_uint64_t fsinfo;
sigar_uint64_t pathconf;
sigar_uint64_t commit;
} sigar_nfs_v3_t;
typedef sigar_nfs_v3_t sigar_nfs_client_v3_t;
typedef sigar_nfs_v3_t sigar_nfs_server_v3_t;
SIGAR_DECLARE(int)
sigar_nfs_client_v3_get(sigar_t *sigar,
sigar_nfs_client_v3_t *nfs);
SIGAR_DECLARE(int)
sigar_nfs_server_v3_get(sigar_t *sigar,
sigar_nfs_server_v3_t *nfs);
SIGAR_DECLARE(int)
sigar_net_listen_address_get(sigar_t *sigar,
unsigned long port,
sigar_net_address_t *address);
typedef struct {
char ifname[MAX_INTERFACE_NAME_LEN];
char type[64];
sigar_net_address_t hwaddr;
sigar_net_address_t address;
sigar_uint64_t flags;
} sigar_arp_t;
typedef struct {
unsigned long number;
unsigned long size;
sigar_arp_t *data;
} sigar_arp_list_t;
SIGAR_DECLARE(int) sigar_arp_list_get(sigar_t *sigar,
sigar_arp_list_t *arplist);
SIGAR_DECLARE(int) sigar_arp_list_destroy(sigar_t *sigar,
sigar_arp_list_t *arplist);
typedef struct {
char user[32];
char device[32];
char host[256];
sigar_uint64_t time;
} sigar_who_t;
typedef struct {
unsigned long number;
unsigned long size;
sigar_who_t *data;
} sigar_who_list_t;
SIGAR_DECLARE(int) sigar_who_list_get(sigar_t *sigar,
sigar_who_list_t *wholist);
SIGAR_DECLARE(int) sigar_who_list_destroy(sigar_t *sigar,
sigar_who_list_t *wholist);
SIGAR_DECLARE(int) sigar_proc_port_get(sigar_t *sigar,
int protocol, unsigned long port,
sigar_pid_t *pid);
typedef struct {
const char *build_date;
const char *scm_revision;
const char *version;
const char *archname;
const char *archlib;
const char *binname;
const char *description;
int major, minor, maint, build;
} sigar_version_t;
SIGAR_DECLARE(sigar_version_t *) sigar_version_get(void);
#define SIGAR_SYS_INFO_LEN SIGAR_MAXHOSTNAMELEN /* more than enough */
typedef struct {
char name[SIGAR_SYS_INFO_LEN]; /* canonicalized sysname */
char version[SIGAR_SYS_INFO_LEN]; /* utsname.release */
char arch[SIGAR_SYS_INFO_LEN];
char machine[SIGAR_SYS_INFO_LEN];
char description[SIGAR_SYS_INFO_LEN];
char patch_level[SIGAR_SYS_INFO_LEN];
char vendor[SIGAR_SYS_INFO_LEN];
char vendor_version[SIGAR_SYS_INFO_LEN];
char vendor_name[SIGAR_SYS_INFO_LEN]; /* utsname.sysname */
char vendor_code_name[SIGAR_SYS_INFO_LEN];
} sigar_sys_info_t;
SIGAR_DECLARE(int) sigar_sys_info_get(sigar_t *sigar, sigar_sys_info_t *sysinfo);
#define SIGAR_FQDN_LEN 512
SIGAR_DECLARE(int) sigar_fqdn_get(sigar_t *sigar, char *name, int namelen);
SIGAR_DECLARE(int) sigar_rpc_ping(char *hostname,
int protocol,
unsigned long program,
unsigned long version);
SIGAR_DECLARE(char *) sigar_rpc_strerror(int err);
SIGAR_DECLARE(char *) sigar_password_get(const char *prompt);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,157 @@
/*
* Copyright (c) 2004-2005 Hyperic, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
#include "sigar.h"
typedef enum {
SIGAR_FILETYPE_NOFILE = 0, /**< no file type determined */
SIGAR_FILETYPE_REG, /**< a regular file */
SIGAR_FILETYPE_DIR, /**< a directory */
SIGAR_FILETYPE_CHR, /**< a character device */
SIGAR_FILETYPE_BLK, /**< a block device */
SIGAR_FILETYPE_PIPE, /**< a FIFO / pipe */
SIGAR_FILETYPE_LNK, /**< a symbolic link */
SIGAR_FILETYPE_SOCK, /**< a [unix domain] socket */
SIGAR_FILETYPE_UNKFILE /**< a file of some other unknown type */
} sigar_file_type_e;
#define SIGAR_UREAD 0x0400 /**< Read by user */
#define SIGAR_UWRITE 0x0200 /**< Write by user */
#define SIGAR_UEXECUTE 0x0100 /**< Execute by user */
#define SIGAR_GREAD 0x0040 /**< Read by group */
#define SIGAR_GWRITE 0x0020 /**< Write by group */
#define SIGAR_GEXECUTE 0x0010 /**< Execute by group */
#define SIGAR_WREAD 0x0004 /**< Read by others */
#define SIGAR_WWRITE 0x0002 /**< Write by others */
#define SIGAR_WEXECUTE 0x0001 /**< Execute by others */
typedef struct {
/** The access permissions of the file. Mimics Unix access rights. */
sigar_uint64_t permissions;
sigar_file_type_e type;
/** The user id that owns the file */
sigar_uid_t uid;
/** The group id that owns the file */
sigar_gid_t gid;
/** The inode of the file. */
sigar_uint64_t inode;
/** The id of the device the file is on. */
sigar_uint64_t device;
/** The number of hard links to the file. */
sigar_uint64_t nlink;
/** The size of the file */
sigar_uint64_t size;
/** The time the file was last accessed */
sigar_uint64_t atime;
/** The time the file was last modified */
sigar_uint64_t mtime;
/** The time the file was last changed */
sigar_uint64_t ctime;
} sigar_file_attrs_t;
typedef struct {
sigar_uint64_t total;
sigar_uint64_t files;
sigar_uint64_t subdirs;
sigar_uint64_t symlinks;
sigar_uint64_t chrdevs;
sigar_uint64_t blkdevs;
sigar_uint64_t sockets;
sigar_uint64_t disk_usage;
} sigar_dir_stat_t;
typedef sigar_dir_stat_t sigar_dir_usage_t;
SIGAR_DECLARE(const char *)
sigar_file_attrs_type_string_get(sigar_file_type_e type);
SIGAR_DECLARE(int) sigar_file_attrs_get(sigar_t *sigar,
const char *file,
sigar_file_attrs_t *fileattrs);
SIGAR_DECLARE(int) sigar_link_attrs_get(sigar_t *sigar,
const char *file,
sigar_file_attrs_t *fileattrs);
SIGAR_DECLARE(int)sigar_file_attrs_mode_get(sigar_uint64_t permissions);
SIGAR_DECLARE(char *)
sigar_file_attrs_permissions_string_get(sigar_uint64_t permissions,
char *str);
SIGAR_DECLARE(int) sigar_dir_stat_get(sigar_t *sigar,
const char *dir,
sigar_dir_stat_t *dirstats);
SIGAR_DECLARE(int) sigar_dir_usage_get(sigar_t *sigar,
const char *dir,
sigar_dir_usage_t *dirusage);

View File

@ -0,0 +1,65 @@
/*
* Copyright (c) 2007-2008 Hyperic, Inc.
* Copyright (c) 2009 SpringSource, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef SIGAR_FORMAT_H
#define SIGAR_FORMAT_H
typedef struct {
double user;
double sys;
double nice;
double idle;
double wait;
double irq;
double soft_irq;
double stolen;
double combined;
} sigar_cpu_perc_t;
SIGAR_DECLARE(int) sigar_cpu_perc_calculate(sigar_cpu_t *prev,
sigar_cpu_t *curr,
sigar_cpu_perc_t *perc);
SIGAR_DECLARE(int) sigar_uptime_string(sigar_t *sigar,
sigar_uptime_t *uptime,
char *buffer,
int buflen);
SIGAR_DECLARE(char *) sigar_format_size(sigar_uint64_t size, char *buf);
SIGAR_DECLARE(int) sigar_net_address_equals(sigar_net_address_t *addr1,
sigar_net_address_t *addr2);
SIGAR_DECLARE(int) sigar_net_address_to_string(sigar_t *sigar,
sigar_net_address_t *address,
char *addr_str);
SIGAR_DECLARE(const char *)sigar_net_scope_to_string(int type);
SIGAR_DECLARE(sigar_uint32_t) sigar_net_address_hash(sigar_net_address_t *address);
SIGAR_DECLARE(const char *)sigar_net_connection_type_get(int type);
SIGAR_DECLARE(const char *)sigar_net_connection_state_get(int state);
SIGAR_DECLARE(char *) sigar_net_interface_flags_to_string(sigar_uint64_t flags, char *buf);
SIGAR_DECLARE(char *)sigar_net_services_name_get(sigar_t *sigar,
int protocol, unsigned long port);
#endif

View File

@ -0,0 +1,18 @@
#ifndef SIGAR_GETLINE_H
#define SIGAR_GETLINE_H
#include "sigar.h"
typedef int (*sigar_getline_completer_t)(char *, int, int *);
SIGAR_DECLARE(char *) sigar_getline(char *prompt);
SIGAR_DECLARE(void) sigar_getline_setwidth(int width);
SIGAR_DECLARE(void) sigar_getline_redraw(void);
SIGAR_DECLARE(void) sigar_getline_reset(void);
SIGAR_DECLARE(void) sigar_getline_windowchanged();
SIGAR_DECLARE(void) sigar_getline_histinit(char *file);
SIGAR_DECLARE(void) sigar_getline_histadd(char *buf);
SIGAR_DECLARE(int) sigar_getline_eof();
SIGAR_DECLARE(void) sigar_getline_completer_set(sigar_getline_completer_t func);
#endif /* SIGAR_GETLINE_H */

View File

@ -0,0 +1,80 @@
/*
* Copyright (c) 2004, 2006 Hyperic, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef SIGAR_LOG_H
#define SIGAR_LOG_H
#include <stdarg.h>
#define SIGAR_LOG_FATAL 0
#define SIGAR_LOG_ERROR 1
#define SIGAR_LOG_WARN 2
#define SIGAR_LOG_INFO 3
#define SIGAR_LOG_DEBUG 4
#define SIGAR_LOG_TRACE 5
#define SIGAR_LOG_IS_FATAL(sigar) \
(sigar->log_level >= SIGAR_LOG_FATAL)
#define SIGAR_LOG_IS_ERROR(sigar) \
(sigar->log_level >= SIGAR_LOG_ERROR)
#define SIGAR_LOG_IS_WARN(sigar) \
(sigar->log_level >= SIGAR_LOG_WARN)
#define SIGAR_LOG_IS_INFO(sigar) \
(sigar->log_level >= SIGAR_LOG_INFO)
#define SIGAR_LOG_IS_DEBUG(sigar) \
(sigar->log_level >= SIGAR_LOG_DEBUG)
#define SIGAR_LOG_IS_TRACE(sigar) \
(sigar->log_level >= SIGAR_LOG_TRACE)
#define SIGAR_STRINGIFY(n) #n
#define SIGAR_LOG_FILELINE \
__FILE__ ":" SIGAR_STRINGIFY(__LINE__)
#if defined(__GNUC__)
# if (__GNUC__ > 2)
# define SIGAR_FUNC __func__
# else
# define SIGAR_FUNC __FUNCTION__
# endif
#else
# define SIGAR_FUNC SIGAR_LOG_FILELINE
#endif
typedef void (*sigar_log_impl_t)(sigar_t *, void *, int, char *);
SIGAR_DECLARE(void) sigar_log_printf(sigar_t *sigar, int level,
const char *format, ...);
SIGAR_DECLARE(void) sigar_log(sigar_t *sigar, int level, char *message);
SIGAR_DECLARE(void) sigar_log_impl_set(sigar_t *sigar, void *data,
sigar_log_impl_t impl);
SIGAR_DECLARE(void) sigar_log_impl_file(sigar_t *sigar, void *data,
int level, char *message);
SIGAR_DECLARE(int) sigar_log_level_get(sigar_t *sigar);
SIGAR_DECLARE(void) sigar_log_level_set(sigar_t *sigar, int level);
#endif /* SIGAR_LOG_H */

View File

@ -0,0 +1,685 @@
/*
* Copyright (c) 2004-2009 Hyperic, Inc.
* Copyright (c) 2009 SpringSource, Inc.
* Copyright (c) 2009-2010 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef SIGAR_OS_H
#define SIGAR_OS_H
#if !defined(MSVC) && defined(_MSC_VER)
#define MSVC
#endif
#ifdef MSVC
#define WIN32_LEAN_AND_MEAN
//#define snprintf _snprintf fixed by ilan
#if _MSC_VER <= 1200
#define SIGAR_USING_MSC6 /* Visual Studio version 6 */
#define HAVE_MIB_IPADDRROW_WTYPE 0
#else
#define HAVE_MIB_IPADDRROW_WTYPE 1
#endif
#else
/* Cross compiling */
#define _WIN32_WINNT 0x0501
#endif
#include <windows.h>
#include <winreg.h>
#include <winperf.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stddef.h>
#include <sys/types.h>
#include <malloc.h>
#include <stdio.h>
#include <errno.h>
#include <tlhelp32.h>
#include "sigar_util.h"
#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)
#define SIGAR_CMDLINE_MAX 4096<<2
/* XXX: support CP_UTF8 ? */
#define SIGAR_A2W(lpa, lpw, bytes) \
(lpw[0] = 0, MultiByteToWideChar(CP_ACP, 0, \
lpa, -1, lpw, (bytes/sizeof(WCHAR))))
#define SIGAR_W2A(lpw, lpa, chars) \
(lpa[0] = '\0', WideCharToMultiByte(CP_ACP, 0, \
lpw, -1, (LPSTR)lpa, chars, \
NULL, NULL))
/* iptypes.h from vc7, not available in vc6 */
/* copy from PSDK if using vc6 */
#include "iptypes.h"
/* from wtsapi32.h not in vs6.0 */
typedef enum {
WTSInitialProgram,
WTSApplicationName,
WTSWorkingDirectory,
WTSOEMId,
WTSSessionId,
WTSUserName,
WTSWinStationName,
WTSDomainName,
WTSConnectState,
WTSClientBuildNumber,
WTSClientName,
WTSClientDirectory,
WTSClientProductId,
WTSClientHardwareId,
WTSClientAddress,
WTSClientDisplay,
WTSClientProtocolType,
} WTS_INFO_CLASS;
typedef enum _WTS_CONNECTSTATE_CLASS {
WTSActive,
WTSConnected,
WTSConnectQuery,
WTSShadow,
WTSDisconnected,
WTSIdle,
WTSListen,
WTSReset,
WTSDown,
WTSInit
} WTS_CONNECTSTATE_CLASS;
#define WTS_PROTOCOL_TYPE_CONSOLE 0
#define WTS_PROTOCOL_TYPE_ICA 1
#define WTS_PROTOCOL_TYPE_RDP 2
typedef struct _WTS_SESSION_INFO {
DWORD SessionId;
LPTSTR pWinStationName;
DWORD State;
} WTS_SESSION_INFO, *PWTS_SESSION_INFO;
typedef struct _WTS_PROCESS_INFO {
DWORD SessionId;
DWORD ProcessId;
LPSTR pProcessName;
PSID pUserSid;
} WTS_PROCESS_INFO, *PWTS_PROCESS_INFO;
typedef struct _WTS_CLIENT_ADDRESS {
DWORD AddressFamily;
BYTE Address[20];
} WTS_CLIENT_ADDRESS, *PWTS_CLIENT_ADDRESS;
/* the WINSTATION_INFO stuff here is undocumented
* got the howto from google groups:
* http://redirx.com/?31gy
*/
typedef enum _WINSTATION_INFO_CLASS {
WinStationInformation = 8
} WINSTATION_INFO_CLASS;
typedef struct _WINSTATION_INFO {
BYTE Reserved1[72];
ULONG SessionId;
BYTE Reserved2[4];
FILETIME ConnectTime;
FILETIME DisconnectTime;
FILETIME LastInputTime;
FILETIME LoginTime;
BYTE Reserved3[1096];
FILETIME CurrentTime;
} WINSTATION_INFO, *PWINSTATION_INFO;
/* end wtsapi32.h */
#ifdef SIGAR_USING_MSC6
/* from winbase.h not in vs6.0 */
typedef struct {
DWORD dwLength;
DWORD dwMemoryLoad;
DWORDLONG ullTotalPhys;
DWORDLONG ullAvailPhys;
DWORDLONG ullTotalPageFile;
DWORDLONG ullAvailPageFile;
DWORDLONG ullTotalVirtual;
DWORDLONG ullAvailVirtual;
DWORDLONG ullAvailExtendedVirtual;
} MEMORYSTATUSEX;
/* service manager stuff not in vs6.0 */
typedef struct _SERVICE_STATUS_PROCESS {
DWORD dwServiceType;
DWORD dwCurrentState;
DWORD dwControlsAccepted;
DWORD dwWin32ExitCode;
DWORD dwServiceSpecificExitCode;
DWORD dwCheckPoint;
DWORD dwWaitHint;
DWORD dwProcessId;
DWORD dwServiceFlags;
} SERVICE_STATUS_PROCESS;
typedef enum {
SC_STATUS_PROCESS_INFO = 0
} SC_STATUS_TYPE;
#ifndef ERROR_DATATYPE_MISMATCH
#define ERROR_DATATYPE_MISMATCH 1629L
#endif
#endif /* _MSC_VER */
#include <iprtrmib.h>
/* undocumented structures */
typedef struct {
DWORD dwState;
DWORD dwLocalAddr;
DWORD dwLocalPort;
DWORD dwRemoteAddr;
DWORD dwRemotePort;
DWORD dwProcessId;
} MIB_TCPEXROW, *PMIB_TCPEXROW;
typedef struct {
DWORD dwNumEntries;
MIB_TCPEXROW table[ANY_SIZE];
} MIB_TCPEXTABLE, *PMIB_TCPEXTABLE;
typedef struct {
DWORD dwLocalAddr;
DWORD dwLocalPort;
DWORD dwProcessId;
} MIB_UDPEXROW, *PMIB_UDPEXROW;
typedef struct {
DWORD dwNumEntries;
MIB_UDPEXROW table[ANY_SIZE];
} MIB_UDPEXTABLE, *PMIB_UDPEXTABLE;
/* end undocumented structures */
/* no longer in the standard header files */
typedef struct {
LARGE_INTEGER IdleTime;
LARGE_INTEGER KernelTime;
LARGE_INTEGER UserTime;
LARGE_INTEGER DpcTime;
LARGE_INTEGER InterruptTime;
ULONG InterruptCount;
} SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION;
#define SystemProcessorPerformanceInformation 8
/* PEB decls from msdn docs w/ slight mods */
#define ProcessBasicInformation 0
typedef struct _UNICODE_STRING {
USHORT Length;
USHORT MaximumLength;
PWSTR Buffer;
} UNICODE_STRING, *PUNICODE_STRING;
typedef struct _PEB_LDR_DATA {
BYTE Reserved1[8];
PVOID Reserved2[3];
LIST_ENTRY InMemoryOrderModuleList;
} PEB_LDR_DATA, *PPEB_LDR_DATA;
typedef struct RTL_DRIVE_LETTER_CURDIR {
USHORT Flags;
USHORT Length;
ULONG TimeStamp;
UNICODE_STRING DosPath;
} RTL_DRIVE_LETTER_CURDIR, *PRTL_DRIVE_LETTER_CURDIR;
/* from: http://source.winehq.org/source/include/winternl.h */
typedef struct _RTL_USER_PROCESS_PARAMETERS {
ULONG AllocationSize;
ULONG Size;
ULONG Flags;
ULONG DebugFlags;
HANDLE hConsole;
ULONG ProcessGroup;
HANDLE hStdInput;
HANDLE hStdOutput;
HANDLE hStdError;
UNICODE_STRING CurrentDirectoryName;
HANDLE CurrentDirectoryHandle;
UNICODE_STRING DllPath;
UNICODE_STRING ImagePathName;
UNICODE_STRING CommandLine;
PWSTR Environment;
ULONG dwX;
ULONG dwY;
ULONG dwXSize;
ULONG dwYSize;
ULONG dwXCountChars;
ULONG dwYCountChars;
ULONG dwFillAttribute;
ULONG dwFlags;
ULONG wShowWindow;
UNICODE_STRING WindowTitle;
UNICODE_STRING Desktop;
UNICODE_STRING ShellInfo;
UNICODE_STRING RuntimeInfo;
RTL_DRIVE_LETTER_CURDIR DLCurrentDirectory[0x20];
} RTL_USER_PROCESS_PARAMETERS, *PRTL_USER_PROCESS_PARAMETERS;
/* from msdn docs
typedef struct _RTL_USER_PROCESS_PARAMETERS {
BYTE Reserved1[16];
PVOID Reserved2[10];
UNICODE_STRING ImagePathName;
UNICODE_STRING CommandLine;
} RTL_USER_PROCESS_PARAMETERS, *PRTL_USER_PROCESS_PARAMETERS;
*/
typedef struct _PEB {
BYTE Reserved1[2];
BYTE BeingDebugged;
BYTE Reserved2[1];
PVOID Reserved3[2];
PPEB_LDR_DATA Ldr;
PRTL_USER_PROCESS_PARAMETERS ProcessParameters;
BYTE Reserved4[104];
PVOID Reserved5[52];
/*PPS_POST_PROCESS_INIT_ROUTINE*/ PVOID PostProcessInitRoutine;
BYTE Reserved6[128];
PVOID Reserved7[1];
ULONG SessionId;
} PEB, *PPEB;
typedef struct _PROCESS_BASIC_INFORMATION {
PVOID Reserved1;
PPEB PebBaseAddress;
PVOID Reserved2[2];
/*ULONG_PTR*/ UINT_PTR UniqueProcessId;
PVOID Reserved3;
} PROCESS_BASIC_INFORMATION;
typedef struct {
sigar_pid_t pid;
int ppid;
int priority;
time_t mtime;
sigar_uint64_t size;
sigar_uint64_t resident;
char name[SIGAR_PROC_NAME_LEN];
char state;
sigar_uint64_t handles;
sigar_uint64_t threads;
sigar_uint64_t page_faults;
sigar_uint64_t bytes_read;
sigar_uint64_t bytes_written;
} sigar_win32_pinfo_t;
typedef struct {
const char *name;
HINSTANCE handle;
} sigar_dll_handle_t;
typedef struct {
const char *name;
FARPROC func;
} sigar_dll_func_t;
typedef struct {
const char *name;
HINSTANCE handle;
sigar_dll_func_t funcs[12];
} sigar_dll_module_t;
/* wtsapi.dll */
typedef BOOL (CALLBACK *wtsapi_enum_sessions)(HANDLE,
DWORD,
DWORD,
PWTS_SESSION_INFO *,
DWORD *);
typedef void (CALLBACK *wtsapi_free_mem)(PVOID);
typedef BOOL (CALLBACK *wtsapi_query_session)(HANDLE,
DWORD,
WTS_INFO_CLASS,
LPSTR *, DWORD *);
/* iphlpapi.dll */
typedef DWORD (CALLBACK *iphlpapi_get_ipforward_table)(PMIB_IPFORWARDTABLE,
PULONG,
BOOL);
typedef DWORD (CALLBACK *iphlpapi_get_ipaddr_table)(PMIB_IPADDRTABLE,
PULONG,
BOOL);
typedef DWORD (CALLBACK *iphlpapi_get_if_table)(PMIB_IFTABLE,
PULONG,
BOOL);
typedef DWORD (CALLBACK *iphlpapi_get_if_entry)(PMIB_IFROW);
typedef DWORD (CALLBACK *iphlpapi_get_num_if)(PDWORD);
typedef DWORD (CALLBACK *iphlpapi_get_tcp_table)(PMIB_TCPTABLE,
PDWORD,
BOOL);
typedef DWORD (CALLBACK *iphlpapi_get_udp_table)(PMIB_UDPTABLE,
PDWORD,
BOOL);
typedef DWORD (CALLBACK *iphlpapi_get_tcpx_table)(PMIB_TCPEXTABLE *,
BOOL,
HANDLE,
DWORD,
DWORD);
typedef DWORD (CALLBACK *iphlpapi_get_udpx_table)(PMIB_UDPEXTABLE *,
BOOL,
HANDLE,
DWORD,
DWORD);
typedef DWORD (CALLBACK *iphlpapi_get_tcp_stats)(PMIB_TCPSTATS);
typedef DWORD (CALLBACK *iphlpapi_get_net_params)(PFIXED_INFO,
PULONG);
typedef DWORD (CALLBACK *iphlpapi_get_adapters_info)(PIP_ADAPTER_INFO,
PULONG);
typedef ULONG (CALLBACK *iphlpapi_get_adapters_addrs)(ULONG,
ULONG,
PVOID,
PIP_ADAPTER_ADDRESSES,
PULONG);
/* advapi32.dll */
typedef BOOL (CALLBACK *advapi_convert_string_sid)(LPCSTR,
PSID *);
typedef BOOL (CALLBACK *advapi_query_service_status)(SC_HANDLE,
SC_STATUS_TYPE,
LPBYTE,
DWORD,
LPDWORD);
typedef DWORD (CALLBACK *iphlpapi_get_ipnet_table)(PMIB_IPNETTABLE,
PDWORD,
BOOL);
/* ntdll.dll */
typedef DWORD (CALLBACK *ntdll_query_sys_info)(DWORD,
PVOID,
ULONG,
PULONG);
typedef DWORD (CALLBACK *ntdll_query_proc_info)(HANDLE,
DWORD,
PVOID,
ULONG,
PULONG);
/* psapi.dll */
typedef BOOL (CALLBACK *psapi_enum_modules)(HANDLE,
HMODULE *,
DWORD,
LPDWORD);
typedef DWORD (CALLBACK *psapi_get_module_name)(HANDLE,
HMODULE,
LPTSTR,
DWORD);
typedef BOOL (CALLBACK *psapi_enum_processes)(DWORD *,
DWORD,
DWORD *);
/* winsta.dll */
typedef BOOLEAN (CALLBACK *winsta_query_info)(HANDLE,
ULONG,
WINSTATION_INFO_CLASS,
PVOID,
ULONG,
PULONG);
/* kernel32.dll */
typedef BOOL (CALLBACK *kernel_memory_status)(MEMORYSTATUSEX *);
/* mpr.dll */
typedef BOOL (CALLBACK *mpr_get_net_connection)(LPCTSTR,
LPTSTR,
LPDWORD);
typedef struct {
sigar_dll_handle_t handle;
SIGAR_DLLFUNC(wtsapi, enum_sessions);
SIGAR_DLLFUNC(wtsapi, free_mem);
SIGAR_DLLFUNC(wtsapi, query_session);
sigar_dll_func_t end;
} sigar_wtsapi_t;
typedef struct {
sigar_dll_handle_t handle;
SIGAR_DLLFUNC(iphlpapi, get_ipforward_table);
SIGAR_DLLFUNC(iphlpapi, get_ipaddr_table);
SIGAR_DLLFUNC(iphlpapi, get_if_table);
SIGAR_DLLFUNC(iphlpapi, get_if_entry);
SIGAR_DLLFUNC(iphlpapi, get_num_if);
SIGAR_DLLFUNC(iphlpapi, get_tcp_table);
SIGAR_DLLFUNC(iphlpapi, get_udp_table);
SIGAR_DLLFUNC(iphlpapi, get_tcpx_table);
SIGAR_DLLFUNC(iphlpapi, get_udpx_table);
SIGAR_DLLFUNC(iphlpapi, get_tcp_stats);
SIGAR_DLLFUNC(iphlpapi, get_net_params);
SIGAR_DLLFUNC(iphlpapi, get_adapters_info);
SIGAR_DLLFUNC(iphlpapi, get_adapters_addrs);
SIGAR_DLLFUNC(iphlpapi, get_ipnet_table);
sigar_dll_func_t end;
} sigar_iphlpapi_t;
typedef struct {
sigar_dll_handle_t handle;
SIGAR_DLLFUNC(advapi, convert_string_sid);
SIGAR_DLLFUNC(advapi, query_service_status);
sigar_dll_func_t end;
} sigar_advapi_t;
typedef struct {
sigar_dll_handle_t handle;
SIGAR_DLLFUNC(ntdll, query_sys_info);
SIGAR_DLLFUNC(ntdll, query_proc_info);
sigar_dll_func_t end;
} sigar_ntdll_t;
typedef struct {
sigar_dll_handle_t handle;
SIGAR_DLLFUNC(psapi, enum_modules);
SIGAR_DLLFUNC(psapi, enum_processes);
SIGAR_DLLFUNC(psapi, get_module_name);
sigar_dll_func_t end;
} sigar_psapi_t;
typedef struct {
sigar_dll_handle_t handle;
SIGAR_DLLFUNC(winsta, query_info);
sigar_dll_func_t end;
} sigar_winsta_t;
typedef struct {
sigar_dll_handle_t handle;
SIGAR_DLLFUNC(kernel, memory_status);
sigar_dll_func_t end;
} sigar_kernel_t;
typedef struct {
sigar_dll_handle_t handle;
SIGAR_DLLFUNC(mpr, get_net_connection);
sigar_dll_func_t end;
} sigar_mpr_t;
typedef struct
{
char *buffer;
DWORD size;
time_t create_time;
} buffer_t;
struct sigar_t {
SIGAR_T_BASE;
char *machine;
int using_wide;
long pagesize;
HKEY handle;
buffer_t** performanceBuffers;
buffer_t* processesBuffer;
sigar_wtsapi_t wtsapi;
sigar_iphlpapi_t iphlpapi;
sigar_advapi_t advapi;
sigar_ntdll_t ntdll;
sigar_psapi_t psapi;
sigar_winsta_t winsta;
sigar_kernel_t kernel;
sigar_mpr_t mpr;
sigar_win32_pinfo_t pinfo;
sigar_cache_t *netif_adapters;
sigar_cache_t *netif_mib_rows;
sigar_cache_t *netif_addr_rows;
sigar_cache_t *netif_names; /* dwIndex -> net_interface_config.name */
int netif_name_short;
WORD ws_version;
int ws_error;
int ht_enabled;
int lcpu; //number of logical cpus
int winnt;
};
#ifdef __cplusplus
extern "C" {
#endif
sigar_uint64_t sigar_FileTimeToTime(FILETIME *ft);
int sigar_wsa_init(sigar_t *sigar);
int sigar_proc_exe_peb_get(sigar_t *sigar, HANDLE proc,
sigar_proc_exe_t *procexe);
int sigar_proc_args_peb_get(sigar_t *sigar, HANDLE proc,
sigar_proc_args_t *procargs);
int sigar_proc_env_peb_get(sigar_t *sigar, HANDLE proc,
WCHAR *env, DWORD envlen);
int sigar_proc_args_wmi_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_args_t *procargs);
int sigar_proc_exe_wmi_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_exe_t *procexe);
int sigar_parse_proc_args(sigar_t *sigar, WCHAR *buf,
sigar_proc_args_t *procargs);
int sigar_service_pid_get(sigar_t *sigar, char *name, sigar_pid_t *pid);
typedef struct {
DWORD size;
DWORD count;
ENUM_SERVICE_STATUS *services;
SC_HANDLE handle;
} sigar_services_status_t;
int sigar_services_status_get(sigar_services_status_t *ss, DWORD state);
void sigar_services_status_close(sigar_services_status_t *ss);
typedef struct sigar_services_walker_t sigar_services_walker_t;
struct sigar_services_walker_t {
sigar_t *sigar;
int flags;
void *data; /* user data */
int (*add_service)(sigar_services_walker_t *walker, char *name);
};
int sigar_services_query(char *ptql,
sigar_ptql_error_t *error,
sigar_services_walker_t *walker);
char *sigar_service_exe_get(char *path, char *buffer, int basename);
typedef struct {
WORD product_major;
WORD product_minor;
WORD product_build;
WORD product_revision;
WORD file_major;
WORD file_minor;
WORD file_build;
WORD file_revision;
} sigar_file_version_t;
int sigar_file_version_get(sigar_file_version_t *version,
char *name,
sigar_proc_env_t *infocb);
#ifdef __cplusplus
}
#endif
#define SIGAR_NO_SUCH_PROCESS (SIGAR_OS_START_ERROR+1)
#endif /* SIGAR_OS_H */

View File

@ -0,0 +1,47 @@
/*
* Copyright (c) 2004, 2006 Hyperic, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef SIGAR_PDH_H
#define SIGAR_PDH_H
/* performance data helpers */
#define PdhFirstObject(block) \
((PERF_OBJECT_TYPE *)((BYTE *) block + block->HeaderLength))
#define PdhNextObject(object) \
((PERF_OBJECT_TYPE *)((BYTE *) object + object->TotalByteLength))
#define PdhFirstCounter(object) \
((PERF_COUNTER_DEFINITION *)((BYTE *) object + object->HeaderLength))
#define PdhNextCounter(counter) \
((PERF_COUNTER_DEFINITION *)((BYTE *) counter + counter->ByteLength))
#define PdhGetCounterBlock(inst) \
((PERF_COUNTER_BLOCK *)((BYTE *) inst + inst->ByteLength))
#define PdhFirstInstance(object) \
((PERF_INSTANCE_DEFINITION *)((BYTE *) object + object->DefinitionLength))
#define PdhNextInstance(inst) \
((PERF_INSTANCE_DEFINITION *)((BYTE *)inst + inst->ByteLength + \
PdhGetCounterBlock(inst)->ByteLength))
#define PdhInstanceName(inst) \
((wchar_t *)((BYTE *)inst + inst->NameOffset))
#endif /* SIGAR_PDH_H */

View File

@ -0,0 +1,429 @@
/*
* Copyright (c) 2004-2008 Hyperic, Inc.
* Copyright (c) 2009 SpringSource, Inc.
* Copyright (c) 2009-2010 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef SIGAR_PRIVATE_DOT_H
#define SIGAR_PRIVATE_DOT_H
#include "sigar_log.h"
#include "sigar_ptql.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#ifndef WIN32
#include <unistd.h>
#include <stddef.h>
#ifndef DARWIN
#include <strings.h>
#endif
#endif
#ifdef DMALLOC
#define _MEMORY_H /* exclude memory.h on solaris */
#define DMALLOC_FUNC_CHECK
#include <dmalloc.h>
#endif
/* common to all os sigar_t's */
/* XXX: this is ugly; but don't want the same stuffs
* duplicated on 4 platforms and am too lazy to change
* sigar_t to the way it was originally where sigar_t was
* common and contained a sigar_os_t.
* feel free trav ;-)
*/
#define SIGAR_T_BASE \
int cpu_list_cores; \
int log_level; \
void *log_data; \
sigar_log_impl_t log_impl; \
void *ptql_re_data; \
sigar_ptql_re_impl_t ptql_re_impl; \
unsigned int ncpu; \
unsigned long version; \
unsigned long boot_time; \
int ticks; \
sigar_pid_t pid; \
char errbuf[256]; \
char *ifconf_buf; \
int ifconf_len; \
char *self_path; \
sigar_proc_list_t *pids; \
sigar_cache_t *fsdev; \
sigar_cache_t *proc_cpu; \
sigar_cache_t *net_listen; \
sigar_cache_t *net_services_tcp; \
sigar_cache_t *net_services_udp;\
sigar_cache_t *proc_io
#if defined(WIN32)
# define SIGAR_INLINE __inline
#elif defined(__GNUC__)
# define SIGAR_INLINE inline
#else
# define SIGAR_INLINE
#endif
#ifdef DMALLOC
/* linux has its own strdup macro, make sure we use dmalloc's */
#define sigar_strdup(s) \
dmalloc_strndup(__FILE__, __LINE__, (s), -1, 0)
#else
# ifdef WIN32
# define sigar_strdup(s) _strdup(s)
# else
# define sigar_strdup(s) strdup(s)
# endif
#endif
#define SIGAR_ZERO(s) \
memset(s, '\0', sizeof(*(s)))
#define SIGAR_STRNCPY(dest, src, len) \
strncpy(dest, src, len); \
dest[len-1] = '\0'
/* we use fixed size buffers pretty much everywhere */
/* this is strncpy + ensured \0 terminator */
#define SIGAR_SSTRCPY(dest, src) \
SIGAR_STRNCPY(dest, src, sizeof(dest))
#ifndef strEQ
#define strEQ(s1, s2) (strcmp(s1, s2) == 0)
#endif
#ifndef strnEQ
#define strnEQ(s1, s2, n) (strncmp(s1, s2, n) == 0)
#endif
#ifdef WIN32
#define strcasecmp stricmp
#define strncasecmp strnicmp
#endif
#ifndef strcaseEQ
#define strcaseEQ(s1, s2) (strcasecmp(s1, s2) == 0)
#endif
#ifndef strncaseEQ
#define strncaseEQ(s1, s2, n) (strncasecmp(s1, s2, n) == 0)
#endif
#ifdef offsetof
#define sigar_offsetof offsetof
#else
#define sigar_offsetof(type, field) ((size_t)(&((type *)0)->field))
#endif
#define SIGAR_MSEC 1000L
#define SIGAR_USEC 1000000L
#define SIGAR_NSEC 1000000000L
#define SIGAR_SEC2NANO(s) \
((sigar_uint64_t)(s) * (sigar_uint64_t)SIGAR_NSEC)
/* cpu ticks to milliseconds */
#define SIGAR_TICK2MSEC(s) \
((sigar_uint64_t)(s) * ((sigar_uint64_t)SIGAR_MSEC / (double)sigar->ticks))
#define SIGAR_TICK2NSEC(s) \
((sigar_uint64_t)(s) * ((sigar_uint64_t)SIGAR_NSEC / (double)sigar->ticks))
/* nanoseconds to milliseconds */
#define SIGAR_NSEC2MSEC(s) \
((sigar_uint64_t)(s) / ((sigar_uint64_t)1000000L))
#define IFTYPE_LO 2
#define IFTYPE_ETH 3
#define SIGAR_LAST_PROC_EXPIRE 2
#define SIGAR_BUFFER_EXPIRE 1000
#define SIGAR_FS_MAX 10
#define SIGAR_CPU_INFO_MAX 4
#define SIGAR_CPU_LIST_MAX 4
#define SIGAR_PROC_LIST_MAX 256
#define SIGAR_PROC_ARGS_MAX 12
#define SIGAR_NET_ROUTE_LIST_MAX 6
#define SIGAR_NET_IFLIST_MAX 20
#define SIGAR_NET_CONNLIST_MAX 20
#define SIGAR_ARP_LIST_MAX 12
#define SIGAR_WHO_LIST_MAX 12
int sigar_os_open(sigar_t **sigar);
int sigar_os_close(sigar_t *sigar);
char *sigar_os_error_string(sigar_t *sigar, int err);
char *sigar_strerror_get(int err, char *errbuf, int buflen);
void sigar_strerror_set(sigar_t *sigar, char *msg);
void sigar_strerror_printf(sigar_t *sigar, const char *format, ...);
int sigar_sys_info_get_uname(sigar_sys_info_t *sysinfo);
int sigar_os_sys_info_get(sigar_t *sigar, sigar_sys_info_t *sysinfo);
int sigar_os_proc_list_get(sigar_t *sigar,
sigar_proc_list_t *proclist);
int sigar_proc_list_create(sigar_proc_list_t *proclist);
int sigar_proc_list_grow(sigar_proc_list_t *proclist);
#define SIGAR_PROC_LIST_GROW(proclist) \
if (proclist->number >= proclist->size) { \
sigar_proc_list_grow(proclist); \
}
int sigar_proc_args_create(sigar_proc_args_t *proclist);
int sigar_proc_args_grow(sigar_proc_args_t *procargs);
#define SIGAR_PROC_ARGS_GROW(procargs) \
if (procargs->number >= procargs->size) { \
sigar_proc_args_grow(procargs); \
}
int sigar_os_proc_args_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_args_t *procargs);
int sigar_file_system_list_create(sigar_file_system_list_t *fslist);
int sigar_file_system_list_grow(sigar_file_system_list_t *fslist);
#define SIGAR_FILE_SYSTEM_LIST_GROW(fslist) \
if (fslist->number >= fslist->size) { \
sigar_file_system_list_grow(fslist); \
}
int sigar_os_fs_type_get(sigar_file_system_t *fsp);
/* os plugins that set fsp->type call fs_type_get directly */
#define sigar_fs_type_init(fsp) \
fsp->type = SIGAR_FSTYPE_UNKNOWN; \
sigar_fs_type_get(fsp)
void sigar_fs_type_get(sigar_file_system_t *fsp);
int sigar_cpu_info_list_create(sigar_cpu_info_list_t *cpu_infos);
int sigar_cpu_info_list_grow(sigar_cpu_info_list_t *cpu_infos);
#define SIGAR_CPU_INFO_LIST_GROW(cpu_infos) \
if (cpu_infos->number >= cpu_infos->size) { \
sigar_cpu_info_list_grow(cpu_infos); \
}
int sigar_cpu_list_create(sigar_cpu_list_t *cpulist);
int sigar_cpu_list_grow(sigar_cpu_list_t *cpulist);
#define SIGAR_CPU_LIST_GROW(cpulist) \
if (cpulist->number >= cpulist->size) { \
sigar_cpu_list_grow(cpulist); \
}
int sigar_net_route_list_create(sigar_net_route_list_t *routelist);
int sigar_net_route_list_grow(sigar_net_route_list_t *net_routelist);
#define SIGAR_NET_ROUTE_LIST_GROW(routelist) \
if (routelist->number >= routelist->size) { \
sigar_net_route_list_grow(routelist); \
}
int sigar_net_interface_list_create(sigar_net_interface_list_t *iflist);
int sigar_net_interface_list_grow(sigar_net_interface_list_t *iflist);
#define SIGAR_NET_IFLIST_GROW(iflist) \
if (iflist->number >= iflist->size) { \
sigar_net_interface_list_grow(iflist); \
}
int sigar_net_connection_list_create(sigar_net_connection_list_t *connlist);
int sigar_net_connection_list_grow(sigar_net_connection_list_t *connlist);
#define SIGAR_NET_CONNLIST_GROW(connlist) \
if (connlist->number >= connlist->size) { \
sigar_net_connection_list_grow(connlist); \
}
#define sigar_net_address_set(a, val) \
(a).addr.in = val; \
(a).family = SIGAR_AF_INET
#define sigar_net_address6_set(a, val) \
memcpy(&((a).addr.in6), val, sizeof((a).addr.in6)); \
(a).family = SIGAR_AF_INET6
#define SIGAR_IFHWADDRLEN 6
#define sigar_net_address_mac_set(a, val, len) \
memcpy(&((a).addr.mac), val, len); \
(a).family = SIGAR_AF_LINK
#define sigar_hwaddr_set_null(ifconfig) \
SIGAR_ZERO(&ifconfig->hwaddr.addr.mac); \
ifconfig->hwaddr.family = SIGAR_AF_LINK
int sigar_net_interface_ipv6_config_get(sigar_t *sigar, const char *name,
sigar_net_interface_config_t *ifconfig);
#define sigar_net_interface_ipv6_config_init(ifconfig) \
ifconfig->address6.family = SIGAR_AF_INET6; \
ifconfig->prefix6_length = 0; \
ifconfig->scope6 = 0
#define SIGAR_SIN6(s) ((struct sockaddr_in6 *)(s))
#define SIGAR_SIN6_ADDR(s) &SIGAR_SIN6(s)->sin6_addr
#define sigar_net_interface_scope6_set(ifconfig, addr) \
if (IN6_IS_ADDR_LINKLOCAL(addr)) \
ifconfig->scope6 = SIGAR_IPV6_ADDR_LINKLOCAL; \
else if (IN6_IS_ADDR_SITELOCAL(addr)) \
ifconfig->scope6 = SIGAR_IPV6_ADDR_SITELOCAL; \
else if (IN6_IS_ADDR_V4COMPAT(addr)) \
ifconfig->scope6 = SIGAR_IPV6_ADDR_COMPATv4; \
else if (IN6_IS_ADDR_LOOPBACK(addr)) \
ifconfig->scope6 = SIGAR_IPV6_ADDR_LOOPBACK; \
else \
ifconfig->scope6 = SIGAR_IPV6_ADDR_ANY
int sigar_tcp_curr_estab(sigar_t *sigar, sigar_tcp_t *tcp);
int sigar_arp_list_create(sigar_arp_list_t *arplist);
int sigar_arp_list_grow(sigar_arp_list_t *arplist);
#define SIGAR_ARP_LIST_GROW(arplist) \
if (arplist->number >= arplist->size) { \
sigar_arp_list_grow(arplist); \
}
int sigar_who_list_create(sigar_who_list_t *wholist);
int sigar_who_list_grow(sigar_who_list_t *wholist);
#define SIGAR_WHO_LIST_GROW(wholist) \
if (wholist->number >= wholist->size) { \
sigar_who_list_grow(wholist); \
}
int sigar_user_id_get(sigar_t *sigar, const char *name, int *uid);
int sigar_user_name_get(sigar_t *sigar, int uid, char *buf, int buflen);
int sigar_group_name_get(sigar_t *sigar, int gid, char *buf, int buflen);
#define SIGAR_PROC_ENV_KEY_LOOKUP() \
if ((procenv->type == SIGAR_PROC_ENV_KEY) && \
(pid == sigar->pid)) \
{ \
char *value = getenv(procenv->key); \
if (value != NULL) { \
procenv->env_getter(procenv->data, \
procenv->key, \
procenv->klen, \
value, strlen(value)); \
} \
return SIGAR_OK; \
}
#define SIGAR_DISK_STATS_INIT(disk) \
(disk)->reads = (disk)->writes = \
(disk)->read_bytes = (disk)->write_bytes = \
(disk)->rtime = (disk)->wtime = (disk)->qtime = (disk)->time = \
(disk)->queue = (disk)->service_time = SIGAR_FIELD_NOTIMPL; \
(disk)->snaptime = 0
/* key used for filesystem (/) -> device (/dev/hda1) mapping */
/* and disk_usage cache for service_time */
#define SIGAR_FSDEV_ID(sb) \
(S_ISBLK((sb).st_mode) ? (sb).st_rdev : ((sb).st_ino + (sb).st_dev))
#if defined(WIN32) || defined(NETWARE)
int sigar_get_iftype(const char *name, int *type, int *inst);
#endif
#define SIGAR_NIC_LOOPBACK "Local Loopback"
#define SIGAR_NIC_UNSPEC "UNSPEC"
#define SIGAR_NIC_SLIP "Serial Line IP"
#define SIGAR_NIC_CSLIP "VJ Serial Line IP"
#define SIGAR_NIC_SLIP6 "6-bit Serial Line IP"
#define SIGAR_NIC_CSLIP6 "VJ 6-bit Serial Line IP"
#define SIGAR_NIC_ADAPTIVE "Adaptive Serial Line IP"
#define SIGAR_NIC_ETHERNET "Ethernet"
#define SIGAR_NIC_ASH "Ash"
#define SIGAR_NIC_FDDI "Fiber Distributed Data Interface"
#define SIGAR_NIC_HIPPI "HIPPI"
#define SIGAR_NIC_AX25 "AMPR AX.25"
#define SIGAR_NIC_ROSE "AMPR ROSE"
#define SIGAR_NIC_NETROM "AMPR NET/ROM"
#define SIGAR_NIC_X25 "generic X.25"
#define SIGAR_NIC_TUNNEL "IPIP Tunnel"
#define SIGAR_NIC_PPP "Point-to-Point Protocol"
#define SIGAR_NIC_HDLC "(Cisco)-HDLC"
#define SIGAR_NIC_LAPB "LAPB"
#define SIGAR_NIC_ARCNET "ARCnet"
#define SIGAR_NIC_DLCI "Frame Relay DLCI"
#define SIGAR_NIC_FRAD "Frame Relay Access Device"
#define SIGAR_NIC_SIT "IPv6-in-IPv4"
#define SIGAR_NIC_IRDA "IrLAP"
#define SIGAR_NIC_EC "Econet"
#define PID_CACHE_CLEANUP_PERIOD 1000*60*10 /* 10 minutes */
#define PID_CACHE_ENTRY_EXPIRE_PERIOD 1000*60*20 /* 20 minutes */
#ifndef WIN32
#include <netdb.h>
#endif
#define PROC_PID_CPU_CACHE 1
#define PROC_PID_IO_CACHE 2
#define SIGAR_HOSTENT_LEN 1024
#if defined(_AIX)
#define SIGAR_HAS_HOSTENT_DATA
#endif
typedef struct {
char buffer[SIGAR_HOSTENT_LEN];
int error;
#ifndef WIN32
struct hostent hs;
#endif
#ifdef SIGAR_HAS_HOSTENT_DATA
struct hostent_data hd;
#endif
} sigar_hostent_t;
#endif

View File

@ -0,0 +1,53 @@
/*
* Copyright (c) 2006-2007 Hyperic, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef SIGAR_PTQL_H
#define SIGAR_PTQL_H
#define SIGAR_PTQL_MALFORMED_QUERY -1
typedef struct sigar_ptql_query_t sigar_ptql_query_t;
#define SIGAR_PTQL_ERRMSG_SIZE 1024
typedef struct {
char message[SIGAR_PTQL_ERRMSG_SIZE];
} sigar_ptql_error_t;
typedef int (*sigar_ptql_re_impl_t)(void *, char *, char *);
SIGAR_DECLARE(void) sigar_ptql_re_impl_set(sigar_t *sigar, void *data,
sigar_ptql_re_impl_t impl);
SIGAR_DECLARE(int) sigar_ptql_query_create(sigar_ptql_query_t **query,
char *ptql,
sigar_ptql_error_t *error);
SIGAR_DECLARE(int) sigar_ptql_query_match(sigar_t *sigar,
sigar_ptql_query_t *query,
sigar_pid_t pid);
SIGAR_DECLARE(int) sigar_ptql_query_destroy(sigar_ptql_query_t *query);
SIGAR_DECLARE(int) sigar_ptql_query_find_process(sigar_t *sigar,
sigar_ptql_query_t *query,
sigar_pid_t *pid);
SIGAR_DECLARE(int) sigar_ptql_query_find(sigar_t *sigar,
sigar_ptql_query_t *query,
sigar_proc_list_t *proclist);
#endif /*SIGAR_PTQL_H*/

View File

@ -0,0 +1,197 @@
/*
* Copyright (c) 2004-2008 Hyperic, Inc.
* Copyright (c) 2009 SpringSource, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef SIGAR_UTIL_H
#define SIGAR_UTIL_H
/* most of this is crap for dealing with linux /proc */
#define UITOA_BUFFER_SIZE \
(sizeof(int) * 3 + 1)
#define SSTRLEN(s) \
(sizeof(s)-1)
#define sigar_strtoul(ptr) \
strtoul(ptr, &ptr, 10)
#define sigar_strtoull(ptr) \
strtoull(ptr, &ptr, 10)
#define sigar_isspace(c) \
(isspace(((unsigned char)(c))))
#define sigar_isdigit(c) \
(isdigit(((unsigned char)(c))))
#define sigar_isalpha(c) \
(isalpha(((unsigned char)(c))))
#define sigar_isupper(c) \
(isupper(((unsigned char)(c))))
#define sigar_tolower(c) \
(tolower(((unsigned char)(c))))
#ifdef WIN32
#define sigar_fileno _fileno
#define sigar_isatty _isatty
#define sigar_write _write
#else
#define sigar_fileno fileno
#define sigar_isatty isatty
#define sigar_write write
#endif
#ifndef PROC_FS_ROOT
#define PROC_FS_ROOT "/proc/"
#endif
#ifndef PROCP_FS_ROOT
#define PROCP_FS_ROOT "/proc/"
#endif
sigar_int64_t sigar_time_now_millis(void);
char *sigar_uitoa(char *buf, unsigned int n, int *len);
int sigar_inet_ntoa(sigar_t *sigar,
sigar_uint32_t address,
char *addr_str);
struct hostent *sigar_gethostbyname(const char *name,
sigar_hostent_t *data);
SIGAR_INLINE char *sigar_skip_line(char *buffer, int buflen);
SIGAR_INLINE char *sigar_skip_token(char *p);
SIGAR_INLINE char *sigar_skip_multiple_token(char *p, int count);
char *sigar_getword(char **line, char stop);
char *sigar_strcasestr(const char *s1, const char *s2);
int sigar_file2str(const char *fname, char *buffer, int buflen);
int sigar_proc_file2str(char *buffer, int buflen,
sigar_pid_t pid,
const char *fname,
int fname_len);
#define SIGAR_PROC_FILE2STR(buffer, pid, fname) \
sigar_proc_file2str(buffer, sizeof(buffer), \
pid, fname, SSTRLEN(fname))
#define SIGAR_PROC_FILENAME(buffer, pid, fname) \
sigar_proc_filename(buffer, sizeof(buffer), \
pid, fname, SSTRLEN(fname))
#define SIGAR_SKIP_SPACE(ptr) \
while (sigar_isspace(*ptr)) ++ptr
char *sigar_proc_filename(char *buffer, int buflen,
sigar_pid_t pid,
const char *fname, int fname_len);
int sigar_proc_list_procfs_get(sigar_t *sigar,
sigar_proc_list_t *proclist);
int sigar_proc_fd_count(sigar_t *sigar, sigar_pid_t pid,
sigar_uint64_t *total);
/* linux + freebsd */
int sigar_procfs_args_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_args_t *procargs);
int sigar_mem_calc_ram(sigar_t *sigar, sigar_mem_t *mem);
int sigar_statvfs(sigar_t *sigar,
const char *dirname,
sigar_file_system_usage_t *fsusage);
double sigar_file_system_usage_calc_used(sigar_t *sigar,
sigar_file_system_usage_t *fs);
#define SIGAR_DEV_PREFIX "/dev/"
#define SIGAR_NAME_IS_DEV(dev) \
strnEQ(dev, SIGAR_DEV_PREFIX, SSTRLEN(SIGAR_DEV_PREFIX))
typedef struct {
char name[256];
int is_partition;
sigar_disk_usage_t disk;
} sigar_iodev_t;
sigar_iodev_t *sigar_iodev_get(sigar_t *sigar,
const char *dirname);
int sigar_cpu_core_count(sigar_t *sigar);
/* e.g. VM guest may have 1 virtual ncpu on multicore hosts */
#define sigar_cpu_socket_count(sigar) \
(sigar->ncpu < sigar->lcpu) ? sigar->ncpu : \
(sigar->ncpu / sigar->lcpu)
int sigar_cpu_core_rollup(sigar_t *sigar);
void sigar_cpu_model_adjust(sigar_t *sigar, sigar_cpu_info_t *info);
int sigar_cpu_mhz_from_model(char *model);
char *sigar_get_self_path(sigar_t *sigar);
#if defined(__sun) || defined(__FreeBSD__)
#define SIGAR_HAS_DLINFO_MODULES
#include <dlfcn.h>
#include <link.h>
int sigar_dlinfo_modules(sigar_t *sigar, sigar_proc_modules_t *procmods);
#endif
typedef struct sigar_cache_entry_t sigar_cache_entry_t;
struct sigar_cache_entry_t {
sigar_cache_entry_t *next;
sigar_uint64_t id;
void *value;
sigar_uint64_t last_access_time;
};
typedef struct {
sigar_cache_entry_t **entries;
unsigned int count, size;
void (*free_value)(void *ptr);
sigar_uint64_t entry_expire_period;
sigar_uint64_t cleanup_period_millis;
sigar_uint64_t last_cleanup_time;
} sigar_cache_t;
sigar_cache_t *sigar_cache_new(int size);
sigar_cache_t *sigar_expired_cache_new(int size, sigar_uint64_t cleanup_period_millis, sigar_uint64_t entry_expire_period);
void sigar_cache_dump(sigar_cache_t *table);
sigar_cache_entry_t *sigar_cache_get(sigar_cache_t *table,
sigar_uint64_t key);
sigar_cache_entry_t *sigar_cache_find(sigar_cache_t *table,
sigar_uint64_t key);
void sigar_cache_destroy(sigar_cache_t *table);
#endif /* SIGAR_UTIL_H */

View File

@ -0,0 +1,212 @@
/*
* Copyright (c) 2004, 2006-2008 Hyperic, Inc.
* Copyright (c) 2009 SpringSource, Inc.
* Copyright (c) 2009 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* functions for getting info from the Process Environment Block
*/
#define UNICODE
#define _UNICODE
#include "sigar.h"
#include "sigar_private.h"
#include "sigar_os.h"
#include <shellapi.h>
void dllmod_init_ntdll(sigar_t *sigar);
#define sigar_NtQueryInformationProcess \
sigar->ntdll.query_proc_info.func
static int sigar_pbi_get(sigar_t *sigar, HANDLE proc, PEB *peb)
{
int status;
PROCESS_BASIC_INFORMATION pbi;
DWORD size=sizeof(pbi);
dllmod_init_ntdll(sigar);
if (!sigar_NtQueryInformationProcess) {
return SIGAR_ENOTIMPL;
}
SIGAR_ZERO(&pbi);
status =
sigar_NtQueryInformationProcess(proc,
ProcessBasicInformation,
&pbi,
size, NULL);
if (status != ERROR_SUCCESS) {
return status;
}
if (!pbi.PebBaseAddress) {
/* likely we are 32-bit, pid process is 64-bit */
return ERROR_DATATYPE_MISMATCH;
}
size = sizeof(*peb);
if (ReadProcessMemory(proc, pbi.PebBaseAddress, peb, size, NULL)) {
return SIGAR_OK;
}
else {
return GetLastError();
}
}
static int sigar_rtl_get(sigar_t *sigar, HANDLE proc,
RTL_USER_PROCESS_PARAMETERS *rtl)
{
PEB peb;
int status = sigar_pbi_get(sigar, proc, &peb);
DWORD size=sizeof(*rtl);
if (status != SIGAR_OK) {
return status;
}
if (ReadProcessMemory(proc, peb.ProcessParameters, rtl, size, NULL)) {
return SIGAR_OK;
}
else {
return GetLastError();
}
}
#define rtl_bufsize(buf, uc) \
((sizeof(buf) < uc.Length) ? sizeof(buf) : uc.Length)
int sigar_proc_exe_peb_get(sigar_t *sigar, HANDLE proc,
sigar_proc_exe_t *procexe)
{
int status;
WCHAR buf[MAX_PATH+1];
RTL_USER_PROCESS_PARAMETERS rtl;
DWORD size;
procexe->name[0] = '\0';
procexe->cwd[0] = '\0';
if ((status = sigar_rtl_get(sigar, proc, &rtl)) != SIGAR_OK) {
return status;
}
size = rtl_bufsize(buf, rtl.ImagePathName);
memset(buf, '\0', sizeof(buf));
if ((size > 0) &&
ReadProcessMemory(proc, rtl.ImagePathName.Buffer, buf, size, NULL))
{
SIGAR_W2A(buf, procexe->name, sizeof(procexe->name));
}
size = rtl_bufsize(buf, rtl.CurrentDirectoryName);
memset(buf, '\0', sizeof(buf));
if ((size > 0) &&
ReadProcessMemory(proc, rtl.CurrentDirectoryName.Buffer, buf, size, NULL))
{
SIGAR_W2A(buf, procexe->cwd, sizeof(procexe->cwd));
}
return SIGAR_OK;
}
int sigar_parse_proc_args(sigar_t *sigar, WCHAR *buf,
sigar_proc_args_t *procargs)
{
char arg[SIGAR_CMDLINE_MAX];
LPWSTR *args;
int num, i;
if (!buf) {
buf = GetCommandLine();
}
args = CommandLineToArgvW(buf, &num);
if (args == NULL) {
return SIGAR_OK;
}
for (i=0; i<num; i++) {
SIGAR_W2A(args[i], arg, SIGAR_CMDLINE_MAX);
SIGAR_PROC_ARGS_GROW(procargs);
procargs->data[procargs->number++] = sigar_strdup(arg);
}
GlobalFree(args);
return SIGAR_OK;
}
int sigar_proc_args_peb_get(sigar_t *sigar, HANDLE proc,
sigar_proc_args_t *procargs)
{
int status;
WCHAR buf[SIGAR_CMDLINE_MAX];
RTL_USER_PROCESS_PARAMETERS rtl;
DWORD size;
if ((status = sigar_rtl_get(sigar, proc, &rtl)) != SIGAR_OK) {
return status;
}
size = rtl_bufsize(buf, rtl.CommandLine);
if (size <= 0) {
return ERROR_DATATYPE_MISMATCH; /* fallback to wmi */
}
memset(buf, '\0', sizeof(buf));
if (ReadProcessMemory(proc, rtl.CommandLine.Buffer, buf, size, NULL)) {
return sigar_parse_proc_args(sigar, buf, procargs);
}
else {
return GetLastError();
}
}
int sigar_proc_env_peb_get(sigar_t *sigar, HANDLE proc,
WCHAR *buf, DWORD size)
{
int status;
RTL_USER_PROCESS_PARAMETERS rtl;
MEMORY_BASIC_INFORMATION info;
if ((status = sigar_rtl_get(sigar, proc, &rtl)) != SIGAR_OK) {
return status;
}
memset(buf, '\0', size);
/* -2 to ensure \0\0 terminator */
size -= 2;
if (VirtualQueryEx(proc, rtl.Environment, &info, sizeof(info))) {
if (size > info.RegionSize) {
/* ReadProcessMemory beyond region would fail */
size = info.RegionSize;
}
}
if (ReadProcessMemory(proc, rtl.Environment, buf, size, NULL)) {
return SIGAR_OK;
}
else {
return GetLastError();
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,243 @@
/*
* Copyright (c) 2009 SpringSource, Inc.
* Copyright (c) 2009 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define UNICODE
#define _UNICODE
#define _WIN32_DCOM
#include <windows.h>
#include <objbase.h>
#include <comdef.h>
#include <wbemidl.h>
#include "sigar.h"
#pragma comment(lib, "wbemuuid.lib")
#ifndef SIGAR_CMDLINE_MAX
#define SIGAR_CMDLINE_MAX 4096<<2
#endif
class WMI {
public:
WMI();
~WMI();
HRESULT Open(LPCTSTR machine=NULL, LPCTSTR user=NULL, LPCTSTR pass=NULL);
void Close();
HRESULT GetProcStringProperty(DWORD pid, TCHAR *name, TCHAR *value, DWORD len);
HRESULT GetProcExecutablePath(DWORD pid, TCHAR *value);
HRESULT GetProcCommandLine(DWORD pid, TCHAR *value);
int GetLastError();
private:
IWbemServices *wbem;
HRESULT result;
BSTR GetProcQuery(DWORD pid);
};
WMI::WMI()
{
wbem = NULL;
result = S_OK;
CoInitializeEx(NULL, COINIT_MULTITHREADED);
}
WMI::~WMI()
{
Close();
CoUninitialize();
}
/* XXX must be a better way to map HRESULT */
int WMI::GetLastError()
{
switch (result) {
case S_OK:
return ERROR_SUCCESS;
case WBEM_E_NOT_FOUND:
return ERROR_NOT_FOUND;
case WBEM_E_ACCESS_DENIED:
return ERROR_ACCESS_DENIED;
case WBEM_E_NOT_SUPPORTED:
return SIGAR_ENOTIMPL;
default:
return ERROR_INVALID_FUNCTION;
}
}
HRESULT WMI::Open(LPCTSTR machine, LPCTSTR user, LPCTSTR pass)
{
IWbemLocator *locator;
wchar_t path[MAX_PATH];
if (wbem) {
result = S_OK;
return result;
}
result =
CoInitializeSecurity(NULL, //Security Descriptor
-1, //COM authentication
NULL, //Authentication services
NULL, //Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, //Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, //Default Impersonation
NULL, //Authentication info
EOAC_NONE, //Additional capabilities
NULL); //Reserved
result = CoCreateInstance(CLSID_WbemLocator,
NULL, /* IUnknown */
CLSCTX_INPROC_SERVER,
IID_IWbemLocator,
(LPVOID *)&locator);
if (FAILED(result)) {
return result;
}
if (machine == NULL) {
machine = L".";
}
wsprintf(path, L"\\\\%S\\ROOT\\CIMV2", machine);
result = locator->ConnectServer(bstr_t(path), //Object path of WMI namespace
bstr_t(user), //User name. NULL = current user
bstr_t(pass), //User password. NULL = current
NULL, //Locale. NULL indicates current
0, //Security flags
NULL, //Authority (e.g. Kerberos)
NULL, //Context object
&wbem); //pointer to IWbemServices proxy
locator->Release();
return result;
}
void WMI::Close()
{
if (wbem) {
wbem->Release();
wbem = NULL;
result = S_OK;
}
}
BSTR WMI::GetProcQuery(DWORD pid)
{
wchar_t query[56];
wsprintf(query, L"Win32_Process.Handle=%d", pid);
return bstr_t(query);
}
HRESULT WMI::GetProcStringProperty(DWORD pid, TCHAR *name, TCHAR *value, DWORD len)
{
IWbemClassObject *obj;
VARIANT var;
result = wbem->GetObject(GetProcQuery(pid), 0, 0, &obj, 0);
if (FAILED(result)) {
return result;
}
result = obj->Get(name, 0, &var, 0, 0);
if (SUCCEEDED(result)) {
if (var.vt == VT_NULL) {
result = E_INVALIDARG;
}
else {
lstrcpyn(value, var.bstrVal, len);
}
VariantClear(&var);
}
obj->Release();
return result;
}
HRESULT WMI::GetProcExecutablePath(DWORD pid, TCHAR *value)
{
return GetProcStringProperty(pid, L"ExecutablePath", value, MAX_PATH);
}
HRESULT WMI::GetProcCommandLine(DWORD pid, TCHAR *value)
{
return GetProcStringProperty(pid, L"CommandLine", value, SIGAR_CMDLINE_MAX);
}
/* in peb.c */
extern "C" int sigar_parse_proc_args(sigar_t *sigar, WCHAR *buf,
sigar_proc_args_t *procargs);
extern "C" int sigar_proc_args_wmi_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_args_t *procargs)
{
int status;
TCHAR buf[SIGAR_CMDLINE_MAX];
WMI *wmi = new WMI();
if (FAILED(wmi->Open())) {
return wmi->GetLastError();
}
if (FAILED(wmi->GetProcCommandLine(pid, buf))) {
status = wmi->GetLastError();
}
else {
status = sigar_parse_proc_args(sigar, buf, procargs);
}
wmi->Close();
delete wmi;
return status;
}
extern "C" int sigar_proc_exe_wmi_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_exe_t *procexe)
{
int status;
TCHAR buf[MAX_PATH+1];
WMI *wmi = new WMI();
if (FAILED(wmi->Open())) {
return wmi->GetLastError();
}
procexe->name[0] = '\0';
if (FAILED(wmi->GetProcExecutablePath(pid, buf))) {
status = wmi->GetLastError();
}
else {
status = SIGAR_OK;
/* SIGAR_W2A(buf, procexe->name, sizeof(procexe->name)); */
WideCharToMultiByte(CP_ACP, 0, buf, -1,
(LPSTR)procexe->name, sizeof(procexe->name),
NULL, NULL);
}
wmi->Close();
delete wmi;
return status;
}

212
go_bindings/Sigar/src/peb.c Normal file
View File

@ -0,0 +1,212 @@
/*
* Copyright (c) 2004, 2006-2008 Hyperic, Inc.
* Copyright (c) 2009 SpringSource, Inc.
* Copyright (c) 2009 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* functions for getting info from the Process Environment Block
*/
#define UNICODE
#define _UNICODE
#include "sigar.h"
#include "sigar_private.h"
#include "sigar_os.h"
#include <shellapi.h>
void dllmod_init_ntdll(sigar_t *sigar);
#define sigar_NtQueryInformationProcess \
sigar->ntdll.query_proc_info.func
static int sigar_pbi_get(sigar_t *sigar, HANDLE proc, PEB *peb)
{
int status;
PROCESS_BASIC_INFORMATION pbi;
DWORD size=sizeof(pbi);
dllmod_init_ntdll(sigar);
if (!sigar_NtQueryInformationProcess) {
return SIGAR_ENOTIMPL;
}
SIGAR_ZERO(&pbi);
status =
sigar_NtQueryInformationProcess(proc,
ProcessBasicInformation,
&pbi,
size, NULL);
if (status != ERROR_SUCCESS) {
return status;
}
if (!pbi.PebBaseAddress) {
/* likely we are 32-bit, pid process is 64-bit */
return ERROR_DATATYPE_MISMATCH;
}
size = sizeof(*peb);
if (ReadProcessMemory(proc, pbi.PebBaseAddress, peb, size, NULL)) {
return SIGAR_OK;
}
else {
return GetLastError();
}
}
static int sigar_rtl_get(sigar_t *sigar, HANDLE proc,
RTL_USER_PROCESS_PARAMETERS *rtl)
{
PEB peb;
int status = sigar_pbi_get(sigar, proc, &peb);
DWORD size=sizeof(*rtl);
if (status != SIGAR_OK) {
return status;
}
if (ReadProcessMemory(proc, peb.ProcessParameters, rtl, size, NULL)) {
return SIGAR_OK;
}
else {
return GetLastError();
}
}
#define rtl_bufsize(buf, uc) \
((sizeof(buf) < uc.Length) ? sizeof(buf) : uc.Length)
int sigar_proc_exe_peb_get(sigar_t *sigar, HANDLE proc,
sigar_proc_exe_t *procexe)
{
int status;
WCHAR buf[MAX_PATH+1];
RTL_USER_PROCESS_PARAMETERS rtl;
DWORD size;
procexe->name[0] = '\0';
procexe->cwd[0] = '\0';
if ((status = sigar_rtl_get(sigar, proc, &rtl)) != SIGAR_OK) {
return status;
}
size = rtl_bufsize(buf, rtl.ImagePathName);
memset(buf, '\0', sizeof(buf));
if ((size > 0) &&
ReadProcessMemory(proc, rtl.ImagePathName.Buffer, buf, size, NULL))
{
SIGAR_W2A(buf, procexe->name, sizeof(procexe->name));
}
size = rtl_bufsize(buf, rtl.CurrentDirectoryName);
memset(buf, '\0', sizeof(buf));
if ((size > 0) &&
ReadProcessMemory(proc, rtl.CurrentDirectoryName.Buffer, buf, size, NULL))
{
SIGAR_W2A(buf, procexe->cwd, sizeof(procexe->cwd));
}
return SIGAR_OK;
}
int sigar_parse_proc_args(sigar_t *sigar, WCHAR *buf,
sigar_proc_args_t *procargs)
{
char arg[SIGAR_CMDLINE_MAX];
LPWSTR *args;
int num, i;
if (!buf) {
buf = GetCommandLine();
}
args = CommandLineToArgvW(buf, &num);
if (args == NULL) {
return SIGAR_OK;
}
for (i=0; i<num; i++) {
SIGAR_W2A(args[i], arg, SIGAR_CMDLINE_MAX);
SIGAR_PROC_ARGS_GROW(procargs);
procargs->data[procargs->number++] = sigar_strdup(arg);
}
GlobalFree(args);
return SIGAR_OK;
}
int sigar_proc_args_peb_get(sigar_t *sigar, HANDLE proc,
sigar_proc_args_t *procargs)
{
int status;
WCHAR buf[SIGAR_CMDLINE_MAX];
RTL_USER_PROCESS_PARAMETERS rtl;
DWORD size;
if ((status = sigar_rtl_get(sigar, proc, &rtl)) != SIGAR_OK) {
return status;
}
size = rtl_bufsize(buf, rtl.CommandLine);
if (size <= 0) {
return ERROR_DATATYPE_MISMATCH; /* fallback to wmi */
}
memset(buf, '\0', sizeof(buf));
if (ReadProcessMemory(proc, rtl.CommandLine.Buffer, buf, size, NULL)) {
return sigar_parse_proc_args(sigar, buf, procargs);
}
else {
return GetLastError();
}
}
int sigar_proc_env_peb_get(sigar_t *sigar, HANDLE proc,
WCHAR *buf, DWORD size)
{
int status;
RTL_USER_PROCESS_PARAMETERS rtl;
MEMORY_BASIC_INFORMATION info;
if ((status = sigar_rtl_get(sigar, proc, &rtl)) != SIGAR_OK) {
return status;
}
memset(buf, '\0', size);
/* -2 to ensure \0\0 terminator */
size -= 2;
if (VirtualQueryEx(proc, rtl.Environment, &info, sizeof(info))) {
if (size > info.RegionSize) {
/* ReadProcessMemory beyond region would fail */
size = info.RegionSize;
}
}
if (ReadProcessMemory(proc, rtl.Environment, buf, size, NULL)) {
return SIGAR_OK;
}
else {
return GetLastError();
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,253 @@
/*
* Copyright (c) 2004-2006 Hyperic, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "sigar.h"
#include "sigar_private.h"
#include "sigar_util.h"
#include <stdio.h>
/*
* hash table to cache values where key is a unique number
* such as:
* pid -> some process data
* uid -> user name
* gid -> group name
*/
#define ENTRIES_SIZE(n) \
(sizeof(sigar_cache_entry_t *) * (n))
/* wrap free() for use w/ dmalloc */
static void free_value(void *ptr)
{
free(ptr);
}
sigar_cache_t *sigar_expired_cache_new(int size, sigar_uint64_t cleanup_period_millis, sigar_uint64_t entry_expire_period)
{
sigar_cache_t *table = malloc(sizeof(*table));
table->count = 0;
table->size = size;
table->entries = malloc(ENTRIES_SIZE(size));
memset(table->entries, '\0', ENTRIES_SIZE(size));
table->free_value = free_value;
table->cleanup_period_millis = cleanup_period_millis;
table->last_cleanup_time = sigar_time_now_millis();
table->entry_expire_period = entry_expire_period;
return table;
}
sigar_cache_t *sigar_cache_new(int size)
{
return sigar_expired_cache_new(size, SIGAR_FIELD_NOTIMPL, SIGAR_FIELD_NOTIMPL);
}
/*#ifdef DEBUG_CACHE*/
/* see how well entries are distributed */
void sigar_cache_dump(sigar_cache_t *table)
{
int i;
sigar_cache_entry_t **entries = table->entries;
printf("table size %lu\n", (long)table->size);
printf("table count %lu\n", (long)table->count);
for (i=0; i<table->size; i++) {
sigar_cache_entry_t *entry = *entries++;
printf("|");
while (entry) {
printf("%lld", entry->id);
if (entry->next) {
printf(",");
}
entry = entry->next;
}
}
printf("\n");
fflush(stdout);
}
/*#endif*/
static void sigar_cache_rehash(sigar_cache_t *table)
{
int i;
unsigned int new_size = table->count * 2 + 1;
sigar_cache_entry_t **entries = table->entries;
sigar_cache_entry_t **new_entries =
malloc(ENTRIES_SIZE(new_size));
memset(new_entries, '\0', ENTRIES_SIZE(new_size));
for (i=0; i<table->size; i++) {
sigar_cache_entry_t *entry = *entries++;
while (entry) {
sigar_cache_entry_t *next = entry->next;
sigar_uint64_t hash = entry->id % new_size;
entry->next = new_entries[hash];
new_entries[hash] = entry;
entry = next;
}
}
free(table->entries);
table->entries = new_entries;
table->size = new_size;
}
#define SIGAR_CACHE_IX(t, k) \
t->entries + (k % t->size)
void sigar_perform_cleanup_if_necessary(sigar_cache_t *table) {
sigar_uint64_t current_time;
int i;
sigar_cache_entry_t **entries;
if (table->cleanup_period_millis == SIGAR_FIELD_NOTIMPL) {
/* no cleanup for this cache) */
return;
}
current_time = sigar_time_now_millis();
if ((current_time - table->last_cleanup_time) < table->cleanup_period_millis) {
/* not enough time has passed since last cleanup */
return;
}
/* performing cleanup */
entries = table->entries;
table->last_cleanup_time = current_time;
for (i=0; i<table->size; i++) {
sigar_cache_entry_t *entry, *ptr, *entry_prev=NULL, **entry_in_table;
entry_in_table = entries;
entry = *entries++;
while (entry) {
sigar_uint64_t period_with_no_access = current_time - entry->last_access_time;
ptr = entry->next;
if (table->entry_expire_period < period_with_no_access) {
/* no one acess this entry for too long - we can delete it */
if (entry->value) {
table->free_value(entry->value);
}
free(entry);
table->count--;
if (entry_prev != NULL) {
entry_prev->next = ptr;
}
else {
/* removing first entry - head of list should point to next entry */
*entry_in_table = ptr;
}
}
else {
/* entry not expired - advance entry_prev to current entry*/
entry_prev = entry;
}
entry = ptr;
}
}
if (table->count < (table->size/4)) {
/* hash table (the array size) too big for the amount of values it contains perform rehash */
sigar_cache_rehash(table);
}
}
sigar_cache_entry_t *sigar_cache_find(sigar_cache_t *table,
sigar_uint64_t key)
{
sigar_cache_entry_t *entry, **ptr;
sigar_perform_cleanup_if_necessary(table);
for (ptr = SIGAR_CACHE_IX(table, key), entry = *ptr;
entry;
ptr = &entry->next, entry = *ptr)
{
if (entry->id == key) {
entry->last_access_time = sigar_time_now_millis();
return entry;
}
}
return NULL;
}
/* create entry if it does not exist */
sigar_cache_entry_t *sigar_cache_get(sigar_cache_t *table,
sigar_uint64_t key)
{
sigar_cache_entry_t *entry, **ptr;
sigar_perform_cleanup_if_necessary(table);
for (ptr = SIGAR_CACHE_IX(table, key), entry = *ptr;
entry;
ptr = &entry->next, entry = *ptr)
{
if (entry->id == key) {
entry->last_access_time = sigar_time_now_millis();
return entry;
}
}
if (++table->count > table->size) {
sigar_cache_rehash(table);
for (ptr = SIGAR_CACHE_IX(table, key), entry = *ptr;
entry;
ptr = &entry->next, entry = *ptr)
{
}
}
*ptr = entry = malloc(sizeof(*entry));
entry->id = key;
entry->value = NULL;
entry->next = NULL;
entry->last_access_time = sigar_time_now_millis();
return entry;
}
void sigar_cache_destroy(sigar_cache_t *table)
{
int i;
sigar_cache_entry_t **entries = table->entries;
#ifdef DEBUG_CACHE
sigar_cache_dump(table);
#endif
for (i=0; i<table->size; i++) {
sigar_cache_entry_t *entry, *ptr;
entry = *entries++;
while (entry) {
if (entry->value) {
table->free_value(entry->value);
}
ptr = entry->next;
free(entry);
entry = ptr;
}
}
free(table->entries);
free(table);
}

View File

@ -0,0 +1,815 @@
/*
* Copyright (c) 2004-2005, 2007-2008 Hyperic, Inc.
* Copyright (c) 2009 SpringSource, Inc.
* Copyright (c) 2010 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
#ifndef WIN32
# ifdef _AIX
# define _LARGE_FILES
# else
# define _FILE_OFFSET_BITS 64
# define _LARGEFILE64_SOURCE
# endif
#endif
#include "sigar.h"
#ifndef WIN32
#if defined(__FreeBSD__) || defined(__OpenBSD__)
# include <sys/param.h>
# include <sys/mount.h>
#else
# include <sys/statvfs.h>
# define HAVE_STATVFS
#endif
#include <errno.h>
#define SIGAR_FS_BLOCKS_TO_BYTES(val, bsize) ((val * bsize) >> 1)
int sigar_statvfs(sigar_t *sigar,
const char *dirname,
sigar_file_system_usage_t *fsusage)
{
sigar_uint64_t val, bsize;
#ifdef HAVE_STATVFS
struct statvfs buf;
int status =
# if defined(__sun) && !defined(_LP64)
/* http://bugs.opensolaris.org/view_bug.do?bug_id=4462986 */
statvfs(dirname, (void *)&buf);
# else
statvfs(dirname, &buf);
# endif
#else
struct statfs buf;
int status = statfs(dirname, &buf);
#endif
if (status != 0) {
return errno;
}
#ifdef HAVE_STATVFS
bsize = buf.f_frsize / 512;
#else
bsize = buf.f_bsize / 512;
#endif
val = buf.f_blocks;
fsusage->total = SIGAR_FS_BLOCKS_TO_BYTES(val, bsize);
val = buf.f_bfree;
fsusage->free = SIGAR_FS_BLOCKS_TO_BYTES(val, bsize);
val = buf.f_bavail;
fsusage->avail = SIGAR_FS_BLOCKS_TO_BYTES(val, bsize);
fsusage->used = fsusage->total - fsusage->free;
fsusage->files = buf.f_files;
fsusage->free_files = buf.f_ffree;
return SIGAR_OK;
}
#endif
/*
* whittled down version of apr/file_info/{unix,win32}/filestat.c
* to fillin sigar_fileattrs_t
*/
#include "sigar_fileinfo.h"
#include "sigar_log.h"
#ifndef SIGAR_ZERO
#define SIGAR_ZERO(s) \
memset(s, '\0', sizeof(*(s)))
#endif
#ifdef WIN32
#include <windows.h>
sigar_uint64_t sigar_FileTimeToTime(FILETIME *ft);
#else
#include <string.h>
#endif
static const char* types[] = {
"none",
"regular",
"directory",
"character device",
"block device",
"pipe",
"symbolic link",
"socket",
"unknown"
};
SIGAR_DECLARE(const char *)
sigar_file_attrs_type_string_get(sigar_file_type_e type)
{
if ((type < SIGAR_FILETYPE_NOFILE) ||
(type > SIGAR_FILETYPE_UNKFILE))
{
type = SIGAR_FILETYPE_UNKFILE;
}
return types[type];
}
static const sigar_uint64_t perm_modes[] = {
SIGAR_UREAD, SIGAR_UWRITE, SIGAR_UEXECUTE,
SIGAR_GREAD, SIGAR_GWRITE, SIGAR_GEXECUTE,
SIGAR_WREAD, SIGAR_WWRITE, SIGAR_WEXECUTE
};
static const char perm_chars[] = "rwx";
SIGAR_DECLARE(char *)
sigar_file_attrs_permissions_string_get(sigar_uint64_t permissions,
char *str)
{
char *ptr = str;
int i=0, j=0;
for (i=0; i<9; i+=3) {
for (j=0; j<3; j++) {
if (permissions & perm_modes[i+j]) {
*ptr = perm_chars[j];
}
else {
*ptr = '-';
}
ptr++;
}
}
*ptr = '\0';
return str;
}
static const int perm_int[] = {
400, 200, 100,
40, 20, 10,
4, 2, 1
};
SIGAR_DECLARE(int)sigar_file_attrs_mode_get(sigar_uint64_t permissions)
{
int i=0;
int perms = 0;
/* no doubt there is some fancy bitshifting
* to convert, but this works fine.
*/
for (i=0; i<9; i++) {
if (permissions & perm_modes[i]) {
perms += perm_int[i];
}
}
return perms;
}
#define IS_DOTDIR(dir) \
((dir[0] == '.') && (!dir[1] || ((dir[1] == '.') && !dir[2])))
#define DIR_STAT_WARN() \
sigar_log_printf(sigar, SIGAR_LOG_WARN, \
"dir_stat: cannot stat `%s': %s", \
name, \
sigar_strerror(sigar, status))
#if defined(NETWARE)
int sigar_dir_stat_get(sigar_t *sigar,
const char *dir,
sigar_dir_stat_t *dirstats)
{
return SIGAR_ENOTIMPL;
}
int sigar_file_attrs_get(sigar_t *sigar,
const char *file,
sigar_file_attrs_t *fileattrs)
{
return SIGAR_ENOTIMPL;
}
int sigar_link_attrs_get(sigar_t *sigar,
const char *file,
sigar_file_attrs_t *fileattrs)
{
return SIGAR_ENOTIMPL;
}
#elif defined(WIN32)
#include <accctrl.h>
#include <aclapi.h>
static void fillin_fileattrs(sigar_file_attrs_t *finfo,
WIN32_FILE_ATTRIBUTE_DATA *wininfo,
int linkinfo)
{
DWORD *sizes = &wininfo->nFileSizeHigh;
finfo->atime = sigar_FileTimeToTime(&wininfo->ftLastAccessTime) / 1000;
finfo->ctime = sigar_FileTimeToTime(&wininfo->ftCreationTime) / 1000;
finfo->mtime = sigar_FileTimeToTime(&wininfo->ftLastWriteTime) / 1000;
finfo->size =
(sigar_uint64_t)sizes[1] | ((sigar_uint64_t)sizes[0] << 32);
if (linkinfo &&
(wininfo->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)) {
finfo->type = SIGAR_FILETYPE_LNK;
}
else if (wininfo->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
finfo->type = SIGAR_FILETYPE_DIR;
}
else {
finfo->type = SIGAR_FILETYPE_REG;
}
}
static sigar_uint64_t convert_perms(ACCESS_MASK acc, sigar_uint64_t scope)
{
sigar_uint64_t perms = 0;
if (acc & FILE_EXECUTE) {
perms |= SIGAR_WEXECUTE;
}
if (acc & FILE_WRITE_DATA) {
perms |= SIGAR_WWRITE;
}
if (acc & FILE_READ_DATA) {
perms |= SIGAR_WREAD;
}
return (perms << scope);
}
static int get_security_info(sigar_t *sigar,
const char *file,
sigar_file_attrs_t *fileattrs)
{
DWORD retval;
PSID user = NULL, group = NULL, world = NULL;
PACL dacl = NULL;
PSECURITY_DESCRIPTOR pdesc = NULL;
SECURITY_INFORMATION sinfo =
OWNER_SECURITY_INFORMATION |
GROUP_SECURITY_INFORMATION |
DACL_SECURITY_INFORMATION;
TRUSTEE ident = {NULL, NO_MULTIPLE_TRUSTEE, TRUSTEE_IS_SID};
ACCESS_MASK acc;
SID_IDENTIFIER_AUTHORITY auth = SECURITY_WORLD_SID_AUTHORITY;
retval = GetNamedSecurityInfo((char *)file,
SE_FILE_OBJECT,
sinfo,
&user,
&group,
&dacl,
NULL,
&pdesc);
if (retval != ERROR_SUCCESS) {
return retval;
}
if (!AllocateAndInitializeSid(&auth, 1, SECURITY_WORLD_RID,
0, 0, 0, 0, 0, 0, 0, &world))
{
world = NULL;
}
ident.TrusteeType = TRUSTEE_IS_USER;
ident.ptstrName = user;
if (GetEffectiveRightsFromAcl(dacl, &ident, &acc) == ERROR_SUCCESS) {
fileattrs->permissions |= convert_perms(acc, 8);
}
ident.TrusteeType = TRUSTEE_IS_GROUP;
ident.ptstrName = group;
if (GetEffectiveRightsFromAcl(dacl, &ident, &acc) == ERROR_SUCCESS) {
fileattrs->permissions |= convert_perms(acc, 4);
}
if (world) {
ident.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
ident.ptstrName = world;
if (GetEffectiveRightsFromAcl(dacl, &ident, &acc) == ERROR_SUCCESS) {
fileattrs->permissions |= convert_perms(acc, 0);
}
}
if (world) {
FreeSid(world);
}
LocalFree(pdesc);
return SIGAR_OK;
}
static int fileattrs_get(sigar_t *sigar,
const char *file,
sigar_file_attrs_t *fileattrs,
int linkinfo)
{
BY_HANDLE_FILE_INFORMATION info;
WIN32_FILE_ATTRIBUTE_DATA attrs;
HANDLE handle;
DWORD flags;
SIGAR_ZERO(fileattrs);
if (!GetFileAttributesExA(file,
GetFileExInfoStandard,
&attrs))
{
return GetLastError();
}
fillin_fileattrs(fileattrs, &attrs, linkinfo);
flags = fileattrs->type == SIGAR_FILETYPE_DIR ?
FILE_FLAG_BACKUP_SEMANTICS :
FILE_ATTRIBUTE_NORMAL;
/**
* We need to set dwDesiredAccess to 0 to work in cases where GENERIC_READ can fail.
*
* see: http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx
*/
handle = CreateFile(file,
0,
0,
NULL,
OPEN_EXISTING,
flags,
NULL);
if (handle != INVALID_HANDLE_VALUE) {
if (GetFileInformationByHandle(handle, &info)) {
fileattrs->inode =
info.nFileIndexLow |
(info.nFileIndexHigh << 32);
fileattrs->device = info.dwVolumeSerialNumber;
fileattrs->nlink = info.nNumberOfLinks;
}
CloseHandle(handle);
}
get_security_info(sigar, file, fileattrs);
return SIGAR_OK;
}
SIGAR_DECLARE(int) sigar_file_attrs_get(sigar_t *sigar,
const char *file,
sigar_file_attrs_t *fileattrs)
{
return fileattrs_get(sigar, file, fileattrs, 0);
}
SIGAR_DECLARE(int) sigar_link_attrs_get(sigar_t *sigar,
const char *file,
sigar_file_attrs_t *fileattrs)
{
return fileattrs_get(sigar, file, fileattrs, 1);
}
static __inline int file_type(char *file)
{
WIN32_FILE_ATTRIBUTE_DATA attrs;
if (!GetFileAttributesExA(file,
GetFileExInfoStandard,
&attrs))
{
return -1;
}
if (attrs.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
return SIGAR_FILETYPE_LNK;
}
else if (attrs.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
return SIGAR_FILETYPE_DIR;
}
else {
return SIGAR_FILETYPE_REG;
}
}
static int dir_stat_get(sigar_t *sigar,
const char *dir,
sigar_dir_stat_t *dirstats,
int recurse)
{
int status;
char name[SIGAR_PATH_MAX+1];
int len = strlen(dir);
int max = sizeof(name)-len-1;
char *ptr = name;
WIN32_FIND_DATA data;
HANDLE handle;
DWORD error;
char delim;
if (file_type((char *)dir) != SIGAR_FILETYPE_DIR) {
return ERROR_NO_MORE_FILES;
}
strncpy(name, dir, sizeof(name));
ptr += len;
if (strchr(dir, '/')) {
delim = '/';
}
else {
delim = '\\';
}
if (name[len] != delim) {
*ptr++ = delim;
len++;
max--;
}
/* e.g. "C:\sigar\*" */
name[len] = '*';
name[len+1] = '\0';
handle = FindFirstFile(name, &data);
if (handle == INVALID_HANDLE_VALUE) {
return GetLastError();
}
do {
/* skip '.' and '..' */
if (IS_DOTDIR(data.cFileName)) {
continue;
}
dirstats->disk_usage +=
(data.nFileSizeHigh * (MAXDWORD+1)) +
data.nFileSizeLow;
/* e.g. "C:\sigar\lib" */
strncpy(ptr, data.cFileName, max);
ptr[max] = '\0';
switch (file_type(name)) {
case -1:
break;
case SIGAR_FILETYPE_REG:
++dirstats->files;
break;
case SIGAR_FILETYPE_DIR:
++dirstats->subdirs;
if (recurse) {
status =
dir_stat_get(sigar, name,
dirstats, recurse);
if (status != SIGAR_OK) {
DIR_STAT_WARN();
}
}
break;
case SIGAR_FILETYPE_LNK:
++dirstats->symlinks;
break;
case SIGAR_FILETYPE_CHR:
++dirstats->chrdevs;
break;
case SIGAR_FILETYPE_BLK:
++dirstats->blkdevs;
break;
case SIGAR_FILETYPE_SOCK:
++dirstats->sockets;
break;
default:
++dirstats->total;
}
} while (FindNextFile(handle, &data));
error = GetLastError();
FindClose(handle);
if (error != ERROR_NO_MORE_FILES) {
return error;
}
dirstats->total =
dirstats->files +
dirstats->subdirs +
dirstats->symlinks +
dirstats->chrdevs +
dirstats->blkdevs +
dirstats->sockets;
return SIGAR_OK;
}
#else
#include <dirent.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
static sigar_file_type_e filetype_from_mode(mode_t mode)
{
sigar_file_type_e type;
switch (mode & S_IFMT) {
case S_IFREG:
type = SIGAR_FILETYPE_REG; break;
case S_IFDIR:
type = SIGAR_FILETYPE_DIR; break;
case S_IFLNK:
type = SIGAR_FILETYPE_LNK; break;
case S_IFCHR:
type = SIGAR_FILETYPE_CHR; break;
case S_IFBLK:
type = SIGAR_FILETYPE_BLK; break;
#if defined(S_IFFIFO)
case S_IFFIFO:
type = SIGAR_FILETYPE_PIPE; break;
#endif
#if !defined(BEOS) && defined(S_IFSOCK)
case S_IFSOCK:
type = SIGAR_FILETYPE_SOCK; break;
#endif
default:
/* Work around missing S_IFxxx values above
* for Linux et al.
*/
#if !defined(S_IFFIFO) && defined(S_ISFIFO)
if (S_ISFIFO(mode)) {
type = SIGAR_FILETYPE_PIPE;
} else
#endif
#if !defined(BEOS) && !defined(S_IFSOCK) && defined(S_ISSOCK)
if (S_ISSOCK(mode)) {
type = SIGAR_FILETYPE_SOCK;
} else
#endif
type = SIGAR_FILETYPE_UNKFILE;
}
return type;
}
static sigar_uint64_t sigar_unix_mode2perms(mode_t mode)
{
sigar_uint64_t perms = 0;
if (mode & S_IRUSR)
perms |= SIGAR_UREAD;
if (mode & S_IWUSR)
perms |= SIGAR_UWRITE;
if (mode & S_IXUSR)
perms |= SIGAR_UEXECUTE;
if (mode & S_IRGRP)
perms |= SIGAR_GREAD;
if (mode & S_IWGRP)
perms |= SIGAR_GWRITE;
if (mode & S_IXGRP)
perms |= SIGAR_GEXECUTE;
if (mode & S_IROTH)
perms |= SIGAR_WREAD;
if (mode & S_IWOTH)
perms |= SIGAR_WWRITE;
if (mode & S_IXOTH)
perms |= SIGAR_WEXECUTE;
return perms;
}
static void copy_stat_info(sigar_file_attrs_t *fileattrs,
struct stat *info)
{
fileattrs->permissions = sigar_unix_mode2perms(info->st_mode);
fileattrs->type = filetype_from_mode(info->st_mode);
fileattrs->uid = info->st_uid;
fileattrs->gid = info->st_gid;
fileattrs->size = info->st_size;
fileattrs->inode = info->st_ino;
fileattrs->device = info->st_dev;
fileattrs->nlink = info->st_nlink;
fileattrs->atime = info->st_atime;
fileattrs->mtime = info->st_mtime;
fileattrs->ctime = info->st_ctime;
fileattrs->atime *= 1000;
fileattrs->mtime *= 1000;
fileattrs->ctime *= 1000;
}
int sigar_file_attrs_get(sigar_t *sigar,
const char *file,
sigar_file_attrs_t *fileattrs)
{
struct stat info;
if (stat(file, &info) == 0) {
copy_stat_info(fileattrs, &info);
return SIGAR_OK;
}
else {
return errno;
}
}
int sigar_link_attrs_get(sigar_t *sigar,
const char *file,
sigar_file_attrs_t *fileattrs)
{
struct stat info;
if (lstat(file, &info) == 0) {
copy_stat_info(fileattrs, &info);
return SIGAR_OK;
}
else {
return errno;
}
}
static int dir_stat_get(sigar_t *sigar,
const char *dir,
sigar_dir_stat_t *dirstats,
int recurse)
{
int status;
char name[SIGAR_PATH_MAX+1];
int len = strlen(dir);
int max = sizeof(name)-len-1;
char *ptr = name;
DIR *dirp = opendir(dir);
struct dirent *ent;
struct stat info;
#ifdef HAVE_READDIR_R
struct dirent dbuf;
#endif
if (!dirp) {
return errno;
}
strncpy(name, dir, sizeof(name));
ptr += len;
if (name[len] != '/') {
*ptr++ = '/';
len++;
max--;
}
#ifdef HAVE_READDIR_R
while (readdir_r(dirp, &dbuf, &ent) == 0) {
if (ent == NULL) {
break;
}
#else
while ((ent = readdir(dirp))) {
#endif
/* skip '.' and '..' */
if (IS_DOTDIR(ent->d_name)) {
continue;
}
strncpy(ptr, ent->d_name, max);
ptr[max] = '\0';
if (lstat(name, &info) != 0) {
continue;
}
dirstats->disk_usage += info.st_size;
switch (filetype_from_mode(info.st_mode)) {
case SIGAR_FILETYPE_REG:
++dirstats->files;
break;
case SIGAR_FILETYPE_DIR:
++dirstats->subdirs;
if (recurse) {
status =
dir_stat_get(sigar, name,
dirstats, recurse);
if (status != SIGAR_OK) {
DIR_STAT_WARN();
}
}
break;
case SIGAR_FILETYPE_LNK:
++dirstats->symlinks;
break;
case SIGAR_FILETYPE_CHR:
++dirstats->chrdevs;
break;
case SIGAR_FILETYPE_BLK:
++dirstats->blkdevs;
break;
case SIGAR_FILETYPE_SOCK:
++dirstats->sockets;
break;
default:
++dirstats->total;
}
}
dirstats->total =
dirstats->files +
dirstats->subdirs +
dirstats->symlinks +
dirstats->chrdevs +
dirstats->blkdevs +
dirstats->sockets;
closedir(dirp);
return SIGAR_OK;
}
#endif
SIGAR_DECLARE(int) sigar_dir_stat_get(sigar_t *sigar,
const char *dir,
sigar_dir_stat_t *dirstats)
{
SIGAR_ZERO(dirstats);
return dir_stat_get(sigar, dir, dirstats, 0);
}
SIGAR_DECLARE(int) sigar_dir_usage_get(sigar_t *sigar,
const char *dir,
sigar_dir_usage_t *dirusage)
{
SIGAR_ZERO(dirusage);
return dir_stat_get(sigar, dir, dirusage, 1);
}

View File

@ -0,0 +1,696 @@
/*
* Copyright (c) 2007-2008 Hyperic, Inc.
* Copyright (c) 2009 SpringSource, Inc.
* Copyright (c) 2010 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* Utility functions to provide string formatting of SIGAR data */
#include "sigar.h"
#include "sigar_private.h"
#include "sigar_util.h"
#include "sigar_os.h"
#include "sigar_format.h"
#include <errno.h>
#include <stdio.h>
#ifndef WIN32
#include <netinet/in.h>
#include <arpa/inet.h>
#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(_AIX)
#include <sys/socket.h>
#endif
#include <pwd.h>
#include <grp.h>
/* sysconf(_SC_GET{PW,GR}_R_SIZE_MAX) */
#define R_SIZE_MAX 2048
int sigar_user_name_get(sigar_t *sigar, int uid, char *buf, int buflen)
{
struct passwd *pw = NULL;
/* XXX cache lookup */
# ifdef HAVE_GETPWUID_R
struct passwd pwbuf;
char buffer[R_SIZE_MAX];
if (getpwuid_r(uid, &pwbuf, buffer, sizeof(buffer), &pw) != 0) {
return errno;
}
if (!pw) {
return ENOENT;
}
# else
if ((pw = getpwuid(uid)) == NULL) {
return errno;
}
# endif
strncpy(buf, pw->pw_name, buflen);
buf[buflen-1] = '\0';
return SIGAR_OK;
}
int sigar_group_name_get(sigar_t *sigar, int gid, char *buf, int buflen)
{
struct group *gr;
/* XXX cache lookup */
# ifdef HAVE_GETGRGID_R
struct group grbuf;
char buffer[R_SIZE_MAX];
if (getgrgid_r(gid, &grbuf, buffer, sizeof(buffer), &gr) != 0) {
return errno;
}
# else
if ((gr = getgrgid(gid)) == NULL) {
return errno;
}
# endif
if (gr && gr->gr_name) {
strncpy(buf, gr->gr_name, buflen);
}
else {
/* seen on linux.. apache httpd.conf has:
* Group #-1
* results in uid == -1 and gr == NULL.
* wtf getgrgid_r doesnt fail instead?
*/
sprintf(buf, "%d", gid);
}
buf[buflen-1] = '\0';
return SIGAR_OK;
}
int sigar_user_id_get(sigar_t *sigar, const char *name, int *uid)
{
/* XXX cache lookup */
struct passwd *pw;
# ifdef HAVE_GETPWNAM_R
struct passwd pwbuf;
char buf[R_SIZE_MAX];
if (getpwnam_r(name, &pwbuf, buf, sizeof(buf), &pw) != 0) {
return errno;
}
# else
if (!(pw = getpwnam(name))) {
return errno;
}
# endif
*uid = (int)pw->pw_uid;
return SIGAR_OK;
}
#endif /* WIN32 */
static char *sigar_error_string(int err)
{
switch (err) {
case SIGAR_ENOTIMPL:
return "This function has not been implemented on this platform";
default:
return "Error string not specified yet";
}
}
SIGAR_DECLARE(char *) sigar_strerror(sigar_t *sigar, int err)
{
char *buf;
if (err < 0) {
return sigar->errbuf;
}
if (err > SIGAR_OS_START_ERROR) {
if ((buf = sigar_os_error_string(sigar, err)) != NULL) {
return buf;
}
return "Unknown OS Error"; /* should never happen */
}
if (err > SIGAR_START_ERROR) {
return sigar_error_string(err);
}
return sigar_strerror_get(err, sigar->errbuf, sizeof(sigar->errbuf));
}
char *sigar_strerror_get(int err, char *errbuf, int buflen)
{
char *buf = NULL;
#ifdef WIN32
DWORD len;
len = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
err,
MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), /* force english */
(LPTSTR)errbuf,
(DWORD)buflen,
NULL);
#else
#if defined(HAVE_STRERROR_R) && defined(HAVE_STRERROR_R_GLIBC)
/*
* strerror_r man page says:
* "The GNU version may, but need not, use the user supplied buffer"
*/
buf = strerror_r(err, errbuf, buflen);
#elif defined(HAVE_STRERROR_R)
if (strerror_r(err, errbuf, buflen) < 0) {
buf = "Unknown Error";
}
#else
/* strerror() is thread safe on solaris and hpux */
buf = strerror(err);
#endif
if (buf != NULL) {
SIGAR_STRNCPY(errbuf, buf, buflen);
}
#endif
return errbuf;
}
void sigar_strerror_set(sigar_t *sigar, char *msg)
{
SIGAR_SSTRCPY(sigar->errbuf, msg);
}
#ifdef WIN32
#define vsnprintf _vsnprintf
#endif
void sigar_strerror_printf(sigar_t *sigar, const char *format, ...)
{
va_list args;
va_start(args, format);
vsnprintf(sigar->errbuf, sizeof(sigar->errbuf), format, args);
va_end(args);
}
/* copy apr_strfsize */
SIGAR_DECLARE(char *) sigar_format_size(sigar_uint64_t size, char *buf)
{
const char ord[] = "KMGTPE";
const char *o = ord;
int remain;
if (size == SIGAR_FIELD_NOTIMPL) {
buf[0] = '-';
buf[1] = '\0';
return buf;
}
if (size < 973) {
sprintf(buf, "%3d ", (int) size);
return buf;
}
do {
remain = (int)(size & 1023);
size >>= 10;
if (size >= 973) {
++o;
continue;
}
if (size < 9 || (size == 9 && remain < 973)) {
if ((remain = ((remain * 5) + 256) / 512) >= 10) {
++size;
remain = 0;
}
sprintf(buf, "%d.%d%c", (int) size, remain, *o);
return buf;
}
if (remain >= 512) {
++size;
}
sprintf(buf, "%3d%c", (int) size, *o);
return buf;
} while (1);
}
SIGAR_DECLARE(int) sigar_uptime_string(sigar_t *sigar,
sigar_uptime_t *uptime,
char *buffer,
int buflen)
{
char *ptr = buffer;
int time = (int)uptime->uptime;
int minutes, hours, days, offset = 0;
/* XXX: get rid of sprintf and/or check for overflow */
days = time / (60*60*24);
if (days) {
offset += sprintf(ptr + offset, "%d day%s, ",
days, (days > 1) ? "s" : "");
}
minutes = time / 60;
hours = minutes / 60;
hours = hours % 24;
minutes = minutes % 60;
if (hours) {
offset += sprintf(ptr + offset, "%2d:%02d",
hours, minutes);
}
else {
offset += sprintf(ptr + offset, "%d min", minutes);
}
return SIGAR_OK;
}
/* threadsafe alternative to inet_ntoa (inet_ntop4 from apr) */
int sigar_inet_ntoa(sigar_t *sigar,
sigar_uint32_t address,
char *addr_str)
{
char *next=addr_str;
int n=0;
const unsigned char *src =
(const unsigned char *)&address;
do {
unsigned char u = *src++;
if (u > 99) {
*next++ = '0' + u/100;
u %= 100;
*next++ = '0' + u/10;
u %= 10;
}
else if (u > 9) {
*next++ = '0' + u/10;
u %= 10;
}
*next++ = '0' + u;
*next++ = '.';
n++;
} while (n < 4);
*--next = 0;
return SIGAR_OK;
}
static int sigar_ether_ntoa(char *buff, unsigned char *ptr)
{
sprintf(buff, "%02X:%02X:%02X:%02X:%02X:%02X",
(ptr[0] & 0xff), (ptr[1] & 0xff), (ptr[2] & 0xff),
(ptr[3] & 0xff), (ptr[4] & 0xff), (ptr[5] & 0xff));
return SIGAR_OK;
}
SIGAR_DECLARE(int) sigar_net_address_equals(sigar_net_address_t *addr1,
sigar_net_address_t *addr2)
{
if (addr1->family != addr2->family) {
return EINVAL;
}
switch (addr1->family) {
case SIGAR_AF_INET:
return memcmp(&addr1->addr.in, &addr2->addr.in, sizeof(addr1->addr.in));
case SIGAR_AF_INET6:
return memcmp(&addr1->addr.in6, &addr2->addr.in6, sizeof(addr1->addr.in6));
case SIGAR_AF_LINK:
return memcmp(&addr1->addr.mac, &addr2->addr.mac, sizeof(addr1->addr.mac));
default:
return EINVAL;
}
}
#if defined(SIGAR_USING_MSC6)
#define sigar_inet_ntop(af, src, dst, size) NULL
#define sigar_inet_ntop_errno SIGAR_ENOTIMPL
#elif defined(WIN32)
static char *sigar_inet_ntop(int af, const void *src, char *dst, int cnt)
{
struct sockaddr_in6 sa; /* note only using this for AF_INET6 */
memset(&sa, '\0', sizeof(sa));
sa.sin6_family = af;
memcpy(&sa.sin6_addr, src, sizeof(sa.sin6_addr));
if (getnameinfo((struct sockaddr *)&sa, sizeof(sa),
dst, cnt, NULL, 0, NI_NUMERICHOST))
{
return NULL;
}
else {
return dst;
}
}
#define sigar_inet_ntop_errno GetLastError()
#else
#define sigar_inet_ntop inet_ntop
#define sigar_inet_ntop_errno errno
#endif
SIGAR_DECLARE(int) sigar_net_address_to_string(sigar_t *sigar,
sigar_net_address_t *address,
char *addr_str)
{
*addr_str = '\0';
switch (address->family) {
case SIGAR_AF_INET6:
if (sigar_inet_ntop(AF_INET6, (const void *)&address->addr.in6,
addr_str, SIGAR_INET6_ADDRSTRLEN))
{
return SIGAR_OK;
}
else {
return sigar_inet_ntop_errno;
}
case SIGAR_AF_INET:
return sigar_inet_ntoa(sigar, address->addr.in, addr_str);
case SIGAR_AF_UNSPEC:
return sigar_inet_ntoa(sigar, 0, addr_str); /*XXX*/
case SIGAR_AF_LINK:
return sigar_ether_ntoa(addr_str, &address->addr.mac[0]);
default:
return EINVAL;
}
}
SIGAR_DECLARE(const char *)sigar_net_scope_to_string(int type)
{
switch (type) {
case SIGAR_IPV6_ADDR_ANY:
return "Global";
case SIGAR_IPV6_ADDR_LOOPBACK:
return "Host";
case SIGAR_IPV6_ADDR_LINKLOCAL:
return "Link";
case SIGAR_IPV6_ADDR_SITELOCAL:
return "Site";
case SIGAR_IPV6_ADDR_COMPATv4:
return "Compat";
default:
return "Unknown";
}
}
SIGAR_DECLARE(sigar_uint32_t) sigar_net_address_hash(sigar_net_address_t *address)
{
sigar_uint32_t hash = 0;
unsigned char *data;
int i=0, size, elts;
switch (address->family) {
case SIGAR_AF_UNSPEC:
case SIGAR_AF_INET:
return address->addr.in;
case SIGAR_AF_INET6:
data = (unsigned char *)&address->addr.in6;
size = sizeof(address->addr.in6);
elts = 4;
break;
case SIGAR_AF_LINK:
data = (unsigned char *)&address->addr.mac;
size = sizeof(address->addr.mac);
elts = 2;
break;
default:
return -1;
}
while (i<size) {
int j=0;
int component=0;
while (j<elts && i<size) {
component = (component << 8) + data[i];
j++;
i++;
}
hash += component;
}
return hash;
}
SIGAR_DECLARE(const char *)sigar_net_connection_type_get(int type)
{
switch (type) {
case SIGAR_NETCONN_TCP:
return "tcp";
case SIGAR_NETCONN_UDP:
return "udp";
case SIGAR_NETCONN_RAW:
return "raw";
case SIGAR_NETCONN_UNIX:
return "unix";
default:
return "unknown";
}
}
SIGAR_DECLARE(const char *)sigar_net_connection_state_get(int state)
{
switch (state) {
case SIGAR_TCP_ESTABLISHED:
return "ESTABLISHED";
case SIGAR_TCP_SYN_SENT:
return "SYN_SENT";
case SIGAR_TCP_SYN_RECV:
return "SYN_RECV";
case SIGAR_TCP_FIN_WAIT1:
return "FIN_WAIT1";
case SIGAR_TCP_FIN_WAIT2:
return "FIN_WAIT2";
case SIGAR_TCP_TIME_WAIT:
return "TIME_WAIT";
case SIGAR_TCP_CLOSE:
return "CLOSE";
case SIGAR_TCP_CLOSE_WAIT:
return "CLOSE_WAIT";
case SIGAR_TCP_LAST_ACK:
return "LAST_ACK";
case SIGAR_TCP_LISTEN:
return "LISTEN";
case SIGAR_TCP_CLOSING:
return "CLOSING";
case SIGAR_TCP_IDLE:
return "IDLE";
case SIGAR_TCP_BOUND:
return "BOUND";
case SIGAR_TCP_UNKNOWN:
default:
return "UNKNOWN";
}
}
SIGAR_DECLARE(char *) sigar_net_interface_flags_to_string(sigar_uint64_t flags, char *buf)
{
*buf = '\0';
if (flags == 0) {
strcat(buf, "[NO FLAGS] ");
}
if (flags & SIGAR_IFF_UP) {
strcat(buf, "UP ");
}
if (flags & SIGAR_IFF_BROADCAST) {
strcat(buf, "BROADCAST ");
}
if (flags & SIGAR_IFF_DEBUG) {
strcat(buf, "DEBUG ");
}
if (flags & SIGAR_IFF_LOOPBACK) {
strcat(buf, "LOOPBACK ");
}
if (flags & SIGAR_IFF_POINTOPOINT) {
strcat(buf, "POINTOPOINT ");
}
if (flags & SIGAR_IFF_NOTRAILERS) {
strcat(buf, "NOTRAILERS ");
}
if (flags & SIGAR_IFF_RUNNING) {
strcat(buf, "RUNNING ");
}
if (flags & SIGAR_IFF_NOARP) {
strcat(buf, "NOARP ");
}
if (flags & SIGAR_IFF_PROMISC) {
strcat(buf, "PROMISC ");
}
if (flags & SIGAR_IFF_ALLMULTI) {
strcat(buf, "ALLMULTI ");
}
if (flags & SIGAR_IFF_MULTICAST) {
strcat(buf, "MULTICAST ");
}
if (flags & SIGAR_IFF_SLAVE) {
strcat(buf, "SLAVE ");
}
if (flags & SIGAR_IFF_MASTER) {
strcat(buf, "MASTER ");
}
if (flags & SIGAR_IFF_DYNAMIC) {
strcat(buf, "DYNAMIC ");
}
return buf;
}
#ifdef WIN32
#define NET_SERVICES_FILE "C:\\windows\\system32\\drivers\\etc\\services"
#else
#define NET_SERVICES_FILE "/etc/services"
#endif
static int net_services_parse(sigar_cache_t *names, char *type)
{
FILE *fp;
char buffer[8192], *ptr;
char *file;
if (!(file = getenv("SIGAR_NET_SERVICES_FILE"))) {
file = NET_SERVICES_FILE;
}
if (!(fp = fopen(file, "r"))) {
return errno;
}
while ((ptr = fgets(buffer, sizeof(buffer), fp))) {
int port;
char name[256], proto[56];
sigar_cache_entry_t *entry;
while (sigar_isspace(*ptr)) {
++ptr;
}
if ((*ptr == '#') || (*ptr == '\0')) {
continue;
}
if (sscanf(ptr, "%s%d/%s", name, &port, proto) != 3) {
continue;
}
if (!strEQ(type, proto)) {
continue;
}
entry = sigar_cache_get(names, port);
if (!entry->value) {
entry->value = strdup(name);
}
}
fclose(fp);
return SIGAR_OK;
}
SIGAR_DECLARE(char *)sigar_net_services_name_get(sigar_t *sigar,
int protocol, unsigned long port)
{
sigar_cache_entry_t *entry;
sigar_cache_t **names;
char *pname;
switch (protocol) {
case SIGAR_NETCONN_TCP:
names = &sigar->net_services_tcp;
pname = "tcp";
break;
case SIGAR_NETCONN_UDP:
names = &sigar->net_services_udp;
pname = "udp";
break;
default:
return NULL;
}
if (*names == NULL) {
*names = sigar_cache_new(1024);
net_services_parse(*names, pname);
}
if ((entry = sigar_cache_find(*names, port))) {
return (char *)entry->value;
}
else {
return NULL;
}
}
SIGAR_DECLARE(int) sigar_cpu_perc_calculate(sigar_cpu_t *prev,
sigar_cpu_t *curr,
sigar_cpu_perc_t *perc)
{
double diff_user, diff_sys, diff_nice, diff_idle;
double diff_wait, diff_irq, diff_soft_irq, diff_stolen;
double diff_total;
diff_user = curr->user - prev->user;
diff_sys = curr->sys - prev->sys;
diff_nice = curr->nice - prev->nice;
diff_idle = curr->idle - prev->idle;
diff_wait = curr->wait - prev->wait;
diff_irq = curr->irq - prev->irq;
diff_soft_irq = curr->soft_irq - prev->soft_irq;
diff_stolen = curr->stolen - prev->stolen;
diff_user = diff_user < 0 ? 0 : diff_user;
diff_sys = diff_sys < 0 ? 0 : diff_sys;
diff_nice = diff_nice < 0 ? 0 : diff_nice;
diff_idle = diff_idle < 0 ? 0 : diff_idle;
diff_wait = diff_wait < 0 ? 0 : diff_wait;
diff_irq = diff_irq < 0 ? 0 : diff_irq;
diff_soft_irq = diff_soft_irq < 0 ? 0 : diff_soft_irq;
diff_stolen = diff_stolen < 0 ? 0 : diff_stolen;
diff_total =
diff_user + diff_sys + diff_nice + diff_idle +
diff_wait + diff_irq + diff_soft_irq +
diff_stolen;
perc->user = diff_user / diff_total;
perc->sys = diff_sys / diff_total;
perc->nice = diff_nice / diff_total;
perc->idle = diff_idle / diff_total;
perc->wait = diff_wait / diff_total;
perc->irq = diff_irq / diff_total;
perc->soft_irq = diff_soft_irq / diff_total;
perc->stolen = diff_stolen / diff_total;
perc->combined =
perc->user + perc->sys + perc->nice + perc->wait;
return SIGAR_OK;
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,216 @@
/*
* Copyright (c) 2007 Hyperic, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "sigar.h"
#include "sigar_private.h"
#include "sigar_util.h"
#ifdef WIN32
#include <windows.h>
#endif
#include <signal.h>
#include <errno.h>
SIGAR_DECLARE(int) sigar_proc_kill(sigar_pid_t pid, int signum)
{
#ifdef WIN32
int status = -1;
HANDLE proc =
OpenProcess(PROCESS_ALL_ACCESS,
TRUE, (DWORD)pid);
if (proc) {
switch (signum) {
case 0:
status = SIGAR_OK;
break;
default:
if (TerminateProcess(proc, signum)) {
status = SIGAR_OK;
}
break;
}
CloseHandle(proc);
if (status == SIGAR_OK) {
return SIGAR_OK;
}
}
return GetLastError();
#else
if (kill(pid, signum) == -1) {
return errno;
}
return SIGAR_OK;
#endif
}
SIGAR_DECLARE(int) sigar_signum_get(char *name)
{
if (strnEQ(name, "SIG", 3)) {
name += 3;
}
switch (*name) {
case 'A':
#ifdef SIGABRT
if (strEQ(name, "ABRT")) return SIGABRT;
#endif
#ifdef SIGALRM
if (strEQ(name, "ALRM")) return SIGALRM;
#endif
break;
case 'B':
#ifdef SIGBUS
if (strEQ(name, "BUS")) return SIGBUS;
#endif
break;
case 'C':
#ifdef SIGCONT
if (strEQ(name, "CONT")) return SIGCONT;
#endif
#ifdef SIGCHLD
if (strEQ(name, "CHLD")) return SIGCHLD;
#endif
#ifdef SIGCLD
if (strEQ(name, "CLD")) return SIGCLD;
#endif
break;
case 'E':
#ifdef SIGEMT
if (strEQ(name, "EMT")) return SIGEMT;
#endif
break;
case 'F':
#ifdef SIGFPE
if (strEQ(name, "FPE")) return SIGFPE;
#endif
break;
case 'H':
#ifdef SIGHUP
if (strEQ(name, "HUP")) return SIGHUP;
#endif
break;
case 'I':
#ifdef SIGINT
if (strEQ(name, "INT")) return SIGINT;
#endif
#ifdef SIGILL
if (strEQ(name, "ILL")) return SIGILL;
#endif
#ifdef SIGIOT
if (strEQ(name, "IOT")) return SIGIOT;
#endif
#ifdef SIGIO
if (strEQ(name, "IO")) return SIGIO;
#endif
#ifdef SIGINFO
if (strEQ(name, "INFO")) return SIGINFO;
#endif
break;
case 'K':
#ifdef SIGKILL
if (strEQ(name, "KILL")) return SIGKILL;
#endif
break;
case 'P':
#ifdef SIGPOLL
if (strEQ(name, "POLL")) return SIGPOLL;
#endif
#ifdef SIGPIPE
if (strEQ(name, "PIPE")) return SIGPIPE;
#endif
#ifdef SIGPROF
if (strEQ(name, "PROF")) return SIGPROF;
#endif
#ifdef SIGPWR
if (strEQ(name, "PWR")) return SIGPWR;
#endif
break;
case 'Q':
#ifdef SIGQUIT
if (strEQ(name, "QUIT")) return SIGQUIT;
#endif
break;
case 'S':
#ifdef SIGSEGV
if (strEQ(name, "SEGV")) return SIGSEGV;
#endif
#ifdef SIGSYS
if (strEQ(name, "SYS")) return SIGSYS;
#endif
#ifdef SIGSTOP
if (strEQ(name, "STOP")) return SIGSTOP;
#endif
#ifdef SIGSTKFLT
if (strEQ(name, "STKFLT")) return SIGSTKFLT;
#endif
break;
case 'T':
#ifdef SIGTRAP
if (strEQ(name, "TRAP")) return SIGTRAP;
#endif
#ifdef SIGTERM
if (strEQ(name, "TERM")) return SIGTERM;
#endif
#ifdef SIGTSTP
if (strEQ(name, "TSTP")) return SIGTSTP;
#endif
#ifdef SIGTTIN
if (strEQ(name, "TTIN")) return SIGTTIN;
#endif
#ifdef SIGTTOU
if (strEQ(name, "TTOU")) return SIGTTOU;
#endif
break;
case 'U':
#ifdef SIGURG
if (strEQ(name, "URG")) return SIGURG;
#endif
#ifdef SIGUSR1
if (strEQ(name, "USR1")) return SIGUSR1;
#endif
#ifdef SIGUSR2
if (strEQ(name, "USR2")) return SIGUSR2;
#endif
break;
case 'V':
#ifdef SIGVTALRM
if (strEQ(name, "VTALRM")) return SIGVTALRM;
#endif
break;
case 'W':
#ifdef SIGWINCH
if (strEQ(name, "WINCH")) return SIGWINCH;
#endif
break;
case 'X':
#ifdef SIGXCPU
if (strEQ(name, "XCPU")) return SIGXCPU;
#endif
#ifdef SIGXFSZ
if (strEQ(name, "XFSZ")) return SIGXFSZ;
#endif
break;
default:
break;
}
return -1;
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,243 @@
/*
* Copyright (c) 2009 SpringSource, Inc.
* Copyright (c) 2009 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define UNICODE
#define _UNICODE
#define _WIN32_DCOM
#include <windows.h>
#include <objbase.h>
#include <comdef.h>
#include <wbemidl.h>
#include "sigar.h"
#pragma comment(lib, "wbemuuid.lib")
#ifndef SIGAR_CMDLINE_MAX
#define SIGAR_CMDLINE_MAX 4096<<2
#endif
class WMI {
public:
WMI();
~WMI();
HRESULT Open(LPCTSTR machine=NULL, LPCTSTR user=NULL, LPCTSTR pass=NULL);
void Close();
HRESULT GetProcStringProperty(DWORD pid, TCHAR *name, TCHAR *value, DWORD len);
HRESULT GetProcExecutablePath(DWORD pid, TCHAR *value);
HRESULT GetProcCommandLine(DWORD pid, TCHAR *value);
int GetLastError();
private:
IWbemServices *wbem;
HRESULT result;
BSTR GetProcQuery(DWORD pid);
};
WMI::WMI()
{
wbem = NULL;
result = S_OK;
CoInitializeEx(NULL, COINIT_MULTITHREADED);
}
WMI::~WMI()
{
Close();
CoUninitialize();
}
/* XXX must be a better way to map HRESULT */
int WMI::GetLastError()
{
switch (result) {
case S_OK:
return ERROR_SUCCESS;
case WBEM_E_NOT_FOUND:
return ERROR_NOT_FOUND;
case WBEM_E_ACCESS_DENIED:
return ERROR_ACCESS_DENIED;
case WBEM_E_NOT_SUPPORTED:
return SIGAR_ENOTIMPL;
default:
return ERROR_INVALID_FUNCTION;
}
}
HRESULT WMI::Open(LPCTSTR machine, LPCTSTR user, LPCTSTR pass)
{
IWbemLocator *locator;
wchar_t path[MAX_PATH];
if (wbem) {
result = S_OK;
return result;
}
result =
CoInitializeSecurity(NULL, //Security Descriptor
-1, //COM authentication
NULL, //Authentication services
NULL, //Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, //Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, //Default Impersonation
NULL, //Authentication info
EOAC_NONE, //Additional capabilities
NULL); //Reserved
result = CoCreateInstance(CLSID_WbemLocator,
NULL, /* IUnknown */
CLSCTX_INPROC_SERVER,
IID_IWbemLocator,
(LPVOID *)&locator);
if (FAILED(result)) {
return result;
}
if (machine == NULL) {
machine = L".";
}
wsprintf(path, L"\\\\%S\\ROOT\\CIMV2", machine);
result = locator->ConnectServer(bstr_t(path), //Object path of WMI namespace
bstr_t(user), //User name. NULL = current user
bstr_t(pass), //User password. NULL = current
NULL, //Locale. NULL indicates current
0, //Security flags
NULL, //Authority (e.g. Kerberos)
NULL, //Context object
&wbem); //pointer to IWbemServices proxy
locator->Release();
return result;
}
void WMI::Close()
{
if (wbem) {
wbem->Release();
wbem = NULL;
result = S_OK;
}
}
BSTR WMI::GetProcQuery(DWORD pid)
{
wchar_t query[56];
wsprintf(query, L"Win32_Process.Handle=%d", pid);
return bstr_t(query);
}
HRESULT WMI::GetProcStringProperty(DWORD pid, TCHAR *name, TCHAR *value, DWORD len)
{
IWbemClassObject *obj;
VARIANT var;
result = wbem->GetObject(GetProcQuery(pid), 0, 0, &obj, 0);
if (FAILED(result)) {
return result;
}
result = obj->Get(name, 0, &var, 0, 0);
if (SUCCEEDED(result)) {
if (var.vt == VT_NULL) {
result = E_INVALIDARG;
}
else {
lstrcpyn(value, var.bstrVal, len);
}
VariantClear(&var);
}
obj->Release();
return result;
}
HRESULT WMI::GetProcExecutablePath(DWORD pid, TCHAR *value)
{
return GetProcStringProperty(pid, L"ExecutablePath", value, MAX_PATH);
}
HRESULT WMI::GetProcCommandLine(DWORD pid, TCHAR *value)
{
return GetProcStringProperty(pid, L"CommandLine", value, SIGAR_CMDLINE_MAX);
}
/* in peb.c */
extern "C" int sigar_parse_proc_args(sigar_t *sigar, WCHAR *buf,
sigar_proc_args_t *procargs);
extern "C" int sigar_proc_args_wmi_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_args_t *procargs)
{
int status;
TCHAR buf[SIGAR_CMDLINE_MAX];
WMI *wmi = new WMI();
if (FAILED(wmi->Open())) {
return wmi->GetLastError();
}
if (FAILED(wmi->GetProcCommandLine(pid, buf))) {
status = wmi->GetLastError();
}
else {
status = sigar_parse_proc_args(sigar, buf, procargs);
}
wmi->Close();
delete wmi;
return status;
}
extern "C" int sigar_proc_exe_wmi_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_exe_t *procexe)
{
int status;
TCHAR buf[MAX_PATH+1];
WMI *wmi = new WMI();
if (FAILED(wmi->Open())) {
return wmi->GetLastError();
}
procexe->name[0] = '\0';
if (FAILED(wmi->GetProcExecutablePath(pid, buf))) {
status = wmi->GetLastError();
}
else {
status = SIGAR_OK;
/* SIGAR_W2A(buf, procexe->name, sizeof(procexe->name)); */
WideCharToMultiByte(CP_ACP, 0, buf, -1,
(LPSTR)procexe->name, sizeof(procexe->name),
NULL, NULL);
}
wmi->Close();
delete wmi;
return status;
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.