pgsql: Rework signal handler infrastructure to pass sender info as argu - Mailing list pgsql-committers

From Andrew Dunstan
Subject pgsql: Rework signal handler infrastructure to pass sender info as argu
Date
Msg-id E1wCyYE-001529-2i@gemulon.postgresql.org
Whole thread
List pgsql-committers
Rework signal handler infrastructure to pass sender info as argument.

Commit 095c9d4cf06 added errdetail() reporting of the PID and UID of
the process that sent a termination signal.  However, as noted by
Andres Freund, the implementation had architectural problems:

1. wrapper_handler() in pqsignal.c contained SIGTERM-specific logic
   (setting ProcDieSenderPid/Uid), violating its role as a generic
   signal dispatch wrapper.

2. Using globals to pass sender info between wrapper_handler and the
   real handler is unsafe when signals nest on some platforms.

3. The syncrep.c errdetail used psprintf() to conditionally embed
   text via %s, breaking translatability.

Adopt the approach proposed by Andres Freund: introduce a
pg_signal_info struct that is passed as an argument to all signal
handlers via the SIGNAL_ARGS macro.  wrapper_handler populates it
from siginfo_t when SA_SIGINFO is available, or with zeros otherwise.
This keeps wrapper_handler fully generic and avoids any globals for
passing signal metadata.

Since pqsigfunc now has a different signature from the system's
signal handler type, SIG_IGN and SIG_DFL can no longer be passed
directly to pqsignal().  Introduce PG_SIG_IGN and PG_SIG_DFL macros
that cast to the new pqsigfunc type, and update all call sites.
The legacy pqsignal() in libpq retains its original signature via
a local typedef.

Only die() reads pg_siginfo today, copying the sender PID/UID into
ProcDieSenderPid/Uid for later use by ProcessInterrupts().  Only the
first SIGTERM's sender info is recorded.

Also fix the syncrep.c translatability issue by using separate ereport
calls with complete, independently translatable errdetail strings.

Also make the psql TAP test require the DETAIL line on platforms with
SA_SIGINFO, rather than making it unconditionally optional.

On Windows, pg_signal_info uses uint32_t for pid and uid fields
since pid_t/uid_t are not available early enough in the include
chain.  The Windows signal dispatch in pgwin32_dispatch_queued_signals()
passes a zeroed pg_signal_info to handlers.

Author: Andres Freund <andres@anarazel.de>
Author: Jakub Wartak <jakub.wartak@enterprisedb.com>
Reviewed-by: Andrew Dunstan <andrew@dunslane.net>
Reviewed-by: Chao Li <li.evan.chao@gmail.com>
Discussion: https://postgr.es/m/cwyyryh2veejuxbj5ifzyaejw7jhhqc5mrdeq56xckknsdecn2@6hzfcxde2nm5
Discussion: https://postgr.es/m/jygesyr7mwg7ovdbxpmjvvbi3hccptpkcreqb645h7f56puwbz@hmkkwi3melfe

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/3e2a1496bae628c379ca0a11ef5f5ba666f24ae8

Modified Files
--------------
src/backend/bootstrap/bootstrap.c           |  8 +--
src/backend/port/win32/signal.c             | 14 +++--
src/backend/postmaster/autovacuum.c         | 10 +--
src/backend/postmaster/bgworker.c           | 14 ++---
src/backend/postmaster/bgwriter.c           | 10 +--
src/backend/postmaster/checkpointer.c       |  8 +--
src/backend/postmaster/datachecksum_state.c |  2 +-
src/backend/postmaster/pgarch.c             |  8 +--
src/backend/postmaster/postmaster.c         | 12 ++--
src/backend/postmaster/startup.c            |  6 +-
src/backend/postmaster/syslogger.c          | 14 ++---
src/backend/postmaster/walsummarizer.c      | 10 +--
src/backend/postmaster/walwriter.c          | 10 +--
src/backend/replication/logical/slotsync.c  |  6 +-
src/backend/replication/syncrep.c           | 28 ++++-----
src/backend/replication/walreceiver.c       | 10 +--
src/backend/replication/walsender.c         |  4 +-
src/backend/storage/aio/method_worker.c     |  6 +-
src/backend/storage/file/fd.c               |  4 +-
src/backend/storage/ipc/waiteventset.c      |  2 +-
src/backend/tcop/postgres.c                 | 19 ++++--
src/bin/initdb/initdb.c                     |  4 +-
src/bin/pg_ctl/pg_ctl.c                     |  2 +-
src/bin/pg_dump/parallel.c                  |  8 +--
src/bin/psql/t/001_basic.pl                 |  5 +-
src/fe_utils/print.c                        |  4 +-
src/include/c.h                             | 29 ++++++---
src/include/port.h                          |  3 +
src/interfaces/libpq/legacy-pqsignal.c      |  8 ++-
src/port/pqsignal.c                         | 96 ++++++++++++++++++++---------
src/test/regress/pg_regress.c               |  2 +-
src/tools/pgindent/typedefs.list            |  1 +
32 files changed, 218 insertions(+), 149 deletions(-)


pgsql-committers by date:

Previous
From: Aleksander Alekseev
Date:
Subject: Re: pgsql: doc: first draft of PG 19 release notes
Next
From: Andrew Dunstan
Date:
Subject: pgsql: Fix COPY TO FORMAT JSON to exclude generated columns.