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 void socket_startcopyout (void)
 
static void socket_endcopyout (bool errorAbort)
 
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)
 
int pq_getstring (StringInfo s)
 
void pq_startmsgread (void)
 
void pq_endmsgread (void)
 
bool pq_is_reading_msg (void)
 
int pq_getmessage (StringInfo s, int maxlen)
 
int pq_putbytes (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)
 

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 bool DoingCopyOut
 
static const PQcommMethods PqCommSocketMethods
 
const PQcommMethodsPqCommMethods = &PqCommSocketMethods
 
WaitEventSetFeBeWaitSet
 

Macro Definition Documentation

◆ PQ_RECV_BUFFER_SIZE

#define PQ_RECV_BUFFER_SIZE   8192

Definition at line 133 of file pqcomm.c.

Referenced by pq_recvbuf().

◆ PQ_SEND_BUFFER_SIZE

#define PQ_SEND_BUFFER_SIZE   8192

Definition at line 132 of file pqcomm.c.

Referenced by pq_init(), and StreamConnection().

Function Documentation

◆ internal_flush()

static int internal_flush ( void  )
static

Definition at line 1409 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().

1410 {
1411  static int last_reported_send_errno = 0;
1412 
1413  char *bufptr = PqSendBuffer + PqSendStart;
1414  char *bufend = PqSendBuffer + PqSendPointer;
1415 
1416  while (bufptr < bufend)
1417  {
1418  int r;
1419 
1420  r = secure_write(MyProcPort, bufptr, bufend - bufptr);
1421 
1422  if (r <= 0)
1423  {
1424  if (errno == EINTR)
1425  continue; /* Ok if we were interrupted */
1426 
1427  /*
1428  * Ok if no data writable without blocking, and the socket is in
1429  * non-blocking mode.
1430  */
1431  if (errno == EAGAIN ||
1432  errno == EWOULDBLOCK)
1433  {
1434  return 0;
1435  }
1436 
1437  /*
1438  * Careful: an ereport() that tries to write to the client would
1439  * cause recursion to here, leading to stack overflow and core
1440  * dump! This message must go *only* to the postmaster log.
1441  *
1442  * If a client disconnects while we're in the midst of output, we
1443  * might write quite a bit of data before we get to a safe query
1444  * abort point. So, suppress duplicate log messages.
1445  */
1446  if (errno != last_reported_send_errno)
1447  {
1448  last_reported_send_errno = errno;
1451  errmsg("could not send data to client: %m")));
1452  }
1453 
1454  /*
1455  * We drop the buffered data anyway so that processing can
1456  * continue, even though we'll probably quit soon. We also set a
1457  * flag that'll cause the next CHECK_FOR_INTERRUPTS to terminate
1458  * the connection.
1459  */
1460  PqSendStart = PqSendPointer = 0;
1462  InterruptPending = 1;
1463  return EOF;
1464  }
1465 
1466  last_reported_send_errno = 0; /* reset after any successful send */
1467  bufptr += r;
1468  PqSendStart += r;
1469  }
1470 
1471  PqSendStart = PqSendPointer = 0;
1472  return 0;
1473 }
struct Port * MyProcPort
Definition: globals.c:43
#define EAGAIN
Definition: win32_port.h:321
static char * PqSendBuffer
Definition: pqcomm.c:135
static int PqSendStart
Definition: pqcomm.c:138
static int PqSendPointer
Definition: pqcomm.c:137
#define COMMERROR
Definition: elog.h:30
volatile sig_atomic_t ClientConnectionLost
Definition: globals.c:33
ssize_t secure_write(Port *port, void *ptr, size_t len)
Definition: be-secure.c:258
int errcode_for_socket_access(void)
Definition: elog.c:704
#define ereport(elevel,...)
Definition: elog.h:144
volatile sig_atomic_t InterruptPending
Definition: globals.c:30
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define EWOULDBLOCK
Definition: win32_port.h:329
#define EINTR
Definition: win32_port.h:323

◆ internal_putbytes()

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

Definition at line 1356 of file pqcomm.c.

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

Referenced by pq_putbytes(), and socket_putmessage().

1357 {
1358  size_t amount;
1359 
1360  while (len > 0)
1361  {
1362  /* If buffer is full, then flush it out */
1364  {
1365  socket_set_nonblocking(false);
1366  if (internal_flush())
1367  return EOF;
1368  }
1369  amount = PqSendBufferSize - PqSendPointer;
1370  if (amount > len)
1371  amount = len;
1372  memcpy(PqSendBuffer + PqSendPointer, s, amount);
1373  PqSendPointer += amount;
1374  s += amount;
1375  len -= amount;
1376  }
1377  return 0;
1378 }
static char * PqSendBuffer
Definition: pqcomm.c:135
static int internal_flush(void)
Definition: pqcomm.c:1409
static void socket_set_nonblocking(bool nonblocking)
Definition: pqcomm.c:909
static int PqSendPointer
Definition: pqcomm.c:137
static int PqSendBufferSize
Definition: pqcomm.c:136

◆ pq_discardbytes()

static int pq_discardbytes ( size_t  len)
static

Definition at line 1115 of file pqcomm.c.

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

Referenced by pq_getmessage().

1116 {
1117  size_t amount;
1118 
1120 
1121  while (len > 0)
1122  {
1123  while (PqRecvPointer >= PqRecvLength)
1124  {
1125  if (pq_recvbuf()) /* If nothing in buffer, then recv some */
1126  return EOF; /* Failed to recv data */
1127  }
1128  amount = PqRecvLength - PqRecvPointer;
1129  if (amount > len)
1130  amount = len;
1131  PqRecvPointer += amount;
1132  len -= amount;
1133  }
1134  return 0;
1135 }
static int PqRecvLength
Definition: pqcomm.c:142
static int pq_recvbuf(void)
Definition: pqcomm.c:926
#define Assert(condition)
Definition: c.h:745
static int PqRecvPointer
Definition: pqcomm.c:141
static bool PqCommReadingMsg
Definition: pqcomm.c:148

◆ pq_endmsgread()

void pq_endmsgread ( void  )

Definition at line 1221 of file pqcomm.c.

References Assert, and PqCommReadingMsg.

Referenced by CopyFrom(), ProcessRepliesIfAny(), ProcessStartupPacket(), and SocketBackend().

1222 {
1224 
1225  PqCommReadingMsg = false;
1226 }
#define Assert(condition)
Definition: c.h:745
static bool PqCommReadingMsg
Definition: pqcomm.c:148

