Skip to content

Commit 3913a40

Browse files
committed
initdb: Use atexit()
Replace exit_nicely() calls with standard exit() and register the cleanup actions using atexit(). The coding pattern used here mirrors existing use in pg_basebackup.c. Reviewed-by: Alvaro Herrera <alvherre@2ndquadrant.com> Reviewed-by: Michael Paquier <michael@paquier.xyz> Discussion: https://www.postgresql.org/message-id/flat/ec4135ba-84e9-28bf-b584-0e78d47448d5@2ndquadrant.com/
1 parent a4205fa commit 3913a40

File tree

1 file changed

+43
-39
lines changed

1 file changed

+43
-39
lines changed

src/bin/initdb/initdb.c

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ static char *dictionary_file;
159159
static char *info_schema_file;
160160
static char *features_file;
161161
static char *system_views_file;
162+
static bool success = false;
162163
static bool made_new_pgdata = false;
163164
static bool found_existing_pgdata = false;
164165
static bool made_new_xlogdir = false;
@@ -237,7 +238,6 @@ static char **filter_lines_with_token(char **lines, const char *token);
237238
static char **readfile(const char *path);
238239
static void writefile(char *path, char **lines);
239240
static FILE *popen_check(const char *command, const char *mode);
240-
static void exit_nicely(void) pg_attribute_noreturn();
241241
static char *get_id(void);
242242
static int get_encoding_id(const char *encoding_name);
243243
static void set_input(char **dest, const char *filename);
@@ -291,13 +291,13 @@ void initialize_data_directory(void);
291291
do { \
292292
cmdfd = popen_check(cmd, "w"); \
293293
if (cmdfd == NULL) \
294-
exit_nicely(); /* message already printed by popen_check */ \
294+
exit(1); /* message already printed by popen_check */ \
295295
} while (0)
296296

297297
#define PG_CMD_CLOSE \
298298
do { \
299299
if (pclose_check(cmdfd)) \
300-
exit_nicely(); /* message already printed by pclose_check */ \
300+
exit(1); /* message already printed by pclose_check */ \
301301
} while (0)
302302

303303
#define PG_CMD_PUTS(line) \
@@ -493,7 +493,7 @@ readfile(const char *path)
493493
{
494494
fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
495495
progname, path, strerror(errno));
496-
exit_nicely();
496+
exit(1);
497497
}
498498

499499
/* pass over the file twice - the first time to size the result */
@@ -549,23 +549,23 @@ writefile(char *path, char **lines)
549549
{
550550
fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
551551
progname, path, strerror(errno));
552-
exit_nicely();
552+
exit(1);
553553
}
554554
for (line = lines; *line != NULL; line++)
555555
{
556556
if (fputs(*line, out_file) < 0)
557557
{
558558
fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
559559
progname, path, strerror(errno));
560-
exit_nicely();
560+
exit(1);
561561
}
562562
free(*line);
563563
}
564564
if (fclose(out_file))
565565
{
566566
fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
567567
progname, path, strerror(errno));
568-
exit_nicely();
568+
exit(1);
569569
}
570570
}
571571

@@ -592,8 +592,11 @@ popen_check(const char *command, const char *mode)
592592
* if we created the data directory remove it too
593593
*/
594594
static void
595-
exit_nicely(void)
595+
cleanup_directories_atexit(void)
596596
{
597+
if (success)
598+
return;
599+
597600
if (!noclean)
598601
{
599602
if (made_new_pgdata)
@@ -645,8 +648,6 @@ exit_nicely(void)
645648
_("%s: WAL directory \"%s\" not removed at user's request\n"),
646649
progname, xlog_dir);
647650
}
648-
649-
exit(1);
650651
}
651652

