Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 28e4632

Browse files
Centralize logic for restoring errno in signal handlers.
Presently, we rely on each individual signal handler to save the initial value of errno and then restore it before returning if needed. This is easily forgotten and, if missed, often goes undetected for a long time. In commit 3b00fdb, we introduced a wrapper signal handler function that checks whether MyProcPid matches getpid(). This commit moves the aforementioned errno restoration code from the individual signal handlers to the new wrapper handler so that we no longer need to worry about missing it. Reviewed-by: Andres Freund, Noah Misch Discussion: https://postgr.es/m/20231121212008.GA3742740%40nathanxps13
1 parent 3b00fdb commit 28e4632

File tree

15 files changed

+6
-87
lines changed

15 files changed

+6
-87
lines changed

doc/src/sgml/sources.sgml

-8
Original file line numberDiff line numberDiff line change
@@ -1007,18 +1007,10 @@ MemoryContextSwitchTo(MemoryContext context)
10071007
static void
10081008
handle_sighup(SIGNAL_ARGS)
10091009
{
1010-
int save_errno = errno;
1011-
10121010
got_SIGHUP = true;
10131011
SetLatch(MyLatch);
1014-
1015-
errno = save_errno;
10161012
}
10171013
</programlisting>
1018-
<varname>errno</varname> is saved and restored because
1019-
<function>SetLatch()</function> might change it. If that were not done
1020-
interrupted code that's currently inspecting <varname>errno</varname> might see the wrong
1021-
value.
10221014
</para>
10231015
</simplesect>
10241016

src/backend/postmaster/autovacuum.c

-4
Original file line numberDiff line numberDiff line change
@@ -1410,12 +1410,8 @@ AutoVacWorkerFailed(void)
14101410
static void
14111411
avl_sigusr2_handler(SIGNAL_ARGS)
14121412
{
1413-
int save_errno = errno;
1414-
14151413
got_SIGUSR2 = true;
14161414
SetLatch(MyLatch);
1417-
1418-
errno = save_errno;
14191415
}
14201416

14211417

src/backend/postmaster/checkpointer.c

-4
Original file line numberDiff line numberDiff line change
@@ -852,15 +852,11 @@ IsCheckpointOnSchedule(double progress)
852852
static void
853853
ReqCheckpointHandler(SIGNAL_ARGS)
854854
{
855-
int save_errno = errno;
856-
857855
/*
858856
* The signaling process should have set ckpt_flags nonzero, so all we
859857
* need do is ensure that our main loop gets kicked out of any wait.
860858
*/
861859
SetLatch(MyLatch);
862-
863-
errno = save_errno;
864860
}
865861

866862

src/backend/postmaster/interrupt.c

-8
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,8 @@ HandleMainLoopInterrupts(void)
6060
void
6161
SignalHandlerForConfigReload(SIGNAL_ARGS)
6262
{
63-
int save_errno = errno;
64-
6563
ConfigReloadPending = true;
6664
SetLatch(MyLatch);
67-
68-
errno = save_errno;
6965
}
7066

7167
/*
@@ -108,10 +104,6 @@ SignalHandlerForCrashExit(SIGNAL_ARGS)
108104
void
109105
SignalHandlerForShutdownRequest(SIGNAL_ARGS)
110106
{
111-
int save_errno = errno;
112-
113107
ShutdownRequestPending = true;
114108
SetLatch(MyLatch);
115-
116-
errno = save_errno;
117109
}

src/backend/postmaster/pgarch.c

-4
Original file line numberDiff line numberDiff line change
@@ -283,13 +283,9 @@ PgArchWakeup(void)
283283
static void
284284
pgarch_waken_stop(SIGNAL_ARGS)
285285
{
286-
int save_errno = errno;
287-
288286
/* set flag to do a final cycle and shut down afterwards */
289287
ready_to_stop = true;
290288
SetLatch(MyLatch);
291-
292-
errno = save_errno;
293289
}
294290

