LCOV - code coverage report
Current view: top level - src/backend/libpq - pqcomm.c (source / functions) Hit Total Coverage
Test: PostgreSQL 17devel Lines: 340 549 61.9 %
Date: 2024-05-19 07:11:14 Functions: 43 47 91.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * pqcomm.c
       4             :  *    Communication functions between the Frontend and the Backend
       5             :  *
       6             :  * These routines handle the low-level details of communication between
       7             :  * frontend and backend.  They just shove data across the communication
       8             :  * channel, and are ignorant of the semantics of the data.
       9             :  *
      10             :  * To emit an outgoing message, use the routines in pqformat.c to construct
      11             :  * the message in a buffer and then emit it in one call to pq_putmessage.
      12             :  * There are no functions to send raw bytes or partial messages; this
      13             :  * ensures that the channel will not be clogged by an incomplete message if
      14             :  * execution is aborted by ereport(ERROR) partway through the message.
      15             :  *
      16             :  * At one time, libpq was shared between frontend and backend, but now
      17             :  * the backend's "backend/libpq" is quite separate from "interfaces/libpq".
      18             :  * All that remains is similarities of names to trap the unwary...
      19             :  *
      20             :  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
      21             :  * Portions Copyright (c) 1994, Regents of the University of California
      22             :  *
      23             :  *  src/backend/libpq/pqcomm.c
      24             :  *
      25             :  *-------------------------------------------------------------------------
      26             :  */
      27             : 
      28             : /*------------------------
      29             :  * INTERFACE ROUTINES
      30             :  *
      31             :  * setup/teardown:
      32             :  *      ListenServerPort    - Open postmaster's server port
      33             :  *      AcceptConnection    - Accept new connection with client
      34             :  *      TouchSocketFiles    - Protect socket files against /tmp cleaners
      35             :  *      pq_init             - initialize libpq at backend startup
      36             :  *      socket_comm_reset   - reset libpq during error recovery
      37             :  *      socket_close        - shutdown libpq at backend exit
      38             :  *
      39             :  * low-level I/O:
      40             :  *      pq_getbytes     - get a known number of bytes from connection
      41             :  *      pq_getmessage   - get a message with length word from connection
      42             :  *      pq_getbyte      - get next byte from connection
      43             :  *      pq_peekbyte     - peek at next byte from connection
      44             :  *      pq_flush        - flush pending output
      45             :  *      pq_flush_if_writable - flush pending output if writable without blocking
      46             :  *      pq_getbyte_if_available - get a byte if available without blocking
      47             :  *
      48             :  * message-level I/O
      49             :  *      pq_putmessage   - send a normal message (suppressed in COPY OUT mode)
      50             :  *      pq_putmessage_noblock - buffer a normal message (suppressed in COPY OUT)
      51             :  *
      52             :  *------------------------
      53             :  */
      54             : #include "postgres.h"
      55             : 
      56             : #ifdef HAVE_POLL_H
      57             : #include <poll.h>
      58             : #endif
      59             : #include <signal.h>
      60             : #include <fcntl.h>
      61             : #include <grp.h>
      62             : #include <unistd.h>
      63             : #include <sys/file.h>
      64             : #include <sys/socket.h>
      65             : #include <sys/stat.h>
      66             : #include <sys/time.h>
      67             : #include <netdb.h>
      68             : #include <netinet/in.h>
      69             : #include <netinet/tcp.h>
      70             : #include <utime.h>
      71             : #ifdef WIN32
      72             : #include <mstcpip.h>
      73             : #endif
      74             : 
      75             : #include "common/ip.h"
      76             : #include "libpq/libpq.h"
      77             : #include "miscadmin.h"
      78             : #include "port/pg_bswap.h"
      79             : #include "storage/ipc.h"
      80             : #include "utils/guc_hooks.h"
      81             : #include "utils/memutils.h"
      82             : 
      83             : /*
      84             :  * Cope with the various platform-specific ways to spell TCP keepalive socket
      85             :  * options.  This doesn't cover Windows, which as usual does its own thing.
      86             :  */
      87             : #if defined(TCP_KEEPIDLE)
      88             : /* TCP_KEEPIDLE is the name of this option on Linux and *BSD */
      89             : #define PG_TCP_KEEPALIVE_IDLE TCP_KEEPIDLE
      90             : #define PG_TCP_KEEPALIVE_IDLE_STR "TCP_KEEPIDLE"
      91             : #elif defined(TCP_KEEPALIVE_THRESHOLD)
      92             : /* TCP_KEEPALIVE_THRESHOLD is the name of this option on Solaris >= 11 */
      93             : #define PG_TCP_KEEPALIVE_IDLE TCP_KEEPALIVE_THRESHOLD
      94             : #define PG_TCP_KEEPALIVE_IDLE_STR "TCP_KEEPALIVE_THRESHOLD"
      95             : #elif defined(TCP_KEEPALIVE) && defined(__darwin__)
      96             : /* TCP_KEEPALIVE is the name of this option on macOS */
      97             : /* Caution: Solaris has this symbol but it means something different */
      98             : #define PG_TCP_KEEPALIVE_IDLE TCP_KEEPALIVE
      99             : #define PG_TCP_KEEPALIVE_IDLE_STR "TCP_KEEPALIVE"
     100             : #endif
     101             : 
     102             : /*
     103             :  * Configuration options
     104             :  */
     105             : int         Unix_socket_permissions;
     106             : char       *Unix_socket_group;
     107             : 
     108             : /* Where the Unix socket files are (list of palloc'd strings) */
     109             : static List *sock_paths = NIL;
     110             : 
     111             : /*
     112             :  * Buffers for low-level I/O.
     113             :  *
     114             :  * The receive buffer is fixed size. Send buffer is usually 8k, but can be
     115             :  * enlarged by pq_putmessage_noblock() if the message doesn't fit otherwise.
     116             :  */
     117             : 
     118             : #define PQ_SEND_BUFFER_SIZE 8192
     119             : #define PQ_RECV_BUFFER_SIZE 8192
     120             : 
     121             : static char *PqSendBuffer;
     122             : static int  PqSendBufferSize;   /* Size send buffer */
     123             : static size_t PqSendPointer;    /* Next index to store a byte in PqSendBuffer */
     124             : static size_t PqSendStart;      /* Next index to send a byte in PqSendBuffer */
     125             : 
     126             : static char PqRecvBuffer[PQ_RECV_BUFFER_SIZE];
     127             : static int  PqRecvPointer;      /* Next index to read a byte from PqRecvBuffer */
     128             : static int  PqRecvLength;       /* End of data available in PqRecvBuffer */
     129             : 
     130             : /*
     131             :  * Message status
     132             :  */
     133             : static bool PqCommBusy;         /* busy sending data to the client */
     134             : static bool PqCommReadingMsg;   /* in the middle of reading a message */
     135             : 
     136             : 
     137             : /* Internal functions */
     138             : static void socket_comm_reset(void);
     139             : static void socket_close(int code, Datum arg);
     140             : static void socket_set_nonblocking(bool nonblocking);
     141             : static int  socket_flush(void);
     142             : static int  socket_flush_if_writable(void);
     143             : static bool socket_is_send_pending(void);
     144             : static int  socket_putmessage(char msgtype, const char *s, size_t len);
     145             : static void socket_putmessage_noblock(char msgtype, const char *s, size_t len);
     146             : static inline int internal_putbytes(const char *s, size_t len);
     147             : static inline int internal_flush(void);
     148             : static pg_noinline int internal_flush_buffer(const char *buf, size_t *start,
     149             :                                              size_t *end);
     150             : 
     151             : static int  Lock_AF_UNIX(const char *unixSocketDir, const char *unixSocketPath);
     152             : static int  Setup_AF_UNIX(const char *sock_path);
     153             : 
     154             : static const PQcommMethods PqCommSocketMethods = {
     155             :     .comm_reset = socket_comm_reset,
     156             :     .flush = socket_flush,
     157             :     .flush_if_writable = socket_flush_if_writable,
     158             :     .is_send_pending = socket_is_send_pending,
     159             :     .putmessage = socket_putmessage,
     160             :     .putmessage_noblock = socket_putmessage_noblock
     161             : };
     162             : 
     163             : const PQcommMethods *PqCommMethods = &PqCommSocketMethods;
     164             : 
     165             : WaitEventSet *FeBeWaitSet;
     166             : 
     167             : 
     168             : /* --------------------------------
     169             :  *      pq_init - initialize libpq at backend startup
     170             :  * --------------------------------
     171             :  */
     172             : Port *
     173       21008 : pq_init(ClientSocket *client_sock)
     174             : {
     175             :     Port       *port;
     176             :     int         socket_pos PG_USED_FOR_ASSERTS_ONLY;
     177             :     int         latch_pos PG_USED_FOR_ASSERTS_ONLY;
     178             : 
     179             :     /* allocate the Port struct and copy the ClientSocket contents to it */
     180       21008 :     port = palloc0(sizeof(Port));
     181       21008 :     port->sock = client_sock->sock;
     182       21008 :     memcpy(&port->raddr.addr, &client_sock->raddr.addr, client_sock->raddr.salen);
     183       21008 :     port->raddr.salen = client_sock->raddr.salen;
     184             : 
     185             :     /* fill in the server (local) address */
     186       21008 :     port->laddr.salen = sizeof(port->laddr.addr);
     187       21008 :     if (getsockname(port->sock,
     188       21008 :                     (struct sockaddr *) &port->laddr.addr,
     189             :                     &port->laddr.salen) < 0)
     190             :     {
     191           0 :         ereport(FATAL,
     192             :                 (errmsg("%s() failed: %m", "getsockname")));
     193             :     }
     194             : 
     195             :     /* select NODELAY and KEEPALIVE options if it's a TCP connection */
     196       21008 :     if (port->laddr.addr.ss_family != AF_UNIX)
     197             :     {
     198             :         int         on;
     199             : #ifdef WIN32
     200             :         int         oldopt;
     201             :         int         optlen;
     202             :         int         newopt;
     203             : #endif
     204             : 
     205             : #ifdef  TCP_NODELAY
     206         516 :         on = 1;
     207         516 :         if (setsockopt(port->sock, IPPROTO_TCP, TCP_NODELAY,
     208             :                        (char *) &on, sizeof(on)) < 0)
     209             :         {
     210           0 :             ereport(FATAL,
     211             :                     (errmsg("%s(%s) failed: %m", "setsockopt", "TCP_NODELAY")));
     212             :         }
     213             : #endif
     214         516 :         on = 1;
     215         516 :         if (setsockopt(port->sock, SOL_SOCKET, SO_KEEPALIVE,
     216             :                        (char *) &on, sizeof(on)) < 0)
     217             :         {
     218           0 :             ereport(FATAL,
     219             :                     (errmsg("%s(%s) failed: %m", "setsockopt", "SO_KEEPALIVE")));
     220             :         }
     221             : 
     222             : #ifdef WIN32
     223             : 
     224             :         /*
     225             :          * This is a Win32 socket optimization.  The OS send buffer should be
     226             :          * large enough to send the whole Postgres send buffer in one go, or
     227             :          * performance suffers.  The Postgres send buffer can be enlarged if a
     228             :          * very large message needs to be sent, but we won't attempt to
     229             :          * enlarge the OS buffer if that happens, so somewhat arbitrarily
     230             :          * ensure that the OS buffer is at least PQ_SEND_BUFFER_SIZE * 4.
     231             :          * (That's 32kB with the current default).
     232             :          *
     233             :          * The default OS buffer size used to be 8kB in earlier Windows
     234             :          * versions, but was raised to 64kB in Windows 2012.  So it shouldn't
     235             :          * be necessary to change it in later versions anymore.  Changing it
     236             :          * unnecessarily can even reduce performance, because setting
     237             :          * SO_SNDBUF in the application disables the "dynamic send buffering"
     238             :          * feature that was introduced in Windows 7.  So before fiddling with
     239             :          * SO_SNDBUF, check if the current buffer size is already large enough
     240             :          * and only increase it if necessary.
     241             :          *
     242             :          * See https://support.microsoft.com/kb/823764/EN-US/ and
     243             :          * https://msdn.microsoft.com/en-us/library/bb736549%28v=vs.85%29.aspx
     244             :          */
     245             :         optlen = sizeof(oldopt);
     246             :         if (getsockopt(port->sock, SOL_SOCKET, SO_SNDBUF, (char *) &oldopt,
     247             :                        &optlen) < 0)
     248             :         {
     249             :             ereport(FATAL,
     250             :                     (errmsg("%s(%s) failed: %m", "getsockopt", "SO_SNDBUF")));
     251             :         }
     252             :         newopt = PQ_SEND_BUFFER_SIZE * 4;
     253             :         if (oldopt < newopt)
     254             :         {
     255             :             if (setsockopt(port->sock, SOL_SOCKET, SO_SNDBUF, (char *) &newopt,
     256             :                            sizeof(newopt)) < 0)
     257             :             {
     258             :                 ereport(FATAL,
     259             :                         (errmsg("%s(%s) failed: %m", "setsockopt", "SO_SNDBUF")));
     260             :             }
     261             :         }
     262             : #endif
     263             : 
     264             :         /*
     265             :          * Also apply the current keepalive parameters.  If we fail to set a
     266             :          * parameter, don't error out, because these aren't universally
     267             :          * supported.  (Note: you might think we need to reset the GUC
     268             :          * variables to 0 in such a case, but it's not necessary because the
     269             :          * show hooks for these variables report the truth anyway.)
     270             :          */
     271         516 :         (void) pq_setkeepalivesidle(tcp_keepalives_idle, port);
     272         516 :         (void) pq_setkeepalivesinterval(tcp_keepalives_interval, port);
     273         516 :         (void) pq_setkeepalivescount(tcp_keepalives_count, port);
     274         516 :         (void) pq_settcpusertimeout(tcp_user_timeout, port);
     275             :     }
     276             : 
     277             :     /* initialize state variables */
     278       21008 :     PqSendBufferSize = PQ_SEND_BUFFER_SIZE;
     279       21008 :     PqSendBuffer = MemoryContextAlloc(TopMemoryContext, PqSendBufferSize);
     280       21008 :     PqSendPointer = PqSendStart = PqRecvPointer = PqRecvLength = 0;
     281       21008 :     PqCommBusy = false;
     282       21008 :     PqCommReadingMsg = false;
     283             : 
     284             :     /* set up process-exit hook to close the socket */
     285       21008 :     on_proc_exit(socket_close, 0);
     286             : 
     287             :     /*
     288             :      * In backends (as soon as forked) we operate the underlying socket in
     289             :      * nonblocking mode and use latches to implement blocking semantics if
     290             :      * needed. That allows us to provide safely interruptible reads and
     291             :      * writes.
     292             :      */
     293             : #ifndef WIN32
     294       21008 :     if (!pg_set_noblock(port->sock))
     295           0 :         ereport(FATAL,
     296             :                 (errmsg("could not set socket to nonblocking mode: %m")));
     297             : #endif
     298             : 
     299             : #ifndef WIN32
     300             : 
     301             :     /* Don't give the socket to any subprograms we execute. */
     302       21008 :     if (fcntl(port->sock, F_SETFD, FD_CLOEXEC) < 0)
     303           0 :         elog(FATAL, "fcntl(F_SETFD) failed on socket: %m");
     304             : #endif
     305             : 
     306       21008 :     FeBeWaitSet = CreateWaitEventSet(NULL, FeBeWaitSetNEvents);
     307       21008 :     socket_pos = AddWaitEventToSet(FeBeWaitSet, WL_SOCKET_WRITEABLE,
     308             :                                    port->sock, NULL, NULL);
     309       21008 :     latch_pos = AddWaitEventToSet(FeBeWaitSet, WL_LATCH_SET, PGINVALID_SOCKET,
     310             :                                   MyLatch, NULL);
     311       21008 :     AddWaitEventToSet(FeBeWaitSet, WL_POSTMASTER_DEATH, PGINVALID_SOCKET,
     312             :                       NULL, NULL);
     313             : 
     314             :     /*
     315             :      * The event positions match the order we added them, but let's sanity
     316             :      * check them to be sure.
     317             :      */
     318             :     Assert(socket_pos == FeBeWaitSetSocketPos);
     319             :     Assert(latch_pos == FeBeWaitSetLatchPos);
     320             : 
     321       21008 :     return port;
     322             : }
     323             : 
     324             : /* --------------------------------
     325             :  *      socket_comm_reset - reset libpq during error recovery
     326             :  *
     327             :  * This is called from error recovery at the outer idle loop.  It's
     328             :  * just to get us out of trouble if we somehow manage to elog() from
     329             :  * inside a pqcomm.c routine (which ideally will never happen, but...)
     330             :  * --------------------------------
     331             :  */
     332             : static void
     333       39444 : socket_comm_reset(void)
     334             : {
     335             :     /* Do not throw away pending data, but do reset the busy flag */
     336       39444 :     PqCommBusy = false;
     337       39444 : }
     338             : 
     339             : /* --------------------------------
     340             :  *      socket_close - shutdown libpq at backend exit
     341             :  *
     342             :  * This is the one pg_on_exit_callback in place during BackendInitialize().
     343             :  * That function's unusual signal handling constrains that this callback be
     344             :  * safe to run at any instant.
     345             :  * --------------------------------
     346             :  */
     347             : static void
     348       21008 : socket_close(int code, Datum arg)
     349             : {
     350             :     /* Nothing to do in a standalone backend, where MyProcPort is NULL. */
     351       21008 :     if (MyProcPort != NULL)
     352             :     {
     353             : #ifdef ENABLE_GSS
     354             :         /*
     355             :          * Shutdown GSSAPI layer.  This section does nothing when interrupting
     356             :          * BackendInitialize(), because pg_GSS_recvauth() makes first use of
     357             :          * "ctx" and "cred".
     358             :          *
     359             :          * Note that we don't bother to free MyProcPort->gss, since we're
     360             :          * about to exit anyway.
     361             :          */
     362             :         if (MyProcPort->gss)
     363             :         {
     364             :             OM_uint32   min_s;
     365             : 
     366             :             if (MyProcPort->gss->ctx != GSS_C_NO_CONTEXT)
     367             :                 gss_delete_sec_context(&min_s, &MyProcPort->gss->ctx, NULL);
     368             : 
     369             :             if (MyProcPort->gss->cred != GSS_C_NO_CREDENTIAL)
     370             :                 gss_release_cred(&min_s, &MyProcPort->gss->cred);
     371             :         }
     372             : #endif                          /* ENABLE_GSS */
     373             : 
     374             :         /*
     375             :          * Cleanly shut down SSL layer.  Nowhere else does a postmaster child
     376             :          * call this, so this is safe when interrupting BackendInitialize().
     377             :          */
     378       21008 :         secure_close(MyProcPort);
     379             : 
     380             :         /*
     381             :          * Formerly we did an explicit close() here, but it seems better to
     382             :          * leave the socket open until the process dies.  This allows clients
     383             :          * to perform a "synchronous close" if they care --- wait till the
     384             :          * transport layer reports connection closure, and you can be sure the
     385             :          * backend has exited.
     386             :          *
     387             :          * We do set sock to PGINVALID_SOCKET to prevent any further I/O,
     388             :          * though.
     389             :          */
     390       21008 :         MyProcPort->sock = PGINVALID_SOCKET;
     391             :     }
     392       21008 : }
     393             : 
     394             : 
     395             : 
     396             : /* --------------------------------
     397             :  * Postmaster functions to handle sockets.
     398             :  * --------------------------------
     399             :  */
     400             : 
     401             : /*
     402             :  * ListenServerPort -- open a "listening" port to accept connections.
     403             :  *
     404             :  * family should be AF_UNIX or AF_UNSPEC; portNumber is the port number.
     405             :  * For AF_UNIX ports, hostName should be NULL and unixSocketDir must be
     406             :  * specified.  For TCP ports, hostName is either NULL for all interfaces or
     407             :  * the interface to listen on, and unixSocketDir is ignored (can be NULL).
     408             :  *
     409             :  * Successfully opened sockets are appended to the ListenSockets[] array.  On
     410             :  * entry, *NumListenSockets holds the number of elements currently in the
     411             :  * array, and it is updated to reflect the opened sockets.  MaxListen is the
     412             :  * allocated size of the array.
     413             :  *
     414             :  * RETURNS: STATUS_OK or STATUS_ERROR
     415             :  */
     416             : int
     417        1470 : ListenServerPort(int family, const char *hostName, unsigned short portNumber,
     418             :                  const char *unixSocketDir,
     419             :                  pgsocket ListenSockets[], int *NumListenSockets, int MaxListen)
     420             : {
     421             :     pgsocket    fd;
     422             :     int         err;
     423             :     int         maxconn;
     424             :     int         ret;
     425             :     char        portNumberStr[32];
     426             :     const char *familyDesc;
     427             :     char        familyDescBuf[64];
     428             :     const char *addrDesc;
     429             :     char        addrBuf[NI_MAXHOST];
     430             :     char       *service;
     431        1470 :     struct addrinfo *addrs = NULL,
     432             :                *addr;
     433             :     struct addrinfo hint;
     434        1470 :     int         added = 0;
     435             :     char        unixSocketPath[MAXPGPATH];
     436             : #if !defined(WIN32) || defined(IPV6_V6ONLY)
     437        1470 :     int         one = 1;
     438             : #endif
     439             : 
     440             :     /* Initialize hint structure */
     441       10290 :     MemSet(&hint, 0, sizeof(hint));
     442        1470 :     hint.ai_family = family;
     443        1470 :     hint.ai_flags = AI_PASSIVE;
     444        1470 :     hint.ai_socktype = SOCK_STREAM;
     445             : 
     446        1470 :     if (family == AF_UNIX)
     447             :     {
     448             :         /*
     449             :          * Create unixSocketPath from portNumber and unixSocketDir and lock
     450             :          * that file path
     451             :          */
     452        1420 :         UNIXSOCK_PATH(unixSocketPath, portNumber, unixSocketDir);
     453        1420 :         if (strlen(unixSocketPath) >= UNIXSOCK_PATH_BUFLEN)
     454             :         {
     455           0 :             ereport(LOG,
     456             :                     (errmsg("Unix-domain socket path \"%s\" is too long (maximum %d bytes)",
     457             :                             unixSocketPath,
     458             :                             (int) (UNIXSOCK_PATH_BUFLEN - 1))));
     459           0 :             return STATUS_ERROR;
     460             :         }
     461        1420 :         if (Lock_AF_UNIX(unixSocketDir, unixSocketPath) != STATUS_OK)
     462           0 :             return STATUS_ERROR;
     463        1420 :         service = unixSocketPath;
     464             :     }
     465             :     else
     466             :     {
     467          50 :         snprintf(portNumberStr, sizeof(portNumberStr), "%d", portNumber);
     468          50 :         service = portNumberStr;
     469             :     }
     470             : 
     471        1470 :     ret = pg_getaddrinfo_all(hostName, service, &hint, &addrs);
     472        1470 :     if (ret || !addrs)
     473             :     {
     474           0 :         if (hostName)
     475           0 :             ereport(LOG,
     476             :                     (errmsg("could not translate host name \"%s\", service \"%s\" to address: %s",
     477             :                             hostName, service, gai_strerror(ret))));
     478             :         else
     479           0 :             ereport(LOG,
     480             :                     (errmsg("could not translate service \"%s\" to address: %s",
     481             :                             service, gai_strerror(ret))));
     482           0 :         if (addrs)
     483           0 :             pg_freeaddrinfo_all(hint.ai_family, addrs);
     484           0 :         return STATUS_ERROR;
     485             :     }
     486             : 
     487        2942 :     for (addr = addrs; addr; addr = addr->ai_next)
     488             :     {
     489        1472 :         if (family != AF_UNIX && addr->ai_family == AF_UNIX)
     490             :         {
     491             :             /*
     492             :              * Only set up a unix domain socket when they really asked for it.
     493             :              * The service/port is different in that case.
     494             :              */
     495           0 :             continue;
     496             :         }
     497             : 
     498             :         /* See if there is still room to add 1 more socket. */
     499        1472 :         if (*NumListenSockets == MaxListen)
     500             :         {
     501           0 :             ereport(LOG,
     502             :                     (errmsg("could not bind to all requested addresses: MAXLISTEN (%d) exceeded",
     503             :                             MaxListen)));
     504           0 :             break;
     505             :         }
     506             : 
     507             :         /* set up address family name for log messages */
     508        1472 :         switch (addr->ai_family)
     509             :         {
     510          50 :             case AF_INET:
     511          50 :                 familyDesc = _("IPv4");
     512          50 :                 break;
     513           2 :             case AF_INET6:
     514           2 :                 familyDesc = _("IPv6");
     515           2 :                 break;
     516        1420 :             case AF_UNIX:
     517        1420 :                 familyDesc = _("Unix");
     518        1420 :                 break;
     519           0 :             default:
     520           0 :                 snprintf(familyDescBuf, sizeof(familyDescBuf),
     521           0 :                          _("unrecognized address family %d"),
     522             :                          addr->ai_family);
     523           0 :                 familyDesc = familyDescBuf;
     524           0 :                 break;
     525             :         }
     526             : 
     527             :         /* set up text form of address for log messages */
     528        1472 :         if (addr->ai_family == AF_UNIX)
     529        1420 :             addrDesc = unixSocketPath;
     530             :         else
     531             :         {
     532          52 :             pg_getnameinfo_all((const struct sockaddr_storage *) addr->ai_addr,
     533          52 :                                addr->ai_addrlen,
     534             :                                addrBuf, sizeof(addrBuf),
     535             :                                NULL, 0,
     536             :                                NI_NUMERICHOST);
     537          52 :             addrDesc = addrBuf;
     538             :         }
     539             : 
     540        1472 :         if ((fd = socket(addr->ai_family, SOCK_STREAM, 0)) == PGINVALID_SOCKET)
     541             :         {
     542           0 :             ereport(LOG,
     543             :                     (errcode_for_socket_access(),
     544             :             /* translator: first %s is IPv4, IPv6, or Unix */
     545             :                      errmsg("could not create %s socket for address \"%s\": %m",
     546             :                             familyDesc, addrDesc)));
     547           0 :             continue;
     548             :         }
     549             : 
     550             : #ifndef WIN32
     551             :         /* Don't give the listen socket to any subprograms we execute. */
     552        1472 :         if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0)
     553           0 :             elog(FATAL, "fcntl(F_SETFD) failed on socket: %m");
     554             : 
     555             :         /*
     556             :          * Without the SO_REUSEADDR flag, a new postmaster can't be started
     557             :          * right away after a stop or crash, giving "address already in use"
     558             :          * error on TCP ports.
     559             :          *
     560             :          * On win32, however, this behavior only happens if the
     561             :          * SO_EXCLUSIVEADDRUSE is set. With SO_REUSEADDR, win32 allows
     562             :          * multiple servers to listen on the same address, resulting in
     563             :          * unpredictable behavior. With no flags at all, win32 behaves as Unix
     564             :          * with SO_REUSEADDR.
     565             :          */
     566        1472 :         if (addr->ai_family != AF_UNIX)
     567             :         {
     568          52 :             if ((setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
     569             :                             (char *) &one, sizeof(one))) == -1)
     570             :             {
     571           0 :                 ereport(LOG,
     572             :                         (errcode_for_socket_access(),
     573             :                 /* translator: third %s is IPv4, IPv6, or Unix */
     574             :                          errmsg("%s(%s) failed for %s address \"%s\": %m",
     575             :                                 "setsockopt", "SO_REUSEADDR",
     576             :                                 familyDesc, addrDesc)));
     577           0 :                 closesocket(fd);
     578           0 :                 continue;
     579             :             }
     580             :         }
     581             : #endif
     582             : 
     583             : #ifdef IPV6_V6ONLY
     584        1472 :         if (addr->ai_family == AF_INET6)
     585             :         {
     586           2 :             if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY,
     587             :                            (char *) &one, sizeof(one)) == -1)
     588             :             {
     589           0 :                 ereport(LOG,
     590             :                         (errcode_for_socket_access(),
     591             :                 /* translator: third %s is IPv4, IPv6, or Unix */
     592             :                          errmsg("%s(%s) failed for %s address \"%s\": %m",
     593             :                                 "setsockopt", "IPV6_V6ONLY",
     594             :                                 familyDesc, addrDesc)));
     595           0 :                 closesocket(fd);
     596           0 :                 continue;
     597             :             }
     598             :         }
     599             : #endif
     600             : 
     601             :         /*
     602             :          * Note: This might fail on some OS's, like Linux older than
     603             :          * 2.4.21-pre3, that don't have the IPV6_V6ONLY socket option, and map
     604             :          * ipv4 addresses to ipv6.  It will show ::ffff:ipv4 for all ipv4
     605             :          * connections.
     606             :          */
     607        1472 :         err = bind(fd, addr->ai_addr, addr->ai_addrlen);
     608        1472 :         if (err < 0)
     609             :         {
     610           0 :             int         saved_errno = errno;
     611             : 
     612           0 :             ereport(LOG,
     613             :                     (errcode_for_socket_access(),
     614             :             /* translator: first %s is IPv4, IPv6, or Unix */
     615             :                      errmsg("could not bind %s address \"%s\": %m",
     616             :                             familyDesc, addrDesc),
     617             :                      saved_errno == EADDRINUSE ?
     618             :                      (addr->ai_family == AF_UNIX ?
     619             :                       errhint("Is another postmaster already running on port %d?",
     620             :                               (int) portNumber) :
     621             :                       errhint("Is another postmaster already running on port %d?"
     622             :                               " If not, wait a few seconds and retry.",
     623             :                               (int) portNumber)) : 0));
     624           0 :             closesocket(fd);
     625           0 :             continue;
     626             :         }
     627             : 
     628        1472 :         if (addr->ai_family == AF_UNIX)
     629             :         {
     630        1420 :             if (Setup_AF_UNIX(service) != STATUS_OK)
     631             :             {
     632           0 :                 closesocket(fd);
     633           0 :                 break;
     634             :             }
     635             :         }
     636             : 
     637             :         /*
     638             :          * Select appropriate accept-queue length limit.  It seems reasonable
     639             :          * to use a value similar to the maximum number of child processes
     640             :          * that the postmaster will permit.
     641             :          */
     642        1472 :         maxconn = MaxConnections * 2;
     643             : 
     644        1472 :         err = listen(fd, maxconn);
     645        1472 :         if (err < 0)
     646             :         {
     647           0 :             ereport(LOG,
     648             :                     (errcode_for_socket_access(),
     649             :             /* translator: first %s is IPv4, IPv6, or Unix */
     650             :                      errmsg("could not listen on %s address \"%s\": %m",
     651             :                             familyDesc, addrDesc)));
     652           0 :             closesocket(fd);
     653           0 :             continue;
     654             :         }
     655             : 
     656        1472 :         if (addr->ai_family == AF_UNIX)
     657        1420 :             ereport(LOG,
     658             :                     (errmsg("listening on Unix socket \"%s\"",
     659             :                             addrDesc)));
     660             :         else
     661          52 :             ereport(LOG,
     662             :             /* translator: first %s is IPv4 or IPv6 */
     663             :                     (errmsg("listening on %s address \"%s\", port %d",
     664             :                             familyDesc, addrDesc, (int) portNumber)));
     665             : 
     666        1472 :         ListenSockets[*NumListenSockets] = fd;
     667        1472 :         (*NumListenSockets)++;
     668        1472 :         added++;
     669             :     }
     670             : 
     671        1470 :     pg_freeaddrinfo_all(hint.ai_family, addrs);
     672             : 
     673        1470 :     if (!added)
     674           0 :         return STATUS_ERROR;
     675             : 
     676        1470 :     return STATUS_OK;
     677             : }
     678             : 
     679             : 
     680             : /*
     681             :  * Lock_AF_UNIX -- configure unix socket file path
     682             :  */
     683             : static int
     684        1420 : Lock_AF_UNIX(const char *unixSocketDir, const char *unixSocketPath)
     685             : {
     686             :     /* no lock file for abstract sockets */
     687        1420 :     if (unixSocketPath[0] == '@')
     688           0 :         return STATUS_OK;
     689             : 
     690             :     /*
     691             :      * Grab an interlock file associated with the socket file.
     692             :      *
     693             :      * Note: there are two reasons for using a socket lock file, rather than
     694             :      * trying to interlock directly on the socket itself.  First, it's a lot
     695             :      * more portable, and second, it lets us remove any pre-existing socket
     696             :      * file without race conditions.
     697             :      */
     698        1420 :     CreateSocketLockFile(unixSocketPath, true, unixSocketDir);
     699             : 
     700             :     /*
     701             :      * Once we have the interlock, we can safely delete any pre-existing
     702             :      * socket file to avoid failure at bind() time.
     703             :      */
     704        1420 :     (void) unlink(unixSocketPath);
     705             : 
     706             :     /*
     707             :      * Remember socket file pathnames for later maintenance.
     708             :      */
     709        1420 :     sock_paths = lappend(sock_paths, pstrdup(unixSocketPath));
     710             : 
     711        1420 :     return STATUS_OK;
     712             : }
     713             : 
     714             : 
     715             : /*
     716             :  * Setup_AF_UNIX -- configure unix socket permissions
     717             :  */
     718             : static int
     719        1420 : Setup_AF_UNIX(const char *sock_path)
     720             : {
     721             :     /* no file system permissions for abstract sockets */
     722        1420 :     if (sock_path[0] == '@')
     723           0 :         return STATUS_OK;
     724             : 
     725             :     /*
     726             :      * Fix socket ownership/permission if requested.  Note we must do this
     727             :      * before we listen() to avoid a window where unwanted connections could
     728             :      * get accepted.
     729             :      */
     730             :     Assert(Unix_socket_group);
     731        1420 :     if (Unix_socket_group[0] != '\0')
     732             :     {
     733             : #ifdef WIN32
     734             :         elog(WARNING, "configuration item \"unix_socket_group\" is not supported on this platform");
     735             : #else
     736             :         char       *endptr;
     737             :         unsigned long val;
     738             :         gid_t       gid;
     739             : 
     740           0 :         val = strtoul(Unix_socket_group, &endptr, 10);
     741           0 :         if (*endptr == '\0')
     742             :         {                       /* numeric group id */
     743           0 :             gid = val;
     744             :         }
     745             :         else
     746             :         {                       /* convert group name to id */
     747             :             struct group *gr;
     748             : 
     749           0 :             gr = getgrnam(Unix_socket_group);
     750           0 :             if (!gr)
     751             :             {
     752           0 :                 ereport(LOG,
     753             :                         (errmsg("group \"%s\" does not exist",
     754             :                                 Unix_socket_group)));
     755           0 :                 return STATUS_ERROR;
     756             :             }
     757           0 :             gid = gr->gr_gid;
     758             :         }
     759           0 :         if (chown(sock_path, -1, gid) == -1)
     760             :         {
     761           0 :             ereport(LOG,
     762             :                     (errcode_for_file_access(),
     763             :                      errmsg("could not set group of file \"%s\": %m",
     764             :                             sock_path)));
     765           0 :             return STATUS_ERROR;
     766             :         }
     767             : #endif
     768             :     }
     769             : 
     770        1420 :     if (chmod(sock_path, Unix_socket_permissions) == -1)
     771             :     {
     772           0 :         ereport(LOG,
     773             :                 (errcode_for_file_access(),
     774             :                  errmsg("could not set permissions of file \"%s\": %m",
     775             :                         sock_path)));
     776           0 :         return STATUS_ERROR;
     777             :     }
     778        1420 :     return STATUS_OK;
     779             : }
     780             : 
     781             : 
     782             : /*
     783             :  * AcceptConnection -- accept a new connection with client using
     784             :  *      server port.  Fills *client_sock with the FD and endpoint info
     785             :  *      of the new connection.
     786             :  *
     787             :  * ASSUME: that this doesn't need to be non-blocking because
     788             :  *      the Postmaster waits for the socket to be ready to accept().
     789             :  *
     790             :  * RETURNS: STATUS_OK or STATUS_ERROR
     791             :  */
     792             : int
     793       21374 : AcceptConnection(pgsocket server_fd, ClientSocket *client_sock)
     794             : {
     795             :     /* accept connection and fill in the client (remote) address */
     796       21374 :     client_sock->raddr.salen = sizeof(client_sock->raddr.addr);
     797       21374 :     if ((client_sock->sock = accept(server_fd,
     798       21374 :                                     (struct sockaddr *) &client_sock->raddr.addr,
     799             :                                     &client_sock->raddr.salen)) == PGINVALID_SOCKET)
     800             :     {
     801           0 :         ereport(LOG,
     802             :                 (errcode_for_socket_access(),
     803             :                  errmsg("could not accept new connection: %m")));
     804             : 
     805             :         /*
     806             :          * If accept() fails then postmaster.c will still see the server
     807             :          * socket as read-ready, and will immediately try again.  To avoid
     808             :          * uselessly sucking lots of CPU, delay a bit before trying again.
     809             :          * (The most likely reason for failure is being out of kernel file
     810             :          * table slots; we can do little except hope some will get freed up.)
     811             :          */
     812           0 :         pg_usleep(100000L);     /* wait 0.1 sec */
     813           0 :         return STATUS_ERROR;
     814             :     }
     815             : 
     816       21374 :     return STATUS_OK;
     817             : }
     818             : 
     819             : /*
     820             :  * TouchSocketFiles -- mark socket files as recently accessed
     821             :  *
     822             :  * This routine should be called every so often to ensure that the socket
     823             :  * files have a recent mod date (ordinary operations on sockets usually won't
     824             :  * change the mod date).  That saves them from being removed by
     825             :  * overenthusiastic /tmp-directory-cleaner daemons.  (Another reason we should
     826             :  * never have put the socket file in /tmp...)
     827             :  */
     828             : void
     829           0 : TouchSocketFiles(void)
     830             : {
     831             :     ListCell   *l;
     832             : 
     833             :     /* Loop through all created sockets... */
     834           0 :     foreach(l, sock_paths)
     835             :     {
     836           0 :         char       *sock_path = (char *) lfirst(l);
     837             : 
     838             :         /* Ignore errors; there's no point in complaining */
     839           0 :         (void) utime(sock_path, NULL);
     840             :     }
     841           0 : }
     842             : 
     843             : /*
     844             :  * RemoveSocketFiles -- unlink socket files at postmaster shutdown
     845             :  */
     846             : void
     847        1416 : RemoveSocketFiles(void)
     848             : {
     849             :     ListCell   *l;
     850             : 
     851             :     /* Loop through all created sockets... */
     852        2830 :     foreach(l, sock_paths)
     853             :     {
     854        1414 :         char       *sock_path = (char *) lfirst(l);
     855             : 
     856             :         /* Ignore any error. */
     857        1414 :         (void) unlink(sock_path);
     858             :     }
     859             :     /* Since we're about to exit, no need to reclaim storage */
     860        1416 :     sock_paths = NIL;
     861        1416 : }
     862             : 
     863             : 
     864             : /* --------------------------------
     865             :  * Low-level I/O routines begin here.
     866             :  *
     867             :  * These routines communicate with a frontend client across a connection
     868             :  * already established by the preceding routines.
     869             :  * --------------------------------
     870             :  */
     871             : 
     872             : /* --------------------------------
     873             :  *            socket_set_nonblocking - set socket blocking/non-blocking
     874             :  *
     875             :  * Sets the socket non-blocking if nonblocking is true, or sets it
     876             :  * blocking otherwise.
     877             :  * --------------------------------
     878             :  */
     879             : static void
     880     4305938 : socket_set_nonblocking(bool nonblocking)
     881             : {
     882     4305938 :     if (MyProcPort == NULL)
     883           0 :         ereport(ERROR,
     884             :                 (errcode(ERRCODE_CONNECTION_DOES_NOT_EXIST),
     885             :                  errmsg("there is no client connection")));
     886             : 
     887     4305938 :     MyProcPort->noblock = nonblocking;
     888     4305938 : }
     889             : 
     890             : /* --------------------------------
     891             :  *      pq_recvbuf - load some bytes into the input buffer
     892             :  *
     893             :  *      returns 0 if OK, EOF if trouble
     894             :  * --------------------------------
     895             :  */
     896             : static int
     897      684782 : pq_recvbuf(void)
     898             : {
     899      684782 :     if (PqRecvPointer > 0)
     900             :     {
     901      663774 :         if (PqRecvLength > PqRecvPointer)
     902             :         {
     903             :             /* still some unread data, left-justify it in the buffer */
     904           0 :             memmove(PqRecvBuffer, PqRecvBuffer + PqRecvPointer,
     905           0 :                     PqRecvLength - PqRecvPointer);
     906           0 :             PqRecvLength -= PqRecvPointer;
     907           0 :             PqRecvPointer = 0;
     908             :         }
     909             :         else
     910      663774 :             PqRecvLength = PqRecvPointer = 0;
     911             :     }
     912             : 
     913             :     /* Ensure that we're in blocking mode */
     914      684782 :     socket_set_nonblocking(false);
     915             : 
     916             :     /* Can fill buffer from PqRecvLength and upwards */
     917             :     for (;;)
     918           0 :     {
     919             :         int         r;
     920             : 
     921      684782 :         errno = 0;
     922             : 
     923     1369508 :         r = secure_read(MyProcPort, PqRecvBuffer + PqRecvLength,
     924      684782 :                         PQ_RECV_BUFFER_SIZE - PqRecvLength);
     925             : 
     926      684726 :         if (r < 0)
     927             :         {
     928           4 :             if (errno == EINTR)
     929           0 :                 continue;       /* Ok if interrupted */
     930             : 
     931             :             /*
     932             :              * Careful: an ereport() that tries to write to the client would
     933             :              * cause recursion to here, leading to stack overflow and core
     934             :              * dump!  This message must go *only* to the postmaster log.
     935             :              *
     936             :              * If errno is zero, assume it's EOF and let the caller complain.
     937             :              */
     938           4 :             if (errno != 0)
     939           4 :                 ereport(COMMERROR,
     940             :                         (errcode_for_socket_access(),
     941             :                          errmsg("could not receive data from client: %m")));
     942           4 :             return EOF;
     943             :         }
     944      684722 :         if (r == 0)
     945             :         {
     946             :             /*
     947             :              * EOF detected.  We used to write a log message here, but it's
     948             :              * better to expect the ultimate caller to do that.
     949             :              */
     950         134 :             return EOF;
     951             :         }
     952             :         /* r contains number of bytes read, so just incr length */
     953      684588 :         PqRecvLength += r;
     954      684588 :         return 0;
     955             :     }
     956             : }
     957             : 
     958             : /* --------------------------------
     959             :  *      pq_getbyte  - get a single byte from connection, or return EOF
     960             :  * --------------------------------
     961             :  */
     962             : int
     963     1038226 : pq_getbyte(void)
     964             : {
     965             :     Assert(PqCommReadingMsg);
     966             : 
     967     1604332 :     while (PqRecvPointer >= PqRecvLength)
     968             :     {
     969      566274 :         if (pq_recvbuf())       /* If nothing in buffer, then recv some */
     970         112 :             return EOF;         /* Failed to recv data */
     971             :     }
     972     1038058 :     return (unsigned char) PqRecvBuffer[PqRecvPointer++];
     973             : }
     974             : 
     975             : /* --------------------------------
     976             :  *      pq_peekbyte     - peek at next byte from connection
     977             :  *
     978             :  *   Same as pq_getbyte() except we don't advance the pointer.
     979             :  * --------------------------------
     980             :  */
     981             : int
     982       21008 : pq_peekbyte(void)
     983             : {
     984             :     Assert(PqCommReadingMsg);
     985             : 
     986       42016 :     while (PqRecvPointer >= PqRecvLength)
     987             :     {
     988       21008 :         if (pq_recvbuf())       /* If nothing in buffer, then recv some */
     989           0 :             return EOF;         /* Failed to recv data */
     990             :     }
     991       21008 :     return (unsigned char) PqRecvBuffer[PqRecvPointer];
     992             : }
     993             : 
     994             : /* --------------------------------
     995             :  *      pq_getbyte_if_available - get a single byte from connection,
     996             :  *          if available
     997             :  *
     998             :  * The received byte is stored in *c. Returns 1 if a byte was read,
     999             :  * 0 if no data was available, or EOF if trouble.
    1000             :  * --------------------------------
    1001             :  */
    1002             : int
    1003     2092060 : pq_getbyte_if_available(unsigned char *c)
    1004             : {
    1005             :     int         r;
    1006             : 
    1007             :     Assert(PqCommReadingMsg);
    1008             : 
    1009     2092060 :     if (PqRecvPointer < PqRecvLength)
    1010             :     {
    1011       67998 :         *c = PqRecvBuffer[PqRecvPointer++];
    1012       67998 :         return 1;
    1013             :     }
    1014             : 
    1015             :     /* Put the socket into non-blocking mode */
    1016     2024062 :     socket_set_nonblocking(true);
    1017             : 
    1018     2024062 :     errno = 0;
    1019             : 
    1020     2024062 :     r = secure_read(MyProcPort, c, 1);
    1021     2024062 :     if (r < 0)
    1022             :     {
    1023             :         /*
    1024             :          * Ok if no data available without blocking or interrupted (though
    1025             :          * EINTR really shouldn't happen with a non-blocking socket). Report
    1026             :          * other errors.
    1027             :          */
    1028     1929238 :         if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
    1029     1929232 :             r = 0;
    1030             :         else
    1031             :         {
    1032             :             /*
    1033             :              * Careful: an ereport() that tries to write to the client would
    1034             :              * cause recursion to here, leading to stack overflow and core
    1035             :              * dump!  This message must go *only* to the postmaster log.
    1036             :              *
    1037             :              * If errno is zero, assume it's EOF and let the caller complain.
    1038             :              */
    1039           6 :             if (errno != 0)
    1040           6 :                 ereport(COMMERROR,
    1041             :                         (errcode_for_socket_access(),
    1042             :                          errmsg("could not receive data from client: %m")));
    1043           6 :             r = EOF;
    1044             :         }
    1045             :     }
    1046       94824 :     else if (r == 0)
    1047             :     {
    1048             :         /* EOF detected */
    1049          32 :         r = EOF;
    1050             :     }
    1051             : 
    1052     2024062 :     return r;
    1053             : }
    1054             : 
    1055             : /* --------------------------------
    1056             :  *      pq_getbytes     - get a known number of bytes from connection
    1057             :  *
    1058             :  *      returns 0 if OK, EOF if trouble
    1059             :  * --------------------------------
    1060             :  */
    1061             : int
    1062     2423176 : pq_getbytes(char *s, size_t len)
    1063             : {
    1064             :     size_t      amount;
    1065             : 
    1066             :     Assert(PqCommReadingMsg);
    1067             : 
    1068     4848542 :     while (len > 0)
    1069             :     {
    1070     2522866 :         while (PqRecvPointer >= PqRecvLength)
    1071             :         {
    1072       97500 :             if (pq_recvbuf())   /* If nothing in buffer, then recv some */
    1073          26 :                 return EOF;     /* Failed to recv data */
    1074             :         }
    1075     2425366 :         amount = PqRecvLength - PqRecvPointer;
    1076     2425366 :         if (amount > len)
    1077     1740840 :             amount = len;
    1078     2425366 :         memcpy(s, PqRecvBuffer + PqRecvPointer, amount);
    1079     2425366 :         PqRecvPointer += amount;
    1080     2425366 :         s += amount;
    1081     2425366 :         len -= amount;
    1082             :     }
    1083     2423150 :     return 0;
    1084             : }
    1085             : 
    1086             : /* --------------------------------
    1087             :  *      pq_discardbytes     - throw away a known number of bytes
    1088             :  *
    1089             :  *      same as pq_getbytes except we do not copy the data to anyplace.
    1090             :  *      this is used for resynchronizing after read errors.
    1091             :  *
    1092             :  *      returns 0 if OK, EOF if trouble
    1093             :  * --------------------------------
    1094             :  */
    1095             : static int
    1096           0 : pq_discardbytes(size_t len)
    1097             : {
    1098             :     size_t      amount;
    1099             : 
    1100             :     Assert(PqCommReadingMsg);
    1101             : 
    1102           0 :     while (len > 0)
    1103             :     {
    1104           0 :         while (PqRecvPointer >= PqRecvLength)
    1105             :         {
    1106           0 :             if (pq_recvbuf())   /* If nothing in buffer, then recv some */
    1107           0 :                 return EOF;     /* Failed to recv data */
    1108             :         }
    1109           0 :         amount = PqRecvLength - PqRecvPointer;
    1110           0 :         if (amount > len)
    1111           0 :             amount = len;
    1112           0 :         PqRecvPointer += amount;
    1113           0 :         len -= amount;
    1114             :     }
    1115           0 :     return 0;
    1116             : }
    1117             : 
    1118             : /* --------------------------------
    1119             :  *      pq_buffer_remaining_data    - return number of bytes in receive buffer
    1120             :  *
    1121             :  * This will *not* attempt to read more data. And reading up to that number of
    1122             :  * bytes should not cause reading any more data either.
    1123             :  * --------------------------------
    1124             :  */
    1125             : ssize_t
    1126         704 : pq_buffer_remaining_data(void)
    1127             : {
    1128             :     Assert(PqRecvLength >= PqRecvPointer);
    1129         704 :     return (PqRecvLength - PqRecvPointer);
    1130             : }
    1131             : 
    1132             : 
    1133             : /* --------------------------------
    1134             :  *      pq_startmsgread - begin reading a message from the client.
    1135             :  *
    1136             :  *      This must be called before any of the pq_get* functions.
    1137             :  * --------------------------------
    1138             :  */
    1139             : void
    1140     3172780 : pq_startmsgread(void)
    1141             : {
    1142             :     /*
    1143             :      * There shouldn't be a read active already, but let's check just to be
    1144             :      * sure.
    1145             :      */
    1146     3172780 :     if (PqCommReadingMsg)
    1147           0 :         ereport(FATAL,
    1148             :                 (errcode(ERRCODE_PROTOCOL_VIOLATION),
    1149             :                  errmsg("terminating connection because protocol synchronization was lost")));
    1150             : 
    1151     3172780 :     PqCommReadingMsg = true;
    1152     3172780 : }
    1153             : 
    1154             : 
    1155             : /* --------------------------------
    1156             :  *      pq_endmsgread   - finish reading message.
    1157             :  *
    1158             :  *      This must be called after reading a message with pq_getbytes()
    1159             :  *      and friends, to indicate that we have read the whole message.
    1160             :  *      pq_getmessage() does this implicitly.
    1161             :  * --------------------------------
    1162             :  */
    1163             : void
    1164     1971700 : pq_endmsgread(void)
    1165             : {
    1166             :     Assert(PqCommReadingMsg);
    1167             : 
    1168     1971700 :     PqCommReadingMsg = false;
    1169     1971700 : }
    1170             : 
    1171             : /* --------------------------------
    1172             :  *      pq_is_reading_msg - are we currently reading a message?
    1173             :  *
    1174             :  * This is used in error recovery at the outer idle loop to detect if we have
    1175             :  * lost protocol sync, and need to terminate the connection. pq_startmsgread()
    1176             :  * will check for that too, but it's nicer to detect it earlier.
    1177             :  * --------------------------------
    1178             :  */
    1179             : bool
    1180       39444 : pq_is_reading_msg(void)
    1181             : {
    1182       39444 :     return PqCommReadingMsg;
    1183             : }
    1184             : 
    1185             : /* --------------------------------
    1186             :  *      pq_getmessage   - get a message with length word from connection
    1187             :  *
    1188             :  *      The return value is placed in an expansible StringInfo, which has
    1189             :  *      already been initialized by the caller.
    1190             :  *      Only the message body is placed in the StringInfo; the length word
    1191             :  *      is removed.  Also, s->cursor is initialized to zero for convenience
    1192             :  *      in scanning the message contents.
    1193             :  *
    1194             :  *      maxlen is the upper limit on the length of the
    1195             :  *      message we are willing to accept.  We abort the connection (by
    1196             :  *      returning EOF) if client tries to send more than that.
    1197             :  *
    1198             :  *      returns 0 if OK, EOF if trouble
    1199             :  * --------------------------------
    1200             :  */
    1201             : int
    1202     1200848 : pq_getmessage(StringInfo s, int maxlen)
    1203             : {
    1204             :     int32       len;
    1205             : 
    1206             :     Assert(PqCommReadingMsg);
    1207             : 
    1208     1200848 :     resetStringInfo(s);
    1209             : 
    1210             :     /* Read message length word */
    1211     1200848 :     if (pq_getbytes((char *) &len, 4) == EOF)
    1212             :     {
    1213           0 :         ereport(COMMERROR,
    1214             :                 (errcode(ERRCODE_PROTOCOL_VIOLATION),
    1215             :                  errmsg("unexpected EOF within message length word")));
    1216           0 :         return EOF;
    1217             :     }
    1218             : 
    1219     1200848 :     len = pg_ntoh32(len);
    1220             : 
    1221     1200848 :     if (len < 4 || len > maxlen)
    1222             :     {
    1223           0 :         ereport(COMMERROR,
    1224             :                 (errcode(ERRCODE_PROTOCOL_VIOLATION),
    1225             :                  errmsg("invalid message length")));
    1226           0 :         return EOF;
    1227             :     }
    1228             : 
    1229     1200848 :     len -= 4;                   /* discount length itself */
    1230             : 
    1231     1200848 :     if (len > 0)
    1232             :     {
    1233             :         /*
    1234             :          * Allocate space for message.  If we run out of room (ridiculously
    1235             :          * large message), we will elog(ERROR), but we want to discard the
    1236             :          * message body so as not to lose communication sync.
    1237             :          */
    1238     1157922 :         PG_TRY();
    1239             :         {
    1240     1157922 :             enlargeStringInfo(s, len);
    1241             :         }
    1242           0 :         PG_CATCH();
    1243             :         {
    1244           0 :             if (pq_discardbytes(len) == EOF)
    1245           0 :                 ereport(COMMERROR,
    1246             :                         (errcode(ERRCODE_PROTOCOL_VIOLATION),
    1247             :                          errmsg("incomplete message from client")));
    1248             : 
    1249             :             /* we discarded the rest of the message so we're back in sync. */
    1250           0 :             PqCommReadingMsg = false;
    1251           0 :             PG_RE_THROW();
    1252             :         }
    1253     1157922 :         PG_END_TRY();
    1254             : 
    1255             :         /* And grab the message */
    1256     1157922 :         if (pq_getbytes(s->data, len) == EOF)
    1257             :         {
    1258           0 :             ereport(COMMERROR,
    1259             :                     (errcode(ERRCODE_PROTOCOL_VIOLATION),
    1260             :                      errmsg("incomplete message from client")));
    1261           0 :             return EOF;
    1262             :         }
    1263     1157922 :         s->len = len;
    1264             :         /* Place a trailing null per StringInfo convention */
    1265     1157922 :         s->data[len] = '\0';
    1266             :     }
    1267             : 
    1268             :     /* finished reading the message. */
    1269     1200848 :     PqCommReadingMsg = false;
    1270             : 
    1271     1200848 :     return 0;
    1272             : }
    1273             : 
    1274             : 
    1275             : static inline int
    1276    36639612 : internal_putbytes(const char *s, size_t len)
    1277             : {
    1278    73563780 :     while (len > 0)
    1279             :     {
    1280             :         /* If buffer is full, then flush it out */
    1281    36924178 :         if (PqSendPointer >= PqSendBufferSize)
    1282             :         {
    1283      338270 :             socket_set_nonblocking(false);
    1284      338270 :             if (internal_flush())
    1285           8 :                 return EOF;
    1286             :         }
    1287             : 
    1288             :         /*
    1289             :          * If the buffer is empty and data length is larger than the buffer
    1290             :          * size, send it without buffering.  Otherwise, copy as much data as
    1291             :          * possible into the buffer.
    1292             :          */
    1293    36924170 :         if (len >= PqSendBufferSize && PqSendStart == PqSendPointer)
    1294      189394 :         {
    1295      189396 :             size_t      start = 0;
    1296             : 
    1297      189396 :             socket_set_nonblocking(false);
    1298      189396 :             if (internal_flush_buffer(s, &start, &len))
    1299           2 :                 return EOF;
    1300             :         }
    1301             :         else
    1302             :         {
    1303    36734774 :             size_t      amount = PqSendBufferSize - PqSendPointer;
    1304             : 
    1305    36734774 :             if (amount > len)
    1306    36384646 :                 amount = len;
    1307    36734774 :             memcpy(PqSendBuffer + PqSendPointer, s, amount);
    1308    36734774 :             PqSendPointer += amount;
    1309    36734774 :             s += amount;
    1310    36734774 :             len -= amount;
    1311             :         }
    1312             :     }
    1313             : 
    1314    36639602 :     return 0;
    1315             : }
    1316             : 
    1317             : /* --------------------------------
    1318             :  *      socket_flush        - flush pending output
    1319             :  *
    1320             :  *      returns 0 if OK, EOF if trouble
    1321             :  * --------------------------------
    1322             :  */
    1323             : static int
    1324      629726 : socket_flush(void)
    1325             : {
    1326             :     int         res;
    1327             : 
    1328             :     /* No-op if reentrant call */
    1329      629726 :     if (PqCommBusy)
    1330           0 :         return 0;
    1331      629726 :     PqCommBusy = true;
    1332      629726 :     socket_set_nonblocking(false);
    1333      629726 :     res = internal_flush();
    1334      629726 :     PqCommBusy = false;
    1335      629726 :     return res;
    1336             : }
    1337             : 
    1338             : /* --------------------------------
    1339             :  *      internal_flush - flush pending output
    1340             :  *
    1341             :  * Returns 0 if OK (meaning everything was sent, or operation would block
    1342             :  * and the socket is in non-blocking mode), or EOF if trouble.
    1343             :  * --------------------------------
    1344             :  */
    1345             : static inline int
    1346     1407698 : internal_flush(void)
    1347             : {
    1348     1407698 :     return internal_flush_buffer(PqSendBuffer, &PqSendStart, &PqSendPointer);
    1349             : }
    1350             : 
    1351             : /* --------------------------------
    1352             :  *      internal_flush_buffer - flush the given buffer content
    1353             :  *
    1354             :  * Returns 0 if OK (meaning everything was sent, or operation would block
    1355             :  * and the socket is in non-blocking mode), or EOF if trouble.
    1356             :  * --------------------------------
    1357             :  */
    1358             : static pg_noinline int
    1359     1597094 : internal_flush_buffer(const char *buf, size_t *start, size_t *end)
    1360             : {
    1361             :     static int  last_reported_send_errno = 0;
    1362             : 
    1363     1597094 :     const char *bufptr = buf + *start;
    1364     1597094 :     const char *bufend = buf + *end;
    1365             : 
    1366     3196420 :     while (bufptr < bufend)
    1367             :     {
    1368             :         int         r;
    1369             : 
    1370     1603910 :         r = secure_write(MyProcPort, (char *) bufptr, bufend - bufptr);
    1371             : 
    1372     1603910 :         if (r <= 0)
    1373             :         {
    1374        4584 :             if (errno == EINTR)
    1375           0 :                 continue;       /* Ok if we were interrupted */
    1376             : 
    1377             :             /*
    1378             :              * Ok if no data writable without blocking, and the socket is in
    1379             :              * non-blocking mode.
    1380             :              */
    1381        4584 :             if (errno == EAGAIN ||
    1382          50 :                 errno == EWOULDBLOCK)
    1383             :             {
    1384        4534 :                 return 0;
    1385             :             }
    1386             : 
    1387             :             /*
    1388             :              * Careful: an ereport() that tries to write to the client would
    1389             :              * cause recursion to here, leading to stack overflow and core
    1390             :              * dump!  This message must go *only* to the postmaster log.
    1391             :              *
    1392             :              * If a client disconnects while we're in the midst of output, we
    1393             :              * might write quite a bit of data before we get to a safe query
    1394             :              * abort point.  So, suppress duplicate log messages.
    1395             :              */
    1396          50 :             if (errno != last_reported_send_errno)
    1397             :             {
    1398          50 :                 last_reported_send_errno = errno;
    1399          50 :                 ereport(COMMERROR,
    1400             :                         (errcode_for_socket_access(),
    1401             :                          errmsg("could not send data to client: %m")));
    1402             :             }
    1403             : 
    1404             :             /*
    1405             :              * We drop the buffered data anyway so that processing can
    1406             :              * continue, even though we'll probably quit soon. We also set a
    1407             :              * flag that'll cause the next CHECK_FOR_INTERRUPTS to terminate
    1408             :              * the connection.
    1409             :              */
    1410          50 :             *start = *end = 0;
    1411          50 :             ClientConnectionLost = 1;
    1412          50 :             InterruptPending = 1;
    1413          50 :             return EOF;
    1414             :         }
    1415             : 
    1416     1599326 :         last_reported_send_errno = 0;   /* reset after any successful send */
    1417     1599326 :         bufptr += r;
    1418     1599326 :         *start += r;
    1419             :     }
    1420             : 
    1421     1592510 :     *start = *end = 0;
    1422     1592510 :     return 0;
    1423             : }
    1424             : 
    1425             : /* --------------------------------
    1426             :  *      pq_flush_if_writable - flush pending output if writable without blocking
    1427             :  *
    1428             :  * Returns 0 if OK, or EOF if trouble.
    1429             :  * --------------------------------
    1430             :  */
    1431             : static int
    1432     2252260 : socket_flush_if_writable(void)
    1433             : {
    1434             :     int         res;
    1435             : 
    1436             :     /* Quick exit if nothing to do */
    1437     2252260 :     if (PqSendPointer == PqSendStart)
    1438     1812558 :         return 0;
    1439             : 
    1440             :     /* No-op if reentrant call */
    1441      439702 :     if (PqCommBusy)
    1442           0 :         return 0;
    1443             : 
    1444             :     /* Temporarily put the socket into non-blocking mode */
    1445      439702 :     socket_set_nonblocking(true);
    1446             : 
    1447      439702 :     PqCommBusy = true;
    1448      439702 :     res = internal_flush();
    1449      439702 :     PqCommBusy = false;
    1450      439702 :     return res;
    1451             : }
    1452             : 
    1453             : /* --------------------------------
    1454             :  *  socket_is_send_pending  - is there any pending data in the output buffer?
    1455             :  * --------------------------------
    1456             :  */
    1457             : static bool
    1458     4344128 : socket_is_send_pending(void)
    1459             : {
    1460     4344128 :     return (PqSendStart < PqSendPointer);
    1461             : }
    1462             : 
    1463             : /* --------------------------------
    1464             :  * Message-level I/O routines begin here.
    1465             :  * --------------------------------
    1466             :  */
    1467             : 
    1468             : 
    1469             : /* --------------------------------
    1470             :  *      socket_putmessage - send a normal message (suppressed in COPY OUT mode)
    1471             :  *
    1472             :  *      msgtype is a message type code to place before the message body.
    1473             :  *
    1474             :  *      len is the length of the message body data at *s.  A message length
    1475             :  *      word (equal to len+4 because it counts itself too) is inserted by this
    1476             :  *      routine.
    1477             :  *
    1478             :  *      We suppress messages generated while pqcomm.c is busy.  This
    1479             :  *      avoids any possibility of messages being inserted within other
    1480             :  *      messages.  The only known trouble case arises if SIGQUIT occurs
    1481             :  *      during a pqcomm.c routine --- quickdie() will try to send a warning
    1482             :  *      message, and the most reasonable approach seems to be to drop it.
    1483             :  *
    1484             :  *      returns 0 if OK, EOF if trouble
    1485             :  * --------------------------------
    1486             :  */
    1487             : static int
    1488    12213204 : socket_putmessage(char msgtype, const char *s, size_t len)
    1489             : {
    1490             :     uint32      n32;
    1491             : 
    1492             :     Assert(msgtype != 0);
    1493             : 
    1494    12213204 :     if (PqCommBusy)
    1495           0 :         return 0;
    1496    12213204 :     PqCommBusy = true;
    1497    12213204 :     if (internal_putbytes(&msgtype, 1))
    1498           0 :         goto fail;
    1499             : 
    1500    12213204 :     n32 = pg_hton32((uint32) (len + 4));
    1501    12213204 :     if (internal_putbytes((char *) &n32, 4))
    1502           0 :         goto fail;
    1503             : 
    1504    12213204 :     if (internal_putbytes(s, len))
    1505          10 :         goto fail;
    1506    12213194 :     PqCommBusy = false;
    1507    12213194 :     return 0;
    1508             : 
    1509          10 : fail:
    1510          10 :     PqCommBusy = false;
    1511          10 :     return EOF;
    1512             : }
    1513             : 
    1514             : /* --------------------------------
    1515             :  *      pq_putmessage_noblock   - like pq_putmessage, but never blocks
    1516             :  *
    1517             :  *      If the output buffer is too small to hold the message, the buffer
    1518             :  *      is enlarged.
    1519             :  */
    1520             : static void
    1521      435068 : socket_putmessage_noblock(char msgtype, const char *s, size_t len)
    1522             : {
    1523             :     int         res PG_USED_FOR_ASSERTS_ONLY;
    1524             :     int         required;
    1525             : 
    1526             :     /*
    1527             :      * Ensure we have enough space in the output buffer for the message header
    1528             :      * as well as the message itself.
    1529             :      */
    1530      435068 :     required = PqSendPointer + 1 + 4 + len;
    1531      435068 :     if (required > PqSendBufferSize)
    1532             :     {
    1533         846 :         PqSendBuffer = repalloc(PqSendBuffer, required);
    1534         846 :         PqSendBufferSize = required;
    1535             :     }
    1536      435068 :     res = pq_putmessage(msgtype, s, len);
    1537             :     Assert(res == 0);           /* should not fail when the message fits in
    1538             :                                  * buffer */
    1539      435068 : }
    1540             : 
    1541             : /* --------------------------------
    1542             :  *      pq_putmessage_v2 - send a message in protocol version 2
    1543             :  *
    1544             :  *      msgtype is a message type code to place before the message body.
    1545             :  *
    1546             :  *      We no longer support protocol version 2, but we have kept this
    1547             :  *      function so that if a client tries to connect with protocol version 2,
    1548             :  *      as a courtesy we can still send the "unsupported protocol version"
    1549             :  *      error to the client in the old format.
    1550             :  *
    1551             :  *      Like in pq_putmessage(), we suppress messages generated while
    1552             :  *      pqcomm.c is busy.
    1553             :  *
    1554             :  *      returns 0 if OK, EOF if trouble
    1555             :  * --------------------------------
    1556             :  */
    1557             : int
    1558           0 : pq_putmessage_v2(char msgtype, const char *s, size_t len)
    1559             : {
    1560             :     Assert(msgtype != 0);
    1561             : 
    1562           0 :     if (PqCommBusy)
    1563           0 :         return 0;
    1564           0 :     PqCommBusy = true;
    1565           0 :     if (internal_putbytes(&msgtype, 1))
    1566           0 :         goto fail;
    1567             : 
    1568           0 :     if (internal_putbytes(s, len))
    1569           0 :         goto fail;
    1570           0 :     PqCommBusy = false;
    1571           0 :     return 0;
    1572             : 
    1573           0 : fail:
    1574           0 :     PqCommBusy = false;
    1575           0 :     return EOF;
    1576             : }
    1577             : 
    1578             : /*
    1579             :  * Support for TCP Keepalive parameters
    1580             :  */
    1581             : 
    1582             : /*
    1583             :  * On Windows, we need to set both idle and interval at the same time.
    1584             :  * We also cannot reset them to the default (setting to zero will
    1585             :  * actually set them to zero, not default), therefore we fallback to
    1586             :  * the out-of-the-box default instead.
    1587             :  */
    1588             : #if defined(WIN32) && defined(SIO_KEEPALIVE_VALS)
    1589             : static int
    1590             : pq_setkeepaliveswin32(Port *port, int idle, int interval)
    1591             : {
    1592             :     struct tcp_keepalive ka;
    1593             :     DWORD       retsize;
    1594             : 
    1595             :     if (idle <= 0)
    1596             :         idle = 2 * 60 * 60;     /* default = 2 hours */
    1597             :     if (interval <= 0)
    1598             :         interval = 1;           /* default = 1 second */
    1599             : 
    1600             :     ka.onoff = 1;
    1601             :     ka.keepalivetime = idle * 1000;
    1602             :     ka.keepaliveinterval = interval * 1000;
    1603             : 
    1604             :     if (WSAIoctl(port->sock,
    1605             :                  SIO_KEEPALIVE_VALS,
    1606             :                  (LPVOID) &ka,
    1607             :                  sizeof(ka),
    1608             :                  NULL,
    1609             :                  0,
    1610             :                  &retsize,
    1611             :                  NULL,
    1612             :                  NULL)
    1613             :         != 0)
    1614             :     {
    1615             :         ereport(LOG,
    1616             :                 (errmsg("%s(%s) failed: error code %d",
    1617             :                         "WSAIoctl", "SIO_KEEPALIVE_VALS", WSAGetLastError())));
    1618             :         return STATUS_ERROR;
    1619             :     }
    1620             :     if (port->keepalives_idle != idle)
    1621             :         port->keepalives_idle = idle;
    1622             :     if (port->keepalives_interval != interval)
    1623             :         port->keepalives_interval = interval;
    1624             :     return STATUS_OK;
    1625             : }
    1626             : #endif
    1627             : 
    1628             : int
    1629        3906 : pq_getkeepalivesidle(Port *port)
    1630             : {
    1631             : #if defined(PG_TCP_KEEPALIVE_IDLE) || defined(SIO_KEEPALIVE_VALS)
    1632        3906 :     if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
    1633        3906 :         return 0;
    1634             : 
    1635           0 :     if (port->keepalives_idle != 0)
    1636           0 :         return port->keepalives_idle;
    1637             : 
    1638           0 :     if (port->default_keepalives_idle == 0)
    1639             :     {
    1640             : #ifndef WIN32
    1641           0 :         socklen_t   size = sizeof(port->default_keepalives_idle);
    1642             : 
    1643           0 :         if (getsockopt(port->sock, IPPROTO_TCP, PG_TCP_KEEPALIVE_IDLE,
    1644           0 :                        (char *) &port->default_keepalives_idle,
    1645             :                        &size) < 0)
    1646             :         {
    1647           0 :             ereport(LOG,
    1648             :                     (errmsg("%s(%s) failed: %m", "getsockopt", PG_TCP_KEEPALIVE_IDLE_STR)));
    1649           0 :             port->default_keepalives_idle = -1; /* don't know */
    1650             :         }
    1651             : #else                           /* WIN32 */
    1652             :         /* We can't get the defaults on Windows, so return "don't know" */
    1653             :         port->default_keepalives_idle = -1;
    1654             : #endif                          /* WIN32 */
    1655             :     }
    1656             : 
    1657           0 :     return port->default_keepalives_idle;
    1658             : #else
    1659             :     return 0;
    1660             : #endif
    1661             : }
    1662             : 
    1663             : int
    1664        2346 : pq_setkeepalivesidle(int idle, Port *port)
    1665             : {
    1666        2346 :     if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
    1667        1830 :         return STATUS_OK;
    1668             : 
    1669             : /* check SIO_KEEPALIVE_VALS here, not just WIN32, as some toolchains lack it */
    1670             : #if defined(PG_TCP_KEEPALIVE_IDLE) || defined(SIO_KEEPALIVE_VALS)
    1671         516 :     if (idle == port->keepalives_idle)
    1672         516 :         return STATUS_OK;
    1673             : 
    1674             : #ifndef WIN32
    1675           0 :     if (port->default_keepalives_idle <= 0)
    1676             :     {
    1677           0 :         if (pq_getkeepalivesidle(port) < 0)
    1678             :         {
    1679           0 :             if (idle == 0)
    1680           0 :                 return STATUS_OK;   /* default is set but unknown */
    1681             :             else
    1682           0 :                 return STATUS_ERROR;
    1683             :         }
    1684             :     }
    1685             : 
    1686           0 :     if (idle == 0)
    1687           0 :         idle = port->default_keepalives_idle;
    1688             : 
    1689           0 :     if (setsockopt(port->sock, IPPROTO_TCP, PG_TCP_KEEPALIVE_IDLE,
    1690             :                    (char *) &idle, sizeof(idle)) < 0)
    1691             :     {
    1692           0 :         ereport(LOG,
    1693             :                 (errmsg("%s(%s) failed: %m", "setsockopt", PG_TCP_KEEPALIVE_IDLE_STR)));
    1694           0 :         return STATUS_ERROR;
    1695             :     }
    1696             : 
    1697           0 :     port->keepalives_idle = idle;
    1698             : #else                           /* WIN32 */
    1699             :     return pq_setkeepaliveswin32(port, idle, port->keepalives_interval);
    1700             : #endif
    1701             : #else
    1702             :     if (idle != 0)
    1703             :     {
    1704             :         ereport(LOG,
    1705             :                 (errmsg("setting the keepalive idle time is not supported")));
    1706             :         return STATUS_ERROR;
    1707             :     }
    1708             : #endif
    1709             : 
    1710           0 :     return STATUS_OK;
    1711             : }
    1712             : 
    1713             : int
    1714        3906 : pq_getkeepalivesinterval(Port *port)
    1715             : {
    1716             : #if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
    1717        3906 :     if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
    1718        3906 :         return 0;
    1719             : 
    1720           0 :     if (port->keepalives_interval != 0)
    1721           0 :         return port->keepalives_interval;
    1722             : 
    1723           0 :     if (port->default_keepalives_interval == 0)
    1724             :     {
    1725             : #ifndef WIN32
    1726           0 :         socklen_t   size = sizeof(port->default_keepalives_interval);
    1727             : 
    1728           0 :         if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPINTVL,
    1729           0 :                        (char *) &port->default_keepalives_interval,
    1730             :                        &size) < 0)
    1731             :         {
    1732           0 :             ereport(LOG,
    1733             :                     (errmsg("%s(%s) failed: %m", "getsockopt", "TCP_KEEPINTVL")));
    1734           0 :             port->default_keepalives_interval = -1; /* don't know */
    1735             :         }
    1736             : #else
    1737             :         /* We can't get the defaults on Windows, so return "don't know" */
    1738             :         port->default_keepalives_interval = -1;
    1739             : #endif                          /* WIN32 */
    1740             :     }
    1741             : 
    1742           0 :     return port->default_keepalives_interval;
    1743             : #else
    1744             :     return 0;
    1745             : #endif
    1746             : }
    1747             : 
    1748             : int
    1749        2346 : pq_setkeepalivesinterval(int interval, Port *port)
    1750             : {
    1751        2346 :     if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
    1752        1830 :         return STATUS_OK;
    1753             : 
    1754             : #if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
    1755         516 :     if (interval == port->keepalives_interval)
    1756         516 :         return STATUS_OK;
    1757             : 
    1758             : #ifndef WIN32
    1759           0 :     if (port->default_keepalives_interval <= 0)
    1760             :     {
    1761           0 :         if (pq_getkeepalivesinterval(port) < 0)
    1762             :         {
    1763           0 :             if (interval == 0)
    1764           0 :                 return STATUS_OK;   /* default is set but unknown */
    1765             :             else
    1766           0 :                 return STATUS_ERROR;
    1767             :         }
    1768             :     }
    1769             : 
    1770           0 :     if (interval == 0)
    1771           0 :         interval = port->default_keepalives_interval;
    1772             : 
    1773           0 :     if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPINTVL,
    1774             :                    (char *) &interval, sizeof(interval)) < 0)
    1775             :     {
    1776           0 :         ereport(LOG,
    1777             :                 (errmsg("%s(%s) failed: %m", "setsockopt", "TCP_KEEPINTVL")));
    1778           0 :         return STATUS_ERROR;
    1779             :     }
    1780             : 
    1781           0 :     port->keepalives_interval = interval;
    1782             : #else                           /* WIN32 */
    1783             :     return pq_setkeepaliveswin32(port, port->keepalives_idle, interval);
    1784             : #endif
    1785             : #else
    1786             :     if (interval != 0)
    1787             :     {
    1788             :         ereport(LOG,
    1789             :                 (errmsg("%s(%s) not supported", "setsockopt", "TCP_KEEPINTVL")));
    1790             :         return STATUS_ERROR;
    1791             :     }
    1792             : #endif
    1793             : 
    1794           0 :     return STATUS_OK;
    1795             : }
    1796             : 
    1797             : int
    1798        3906 : pq_getkeepalivescount(Port *port)
    1799             : {
    1800             : #ifdef TCP_KEEPCNT
    1801        3906 :     if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
    1802        3906 :         return 0;
    1803             : 
    1804           0 :     if (port->keepalives_count != 0)
    1805           0 :         return port->keepalives_count;
    1806             : 
    1807           0 :     if (port->default_keepalives_count == 0)
    1808             :     {
    1809           0 :         socklen_t   size = sizeof(port->default_keepalives_count);
    1810             : 
    1811           0 :         if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPCNT,
    1812           0 :                        (char *) &port->default_keepalives_count,
    1813             :                        &size) < 0)
    1814             :         {
    1815           0 :             ereport(LOG,
    1816             :                     (errmsg("%s(%s) failed: %m", "getsockopt", "TCP_KEEPCNT")));
    1817           0 :             port->default_keepalives_count = -1; /* don't know */
    1818             :         }
    1819             :     }
    1820             : 
    1821           0 :     return port->default_keepalives_count;
    1822             : #else
    1823             :     return 0;
    1824             : #endif
    1825             : }
    1826             : 
    1827             : int
    1828        2346 : pq_setkeepalivescount(int count, Port *port)
    1829             : {
    1830        2346 :     if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
    1831        1830 :         return STATUS_OK;
    1832             : 
    1833             : #ifdef TCP_KEEPCNT
    1834         516 :     if (count == port->keepalives_count)
    1835         516 :         return STATUS_OK;
    1836             : 
    1837           0 :     if (port->default_keepalives_count <= 0)
    1838             :     {
    1839           0 :         if (pq_getkeepalivescount(port) < 0)
    1840             :         {
    1841           0 :             if (count == 0)
    1842           0 :                 return STATUS_OK;   /* default is set but unknown */
    1843             :             else
    1844           0 :                 return STATUS_ERROR;
    1845             :         }
    1846             :     }
    1847             : 
    1848           0 :     if (count == 0)
    1849           0 :         count = port->default_keepalives_count;
    1850             : 
    1851           0 :     if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPCNT,
    1852             :                    (char *) &count, sizeof(count)) < 0)
    1853             :     {
    1854           0 :         ereport(LOG,
    1855             :                 (errmsg("%s(%s) failed: %m", "setsockopt", "TCP_KEEPCNT")));
    1856           0 :         return STATUS_ERROR;
    1857             :     }
    1858             : 
    1859           0 :     port->keepalives_count = count;
    1860             : #else
    1861             :     if (count != 0)
    1862             :     {
    1863             :         ereport(LOG,
    1864             :                 (errmsg("%s(%s) not supported", "setsockopt", "TCP_KEEPCNT")));
    1865             :         return STATUS_ERROR;
    1866             :     }
    1867             : #endif
    1868             : 
    1869           0 :     return STATUS_OK;
    1870             : }
    1871             : 
    1872             : int
    1873        3906 : pq_gettcpusertimeout(Port *port)
    1874             : {
    1875             : #ifdef TCP_USER_TIMEOUT
    1876        3906 :     if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
    1877        3906 :         return 0;
    1878             : 
    1879           0 :     if (port->tcp_user_timeout != 0)
    1880           0 :         return port->tcp_user_timeout;
    1881             : 
    1882           0 :     if (port->default_tcp_user_timeout == 0)
    1883             :     {
    1884           0 :         socklen_t   size = sizeof(port->default_tcp_user_timeout);
    1885             : 
    1886           0 :         if (getsockopt(port->sock, IPPROTO_TCP, TCP_USER_TIMEOUT,
    1887           0 :                        (char *) &port->default_tcp_user_timeout,
    1888             :                        &size) < 0)
    1889             :         {
    1890           0 :             ereport(LOG,
    1891             :                     (errmsg("%s(%s) failed: %m", "getsockopt", "TCP_USER_TIMEOUT")));
    1892           0 :             port->default_tcp_user_timeout = -1; /* don't know */
    1893             :         }
    1894             :     }
    1895             : 
    1896           0 :     return port->default_tcp_user_timeout;
    1897             : #else
    1898             :     return 0;
    1899             : #endif
    1900             : }
    1901             : 
    1902             : int
    1903        2346 : pq_settcpusertimeout(int timeout, Port *port)
    1904             : {
    1905        2346 :     if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
    1906        1830 :         return STATUS_OK;
    1907             : 
    1908             : #ifdef TCP_USER_TIMEOUT
    1909         516 :     if (timeout == port->tcp_user_timeout)
    1910         516 :         return STATUS_OK;
    1911             : 
    1912           0 :     if (port->default_tcp_user_timeout <= 0)
    1913             :     {
    1914           0 :         if (pq_gettcpusertimeout(port) < 0)
    1915             :         {
    1916           0 :             if (timeout == 0)
    1917           0 :                 return STATUS_OK;   /* default is set but unknown */
    1918             :             else
    1919           0 :                 return STATUS_ERROR;
    1920             :         }
    1921             :     }
    1922             : 
    1923           0 :     if (timeout == 0)
    1924           0 :         timeout = port->default_tcp_user_timeout;
    1925             : 
    1926           0 :     if (setsockopt(port->sock, IPPROTO_TCP, TCP_USER_TIMEOUT,
    1927             :                    (char *) &timeout, sizeof(timeout)) < 0)
    1928             :     {
    1929           0 :         ereport(LOG,
    1930             :                 (errmsg("%s(%s) failed: %m", "setsockopt", "TCP_USER_TIMEOUT")));
    1931           0 :         return STATUS_ERROR;
    1932             :     }
    1933             : 
    1934           0 :     port->tcp_user_timeout = timeout;
    1935             : #else
    1936             :     if (timeout != 0)
    1937             :     {
    1938             :         ereport(LOG,
    1939             :                 (errmsg("%s(%s) not supported", "setsockopt", "TCP_USER_TIMEOUT")));
    1940             :         return STATUS_ERROR;
    1941             :     }
    1942             : #endif
    1943             : 
    1944           0 :     return STATUS_OK;
    1945             : }
    1946             : 
    1947             : /*
    1948             :  * GUC assign_hook for tcp_keepalives_idle
    1949             :  */
    1950             : void
    1951        1830 : assign_tcp_keepalives_idle(int newval, void *extra)
    1952             : {
    1953             :     /*
    1954             :      * The kernel API provides no way to test a value without setting it; and
    1955             :      * once we set it we might fail to unset it.  So there seems little point
    1956             :      * in fully implementing the check-then-assign GUC API for these
    1957             :      * variables.  Instead we just do the assignment on demand.
    1958             :      * pq_setkeepalivesidle reports any problems via ereport(LOG).
    1959             :      *
    1960             :      * This approach means that the GUC value might have little to do with the
    1961             :      * actual kernel value, so we use a show_hook that retrieves the kernel
    1962             :      * value rather than trusting GUC's copy.
    1963             :      */
    1964        1830 :     (void) pq_setkeepalivesidle(newval, MyProcPort);
    1965        1830 : }
    1966             : 
    1967             : /*
    1968             :  * GUC show_hook for tcp_keepalives_idle
    1969             :  */
    1970             : const char *
    1971        3906 : show_tcp_keepalives_idle(void)
    1972             : {
    1973             :     /* See comments in assign_tcp_keepalives_idle */
    1974             :     static char nbuf[16];
    1975             : 
    1976        3906 :     snprintf(nbuf, sizeof(nbuf), "%d", pq_getkeepalivesidle(MyProcPort));
    1977        3906 :     return nbuf;
    1978             : }
    1979             : 
    1980             : /*
    1981             :  * GUC assign_hook for tcp_keepalives_interval
    1982             :  */
    1983             : void
    1984        1830 : assign_tcp_keepalives_interval(int newval, void *extra)
    1985             : {
    1986             :     /* See comments in assign_tcp_keepalives_idle */
    1987        1830 :     (void) pq_setkeepalivesinterval(newval, MyProcPort);
    1988        1830 : }
    1989             : 
    1990             : /*
    1991             :  * GUC show_hook for tcp_keepalives_interval
    1992             :  */
    1993             : const char *
    1994        3906 : show_tcp_keepalives_interval(void)
    1995             : {
    1996             :     /* See comments in assign_tcp_keepalives_idle */
    1997             :     static char nbuf[16];
    1998             : 
    1999        3906 :     snprintf(nbuf, sizeof(nbuf), "%d", pq_getkeepalivesinterval(MyProcPort));
    2000        3906 :     return nbuf;
    2001             : }
    2002             : 
    2003             : /*
    2004             :  * GUC assign_hook for tcp_keepalives_count
    2005             :  */
    2006             : void
    2007        1830 : assign_tcp_keepalives_count(int newval, void *extra)
    2008             : {
    2009             :     /* See comments in assign_tcp_keepalives_idle */
    2010        1830 :     (void) pq_setkeepalivescount(newval, MyProcPort);
    2011        1830 : }
    2012             : 
    2013             : /*
    2014             :  * GUC show_hook for tcp_keepalives_count
    2015             :  */
    2016             : const char *
    2017        3906 : show_tcp_keepalives_count(void)
    2018             : {
    2019             :     /* See comments in assign_tcp_keepalives_idle */
    2020             :     static char nbuf[16];
    2021             : 
    2022        3906 :     snprintf(nbuf, sizeof(nbuf), "%d", pq_getkeepalivescount(MyProcPort));
    2023        3906 :     return nbuf;
    2024             : }
    2025             : 
    2026             : /*
    2027             :  * GUC assign_hook for tcp_user_timeout
    2028             :  */
    2029             : void
    2030        1830 : assign_tcp_user_timeout(int newval, void *extra)
    2031             : {
    2032             :     /* See comments in assign_tcp_keepalives_idle */
    2033        1830 :     (void) pq_settcpusertimeout(newval, MyProcPort);
    2034        1830 : }
    2035             : 
    2036             : /*
    2037             :  * GUC show_hook for tcp_user_timeout
    2038             :  */
    2039             : const char *
    2040        3906 : show_tcp_user_timeout(void)
    2041             : {
    2042             :     /* See comments in assign_tcp_keepalives_idle */
    2043             :     static char nbuf[16];
    2044             : 
    2045        3906 :     snprintf(nbuf, sizeof(nbuf), "%d", pq_gettcpusertimeout(MyProcPort));
    2046        3906 :     return nbuf;
    2047             : }
    2048             : 
    2049             : /*
    2050             :  * Check if the client is still connected.
    2051             :  */
    2052             : bool
    2053           0 : pq_check_connection(void)
    2054             : {
    2055             :     WaitEvent   events[FeBeWaitSetNEvents];
    2056             :     int         rc;
    2057             : 
    2058             :     /*
    2059             :      * It's OK to modify the socket event filter without restoring, because
    2060             :      * all FeBeWaitSet socket wait sites do the same.
    2061             :      */
    2062           0 :     ModifyWaitEvent(FeBeWaitSet, FeBeWaitSetSocketPos, WL_SOCKET_CLOSED, NULL);
    2063             : 
    2064           0 : retry:
    2065           0 :     rc = WaitEventSetWait(FeBeWaitSet, 0, events, lengthof(events), 0);
    2066           0 :     for (int i = 0; i < rc; ++i)
    2067             :     {
    2068           0 :         if (events[i].events & WL_SOCKET_CLOSED)
    2069           0 :             return false;
    2070           0 :         if (events[i].events & WL_LATCH_SET)
    2071             :         {
    2072             :             /*
    2073             :              * A latch event might be preventing other events from being
    2074             :              * reported.  Reset it and poll again.  No need to restore it
    2075             :              * because no code should expect latches to survive across
    2076             :              * CHECK_FOR_INTERRUPTS().
    2077             :              */
    2078           0 :             ResetLatch(MyLatch);
    2079           0 :             goto retry;
    2080             :         }
    2081             :     }
    2082             : 
    2083           0 :     return true;
    2084             : }

Generated by: LCOV version 1.14