136 lines
7.1 KiB
C
136 lines
7.1 KiB
C
/* Standalone diagnostic only. The original C/Auth/MIDL inputs are linked
|
|
* unchanged. Exactly one trace query, only under this process's SYSTEM token.
|
|
* No clock setter, token impersonation, NTP/ACL/provider or product gate. */
|
|
#define _WIN32_WINNT 0x0a00
|
|
#include <windows.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <wchar.h>
|
|
|
|
typedef struct {
|
|
uint32_t stage, native_rpc_return, native_rpc_return_present, rpc_exception;
|
|
uint32_t status_present, ul_size, c_entries, p_entries_present;
|
|
} RmmClockTrace;
|
|
_Static_assert(sizeof(RmmClockTrace) == 32, "same diagnostic trace ABI");
|
|
typedef struct {
|
|
uint32_t generating_component, status, detection_location, flags;
|
|
int32_t number_of_parameters;
|
|
} RmmClockEeRecord;
|
|
typedef struct {
|
|
uint32_t capture_attempted, start_status, next_status, end_status, count, limit_reached;
|
|
RmmClockEeRecord records[4];
|
|
} RmmClockEeInfo;
|
|
_Static_assert(sizeof(RmmClockEeInfo) == 104, "same four-record numeric EEInfo ABI");
|
|
uint32_t rmm_clock_query_trace_eeinfo(RmmClockTrace *trace, RmmClockEeInfo *eeinfo);
|
|
|
|
static WCHAR service_name[] = L"RmmClockSystemEeInfoDiagnostic";
|
|
static WCHAR case_id[37];
|
|
static SERVICE_STATUS_HANDLE status_handle;
|
|
static SERVICE_STATUS service_status;
|
|
|
|
static DWORD WINAPI control(DWORD event, DWORD type, void *data, void *context) {
|
|
(void)event; (void)type; (void)data; (void)context;
|
|
return ERROR_CALL_NOT_IMPLEMENTED;
|
|
}
|
|
|
|
static DWORD own_system_token(void) {
|
|
HANDLE token = NULL;
|
|
DWORD needed = 0, result = ERROR_SUCCESS;
|
|
_Alignas(8) BYTE storage[4096];
|
|
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token)) return GetLastError();
|
|
if (!GetTokenInformation(token, TokenUser, storage, sizeof(storage), &needed)) result = GetLastError();
|
|
else if (needed > sizeof(storage) || !IsWellKnownSid(((TOKEN_USER *)storage)->User.Sid, WinLocalSystemSid)) result = ERROR_ACCESS_DENIED;
|
|
CloseHandle(token);
|
|
return result;
|
|
}
|
|
|
|
static DWORD write_result(uint32_t rpc, const RmmClockTrace *trace, const RmmClockEeInfo *eeinfo) {
|
|
WCHAR path[256];
|
|
char id[37], report[4096];
|
|
DWORD written = 0, result = ERROR_SUCCESS;
|
|
for (unsigned i = 0; i < 37; ++i) id[i] = (char)case_id[i];
|
|
int count = swprintf(path, sizeof(path) / sizeof(path[0]),
|
|
L"C:\\ProgramData\\RustDiscoveryBootstrap\\clock-eeinfo-trace-%ls\\result.json", case_id);
|
|
if (count <= 0 || count >= (int)(sizeof(path) / sizeof(path[0]))) return ERROR_INVALID_PARAMETER;
|
|
count = snprintf(report, sizeof(report),
|
|
"{\"schema_version\":1,\"profile\":\"clock-system-eeinfo-v1\",\"case_id\":\"%s\","
|
|
"\"token_system\":true,\"read_only\":true,\"diagnostic_only\":true,\"query_invocations\":1,"
|
|
"\"clock_qualified\":false,\"adapter_qualified\":false,\"product_gate_open\":false,\"rpc_code\":%lu,"
|
|
"\"trace\":{\"stage\":%lu,\"native_rpc_return\":%lu,\"native_rpc_return_present\":%lu,"
|
|
"\"rpc_exception\":%lu,\"status_present\":%lu,\"ul_size\":%lu,\"c_entries\":%lu,\"p_entries_present\":%lu}}\n",
|
|
id, (unsigned long)rpc, (unsigned long)trace->stage,
|
|
(unsigned long)trace->native_rpc_return, (unsigned long)trace->native_rpc_return_present,
|
|
(unsigned long)trace->rpc_exception, (unsigned long)trace->status_present,
|
|
(unsigned long)trace->ul_size, (unsigned long)trace->c_entries, (unsigned long)trace->p_entries_present);
|
|
if (count <= 0 || count >= (int)sizeof(report)) return ERROR_BUFFER_OVERFLOW;
|
|
if (count < 3 || eeinfo->count > 4) return ERROR_INVALID_DATA;
|
|
/* Replace only the final root-object closure; original trace stays intact. */
|
|
size_t used = (size_t)count - 2;
|
|
int appended = snprintf(report + used, sizeof(report) - used,
|
|
",\"eeinfo\":{\"capture_attempted\":%lu,\"start_status\":%lu,\"next_status\":%lu,"
|
|
"\"end_status\":%lu,\"count\":%lu,\"limit_reached\":%lu,\"records\":[",
|
|
(unsigned long)eeinfo->capture_attempted, (unsigned long)eeinfo->start_status,
|
|
(unsigned long)eeinfo->next_status, (unsigned long)eeinfo->end_status,
|
|
(unsigned long)eeinfo->count, (unsigned long)eeinfo->limit_reached);
|
|
if (appended <= 0 || (size_t)appended >= sizeof(report) - used) return ERROR_BUFFER_OVERFLOW;
|
|
used += (size_t)appended;
|
|
for (unsigned i = 0; i < eeinfo->count; ++i) {
|
|
const RmmClockEeRecord *record = &eeinfo->records[i];
|
|
appended = snprintf(report + used, sizeof(report) - used,
|
|
"%s{\"generating_component\":%lu,\"status\":%lu,\"detection_location\":%lu,"
|
|
"\"flags\":%lu,\"number_of_parameters\":%ld}", i ? "," : "",
|
|
(unsigned long)record->generating_component, (unsigned long)record->status,
|
|
(unsigned long)record->detection_location, (unsigned long)record->flags,
|
|
(long)record->number_of_parameters);
|
|
if (appended <= 0 || (size_t)appended >= sizeof(report) - used) return ERROR_BUFFER_OVERFLOW;
|
|
used += (size_t)appended;
|
|
}
|
|
appended = snprintf(report + used, sizeof(report) - used, "]}}\n");
|
|
if (appended <= 0 || (size_t)appended >= sizeof(report) - used) return ERROR_BUFFER_OVERFLOW;
|
|
used += (size_t)appended;
|
|
count = (int)used;
|
|
HANDLE file = CreateFileW(path, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_NEW,
|
|
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH, NULL);
|
|
if (file == INVALID_HANDLE_VALUE) return GetLastError();
|
|
if (!WriteFile(file, report, (DWORD)count, &written, NULL) || written != (DWORD)count) result = ERROR_WRITE_FAULT;
|
|
else if (!FlushFileBuffers(file)) result = GetLastError();
|
|
CloseHandle(file);
|
|
return result;
|
|
}
|
|
|
|
static void WINAPI run_service(DWORD argc, WCHAR **argv) {
|
|
(void)argc; (void)argv;
|
|
status_handle = RegisterServiceCtrlHandlerExW(service_name, control, NULL);
|
|
if (!status_handle) return;
|
|
ZeroMemory(&service_status, sizeof(service_status));
|
|
service_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
|
|
service_status.dwCurrentState = SERVICE_RUNNING;
|
|
service_status.dwControlsAccepted = 0;
|
|
if (!SetServiceStatus(status_handle, &service_status)) return;
|
|
DWORD result = own_system_token();
|
|
if (!result) {
|
|
RmmClockTrace trace = {0};
|
|
RmmClockEeInfo eeinfo = {0};
|
|
uint32_t rpc = rmm_clock_query_trace_eeinfo(&trace, &eeinfo); /* exactly one fixed call */
|
|
result = write_result(rpc, &trace, &eeinfo);
|
|
}
|
|
service_status.dwCurrentState = SERVICE_STOPPED;
|
|
service_status.dwWin32ExitCode = result;
|
|
SetServiceStatus(status_handle, &service_status);
|
|
}
|
|
|
|
int wmain(int argc, WCHAR **argv) {
|
|
if (argc != 3 || wcscmp(argv[1], L"--service") || wcslen(argv[2]) != 36) return ERROR_INVALID_PARAMETER;
|
|
for (unsigned i = 0; i < 36; ++i) {
|
|
WCHAR value = argv[2][i];
|
|
if (i == 8 || i == 13 || i == 18 || i == 23) {
|
|
if (value != L'-') return ERROR_INVALID_PARAMETER;
|
|
} else if (!((value >= L'0' && value <= L'9') || (value >= L'a' && value <= L'f'))) return ERROR_INVALID_PARAMETER;
|
|
case_id[i] = value;
|
|
}
|
|
case_id[36] = 0;
|
|
SERVICE_TABLE_ENTRYW table[] = {{service_name, run_service}, {NULL, NULL}};
|
|
if (!StartServiceCtrlDispatcherW(table)) return (int)GetLastError();
|
|
return 0;
|
|
}
|