diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2022-01-11 13:46:12 -0500 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2022-01-11 13:46:20 -0500 |
commit | 98e93a1fc93e9b54eb477d870ec744e9e1669f34 (patch) | |
tree | 8d16b420c2b1b49c9c21af5d8fe93c9d1da93e1e /src/port/getaddrinfo.c | |
parent | 7fa945b857cc1b2964799411f1633468826861ff (diff) |
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
Diffstat (limited to 'src/port/getaddrinfo.c')
-rw-r--r-- | src/port/getaddrinfo.c | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/port/getaddrinfo.c b/src/port/getaddrinfo.c index b0ca4c778ed..3284c6eb52a 100644 --- a/src/port/getaddrinfo.c +++ b/src/port/getaddrinfo.c @@ -34,6 +34,14 @@ #include "port/pg_bswap.h" +#ifdef FRONTEND +static int pqGethostbyname(const char *name, + struct hostent *resultbuf, + char *buffer, size_t buflen, + struct hostent **result, + int *herrno); +#endif + #ifdef WIN32 /* * The native routines may or may not exist on the Windows platform we are on, @@ -394,3 +402,39 @@ getnameinfo(const struct sockaddr *sa, int salen, return 0; } + +/* + * Wrapper around gethostbyname() or gethostbyname_r() to mimic + * POSIX gethostbyname_r() behaviour, if it is not available or required. + */ +#ifdef FRONTEND +static int +pqGethostbyname(const char *name, + struct hostent *resultbuf, + char *buffer, size_t buflen, + struct hostent **result, + int *herrno) +{ +#if defined(ENABLE_THREAD_SAFETY) && defined(HAVE_GETHOSTBYNAME_R) + + /* + * broken (well early POSIX draft) gethostbyname_r() which returns 'struct + * hostent *' + */ + *result = gethostbyname_r(name, resultbuf, buffer, buflen, herrno); + return (*result == NULL) ? -1 : 0; +#else + + /* no gethostbyname_r(), just use gethostbyname() */ + *result = gethostbyname(name); + + if (*result != NULL) + *herrno = h_errno; + + if (*result != NULL) + return 0; + else + return -1; +#endif +} +#endif /* FRONTEND */ |