◆ pq_getbyte()

int pq_getbyte ( void  )

Definition at line 987 of file pqcomm.c.

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

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

988 {
990 
991  while (PqRecvPointer >= PqRecvLength)
992  {
993  if (pq_recvbuf()) /* If nothing in buffer, then recv some */
994  return EOF; /* Failed to recv data */
995  }
996  return (unsigned char) PqRecvBuffer[PqRecvPointer++];
997 }
static char PqRecvBuffer[PQ_RECV_BUFFER_SIZE]
Definition: pqcomm.c:140
static int PqRecvLength
Definition: pqcomm.c:142
static int pq_recvbuf(void)
Definition: pqcomm.c:926
#define Assert(condition)
Definition: c.h:745
static int PqRecvPointer
Definition: pqcomm.c:141
static bool PqCommReadingMsg
Definition: pqcomm.c:148

◆ pq_getbyte_if_available()

int pq_getbyte_if_available ( unsigned char *  c)

Definition at line 1027 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().

1028 {
1029  int r;
1030 
1032 
1034  {
1036  return 1;
1037  }
1038 
1039  /* Put the socket into non-blocking mode */
1040  socket_set_nonblocking(true);
1041 
1042  r = secure_read(MyProcPort, c, 1);
1043  if (r < 0)
1044  {
1045  /*
1046  * Ok if no data available without blocking or interrupted (though
1047  * EINTR really shouldn't happen with a non-blocking socket). Report
1048  * other errors.
1049  */
1050  if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
1051  r = 0;
1052  else
1053  {
1054  /*
1055  * Careful: an ereport() that tries to write to the client would
1056  * cause recursion to here, leading to stack overflow and core
1057  * dump! This message must go *only* to the postmaster log.
1058  */
1061  errmsg("could not receive data from client: %m")));
1062  r = EOF;
1063  }
1064  }
1065  else if (r == 0)
1066  {
1067  /* EOF detected */
1068  r = EOF;
1069  }
1070 
1071  return r;
1072 }
static char PqRecvBuffer[PQ_RECV_BUFFER_SIZE]
Definition: pqcomm.c:140
struct Port * MyProcPort
Definition: globals.c:43
#define EAGAIN
Definition: win32_port.h:321
ssize_t secure_read(Port *port, void *ptr, size_t len)
Definition: be-secure.c:145
static void socket_set_nonblocking(bool nonblocking)
Definition: pqcomm.c:909
static int PqRecvLength
Definition: pqcomm.c:142
char * c
#define COMMERROR
Definition: elog.h:30
int errcode_for_socket_access(void)
Definition: elog.c:704
#define ereport(elevel,...)
Definition: elog.h:144
#define Assert(condition)
Definition: c.h:745
int errmsg(const char *fmt,...)
Definition: elog.c:824
static int PqRecvPointer
Definition: pqcomm.c:141
#define EWOULDBLOCK
Definition: win32_port.h:329
static bool PqCommReadingMsg
Definition: pqcomm.c:148
#define EINTR
Definition: win32_port.h:323

◆ pq_getbytes()

int pq_getbytes ( char *  s,
size_t  len 
)

Definition at line 1081 of file pqcomm.c.

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

Referenced by CopyGetData(), GetOldFunctionMessage(), pq_getmessage(), and ProcessStartupPacket().

1082 {
1083  size_t amount;
1084 
1086 
1087  while (len > 0)
1088  {
1089  while (PqRecvPointer >= PqRecvLength)
1090  {
1091  if (pq_recvbuf()) /* If nothing in buffer, then recv some */
1092  return EOF; /* Failed to recv data */
1093  }
1094  amount = PqRecvLength - PqRecvPointer;
1095  if (amount > len)
1096  amount = len;
1097  memcpy(s, PqRecvBuffer + PqRecvPointer, amount);
1098  PqRecvPointer += amount;
1099  s += amount;
1100  len -= amount;
1101  }
1102  return 0;
1103 }
static char PqRecvBuffer[PQ_RECV_BUFFER_SIZE]
Definition: pqcomm.c:140
static int PqRecvLength
Definition: pqcomm.c:142
static int pq_recvbuf(void)
Definition: pqcomm.c:926
#define Assert(condition)
Definition: c.h:745
static int PqRecvPointer
Definition: pqcomm.c:141
static bool PqCommReadingMsg
Definition: pqcomm.c:148

◆ pq_getkeepalivescount()

int pq_getkeepalivescount ( Port port)

Definition at line 1846 of file pqcomm.c.

References SockAddr::addr, Port::default_keepalives_count, elog, IS_AF_UNIX, Port::keepalives_count, Port::laddr, LOG, and Port::sock.

Referenced by pq_setkeepalivescount(), and show_tcp_keepalives_count().

1847 {
1848 #ifdef TCP_KEEPCNT
1849  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1850  return 0;
1851 
1852  if (port->keepalives_count != 0)
1853  return port->keepalives_count;
1854 
1855  if (port->default_keepalives_count == 0)
1856  {
1857  ACCEPT_TYPE_ARG3 size = sizeof(port->default_keepalives_count);
1858 
1859  if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPCNT,
1860  (char *) &port->default_keepalives_count,
1861  &size) < 0)
1862  {
1863  elog(LOG, "getsockopt(%s) failed: %m", "TCP_KEEPCNT");
1864  port->default_keepalives_count = -1; /* don't know */
1865  }
1866  }
1867 
1868  return port->default_keepalives_count;
1869 #else
1870  return 0;
1871 #endif
1872 }
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define LOG
Definition: elog.h:26
int default_keepalives_count
Definition: libpq-be.h:166
pgsocket sock
Definition: libpq-be.h:122
int keepalives_count
Definition: libpq-be.h:170
#define IS_AF_UNIX(fam)
Definition: ip.h:24
SockAddr laddr
Definition: libpq-be.h:125
#define elog(elevel,...)
Definition: elog.h:214

◆ pq_getkeepalivesidle()

int pq_getkeepalivesidle ( Port port)

Definition at line 1683 of file pqcomm.c.

References SockAddr::addr, Port::default_keepalives_idle, elog, IS_AF_UNIX, Port::keepalives_idle, Port::laddr, LOG, and Port::sock.

Referenced by pq_setkeepalivesidle(), and show_tcp_keepalives_idle().

