PostgreSQL Source Code  git master
pqcomm.c File Reference
#include "postgres.h"
#include <signal.h>
#include <fcntl.h>
#include <grp.h>
#include <unistd.h>
#include <sys/file.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <netdb.h>
#include <netinet/in.h>
#include <utime.h>
#include "common/ip.h"
#include "libpq/libpq.h"
#include "miscadmin.h"
#include "port/pg_bswap.h"
#include "storage/ipc.h"
#include "utils/guc.h"
#include "utils/memutils.h"
Include dependency graph for pqcomm.c:

Go to the source code of this file.

Macros

#define PQ_SEND_BUFFER_SIZE   8192
 
#define PQ_RECV_BUFFER_SIZE   8192
 

Functions

static void socket_comm_reset (void)
 
static void socket_close (int code, Datum arg)
 
static void socket_set_nonblocking (bool nonblocking)
 
static int socket_flush (void)
 
static int socket_flush_if_writable (void)
 
static bool socket_is_send_pending (void)
 
static int socket_putmessage (char msgtype, const char *s, size_t len)
 
static void socket_putmessage_noblock (char msgtype, const char *s, size_t len)
 
static int internal_putbytes (const char *s, size_t len)
 
static int internal_flush (void)
 
void pq_init (void)
 
int StreamServerPort (int family, const char *hostName, unsigned short portNumber, const char *unixSocketDir, pgsocket ListenSocket[], int MaxListen)
 
int StreamConnection (pgsocket server_fd, Port *port)
 
void StreamClose (pgsocket sock)
 
void TouchSocketFiles (void)
 
void RemoveSocketFiles (void)
 
static int pq_recvbuf (void)
 
int pq_getbyte (void)
 
int pq_peekbyte (void)
 
int pq_getbyte_if_available (unsigned char *c)
 
int pq_getbytes (char *s, size_t len)
 
static int pq_discardbytes (size_t len)
 
void pq_startmsgread (void)
 
void pq_endmsgread (void)
 
bool pq_is_reading_msg (void)
 
int pq_getmessage (StringInfo s, int maxlen)
 
int pq_putmessage_v2 (char msgtype, const char *s, size_t len)
 
int pq_getkeepalivesidle (Port *port)
 
int pq_setkeepalivesidle (int idle, Port *port)
 
int pq_getkeepalivesinterval (Port *port)
 
int pq_setkeepalivesinterval (int interval, Port *port)
 
int pq_getkeepalivescount (Port *port)
 
int pq_setkeepalivescount (int count, Port *port)
 
int pq_gettcpusertimeout (Port *port)
 
int pq_settcpusertimeout (int timeout, Port *port)
 
bool pq_check_connection (void)
 

Variables

int Unix_socket_permissions
 
char * Unix_socket_group
 
static Listsock_paths = NIL
 
static char * PqSendBuffer
 
static int PqSendBufferSize
 
static int PqSendPointer
 
static int PqSendStart
 
static char PqRecvBuffer [PQ_RECV_BUFFER_SIZE]
 
static int PqRecvPointer
 
static int PqRecvLength
 
static bool PqCommBusy
 
static bool PqCommReadingMsg
 
static const PQcommMethods PqCommSocketMethods
 
const PQcommMethodsPqCommMethods = &PqCommSocketMethods
 
WaitEventSetFeBeWaitSet
 

Macro Definition Documentation

◆ PQ_RECV_BUFFER_SIZE

#define PQ_RECV_BUFFER_SIZE   8192

Definition at line 122 of file pqcomm.c.

Referenced by pq_recvbuf().

◆ PQ_SEND_BUFFER_SIZE

#define PQ_SEND_BUFFER_SIZE   8192

Definition at line 121 of file pqcomm.c.

Referenced by pq_init(), and StreamConnection().

Function Documentation

◆ internal_flush()

static int internal_flush ( void  )
static

Definition at line 1341 of file pqcomm.c.

References ClientConnectionLost, COMMERROR, EAGAIN, EINTR, ereport, errcode_for_socket_access(), errmsg(), EWOULDBLOCK, InterruptPending, MyProcPort, PqSendBuffer, PqSendPointer, PqSendStart, and secure_write().

Referenced by internal_putbytes(), socket_flush(), and socket_flush_if_writable().

1342 {
1343  static int last_reported_send_errno = 0;
1344 
1345  char *bufptr = PqSendBuffer + PqSendStart;
1346  char *bufend = PqSendBuffer + PqSendPointer;
1347 
1348  while (bufptr < bufend)
1349  {
1350  int r;
1351 
1352  r = secure_write(MyProcPort, bufptr, bufend - bufptr);
1353 
1354  if (r <= 0)
1355  {
1356  if (errno == EINTR)
1357  continue; /* Ok if we were interrupted */
1358 
1359  /*
1360  * Ok if no data writable without blocking, and the socket is in
1361  * non-blocking mode.
1362  */
1363  if (errno == EAGAIN ||
1364  errno == EWOULDBLOCK)
1365  {
1366  return 0;
1367  }
1368 
1369  /*
1370  * Careful: an ereport() that tries to write to the client would
1371  * cause recursion to here, leading to stack overflow and core
1372  * dump! This message must go *only* to the postmaster log.
1373  *
1374  * If a client disconnects while we're in the midst of output, we
1375  * might write quite a bit of data before we get to a safe query
1376  * abort point. So, suppress duplicate log messages.
1377  */
1378  if (errno != last_reported_send_errno)
1379  {
1380  last_reported_send_errno = errno;
1383  errmsg("could not send data to client: %m")));
1384  }
1385 
1386  /*
1387  * We drop the buffered data anyway so that processing can
1388  * continue, even though we'll probably quit soon. We also set a
1389  * flag that'll cause the next CHECK_FOR_INTERRUPTS to terminate
1390  * the connection.
1391  */
1392  PqSendStart = PqSendPointer = 0;
1394  InterruptPending = 1;
1395  return EOF;
1396  }
1397 
1398  last_reported_send_errno = 0; /* reset after any successful send */
1399  bufptr += r;
1400  PqSendStart += r;
1401  }
1402 
1403  PqSendStart = PqSendPointer = 0;
1404  return 0;
1405 }
struct Port * MyProcPort
Definition: globals.c:46
#define EAGAIN
Definition: win32_port.h:341
static char * PqSendBuffer
Definition: pqcomm.c:124
static int PqSendStart
Definition: pqcomm.c:127
static int PqSendPointer
Definition: pqcomm.c:126
#define COMMERROR
Definition: elog.h:30
volatile sig_atomic_t ClientConnectionLost
Definition: globals.c:34
ssize_t secure_write(Port *port, void *ptr, size_t len)
Definition: be-secure.c:260
int errcode_for_socket_access(void)
Definition: elog.c:792
#define ereport(elevel,...)
Definition: elog.h:157
volatile sig_atomic_t InterruptPending
Definition: globals.c:30
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define EWOULDBLOCK
Definition: win32_port.h:349
#define EINTR
Definition: win32_port.h:343

◆ internal_putbytes()

static int internal_putbytes ( const char *  s,
size_t  len 
)
static

Definition at line 1288 of file pqcomm.c.

References internal_flush(), PqSendBuffer, PqSendBufferSize, PqSendPointer, and socket_set_nonblocking().

Referenced by pq_putmessage_v2(), and socket_putmessage().

1289 {
1290  size_t amount;
1291 
1292  while (len > 0)
1293  {
1294  /* If buffer is full, then flush it out */
1296  {
1297  socket_set_nonblocking(false);
1298  if (internal_flush())
1299  return EOF;
1300  }
1301  amount = PqSendBufferSize - PqSendPointer;
1302  if (amount > len)
1303  amount = len;
1304  memcpy(PqSendBuffer + PqSendPointer, s, amount);
1305  PqSendPointer += amount;
1306  s += amount;
1307  len -= amount;
1308  }
1309  return 0;
1310 }
static char * PqSendBuffer
Definition: pqcomm.c:124
static int internal_flush(void)
Definition: pqcomm.c:1341
static void socket_set_nonblocking(bool nonblocking)
Definition: pqcomm.c:916
static int PqSendPointer
Definition: pqcomm.c:126
static int PqSendBufferSize
Definition: pqcomm.c:125

◆ pq_check_connection()

bool pq_check_connection ( void  )

Definition at line 1933 of file pqcomm.c.

References COMMERROR, ereport, errcode_for_socket_access(), errmsg(), MyProcPort, and Port::sock.

Referenced by ProcessInterrupts().

1934 {
1935 #if defined(POLLRDHUP)
1936  /*
1937  * POLLRDHUP is a Linux extension to poll(2) to detect sockets closed by
1938  * the other end. We don't have a portable way to do that without
1939  * actually trying to read or write data on other systems. We don't want
1940  * to read because that would be confused by pipelined queries and COPY
1941  * data. Perhaps in future we'll try to write a heartbeat message instead.
1942  */
1943  struct pollfd pollfd;
1944  int rc;
1945 
1946  pollfd.fd = MyProcPort->sock;
1947  pollfd.events = POLLOUT | POLLIN | POLLRDHUP;
1948  pollfd.revents = 0;
1949 
1950  rc = poll(&pollfd, 1, 0);
1951 
1952  if (rc < 0)
1953  {
1956  errmsg("could not poll socket: %m")));
1957  return false;
1958  }
1959  else if (rc == 1 && (pollfd.revents & (POLLHUP | POLLRDHUP)))
1960  return false;
1961 #endif
1962 
1963  return true;
1964 }
struct Port * MyProcPort
Definition: globals.c:46
pgsocket sock
Definition: libpq-be.h:127
#define COMMERROR
Definition: elog.h:30
int errcode_for_socket_access(void)
Definition: elog.c:792
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ pq_discardbytes()

