From 86947e666d39229558311d7b0be45608fd071ed8 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Sun, 11 Mar 2012 01:52:05 +0200 Subject: [PATCH] Add more detail to error message for invalid arguments for server process It now prints the argument that was at fault. Also fix a small misbehavior where the error message issued by getopt() would complain about a program named "--single", because that's what argv[0] is in the server process. --- src/backend/tcop/postgres.c | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c index 49a396918d..397c0734c2 100644 --- a/src/backend/tcop/postgres.c +++ b/src/backend/tcop/postgres.c @@ -3190,6 +3190,13 @@ process_postgres_switches(int argc, char *argv[], GucContext ctx) gucsource = PGC_S_CLIENT; /* switches came from client */ } +#ifdef HAVE_INT_OPTERR + /* Turn this off because it's either printed to stderr and not the log + * where we'd want it, or argv[0] is now "--single", which would make for a + * weird error message. We print our own error message below. */ + opterr = 0; +#endif + /* * Parse command-line options. CAUTION: keep this in sync with * postmaster/postmaster.c (the option sets should not conflict) and with @@ -3363,33 +3370,39 @@ process_postgres_switches(int argc, char *argv[], GucContext ctx) errs++; break; } + + if (errs) + break; } /* * Should be no more arguments except an optional database name, and * that's only in the secure case. */ - if (errs || argc - optind > 1 || (argc != optind && !secure)) + if (!errs && secure && argc - optind >= 1) + dbname = strdup(argv[optind++]); + else + dbname = NULL; + + if (errs || argc != optind) { + if (errs) + optind--; /* complain about the previous argument */ + /* spell the error message a bit differently depending on context */ if (IsUnderPostmaster) ereport(FATAL, (errcode(ERRCODE_SYNTAX_ERROR), - errmsg("invalid command-line arguments for server process"), + errmsg("invalid command-line argument for server process: %s", argv[optind]), errhint("Try \"%s --help\" for more information.", progname))); else ereport(FATAL, (errcode(ERRCODE_SYNTAX_ERROR), - errmsg("%s: invalid command-line arguments", - progname), + errmsg("%s: invalid command-line argument: %s", + progname, argv[optind]), errhint("Try \"%s --help\" for more information.", progname))); } - if (argc - optind == 1) - dbname = strdup(argv[optind]); - else - dbname = NULL; - /* * Reset getopt(3) library so that it will work correctly in subprocesses * or when this function is called a second time with another array.