From 24dfa629d2f7682fd80278d86d87f7a92f0c0687 Mon Sep 17 00:00:00 2001 From: "kuroda.hayato%40jp.fujitsu.com" Date: Tue, 1 Nov 2022 09:13:20 +0000 Subject: [PATCH v24 1/3] Add PQConncheck and PQCanConncheck to libpq PQConncheck() function allows to check the status of socket by polling the socket. This function is currently available only on systems that support the non-standard POLLRDHUP extension to the poll system call, including Linux. PQCanConncheck() checks whether above function is available or not. --- doc/src/sgml/libpq.sgml | 44 ++++++++++++++++++++++++ src/interfaces/libpq/exports.txt | 2 ++ src/interfaces/libpq/fe-misc.c | 57 ++++++++++++++++++++++++++++++++ src/interfaces/libpq/libpq-fe.h | 4 +++ 4 files changed, 107 insertions(+) diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml index 0e7ae70c70..f7cff16f7f 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -2679,6 +2679,50 @@ void *PQgetssl(const PGconn *conn); + + PQConncheckPQConncheck + + + Returns the status of the socket. + + +int PQConncheck(PGconn *conn); + + + + + Unlike , this function checks socket + health. This check is performed by polling the socket. This function is + currently available only on systems that support the non-standard + POLLRDHUP extension to the poll system + call, including Linux. returns 1 + if the remote peer seems to be closed, returns 0 if + the socket is valid, and returns -1 if the connection + has been already invalid. + + + + + + PQCanConncheckPQCanConncheck + + + Returns the status of the socket. + + +int PQCanConncheck(void); + + + + + This function checks whether is + available or not on this platform. + returns 0 if the function is supported, otherwise + returns -1. + + + + diff --git a/src/interfaces/libpq/exports.txt b/src/interfaces/libpq/exports.txt index e8bcc88370..b90d178047 100644 --- a/src/interfaces/libpq/exports.txt +++ b/src/interfaces/libpq/exports.txt @@ -186,3 +186,5 @@ PQpipelineStatus 183 PQsetTraceFlags 184 PQmblenBounded 185 PQsendFlushRequest 186 +PQConncheck 187 +PQCanConncheck 188 diff --git a/src/interfaces/libpq/fe-misc.c b/src/interfaces/libpq/fe-misc.c index 3653a1a8a6..2888b94c0d 100644 --- a/src/interfaces/libpq/fe-misc.c +++ b/src/interfaces/libpq/fe-misc.c @@ -1218,6 +1218,63 @@ PQenv2encoding(void) return encoding; } +/* + * Helper function for PQconncheck(). + * + * Return >0 if opposite side seems to be disconnected. + */ +static int +pqConncheck_internal(int sock) +{ +#if (defined(HAVE_POLL) && defined(POLLRDHUP)) + struct pollfd input_fd; + int errflags = POLLHUP | POLLERR | POLLNVAL; + + input_fd.fd = sock; + input_fd.events = POLLRDHUP | errflags; + input_fd.revents = 0; + + poll(&input_fd, 1, 0); + + return input_fd.revents; +#else + /* Do not support socket checking on this platform, return 0 */ + return 0; +#endif +} + +/* + * Check whether the socket peer closed connection or not. + * + * Returns >0 if remote peer seems to be closed, 0 if it is valid, + * -1 if the input connection is bad. + */ +int +PQConncheck(PGconn *conn) +{ + /* quick exit if invalid connection has come */ + if (conn == NULL || + conn->sock == PGINVALID_SOCKET || + conn->status != CONNECTION_OK) + return -1; + + return pqConncheck_internal(conn->sock); +} + +/* + * Check whether PQConncheck() can work well on this platform. + * + * Returns 0 if this can use PQConncheck(), otherwise -1. + */ +int +PQCanConncheck(void) +{ +#if (defined(HAVE_POLL) && defined(POLLRDHUP)) + return 0; +#else + return -1; +#endif +} #ifdef ENABLE_NLS diff --git a/src/interfaces/libpq/libpq-fe.h b/src/interfaces/libpq/libpq-fe.h index f3d9220496..4771ee1124 100644 --- a/src/interfaces/libpq/libpq-fe.h +++ b/src/interfaces/libpq/libpq-fe.h @@ -648,6 +648,10 @@ extern int PQdsplen(const char *s, int encoding); /* Get encoding id from environment variable PGCLIENTENCODING */ extern int PQenv2encoding(void); +/* Check whether the postgres server is still alive or not */ +extern int PQConncheck(PGconn *conn); +extern int PQCanConncheck(void); + /* === in fe-auth.c === */ extern char *PQencryptPassword(const char *passwd, const char *user); -- 2.27.0