1684 {
1685 #if defined(PG_TCP_KEEPALIVE_IDLE) || defined(SIO_KEEPALIVE_VALS)
1686  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1687  return 0;
1688 
1689  if (port->keepalives_idle != 0)
1690  return port->keepalives_idle;
1691 
1692  if (port->default_keepalives_idle == 0)
1693  {
1694 #ifndef WIN32
1695  ACCEPT_TYPE_ARG3 size = sizeof(port->default_keepalives_idle);
1696 
1697  if (getsockopt(port->sock, IPPROTO_TCP, PG_TCP_KEEPALIVE_IDLE,
1698  (char *) &port->default_keepalives_idle,
1699  &size) < 0)
1700  {
1701  elog(LOG, "getsockopt(%s) failed: %m", PG_TCP_KEEPALIVE_IDLE_STR);
1702  port->default_keepalives_idle = -1; /* don't know */
1703  }
1704 #else /* WIN32 */
1705  /* We can't get the defaults on Windows, so return "don't know" */
1706  port->default_keepalives_idle = -1;
1707 #endif /* WIN32 */
1708  }
1709 
1710  return port->default_keepalives_idle;
1711 #else
1712  return 0;
1713 #endif
1714 }
int keepalives_idle
Definition: libpq-be.h:168
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define LOG
Definition: elog.h:26
pgsocket sock
Definition: libpq-be.h:122
#define IS_AF_UNIX(fam)
Definition: ip.h:24
SockAddr laddr
Definition: libpq-be.h:125
int default_keepalives_idle
Definition: libpq-be.h:164
#define elog(elevel,...)
Definition: elog.h:214

◆ pq_getkeepalivesinterval()

int pq_getkeepalivesinterval ( Port port)

Definition at line 1765 of file pqcomm.c.

References SockAddr::addr, Port::default_keepalives_interval, elog, IS_AF_UNIX, Port::keepalives_interval, Port::laddr, LOG, and Port::sock.

Referenced by pq_setkeepalivesinterval(), and show_tcp_keepalives_interval().

1766 {
1767 #if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
1768  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1769  return 0;
1770 
1771  if (port->keepalives_interval != 0)
1772  return port->keepalives_interval;
1773 
1774  if (port->default_keepalives_interval == 0)
1775  {
1776 #ifndef WIN32
1777  ACCEPT_TYPE_ARG3 size = sizeof(port->default_keepalives_interval);
1778 
1779  if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPINTVL,
1780  (char *) &port->default_keepalives_interval,
1781  &size) < 0)
1782  {
1783  elog(LOG, "getsockopt(%s) failed: %m", "TCP_KEEPINTVL");
1784  port->default_keepalives_interval = -1; /* don't know */
1785  }
1786 #else
1787  /* We can't get the defaults on Windows, so return "don't know" */
1788  port->default_keepalives_interval = -1;
1789 #endif /* WIN32 */
1790  }
1791 
1792  return port->default_keepalives_interval;
1793 #else
1794  return 0;
1795 #endif
1796 }
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define LOG
Definition: elog.h:26
int default_keepalives_interval
Definition: libpq-be.h:165
pgsocket sock
Definition: libpq-be.h:122
#define IS_AF_UNIX(fam)
Definition: ip.h:24
SockAddr laddr
Definition: libpq-be.h:125
#define elog(elevel,...)
Definition: elog.h:214
int keepalives_interval
Definition: libpq-be.h:169

◆ pq_getmessage()

int pq_getmessage ( StringInfo  s,
int  maxlen 
)

Definition at line 1259 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().

1260 {
1261  int32 len;
1262 
1264 
1265  resetStringInfo(s);
1266 
1267  /* Read message length word */
1268  if (pq_getbytes((char *) &len, 4) == EOF)
1269  {
1271  (errcode(ERRCODE_PROTOCOL_VIOLATION),
1272  errmsg("unexpected EOF within message length word")));
1273  return EOF;
1274  }
1275 
1276  len = pg_ntoh32(len);
1277 
1278  if (len < 4 ||
1279  (maxlen > 0 && len > maxlen))
1280  {
1282  (errcode(ERRCODE_PROTOCOL_VIOLATION),
1283  errmsg("invalid message length")));
1284  return EOF;
1285  }
1286 
1287  len -= 4; /* discount length itself */
1288 
1289  if (len > 0)
1290  {
1291  /*
1292  * Allocate space for message. If we run out of room (ridiculously
1293  * large message), we will elog(ERROR), but we want to discard the
1294  * message body so as not to lose communication sync.
1295  */
1296  PG_TRY();
1297  {
1298  enlargeStringInfo(s, len);
1299  }
1300  PG_CATCH();
1301  {
1302  if (pq_discardbytes(len) == EOF)
1304  (errcode(ERRCODE_PROTOCOL_VIOLATION),
1305  errmsg("incomplete message from client")));
1306 
1307  /* we discarded the rest of the message so we're back in sync. */
1308  PqCommReadingMsg = false;
1309  PG_RE_THROW();
1310  }
1311  PG_END_TRY();
1312 
1313  /* And grab the message */
1314  if (pq_getbytes(s->data, len) == EOF)
1315  {
1317  (errcode(ERRCODE_PROTOCOL_VIOLATION),
1318  errmsg("incomplete message from client")));
1319  return EOF;
1320  }
1321  s->len = len;
1322  /* Place a trailing null per StringInfo convention */
1323  s->data[len] = '\0';
1324  }
1325 
1326  /* finished reading the message. */
1327  PqCommReadingMsg = false;
1328 
1329  return 0;
1330 }
static int pq_discardbytes(size_t len)
Definition: pqcomm.c:1115
int errcode(int sqlerrcode)
Definition: elog.c:610
signed int int32
Definition: c.h:362
#define pg_ntoh32(x)
Definition: pg_bswap.h:125
int pq_getbytes(char *s, size_t len)
Definition: pqcomm.c:1081
#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:144
#define PG_CATCH()
Definition: elog.h:305
#define Assert(condition)
Definition: c.h:745
#define PG_RE_THROW()
Definition: elog.h:336
int errmsg(const char *fmt,...)
Definition: elog.c:824
static bool PqCommReadingMsg
Definition: pqcomm.c:148
#define PG_TRY()
Definition: elog.h:295
#define PG_END_TRY()
Definition: elog.h:320

◆ pq_getstring()

int pq_getstring ( StringInfo  s)

Definition at line 1153 of file pqcomm.c.

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

Referenced by GetOldFunctionMessage(), and SocketBackend().

