summaryrefslogtreecommitdiff
path: root/src/backend/port/win32/signal.c
blob: 7afae60e02af785b4d045a3b98cd14f9b8762861 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*-------------------------------------------------------------------------
 *
 * signal.c
 *	  Microsoft Windows Win32 Signal Emulation Functions
 *
 * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
 *
 * IDENTIFICATION
 *	  src/backend/port/win32/signal.c
 *
 *-------------------------------------------------------------------------
 */

#include "postgres.h"

#include "libpq/pqsignal.h"

/*
 * These are exported for use by the UNBLOCKED_SIGNAL_QUEUE() macro.
 * pg_signal_queue must be volatile since it is changed by the signal
 * handling thread and inspected without any lock by the main thread.
 * pg_signal_mask is only changed by main thread so shouldn't need it.
 */
volatile int pg_signal_queue;
int			pg_signal_mask;

HANDLE		pgwin32_signal_event;
HANDLE		pgwin32_initial_signal_pipe = INVALID_HANDLE_VALUE;

/*
 * 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;

/* Note that array elements 0 are unused since they correspond to signal 0 */
static struct sigaction pg_signal_array[PG_SIGNAL_COUNT];
static pqsigfunc pg_signal_defaults[PG_SIGNAL_COUNT];


/* Signal handling thread functions */
static DWORD WINAPI pg_signal_thread(LPVOID param);
static BOOL WINAPI pg_console_handler(DWORD dwCtrlType);


/*
 * pg_usleep --- delay the specified number of microseconds, but
 * stop waiting if a signal arrives.
 *
 * This replaces the non-signal-aware version provided by src/port/pgsleep.c.
 */
void
pg_usleep(long microsec)
{
	if (unlikely(pgwin32_signal_event == NULL))
	{
		/*
		 * If we're reached by pgwin32_open_handle() early in startup before
		 * the signal event is set up, just fall back to a regular
		 * non-interruptible sleep.
		 */
		SleepEx((microsec < 500 ? 1 : (microsec + 500) / 1000), FALSE);
		return;
	}

	if (WaitForSingleObject(pgwin32_signal_event,
							(microsec < 500 ? 1 : (microsec + 500) / 1000))
		== WAIT_OBJECT_0)
	{
		pgwin32_dispatch_queued_signals();
		errno = EINTR;
		return;
	}
}