static int pq_discardbytes ( size_t  len)
static

Definition at line 1122 of file pqcomm.c.

References Assert, pq_recvbuf(), PqCommReadingMsg, PqRecvLength, and PqRecvPointer.

Referenced by pq_getmessage().

1123 {
1124  size_t amount;
1125 
1127 
1128  while (len > 0)
1129  {
1130  while (PqRecvPointer >= PqRecvLength)
1131  {
1132  if (pq_recvbuf()) /* If nothing in buffer, then recv some */
1133  return EOF; /* Failed to recv data */
1134  }
1135  amount = PqRecvLength - PqRecvPointer;
1136  if (amount > len)
1137  amount = len;
1138  PqRecvPointer += amount;
1139  len -= amount;
1140  }
1141  return 0;
1142 }
static int PqRecvLength
Definition: pqcomm.c:131
static int pq_recvbuf(void)
Definition: pqcomm.c:933
#define Assert(condition)
Definition: c.h:804
static int PqRecvPointer
Definition: pqcomm.c:130
static bool PqCommReadingMsg
Definition: pqcomm.c:137

◆ pq_endmsgread()

void pq_endmsgread ( void  )

Definition at line 1176 of file pqcomm.c.

References Assert, and PqCommReadingMsg.

Referenced by ProcessRepliesIfAny(), and ProcessStartupPacket().

1177 {
1179 
1180  PqCommReadingMsg = false;
1181 }
#define Assert(condition)
Definition: c.h:804
static bool PqCommReadingMsg
Definition: pqcomm.c:137

◆ pq_getbyte()

int pq_getbyte ( void  )

Definition at line 994 of file pqcomm.c.

References Assert, pq_recvbuf(), PqCommReadingMsg, PqRecvBuffer, PqRecvLength, and PqRecvPointer.

Referenced by CheckSCRAMAuth(), CopyGetData(), recv_password_packet(), and SocketBackend().

995 {
997 
998  while (PqRecvPointer >= PqRecvLength)
999  {
1000  if (pq_recvbuf()) /* If nothing in buffer, then recv some */
1001  return EOF; /* Failed to recv data */
1002  }
1003  return (unsigned char) PqRecvBuffer[PqRecvPointer++];
1004 }
static char PqRecvBuffer[PQ_RECV_BUFFER_SIZE]
Definition: pqcomm.c:129
static int PqRecvLength
Definition: pqcomm.c:131
static int pq_recvbuf(void)
Definition: pqcomm.c:933
#define Assert(condition)
Definition: c.h:804
static int PqRecvPointer
Definition: pqcomm.c:130
static bool PqCommReadingMsg
Definition: pqcomm.c:137

◆ pq_getbyte_if_available()

int pq_getbyte_if_available ( unsigned char *  c)

Definition at line 1034 of file pqcomm.c.

References Assert, COMMERROR, EAGAIN, EINTR, ereport, errcode_for_socket_access(), errmsg(), EWOULDBLOCK, MyProcPort, PqCommReadingMsg, PqRecvBuffer, PqRecvLength, PqRecvPointer, secure_read(), and socket_set_nonblocking().

Referenced by ProcessRepliesIfAny().

1035 {
1036  int r;
1037 
1039 
1041  {
1043  return 1;
1044  }
1045 
1046  /* Put the socket into non-blocking mode */
1047  socket_set_nonblocking(true);
1048 
1049  r = secure_read(MyProcPort, c, 1);
1050  if (r < 0)
1051  {
1052  /*
1053  * Ok if no data available without blocking or interrupted (though
1054  * EINTR really shouldn't happen with a non-blocking socket). Report
1055  * other errors.
1056  */
1057  if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
1058  r = 0;
1059  else
1060  {
1061  /*
1062  * Careful: an ereport() that tries to write to the client would
1063  * cause recursion to here, leading to stack overflow and core
1064  * dump! This message must go *only* to the postmaster log.
1065  */
1068  errmsg("could not receive data from client: %m")));
1069  r = EOF;
1070  }
1071  }
1072  else if (r == 0)
1073  {
1074  /* EOF detected */
1075  r = EOF;
1076  }
1077 
1078  return r;
1079 }
static char PqRecvBuffer[PQ_RECV_BUFFER_SIZE]
Definition: pqcomm.c:129
struct Port * MyProcPort
Definition: globals.c:46
#define EAGAIN
Definition: win32_port.h:341
ssize_t secure_read(Port *port, void *ptr, size_t len)
Definition: be-secure.c:147
static void socket_set_nonblocking(bool nonblocking)
Definition: pqcomm.c:916
static int PqRecvLength
Definition: pqcomm.c:131
char * c
#define COMMERROR
Definition: elog.h:30
int errcode_for_socket_access(void)
Definition: elog.c:792
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
int errmsg(const char *fmt,...)
Definition: elog.c:909
static int PqRecvPointer
Definition: pqcomm.c:130
#define EWOULDBLOCK
Definition: win32_port.h:349
static bool PqCommReadingMsg
Definition: pqcomm.c:137
#define EINTR
Definition: win32_port.h:343

◆ pq_getbytes()

int pq_getbytes ( char *  s,
size_t  len 
)

Definition at line 1088 of file pqcomm.c.

References Assert, pq_recvbuf(), PqCommReadingMsg, PqRecvBuffer, PqRecvLength, and PqRecvPointer.

Referenced by pq_getmessage(), and ProcessStartupPacket().

1089 {
1090  size_t amount;
1091 
1093 
1094  while (len > 0)
1095  {
1096  while (PqRecvPointer >= PqRecvLength)
1097  {
1098  if (pq_recvbuf()) /* If nothing in buffer, then recv some */
1099  return EOF; /* Failed to recv data */
1100  }
1101  amount = PqRecvLength - PqRecvPointer;
1102  if (amount > len)
1103  amount = len;
1104  memcpy(s, PqRecvBuffer + PqRecvPointer, amount);
1105  PqRecvPointer += amount;
1106  s += amount;
1107  len -= amount;
1108  }
1109  return 0;
1110 }
static char PqRecvBuffer[PQ_RECV_BUFFER_SIZE]
Definition: pqcomm.c:129
static int PqRecvLength
Definition: pqcomm.c:131
static int pq_recvbuf(void)
Definition: pqcomm.c:933
#define Assert(condition)
Definition: c.h:804
static int PqRecvPointer
Definition: pqcomm.c:130
static bool PqCommReadingMsg
Definition: pqcomm.c:137

◆ pq_getkeepalivescount()

int pq_getkeepalivescount ( Port port)

Definition at line 1780 of file pqcomm.c.

References SockAddr::addr, Port::default_keepalives_count, ereport, errmsg(), IS_AF_UNIX, Port::keepalives_count, Port::laddr, LOG, and Port::sock.

Referenced by pq_setkeepalivescount(), and show_tcp_keepalives_count().

1781 {
1782 #ifdef TCP_KEEPCNT
1783  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1784  return 0;
1785 
1786  if (port->keepalives_count != 0)
1787  return port->keepalives_count;
1788 
1789  if (port->default_keepalives_count == 0)
1790  {
1791  ACCEPT_TYPE_ARG3 size = sizeof(port->default_keepalives_count);
1792 
1793  if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPCNT,
1794  (char *) &port->default_keepalives_count,
1795  &size) < 0)
1796  {
1797  ereport(LOG,
1798  (errmsg("%s(%s) failed: %m", "getsockopt", "TCP_KEEPCNT")));
1799  port->default_keepalives_count = -1; /* don't know */
1800  }
1801  }
1802 
1803  return port->default_keepalives_count;
1804 #else
1805  return 0;
1806 #endif
1807 }
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define LOG
Definition: elog.h:26
int default_keepalives_count
Definition: libpq-be.h:184
pgsocket sock
Definition: libpq-be.h:127
int keepalives_count
Definition: libpq-be.h:188
#define IS_AF_UNIX(fam)
Definition: ip.h:24
SockAddr laddr
Definition: libpq-be.h:130
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ pq_getkeepalivesidle()

int pq_getkeepalivesidle ( Port port)

Definition at line 1611 of file pqcomm.c.

References SockAddr::addr, Port::default_keepalives_idle, ereport, errmsg(), IS_AF_UNIX, Port::keepalives_idle, Port::laddr, LOG, and Port::sock.

Referenced by pq_setkeepalivesidle(), and show_tcp_keepalives_idle().

1612 {
1613 #if defined(PG_TCP_KEEPALIVE_IDLE) || defined(SIO_KEEPALIVE_VALS)
1614  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1615  return 0;
1616 
1617  if (port->keepalives_idle != 0)
1618  return port->keepalives_idle;
1619 
1620  if (port->default_keepalives_idle == 0)
1621  {
1622 #ifndef WIN32
1623  ACCEPT_TYPE_ARG3 size = sizeof(port->default_keepalives_idle);
1624 
1625  if (getsockopt(port->sock, IPPROTO_TCP, PG_TCP_KEEPALIVE_IDLE,
1626  (char *) &port->default_keepalives_idle,
1627  &size) < 0)
1628  {
1629  ereport(LOG,
1630  (errmsg("%s(%s) failed: %m", "getsockopt", PG_TCP_KEEPALIVE_IDLE_STR)));
1631  port->default_keepalives_idle = -1; /* don't know */
1632  }
1633 #else /* WIN32 */
1634  /* We can't get the defaults on Windows, so return "don't know" */
1635  port->default_keepalives_idle = -1;
1636 #endif /* WIN32 */
1637  }
1638 
1639  return port->default_keepalives_idle;
1640 #else
1641  return 0;
1642 #endif
1643 }
int keepalives_idle
Definition: libpq-be.h:186
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define LOG
Definition: elog.h:26
pgsocket sock
Definition: libpq-be.h:127
#define IS_AF_UNIX(fam)
Definition: ip.h:24
SockAddr laddr
Definition: libpq-be.h:130
int default_keepalives_idle
Definition: libpq-be.h:182
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ pq_getkeepalivesinterval()

