Skip to content

Commit 6b54f12

Browse files
committed
In count_usable_fds(), duplicate stderr not stdin.
We had a complaint that the postmaster fails to start if the invoking program closes stdin. That happens because count_usable_fds expects to be able to dup(0), and if it can't, we conclude there are no free FDs and go belly-up. So far as I can find, though, there is no other place in the server that touches stdin, and it's not unreasonable to expect that a daemon wouldn't use that file. As a simple improvement, let's dup FD 2 (stderr) instead. Unlike stdin, it *is* reasonable for us to expect that stderr be open; even if we are configured not to touch it, common libraries such as libc might try to write error messages there. Per gripe from Mario Emmenlauer. Given the lack of previous complaints, I'm not excited about pushing this into stable branches, but it seems OK to squeeze it into v14. Discussion: https://postgr.es/m/48bafc63-c30f-3962-2ded-f2e985d93e86@emmenlauer.de
1 parent 23c6bc5 commit 6b54f12

File tree

1 file changed

+5
-3
lines changed
  • src/backend/storage/file

1 file changed

+5
-3
lines changed

src/backend/storage/file/fd.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,9 @@ InitFileAccess(void)
900900
* of already_open will give the right answer. In practice, max_to_probe
901901
* of a couple of dozen should be enough to ensure good results.
902902
*
903-
* We assume stdin (FD 0) is available for dup'ing
903+
* We assume stderr (FD 2) is available for dup'ing. While the calling
904+
* script could theoretically close that, it would be a really bad idea,
905+
* since then one risks loss of error messages from, e.g., libc.
904906
*/
905907
static void
906908
count_usable_fds(int max_to_probe, int *usable_fds, int *already_open)
@@ -944,12 +946,12 @@ count_usable_fds(int max_to_probe, int *usable_fds, int *already_open)
944946
break;
945947
#endif
946948

947-
thisfd = dup(0);
949+
thisfd = dup(2);
948950
if (thisfd < 0)
949951
{
950952
/* Expect EMFILE or ENFILE, else it's fishy */
951953
if (errno != EMFILE && errno != ENFILE)
952-
elog(WARNING, "dup(0) failed after %d successes: %m", used);
954+
elog(WARNING, "duplicating stderr file descriptor failed after %d successes: %m", used);
953955
break;
954956
}
955957

0 commit comments

Comments
 (0)