11/*
2- * $PostgreSQL: pgsql/contrib/pg_standby/pg_standby.c,v 1.17 2009/01/06 17:27:06 tgl Exp $
2+ * $PostgreSQL: pgsql/contrib/pg_standby/pg_standby.c,v 1.18 2009/02/27 09:30:21 petere Exp $
33 *
44 *
55 * pg_standby.c
@@ -42,6 +42,8 @@ intgetopt(int argc, char *const argv[], const char *optstring);
4242extern char * optarg ;
4343extern int optind ;
4444
45+ const char * progname ;
46+
4547/* Options and defaults */
4648int sleeptime = 5 ;/* amount of time to sleep between file checks */
4749int waittime = -1 ;/* how long we have been waiting, -1 no wait
@@ -146,7 +148,7 @@ CustomizableInitialize(void)
146148 */
147149if (stat (archiveLocation ,& stat_buf )!= 0 )
148150{
149- fprintf (stderr ,"pg_standby : archiveLocation \"%s\" does not exist\n" ,archiveLocation );
151+ fprintf (stderr ,"%s : archiveLocation \"%s\" does not exist\n" , progname ,archiveLocation );
150152fflush (stderr );
151153exit (2 );
152154}
@@ -261,8 +263,8 @@ CustomizableCleanupPriorWALFiles(void)
261263rc = unlink (WALFilePath );
262264if (rc != 0 )
263265{
264- fprintf (stderr ,"\npg_standby : ERROR failed to remove \"%s\": %s" ,
265- WALFilePath ,strerror (errno ));
266+ fprintf (stderr ,"\n%s : ERROR failed to remove \"%s\": %s" ,
267+ progname , WALFilePath ,strerror (errno ));
266268break ;
267269}
268270}
@@ -271,7 +273,7 @@ CustomizableCleanupPriorWALFiles(void)
271273fprintf (stderr ,"\n" );
272274}
273275else
274- fprintf (stderr ,"pg_standby : archiveLocation \"%s\" open error\n" ,archiveLocation );
276+ fprintf (stderr ,"%s : archiveLocation \"%s\" open error\n" , progname ,archiveLocation );
275277
276278closedir (xldir );
277279fflush (stderr );
@@ -430,23 +432,29 @@ RestoreWALFileForRecovery(void)
430432static void
431433usage (void )
432434{
433- fprintf (stderr ,"\npg_standby allows Warm Standby servers to be configured\n" );
434- fprintf (stderr ,"Usage:\n" );
435- fprintf (stderr ," pg_standby [OPTION]... ARCHIVELOCATION NEXTWALFILE XLOGFILEPATH [RESTARTWALFILE]\n" );
436- fprintf (stderr ,"note space between ARCHIVELOCATION and NEXTWALFILE\n" );
437- fprintf (stderr ,"with main intended use as a restore_command in the recovery.conf\n" );
438- fprintf (stderr ," restore_command = 'pg_standby [OPTION]... ARCHIVELOCATION %%f %%p %%r'\n" );
439- fprintf (stderr ,"e.g. restore_command = 'pg_standby -l /mnt/server/archiverdir %%f %%p %%r'\n" );
440- fprintf (stderr ,"\nOptions:\n" );
441- fprintf (stderr ," -ccopies file from archive (default)\n" );
442- fprintf (stderr ," -dgenerate lots of debugging output (testing only)\n" );
443- fprintf (stderr ," -k NUMFILESTOKEEPif RESTARTWALFILE not used, removes files prior to limit (0 keeps all)\n" );
444- fprintf (stderr ," -llinks into archive (leaves file in archive)\n" );
445- fprintf (stderr ," -r MAXRETRIESmax number of times to retry, with progressive wait (default=3)\n" );
446- fprintf (stderr ," -s SLEEPTIMEseconds to wait between file checks (min=1, max=60, default=5)\n" );
447- fprintf (stderr ," -t TRIGGERFILEdefines a trigger file to initiate failover (no default)\n" );
448- fprintf (stderr ," -w MAXWAITTIMEmax seconds to wait for a file (0=no limit)(default=0)\n" );
449- fflush (stderr );
435+ printf ("%s allows PostgreSQL warm standby servers to be configured.\n\n" ,progname );
436+ printf ("Usage:\n" );
437+ printf (" %s [OPTION]... ARCHIVELOCATION NEXTWALFILE XLOGFILEPATH [RESTARTWALFILE]\n" ,progname );
438+ printf ("\n"
439+ "with main intended use as a restore_command in the recovery.conf:\n"
440+ " restore_command = 'pg_standby [OPTION]... ARCHIVELOCATION %%f %%p %%r'\n"
441+ "e.g.\n"
442+ " restore_command = 'pg_standby -l /mnt/server/archiverdir %%f %%p %%r'\n" );
443+ printf ("\nOptions:\n" );
444+ printf (" -c copies file from archive (default)\n" );
445+ printf (" -d generate lots of debugging output (testing only)\n" );
446+ printf (" -k NUMFILESTOKEEP if RESTARTWALFILE not used, removes files prior to limit\n"
447+ " (0 keeps all)\n" );
448+ printf (" -l links into archive (leaves file in archive)\n" );
449+ printf (" -r MAXRETRIES max number of times to retry, with progressive wait\n"
450+ " (default=3)\n" );
451+ printf (" -s SLEEPTIME seconds to wait between file checks (min=1, max=60,\n"
452+ " default=5)\n" );
453+ printf (" -t TRIGGERFILE defines a trigger file to initiate failover (no default)\n" );
454+ printf (" -w MAXWAITTIME max seconds to wait for a file (0=no limit) (default=0)\n" );
455+ printf (" --help show this help, then exit\n" );
456+ printf (" --version output version information, then exit\n" );
457+ printf ("\nReport bugs to <pgsql-bugs@postgresql.org>.\n" );
450458}
451459
452460static void
@@ -461,6 +469,22 @@ main(int argc, char **argv)
461469{
462470int c ;
463471
472+ progname = get_progname (argv [0 ]);
473+
474+ if (argc > 1 )
475+ {
476+ if (strcmp (argv [1 ],"--help" )== 0 || strcmp (argv [1 ],"-?" )== 0 )
477+ {
478+ usage ();
479+ exit (0 );
480+ }
481+ if (strcmp (argv [1 ],"--version" )== 0 || strcmp (argv [1 ],"-V" )== 0 )
482+ {
483+ puts ("pg_standby (PostgreSQL) " PG_VERSION );
484+ exit (0 );
485+ }
486+ }
487+
464488(void )signal (SIGINT ,sighandler );
465489(void )signal (SIGQUIT ,sighandler );
466490
@@ -478,8 +502,7 @@ main(int argc, char **argv)
478502keepfiles = atoi (optarg );
479503if (keepfiles < 0 )
480504{
481- fprintf (stderr ,"usage: pg_standby -k keepfiles must be >= 0\n" );
482- usage ();
505+ fprintf (stderr ,"%s: -k keepfiles must be >= 0\n" ,progname );
483506exit (2 );
484507}
485508break ;
@@ -490,17 +513,15 @@ main(int argc, char **argv)
490513maxretries = atoi (optarg );
491514if (maxretries < 0 )
492515{
493- fprintf (stderr ,"usage: pg_standby -r maxretries must be >= 0\n" );
494- usage ();
516+ fprintf (stderr ,"%s: -r maxretries must be >= 0\n" ,progname );
495517exit (2 );
496518}
497519break ;
498520case 's' :/* Sleep time */
499521sleeptime = atoi (optarg );
500522if (sleeptime <=0 || sleeptime > 60 )
501523{
502- fprintf (stderr ,"usage: pg_standby -s sleeptime incorrectly set\n" );
503- usage ();
524+ fprintf (stderr ,"%s: -s sleeptime incorrectly set\n" ,progname );
504525exit (2 );
505526}
506527break ;
@@ -513,13 +534,12 @@ main(int argc, char **argv)
513534maxwaittime = atoi (optarg );
514535if (maxwaittime < 0 )
515536{
516- fprintf (stderr ,"usage: pg_standby -w maxwaittime incorrectly set\n" );
517- usage ();
537+ fprintf (stderr ,"%s: -w maxwaittime incorrectly set\n" ,progname );
518538exit (2 );
519539}
520540break ;
521541default :
522- usage ( );
542+ fprintf ( stderr , "Try \"%s --help\" for more information.\n" , progname );
523543exit (2 );
524544break ;
525545}
@@ -530,7 +550,7 @@ main(int argc, char **argv)
530550 */
531551if (argc == 1 )
532552{
533- usage ( );
553+ fprintf ( stderr , "%s: not enough command-line arguments\n" , progname );
534554exit (2 );
535555}
536556
@@ -547,8 +567,8 @@ main(int argc, char **argv)
547567}
548568else
549569{
550- fprintf (stderr ,"pg_standby : must specifyarchiveLocation \n" );
551- usage ( );
570+ fprintf (stderr ,"%s : must specifyarchive location \n" , progname );
571+ fprintf ( stderr , "Try \"%s --help\" for more information.\n" , progname );
552572exit (2 );
553573}
554574
@@ -559,8 +579,8 @@ main(int argc, char **argv)
559579}
560580else
561581{
562- fprintf (stderr ,"pg_standby : use %%f to specify nextWALFileName\n" );
563- usage ( );
582+ fprintf (stderr ,"%s : use %%f to specify nextWALFileName\n" , progname );
583+ fprintf ( stderr , "Try \"%s --help\" for more information.\n" , progname );
564584exit (2 );
565585}
566586
@@ -571,8 +591,8 @@ main(int argc, char **argv)
571591}
572592else
573593{
574- fprintf (stderr ,"pg_standby : use %%p to specify xlogFilePath\n" );
575- usage ( );
594+ fprintf (stderr ,"%s : use %%p to specify xlogFilePath\n" , progname );
595+ fprintf ( stderr , "Try \"%s --help\" for more information.\n" , progname );
576596exit (2 );
577597}
578598