1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-15 03:41:20 +03:00

Clean up messy API for src/port/thread.c.

The point of this patch is to reduce inclusion spam by not needing
to #include <netdb.h> or <pwd.h> in port.h (which is read by every
compile in our tree).  To do that, we must remove port.h's
declarations of pqGetpwuid and pqGethostbyname.

pqGethostbyname is only used, and is only ever likely to be used,
in src/port/getaddrinfo.c --- which isn't even built on most
platforms, making pqGethostbyname dead code for most people.
Hence, deal with that by just moving it into getaddrinfo.c.

To clean up pqGetpwuid, invent a couple of simple wrapper
functions with less-messy APIs.  This allows removing some
duplicate error-handling code, too.

In passing, remove thread.c from the MSVC build, since it
contains nothing we use on Windows.

Noted while working on 376ce3e40.

Discussion: https://postgr.es/m/1634252654444.90107@mit.edu
This commit is contained in:
Tom Lane
2022-01-11 13:46:12 -05:00
parent 7fa945b857
commit 98e93a1fc9
11 changed files with 160 additions and 112 deletions

View File

@@ -2813,10 +2813,7 @@ keep_going: /* We will come back to here until there is
IS_AF_UNIX(conn->raddr.addr.ss_family))
{
#ifndef WIN32
char pwdbuf[BUFSIZ];
struct passwd pass_buf;
struct passwd *pass;
int passerr;
char *remote_username;
#endif
uid_t uid;
gid_t gid;
@@ -2839,28 +2836,20 @@ keep_going: /* We will come back to here until there is
}
#ifndef WIN32
passerr = pqGetpwuid(uid, &pass_buf, pwdbuf, sizeof(pwdbuf), &pass);
if (pass == NULL)
{
if (passerr != 0)
appendPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not look up local user ID %d: %s\n"),
(int) uid,
strerror_r(passerr, sebuf, sizeof(sebuf)));
else
appendPQExpBuffer(&conn->errorMessage,
libpq_gettext("local user with ID %d does not exist\n"),
(int) uid);
goto error_return;
}
remote_username = pg_fe_getusername(uid,
&conn->errorMessage);
if (remote_username == NULL)
goto error_return; /* message already logged */
if (strcmp(pass->pw_name, conn->requirepeer) != 0)
if (strcmp(remote_username, conn->requirepeer) != 0)
{
appendPQExpBuffer(&conn->errorMessage,
libpq_gettext("requirepeer specifies \"%s\", but actual peer user name is \"%s\"\n"),
conn->requirepeer, pass->pw_name);
conn->requirepeer, remote_username);
free(remote_username);
goto error_return;
}
free(remote_username);
#else /* WIN32 */
/* should have failed with ENOSYS above */
Assert(false);
@@ -7271,16 +7260,7 @@ pqGetHomeDirectory(char *buf, int bufsize)
home = getenv("HOME");
if (home == NULL || home[0] == '\0')
{
char pwdbuf[BUFSIZ];
struct passwd pwdstr;
struct passwd *pwd = NULL;
(void) pqGetpwuid(geteuid(), &pwdstr, pwdbuf, sizeof(pwdbuf), &pwd);
if (pwd == NULL)
return false;
home = pwd->pw_dir;
}
return pg_get_user_home_dir(geteuid(), buf, bufsize);
strlcpy(buf, home, bufsize);
return true;
#else