Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 240 lines (208 sloc) 6.592 kb
a12e719 Initial commit
postgres authored
1 /*
9b7a078 Gabriele Bartolini Added license information in every source file
gbartolini authored
2 * config.c - Functions to parse the config file
1d32133 Update all the copyrights to add 2011
Greg Smith authored
3 * Copyright (C) 2ndQuadrant, 2010-2011
a12e719 Initial commit
postgres authored
4 *
9b7a078 Gabriele Bartolini Added license information in every source file
gbartolini authored
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9843205 Reformat all source code using astyle
Greg Smith authored
9 *
9b7a078 Gabriele Bartolini Added license information in every source file
gbartolini authored
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
9843205 Reformat all source code using astyle
Greg Smith authored
14 *
9b7a078 Gabriele Bartolini Added license information in every source file
gbartolini authored
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
9843205 Reformat all source code using astyle
Greg Smith authored
17 *
a12e719 Initial commit
postgres authored
18 */
19
2c1eafd Gabriele Bartolini first alpha version for syslog support
gbartolini authored
20 #include "config.h"
e52f2e8 Update use of paranthesis in logging messages.
Greg Smith authored
21 #include "log.h"
916c049 sprintf to snprintf conversion
Dan Farina authored
22 #include "strutil.h"
e52f2e8 Update use of paranthesis in logging messages.
Greg Smith authored
23 #include "repmgr.h"
f2bec9a Gabriele Bartolini Some cosmetic changes
gbartolini authored
24
a12e719 Initial commit
postgres authored
25 void
8200b68 Convert some remaining fprintf messages to logger
Greg Smith authored
26 parse_config(const char *config_file, t_configuration_options *options)
a12e719 Initial commit
postgres authored
27 {
f2bec9a Gabriele Bartolini Some cosmetic changes
gbartolini authored
28 char *s, buff[MAXLINELENGTH];
c687d0d Gabriele Bartolini Reviewed the code, fixed a small bug with option name detection
gbartolini authored
29 char name[MAXLEN];
30 char value[MAXLEN];
31
ae628d0 Jaime2ndQuadrant Changes in repmgr are:
Jaime2ndQuadrant authored
32 FILE *fp = fopen (config_file, "r");
a12e719 Initial commit
postgres authored
33
f928edf Improve parameter validation, logging, and error
Greg Smith authored
34 /* Initialize */
35 memset(options->cluster_name, 0, sizeof(options->cluster_name));
36 options->node = -1;
37 memset(options->conninfo, 0, sizeof(options->conninfo));
3a950c9 Squashed commit of the following:
Greg Smith authored
38 options->failover = MANUAL_FAILOVER;
39 options->priority = 0;
40 memset(options->promote_command, 0, sizeof(options->promote_command));
41 memset(options->follow_command, 0, sizeof(options->follow_command));
f928edf Improve parameter validation, logging, and error
Greg Smith authored
42 memset(options->rsync_options, 0, sizeof(options->rsync_options));
43
3b2203c Make verbose command line option useful on top of
Greg Smith authored
44 /*
45 * Since some commands don't require a config file at all, not
46 * having one isn't necessarily a problem.
47 */
0130420 astyle cleanup run after recent merges
Greg Smith authored
48 if (fp == NULL)
49 {
8200b68 Convert some remaining fprintf messages to logger
Greg Smith authored
50 log_err(_("Did not find the configuration file '%s', continuing\n"), config_file);
3b2203c Make verbose command line option useful on top of
Greg Smith authored
51 return;
814863e trbs use struct for config file information
trbs authored
52 }
0130420 astyle cleanup run after recent merges
Greg Smith authored
53
a12e719 Initial commit
postgres authored
54 /* Read next line */
55 while ((s = fgets (buff, sizeof buff, fp)) != NULL)
56 {
c687d0d Gabriele Bartolini Reviewed the code, fixed a small bug with option name detection
gbartolini authored
57 /* Skip blank lines and comments */
58 if (buff[0] == '\n' || buff[0] == '#')
59 continue;
a12e719 Initial commit
postgres authored
60
c687d0d Gabriele Bartolini Reviewed the code, fixed a small bug with option name detection
gbartolini authored
61 /* Parse name/value pair from line */
a12e719 Initial commit
postgres authored
62 parse_line(buff, name, value);
63
c687d0d Gabriele Bartolini Reviewed the code, fixed a small bug with option name detection
gbartolini authored
64 /* Copy into correct entry in parameters struct */
05e88a2 Gabriele Bartolini removed strncmp improper usage, initialise values asap
gbartolini authored
65 if (strcmp(name, "cluster") == 0)
1be62fa Fix use of "options" broken by recent merging
Greg Smith authored
66 strncpy (options->cluster_name, value, MAXLEN);
05e88a2 Gabriele Bartolini removed strncmp improper usage, initialise values asap
gbartolini authored
67 else if (strcmp(name, "node") == 0)
1be62fa Fix use of "options" broken by recent merging
Greg Smith authored
68 options->node = atoi(value);
05e88a2 Gabriele Bartolini removed strncmp improper usage, initialise values asap
gbartolini authored
69 else if (strcmp(name, "conninfo") == 0)
1be62fa Fix use of "options" broken by recent merging
Greg Smith authored
70 strncpy (options->conninfo, value, MAXLEN);
52313a2 trbs made rsync_options configurable
trbs authored
71 else if (strcmp(name, "rsync_options") == 0)
1be62fa Fix use of "options" broken by recent merging
Greg Smith authored
72 strncpy (options->rsync_options, value, QUERY_STR_LEN);
2c1eafd Gabriele Bartolini first alpha version for syslog support
gbartolini authored
73 else if (strcmp(name, "loglevel") == 0)
74 strncpy (options->loglevel, value, MAXLEN);
75 else if (strcmp(name, "logfacility") == 0)
76 strncpy (options->logfacility, value, MAXLEN);
3a950c9 Squashed commit of the following:
Greg Smith authored
77 else if (strcmp(name, "failover") == 0)
78 {
79 char failoverstr[MAXLEN];
80 strncpy(failoverstr, value, MAXLEN);
81
82 if (strcmp(failoverstr, "manual") == 0)
83 options->failover = MANUAL_FAILOVER;
84 else if (strcmp(failoverstr, "automatic") == 0)
85 options->failover = AUTOMATIC_FAILOVER;
86 else
87 {
88 log_warning(_("value for failover option is incorrect, it should be automatic or manual. Defaulting to manual.\n"));
89 options->failover = MANUAL_FAILOVER;
90 }
91 }
92 else if (strcmp(name, "priority") == 0)
93 options->priority = atoi(value);
94 else if (strcmp(name, "promote_command") == 0)
95 strncpy(options->promote_command, value, MAXLEN);
96 else if (strcmp(name, "follow_command") == 0)
97 strncpy(options->follow_command, value, MAXLEN);
c687d0d Gabriele Bartolini Reviewed the code, fixed a small bug with option name detection
gbartolini authored
98 else
8200b68 Convert some remaining fprintf messages to logger
Greg Smith authored
99 log_warning(_("%s/%s: Unknown name/value pair!\n"), name, value);
c687d0d Gabriele Bartolini Reviewed the code, fixed a small bug with option name detection
gbartolini authored
100 }
101
9843205 Reformat all source code using astyle
Greg Smith authored
102 /* Close file */
103 fclose (fp);
814863e trbs use struct for config file information
trbs authored
104
105 /* Check config settings */
1be62fa Fix use of "options" broken by recent merging
Greg Smith authored
106 if (strnlen(options->cluster_name, MAXLEN)==0)
814863e trbs use struct for config file information
trbs authored
107 {
8200b68 Convert some remaining fprintf messages to logger
Greg Smith authored
108 log_err(_("Cluster name is missing. Check the configuration file.\n"));
78b925e Unify use of error codes across all exit statements
Greg Smith authored
109 exit(ERR_BAD_CONFIG);
814863e trbs use struct for config file information
trbs authored
110 }
111
1be62fa Fix use of "options" broken by recent merging
Greg Smith authored
112 if (options->node == -1)
814863e trbs use struct for config file information
trbs authored
113 {
8200b68 Convert some remaining fprintf messages to logger
Greg Smith authored
114 log_err(_("Node information is missing. Check the configuration file.\n"));
78b925e Unify use of error codes across all exit statements
Greg Smith authored
115 exit(ERR_BAD_CONFIG);
814863e trbs use struct for config file information
trbs authored
116 }
a12e719 Initial commit
postgres authored
117 }
118
8200b68 Convert some remaining fprintf messages to logger
Greg Smith authored
119
a12e719 Initial commit
postgres authored
120 char *
121 trim (char *s)
122 {
9843205 Reformat all source code using astyle
Greg Smith authored
123 /* Initialize start, end pointers */
124 char *s1 = s, *s2 = &s[strlen (s) - 1];
a12e719 Initial commit
postgres authored
125
9843205 Reformat all source code using astyle
Greg Smith authored
126 /* Trim and delimit right side */
127 while ( (isspace (*s2)) && (s2 >= s1) )
128 --s2;
129 *(s2+1) = '\0';
a12e719 Initial commit
postgres authored
130
9843205 Reformat all source code using astyle
Greg Smith authored
131 /* Trim left side */
132 while ( (isspace (*s1)) && (s1 < s2) )
133 ++s1;
a12e719 Initial commit
postgres authored
134
9843205 Reformat all source code using astyle
Greg Smith authored
135 /* Copy finished string */
136 strcpy (s, s1);
137 return s;
a12e719 Initial commit
postgres authored
138 }
139
140 void
141 parse_line(char *buff, char *name, char *value)
142 {
c687d0d Gabriele Bartolini Reviewed the code, fixed a small bug with option name detection
gbartolini authored
143 int i = 0;
144 int j = 0;
a12e719 Initial commit
postgres authored
145
146 /*
147 * first we find the name of the parameter
148 */
c687d0d Gabriele Bartolini Reviewed the code, fixed a small bug with option name detection
gbartolini authored
149 for ( ; i < MAXLEN; ++i)
a12e719 Initial commit
postgres authored
150 {
151 if (buff[i] != '=')
152 name[j++] = buff[i];
153 else
154 break;
155 }
156 name[j] = '\0';
157
158 /*
159 * Now the value
9843205 Reformat all source code using astyle
Greg Smith authored
160 */
a12e719 Initial commit
postgres authored
161 j = 0;
763a1e8 Gabriele Bartolini Reviewed the code, fixed a small bug with option name detection
gbartolini authored
162 for ( ++i ; i < MAXLEN; ++i)
a12e719 Initial commit
postgres authored
163 if (buff[i] == '\'')
164 continue;
165 else if (buff[i] != '\n')
166 value[j++] = buff[i];
167 else
168 break;
169 value[j] = '\0';
c687d0d Gabriele Bartolini Reviewed the code, fixed a small bug with option name detection
gbartolini authored
170 trim(value);
a12e719 Initial commit
postgres authored
171 }
8200b68 Convert some remaining fprintf messages to logger
Greg Smith authored
172
3a950c9 Squashed commit of the following:
Greg Smith authored
173 bool
174 reload_configuration(char *config_file, t_configuration_options *orig_options)
175 {
176 PGconn *conn;
177
178 t_configuration_options new_options;
179
180 /*
181 * Re-read the configuration file: repmgr.conf
182 */
183 log_info(_("Reloading configuration file and updating repmgr tables\n"));
184 parse_config(config_file, &new_options);
185 if (new_options.node == -1)
186 {
187 log_warning(_("\nCannot load new configuration, will keep current one.\n"));
188 return false;
189 }
190
191 if (strcmp(new_options.cluster_name, orig_options->cluster_name) != 0)
192 {
193 log_warning(_("\nCannot change cluster name, will keep current configuration.\n"));
194 return false;
195 }
196
197 if (new_options.node != orig_options->node)
198 {
199 log_warning(_("\nCannot change node number, will keep current configuration.\n"));
200 return false;
201 }
202
203 if (new_options.failover != MANUAL_FAILOVER && new_options.failover != AUTOMATIC_FAILOVER)
204 {
205 log_warning(_("\nNew value for failover is not valid. Should be MANUAL or AUTOMATIC.\n"));
206 return false;
207 }
208
209 /* Test conninfo string */
210 conn = establishDBConnection(new_options.conninfo, false);
211 if (!conn || (PQstatus(conn) != CONNECTION_OK))
212 {
213 log_warning(_("\nconninfo string is not valid, will keep current configuration.\n"));
214 return false;
215 }
216 PQfinish(conn);
217
218 /* Configuration seems ok, will load new values */
219 strcpy(orig_options->cluster_name, new_options.cluster_name);
220 orig_options->node = new_options.node;
221 strcpy(orig_options->conninfo, new_options.conninfo);
222 orig_options->failover = new_options.failover;
223 orig_options->priority = new_options.priority;
224 strcpy(orig_options->promote_command, new_options.promote_command);
225 strcpy(orig_options->follow_command, new_options.follow_command);
226 strcpy(orig_options->rsync_options, new_options.rsync_options);
227 /*
228 * XXX These ones can change with a simple SIGHUP?
229
230 strcpy (orig_options->loglevel, new_options.loglevel);
231 strcpy (orig_options->logfacility, new_options.logfacility);
232
233 logger_shutdown();
234 XXX do we have progname here ?
235 logger_init(progname, orig_options.loglevel, orig_options.logfacility);
236 */
237
238 return true;
239 }
Something went wrong with that request. Please try again.