From ebe21663797de5376a569cc7b00ec31a89400638 Mon Sep 17 00:00:00 2001 From: Michael Paquier Date: Thu, 12 Dec 2013 23:54:52 +0900 Subject: [PATCH] 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. --- Makefile | 2 +- pgsql_src/COPYRIGHT.pgsql_src | 24 --------------- pgsql_src/pg_ctl.c => status.c | 55 +++++++++++++++++++++------------- 3 files changed, 35 insertions(+), 46 deletions(-) delete mode 100644 pgsql_src/COPYRIGHT.pgsql_src rename pgsql_src/pg_ctl.c => status.c (70%) diff --git a/Makefile b/Makefile index 0f53c969..908ba0e3 100644 --- a/Makefile +++ b/Makefile @@ -10,10 +10,10 @@ SRCS = \ pg_rman.c \ restore.c \ show.c \ + status.c \ util.c \ validate.c \ xlog.c \ - pgsql_src/pg_ctl.c \ pgut/pgut.c \ pgut/pgut-port.c OBJS = $(SRCS:.c=.o) diff --git a/pgsql_src/COPYRIGHT.pgsql_src b/pgsql_src/COPYRIGHT.pgsql_src deleted file mode 100644 index eee220e0..00000000 --- a/pgsql_src/COPYRIGHT.pgsql_src +++ /dev/null @@ -1,24 +0,0 @@ -Files in this directory are parts of PostgreSQL Database Management System. -Copyright holders of those files are following organizations: - - -Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group - -Portions Copyright (c) 1994, The Regents of the University of California - -Permission to use, copy, modify, and distribute this software and its -documentation for any purpose, without fee, and without a written agreement -is hereby granted, provided that the above copyright notice and this -paragraph and the following two paragraphs appear in all copies. - -IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR -DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING -LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS -DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE -POSSIBILITY OF SUCH DAMAGE. - -THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, -INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY -AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS -ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO -PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. diff --git a/pgsql_src/pg_ctl.c b/status.c similarity index 70% rename from pgsql_src/pg_ctl.c rename to status.c index 90ed71f8..fa5f7f69 100644 --- a/pgsql_src/pg_ctl.c +++ b/status.c @@ -1,10 +1,10 @@ /*------------------------------------------------------------------------- * - * pg_ctl --- start/stops/restarts the PostgreSQL server + * status.c * * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group * - * $PostgreSQL: pgsql/src/bin/pg_ctl/pg_ctl.c,v 1.111 2009/06/11 14:49:07 momjian Exp $ + * Monitor status of a PostgreSQL server. * *------------------------------------------------------------------------- */ @@ -24,16 +24,20 @@ typedef long pgpid_t; static pgpid_t get_pgpid(void); static bool postmaster_is_alive(pid_t pid); -static char pid_file[MAXPGPATH]; - - +/* + * get_pgpid + * + * Get PID of postmaster, by scanning postmaster.pid. + */ static pgpid_t get_pgpid(void) { FILE *pidf; long pid; + char pid_file[MAXPGPATH]; snprintf(pid_file, lengthof(pid_file), "%s/postmaster.pid", pgdata); + pidf = fopen(pid_file, "r"); if (pidf == NULL) { @@ -41,19 +45,30 @@ get_pgpid(void) if (errno == ENOENT) return 0; else + { elog(ERROR_SYSTEM, _("could not open PID file \"%s\": %s\n"), - pid_file, strerror(errno)); + pid_file, strerror(errno)); + } + if (fscanf(pidf, "%ld", &pid) != 1) + { + /* Is the file empty? */ + if (ftell(pidf) == 0 && feof(pidf)) + elog(ERROR_SYSTEM, _("the PID file \"%s\" is empty\n"), + pid_file); + else + elog(ERROR_SYSTEM, _("invalid data in PID file \"%s\"\n"), + pid_file); + } } - if (fscanf(pidf, "%ld", &pid) != 1) - elog(ERROR_PID_BROKEN, _("invalid data in PID file \"%s\"\n"), pid_file); fclose(pidf); return (pgpid_t) pid; } /* - * utility routines + * postmaster_is_alive + * + * Check whether postmaster is alive or not. */ - static bool postmaster_is_alive(pid_t pid) { @@ -79,13 +94,9 @@ postmaster_is_alive(pid_t pid) } /* - * original is do_status() in src/bin/pg_ctl/pg_ctl.c - * changes are: - * renamed from do_status() from do_status(). - * return true if PG server is running. - * don't print any message. - * don't print postopts file. - * log with elog() in pgut library. + * is_pg_running + * + * */ bool is_pg_running(void) @@ -93,13 +104,15 @@ is_pg_running(void) pgpid_t pid; pid = get_pgpid(); - if (pid == 0) /* 0 means no pid file */ + + /* 0 means no pid file */ + if (pid == 0) return false; - if (pid < 0) /* standalone backend */ + /* Case of a standalone backend */ + if (pid < 0) pid = -pid; - + /* Check if postmaster is alive */ return postmaster_is_alive((pid_t) pid); } -