1154 {
1155  int i;
1156 
1158 
1159  resetStringInfo(s);
1160 
1161  /* Read until we get the terminating '\0' */
1162  for (;;)
1163  {
1164  while (PqRecvPointer >= PqRecvLength)
1165  {
1166  if (pq_recvbuf()) /* If nothing in buffer, then recv some */
1167  return EOF; /* Failed to recv data */
1168  }
1169 
1170  for (i = PqRecvPointer; i < PqRecvLength; i++)
1171  {
1172  if (PqRecvBuffer[i] == '\0')
1173  {
1174  /* include the '\0' in the copy */
1176  i - PqRecvPointer + 1);
1177  PqRecvPointer = i + 1; /* advance past \0 */
1178  return 0;
1179  }
1180  }
1181 
1182  /* If we're here we haven't got the \0 in the buffer yet. */
1184  PqRecvLength - PqRecvPointer);
1186  }
1187 }
static char PqRecvBuffer[PQ_RECV_BUFFER_SIZE]
Definition: pqcomm.c:140
static int PqRecvLength
Definition: pqcomm.c:142
static int pq_recvbuf(void)
Definition: pqcomm.c:926
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:75
#define Assert(condition)
Definition: c.h:745
int i
static int PqRecvPointer
Definition: pqcomm.c:141
static bool PqCommReadingMsg
Definition: pqcomm.c:148
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:227

◆ pq_gettcpusertimeout()

int pq_gettcpusertimeout ( Port port)

Definition at line 1918 of file pqcomm.c.

References SockAddr::addr, Port::default_tcp_user_timeout, elog, IS_AF_UNIX, Port::laddr, LOG, Port::sock, and Port::tcp_user_timeout.

Referenced by pq_settcpusertimeout(), and show_tcp_user_timeout().

1919 {
1920 #ifdef TCP_USER_TIMEOUT
1921  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1922  return 0;
1923 
1924  if (port->tcp_user_timeout != 0)
1925  return port->tcp_user_timeout;
1926 
1927  if (port->default_tcp_user_timeout == 0)
1928  {
1929  ACCEPT_TYPE_ARG3 size = sizeof(port->default_tcp_user_timeout);
1930 
1931  if (getsockopt(port->sock, IPPROTO_TCP, TCP_USER_TIMEOUT,
1932  (char *) &port->default_tcp_user_timeout,
1933  &size) < 0)
1934  {
1935  elog(LOG, "getsockopt(%s) failed: %m", "TCP_USER_TIMEOUT");
1936  port->default_tcp_user_timeout = -1; /* don't know */
1937  }
1938  }
1939 
1940  return port->default_tcp_user_timeout;
1941 #else
1942  return 0;
1943 #endif
1944 }
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define LOG
Definition: elog.h:26
pgsocket sock
Definition: libpq-be.h:122
#define IS_AF_UNIX(fam)
Definition: ip.h:24
int tcp_user_timeout
Definition: libpq-be.h:171
SockAddr laddr
Definition: libpq-be.h:125
#define elog(elevel,...)
Definition: elog.h:214
int default_tcp_user_timeout
Definition: libpq-be.h:167

◆ pq_init()

void pq_init ( void  )

Definition at line 192 of file pqcomm.c.

References AddWaitEventToSet(), COMMERROR, CreateWaitEventSet(), DoingCopyOut, ereport, errmsg(), MemoryContextAlloc(), MyLatch, MyProcPort, on_proc_exit(), pg_set_noblock(), 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().

193 {
194  /* initialize state variables */
198  PqCommBusy = false;
199  PqCommReadingMsg = false;
200  DoingCopyOut = false;
201 
202  /* set up process-exit hook to close the socket */
204 
205  /*
206  * In backends (as soon as forked) we operate the underlying socket in
207  * nonblocking mode and use latches to implement blocking semantics if
208  * needed. That allows us to provide safely interruptible reads and
209  * writes.
210  *
211  * Use COMMERROR on failure, because ERROR would try to send the error to
212  * the client, which might require changing the mode again, leading to
213  * infinite recursion.
214  */
215 #ifndef WIN32
218  (errmsg("could not set socket to nonblocking mode: %m")));
219 #endif
220 
223  NULL, NULL);
226 }
#define WL_SOCKET_WRITEABLE
Definition: latch.h:126
static bool DoingCopyOut
Definition: pqcomm.c:149
struct Port * MyProcPort
Definition: globals.c:43
int AddWaitEventToSet(WaitEventSet *set, uint32 events, pgsocket fd, Latch *latch, void *user_data)
Definition: latch.c:808
void on_proc_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:305
static char * PqSendBuffer
Definition: pqcomm.c:135
static int PqSendStart
Definition: pqcomm.c:138
WaitEventSet * FeBeWaitSet
Definition: pqcomm.c:184
pgsocket sock
Definition: libpq-be.h:122
WaitEventSet * CreateWaitEventSet(MemoryContext context, int nevents)
Definition: latch.c:611
static int PqRecvLength
Definition: pqcomm.c:142
static int PqSendPointer
Definition: pqcomm.c:137
#define COMMERROR
Definition: elog.h:30
MemoryContext TopMemoryContext
Definition: mcxt.c:44
static int PqSendBufferSize
Definition: pqcomm.c:136
static bool PqCommBusy
Definition: pqcomm.c:147
#define WL_POSTMASTER_DEATH
Definition: latch.h:128
#define ereport(elevel,...)
Definition: elog.h:144
bool pg_set_noblock(pgsocket sock)
Definition: noblock.c:25
static void socket_close(int code, Datum arg)
Definition: pqcomm.c:254
int errmsg(const char *fmt,...)
Definition: elog.c:824
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:797
struct Latch * MyLatch
Definition: globals.c:54
static int PqRecvPointer
Definition: pqcomm.c:141
static bool PqCommReadingMsg
Definition: pqcomm.c:148
#define WL_LATCH_SET
Definition: latch.h:124
#define PQ_SEND_BUFFER_SIZE
Definition: pqcomm.c:132

◆ pq_is_reading_msg()

bool pq_is_reading_msg ( void  )

Definition at line 1237 of file pqcomm.c.

References PqCommReadingMsg.

Referenced by PostgresMain().

1238 {
1239  return PqCommReadingMsg;
1240 }
static bool PqCommReadingMsg
Definition: pqcomm.c:148

◆ pq_peekbyte()

int pq_peekbyte ( void  )

Definition at line 1006 of file pqcomm.c.

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

Referenced by recv_password_packet().

