go bindings
statically build go with sigar using visual studio 2015 IDE
This commit is contained in:
1 parent
7a6aefc7fb
commit
0f0dbe34ce
88 files changed
+17394
No files matched your search
@@ -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
@@ -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;
|
||||
}
|
||||
@@ -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
@@ -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);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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
@@ -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
@@ -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;
|
||||
}
|
||||
Reference in new issue
Block a user