@@ -81,6 +81,7 @@ static ShutdownMode shutdown_mode = SMART_MODE;
8181static int sig = SIGTERM ; /* default */
8282static CtlCommand ctl_command = NO_COMMAND ;
8383static char * pg_data = NULL ;
84+ static char * pg_config = NULL ;
8485static char * pgdata_opt = NULL ;
8586static char * post_opts = NULL ;
8687static const char * progname ;
@@ -131,6 +132,7 @@ static void do_status(void);
131132static void do_promote (void );
132133static void do_kill (pgpid_t pid );
133134static void print_msg (const char * msg );
135+ static void adjust_data_dir (void );
134136
135137#if defined(WIN32 ) || defined(__CYGWIN__ )
136138static bool pgwin32_IsInstalled (SC_HANDLE );
@@ -1265,10 +1267,10 @@ pgwin32_CommandLine(bool registration)
12651267 strcat (cmdLine , "\"" );
12661268 }
12671269
1268- if (pg_data )
1270+ if (pg_config )
12691271 {
12701272 strcat (cmdLine , " -D \"" );
1271- strcat (cmdLine , pg_data );
1273+ strcat (cmdLine , pg_config );
12721274 strcat (cmdLine , "\"" );
12731275 }
12741276
@@ -1886,6 +1888,59 @@ set_starttype(char *starttypeopt)
18861888}
18871889#endif
18881890
1891+ /*
1892+ * adjust_data_dir
1893+ *
1894+ * If a configuration-only directory was specified, find the real data dir.
1895+ */
1896+ void
1897+ adjust_data_dir (void )
1898+ {
1899+ char cmd [MAXPGPATH ], filename [MAXPGPATH ], * my_exec_path ;
1900+ FILE * fd ;
1901+
1902+ /* If there is no postgresql.conf, it can't be a config-only dir */
1903+ snprintf (filename , sizeof (filename ), "%s/postgresql.conf" , pg_config );
1904+ if ((fd = fopen (filename , "r" )) == NULL )
1905+ return ;
1906+ fclose (fd );
1907+
1908+ /* If PG_VERSION exists, it can't be a config-only dir */
1909+ snprintf (filename , sizeof (filename ), "%s/PG_VERSION" , pg_config );
1910+ if ((fd = fopen (filename , "r" )) != NULL )
1911+ {
1912+ fclose (fd );
1913+ return ;
1914+ }
1915+
1916+ /* Must be a configuration directory, so find the data directory */
1917+
1918+ /* we use a private my_exec_path to avoid interfering with later uses */
1919+ if (exec_path == NULL )
1920+ my_exec_path = find_other_exec_or_die (argv0 , "postgres" , PG_BACKEND_VERSIONSTR );
1921+ else
1922+ my_exec_path = xstrdup (exec_path );
1923+
1924+ snprintf (cmd , MAXPGPATH , SYSTEMQUOTE "\"%s\" %s%s -C data_directory" SYSTEMQUOTE ,
1925+ my_exec_path , pgdata_opt ? pgdata_opt : "" , post_opts ?
1926+ post_opts : "" );
1927+
1928+ fd = popen (cmd , "r" );
1929+ if (fd == NULL || fgets (filename , sizeof (filename ), fd ) == NULL )
1930+ {
1931+ write_stderr (_ ("%s: cannot find the data directory using %s\n" ), progname , my_exec_path );
1932+ exit (1 );
1933+ }
1934+ pclose (fd );
1935+ free (my_exec_path );
1936+
1937+ if (strlen (filename ) > 0 && filename [strlen (filename ) - 1 ] == '\n' )
1938+ filename [strlen (filename ) - 1 ] = '\0' ;
1939+ free (pg_data );
1940+ pg_data = xstrdup (filename );
1941+ canonicalize_path (pg_data );
1942+ }
1943+
18891944
18901945int
18911946main (int argc , char * * argv )
@@ -2120,14 +2175,17 @@ main(int argc, char **argv)
21202175 }
21212176
21222177 /* Note we put any -D switch into the env var above */
2123- pg_data = getenv ("PGDATA ");
2124- if (pg_data )
2178+ pg_config = getenv ("PGDATA ");
2179+ if (pg_config )
21252180 {
2126- pg_data = xstrdup (pg_data );
2127- canonicalize_path (pg_data );
2181+ pg_config = xstrdup (pg_config );
2182+ canonicalize_path (pg_config );
2183+ pg_data = xstrdup (pg_config );
21282184 }
21292185
2130- if (pg_data == NULL &&
2186+ adjust_data_dir ();
2187+
2188+ if (pg_config == NULL &&
21312189 ctl_command != KILL_COMMAND && ctl_command != UNREGISTER_COMMAND )
21322190 {
21332191 write_stderr (_ ("%s : no database directory specified and environment variable PGDATA unset \n "),
0 commit comments