@@ -392,49 +392,65 @@ void
392392get_sock_dir (ClusterInfo * cluster , bool live_check )
393393{
394394#ifdef HAVE_UNIX_SOCKETS
395- if (!live_check )
396- {
397- /* Use the current directory for the socket */
398- cluster -> sockdir = pg_malloc (MAXPGPATH );
399- if (!getcwd (cluster -> sockdir , MAXPGPATH ))
400- pg_log (PG_FATAL , "cannot find current directory\n" );
401- }
402- else
395+ /*
396+ * sockdir and port were added to postmaster.pid in PG 9.1.
397+ * Pre-9.1 cannot process pg_ctl -w for sockets in non-default
398+ * locations.
399+ */
400+ if (GET_MAJOR_VERSION (cluster -> major_version ) >= 901 )
403401 {
404- /*
405- * If we are doing a live check, we will use the old cluster's Unix
406- * domain socket directory so we can connect to the live server.
407- */
408-
409- /* sockdir was added to postmaster.pid in PG 9.1 */
410- if (GET_MAJOR_VERSION (cluster -> major_version ) >= 901 )
402+ if (!live_check )
411403 {
412- char filename [MAXPGPATH ];
404+ /* Use the current directory for the socket */
405+ cluster -> sockdir = pg_malloc (MAXPGPATH );
406+ if (!getcwd (cluster -> sockdir , MAXPGPATH ))
407+ pg_log (PG_FATAL , "cannot find current directory\n" );
408+ }
409+ else
410+ {
411+ /*
412+ * If we are doing a live check, we will use the old cluster's Unix
413+ * domain socket directory so we can connect to the live server.
414+ */
415+ unsigned short orig_port = cluster -> port ;
416+ char filename [MAXPGPATH ], line [MAXPGPATH ];
413417 FILE * fp ;
414- int i ;
415-
418+ int lineno ;
419+
416420 snprintf (filename , sizeof (filename ), "%s/postmaster.pid" ,
417421 cluster -> pgdata );
418422 if ((fp = fopen (filename , "r" )) == NULL )
419- pg_log (PG_FATAL , "Could not get socket directory of the old server\n" );
420-
421- cluster -> sockdir = pg_malloc (MAXPGPATH );
422- for (i = 0 ; i < LOCK_FILE_LINE_SOCKET_DIR ; i ++ )
423- if (fgets (cluster -> sockdir , MAXPGPATH , fp ) == NULL )
424- pg_log (PG_FATAL , "Could not get socket directory of the old server\n" );
425-
423+ pg_log (PG_FATAL , "Cannot open file %s: %m\n" , filename );
424+
425+ for (lineno = 1 ;
426+ lineno <= Max (LOCK_FILE_LINE_PORT , LOCK_FILE_LINE_SOCKET_DIR );
427+ lineno ++ )
428+ {
429+ if (fgets (line , sizeof (line ), fp ) == NULL )
430+ pg_log (PG_FATAL , "Cannot read line %d from %s: %m\n" , lineno , filename );
431+
432+ /* potentially overwrite user-supplied value */
433+ if (lineno == LOCK_FILE_LINE_PORT )
434+ sscanf (line , "%hu" , & old_cluster .port );
435+ if (lineno == LOCK_FILE_LINE_SOCKET_DIR )
436+ {
437+ cluster -> sockdir = pg_malloc (MAXPGPATH );
438+ /* strip off newline */
439+ sscanf (line , "%s\n" , cluster -> sockdir );
440+ }
441+ }
426442 fclose (fp );
427-
428- /* Remove trailing newline */
429- if (strchr (cluster -> sockdir , '\n' ) != NULL )
430- * strchr (cluster -> sockdir , '\n' ) = '\0' ;
431- }
432- else
433- {
434- /* Can't get live sockdir, so assume the default is OK. */
435- cluster -> sockdir = NULL ;
443+
444+ /* warn of port number correction */
445+ if (orig_port != DEF_PGUPORT && old_cluster .port != orig_port )
446+ pg_log (PG_WARNING , "User-supplied old port number %hu corrected to %hu\n" ,
447+ orig_port , cluster -> port );
436448 }
437449 }
450+ else
451+ /* Can't get sockdir and pg_ctl -w can't use a non-default, use default */
452+ cluster -> sockdir = NULL ;
453+
438454#else /* !HAVE_UNIX_SOCKETS */
439455 cluster -> sockdir = NULL ;
440456#endif
0 commit comments