295291
/*

src/backend/postmaster/postmaster.c

-16
Original file line numberDiff line numberDiff line change
@@ -2612,12 +2612,8 @@ InitProcessGlobals(void)
26122612
static void
26132613
handle_pm_pmsignal_signal(SIGNAL_ARGS)
26142614
{
2615-
int save_errno = errno;
2616-
26172615
pending_pm_pmsignal = true;
26182616
SetLatch(MyLatch);
2619-
2620-
errno = save_errno;
26212617
}
26222618

26232619
/*
@@ -2626,12 +2622,8 @@ handle_pm_pmsignal_signal(SIGNAL_ARGS)
26262622
static void
26272623
handle_pm_reload_request_signal(SIGNAL_ARGS)
26282624
{
2629-
int save_errno = errno;
2630-
26312625
pending_pm_reload_request = true;
26322626
SetLatch(MyLatch);
2633-
2634-
errno = save_errno;
26352627
}
26362628

26372629
/*
@@ -2711,8 +2703,6 @@ process_pm_reload_request(void)
27112703
static void
27122704
handle_pm_shutdown_request_signal(SIGNAL_ARGS)
27132705
{
2714-
int save_errno = errno;
2715-
27162706
switch (postgres_signal_arg)
27172707
{
27182708
case SIGTERM:
@@ -2729,8 +2719,6 @@ handle_pm_shutdown_request_signal(SIGNAL_ARGS)
27292719
break;
27302720
}
27312721
SetLatch(MyLatch);
2732-
2733-
errno = save_errno;
27342722
}
27352723

27362724
/*
@@ -2890,12 +2878,8 @@ process_pm_shutdown_request(void)
28902878
static void
28912879
handle_pm_child_exit_signal(SIGNAL_ARGS)
28922880
{
2893-
int save_errno = errno;
2894-
28952881
pending_pm_child_exit = true;
28962882
SetLatch(MyLatch);
2897-
2898-
errno = save_errno;
28992883
}
29002884

29012885
/*

src/backend/postmaster/startup.c

-12
Original file line numberDiff line numberDiff line change
@@ -95,32 +95,22 @@ static void StartupProcExit(int code, Datum arg);
9595
static void
9696
StartupProcTriggerHandler(SIGNAL_ARGS)
9797
{
98-
int save_errno = errno;
99-
10098
promote_signaled = true;
10199
WakeupRecovery();
102-
103-
errno = save_errno;
104100
}
105101

106102
/* SIGHUP: set flag to re-read config file at next convenient time */
107103
static void
108104
StartupProcSigHupHandler(SIGNAL_ARGS)
109105
{
110-
int save_errno = errno;
111-
112106
got_SIGHUP = true;
113107
WakeupRecovery();
114-
115-
errno = save_errno;
116108
}
117109

