Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7fed801

Browse files
committed
Clean up inconsistent use of fflush().
More than twenty years ago (79fcde4), we hacked the postmaster to avoid a core-dump on systems that didn't support fflush(NULL). We've mostly, though not completely, hewed to that rule ever since. But such systems are surely gone in the wild, so in the spirit of cleaning out no-longer-needed portability hacks let's get rid of multiple per-file fflush() calls in favor of using fflush(NULL). Also, we were fairly inconsistent about whether to fflush() before popen() and system() calls. While we've received no bug reports about that, it seems likely that at least some of these call sites are at risk of odd behavior, such as error messages appearing in an unexpected order. Rather than expend a lot of brain cells figuring out which places are at hazard, let's just establish a uniform coding rule that we should fflush(NULL) before these calls. A no-op fflush() is surely of trivial cost compared to launching a sub-process via a shell; while if it's not a no-op then we likely need it. Discussion: https://postgr.es/m/[email protected]
1 parent 2079653 commit 7fed801

File tree

24 files changed

+52
-41
lines changed

24 files changed

+52
-41
lines changed

src/backend/access/transam/xlogarchive.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ RestoreArchivedFile(char *path, const char *xlogfname,
169169
/*
170170
* Copy xlog from archival storage to XLOGDIR
171171
*/
172+
fflush(NULL);
172173
pgstat_report_wait_start(WAIT_EVENT_RESTORE_COMMAND);
173174
rc = system(xlogRestoreCmd);
174175
pgstat_report_wait_end();
@@ -358,6 +359,7 @@ ExecuteRecoveryCommand(const char *command, const char *commandName,
358359
/*
359360
* execute the constructed command
360361
*/
362+
fflush(NULL);
361363
pgstat_report_wait_start(wait_event_info);
362364
rc = system(xlogRecoveryCmd);
363365
pgstat_report_wait_end();

src/backend/postmaster/fork_process.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,8 @@ fork_process(void)
3737

3838
/*
3939
* Flush stdio channels just before fork, to avoid double-output problems.
40-
* Ideally we'd use fflush(NULL) here, but there are still a few non-ANSI
41-
* stdio libraries out there (like SunOS 4.1.x) that coredump if we do.
42-
* Presently stdout and stderr are the only stdio output channels used by
43-
* the postmaster, so fflush'ing them should be sufficient.
4440
*/
45-
fflush(stdout);
46-
fflush(stderr);
41+
fflush(NULL);
4742

4843
#ifdef LINUX_PROFILE
4944

src/backend/postmaster/shell_archive.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ shell_archive_file(const char *file, const char *path)
9999
(errmsg_internal("executing archive command \"%s\"",
100100
xlogarchcmd)));
101101

102+
fflush(NULL);
102103
pgstat_report_wait_start(WAIT_EVENT_ARCHIVE_COMMAND);
103104
rc = system(xlogarchcmd);
104105
pgstat_report_wait_end();

src/backend/storage/file/fd.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2503,8 +2503,7 @@ OpenPipeStream(const char *command, const char *mode)
25032503
ReleaseLruFiles();
25042504

25052505
TryAgain:
2506-
fflush(stdout);
2507-
fflush(stderr);
2506+
fflush(NULL);
25082507
pqsignal(SIGPIPE, SIG_DFL);
25092508
errno = 0;
25102509
file = popen(command, mode);

src/backend/utils/error/elog.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -643,8 +643,7 @@ errfinish(const char *filename, int lineno, const char *funcname)
643643
* Any other code you might be tempted to add here should probably be
644644
* in an on_proc_exit or on_shmem_exit callback instead.
645645
*/
646-
fflush(stdout);
647-
fflush(stderr);
646+
fflush(NULL);
648647

649648
/*
650649
* Let the cumulative stats system know. Only mark the session as
@@ -670,8 +669,7 @@ errfinish(const char *filename, int lineno, const char *funcname)
670669
* XXX: what if we are *in* the postmaster? abort() won't kill our
671670
* children...
672671
*/
673-
fflush(stdout);
674-
fflush(stderr);
672+
fflush(NULL);
675673
abort();
676674
}
677675

src/bin/initdb/initdb.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,8 +489,7 @@ popen_check(const char *command, const char *mode)
489489
{
490490
FILE *cmdfd;
491491

492-
fflush(stdout);
493-
fflush(stderr);
492+
fflush(NULL);
494493
errno = 0;
495494
cmdfd = popen(command, mode);
496495
if (cmdfd == NULL)
@@ -914,6 +913,7 @@ test_config_settings(void)
914913
test_conns, test_buffs,
915914
dynamic_shared_memory_type,
916915
DEVNULL, DEVNULL);
916+
fflush(NULL);
917917
status = system(cmd);
918918
if (status == 0)
919919
{
@@ -950,6 +950,7 @@ test_config_settings(void)
950950
n_connections, test_buffs,
951951
dynamic_shared_memory_type,
952952
DEVNULL, DEVNULL);
953+
fflush(NULL);
953954
status = system(cmd);
954955
if (status == 0)
955956
break;

src/bin/pg_ctl/pg_ctl.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,7 @@ start_postmaster(void)
448448
pgpid_t pm_pid;
449449

450450
/* Flush stdio channels just before fork, to avoid double-output problems */
451-
fflush(stdout);
452-
fflush(stderr);
451+
fflush(NULL);
453452

454453
#ifdef EXEC_BACKEND
455454
pg_disable_aslr();
@@ -916,6 +915,7 @@ do_init(void)
916915
cmd = psprintf("\"%s\" %s%s > \"%s\"",
917916
exec_path, pgdata_opt, post_opts, DEVNULL);
918917

918+
fflush(NULL);
919919
if (system(cmd) != 0)
920920
{
921921
write_stderr(_("%s: database system initialization failed\n"), progname);
@@ -2222,6 +2222,7 @@ adjust_data_dir(void)
22222222
my_exec_path,
22232223
pgdata_opt ? pgdata_opt : "",
22242224
post_opts ? post_opts : "");
2225+
fflush(NULL);
22252226

22262227
fd = popen(cmd, "r");
22272228
if (fd == NULL || fgets(filename, sizeof(filename), fd) == NULL)

src/bin/pg_dump/pg_dumpall.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,8 +1578,7 @@ runPgDump(const char *dbname, const char *create_opts)
15781578

15791579
pg_log_info("running \"%s\"", cmd->data);
15801580

1581-
fflush(stdout);
1582-
fflush(stderr);
1581+
fflush(NULL);
15831582

15841583
ret = system(cmd->data);
15851584

src/bin/pg_rewind/pg_rewind.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,7 @@ ensureCleanShutdown(const char *argv0)
11511151
appendPQExpBufferStr(postgres_cmd, " template1 < ");
11521152
appendShellString(postgres_cmd, DEVNULL);
11531153

1154+
fflush(NULL);
11541155
if (system(postgres_cmd->data) != 0)
11551156
{
11561157
pg_log_error("postgres single-user mode in target cluster failed");

src/bin/pg_upgrade/controldata.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ get_control_data(ClusterInfo *cluster, bool live_check)
123123
/* only pg_controldata outputs the cluster state */
124124
snprintf(cmd, sizeof(cmd), "\"%s/pg_controldata\" \"%s\"",
125125
cluster->bindir, cluster->pgdata);
126-
fflush(stdout);
127-
fflush(stderr);
126+
fflush(NULL);
128127

129128
if ((output = popen(cmd, "r")) == NULL)
130129
pg_fatal("could not get control data using %s: %s",
@@ -191,8 +190,7 @@ get_control_data(ClusterInfo *cluster, bool live_check)
191190
cluster->bindir,
192191
live_check ? "pg_controldata\"" : resetwal_bin,
193192
cluster->pgdata);
194-
fflush(stdout);
195-
fflush(stderr);
193+
fflush(NULL);
196194

197195
if ((output = popen(cmd, "r")) == NULL)
198196
pg_fatal("could not get control data using %s: %s",

0 commit comments

Comments
 (0)