1 /*-------------------------------------------------------------------------
4 * Microsoft Windows Win32 Signal Emulation Functions
6 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
9 * $PostgreSQL: pgsql/src/backend/port/win32/signal.c,v 1.20 2008/01/01 19:45:51 momjian Exp $
11 *-------------------------------------------------------------------------
16 #include <libpq/pqsignal.h>
19 * These are exported for use by the UNBLOCKED_SIGNAL_QUEUE() macro.
20 * pg_signal_queue must be volatile since it is changed by the signal
21 * handling thread and inspected without any lock by the main thread.
22 * pg_signal_mask is only changed by main thread so shouldn't need it.
24 volatile int pg_signal_queue;
27 HANDLE pgwin32_signal_event;
28 HANDLE pgwin32_initial_signal_pipe = INVALID_HANDLE_VALUE;
31 * pg_signal_crit_sec is used to protect only pg_signal_queue. That is the only
32 * variable that can be accessed from the signal sending threads!
34 static CRITICAL_SECTION pg_signal_crit_sec;
36 static pqsigfunc pg_signal_array[PG_SIGNAL_COUNT];
37 static pqsigfunc pg_signal_defaults[PG_SIGNAL_COUNT];
40 /* Signal handling thread function */
41 static DWORD WINAPI pg_signal_thread(LPVOID param);
42 static BOOL WINAPI pg_console_handler(DWORD dwCtrlType);
46 * pg_usleep --- delay the specified number of microseconds, but
47 * stop waiting if a signal arrives.
49 * This replaces the non-signal-aware version provided by src/port/pgsleep.c.
52 pg_usleep(long microsec)
54 if (WaitForSingleObject(pgwin32_signal_event,
55 (microsec < 500 ? 1 : (microsec + 500) / 1000))
58 pgwin32_dispatch_queued_signals();
67 pgwin32_signal_initialize(void)
70 HANDLE signal_thread_handle;
72 InitializeCriticalSection(&pg_signal_crit_sec);
74 for (i = 0; i < PG_SIGNAL_COUNT; i++)
76 pg_signal_array[i] = SIG_DFL;
77 pg_signal_defaults[i] = SIG_IGN;
82 /* Create the global event handle used to flag signals */
83 pgwin32_signal_event = CreateEvent(NULL, TRUE, FALSE, NULL);
84 if (pgwin32_signal_event == NULL)
86 (errmsg_internal("failed to create signal event: %d", (int) GetLastError())));
88 /* Create thread for handling signals */
89 signal_thread_handle = CreateThread(NULL, 0, pg_signal_thread, NULL, 0, NULL);
90 if (signal_thread_handle == NULL)
92 (errmsg_internal("failed to create signal handler thread")));
94 /* Create console control handle to pick up Ctrl-C etc */
95 if (!SetConsoleCtrlHandler(pg_console_handler, TRUE))
97 (errmsg_internal("failed to set console control handler")));
101 * Dispatch all signals currently queued and not blocked
102 * Blocked signals are ignored, and will be fired at the time of
103 * the sigsetmask() call.
106 pgwin32_dispatch_queued_signals(void)
110 EnterCriticalSection(&pg_signal_crit_sec);
111 while (UNBLOCKED_SIGNAL_QUEUE())
113 /* One or more unblocked signals queued for execution */
114 int exec_mask = UNBLOCKED_SIGNAL_QUEUE();
116 for (i = 0; i < PG_SIGNAL_COUNT; i++)
118 if (exec_mask & sigmask(i))
120 /* Execute this signal */
121 pqsigfunc sig = pg_signal_array[i];
124 sig = pg_signal_defaults[i];
125 pg_signal_queue &= ~sigmask(i);
126 if (sig != SIG_ERR && sig != SIG_IGN && sig != SIG_DFL)
128 LeaveCriticalSection(&pg_signal_crit_sec);
130 EnterCriticalSection(&pg_signal_crit_sec);
131 break; /* Restart outer loop, in case signal mask or
132 * queue has been modified inside signal
138 ResetEvent(pgwin32_signal_event);
139 LeaveCriticalSection(&pg_signal_crit_sec);
142 /* signal masking. Only called on main thread, no sync required */
144 pqsigsetmask(int mask)
148 prevmask = pg_signal_mask;
149 pg_signal_mask = mask;
152 * Dispatch any signals queued up right away, in case we have unblocked
153 * one or more signals previously queued
155 pgwin32_dispatch_queued_signals();
161 /* signal manipulation. Only called on main thread, no sync required */
163 pqsignal(int signum, pqsigfunc handler)
167 if (signum >= PG_SIGNAL_COUNT || signum < 0)
169 prevfunc = pg_signal_array[signum];
170 pg_signal_array[signum] = handler;
174 /* Create the signal listener pipe for specified pid */
176 pgwin32_create_signal_listener(pid_t pid)
181 snprintf(pipename, sizeof(pipename), "\\\\.\\pipe\\pgsignal_%u", (int) pid);
183 pipe = CreateNamedPipe(pipename, PIPE_ACCESS_DUPLEX,
184 PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
185 PIPE_UNLIMITED_INSTANCES, 16, 16, 1000, NULL);
187 if (pipe == INVALID_HANDLE_VALUE)
189 (errmsg("could not create signal listener pipe for pid %d: error code %d",
190 (int) pid, (int) GetLastError())));
197 * All functions below execute on the signal handler thread
198 * and must be synchronized as such!
199 * NOTE! The only global variable that can be used is
205 pg_queue_signal(int signum)
207 if (signum >= PG_SIGNAL_COUNT || signum <= 0)
210 EnterCriticalSection(&pg_signal_crit_sec);
211 pg_signal_queue |= sigmask(signum);
212 LeaveCriticalSection(&pg_signal_crit_sec);
214 SetEvent(pgwin32_signal_event);
217 /* Signal dispatching thread */
219 pg_signal_dispatch_thread(LPVOID param)
221 HANDLE pipe = (HANDLE) param;
225 if (!ReadFile(pipe, &sigNum, 1, &bytes, NULL))
227 /* Client died before sending */
233 /* Received <bytes> bytes over signal pipe (should be 1) */
237 WriteFile(pipe, &sigNum, 1, &bytes, NULL); /* Don't care if it works or
239 FlushFileBuffers(pipe);
240 DisconnectNamedPipe(pipe);
243 pg_queue_signal(sigNum);
247 /* Signal handling thread */
249 pg_signal_thread(LPVOID param)
252 HANDLE pipe = pgwin32_initial_signal_pipe;
254 snprintf(pipename, sizeof(pipename), "\\\\.\\pipe\\pgsignal_%u", GetCurrentProcessId());
261 if (pipe == INVALID_HANDLE_VALUE)
263 pipe = CreateNamedPipe(pipename, PIPE_ACCESS_DUPLEX,
264 PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
265 PIPE_UNLIMITED_INSTANCES, 16, 16, 1000, NULL);
267 if (pipe == INVALID_HANDLE_VALUE)
269 write_stderr("could not create signal listener pipe: error code %d; retrying\n", (int) GetLastError());
275 fConnected = ConnectNamedPipe(pipe, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
278 hThread = CreateThread(NULL, 0,
279 (LPTHREAD_START_ROUTINE) pg_signal_dispatch_thread,
280 (LPVOID) pipe, 0, NULL);
281 if (hThread == INVALID_HANDLE_VALUE)
282 write_stderr("could not create signal dispatch thread: error code %d\n",
283 (int) GetLastError());
285 CloseHandle(hThread);
288 /* Connection failed. Cleanup and try again */
291 /* Set up so we create a new pipe on next loop */
292 pipe = INVALID_HANDLE_VALUE;
298 /* Console control handler will execute on a thread created
299 by the OS at the time of invocation */
301 pg_console_handler(DWORD dwCtrlType)
303 if (dwCtrlType == CTRL_C_EVENT ||
304 dwCtrlType == CTRL_BREAK_EVENT ||
305 dwCtrlType == CTRL_CLOSE_EVENT ||
306 dwCtrlType == CTRL_SHUTDOWN_EVENT)
308 pg_queue_signal(SIGINT);