You've already forked pg_probackup
							
							
				mirror of
				https://github.com/postgrespro/pg_probackup.git
				synced 2025-10-31 00:17:52 +02:00 
			
		
		
		
	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.
This commit is contained in:
		
							
								
								
									
										2
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								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) | ||||
|   | ||||
| @@ -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. | ||||
| @@ -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); | ||||
| } | ||||
| 
 | ||||
		Reference in New Issue
	
	Block a user