This source file includes following definitions.
- uv__util_init
- uv_utf16_to_utf8
- uv_utf8_to_utf16
- uv_exepath
- uv_cwd
- uv_chdir
- uv_loadavg
- uv_get_free_memory
- uv_get_total_memory
- uv_parent_pid
- uv_setup_args
- uv_set_process_title
- uv__get_process_title
- uv_get_process_title
- uv_hrtime
- uv_resident_set_memory
- uv_uptime
- uv_cpu_info
- uv_free_cpu_info
- uv_interface_addresses
- uv_free_interface_addresses
#include <assert.h>
#include <direct.h>
#include <limits.h>
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <wchar.h>
#include "uv.h"
#include "internal.h"
#include <winsock2.h>
#include <winperf.h>
#include <iphlpapi.h>
#include <psapi.h>
#include <tlhelp32.h>
#define MAX_TITLE_LENGTH 8192
#undef NANOSEC
#define NANOSEC 1000000000
static char *process_title;
static CRITICAL_SECTION process_title_lock;
static uint64_t hrtime_frequency_ = 0;
void uv__util_init() {
InitializeCriticalSection(&process_title_lock);
if (!QueryPerformanceFrequency((LARGE_INTEGER*) &hrtime_frequency_))
hrtime_frequency_ = 0;
}
int uv_utf16_to_utf8(const WCHAR* utf16Buffer, size_t utf16Size,
char* utf8Buffer, size_t utf8Size) {
return WideCharToMultiByte(CP_UTF8,
0,
utf16Buffer,
utf16Size,
utf8Buffer,
utf8Size,
NULL,
NULL);
}
int uv_utf8_to_utf16(const char* utf8Buffer, WCHAR* utf16Buffer,
size_t utf16Size) {
return MultiByteToWideChar(CP_UTF8,
0,
utf8Buffer,
-1,
utf16Buffer,
utf16Size);
}
int uv_exepath(char* buffer, size_t* size_ptr) {
int utf8_len, utf16_buffer_len, utf16_len;
WCHAR* utf16_buffer;
int err;
if (buffer == NULL || size_ptr == NULL || *size_ptr == 0) {
return UV_EINVAL;
}
if (*size_ptr > 32768) {
utf16_buffer_len = 32768;
} else {
utf16_buffer_len = (int) *size_ptr;
}
utf16_buffer = (WCHAR*) malloc(sizeof(WCHAR) * utf16_buffer_len);
if (!utf16_buffer) {
return UV_ENOMEM;
}
utf16_len = GetModuleFileNameW(NULL, utf16_buffer, utf16_buffer_len);
if (utf16_len <= 0) {
err = GetLastError();
goto error;
}
utf16_buffer[utf16_len] = L'\0';
utf8_len = WideCharToMultiByte(CP_UTF8,
0,
utf16_buffer,
-1,
buffer,
*size_ptr > INT_MAX ? INT_MAX : (int) *size_ptr,
NULL,
NULL);
if (utf8_len == 0) {
err = GetLastError();
goto error;
}
free(utf16_buffer);
*size_ptr = utf8_len - 1;
return 0;
error:
free(utf16_buffer);
return uv_translate_sys_error(err);
}
int uv_cwd(char* buffer, size_t size) {
DWORD utf16_len;
WCHAR utf16_buffer[MAX_PATH];
int r;
if (buffer == NULL || size == 0) {
return UV_EINVAL;
}
utf16_len = GetCurrentDirectoryW(MAX_PATH, utf16_buffer);
if (utf16_len == 0) {
return uv_translate_sys_error(GetLastError());
} else if (utf16_len > MAX_PATH) {
return UV_EIO;
}
utf16_buffer[utf16_len] = L'\0';
if (utf16_buffer[utf16_len - 1] == L'\\' &&
!(utf16_len == 3 && utf16_buffer[1] == L':')) {
utf16_len--;
utf16_buffer[utf16_len] = L'\0';
}
r = WideCharToMultiByte(CP_UTF8,
0,
utf16_buffer,
-1,
buffer,
size > INT_MAX ? INT_MAX : (int) size,
NULL,
NULL);
if (r == 0) {
return uv_translate_sys_error(GetLastError());
}
return 0;
}
int uv_chdir(const char* dir) {
WCHAR utf16_buffer[MAX_PATH];
size_t utf16_len;
WCHAR drive_letter, env_var[4];
if (dir == NULL) {
return UV_EINVAL;
}
if (MultiByteToWideChar(CP_UTF8,
0,
dir,
-1,
utf16_buffer,
MAX_PATH) == 0) {
DWORD error = GetLastError();
if (error == ERROR_INSUFFICIENT_BUFFER) {
return UV_ENAMETOOLONG;
} else {
return uv_translate_sys_error(error);
}
}
if (!SetCurrentDirectoryW(utf16_buffer)) {
return uv_translate_sys_error(GetLastError());
}
utf16_len = GetCurrentDirectoryW(MAX_PATH, utf16_buffer);
if (utf16_len == 0) {
return uv_translate_sys_error(GetLastError());
} else if (utf16_len > MAX_PATH) {
return UV_EIO;
}
if (utf16_buffer[utf16_len - 1] == L'\\' &&
!(utf16_len == 3 && utf16_buffer[1] == L':')) {
utf16_len--;
utf16_buffer[utf16_len] = L'\0';
}
if (utf16_len < 2 || utf16_buffer[1] != L':') {
drive_letter = 0;
} else if (utf16_buffer[0] >= L'A' && utf16_buffer[0] <= L'Z') {
drive_letter = utf16_buffer[0];
} else if (utf16_buffer[0] >= L'a' && utf16_buffer[0] <= L'z') {
drive_letter = utf16_buffer[0] - L'a' + L'A';
} else {
drive_letter = 0;
}
if (drive_letter != 0) {
env_var[0] = L'=';
env_var[1] = drive_letter;
env_var[2] = L':';
env_var[3] = L'\0';
if (!SetEnvironmentVariableW(env_var, utf16_buffer)) {
return uv_translate_sys_error(GetLastError());
}
}
return 0;
}
void uv_loadavg(double avg[3]) {
avg[0] = avg[1] = avg[2] = 0;
}
uint64_t uv_get_free_memory(void) {
MEMORYSTATUSEX memory_status;
memory_status.dwLength = sizeof(memory_status);
if(!GlobalMemoryStatusEx(&memory_status))
{
return -1;
}
return (uint64_t)memory_status.ullAvailPhys;
}
uint64_t uv_get_total_memory(void) {
MEMORYSTATUSEX memory_status;
memory_status.dwLength = sizeof(memory_status);
if(!GlobalMemoryStatusEx(&memory_status))
{
return -1;
}
return (uint64_t)memory_status.ullTotalPhys;
}
int uv_parent_pid() {
int parent_pid = -1;
HANDLE handle;
PROCESSENTRY32 pe;
int current_pid = GetCurrentProcessId();
pe.dwSize = sizeof(PROCESSENTRY32);
handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (Process32First(handle, &pe)) {
do {
if (pe.th32ProcessID == current_pid) {
parent_pid = pe.th32ParentProcessID;
break;
}
} while( Process32Next(handle, &pe));
}
CloseHandle(handle);
return parent_pid;
}
char** uv_setup_args(int argc, char** argv) {
return argv;
}
int uv_set_process_title(const char* title) {
int err;
int length;
WCHAR* title_w = NULL;
uv__once_init();
length = uv_utf8_to_utf16(title, NULL, 0);
if (!length) {
err = GetLastError();
goto done;
}
title_w = (WCHAR*)malloc(sizeof(WCHAR) * length);
if (!title_w) {
uv_fatal_error(ERROR_OUTOFMEMORY, "malloc");
}
length = uv_utf8_to_utf16(title, title_w, length);
if (!length) {
err = GetLastError();
goto done;
};
if (length > MAX_TITLE_LENGTH) {
title_w[MAX_TITLE_LENGTH - 1] = L'\0';
}
if (!SetConsoleTitleW(title_w)) {
err = GetLastError();
goto done;
}
EnterCriticalSection(&process_title_lock);
free(process_title);
process_title = strdup(title);
LeaveCriticalSection(&process_title_lock);
err = 0;
done:
free(title_w);
return uv_translate_sys_error(err);
}
static int uv__get_process_title() {
WCHAR title_w[MAX_TITLE_LENGTH];
int length;
if (!GetConsoleTitleW(title_w, sizeof(title_w) / sizeof(WCHAR))) {
return -1;
}
length = uv_utf16_to_utf8(title_w, -1, NULL, 0);
if (!length) {
return -1;
}
assert(!process_title);
process_title = (char*)malloc(length);
if (!process_title) {
uv_fatal_error(ERROR_OUTOFMEMORY, "malloc");
}
if (!uv_utf16_to_utf8(title_w, -1, process_title, length)) {
free(process_title);
return -1;
}
return 0;
}
int uv_get_process_title(char* buffer, size_t size) {
uv__once_init();
EnterCriticalSection(&process_title_lock);
if (!process_title && uv__get_process_title() == -1) {
return uv_translate_sys_error(GetLastError());
}
assert(process_title);
strncpy(buffer, process_title, size);
LeaveCriticalSection(&process_title_lock);
return 0;
}
uint64_t uv_hrtime(void) {
LARGE_INTEGER counter;
uv__once_init();
if (!hrtime_frequency_) {
return 0;
}
if (!QueryPerformanceCounter(&counter)) {
return 0;
}
return ((uint64_t) counter.LowPart * NANOSEC / hrtime_frequency_) +
(((uint64_t) counter.HighPart * NANOSEC / hrtime_frequency_)
<< 32);
}
int uv_resident_set_memory(size_t* rss) {
HANDLE current_process;
PROCESS_MEMORY_COUNTERS pmc;
current_process = GetCurrentProcess();
if (!GetProcessMemoryInfo(current_process, &pmc, sizeof(pmc))) {
return uv_translate_sys_error(GetLastError());
}
*rss = pmc.WorkingSetSize;
return 0;
}
int uv_uptime(double* uptime) {
BYTE stack_buffer[4096];
BYTE* malloced_buffer = NULL;
BYTE* buffer = (BYTE*) stack_buffer;
size_t buffer_size = sizeof(stack_buffer);
DWORD data_size;
PERF_DATA_BLOCK* data_block;
PERF_OBJECT_TYPE* object_type;
PERF_COUNTER_DEFINITION* counter_definition;
DWORD i;
for (;;) {
LONG result;
data_size = (DWORD) buffer_size;
result = RegQueryValueExW(HKEY_PERFORMANCE_DATA,
L"2",
NULL,
NULL,
buffer,
&data_size);
if (result == ERROR_SUCCESS) {
break;
} else if (result != ERROR_MORE_DATA) {
*uptime = 0;
return uv_translate_sys_error(result);
}
free(malloced_buffer);
buffer_size *= 2;
if (buffer_size > 1 << 20) {
goto internalError;
}
buffer = malloced_buffer = (BYTE*) malloc(buffer_size);
if (malloced_buffer == NULL) {
*uptime = 0;
return UV_ENOMEM;
}
}
if (data_size < sizeof(*data_block))
goto internalError;
data_block = (PERF_DATA_BLOCK*) buffer;
if (wmemcmp(data_block->Signature, L"PERF", 4) != 0)
goto internalError;
if (data_size < data_block->HeaderLength + sizeof(*object_type))
goto internalError;
object_type = (PERF_OBJECT_TYPE*) (buffer + data_block->HeaderLength);
if (object_type->NumInstances != PERF_NO_INSTANCES)
goto internalError;
counter_definition = (PERF_COUNTER_DEFINITION*) (buffer +
data_block->HeaderLength + object_type->HeaderLength);
for (i = 0; i < object_type->NumCounters; i++) {
if ((BYTE*) counter_definition + sizeof(*counter_definition) >
buffer + data_size) {
break;
}
if (counter_definition->CounterNameTitleIndex == 674 &&
counter_definition->CounterSize == sizeof(uint64_t)) {
if (counter_definition->CounterOffset + sizeof(uint64_t) > data_size ||
!(counter_definition->CounterType & PERF_OBJECT_TIMER)) {
goto internalError;
} else {
BYTE* address = (BYTE*) object_type + object_type->DefinitionLength +
counter_definition->CounterOffset;
uint64_t value = *((uint64_t*) address);
*uptime = (double) (object_type->PerfTime.QuadPart - value) /
(double) object_type->PerfFreq.QuadPart;
free(malloced_buffer);
return 0;
}
}
counter_definition = (PERF_COUNTER_DEFINITION*)
((BYTE*) counter_definition + counter_definition->ByteLength);
}
free(malloced_buffer);
*uptime = 0;
return UV_ENOSYS;
internalError:
free(malloced_buffer);
*uptime = 0;
return UV_EIO;
}
int uv_cpu_info(uv_cpu_info_t** cpu_infos_ptr, int* cpu_count_ptr) {
uv_cpu_info_t* cpu_infos;
SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION* sppi;
DWORD sppi_size;
SYSTEM_INFO system_info;
DWORD cpu_count, r, i;
NTSTATUS status;
ULONG result_size;
int err;
uv_cpu_info_t* cpu_info;
cpu_infos = NULL;
cpu_count = 0;
sppi = NULL;
uv__once_init();
GetSystemInfo(&system_info);
cpu_count = system_info.dwNumberOfProcessors;
cpu_infos = calloc(cpu_count, sizeof *cpu_infos);
if (cpu_infos == NULL) {
err = ERROR_OUTOFMEMORY;
goto error;
}
sppi_size = cpu_count * sizeof(*sppi);
sppi = malloc(sppi_size);
if (sppi == NULL) {
err = ERROR_OUTOFMEMORY;
goto error;
}
status = pNtQuerySystemInformation(SystemProcessorPerformanceInformation,
sppi,
sppi_size,
&result_size);
if (!NT_SUCCESS(status)) {
err = pRtlNtStatusToDosError(status);
goto error;
}
assert(result_size == sppi_size);
for (i = 0; i < cpu_count; i++) {
WCHAR key_name[128];
HKEY processor_key;
DWORD cpu_speed;
DWORD cpu_speed_size = sizeof(cpu_speed);
WCHAR cpu_brand[256];
DWORD cpu_brand_size = sizeof(cpu_brand);
int len;
len = _snwprintf(key_name,
ARRAY_SIZE(key_name),
L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\%d",
i);
assert(len > 0 && len < ARRAY_SIZE(key_name));
r = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
key_name,
0,
KEY_QUERY_VALUE,
&processor_key);
if (r != ERROR_SUCCESS) {
err = GetLastError();
goto error;
}
if (RegQueryValueExW(processor_key,
L"~MHz",
NULL,
NULL,
(BYTE*) &cpu_speed,
&cpu_speed_size) != ERROR_SUCCESS) {
err = GetLastError();
RegCloseKey(processor_key);
goto error;
}
if (RegQueryValueExW(processor_key,
L"ProcessorNameString",
NULL,
NULL,
(BYTE*) &cpu_brand,
&cpu_brand_size) != ERROR_SUCCESS) {
err = GetLastError();
RegCloseKey(processor_key);
goto error;
}
RegCloseKey(processor_key);
cpu_info = &cpu_infos[i];
cpu_info->speed = cpu_speed;
cpu_info->cpu_times.user = sppi[i].UserTime.QuadPart / 10000;
cpu_info->cpu_times.sys = (sppi[i].KernelTime.QuadPart -
sppi[i].IdleTime.QuadPart) / 10000;
cpu_info->cpu_times.idle = sppi[i].IdleTime.QuadPart / 10000;
cpu_info->cpu_times.irq = sppi[i].InterruptTime.QuadPart / 10000;
cpu_info->cpu_times.nice = 0;
len = WideCharToMultiByte(CP_UTF8,
0,
cpu_brand,
cpu_brand_size / sizeof(WCHAR),
NULL,
0,
NULL,
NULL);
if (len == 0) {
err = GetLastError();
goto error;
}
assert(len > 0);
cpu_info->model = malloc(len + 1);
if (cpu_info->model == NULL) {
err = ERROR_OUTOFMEMORY;
goto error;
}
if (WideCharToMultiByte(CP_UTF8,
0,
cpu_brand,
cpu_brand_size / sizeof(WCHAR),
cpu_info->model,
len,
NULL,
NULL) == 0) {
err = GetLastError();
goto error;
}
cpu_info->model[len] = '\0';
}
free(sppi);
*cpu_count_ptr = cpu_count;
*cpu_infos_ptr = cpu_infos;
return 0;
error:
for (i = 0; i < cpu_count; i++)
free(cpu_infos[i].model);
free(cpu_infos);
free(sppi);
return uv_translate_sys_error(err);
}
void uv_free_cpu_info(uv_cpu_info_t* cpu_infos, int count) {
int i;
for (i = 0; i < count; i++) {
free(cpu_infos[i].model);
}
free(cpu_infos);
}
int uv_interface_addresses(uv_interface_address_t** addresses_ptr,
int* count_ptr) {
IP_ADAPTER_ADDRESSES* win_address_buf;
ULONG win_address_buf_size;
IP_ADAPTER_ADDRESSES* win_address;
uv_interface_address_t* uv_address_buf;
char* name_buf;
size_t uv_address_buf_size;
uv_interface_address_t* uv_address;
int count;
win_address_buf_size = 0;
win_address_buf = NULL;
for (;;) {
ULONG r;
r = GetAdaptersAddresses(AF_UNSPEC,
GAA_FLAG_INCLUDE_PREFIX,
NULL,
win_address_buf,
&win_address_buf_size);
if (r == ERROR_SUCCESS)
break;
free(win_address_buf);
switch (r) {
case ERROR_BUFFER_OVERFLOW:
win_address_buf = malloc(win_address_buf_size);
if (win_address_buf == NULL)
return UV_ENOMEM;
continue;
case ERROR_NO_DATA: {
uv_address_buf = malloc(1);
if (uv_address_buf == NULL)
return UV_ENOMEM;
*count_ptr = 0;
*addresses_ptr = uv_address_buf;
return 0;
}
case ERROR_ADDRESS_NOT_ASSOCIATED:
return UV_EAGAIN;
case ERROR_INVALID_PARAMETER:
return UV_ENOBUFS;
default:
assert(r != ERROR_SUCCESS);
return uv_translate_sys_error(r);
}
}
count = 0;
uv_address_buf_size = 0;
for (win_address = win_address_buf;
win_address != NULL;
win_address = win_address->Next) {
IP_ADAPTER_UNICAST_ADDRESS_XP* unicast_address;
int name_size;
if (win_address->OperStatus != IfOperStatusUp ||
win_address->FirstUnicastAddress == NULL)
continue;
name_size = WideCharToMultiByte(CP_UTF8,
0,
win_address->FriendlyName,
-1,
NULL,
0,
NULL,
FALSE);
if (name_size <= 0) {
free(win_address_buf);
return uv_translate_sys_error(GetLastError());
}
uv_address_buf_size += name_size;
for (unicast_address = (IP_ADAPTER_UNICAST_ADDRESS_XP*)
win_address->FirstUnicastAddress;
unicast_address != NULL;
unicast_address = unicast_address->Next) {
count++;
uv_address_buf_size += sizeof(uv_interface_address_t);
}
}
uv_address_buf = malloc(uv_address_buf_size);
if (uv_address_buf == NULL) {
free(win_address_buf);
return UV_ENOMEM;
}
uv_address = uv_address_buf;
name_buf = (char*) (uv_address_buf + count);
for (win_address = win_address_buf;
win_address != NULL;
win_address = win_address->Next) {
IP_ADAPTER_UNICAST_ADDRESS_XP* unicast_address;
IP_ADAPTER_PREFIX* prefix;
int name_size;
size_t max_name_size;
if (win_address->OperStatus != IfOperStatusUp ||
win_address->FirstUnicastAddress == NULL)
continue;
max_name_size = (char*) uv_address_buf + uv_address_buf_size - name_buf;
if (max_name_size > (size_t) INT_MAX)
max_name_size = INT_MAX;
name_size = WideCharToMultiByte(CP_UTF8,
0,
win_address->FriendlyName,
-1,
name_buf,
(int) max_name_size,
NULL,
FALSE);
if (name_size <= 0) {
free(win_address_buf);
free(uv_address_buf);
return uv_translate_sys_error(GetLastError());
}
prefix = win_address->FirstPrefix;
for (unicast_address = (IP_ADAPTER_UNICAST_ADDRESS_XP*)
win_address->FirstUnicastAddress;
unicast_address != NULL && prefix != NULL;
unicast_address = unicast_address->Next, prefix = prefix->Next) {
struct sockaddr* sa;
ULONG prefix_len;
sa = unicast_address->Address.lpSockaddr;
prefix_len = prefix->PrefixLength;
memset(uv_address, 0, sizeof *uv_address);
uv_address->name = name_buf;
if (win_address->PhysicalAddressLength == sizeof(uv_address->phys_addr)) {
memcpy(uv_address->phys_addr,
win_address->PhysicalAddress,
sizeof(uv_address->phys_addr));
}
uv_address->is_internal =
(win_address->IfType == IF_TYPE_SOFTWARE_LOOPBACK);
if (sa->sa_family == AF_INET6) {
uv_address->address.address6 = *((struct sockaddr_in6 *) sa);
uv_address->netmask.netmask6.sin6_family = AF_INET6;
memset(uv_address->netmask.netmask6.sin6_addr.s6_addr, 0xff, prefix_len >> 3);
uv_address->netmask.netmask6.sin6_addr.s6_addr[prefix_len >> 3] =
0xff << (8 - prefix_len % 8);
} else {
uv_address->address.address4 = *((struct sockaddr_in *) sa);
uv_address->netmask.netmask4.sin_family = AF_INET;
uv_address->netmask.netmask4.sin_addr.s_addr =
htonl(0xffffffff << (32 - prefix_len));
}
uv_address++;
}
name_buf += name_size;
}
free(win_address_buf);
*addresses_ptr = uv_address_buf;
*count_ptr = count;
return 0;
}
void uv_free_interface_addresses(uv_interface_address_t* addresses,
int count) {
free(addresses);
}