From 41659f76804df94874469e7fa66ef5d1808753d5 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Wed, 27 Feb 2019 11:28:43 +0000 Subject: win32: make more use of common lazy loading code --- win32/mingw.c | 15 +++++---------- win32/process.c | 22 +++++----------------- 2 files changed, 10 insertions(+), 27 deletions(-) (limited to 'win32') diff --git a/win32/mingw.c b/win32/mingw.c index 378c4001f..f748d2d09 100644 --- a/win32/mingw.c +++ b/win32/mingw.c @@ -921,19 +921,14 @@ clock_t times(struct tms *buf) int link(const char *oldpath, const char *newpath) { - typedef BOOL (WINAPI *T)(const char*, const char*, LPSECURITY_ATTRIBUTES); - static T create_hard_link = NULL; - if (!create_hard_link) { - create_hard_link = (T) GetProcAddress( - GetModuleHandle("kernel32.dll"), "CreateHardLinkA"); - if (!create_hard_link) - create_hard_link = (T)-1; - } - if (create_hard_link == (T)-1) { + DECLARE_PROC_ADDR(BOOL, CreateHardLinkA, LPCSTR, LPCSTR, + LPSECURITY_ATTRIBUTES); + + if (!INIT_PROC_ADDR(kernel32.dll, CreateHardLinkA)) { errno = ENOSYS; return -1; } - if (!create_hard_link(newpath, oldpath, NULL)) { + if (!CreateHardLinkA(newpath, oldpath, NULL)) { errno = err_win_to_posix(GetLastError()); return -1; } diff --git a/win32/process.c b/win32/process.c index 46efdc160..33ae13ab4 100644 --- a/win32/process.c +++ b/win32/process.c @@ -1,6 +1,7 @@ #include "libbb.h" #include #include +#include "lazyload.h" int waitpid(pid_t pid, int *status, int options) { @@ -668,33 +669,20 @@ int kill_SIGTERM_by_handle(HANDLE process, int exit_code) int ret = 0; if (GetExitCodeProcess(process, &code) && code == STILL_ACTIVE) { - static int initialized; - static LPTHREAD_START_ROUTINE exit_process_address; + DECLARE_PROC_ADDR(DWORD, ExitProcess, LPVOID); PVOID arg = (PVOID)(intptr_t)exit_code; DWORD thread_id; HANDLE thread; - if (!initialized) { - HINSTANCE kernel32 = GetModuleHandle("kernel32"); - if (!kernel32) { - fprintf(stderr, "BUG: cannot find kernel32"); - ret = -1; - goto finish; - } - exit_process_address = (LPTHREAD_START_ROUTINE) - GetProcAddress(kernel32, "ExitProcess"); - initialized = 1; - } - if (!exit_process_address || - !process_architecture_matches_current(process)) { + if (!INIT_PROC_ADDR(kernel32, ExitProcess) || + !process_architecture_matches_current(process)) { SetLastError(ERROR_ACCESS_DENIED); ret = -1; goto finish; } if ((thread = CreateRemoteThread(process, NULL, 0, - exit_process_address, - arg, 0, &thread_id))) { + ExitProcess, arg, 0, &thread_id))) { CloseHandle(thread); } } -- cgit v1.2.3-55-g6feb