diff options
author | Steve Bennett <steveb@workware.net.au> | 2022-04-14 08:10:41 +1000 |
---|---|---|
committer | Steve Bennett <steveb@workware.net.au> | 2022-04-14 11:13:02 +1000 |
commit | 703ac20adb9daff423eea44f4af548f485a97254 (patch) | |
tree | df2028559e32c5cf4988828e127ddaace7dae359 /jimiocompat.c | |
parent | 5b967d60f4f5277e0f843d15fc54c0cb906a1bc2 (diff) | |
download | jimtcl-703ac20adb9daff423eea44f4af548f485a97254.zip jimtcl-703ac20adb9daff423eea44f4af548f485a97254.tar.gz jimtcl-703ac20adb9daff423eea44f4af548f485a97254.tar.bz2 |
win32: Fix process handle vs pid distinction
On win32, a process is identified by a HANDLE, but for identifying
a running process we should use GetProcessId() to return a meaningful integer.
This means we need to be able to back and forth between a pid and a process handle
(phandle). We also need to be careful to get the pid before the process handle closes
since it isn't available afterwards.
Also call the handle to intptr_t for open_osfhandle() to avoid a compiler warning.
Fixes #217
Signed-off-by: Steve Bennett <steveb@workware.net.au>
Diffstat (limited to 'jimiocompat.c')
-rw-r--r-- | jimiocompat.c | 44 |
1 files changed, 38 insertions, 6 deletions
diff --git a/jimiocompat.c b/jimiocompat.c index c8c3f86..665a65a 100644 --- a/jimiocompat.c +++ b/jimiocompat.c @@ -90,16 +90,48 @@ int Jim_Errno(void) return EINVAL; } -pidtype waitpid(pidtype pid, int *status, int nohang) +long JimProcessPid(phandle_t pid) { - DWORD ret = WaitForSingleObject(pid, nohang ? 0 : INFINITE); + if (pid == INVALID_HANDLE_VALUE) { + return -1; + } + return GetProcessId(pid); +} + +/** + * Returns the phandle of the process identified by 'pid' or JIM_BAD_PHANDLE on error. + * Note that on success, the handle will no longer be valid. + * It can only be used as a token (e.g. to look up the wait table) + */ +phandle_t JimWaitPid(long pid, int *status, int nohang) +{ + HANDLE h = OpenProcess(PROCESS_QUERY_INFORMATION | SYNCHRONIZE, FALSE, pid); + if (h) { + long pid = waitpid(h, status, nohang); + CloseHandle(h); + if (pid > 0) { + return h; + } + } + return JIM_BAD_PHANDLE; +} + +/** + * Returns the pid of the process if OK or -1 on error. + */ +long waitpid(phandle_t phandle, int *status, int nohang) +{ + long pid; + DWORD ret = WaitForSingleObject(phandle, nohang ? 0 : INFINITE); if (ret == WAIT_TIMEOUT || ret == WAIT_FAILED) { /* WAIT_TIMEOUT can only happend with WNOHANG */ - return JIM_BAD_PID; + return -1; } - GetExitCodeProcess(pid, &ret); + GetExitCodeProcess(phandle, &ret); *status = ret; - CloseHandle(pid); + /* We won't be able to get this after we close the handle */ + pid = GetProcessId(phandle); + CloseHandle(phandle); return pid; } @@ -121,7 +153,7 @@ int Jim_MakeTempFile(Jim_Interp *interp, const char *filename_template, int unli } Jim_SetResultString(interp, name, -1); - return _open_osfhandle((int)handle, _O_RDWR | _O_TEXT); + return _open_osfhandle((intptr_t)handle, _O_RDWR | _O_TEXT); error: Jim_SetResultErrno(interp, name); |