1
1
/*-------------------------------------------------------------------------
2
2
*
3
- * pg_ctl --- start/stops/restarts the PostgreSQL server
3
+ * status.c
4
4
*
5
5
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
6
6
*
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.
8
8
*
9
9
*-------------------------------------------------------------------------
10
10
*/
@@ -24,36 +24,51 @@ typedef long pgpid_t;
24
24
static pgpid_t get_pgpid (void );
25
25
static bool postmaster_is_alive (pid_t pid );
26
26
27
- static char pid_file [MAXPGPATH ];
28
-
29
-
27
+ /*
28
+ * get_pgpid
29
+ *
30
+ * Get PID of postmaster, by scanning postmaster.pid.
31
+ */
30
32
static pgpid_t
31
33
get_pgpid (void )
32
34
{
33
35
FILE * pidf ;
34
36
long pid ;
37
+ char pid_file [MAXPGPATH ];
35
38
36
39
snprintf (pid_file , lengthof (pid_file ), "%s/postmaster.pid" , pgdata );
40
+
37
41
pidf = fopen (pid_file , "r" );
38
42
if (pidf == NULL )
39
43
{
40
44
/* No pid file, not an error on startup */
41
45
if (errno == ENOENT )
42
46
return 0 ;
43
47
else
48
+ {
44
49
elog (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
+ }
46
62
}
47
- if (fscanf (pidf , "%ld" , & pid ) != 1 )
48
- elog (ERROR_PID_BROKEN , _ ("invalid data in PID file \"%s\"\n" ), pid_file );
49
63
fclose (pidf );
50
64
return (pgpid_t ) pid ;
51
65
}
52
66
53
67
/*
54
- * utility routines
68
+ * postmaster_is_alive
69
+ *
70
+ * Check whether postmaster is alive or not.
55
71
*/
56
-
57
72
static bool
58
73
postmaster_is_alive (pid_t pid )
59
74
{
@@ -79,27 +94,25 @@ postmaster_is_alive(pid_t pid)
79
94
}
80
95
81
96
/*
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
+ *
89
100
*/
90
101
bool
91
102
is_pg_running (void )
92
103
{
93
104
pgpid_t pid ;
94
105
95
106
pid = get_pgpid ();
96
- if (pid == 0 ) /* 0 means no pid file */
107
+
108
+ /* 0 means no pid file */
109
+ if (pid == 0 )
97
110
return false;
98
111
99
- if (pid < 0 ) /* standalone backend */
112
+ /* Case of a standalone backend */
113
+ if (pid < 0 )
100
114
pid = - pid ;
101
115
102
-
116
+ /* Check if postmaster is alive */
103
117
return postmaster_is_alive ((pid_t ) pid );
104
118
}
105
-
0 commit comments