int pq_getkeepalivesinterval ( Port port)

Definition at line 1696 of file pqcomm.c.

References SockAddr::addr, Port::default_keepalives_interval, ereport, errmsg(), IS_AF_UNIX, Port::keepalives_interval, Port::laddr, LOG, and Port::sock.

Referenced by pq_setkeepalivesinterval(), and show_tcp_keepalives_interval().

1697 {
1698 #if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
1699  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1700  return 0;
1701 
1702  if (port->keepalives_interval != 0)
1703  return port->keepalives_interval;
1704 
1705  if (port->default_keepalives_interval == 0)
1706  {
1707 #ifndef WIN32
1708  ACCEPT_TYPE_ARG3 size = sizeof(port->default_keepalives_interval);
1709 
1710  if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPINTVL,
1711  (char *) &port->default_keepalives_interval,
1712  &size) < 0)
1713  {
1714  ereport(LOG,
1715  (errmsg("%s(%s) failed: %m", "getsockopt", "TCP_KEEPINTVL")));
1716  port->default_keepalives_interval = -1; /* don't know */
1717  }
1718 #else
1719  /* We can't get the defaults on Windows, so return "don't know" */
1720  port->default_keepalives_interval = -1;
1721 #endif /* WIN32 */
1722  }
1723 
1724  return port->default_keepalives_interval;
1725 #else
1726  return 0;
1727 #endif
1728 }
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define LOG
Definition: elog.h:26
int default_keepalives_interval
Definition: libpq-be.h:183
pgsocket sock
Definition: libpq-be.h:127
#define IS_AF_UNIX(fam)
Definition: ip.h:24
SockAddr laddr
Definition: libpq-be.h:130
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
int keepalives_interval
Definition: libpq-be.h:187

◆ pq_getmessage()

int pq_getmessage ( StringInfo  s,
int  maxlen 
)

Definition at line 1214 of file pqcomm.c.

References Assert, COMMERROR, StringInfoData::data, enlargeStringInfo(), ereport, errcode(), errmsg(), StringInfoData::len, PG_CATCH, PG_END_TRY, pg_ntoh32, PG_RE_THROW, PG_TRY, pq_discardbytes(), pq_getbytes(), PqCommReadingMsg, and resetStringInfo().

Referenced by CheckSCRAMAuth(), CopyGetData(), ProcessRepliesIfAny(), recv_password_packet(), and SocketBackend().

1215 {
1216  int32 len;
1217 
1219 
1220  resetStringInfo(s);
1221 
1222  /* Read message length word */
1223  if (pq_getbytes((char *) &len, 4) == EOF)
1224  {
1226  (errcode(ERRCODE_PROTOCOL_VIOLATION),
1227  errmsg("unexpected EOF within message length word")));
1228  return EOF;
1229  }
1230 
1231  len = pg_ntoh32(len);
1232 
1233  if (len < 4 || len > maxlen)
1234  {
1236  (errcode(ERRCODE_PROTOCOL_VIOLATION),
1237  errmsg("invalid message length")));
1238  return EOF;
1239  }
1240 
1241  len -= 4; /* discount length itself */
1242 
1243  if (len > 0)
1244  {
1245  /*
1246  * Allocate space for message. If we run out of room (ridiculously
1247  * large message), we will elog(ERROR), but we want to discard the
1248  * message body so as not to lose communication sync.
1249  */
1250  PG_TRY();
1251  {
1252  enlargeStringInfo(s, len);
1253  }
1254  PG_CATCH();
1255  {
1256  if (pq_discardbytes(len) == EOF)
1258  (errcode(ERRCODE_PROTOCOL_VIOLATION),
1259  errmsg("incomplete message from client")));
1260 
1261  /* we discarded the rest of the message so we're back in sync. */
1262  PqCommReadingMsg = false;
1263  PG_RE_THROW();
1264  }
1265  PG_END_TRY();
1266 
1267  /* And grab the message */
1268  if (pq_getbytes(s->data, len) == EOF)
1269  {
1271  (errcode(ERRCODE_PROTOCOL_VIOLATION),
1272  errmsg("incomplete message from client")));
1273  return EOF;
1274  }
1275  s->len = len;
1276  /* Place a trailing null per StringInfo convention */
1277  s->data[len] = '\0';
1278  }
1279 
1280  /* finished reading the message. */
1281  PqCommReadingMsg = false;
1282 
1283  return 0;
1284 }
static int pq_discardbytes(size_t len)
Definition: pqcomm.c:1122
int errcode(int sqlerrcode)
Definition: elog.c:698
signed int int32
Definition: c.h:429
#define pg_ntoh32(x)
Definition: pg_bswap.h:125
int pq_getbytes(char *s, size_t len)
Definition: pqcomm.c:1088
#define COMMERROR
Definition: elog.h:30
void enlargeStringInfo(StringInfo str, int needed)
Definition: stringinfo.c:283
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:75
#define ereport(elevel,...)
Definition: elog.h:157
#define PG_CATCH()
Definition: elog.h:323
#define Assert(condition)
Definition: c.h:804
#define PG_RE_THROW()
Definition: elog.h:354
int errmsg(const char *fmt,...)
Definition: elog.c:909
static bool PqCommReadingMsg
Definition: pqcomm.c:137
#define PG_TRY()
Definition: elog.h:313
#define PG_END_TRY()
Definition: elog.h:338

◆ pq_gettcpusertimeout()

int pq_gettcpusertimeout ( Port port)

Definition at line 1855 of file pqcomm.c.

References SockAddr::addr, Port::default_tcp_user_timeout, ereport, errmsg(), IS_AF_UNIX, Port::laddr, LOG, Port::sock, and Port::tcp_user_timeout.

Referenced by pq_settcpusertimeout(), and show_tcp_user_timeout().

1856 {
1857 #ifdef TCP_USER_TIMEOUT
1858  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1859  return 0;
1860 
1861  if (port->tcp_user_timeout != 0)
1862  return port->tcp_user_timeout;
1863 
1864  if (port->default_tcp_user_timeout == 0)
1865  {
1866  ACCEPT_TYPE_ARG3 size = sizeof(port->default_tcp_user_timeout);
1867 
1868  if (getsockopt(port->sock, IPPROTO_TCP, TCP_USER_TIMEOUT,
1869  (char *) &port->default_tcp_user_timeout,
1870  &size) < 0)
1871  {
1872  ereport(LOG,
1873  (errmsg("%s(%s) failed: %m", "getsockopt", "TCP_USER_TIMEOUT")));
1874  port->default_tcp_user_timeout = -1; /* don't know */
1875  }
1876  }
1877 
1878  return port->default_tcp_user_timeout;
1879 #else
1880  return 0;
1881 #endif
1882 }
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define LOG
Definition: elog.h:26
pgsocket sock
Definition: libpq-be.h:127
#define IS_AF_UNIX(fam)
Definition: ip.h:24
int tcp_user_timeout
Definition: libpq-be.h:189
SockAddr laddr
Definition: libpq-be.h:130
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
int default_tcp_user_timeout
Definition: libpq-be.h:185

◆ pq_init()

void pq_init ( void  )

Definition at line 176 of file pqcomm.c.

References AddWaitEventToSet(), Assert, COMMERROR, CreateWaitEventSet(), ereport, errmsg(), FeBeWaitSetLatchPos, FeBeWaitSetSocketPos, MemoryContextAlloc(), MyLatch, MyProcPort, on_proc_exit(), pg_set_noblock(), PG_USED_FOR_ASSERTS_ONLY, PGINVALID_SOCKET, PQ_SEND_BUFFER_SIZE, PqCommBusy, PqCommReadingMsg, PqRecvLength, PqRecvPointer, PqSendBuffer, PqSendBufferSize, PqSendPointer, PqSendStart, Port::sock, socket_close(), TopMemoryContext, WL_LATCH_SET, WL_POSTMASTER_DEATH, and WL_SOCKET_WRITEABLE.

Referenced by BackendInitialize().

177 {
178  int socket_pos PG_USED_FOR_ASSERTS_ONLY;
179  int latch_pos PG_USED_FOR_ASSERTS_ONLY;
180 
181  /* initialize state variables */
185  PqCommBusy = false;
186  PqCommReadingMsg = false;
187 
188  /* set up process-exit hook to close the socket */
190 
191  /*
192  * In backends (as soon as forked) we operate the underlying socket in
193  * nonblocking mode and use latches to implement blocking semantics if
194  * needed. That allows us to provide safely interruptible reads and
195  * writes.
196  *
197  * Use COMMERROR on failure, because ERROR would try to send the error to
198  * the client, which might require changing the mode again, leading to
199  * infinite recursion.
200  */
201 #ifndef WIN32
204  (errmsg("could not set socket to nonblocking mode: %m")));
205 #endif
206 
209  MyProcPort->sock, NULL, NULL);
211  MyLatch, NULL);
213  NULL, NULL);
214 
215  /*
216  * The event positions match the order we added them, but let's sanity
217  * check them to be sure.
218  */
219  Assert(socket_pos == FeBeWaitSetSocketPos);
220  Assert(latch_pos == FeBeWaitSetLatchPos);
221 }
#define FeBeWaitSetSocketPos
Definition: libpq.h:63
#define WL_SOCKET_WRITEABLE
Definition: latch.h:127
struct Port * MyProcPort
Definition: globals.c:46
int AddWaitEventToSet(WaitEventSet *set, uint32 events, pgsocket fd, Latch *latch, void *user_data)
Definition: latch.c:862
void on_proc_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:305
static char * PqSendBuffer
Definition: pqcomm.c:124
static int PqSendStart
Definition: pqcomm.c:127
#define FeBeWaitSetLatchPos
Definition: libpq.h:64
WaitEventSet * FeBeWaitSet
Definition: pqcomm.c:168
pgsocket sock
Definition: libpq-be.h:127
WaitEventSet * CreateWaitEventSet(MemoryContext context, int nevents)
Definition: latch.c:684
static int PqRecvLength
Definition: pqcomm.c:131
static int PqSendPointer
Definition: pqcomm.c:126
#define COMMERROR
Definition: elog.h:30
MemoryContext TopMemoryContext
Definition: mcxt.c:48
static int PqSendBufferSize
Definition: pqcomm.c:125
static bool PqCommBusy
Definition: pqcomm.c:136
#define WL_POSTMASTER_DEATH
Definition: latch.h:129
#define PGINVALID_SOCKET
Definition: port.h:33
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
bool pg_set_noblock(pgsocket sock)
Definition: noblock.c:25
static void socket_close(int code, Datum arg)
Definition: pqcomm.c:247
int errmsg(const char *fmt,...)
Definition: elog.c:909
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:863
struct Latch * MyLatch
Definition: globals.c:57
static int PqRecvPointer
Definition: pqcomm.c:130
static bool PqCommReadingMsg
Definition: pqcomm.c:137
#define WL_LATCH_SET
Definition: latch.h:125
#define PQ_SEND_BUFFER_SIZE
Definition: pqcomm.c:121
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:155

