On Fri, Mar  7, 2014 at 09:07:24AM +0530, Amit Kapila wrote:
> One reason could be that as we are already returning special exit code
> for 'status' option of pg_ctl (exit(3)), this seems to be inline with it as 
> this
> also get called during status command.
> 
> Also in the link sent by you in another mail, I could see some statement
> which indicates it is more important to return correct codes in case of
> status which sounds a good reason to me.
> 
> Link and statement
> http://www.postgresql.org/message-id/51d1e482.5090...@gmx.net
> 
> "The "status" case is different, because there the exit code
> can be passed out by the init script directly."
> 
> If we just want to handle correct exit codes for "status" option, then
> may be we need to refactor the code a bit to ensure the same.

OK, done with the attached patch  Three is returned for status, but one
for other cases.

-- 
  Bruce Momjian  <br...@momjian.us>        http://momjian.us
  EnterpriseDB                             http://enterprisedb.com

  + Everyone has their own god. +
diff --git a/src/bin/pg_ctl/pg_ctl.c b/src/bin/pg_ctl/pg_ctl.c
new file mode 100644
index 0dbaa6e..8c9970d
*** a/src/bin/pg_ctl/pg_ctl.c
--- b/src/bin/pg_ctl/pg_ctl.c
*************** static bool allow_core_files = false;
*** 97,102 ****
--- 97,103 ----
  static time_t start_time;
  
  static char postopts_file[MAXPGPATH];
+ static char version_file[MAXPGPATH];
  static char pid_file[MAXPGPATH];
  static char backup_file[MAXPGPATH];
  static char recovery_file[MAXPGPATH];
*************** static void pgwin32_doRunAsService(void)
*** 152,158 ****
  static int	CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION *processInfo, bool as_service);
  #endif
  
! static pgpid_t get_pgpid(void);
  static char **readfile(const char *path);
  static void free_readfile(char **optlines);
  static int	start_postmaster(void);
--- 153,159 ----
  static int	CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION *processInfo, bool as_service);
  #endif
  
! static pgpid_t get_pgpid(bool is_status_request);
  static char **readfile(const char *path);
  static void free_readfile(char **optlines);
  static int	start_postmaster(void);
*************** print_msg(const char *msg)
*** 246,255 ****
  }
  
  static pgpid_t
! get_pgpid(void)
  {
  	FILE	   *pidf;
  	long		pid;
  
  	pidf = fopen(pid_file, "r");
  	if (pidf == NULL)
--- 247,275 ----
  }
  
  static pgpid_t
