Thread (47 messages) flat view 47 messages, 3 authors, 2016-06-15
STALE3733d

[PATCH v2 06/14] mingw: use real pid

From: Erik Faye-Lund <hidden>
Date: 2016-06-15 22:48:02
Subsystem: the rest · Maintainer: Linus Torvalds

The Windows port so far used process handles as PID. However,
this does not work consistently with getpid.

Change the code to use the real PID, and use OpenProcess to
get a process-handle.

Signed-off-by: Erik Faye-Lund <redacted>
---
 compat/mingw.c |    2 +-
 compat/mingw.h |   35 +++++++++++++++++++++++++++++++----
 2 files changed, 32 insertions(+), 5 deletions(-)
diff --git a/compat/mingw.c b/compat/mingw.c
index 54be905..ce4f829 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -729,7 +729,7 @@ static pid_t mingw_spawnve(const char *cmd, const char **argv, char **env,
 		return -1;
 	}
 	CloseHandle(pi.hThread);
-	return (pid_t)pi.hProcess;
+	return (pid_t)pi.dwProcessId;
 }
 
 pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **env)
diff --git a/compat/mingw.h b/compat/mingw.h
index 3005472..ff4a76b 100644
--- a/compat/mingw.h
+++ b/compat/mingw.h
@@ -137,14 +137,41 @@ static inline int mingw_unlink(const char *pathname)
 #define WNOHANG 1
 static inline int waitpid(pid_t pid, int *status, unsigned options)
 {
-	if (pid > 0 && options & WNOHANG) {
-		if (WAIT_OBJECT_0 != WaitForSingleObject((HANDLE)pid, 0))
+	HANDLE h;
+
+	if (pid <= 0) {
+		errno = EINVAL;
+		return -1;
+	}
+
+	h = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION, FALSE, pid);
+	if (!h) {
+		errno = ECHILD;
+		return -1;
+	}
+
+	if (options & WNOHANG) {
+		if (WaitForSingleObject(h, 0) != WAIT_OBJECT_0) {
+			CloseHandle(h);
 			return 0;
+		}
 		options &= ~WNOHANG;
 	}
 
-	if (options == 0)
-		return _cwait(status, pid, 0);
+	if (options == 0) {
+		if (WaitForSingleObject(h, INFINITE) != WAIT_OBJECT_0) {
+			CloseHandle(h);
+			return 0;
+		}
+
+		if (status)
+			GetExitCodeProcess(h, (LPDWORD)status);
+
+		CloseHandle(h);
+		return pid;
+	}
+	CloseHandle(h);
+
 	errno = EINVAL;
 	return -1;
 }
-- 
1.6.6.211.g26720
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help