118110
/* SIGTERM: set flag to abort redo and exit */
119111
static void
120112
StartupProcShutdownHandler(SIGNAL_ARGS)
121113
{
122-
int save_errno = errno;
123-
124114
if (in_restore_command)
125115
{
126116
/*
@@ -139,8 +129,6 @@ StartupProcShutdownHandler(SIGNAL_ARGS)
139129
else
140130
shutdown_requested = true;
141131
WakeupRecovery();
142-
143-
errno = save_errno;
144132
}
145133

146134
/*

src/backend/postmaster/syslogger.c

-4
Original file line numberDiff line numberDiff line change
@@ -1642,10 +1642,6 @@ RemoveLogrotateSignalFiles(void)
16421642
static void
16431643
sigUsr1Handler(SIGNAL_ARGS)
16441644
{
1645-
int save_errno = errno;
1646-
16471645
rotation_requested = true;
16481646
SetLatch(MyLatch);
1649-
1650-
errno = save_errno;
16511647
}

src/backend/replication/walsender.c

-4
Original file line numberDiff line numberDiff line change
@@ -3476,12 +3476,8 @@ HandleWalSndInitStopping(void)
34763476
static void
34773477
WalSndLastCycleHandler(SIGNAL_ARGS)
34783478
{
3479-
int save_errno = errno;
3480-
34813479
got_SIGUSR2 = true;
34823480
SetLatch(MyLatch);
3483-
3484-
errno = save_errno;
34853481
}
34863482

34873483
/* Set up signal handlers */

src/backend/storage/ipc/latch.c

-4
Original file line numberDiff line numberDiff line change
@@ -2243,12 +2243,8 @@ GetNumRegisteredWaitEvents(WaitEventSet *set)
22432243
static void
22442244
latch_sigurg_handler(SIGNAL_ARGS)
22452245
{
2246-
int save_errno = errno;
2247-
22482246
if (waiting)
22492247
sendSelfPipeByte();
2250-
2251-
errno = save_errno;
22522248
}
22532249

22542250
/* Send one byte to the self-pipe, to wake up WaitLatch */

src/backend/storage/ipc/procsignal.c

-4
Original file line numberDiff line numberDiff line change
@@ -638,8 +638,6 @@ CheckProcSignal(ProcSignalReason reason)
638638
void
639639
procsignal_sigusr1_handler(SIGNAL_ARGS)
640640
{
641-
int save_errno = errno;
642-
643641
if (CheckProcSignal(PROCSIG_CATCHUP_INTERRUPT))
644642
HandleCatchupInterrupt();
645643

@@ -683,6 +681,4 @@ procsignal_sigusr1_handler(SIGNAL_ARGS)
683681
HandleRecoveryConflictInterrupt(PROCSIG_RECOVERY_CONFLICT_BUFFERPIN);
684682

685683
SetLatch(MyLatch);
686-
687-
errno = save_errno;
688684
}

src/backend/tcop/postgres.c

-8
Original file line numberDiff line numberDiff line change
@@ -2970,8 +2970,6 @@ quickdie(SIGNAL_ARGS)
29702970
void
29712971
die(SIGNAL_ARGS)
29722972
{
2973-
int save_errno = errno;
2974-
29752973
/* Don't joggle the elbow of proc_exit */
29762974
if (!proc_exit_inprogress)
29772975
{
@@ -2993,8 +2991,6 @@ die(SIGNAL_ARGS)
29932991
*/
29942992
if (DoingCommandRead && whereToSendOutput != DestRemote)
29952993
ProcessInterrupts();
2996-
2997-
errno = save_errno;
29982994
}
29992995

30002996
/*
@@ -3004,8 +3000,6 @@ die(SIGNAL_ARGS)
30043000
void
30053001
StatementCancelHandler(SIGNAL_ARGS)
30063002
{
3007-
int save_errno = errno;
3008-
30093003
/*
30103004
* Don't joggle the elbow of proc_exit
30113005
*/
@@ -3017,8 +3011,6 @@ StatementCancelHandler(SIGNAL_ARGS)
30173011

30183012
/* If we're still here, waken anything waiting on the process latch */
30193013
SetLatch(MyLatch);
3020-
3021-
errno = save_errno;
30223014
}
30233015

30243016
/* signal handler for floating point exception */

src/backend/utils/misc/timeout.c

-4
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,6 @@ schedule_alarm(TimestampTz now)
363363
static void
364364
handle_sig_alarm(SIGNAL_ARGS)
365365
{
366-
int save_errno = errno;
367-
368366
/*
369367
* Bump the holdoff counter, to make sure nothing we call will process
370368
* interrupts directly. No timeout handler should do that, but these
@@ -452,8 +450,6 @@ handle_sig_alarm(SIGNAL_ARGS)
452450
}
453451

454452
RESUME_INTERRUPTS();
455-
456-
errno = save_errno;
457453
}
458454

459455

src/fe_utils/cancel.c

-3
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ ResetCancelConn(void)
152152
static void
153153
handle_sigint(SIGNAL_ARGS)
154154
{
155-
int save_errno = errno;
156155
char errbuf[256];
157156

158157
CancelRequested = true;
@@ -173,8 +172,6 @@ handle_sigint(SIGNAL_ARGS)
173172
write_stderr(errbuf);
174173
}
175174
}
176-
177-
errno = save_errno; /* just in case the write changed it */
178175
}
179176

180177
/*

src/port/pqsignal.c

+6
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,14 @@ static volatile pqsigfunc pqsignal_handlers[PG_NSIG];
8080
* processes do not modify shared memory, which is often detrimental. If the
8181
* check succeeds, the function originally provided to pqsignal() is called.
8282
* Otherwise, the default signal handler is installed and then called.
83+
*
84+
* This wrapper also handles restoring the value of errno.
8385
*/
8486
static void
8587
wrapper_handler(SIGNAL_ARGS)
8688
{
89+
int save_errno = errno;
90+
8791
#ifndef FRONTEND
8892

8993
/*
@@ -102,6 +106,8 @@ wrapper_handler(SIGNAL_ARGS)
102106
#endif
103107

104108
(*pqsignal_handlers[postgres_signal_arg]) (postgres_signal_arg);
109+
110+
errno = save_errno;
105111
}
106112

107113
/*

0 commit comments

Comments
 (0)