1007 {
1009 
1010  while (PqRecvPointer >= PqRecvLength)
1011  {
1012  if (pq_recvbuf()) /* If nothing in buffer, then recv some */
1013  return EOF; /* Failed to recv data */
1014  }
1015  return (unsigned char) PqRecvBuffer[PqRecvPointer];
1016 }
static char PqRecvBuffer[PQ_RECV_BUFFER_SIZE]
Definition: pqcomm.c:140
static int PqRecvLength
Definition: pqcomm.c:142
static int pq_recvbuf(void)
Definition: pqcomm.c:926
#define Assert(condition)
Definition: c.h:745
static int PqRecvPointer
Definition: pqcomm.c:141
static bool PqCommReadingMsg
Definition: pqcomm.c:148

◆ pq_putbytes()

int pq_putbytes ( const char *  s,
size_t  len 
)

Definition at line 1340 of file pqcomm.c.

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

Referenced by CopySendEndOfRow(), and socket_endcopyout().

1341 {
1342  int res;
1343 
1344  /* Should only be called by old-style COPY OUT */
1346  /* No-op if reentrant call */
1347  if (PqCommBusy)
1348  return 0;
1349  PqCommBusy = true;
1350  res = internal_putbytes(s, len);
1351  PqCommBusy = false;
1352  return res;
1353 }
static bool DoingCopyOut
Definition: pqcomm.c:149
static int internal_putbytes(const char *s, size_t len)
Definition: pqcomm.c:1356
static bool PqCommBusy
Definition: pqcomm.c:147
#define Assert(condition)
Definition: c.h:745

◆ pq_recvbuf()

static int pq_recvbuf ( void  )
static

Definition at line 926 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(), pq_getstring(), and pq_peekbyte().

927 {
928  if (PqRecvPointer > 0)
929  {
931  {
932  /* still some unread data, left-justify it in the buffer */
936  PqRecvPointer = 0;
937  }
938  else
940  }
941 
942  /* Ensure that we're in blocking mode */
943  socket_set_nonblocking(false);
944 
945  /* Can fill buffer from PqRecvLength and upwards */
946  for (;;)
947  {
948  int r;
949 
952 
953  if (r < 0)
954  {
955  if (errno == EINTR)
956  continue; /* Ok if interrupted */
957 
958  /*
959  * Careful: an ereport() that tries to write to the client would
960  * cause recursion to here, leading to stack overflow and core
961  * dump! This message must go *only* to the postmaster log.
962  */
965  errmsg("could not receive data from client: %m")));
966  return EOF;
967  }
968  if (r == 0)
969  {
970  /*
971  * EOF detected. We used to write a log message here, but it's
972  * better to expect the ultimate caller to do that.
973  */
974  return EOF;
975  }
976  /* r contains number of bytes read, so just incr length */
977  PqRecvLength += r;
978  return 0;
979  }
980 }
static char PqRecvBuffer[PQ_RECV_BUFFER_SIZE]
Definition: pqcomm.c:140
struct Port * MyProcPort
Definition: globals.c:43
ssize_t secure_read(Port *port, void *ptr, size_t len)
Definition: be-secure.c:145
static void socket_set_nonblocking(bool nonblocking)
Definition: pqcomm.c:909
static int PqRecvLength
Definition: pqcomm.c:142
#define COMMERROR
Definition: elog.h:30
int errcode_for_socket_access(void)
Definition: elog.c:704
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define PQ_RECV_BUFFER_SIZE
Definition: pqcomm.c:133
static int PqRecvPointer
Definition: pqcomm.c:141
#define EINTR
Definition: win32_port.h:323

◆ pq_setkeepalivescount()

int pq_setkeepalivescount ( int  count,
Port port 
)

Definition at line 1875 of file pqcomm.c.

References SockAddr::addr, Port::default_keepalives_count, elog, 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().

1876 {
1877  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1878  return STATUS_OK;
1879 
1880 #ifdef TCP_KEEPCNT
1881  if (count == port->keepalives_count)
1882  return STATUS_OK;
1883 
1884  if (port->default_keepalives_count <= 0)
1885  {
1886  if (pq_getkeepalivescount(port) < 0)
1887  {
1888  if (count == 0)
1889  return STATUS_OK; /* default is set but unknown */
1890  else
1891  return STATUS_ERROR;
1892  }
1893  }
1894 
1895  if (count == 0)
1896  count = port->default_keepalives_count;
1897 
1898  if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPCNT,
1899  (char *) &count, sizeof(count)) < 0)
1900  {
1901  elog(LOG, "setsockopt(%s) failed: %m", "TCP_KEEPCNT");
1902  return STATUS_ERROR;
1903  }
1904 
1905  port->keepalives_count = count;
1906 #else
1907  if (count != 0)
1908  {
1909  elog(LOG, "setsockopt(%s) not supported", "TCP_KEEPCNT");
1910  return STATUS_ERROR;
1911  }
1912 #endif
1913 
1914  return STATUS_OK;
1915 }
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define STATUS_ERROR
Definition: c.h:1112
#define LOG
Definition: elog.h:26
int default_keepalives_count
Definition: libpq-be.h:166
pgsocket sock
Definition: libpq-be.h:122
int keepalives_count
Definition: libpq-be.h:170
#define IS_AF_UNIX(fam)
Definition: ip.h:24
#define STATUS_OK
Definition: c.h:1111
SockAddr laddr
Definition: libpq-be.h:125
int pq_getkeepalivescount(Port *port)
Definition: pqcomm.c:1846
#define elog(elevel,...)
Definition: elog.h:214

◆ pq_setkeepalivesidle()

int pq_setkeepalivesidle ( int  idle,
Port port 
)

Definition at line 1717 of file pqcomm.c.

References SockAddr::addr, Port::default_keepalives_idle, elog, 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().

