Fixed some issues on windows
This commit is contained in:
parent
e9f0dfb572
commit
bbb107e8da
1 changed files with 31 additions and 22 deletions
|
@ -8,17 +8,18 @@
|
|||
# include <fcntl.h>
|
||||
# include <winnt.h>
|
||||
# include <setjmp.h>
|
||||
# include <stdint.h>
|
||||
|
||||
# define CREATE_SUSPENDED(Filename, CmdLine, StartupInfo, Info) \
|
||||
CreateProcessW(Filename, \
|
||||
CmdLine(), \
|
||||
NULL, \
|
||||
NULL, \
|
||||
TRUE, \
|
||||
CREATE_SUSPENDED, \
|
||||
NULL, \
|
||||
NULL, \
|
||||
&(StartupInfo), \
|
||||
# define CREATE_SUSPENDED_(Filename, CmdLine, StartupInfo, Info) \
|
||||
CreateProcessW(Filename, \
|
||||
CmdLine, \
|
||||
NULL, \
|
||||
NULL, \
|
||||
TRUE, \
|
||||
CREATE_SUSPENDED, \
|
||||
NULL, \
|
||||
NULL, \
|
||||
&(StartupInfo), \
|
||||
&(Info))
|
||||
|
||||
# ifdef _WIN64
|
||||
|
@ -52,14 +53,14 @@ struct pipe_handle {
|
|||
};
|
||||
|
||||
#ifdef _WIN32
|
||||
# define CONTEXT_INIT {CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS | CONTEXT_FLOATING_POINT}
|
||||
# define CONTEXT_INIT { .ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS | CONTEXT_FLOATING_POINT }
|
||||
struct stack_info {
|
||||
char *ptr;
|
||||
char *base;
|
||||
size_t size;
|
||||
};
|
||||
|
||||
static inline get_stack_info(struct stack_info *stack) {
|
||||
static inline void get_stack_info(struct stack_info *stack) {
|
||||
CONTEXT context = CONTEXT_INIT;
|
||||
MEMORY_BASIC_INFORMATION mbi;
|
||||
|
||||
|
@ -67,8 +68,8 @@ static inline get_stack_info(struct stack_info *stack) {
|
|||
stack->ptr = (char *) Register(sp, context);
|
||||
|
||||
VirtualQuery(stack->ptr, &mbi, sizeof (mbi));
|
||||
stack->base = mbi->BaseAddress;
|
||||
stack->size = mbi->RegionSize;
|
||||
stack->base = mbi.BaseAddress;
|
||||
stack->size = mbi.RegionSize;
|
||||
}
|
||||
|
||||
static g_jmp;
|
||||
|
@ -85,7 +86,7 @@ s_proc_handle *fork_process() {
|
|||
|
||||
ZeroMemory(&info, sizeof (info));
|
||||
|
||||
CONTEXT context = {CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS | CONTEXT_FLOATING_POINT};
|
||||
CONTEXT context = CONTEXT_INIT;
|
||||
|
||||
// Initialize longjmp buffer
|
||||
if (setjmp(g_jmp))
|
||||
|
@ -95,20 +96,20 @@ s_proc_handle *fork_process() {
|
|||
wchar_t filename[MAX_PATH];
|
||||
GetModuleFileNameW(NULL, filename, MAX_PATH);
|
||||
|
||||
if (!CREATE_SUSPENDED(filename, GetCommandLineW(), si, info))
|
||||
if (!CREATE_SUSPENDED_(filename, GetCommandLineW(), si, info))
|
||||
return NULL;
|
||||
|
||||
// Set child's instruction pointer to resume_child
|
||||
GetThreadContext(info.hThread, &context);
|
||||
Register(ip, context) = resume_child;
|
||||
Register(ip, context) = (intptr_t) resume_child;
|
||||
SetThreadContext(info.hThread, &context);
|
||||
|
||||
// Copy stack
|
||||
struct stack_info stack;
|
||||
get_stack_info(&stack);
|
||||
|
||||
VirtualAllocEx(info.hProcess, stack->base, stack->size, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
|
||||
WriteProcessMemory(info.hProcess, stack->ptr, stack->ptr, stack->base + stack->size - stack->ptr, NULL);
|
||||
VirtualAllocEx(info.hProcess, stack.base, stack.size, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
|
||||
WriteProcessMemory(info.hProcess, stack.ptr, stack.ptr, stack.base + stack.size - stack.ptr, NULL);
|
||||
|
||||
ResumeThread(info.hThread);
|
||||
CloseHandle(info.hThread);
|
||||
|
@ -139,10 +140,10 @@ void wait_process(s_proc_handle *handle, int *status) {
|
|||
FILE *pipe_in(s_pipe_handle *p) {
|
||||
#ifdef _WIN32
|
||||
CloseHandle(p->fhs[1]);
|
||||
int fd = _open_osfhandle(p->fhs[0], _O_RDONLY);
|
||||
int fd = _open_osfhandle((intptr_t) p->fhs[0], _O_RDONLY);
|
||||
if (fd == -1)
|
||||
return NULL;
|
||||
FILE *in = _fdopen(, "r");
|
||||
FILE *in = _fdopen(fd, "r");
|
||||
#else
|
||||
close(p->fds[1]);
|
||||
FILE *in = fdopen(p->fds[0], "r");
|
||||
|
@ -157,7 +158,7 @@ FILE *pipe_in(s_pipe_handle *p) {
|
|||
FILE *pipe_out(s_pipe_handle *p) {
|
||||
#ifdef _WIN32
|
||||
CloseHandle(p->fhs[0]);
|
||||
int fd = _open_osfhandle(p->fhs[1], _O_WRONLY);
|
||||
int fd = _open_osfhandle((intptr_t) p->fhs[1], _O_WRONLY);
|
||||
if (fd == -1)
|
||||
return NULL;
|
||||
FILE *out = _fdopen(fd, "w");
|
||||
|
@ -187,9 +188,17 @@ s_pipe_handle *stdpipe() {
|
|||
}
|
||||
|
||||
s_proc_handle *get_current_process() {
|
||||
#ifdef _WIN32
|
||||
return unique_ptr(s_proc_handle, { GetCurrentProcess() });
|
||||
#else
|
||||
return unique_ptr(s_proc_handle, { getpid() });
|
||||
#endif
|
||||
}
|
||||
|
||||
bool is_current_process(s_proc_handle *proc) {
|
||||
#ifdef _WIN32
|
||||
return GetProcessId(proc->handle) == GetProcessId(GetCurrentProcess());
|
||||
#else
|
||||
return proc->pid == getpid();
|
||||
#endif
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue