Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit40cb21f

Browse files
committed
Improve PQhost() to return useful data for default Unix-socket connections.
Previously, if no host information had been specified at connection time,PQhost() would return NULL (unless you are on Windows, in which case yougot "localhost"). This is an unhelpful definition for a couple of reasons:it can cause corner-case crashes in applications (cf commitc5ef8ce),and there's no well-defined way for applications to find out the socketdirectory path that's actually in use. As an example of the latterproblem, psql substituted DEFAULT_PGSOCKET_DIR for NULL in a couple ofplaces, but this is subtly wrong because it's conceivable that psql isusing a libpq shared library that was built with a different setting.Hence, change PQhost() to return DEFAULT_PGSOCKET_DIR when appropriate,and strip out the now-dead substitutions in psql. (There is still oneremaining reference to DEFAULT_PGSOCKET_DIR in psql, in prompt.c, whichI don't see a nice way to get rid of. But it only controls a promptabbreviation decision, so it seems noncritical.)Also update the docs for PQhost, which had never previously mentionedthe possibility of a socket directory path being returned. In passingfix the outright-incorrect code comment about PGconn.pgunixsocket.
1 parent92e3818 commit40cb21f

File tree

4 files changed

+13
-13
lines changed

4 files changed

+13
-13
lines changed

‎doc/src/sgml/libpq.sgml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,6 +1463,10 @@ char *PQpass(const PGconn *conn);
14631463
<listitem>
14641464
<para>
14651465
Returns the server host name of the connection.
1466+
This can be a host name, an IP address, or a directory path if the
1467+
connection is via Unix socket. (The path case can be distinguished
1468+
because it will always be an absolute path, beginning
1469+
with <literal>/</>.)
14661470
<synopsis>
14671471
char *PQhost(const PGconn *conn);
14681472
</synopsis>

‎src/bin/psql/command.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,6 @@ exec_command(const char *cmd,
322322
PQconninfoOption*option;
323323

324324
host=PQhost(pset.db);
325-
if (host==NULL)
326-
host=DEFAULT_PGSOCKET_DIR;
327325
/* A usable "hostaddr" overrides the basic sense of host. */
328326
connOptions=PQconninfo(pset.db);
329327
if (connOptions==NULL)
@@ -1750,16 +1748,14 @@ do_connect(char *dbname, char *user, char *host, char *port)
17501748
/*
17511749
* Any change in the parameters read above makes us discard the password.
17521750
* We also discard it if we're to use a conninfo rather than the
1753-
* positional syntax. Note that currently, PQhost() can return NULL for a
1754-
* default Unix-socket connection, so we have to allow NULL for host.
1751+
* positional syntax.
17551752
*/
17561753
if (has_connection_string)
17571754
keep_password= false;
17581755
else
17591756
keep_password=
17601757
(user&&PQuser(o_conn)&&strcmp(user,PQuser(o_conn))==0)&&
1761-
((host&&PQhost(o_conn)&&strcmp(host,PQhost(o_conn))==0)||
1762-
(host==NULL&&PQhost(o_conn)==NULL))&&
1758+
(host&&PQhost(o_conn)&&strcmp(host,PQhost(o_conn))==0)&&
17631759
(port&&PQport(o_conn)&&strcmp(port,PQport(o_conn))==0);
17641760

17651761
/*
@@ -1890,8 +1886,6 @@ do_connect(char *dbname, char *user, char *host, char *port)
18901886
{
18911887
char*host=PQhost(pset.db);
18921888

1893-
if (host==NULL)
1894-
host=DEFAULT_PGSOCKET_DIR;
18951889
/* If the host is an absolute path, the connection is via socket */
18961890
if (is_absolute_path(host))
18971891
printf(_("You are now connected to database \"%s\" as user \"%s\" via socket in \"%s\" at port \"%s\".\n"),

‎src/interfaces/libpq/fe-connect.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5353,7 +5353,10 @@ PQhost(const PGconn *conn)
53535353
else
53545354
{
53555355
#ifdefHAVE_UNIX_SOCKETS
5356-
returnconn->pgunixsocket;
5356+
if (conn->pgunixsocket!=NULL&&conn->pgunixsocket[0]!='\0')
5357+
returnconn->pgunixsocket;
5358+
else
5359+
returnDEFAULT_PGSOCKET_DIR;
53575360
#else
53585361
returnDefaultHost;
53595362
#endif

‎src/interfaces/libpq/libpq-int.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,9 @@ struct pg_conn
302302
char*pghostaddr;/* the numeric IP address of the machine on
303303
* which the server is running. Takes
304304
* precedence over above. */
305-
char*pgport;/* the server's communication port */
306-
char*pgunixsocket;/* the Unix-domain socket that the server is
307-
* listening on; if NULL, uses a default
308-
* constructed from pgport */
305+
char*pgport;/* the server's communication port number */
306+
char*pgunixsocket;/* the directory of the server's Unix-domain
307+
* socket; if NULL, use DEFAULT_PGSOCKET_DIR */
309308
char*pgtty;/* tty on which the backend messages is
310309
* displayed (OBSOLETE, NOT USED) */
311310
char*connect_timeout;/* connection timeout (numeric string) */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp