Fix oversight in previous error-reporting patch; mustn't pfree path string
[PostgreSQL.git] / src / backend / utils / misc / guc-file.l
blob1e3e972e8ab701418e71535dd62affcc4e04e38d
1 /* -*-pgsql-c-*- */
2 /*
3  * Scanner for the configuration file
4  *
5  * Copyright (c) 2000-2008, PostgreSQL Global Development Group
6  *
7  * $PostgreSQL$
8  */
12 #include "postgres.h"
14 #include <ctype.h>
15 #include <unistd.h>
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) */
23 #undef fprintf
24 #define fprintf(file, fmt, msg)  ereport(ERROR, (errmsg_internal("%s", msg)))
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
38 struct name_value_pair
40         char       *name;
41         char       *value;
42         char       *filename;
43         int                     sourceline;
44         struct name_value_pair *next;
47 static unsigned int ConfigFileLineno;
49 /* flex fails to supply a prototype for yylex, so provide one */
50 int GUC_yylex(void);
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);
61 %option 8bit
62 %option never-interactive
63 %option nodefault
64 %option noinput
65 %option nounput
66 %option noyywrap
67 %option prefix="GUC_yy"
70 SIGN            ("-"|"+")
71 DIGIT           [0-9]
72 HEXDIGIT        [0-9a-fA-F]
74 UNIT_LETTER     [a-zA-Z]
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) */
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;
104 .               return GUC_ERROR;
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)
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;
127         Assert(context == PGC_POSTMASTER || context == PGC_SIGHUP);
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;
140         /* Parse the file into a list of option names and values */
141         head = tail = NULL;
143         if (!ParseConfigFile(ConfigFileName, NULL,
144                                                  0, context, elevel,
145                                                  &head, &tail))
146                 goto cleanup_list;
148         /*
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.)
155          */
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)
159         {
160                 cvc = guc_strdup(elevel, cvc_struct->reset_val);
161                 if (cvc == NULL)
162                         goto cleanup_list;
163         }
164         else if (head != NULL &&
165                          guc_name_compare(head->name, "custom_variable_classes") == 0)
166         {
167                 /*
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.
170                  */
171                 cvc = (char *) assign_custom_variable_classes(head->value,
172                                                                                                           false, PGC_S_FILE);
173                 if (cvc == NULL)
174                 {
175                         ereport(elevel,
176                                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
177                                          errmsg("invalid value for parameter \"%s\": \"%s\"",
178                                                         head->name, head->value)));
179                         goto cleanup_list;
180                 }
181         }
183         /*
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.
187          */
188         for (i = 0; i < num_guc_variables; i++)
189         {
190                 struct config_generic *gconf = guc_variables[i];
192                 gconf->status &= ~GUC_IS_IN_FILE;
193         }
195         /*
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.
198          */
199         for (item = head; item; item = item->next)
200         {
201                 char *sep = strchr(item->name, GUC_QUALIFIER_SEPARATOR);
203                 if (sep)
204                 {
205                         /*
206                          * We have to consider three cases for custom variables:
207                          *
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.
211                          */
212                         if (!is_custom_class(item->name, sep - item->name, cvc))
213                         {
214                                 ereport(elevel,
215                                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
216                                                  errmsg("unrecognized configuration parameter \"%s\"",
217                                                                 item->name)));
218                                 goto cleanup_list;
219                         }
220                         /*
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).
225                          */
226                         if (find_option(item->name, false, elevel) == NULL)
227                                 continue;
228                         /*
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
232                          * entry.
233                          */
234                 }
236                 if (!set_config_option(item->name, item->value, context,
237                                                            PGC_S_FILE, GUC_ACTION_SET, false))
238                         goto cleanup_list;
239         }
241         /*
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
249          * changes.)
250          */
251         for (i = 0; i < num_guc_variables; i++)
252         {
253                 struct config_generic *gconf = guc_variables[i];
254                 GucStack   *stack;
256                 if (gconf->reset_source != PGC_S_FILE ||
257                         (gconf->status & GUC_IS_IN_FILE))
258                         continue;
259                 if (gconf->context < PGC_SIGHUP)
260                 {
261                         ereport(elevel,
262                                         (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
263                                          errmsg("attempted change of parameter \"%s\" ignored",
264                                                         gconf->name),
265                                          errdetail("This parameter cannot be changed after server start.")));
266                         continue;
267                 }
269                 /*
270                  * Reset any "file" sources to "default", else set_config_option
271                  * will not override those settings.
272                  */
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)
278                 {
279                         if (stack->source == PGC_S_FILE)
280                                 stack->source = PGC_S_DEFAULT;
281                 }
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);
286         }
288         /*
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.
293          *
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.
297          */
298         envvar = getenv("PGDATESTYLE");
299         if (envvar != NULL)
300                 set_config_option("datestyle", envvar, PGC_POSTMASTER,
301                                                   PGC_S_ENV_VAR, GUC_ACTION_SET, true);
303         envvar = getenv("PGCLIENTENCODING");
304         if (envvar != NULL)
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)
311         {
312                 if (set_config_option(item->name, item->value, context,
313                                                                  PGC_S_FILE, GUC_ACTION_SET, true))
314                 {
315                         set_config_sourcefile(item->name, item->filename,
316                                                                   item->sourceline);
317                 }
318         }
320         /* Remember when we last successfully loaded the config file. */
321         PgReloadTime = GetCurrentTimestamp();
323  cleanup_list:
324         free_name_value_list(head);
325         if (cvc)
326                 free(cvc);
331  * Read and parse a single configuration file.  This function recurses
332  * to handle "include" directives.
334  * Input parameters:
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()
341  * Output parameters:
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.
357  */
358 static bool
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)
364         bool            OK = true;
365         char            abs_path[MAXPGPATH];
366         FILE       *fp;
367         YY_BUFFER_STATE lex_buffer;
368         int                     token;
370         /*
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.
374          */
375         if (depth > 10)
376         {
377                 ereport(elevel,
378                                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
379                                  errmsg("could not open configuration file \"%s\": maximum nesting depth exceeded",
380                                                 config_file)));
381                 return false;
382         }
384         /*
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.
387          */
388         if (!is_absolute_path(config_file))
389         {
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;
396         }
398         fp = AllocateFile(config_file, "r");
399         if (!fp)
400         {
401                 ereport(elevel,
402                                 (errcode_for_file_access(),
403                                  errmsg("could not open configuration file \"%s\": %m",
404                                                 config_file)));
405                 return false;
406         }
408         /*
409          * Parse
410          */
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()))
418         {
419                 char       *opt_name, *opt_value;
420                 struct name_value_pair *item;
422                 if (token == GUC_EOL)   /* empty or comment line */
423                         continue;
425                 /* first token on line is option name */
426                 if (token != GUC_ID && token != GUC_QUALIFIED_ID)
427                         goto parse_error;
428                 opt_name = pstrdup(yytext);
430                 /* next we have an optional equal sign; discard if present */
431                 token = yylex();
432                 if (token == GUC_EQUALS)
433                         token = yylex();
435                 /* now we must have the option value */
436                 if (token != GUC_ID &&
437                         token != GUC_STRING && 
438                         token != GUC_INTEGER && 
439                         token != GUC_REAL && 
440                         token != GUC_UNQUOTED_STRING)
441                         goto parse_error;
442                 if (token == GUC_STRING)        /* strip quotes and escapes */
443                         opt_value = GUC_scanstr(yytext);
444                 else
445                         opt_value = pstrdup(yytext);
447                 /* now we'd like an end of line, or possibly EOF */
448                 token = yylex();
449                 if (token != GUC_EOL && token != 0)
450                         goto parse_error;
452                 /* OK, process the option name and value */
453                 if (guc_name_compare(opt_name, "include") == 0)
454                 {
455                         /*
456                          * An include directive isn't a variable and should be processed
457                          * immediately.
458                          */
459                         unsigned int save_ConfigFileLineno = ConfigFileLineno;
461                         if (!ParseConfigFile(opt_value, config_file,
462                                                                  depth + 1, context, elevel,
463                                                                  head_p, tail_p))
464                         {
465                                 pfree(opt_name);
466                                 pfree(opt_value);
467                                 OK = false;
468                                 goto cleanup_exit;
469                         }
470                         yy_switch_to_buffer(lex_buffer);
471                         ConfigFileLineno = save_ConfigFileLineno;
472                         pfree(opt_name);
473                         pfree(opt_value);
474                 }
475                 else if (guc_name_compare(opt_name, "custom_variable_classes") == 0)
476                 {
477                         /*
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.
482                          */
483                         item = *head_p;
484                         if (item != NULL &&
485                                 guc_name_compare(item->name, "custom_variable_classes") == 0)
486                         {
487                                 /* replace existing head item */
488                                 pfree(item->name);
489                                 pfree(item->value);
490                                 item->name = opt_name;
491                                 item->value = opt_value;
492                                 item->filename = pstrdup(config_file);
493                                 item->sourceline = ConfigFileLineno-1;
494                         }
495                         else
496                         {
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;
504                                 *head_p = item;
505                                 if (*tail_p == NULL)
506                                         *tail_p = item;
507                         }
508                 }
509                 else
510                 {
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;
517                         item->next = NULL;
518                         if (*head_p == NULL)
519                                 *head_p = item;
520                         else
521                                 (*tail_p)->next = item;
522                         *tail_p = item;
523                 }
525                 /* break out of loop if read EOF, else loop for next line */
526                 if (token == 0)
527                         break;
528         }
530         /* successful completion of parsing */
531         goto cleanup_exit;
533  parse_error:
534         if (token == GUC_EOL || token == 0)
535                 ereport(elevel,
536                                 (errcode(ERRCODE_SYNTAX_ERROR),
537                                  errmsg("syntax error in file \"%s\" line %u, near end of line",
538                                                 config_file, ConfigFileLineno - 1)));
539         else
540                 ereport(elevel,
541                                 (errcode(ERRCODE_SYNTAX_ERROR),
542                                  errmsg("syntax error in file \"%s\" line %u, near token \"%s\"", 
543                                                 config_file, ConfigFileLineno, yytext)));
544         OK = false;
546 cleanup_exit:
547         yy_delete_buffer(lex_buffer);
548         FreeFile(fp);
549         return OK;
554  * Free a list of name/value pairs, including the names and the values
555  */
556 static void
557 free_name_value_list(struct name_value_pair *list)
559         struct name_value_pair *item;
561         item = list;
562         while (item)
563         {
564                 struct name_value_pair *next = item->next;
566                 pfree(item->name);
567                 pfree(item->value);
568                 pfree(item->filename);
569                 pfree(item);
570                 item = next;
571         }
576  *              scanstr
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
582  * caller.
583  */
584 static char *
585 GUC_scanstr(const char *s)
587         char       *newStr;
588         int                     len,
589                                 i,
590                                 j;
592         Assert(s != NULL && s[0] == '\'');
593         len = strlen(s);
594         Assert(len >= 2);
595         Assert(s[len-1] == '\'');
597         /* Skip the leading quote; we'll handle the trailing quote below */
598         s++, len--;
600         /* Since len still includes trailing quote, this is enough space */
601         newStr = palloc(len);
603         for (i = 0, j = 0; i < len; i++)
604         {
605                 if (s[i] == '\\')
606                 {
607                         i++;
608                         switch (s[i])
609                         {
610                                 case 'b':
611                                         newStr[j] = '\b';
612                                         break;
613                                 case 'f':
614                                         newStr[j] = '\f';
615                                         break;
616                                 case 'n':
617                                         newStr[j] = '\n';
618                                         break;
619                                 case 'r':
620                                         newStr[j] = '\r';
621                                         break;
622                                 case 't':
623                                         newStr[j] = '\t';
624                                         break;
625                                 case '0':
626                                 case '1':
627                                 case '2':
628                                 case '3':
629                                 case '4':
630                                 case '5':
631                                 case '6':
632                                 case '7':
633                                         {
634                                                 int                     k;
635                                                 long            octVal = 0;
637                                                 for (k = 0;
638                                                          s[i + k] >= '0' && s[i + k] <= '7' && k < 3;
639                                                          k++)
640                                                         octVal = (octVal << 3) + (s[i + k] - '0');
641                                                 i += k - 1;
642                                                 newStr[j] = ((char) octVal);
643                                         }
644                                         break;
645                                 default:
646                                         newStr[j] = s[i];
647                                         break;
648                         }                                       /* switch */
649                 }
650                 else if (s[i] == '\'' && s[i+1] == '\'')
651                 {
652                         /* doubled quote becomes just one quote */
653                         newStr[j] = s[++i];
654                 }
655                 else
656                         newStr[j] = s[i];
657                 j++;
658         }
660         /* We copied the ending quote to newStr, so replace with \0 */
661         Assert(j > 0 && j <= len);
662         newStr[--j] = '\0';
664         return newStr;