Skip to content

Commit c751638

Browse files
committed
Replace the confusing exit_nicely() by an atexit/on_exit hook
1 parent 2bd78eb commit c751638

File tree

5 files changed

+37
-43
lines changed

5 files changed

+37
-43
lines changed

contrib/pg_upgrade/check.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ report_clusters_compatible(void)
131131
pg_log(PG_REPORT, "\n*Clusters are compatible*\n");
132132
/* stops new cluster */
133133
stop_postmaster(false, false);
134-
exit_nicely(false);
134+
exit(0);
135135
}
136136

137137
pg_log(PG_REPORT, "\n"

contrib/pg_upgrade/option.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ parseCommandLine(int argc, char *argv[])
7777
strcmp(argv[1], "-?") == 0)
7878
{
7979
usage();
80-
exit_nicely(false);
80+
exit(0);
8181
}
8282
if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
8383
{
8484
pg_log(PG_REPORT, "pg_upgrade " PG_VERSION "\n");
85-
exit_nicely(false);
85+
exit(0);
8686
}
8787
}
8888

@@ -125,7 +125,7 @@ parseCommandLine(int argc, char *argv[])
125125
if ((log_opts.debug_fd = fopen(optarg, "w")) == NULL)
126126
{
127127
pg_log(PG_FATAL, "cannot open debug file\n");
128-
exit_nicely(false);
128+
exit(1);
129129
}
130130
break;
131131

@@ -141,15 +141,15 @@ parseCommandLine(int argc, char *argv[])
141141
if ((old_cluster.port = atoi(optarg)) <= 0)
142142
{
143143
pg_log(PG_FATAL, "invalid old port number\n");
144-
exit_nicely(false);
144+
exit(1);
145145
}
146146
break;
147147

148148
case 'P':
149149
if ((new_cluster.port = atoi(optarg)) <= 0)
150150
{
151151
pg_log(PG_FATAL, "invalid new port number\n");
152-
exit_nicely(false);
152+
exit(1);
153153
}
154154
break;
155155

contrib/pg_upgrade/pg_upgrade.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,6 @@ void check_for_libpq_envvars(void);
363363

364364
/* util.c */
365365

366-
void exit_nicely(bool need_cleanup);
367366
char *quote_identifier(const char *s);
368367
int get_user_info(char **user_name);
369368
void check_ok(void);

contrib/pg_upgrade/server.c

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ static bool test_server_conn(ClusterInfo *cluster, int timeout);
2323
*
2424
* Connects to the desired database on the designated server.
2525
* If the connection attempt fails, this function logs an error
26-
* message and calls exit_nicely() to kill the program.
26+
* message and calls exit() to kill the program.
2727
*/
2828
PGconn *
2929
connectToServer(ClusterInfo *cluster, const char *db_name)
@@ -45,7 +45,8 @@ connectToServer(ClusterInfo *cluster, const char *db_name)
4545
if (conn)
4646
PQfinish(conn);
4747

48-
exit_nicely(true);
48+
printf("Failure, exiting\n");
49+
exit(1);
4950
}
5051

5152
return conn;
@@ -57,7 +58,7 @@ connectToServer(ClusterInfo *cluster, const char *db_name)
5758
*
5859
* Formats a query string from the given arguments and executes the
5960
* resulting query. If the query fails, this function logs an error
60-
* message and calls exit_nicely() to kill the program.
61+
* message and calls exit() to kill the program.
6162
*/
6263
PGresult *
6364
executeQueryOrDie(PGconn *conn, const char *fmt,...)
@@ -81,8 +82,8 @@ executeQueryOrDie(PGconn *conn, const char *fmt,...)
8182
PQerrorMessage(conn));
8283
PQclear(result);
8384
PQfinish(conn);
84-
exit_nicely(true);
85-
return NULL; /* Never get here, but keeps compiler happy */
85+
printf("Failure, exiting\n");
86+
exit(1);
8687
}
8788
else
8889
return result;
@@ -152,18 +153,41 @@ get_major_server_version(ClusterInfo *cluster)
152153
}
153154

154155

156+
static void
157+
#ifdef HAVE_ATEXIT
158+
stop_postmaster_atexit(void)
159+
#else
160+
stop_postmaster_on_exit(int exitstatus, void *arg)
161+
#endif
162+
{
163+
stop_postmaster(true, true);
164+
165+
}
166+
167+
155168
void
156169
start_postmaster(ClusterInfo *cluster, bool quiet)
157170
{
158171
char cmd[MAXPGPATH];
159172
const char *bindir;
160173
const char *datadir;
161174
unsigned short port;
175+
bool exit_hook_registered = false;
162176

163177
bindir = cluster->bindir;
164178
datadir = cluster->pgdata;
165179
port = cluster->port;
166180

181+
if (!exit_hook_registered)
182+
{
183+
#ifdef HAVE_ATEXIT
184+
atexit(stop_postmaster_atexit);
185+
#else
186+
on_exit(stop_postmaster_on_exit);
187+
#endif
188+
exit_hook_registered = true;
189+
}
190+
167191
/*
168192
* On Win32, we can't send both pg_upgrade output and pg_ctl output to the
169193
* same file because we get the error: "The process cannot access the file

contrib/pg_upgrade/util.c

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ pg_log(eLogType type, char *fmt,...)
9999
case PG_FATAL:
100100
printf("%s", "\n");
101101
printf("%s", _(message));
102-
exit_nicely(true);
102+
printf("Failure, exiting\n");
103+
exit(1);
103104
break;
104105

105106
case PG_DEBUG:
@@ -184,36 +185,6 @@ get_user_info(char **user_name)
184185
}
185186

186187

187-
void
188-
exit_nicely(bool need_cleanup)
189-
{
190-
stop_postmaster(true, true);
191-
192-
pg_free(log_opts.filename);
193-
194-
if (log_opts.fd)
195-
fclose(log_opts.fd);
196-
197-
if (log_opts.debug_fd)
198-
fclose(log_opts.debug_fd);
199-
200-
/* terminate any running instance of postmaster */
201-
if (os_info.postmasterPID != 0)
202-
kill(os_info.postmasterPID, SIGTERM);
203-
204-
if (need_cleanup)
205-
{
206-
printf("Failure, exiting\n");
207-
/*
208-
* FIXME must delete intermediate files
209-
*/
210-
exit(1);
211-
}
212-
else
213-
exit(0);
214-
}
215-
216-
217188
void *
218189
pg_malloc(int n)
219190
{

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