1 /*
2  * configfile.h
3  *
4  * Copyright (c) 2ndQuadrant, 2010-2020
5  *
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef _REPMGR_CONFIGFILE_H_
22 #define _REPMGR_CONFIGFILE_H_
23 
24 #include <getopt_long.h>
25 
26 #define CONFIG_FILE_NAME	"repmgr.conf"
27 #define MAXLINELENGTH		4096
28 /* magic number for use in t_recovery_conf */
29 #define TARGET_TIMELINE_LATEST 0
30 
31 /*
32  * This is defined in src/include/utils.h, however it's not practical
33  * to include that from a frontend application.
34  */
35 #define PG_AUTOCONF_FILENAME "postgresql.auto.conf"
36 
37 extern bool config_file_found;
38 extern char config_file_path[MAXPGPATH];
39 
40 typedef enum
41 {
42 	FAILOVER_MANUAL,
43 	FAILOVER_AUTOMATIC
44 } failover_mode_opt;
45 
46 typedef enum
47 {
48 	CHECK_PING,
49 	CHECK_QUERY,
50 	CHECK_CONNECTION
51 } ConnectionCheckType;
52 
53 typedef struct EventNotificationListCell
54 {
55 	struct EventNotificationListCell *next;
56 	char		event_type[MAXLEN];
57 } EventNotificationListCell;
58 
59 typedef struct EventNotificationList
60 {
61 	EventNotificationListCell *head;
62 	EventNotificationListCell *tail;
63 } EventNotificationList;
64 
65 
66 
67 typedef struct TablespaceListCell
68 {
69 	struct TablespaceListCell *next;
70 	char		old_dir[MAXPGPATH];
71 	char		new_dir[MAXPGPATH];
72 } TablespaceListCell;
73 
74 typedef struct TablespaceList
75 {
76 	TablespaceListCell *head;
77 	TablespaceListCell *tail;
78 } TablespaceList;
79 
80 
81 typedef enum
82 {
83 	CONFIG_BOOL,
84 	CONFIG_INT,
85 	CONFIG_STRING,
86 	CONFIG_FAILOVER_MODE,
87 	CONFIG_CONNECTION_CHECK_TYPE,
88 	CONFIG_EVENT_NOTIFICATION_LIST,
89 	CONFIG_TABLESPACE_MAPPING
90 } ConfigItemType;
91 
92 
93 typedef struct ConfigFileSetting
94 {
95 	const char	   *name;
96 	ConfigItemType  type;
97     union
98     {
99         int		   *intptr;
100         char	   *strptr;
101 		bool	   *boolptr;
102 		failover_mode_opt *failovermodeptr;
103 		ConnectionCheckType *checktypeptr;
104 		EventNotificationList *notificationlistptr;
105 		TablespaceList *tablespacemappingptr;
106 	} val;
107 	union {
108 		int			intdefault;
109 		const char *strdefault;
110 		bool		booldefault;
111 		failover_mode_opt failovermodedefault;
112 		ConnectionCheckType checktypedefault;
113 	} defval;
114 	union {
115 		int				intminval;
116 	} minval;
117 	union {
118 		int				strmaxlen;
119 	} maxval;
120 	struct {
121 		void (*process_func)(const char *, const char *, char *, ItemList *errors);
122 		void (*postprocess_func)(const char *, const char *, char *, ItemList *errors);
123 		bool	   *providedptr;
124 	} process;
125 } ConfigFileSetting;
126 
127 /* Declare the main configfile structure for client applications */
128 extern ConfigFileSetting config_file_settings[];
129 
130 typedef struct
131 {
132 	/* node information */
133 	int			node_id;
134 	char		node_name[NAMEDATALEN];
135 	char		conninfo[MAXLEN];
136 	char		replication_user[NAMEDATALEN];
137 	char		data_directory[MAXPGPATH];
138 	char		config_directory[MAXPGPATH];
139 	char		pg_bindir[MAXPGPATH];
140 	char		repmgr_bindir[MAXPGPATH];
141 	int			replication_type;
142 
143 	/* log settings */
144 	char		log_level[MAXLEN];
145 	char		log_facility[MAXLEN];
146 	char		log_file[MAXPGPATH];
147 	int			log_status_interval;
148 
149 	/* standby clone settings */
150 	bool		use_replication_slots;
151 	char		pg_basebackup_options[MAXLEN];
152 	char		restore_command[MAXLEN];
153 	TablespaceList tablespace_mapping;
154 	char		recovery_min_apply_delay[MAXLEN];
155 	bool		recovery_min_apply_delay_provided;
156 	char		archive_cleanup_command[MAXLEN];
157 	bool		use_primary_conninfo_password;
158 	char		passfile[MAXPGPATH];
159 
160 	/* standby promote settings */
161 	int			promote_check_timeout;
162 	int			promote_check_interval;
163 
164 	/* standby follow settings */
165 	int			primary_follow_timeout;
166 	int			standby_follow_timeout;
167 	bool		standby_follow_restart;
168 
169 	/* standby switchover settings */
170 	int			shutdown_check_timeout;
171 	int			standby_reconnect_timeout;
172 	int			wal_receive_check_timeout;
173 
174 	/* node rejoin settings */
175 	int			node_rejoin_timeout;
176 
177 	/* node check settings */
178 	int			archive_ready_warning;
179 	int			archive_ready_critical;
180 	int			replication_lag_warning;
181 	int			replication_lag_critical;
182 
183 	/* witness settings */
184 	int			witness_sync_interval;
185 
186 	/* repmgrd settings */
187 	failover_mode_opt failover;
188 	char		location[MAXLEN];
189 	int			priority;
190 	char		promote_command[MAXLEN];
191 	char		follow_command[MAXLEN];
192 	int			monitor_interval_secs;
193 	int			reconnect_attempts;
194 	int			reconnect_interval;
195 	bool		monitoring_history;
196 	int			degraded_monitoring_timeout;
197 	int			async_query_timeout;
198 	int			primary_notification_timeout;
199 	int			repmgrd_standby_startup_timeout;
200 	char		repmgrd_pid_file[MAXPGPATH];
201 	bool		standby_disconnect_on_failover;
202 	int			sibling_nodes_disconnect_timeout;
203 	ConnectionCheckType connection_check_type;
204 	bool		primary_visibility_consensus;
205 	bool		always_promote;
206 	char		failover_validation_command[MAXPGPATH];
207 	int			election_rerun_interval;
208 	int			child_nodes_check_interval;
209 	int			child_nodes_disconnect_min_count;
210 	int			child_nodes_connected_min_count;
211 	bool		child_nodes_connected_include_witness;
212 	int			child_nodes_disconnect_timeout;
213 	char		child_nodes_disconnect_command[MAXPGPATH];
214 
215 	/* service settings */
216 	char		pg_ctl_options[MAXLEN];
217 	char		service_start_command[MAXPGPATH];
218 	char		service_stop_command[MAXPGPATH];
219 	char		service_restart_command[MAXPGPATH];
220 	char		service_reload_command[MAXPGPATH];
221 	char		service_promote_command[MAXPGPATH];
222 
223 	/* repmgrd service settings */
224 	char		repmgrd_service_start_command[MAXPGPATH];
225 	char		repmgrd_service_stop_command[MAXPGPATH];
226 
227 	/* event notification settings */
228 	char		event_notification_command[MAXPGPATH];
229 	char		event_notifications_orig[MAXLEN];
230 	EventNotificationList event_notifications;
231 
232 	/* barman settings */
233 	char		barman_host[MAXLEN];
234 	char		barman_server[MAXLEN];
235 	char		barman_config[MAXLEN];
236 
237 	/* rsync/ssh settings */
238 	char		rsync_options[MAXLEN];
239 	char		ssh_options[MAXLEN];
240 
241 	/*
242 	 * undocumented settings
243 	 *
244 	 * These settings are for testing or experimential features
245 	 * and may be changed without notice.
246 	 */
247 
248 	/* experimental settings */
249 	bool		reconnect_loop_sync;
250 
251 	/* test settings */
252 	int			promote_delay;
253 	int			failover_delay;
254 	char		connection_check_query[MAXLEN];
255 } t_configuration_options;
256 
257 
258 /* Declare the main configfile structure for client applications */
259 extern t_configuration_options config_file_options;
260 
261 typedef struct
262 {
263 	char		slot[MAXLEN];
264 	char		wal_method[MAXLEN];
265 	char		waldir[MAXPGPATH];
266 	bool		no_slot;		/* from PostgreSQL 10 */
267 } t_basebackup_options;
268 
269 #define T_BASEBACKUP_OPTIONS_INITIALIZER { "", "", "", false }
270 
271 
272 typedef enum
273 {
274 	RTA_PAUSE,
275 	RTA_PROMOTE,
276 	RTA_SHUTDOWN
277 } RecoveryTargetAction;
278 
279 /*
280  * Struct to hold the contents of a parsed recovery.conf file.
281  * We're only really interested in those related to streaming
282  * replication (and also "restore_command") but include the
283  * others for completeness.
284  *
285  * NOTE: "recovery_target" not included as it can only have
286  * one value, "immediate".
287  */
288 typedef struct
289 {
290 	/* archive recovery settings */
291 	char		restore_command[MAXLEN];
292 	char		archive_cleanup_command[MAXLEN];
293 	char		recovery_end_command[MAXLEN];
294 	/* recovery target settings */
295 	char		recovery_target_name[MAXLEN];
296 	char		recovery_target_time[MAXLEN];
297 	char		recovery_target_xid[MAXLEN];
298 	bool		recovery_target_inclusive;
299 	int			recovery_target_timeline;
300 	RecoveryTargetAction recovery_target_action;	/* default: RTA_PAUSE */
301 	/* standby server settings */
302 	bool		standby_mode;
303 	char		primary_conninfo[MAXLEN];
304 	char		primary_slot_name[MAXLEN];
305 	char		trigger_file[MAXLEN];
306 	char		recovery_min_apply_delay[MAXLEN];
307 } t_recovery_conf;
308 
309 #define T_RECOVERY_CONF_INITIALIZER { \
310 	/* archive recovery settings */ \
311 	"", "", "", \
312 	/* recovery target settings */ \
313 	"", "", "", true, \
314 	TARGET_TIMELINE_LATEST, \
315 	RTA_PAUSE, \
316 	/* standby server settings */ \
317 	true, \
318 	"", "", "", "" \
319 }
320 
321 #include "dbutils.h"
322 
323 void		set_progname(const char *argv0);
324 const char *progname(void);
325 
326 void		load_config(const char *config_file, bool verbose, bool terse, char *argv0);
327 bool		reload_config(t_server_type server_type);
328 void		dump_config(void);
329 
330 void		parse_configuration_item(ItemList *error_list, ItemList *warning_list, const char *name, const char *value);
331 
332 bool		parse_recovery_conf(const char *data_dir, t_recovery_conf *conf);
333 
334 bool		parse_bool(const char *s,
335 					   const char *config_item,
336 					   ItemList *error_list);
337 
338 int repmgr_atoi(const char *s,
339 			const char *config_item,
340 			ItemList *error_list,
341 			int minval);
342 
343 void parse_time_unit_parameter(const char *name, const char *value, char *dest, ItemList *errors);
344 void repmgr_canonicalize_path(const char *name, const char *value, char *config_item, ItemList *errors);
345 
346 bool parse_pg_basebackup_options(const char *pg_basebackup_options,
347 							t_basebackup_options *backup_options,
348 							int server_version_num,
349 							ItemList *error_list);
350 
351 int parse_output_to_argv(const char *string, char ***argv_array);
352 void free_parsed_argv(char ***argv_array);
353 const char *format_failover_mode(failover_mode_opt failover);
354 
355 /* called by repmgr-client and repmgrd */
356 void		exit_with_cli_errors(ItemList *error_list, const char *repmgr_command);
357 
358 void		print_item_list(ItemList *item_list);
359 const char *print_connection_check_type(ConnectionCheckType type);
360 char 	   *print_event_notification_list(EventNotificationList *list);
361 char 	   *print_tablespace_mapping(TablespaceList *tablespacemappingptr);
362 
363 extern bool modify_auto_conf(const char *data_dir, KeyValueList *items);
364 
365 extern bool ProcessRepmgrConfigFile(const char *config_file, const char *base_dir, ItemList *error_list, ItemList *warning_list);
366 
367 extern bool ProcessPostgresConfigFile(const char *config_file, const char *base_dir, KeyValueList *contents, ItemList *error_list, ItemList *warning_list);
368 
369 #endif							/* _REPMGR_CONFIGFILE_H_ */
370