-
Notifications
You must be signed in to change notification settings - Fork 113
/
config.c
401 lines (342 loc) · 9.48 KB
/
config.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
* src/bin/pg_autoctl/config.c
* Common configuration functions
*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the PostgreSQL License.
*
*/
#include <stdbool.h>
#include <unistd.h>
#include "postgres_fe.h"
#include "config.h"
#include "defaults.h"
#include "env_utils.h"
#include "file_utils.h"
#include "ini_file.h"
#include "keeper.h"
#include "keeper_config.h"
#include "log.h"
#include "pgctl.h"
/*
* build_xdg_path is an helper function that builds the full path to an XDG
* compatible resource: either a configuration file, a runtime file, or a data
* file.
*/
bool
build_xdg_path(char *dst,
XDGResourceType xdgType,
const char *pgdata,
const char *name)
{
char filename[MAXPGPATH];
char home[MAXPGPATH];
char fallback[MAXPGPATH];
char xdg_topdir[MAXPGPATH];
char *envVarName = NULL;
if (!get_env_copy("HOME", home, MAXPGPATH))
{
/* errors have already been logged */
exit(EXIT_CODE_INTERNAL_ERROR);
}
switch (xdgType)
{
case XDG_DATA:
{
join_path_components(fallback, home, ".local/share");
envVarName = "XDG_DATA_HOME";
break;
}
case XDG_CONFIG:
{
join_path_components(fallback, home, ".config");
envVarName = "XDG_CONFIG_HOME";
break;
}
case XDG_RUNTIME:
{
strlcpy(fallback, "/tmp", MAXPGPATH);
envVarName = "XDG_RUNTIME_DIR";
break;
}
default:
/* developper error */
log_error("No support for XDG Resource Type %d", xdgType);
return false;
}
if (!get_env_copy_with_fallback(envVarName, xdg_topdir, MAXPGPATH, fallback))
{
/* errors have already been logged */
return false;
}
if (xdgType == XDG_RUNTIME && !directory_exists(xdg_topdir))
{
strlcpy(xdg_topdir, "/tmp", MAXPGPATH);
}
join_path_components(filename, xdg_topdir, "pg_autoctl");
/* append PGDATA now */
if (pgdata[0] == '/')
{
/* skip the first / to avoid having a double-slash in the name */
join_path_components(filename, filename, pgdata + 1);
}
else
{
/*
* We have a relative pathname to PGDATA, and we want an absolute
* pathname in our configuration directory name so that we make
* sure to find it again.
*
* It could be that the PGDATA directory we are given doesn't exist
* yet, precluding the use of realpath(3) to get the absolute name
* here.
*/
char currentWorkingDirectory[MAXPGPATH] = { 0 };
if (getcwd(currentWorkingDirectory, MAXPGPATH) == NULL)
{
log_error("Failed to get the current working directory: %m");
return false;
}
/* avoid double-slash by skipping the first one */
join_path_components(filename, filename, currentWorkingDirectory + 1);
/* now add in pgdata */
join_path_components(filename, filename, pgdata);
}
/* mkdir -p the target directory */
if (pg_mkdir_p(filename, 0755) == -1)
{
log_error("Failed to create state directory \"%s\": %m", filename);
return false;
}
/* normalize the existing path to the configuration file */
if (!normalize_filename(filename, dst, MAXPGPATH))
{
/* errors have already been logged */
return false;
}
/* and finally add the configuration file name */
join_path_components(dst, dst, name);
return true;
}
/*
* SetConfigFilePath sets config.pathnames.config from config.pgSetup.pgdata,
* which must have been set previously.
*/
bool
SetConfigFilePath(ConfigFilePaths *pathnames, const char *pgdata)
{
/* don't overwrite already computed value */
if (IS_EMPTY_STRING_BUFFER(pathnames->config))
{
if (!build_xdg_path(pathnames->config,
XDG_CONFIG,
pgdata,
KEEPER_CONFIGURATION_FILENAME))
{
log_error("Failed to build our configuration file pathname, "
"see above.");
return false;
}
}
log_trace("SetConfigFilePath: \"%s\"", pathnames->config);
return true;
}
/*
* SetStateFilePath sets config.pathnames.state from our PGDATA value, and
* using the XDG Base Directory Specification for a data file. Per specs at:
*
* https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
*
*/
bool
SetStateFilePath(ConfigFilePaths *pathnames, const char *pgdata)
{
if (IS_EMPTY_STRING_BUFFER(pathnames->state))
{
if (!build_xdg_path(pathnames->state,
XDG_DATA,
pgdata,
KEEPER_STATE_FILENAME))
{
log_error("Failed to build pg_autoctl state file pathname, "
"see above.");
return false;
}
}
log_trace("SetStateFilePath: \"%s\"", pathnames->state);
/* now the init state file */
if (IS_EMPTY_STRING_BUFFER(pathnames->init))
{
if (!build_xdg_path(pathnames->init,
XDG_DATA,
pgdata,
KEEPER_INIT_STATE_FILENAME))
{
log_error("Failed to build pg_autoctl init state file pathname, "
"see above.");
return false;
}
}
log_trace("SetKeeperStateFilePath: \"%s\"", pathnames->init);
return true;
}
/*
* SetNodesFilePath sets config.pathnames.nodes from our PGDATA value, and
* using the XDG Base Directory Specification for a data file. Per specs at:
*
* https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
*
*/
bool
SetNodesFilePath(ConfigFilePaths *pathnames, const char *pgdata)
{
if (IS_EMPTY_STRING_BUFFER(pathnames->nodes))
{
if (!build_xdg_path(pathnames->nodes,
XDG_DATA,
pgdata,
KEEPER_NODES_FILENAME))
{
log_error("Failed to build pg_autoctl state file pathname, "
"see above.");
return false;
}
}
log_trace("SetNodesFilePath: \"%s\"", pathnames->nodes);
return true;
}
/*
* SetPidFilePath sets config.pathnames.pidfile from our PGDATA value, and
* using the XDG Base Directory Specification for a runtime file.
*/
bool
SetPidFilePath(ConfigFilePaths *pathnames, const char *pgdata)
{
if (IS_EMPTY_STRING_BUFFER(pathnames->pid))
{
if (!build_xdg_path(pathnames->pid,
XDG_RUNTIME,
pgdata,
KEEPER_PID_FILENAME))
{
log_error("Failed to build pg_autoctl pid file pathname, "
"see above.");
return false;
}
}
log_trace("SetPidFilePath: \"%s\"", pathnames->pid);
return true;
}
/*
* ProbeConfigurationFileRole opens a configuration file at given filename and
* probes the pg_autoctl role it belongs to: either a monitor or a keeper.
*
* We use a IniOption array with a single entry here, the pg_autoctl.role
* setting that indicates which role is our configuration file intended to be
* read as: either "monitor" or "keeper".
*/
pgAutoCtlNodeRole
ProbeConfigurationFileRole(const char *filename)
{
MinimalConfig config = { 0 };
IniOption configOptions[] = {
make_strbuf_option("pg_autoctl", "role",
NULL, true, NAMEDATALEN, config.role),
INI_OPTION_LAST
};
log_debug("Probing configuration file \"%s\"", filename);
/*
* There is a race condition at process startup where a configuration file
* can disappear while being overwritten. Reduce the chances of that
* happening by making more than one attempt at reading the file.
*/
char *fileContents = NULL;
for (int attempts = 0; fileContents == NULL && attempts < 3; attempts++)
{
long fileSize = 0L;
if (read_file_if_exists(filename, &fileContents, &fileSize))
{
break;
}
pg_usleep(100 * 1000); /* 100ms */
}
if (fileContents == NULL)
{
log_error("Failed to read configuration file \"%s\"", filename);
return PG_AUTOCTL_ROLE_UNKNOWN;
}
if (!parse_ini_buffer(filename, fileContents, configOptions))
{
log_error("Failed to parse configuration file \"%s\"", filename);
return PG_AUTOCTL_ROLE_UNKNOWN;
}
log_debug("ProbeConfigurationFileRole: %s", config.role);
if (strcmp(config.role, MONITOR_ROLE) == 0)
{
return PG_AUTOCTL_ROLE_MONITOR;
}
else if (strcmp(config.role, KEEPER_ROLE) == 0)
{
return PG_AUTOCTL_ROLE_KEEPER;
}
else
{
log_fatal("Failed to recognize configuration file setting for "
"pg_autoctl.role: \"%s\"", config.role);
exit(EXIT_CODE_BAD_CONFIG);
}
/* can't happen: keep compiler happy */
return PG_AUTOCTL_ROLE_UNKNOWN;
}
/*
* config_accept_new_ssloptions allows to reload SSL options at runtime.
*/
bool
config_accept_new_ssloptions(PostgresSetup *pgSetup, PostgresSetup *newPgSetup)
{
if (pgSetup->ssl.active != newPgSetup->ssl.active)
{
log_info("Reloading configuration: ssl is now %s; used to be %s",
newPgSetup->ssl.active ? "active" : "disabled",
pgSetup->ssl.active ? "active" : "disabled");
}
if (pgSetup->ssl.sslMode != newPgSetup->ssl.sslMode)
{
log_info("Reloading configuration: sslmode is now \"%s\"; "
"used to be \"%s\"",
pgsetup_sslmode_to_string(newPgSetup->ssl.sslMode),
pgsetup_sslmode_to_string(pgSetup->ssl.sslMode));
}
if (strneq(pgSetup->ssl.caFile, newPgSetup->ssl.caFile))
{
log_info("Reloading configuration: ssl CA file is now \"%s\"; "
"used to be \"%s\"",
newPgSetup->ssl.caFile, pgSetup->ssl.caFile);
}
if (strneq(pgSetup->ssl.crlFile, newPgSetup->ssl.crlFile))
{
log_info("Reloading configuration: ssl CRL file is now \"%s\"; "
"used to be \"%s\"",
newPgSetup->ssl.crlFile, pgSetup->ssl.crlFile);
}
if (strneq(pgSetup->ssl.serverCert, newPgSetup->ssl.serverCert))
{
log_info("Reloading configuration: ssl server cert file is now \"%s\"; "
"used to be \"%s\"",
newPgSetup->ssl.serverCert,
pgSetup->ssl.serverCert);
}
if (strneq(pgSetup->ssl.serverKey, newPgSetup->ssl.serverKey))
{
log_info("Reloading configuration: ssl server key file is now \"%s\"; "
"used to be \"%s\"",
newPgSetup->ssl.serverKey,
pgSetup->ssl.serverKey);
}
/* install the new SSL settings, wholesale */
pgSetup->ssl = newPgSetup->ssl;
strlcpy(pgSetup->ssl.sslModeStr,
pgsetup_sslmode_to_string(pgSetup->ssl.sslMode),
SSL_MODE_STRLEN);
return true;
}