1111 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
1212 * Portions Copyright (c) 1994, Regents of the University of California
1313 *
14- * $PostgreSQL: pgsql/src/test/regress/pg_regress.c,v 1.53 2008/11/26 13:26:52 tgl Exp $
14+ * $PostgreSQL: pgsql/src/test/regress/pg_regress.c,v 1.54 2008/11/28 12:45:34 petere Exp $
1515 *
1616 *-------------------------------------------------------------------------
1717 */
@@ -83,10 +83,10 @@ static _stringlist *extra_tests = NULL;
8383static char * temp_install = NULL ;
8484static char * temp_config = NULL ;
8585static char * top_builddir = NULL ;
86- static int temp_port = 65432 ;
8786static bool nolocale = false;
8887static char * hostname = NULL ;
8988static int port = -1 ;
89+ static bool port_specified_by_user = false;
9090static char * dlpath = PKGLIBDIR ;
9191static char * user = NULL ;
9292static _stringlist * extraroles = NULL ;
@@ -1844,7 +1844,7 @@ help(void)
18441844printf (_ ("Options for \"temp-install\" mode:\n" ));
18451845printf (_ (" --no-locale use C locale\n" ));
18461846printf (_ (" --top-builddir=DIR (relative) path to top level build directory\n" ));
1847- printf (_ (" --temp- port=PORTport number to start temp postmaster on\n" ));
1847+ printf (_ (" --port=PORT start postmaster on PORT \n" ));
18481848printf (_ (" --temp-config=PATH append contents of PATH to temporary config\n" ));
18491849printf (_ ("\n" ));
18501850printf (_ ("Options for using an existing installation:\n" ));
@@ -1867,6 +1867,7 @@ regression_main(int argc, char *argv[], init_function ifunc, test_function tfunc
18671867int i ;
18681868int option_index ;
18691869char buf [MAXPGPATH * 4 ];
1870+ char buf2 [MAXPGPATH * 4 ];
18701871
18711872static struct option long_options []= {
18721873{"help" ,no_argument ,NULL ,'h' },
@@ -1882,7 +1883,6 @@ regression_main(int argc, char *argv[], init_function ifunc, test_function tfunc
18821883{"temp-install" ,required_argument ,NULL ,9 },
18831884{"no-locale" ,no_argument ,NULL ,10 },
18841885{"top-builddir" ,required_argument ,NULL ,11 },
1885- {"temp-port" ,required_argument ,NULL ,12 },
18861886{"host" ,required_argument ,NULL ,13 },
18871887{"port" ,required_argument ,NULL ,14 },
18881888{"user" ,required_argument ,NULL ,15 },
@@ -1956,20 +1956,12 @@ regression_main(int argc, char *argv[], init_function ifunc, test_function tfunc
19561956case 11 :
19571957top_builddir = strdup (optarg );
19581958break ;
1959- case 12 :
1960- {
1961- int p = atoi (optarg );
1962-
1963- /* Since Makefile isn't very bright, check port range */
1964- if (p >=1024 && p <=65535 )
1965- temp_port = p ;
1966- }
1967- break ;
19681959case 13 :
19691960hostname = strdup (optarg );
19701961break ;
19711962case 14 :
19721963port = atoi (optarg );
1964+ port_specified_by_user = true;
19731965break ;
19741966case 15 :
19751967user = strdup (optarg );
@@ -2005,8 +1997,13 @@ regression_main(int argc, char *argv[], init_function ifunc, test_function tfunc
20051997optind ++ ;
20061998}
20071999
2008- if (temp_install )
2009- port = temp_port ;
2000+ if (temp_install && !port_specified_by_user )
2001+ /*
2002+ * To reduce chances of interference with parallel
2003+ * installations, use a port number starting in the private
2004+ * range (49152-65535) calculated from the version number.
2005+ */
2006+ port = 0xC000 | (PG_VERSION_NUM & 0x3FFF );
20102007
20112008inputdir = make_absolute_path (inputdir );
20122009outputdir = make_absolute_path (outputdir );
@@ -2106,6 +2103,37 @@ regression_main(int argc, char *argv[], init_function ifunc, test_function tfunc
21062103fclose (pg_conf );
21072104}
21082105
2106+ /*
2107+ * Check if there is a postmaster running already.
2108+ */
2109+ snprintf (buf2 ,sizeof (buf2 ),
2110+ SYSTEMQUOTE "\"%s/psql\" -X postgres <%s 2>%s" SYSTEMQUOTE ,
2111+ bindir ,DEVNULL ,DEVNULL );
2112+
2113+ for (i = 0 ;i < 16 ;i ++ )
2114+ {
2115+ if (system (buf2 )== 0 )
2116+ {
2117+ char s [16 ];
2118+
2119+ if (port_specified_by_user || i == 15 )
2120+ {
2121+ fprintf (stderr ,_ ("port %d apparently in use\n" ),port );
2122+ if (!port_specified_by_user )
2123+ fprintf (stderr ,_ ("%s: could not determine an available port\n" ),progname );
2124+ fprintf (stderr ,_ ("Specify an unused port using the --port option or shut down any conflicting PostgreSQL servers.\n" ));
2125+ exit_nicely (2 );
2126+ }
2127+
2128+ fprintf (stderr ,_ ("port %d apparently in use, trying %d\n" ),port ,port + 1 );
2129+ port ++ ;
2130+ sprintf (s ,"%d" ,port );
2131+ doputenv ("PGPORT" ,s );
2132+ }
2133+ else
2134+ break ;
2135+ }
2136+
21092137/*
21102138 * Start the temp postmaster
21112139 */
@@ -2129,13 +2157,10 @@ regression_main(int argc, char *argv[], init_function ifunc, test_function tfunc
21292157 * second or so, but Cygwin is reportedly *much* slower). Don't wait
21302158 * forever, however.
21312159 */
2132- snprintf (buf ,sizeof (buf ),
2133- SYSTEMQUOTE "\"%s/psql\" -X postgres <%s 2>%s" SYSTEMQUOTE ,
2134- bindir ,DEVNULL ,DEVNULL );
21352160for (i = 0 ;i < 60 ;i ++ )
21362161{
21372162/* Done if psql succeeds */
2138- if (system (buf )== 0 )
2163+ if (system (buf2 )== 0 )
21392164break ;
21402165
21412166/*
@@ -2180,7 +2205,7 @@ regression_main(int argc, char *argv[], init_function ifunc, test_function tfunc
21802205postmaster_running = true;
21812206
21822207printf (_ ("running on port %d with pid %lu\n" ),
2183- temp_port , (unsigned long )postmaster_pid );
2208+ port , (unsigned long )postmaster_pid );
21842209}
21852210else
21862211{