From bbb107e8da85608b68c9bcfc97901ebf10999d15 Mon Sep 17 00:00:00 2001 From: Snaipe Date: Wed, 25 Mar 2015 20:23:06 +0100 Subject: [PATCH] Fixed some issues on windows --- src/posix-compat.c | 53 +++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/src/posix-compat.c b/src/posix-compat.c index 236f4f8..7c8b8ea 100644 --- a/src/posix-compat.c +++ b/src/posix-compat.c @@ -8,17 +8,18 @@ # include # include # include +# include -# 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 }