--- postgresql-8.3.0/src/bin/pg_ctl/pg_ctl.c.orig 2008-01-01 20:45:55.000000000 +0100 +++ postgresql-8.3.0/src/bin/pg_ctl/pg_ctl.c 2008-02-19 21:30:21.369174813 +0100 @@ -414,11 +414,11 @@ PGconn *conn; bool success = false; int i; - char portstr[32]; + char portstr[32], sockstr[MAXPGPATH]; char *p; - char connstr[128]; /* Should be way more than enough! */ + char connstr[128+MAXPGPATH]; /* Should be way more than enough! */ - *portstr = '\0'; + *portstr = *sockstr = '\0'; /* post_opts */ for (p = post_opts; *p;) @@ -436,6 +436,15 @@ strlcpy(portstr, p, Min(strcspn(p, "\"'" WHITESPACE) + 1, sizeof(portstr))); /* keep looking, maybe there is another -p */ + } else if (strncmp(p, "-k", strlen("-k")) == 0) + { + p += strlen("-k"); + /* advance past whitespace/quoting */ + while (isspace((unsigned char) *p) || *p == '\'' || *p == '"') + p++; + strlcpy(sockstr, p, Min(strcspn(p, "\"'" WHITESPACE) + 1, + sizeof(sockstr))); + /* keep looking, maybe there is another -k */ } /* Advance to next whitespace */ while (*p && !isspace((unsigned char) *p)) @@ -443,7 +452,7 @@ } /* config file */ - if (!*portstr) + if (!*portstr || !*sockstr) { char **optlines; @@ -456,19 +465,30 @@ while (isspace((unsigned char) *p)) p++; - if (strncmp(p, "port", strlen("port")) != 0) - continue; - p += strlen("port"); - while (isspace((unsigned char) *p)) + if (!strncmp(p, "port", strlen("port"))) { + p += strlen("port"); + while (isspace((unsigned char) *p)) + p++; + if (*p != '=') + continue; p++; - if (*p != '=') - continue; - p++; - while (isspace((unsigned char) *p)) + while (isspace((unsigned char) *p)) + p++; + strlcpy(portstr, p, Min(strcspn(p, "#" WHITESPACE) + 1, + sizeof(portstr))); + /* keep looking, maybe there is another */ + } else if(!strncmp(p, "unix_socket_directory", strlen("unix_socket_directory"))) { + p += strlen("unix_socket_directory"); + while (isspace((unsigned char) *p)) + p++; + if (*p != '=') + continue; p++; - strlcpy(portstr, p, Min(strcspn(p, "#" WHITESPACE) + 1, - sizeof(portstr))); - /* keep looking, maybe there is another */ + while (isspace((unsigned char) *p) || *p == '\'' || *p == '"') + p++; + strlcpy(sockstr, p, Min(strcspn(p, "\"'#" WHITESPACE) + 1, + sizeof(sockstr))); + } } } } @@ -486,7 +506,8 @@ * probably timeout first */ snprintf(connstr, sizeof(connstr), - "dbname=postgres port=%s connect_timeout=5", portstr); + "dbname=postgres port=%s connect_timeout=5%s%s", portstr, + (*sockstr ? " host=" : ""), sockstr); for (i = 0; i < wait_seconds; i++) {