Skip to content

Commit cfd7aad

Browse files
committed
Avoid symbol collisions between pqsignal.c and legacy-pqsignal.c.
In the name of ABI stability (that is, to avoid a library major version bump for libpq), libpq still exports a version of pqsignal() that we no longer want to use ourselves. However, since that has the same link name as the function exported by src/port/pqsignal.c, there is a link ordering dependency determining which version will actually get used by code that uses libpq as well as libpgport.a. It now emerges that the wrong version has been used by pgbench and psql since commit 06843df rearranged their link commands. This can result in odd failures in pgbench with the -T switch, since its SIGALRM handler will now not be marked SA_RESTART. psql may have some edge-case problems in \watch, too. Since we don't want to depend on link ordering effects anymore, let's fix this in the same spirit as b6c7cfa: use macros to change the actual link names of the competing functions. We cannot change legacy-pqsignal.c's exported name of course, so the victim has to be src/port/pqsignal.c. In master, rename its exported name to be pqsignal_fe in frontend or pqsignal_be in backend. (We could perhaps have gotten away with using the same symbol in both cases, but since the FE and BE versions now work a little differently, it seems advisable to use different names.) In back branches, rename to pqsignal_fe in frontend but keep it as pqsignal in backend. The frontend change could affect third-party code that is calling pqsignal from libpgport.a or libpgport_shlib.a, but only if the code is compiled against port.h from a different minor release than libpgport. Since we don't support using libpgport as a shared library, it seems unlikely that there will be such a problem. I left the backend symbol unchanged to avoid an ABI break for extensions. This means that the link ordering hazard still exists for any extension that links against libpq. However, none of our own extensions use both pqsignal() and libpq, and we're not making things any worse for third-party extensions that do. Report from Andy Fan, diagnosis by Fujii Masao, patch by me. Back-patch to all supported branches, as 06843df was. Discussion: https://postgr.es/m/87msfz5qv2.fsf@163.com
1 parent f217c41 commit cfd7aad

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

src/include/port.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,10 @@ extern int pg_check_dir(const char *dir);
550550
/* port/pgmkdirp.c */
551551
extern int pg_mkdir_p(char *path, int omode);
552552

553-
/* port/pqsignal.c */
553+
/* port/pqsignal.c (see also interfaces/libpq/legacy-pqsignal.c) */
554+
#ifdef FRONTEND
555+
#define pqsignal pqsignal_fe
556+
#endif
554557
typedef void (*pqsigfunc) (int signo);
555558
extern pqsigfunc pqsignal(int signo, pqsigfunc func);
556559

src/interfaces/libpq/legacy-pqsignal.c

+9-3
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,16 @@
2828
* with the semantics it had in 9.2; in particular, this has different
2929
* behavior for SIGALRM than the version in src/port/pqsignal.c.
3030
*
31-
* libpq itself uses this only for SIGPIPE (and even then, only in
32-
* non-ENABLE_THREAD_SAFETY builds), so the incompatibility isn't
33-
* troublesome for internal references.
31+
* libpq itself does not use this, nor does anything else in our code.
32+
*
33+
* src/include/port.h will #define pqsignal as pqsignal_fe,
34+
* but here we want to export just plain "pqsignal". We can't rely on
35+
* port.h's extern declaration either. (The point of that #define
36+
* is to ensure that no in-tree code accidentally calls this version.)
3437
*/
38+
#undef pqsignal
39+
extern pqsigfunc pqsignal(int signo, pqsigfunc func);
40+
3541
pqsigfunc
3642
pqsignal(int signo, pqsigfunc func)
3743
{

src/port/pqsignal.c

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
* Set up a signal handler, with SA_RESTART, for signal "signo"
3636
*
3737
* Returns the previous handler.
38+
*
39+
* Note: the actual name of this function is either pqsignal_fe when
40+
* compiled with -DFRONTEND, or pqsignal when compiled without that.
41+
* This is to avoid a name collision with libpq's legacy-pqsignal.c.
3842
*/
3943
pqsigfunc
4044
pqsignal(int signo, pqsigfunc func)

0 commit comments

Comments
 (0)