Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf06e795

Browse files
author
Neil Conway
committed
Win32 signals cleanup. Patch by Magnus Hagander, with input from Claudio
Natoli and Bruce Momjian (and some cosmetic fixes from Neil Conway).Changes: - remove duplicate signal definitions from pqsignal.h - replace pqkill() with kill() and redefine kill() in Win32 - use ereport() in place of fprintf() in some error handling in pqsignal.c - export pg_queue_signal() and make use of it where necessary - add a console control handler for Ctrl-C and similar handling on Win32 - do WaitForSingleObjectEx() in CHECK_FOR_INTERRUPTS() on Win32; query cancelling should now work on Win32 - various other fixes and cleanups
1 parent04e82e5 commitf06e795

File tree

11 files changed

+118
-163
lines changed

11 files changed

+118
-163
lines changed

‎src/backend/commands/async.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
99
* IDENTIFICATION
10-
* $PostgreSQL: pgsql/src/backend/commands/async.c,v 1.108 2004/01/27 00:45:26 momjian Exp $
10+
* $PostgreSQL: pgsql/src/backend/commands/async.c,v 1.109 2004/02/08 22:28:56 neilc Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -83,7 +83,6 @@
8383
#include"commands/async.h"
8484
#include"libpq/libpq.h"
8585
#include"libpq/pqformat.h"
86-
#include"libpq/pqsignal.h"
8786
#include"miscadmin.h"
8887
#include"storage/ipc.h"
8988
#include"tcop/tcopprot.h"
@@ -498,7 +497,7 @@ AtCommit_Notify(void)
498497
* for some reason. It's OK to send the signal first, because
499498
* the other guy can't read pg_listener until we unlock it.
500499
*/
501-
if (pqkill(listenerPID,SIGUSR2)<0)
500+
if (kill(listenerPID,SIGUSR2)<0)
502501
{
503502
/*
504503
* Get rid of pg_listener entry if it refers to a PID that

‎src/backend/libpq/pqsignal.c

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $PostgreSQL: pgsql/src/backend/libpq/pqsignal.c,v 1.30 2004/01/27 00:46:58 momjian Exp $
12+
* $PostgreSQL: pgsql/src/backend/libpq/pqsignal.c,v 1.31 2004/02/08 22:28:56 neilc Exp $
1313
*
1414
* NOTES
1515
*This shouldn't be in libpq, but the monitor and some other
@@ -39,17 +39,12 @@
3939
*at all.
4040
* ------------------------------------------------------------------------*/
4141
#ifdefWIN32
42-
#defineWIN32_LEAN_AND_MEAN
4342
#define_WIN32_WINNT 0x0400
4443
#endif
4544

4645
#include"postgres.h"
4746

48-
#ifndefWIN32
4947
#include<signal.h>
50-
#else
51-
#include<windows.h>
52-
#endif
5348

5449
#include"libpq/pqsignal.h"
5550

@@ -180,6 +175,7 @@ HANDLEpgwin32_main_thread_handle;
180175

181176
/* Signal handling thread function */
182177
staticDWORDWINAPIpg_signal_thread(LPVOIDparam);
178+
staticBOOLWINAPIpg_console_handler(DWORDdwCtrlType);
183179

184180
/* Initialization */
185181
void
@@ -202,18 +198,18 @@ pgwin32_signal_initialize(void)
202198
if (!DuplicateHandle(GetCurrentProcess(),GetCurrentThread(),
203199
GetCurrentProcess(),&pgwin32_main_thread_handle,
204200
0, FALSE,DUPLICATE_SAME_ACCESS))
205-
{
206-
fprintf(stderr,gettext("Failed to get main thread handle!\n"));
207-
exit(1);
208-
}
209-
201+
ereport(FATAL,
202+
(errmsg_internal("Failed to get main thread handle!")));
203+
210204
/* Create thread for handling signals */
211205
signal_thread_handle=CreateThread(NULL,0,pg_signal_thread,NULL,0,NULL);
212206
if (signal_thread_handle==NULL)
213-
{
214-
fprintf(stderr,gettext("Failed to create signal handler thread!\n"));
215-
exit(1);
216-
}
207+
ereport(FATAL,
208+
(errmsg_internal("Failed to create signal handler thread!")));
209+
210+
if (!SetConsoleCtrlHandler(pg_console_handler, TRUE))
211+
ereport(FATAL,
212+
(errmsg_internal("Failed to set console control handler!")));
217213
}
218214

219215

