3 * Scanner for the configuration file
5 * Copyright (c) 2000-2008, PostgreSQL Global Development Group
17 #include "miscadmin.h"
18 #include "storage/fd.h"
19 #include "utils/guc.h"
22 /* Avoid exit() on fatal scanner errors (a bit ugly -- see yy_fatal_error) */
24 #define fprintf(file, fmt, msg) ereport(ERROR, (errmsg_internal("%s", msg)))
32 GUC_UNQUOTED_STRING = 6,
38 struct name_value_pair
44 struct name_value_pair *next;
47 static unsigned int ConfigFileLineno;
49 /* flex fails to supply a prototype for yylex, so provide one */
52 static bool ParseConfigFile(const char *config_file, const char *calling_file,
53 int depth, GucContext context, int elevel,
54 struct name_value_pair **head_p,
55 struct name_value_pair **tail_p);
56 static void free_name_value_list(struct name_value_pair * list);
57 static char *GUC_scanstr(const char *s);
62 %option never-interactive
67 %option prefix="GUC_yy"
76 INTEGER {SIGN}?({DIGIT}+|0x{HEXDIGIT}+){UNIT_LETTER}*
78 EXPONENT [Ee]{SIGN}?{DIGIT}+
79 REAL {SIGN}?{DIGIT}*"."{DIGIT}*{EXPONENT}?
81 LETTER [A-Za-z_\200-\377]
82 LETTER_OR_DIGIT [A-Za-z_0-9\200-\377]
84 ID {LETTER}{LETTER_OR_DIGIT}*
85 QUALIFIED_ID {ID}"."{ID}
87 UNQUOTED_STRING {LETTER}({LETTER_OR_DIGIT}|[-._:/])*
88 STRING \'([^'\\\n]|\\.|\'\')*\'
92 \n ConfigFileLineno++; return GUC_EOL;
93 [ \t\r]+ /* eat whitespace */
94 #.* /* eat comment (.* matches anything until newline) */
97 {QUALIFIED_ID} return GUC_QUALIFIED_ID;
98 {STRING} return GUC_STRING;
99 {UNQUOTED_STRING} return GUC_UNQUOTED_STRING;
100 {INTEGER} return GUC_INTEGER;
101 {REAL} return GUC_REAL;
111 * Exported function to read and process the configuration file. The
112 * parameter indicates in what context the file is being read --- either
113 * postmaster startup (including standalone-backend startup) or SIGHUP.
114 * All options mentioned in the configuration file are set to new values.
115 * If an error occurs, no values will be changed.
118 ProcessConfigFile(GucContext context)
121 struct name_value_pair *item, *head, *tail;
123 struct config_string *cvc_struct;
127 Assert(context == PGC_POSTMASTER || context == PGC_SIGHUP);
129 if (context == PGC_SIGHUP)
132 * To avoid cluttering the log, only the postmaster bleats loudly
133 * about problems with the config file.
135 elevel = IsUnderPostmaster ? DEBUG2 : LOG;
140 /* Parse the file into a list of option names and values */
143 if (!ParseConfigFile(ConfigFileName, NULL,
149 * We need the proposed new value of custom_variable_classes to check
150 * custom variables with. ParseConfigFile ensured that if it's in
151 * the file, it's first in the list. But first check to see if we
152 * have an active value from the command line, which should override
153 * the file in any case. (Since there's no relevant env var, the
154 * only possible nondefault sources are the file and ARGV.)
156 cvc_struct = (struct config_string *)
157 find_option("custom_variable_classes", false, elevel);
158 if (cvc_struct && cvc_struct->gen.reset_source > PGC_S_FILE)
160 cvc = guc_strdup(elevel, cvc_struct->reset_val);
164 else if (head != NULL &&
165 guc_name_compare(head->name, "custom_variable_classes") == 0)
168 * Need to canonicalize the value via the assign hook. Casting away
169 * const is a bit ugly, but we know the result is malloc'd.
171 cvc = (char *) assign_custom_variable_classes(head->value,
176 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
177 errmsg("invalid value for parameter \"%s\": \"%s\"",
178 head->name, head->value)));
184 * Mark all extant GUC variables as not present in the config file.
185 * We need this so that we can tell below which ones have been removed
186 * from the file since we last processed it.
188 for (i = 0; i < num_guc_variables; i++)
190 struct config_generic *gconf = guc_variables[i];
192 gconf->status &= ~GUC_IS_IN_FILE;
196 * Check if all options are valid. As a side-effect, the GUC_IS_IN_FILE
197 * flag is set on each GUC variable mentioned in the list.
199 for (item = head; item; item = item->next)
201 char *sep = strchr(item->name, GUC_QUALIFIER_SEPARATOR);
206 * We have to consider three cases for custom variables:
208 * 1. The class name is not valid according to the (new) setting
209 * of custom_variable_classes. If so, reject. We don't care
210 * which side is at fault.
212 if (!is_custom_class(item->name, sep - item->name, cvc))
215 (errcode(ERRCODE_UNDEFINED_OBJECT),
216 errmsg("unrecognized configuration parameter \"%s\"",
221 * 2. There is no GUC entry. If we called set_config_option then
222 * it would make a placeholder, which we don't want to do yet,
223 * since we could still fail further down the list. Do nothing
224 * (assuming that making the placeholder will succeed later).
226 if (find_option(item->name, false, elevel) == NULL)
229 * 3. There is already a GUC entry (either real or placeholder) for
230 * the variable. In this case we should let set_config_option
231 * check it, since the assignment could well fail if it's a real
236 if (!set_config_option(item->name, item->value, context,
237 PGC_S_FILE, GUC_ACTION_SET, false))
242 * Check for variables having been removed from the config file, and
243 * revert their reset values (and perhaps also effective values) to the
244 * boot-time defaults. If such a variable can't be changed after startup,
245 * just throw a warning and continue. (This is analogous to the fact that
246 * set_config_option only throws a warning for a new but different value.
247 * If we wanted to make it a hard error, we'd need an extra pass over the
248 * list so that we could throw the error before starting to apply
251 for (i = 0; i < num_guc_variables; i++)
253 struct config_generic *gconf = guc_variables[i];
256 if (gconf->reset_source != PGC_S_FILE ||
257 (gconf->status & GUC_IS_IN_FILE))
259 if (gconf->context < PGC_SIGHUP)
262 (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
263 errmsg("attempted change of parameter \"%s\" ignored",
265 errdetail("This parameter cannot be changed after server start.")));
270 * Reset any "file" sources to "default", else set_config_option
271 * will not override those settings.
273 if (gconf->reset_source == PGC_S_FILE)
274 gconf->reset_source = PGC_S_DEFAULT;
275 if (gconf->source == PGC_S_FILE)
276 gconf->source = PGC_S_DEFAULT;
277 for (stack = gconf->stack; stack; stack = stack->prev)
279 if (stack->source == PGC_S_FILE)
280 stack->source = PGC_S_DEFAULT;
283 /* Now we can re-apply the wired-in default */
284 set_config_option(gconf->name, NULL, context, PGC_S_DEFAULT,
285 GUC_ACTION_SET, true);
289 * Restore any variables determined by environment variables. This
290 * is a no-op except in the case where one of these had been in the
291 * config file and is now removed. PGC_S_ENV_VAR will override the
292 * wired-in default we just applied, but cannot override any other source.
294 * Keep this list in sync with InitializeGUCOptions()!
295 * PGPORT can be ignored, because it cannot be changed without restart.
296 * We assume rlimit hasn't changed, either.
298 envvar = getenv("PGDATESTYLE");
300 set_config_option("datestyle", envvar, PGC_POSTMASTER,
301 PGC_S_ENV_VAR, GUC_ACTION_SET, true);
303 envvar = getenv("PGCLIENTENCODING");
305 set_config_option("client_encoding", envvar, PGC_POSTMASTER,
306 PGC_S_ENV_VAR, GUC_ACTION_SET, true);
309 /* If we got here all the options checked out okay, so apply them. */
310 for (item = head; item; item = item->next)
312 if (set_config_option(item->name, item->value, context,
313 PGC_S_FILE, GUC_ACTION_SET, true))
315 set_config_sourcefile(item->name, item->filename,
320 /* Remember when we last successfully loaded the config file. */
321 PgReloadTime = GetCurrentTimestamp();
324 free_name_value_list(head);
331 * Read and parse a single configuration file. This function recurses
332 * to handle "include" directives.
335 * config_file: absolute or relative path of file to read
336 * calling_file: absolute path of file containing the "include" directive,
337 * or NULL at outer level (config_file must be absolute at outer level)
338 * depth: recursion depth (used only to prevent infinite recursion)
339 * context: GucContext passed to ProcessConfigFile()
340 * elevel: error logging level determined by ProcessConfigFile()
342 * head_p, tail_p: head and tail of linked list of name/value pairs
344 * *head_p and *tail_p must be initialized to NULL before calling the outer
345 * recursion level. On exit, they contain a list of name-value pairs read
346 * from the input file(s).
348 * Returns TRUE if successful, FALSE if an error occurred. The error has
349 * already been ereport'd, it is only necessary for the caller to clean up
350 * its own state and release the name/value pairs list.
352 * Note: if elevel >= ERROR then an error will not return control to the
353 * caller, and internal state such as open files will not be cleaned up.
354 * This case occurs only during postmaster or standalone-backend startup,
355 * where an error will lead to immediate process exit anyway; so there is
356 * no point in contorting the code so it can clean up nicely.
359 ParseConfigFile(const char *config_file, const char *calling_file,
360 int depth, GucContext context, int elevel,
361 struct name_value_pair **head_p,
362 struct name_value_pair **tail_p)
365 char abs_path[MAXPGPATH];
367 YY_BUFFER_STATE lex_buffer;
371 * Reject too-deep include nesting depth. This is just a safety check
372 * to avoid dumping core due to stack overflow if an include file loops
373 * back to itself. The maximum nesting depth is pretty arbitrary.
378 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
379 errmsg("could not open configuration file \"%s\": maximum nesting depth exceeded",
385 * If config_file is a relative path, convert to absolute. We consider
386 * it to be relative to the directory holding the calling file.
388 if (!is_absolute_path(config_file))
390 Assert(calling_file != NULL);
391 strlcpy(abs_path, calling_file, sizeof(abs_path));
392 get_parent_directory(abs_path);
393 join_path_components(abs_path, abs_path, config_file);
394 canonicalize_path(abs_path);
395 config_file = abs_path;
398 fp = AllocateFile(config_file, "r");
402 (errcode_for_file_access(),
403 errmsg("could not open configuration file \"%s\": %m",
411 lex_buffer = yy_create_buffer(fp, YY_BUF_SIZE);
412 yy_switch_to_buffer(lex_buffer);
414 ConfigFileLineno = 1;
416 /* This loop iterates once per logical line */
417 while ((token = yylex()))
419 char *opt_name, *opt_value;
420 struct name_value_pair *item;
422 if (token == GUC_EOL) /* empty or comment line */
425 /* first token on line is option name */
426 if (token != GUC_ID && token != GUC_QUALIFIED_ID)
428 opt_name = pstrdup(yytext);
430 /* next we have an optional equal sign; discard if present */
432 if (token == GUC_EQUALS)
435 /* now we must have the option value */
436 if (token != GUC_ID &&
437 token != GUC_STRING &&
438 token != GUC_INTEGER &&
440 token != GUC_UNQUOTED_STRING)
442 if (token == GUC_STRING) /* strip quotes and escapes */
443 opt_value = GUC_scanstr(yytext);
445 opt_value = pstrdup(yytext);
447 /* now we'd like an end of line, or possibly EOF */
449 if (token != GUC_EOL && token != 0)
452 /* OK, process the option name and value */
453 if (guc_name_compare(opt_name, "include") == 0)
456 * An include directive isn't a variable and should be processed
459 unsigned int save_ConfigFileLineno = ConfigFileLineno;
461 if (!ParseConfigFile(opt_value, config_file,
462 depth + 1, context, elevel,
470 yy_switch_to_buffer(lex_buffer);
471 ConfigFileLineno = save_ConfigFileLineno;
475 else if (guc_name_compare(opt_name, "custom_variable_classes") == 0)
478 * This variable must be processed first as it controls
479 * the validity of other variables; so it goes at the head
480 * of the result list. If we already found a value for it,
481 * replace with this one.
485 guc_name_compare(item->name, "custom_variable_classes") == 0)
487 /* replace existing head item */
490 item->name = opt_name;
491 item->value = opt_value;
492 item->filename = pstrdup(config_file);
493 item->sourceline = ConfigFileLineno-1;
497 /* prepend to list */
498 item = palloc(sizeof *item);
499 item->name = opt_name;
500 item->value = opt_value;
501 item->filename = pstrdup(config_file);
502 item->sourceline = ConfigFileLineno-1;
503 item->next = *head_p;
511 /* ordinary variable, append to list */
512 item = palloc(sizeof *item);
513 item->name = opt_name;
514 item->value = opt_value;
515 item->filename = pstrdup(config_file);
516 item->sourceline = ConfigFileLineno-1;
521 (*tail_p)->next = item;
525 /* break out of loop if read EOF, else loop for next line */
530 /* successful completion of parsing */
534 if (token == GUC_EOL || token == 0)
536 (errcode(ERRCODE_SYNTAX_ERROR),
537 errmsg("syntax error in file \"%s\" line %u, near end of line",
538 config_file, ConfigFileLineno - 1)));
541 (errcode(ERRCODE_SYNTAX_ERROR),
542 errmsg("syntax error in file \"%s\" line %u, near token \"%s\"",
543 config_file, ConfigFileLineno, yytext)));
547 yy_delete_buffer(lex_buffer);
554 * Free a list of name/value pairs, including the names and the values
557 free_name_value_list(struct name_value_pair *list)
559 struct name_value_pair *item;
564 struct name_value_pair *next = item->next;
568 pfree(item->filename);
578 * Strip the quotes surrounding the given string, and collapse any embedded
579 * '' sequences and backslash escapes.
581 * the string returned is palloc'd and should eventually be pfree'd by the
585 GUC_scanstr(const char *s)
592 Assert(s != NULL && s[0] == '\'');
595 Assert(s[len-1] == '\'');
597 /* Skip the leading quote; we'll handle the trailing quote below */
600 /* Since len still includes trailing quote, this is enough space */
601 newStr = palloc(len);
603 for (i = 0, j = 0; i < len; i++)
638 s[i + k] >= '0' && s[i + k] <= '7' && k < 3;
640 octVal = (octVal << 3) + (s[i + k] - '0');
642 newStr[j] = ((char) octVal);
650 else if (s[i] == '\'' && s[i+1] == '\'')
652 /* doubled quote becomes just one quote */
660 /* We copied the ending quote to newStr, so replace with \0 */
661 Assert(j > 0 && j <= len);