PostgreSQL Source Code git master
pg_backup_utils.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * pg_backup_utils.c
4 * Utility routines shared by pg_dump and pg_restore
5 *
6 *
7 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
9 *
10 * src/bin/pg_dump/pg_backup_utils.c
11 *
12 *-------------------------------------------------------------------------
13 */
14#include "postgres_fe.h"
15
16#ifdef WIN32
17#include "parallel.h"
18#endif
19#include "pg_backup_utils.h"
20
21/* Globals exported by this file */
22const char *progname = NULL;
23
24#define MAX_ON_EXIT_NICELY 20
25
26static struct
27{
29 void *arg;
31
33
34/*
35 * Parse a --section=foo command line argument.
36 *
37 * Set or update the bitmask in *dumpSections according to arg.
38 * dumpSections is initialised as DUMP_UNSECTIONED by pg_dump and
39 * pg_restore so they can know if this has even been called.
40 */
41void
42set_dump_section(const char *arg, int *dumpSections)
43{
44 /* if this is the first call, clear all the bits */
45 if (*dumpSections == DUMP_UNSECTIONED)
46 *dumpSections = 0;
47
48 if (strcmp(arg, "pre-data") == 0)
49 *dumpSections |= DUMP_PRE_DATA;
50 else if (strcmp(arg, "data") == 0)
51 *dumpSections |= DUMP_DATA;
52 else if (strcmp(arg, "post-data") == 0)
53 *dumpSections |= DUMP_POST_DATA;
54 else
55 {
56 pg_log_error("unrecognized section name: \"%s\"", arg);
57 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
58 exit_nicely(1);
59 }
60}
61
62
63/* Register a callback to be run when exit_nicely is invoked. */
64void
66{
68 pg_fatal("out of on_exit_nicely slots");
72}
73
74/*
75 * Run accumulated on_exit_nicely callbacks in reverse order and then exit
76 * without printing any message.
77 *
78 * If running in a parallel worker thread on Windows, we only exit the thread,
79 * not the whole process.
80 *
81 * Note that in parallel operation on Windows, the callback(s) will be run
82 * by each thread since the list state is necessarily shared by all threads;
83 * each callback must contain logic to ensure it does only what's appropriate
84 * for its thread. On Unix, callbacks are also run by each process, but only
85 * for callbacks established before we fork off the child processes. (It'd
86 * be cleaner to reset the list after fork(), and let each child establish
87 * its own callbacks; but then the behavior would be completely inconsistent
88 * between Windows and Unix. For now, just be sure to establish callbacks
89 * before forking to avoid inconsistency.)
90 */
91void
92exit_nicely(int code)
93{
94 int i;
95
96 for (i = on_exit_nicely_index - 1; i >= 0; i--)
99
100#ifdef WIN32
101 if (parallel_init_done && GetCurrentThreadId() != mainThreadId)
102 _endthreadex(code);
103#endif
104
105 exit(code);
106}
int i
Definition: isn.c:72
exit(1)
#define pg_log_error(...)
Definition: logging.h:106
#define pg_log_error_hint(...)
Definition: logging.h:112
static int on_exit_nicely_index
static struct @34 on_exit_nicely_list[MAX_ON_EXIT_NICELY]
void exit_nicely(int code)
on_exit_nicely_callback function
#define MAX_ON_EXIT_NICELY
void set_dump_section(const char *arg, int *dumpSections)
void * arg
void on_exit_nicely(on_exit_nicely_callback function, void *arg)
const char * progname
#define DUMP_PRE_DATA
void(* on_exit_nicely_callback)(int code, void *arg)
#define DUMP_DATA
#define DUMP_UNSECTIONED
#define pg_fatal(...)
#define DUMP_POST_DATA