]> granicus.if.org Git - postgresql/blob - src/backend/utils/misc/guc-file.l
2986d2f25ba96f6f6ba331ff24f41dabae9b952c
[postgresql] / src / backend / utils / misc / guc-file.l
1 /* -*-pgsql-c-*- */
2 /*
3  * Scanner for the configuration file
4  *
5  * Copyright (c) 2000-2010, PostgreSQL Global Development Group
6  *
7  * src/backend/utils/misc/guc-file.l
8  */
9
10 %{
11
12 #include "postgres.h"
13
14 #include <ctype.h>
15 #include <unistd.h>
16
17 #include "miscadmin.h"
18 #include "storage/fd.h"
19 #include "utils/guc.h"
20
21
22 /* Avoid exit() on fatal scanner errors (a bit ugly -- see yy_fatal_error) */
23 #undef fprintf
24 #define fprintf(file, fmt, msg)  ereport(ERROR, (errmsg_internal("%s", msg)))
25
26 enum {
27         GUC_ID = 1,
28         GUC_STRING = 2,
29         GUC_INTEGER = 3,
30         GUC_REAL = 4,
31         GUC_EQUALS = 5,
32         GUC_UNQUOTED_STRING = 6,
33         GUC_QUALIFIED_ID = 7,
34         GUC_EOL = 99,
35         GUC_ERROR = 100
36 };
37
38 struct name_value_pair
39 {
40         char       *name;
41         char       *value;
42         char       *filename;
43         int                     sourceline;
44         struct name_value_pair *next;
45 };
46
47 static unsigned int ConfigFileLineno;
48
49 /* flex fails to supply a prototype for yylex, so provide one */
50 int GUC_yylex(void);
51
52 static bool ParseConfigFile(const char *config_file, const char *calling_file,
53                                                         int depth, 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);
58
59 %}
60
61 %option 8bit
62 %option never-interactive
63 %option nodefault
64 %option noinput
65 %option nounput
66 %option noyywrap
67 %option prefix="GUC_yy"
68
69
70 SIGN            ("-"|"+")
71 DIGIT           [0-9]
72 HEXDIGIT        [0-9a-fA-F]
73
74 UNIT_LETTER     [a-zA-Z]
75
76 INTEGER         {SIGN}?({DIGIT}+|0x{HEXDIGIT}+){UNIT_LETTER}*
77
78 EXPONENT        [Ee]{SIGN}?{DIGIT}+
79 REAL            {SIGN}?{DIGIT}*"."{DIGIT}*{EXPONENT}?
80
81 LETTER          [A-Za-z_\200-\377]
82 LETTER_OR_DIGIT [A-Za-z_0-9\200-\377]
83
84 ID              {LETTER}{LETTER_OR_DIGIT}*
85 QUALIFIED_ID    {ID}"."{ID}
86
87 UNQUOTED_STRING {LETTER}({LETTER_OR_DIGIT}|[-._:/])*
88 STRING          \'([^'\\\n]|\\.|\'\')*\'
89
90 %%
91
92 \n              ConfigFileLineno++; return GUC_EOL;
93 [ \t\r]+        /* eat whitespace */
94 #.*             /* eat comment (.* matches anything until newline) */
95
96 {ID}            return GUC_ID;
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;
102 =               return GUC_EQUALS;
103
104 .               return GUC_ERROR;
105
106 %%
107
108
109
110 /*
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.
116  */
117 void
118 ProcessConfigFile(GucContext context)
119 {
120         int                     elevel;
121         struct name_value_pair *item, *head, *tail;
122         char       *cvc = NULL;
123         struct config_string *cvc_struct;
124         const char *envvar;
125         int                     i;
126
127         Assert(context == PGC_POSTMASTER || context == PGC_SIGHUP);
128
129         if (context == PGC_SIGHUP)
130         {
131                 /*
132                  * To avoid cluttering the log, only the postmaster bleats loudly
133                  * about problems with the config file.
134                  */
135                 elevel = IsUnderPostmaster ? DEBUG2 : LOG;
136         }
137         else
138                 elevel = ERROR;
139
140         /* Parse the file into a list of option names and values */
141         head = tail = NULL;
142
143         if (!ParseConfigFile(ConfigFileName, NULL, 0, elevel, &head, &tail))
144                 goto cleanup_list;
145
146         /*
147          * We need the proposed new value of custom_variable_classes to check
148          * custom variables with.  ParseConfigFile ensured that if it's in
149          * the file, it's first in the list.  But first check to see if we
150          * have an active value from the command line, which should override
151          * the file in any case.  (Since there's no relevant env var, the
152          * only possible nondefault sources are the file and ARGV.)
153          */
154         cvc_struct = (struct config_string *)
155                 find_option("custom_variable_classes", false, elevel);
156         if (cvc_struct && cvc_struct->gen.reset_source > PGC_S_FILE)
157         {
158                 cvc = guc_strdup(elevel, cvc_struct->reset_val);
159                 if (cvc == NULL)
160                         goto cleanup_list;
161         }
162         else if (head != NULL &&
163                          guc_name_compare(head->name, "custom_variable_classes") == 0)
164         {
165                 /*
166                  * Need to canonicalize the value via the assign hook.  Casting away
167                  * const is a bit ugly, but we know the result is malloc'd.
168                  */
169                 cvc = (char *) assign_custom_variable_classes(head->value,
170                                                                                                           false, PGC_S_FILE);
171                 if (cvc == NULL)
172                 {
173                         ereport(elevel,
174                                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
175                                          errmsg("invalid value for parameter \"%s\": \"%s\"",
176                                                         head->name, head->value)));
177                         goto cleanup_list;
178                 }
179         }
180
181         /*
182          * Mark all extant GUC variables as not present in the config file.
183          * We need this so that we can tell below which ones have been removed
184          * from the file since we last processed it.
185          */
186         for (i = 0; i < num_guc_variables; i++)
187         {
188                 struct config_generic *gconf = guc_variables[i];
189
190                 gconf->status &= ~GUC_IS_IN_FILE;
191         }
192
193         /*
194          * Check if all options are valid.  As a side-effect, the GUC_IS_IN_FILE
195          * flag is set on each GUC variable mentioned in the list.
196          */
197         for (item = head; item; item = item->next)
198         {
199                 char *sep = strchr(item->name, GUC_QUALIFIER_SEPARATOR);
200
201                 if (sep)
202                 {
203                         /*
204                          * We have to consider three cases for custom variables:
205                          *
206                          * 1. The class name is not valid according to the (new) setting
207                          * of custom_variable_classes.  If so, reject.  We don't care
208                          * which side is at fault.
209                          */
210                         if (!is_custom_class(item->name, sep - item->name, cvc))
211                         {
212                                 ereport(elevel,
213                                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
214                                                  errmsg("unrecognized configuration parameter \"%s\"",
215                                                                 item->name)));
216                                 goto cleanup_list;
217                         }
218                         /*
219                          * 2. There is no GUC entry.  If we called set_config_option then
220                          * it would make a placeholder, which we don't want to do yet,
221                          * since we could still fail further down the list.  Do nothing
222                          * (assuming that making the placeholder will succeed later).
223                          */
224                         if (find_option(item->name, false, elevel) == NULL)
225                                 continue;
226                         /*
227                          * 3. There is already a GUC entry (either real or placeholder) for
228                          * the variable.  In this case we should let set_config_option
229                          * check it, since the assignment could well fail if it's a real
230                          * entry.
231                          */
232                 }
233
234                 if (!set_config_option(item->name, item->value, context,
235                                                            PGC_S_FILE, GUC_ACTION_SET, false))
236                         goto cleanup_list;
237         }
238
239         /*
240          * Check for variables having been removed from the config file, and
241          * revert their reset values (and perhaps also effective values) to the
242          * boot-time defaults.  If such a variable can't be changed after startup,
243          * just throw a warning and continue.  (This is analogous to the fact that
244          * set_config_option only throws a warning for a new but different value.
245          * If we wanted to make it a hard error, we'd need an extra pass over the
246          * list so that we could throw the error before starting to apply
247          * changes.)
248          */
249         for (i = 0; i < num_guc_variables; i++)
250         {
251                 struct config_generic *gconf = guc_variables[i];
252                 GucStack   *stack;
253
254                 if (gconf->reset_source != PGC_S_FILE ||
255                         (gconf->status & GUC_IS_IN_FILE))
256                         continue;
257                 if (gconf->context < PGC_SIGHUP)
258                 {
259                         ereport(elevel,
260                                         (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
261                                          errmsg("parameter \"%s\" cannot be changed without restarting the server",
262                                                         gconf->name)));
263                         continue;
264                 }
265
266                 /*
267                  * Reset any "file" sources to "default", else set_config_option
268                  * will not override those settings.
269                  */
270                 if (gconf->reset_source == PGC_S_FILE)
271                         gconf->reset_source = PGC_S_DEFAULT;
272                 if (gconf->source == PGC_S_FILE)
273                         gconf->source = PGC_S_DEFAULT;
274                 for (stack = gconf->stack; stack; stack = stack->prev)
275                 {
276                         if (stack->source == PGC_S_FILE)
277                                 stack->source = PGC_S_DEFAULT;
278                 }
279
280                 /* Now we can re-apply the wired-in default */
281                 set_config_option(gconf->name, NULL, context, PGC_S_DEFAULT,
282                                                   GUC_ACTION_SET, true);
283                 if (context == PGC_SIGHUP)
284                         ereport(elevel,
285                                         (errmsg("parameter \"%s\" removed from configuration file, reset to default",
286                                                         gconf->name)));
287         }
288
289         /*
290          * Restore any variables determined by environment variables.  This
291          * is a no-op except in the case where one of these had been in the
292          * config file and is now removed.  PGC_S_ENV_VAR will override the
293          * wired-in default we just applied, but cannot override any other source.
294          *
295          * Keep this list in sync with InitializeGUCOptions()!
296          * PGPORT can be ignored, because it cannot be changed without restart.
297          * We assume rlimit hasn't changed, either.
298          */
299         envvar = getenv("PGDATESTYLE");
300         if (envvar != NULL)
301                 set_config_option("datestyle", envvar, PGC_POSTMASTER,
302                                                   PGC_S_ENV_VAR, GUC_ACTION_SET, true);
303
304         envvar = getenv("PGCLIENTENCODING");
305         if (envvar != NULL)
306                 set_config_option("client_encoding", envvar, PGC_POSTMASTER,
307                                                   PGC_S_ENV_VAR, GUC_ACTION_SET, true);
308
309
310         /* If we got here all the options checked out okay, so apply them. */
311         for (item = head; item; item = item->next)
312         {
313                 char   *pre_value = NULL;
314
315                 /* In SIGHUP cases in the postmaster, report changes */
316                 if (context == PGC_SIGHUP && !IsUnderPostmaster)
317                 {
318                         const char *preval = GetConfigOption(item->name, false);
319
320                         /* string variables could be NULL; treat that as empty */
321                         if (!preval)
322                                 preval = "";
323                         /* must dup, else might have dangling pointer below */
324                         pre_value = pstrdup(preval);
325                 }
326
327                 if (set_config_option(item->name, item->value, context,
328                                                                  PGC_S_FILE, GUC_ACTION_SET, true))
329                 {
330                         set_config_sourcefile(item->name, item->filename,
331                                                                   item->sourceline);
332
333                         if (pre_value)
334                         {
335                                 const char *post_value = GetConfigOption(item->name, false);
336
337                                 if (!post_value)
338                                         post_value = "";
339                                 if (strcmp(pre_value, post_value) != 0)
340                                         ereport(elevel,
341                                                         (errmsg("parameter \"%s\" changed to \"%s\"",
342                                                                         item->name, item->value)));
343                         }
344                 }
345
346                 if (pre_value)
347                         pfree(pre_value);
348         }
349
350         /* Remember when we last successfully loaded the config file. */
351         PgReloadTime = GetCurrentTimestamp();
352
353  cleanup_list:
354         free_name_value_list(head);
355         if (cvc)
356                 free(cvc);
357 }
358
359
360 /*
361  * Read and parse a single configuration file.  This function recurses
362  * to handle "include" directives.
363  *
364  * Input parameters:
365  *      config_file: absolute or relative path of file to read
366  *      calling_file: absolute path of file containing the "include" directive,
367  *              or NULL at outer level (config_file must be absolute at outer level)
368  *      depth: recursion depth (used only to prevent infinite recursion)
369  *      elevel: error logging level determined by ProcessConfigFile()
370  * Output parameters:
371  *      head_p, tail_p: head and tail of linked list of name/value pairs
372  *
373  * *head_p and *tail_p must be initialized to NULL before calling the outer
374  * recursion level.  On exit, they contain a list of name-value pairs read
375  * from the input file(s).
376  *
377  * Returns TRUE if successful, FALSE if an error occurred.  The error has
378  * already been ereport'd, it is only necessary for the caller to clean up
379  * its own state and release the name/value pairs list.
380  *
381  * Note: if elevel >= ERROR then an error will not return control to the
382  * caller, and internal state such as open files will not be cleaned up.
383  * This case occurs only during postmaster or standalone-backend startup,
384  * where an error will lead to immediate process exit anyway; so there is
385  * no point in contorting the code so it can clean up nicely.
386  */
387 static bool
388 ParseConfigFile(const char *config_file, const char *calling_file,
389                                 int depth, int elevel,
390                                 struct name_value_pair **head_p,
391                                 struct name_value_pair **tail_p)
392 {
393         bool            OK = true;
394         char            abs_path[MAXPGPATH];
395         FILE       *fp;
396         YY_BUFFER_STATE lex_buffer;
397         int                     token;
398
399         /*
400          * Reject too-deep include nesting depth.  This is just a safety check
401          * to avoid dumping core due to stack overflow if an include file loops
402          * back to itself.  The maximum nesting depth is pretty arbitrary.
403          */
404         if (depth > 10)
405         {
406                 ereport(elevel,
407                                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
408                                  errmsg("could not open configuration file \"%s\": maximum nesting depth exceeded",
409                                                 config_file)));
410                 return false;
411         }
412
413         /*
414          * If config_file is a relative path, convert to absolute.  We consider
415          * it to be relative to the directory holding the calling file.
416          */
417         if (!is_absolute_path(config_file))
418         {
419                 Assert(calling_file != NULL);
420                 strlcpy(abs_path, calling_file, sizeof(abs_path));
421                 get_parent_directory(abs_path);
422                 join_path_components(abs_path, abs_path, config_file);
423                 canonicalize_path(abs_path);
424                 config_file = abs_path;
425         }
426
427         fp = AllocateFile(config_file, "r");
428         if (!fp)
429         {
430                 ereport(elevel,
431                                 (errcode_for_file_access(),
432                                  errmsg("could not open configuration file \"%s\": %m",
433                                                 config_file)));
434                 return false;
435         }
436
437         /*
438          * Parse
439          */
440         lex_buffer = yy_create_buffer(fp, YY_BUF_SIZE);
441         yy_switch_to_buffer(lex_buffer);
442
443         ConfigFileLineno = 1;
444
445         /* This loop iterates once per logical line */
446         while ((token = yylex()))
447         {
448                 char       *opt_name, *opt_value;
449                 struct name_value_pair *item;
450
451                 if (token == GUC_EOL)   /* empty or comment line */
452                         continue;
453
454                 /* first token on line is option name */
455                 if (token != GUC_ID && token != GUC_QUALIFIED_ID)
456                         goto parse_error;
457                 opt_name = pstrdup(yytext);
458
459                 /* next we have an optional equal sign; discard if present */
460                 token = yylex();
461                 if (token == GUC_EQUALS)
462                         token = yylex();
463
464                 /* now we must have the option value */
465                 if (token != GUC_ID &&
466                         token != GUC_STRING &&
467                         token != GUC_INTEGER &&
468                         token != GUC_REAL &&
469                         token != GUC_UNQUOTED_STRING)
470                         goto parse_error;
471                 if (token == GUC_STRING)        /* strip quotes and escapes */
472                         opt_value = GUC_scanstr(yytext);
473                 else
474                         opt_value = pstrdup(yytext);
475
476                 /* now we'd like an end of line, or possibly EOF */
477                 token = yylex();
478                 if (token != GUC_EOL)
479                 {
480                         if (token != 0)
481                                 goto parse_error;
482                         /* treat EOF like \n for line numbering purposes, cf bug 4752 */
483                         ConfigFileLineno++;
484                 }
485
486                 /* OK, process the option name and value */
487                 if (guc_name_compare(opt_name, "include") == 0)
488                 {
489                         /*
490                          * An include directive isn't a variable and should be processed
491                          * immediately.
492                          */
493                         unsigned int save_ConfigFileLineno = ConfigFileLineno;
494
495                         if (!ParseConfigFile(opt_value, config_file,
496                                                                  depth + 1, elevel,
497                                                                  head_p, tail_p))
498                         {
499                                 pfree(opt_name);
500                                 pfree(opt_value);
501                                 OK = false;
502                                 goto cleanup_exit;
503                         }
504                         yy_switch_to_buffer(lex_buffer);
505                         ConfigFileLineno = save_ConfigFileLineno;
506                         pfree(opt_name);
507                         pfree(opt_value);
508                 }
509                 else if (guc_name_compare(opt_name, "custom_variable_classes") == 0)
510                 {
511                         /*
512                          * This variable must be processed first as it controls
513                          * the validity of other variables; so it goes at the head
514                          * of the result list.  If we already found a value for it,
515                          * replace with this one.
516                          */
517                         item = *head_p;
518                         if (item != NULL &&
519                                 guc_name_compare(item->name, "custom_variable_classes") == 0)
520                         {
521                                 /* replace existing head item */
522                                 pfree(item->name);
523                                 pfree(item->value);
524                                 item->name = opt_name;
525                                 item->value = opt_value;
526                                 item->filename = pstrdup(config_file);
527                                 item->sourceline = ConfigFileLineno-1;
528                         }
529                         else
530                         {
531                                 /* prepend to list */
532                                 item = palloc(sizeof *item);
533                                 item->name = opt_name;
534                                 item->value = opt_value;
535                                 item->filename = pstrdup(config_file);
536                                 item->sourceline = ConfigFileLineno-1;
537                                 item->next = *head_p;
538                                 *head_p = item;
539                                 if (*tail_p == NULL)
540                                         *tail_p = item;
541                         }
542                 }
543                 else
544                 {
545                         /* ordinary variable, append to list */
546                         item = palloc(sizeof *item);
547                         item->name = opt_name;
548                         item->value = opt_value;
549                         item->filename = pstrdup(config_file);
550                         item->sourceline = ConfigFileLineno-1;
551                         item->next = NULL;
552                         if (*head_p == NULL)
553                                 *head_p = item;
554                         else
555                                 (*tail_p)->next = item;
556                         *tail_p = item;
557                 }
558
559                 /* break out of loop if read EOF, else loop for next line */
560                 if (token == 0)
561                         break;
562         }
563
564         /* successful completion of parsing */
565         goto cleanup_exit;
566
567  parse_error:
568         if (token == GUC_EOL || token == 0)
569                 ereport(elevel,
570                                 (errcode(ERRCODE_SYNTAX_ERROR),
571                                  errmsg("syntax error in file \"%s\" line %u, near end of line",
572                                                 config_file, ConfigFileLineno - 1)));
573         else
574                 ereport(elevel,
575                                 (errcode(ERRCODE_SYNTAX_ERROR),
576                                  errmsg("syntax error in file \"%s\" line %u, near token \"%s\"",
577                                                 config_file, ConfigFileLineno, yytext)));
578         OK = false;
579
580 cleanup_exit:
581         yy_delete_buffer(lex_buffer);
582         FreeFile(fp);
583         return OK;
584 }
585
586
587 /*
588  * Free a list of name/value pairs, including the names and the values
589  */
590 static void
591 free_name_value_list(struct name_value_pair *list)
592 {
593         struct name_value_pair *item;
594
595         item = list;
596         while (item)
597         {
598                 struct name_value_pair *next = item->next;
599
600                 pfree(item->name);
601                 pfree(item->value);
602                 pfree(item->filename);
603                 pfree(item);
604                 item = next;
605         }
606 }
607
608
609 /*
610  *              scanstr
611  *
612  * Strip the quotes surrounding the given string, and collapse any embedded
613  * '' sequences and backslash escapes.
614  *
615  * the string returned is palloc'd and should eventually be pfree'd by the
616  * caller.
617  */
618 static char *
619 GUC_scanstr(const char *s)
620 {
621         char       *newStr;
622         int                     len,
623                                 i,
624                                 j;
625
626         Assert(s != NULL && s[0] == '\'');
627         len = strlen(s);
628         Assert(len >= 2);
629         Assert(s[len-1] == '\'');
630
631         /* Skip the leading quote; we'll handle the trailing quote below */
632         s++, len--;
633
634         /* Since len still includes trailing quote, this is enough space */
635         newStr = palloc(len);
636
637         for (i = 0, j = 0; i < len; i++)
638         {
639                 if (s[i] == '\\')
640                 {
641                         i++;
642                         switch (s[i])
643                         {
644                                 case 'b':
645                                         newStr[j] = '\b';
646                                         break;
647                                 case 'f':
648                                         newStr[j] = '\f';
649                                         break;
650                                 case 'n':
651                                         newStr[j] = '\n';
652                                         break;
653                                 case 'r':
654                                         newStr[j] = '\r';
655                                         break;
656                                 case 't':
657                                         newStr[j] = '\t';
658                                         break;
659                                 case '0':
660                                 case '1':
661                                 case '2':
662                                 case '3':
663                                 case '4':
664                                 case '5':
665                                 case '6':
666                                 case '7':
667                                         {
668                                                 int                     k;
669                                                 long            octVal = 0;
670
671                                                 for (k = 0;
672                                                          s[i + k] >= '0' && s[i + k] <= '7' && k < 3;
673                                                          k++)
674                                                         octVal = (octVal << 3) + (s[i + k] - '0');
675                                                 i += k - 1;
676                                                 newStr[j] = ((char) octVal);
677                                         }
678                                         break;
679                                 default:
680                                         newStr[j] = s[i];
681                                         break;
682                         }                                       /* switch */
683                 }
684                 else if (s[i] == '\'' && s[i+1] == '\'')
685                 {
686                         /* doubled quote becomes just one quote */
687                         newStr[j] = s[++i];
688                 }
689                 else
690                         newStr[j] = s[i];
691                 j++;
692         }
693
694         /* We copied the ending quote to newStr, so replace with \0 */
695         Assert(j > 0 && j <= len);
696         newStr[--j] = '\0';
697
698         return newStr;
699 }