From c6719a2784e998d47713de78d284eec5ba8fd976 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Sun, 30 May 2004 03:50:15 +0000
Subject: [PATCH] Implement new PostmasterIsAlive() check for WIN32, per
 Claudio Natoli. In passing, align a few error messages with the style guide.

---
 src/backend/postmaster/postmaster.c | 51 +++++++++++++++++++++--------
 src/backend/storage/ipc/pmsignal.c  |  8 ++---
 src/include/postmaster/postmaster.h |  6 +++-
 3 files changed, 45 insertions(+), 20 deletions(-)

diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index e7186c01b39..6a0cfbf052b 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -37,7 +37,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.400 2004/05/29 22:48:19 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.401 2004/05/30 03:50:11 tgl Exp $
  *
  * NOTES
  *
@@ -276,6 +276,8 @@ static DWORD WINAPI win32_sigchld_waiter(LPVOID param);
 static pid_t *win32_childPIDArray;
 static HANDLE *win32_childHNDArray;
 static unsigned long win32_numChildren = 0;
+
+HANDLE PostmasterHandle;
 #endif
 
 static pid_t backend_forkexec(Port *port);
@@ -748,6 +750,21 @@ PostmasterMain(int argc, char *argv[])
 		ereport(FATAL,
 				(errcode(ERRCODE_OUT_OF_MEMORY),
 				 errmsg("out of memory")));
+
+	/*
+	 * Set up a handle that child processes can use to check whether the
+	 * postmaster is still running.
+	 */
+	if (DuplicateHandle(GetCurrentProcess(),
+						GetCurrentProcess(),
+						GetCurrentProcess(),
+						&PostmasterHandle,
+						0,
+						TRUE,
+						DUPLICATE_SAME_ACCESS) == 0)
+		ereport(FATAL,
+				(errmsg_internal("could not duplicate postmaster handle: %d",
+								 (int) GetLastError())));
 #endif
 
 	/*
@@ -3221,6 +3238,9 @@ write_backend_variables(char *filename, Port *port)
 
 	write_var(debug_flag, fp);
 	write_var(PostmasterPid, fp);
+#ifdef WIN32
+	write_var(PostmasterHandle, fp);
+#endif
 
 	StrNCpy(str_buf, my_exec_path, MAXPGPATH);
 	write_array_var(str_buf, fp);
@@ -3289,6 +3309,9 @@ read_backend_variables(char *filename, Port *port)
 
 	read_var(debug_flag, fp);
 	read_var(PostmasterPid, fp);
+#ifdef WIN32
+	read_var(PostmasterHandle, fp);
+#endif
 
 	read_array_var(str_buf, fp);
 	StrNCpy(my_exec_path, str_buf, MAXPGPATH);
@@ -3360,7 +3383,7 @@ ShmemBackendArrayRemove(pid_t pid)
 	}
 
 	ereport(WARNING,
-			(errmsg_internal("unable to find backend entry with pid %d",
+			(errmsg_internal("could not find backend entry with pid %d",
 							 (int) pid)));
 }
 
@@ -3411,22 +3434,22 @@ win32_forkexec(const char *path, char *argv[])
 		win32_AddChild(pi.dwProcessId, pi.hProcess);
 	}
 
-	if (!DuplicateHandle(GetCurrentProcess(),
-						 pi.hProcess,
-						 GetCurrentProcess(),
-						 &childHandleCopy,
-						 0,
-						 FALSE,
-						 DUPLICATE_SAME_ACCESS))
+	if (DuplicateHandle(GetCurrentProcess(),
+						pi.hProcess,
+						GetCurrentProcess(),
+						&childHandleCopy,
+						0,
+						FALSE,
+						DUPLICATE_SAME_ACCESS) == 0)
 		ereport(FATAL,
-				(errmsg_internal("failed to duplicate child handle: %d",
+				(errmsg_internal("could not duplicate child handle: %d",
 								 (int) GetLastError())));
 
 	waiterThread = CreateThread(NULL, 64 * 1024, win32_sigchld_waiter,
 								(LPVOID) childHandleCopy, 0, NULL);
 	if (!waiterThread)
 		ereport(FATAL,
-				(errmsg_internal("failed to create sigchld waiter thread: %d",
+				(errmsg_internal("could not create sigchld waiter thread: %d",
 								 (int) GetLastError())));
 	CloseHandle(waiterThread);
 
@@ -3460,7 +3483,7 @@ win32_AddChild(pid_t pid, HANDLE handle)
 	}
 	else
 		ereport(FATAL,
-				(errmsg_internal("unable to add child entry with pid %lu",
+				(errmsg_internal("no room for child entry with pid %lu",
 								 (unsigned long) pid)));
 }
 
@@ -3486,7 +3509,7 @@ win32_RemoveChild(pid_t pid)
 	}
 
 	ereport(WARNING,
-			(errmsg_internal("unable to find child entry with pid %lu",
+			(errmsg_internal("could not find child entry with pid %lu",
 							 (unsigned long) pid)));
 }
 
@@ -3562,7 +3585,7 @@ win32_sigchld_waiter(LPVOID param)
 	if (r == WAIT_OBJECT_0)
 		pg_queue_signal(SIGCHLD);
 	else
-		fprintf(stderr, "ERROR: Failed to wait on child process handle: %i\n",
+		fprintf(stderr, "ERROR: failed to wait on child process handle: %d\n",
 				(int) GetLastError());
 	CloseHandle(procHandle);
 	return 0;
diff --git a/src/backend/storage/ipc/pmsignal.c b/src/backend/storage/ipc/pmsignal.c
index 4efa4adac6f..c8eaa63c114 100644
--- a/src/backend/storage/ipc/pmsignal.c
+++ b/src/backend/storage/ipc/pmsignal.c
@@ -8,7 +8,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/storage/ipc/pmsignal.c,v 1.14 2004/05/29 22:48:20 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/storage/ipc/pmsignal.c,v 1.15 2004/05/30 03:50:14 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -18,6 +18,7 @@
 #include <unistd.h>
 
 #include "miscadmin.h"
+#include "postmaster/postmaster.h"
 #include "storage/pmsignal.h"
 #include "storage/shmem.h"
 
@@ -115,9 +116,6 @@ PostmasterIsAlive(bool amDirectChild)
 		return (kill(PostmasterPid, 0) == 0);
 	}
 #else /* WIN32 */
-	/*
-	 * XXX needs to be implemented by somebody
-	 */
-	return true;
+	return (WaitForSingleObject(PostmasterHandle, 0) == WAIT_TIMEOUT);
 #endif /* WIN32 */
 }
diff --git a/src/include/postmaster/postmaster.h b/src/include/postmaster/postmaster.h
index d349018f4b5..301f7652ffe 100644
--- a/src/include/postmaster/postmaster.h
+++ b/src/include/postmaster/postmaster.h
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/postmaster/postmaster.h,v 1.1 2004/05/29 22:48:23 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/postmaster/postmaster.h,v 1.2 2004/05/30 03:50:15 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -30,6 +30,10 @@ extern bool Log_connections;
 extern bool log_hostname;
 extern char *rendezvous_name;
 
+#ifdef WIN32
+extern HANDLE PostmasterHandle;
+#endif
+
 
 extern int	PostmasterMain(int argc, char *argv[]);
 extern void ClosePostmasterPorts(void);
-- 
GitLab