1718 {
1719  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1720  return STATUS_OK;
1721 
1722 /* check SIO_KEEPALIVE_VALS here, not just WIN32, as some toolchains lack it */
1723 #if defined(PG_TCP_KEEPALIVE_IDLE) || defined(SIO_KEEPALIVE_VALS)
1724  if (idle == port->keepalives_idle)
1725  return STATUS_OK;
1726 
1727 #ifndef WIN32
1728  if (port->default_keepalives_idle <= 0)
1729  {
1730  if (pq_getkeepalivesidle(port) < 0)
1731  {
1732  if (idle == 0)
1733  return STATUS_OK; /* default is set but unknown */
1734  else
1735  return STATUS_ERROR;
1736  }
1737  }
1738 
1739  if (idle == 0)
1740  idle = port->default_keepalives_idle;
1741 
1742  if (setsockopt(port->sock, IPPROTO_TCP, PG_TCP_KEEPALIVE_IDLE,
1743  (char *) &idle, sizeof(idle)) < 0)
1744  {
1745  elog(LOG, "setsockopt(%s) failed: %m", PG_TCP_KEEPALIVE_IDLE_STR);
1746  return STATUS_ERROR;
1747  }
1748 
1749  port->keepalives_idle = idle;
1750 #else /* WIN32 */
1751  return pq_setkeepaliveswin32(port, idle, port->keepalives_interval);
1752 #endif
1753 #else
1754  if (idle != 0)
1755  {
1756  elog(LOG, "setting the keepalive idle time is not supported");
1757  return STATUS_ERROR;
1758  }
1759 #endif
1760 
1761  return STATUS_OK;
1762 }
int keepalives_idle
Definition: libpq-be.h:168
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define STATUS_ERROR
Definition: c.h:1112
#define LOG
Definition: elog.h:26
pgsocket sock
Definition: libpq-be.h:122
#define IS_AF_UNIX(fam)
Definition: ip.h:24
#define STATUS_OK
Definition: c.h:1111
SockAddr laddr
Definition: libpq-be.h:125
int default_keepalives_idle
Definition: libpq-be.h:164
int pq_getkeepalivesidle(Port *port)
Definition: pqcomm.c:1683
#define elog(elevel,...)
Definition: elog.h:214
int keepalives_interval
Definition: libpq-be.h:169

◆ pq_setkeepalivesinterval()

int pq_setkeepalivesinterval ( int  interval,
Port port 
)

Definition at line 1799 of file pqcomm.c.

References SockAddr::addr, Port::default_keepalives_interval, elog, 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().

1800 {
1801  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1802  return STATUS_OK;
1803 
1804 #if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
1805  if (interval == port->keepalives_interval)
1806  return STATUS_OK;
1807 
1808 #ifndef WIN32
1809  if (port->default_keepalives_interval <= 0)
1810  {
1811  if (pq_getkeepalivesinterval(port) < 0)
1812  {
1813  if (interval == 0)
1814  return STATUS_OK; /* default is set but unknown */
1815  else
1816  return STATUS_ERROR;
1817  }
1818  }
1819 
1820  if (interval == 0)
1822 
1823  if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPINTVL,
1824  (char *) &interval, sizeof(interval)) < 0)
1825  {
1826  elog(LOG, "setsockopt(%s) failed: %m", "TCP_KEEPINTVL");
1827  return STATUS_ERROR;
1828  }
1829 
1830  port->keepalives_interval = interval;
1831 #else /* WIN32 */
1832  return pq_setkeepaliveswin32(port, port->keepalives_idle, interval);
1833 #endif
1834 #else
1835  if (interval != 0)
1836  {
1837  elog(LOG, "setsockopt(%s) not supported", "TCP_KEEPINTVL");
1838  return STATUS_ERROR;
1839  }
1840 #endif
1841 
1842  return STATUS_OK;
1843 }
int keepalives_idle
Definition: libpq-be.h:168
int pq_getkeepalivesinterval(Port *port)
Definition: pqcomm.c:1765
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define STATUS_ERROR
Definition: c.h:1112
#define LOG
Definition: elog.h:26
int default_keepalives_interval
Definition: libpq-be.h:165
pgsocket sock
Definition: libpq-be.h:122
#define IS_AF_UNIX(fam)
Definition: ip.h:24
#define STATUS_OK
Definition: c.h:1111
SockAddr laddr
Definition: libpq-be.h:125
#define elog(elevel,...)
Definition: elog.h:214
int keepalives_interval
Definition: libpq-be.h:169

◆ pq_settcpusertimeout()

int pq_settcpusertimeout ( int  timeout,
Port port 
)

Definition at line 1947 of file pqcomm.c.

References SockAddr::addr, Port::default_tcp_user_timeout, elog, 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().

1948 {
1949  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1950  return STATUS_OK;
1951 
1952 #ifdef TCP_USER_TIMEOUT
1953  if (timeout == port->tcp_user_timeout)
1954  return STATUS_OK;
1955 
1956  if (port->default_tcp_user_timeout <= 0)
1957  {
1958  if (pq_gettcpusertimeout(port) < 0)
1959  {
1960  if (timeout == 0)
1961  return STATUS_OK; /* default is set but unknown */
1962  else
1963  return STATUS_ERROR;
1964  }
1965  }
1966 
1967  if (timeout == 0)
1968  timeout = port->default_tcp_user_timeout;
1969 
1970  if (setsockopt(port->sock, IPPROTO_TCP, TCP_USER_TIMEOUT,
1971  (char *) &timeout, sizeof(timeout)) < 0)
1972  {
1973  elog(LOG, "setsockopt(%s) failed: %m", "TCP_USER_TIMEOUT");
1974  return STATUS_ERROR;
1975  }
1976 
1977  port->tcp_user_timeout = timeout;
1978 #else
1979  if (timeout != 0)
1980  {
1981  elog(LOG, "setsockopt(%s) not supported", "TCP_USER_TIMEOUT");
1982  return STATUS_ERROR;
1983  }
1984 #endif
1985 
1986  return STATUS_OK;
1987 }
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define STATUS_ERROR
Definition: c.h:1112
#define LOG
Definition: elog.h:26
pgsocket sock
Definition: libpq-be.h:122
int pq_gettcpusertimeout(Port *port)
Definition: pqcomm.c:1918
#define IS_AF_UNIX(fam)
Definition: ip.h:24
int tcp_user_timeout
Definition: libpq-be.h:171
#define STATUS_OK
Definition: c.h:1111
SockAddr laddr
Definition: libpq-be.h:125
#define elog(elevel,...)
Definition: elog.h:214
int default_tcp_user_timeout
Definition: libpq-be.h:167

◆ pq_startmsgread()

void pq_startmsgread ( void  )

Definition at line 1197 of file pqcomm.c.

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

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

1198 {
1199  /*
1200  * There shouldn't be a read active already, but let's check just to be
1201  * sure.
1202  */
1203  if (PqCommReadingMsg)
1204  ereport(FATAL,
1205  (errcode(ERRCODE_PROTOCOL_VIOLATION),
1206  errmsg("terminating connection because protocol synchronization was lost")));
1207 
1208  PqCommReadingMsg = true;
1209 }
int errcode(int sqlerrcode)
Definition: elog.c:610
#define FATAL
Definition: elog.h:52
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824
static bool PqCommReadingMsg
Definition: pqcomm.c:148

