--- postgresql-8.2.9.orig/src/bin/pg_ctl/pg_ctl.c 2008-06-19 09:58:21.653344252 +0200 +++ postgresql-8.2.9/src/bin/pg_ctl/pg_ctl.c 2008-06-19 10:05:45.576633140 +0200 @@ -19,6 +19,7 @@ #include "postgres_fe.h" #include "libpq-fe.h" +#include "pg_config_manual.h" #include #include @@ -402,11 +403,13 @@ bool success = false; int i; char portstr[32]; + char sockstr[MAXPGPATH]; char *p; char *q; - char connstr[128]; /* Should be way more than enough! */ + char connstr[MAXPGPATH+128]; /* Should be way more than enough! */ *portstr = '\0'; + *sockstr = '\0'; /* * Look in post_opts for a -p switch. @@ -449,7 +452,7 @@ * This parsing code isn't amazingly bright either, but it should be * okay for valid port settings. */ - if (!*portstr) + if (!*portstr || !*sockstr) { char **optlines; @@ -462,25 +465,40 @@ while (isspace((unsigned char) *p)) p++; - if (strncmp(p, "port", 4) != 0) - continue; - p += 4; - while (isspace((unsigned char) *p)) + if (!*portstr && strncmp(p, "port", 4) == 0) { + p += 4; + while (isspace((unsigned char) *p)) + p++; + if (*p != '=') + continue; p++; - if (*p != '=') - continue; - p++; - /* advance past any whitespace/quoting */ - while (isspace((unsigned char) *p) || *p == '\'' || *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 */ + StrNCpy(portstr, p, Min((q - p) + 1, sizeof(portstr))); + } + if (!*portstr && strncmp(p, "unix_socket_directory", strlen("unix_socket_directory")) == 0) { + p += 4; + while (isspace((unsigned char) *p)) + p++; + if (*p != '=') + continue; 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 */ - StrNCpy(portstr, p, Min((q - p) + 1, sizeof(portstr))); + /* 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 */ + StrNCpy(sockstr, p, Min((q - p) + 1, sizeof(sockstr))); + } /* keep looking, maybe there is another */ } } @@ -497,6 +515,13 @@ /* We need to set a connect timeout otherwise on Windows the SCM will probably timeout first */ snprintf(connstr, sizeof(connstr), "dbname=postgres port=%s connect_timeout=5", portstr); + /* path to directory containing the unix socket */ + if (*sockstr) { + strncat(connstr, " host=", sizeof(connstr) - strlen(connstr)); + strncat(connstr, sockstr, sizeof(connstr) - strlen(connstr)); + } + + for (i = 0; i < wait_seconds; i++) { if ((conn = PQconnectdb(connstr)) != NULL &&