44 *
55 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
66 *
7- * $PostgreSQL: pgsql/src/bin/pg_ctl/pg_ctl.c,v 1.55 2005/03/11 17:20:33 momjian Exp $
7+ * $PostgreSQL: pgsql/src/bin/pg_ctl/pg_ctl.c,v 1.56 2005/04/20 23:10:16 tgl Exp $
88 *
99 *-------------------------------------------------------------------------
1010 */
@@ -236,7 +236,7 @@ static pgpid_t
236236get_pgpid (void )
237237{
238238 FILE * pidf ;
239- pgpid_t pid ;
239+ long pid ;
240240
241241 pidf = fopen (pid_file , "r" );
242242 if (pidf == NULL )
@@ -246,14 +246,19 @@ get_pgpid(void)
246246 return 0 ;
247247 else
248248 {
249- write_stderr (_ ("%s: could not open PID file \"%s\": %s" ),
249+ write_stderr (_ ("%s: could not open PID file \"%s\": %s\n " ),
250250 progname , pid_file , strerror (errno ));
251251 exit (1 );
252252 }
253253 }
254- fscanf (pidf , "%ld" , & pid );
254+ if (fscanf (pidf , "%ld" , & pid ) != 1 )
255+ {
256+ write_stderr (_ ("%s: invalid data in PID file \"%s\"\n" ),
257+ progname , pid_file );
258+ exit (1 );
259+ }
255260 fclose (pidf );
256- return pid ;
261+ return ( pgpid_t ) pid ;
257262}
258263
259264
@@ -766,34 +771,67 @@ do_reload(void)
766771 * utility routines
767772 */
768773
774+ static bool
775+ postmaster_is_alive (pid_t pid )
776+ {
777+ /*
778+ * Test to see if the process is still there. Note that we do not
779+ * consider an EPERM failure to mean that the process is still there;
780+ * EPERM must mean that the given PID belongs to some other userid,
781+ * and considering the permissions on $PGDATA, that means it's not
782+ * the postmaster we are after.
783+ *
784+ * Don't believe that our own PID or parent shell's PID is the postmaster,
785+ * either. (Windows hasn't got getppid(), though.)
786+ */
787+ if (pid == getpid ())
788+ return false;
789+ #ifndef WIN32
790+ if (pid == getppid ())
791+ return false;
792+ #endif
793+ if (kill (pid , 0 ) == 0 )
794+ return true;
795+ return false;
796+ }
797+
769798static void
770799do_status (void )
771800{
772801 pgpid_t pid ;
773802
774803 pid = get_pgpid ();
775- if (pid == 0 ) /* no pid file */
776- {
777- printf (_ ("%s: neither postmaster nor postgres running\n" ), progname );
778- exit (1 );
779- }
780- else if (pid < 0 ) /* standalone backend */
781- {
782- pid = - pid ;
783- printf (_ ("%s: a standalone backend \"postgres\" is running (PID: %ld)\n" ), progname , pid );
784- }
785- else
786- /* postmaster */
804+ if (pid != 0 ) /* 0 means no pid file */
787805 {
788- char * * optlines ;
806+ if (pid < 0 ) /* standalone backend */
807+ {
808+ pid = - pid ;
809+ if (postmaster_is_alive ((pid_t ) pid ))
810+ {
811+ printf (_ ("%s: a standalone backend \"postgres\" is running (PID: %ld)\n" ),
812+ progname , pid );
813+ return ;
814+ }
815+ }
816+ else /* postmaster */
817+ {
818+ if (postmaster_is_alive ((pid_t ) pid ))
819+ {
820+ char * * optlines ;
789821
790- printf (_ ("%s: postmaster is running (PID: %ld)\n" ), progname , pid );
822+ printf (_ ("%s: postmaster is running (PID: %ld)\n" ),
823+ progname , pid );
791824
792- optlines = readfile (postopts_file );
793- if (optlines != NULL )
794- for (; * optlines != NULL ; optlines ++ )
795- fputs (* optlines , stdout );
825+ optlines = readfile (postopts_file );
826+ if (optlines != NULL )
827+ for (; * optlines != NULL ; optlines ++ )
828+ fputs (* optlines , stdout );
829+ return ;
830+ }
831+ }
796832 }
833+ printf (_ ("%s: neither postmaster nor postgres running\n" ), progname );
834+ exit (1 );
797835}
798836
799837
0 commit comments