@@ -344,7 +340,7 @@ pg_signal_apc(ULONG_PTR param)
344340
*/
345341

346342

347-
staticvoid
343+
void
348344
pg_queue_signal(intsignum)
349345
{
350346
if (signum >=PG_SIGNAL_COUNT||signum<0)
@@ -430,4 +426,20 @@ pg_signal_thread(LPVOID param)
430426
}
431427

432428

429+
/* Console control handler will execute on a thread created
430+
by the OS at the time of invocation */
431+
staticBOOLWINAPIpg_console_handler(DWORDdwCtrlType) {
432+
printf("Console handler being called!\n");
433+
fflush(stdout);
434+
if (dwCtrlType==CTRL_C_EVENT||
435+
dwCtrlType==CTRL_BREAK_EVENT||
436+
dwCtrlType==CTRL_CLOSE_EVENT||
437+
dwCtrlType==CTRL_SHUTDOWN_EVENT) {
438+
pg_queue_signal(SIGINT);
439+
return TRUE;
440+
}
441+
return FALSE;
442+
}
443+
444+
433445
#endif

‎src/backend/port/sysv_sema.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/port/sysv_sema.c,v 1.13 2004/01/27 00:45:26 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/port/sysv_sema.c,v 1.14 2004/02/08 22:28:56 neilc Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -31,7 +31,6 @@
3131
#include"miscadmin.h"
3232
#include"storage/ipc.h"
3333
#include"storage/pg_sema.h"
34-
#include"libpq/pqsignal.h"
3534

3635

3736
#ifndefHAVE_UNION_SEMUN
@@ -233,8 +232,7 @@ IpcSemaphoreCreate(int numSems)
233232
continue;/* oops, GETPID failed */
234233
if (creatorPID!=getpid())
235234
{
236-
if (pqkill(creatorPID,0)==0||
237-
errno!=ESRCH)
235+
if (kill(creatorPID,0)==0||errno!=ESRCH)
238236
continue;/* sema belongs to a live process */
239237
}
240238

‎src/backend/port/sysv_shmem.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Portions Copyright (c) 1994, Regents of the University of California
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/port/sysv_shmem.c,v 1.30 2004/02/02 00:11:31 momjian Exp $
13+
* $PostgreSQL: pgsql/src/backend/port/sysv_shmem.c,v 1.31 2004/02/08 22:28:56 neilc Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -33,7 +33,6 @@
3333
#include"miscadmin.h"
3434
#include"storage/ipc.h"
3535
#include"storage/pg_shmem.h"
36-
#include"libpq/pqsignal.h"
3736

3837

