Skip to content

Commit d4a43b2

Browse files
Convert libpgport's pqsignal() to a void function.
The protections added by commit 3b00fdb introduced race conditions to this function that can lead to bogus return values. Since nobody seems to inspect the return value, this is of little consequence, but it would have been nice to convert it to a void function to avoid any possibility of a bogus return value. I originally thought that doing so would have required also modifying legacy-pqsignal.c's version of the function (which would've required an SONAME bump), but commit 9a45a89 gave legacy-pqsignal.c its own dedicated extern for pqsignal(), thereby decoupling it enough that libpgport's pqsignal() can be modified. This commit also adds an assertion for the return value of sigaction()/signal(). Since a failure most likely indicates a coding error, and nobody has ever bothered to check pqsignal()'s return value, it's probably not worth the effort to do anything fancier. Reviewed-by: Tom Lane Discussion: https://postgr.es/m/Z4chOKfnthRH71mw%40nathan
1 parent 5cda4fd commit d4a43b2

File tree

2 files changed

+7
-29
lines changed

2 files changed

+7
-29
lines changed

src/include/port.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ extern int pg_mkdir_p(char *path, int omode);
520520
#define pqsignal pqsignal_be
521521
#endif
522522
typedef void (*pqsigfunc) (SIGNAL_ARGS);
523-
extern pqsigfunc pqsignal(int signo, pqsigfunc func);
523+
extern void pqsignal(int signo, pqsigfunc func);
524524

525525
/* port/quotes.c */
526526
extern char *escape_single_quotes_ascii(const char *src);

src/port/pqsignal.c

+6-28
Original file line numberDiff line numberDiff line change
@@ -112,31 +112,15 @@ wrapper_handler(SIGNAL_ARGS)
112112
/*
113113
* Set up a signal handler, with SA_RESTART, for signal "signo"
114114
*
115-
* Returns the previous handler.
116-
*
117-
* NB: If called within a signal handler, race conditions may lead to bogus
118-
* return values. You should either avoid calling this within signal handlers
119-
* or ignore the return value.
120-
*
121-
* XXX: Since no in-tree callers use the return value, and there is little
122-
* reason to do so, it would be nice if we could convert this to a void
123-
* function instead of providing potentially-bogus return values.
124-
* Unfortunately, that requires modifying the pqsignal() in legacy-pqsignal.c,
125-
* which in turn requires an SONAME bump, which is probably not worth it.
126-
*
127115
* Note: the actual name of this function is either pqsignal_fe when
128116
* compiled with -DFRONTEND, or pqsignal_be when compiled without that.
129117
* This is to avoid a name collision with libpq's legacy-pqsignal.c.
130118
*/
131-
pqsigfunc
119+
void
132120
pqsignal(int signo, pqsigfunc func)
133121
{
134-
pqsigfunc orig_func = pqsignal_handlers[signo]; /* assumed atomic */
135122
#if !(defined(WIN32) && defined(FRONTEND))
136-
struct sigaction act,
137-
oact;
138-
#else
139-
pqsigfunc ret;
123+
struct sigaction act;
140124
#endif
141125

142126
Assert(signo < PG_NSIG);
@@ -155,17 +139,11 @@ pqsignal(int signo, pqsigfunc func)
155139
if (signo == SIGCHLD)
156140
act.sa_flags |= SA_NOCLDSTOP;
157141
#endif
158-
if (sigaction(signo, &act, &oact) < 0)
159-
return SIG_ERR;
160-
else if (oact.sa_handler == wrapper_handler)
161-
return orig_func;
162-
else
163-
return oact.sa_handler;
142+
if (sigaction(signo, &act, NULL) < 0)
143+
Assert(false); /* probably indicates coding error */
164144
#else
165145
/* Forward to Windows native signal system. */
166-
if ((ret = signal(signo, func)) == wrapper_handler)
167-
return orig_func;
168-
else
169-
return ret;
146+
if (signal(signo, func) == SIG_ERR)
147+
Assert(false); /* probably indicates coding error */
170148
#endif
171149
}

0 commit comments

Comments
 (0)