Patch BUG #5103: "pg_ctl -w (re)start" fails with custom unix_socket_directory

Started by Quan Zongliangabout 15 years ago6 messages
#1Quan Zongliang
quanzongliang@gmail.com
1 attachment(s)

Hi, all

I created a pg_ctl patch to fix:
* BUG #5103: "pg_ctl -w (re)start" fails with custom unix_socket_directory
Allow pg_ctl to work properly with configuration files located outside the PGDATA directory

I tested it under Windows XP sp3.
All of configuration files(postgresql.conf pg_hba.conf pg_ident.conf) are in c:\data,
and data_dir is in C:\Program Files\PostgreSQL\9.0\data

Check the attchment, please.

Another question, after clone source with git I can not compile them:
Bad format filename 'src\bin\scripts\submake-libpq'
Former makefile like "createdb: createdb.o ... keywords.o"
Now it is "createdb: createdb.o ... keywords.o | submake-libpq"
How to do this?

------------------------------------------
SEARCHING JOB. I can work on C/C++.

Quan Zongliang <quanzongliang@gmail.com>

Attachments:

pg_ctl.difftext/plain; name=pg_ctl.diffDownload
diff --git a/src/bin/pg_ctl/pg_ctl.c b/src/bin/pg_ctl/pg_ctl.c
old mode 100644
new mode 100755
index 14d36b5..c708ba8
--- a/src/bin/pg_ctl/pg_ctl.c
+++ b/src/bin/pg_ctl/pg_ctl.c
@@ -89,6 +89,8 @@ static char *register_username = NULL;
 static char *register_password = NULL;
 static char *argv0 = NULL;
 static bool allow_core_files = false;
+static char *pgconf_portstr = NULL;
+static char *pgconf_datadir = NULL;
 
 static void
 write_stderr(const char *fmt,...)
@@ -455,41 +457,7 @@ test_postmaster_connection(bool do_checkpoint)
 	 * for valid port settings.
 	 */
 	if (!*portstr)
-	{
-		char	  **optlines;
-
-		optlines = readfile(conf_file);
-		if (optlines != NULL)
-		{
-			for (; *optlines != NULL; optlines++)
-			{
-				p = *optlines;
-
-				while (isspace((unsigned char) *p))
-					p++;
-				if (strncmp(p, "port", 4) != 0)
-					continue;
-				p += 4;
-				while (isspace((unsigned char) *p))
-					p++;
-				if (*p != '=')
-					continue;
-				p++;
-				/* advance past any whitespace/quoting */
-				while (isspace((unsigned char) *p) || *p == '\'' || *p == '"')
-					p++;
-				/* find end of value (not including any ending quote/comment!) */
-				q = p;
-				while (*q &&
-					   !(isspace((unsigned char) *q) ||
-						 *q == '\'' || *q == '"' || *q == '#'))
-					q++;
-				/* and save the argument value */
-				strlcpy(portstr, p, Min((q - p) + 1, sizeof(portstr)));
-				/* keep looking, maybe there is another */
-			}
-		}
-	}
+		strlcpy(portstr, pgconf_portstr, Min(sizeof(pgconf_portstr)+1, sizeof(portstr)));
 
 	/* Check environment */
 	if (!*portstr && getenv("PGPORT") != NULL)
@@ -547,6 +515,75 @@ test_postmaster_connection(bool do_checkpoint)
 }
 
 
+static void
+read_conf_file(void)
+{
+	char	**optlines;
+	char	 *p, *q;
+	bool     isportnum, isdatadir, isquoted;
+
+	optlines = readfile(conf_file);
+	if (optlines == NULL)
+		return;
+
+	for (; *optlines != NULL; optlines++)
+	{
+		p = *optlines;
+
+		while (isspace((unsigned char) *p))
+			p++;
+
+		if (strncmp(p, "port", 4) == 0)
+		{
+			isportnum = true;
+			p += 4;
+		}
+		else if (strncmp(p, "data_directory", 14) == 0)
+		{
+			isdatadir = true;
+			p += 14;
+		}
+		else
+			continue;
+
+		while (isspace((unsigned char) *p))
+			p++;
+		if (*p != '=')
+			continue;
+		p++;
+		isquoted = false;
+		/* advance past any whitespace/quoting */
+		while (isspace((unsigned char) *p) || *p == '\'' || *p == '"')
+		{
+			if (*p == '\'' || *p == '"')
+				isquoted = true;
+			p++;
+		}
+		/* find end of value (not including any ending quote/comment!) */
+		q = p;
+		while (*q &&
+			   !((!isquoted && isspace((unsigned char) *q)) ||
+				 *q == '\'' || *q == '"' || *q == '#'))
+			q++;
+		/* and save the argument value */
+		if (isportnum)
+		{
+			pgconf_portstr = (char *) pg_malloc((q - p) + 2);
+			strlcpy(pgconf_portstr, p, (q - p) + 1);
+		}
+		else if (isdatadir)
+		{
+			pgconf_datadir = (char *) pg_malloc((q - p) + 2);
+			strlcpy(pgconf_datadir, p, (q - p) + 1);
+			snprintf(pid_file, MAXPGPATH, "%s/postmaster.pid", pgconf_datadir);
+		}
+		/* keep looking, maybe there is another */
+	}
+
+	free(optlines);
+}
+
+
 #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_CORE)
 static void
 unlimit_core_size(void)
@@ -2010,6 +2047,8 @@ main(int argc, char **argv)
 		snprintf(conf_file, MAXPGPATH, "%s/postgresql.conf", pg_data);
 		snprintf(backup_file, MAXPGPATH, "%s/backup_label", pg_data);
 		snprintf(recovery_file, MAXPGPATH, "%s/recovery.conf", pg_data);
