1 /*-------------------------------------------------------------------------
4 * Microsoft Windows Win32 Signal Emulation Functions
6 * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
9 * $PostgreSQL: pgsql/src/backend/port/win32/signal.c,v 1.1 2004/04/12 16:19:18 momjian Exp $
11 *-------------------------------------------------------------------------
16 #include <libpq/pqsignal.h>
19 /* pg_signal_crit_sec is used to protect only pg_signal_queue. That is the only
20 * variable that can be accessed from the signal sending threads! */
21 static CRITICAL_SECTION pg_signal_crit_sec;
22 static int pg_signal_queue;
24 #define PG_SIGNAL_COUNT 32
25 static pqsigfunc pg_signal_array[PG_SIGNAL_COUNT];
26 static pqsigfunc pg_signal_defaults[PG_SIGNAL_COUNT];
27 static int pg_signal_mask;
29 DLLIMPORT HANDLE pgwin32_signal_event;
32 /* Signal handling thread function */
33 static DWORD WINAPI pg_signal_thread(LPVOID param);
34 static BOOL WINAPI pg_console_handler(DWORD dwCtrlType);
36 /* Sleep function that can be interrupted by signals */
37 void pgwin32_backend_usleep(long microsec) {
38 if (WaitForSingleObject(pgwin32_signal_event, (microsec < 500 ? 1 : (microsec + 500) / 1000)) == WAIT_OBJECT_0) {
39 pgwin32_dispatch_queued_signals();
48 pgwin32_signal_initialize(void)
51 HANDLE signal_thread_handle;
53 InitializeCriticalSection(&pg_signal_crit_sec);
55 for (i = 0; i < PG_SIGNAL_COUNT; i++)
57 pg_signal_array[i] = SIG_DFL;
58 pg_signal_defaults[i] = SIG_IGN;
63 /* Create the global event handle used to flag signals */
64 pgwin32_signal_event = CreateEvent(NULL, TRUE, FALSE, NULL);
65 if (pgwin32_signal_event == NULL)
67 (errmsg_internal("Failed to create signal event: %i!",(int)GetLastError())));
69 /* Create thread for handling signals */
70 signal_thread_handle = CreateThread(NULL, 0, pg_signal_thread, NULL, 0, NULL);
71 if (signal_thread_handle == NULL)
73 (errmsg_internal("Failed to create signal handler thread!")));
75 /* Create console control handle to pick up Ctrl-C etc */
76 if (!SetConsoleCtrlHandler(pg_console_handler, TRUE))
78 (errmsg_internal("Failed to set console control handler!")));
82 /* Dispatch all signals currently queued and not blocked
83 * Blocked signals are ignored, and will be fired at the time of
84 * the sigsetmask() call. */
86 pgwin32_dispatch_queued_signals(void)
90 EnterCriticalSection(&pg_signal_crit_sec);
91 while (pg_signal_queue & ~pg_signal_mask)
93 /* One or more unblocked signals queued for execution */
95 int exec_mask = pg_signal_queue & ~pg_signal_mask;
97 for (i = 0; i < PG_SIGNAL_COUNT; i++)
99 if (exec_mask & sigmask(i))
101 /* Execute this signal */
102 pqsigfunc sig = pg_signal_array[i];
105 sig = pg_signal_defaults[i];
106 pg_signal_queue &= ~sigmask(i);
107 if (sig != SIG_ERR && sig != SIG_IGN && sig != SIG_DFL)
109 LeaveCriticalSection(&pg_signal_crit_sec);
111 EnterCriticalSection(&pg_signal_crit_sec);
112 break; /* Restart outer loop, in case signal mask
113 * or queue has been modified inside
119 ResetEvent(pgwin32_signal_event);
120 LeaveCriticalSection(&pg_signal_crit_sec);
123 /* signal masking. Only called on main thread, no sync required */
125 pqsigsetmask(int mask)
129 prevmask = pg_signal_mask;
130 pg_signal_mask = mask;
133 * Dispatch any signals queued up right away, in case we have
134 * unblocked one or more signals previously queued
136 pgwin32_dispatch_queued_signals();
142 /* signal manipulation. Only called on main thread, no sync required */
144 pqsignal(int signum, pqsigfunc handler)
148 if (signum >= PG_SIGNAL_COUNT || signum < 0)
150 prevfunc = pg_signal_array[signum];
151 pg_signal_array[signum] = handler;
157 pqkill(int pid, int sig)
164 if (sig >= PG_SIGNAL_COUNT || sig <= 0)
171 /* No support for process groups */
175 wsprintf(pipename, "\\\\.\\pipe\\pgsignal_%i", pid);
176 if (!CallNamedPipe(pipename, &sigData, 1, &sigRet, 1, &bytes, 1000))
178 if (GetLastError() == ERROR_FILE_NOT_FOUND)
180 else if (GetLastError() == ERROR_ACCESS_DENIED)
186 if (bytes != 1 || sigRet != sig)
196 * All functions below execute on the signal handler thread
197 * and must be synchronized as such!
198 * NOTE! The only global variable that can be used is
204 pg_queue_signal(int signum)
206 if (signum >= PG_SIGNAL_COUNT || signum < 0)
209 EnterCriticalSection(&pg_signal_crit_sec);
210 pg_signal_queue |= sigmask(signum);
211 LeaveCriticalSection(&pg_signal_crit_sec);
213 SetEvent(pgwin32_signal_event);
216 /* Signal dispatching thread */
218 pg_signal_dispatch_thread(LPVOID param)
220 HANDLE pipe = (HANDLE) param;
224 if (!ReadFile(pipe, &sigNum, 1, &bytes, NULL))
226 /* Client died before sending */
232 /* Received <bytes> bytes over signal pipe (should be 1) */
236 WriteFile(pipe, &sigNum, 1, &bytes, NULL); /* Don't care if it works
238 FlushFileBuffers(pipe);
239 DisconnectNamedPipe(pipe);
242 pg_queue_signal(sigNum);
246 /* Signal handling thread */
248 pg_signal_thread(LPVOID param)
251 HANDLE pipe = INVALID_HANDLE_VALUE;
253 wsprintf(pipename, "\\\\.\\pipe\\pgsignal_%i", GetCurrentProcessId());
260 pipe = CreateNamedPipe(pipename, PIPE_ACCESS_DUPLEX,
261 PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
262 PIPE_UNLIMITED_INSTANCES, 16, 16, 1000, NULL);
263 if (pipe == INVALID_HANDLE_VALUE)
265 fprintf(stderr, gettext("Failed to create signal listener pipe: %i. Retrying.\n"), (int) GetLastError());
270 fConnected = ConnectNamedPipe(pipe, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
273 hThread = CreateThread(NULL, 0,
274 (LPTHREAD_START_ROUTINE) pg_signal_dispatch_thread,
275 (LPVOID) pipe, 0, NULL);
276 if (hThread == INVALID_HANDLE_VALUE)
277 fprintf(stderr, gettext("Failed to create signal dispatch thread: %i\n"), (int) GetLastError());
279 CloseHandle(hThread);
282 /* Connection failed. Cleanup and try again */
289 /* Console control handler will execute on a thread created
290 by the OS at the time of invocation */
291 static BOOL WINAPI pg_console_handler(DWORD dwCtrlType) {
292 if (dwCtrlType == CTRL_C_EVENT ||
293 dwCtrlType == CTRL_BREAK_EVENT ||
294 dwCtrlType == CTRL_CLOSE_EVENT ||
295 dwCtrlType == CTRL_SHUTDOWN_EVENT) {
296 pg_queue_signal(SIGINT);