Index: backend/port/win32/signal.c =================================================================== RCS file: /projects/cvsroot/pgsql/src/backend/port/win32/signal.c,v retrieving revision 1.11 diff -c -r1.11 signal.c *** backend/port/win32/signal.c 31 Dec 2004 22:00:37 -0000 1.11 --- backend/port/win32/signal.c 2 Jun 2005 12:38:32 -0000 *************** *** 14,43 **** #include "postgres.h" #include ! /* pg_signal_crit_sec is used to protect only pg_signal_queue. That is the only ! * variable that can be accessed from the signal sending threads! */ ! static CRITICAL_SECTION pg_signal_crit_sec; ! static int pg_signal_queue; static pqsigfunc pg_signal_array[PG_SIGNAL_COUNT]; static pqsigfunc pg_signal_defaults[PG_SIGNAL_COUNT]; static int pg_signal_mask; - DLLIMPORT HANDLE pgwin32_signal_event; - HANDLE pgwin32_initial_signal_pipe = INVALID_HANDLE_VALUE; - - - /* Signal handling thread function */ - static DWORD WINAPI pg_signal_thread(LPVOID param); static BOOL WINAPI pg_console_handler(DWORD dwCtrlType); ! /* Sleep function that can be interrupted by signals */ void pgwin32_backend_usleep(long microsec) { ! if (WaitForSingleObject(pgwin32_signal_event, (microsec < 500 ? 1 : (microsec + 500) / 1000)) == WAIT_OBJECT_0) { pgwin32_dispatch_queued_signals(); errno = EINTR; --- 14,57 ---- #include "postgres.h" #include + #include "miscadmin.h" + /* Simplified ereport for this module */ + #define ereport_signal(elevel, msg) \ + ereport((elevel), \ + (errmsg_internal("%s: error code: %d", (msg), (int)GetLastError()))); + + /* Lock/Unlock signal area primitives */ + #define lock_signal_shmem() \ + do { \ + if (WaitForSingleObject(MySignalShmem->mutex, INFINITE) != WAIT_OBJECT_0) \ + ereport_signal(ERROR, "failed to wait on signal mutex"); \ + }while(0) + + #define unlock_signal_shmem() \ + do{ \ + if (!ReleaseMutex(MySignalShmem->mutex)) \ + ereport_signal(ERROR, "failed to release signal mutex"); \ + }while(0) ! /* Point to signal shared memory of current process */ ! DLLIMPORT Win32SignalShmemStruct *MySignalShmem; static pqsigfunc pg_signal_array[PG_SIGNAL_COUNT]; static pqsigfunc pg_signal_defaults[PG_SIGNAL_COUNT]; static int pg_signal_mask; static BOOL WINAPI pg_console_handler(DWORD dwCtrlType); ! /* ! * pgwin32_backend_usleep ! * Sleep function that can be interrupted by signals ! */ void pgwin32_backend_usleep(long microsec) { ! if (WaitForSingleObject(pgwin32_signal_event, (microsec < 500 ? ! 1 : (microsec + 500) / 1000)) == WAIT_OBJECT_0) { pgwin32_dispatch_queued_signals(); errno = EINTR; *************** *** 45,101 **** } } ! ! /* Initialization */ void pgwin32_signal_initialize(void) { int i; ! HANDLE signal_thread_handle; ! ! InitializeCriticalSection(&pg_signal_crit_sec); for (i = 0; i < PG_SIGNAL_COUNT; i++) { pg_signal_array[i] = SIG_DFL; pg_signal_defaults[i] = SIG_IGN; } pg_signal_mask = 0; - pg_signal_queue = 0; ! /* Create the global event handle used to flag signals */ ! pgwin32_signal_event = CreateEvent(NULL, TRUE, FALSE, NULL); ! if (pgwin32_signal_event == NULL) ! ereport(FATAL, ! (errmsg_internal("failed to create signal event: %d", (int) GetLastError()))); ! ! /* Create thread for handling signals */ ! signal_thread_handle = CreateThread(NULL, 0, pg_signal_thread, NULL, 0, NULL); ! if (signal_thread_handle == NULL) ! ereport(FATAL, ! (errmsg_internal("failed to create signal handler thread"))); /* Create console control handle to pick up Ctrl-C etc */ if (!SetConsoleCtrlHandler(pg_console_handler, TRUE)) ! ereport(FATAL, ! (errmsg_internal("failed to set console control handler"))); } ! ! /* Dispatch all signals currently queued and not blocked ! * Blocked signals are ignored, and will be fired at the time of ! * the sigsetmask() call. */ void pgwin32_dispatch_queued_signals(void) { int i; ! EnterCriticalSection(&pg_signal_crit_sec); ! while (pg_signal_queue & ~pg_signal_mask) { /* One or more unblocked signals queued for execution */ ! ! int exec_mask = pg_signal_queue & ~pg_signal_mask; for (i = 0; i < PG_SIGNAL_COUNT; i++) { --- 59,152 ---- } } ! /* ! * pgwin32_signal_initialize ! * Allocate and initialize signal emulation shared memory, ! * including a mutex protecting the whole area, an event ! * notifying that a signal was sent to current process, and ! * a queue saving signals. All of them are in global namespace. ! */ void pgwin32_signal_initialize(void) { int i; ! char name[64]; ! HANDLE hShmem; ! SECURITY_ATTRIBUTES sa; ! ! ZeroMemory(&sa, sizeof(sa)); ! sa.nLength = sizeof(sa); ! sa.bInheritHandle = FALSE; + /* Initialize the local part */ for (i = 0; i < PG_SIGNAL_COUNT; i++) { pg_signal_array[i] = SIG_DFL; pg_signal_defaults[i] = SIG_IGN; } pg_signal_mask = 0; ! /* ! * Create my signal shared memory area. If any of step failed, ! * we will exit the process and rely on the windows system ! * to do the clean up. ! */ ! wsprintf(name, "%s.%d", SIGNAL_SHMEM_PREFIX, MyProcPid); ! if (NULL != (hShmem = OpenFileMapping(FILE_MAP_ALL_ACCESS, ! FALSE, ! name))) ! ereport(FATAL, ! (errmsg_internal("shmem segement \"%s\" already exists", name))); ! ! if (NULL == (hShmem = CreateFileMapping((HANDLE) 0xFFFFFFFF, ! NULL, PAGE_READWRITE, ! 0L, (DWORD) sizeof(Win32SignalShmemStruct), ! name))) ! ereport_signal(FATAL, ! "out of shared memory for signaling"); ! ! if (NULL == (MySignalShmem = MapViewOfFile(hShmem, ! FILE_MAP_ALL_ACCESS, ! 0, 0, 0))) ! ereport_signal(FATAL, ! "failed to attach to signal area"); ! ! /* Create the global event */ ! wsprintf(name, "%s.%d", SIGNAL_EVENT_PREFIX, MyProcPid); ! if (NULL == (MySignalShmem->event = CreateEvent(&sa, ! TRUE, FALSE, name))) ! ereport_signal(FATAL, ! "failed to create signal event"); ! ! /* Create the global mutex */ ! wsprintf(name, "%s.%d", SIGNAL_MUTEX_PREFIX, MyProcPid); ! if (NULL == (MySignalShmem->mutex = CreateMutex(&sa, ! FALSE, name))) ! ereport_signal(FATAL, ! "failed to create signal mutex"); /* Create console control handle to pick up Ctrl-C etc */ if (!SetConsoleCtrlHandler(pg_console_handler, TRUE)) ! ereport_signal(FATAL, ! "failed to set console control"); } ! /* ! * pgwin32_dispatch_queued_signals ! * Dispatch all signals currently queued and not blocked ! * Blocked signals are ignored, and will be fired at the ! * time of the sigsetmask() call. ! */ void pgwin32_dispatch_queued_signals(void) { int i; ! lock_signal_shmem(); ! while (MySignalShmem->queue & ~pg_signal_mask) { /* One or more unblocked signals queued for execution */ ! int exec_mask = MySignalShmem->queue & ~pg_signal_mask; for (i = 0; i < PG_SIGNAL_COUNT; i++) { *************** *** 106,129 **** if (sig == SIG_DFL) sig = pg_signal_defaults[i]; ! pg_signal_queue &= ~sigmask(i); if (sig != SIG_ERR && sig != SIG_IGN && sig != SIG_DFL) { ! LeaveCriticalSection(&pg_signal_crit_sec); sig(i); ! EnterCriticalSection(&pg_signal_crit_sec); ! break; /* Restart outer loop, in case signal mask ! * or queue has been modified inside ! * signal handler */ } } } } ! ResetEvent(pgwin32_signal_event); ! LeaveCriticalSection(&pg_signal_crit_sec); } ! /* signal masking. Only called on main thread, no sync required */ int pqsigsetmask(int mask) { --- 157,188 ---- if (sig == SIG_DFL) sig = pg_signal_defaults[i]; ! MySignalShmem->queue &= ~sigmask(i); if (sig != SIG_ERR && sig != SIG_IGN && sig != SIG_DFL) { ! unlock_signal_shmem(); sig(i); ! lock_signal_shmem(); ! ! /* ! * Restart outer loop, in case signal mask or ! * queue has been modified inside signal handler ! */ ! break; } } } } ! unlock_signal_shmem(); ! ! if (!ResetEvent(MySignalShmem->event)) ! ereport_signal(ERROR, "failed to reset signal waiting event"); } ! /* ! * pqsigsetmask ! * Signal masking emulation in win32. ! */ int pqsigsetmask(int mask) { *************** *** 141,148 **** return prevmask; } ! ! /* signal manipulation. Only called on main thread, no sync required */ pqsigfunc pqsignal(int signum, pqsigfunc handler) { --- 200,209 ---- return prevmask; } ! /* ! * pqsignal ! * Function signal() emulation in win32 ! */ pqsigfunc pqsignal(int signum, pqsigfunc handler) { *************** *** 155,286 **** return prevfunc; } ! /* Create the signal listener pipe for specified pid */ ! HANDLE ! pgwin32_create_signal_listener(pid_t pid) ! { ! char pipename[128]; ! HANDLE pipe; ! ! wsprintf(pipename, "\\\\.\\pipe\\pgsignal_%d", (int) pid); ! ! pipe = CreateNamedPipe(pipename, PIPE_ACCESS_DUPLEX, ! PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, ! PIPE_UNLIMITED_INSTANCES, 16, 16, 1000, NULL); ! ! if (pipe == INVALID_HANDLE_VALUE) ! ereport(ERROR, ! (errmsg("could not create signal listener pipe for pid %d: error code %d", ! (int) pid, (int) GetLastError()))); ! ! return pipe; ! } ! ! ! /* ! * All functions below execute on the signal handler thread ! * and must be synchronized as such! ! * NOTE! The only global variable that can be used is ! * pg_signal_queue! */ - - void pg_queue_signal(int signum) { if (signum >= PG_SIGNAL_COUNT || signum <= 0) return; ! EnterCriticalSection(&pg_signal_crit_sec); ! pg_signal_queue |= sigmask(signum); ! LeaveCriticalSection(&pg_signal_crit_sec); ! ! SetEvent(pgwin32_signal_event); ! } ! ! /* Signal dispatching thread */ ! static DWORD WINAPI ! pg_signal_dispatch_thread(LPVOID param) ! { ! HANDLE pipe = (HANDLE) param; ! BYTE sigNum; ! DWORD bytes; ! ! if (!ReadFile(pipe, &sigNum, 1, &bytes, NULL)) ! { ! /* Client died before sending */ ! CloseHandle(pipe); ! return 0; ! } ! if (bytes != 1) ! { ! /* Received bytes over signal pipe (should be 1) */ ! CloseHandle(pipe); ! return 0; ! } ! WriteFile(pipe, &sigNum, 1, &bytes, NULL); /* Don't care if it works ! * or not.. */ ! FlushFileBuffers(pipe); ! DisconnectNamedPipe(pipe); ! CloseHandle(pipe); ! ! pg_queue_signal(sigNum); ! return 0; } ! /* Signal handling thread */ ! static DWORD WINAPI ! pg_signal_thread(LPVOID param) ! { ! char pipename[128]; ! HANDLE pipe = pgwin32_initial_signal_pipe; ! ! wsprintf(pipename, "\\\\.\\pipe\\pgsignal_%d", GetCurrentProcessId()); ! ! for (;;) ! { ! BOOL fConnected; ! HANDLE hThread; ! ! if (pipe == INVALID_HANDLE_VALUE) ! { ! pipe = CreateNamedPipe(pipename, PIPE_ACCESS_DUPLEX, ! PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, ! PIPE_UNLIMITED_INSTANCES, 16, 16, 1000, NULL); ! ! if (pipe == INVALID_HANDLE_VALUE) ! { ! write_stderr("could not create signal listener pipe: error code %d; retrying\n", (int) GetLastError()); ! SleepEx(500, FALSE); ! continue; ! } ! } ! ! fConnected = ConnectNamedPipe(pipe, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED); ! if (fConnected) ! { ! hThread = CreateThread(NULL, 0, ! (LPTHREAD_START_ROUTINE) pg_signal_dispatch_thread, ! (LPVOID) pipe, 0, NULL); ! if (hThread == INVALID_HANDLE_VALUE) ! write_stderr("could not create signal dispatch thread: error code %d\n", ! (int) GetLastError()); ! else ! CloseHandle(hThread); ! } ! else ! /* Connection failed. Cleanup and try again */ ! CloseHandle(pipe); ! ! /* Set up so we create a new pipe on next loop */ ! pipe = INVALID_HANDLE_VALUE; ! } ! return 0; ! } ! ! ! /* Console control handler will execute on a thread created ! by the OS at the time of invocation */ static BOOL WINAPI pg_console_handler(DWORD dwCtrlType) { --- 216,244 ---- return prevfunc; } ! /* ! * pg_queue_signal ! * Trigger a signal handling of current process */ void pg_queue_signal(int signum) { if (signum >= PG_SIGNAL_COUNT || signum <= 0) return; ! lock_signal_shmem(); ! MySignalShmem->queue |= sigmask(signum); ! unlock_signal_shmem(); ! ! if (!SetEvent(MySignalShmem->event)) ! ereport_signal(ERROR, "failed to set signal waiting event"); } ! /* ! * pg_console_handler ! * Console control handler will execute on a thread created ! * by the OS at the time of invocation ! */ static BOOL WINAPI pg_console_handler(DWORD dwCtrlType) { Index: backend/postmaster/postmaster.c =================================================================== RCS file: /projects/cvsroot/pgsql/src/backend/postmaster/postmaster.c,v retrieving revision 1.451 diff -c -r1.451 postmaster.c *** backend/postmaster/postmaster.c 15 May 2005 00:26:18 -0000 1.451 --- backend/postmaster/postmaster.c 2 Jun 2005 12:28:15 -0000 *************** *** 332,338 **** pid_t PostmasterPid; #ifdef WIN32 HANDLE PostmasterHandle; - HANDLE initial_signal_pipe; HANDLE syslogPipe[2]; #else int syslogPipe[2]; --- 332,337 ---- *************** *** 3606,3614 **** #ifdef WIN32 param->PostmasterHandle = PostmasterHandle; - write_duplicated_handle(¶m->initial_signal_pipe, - pgwin32_create_signal_listener(childPid), - childProcess); #endif memcpy(¶m->syslogPipe, &syslogPipe, sizeof(syslogPipe)); --- 3605,3610 ---- *************** *** 3626,3654 **** #ifdef WIN32 /* - * Duplicate a handle for usage in a child process, and write the child - * process instance of the handle to the parameter file. - */ - static void - write_duplicated_handle(HANDLE *dest, HANDLE src, HANDLE childProcess) - { - HANDLE hChild = INVALID_HANDLE_VALUE; - - if (!DuplicateHandle(GetCurrentProcess(), - src, - childProcess, - &hChild, - 0, - TRUE, - DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) - ereport(ERROR, - (errmsg_internal("could not duplicate handle to be written to backend parameter file: error code %d", - (int) GetLastError()))); - - *dest = hChild; - } - - /* * Duplicate a socket for usage in a child process, and write the resulting * structure to the parameter file. * This is required because a number of LSPs (Layered Service Providers) very --- 3622,3627 ---- *************** *** 3808,3814 **** #ifdef WIN32 PostmasterHandle = param->PostmasterHandle; - pgwin32_initial_signal_pipe = param->initial_signal_pipe; #endif memcpy(&syslogPipe, ¶m->syslogPipe, sizeof(syslogPipe)); --- 3781,3786 ---- Index: port/kill.c =================================================================== RCS file: /projects/cvsroot/pgsql/src/port/kill.c,v retrieving revision 1.6 diff -c -r1.6 kill.c *** port/kill.c 31 Dec 2004 22:03:53 -0000 1.6 --- port/kill.c 2 Jun 2005 12:30:54 -0000 *************** *** 17,61 **** #include "c.h" #ifdef WIN32 ! /* signal sending */ int pgkill(int pid, int sig) { ! char pipename[128]; ! BYTE sigData = sig; ! BYTE sigRet = 0; ! DWORD bytes; ! /* we allow signal 0 here, but it will be ignored in pg_queue_signal */ if (sig >= PG_SIGNAL_COUNT || sig < 0) ! { ! errno = EINVAL; ! return -1; ! } if (pid <= 0) ! { ! /* No support for process groups */ ! errno = EINVAL; ! return -1; ! } ! wsprintf(pipename, "\\\\.\\pipe\\pgsignal_%i", pid); ! if (!CallNamedPipe(pipename, &sigData, 1, &sigRet, 1, &bytes, 1000)) ! { ! if (GetLastError() == ERROR_FILE_NOT_FOUND) ! errno = ESRCH; ! else if (GetLastError() == ERROR_ACCESS_DENIED) ! errno = EPERM; ! else ! errno = EINVAL; ! return -1; ! } ! if (bytes != 1 || sigRet != sig) ! { ! errno = ESRCH; ! return -1; ! } ! return 0; } #endif --- 17,120 ---- #include "c.h" #ifdef WIN32 ! ! /* Convenience macro to set errno and return */ ! #define set_error_and_return(e) \ ! do{ \ ! if (shmem) UnmapViewOfFile(shmem); \ ! if (hShmem) CloseHandle(hShmem); \ ! if (hEvent) CloseHandle(hEvent); \ ! if (hMutex) CloseHandle(hMutex); \ ! \ ! errno = (e); \ ! return ((e) == 0)? 0 : -1; \ ! } while (0) ! ! /* ! * pgkill ! * kill() win32 emulation. ! * ! * pgkill() will fail if: ! * [EINVAL] The value of the sig argument is an invalid ! * or target process id <= 0. ! * [ESRCH] No process can be found corresponding to that ! * specified by pid. ! * [EPERM] Any other win32 system call fails. ! */ int pgkill(int pid, int sig) { ! char name[64]; ! HANDLE hShmem, hMutex, hEvent; ! Win32SignalShmemStruct *shmem; ! ! shmem = NULL; ! hShmem = NULL; ! hMutex = NULL; ! hEvent = NULL; ! /* we allow signal 0 here, and handle later */ if (sig >= PG_SIGNAL_COUNT || sig < 0) ! set_error_and_return(EINVAL); ! ! /* No support for process groups */ if (pid <= 0) ! set_error_and_return(EINVAL); ! ! /* ! * Attach to the signaling shared memory. If it is attachable, ! * we decide that the process is alive. ! */ ! wsprintf(name, "%s.%d", SIGNAL_SHMEM_PREFIX, pid); ! if (NULL == (hShmem = OpenFileMapping(FILE_MAP_ALL_ACCESS, ! FALSE, ! name))) ! set_error_and_return(ESRCH); ! ! if (sig == 0) ! set_error_and_return(0); ! ! if (NULL == (shmem = MapViewOfFile(hShmem, ! FILE_MAP_ALL_ACCESS, ! 0, 0, 0))) ! set_error_and_return(EPERM); ! ! /* Grab the mutex */ ! wsprintf(name, "%s.%d", SIGNAL_MUTEX_PREFIX, pid); ! if (NULL == (hMutex = OpenMutex(MUTEX_ALL_ACCESS, ! FALSE, name))) ! set_error_and_return(EPERM); ! ! if (WaitForSingleObject(hMutex, INFINITE) != WAIT_OBJECT_0) ! set_error_and_return(EPERM); ! ! /* Write down the signal number */ ! shmem->queue |= sigmask(sig); ! ! /* ! * If we can't release the mutex, we have to exit the ! * process to make mutex released by the system. If we ! * can't set the event, that's really awkward, but the ! * target process would find out the signal when next ! * set event is successfully done. ! */ ! if (!ReleaseMutex(hMutex)) ! set_error_and_return(EPERM); ! ! if (!UnmapViewOfFile(shmem)) ! set_error_and_return(EPERM); ! ! /* Set event */ ! wsprintf(name, "%s.%d", SIGNAL_EVENT_PREFIX, pid); ! if (NULL == (hEvent = OpenEvent(EVENT_ALL_ACCESS, ! FALSE, name))) ! set_error_and_return(EPERM); ! ! if (!SetEvent(hEvent)) ! set_error_and_return(EPERM); ! /* Successfully done */ ! set_error_and_return(0); } #endif Index: include/port/win32.h =================================================================== RCS file: /projects/cvsroot/pgsql/src/include/port/win32.h,v retrieving revision 1.45 diff -c -r1.45 win32.h *** include/port/win32.h 20 May 2005 14:53:26 -0000 1.45 --- include/port/win32.h 2 Jun 2005 12:36:49 -0000 *************** *** 144,149 **** --- 144,161 ---- #define SIGUSR1 30 #define SIGUSR2 31 + /* signaling variables name prefix */ + #define SIGNAL_SHMEM_PREFIX "Global\\PostgreSQL.SignalShmem" + #define SIGNAL_MUTEX_PREFIX "Global\\PostgreSQL.SignalMutex" + #define SIGNAL_EVENT_PREFIX "Global\\PostgreSQL.SignalEvent" + + /* signal shared memory structure */ + typedef struct Win32SignalShmemStruct{ + HANDLE mutex; + HANDLE event; + int queue; + }Win32SignalShmemStruct; + struct timezone { int tz_minuteswest; /* Minutes west of GMT. */ *************** *** 212,225 **** /* In backend/port/win32/signal.c */ - extern DLLIMPORT HANDLE pgwin32_signal_event; - extern HANDLE pgwin32_initial_signal_pipe; - void pgwin32_signal_initialize(void); - HANDLE pgwin32_create_signal_listener(pid_t pid); void pgwin32_dispatch_queued_signals(void); void pg_queue_signal(int signum); #ifndef FRONTEND #define pg_usleep(t) pgwin32_backend_usleep(t) void pgwin32_backend_usleep(long microsec); --- 224,237 ---- /* In backend/port/win32/signal.c */ void pgwin32_signal_initialize(void); void pgwin32_dispatch_queued_signals(void); void pg_queue_signal(int signum); + extern DLLIMPORT Win32SignalShmemStruct *MySignalShmem; + + #define pgwin32_signal_event (MySignalShmem->event) + #ifndef FRONTEND #define pg_usleep(t) pgwin32_backend_usleep(t) void pgwin32_backend_usleep(long microsec);