◆ pq_is_reading_msg()

bool pq_is_reading_msg ( void  )

Definition at line 1192 of file pqcomm.c.

References PqCommReadingMsg.

Referenced by PostgresMain().

1193 {
1194  return PqCommReadingMsg;
1195 }
static bool PqCommReadingMsg
Definition: pqcomm.c:137

◆ pq_peekbyte()

int pq_peekbyte ( void  )

Definition at line 1013 of file pqcomm.c.

References Assert, pq_recvbuf(), PqCommReadingMsg, PqRecvBuffer, PqRecvLength, and PqRecvPointer.

1014 {
1016 
1017  while (PqRecvPointer >= PqRecvLength)
1018  {
1019  if (pq_recvbuf()) /* If nothing in buffer, then recv some */
1020  return EOF; /* Failed to recv data */
1021  }
1022  return (unsigned char) PqRecvBuffer[PqRecvPointer];
1023 }
static char PqRecvBuffer[PQ_RECV_BUFFER_SIZE]
Definition: pqcomm.c:129
static int PqRecvLength
Definition: pqcomm.c:131
static int pq_recvbuf(void)
Definition: pqcomm.c:933
#define Assert(condition)
Definition: c.h:804
static int PqRecvPointer
Definition: pqcomm.c:130
static bool PqCommReadingMsg
Definition: pqcomm.c:137

◆ pq_putmessage_v2()

int pq_putmessage_v2 ( char  msgtype,
const char *  s,
size_t  len 
)

Definition at line 1540 of file pqcomm.c.

References Assert, ereport, errmsg(), internal_putbytes(), Port::keepalives_idle, Port::keepalives_interval, LOG, port, PqCommBusy, Port::sock, STATUS_ERROR, and STATUS_OK.

Referenced by send_message_to_frontend().

1541 {
1542  Assert(msgtype != 0);
1543 
1544  if (PqCommBusy)
1545  return 0;
1546  PqCommBusy = true;
1547  if (internal_putbytes(&msgtype, 1))
1548  goto fail;
1549 
1550  if (internal_putbytes(s, len))
1551  goto fail;
1552  PqCommBusy = false;
1553  return 0;
1554 
1555 fail:
1556  PqCommBusy = false;
1557  return EOF;
1558 }
static int internal_putbytes(const char *s, size_t len)
Definition: pqcomm.c:1288
static bool PqCommBusy
Definition: pqcomm.c:136
#define Assert(condition)
Definition: c.h:804

◆ pq_recvbuf()

static int pq_recvbuf ( void  )
static

Definition at line 933 of file pqcomm.c.

References COMMERROR, EINTR, ereport, errcode_for_socket_access(), errmsg(), MyProcPort, PQ_RECV_BUFFER_SIZE, PqRecvBuffer, PqRecvLength, PqRecvPointer, secure_read(), and socket_set_nonblocking().

Referenced by pq_discardbytes(), pq_getbyte(), pq_getbytes(), and pq_peekbyte().

934 {
935  if (PqRecvPointer > 0)
936  {
938  {
939  /* still some unread data, left-justify it in the buffer */
943  PqRecvPointer = 0;
944  }
945  else
947  }
948 
949  /* Ensure that we're in blocking mode */
950  socket_set_nonblocking(false);
951 
952  /* Can fill buffer from PqRecvLength and upwards */
953  for (;;)
954  {
955  int r;
956 
959 
960  if (r < 0)
961  {
962  if (errno == EINTR)
963  continue; /* Ok if interrupted */
964 
965  /*
966  * Careful: an ereport() that tries to write to the client would
967  * cause recursion to here, leading to stack overflow and core
968  * dump! This message must go *only* to the postmaster log.
969  */
972  errmsg("could not receive data from client: %m")));
973  return EOF;
974  }
975  if (r == 0)
976  {
977  /*
978  * EOF detected. We used to write a log message here, but it's
979  * better to expect the ultimate caller to do that.
980  */
981  return EOF;
982  }
983  /* r contains number of bytes read, so just incr length */
984  PqRecvLength += r;
985  return 0;
986  }
987 }
static char PqRecvBuffer[PQ_RECV_BUFFER_SIZE]
Definition: pqcomm.c:129
struct Port * MyProcPort
Definition: globals.c:46
ssize_t secure_read(Port *port, void *ptr, size_t len)
Definition: be-secure.c:147
static void socket_set_nonblocking(bool nonblocking)
Definition: pqcomm.c:916
static int PqRecvLength
Definition: pqcomm.c:131
#define COMMERROR
Definition: elog.h:30
int errcode_for_socket_access(void)
Definition: elog.c:792
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define PQ_RECV_BUFFER_SIZE
Definition: pqcomm.c:122
static int PqRecvPointer
Definition: pqcomm.c:130
#define EINTR
Definition: win32_port.h:343

◆ pq_setkeepalivescount()

int pq_setkeepalivescount ( int  count,
Port port 
)

Definition at line 1810 of file pqcomm.c.

References SockAddr::addr, Port::default_keepalives_count, ereport, errmsg(), IS_AF_UNIX, Port::keepalives_count, Port::laddr, LOG, pq_getkeepalivescount(), Port::sock, STATUS_ERROR, and STATUS_OK.

Referenced by assign_tcp_keepalives_count(), and StreamConnection().

1811 {
1812  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1813  return STATUS_OK;
1814 
1815 #ifdef TCP_KEEPCNT
1816  if (count == port->keepalives_count)
1817  return STATUS_OK;
1818 
1819  if (port->default_keepalives_count <= 0)
1820  {
1821  if (pq_getkeepalivescount(port) < 0)
1822  {
1823  if (count == 0)
1824  return STATUS_OK; /* default is set but unknown */
1825  else
1826  return STATUS_ERROR;
1827  }
1828  }
1829 
1830  if (count == 0)
1831  count = port->default_keepalives_count;
1832 
1833  if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPCNT,
1834  (char *) &count, sizeof(count)) < 0)
1835  {
1836  ereport(LOG,
1837  (errmsg("%s(%s) failed: %m", "setsockopt", "TCP_KEEPCNT")));
1838  return STATUS_ERROR;
1839  }
1840 
1841  port->keepalives_count = count;
1842 #else
1843  if (count != 0)
1844  {
1845  ereport(LOG,
1846  (errmsg("%s(%s) not supported", "setsockopt", "TCP_KEEPCNT")));
1847  return STATUS_ERROR;
1848  }
1849 #endif
1850 
1851  return STATUS_OK;
1852 }
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define STATUS_ERROR
Definition: c.h:1171
#define LOG
Definition: elog.h:26
int default_keepalives_count
Definition: libpq-be.h:184
pgsocket sock
Definition: libpq-be.h:127
int keepalives_count
Definition: libpq-be.h:188
#define IS_AF_UNIX(fam)
Definition: ip.h:24
#define STATUS_OK
Definition: c.h:1170
SockAddr laddr
Definition: libpq-be.h:130
#define ereport(elevel,...)
Definition: elog.h:157
int pq_getkeepalivescount(Port *port)
Definition: pqcomm.c:1780
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ pq_setkeepalivesidle()

int pq_setkeepalivesidle ( int  idle,
Port port 
)

Definition at line 1646 of file pqcomm.c.

References SockAddr::addr, Port::default_keepalives_idle, ereport, errmsg(), IS_AF_UNIX, Port::keepalives_idle, Port::keepalives_interval, Port::laddr, LOG, pq_getkeepalivesidle(), Port::sock, STATUS_ERROR, and STATUS_OK.

Referenced by assign_tcp_keepalives_idle(), and StreamConnection().

1647 {
1648  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1649  return STATUS_OK;
1650 
1651 /* check SIO_KEEPALIVE_VALS here, not just WIN32, as some toolchains lack it */
1652 #if defined(PG_TCP_KEEPALIVE_IDLE) || defined(SIO_KEEPALIVE_VALS)
1653  if (idle == port->keepalives_idle)
1654  return STATUS_OK;
1655 
1656 #ifndef WIN32
1657  if (port->default_keepalives_idle <= 0)
1658  {
1659  if (pq_getkeepalivesidle(port) < 0)
1660  {
1661  if (idle == 0)
1662  return STATUS_OK; /* default is set but unknown */
1663  else
1664  return STATUS_ERROR;
1665  }
1666  }
1667 
1668  if (idle == 0)
1669  idle = port->default_keepalives_idle;
1670 
1671  if (setsockopt(port->sock, IPPROTO_TCP, PG_TCP_KEEPALIVE_IDLE,
1672  (char *) &idle, sizeof(idle)) < 0)
1673  {
1674  ereport(LOG,
1675  (errmsg("%s(%s) failed: %m", "setsockopt", PG_TCP_KEEPALIVE_IDLE_STR)));
1676  return STATUS_ERROR;
1677  }
1678 
1679  port->keepalives_idle = idle;
1680 #else /* WIN32 */
1681  return pq_setkeepaliveswin32(port, idle, port->keepalives_interval);
1682 #endif
1683 #else
1684  if (idle != 0)
1685  {
1686  ereport(LOG,
1687  (errmsg("setting the keepalive idle time is not supported")));
1688  return STATUS_ERROR;
1689  }
1690 #endif
1691 
1692  return STATUS_OK;
1693 }
int keepalives_idle
Definition: libpq-be.h:186
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define STATUS_ERROR
Definition: c.h:1171
#define LOG
Definition: elog.h:26
pgsocket sock
Definition: libpq-be.h:127
#define IS_AF_UNIX(fam)
Definition: ip.h:24
#define STATUS_OK
Definition: c.h:1170
SockAddr laddr
Definition: libpq-be.h:130
int default_keepalives_idle
Definition: libpq-be.h:182
#define ereport(elevel,...)
Definition: elog.h:157
int pq_getkeepalivesidle(Port *port)
Definition: pqcomm.c:1611
int errmsg(const char *fmt,...)
Definition: elog.c:909
int keepalives_interval
Definition: libpq-be.h:187

◆ pq_setkeepalivesinterval()

int pq_setkeepalivesinterval ( int  interval,
Port port 
)

Definition at line 1731 of file pqcomm.c.

References SockAddr::addr, Port::default_keepalives_interval, ereport, errmsg(), IS_AF_UNIX, Port::keepalives_idle, Port::keepalives_interval, Port::laddr, LOG, pq_getkeepalivesinterval(), Port::sock, STATUS_ERROR, and STATUS_OK.

Referenced by assign_tcp_keepalives_interval(), and StreamConnection().

1732 {
1733  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1734  return STATUS_OK;
1735 
1736 #if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
1737  if (interval == port->keepalives_interval)
1738  return STATUS_OK;
1739 
1740 #ifndef WIN32
1741  if (port->default_keepalives_interval <= 0)
1742  {
1743  if (pq_getkeepalivesinterval(port) < 0)
1744  {
1745  if (interval == 0)
1746  return STATUS_OK; /* default is set but unknown */
1747  else
1748  return STATUS_ERROR;
1749  }
1750  }
1751 
1752  if (interval == 0)
1754 
1755  if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPINTVL,
1756  (char *) &interval, sizeof(interval)) < 0)
1757  {
1758  ereport(LOG,
1759  (errmsg("%s(%s) failed: %m", "setsockopt", "TCP_KEEPINTVL")));
1760  return STATUS_ERROR;
1761  }
1762 
1763  port->keepalives_interval = interval;
1764 #else /* WIN32 */
1765  return pq_setkeepaliveswin32(port, port->keepalives_idle, interval);
1766 #endif
1767 #else
1768  if (interval != 0)
1769  {
1770  ereport(LOG,
1771  (errmsg("%s(%s) not supported", "setsockopt", "TCP_KEEPINTVL")));
1772  return STATUS_ERROR;
1773  }
1774 #endif
1775 
1776  return STATUS_OK;
1777 }
int keepalives_idle
Definition: libpq-be.h:186
int pq_getkeepalivesinterval(Port *port)
Definition: pqcomm.c:1696
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define STATUS_ERROR
Definition: c.h:1171
#define LOG
Definition: elog.h:26
int default_keepalives_interval
Definition: libpq-be.h:183
pgsocket sock
Definition: libpq-be.h:127
#define IS_AF_UNIX(fam)
Definition: ip.h:24
#define STATUS_OK
Definition: c.h:1170
SockAddr laddr
Definition: libpq-be.h:130
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
int keepalives_interval
Definition: libpq-be.h:187

◆ pq_settcpusertimeout()

int pq_settcpusertimeout ( int  timeout,
Port port 
)

Definition at line 1885 of file pqcomm.c.

References SockAddr::addr, Port::default_tcp_user_timeout, ereport, errmsg(), IS_AF_UNIX, Port::laddr, LOG, pq_gettcpusertimeout(), Port::sock, STATUS_ERROR, STATUS_OK, and Port::tcp_user_timeout.

Referenced by assign_tcp_user_timeout(), and StreamConnection().

1886 {
1887  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1888  return STATUS_OK;
1889 
1890 #ifdef TCP_USER_TIMEOUT
1891  if (timeout == port->tcp_user_timeout)
1892  return STATUS_OK;
1893 
1894  if (port->default_tcp_user_timeout <= 0)
1895  {
1896  if (pq_gettcpusertimeout(port) < 0)
1897  {
1898  if (timeout == 0)
1899  return STATUS_OK; /* default is set but unknown */
1900  else
1901  return STATUS_ERROR;
1902  }
1903  }
1904 
1905  if (timeout == 0)
1906  timeout = port->default_tcp_user_timeout;
1907 
1908  if (setsockopt(port->sock, IPPROTO_TCP, TCP_USER_TIMEOUT,
1909  (char *) &timeout, sizeof(timeout)) < 0)
1910  {
1911  ereport(LOG,
1912  (errmsg("%s(%s) failed: %m", "setsockopt", "TCP_USER_TIMEOUT")));
1913  return STATUS_ERROR;
1914  }
1915 
1916  port->tcp_user_timeout = timeout;
1917 #else
1918  if (timeout != 0)
1919  {
1920  ereport(LOG,
1921  (errmsg("%s(%s) not supported", "setsockopt", "TCP_USER_TIMEOUT")));
1922  return STATUS_ERROR;
1923  }
1924 #endif
1925 
1926  return STATUS_OK;
1927 }
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define STATUS_ERROR
Definition: c.h:1171
#define LOG
Definition: elog.h:26
pgsocket sock
Definition: libpq-be.h:127
int pq_gettcpusertimeout(Port *port)
Definition: pqcomm.c:1855
#define IS_AF_UNIX(fam)
Definition: ip.h:24
int tcp_user_timeout
Definition: libpq-be.h:189
#define STATUS_OK
Definition: c.h:1170
SockAddr laddr
Definition: libpq-be.h:130
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
int default_tcp_user_timeout
Definition: libpq-be.h:185

◆ pq_startmsgread()

void pq_startmsgread ( void  )

Definition at line 1152 of file pqcomm.c.

References ereport, errcode(), errmsg(), FATAL, and PqCommReadingMsg.

Referenced by CheckSCRAMAuth(), CopyGetData(), ProcessRepliesIfAny(), ProcessStartupPacket(), recv_password_packet(), and SocketBackend().

1153 {
1154  /*
1155  * There shouldn't be a read active already, but let's check just to be
1156  * sure.
1157  */
1158  if (PqCommReadingMsg)
1159  ereport(FATAL,
1160  (errcode(ERRCODE_PROTOCOL_VIOLATION),
1161  errmsg("terminating connection because protocol synchronization was lost")));
1162 
1163  PqCommReadingMsg = true;
1164 }
int errcode(int sqlerrcode)
Definition: elog.c:698
#define FATAL
Definition: elog.h:49
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
static bool PqCommReadingMsg
Definition: pqcomm.c:137

◆ RemoveSocketFiles()

void RemoveSocketFiles ( void  )

Definition at line 883 of file pqcomm.c.

References lfirst, and NIL.

Referenced by CloseServerPorts().

884 {
885  ListCell *l;
886 
887  /* Loop through all created sockets... */
888  foreach(l, sock_paths)
889  {
890  char *sock_path = (char *) lfirst(l);
891 
892  /* Ignore any error. */
893  (void) unlink(sock_path);
894  }
895  /* Since we're about to exit, no need to reclaim storage */
896  sock_paths = NIL;
897 }
#define NIL
Definition: pg_list.h:65
static List * sock_paths
Definition: pqcomm.c:112
#define lfirst(lc)
Definition: pg_list.h:169

◆ socket_close()

static void socket_close ( int  code,
Datum  arg 
)
static

Definition at line 247 of file pqcomm.c.

References Port::gss, MyProcPort, PGINVALID_SOCKET, secure_close(), and Port::sock.

Referenced by pq_init().

248 {
249  /* Nothing to do in a standalone backend, where MyProcPort is NULL. */
250  if (MyProcPort != NULL)
251  {
252 #ifdef ENABLE_GSS
253  /*
254  * Shutdown GSSAPI layer. This section does nothing when interrupting
255  * BackendInitialize(), because pg_GSS_recvauth() makes first use of
256  * "ctx" and "cred".
257  *
258  * Note that we don't bother to free MyProcPort->gss, since we're
259  * about to exit anyway.
260  */
261  if (MyProcPort->gss)
262  {
263  OM_uint32 min_s;
264 
265  if (MyProcPort->gss->ctx != GSS_C_NO_CONTEXT)
266  gss_delete_sec_context(&min_s, &MyProcPort->gss->ctx, NULL);
267 
268  if (MyProcPort->gss->cred != GSS_C_NO_CREDENTIAL)
269  gss_release_cred(&min_s, &MyProcPort->gss->cred);
270  }
271 #endif /* ENABLE_GSS */
272 
273  /*
274  * Cleanly shut down SSL layer. Nowhere else does a postmaster child
275  * call this, so this is safe when interrupting BackendInitialize().
276  */
278 
279  /*
280  * Formerly we did an explicit close() here, but it seems better to
281  * leave the socket open until the process dies. This allows clients
282  * to perform a "synchronous close" if they care --- wait till the
283  * transport layer reports connection closure, and you can be sure the
284  * backend has exited.
285  *
286  * We do set sock to PGINVALID_SOCKET to prevent any further I/O,
287  * though.
288  */
290  }
291 }
struct Port * MyProcPort
Definition: globals.c:46
pgsocket sock
Definition: libpq-be.h:127
#define PGINVALID_SOCKET
Definition: port.h:33
void secure_close(Port *port)
Definition: be-secure.c:135
void * gss
Definition: libpq-be.h:203