! get_pgpid(bool is_status_request)
  {
  	FILE	   *pidf;
  	long		pid;
+ 	struct stat statbuf;
+ 
+ 	if (stat(pg_data, &statbuf) != 0)
+ 	{
+ 		if (errno == ENOENT)
+ 			printf(_("%s: directory \"%s\" does not exist\n"), progname,
+ 					 pg_data);
+ 		else
+ 			printf(_("%s: cannot access directory \"%s\"\n"), progname,
+ 					 pg_data);
+ 		exit(is_status_request ? 3 : 1);
+ 	}
+ 
+ 	if (stat(version_file, &statbuf) != 0 && errno == ENOENT)
+ 	{
+ 		printf(_("%s: directory \"%s\" is not a database cluster directory\n"),
+ 				 progname, pg_data);
+ 		exit(is_status_request ? 3 : 1);
+ 	}
  
  	pidf = fopen(pid_file, "r");
  	if (pidf == NULL)
*************** do_start(void)
*** 810,816 ****
  
  	if (ctl_command != RESTART_COMMAND)
  	{
! 		old_pid = get_pgpid();
  		if (old_pid != 0)
  			write_stderr(_("%s: another server might be running; "
  						   "trying to start server anyway\n"),
--- 830,836 ----
  
  	if (ctl_command != RESTART_COMMAND)
  	{
! 		old_pid = get_pgpid(false);
  		if (old_pid != 0)
  			write_stderr(_("%s: another server might be running; "
  						   "trying to start server anyway\n"),
*************** do_stop(void)
*** 894,900 ****
  	pgpid_t		pid;
  	struct stat statbuf;
  
! 	pid = get_pgpid();
  
  	if (pid == 0)				/* no pid file */
  	{
--- 914,920 ----
  	pgpid_t		pid;
  	struct stat statbuf;
  
! 	pid = get_pgpid(false);
  
  	if (pid == 0)				/* no pid file */
  	{
*************** do_stop(void)
*** 943,949 ****
  
  		for (cnt = 0; cnt < wait_seconds; cnt++)
  		{
! 			if ((pid = get_pgpid()) != 0)
  			{
  				print_msg(".");
  				pg_usleep(1000000);		/* 1 sec */
--- 963,969 ----
  
  		for (cnt = 0; cnt < wait_seconds; cnt++)
  		{
! 			if ((pid = get_pgpid(false)) != 0)
  			{
  				print_msg(".");
  				pg_usleep(1000000);		/* 1 sec */
*************** do_restart(void)
*** 980,986 ****
  	pgpid_t		pid;
  	struct stat statbuf;
  
! 	pid = get_pgpid();
  
  	if (pid == 0)				/* no pid file */
  	{
--- 1000,1006 ----
  	pgpid_t		pid;
  	struct stat statbuf;
  
! 	pid = get_pgpid(false);
  
  	if (pid == 0)				/* no pid file */
  	{
*************** do_restart(void)
*** 1033,1039 ****
  
  		for (cnt = 0; cnt < wait_seconds; cnt++)
  		{
! 			if ((pid = get_pgpid()) != 0)
  			{
  				print_msg(".");
  				pg_usleep(1000000);		/* 1 sec */
--- 1053,1059 ----
  
  		for (cnt = 0; cnt < wait_seconds; cnt++)
  		{
! 			if ((pid = get_pgpid(false)) != 0)
  			{
  				print_msg(".");
  				pg_usleep(1000000);		/* 1 sec */
*************** do_reload(void)
*** 1071,1077 ****
  {
  	pgpid_t		pid;
  
! 	pid = get_pgpid();
  	if (pid == 0)				/* no pid file */
  	{
  		write_stderr(_("%s: PID file \"%s\" does not exist\n"), progname, pid_file);
--- 1091,1097 ----
  {
  	pgpid_t		pid;
  
! 	pid = get_pgpid(false);
  	if (pid == 0)				/* no pid file */
  	{
  		write_stderr(_("%s: PID file \"%s\" does not exist\n"), progname, pid_file);
*************** do_promote(void)
*** 1110,1116 ****
  	pgpid_t		pid;
  	struct stat statbuf;
  
! 	pid = get_pgpid();
  
  	if (pid == 0)				/* no pid file */
  	{
--- 1130,1136 ----
  	pgpid_t		pid;
  	struct stat statbuf;
  
! 	pid = get_pgpid(false);
  
  	if (pid == 0)				/* no pid file */
  	{
*************** do_status(void)
*** 1204,1210 ****
  {
  	pgpid_t		pid;
  
! 	pid = get_pgpid();
  	/* Is there a pid file? */
  	if (pid != 0)
  	{
--- 1224,1230 ----
  {
  	pgpid_t		pid;
  
! 	pid = get_pgpid(true);
  	/* Is there a pid file? */
  	if (pid != 0)
  	{
*************** main(int argc, char **argv)
*** 2285,2290 ****
--- 2305,2311 ----
  	if (pg_data)
  	{
  		snprintf(postopts_file, MAXPGPATH, "%s/postmaster.opts", pg_data);
+ 		snprintf(version_file, MAXPGPATH, "%s/PG_VERSION", pg_data);
  		snprintf(pid_file, MAXPGPATH, "%s/postmaster.pid", pg_data);
  		snprintf(backup_file, MAXPGPATH, "%s/backup_label", pg_data);
  		snprintf(recovery_file, MAXPGPATH, "%s/recovery.conf", pg_data);
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to