◆ RemoveSocketFiles()

void RemoveSocketFiles ( void  )

Definition at line 876 of file pqcomm.c.

References lfirst, and NIL.

Referenced by CloseServerPorts().

877 {
878  ListCell *l;
879 
880  /* Loop through all created sockets... */
881  foreach(l, sock_paths)
882  {
883  char *sock_path = (char *) lfirst(l);
884 
885  /* Ignore any error. */
886  (void) unlink(sock_path);
887  }
888  /* Since we're about to exit, no need to reclaim storage */
889  sock_paths = NIL;
890 }
#define NIL
Definition: pg_list.h:65
static List * sock_paths
Definition: pqcomm.c:123
#define lfirst(lc)
Definition: pg_list.h:190

◆ socket_close()

static void socket_close ( int  code,
Datum  arg 
)
static

Definition at line 254 of file pqcomm.c.

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

Referenced by pq_init().

255 {
256  /* Nothing to do in a standalone backend, where MyProcPort is NULL. */
257  if (MyProcPort != NULL)
258  {
259 #if defined(ENABLE_GSS) || defined(ENABLE_SSPI)
260 #ifdef ENABLE_GSS
261  OM_uint32 min_s;
262 
263  /*
264  * Shutdown GSSAPI layer. This section does nothing when interrupting
265  * BackendInitialize(), because pg_GSS_recvauth() makes first use of
266  * "ctx" and "cred".
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 #endif /* ENABLE_GSS */
274 
275  /*
276  * GSS and SSPI share the port->gss struct. Since nowhere else does a
277  * postmaster child free this, doing so is safe when interrupting
278  * BackendInitialize().
279  */
280  free(MyProcPort->gss);
281 #endif /* ENABLE_GSS || ENABLE_SSPI */
282 
283  /*
284  * Cleanly shut down SSL layer. Nowhere else does a postmaster child
285  * call this, so this is safe when interrupting BackendInitialize().
286  */
288 
289  /*
290  * Formerly we did an explicit close() here, but it seems better to
291  * leave the socket open until the process dies. This allows clients
292  * to perform a "synchronous close" if they care --- wait till the
293  * transport layer reports connection closure, and you can be sure the
294  * backend has exited.
295  *
296  * We do set sock to PGINVALID_SOCKET to prevent any further I/O,
297  * though.
298  */
300  }
301 }
struct Port * MyProcPort
Definition: globals.c:43
pgsocket sock
Definition: libpq-be.h:122
#define PGINVALID_SOCKET
Definition: port.h:33
#define free(a)
Definition: header.h:65
void secure_close(Port *port)
Definition: be-secure.c:133
void * gss
Definition: libpq-be.h:184

◆ socket_comm_reset()

static void socket_comm_reset ( void  )
static

Definition at line 237 of file pqcomm.c.

References pq_endcopyout, and PqCommBusy.

238 {
239  /* Do not throw away pending data, but do reset the busy flag */
240  PqCommBusy = false;
241  /* We can abort any old-style COPY OUT, too */
242  pq_endcopyout(true);
243 }
static bool PqCommBusy
Definition: pqcomm.c:147
#define pq_endcopyout(errorAbort)
Definition: libpq.h:47

◆ socket_endcopyout()

static void socket_endcopyout ( bool  errorAbort)
static

Definition at line 1623 of file pqcomm.c.

References DoingCopyOut, elog, Port::keepalives_idle, Port::keepalives_interval, LOG, port, pq_putbytes(), Port::sock, STATUS_ERROR, and STATUS_OK.

1624 {
1625  if (!DoingCopyOut)
1626  return;
1627  if (errorAbort)
1628  pq_putbytes("\n\n\\.\n", 5);
1629  /* in non-error case, copy.c will have emitted the terminator line */
1630  DoingCopyOut = false;
1631 }
static bool DoingCopyOut
Definition: pqcomm.c:149
int pq_putbytes(const char *s, size_t len)
Definition: pqcomm.c:1340

◆ socket_flush()

static int socket_flush ( void  )
static

Definition at line 1387 of file pqcomm.c.

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

1388 {
1389  int res;
1390 
1391  /* No-op if reentrant call */
1392  if (PqCommBusy)
1393  return 0;
1394  PqCommBusy = true;
1395  socket_set_nonblocking(false);
1396  res = internal_flush();
1397  PqCommBusy = false;
1398  return res;
1399 }
static int internal_flush(void)
Definition: pqcomm.c:1409
static void socket_set_nonblocking(bool nonblocking)
Definition: pqcomm.c:909
static bool PqCommBusy
Definition: pqcomm.c:147

◆ socket_flush_if_writable()

static int socket_flush_if_writable ( void  )
static

Definition at line 1482 of file pqcomm.c.

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

1483 {
1484  int res;
1485 
1486  /* Quick exit if nothing to do */
1487  if (PqSendPointer == PqSendStart)
1488  return 0;
1489 
1490  /* No-op if reentrant call */
1491  if (PqCommBusy)
1492  return 0;
1493 
1494  /* Temporarily put the socket into non-blocking mode */
1495  socket_set_nonblocking(true);
1496 
1497  PqCommBusy = true;
1498  res = internal_flush();
1499  PqCommBusy = false;
1500  return res;
1501 }
static int PqSendStart
Definition: pqcomm.c:138
static int internal_flush(void)
Definition: pqcomm.c:1409
static void socket_set_nonblocking(bool nonblocking)
Definition: pqcomm.c:909
static int PqSendPointer
Definition: pqcomm.c:137
static bool PqCommBusy
Definition: pqcomm.c:147

◆ socket_is_send_pending()

static bool socket_is_send_pending ( void  )
static

Definition at line 1508 of file pqcomm.c.

References PqSendPointer, and PqSendStart.

1509 {
1510  return (PqSendStart < PqSendPointer);
1511 }
static int PqSendStart
Definition: pqcomm.c:138
static int PqSendPointer
Definition: pqcomm.c:137

◆ socket_putmessage()

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

Definition at line 1547 of file pqcomm.c.

References DoingCopyOut, FrontendProtocol, internal_putbytes(), pg_hton32, PG_PROTOCOL_MAJOR, and PqCommBusy.