+
+		read_conf_file();
 	}
 
 	switch (ctl_command)
#2Alvaro Herrera
alvherre@commandprompt.com
In reply to: Quan Zongliang (#1)
Re: Patch BUG #5103: "pg_ctl -w (re)start" fails with custom unix_socket_directory

Excerpts from Quan Zongliang's message of sáb nov 27 06:03:12 -0300 2010:

Hi, all

I created a pg_ctl patch to fix:
* BUG #5103: "pg_ctl -w (re)start" fails with custom unix_socket_directory
Allow pg_ctl to work properly with configuration files located outside the PGDATA directory

I think the way this should work is that you call postmaster with a new
switch and it prints out its configuration, after reading the
appropriate config file(s). That way it handles all the little details
such as figuring out the correct config file, hadle include files, etc.
This output would be presumably easier to parse and more trustworthy.

Right now we have --describe-config, which is missing the values for
each config option.

--
Álvaro Herrera <alvherre@commandprompt.com>
The PostgreSQL Company - Command Prompt, Inc.
PostgreSQL Replication, Consulting, Custom Development, 24x7 support

#3Quan Zongliang
quanzongliang@gmail.com
In reply to: Alvaro Herrera (#2)
Re: Patch BUG #5103: "pg_ctl -w (re)start" fails with custom unix_socket_directory

On Mon, 29 Nov 2010 10:29:17 -0300
Alvaro Herrera <alvherre@commandprompt.com> wrote:

Excerpts from Quan Zongliang's message of s锟斤拷b nov 27 06:03:12 -0300 2010:

Hi, all

I created a pg_ctl patch to fix:
* BUG #5103: "pg_ctl -w (re)start" fails with custom unix_socket_directory
Allow pg_ctl to work properly with configuration files located outside the PGDATA directory

I think the way this should work is that you call postmaster with a new
switch and it prints out its configuration, after reading the
appropriate config file(s). That way it handles all the little details
such as figuring out the correct config file, hadle include files, etc.
This output would be presumably easier to parse and more trustworthy.

Right now we have --describe-config, which is missing the values for
each config option.

Sorry for my late reply.

I will check the source of postmaster.

--
Quan Zongliang <quanzongliang@gmail.com>

#4Alvaro Herrera
alvherre@commandprompt.com
In reply to: Quan Zongliang (#3)
Re: Patch BUG #5103: "pg_ctl -w (re)start" fails with custom unix_socket_directory

Excerpts from Quan Zongliang's message of mar dic 21 18:36:11 -0300 2010:

On Mon, 29 Nov 2010 10:29:17 -0300
Alvaro Herrera <alvherre@commandprompt.com> wrote:

I think the way this should work is that you call postmaster with a new
switch and it prints out its configuration, after reading the
appropriate config file(s). That way it handles all the little details
such as figuring out the correct config file, hadle include files, etc.
This output would be presumably easier to parse and more trustworthy.

Sorry for my late reply.

I will check the source of postmaster.

Actually Bruce Momjian is now working on a different fix:
unix_socket_directory would be added to postmaster.pid, allowing pg_ctl
to find it.

--
Álvaro Herrera <alvherre@commandprompt.com>
The PostgreSQL Company - Command Prompt, Inc.
PostgreSQL Replication, Consulting, Custom Development, 24x7 support

#5Bruce Momjian
bruce@momjian.us
In reply to: Alvaro Herrera (#4)
Re: Patch BUG #5103: "pg_ctl -w (re)start" fails with custom unix_socket_directory

Alvaro Herrera wrote:

Excerpts from Quan Zongliang's message of mar dic 21 18:36:11 -0300 2010:

On Mon, 29 Nov 2010 10:29:17 -0300
Alvaro Herrera <alvherre@commandprompt.com> wrote:

I think the way this should work is that you call postmaster with a new
switch and it prints out its configuration, after reading the
appropriate config file(s). That way it handles all the little details
such as figuring out the correct config file, hadle include files, etc.
This output would be presumably easier to parse and more trustworthy.

Sorry for my late reply.

I will check the source of postmaster.

Actually Bruce Momjian is now working on a different fix:
unix_socket_directory would be added to postmaster.pid, allowing pg_ctl
to find it.

Yes, I will apply this patch tomorrow and it will be in 9.1. Thanks for
the report.

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

+ It's impossible for everything to be true. +

#6Quan Zongliang
quanzongliang@gmail.com
In reply to: Bruce Momjian (#5)
Re: Patch BUG #5103: "pg_ctl -w (re)start" fails with custom unix_socket_directory

On Wed, 22 Dec 2010 21:02:35 -0500 (EST)
Bruce Momjian <bruce@momjian.us> wrote:

Alvaro Herrera wrote:

Excerpts from Quan Zongliang's message of mar dic 21 18:36:11 -0300 2010:

On Mon, 29 Nov 2010 10:29:17 -0300
Alvaro Herrera <alvherre@commandprompt.com> wrote:

I think the way this should work is that you call postmaster with a new
switch and it prints out its configuration, after reading the
appropriate config file(s). That way it handles all the little details
such as figuring out the correct config file, hadle include files, etc.
This output would be presumably easier to parse and more trustworthy.

Sorry for my late reply.

I will check the source of postmaster.

Actually Bruce Momjian is now working on a different fix:
unix_socket_directory would be added to postmaster.pid, allowing pg_ctl
to find it.

Yes, I will apply this patch tomorrow and it will be in 9.1. Thanks for
the report.

Nice work.

--
Quan Zongliang <quanzongliang@gmail.com>