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

Generated by: LCOV version 1.14