Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 239 lines (208 sloc) 6.592 kb
a12e7192 » postgres
2010-06-17 Initial commit
1 /*
9b7a078e » gbartolini
2010-12-16 Added license information in every source file
2 * config.c - Functions to parse the config file
1d32133d » Greg Smith
2011-02-10 Update all the copyrights to add 2011
3 * Copyright (C) 2ndQuadrant, 2010-2011
a12e7192 » postgres
2010-06-17 Initial commit
4 *
9b7a078e » gbartolini
2010-12-16 Added license information in every source file
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.
9843205a » Greg Smith
2010-12-22 Reformat all source code using astyle
9 *
9b7a078e » gbartolini
2010-12-16 Added license information in every source file
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.
9843205a » Greg Smith
2010-12-22 Reformat all source code using astyle
14 *
9b7a078e » gbartolini
2010-12-16 Added license information in every source file
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/>.
9843205a » Greg Smith
2010-12-22 Reformat all source code using astyle
17 *
a12e7192 » postgres
2010-06-17 Initial commit
18 */
19
2c1eafd7 » gbartolini
2010-12-29 first alpha version for syslog support
20 #include "config.h"
e52f2e88 » Greg Smith
2011-06-07 Update use of paranthesis in logging messages.
21 #include "log.h"
916c0492 » Dan Farina
2010-12-07 sprintf to snprintf conversion
22 #include "strutil.h"
e52f2e88 » Greg Smith
2011-06-07 Update use of paranthesis in logging messages.
23 #include "repmgr.h"
f2bec9a0 » gbartolini
2010-12-16 Some cosmetic changes
24
a12e7192 » postgres
2010-06-17 Initial commit
25 void
8200b68c » Greg Smith
2011-06-07 Convert some remaining fprintf messages to logger
26 parse_config(const char *config_file, t_configuration_options *options)
a12e7192 » postgres
2010-06-17 Initial commit
27 {
f2bec9a0 » gbartolini
2010-12-16 Some cosmetic changes
28 char *s, buff[MAXLINELENGTH];
c687d0d6 » gbartolini
2010-12-16 Reviewed the code, fixed a small bug with option name detection
29 char name[MAXLEN];
30 char value[MAXLEN];
31
ae628d05 » Jaime2ndQuadrant
2010-09-28 Changes in repmgr are:
32 FILE *fp = fopen (config_file, "r");
a12e7192 » postgres
2010-06-17 Initial commit
33
f928edf8 » Greg Smith
2011-02-24 Improve parameter validation, logging, and error
34 /* Initialize */
35 memset(options->cluster_name, 0, sizeof(options->cluster_name));
36 options->node = -1;
37 memset(options->conninfo, 0, sizeof(options->conninfo));
3a950c9f » Greg Smith
2011-06-07 Squashed commit of the following:
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));
f928edf8 » Greg Smith
2011-02-24 Improve parameter validation, logging, and error
42 memset(options->rsync_options, 0, sizeof(options->rsync_options));
43
3b2203c3 » Greg Smith
2011-02-23 Make verbose command line option useful on top of
44 /*
45 * Since some commands don't require a config file at all, not
46 * having one isn't necessarily a problem.
47 */
01304209 » Greg Smith
2011-02-10 astyle cleanup run after recent merges
48 if (fp == NULL)
49 {
8200b68c » Greg Smith
2011-06-07 Convert some remaining fprintf messages to logger
50 log_err(_("Did not find the configuration file '%s', continuing\n"), config_file);
3b2203c3 » Greg Smith
2011-02-23 Make verbose command line option useful on top of
51 return;
814863ed » trbs
2011-01-07 use struct for config file information
52 }
01304209 » Greg Smith
2011-02-10 astyle cleanup run after recent merges
53
a12e7192 » postgres
2010-06-17 Initial commit
54 /* Read next line */
55 while ((s = fgets (buff, sizeof buff, fp)) != NULL)
56 {
c687d0d6 » gbartolini
2010-12-16 Reviewed the code, fixed a small bug with option name detection
57 /* Skip blank lines and comments */
58 if (buff[0] == '\n' || buff[0] == '#')
59 continue;
a12e7192 » postgres
2010-06-17 Initial commit
60
c687d0d6 » gbartolini
2010-12-16 Reviewed the code, fixed a small bug with option name detection
61 /* Parse name/value pair from line */
a12e7192 » postgres
2010-06-17 Initial commit
62 parse_line(buff, name, value);
63
c687d0d6 » gbartolini
2010-12-16 Reviewed the code, fixed a small bug with option name detection
64 /* Copy into correct entry in parameters struct */
05e88a2c » gbartolini
2010-12-16 removed strncmp improper usage, initialise values asap
65 if (strcmp(name, "cluster") == 0)
1be62fa6 » Greg Smith
2011-02-10 Fix use of "options" broken by recent merging
66 strncpy (options->cluster_name, value, MAXLEN);
05e88a2c » gbartolini
2010-12-16 removed strncmp improper usage, initialise values asap
67 else if (strcmp(name, "node") == 0)
1be62fa6 » Greg Smith
2011-02-10 Fix use of "options" broken by recent merging
68 options->node = atoi(value);
05e88a2c » gbartolini
2010-12-16 removed strncmp improper usage, initialise values asap
69 else if (strcmp(name, "conninfo") == 0)
1be62fa6 » Greg Smith
2011-02-10 Fix use of "options" broken by recent merging
70 strncpy (options->conninfo, value, MAXLEN);
52313a2d » trbs
2011-01-08 made rsync_options configurable
71 else if (strcmp(name, "rsync_options") == 0)
1be62fa6 » Greg Smith
2011-02-10 Fix use of "options" broken by recent merging
72 strncpy (options->rsync_options, value, QUERY_STR_LEN);
2c1eafd7 » gbartolini
2010-12-29 first alpha version for syslog support
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);
3a950c9f » Greg Smith
2011-06-07 Squashed commit of the following:
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);
c687d0d6 » gbartolini
2010-12-16 Reviewed the code, fixed a small bug with option name detection
98 else
8200b68c » Greg Smith
2011-06-07 Convert some remaining fprintf messages to logger
99 log_warning(_("%s/%s: Unknown name/value pair!\n"), name, value);
c687d0d6 » gbartolini
2010-12-16 Reviewed the code, fixed a small bug with option name detection
100 }
101
9843205a » Greg Smith
2010-12-22 Reformat all source code using astyle
102 /* Close file */
103 fclose (fp);
814863ed » trbs
2011-01-07 use struct for config file information
104
105 /* Check config settings */
1be62fa6 » Greg Smith
2011-02-10 Fix use of "options" broken by recent merging
106 if (strnlen(options->cluster_name, MAXLEN)==0)
814863ed » trbs
2011-01-07 use struct for config file information
107 {
8200b68c » Greg Smith
2011-06-07 Convert some remaining fprintf messages to logger
108 log_err(_("Cluster name is missing. Check the configuration file.\n"));
78b925ea » Greg Smith
2011-02-10 Unify use of error codes across all exit statements
109 exit(ERR_BAD_CONFIG);
814863ed » trbs
2011-01-07 use struct for config file information
110 }
111
1be62fa6 » Greg Smith
2011-02-10 Fix use of "options" broken by recent merging
112 if (options->node == -1)
814863ed » trbs
2011-01-07 use struct for config file information
113 {
8200b68c » Greg Smith
2011-06-07 Convert some remaining fprintf messages to logger
114 log_err(_("Node information is missing. Check the configuration file.\n"));
78b925ea » Greg Smith
2011-02-10 Unify use of error codes across all exit statements
115 exit(ERR_BAD_CONFIG);
814863ed » trbs
2011-01-07 use struct for config file information
116 }
a12e7192 » postgres
2010-06-17 Initial commit
117 }
118
8200b68c » Greg Smith
2011-06-07 Convert some remaining fprintf messages to logger
119
a12e7192 » postgres
2010-06-17 Initial commit
120 char *
121 trim (char *s)
122 {
9843205a » Greg Smith
2010-12-22 Reformat all source code using astyle
123 /* Initialize start, end pointers */
124 char *s1 = s, *s2 = &s[strlen (s) - 1];
a12e7192 » postgres
2010-06-17 Initial commit
125
9843205a » Greg Smith
2010-12-22 Reformat all source code using astyle
126 /* Trim and delimit right side */
127 while ( (isspace (*s2)) && (s2 >= s1) )
128 --s2;
129 *(s2+1) = '\0';
a12e7192 » postgres
2010-06-17 Initial commit
130
9843205a » Greg Smith
2010-12-22 Reformat all source code using astyle
131 /* Trim left side */
132 while ( (isspace (*s1)) && (s1 < s2) )
133 ++s1;
a12e7192 » postgres
2010-06-17 Initial commit
134
9843205a » Greg Smith
2010-12-22 Reformat all source code using astyle
135 /* Copy finished string */
136 strcpy (s, s1);
137 return s;
a12e7192 » postgres
2010-06-17 Initial commit
138 }
139
140 void
141 parse_line(char *buff, char *name, char *value)
142 {
c687d0d6 » gbartolini
2010-12-16 Reviewed the code, fixed a small bug with option name detection
143 int i = 0;
144 int j = 0;
a12e7192 » postgres
2010-06-17 Initial commit
145
146 /*
147 * first we find the name of the parameter
148 */
c687d0d6 » gbartolini
2010-12-16 Reviewed the code, fixed a small bug with option name detection
149 for ( ; i < MAXLEN; ++i)
a12e7192 » postgres
2010-06-17 Initial commit
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
9843205a » Greg Smith
2010-12-22 Reformat all source code using astyle
160 */
a12e7192 » postgres
2010-06-17 Initial commit
161 j = 0;
763a1e8b » gbartolini
2010-12-16 Reviewed the code, fixed a small bug with option name detection
162 for ( ++i ; i < MAXLEN; ++i)
a12e7192 » postgres
2010-06-17 Initial commit
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';
c687d0d6 » gbartolini
2010-12-16 Reviewed the code, fixed a small bug with option name detection
170 trim(value);
a12e7192 » postgres
2010-06-17 Initial commit
171 }
8200b68c » Greg Smith
2011-06-07 Convert some remaining fprintf messages to logger
172
3a950c9f » Greg Smith
2011-06-07 Squashed commit of the following:
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.