◆ socket_comm_reset()

static void socket_comm_reset ( void  )
static

Definition at line 232 of file pqcomm.c.

References PqCommBusy.

233 {
234  /* Do not throw away pending data, but do reset the busy flag */
235  PqCommBusy = false;
236 }
static bool PqCommBusy
Definition: pqcomm.c:136

◆ socket_flush()

static int socket_flush ( void  )
static

Definition at line 1319 of file pqcomm.c.

References internal_flush(), PqCommBusy, and socket_set_nonblocking().

1320 {
1321  int res;
1322 
1323  /* No-op if reentrant call */
1324  if (PqCommBusy)
1325  return 0;
1326  PqCommBusy = true;
1327  socket_set_nonblocking(false);
1328  res = internal_flush();
1329  PqCommBusy = false;
1330  return res;
1331 }
static int internal_flush(void)
Definition: pqcomm.c:1341
static void socket_set_nonblocking(bool nonblocking)
Definition: pqcomm.c:916
static bool PqCommBusy
Definition: pqcomm.c:136

◆ socket_flush_if_writable()

static int socket_flush_if_writable ( void  )
static

Definition at line 1414 of file pqcomm.c.

References internal_flush(), PqCommBusy, PqSendPointer, PqSendStart, and socket_set_nonblocking().

1415 {
1416  int res;
1417 
1418  /* Quick exit if nothing to do */
1419  if (PqSendPointer == PqSendStart)
1420  return 0;
1421 
1422  /* No-op if reentrant call */
1423  if (PqCommBusy)
1424  return 0;
1425 
1426  /* Temporarily put the socket into non-blocking mode */
1427  socket_set_nonblocking(true);
1428 
1429  PqCommBusy = true;
1430  res = internal_flush();
1431  PqCommBusy = false;
1432  return res;
1433 }
static int PqSendStart
Definition: pqcomm.c:127
static int internal_flush(void)
Definition: pqcomm.c:1341
static void socket_set_nonblocking(bool nonblocking)
Definition: pqcomm.c:916
static int PqSendPointer
Definition: pqcomm.c:126
static bool PqCommBusy
Definition: pqcomm.c:136

◆ socket_is_send_pending()

static bool socket_is_send_pending ( void  )
static

Definition at line 1440 of file pqcomm.c.

References PqSendPointer, and PqSendStart.

1441 {
1442  return (PqSendStart < PqSendPointer);
1443 }
static int PqSendStart
Definition: pqcomm.c:127
static int PqSendPointer
Definition: pqcomm.c:126

◆ socket_putmessage()

static int socket_putmessage ( char  msgtype,
const char *  s,
size_t  len 
)
static

Definition at line 1470 of file pqcomm.c.

References Assert, internal_putbytes(), pg_hton32, and PqCommBusy.

1471 {
1472  uint32 n32;
1473 
1474  Assert(msgtype != 0);
1475 
1476  if (PqCommBusy)
1477  return 0;
1478  PqCommBusy = true;
1479  if (internal_putbytes(&msgtype, 1))
1480  goto fail;
1481 
1482  n32 = pg_hton32((uint32) (len + 4));
1483  if (internal_putbytes((char *) &n32, 4))
1484  goto fail;
1485 
1486  if (internal_putbytes(s, len))
1487  goto fail;
1488  PqCommBusy = false;
1489  return 0;
1490 
1491 fail:
1492  PqCommBusy = false;
1493  return EOF;
1494 }
#define pg_hton32(x)
Definition: pg_bswap.h:121
unsigned int uint32
Definition: c.h:441
static int internal_putbytes(const char *s, size_t len)
Definition: pqcomm.c:1288
static bool PqCommBusy
Definition: pqcomm.c:136
#define Assert(condition)
Definition: c.h:804

◆ socket_putmessage_noblock()

static void socket_putmessage_noblock ( char  msgtype,
const char *  s,
size_t  len 
)
static

Definition at line 1503 of file pqcomm.c.

References Assert, PG_USED_FOR_ASSERTS_ONLY, pq_putmessage, PqSendBuffer, PqSendBufferSize, PqSendPointer, repalloc(), and generate_unaccent_rules::required.

1504 {
1505  int res PG_USED_FOR_ASSERTS_ONLY;
1506  int required;
1507 
1508  /*
1509  * Ensure we have enough space in the output buffer for the message header
1510  * as well as the message itself.
1511  */
1512  required = PqSendPointer + 1 + 4 + len;
1513  if (required > PqSendBufferSize)
1514  {
1515  PqSendBuffer = repalloc(PqSendBuffer, required);
1517  }
1518  res = pq_putmessage(msgtype, s, len);
1519  Assert(res == 0); /* should not fail when the message fits in
1520  * buffer */
1521 }
static char * PqSendBuffer
Definition: pqcomm.c:124
static int PqSendPointer
Definition: pqcomm.c:126
static int PqSendBufferSize
Definition: pqcomm.c:125
#define Assert(condition)
Definition: c.h:804
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1182
#define pq_putmessage(msgtype, s, len)
Definition: libpq.h:49
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:155

◆ socket_set_nonblocking()

static void socket_set_nonblocking ( bool  nonblocking)
static

Definition at line 916 of file pqcomm.c.

References ereport, errcode(), errmsg(), ERROR, MyProcPort, and Port::noblock.

Referenced by internal_putbytes(), pq_getbyte_if_available(), pq_recvbuf(), socket_flush(), and socket_flush_if_writable().

917 {
918  if (MyProcPort == NULL)
919  ereport(ERROR,
920  (errcode(ERRCODE_CONNECTION_DOES_NOT_EXIST),
921  errmsg("there is no client connection")));
922 
923  MyProcPort->noblock = nonblocking;
924 }
struct Port * MyProcPort
Definition: globals.c:46
int errcode(int sqlerrcode)
Definition: elog.c:698
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
bool noblock
Definition: libpq-be.h:128
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ StreamClose()

void StreamClose ( pgsocket  sock)

Definition at line 850 of file pqcomm.c.

References closesocket.

Referenced by ClosePostmasterPorts(), CloseServerPorts(), ConnCreate(), and ServerLoop().

851 {
852  closesocket(sock);
853 }
#define closesocket
Definition: port.h:332

◆ StreamConnection()

int StreamConnection ( pgsocket  server_fd,
Port port 
)

Definition at line 716 of file pqcomm.c.

References accept, SockAddr::addr, ereport, errcode_for_socket_access(), errmsg(), IS_AF_UNIX, Port::laddr, LOG, pg_usleep(), PGINVALID_SOCKET, PQ_SEND_BUFFER_SIZE, pq_setkeepalivescount(), pq_setkeepalivesidle(), pq_setkeepalivesinterval(), pq_settcpusertimeout(), Port::raddr, SockAddr::salen, Port::sock, STATUS_ERROR, STATUS_OK, tcp_keepalives_count, tcp_keepalives_idle, tcp_keepalives_interval, and tcp_user_timeout.

Referenced by ConnCreate().