652653
/*
@@ -877,14 +878,14 @@ write_version_file(const char *extrapath)
877878
{
878879
fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
879880
progname, path, strerror(errno));
880-
exit_nicely();
881+
exit(1);
881882
}
882883
if (fprintf(version_file, "%s\n", PG_MAJORVERSION) < 0 ||
883884
fclose(version_file))
884885
{
885886
fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
886887
progname, path, strerror(errno));
887-
exit_nicely();
888+
exit(1);
888889
}
889890
free(path);
890891
}
@@ -905,13 +906,13 @@ set_null_conf(void)
905906
{
906907
fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
907908
progname, path, strerror(errno));
908-
exit_nicely();
909+
exit(1);
909910
}
910911
if (fclose(conf_file))
911912
{
912913
fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
913914
progname, path, strerror(errno));
914-
exit_nicely();
915+
exit(1);
915916
}
916917
free(path);
917918
}
@@ -1262,7 +1263,7 @@ setup_config(void)
12621263
{
12631264
fprintf(stderr, _("%s: could not change permissions of \"%s\": %s\n"),
12641265
progname, path, strerror(errno));
1265-
exit_nicely();
1266+
exit(1);
12661267
}
12671268

12681269
/*
@@ -1282,7 +1283,7 @@ setup_config(void)
12821283
{
12831284
fprintf(stderr, _("%s: could not change permissions of \"%s\": %s\n"),
12841285
progname, path, strerror(errno));
1285-
exit_nicely();
1286+
exit(1);
12861287
}
12871288

12881289
free(conflines);
@@ -1369,7 +1370,7 @@ setup_config(void)
13691370
{
13701371
fprintf(stderr, _("%s: could not change permissions of \"%s\": %s\n"),
13711372
progname, path, strerror(errno));
1372-
exit_nicely();
1373+
exit(1);
13731374
}
13741375

13751376
free(conflines);
@@ -1385,7 +1386,7 @@ setup_config(void)
13851386
{
13861387
fprintf(stderr, _("%s: could not change permissions of \"%s\": %s\n"),
13871388
progname, path, strerror(errno));
1388-
exit_nicely();
1389+
exit(1);
13891390
}
13901391

13911392
free(conflines);
@@ -1423,7 +1424,7 @@ bootstrap_template1(void)
14231424
"Check your installation or specify the correct path "
14241425
"using the option -L.\n"),
14251426
progname, bki_file, PG_VERSION);
1426-
exit_nicely();
1427+
exit(1);
14271428
}
14281429

14291430
/* Substitute for various symbols used in the BKI file */
@@ -1541,7 +1542,7 @@ get_su_pwd(void)
15411542
if (strcmp(pwd1, pwd2) != 0)
15421543
{
15431544
fprintf(stderr, _("Passwords didn't match.\n"));
1544-
exit_nicely();
1545+
exit(1);
15451546
}
15461547
}
15471548
else
@@ -1561,7 +1562,7 @@ get_su_pwd(void)
15611562
{
15621563
fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
15631564
progname, pwfilename, strerror(errno));
1564-
exit_nicely();
1565+
exit(1);
15651566
}
15661567
if (!fgets(pwd1, sizeof(pwd1), pwf))
15671568
{
@@ -1571,7 +1572,7 @@ get_su_pwd(void)
15711572
else
15721573
fprintf(stderr, _("%s: password file \"%s\" is empty\n"),
15731574
progname, pwfilename);
1574-
exit_nicely();
1575+
exit(1);
15751576
}
15761577
fclose(pwf);
15771578

@@ -2104,7 +2105,7 @@ make_postgres(FILE *cmdfd)
21042105
* if you are handling SIGFPE.
21052106
*
21062107
* I avoided doing the forbidden things by setting a flag instead of calling
2107-
* exit_nicely() directly.
2108+
* exit() directly.
21082109
*
21092110
* Also note the behaviour of Windows with SIGINT, which says this:
21102111
* Note SIGINT is not supported for any Win32 application, including
@@ -2125,7 +2126,7 @@ trapsig(int signum)
21252126
}
21262127

21272128
/*
2128-
* call exit_nicely() if we got a signal, or else output "ok".
2129+
* call exit() if we got a signal, or else output "ok".
21292130
*/
21302131
static void
21312132
check_ok(void)
@@ -2134,14 +2135,14 @@ check_ok(void)
21342135
{
21352136
printf(_("caught signal\n"));
21362137
fflush(stdout);
2137-
exit_nicely();
2138+
exit(1);
21382139
}
21392140
else if (output_failed)
21402141
{
21412142
printf(_("could not write to child process: %s\n"),
21422143
strerror(output_errno));
21432144
fflush(stdout);
2144-
exit_nicely();
2145+
exit(1);
21452146
}
21462147
else
21472148
{
@@ -2775,7 +2776,7 @@ create_data_directory(void)
27752776
{
27762777
fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
27772778
progname, pg_data, strerror(errno));
2778-
exit_nicely();
2779+
exit(1);
27792780
}
27802781
else
27812782
check_ok();
@@ -2793,7 +2794,7 @@ create_data_directory(void)
27932794
{
27942795
fprintf(stderr, _("%s: could not change permissions of directory \"%s\": %s\n"),
27952796
progname, pg_data, strerror(errno));
2796-
exit_nicely();
2797+
exit(1);
27972798
}
27982799
else
27992800
check_ok();
@@ -2822,7 +2823,7 @@ create_data_directory(void)
28222823
/* Trouble accessing directory */
28232824
fprintf(stderr, _("%s: could not access directory \"%s\": %s\n"),
28242825
progname, pg_data, strerror(errno));
2825-
exit_nicely();
2826+
exit(1);
28262827
}
28272828
}
28282829

@@ -2845,7 +2846,7 @@ create_xlog_or_symlink(void)
28452846
if (!is_absolute_path(xlog_dir))
28462847
{
28472848
fprintf(stderr, _("%s: WAL directory location must be an absolute path\n"), progname);
2848-
exit_nicely();
2849+
exit(1);
28492850
}
28502851

28512852
/* check if the specified xlog directory exists/is empty */
@@ -2861,7 +2862,7 @@ create_xlog_or_symlink(void)
28612862
{
28622863
fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
28632864
progname, xlog_dir, strerror(errno));
2864-
exit_nicely();
2865+
exit(1);
28652866
}
28662867
else
28672868
check_ok();
@@ -2879,7 +2880,7 @@ create_xlog_or_symlink(void)
28792880
{
28802881
fprintf(stderr, _("%s: could not change permissions of directory \"%s\": %s\n"),
28812882
progname, xlog_dir, strerror(errno));
2882-
exit_nicely();
2883+
exit(1);
28832884
}
28842885
else
28852886
check_ok();
@@ -2901,25 +2902,25 @@ create_xlog_or_symlink(void)
29012902
_("If you want to store the WAL there, either remove or empty the directory\n"
29022903
"\"%s\".\n"),
29032904
xlog_dir);
2904-
exit_nicely();
2905+
exit(1);
29052906

29062907
default:
29072908
/* Trouble accessing directory */
29082909
fprintf(stderr, _("%s: could not access directory \"%s\": %s\n"),
29092910
progname, xlog_dir, strerror(errno));
2910-
exit_nicely();
2911+
exit(1);
29112912
}
29122913

29132914
#ifdef HAVE_SYMLINK
29142915
if (symlink(xlog_dir, subdirloc) != 0)
29152916
{
29162917
fprintf(stderr, _("%s: could not create symbolic link \"%s\": %s\n"),
29172918
progname, subdirloc, strerror(errno));
2918-
exit_nicely();
2919+
exit(1);
29192920
}
29202921
#else
29212922
fprintf(stderr, _("%s: symlinks are not supported on this platform\n"), progname);
2922-
exit_nicely();
2923+
exit(1);
29232924
#endif
29242925
}
29252926
else
@@ -2929,7 +2930,7 @@ create_xlog_or_symlink(void)
29292930
{
29302931
fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
29312932
progname, subdirloc, strerror(errno));
2932-
exit_nicely();
2933+
exit(1);
29332934
}
29342935
}
29352936

@@ -2991,7 +2992,7 @@ initialize_data_directory(void)
29912992
{
29922993
fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
29932994
progname, path, strerror(errno));
2994-
exit_nicely();
2995+
exit(1);
29952996
}
29962997

29972998
free(path);
@@ -3266,6 +3267,8 @@ main(int argc, char *argv[])
32663267
exit(1);
32673268
}
32683269

3270+
atexit(cleanup_directories_atexit);
3271+
32693272
/* If we only need to fsync, just do it and exit */
32703273
if (sync_only)
32713274
{
@@ -3276,7 +3279,7 @@ main(int argc, char *argv[])
32763279
{
32773280
fprintf(stderr, _("%s: could not access directory \"%s\": %s\n"),
32783281
progname, pg_data, strerror(errno));
3279-
exit_nicely();
3282+
exit(1);
32803283
}
32813284

32823285
fputs(_("syncing data to disk ... "), stdout);
@@ -3412,5 +3415,6 @@ main(int argc, char *argv[])
34123415

34133416
destroyPQExpBuffer(start_db_cmd);
34143417

3418+
success = true;
34153419
return 0;
34163420
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy