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.h | |
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.h')
-rw-r--r-- | jimiocompat.h | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/jimiocompat.h b/jimiocompat.h index 176a5fe..e44e6d5 100644 --- a/jimiocompat.h +++ b/jimiocompat.h @@ -41,10 +41,8 @@ int Jim_OpenForRead(const char *filename); #include <io.h> #include <process.h> - typedef HANDLE pidtype; - #define JIM_BAD_PID INVALID_HANDLE_VALUE - /* Note that this isn't a separate value on Windows since we don't have os.fork */ - #define JIM_NO_PID INVALID_HANDLE_VALUE + typedef HANDLE phandle_t; + #define JIM_BAD_PHANDLE INVALID_HANDLE_VALUE /* These seem to accord with the conventions used by msys/mingw32 */ #define WIFEXITED(STATUS) (((STATUS) & 0xff00) == 0) @@ -57,7 +55,12 @@ int Jim_OpenForRead(const char *filename); * Unix-compatible errno */ int Jim_Errno(void); - pidtype waitpid(pidtype pid, int *status, int nohang); + + long waitpid(phandle_t phandle, int *status, int nohang); + /* Like waitpid() but takes a pid and returns a phandle */ + phandle_t JimWaitPid(long processid, int *status, int nohang); + /* Return pid for a phandle */ + long JimProcessPid(phandle_t phandle); #define HAVE_PIPE #define pipe(P) _pipe((P), 0, O_NOINHERIT) @@ -74,10 +77,11 @@ int Jim_OpenForRead(const char *filename); #include <fcntl.h> #include <sys/wait.h> - typedef int pidtype; + typedef int phandle_t; #define Jim_Errno() errno - #define JIM_BAD_PID -1 - #define JIM_NO_PID 0 + #define JIM_BAD_PHANDLE -1 + #define JimProcessPid(PIDTYPE) (PIDTYPE) + #define JimWaitPid waitpid #ifndef HAVE_EXECVPE #define execvpe(ARG0, ARGV, ENV) execvp(ARG0, ARGV) |