3938
typedefkey_tIpcMemoryKey;/* shared memory key passed to shmget(2) */
@@ -304,7 +303,7 @@ PGSharedMemoryCreate(uint32 size, bool makePrivate, int port)
304303
hdr= (PGShmemHeader*)memAddress;
305304
if (hdr->creatorPID!=getpid())
306305
{
307-
if (pqkill(hdr->creatorPID,0)==0||errno!=ESRCH)
306+
if (kill(hdr->creatorPID,0)==0||errno!=ESRCH)
308307
{
309308
shmdt(memAddress);
310309
continue;/* segment belongs to a live process */

‎src/backend/postmaster/postmaster.c

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*
3838
*
3939
* IDENTIFICATION
40-
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.364 2004/01/28 21:02:40 tgl Exp $
40+
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.365 2004/02/08 22:28:56 neilc Exp $
4141
*
4242
* NOTES
4343
*
@@ -308,6 +308,7 @@ pid_t win32_forkexec(const char* path, char *argv[]);
308308
staticvoidwin32_AddChild(pid_tpid,HANDLEhandle);
309309
staticvoidwin32_RemoveChild(pid_tpid);
310310
staticpid_twin32_waitpid(int*exitstatus);
311+
staticDWORDWINAPIwin32_sigchld_waiter(LPVOIDparam);
311312

312313
staticpid_t*win32_childPIDArray;
313314
staticHANDLE*win32_childHNDArray;
@@ -850,7 +851,7 @@ PostmasterMain(int argc, char *argv[])
850851
/* FIXME: [fork/exec] Ideally, we would resize these arrays with changes
851852
* in MaxBackends, but this'll do as a first order solution.
852853
*/
853-
win32_childPIDArray= (HANDLE*)malloc(NUM_BACKENDARRAY_ELEMS*sizeof(pid_t));
854+
win32_childPIDArray= (pid_t*)malloc(NUM_BACKENDARRAY_ELEMS*sizeof(pid_t));
854855
win32_childHNDArray= (HANDLE*)malloc(NUM_BACKENDARRAY_ELEMS*sizeof(HANDLE));
855856
if (!win32_childPIDArray|| !win32_childHNDArray)
856857
ereport(LOG,
@@ -1566,7 +1567,7 @@ processCancelRequest(Port *port, void *pkt)
15661567
ereport(DEBUG2,
15671568
(errmsg_internal("processing cancel request: sending SIGINT to process %d",
15681569
backendPID)));
1569-
pqkill(bp->pid,SIGINT);
1570+
kill(bp->pid,SIGINT);
15701571
}
15711572
else
15721573
/* Right PID, wrong key: no way, Jose */
@@ -1738,7 +1739,7 @@ SIGHUP_handler(SIGNAL_ARGS)
17381739
* will start a new one with a possibly changed config
17391740
*/
17401741
if (BgWriterPID!=0)
1741-
pqkill(BgWriterPID,SIGTERM);
1742+
kill(BgWriterPID,SIGTERM);
17421743
}
17431744

17441745
PG_SETMASK(&UnBlockSig);
@@ -1772,7 +1773,7 @@ pmdie(SIGNAL_ARGS)
17721773
* Wait for children to end their work and ShutdownDataBase.
17731774
*/
17741775
if (BgWriterPID!=0)
1775-
pqkill(BgWriterPID,SIGTERM);
1776+
kill(BgWriterPID,SIGTERM);
17761777
if (Shutdown >=SmartShutdown)
17771778
break;
17781779
Shutdown=SmartShutdown;
@@ -1806,7 +1807,7 @@ pmdie(SIGNAL_ARGS)
18061807
* and exit) and ShutdownDataBase when they are gone.
18071808
*/
18081809
if (BgWriterPID!=0)
1809-
pqkill(BgWriterPID,SIGTERM);
1810+
kill(BgWriterPID,SIGTERM);
18101811
if (Shutdown >=FastShutdown)
18111812
break;
18121813
ereport(LOG,
@@ -1854,13 +1855,13 @@ pmdie(SIGNAL_ARGS)
18541855
* properly shutdown data base system.
18551856
*/
18561857
if (BgWriterPID!=0)
1857-
pqkill(BgWriterPID,SIGQUIT);
1858+
kill(BgWriterPID,SIGQUIT);
18581859
ereport(LOG,
18591860
(errmsg("received immediate shutdown request")));
18601861
if (ShutdownPID>0)
1861-
pqkill(ShutdownPID,SIGQUIT);
1862+
kill(ShutdownPID,SIGQUIT);
18621863
if (StartupPID>0)
1863-
pqkill(StartupPID,SIGQUIT);
1864+
kill(StartupPID,SIGQUIT);
18641865
if (DLGetHead(BackendList))
18651866
SignalChildren(SIGQUIT);
18661867
ExitPostmaster(0);
@@ -2130,7 +2131,7 @@ CleanupProc(int pid,
21302131
(errmsg_internal("sending %s to process %d",
21312132
(SendStop ?"SIGSTOP" :"SIGQUIT"),
21322133
(int)bp->pid)));
2133-
pqkill(bp->pid, (SendStop ?SIGSTOP :SIGQUIT));
2134+
kill(bp->pid, (SendStop ?SIGSTOP :SIGQUIT));
21342135
}
21352136
}
21362137
else
@@ -2225,7 +2226,7 @@ SignalChildren(int signal)
22252226
(errmsg_internal("sending signal %d to process %d",
22262227
signal,
22272228
(int)bp->pid)));
2228-
pqkill(bp->pid,signal);
2229+
kill(bp->pid,signal);
22292230
}
22302231

22312232
curr=next;
@@ -3491,6 +3492,8 @@ pid_t win32_forkexec(const char* path, char *argv[])
34913492
char*p;
34923493
inti;
34933494
charcmdLine[MAXPGPATH];
3495+
HANDLEchildHandleCopy;
3496+
HANDLEwaiterThread;
34943497