1548 {
1549  if (DoingCopyOut || PqCommBusy)
1550  return 0;
1551  PqCommBusy = true;
1552  if (msgtype)
1553  if (internal_putbytes(&msgtype, 1))
1554  goto fail;
1556  {
1557  uint32 n32;
1558 
1559  n32 = pg_hton32((uint32) (len + 4));
1560  if (internal_putbytes((char *) &n32, 4))
1561  goto fail;
1562  }
1563  if (internal_putbytes(s, len))
1564  goto fail;
1565  PqCommBusy = false;
1566  return 0;
1567 
1568 fail:
1569  PqCommBusy = false;
1570  return EOF;
1571 }
static bool DoingCopyOut
Definition: pqcomm.c:149
#define PG_PROTOCOL_MAJOR(v)
Definition: pqcomm.h:104
#define pg_hton32(x)
Definition: pg_bswap.h:121
unsigned int uint32
Definition: c.h:374
static int internal_putbytes(const char *s, size_t len)
Definition: pqcomm.c:1356
static bool PqCommBusy
Definition: pqcomm.c:147
ProtocolVersion FrontendProtocol
Definition: globals.c:28

◆ socket_putmessage_noblock()

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

Definition at line 1580 of file pqcomm.c.

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

1581 {
1582  int res PG_USED_FOR_ASSERTS_ONLY;
1583  int required;
1584 
1585  /*
1586  * Ensure we have enough space in the output buffer for the message header
1587  * as well as the message itself.
1588  */
1589  required = PqSendPointer + 1 + 4 + len;
1590  if (required > PqSendBufferSize)
1591  {
1592  PqSendBuffer = repalloc(PqSendBuffer, required);
1594  }
1595  res = pq_putmessage(msgtype, s, len);
1596  Assert(res == 0); /* should not fail when the message fits in
1597  * buffer */
1598 }
static char * PqSendBuffer
Definition: pqcomm.c:135
static int PqSendPointer
Definition: pqcomm.c:137
static int PqSendBufferSize
Definition: pqcomm.c:136
#define Assert(condition)
Definition: c.h:745
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1070
#define pq_putmessage(msgtype, s, len)
Definition: libpq.h:42
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:121

◆ socket_set_nonblocking()

static void socket_set_nonblocking ( bool  nonblocking)
static

Definition at line 909 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().

910 {
911  if (MyProcPort == NULL)
912  ereport(ERROR,
913  (errcode(ERRCODE_CONNECTION_DOES_NOT_EXIST),
914  errmsg("there is no client connection")));
915 
916  MyProcPort->noblock = nonblocking;
917 }
struct Port * MyProcPort
Definition: globals.c:43
int errcode(int sqlerrcode)
Definition: elog.c:610
#define ERROR
Definition: elog.h:43
#define ereport(elevel,...)
Definition: elog.h:144
bool noblock
Definition: libpq-be.h:123
int errmsg(const char *fmt,...)
Definition: elog.c:824

◆ socket_startcopyout()

static void socket_startcopyout ( void  )
static

Definition at line 1607 of file pqcomm.c.

References DoingCopyOut.

1608 {
1609  DoingCopyOut = true;
1610 }
static bool DoingCopyOut
Definition: pqcomm.c:149

◆ StreamClose()

void StreamClose ( pgsocket  sock)

Definition at line 843 of file pqcomm.c.

References closesocket.

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

844 {
845  closesocket(sock);
846 }
#define closesocket
Definition: port.h:309

◆ StreamConnection()

int StreamConnection ( pgsocket  server_fd,
Port port 
)

Definition at line 714 of file pqcomm.c.

References accept, SockAddr::addr, elog, 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().

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

◆ StreamServerPort()

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

Definition at line 328 of file pqcomm.c.

References _, addrinfo::ai_family, addrinfo::ai_flags, addrinfo::ai_next, AI_PASSIVE, addrinfo::ai_socktype, Assert, bind, closesocket, CreateSocketLockFile(), 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().

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

◆ TouchSocketFiles()

void TouchSocketFiles ( void  )

Definition at line 858 of file pqcomm.c.

References lfirst.

Referenced by ServerLoop().

859 {
860  ListCell *l;
861 
862  /* Loop through all created sockets... */
863  foreach(l, sock_paths)
864  {
865  char *sock_path = (char *) lfirst(l);
866 
867  /* Ignore errors; there's no point in complaining */
868  (void) utime(sock_path, NULL);
869  }
870 }
static List * sock_paths
Definition: pqcomm.c:123
#define lfirst(lc)
Definition: pg_list.h:190

Variable Documentation

◆ DoingCopyOut

bool DoingCopyOut
static

◆ FeBeWaitSet

WaitEventSet* FeBeWaitSet

Definition at line 184 of file pqcomm.c.

Referenced by secure_read(), secure_write(), SwitchBackToLocalLatch(), and SwitchToSharedLatch().

◆ PqCommBusy

bool PqCommBusy
static

◆ PqCommMethods

const PQcommMethods* PqCommMethods = &PqCommSocketMethods

Definition at line 182 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:1482
static bool socket_is_send_pending(void)
Definition: pqcomm.c:1508
static void socket_comm_reset(void)
Definition: pqcomm.c:237
static void socket_startcopyout(void)
Definition: pqcomm.c:1607
static void socket_endcopyout(bool errorAbort)
Definition: pqcomm.c:1623
static int socket_putmessage(char msgtype, const char *s, size_t len)
Definition: pqcomm.c:1547
static int socket_flush(void)
Definition: pqcomm.c:1387
static void socket_putmessage_noblock(char msgtype, const char *s, size_t len)
Definition: pqcomm.c:1580

Definition at line 171 of file pqcomm.c.

◆ PqRecvBuffer

char PqRecvBuffer[PQ_RECV_BUFFER_SIZE]
static

◆ PqRecvLength

int PqRecvLength
static

◆ PqRecvPointer

int PqRecvPointer
static

◆ PqSendBuffer

char* PqSendBuffer
static

Definition at line 135 of file pqcomm.c.

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

◆ PqSendBufferSize

int PqSendBufferSize
static

Definition at line 136 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 123 of file pqcomm.c.

◆ Unix_socket_group

char* Unix_socket_group

Definition at line 120 of file pqcomm.c.

Referenced by StreamServerPort().

◆ Unix_socket_permissions

int Unix_socket_permissions

Definition at line 119 of file pqcomm.c.

Referenced by show_unix_socket_permissions(), and StreamServerPort().