116 lines
3.2 KiB
C
116 lines
3.2 KiB
C
#include <stdint.h>
|
|
#include <stddef.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <ntlite/syscall.h>
|
|
|
|
#define EVENT_AUTO_RESET 0u
|
|
|
|
static void print(const char* s) {
|
|
SYS_WRITE_CONSOLE_REQUEST req = { SysSvFromCstr(s) };
|
|
SysWriteConsole(&req);
|
|
}
|
|
|
|
static uint32_t open_file(const char* path, uint32_t* handleOut) {
|
|
SYS_PATH_HANDLE_REQUEST req = { SysSvFromCstr(path), handleOut };
|
|
return SysOpenFile(&req);
|
|
}
|
|
|
|
static void print_u64(uint64_t v) {
|
|
char buf[32];
|
|
int i = 0;
|
|
if (v == 0) {
|
|
print("0");
|
|
return;
|
|
}
|
|
while (v && i < (int)sizeof(buf)) {
|
|
buf[i++] = (char)('0' + (v % 10));
|
|
v /= 10;
|
|
}
|
|
while (i--) {
|
|
char c = buf[i];
|
|
do { SYS_WRITE_CONSOLE_REQUEST req = { SysSvFromParts(&c, 1) }; SysWriteConsole(&req); } while (0);
|
|
}
|
|
}
|
|
|
|
int main(void) {
|
|
print("async_cancel: start\n");
|
|
|
|
uint32_t file = 0;
|
|
uint32_t event = 0;
|
|
uint32_t request = 0;
|
|
|
|
uint32_t s = open_file("system/init.exe", &file);
|
|
if (s != SYS_STATUS_SUCCESS) {
|
|
print("async_cancel: open failed status="); print_u64(s); print("\n");
|
|
SysExitProcess(1);
|
|
}
|
|
|
|
s = SysEventCreate(EVENT_AUTO_RESET, 0, &event);
|
|
if (s != SYS_STATUS_SUCCESS) {
|
|
print("async_cancel: event create failed status="); print_u64(s); print("\n");
|
|
SysExitProcess(1);
|
|
}
|
|
|
|
uint8_t* buffer = (uint8_t*)malloc(512);
|
|
if (!buffer) {
|
|
print("async_cancel: alloc failed\n");
|
|
SysExitProcess(1);
|
|
}
|
|
memset(buffer, 0, 512);
|
|
|
|
SYS_IO_STATUS_BLOCK iosb;
|
|
iosb.Status = 0;
|
|
iosb.Information = 0;
|
|
|
|
SYS_ASYNC_FILE_REQUEST req;
|
|
memset(&req, 0, sizeof(req));
|
|
req.FileHandle = file;
|
|
req.EventHandle = event;
|
|
req.Offset = 0;
|
|
req.Buffer = buffer;
|
|
req.Size = 512;
|
|
req.Iosb = &iosb;
|
|
req.RequestHandleOut = &request;
|
|
|
|
s = SysReadFileAsync(&req);
|
|
if (s != SYS_STATUS_PENDING || request == 0) {
|
|
print("async_cancel: submit failed status="); print_u64(s);
|
|
print(" request="); print_u64(request); print("\n");
|
|
SysExitProcess(1);
|
|
}
|
|
|
|
uint32_t cs = SysCancelIo(request);
|
|
print("async_cancel: cancel returned "); print_u64(cs); print("\n");
|
|
|
|
while (iosb.Status == SYS_STATUS_PENDING) {
|
|
s = SysEventWait(event, 1);
|
|
print("async_cancel: wait returned "); print_u64(s); print("\n");
|
|
if (s != SYS_STATUS_SUCCESS && s != SYS_STATUS_KERNEL_APC && s != SYS_STATUS_USER_APC && s != SYS_STATUS_ALERTED)
|
|
break;
|
|
}
|
|
|
|
print("async_cancel: iosb status="); print_u64(iosb.Status);
|
|
print(" info="); print_u64(iosb.Information); print("\n");
|
|
|
|
// If the worker had already started, cancellation is best-effort and the
|
|
// request may complete successfully. If it was still queued, it must report
|
|
// SYS_STATUS_CANCELLED. Both outcomes are valid; SYS_STATUS_PENDING is not.
|
|
uint32_t ok = (iosb.Status == SYS_STATUS_CANCELLED) ||
|
|
(iosb.Status == SYS_STATUS_SUCCESS && iosb.Information == 512);
|
|
|
|
SysCloseHandle(request);
|
|
SysCloseHandle(event);
|
|
SysCloseHandle(file);
|
|
free(buffer);
|
|
|
|
if (!ok) {
|
|
print("async_cancel: FAIL\n");
|
|
SysExitProcess(1);
|
|
}
|
|
|
|
print("async_cancel: PASS\n");
|
|
SysExitProcess(0);
|
|
return 0;
|
|
}
|