717 {
718  /* accept connection and fill in the client (remote) address */
719  port->raddr.salen = sizeof(port->raddr.addr);
720  if ((port->sock = accept(server_fd,
721  (struct sockaddr *) &port->raddr.addr,
722  &port->raddr.salen)) == PGINVALID_SOCKET)
723  {
724  ereport(LOG,
726  errmsg("could not accept new connection: %m")));
727 
728  /*
729  * If accept() fails then postmaster.c will still see the server
730  * socket as read-ready, and will immediately try again. To avoid
731  * uselessly sucking lots of CPU, delay a bit before trying again.
732  * (The most likely reason for failure is being out of kernel file
733  * table slots; we can do little except hope some will get freed up.)
734  */
735  pg_usleep(100000L); /* wait 0.1 sec */
736  return STATUS_ERROR;
737  }
738 
739  /* fill in the server (local) address */
740  port->laddr.salen = sizeof(port->laddr.addr);
741  if (getsockname(port->sock,
742  (struct sockaddr *) &port->laddr.addr,
743  &port->laddr.salen) < 0)
744  {
745  ereport(LOG,
746  (errmsg("%s() failed: %m", "getsockname")));
747  return STATUS_ERROR;
748  }
749 
750  /* select NODELAY and KEEPALIVE options if it's a TCP connection */
751  if (!IS_AF_UNIX(port->laddr.addr.ss_family))
752  {
753  int on;
754 #ifdef WIN32
755  int oldopt;
756  int optlen;
757  int newopt;
758 #endif
759 
760 #ifdef TCP_NODELAY
761  on = 1;
762  if (setsockopt(port->sock, IPPROTO_TCP, TCP_NODELAY,
763  (char *) &on, sizeof(on)) < 0)
764  {
765  ereport(LOG,
766  (errmsg("%s(%s) failed: %m", "setsockopt", "TCP_NODELAY")));
767  return STATUS_ERROR;
768  }
769 #endif
770  on = 1;
771  if (setsockopt(port->sock, SOL_SOCKET, SO_KEEPALIVE,
772  (char *) &on, sizeof(on)) < 0)
773  {
774  ereport(LOG,
775  (errmsg("%s(%s) failed: %m", "setsockopt", "SO_KEEPALIVE")));
776  return STATUS_ERROR;
777  }
778 
779 #ifdef WIN32
780 
781  /*
782  * This is a Win32 socket optimization. The OS send buffer should be
783  * large enough to send the whole Postgres send buffer in one go, or
784  * performance suffers. The Postgres send buffer can be enlarged if a
785  * very large message needs to be sent, but we won't attempt to
786  * enlarge the OS buffer if that happens, so somewhat arbitrarily
787  * ensure that the OS buffer is at least PQ_SEND_BUFFER_SIZE * 4.
788  * (That's 32kB with the current default).
789  *
790  * The default OS buffer size used to be 8kB in earlier Windows
791  * versions, but was raised to 64kB in Windows 2012. So it shouldn't
792  * be necessary to change it in later versions anymore. Changing it
793  * unnecessarily can even reduce performance, because setting
794  * SO_SNDBUF in the application disables the "dynamic send buffering"
795  * feature that was introduced in Windows 7. So before fiddling with
796  * SO_SNDBUF, check if the current buffer size is already large enough
797  * and only increase it if necessary.
798  *
799  * See https://support.microsoft.com/kb/823764/EN-US/ and
800  * https://msdn.microsoft.com/en-us/library/bb736549%28v=vs.85%29.aspx
801  */
802  optlen = sizeof(oldopt);
803  if (getsockopt(port->sock, SOL_SOCKET, SO_SNDBUF, (char *) &oldopt,
804  &optlen) < 0)
805  {
806  ereport(LOG,
807  (errmsg("%s(%s) failed: %m", "getsockopt", "SO_SNDBUF")));
808  return STATUS_ERROR;
809  }
810  newopt = PQ_SEND_BUFFER_SIZE * 4;
811  if (oldopt < newopt)
812  {
813  if (setsockopt(port->sock, SOL_SOCKET, SO_SNDBUF, (char *) &newopt,
814  sizeof(newopt)) < 0)
815  {
816  ereport(LOG,
817  (errmsg("%s(%s) failed: %m", "setsockopt", "SO_SNDBUF")));
818  return STATUS_ERROR;
819  }
820  }
821 #endif
822 
823  /*
824  * Also apply the current keepalive parameters. If we fail to set a
825  * parameter, don't error out, because these aren't universally
826  * supported. (Note: you might think we need to reset the GUC
827  * variables to 0 in such a case, but it's not necessary because the
828  * show hooks for these variables report the truth anyway.)
829  */
834  }
835 
836  return STATUS_OK;
837 }
#define accept(s, addr, addrlen)
Definition: win32_port.h:462
int pq_setkeepalivesinterval(int interval, Port *port)
Definition: pqcomm.c:1731
struct sockaddr_storage addr
Definition: pqcomm.h:64
int tcp_user_timeout
Definition: guc.c:610
#define STATUS_ERROR
Definition: c.h:1171
#define LOG
Definition: elog.h:26
pgsocket sock
Definition: libpq-be.h:127
int tcp_keepalives_idle
Definition: guc.c:607
SockAddr raddr
Definition: libpq-be.h:131
void pg_usleep(long microsec)
Definition: signal.c:53
int pq_setkeepalivesidle(int idle, Port *port)
Definition: pqcomm.c:1646
#define IS_AF_UNIX(fam)
Definition: ip.h:24
ACCEPT_TYPE_ARG3 salen
Definition: pqcomm.h:65
#define STATUS_OK
Definition: c.h:1170
int tcp_keepalives_interval
Definition: guc.c:608
int errcode_for_socket_access(void)
Definition: elog.c:792
SockAddr laddr
Definition: libpq-be.h:130
#define PGINVALID_SOCKET
Definition: port.h:33
#define ereport(elevel,...)
Definition: elog.h:157
int pq_setkeepalivescount(int count, Port *port)
Definition: pqcomm.c:1810
int errmsg(const char *fmt,...)
Definition: elog.c:909
int pq_settcpusertimeout(int timeout, Port *port)
Definition: pqcomm.c:1885
int tcp_keepalives_count
Definition: guc.c:609
#define PQ_SEND_BUFFER_SIZE
Definition: pqcomm.c:121

◆ StreamServerPort()

int StreamServerPort ( int  family,
const char *  hostName,
unsigned short  portNumber,
const char *  unixSocketDir,
pgsocket  ListenSocket[],
int  MaxListen 
)

Definition at line 318 of file pqcomm.c.

References _, addrinfo::ai_family, addrinfo::ai_flags, addrinfo::ai_next, AI_PASSIVE, addrinfo::ai_socktype, Assert, bind, closesocket, CreateSocketLockFile(), EADDRINUSE, elog, ereport, errcode_for_file_access(), errcode_for_socket_access(), errhint(), errmsg(), fd(), gai_strerror, IS_AF_UNIX, lappend(), listen, LOG, MaxBackends, MAXPGPATH, MemSet, NI_MAXHOST, NI_NUMERICHOST, pg_freeaddrinfo_all(), pg_getaddrinfo_all(), pg_getnameinfo_all(), PG_SOMAXCONN, PGINVALID_SOCKET, pstrdup(), snprintf, socket, STATUS_ERROR, STATUS_OK, Unix_socket_group, Unix_socket_permissions, UNIXSOCK_PATH, UNIXSOCK_PATH_BUFLEN, val, and WARNING.

Referenced by PostmasterMain().

