2011-07-19 01:47:30 +00:00
|
|
|
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <assert.h>
|
2011-11-30 23:31:39 +00:00
|
|
|
#include <direct.h>
|
2012-06-08 17:33:26 +00:00
|
|
|
#include <limits.h>
|
2011-07-19 01:47:30 +00:00
|
|
|
#include <malloc.h>
|
2011-12-13 09:29:27 +00:00
|
|
|
#include <stdio.h>
|
2011-07-19 01:47:30 +00:00
|
|
|
#include <string.h>
|
2012-01-11 21:29:27 +00:00
|
|
|
#include <time.h>
|
2012-04-12 15:36:42 +00:00
|
|
|
#include <wchar.h>
|
2011-07-19 01:47:30 +00:00
|
|
|
|
|
|
|
|
#include "uv.h"
|
|
|
|
|
#include "internal.h"
|
2012-05-17 04:28:46 +00:00
|
|
|
|
2013-02-09 22:33:46 +00:00
|
|
|
#include <Winsock2.h>
|
2012-05-17 04:28:46 +00:00
|
|
|
#include <iphlpapi.h>
|
|
|
|
|
#include <psapi.h>
|
|
|
|
|
#include <tlhelp32.h>
|
2011-12-13 09:29:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Max title length; the only thing MSDN tells us about the maximum length
|
|
|
|
|
* of the console title is that it is smaller than 64K. However in practice
|
|
|
|
|
* it is much smaller, and there is no way to figure out what the exact length
|
|
|
|
|
* of the title is or can be, at least not on XP. To make it even more
|
|
|
|
|
* annoying, GetConsoleTitle failes when the buffer to be read into is bigger
|
|
|
|
|
* than the actual maximum length. So we make a conservative guess here;
|
|
|
|
|
* just don't put the novel you're writing in the title, unless the plot
|
|
|
|
|
* survives truncation.
|
|
|
|
|
*/
|
|
|
|
|
#define MAX_TITLE_LENGTH 8192
|
|
|
|
|
|
2012-06-02 18:37:08 +00:00
|
|
|
/* The number of nanoseconds in one second. */
|
|
|
|
|
#undef NANOSEC
|
|
|
|
|
#define NANOSEC 1000000000
|
|
|
|
|
|
2012-06-02 18:35:14 +00:00
|
|
|
|
|
|
|
|
/* Cached copy of the process title, plus a mutex guarding it. */
|
2011-12-13 09:29:27 +00:00
|
|
|
static char *process_title;
|
2012-01-12 02:46:27 +00:00
|
|
|
static CRITICAL_SECTION process_title_lock;
|
2011-07-19 01:47:30 +00:00
|
|
|
|
2012-06-02 18:37:08 +00:00
|
|
|
/* The tick frequency of the high-resolution clock. */
|
|
|
|
|
static uint64_t hrtime_frequency_ = 0;
|
|
|
|
|
|
2012-06-02 18:35:14 +00:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* One-time intialization code for functionality defined in util.c.
|
|
|
|
|
*/
|
|
|
|
|
void uv__util_init() {
|
|
|
|
|
/* Initialize process title access mutex. */
|
|
|
|
|
InitializeCriticalSection(&process_title_lock);
|
2012-06-02 18:37:08 +00:00
|
|
|
|
|
|
|
|
/* Retrieve high-resolution timer frequency. */
|
|
|
|
|
if (!QueryPerformanceFrequency((LARGE_INTEGER*) &hrtime_frequency_))
|
|
|
|
|
hrtime_frequency_ = 0;
|
2012-06-02 18:35:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-08-13 20:19:03 +00:00
|
|
|
int uv_utf16_to_utf8(const WCHAR* utf16Buffer, size_t utf16Size,
|
2011-08-31 02:19:16 +00:00
|
|
|
char* utf8Buffer, size_t utf8Size) {
|
|
|
|
|
return WideCharToMultiByte(CP_UTF8,
|
|
|
|
|
0,
|
|
|
|
|
utf16Buffer,
|
|
|
|
|
utf16Size,
|
|
|
|
|
utf8Buffer,
|
|
|
|
|
utf8Size,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL);
|
2011-07-19 01:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-08-13 20:19:03 +00:00
|
|
|
int uv_utf8_to_utf16(const char* utf8Buffer, WCHAR* utf16Buffer,
|
2011-08-31 02:19:16 +00:00
|
|
|
size_t utf16Size) {
|
|
|
|
|
return MultiByteToWideChar(CP_UTF8,
|
|
|
|
|
0,
|
|
|
|
|
utf8Buffer,
|
|
|
|
|
-1,
|
|
|
|
|
utf16Buffer,
|
|
|
|
|
utf16Size);
|
2011-07-19 01:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-06-04 17:05:15 +00:00
|
|
|
int uv_exepath(char* buffer, size_t* size_ptr) {
|
|
|
|
|
int utf8_len, utf16_buffer_len, utf16_len;
|
|
|
|
|
WCHAR* utf16_buffer;
|
2011-07-19 01:47:30 +00:00
|
|
|
|
2012-06-04 17:05:15 +00:00
|
|
|
if (buffer == NULL || size_ptr == NULL || *size_ptr == 0) {
|
2011-07-19 01:47:30 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-04 17:05:15 +00:00
|
|
|
if (*size_ptr > 32768) {
|
|
|
|
|
/* Windows paths can never be longer than this. */
|
|
|
|
|
utf16_buffer_len = 32768;
|
|
|
|
|
} else {
|
|
|
|
|
utf16_buffer_len = (int) *size_ptr;
|
2011-07-19 01:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
2012-08-13 20:19:03 +00:00
|
|
|
utf16_buffer = (WCHAR*) malloc(sizeof(WCHAR) * utf16_buffer_len);
|
2012-06-04 17:05:15 +00:00
|
|
|
if (!utf16_buffer) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Get the path as UTF-16. */
|
|
|
|
|
utf16_len = GetModuleFileNameW(NULL, utf16_buffer, utf16_buffer_len);
|
|
|
|
|
if (utf16_len <= 0) {
|
|
|
|
|
goto error;
|
2011-07-19 01:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
2012-06-04 17:05:15 +00:00
|
|
|
/* utf16_len contains the length, *not* including the terminating null. */
|
|
|
|
|
utf16_buffer[utf16_len] = L'\0';
|
2011-07-19 01:47:30 +00:00
|
|
|
|
|
|
|
|
/* Convert to UTF-8 */
|
2012-06-04 17:05:15 +00:00
|
|
|
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) {
|
|
|
|
|
goto error;
|
2011-07-19 01:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
2012-06-04 17:05:15 +00:00
|
|
|
free(utf16_buffer);
|
2011-07-19 01:47:30 +00:00
|
|
|
|
2012-06-04 17:05:15 +00:00
|
|
|
/* utf8_len *does* include the terminating null at this point, but the */
|
|
|
|
|
/* returned size shouldn't. */
|
|
|
|
|
*size_ptr = utf8_len - 1;
|
|
|
|
|
return 0;
|
2011-07-19 01:47:30 +00:00
|
|
|
|
2012-06-04 17:05:15 +00:00
|
|
|
error:
|
|
|
|
|
free(utf16_buffer);
|
|
|
|
|
return -1;
|
2011-07-19 01:47:30 +00:00
|
|
|
}
|
2011-10-01 08:45:42 +00:00
|
|
|
|
2011-09-30 00:58:58 +00:00
|
|
|
|
2011-11-30 23:31:39 +00:00
|
|
|
uv_err_t uv_cwd(char* buffer, size_t size) {
|
2012-06-04 17:04:40 +00:00
|
|
|
DWORD utf16_len;
|
2012-06-05 16:18:27 +00:00
|
|
|
WCHAR utf16_buffer[MAX_PATH];
|
2012-06-04 17:04:40 +00:00
|
|
|
int r;
|
2011-11-30 23:31:39 +00:00
|
|
|
|
2012-06-04 17:04:40 +00:00
|
|
|
if (buffer == NULL || size == 0) {
|
|
|
|
|
return uv__new_artificial_error(UV_EINVAL);
|
2011-11-30 23:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
2012-06-04 17:04:40 +00:00
|
|
|
utf16_len = GetCurrentDirectoryW(MAX_PATH, utf16_buffer);
|
|
|
|
|
if (utf16_len == 0) {
|
|
|
|
|
return uv__new_sys_error(GetLastError());
|
2012-06-05 16:18:27 +00:00
|
|
|
} else if (utf16_len > MAX_PATH) {
|
|
|
|
|
/* This should be impossible; however the CRT has a code path to deal */
|
|
|
|
|
/* with this scenario, so I added a check anyway. */
|
|
|
|
|
return uv__new_artificial_error(UV_EIO);
|
2011-11-30 23:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
2012-06-04 17:04:40 +00:00
|
|
|
/* utf16_len contains the length, *not* including the terminating null. */
|
|
|
|
|
utf16_buffer[utf16_len] = L'\0';
|
2011-11-30 23:31:39 +00:00
|
|
|
|
2012-06-04 17:04:40 +00:00
|
|
|
/* The returned directory should not have a trailing slash, unless it */
|
|
|
|
|
/* points at a drive root, like c:\. Remove it if needed.*/
|
|
|
|
|
if (utf16_buffer[utf16_len - 1] == L'\\' &&
|
|
|
|
|
!(utf16_len == 3 && utf16_buffer[1] == L':')) {
|
|
|
|
|
utf16_len--;
|
|
|
|
|
utf16_buffer[utf16_len] = L'\0';
|
2011-11-30 23:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
2012-06-04 17:04:40 +00:00
|
|
|
/* Convert to UTF-8 */
|
|
|
|
|
r = WideCharToMultiByte(CP_UTF8,
|
|
|
|
|
0,
|
|
|
|
|
utf16_buffer,
|
|
|
|
|
-1,
|
|
|
|
|
buffer,
|
|
|
|
|
size > INT_MAX ? INT_MAX : (int) size,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL);
|
|
|
|
|
if (r == 0) {
|
|
|
|
|
return uv__new_sys_error(GetLastError());
|
2011-11-30 23:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
2012-06-04 17:04:40 +00:00
|
|
|
return uv_ok_;
|
2011-11-30 23:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uv_err_t uv_chdir(const char* dir) {
|
2012-06-05 16:19:03 +00:00
|
|
|
WCHAR utf16_buffer[MAX_PATH];
|
|
|
|
|
size_t utf16_len;
|
|
|
|
|
WCHAR drive_letter, env_var[4];
|
2011-11-30 23:31:39 +00:00
|
|
|
|
2012-06-05 16:19:03 +00:00
|
|
|
if (dir == NULL) {
|
|
|
|
|
return uv__new_artificial_error(UV_EINVAL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (MultiByteToWideChar(CP_UTF8,
|
|
|
|
|
0,
|
|
|
|
|
dir,
|
|
|
|
|
-1,
|
|
|
|
|
utf16_buffer,
|
|
|
|
|
MAX_PATH) == 0) {
|
|
|
|
|
DWORD error = GetLastError();
|
|
|
|
|
/* The maximum length of the current working directory is 260 chars, */
|
|
|
|
|
/* including terminating null. If it doesn't fit, the path name must be */
|
|
|
|
|
/* too long. */
|
|
|
|
|
if (error == ERROR_INSUFFICIENT_BUFFER) {
|
|
|
|
|
return uv__new_artificial_error(UV_ENAMETOOLONG);
|
|
|
|
|
} else {
|
|
|
|
|
return uv__new_sys_error(error);
|
|
|
|
|
}
|
2011-11-30 23:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
2012-06-05 16:19:03 +00:00
|
|
|
if (!SetCurrentDirectoryW(utf16_buffer)) {
|
|
|
|
|
return uv__new_sys_error(GetLastError());
|
2011-11-30 23:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
2012-06-05 16:19:03 +00:00
|
|
|
/* Windows stores the drive-local path in an "hidden" environment variable, */
|
|
|
|
|
/* which has the form "=C:=C:\Windows". SetCurrentDirectory does not */
|
|
|
|
|
/* update this, so we'll have to do it. */
|
|
|
|
|
utf16_len = GetCurrentDirectoryW(MAX_PATH, utf16_buffer);
|
|
|
|
|
if (utf16_len == 0) {
|
|
|
|
|
return uv__new_sys_error(GetLastError());
|
|
|
|
|
} else if (utf16_len > MAX_PATH) {
|
|
|
|
|
return uv__new_artificial_error(UV_EIO);
|
2011-11-30 23:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
2012-06-05 16:19:03 +00:00
|
|
|
/* The returned directory should not have a trailing slash, unless it */
|
|
|
|
|
/* points at a drive root, like c:\. Remove it if needed. */
|
|
|
|
|
if (utf16_buffer[utf16_len - 1] == L'\\' &&
|
|
|
|
|
!(utf16_len == 3 && utf16_buffer[1] == L':')) {
|
|
|
|
|
utf16_len--;
|
|
|
|
|
utf16_buffer[utf16_len] = L'\0';
|
2011-11-30 23:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
2012-06-05 16:19:03 +00:00
|
|
|
if (utf16_len < 2 || utf16_buffer[1] != L':') {
|
|
|
|
|
/* Doesn't look like a drive letter could be there - probably an UNC */
|
|
|
|
|
/* path. TODO: Need to handle win32 namespaces like \\?\C:\ ? */
|
|
|
|
|
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') {
|
|
|
|
|
/* Convert to uppercase. */
|
|
|
|
|
drive_letter = utf16_buffer[0] - L'a' + L'A';
|
|
|
|
|
} else {
|
|
|
|
|
/* Not valid. */
|
|
|
|
|
drive_letter = 0;
|
2011-11-30 23:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
2012-06-05 16:19:03 +00:00
|
|
|
if (drive_letter != 0) {
|
|
|
|
|
/* Construct the environment variable name and set it. */
|
|
|
|
|
env_var[0] = L'=';
|
|
|
|
|
env_var[1] = drive_letter;
|
|
|
|
|
env_var[2] = L':';
|
|
|
|
|
env_var[3] = L'\0';
|
2011-11-30 23:31:39 +00:00
|
|
|
|
2012-06-05 16:19:03 +00:00
|
|
|
if (!SetEnvironmentVariableW(env_var, utf16_buffer)) {
|
|
|
|
|
return uv__new_sys_error(GetLastError());
|
|
|
|
|
}
|
2011-11-30 23:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
2012-06-05 16:19:03 +00:00
|
|
|
return uv_ok_;
|
2011-11-30 23:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-10-01 17:48:42 +00:00
|
|
|
void uv_loadavg(double avg[3]) {
|
|
|
|
|
/* Can't be implemented */
|
|
|
|
|
avg[0] = avg[1] = avg[2] = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-30 00:58:58 +00:00
|
|
|
|
2011-10-21 17:00:41 +00:00
|
|
|
uint64_t uv_get_free_memory(void) {
|
2011-10-01 08:45:42 +00:00
|
|
|
MEMORYSTATUSEX memory_status;
|
|
|
|
|
memory_status.dwLength = sizeof(memory_status);
|
|
|
|
|
|
|
|
|
|
if(!GlobalMemoryStatusEx(&memory_status))
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-21 17:00:41 +00:00
|
|
|
return (uint64_t)memory_status.ullAvailPhys;
|
2011-10-01 08:45:42 +00:00
|
|
|
}
|
|
|
|
|
|
2011-09-30 00:58:58 +00:00
|
|
|
|
2011-10-21 17:00:41 +00:00
|
|
|
uint64_t uv_get_total_memory(void) {
|
2011-10-01 08:45:42 +00:00
|
|
|
MEMORYSTATUSEX memory_status;
|
|
|
|
|
memory_status.dwLength = sizeof(memory_status);
|
|
|
|
|
|
|
|
|
|
if(!GlobalMemoryStatusEx(&memory_status))
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-21 17:00:41 +00:00
|
|
|
return (uint64_t)memory_status.ullTotalPhys;
|
2011-10-01 08:45:42 +00:00
|
|
|
}
|
2011-09-30 00:58:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2011-12-13 09:29:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
char** uv_setup_args(int argc, char** argv) {
|
|
|
|
|
return argv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uv_err_t uv_set_process_title(const char* title) {
|
|
|
|
|
uv_err_t err;
|
|
|
|
|
int length;
|
2012-08-13 20:19:03 +00:00
|
|
|
WCHAR* title_w = NULL;
|
2011-12-13 09:29:27 +00:00
|
|
|
|
2012-06-02 18:35:14 +00:00
|
|
|
uv__once_init();
|
2012-01-12 02:46:27 +00:00
|
|
|
|
2011-12-13 09:29:27 +00:00
|
|
|
/* Find out how big the buffer for the wide-char title must be */
|
|
|
|
|
length = uv_utf8_to_utf16(title, NULL, 0);
|
|
|
|
|
if (!length) {
|
|
|
|
|
err = uv__new_sys_error(GetLastError());
|
|
|
|
|
goto done;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Convert to wide-char string */
|
2012-08-13 20:19:03 +00:00
|
|
|
title_w = (WCHAR*)malloc(sizeof(WCHAR) * length);
|
2011-12-13 09:29:27 +00:00
|
|
|
if (!title_w) {
|
|
|
|
|
uv_fatal_error(ERROR_OUTOFMEMORY, "malloc");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
length = uv_utf8_to_utf16(title, title_w, length);
|
|
|
|
|
if (!length) {
|
|
|
|
|
err = uv__new_sys_error(GetLastError());
|
|
|
|
|
goto done;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* If the title must be truncated insert a \0 terminator there */
|
|
|
|
|
if (length > MAX_TITLE_LENGTH) {
|
|
|
|
|
title_w[MAX_TITLE_LENGTH - 1] = L'\0';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!SetConsoleTitleW(title_w)) {
|
|
|
|
|
err = uv__new_sys_error(GetLastError());
|
|
|
|
|
goto done;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-12 02:46:27 +00:00
|
|
|
EnterCriticalSection(&process_title_lock);
|
2011-12-13 09:29:27 +00:00
|
|
|
free(process_title);
|
|
|
|
|
process_title = strdup(title);
|
2012-01-12 02:46:27 +00:00
|
|
|
LeaveCriticalSection(&process_title_lock);
|
2011-12-13 09:29:27 +00:00
|
|
|
|
|
|
|
|
err = uv_ok_;
|
|
|
|
|
|
|
|
|
|
done:
|
|
|
|
|
free(title_w);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int uv__get_process_title() {
|
2012-08-13 20:19:03 +00:00
|
|
|
WCHAR title_w[MAX_TITLE_LENGTH];
|
2011-12-13 09:29:27 +00:00
|
|
|
int length;
|
|
|
|
|
|
|
|
|
|
if (!GetConsoleTitleW(title_w, sizeof(title_w) / sizeof(WCHAR))) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Find out what the size of the buffer is that we need */
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Do utf16 -> utf8 conversion here */
|
|
|
|
|
if (!uv_utf16_to_utf8(title_w, -1, process_title, length)) {
|
|
|
|
|
free(process_title);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uv_err_t uv_get_process_title(char* buffer, size_t size) {
|
2012-06-02 18:35:14 +00:00
|
|
|
uv__once_init();
|
2012-01-12 02:46:27 +00:00
|
|
|
|
|
|
|
|
EnterCriticalSection(&process_title_lock);
|
2011-12-13 09:29:27 +00:00
|
|
|
/*
|
|
|
|
|
* If the process_title was never read before nor explicitly set,
|
|
|
|
|
* we must query it with getConsoleTitleW
|
|
|
|
|
*/
|
|
|
|
|
if (!process_title && uv__get_process_title() == -1) {
|
|
|
|
|
return uv__new_sys_error(GetLastError());
|
|
|
|
|
}
|
2012-04-12 01:14:34 +00:00
|
|
|
|
2011-12-13 09:29:27 +00:00
|
|
|
assert(process_title);
|
2012-04-12 01:14:34 +00:00
|
|
|
strncpy(buffer, process_title, size);
|
2012-01-12 02:46:27 +00:00
|
|
|
LeaveCriticalSection(&process_title_lock);
|
2011-12-13 09:29:27 +00:00
|
|
|
|
|
|
|
|
return uv_ok_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-06-02 18:37:08 +00:00
|
|
|
uint64_t uv_hrtime(void) {
|
|
|
|
|
LARGE_INTEGER counter;
|
|
|
|
|
|
|
|
|
|
uv__once_init();
|
|
|
|
|
|
|
|
|
|
/* If the performance frequency is zero, there's no support. */
|
|
|
|
|
if (!hrtime_frequency_) {
|
|
|
|
|
/* uv__set_sys_error(loop, ERROR_NOT_SUPPORTED); */
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!QueryPerformanceCounter(&counter)) {
|
|
|
|
|
/* uv__set_sys_error(loop, GetLastError()); */
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Because we have no guarantee about the order of magnitude of the */
|
|
|
|
|
/* performance counter frequency, and there may not be much headroom to */
|
|
|
|
|
/* multiply by NANOSEC without overflowing, we use 128-bit math instead. */
|
|
|
|
|
return ((uint64_t) counter.LowPart * NANOSEC / hrtime_frequency_) +
|
|
|
|
|
(((uint64_t) counter.HighPart * NANOSEC / hrtime_frequency_)
|
|
|
|
|
<< 32);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-12-13 09:29:27 +00:00
|
|
|
uv_err_t 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__new_sys_error(GetLastError());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*rss = pmc.WorkingSetSize;
|
|
|
|
|
|
|
|
|
|
return uv_ok_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uv_err_t uv_uptime(double* uptime) {
|
2012-04-12 15:36:42 +00:00
|
|
|
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__new_sys_error(result);
|
2012-02-24 16:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
2012-04-12 15:36:42 +00:00
|
|
|
free(malloced_buffer);
|
|
|
|
|
|
|
|
|
|
buffer_size *= 2;
|
|
|
|
|
/* Don't let the buffer grow infinitely. */
|
|
|
|
|
if (buffer_size > 1 << 20) {
|
|
|
|
|
goto internalError;
|
2012-02-24 16:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
2012-04-12 15:36:42 +00:00
|
|
|
buffer = malloced_buffer = (BYTE*) malloc(buffer_size);
|
|
|
|
|
if (malloced_buffer == NULL) {
|
|
|
|
|
*uptime = 0;
|
|
|
|
|
return uv__new_artificial_error(UV_ENOMEM);
|
|
|
|
|
}
|
2012-02-24 16:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
2012-04-12 15:36:42 +00:00
|
|
|
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;
|
2012-02-24 16:56:15 +00:00
|
|
|
|
2012-04-12 15:36:42 +00:00
|
|
|
if (data_size < data_block->HeaderLength + sizeof(*object_type))
|
|
|
|
|
goto internalError;
|
2012-02-24 16:56:15 +00:00
|
|
|
|
2012-04-12 15:36:42 +00:00
|
|
|
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) {
|
2012-02-24 16:56:15 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-12 15:36:42 +00:00
|
|
|
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 uv_ok_;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-02-24 16:56:15 +00:00
|
|
|
|
2012-04-12 15:36:42 +00:00
|
|
|
counter_definition = (PERF_COUNTER_DEFINITION*)
|
|
|
|
|
((BYTE*) counter_definition + counter_definition->ByteLength);
|
2012-02-24 16:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
2012-04-12 15:36:42 +00:00
|
|
|
/* If we get here, the uptime value was not found. */
|
|
|
|
|
free(malloced_buffer);
|
|
|
|
|
*uptime = 0;
|
|
|
|
|
return uv__new_artificial_error(UV_ENOSYS);
|
|
|
|
|
|
|
|
|
|
internalError:
|
|
|
|
|
free(malloced_buffer);
|
|
|
|
|
*uptime = 0;
|
|
|
|
|
return uv__new_artificial_error(UV_EIO);
|
2011-12-13 09:29:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uv_err_t uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
|
2012-06-02 18:01:33 +00:00
|
|
|
SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION* sppi;
|
|
|
|
|
DWORD sppi_size;
|
2011-12-13 09:29:27 +00:00
|
|
|
SYSTEM_INFO system_info;
|
2012-06-02 18:01:33 +00:00
|
|
|
DWORD cpu_count, i, r;
|
|
|
|
|
ULONG result_size;
|
|
|
|
|
size_t size;
|
|
|
|
|
uv_err_t err;
|
2011-12-13 09:29:27 +00:00
|
|
|
uv_cpu_info_t* cpu_info;
|
|
|
|
|
|
2012-06-02 18:01:33 +00:00
|
|
|
*cpu_infos = NULL;
|
|
|
|
|
*count = 0;
|
2011-12-13 09:29:27 +00:00
|
|
|
|
2012-06-02 18:01:33 +00:00
|
|
|
uv__once_init();
|
2011-12-13 09:29:27 +00:00
|
|
|
|
2012-06-02 18:01:33 +00:00
|
|
|
GetSystemInfo(&system_info);
|
|
|
|
|
cpu_count = system_info.dwNumberOfProcessors;
|
2011-12-13 09:29:27 +00:00
|
|
|
|
2012-06-02 18:01:33 +00:00
|
|
|
size = cpu_count * sizeof(uv_cpu_info_t);
|
|
|
|
|
*cpu_infos = (uv_cpu_info_t*) malloc(size);
|
|
|
|
|
if (*cpu_infos == NULL) {
|
|
|
|
|
err = uv__new_artificial_error(UV_ENOMEM);
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
memset(*cpu_infos, 0, size);
|
2011-12-13 09:29:27 +00:00
|
|
|
|
2012-06-02 18:01:33 +00:00
|
|
|
sppi_size = sizeof(*sppi) * cpu_count;
|
|
|
|
|
sppi = (SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION*) malloc(sppi_size);
|
|
|
|
|
if (!sppi) {
|
|
|
|
|
uv_fatal_error(ERROR_OUTOFMEMORY, "malloc");
|
|
|
|
|
}
|
2011-12-13 09:29:27 +00:00
|
|
|
|
2012-06-02 18:01:33 +00:00
|
|
|
r = pNtQuerySystemInformation(SystemProcessorPerformanceInformation,
|
|
|
|
|
sppi,
|
|
|
|
|
sppi_size,
|
|
|
|
|
&result_size);
|
|
|
|
|
if (r != ERROR_SUCCESS || result_size != sppi_size) {
|
|
|
|
|
err = uv__new_sys_error(GetLastError());
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
_snwprintf(key_name,
|
|
|
|
|
ARRAY_SIZE(key_name),
|
|
|
|
|
L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\%d",
|
|
|
|
|
i);
|
|
|
|
|
|
|
|
|
|
r = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
|
|
|
|
|
key_name,
|
|
|
|
|
0,
|
|
|
|
|
KEY_QUERY_VALUE,
|
|
|
|
|
&processor_key);
|
|
|
|
|
if (r != ERROR_SUCCESS) {
|
|
|
|
|
err = uv__new_sys_error(GetLastError());
|
|
|
|
|
goto out;
|
2011-12-13 09:29:27 +00:00
|
|
|
}
|
|
|
|
|
|
2012-06-02 18:01:33 +00:00
|
|
|
if (RegQueryValueExW(processor_key,
|
|
|
|
|
L"~MHz",
|
|
|
|
|
NULL, NULL,
|
|
|
|
|
(BYTE*) &cpu_speed,
|
|
|
|
|
&cpu_speed_size) != ERROR_SUCCESS) {
|
2011-12-13 09:29:27 +00:00
|
|
|
err = uv__new_sys_error(GetLastError());
|
2012-06-02 18:01:33 +00:00
|
|
|
RegCloseKey(processor_key);
|
|
|
|
|
goto out;
|
2011-12-13 09:29:27 +00:00
|
|
|
}
|
|
|
|
|
|
2012-06-02 18:01:33 +00:00
|
|
|
if (RegQueryValueExW(processor_key,
|
|
|
|
|
L"ProcessorNameString",
|
|
|
|
|
NULL, NULL,
|
|
|
|
|
(BYTE*) &cpu_brand,
|
|
|
|
|
&cpu_brand_size) != ERROR_SUCCESS) {
|
2011-12-13 09:29:27 +00:00
|
|
|
err = uv__new_sys_error(GetLastError());
|
2012-06-02 18:01:33 +00:00
|
|
|
RegCloseKey(processor_key);
|
|
|
|
|
goto out;
|
2011-12-13 09:29:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RegCloseKey(processor_key);
|
2012-04-12 01:14:34 +00:00
|
|
|
|
2011-12-13 09:29:27 +00:00
|
|
|
cpu_info = &(*cpu_infos)[i];
|
2012-06-02 18:01:33 +00:00
|
|
|
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;
|
2011-12-13 09:29:27 +00:00
|
|
|
cpu_info->cpu_times.nice = 0;
|
|
|
|
|
|
2012-06-02 18:01:33 +00:00
|
|
|
size = uv_utf16_to_utf8(cpu_brand,
|
|
|
|
|
cpu_brand_size / sizeof(WCHAR),
|
|
|
|
|
NULL,
|
|
|
|
|
0);
|
|
|
|
|
if (size == 0) {
|
|
|
|
|
err = uv__new_sys_error(GetLastError());
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Allocate 1 extra byte for the null terminator. */
|
|
|
|
|
cpu_info->model = (char*) malloc(size + 1);
|
|
|
|
|
if (cpu_info->model == NULL) {
|
|
|
|
|
err = uv__new_artificial_error(UV_ENOMEM);
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (uv_utf16_to_utf8(cpu_brand,
|
|
|
|
|
cpu_brand_size / sizeof(WCHAR),
|
|
|
|
|
cpu_info->model,
|
|
|
|
|
size) == 0) {
|
|
|
|
|
err = uv__new_sys_error(GetLastError());
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Ensure that cpu_info->model is null terminated. */
|
|
|
|
|
cpu_info->model[size] = '\0';
|
2011-12-13 09:29:27 +00:00
|
|
|
|
|
|
|
|
(*count)++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = uv_ok_;
|
|
|
|
|
|
2012-06-02 18:01:33 +00:00
|
|
|
out:
|
|
|
|
|
if (sppi) {
|
|
|
|
|
free(sppi);
|
2011-12-13 09:29:27 +00:00
|
|
|
}
|
|
|
|
|
|
2012-06-02 18:01:33 +00:00
|
|
|
if (err.code != UV_OK &&
|
|
|
|
|
*cpu_infos != NULL) {
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < *count; i++) {
|
|
|
|
|
/* This is safe because the cpu_infos memory area is zeroed out */
|
|
|
|
|
/* immediately after allocating it. */
|
|
|
|
|
free((*cpu_infos)[i].model);
|
|
|
|
|
}
|
2011-12-13 09:29:27 +00:00
|
|
|
free(*cpu_infos);
|
2012-06-02 18:01:33 +00:00
|
|
|
|
2011-12-13 09:29:27 +00:00
|
|
|
*cpu_infos = NULL;
|
|
|
|
|
*count = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-12-04 13:04:37 +00:00
|
|
|
uv_err_t 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;
|
2011-12-13 09:29:27 +00:00
|
|
|
|
2012-12-04 13:04:37 +00:00
|
|
|
uv_interface_address_t* uv_address_buf;
|
|
|
|
|
char* name_buf;
|
|
|
|
|
size_t uv_address_buf_size;
|
|
|
|
|
uv_interface_address_t* uv_address;
|
2011-12-13 09:29:27 +00:00
|
|
|
|
2012-12-04 13:04:37 +00:00
|
|
|
int count;
|
2011-12-13 09:29:27 +00:00
|
|
|
|
2012-12-04 13:04:37 +00:00
|
|
|
/* Fetch the size of the adapters reported by windows, and then get the */
|
|
|
|
|
/* list itself. */
|
|
|
|
|
win_address_buf_size = 0;
|
|
|
|
|
win_address_buf = NULL;
|
2011-12-13 09:29:27 +00:00
|
|
|
|
2012-12-04 13:04:37 +00:00
|
|
|
for (;;) {
|
|
|
|
|
ULONG r;
|
2011-12-13 09:29:27 +00:00
|
|
|
|
2012-12-04 13:04:37 +00:00
|
|
|
/* If win_address_buf is 0, then GetAdaptersAddresses will fail with */
|
|
|
|
|
/* ERROR_BUFFER_OVERFLOW, and the required buffer size will be stored in */
|
|
|
|
|
/* win_address_buf_size. */
|
|
|
|
|
r = GetAdaptersAddresses(AF_UNSPEC,
|
2013-02-09 22:33:46 +00:00
|
|
|
GAA_FLAG_INCLUDE_PREFIX,
|
2012-12-04 13:04:37 +00:00
|
|
|
NULL,
|
|
|
|
|
win_address_buf,
|
|
|
|
|
&win_address_buf_size);
|
2012-06-09 20:30:18 +00:00
|
|
|
|
2012-12-04 13:04:37 +00:00
|
|
|
if (r == ERROR_SUCCESS)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
free(win_address_buf);
|
|
|
|
|
|
|
|
|
|
switch (r) {
|
|
|
|
|
case ERROR_BUFFER_OVERFLOW:
|
|
|
|
|
/* This happens when win_address_buf is NULL or too small to hold */
|
|
|
|
|
/* all adapters. */
|
|
|
|
|
win_address_buf = malloc(win_address_buf_size);
|
|
|
|
|
if (win_address_buf == NULL)
|
|
|
|
|
return uv__new_artificial_error(UV_ENOMEM);
|
|
|
|
|
|
|
|
|
|
continue;
|
2012-06-09 20:30:18 +00:00
|
|
|
|
2012-12-04 13:04:37 +00:00
|
|
|
case ERROR_NO_DATA: {
|
|
|
|
|
/* No adapters were found. */
|
|
|
|
|
uv_address_buf = malloc(1);
|
|
|
|
|
if (uv_address_buf == NULL)
|
|
|
|
|
return uv__new_artificial_error(UV_ENOMEM);
|
2012-06-09 20:30:18 +00:00
|
|
|
|
2012-12-04 13:04:37 +00:00
|
|
|
*count_ptr = 0;
|
|
|
|
|
*addresses_ptr = uv_address_buf;
|
|
|
|
|
|
|
|
|
|
return uv_ok_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case ERROR_ADDRESS_NOT_ASSOCIATED:
|
|
|
|
|
return uv__new_artificial_error(UV_EAGAIN);
|
|
|
|
|
|
|
|
|
|
case ERROR_INVALID_PARAMETER:
|
|
|
|
|
/* MSDN says:
|
|
|
|
|
* "This error is returned for any of the following conditions: the
|
|
|
|
|
* SizePointer parameter is NULL, the Address parameter is not
|
|
|
|
|
* AF_INET, AF_INET6, or AF_UNSPEC, or the address information for
|
|
|
|
|
* the parameters requested is greater than ULONG_MAX."
|
|
|
|
|
* Since the first two conditions are not met, it must be that the
|
|
|
|
|
* adapter data is too big.
|
|
|
|
|
*/
|
|
|
|
|
return uv__new_artificial_error(UV_ENOBUFS);
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
/* Other (unspecified) errors can happen, but we don't have any */
|
|
|
|
|
/* special meaning for them. */
|
|
|
|
|
assert(r != ERROR_SUCCESS);
|
|
|
|
|
return uv__new_sys_error(r);
|
2011-12-15 06:56:53 +00:00
|
|
|
}
|
2011-12-13 09:29:27 +00:00
|
|
|
}
|
|
|
|
|
|
2012-12-04 13:04:37 +00:00
|
|
|
/* Count the number of enabled interfaces and compute how much space is */
|
|
|
|
|
/* needed to store their info. */
|
|
|
|
|
count = 0;
|
|
|
|
|
uv_address_buf_size = 0;
|
|
|
|
|
|
|
|
|
|
for (win_address = win_address_buf;
|
|
|
|
|
win_address != NULL;
|
|
|
|
|
win_address = win_address->Next) {
|
|
|
|
|
/* Use IP_ADAPTER_UNICAST_ADDRESS_XP to retain backwards compatibility */
|
|
|
|
|
/* with Windows XP */
|
|
|
|
|
IP_ADAPTER_UNICAST_ADDRESS_XP* unicast_address;
|
|
|
|
|
int name_size;
|
|
|
|
|
|
|
|
|
|
/* Interfaces that are not 'up' should not be reported. Also skip */
|
|
|
|
|
/* interfaces that have no associated unicast address, as to avoid */
|
|
|
|
|
/* allocating space for the name for this interface. */
|
|
|
|
|
if (win_address->OperStatus != IfOperStatusUp ||
|
|
|
|
|
win_address->FirstUnicastAddress == NULL)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
/* Compute the size of the interface name. */
|
|
|
|
|
name_size = WideCharToMultiByte(CP_UTF8,
|
|
|
|
|
0,
|
|
|
|
|
win_address->FriendlyName,
|
|
|
|
|
-1,
|
|
|
|
|
NULL,
|
|
|
|
|
0,
|
|
|
|
|
NULL,
|
|
|
|
|
FALSE);
|
|
|
|
|
if (name_size <= 0) {
|
|
|
|
|
free(win_address_buf);
|
|
|
|
|
return uv__new_sys_error(GetLastError());
|
|
|
|
|
}
|
|
|
|
|
uv_address_buf_size += name_size;
|
|
|
|
|
|
|
|
|
|
/* Count the number of addresses associated with this interface, and */
|
|
|
|
|
/* compute the 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);
|
|
|
|
|
}
|
2011-12-13 09:29:27 +00:00
|
|
|
}
|
|
|
|
|
|
2012-12-04 13:04:37 +00:00
|
|
|
/* Allocate space to store interface data plus adapter names. */
|
|
|
|
|
uv_address_buf = malloc(uv_address_buf_size);
|
|
|
|
|
if (uv_address_buf == NULL) {
|
|
|
|
|
free(win_address_buf);
|
|
|
|
|
return uv__new_artificial_error(UV_ENOMEM);
|
|
|
|
|
}
|
2011-12-13 09:29:27 +00:00
|
|
|
|
2012-12-04 13:04:37 +00:00
|
|
|
/* Compute the start of the uv_interface_address_t array, and the place in */
|
|
|
|
|
/* the buffer where the interface names will be stored. */
|
|
|
|
|
uv_address = uv_address_buf;
|
|
|
|
|
name_buf = (char*) (uv_address_buf + count);
|
2012-06-09 20:30:18 +00:00
|
|
|
|
2012-12-04 13:04:37 +00:00
|
|
|
/* Fill out the output buffer. */
|
|
|
|
|
for (win_address = win_address_buf;
|
|
|
|
|
win_address != NULL;
|
|
|
|
|
win_address = win_address->Next) {
|
|
|
|
|
IP_ADAPTER_UNICAST_ADDRESS_XP* unicast_address;
|
2013-02-09 22:33:46 +00:00
|
|
|
IP_ADAPTER_PREFIX* prefix;
|
2012-12-04 13:04:37 +00:00
|
|
|
int name_size;
|
|
|
|
|
size_t max_name_size;
|
|
|
|
|
|
|
|
|
|
if (win_address->OperStatus != IfOperStatusUp ||
|
|
|
|
|
win_address->FirstUnicastAddress == NULL)
|
2012-06-09 20:30:18 +00:00
|
|
|
continue;
|
|
|
|
|
|
2012-12-04 13:04:37 +00:00
|
|
|
/* Convert the interface name to UTF8. */
|
|
|
|
|
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__new_sys_error(GetLastError());
|
|
|
|
|
}
|
2011-12-13 09:29:27 +00:00
|
|
|
|
2013-02-09 22:33:46 +00:00
|
|
|
prefix = win_address->FirstPrefix;
|
|
|
|
|
|
2012-12-04 13:04:37 +00:00
|
|
|
/* Add an uv_interface_address_t element for every unicast address. */
|
|
|
|
|
for (unicast_address = (IP_ADAPTER_UNICAST_ADDRESS_XP*)
|
|
|
|
|
win_address->FirstUnicastAddress;
|
|
|
|
|
unicast_address != NULL;
|
|
|
|
|
unicast_address = unicast_address->Next) {
|
|
|
|
|
struct sockaddr* sa;
|
2013-02-09 22:33:46 +00:00
|
|
|
int prefixlen;
|
2011-12-13 09:29:27 +00:00
|
|
|
|
2012-12-04 13:04:37 +00:00
|
|
|
uv_address->name = name_buf;
|
|
|
|
|
|
2013-02-09 22:33:46 +00:00
|
|
|
/* Walk the prefix list in sync with the address list and extract
|
|
|
|
|
the prefixlen for each address. On Vista and newer, we could
|
|
|
|
|
instead just use: unicast_address->OnLinkPrefixLength */
|
|
|
|
|
if (prefix != NULL) {
|
|
|
|
|
prefixlen = prefix->PrefixLength;
|
|
|
|
|
prefix = prefix->Next;
|
|
|
|
|
} else {
|
|
|
|
|
prefixlen = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-04 13:04:37 +00:00
|
|
|
sa = unicast_address->Address.lpSockaddr;
|
2013-02-09 22:33:46 +00:00
|
|
|
if (sa->sa_family == AF_INET6) {
|
|
|
|
|
int i;
|
|
|
|
|
|
2012-12-04 13:04:37 +00:00
|
|
|
uv_address->address.address6 = *((struct sockaddr_in6 *) sa);
|
2013-02-09 22:33:46 +00:00
|
|
|
|
|
|
|
|
uv_address->netmask.netmask6.sin6_family = AF_INET6;
|
|
|
|
|
prefixlen = prefixlen > 0 ? prefixlen : 128;
|
|
|
|
|
for (i = 0; i < 16; ++i) {
|
|
|
|
|
int bits;
|
|
|
|
|
uint8_t byte_val;
|
|
|
|
|
|
|
|
|
|
bits = prefixlen < 8 ? prefixlen : 8;
|
|
|
|
|
byte_val = ~(0xff >> bits);
|
|
|
|
|
prefixlen -= bits;
|
|
|
|
|
|
|
|
|
|
uv_address->netmask.netmask6.sin6_addr.s6_addr[i] = byte_val;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2012-12-04 13:04:37 +00:00
|
|
|
uv_address->address.address4 = *((struct sockaddr_in *) sa);
|
2012-04-12 01:14:34 +00:00
|
|
|
|
2013-02-09 22:33:46 +00:00
|
|
|
uv_address->netmask.netmask4.sin_family = AF_INET;
|
|
|
|
|
prefixlen = prefixlen > 0 ? prefixlen : 32;
|
|
|
|
|
uv_address->netmask.netmask4.sin_addr.s_addr =
|
|
|
|
|
htonl(0xffffffff << (32 - prefixlen));
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-04 13:04:37 +00:00
|
|
|
uv_address->is_internal =
|
|
|
|
|
(win_address->IfType == IF_TYPE_SOFTWARE_LOOPBACK);
|
2011-12-15 06:56:53 +00:00
|
|
|
|
2012-12-04 13:04:37 +00:00
|
|
|
uv_address++;
|
2011-12-15 06:56:53 +00:00
|
|
|
}
|
2012-12-04 13:04:37 +00:00
|
|
|
|
|
|
|
|
name_buf += name_size;
|
2011-12-13 09:29:27 +00:00
|
|
|
}
|
|
|
|
|
|
2012-12-04 13:04:37 +00:00
|
|
|
free(win_address_buf);
|
|
|
|
|
|
|
|
|
|
*addresses_ptr = uv_address_buf;
|
|
|
|
|
*count_ptr = count;
|
2011-12-13 09:29:27 +00:00
|
|
|
|
|
|
|
|
return uv_ok_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void uv_free_interface_addresses(uv_interface_address_t* addresses,
|
|
|
|
|
int count) {
|
|
|
|
|
free(addresses);
|
|
|
|
|
}
|