Commit 3913a40f authored by Peter Eisentraut's avatar Peter Eisentraut

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: default avatarAlvaro Herrera <alvherre@2ndquadrant.com>
Reviewed-by: default avatarMichael Paquier <michael@paquier.xyz>
Discussion: https://www.postgresql.org/message-id/flat/ec4135ba-84e9-28bf-b584-0e78d47448d5@2ndquadrant.com/
parent a4205fa0
...@@ -159,6 +159,7 @@ static char *dictionary_file; ...@@ -159,6 +159,7 @@ static char *dictionary_file;
static char *info_schema_file; static char *info_schema_file;
static char *features_file; static char *features_file;
static char *system_views_file; static char *system_views_file;
static bool success = false;
static bool made_new_pgdata = false; static bool made_new_pgdata = false;
static bool found_existing_pgdata = false; static bool found_existing_pgdata = false;
static bool made_new_xlogdir = false; static bool made_new_xlogdir = false;
...@@ -237,7 +238,6 @@ static char **filter_lines_with_token(char **lines, const char *token); ...@@ -237,7 +238,6 @@ static char **filter_lines_with_token(char **lines, const char *token);
static char **readfile(const char *path); static char **readfile(const char *path);
static void writefile(char *path, char **lines); static void writefile(char *path, char **lines);
static FILE *popen_check(const char *command, const char *mode); static FILE *popen_check(const char *command, const char *mode);
static void exit_nicely(void) pg_attribute_noreturn();
static char *get_id(void); static char *get_id(void);
static int get_encoding_id(const char *encoding_name); static int get_encoding_id(const char *encoding_name);
static void set_input(char **dest, const char *filename); static void set_input(char **dest, const char *filename);
...@@ -291,13 +291,13 @@ void initialize_data_directory(void); ...@@ -291,13 +291,13 @@ void initialize_data_directory(void);
do { \ do { \
cmdfd = popen_check(cmd, "w"); \ cmdfd = popen_check(cmd, "w"); \
if (cmdfd == NULL) \ if (cmdfd == NULL) \
exit_nicely(); /* message already printed by popen_check */ \ exit(1); /* message already printed by popen_check */ \
} while (0) } while (0)
#define PG_CMD_CLOSE \ #define PG_CMD_CLOSE \
do { \ do { \
if (pclose_check(cmdfd)) \ if (pclose_check(cmdfd)) \
exit_nicely(); /* message already printed by pclose_check */ \ exit(1); /* message already printed by pclose_check */ \
} while (0) } while (0)
#define PG_CMD_PUTS(line) \ #define PG_CMD_PUTS(line) \
...@@ -493,7 +493,7 @@ readfile(const char *path) ...@@ -493,7 +493,7 @@ readfile(const char *path)
{ {
fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"), fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
progname, path, strerror(errno)); progname, path, strerror(errno));
exit_nicely(); exit(1);
} }
/* pass over the file twice - the first time to size the result */ /* pass over the file twice - the first time to size the result */
...@@ -549,7 +549,7 @@ writefile(char *path, char **lines) ...@@ -549,7 +549,7 @@ writefile(char *path, char **lines)
{ {
fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"), fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
progname, path, strerror(errno)); progname, path, strerror(errno));
exit_nicely(); exit(1);
} }
for (line = lines; *line != NULL; line++) for (line = lines; *line != NULL; line++)
{ {
...@@ -557,7 +557,7 @@ writefile(char *path, char **lines) ...@@ -557,7 +557,7 @@ writefile(char *path, char **lines)
{ {
fprintf(stderr, _("%s: could not write file \"%s\": %s\n"), fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
progname, path, strerror(errno)); progname, path, strerror(errno));
exit_nicely(); exit(1);
} }
free(*line); free(*line);
} }
...@@ -565,7 +565,7 @@ writefile(char *path, char **lines) ...@@ -565,7 +565,7 @@ writefile(char *path, char **lines)
{ {
fprintf(stderr, _("%s: could not write file \"%s\": %s\n"), fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
progname, path, strerror(errno)); progname, path, strerror(errno));
exit_nicely(); exit(1);
} }
} }
...@@ -592,8 +592,11 @@ popen_check(const char *command, const char *mode) ...@@ -592,8 +592,11 @@ popen_check(const char *command, const char *mode)
* if we created the data directory remove it too * if we created the data directory remove it too
*/ */
static void static void
exit_nicely(void) cleanup_directories_atexit(void)
{ {
if (success)
return;
if (!noclean) if (!noclean)
{ {
if (made_new_pgdata) if (made_new_pgdata)
...@@ -645,8 +648,6 @@ exit_nicely(void) ...@@ -645,8 +648,6 @@ exit_nicely(void)
_("%s: WAL directory \"%s\" not removed at user's request\n"), _("%s: WAL directory \"%s\" not removed at user's request\n"),
progname, xlog_dir); progname, xlog_dir);
} }
exit(1);
} }
/* /*
...@@ -877,14 +878,14 @@ write_version_file(const char *extrapath) ...@@ -877,14 +878,14 @@ write_version_file(const char *extrapath)
{ {
fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"), fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
progname, path, strerror(errno)); progname, path, strerror(errno));
exit_nicely(); exit(1);
} }
if (fprintf(version_file, "%s\n", PG_MAJORVERSION) < 0 || if (fprintf(version_file, "%s\n", PG_MAJORVERSION) < 0 ||
fclose(version_file)) fclose(version_file))
{ {
fprintf(stderr, _("%s: could not write file \"%s\": %s\n"), fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
progname, path, strerror(errno)); progname, path, strerror(errno));
exit_nicely(); exit(1);
} }
free(path); free(path);
} }
...@@ -905,13 +906,13 @@ set_null_conf(void) ...@@ -905,13 +906,13 @@ set_null_conf(void)
{ {
fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"), fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
progname, path, strerror(errno)); progname, path, strerror(errno));
exit_nicely(); exit(1);
} }
if (fclose(conf_file)) if (fclose(conf_file))
{ {
fprintf(stderr, _("%s: could not write file \"%s\": %s\n"), fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
progname, path, strerror(errno)); progname, path, strerror(errno));
exit_nicely(); exit(1);
} }
free(path); free(path);
} }
...@@ -1262,7 +1263,7 @@ setup_config(void) ...@@ -1262,7 +1263,7 @@ setup_config(void)
{ {
fprintf(stderr, _("%s: could not change permissions of \"%s\": %s\n"), fprintf(stderr, _("%s: could not change permissions of \"%s\": %s\n"),
progname, path, strerror(errno)); progname, path, strerror(errno));
exit_nicely(); exit(1);
} }
/* /*
...@@ -1282,7 +1283,7 @@ setup_config(void) ...@@ -1282,7 +1283,7 @@ setup_config(void)
{ {
fprintf(stderr, _("%s: could not change permissions of \"%s\": %s\n"), fprintf(stderr, _("%s: could not change permissions of \"%s\": %s\n"),
progname, path, strerror(errno)); progname, path, strerror(errno));
exit_nicely(); exit(1);
} }
free(conflines); free(conflines);
...@@ -1369,7 +1370,7 @@ setup_config(void) ...@@ -1369,7 +1370,7 @@ setup_config(void)
{ {
fprintf(stderr, _("%s: could not change permissions of \"%s\": %s\n"), fprintf(stderr, _("%s: could not change permissions of \"%s\": %s\n"),
progname, path, strerror(errno)); progname, path, strerror(errno));
exit_nicely(); exit(1);
} }
free(conflines); free(conflines);
...@@ -1385,7 +1386,7 @@ setup_config(void) ...@@ -1385,7 +1386,7 @@ setup_config(void)
{ {
fprintf(stderr, _("%s: could not change permissions of \"%s\": %s\n"), fprintf(stderr, _("%s: could not change permissions of \"%s\": %s\n"),
progname, path, strerror(errno)); progname, path, strerror(errno));
exit_nicely(); exit(1);
} }
free(conflines); free(conflines);
...@@ -1423,7 +1424,7 @@ bootstrap_template1(void) ...@@ -1423,7 +1424,7 @@ bootstrap_template1(void)
"Check your installation or specify the correct path " "Check your installation or specify the correct path "
"using the option -L.\n"), "using the option -L.\n"),
progname, bki_file, PG_VERSION); progname, bki_file, PG_VERSION);
exit_nicely(); exit(1);
} }
/* Substitute for various symbols used in the BKI file */ /* Substitute for various symbols used in the BKI file */
...@@ -1541,7 +1542,7 @@ get_su_pwd(void) ...@@ -1541,7 +1542,7 @@ get_su_pwd(void)
if (strcmp(pwd1, pwd2) != 0) if (strcmp(pwd1, pwd2) != 0)
{ {
fprintf(stderr, _("Passwords didn't match.\n")); fprintf(stderr, _("Passwords didn't match.\n"));
exit_nicely(); exit(1);
} }
} }
else else
...@@ -1561,7 +1562,7 @@ get_su_pwd(void) ...@@ -1561,7 +1562,7 @@ get_su_pwd(void)
{ {
fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"), fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
progname, pwfilename, strerror(errno)); progname, pwfilename, strerror(errno));
exit_nicely(); exit(1);
} }
if (!fgets(pwd1, sizeof(pwd1), pwf)) if (!fgets(pwd1, sizeof(pwd1), pwf))
{ {
...@@ -1571,7 +1572,7 @@ get_su_pwd(void) ...@@ -1571,7 +1572,7 @@ get_su_pwd(void)
else else
fprintf(stderr, _("%s: password file \"%s\" is empty\n"), fprintf(stderr, _("%s: password file \"%s\" is empty\n"),
progname, pwfilename); progname, pwfilename);
exit_nicely(); exit(1);
} }
fclose(pwf); fclose(pwf);
...@@ -2104,7 +2105,7 @@ make_postgres(FILE *cmdfd) ...@@ -2104,7 +2105,7 @@ make_postgres(FILE *cmdfd)
* if you are handling SIGFPE. * if you are handling SIGFPE.
* *
* I avoided doing the forbidden things by setting a flag instead of calling * I avoided doing the forbidden things by setting a flag instead of calling
* exit_nicely() directly. * exit() directly.
* *
* Also note the behaviour of Windows with SIGINT, which says this: * Also note the behaviour of Windows with SIGINT, which says this:
* Note SIGINT is not supported for any Win32 application, including * Note SIGINT is not supported for any Win32 application, including
...@@ -2125,7 +2126,7 @@ trapsig(int signum) ...@@ -2125,7 +2126,7 @@ trapsig(int signum)
} }
/* /*
* call exit_nicely() if we got a signal, or else output "ok". * call exit() if we got a signal, or else output "ok".
*/ */
static void static void
check_ok(void) check_ok(void)
...@@ -2134,14 +2135,14 @@ check_ok(void) ...@@ -2134,14 +2135,14 @@ check_ok(void)
{ {
printf(_("caught signal\n")); printf(_("caught signal\n"));
fflush(stdout); fflush(stdout);
exit_nicely(); exit(1);
} }
else if (output_failed) else if (output_failed)
{ {
printf(_("could not write to child process: %s\n"), printf(_("could not write to child process: %s\n"),
strerror(output_errno)); strerror(output_errno));
fflush(stdout); fflush(stdout);
exit_nicely(); exit(1);
} }
else else
{ {
...@@ -2775,7 +2776,7 @@ create_data_directory(void) ...@@ -2775,7 +2776,7 @@ create_data_directory(void)
{ {
fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"), fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
progname, pg_data, strerror(errno)); progname, pg_data, strerror(errno));
exit_nicely(); exit(1);
} }
else else
check_ok(); check_ok();
...@@ -2793,7 +2794,7 @@ create_data_directory(void) ...@@ -2793,7 +2794,7 @@ create_data_directory(void)
{ {
fprintf(stderr, _("%s: could not change permissions of directory \"%s\": %s\n"), fprintf(stderr, _("%s: could not change permissions of directory \"%s\": %s\n"),
progname, pg_data, strerror(errno)); progname, pg_data, strerror(errno));
exit_nicely(); exit(1);
} }
else else
check_ok(); check_ok();
...@@ -2822,7 +2823,7 @@ create_data_directory(void) ...@@ -2822,7 +2823,7 @@ create_data_directory(void)
/* Trouble accessing directory */ /* Trouble accessing directory */
fprintf(stderr, _("%s: could not access directory \"%s\": %s\n"), fprintf(stderr, _("%s: could not access directory \"%s\": %s\n"),
progname, pg_data, strerror(errno)); progname, pg_data, strerror(errno));
exit_nicely(); exit(1);
} }
} }
...@@ -2845,7 +2846,7 @@ create_xlog_or_symlink(void) ...@@ -2845,7 +2846,7 @@ create_xlog_or_symlink(void)
if (!is_absolute_path(xlog_dir)) if (!is_absolute_path(xlog_dir))
{ {
fprintf(stderr, _("%s: WAL directory location must be an absolute path\n"), progname); fprintf(stderr, _("%s: WAL directory location must be an absolute path\n"), progname);
exit_nicely(); exit(1);
} }
/* check if the specified xlog directory exists/is empty */ /* check if the specified xlog directory exists/is empty */
...@@ -2861,7 +2862,7 @@ create_xlog_or_symlink(void) ...@@ -2861,7 +2862,7 @@ create_xlog_or_symlink(void)
{ {
fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"), fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
progname, xlog_dir, strerror(errno)); progname, xlog_dir, strerror(errno));
exit_nicely(); exit(1);
} }
else else
check_ok(); check_ok();
...@@ -2879,7 +2880,7 @@ create_xlog_or_symlink(void) ...@@ -2879,7 +2880,7 @@ create_xlog_or_symlink(void)
{ {
fprintf(stderr, _("%s: could not change permissions of directory \"%s\": %s\n"), fprintf(stderr, _("%s: could not change permissions of directory \"%s\": %s\n"),
progname, xlog_dir, strerror(errno)); progname, xlog_dir, strerror(errno));
exit_nicely(); exit(1);
} }
else else
check_ok(); check_ok();
...@@ -2901,13 +2902,13 @@ create_xlog_or_symlink(void) ...@@ -2901,13 +2902,13 @@ create_xlog_or_symlink(void)
_("If you want to store the WAL there, either remove or empty the directory\n" _("If you want to store the WAL there, either remove or empty the directory\n"
"\"%s\".\n"), "\"%s\".\n"),
xlog_dir); xlog_dir);
exit_nicely(); exit(1);
default: default:
/* Trouble accessing directory */ /* Trouble accessing directory */
fprintf(stderr, _("%s: could not access directory \"%s\": %s\n"), fprintf(stderr, _("%s: could not access directory \"%s\": %s\n"),
progname, xlog_dir, strerror(errno)); progname, xlog_dir, strerror(errno));
exit_nicely(); exit(1);
} }
#ifdef HAVE_SYMLINK #ifdef HAVE_SYMLINK
...@@ -2915,11 +2916,11 @@ create_xlog_or_symlink(void) ...@@ -2915,11 +2916,11 @@ create_xlog_or_symlink(void)
{ {
fprintf(stderr, _("%s: could not create symbolic link \"%s\": %s\n"), fprintf(stderr, _("%s: could not create symbolic link \"%s\": %s\n"),
progname, subdirloc, strerror(errno)); progname, subdirloc, strerror(errno));
exit_nicely(); exit(1);
} }
#else #else
fprintf(stderr, _("%s: symlinks are not supported on this platform\n"), progname); fprintf(stderr, _("%s: symlinks are not supported on this platform\n"), progname);
exit_nicely(); exit(1);
#endif #endif
} }
else else
...@@ -2929,7 +2930,7 @@ create_xlog_or_symlink(void) ...@@ -2929,7 +2930,7 @@ create_xlog_or_symlink(void)
{ {
fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"), fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
progname, subdirloc, strerror(errno)); progname, subdirloc, strerror(errno));
exit_nicely(); exit(1);
} }
} }
...@@ -2991,7 +2992,7 @@ initialize_data_directory(void) ...@@ -2991,7 +2992,7 @@ initialize_data_directory(void)
{ {
fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"), fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
progname, path, strerror(errno)); progname, path, strerror(errno));
exit_nicely(); exit(1);
} }
free(path); free(path);
...@@ -3266,6 +3267,8 @@ main(int argc, char *argv[]) ...@@ -3266,6 +3267,8 @@ main(int argc, char *argv[])
exit(1); exit(1);
} }
atexit(cleanup_directories_atexit);
/* If we only need to fsync, just do it and exit */ /* If we only need to fsync, just do it and exit */
if (sync_only) if (sync_only)
{ {
...@@ -3276,7 +3279,7 @@ main(int argc, char *argv[]) ...@@ -3276,7 +3279,7 @@ main(int argc, char *argv[])
{ {
fprintf(stderr, _("%s: could not access directory \"%s\": %s\n"), fprintf(stderr, _("%s: could not access directory \"%s\": %s\n"),
progname, pg_data, strerror(errno)); progname, pg_data, strerror(errno));
exit_nicely(); exit(1);
} }
fputs(_("syncing data to disk ... "), stdout); fputs(_("syncing data to disk ... "), stdout);
...@@ -3412,5 +3415,6 @@ main(int argc, char *argv[]) ...@@ -3412,5 +3415,6 @@ main(int argc, char *argv[])
destroyPQExpBuffer(start_db_cmd); destroyPQExpBuffer(start_db_cmd);
success = true;
return 0; return 0;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment