postgresql/src/include/port
Thomas Munro 1da569ca1f Don't leak descriptors into subprograms.
Open long-lived data and WAL file descriptors with O_CLOEXEC.  This flag
was introduced by SUSv4 (POSIX.1-2008), and by now all of our target
Unix systems have it.  Our open() implementation for Windows already had
that behavior, so provide a dummy O_CLOEXEC flag on that platform.

For now, callers of open() and the "thin" wrappers in fd.c that deal in
raw descriptors need to pass in O_CLOEXEC explicitly if desired.  This
commit does that for WAL files, and automatically for everything
accessed via VFDs including SMgrRelation and BufFile.  (With more
discussion we might decide to turn it on automatically for the thin
open()-wrappers too to avoid risk of missing places that need it, but
these are typically used for short-lived descriptors where we don't
expect to fork/exec, and it's remotely possible that extensions could be
using these APIs and passing descriptors to subprograms deliberately, so
that hasn't been done here.)

Do the same for sockets and the postmaster pipe with FD_CLOEXEC.  (Later
commits might use modern interfaces to remove these extra fcntl() calls
and more where possible, but we'll need them as a fallback for a couple
of systems, so do it that way in this initial commit.)

With this change, subprograms executed for archiving, copying etc will
no longer have access to the server's descriptors, other than the ones
that we decide to pass down.

Reviewed-by: Andres Freund <andres@anarazel.de> (earlier version)
Discussion: https://postgr.es/m/CA%2BhUKGKb6FsAdQWcRL35KJsftv%2B9zXqQbzwkfRf1i0J2e57%2BhQ%40mail.gmail.com
2023-03-03 10:43:33 +13:00
..
atomics Update copyright for 2023 2023-01-02 15:00:37 -05:00
win32 Remove configure probe for netinet/tcp.h. 2022-08-18 16:31:11 +12:00
win32_msvc windows: msvc: Define STDIN/OUT/ERR_FILENO. 2022-07-18 17:21:54 -07:00
aix.h Attempt to work around a 32bit xlc compiler bug from a different place. 2015-08-08 01:19:02 +02:00
atomics.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
cygwin.h Simplify declaring variables exported from libpgcommon and libpgport. 2021-11-29 11:00:00 -05:00
darwin.h Refer to OS X as "macOS", except for the port name which is still "darwin". 2016-09-25 15:40:57 -04:00
freebsd.h Remove configure probe for fdatasync. 2022-08-05 16:37:38 +12:00
linux.h pgindent run for 9.4 2014-05-06 12:12:18 -04:00
netbsd.h Remove cvs keywords from all files. 2010-09-20 22:08:53 +02:00
openbsd.h Remove cvs keywords from all files. 2010-09-20 22:08:53 +02:00
pg_bitutils.h Remove newly added asserts from pg_bitutils.h 2023-02-22 17:22:43 +07:00
pg_bswap.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
pg_crc32c.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
pg_iovec.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
pg_lfind.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
pg_pthread.h Add missing pthread_barrier_t. 2021-03-10 17:44:04 +13:00
simd.h Fix various typos in code and tests 2023-02-09 14:43:53 +09:00
solaris.h Remove obsolete HAVE_BUGGY_SOLARIS_STRTOD 2020-08-15 11:27:41 +02:00
win32.h mingw: Define PGDLLEXPORT as __declspec (dllexport) as done for msvc 2022-09-30 10:50:05 -07:00
win32_port.h Don't leak descriptors into subprograms. 2023-03-03 10:43:33 +13:00
win32ntdll.h Update copyright for 2023 2023-01-02 15:00:37 -05:00