321 {
322  pgsocket fd;
323  int err;
324  int maxconn;
325  int ret;
326  char portNumberStr[32];
327  const char *familyDesc;
328  char familyDescBuf[64];
329  const char *addrDesc;
330  char addrBuf[NI_MAXHOST];
331  char *service;
332  struct addrinfo *addrs = NULL,
333  *addr;
334  struct addrinfo hint;
335  int listen_index = 0;
336  int added = 0;
337 
338 #ifdef HAVE_UNIX_SOCKETS
339  char unixSocketPath[MAXPGPATH];
340 #endif
341 #if !defined(WIN32) || defined(IPV6_V6ONLY)
342  int one = 1;
343 #endif
344 
345  /* Initialize hint structure */
346  MemSet(&hint, 0, sizeof(hint));
347  hint.ai_family = family;
348  hint.ai_flags = AI_PASSIVE;
349  hint.ai_socktype = SOCK_STREAM;
350 
351 #ifdef HAVE_UNIX_SOCKETS
352  if (family == AF_UNIX)
353  {
354  /*
355  * Create unixSocketPath from portNumber and unixSocketDir and lock
356  * that file path
357  */
358  UNIXSOCK_PATH(unixSocketPath, portNumber, unixSocketDir);
359  if (strlen(unixSocketPath) >= UNIXSOCK_PATH_BUFLEN)
360  {
361  ereport(LOG,
362  (errmsg("Unix-domain socket path \"%s\" is too long (maximum %d bytes)",
363  unixSocketPath,
364  (int) (UNIXSOCK_PATH_BUFLEN - 1))));
365  return STATUS_ERROR;
366  }
367  if (Lock_AF_UNIX(unixSocketDir, unixSocketPath) != STATUS_OK)
368  return STATUS_ERROR;
369  service = unixSocketPath;
370  }
371  else
372 #endif /* HAVE_UNIX_SOCKETS */
373  {
374  snprintf(portNumberStr, sizeof(portNumberStr), "%d", portNumber);
375  service = portNumberStr;
376  }
377 
378  ret = pg_getaddrinfo_all(hostName, service, &hint, &addrs);
379  if (ret || !addrs)
380  {
381  if (hostName)
382  ereport(LOG,
383  (errmsg("could not translate host name \"%s\", service \"%s\" to address: %s",
384  hostName, service, gai_strerror(ret))));
385  else
386  ereport(LOG,
387  (errmsg("could not translate service \"%s\" to address: %s",
388  service, gai_strerror(ret))));
389  if (addrs)
390  pg_freeaddrinfo_all(hint.ai_family, addrs);
391  return STATUS_ERROR;
392  }
393 
394  for (addr = addrs; addr; addr = addr->ai_next)
395  {
396  if (!IS_AF_UNIX(family) && IS_AF_UNIX(addr->ai_family))
397  {
398  /*
399  * Only set up a unix domain socket when they really asked for it.
400  * The service/port is different in that case.
401  */
402  continue;
403  }
404 
405  /* See if there is still room to add 1 more socket. */
406  for (; listen_index < MaxListen; listen_index++)
407  {
408  if (ListenSocket[listen_index] == PGINVALID_SOCKET)
409  break;
410  }
411  if (listen_index >= MaxListen)
412  {
413  ereport(LOG,
414  (errmsg("could not bind to all requested addresses: MAXLISTEN (%d) exceeded",
415  MaxListen)));
416  break;
417  }
418 
419  /* set up address family name for log messages */
420  switch (addr->ai_family)
421  {
422  case AF_INET:
423  familyDesc = _("IPv4");
424  break;
425 #ifdef HAVE_IPV6
426  case AF_INET6:
427  familyDesc = _("IPv6");
428  break;
429 #endif
430 #ifdef HAVE_UNIX_SOCKETS
431  case AF_UNIX:
432  familyDesc = _("Unix");
433  break;
434 #endif
435  default:
436  snprintf(familyDescBuf, sizeof(familyDescBuf),
437  _("unrecognized address family %d"),
438  addr->ai_family);
439  familyDesc = familyDescBuf;
440  break;
441  }
442 
443  /* set up text form of address for log messages */
444 #ifdef HAVE_UNIX_SOCKETS
445  if (addr->ai_family == AF_UNIX)
446  addrDesc = unixSocketPath;
447  else
448 #endif
449  {
450  pg_getnameinfo_all((const struct sockaddr_storage *) addr->ai_addr,
451  addr->ai_addrlen,
452  addrBuf, sizeof(addrBuf),
453  NULL, 0,
455  addrDesc = addrBuf;
456  }
457 
458  if ((fd = socket(addr->ai_family, SOCK_STREAM, 0)) == PGINVALID_SOCKET)
459  {
460  ereport(LOG,
462  /* translator: first %s is IPv4, IPv6, or Unix */
463  errmsg("could not create %s socket for address \"%s\": %m",
464  familyDesc, addrDesc)));
465  continue;
466  }
467 
468 #ifndef WIN32
469 
470  /*
471  * Without the SO_REUSEADDR flag, a new postmaster can't be started
472  * right away after a stop or crash, giving "address already in use"
473  * error on TCP ports.
474  *
475  * On win32, however, this behavior only happens if the
476  * SO_EXCLUSIVEADDRUSE is set. With SO_REUSEADDR, win32 allows
477  * multiple servers to listen on the same address, resulting in
478  * unpredictable behavior. With no flags at all, win32 behaves as Unix
479  * with SO_REUSEADDR.
480  */
481  if (!IS_AF_UNIX(addr->ai_family))
482  {
483  if ((setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
484  (char *) &one, sizeof(one))) == -1)
485  {
486  ereport(LOG,
488  /* translator: third %s is IPv4, IPv6, or Unix */
489  errmsg("%s(%s) failed for %s address \"%s\": %m",
490  "setsockopt", "SO_REUSEADDR",
491  familyDesc, addrDesc)));
492  closesocket(fd);
493  continue;
494  }
495  }
496 #endif
497 
498 #ifdef IPV6_V6ONLY
499  if (addr->ai_family == AF_INET6)
500  {
501  if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY,
502  (char *) &one, sizeof(one)) == -1)
503  {
504  ereport(LOG,
506  /* translator: third %s is IPv4, IPv6, or Unix */
507  errmsg("%s(%s) failed for %s address \"%s\": %m",
508  "setsockopt", "IPV6_V6ONLY",
509  familyDesc, addrDesc)));
510  closesocket(fd);
511  continue;
512  }
513  }
514 #endif
515 
516  /*
517  * Note: This might fail on some OS's, like Linux older than
518  * 2.4.21-pre3, that don't have the IPV6_V6ONLY socket option, and map
519  * ipv4 addresses to ipv6. It will show ::ffff:ipv4 for all ipv4
520  * connections.
521  */
522  err = bind(fd, addr->ai_addr, addr->ai_addrlen);
523  if (err < 0)
524  {
525  int saved_errno = errno;
526 
527  ereport(LOG,
529  /* translator: first %s is IPv4, IPv6, or Unix */
530  errmsg("could not bind %s address \"%s\": %m",
531  familyDesc, addrDesc),
532  saved_errno == EADDRINUSE ?
533  (IS_AF_UNIX(addr->ai_family) ?
534  errhint("Is another postmaster already running on port %d?",
535  (int) portNumber) :
536  errhint("Is another postmaster already running on port %d?"
537  " If not, wait a few seconds and retry.",
538  (int) portNumber)) : 0));
539  closesocket(fd);
540  continue;
541  }
542 
543 #ifdef HAVE_UNIX_SOCKETS
544  if (addr->ai_family == AF_UNIX)
545  {
546  if (Setup_AF_UNIX(service) != STATUS_OK)
547  {
548  closesocket(fd);
549  break;
550  }
551  }
552 #endif
553 
554  /*
555  * Select appropriate accept-queue length limit. PG_SOMAXCONN is only
556  * intended to provide a clamp on the request on platforms where an
557  * overly large request provokes a kernel error (are there any?).
558  */
559  maxconn = MaxBackends * 2;
560  if (maxconn > PG_SOMAXCONN)
561  maxconn = PG_SOMAXCONN;
562 
563  err = listen(fd, maxconn);
564  if (err < 0)
565  {
566  ereport(LOG,
568  /* translator: first %s is IPv4, IPv6, or Unix */
569  errmsg("could not listen on %s address \"%s\": %m",
570  familyDesc, addrDesc)));
571  closesocket(fd);
572  continue;
573  }
574 
575 #ifdef HAVE_UNIX_SOCKETS
576  if (addr->ai_family == AF_UNIX)
577  ereport(LOG,
578  (errmsg("listening on Unix socket \"%s\"",
579  addrDesc)));
580  else
581 #endif
582  ereport(LOG,
583  /* translator: first %s is IPv4 or IPv6 */
584  (errmsg("listening on %s address \"%s\", port %d",
585  familyDesc, addrDesc, (int) portNumber)));
586 
587  ListenSocket[listen_index] = fd;
588  added++;
589  }
590 
591  pg_freeaddrinfo_all(hint.ai_family, addrs);
592 
593  if (!added)
594  return STATUS_ERROR;
595 
596  return STATUS_OK;
597 }
void pg_freeaddrinfo_all(int hint_ai_family, struct addrinfo *ai)
Definition: ip.c:88
#define UNIXSOCK_PATH(path, port, sockdir)
Definition: pqcomm.h:70
int errhint(const char *fmt,...)
Definition: elog.c:1156
#define NI_NUMERICHOST
Definition: getaddrinfo.h:78
#define UNIXSOCK_PATH_BUFLEN
Definition: pqcomm.h:86
#define closesocket
Definition: port.h:332
#define STATUS_ERROR
Definition: c.h:1171
#define MemSet(start, val, len)
Definition: c.h:1008
#define LOG
Definition: elog.h:26
#define bind(s, addr, addrlen)
Definition: win32_port.h:460
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define gai_strerror
Definition: getaddrinfo.h:146
int pg_getaddrinfo_all(const char *hostname, const char *servname, const struct addrinfo *hintp, struct addrinfo **result)
Definition: ip.c:57
#define NI_MAXHOST
Definition: getaddrinfo.h:88
#define IS_AF_UNIX(fam)
Definition: ip.h:24
#define MAXPGPATH
int MaxBackends
Definition: globals.c:139
#define AI_PASSIVE
Definition: getaddrinfo.h:62
int pgsocket
Definition: port.h:31
#define STATUS_OK
Definition: c.h:1170
int errcode_for_socket_access(void)
Definition: elog.c:792
#define listen(s, backlog)
Definition: win32_port.h:461
static pgsocket ListenSocket[MAXLISTEN]
Definition: postmaster.c:221
#define socket(af, type, protocol)
Definition: win32_port.h:459
#define PGINVALID_SOCKET
Definition: port.h:33
#define PG_SOMAXCONN
#define ereport(elevel,...)
Definition: elog.h:157
int pg_getnameinfo_all(const struct sockaddr_storage *addr, int salen, char *node, int nodelen, char *service, int servicelen, int flags)
Definition: ip.c:122
#define EADDRINUSE
Definition: win32_port.h:369
struct addrinfo * ai_next
Definition: getaddrinfo.h:107
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define snprintf
Definition: port.h:216
#define _(x)
Definition: elog.c:89

◆ TouchSocketFiles()

void TouchSocketFiles ( void  )

Definition at line 865 of file pqcomm.c.

References lfirst.

Referenced by ServerLoop().

866 {
867  ListCell *l;
868 
869  /* Loop through all created sockets... */
870  foreach(l, sock_paths)
871  {
872  char *sock_path = (char *) lfirst(l);
873 
874  /* Ignore errors; there's no point in complaining */
875  (void) utime(sock_path, NULL);
876  }
877 }
static List * sock_paths
Definition: pqcomm.c:112
#define lfirst(lc)
Definition: pg_list.h:169

Variable Documentation

◆ FeBeWaitSet

WaitEventSet* FeBeWaitSet

◆ PqCommBusy

bool PqCommBusy
static

◆ PqCommMethods

const PQcommMethods* PqCommMethods = &PqCommSocketMethods

Definition at line 166 of file pqcomm.c.

Referenced by pq_redirect_to_shm_mq(), and pq_set_parallel_leader().

◆ PqCommReadingMsg

◆ PqCommSocketMethods

const PQcommMethods PqCommSocketMethods
static
Initial value:
= {
}
static int socket_flush_if_writable(void)
Definition: pqcomm.c:1414
static bool socket_is_send_pending(void)
Definition: pqcomm.c:1440
static void socket_comm_reset(void)
Definition: pqcomm.c:232
static int socket_putmessage(char msgtype, const char *s, size_t len)
Definition: pqcomm.c:1470
static int socket_flush(void)
Definition: pqcomm.c:1319
static void socket_putmessage_noblock(char msgtype, const char *s, size_t len)
Definition: pqcomm.c:1503

Definition at line 157 of file pqcomm.c.

◆ PqRecvBuffer

char PqRecvBuffer[PQ_RECV_BUFFER_SIZE]
static

Definition at line 129 of file pqcomm.c.

Referenced by pq_getbyte(), pq_getbyte_if_available(), pq_getbytes(), pq_peekbyte(), and pq_recvbuf().

◆ PqRecvLength

int PqRecvLength
static

◆ PqRecvPointer

int PqRecvPointer
static

◆ PqSendBuffer

char* PqSendBuffer
static

Definition at line 124 of file pqcomm.c.

Referenced by internal_flush(), internal_putbytes(), pq_init(), and socket_putmessage_noblock().

◆ PqSendBufferSize

int PqSendBufferSize
static

Definition at line 125 of file pqcomm.c.

Referenced by internal_putbytes(), pq_init(), and socket_putmessage_noblock().

◆ PqSendPointer

◆ PqSendStart

int PqSendStart
static

◆ sock_paths

List* sock_paths = NIL
static

Definition at line 112 of file pqcomm.c.

◆ Unix_socket_group

char* Unix_socket_group

Definition at line 109 of file pqcomm.c.

Referenced by StreamServerPort().

◆ Unix_socket_permissions

int Unix_socket_permissions

Definition at line 108 of file pqcomm.c.

Referenced by show_unix_socket_permissions(), and StreamServerPort().