11/*-------------------------------------------------------------------------
22 *
3- *pg_ctl --- start/stops/restarts the PostgreSQL server
3+ *status.c
44 *
55 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
66 *
7- *$PostgreSQL: pgsql/src/bin/pg_ctl/pg_ctl.c,v 1.111 2009/06/11 14:49:07 momjian Exp $
7+ *Monitor status of a PostgreSQL server.
88 *
99 *-------------------------------------------------------------------------
1010 */
@@ -24,36 +24,51 @@ typedef long pgpid_t;
2424static pgpid_t get_pgpid (void );
2525static bool postmaster_is_alive (pid_t pid );
2626
27- static char pid_file [MAXPGPATH ];
28-
29-
27+ /*
28+ * get_pgpid
29+ *
30+ * Get PID of postmaster, by scanning postmaster.pid.
31+ */
3032static pgpid_t
3133get_pgpid (void )
3234{
3335FILE * pidf ;
3436long pid ;
37+ char pid_file [MAXPGPATH ];
3538
3639snprintf (pid_file ,lengthof (pid_file ),"%s/postmaster.pid" ,pgdata );
40+
3741pidf = fopen (pid_file ,"r" );
3842if (pidf == NULL )
3943{
4044/* No pid file, not an error on startup */
4145if (errno == ENOENT )
4246return 0 ;
4347else
48+ {
4449elog (ERROR_SYSTEM ,_ ("could not open PID file \"%s\": %s\n" ),
45- pid_file ,strerror (errno ));
50+ pid_file ,strerror (errno ));
51+ }
52+ if (fscanf (pidf ,"%ld" ,& pid )!= 1 )
53+ {
54+ /* Is the file empty? */
55+ if (ftell (pidf )== 0 && feof (pidf ))
56+ elog (ERROR_SYSTEM ,_ ("the PID file \"%s\" is empty\n" ),
57+ pid_file );
58+ else
59+ elog (ERROR_SYSTEM ,_ ("invalid data in PID file \"%s\"\n" ),
60+ pid_file );
61+ }
4662}
47- if (fscanf (pidf ,"%ld" ,& pid )!= 1 )
48- elog (ERROR_PID_BROKEN ,_ ("invalid data in PID file \"%s\"\n" ),pid_file );
4963fclose (pidf );
5064return (pgpid_t )pid ;
5165}
5266
5367/*
54- *utility routines
68+ * postmaster_is_alive
69+ *
70+ * Check whether postmaster is alive or not.
5571 */
56-
5772static bool
5873postmaster_is_alive (pid_t pid )
5974{
@@ -79,27 +94,25 @@ postmaster_is_alive(pid_t pid)
7994}
8095
8196/*
82- * original is do_status() in src/bin/pg_ctl/pg_ctl.c
83- * changes are:
84- * renamed from do_status() from do_status().
85- * return true if PG server is running.
86- * don't print any message.
87- * don't print postopts file.
88- * log with elog() in pgut library.
97+ * is_pg_running
98+ *
99+ *
89100 */
90101bool
91102is_pg_running (void )
92103{
93104pgpid_t pid ;
94105
95106pid = get_pgpid ();
96- if (pid == 0 )/* 0 means no pid file */
107+
108+ /* 0 means no pid file */
109+ if (pid == 0 )
97110return false;
98111
99- if (pid < 0 )/* standalone backend */
112+ /* Case of a standalone backend */
113+ if (pid < 0 )
100114pid = - pid ;
101115
102-
116+ /* Check if postmaster is alive */
103117return postmaster_is_alive ((pid_t )pid );
104118}
105-