Skip to content

Commit ebe2166

Browse files
author
Michael Paquier
committed
Refactor code in pgsql_src for server monitoring
Most of those things are taken from pg_ctl.c, but were somewhat not really in a place corresponding to their role.
1 parent 5f3823c commit ebe2166

File tree

3 files changed

+35
-46
lines changed

3 files changed

+35
-46
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ SRCS = \
1010
pg_rman.c \
1111
restore.c \
1212
show.c \
13+
status.c \
1314
util.c \
1415
validate.c \
1516
xlog.c \
16-
pgsql_src/pg_ctl.c \
1717
pgut/pgut.c \
1818
pgut/pgut-port.c
1919
OBJS = $(SRCS:.c=.o)

pgsql_src/COPYRIGHT.pgsql_src

Lines changed: 0 additions & 24 deletions
This file was deleted.

pgsql_src/pg_ctl.c renamed to status.c

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
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;
2424
static pgpid_t get_pgpid(void);
2525
static 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+
*/
3032
static pgpid_t
3133
get_pgpid(void)
3234
{
3335
FILE *pidf;
3436
long pid;
37+
char pid_file[MAXPGPATH];
3538

3639
snprintf(pid_file, lengthof(pid_file), "%s/postmaster.pid", pgdata);
40+
3741
pidf = fopen(pid_file, "r");
3842
if (pidf == NULL)
3943
{
4044
/* No pid file, not an error on startup */
4145
if (errno == ENOENT)
4246
return 0;
4347
else
48+
{
4449
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+
}
4662
}
47-
if (fscanf(pidf, "%ld", &pid) != 1)
48-
elog(ERROR_PID_BROKEN, _("invalid data in PID file \"%s\"\n"), pid_file);
4963
fclose(pidf);
5064
return (pgpid_t) pid;
5165
}
5266

5367
/*
54-
* utility routines
68+
* postmaster_is_alive
69+
*
70+
* Check whether postmaster is alive or not.
5571
*/
56-
5772
static bool
5873
postmaster_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
*/
90101
bool
91102
is_pg_running(void)
92103
{
93104
pgpid_t pid;
94105

95106
pid = get_pgpid();
96-
if (pid == 0) /* 0 means no pid file */
107+
108+
/* 0 means no pid file */
109+
if (pid == 0)
97110
return false;
98111

99-
if (pid < 0) /* standalone backend */
112+
/* Case of a standalone backend */
113+
if (pid < 0)
100114
pid = -pid;
101115

102-
116+
/* Check if postmaster is alive */
103117
return postmaster_is_alive((pid_t) pid);
104118
}
105-

0 commit comments

Comments
 (0)