34953498
/* Format the cmd line */
34963499
snprintf(cmdLine,sizeof(cmdLine),"%s",path);
@@ -3522,7 +3525,23 @@ pid_t win32_forkexec(const char* path, char *argv[])
35223525
if (!IsUnderPostmaster)
35233526
/* We are the Postmaster creating a child... */
35243527
win32_AddChild(pi.dwProcessId,pi.hProcess);
3525-
else
3528+
3529+
if (!DuplicateHandle(GetCurrentProcess(),
3530+
pi.hProcess,
3531+
GetCurrentProcess(),
3532+
&childHandleCopy,
3533+
0,
3534+
FALSE,
3535+
DUPLICATE_SAME_ACCESS))
3536+
ereport(FATAL,
3537+
(errmsg_internal("failed to duplicate child handle: %i",GetLastError())));
3538+
waiterThread=CreateThread(NULL,64*1024,win32_sigchld_waiter, (LPVOID)childHandleCopy,0,NULL);
3539+
if (!waiterThread)
3540+
ereport(FATAL,
3541+
(errmsg_internal("failed to create sigchld waiter thread: %i",GetLastError())));
3542+
CloseHandle(waiterThread);
3543+
3544+
if (IsUnderPostmaster)
35263545
CloseHandle(pi.hProcess);
35273546
CloseHandle(pi.hThread);
35283547

@@ -3566,8 +3585,8 @@ static void win32_RemoveChild(pid_t pid)
35663585

35673586
/* Swap last entry into the "removed" one */
35683587
--win32_numChildren;
3569-
win32_childPIDArray[win32_numChildren]=win32_childPIDArray[i];
3570-
win32_childHNDArray[win32_numChildren]=win32_childHNDArray[i];
3588+
win32_childPIDArray[i]=win32_childPIDArray[win32_numChildren];
3589+
win32_childHNDArray[i]=win32_childHNDArray[win32_numChildren];
35713590
return;
35723591
}
35733592
}
@@ -3597,8 +3616,8 @@ static pid_t win32_waitpid(int *exitstatus)
35973616
{
35983617
caseWAIT_FAILED:
35993618
ereport(ERROR,
3600-
(errmsg_internal("failed to wait on %d children",
3601-
win32_numChildren)));
3619+
(errmsg_internal("failed to wait on %d children: %i",
3620+
win32_numChildren,GetLastError())));
36023621
/* Fall through to WAIT_TIMEOUTs return */
36033622

36043623
caseWAIT_TIMEOUT:
@@ -3626,4 +3645,18 @@ static pid_t win32_waitpid(int *exitstatus)
36263645
return-1;
36273646
}
36283647

3648+
/* Note! Code belows executes on separate threads, one for
3649+
each child process created */
3650+
staticDWORDWINAPIwin32_sigchld_waiter(LPVOIDparam) {
3651+
HANDLEprocHandle= (HANDLE)param;
3652+
3653+
DWORDr=WaitForSingleObject(procHandle,INFINITE);
3654+
if (r==WAIT_OBJECT_0)
3655+
pg_queue_signal(SIGCHLD);
3656+
else
3657+
fprintf(stderr,"ERROR: Failed to wait on child process handle: %i\n",GetLastError());
3658+
CloseHandle(procHandle);
3659+
return0;
3660+
}
3661+
36293662
#endif

‎src/backend/storage/ipc/pmsignal.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/storage/ipc/pmsignal.c,v 1.12 2004/01/27 00:45:26 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/storage/ipc/pmsignal.c,v 1.13 2004/02/08 22:28:56 neilc Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -20,7 +20,6 @@
2020
#include"miscadmin.h"
2121
#include"storage/pmsignal.h"
2222
#include"storage/shmem.h"
23-
#include"libpq/pqsignal.h"
2423

2524

2625
/*
@@ -65,7 +64,7 @@ SendPostmasterSignal(PMSignalReason reason)
6564
/* Atomically set the proper flag */
6665
PMSignalFlags[reason]= true;
6766
/* Send signal to postmaster */
68-
pqkill(PostmasterPid,SIGUSR1);
67+
kill(PostmasterPid,SIGUSR1);
6968
}
7069

7170
/*

‎src/backend/storage/lmgr/proc.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/storage/lmgr/proc.c,v 1.145 2004/01/27 00:45:26 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/storage/lmgr/proc.c,v 1.146 2004/02/08 22:28:56 neilc Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -51,7 +51,6 @@
5151
#include"storage/proc.h"
5252
#include"storage/sinval.h"
5353
#include"storage/spin.h"
54-
#include"libpq/pqsignal.h"
5554

5655
/* GUC variables */
5756
intDeadlockTimeout=1000;
@@ -1131,7 +1130,7 @@ CheckStatementTimeout(void)
11311130
{
11321131
/* Time to die */
11331132
statement_timeout_active= false;
1134-
pqkill(MyProcPid,SIGINT);
1133+
kill(MyProcPid,SIGINT);
11351134
}
11361135
else
11371136
{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp