From b23a9b602a3b976187b14906f29b9f6fc59444dc Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Sat, 29 Dec 2018 13:21:57 +0100 Subject: [PATCH 2/2] initdb: Use atexit() Replace exit_nicely() calls with standard exit() and register the cleanup actions using atexit(). --- src/bin/initdb/initdb.c | 80 +++++++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 39 deletions(-) diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c index 017c11bc94..e4507a289b 100644 --- a/src/bin/initdb/initdb.c +++ b/src/bin/initdb/initdb.c @@ -238,7 +238,6 @@ static char **filter_lines_with_token(char **lines, const char *token); static char **readfile(const char *path); static void writefile(char *path, char **lines); static FILE *popen_check(const char *command, const char *mode); -static void exit_nicely(void) pg_attribute_noreturn(); static char *get_id(void); static int get_encoding_id(const char *encoding_name); static void set_input(char **dest, const char *filename); @@ -293,13 +292,13 @@ void initialize_data_directory(void); do { \ cmdfd = popen_check(cmd, "w"); \ if (cmdfd == NULL) \ - exit_nicely(); /* message already printed by popen_check */ \ + exit(1); /* message already printed by popen_check */ \ } while (0) #define PG_CMD_CLOSE \ do { \ if (pclose_check(cmdfd)) \ - exit_nicely(); /* message already printed by pclose_check */ \ + exit(1); /* message already printed by pclose_check */ \ } while (0) #define PG_CMD_PUTS(line) \ @@ -495,7 +494,7 @@ readfile(const char *path) { fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"), progname, path, strerror(errno)); - exit_nicely(); + exit(1); } /* pass over the file twice - the first time to size the result */ @@ -551,7 +550,7 @@ writefile(char *path, char **lines) { fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"), progname, path, strerror(errno)); - exit_nicely(); + exit(1); } for (line = lines; *line != NULL; line++) { @@ -559,7 +558,7 @@ writefile(char *path, char **lines) { fprintf(stderr, _("%s: could not write file \"%s\": %s\n"), progname, path, strerror(errno)); - exit_nicely(); + exit(1); } free(*line); } @@ -567,7 +566,7 @@ writefile(char *path, char **lines) { fprintf(stderr, _("%s: could not write file \"%s\": %s\n"), progname, path, strerror(errno)); - exit_nicely(); + exit(1); } } @@ -594,7 +593,7 @@ popen_check(const char *command, const char *mode) * if we created the data directory remove it too */ static void -exit_nicely(void) +cleanup_atexit(void) { if (!noclean) { @@ -647,8 +646,6 @@ exit_nicely(void) _("%s: WAL directory \"%s\" not removed at user's request\n"), progname, xlog_dir); } - - exit(1); } /* @@ -879,14 +876,14 @@ write_version_file(const char *extrapath) { fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"), progname, path, strerror(errno)); - exit_nicely(); + exit(1); } if (fprintf(version_file, "%s\n", PG_MAJORVERSION) < 0 || fclose(version_file)) { fprintf(stderr, _("%s: could not write file \"%s\": %s\n"), progname, path, strerror(errno)); - exit_nicely(); + exit(1); } free(path); } @@ -907,13 +904,13 @@ set_null_conf(void) { fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"), progname, path, strerror(errno)); - exit_nicely(); + exit(1); } if (fclose(conf_file)) { fprintf(stderr, _("%s: could not write file \"%s\": %s\n"), progname, path, strerror(errno)); - exit_nicely(); + exit(1); } free(path); } @@ -1264,7 +1261,7 @@ setup_config(void) { fprintf(stderr, _("%s: could not change permissions of \"%s\": %s\n"), progname, path, strerror(errno)); - exit_nicely(); + exit(1); } /* @@ -1284,7 +1281,7 @@ setup_config(void) { fprintf(stderr, _("%s: could not change permissions of \"%s\": %s\n"), progname, path, strerror(errno)); - exit_nicely(); + exit(1); } free(conflines); @@ -1371,7 +1368,7 @@ setup_config(void) { fprintf(stderr, _("%s: could not change permissions of \"%s\": %s\n"), progname, path, strerror(errno)); - exit_nicely(); + exit(1); } free(conflines); @@ -1387,7 +1384,7 @@ setup_config(void) { fprintf(stderr, _("%s: could not change permissions of \"%s\": %s\n"), progname, path, strerror(errno)); - exit_nicely(); + exit(1); } free(conflines); @@ -1425,7 +1422,7 @@ bootstrap_template1(void) "Check your installation or specify the correct path " "using the option -L.\n"), progname, bki_file, PG_VERSION); - exit_nicely(); + exit(1); } /* Substitute for various symbols used in the BKI file */ @@ -1543,7 +1540,7 @@ get_su_pwd(void) if (strcmp(pwd1, pwd2) != 0) { fprintf(stderr, _("Passwords didn't match.\n")); - exit_nicely(); + exit(1); } } else @@ -1563,7 +1560,7 @@ get_su_pwd(void) { fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"), progname, pwfilename, strerror(errno)); - exit_nicely(); + exit(1); } if (!fgets(pwd1, sizeof(pwd1), pwf)) { @@ -1573,7 +1570,7 @@ get_su_pwd(void) else fprintf(stderr, _("%s: password file \"%s\" is empty\n"), progname, pwfilename); - exit_nicely(); + exit(1); } fclose(pwf); @@ -2126,7 +2123,7 @@ make_postgres(FILE *cmdfd) * if you are handling SIGFPE. * * 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: * Note SIGINT is not supported for any Win32 application, including @@ -2147,7 +2144,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 check_ok(void) @@ -2156,14 +2153,14 @@ check_ok(void) { printf(_("caught signal\n")); fflush(stdout); - exit_nicely(); + exit(1); } else if (output_failed) { printf(_("could not write to child process: %s\n"), strerror(output_errno)); fflush(stdout); - exit_nicely(); + exit(1); } else { @@ -2799,7 +2796,7 @@ create_data_directory(void) { fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"), progname, pg_data, strerror(errno)); - exit_nicely(); + exit(1); } else check_ok(); @@ -2817,7 +2814,7 @@ create_data_directory(void) { fprintf(stderr, _("%s: could not change permissions of directory \"%s\": %s\n"), progname, pg_data, strerror(errno)); - exit_nicely(); + exit(1); } else check_ok(); @@ -2846,7 +2843,7 @@ create_data_directory(void) /* Trouble accessing directory */ fprintf(stderr, _("%s: could not access directory \"%s\": %s\n"), progname, pg_data, strerror(errno)); - exit_nicely(); + exit(1); } } @@ -2869,7 +2866,7 @@ create_xlog_or_symlink(void) if (!is_absolute_path(xlog_dir)) { 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 */ @@ -2885,7 +2882,7 @@ create_xlog_or_symlink(void) { fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"), progname, xlog_dir, strerror(errno)); - exit_nicely(); + exit(1); } else check_ok(); @@ -2903,7 +2900,7 @@ create_xlog_or_symlink(void) { fprintf(stderr, _("%s: could not change permissions of directory \"%s\": %s\n"), progname, xlog_dir, strerror(errno)); - exit_nicely(); + exit(1); } else check_ok(); @@ -2925,13 +2922,13 @@ create_xlog_or_symlink(void) _("If you want to store the WAL there, either remove or empty the directory\n" "\"%s\".\n"), xlog_dir); - exit_nicely(); + exit(1); default: /* Trouble accessing directory */ fprintf(stderr, _("%s: could not access directory \"%s\": %s\n"), progname, xlog_dir, strerror(errno)); - exit_nicely(); + exit(1); } #ifdef HAVE_SYMLINK @@ -2939,11 +2936,11 @@ create_xlog_or_symlink(void) { fprintf(stderr, _("%s: could not create symbolic link \"%s\": %s\n"), progname, subdirloc, strerror(errno)); - exit_nicely(); + exit(1); } #else fprintf(stderr, _("%s: symlinks are not supported on this platform\n"), progname); - exit_nicely(); + exit(1); #endif } else @@ -2953,7 +2950,7 @@ create_xlog_or_symlink(void) { fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"), progname, subdirloc, strerror(errno)); - exit_nicely(); + exit(1); } } @@ -3015,7 +3012,7 @@ initialize_data_directory(void) { fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"), progname, path, strerror(errno)); - exit_nicely(); + exit(1); } free(path); @@ -3292,6 +3289,8 @@ main(int argc, char *argv[]) exit(1); } + atexit(cleanup_atexit); + /* If we only need to fsync, just do it and exit */ if (sync_only) { @@ -3302,7 +3301,7 @@ main(int argc, char *argv[]) { fprintf(stderr, _("%s: could not access directory \"%s\": %s\n"), progname, pg_data, strerror(errno)); - exit_nicely(); + exit(1); } fputs(_("syncing data to disk ... "), stdout); @@ -3438,5 +3437,8 @@ main(int argc, char *argv[]) destroyPQExpBuffer(start_db_cmd); + /* prevent cleanup */ + made_new_pgdata = found_existing_pgdata = made_new_xlogdir = found_existing_xlogdir = false; + return 0; } -- 2.20.1