@@ -341,6 +341,14 @@ emit_tap_output_v(TAPtype type, const char *fmt, va_list argp)
341
341
{
342
342
va_list argp_logfile ;
343
343
FILE * fp ;
344
+ int save_errno ;
345
+
346
+ /*
347
+ * The fprintf() calls used to output TAP-protocol elements might clobber
348
+ * errno, so save it here and restore it before vfprintf()-ing the user's
349
+ * format string, in case it contains %m placeholders.
350
+ */
351
+ save_errno = errno ;
344
352
345
353
/*
346
354
* Diagnostic output will be hidden by prove unless printed to stderr. The
@@ -379,9 +387,13 @@ emit_tap_output_v(TAPtype type, const char *fmt, va_list argp)
379
387
if (logfile )
380
388
fprintf (logfile , "# " );
381
389
}
390
+ errno = save_errno ;
382
391
vfprintf (fp , fmt , argp );
383
392
if (logfile )
393
+ {
394
+ errno = save_errno ;
384
395
vfprintf (logfile , fmt , argp_logfile );
396
+ }
385
397
386
398
/*
387
399
* If we are entering into a note with more details to follow, register
@@ -492,10 +504,7 @@ make_temp_sockdir(void)
492
504
493
505
temp_sockdir = mkdtemp (template );
494
506
if (temp_sockdir == NULL )
495
- {
496
- bail ("could not create directory \"%s\": %s" ,
497
- template , strerror (errno ));
498
- }
507
+ bail ("could not create directory \"%s\": %m" , template );
499
508
500
509
/* Stage file names for remove_temp(). Unsafe in a signal handler. */
501
510
UNIXSOCK_PATH (sockself , port , temp_sockdir );
@@ -616,8 +625,7 @@ load_resultmap(void)
616
625
/* OK if it doesn't exist, else complain */
617
626
if (errno == ENOENT )
618
627
return ;
619
- bail ("could not open file \"%s\" for reading: %s" ,
620
- buf , strerror (errno ));
628
+ bail ("could not open file \"%s\" for reading: %m" , buf );
621
629
}
622
630
623
631
while (fgets (buf , sizeof (buf ), f ))
@@ -1046,10 +1054,7 @@ config_sspi_auth(const char *pgdata, const char *superuser_name)
1046
1054
#define CW (cond ) \
1047
1055
do { \
1048
1056
if (!(cond)) \
1049
- { \
1050
- bail("could not write to file \"%s\": %s", \
1051
- fname, strerror(errno)); \
1052
- } \
1057
+ bail("could not write to file \"%s\": %m", fname); \
1053
1058
} while (0)
1054
1059
1055
1060
res = snprintf (fname , sizeof (fname ), "%s/pg_hba.conf" , pgdata );
@@ -1064,8 +1069,7 @@ config_sspi_auth(const char *pgdata, const char *superuser_name)
1064
1069
hba = fopen (fname , "w" );
1065
1070
if (hba == NULL )
1066
1071
{
1067
- bail ("could not open file \"%s\" for writing: %s" ,
1068
- fname , strerror (errno ));
1072
+ bail ("could not open file \"%s\" for writing: %m" , fname );
1069
1073
}
1070
1074
CW (fputs ("# Configuration written by config_sspi_auth()\n" , hba ) >= 0 );
1071
1075
CW (fputs ("host all all 127.0.0.1/32 sspi include_realm=1 map=regress\n" ,
@@ -1079,8 +1083,7 @@ config_sspi_auth(const char *pgdata, const char *superuser_name)
1079
1083
ident = fopen (fname , "w" );
1080
1084
if (ident == NULL )
1081
1085
{
1082
- bail ("could not open file \"%s\" for writing: %s" ,
1083
- fname , strerror (errno ));
1086
+ bail ("could not open file \"%s\" for writing: %m" , fname );
1084
1087
}
1085
1088
CW (fputs ("# Configuration written by config_sspi_auth()\n" , ident ) >= 0 );
1086
1089
@@ -1210,7 +1213,7 @@ spawn_process(const char *cmdline)
1210
1213
pid = fork ();
1211
1214
if (pid == -1 )
1212
1215
{
1213
- bail ("could not fork: %s" , strerror ( errno ) );
1216
+ bail ("could not fork: %m" );
1214
1217
}
1215
1218
if (pid == 0 )
1216
1219
{
@@ -1226,7 +1229,7 @@ spawn_process(const char *cmdline)
1226
1229
cmdline2 = psprintf ("exec %s" , cmdline );
1227
1230
execl (shellprog , shellprog , "-c" , cmdline2 , (char * ) NULL );
1228
1231
/* Not using the normal bail() here as we want _exit */
1229
- bail_noatexit ("could not exec \"%s\": %s " , shellprog , strerror ( errno ) );
1232
+ bail_noatexit ("could not exec \"%s\": %m " , shellprog );
1230
1233
}
1231
1234
/* in parent */
1232
1235
return pid ;
@@ -1262,8 +1265,7 @@ file_size(const char *file)
1262
1265
1263
1266
if (!f )
1264
1267
{
1265
- diag ("could not open file \"%s\" for reading: %s" ,
1266
- file , strerror (errno ));
1268
+ diag ("could not open file \"%s\" for reading: %m" , file );
1267
1269
return -1 ;
1268
1270
}
1269
1271
fseek (f , 0 , SEEK_END );
@@ -1284,8 +1286,7 @@ file_line_count(const char *file)
1284
1286
1285
1287
if (!f )
1286
1288
{
1287
- diag ("could not open file \"%s\" for reading: %s" ,
1288
- file , strerror (errno ));
1289
+ diag ("could not open file \"%s\" for reading: %m" , file );
1289
1290
return -1 ;
1290
1291
}
1291
1292
while ((c = fgetc (f )) != EOF )
@@ -1325,9 +1326,7 @@ static void
1325
1326
make_directory (const char * dir )
1326
1327
{
1327
1328
if (mkdir (dir , S_IRWXU | S_IRWXG | S_IRWXO ) < 0 )
1328
- {
1329
- bail ("could not create directory \"%s\": %s" , dir , strerror (errno ));
1330
- }
1329
+ bail ("could not create directory \"%s\": %m" , dir );
1331
1330
}
1332
1331
1333
1332
/*
@@ -1456,10 +1455,7 @@ results_differ(const char *testname, const char *resultsfile, const char *defaul
1456
1455
1457
1456
alt_expectfile = get_alternative_expectfile (expectfile , i );
1458
1457
if (!alt_expectfile )
1459
- {
1460
- bail ("Unable to check secondary comparison files: %s" ,
1461
- strerror (errno ));
1462
- }
1458
+ bail ("Unable to check secondary comparison files: %m" );
1463
1459
1464
1460
if (!file_exists (alt_expectfile ))
1465
1461
{
@@ -1572,9 +1568,7 @@ wait_for_tests(PID_TYPE * pids, int *statuses, instr_time *stoptimes,
1572
1568
p = wait (& exit_status );
1573
1569
1574
1570
if (p == INVALID_PID )
1575
- {
1576
- bail ("failed to wait for subprocesses: %s" , strerror (errno ));
1577
- }
1571
+ bail ("failed to wait for subprocesses: %m" );
1578
1572
#else
1579
1573
DWORD exit_status ;
1580
1574
int r ;
@@ -1664,10 +1658,7 @@ run_schedule(const char *schedule, test_start_function startfunc,
1664
1658
1665
1659
scf = fopen (schedule , "r" );
1666
1660
if (!scf )
1667
- {
1668
- bail ("could not open file \"%s\" for reading: %s" ,
1669
- schedule , strerror (errno ));
1670
- }
1661
+ bail ("could not open file \"%s\" for reading: %m" , schedule );
1671
1662
1672
1663
while (fgets (scbuf , sizeof (scbuf ), scf ))
1673
1664
{
@@ -1931,20 +1922,15 @@ open_result_files(void)
1931
1922
logfilename = pg_strdup (file );
1932
1923
logfile = fopen (logfilename , "w" );
1933
1924
if (!logfile )
1934
- {
1935
- bail ("could not open file \"%s\" for writing: %s" ,
1936
- logfilename , strerror (errno ));
1937
- }
1925
+ bail ("could not open file \"%s\" for writing: %m" , logfilename );
1938
1926
1939
1927
/* create the diffs file as empty */
1940
1928
snprintf (file , sizeof (file ), "%s/regression.diffs" , outputdir );
1941
1929
difffilename = pg_strdup (file );
1942
1930
difffile = fopen (difffilename , "w" );
1943
1931
if (!difffile )
1944
- {
1945
- bail ("could not open file \"%s\" for writing: %s" ,
1946
- difffilename , strerror (errno ));
1947
- }
1932
+ bail ("could not open file \"%s\" for writing: %m" , difffilename );
1933
+
1948
1934
/* we don't keep the diffs file open continuously */
1949
1935
fclose (difffile );
1950
1936
@@ -2406,10 +2392,8 @@ regression_main(int argc, char *argv[],
2406
2392
snprintf (buf , sizeof (buf ), "%s/data/postgresql.conf" , temp_instance );
2407
2393
pg_conf = fopen (buf , "a" );
2408
2394
if (pg_conf == NULL )
2409
- {
2410
- bail ("could not open \"%s\" for adding extra config: %s" ,
2411
- buf , strerror (errno ));
2412
- }
2395
+ bail ("could not open \"%s\" for adding extra config: %m" , buf );
2396
+
2413
2397
fputs ("\n# Configuration added by pg_regress\n\n" , pg_conf );
2414
2398
fputs ("log_autovacuum_min_duration = 0\n" , pg_conf );
2415
2399
fputs ("log_checkpoints = on\n" , pg_conf );
@@ -2427,8 +2411,8 @@ regression_main(int argc, char *argv[],
2427
2411
extra_conf = fopen (temp_config , "r" );
2428
2412
if (extra_conf == NULL )
2429
2413
{
2430
- bail ("could not open \"%s\" to read extra config: %s " ,
2431
- temp_config , strerror ( errno ) );
2414
+ bail ("could not open \"%s\" to read extra config: %m " ,
2415
+ temp_config );
2432
2416
}
2433
2417
while (fgets (line_buf , sizeof (line_buf ), extra_conf ) != NULL )
2434
2418
fputs (line_buf , pg_conf );
@@ -2503,7 +2487,7 @@ regression_main(int argc, char *argv[],
2503
2487
outputdir );
2504
2488
postmaster_pid = spawn_process (buf );
2505
2489
if (postmaster_pid == INVALID_PID )
2506
- bail ("could not spawn postmaster: %s" , strerror ( errno ) );
2490
+ bail ("could not spawn postmaster: %m" );
2507
2491
2508
2492
/*
2509
2493
* Wait till postmaster is able to accept connections; normally takes
@@ -2566,7 +2550,7 @@ regression_main(int argc, char *argv[],
2566
2550
*/
2567
2551
#ifndef WIN32
2568
2552
if (kill (postmaster_pid , SIGKILL ) != 0 && errno != ESRCH )
2569
- bail ("could not kill failed postmaster: %s" , strerror ( errno ) );
2553
+ bail ("could not kill failed postmaster: %m" );
2570
2554
#else
2571
2555
if (TerminateProcess (postmaster_pid , 255 ) == 0 )
2572
2556
bail ("could not kill failed postmaster: error code %lu" ,
0 commit comments