Index: src/backend/postmaster/pgstat.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/backend/postmaster/pgstat.c,v retrieving revision 1.53 diff -c -r1.53 pgstat.c *** src/backend/postmaster/pgstat.c 11 Jan 2004 03:49:31 -0000 1.53 --- src/backend/postmaster/pgstat.c 12 Jan 2004 07:32:30 -0000 *************** *** 147,153 **** #define piperead(a,b,c) read(a,b,c) #define pipewrite(a,b,c) write(a,b,c) #else ! /* pgpipe() is in /src/port */ #define piperead(a,b,c) recv(a,b,c,0) #define pipewrite(a,b,c) send(a,b,c,0) #endif --- 147,153 ---- #define piperead(a,b,c) read(a,b,c) #define pipewrite(a,b,c) write(a,b,c) #else ! extern int pgpipe(int handles[2]); /* pgpipe() is in /src/port */ #define piperead(a,b,c) recv(a,b,c,0) #define pipewrite(a,b,c) send(a,b,c,0) #endif *************** *** 322,328 **** /* * Create the pipe that controls the statistics collector shutdown */ ! if (pipe(pgStatPmPipe) < 0) { ereport(LOG, (errcode_for_socket_access(), --- 322,328 ---- /* * Create the pipe that controls the statistics collector shutdown */ ! if (pgpipe(pgStatPmPipe) < 0) { ereport(LOG, (errcode_for_socket_access(), Index: src/backend/postmaster/postmaster.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/backend/postmaster/postmaster.c,v retrieving revision 1.358 diff -c -r1.358 postmaster.c *** src/backend/postmaster/postmaster.c 11 Jan 2004 03:49:31 -0000 1.358 --- src/backend/postmaster/postmaster.c 12 Jan 2004 07:32:32 -0000 *************** *** 141,146 **** --- 141,151 ---- static Dllist *BackendList; + #ifdef EXEC_BACKEND + #define NUM_BACKENDARRAY_ELEMS (2*MaxBackends) + static Backend *ShmemBackendArray; + #endif + /* The socket number we are listening for connections on */ int PostPortNumber; char *UnixSocketDir; *************** *** 299,304 **** --- 304,317 ---- #ifdef EXEC_BACKEND #ifdef WIN32 pid_t win32_forkexec(const char* path, char *argv[]); + + static void win32_AddChild(pid_t pid, HANDLE handle); + static void win32_RemoveChild(pid_t pid); + static pid_t win32_waitpid(int *exitstatus); + + static pid_t *win32_childPIDArray; + static HANDLE *win32_childHNDArray; + static unsigned long win32_numChildren = 0; #endif static pid_t Backend_forkexec(Port *port); *************** *** 306,311 **** --- 319,329 ---- static unsigned long tmpBackendFileNum = 0; void read_backend_variables(unsigned long id, Port *port); static bool write_backend_variables(Port *port); + + size_t ShmemBackendArraySize(void); + void ShmemBackendArrayAllocation(void); + static void ShmemBackendArrayAdd(Backend *bn); + static void ShmemBackendArrayRemove(pid_t pid); #endif #define StartupDataBase() SSDataBase(BS_XLOG_STARTUP) *************** *** 430,436 **** */ umask((mode_t) 0077); ! MyProcPid = getpid(); /* * Fire up essential subsystems: memory management --- 448,454 ---- */ umask((mode_t) 0077); ! MyProcPid = PostmasterPid = getpid(); /* * Fire up essential subsystems: memory management *************** *** 825,830 **** --- 843,863 ---- */ BackendList = DLNewList(); + #ifdef WIN32 + /* + * Initialize the child pid/HANDLE arrays + */ + /* FIXME: [fork/exec] Ideally, we would resize these arrays with changes + * in MaxBackends, but this'll do as a first order solution. + */ + win32_childPIDArray = (HANDLE*)malloc(NUM_BACKENDARRAY_ELEMS*sizeof(pid_t)); + win32_childHNDArray = (HANDLE*)malloc(NUM_BACKENDARRAY_ELEMS*sizeof(HANDLE)); + if (!win32_childPIDArray || !win32_childHNDArray) + ereport(LOG, + (errcode(ERRCODE_OUT_OF_MEMORY), + errmsg("out of memory"))); + #endif + /* * Record postmaster options. We delay this till now to avoid * recording bogus options (eg, NBuffers too high for available *************** *** 1257,1267 **** if (proto == CANCEL_REQUEST_CODE) { - #ifdef EXEC_BACKEND - abort(); /* FIXME: [fork/exec] Whoops. Not handled... yet */ - #else processCancelRequest(port, buf); - #endif return 127; /* XXX */ } --- 1290,1296 ---- *************** *** 1494,1501 **** CancelRequestPacket *canc = (CancelRequestPacket *) pkt; int backendPID; long cancelAuthCode; - Dlelem *curr; Backend *bp; backendPID = (int) ntohl(canc->backendPID); cancelAuthCode = (long) ntohl(canc->cancelAuthCode); --- 1523,1534 ---- CancelRequestPacket *canc = (CancelRequestPacket *) pkt; int backendPID; long cancelAuthCode; Backend *bp; + #ifndef EXEC_BACKEND + Dlelem *curr; + #else + int i; + #endif backendPID = (int) ntohl(canc->backendPID); cancelAuthCode = (long) ntohl(canc->cancelAuthCode); *************** *** 1514,1529 **** backendPID))); return; } - #ifdef EXEC_BACKEND - else - AttachSharedMemoryAndSemaphores(); - #endif /* See if we have a matching backend */ ! for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr)) { bp = (Backend *) DLE_VAL(curr); if (bp->pid == backendPID) { if (bp->cancel_key == cancelAuthCode) --- 1547,1563 ---- backendPID))); return; } /* See if we have a matching backend */ ! #ifndef EXEC_BACKEND for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr)) { bp = (Backend *) DLE_VAL(curr); + #else + for (i = 0; i < NUM_BACKENDARRAY_ELEMS; i++) + { + bp = (Backend*) &ShmemBackendArray[i]; + #endif if (bp->pid == backendPID) { if (bp->cancel_key == cancelAuthCode) *************** *** 1846,1860 **** { int save_errno = errno; - #ifdef WIN32 - #warning fix waidpid for Win32 - #else #ifdef HAVE_WAITPID int status; /* backend exit status */ - #else union wait status; /* backend exit status */ #endif int exitstatus; int pid; /* process id of dead backend */ --- 1880,1892 ---- { int save_errno = errno; #ifdef HAVE_WAITPID int status; /* backend exit status */ #else + #ifndef WIN32 union wait status; /* backend exit status */ #endif + #endif int exitstatus; int pid; /* process id of dead backend */ *************** *** 1867,1875 **** --- 1899,1919 ---- { exitstatus = status; #else + #ifndef WIN32 while ((pid = wait3(&status, WNOHANG, NULL)) > 0) { exitstatus = status.w_status; + #else + while ((pid = win32_waitpid(&exitstatus)) > 0) + { + /* + * We need to do this here, and not in CleanupProc, since this + * is to be called on all children when we are done with them. + * Could move to LogChildExit, but that seems like asking for + * future trouble... + */ + win32_RemoveChild(pid); + #endif #endif /* *************** *** 1957,1963 **** CleanupProc(pid, exitstatus); } /* loop over pending child-death reports */ - #endif if (FatalError) { --- 2001,2006 ---- *************** *** 2022,2027 **** --- 2065,2073 ---- bp = (Backend *) DLE_VAL(curr); if (bp->pid == pid) { + #ifdef EXEC_BACKEND + ShmemBackendArrayRemove(bp->pid); + #endif DLRemove(curr); free(bp); DLFreeElem(curr); *************** *** 2092,2097 **** --- 2138,2146 ---- /* * Found entry for freshly-dead backend, so remove it. */ + #ifdef EXEC_BACKEND + ShmemBackendArrayRemove(bp->pid); + #endif DLRemove(curr); free(bp); DLFreeElem(curr); *************** *** 2296,2301 **** --- 2345,2353 ---- */ bn->pid = pid; bn->cancel_key = MyCancelKey; + #ifdef EXEC_BACKEND + ShmemBackendArrayAdd(bn); + #endif DLAddHead(BackendList, DLNewElem(bn)); return STATUS_OK; *************** *** 2642,2647 **** --- 2694,2702 ---- memset((void*)&port, 0, sizeof(Port)); Assert(argc == 2); + /* Do this sooner rather than later... */ + IsUnderPostmaster = true; /* we are a postmaster subprocess now */ + /* Setup global context */ MemoryContextInit(); InitializeGUCOptions(); *************** *** 2661,2666 **** --- 2716,2724 ---- load_user(); load_group(); + /* Attach process to shared segments */ + AttachSharedMemoryAndSemaphores(); + /* Run backend */ proc_exit(BackendRun(&port)); } *************** *** 3129,3134 **** --- 3187,3195 ---- bn->pid = pid; bn->cancel_key = PostmasterRandom(); + #ifdef EXEC_BACKEND + ShmemBackendArrayAdd(bn); + #endif DLAddHead(BackendList, DLNewElem(bn)); /* *************** *** 3281,3286 **** --- 3342,3348 ---- write_var(ShmemIndexLock,fp); write_var(ShmemVariableCache,fp); write_var(ShmemIndexAlloc,fp); + write_var(ShmemBackendArray,fp); write_var(LWLockArray,fp); write_var(ProcStructLock,fp); *************** *** 3288,3293 **** --- 3350,3356 ---- write_var(PreAuthDelay,fp); write_var(debug_flag,fp); + write_var(PostmasterPid,fp); /* Release file */ FreeFile(fp); *************** *** 3334,3339 **** --- 3397,3403 ---- read_var(ShmemIndexLock,fp); read_var(ShmemVariableCache,fp); read_var(ShmemIndexAlloc,fp); + read_var(ShmemBackendArray,fp); read_var(LWLockArray,fp); read_var(ProcStructLock,fp); *************** *** 3341,3346 **** --- 3405,3411 ---- read_var(PreAuthDelay,fp); read_var(debug_flag,fp); + read_var(PostmasterPid,fp); /* Release file */ FreeFile(fp); *************** *** 3350,3355 **** --- 3415,3469 ---- errmsg("could not remove file \"%s\": %m", filename))); } + + size_t ShmemBackendArraySize(void) + { + return (NUM_BACKENDARRAY_ELEMS*sizeof(Backend)); + } + + void ShmemBackendArrayAllocation(void) + { + size_t size = ShmemBackendArraySize(); + ShmemBackendArray = (Backend*)ShmemAlloc(size); + memset(ShmemBackendArray, 0, size); + } + + static void ShmemBackendArrayAdd(Backend *bn) + { + int i; + for (i = 0; i < NUM_BACKENDARRAY_ELEMS; i++) + { + /* Find an empty slot */ + if (ShmemBackendArray[i].pid == 0) + { + ShmemBackendArray[i] = *bn; + return; + } + } + + /* FIXME: [fork/exec] some sort of error */ + abort(); + } + + static void ShmemBackendArrayRemove(pid_t pid) + { + int i; + for (i = 0; i < NUM_BACKENDARRAY_ELEMS; i++) + { + if (ShmemBackendArray[i].pid == pid) + { + /* Mark the slot as empty */ + ShmemBackendArray[i].pid = 0; + return; + } + } + + /* Something stronger than WARNING here? */ + ereport(WARNING, + (errmsg_internal("unable to find backend entry with pid %d", + pid))); + } + #endif #ifdef WIN32 *************** *** 3389,3402 **** return -1; } ! /* ! FIXME: [fork/exec] we might need to keep the following handle/s, ! depending on how we implement signalling. ! */ ! CloseHandle(pi.hProcess); CloseHandle(pi.hThread); return pi.dwProcessId; } #endif --- 3503,3613 ---- return -1; } ! if (!IsUnderPostmaster) ! /* We are the Postmaster creating a child... */ ! win32_AddChild(pi.dwProcessId,pi.hProcess); ! else ! CloseHandle(pi.hProcess); CloseHandle(pi.hThread); return pi.dwProcessId; + } + + /* + * Note: The following three functions must not be interrupted (eg. by signals). + * As the Postgres Win32 signalling architecture (currently) requires polling, + * or APC checking functions which aren't used here, this is not an issue. + * + * We keep two separate arrays, instead of a single array of pid/HANDLE structs, + * to avoid having to re-create a handle array for WaitForMultipleObjects on + * each call to win32_waitpid. + */ + + static void win32_AddChild(pid_t pid, HANDLE handle) + { + Assert(win32_childPIDArray && win32_childHNDArray); + if (win32_numChildren < NUM_BACKENDARRAY_ELEMS) + { + win32_childPIDArray[win32_numChildren] = pid; + win32_childHNDArray[win32_numChildren] = handle; + ++win32_numChildren; + } + else + /* FIXME: [fork/exec] some sort of error */ + abort(); + } + + static void win32_RemoveChild(pid_t pid) + { + int i; + Assert(win32_childPIDArray && win32_childHNDArray); + + for (i = 0; i < win32_numChildren; i++) + { + if (win32_childPIDArray[i] == pid) + { + CloseHandle(win32_childHNDArray[i]); + + /* Swap last entry into the "removed" one */ + --win32_numChildren; + win32_childPIDArray[win32_numChildren] = win32_childPIDArray[i]; + win32_childHNDArray[win32_numChildren] = win32_childHNDArray[i]; + return; + } + } + + /* Something stronger than WARNING here? */ + ereport(WARNING, + (errmsg_internal("unable to find child entry with pid %d", + pid))); + } + + static pid_t win32_waitpid(int *exitstatus) + { + Assert(win32_childPIDArray && win32_childHNDArray); + elog(DEBUG3,"waiting on %d children",win32_numChildren); + + if (win32_numChildren > 0) + { + /* + * Note: Do NOT use WaitForMultipleObjectsEx, as we don't + * want to run queued APCs here. + */ + int index; + DWORD exitCode; + DWORD ret = WaitForMultipleObjects(win32_numChildren,win32_childHNDArray,FALSE,0); + + switch (ret) + { + case WAIT_FAILED: + ereport(ERROR, + (errmsg_internal("failed to wait on %d children", + win32_numChildren))); + /* Fall through to WAIT_TIMEOUTs return */ + + case WAIT_TIMEOUT: + /* No children have finished */ + return -1; + + default: + /* Get the exit code, and return the PID of, the respective process */ + index = ret-WAIT_OBJECT_0; + Assert(index >= 0 && index < win32_numChildren); + if (!GetExitCodeProcess(win32_childHNDArray[index],&exitCode)) + /* + * If we get this far, this should never happen, but, then again... + * No choice other than to assume a catastrophic failure. + */ + ereport(FATAL, + (errmsg_internal("failed to get exit code for child %d", + win32_childPIDArray[index]))); + *exitstatus = (int)exitCode; + return win32_childPIDArray[index]; + } + } + + /* No children */ + return -1; } #endif Index: src/backend/storage/ipc/ipci.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/backend/storage/ipc/ipci.c,v retrieving revision 1.60 diff -c -r1.60 ipci.c *** src/backend/storage/ipc/ipci.c 20 Dec 2003 17:31:21 -0000 1.60 --- src/backend/storage/ipc/ipci.c 12 Jan 2004 07:32:32 -0000 *************** *** 63,68 **** --- 63,71 ---- size += LWLockShmemSize(); size += SInvalShmemSize(maxBackends); size += FreeSpaceShmemSize(); + #ifdef EXEC_BACKEND + size += ShmemBackendArraySize(); + #endif #ifdef STABLE_MEMORY_STORAGE size += MMShmemSize(); #endif *************** *** 132,137 **** --- 135,147 ---- * Set up child-to-postmaster signaling mechanism */ PMSignalInit(); + + #ifdef EXEC_BACKEND + /* + * Alloc the win32 shared backend array + */ + ShmemBackendArrayAllocation(); + #endif } Index: src/backend/storage/ipc/pmsignal.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/backend/storage/ipc/pmsignal.c,v retrieving revision 1.8 diff -c -r1.8 pmsignal.c *** src/backend/storage/ipc/pmsignal.c 11 Jan 2004 03:49:31 -0000 1.8 --- src/backend/storage/ipc/pmsignal.c 12 Jan 2004 07:32:32 -0000 *************** *** 63,70 **** return; /* Atomically set the proper flag */ PMSignalFlags[reason] = true; ! /* Send signal to postmaster (assume it is our direct parent) */ ! kill(getppid(), SIGUSR1); } /* --- 63,70 ---- return; /* Atomically set the proper flag */ PMSignalFlags[reason] = true; ! /* Send signal to postmaster */ ! kill(PostmasterPid, SIGUSR1); } /* Index: src/backend/tcop/postgres.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/backend/tcop/postgres.c,v retrieving revision 1.383 diff -c -r1.383 postgres.c *** src/backend/tcop/postgres.c 6 Jan 2004 23:15:22 -0000 1.383 --- src/backend/tcop/postgres.c 12 Jan 2004 07:32:38 -0000 *************** *** 2535,2543 **** errmsg("invalid command-line arguments for server process"), errhint("Try \"%s --help\" for more information.", argv[0]))); } ! #ifdef EXEC_BACKEND ! AttachSharedMemoryAndSemaphores(); ! #endif XLOGPathInit(); BaseInit(); --- 2535,2541 ---- errmsg("invalid command-line arguments for server process"), errhint("Try \"%s --help\" for more information.", argv[0]))); } ! XLOGPathInit(); BaseInit(); Index: src/backend/utils/init/globals.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/backend/utils/init/globals.c,v retrieving revision 1.77 diff -c -r1.77 globals.c *** src/backend/utils/init/globals.c 6 Jan 2004 17:36:31 -0000 1.77 --- src/backend/utils/init/globals.c 12 Jan 2004 07:32:38 -0000 *************** *** 53,58 **** --- 53,60 ---- char *DatabasePath = NULL; Oid MyDatabaseId = InvalidOid; + pid_t PostmasterPid = 0; + /* these are initialized for the bootstrap/standalone case: */ bool IsPostmasterEnvironment = false; bool IsUnderPostmaster = false; Index: src/include/miscadmin.h =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/include/miscadmin.h,v retrieving revision 1.144 diff -c -r1.144 miscadmin.h *** src/include/miscadmin.h 9 Jan 2004 23:29:31 -0000 1.144 --- src/include/miscadmin.h 12 Jan 2004 07:32:38 -0000 *************** *** 109,118 **** #else #define PG_USLEEP(_usec) \ do { \ ! Sleep(_usec < 500) ? 1 : (_usec+500)/ 1000); \ } while(0) #endif /***************************************************************************** * globals.h -- * *****************************************************************************/ --- 109,122 ---- #else #define PG_USLEEP(_usec) \ do { \ ! Sleep((_usec) < 500 ? 1 : ((_usec)+500)/ 1000); \ } while(0) #endif + #ifdef WIN32 + #define ftruncate(a,b) chsize(a,b) + #endif + /***************************************************************************** * globals.h -- * *****************************************************************************/ *************** *** 132,137 **** --- 138,144 ---- /* * from utils/init/globals.c */ + extern pid_t PostmasterPid; extern bool IsPostmasterEnvironment; extern bool IsUnderPostmaster; Index: src/include/port/win32.h =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/include/port/win32.h,v retrieving revision 1.13 diff -c -r1.13 win32.h *** src/include/port/win32.h 29 Nov 2003 19:52:08 -0000 1.13 --- src/include/port/win32.h 12 Jan 2004 07:32:39 -0000 *************** *** 15,21 **** #define NOFILE 100 /* defines for dynamic linking on Win32 platform */ ! #ifdef __CYGWIN__ #if __GNUC__ && ! defined (__declspec) #error You need egcs 1.1 or newer for compiling! --- 15,21 ---- #define NOFILE 100 /* defines for dynamic linking on Win32 platform */ ! #if defined(__CYGWIN__) || defined(__MINGW32__) #if __GNUC__ && ! defined (__declspec) #error You need egcs 1.1 or newer for compiling! *************** *** 27,33 **** #define DLLIMPORT __declspec (dllimport) #endif ! #elif defined(WIN32) && defined(_MSC_VER) /* not CYGWIN */ #if defined(_DLL) #define DLLIMPORT __declspec (dllexport) --- 27,33 ---- #define DLLIMPORT __declspec (dllimport) #endif ! #elif defined(WIN32) && defined(_MSC_VER) /* not CYGWIN or MingW */ #if defined(_DLL) #define DLLIMPORT __declspec (dllexport) *************** *** 35,41 **** #define DLLIMPORT __declspec (dllimport) #endif ! #else /* not CYGWIN, not MSVC */ #define DLLIMPORT #endif --- 35,41 ---- #define DLLIMPORT __declspec (dllimport) #endif ! #else /* not CYGWIN, not MSVC, not MingW */ #define DLLIMPORT #endif