/* 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].sa_handler = SIG_DFL;
		pg_signal_array[i].sa_mask = 0;
		pg_signal_array[i].sa_flags = 0;
		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("could not create signal event: error code %lu", 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("could not create signal handler thread")));

	/* Create console control handle to pick up Ctrl-C etc */
	if (!SetConsoleCtrlHandler(pg_console_handler, TRUE))
		ereport(FATAL,
				(errmsg_internal("could not 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 pqsigprocmask() call.
 */
void
pgwin32_dispatch_queued_signals(void)
{
	int			exec_mask;

	Assert(pgwin32_signal_event != NULL);
	EnterCriticalSection(&pg_signal_crit_sec);
	while ((exec_mask = UNBLOCKED_SIGNAL_QUEUE()) != 0)
	{
		/* One or more unblocked signals queued for execution */
		int			i;

		for (i = 1; i < PG_SIGNAL_COUNT; i++)
		{
			if (exec_mask & sigmask(i))
			{
				/* Execute this signal */
				struct sigaction *act = &pg_signal_array[i];
				pqsigfunc	sig = act->sa_handler;

				if (sig == SIG_DFL)
					sig = pg_signal_defaults[i];
				pg_signal_queue &= ~sigmask(i);
				if (sig != SIG_ERR && sig != SIG_IGN && sig != SIG_DFL)
				{
					sigset_t	block_mask;
					sigset_t	save_mask;

					LeaveCriticalSection(&pg_signal_crit_sec);

					block_mask = act->sa_mask;
					if ((act->sa_flags & SA_NODEFER) == 0)
						block_mask |= sigmask(i);

					sigprocmask(SIG_BLOCK, &block_mask, &save_mask);
					sig(i);
					sigprocmask(SIG_SETMASK, &save_mask, NULL);

					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
pqsigprocmask(int how, const sigset_t *set, sigset_t *oset)
{
	if (oset)
		*oset = pg_signal_mask;

	if (!set)
		return 0;

	switch (how)
	{
		case SIG_BLOCK:
			pg_signal_mask |= *set;
			break;
		case SIG_UNBLOCK:
			pg_signal_mask &= ~*set;
			break;
		case SIG_SETMASK:
			pg_signal_mask = *set;
			break;
		default:
			errno = EINVAL;
			return -1;
	}

	/*
	 * Dispatch any signals queued up right away, in case we have unblocked
	 * one or more signals previously queued
	 */
	pgwin32_dispatch_queued_signals();

	return 0;
}

/*
 * Unix-like signal handler installation
 *
 * Only called on main thread, no sync required
 */
int
pqsigaction(int signum, const struct sigaction *act,
			struct sigaction *oldact)
{
	if (signum >= PG_SIGNAL_COUNT || signum < 0)
	{
		errno = EINVAL;
		return -1;
	}
	if (oldact)
		*oldact = pg_signal_array[signum];
	if (act)
		pg_signal_array[signum] = *act;
	return 0;
}

/* Create the signal listener pipe for specified PID */
HANDLE
pgwin32_create_signal_listener(pid_t pid)
{
	char		pipename[128];
	HANDLE		pipe;

	snprintf(pipename, sizeof(pipename), "\\\\.\\pipe\\pgsignal_%u", (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 %lu",
						(int) pid, 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!
 */


/*
 * Queue a signal for the main thread, by setting the flag bit and event.
 */
void
pg_queue_signal(int signum)
{
	Assert(pgwin32_signal_event != NULL);
	if (signum >= PG_SIGNAL_COUNT || signum <= 0)
		return;					/* ignore any bad signal number */

	EnterCriticalSection(&pg_signal_crit_sec);
	pg_signal_queue |= sigmask(signum);
	LeaveCriticalSection(&pg_signal_crit_sec);

	SetEvent(pgwin32_signal_event);
}

/* Signal handling thread */
static DWORD WINAPI
pg_signal_thread(LPVOID param)
{
	char		pipename[128];
	HANDLE		pipe = pgwin32_initial_signal_pipe;

	/* Set up pipe name, in case we have to re-create the pipe. */
	snprintf(pipename, sizeof(pipename), "\\\\.\\pipe\\pgsignal_%lu", GetCurrentProcessId());

	for (;;)
	{
		BOOL		fConnected;

		/* Create a new pipe instance if we don't have one. */
		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 %lu; retrying\n", GetLastError());
				SleepEx(500, FALSE);
				continue;
			}
		}

		/*
		 * Wait for a client to connect.  If something connects before we
		 * reach here, we'll get back a "failure" with ERROR_PIPE_CONNECTED,
		 * which is actually a success (way to go, Microsoft).
		 */
		fConnected = ConnectNamedPipe(pipe, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
		if (fConnected)
		{
			/*
			 * We have a connection from a would-be signal sender. Process it.
			 */
			BYTE		sigNum;
			DWORD		bytes;

			if (ReadFile(pipe, &sigNum, 1, &bytes, NULL) &&
				bytes == 1)
			{
				/*
				 * Queue the signal before responding to the client.  In this
				 * way, it's guaranteed that once kill() has returned in the
				 * signal sender, the next CHECK_FOR_INTERRUPTS() in the
				 * signal recipient will see the signal.  (This is a stronger
				 * guarantee than POSIX makes; maybe we don't need it?  But
				 * without it, we've seen timing bugs on Windows that do not
				 * manifest on any known Unix.)
				 */
				pg_queue_signal(sigNum);

				/*
				 * Write something back to the client, allowing its
				 * CallNamedPipe() call to terminate.
				 */
				WriteFile(pipe, &sigNum, 1, &bytes, NULL);	/* Don't care if it
															 * works or not */

				/*
				 * We must wait for the client to read the data before we can
				 * disconnect, else the data will be lost.  (If the WriteFile
				 * call failed, there'll be nothing in the buffer, so this
				 * shouldn't block.)
				 */
				FlushFileBuffers(pipe);
			}
			else
			{
				/*
				 * If we fail to read a byte from the client, assume it's the
				 * client's problem and do nothing.  Perhaps it'd be better to
				 * force a pipe close and reopen?
				 */
			}

			/* Disconnect from client so that we can re-use the pipe. */
			DisconnectNamedPipe(pipe);
		}
		else
		{
			/*
			 * Connection failed.  Cleanup and try again.
			 *
			 * This should never happen.  If it does, there's a window where
			 * we'll miss signals until we manage to re-create the pipe.
			 * However, just trying to use the same pipe again is probably not
			 * going to work, so we have little choice.
			 */
			CloseHandle(pipe);
			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)
{
	if (dwCtrlType == CTRL_C_EVENT ||
		dwCtrlType == CTRL_BREAK_EVENT ||
		dwCtrlType == CTRL_CLOSE_EVENT ||
		dwCtrlType == CTRL_SHUTDOWN_EVENT)
	{
		pg_queue_signal(SIGINT);
		return TRUE;
	}
	return FALSE;
}