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 "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, char *hostName, unsigned short portNumber, 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 135 of file pqcomm.c.

Referenced by pq_recvbuf().

◆ PQ_SEND_BUFFER_SIZE

#define PQ_SEND_BUFFER_SIZE   8192

Definition at line 134 of file pqcomm.c.

Referenced by pq_init(), and StreamConnection().

Function Documentation

◆ internal_flush()

static int internal_flush ( void  )
static

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

1424 {
1425  static int last_reported_send_errno = 0;
1426 
1427  char *bufptr = PqSendBuffer + PqSendStart;
1428  char *bufend = PqSendBuffer + PqSendPointer;
1429 
1430  while (bufptr < bufend)
1431  {
1432  int r;
1433 
1434  r = secure_write(MyProcPort, bufptr, bufend - bufptr);
1435 
1436  if (r <= 0)
1437  {
1438  if (errno == EINTR)
1439  continue; /* Ok if we were interrupted */
1440 
1441  /*
1442  * Ok if no data writable without blocking, and the socket is in
1443  * non-blocking mode.
1444  */
1445  if (errno == EAGAIN ||
1446  errno == EWOULDBLOCK)
1447  {
1448  return 0;
1449  }
1450 
1451  /*
1452  * Careful: an ereport() that tries to write to the client would
1453  * cause recursion to here, leading to stack overflow and core
1454  * dump! This message must go *only* to the postmaster log.
1455  *
1456  * If a client disconnects while we're in the midst of output, we
1457  * might write quite a bit of data before we get to a safe query
1458  * abort point. So, suppress duplicate log messages.
1459  */
1460  if (errno != last_reported_send_errno)
1461  {
1462  last_reported_send_errno = errno;
1465  errmsg("could not send data to client: %m")));
1466  }
1467 
1468  /*
1469  * We drop the buffered data anyway so that processing can
1470  * continue, even though we'll probably quit soon. We also set a
1471  * flag that'll cause the next CHECK_FOR_INTERRUPTS to terminate
1472  * the connection.
1473  */
1474  PqSendStart = PqSendPointer = 0;
1476  InterruptPending = 1;
1477  return EOF;
1478  }
1479 
1480  last_reported_send_errno = 0; /* reset after any successful send */
1481  bufptr += r;
1482  PqSendStart += r;
1483  }
1484 
1485  PqSendStart = PqSendPointer = 0;
1486  return 0;
1487 }
struct Port * MyProcPort
Definition: globals.c:43
#define EAGAIN
Definition: win32_port.h:330
static char * PqSendBuffer
Definition: pqcomm.c:137
static int PqSendStart
Definition: pqcomm.c:140
static int PqSendPointer
Definition: pqcomm.c:139
#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:259
#define ereport(elevel, rest)
Definition: elog.h:141
int errcode_for_socket_access(void)
Definition: elog.c:664
volatile sig_atomic_t InterruptPending
Definition: globals.c:30
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define EWOULDBLOCK
Definition: win32_port.h:338
#define EINTR
Definition: win32_port.h:332

◆ internal_putbytes()

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

Definition at line 1370 of file pqcomm.c.

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

Referenced by pq_putbytes(), and socket_putmessage().

1371 {
1372  size_t amount;
1373 
1374  while (len > 0)
1375  {
1376  /* If buffer is full, then flush it out */
1378  {
1379  socket_set_nonblocking(false);
1380  if (internal_flush())
1381  return EOF;
1382  }
1383  amount = PqSendBufferSize - PqSendPointer;
1384  if (amount > len)
1385  amount = len;
1386  memcpy(PqSendBuffer + PqSendPointer, s, amount);
1387  PqSendPointer += amount;
1388  s += amount;
1389  len -= amount;
1390  }
1391  return 0;
1392 }
static char * PqSendBuffer
Definition: pqcomm.c:137
static int internal_flush(void)
Definition: pqcomm.c:1423
static void socket_set_nonblocking(bool nonblocking)
Definition: pqcomm.c:923
static int PqSendPointer
Definition: pqcomm.c:139
static int PqSendBufferSize
Definition: pqcomm.c:138

◆ pq_discardbytes()

static int pq_discardbytes ( size_t  len)
static

Definition at line 1129 of file pqcomm.c.

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

Referenced by pq_getmessage().

1130 {
1131  size_t amount;
1132 
1134 
1135  while (len > 0)
1136  {
1137  while (PqRecvPointer >= PqRecvLength)
1138  {
1139  if (pq_recvbuf()) /* If nothing in buffer, then recv some */
1140  return EOF; /* Failed to recv data */
1141  }
1142  amount = PqRecvLength - PqRecvPointer;
1143  if (amount > len)
1144  amount = len;
1145  PqRecvPointer += amount;
1146  len -= amount;
1147  }
1148  return 0;
1149 }
static int PqRecvLength
Definition: pqcomm.c:144
static int pq_recvbuf(void)
Definition: pqcomm.c:940
#define Assert(condition)
Definition: c.h:732
static int PqRecvPointer
Definition: pqcomm.c:143
static bool PqCommReadingMsg
Definition: pqcomm.c:150

◆ pq_endmsgread()

void pq_endmsgread ( void  )

Definition at line 1235 of file pqcomm.c.

References Assert, and PqCommReadingMsg.

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

1236 {
1238 
1239  PqCommReadingMsg = false;
1240 }
#define Assert(condition)
Definition: c.h:732
static bool PqCommReadingMsg
Definition: pqcomm.c:150

◆ pq_getbyte()

int pq_getbyte ( void  )

Definition at line 1001 of file pqcomm.c.

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

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

1002 {
1004 
1005  while (PqRecvPointer >= PqRecvLength)
1006  {
1007  if (pq_recvbuf()) /* If nothing in buffer, then recv some */
1008  return EOF; /* Failed to recv data */
1009  }
1010  return (unsigned char) PqRecvBuffer[PqRecvPointer++];
1011 }
static char PqRecvBuffer[PQ_RECV_BUFFER_SIZE]
Definition: pqcomm.c:142
static int PqRecvLength
Definition: pqcomm.c:144
static int pq_recvbuf(void)
Definition: pqcomm.c:940
#define Assert(condition)
Definition: c.h:732
static int PqRecvPointer
Definition: pqcomm.c:143
static bool PqCommReadingMsg
Definition: pqcomm.c:150

◆ pq_getbyte_if_available()

int pq_getbyte_if_available ( unsigned char *  c)

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

1042 {
1043  int r;
1044 
1046 
1048  {
1050  return 1;
1051  }
1052 
1053  /* Put the socket into non-blocking mode */
1054  socket_set_nonblocking(true);
1055 
1056  r = secure_read(MyProcPort, c, 1);
1057  if (r < 0)
1058  {
1059  /*
1060  * Ok if no data available without blocking or interrupted (though
1061  * EINTR really shouldn't happen with a non-blocking socket). Report
1062  * other errors.
1063  */
1064  if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
1065  r = 0;
1066  else
1067  {
1068  /*
1069  * Careful: an ereport() that tries to write to the client would
1070  * cause recursion to here, leading to stack overflow and core
1071  * dump! This message must go *only* to the postmaster log.
1072  */
1075  errmsg("could not receive data from client: %m")));
1076  r = EOF;
1077  }
1078  }
1079  else if (r == 0)
1080  {
1081  /* EOF detected */
1082  r = EOF;
1083  }
1084 
1085  return r;
1086 }
static char PqRecvBuffer[PQ_RECV_BUFFER_SIZE]
Definition: pqcomm.c:142
struct Port * MyProcPort
Definition: globals.c:43
#define EAGAIN
Definition: win32_port.h:330
ssize_t secure_read(Port *port, void *ptr, size_t len)
Definition: be-secure.c:146
static void socket_set_nonblocking(bool nonblocking)
Definition: pqcomm.c:923
static int PqRecvLength
Definition: pqcomm.c:144
char * c
#define COMMERROR
Definition: elog.h:30
#define ereport(elevel, rest)
Definition: elog.h:141
int errcode_for_socket_access(void)
Definition: elog.c:664
#define Assert(condition)
Definition: c.h:732
int errmsg(const char *fmt,...)
Definition: elog.c:784
static int PqRecvPointer
Definition: pqcomm.c:143
#define EWOULDBLOCK
Definition: win32_port.h:338
static bool PqCommReadingMsg
Definition: pqcomm.c:150
#define EINTR
Definition: win32_port.h:332

◆ pq_getbytes()

int pq_getbytes ( char *  s,
size_t  len 
)

Definition at line 1095 of file pqcomm.c.

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

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

1096 {
1097  size_t amount;
1098 
1100 
1101  while (len > 0)
1102  {
1103  while (PqRecvPointer >= PqRecvLength)
1104  {
1105  if (pq_recvbuf()) /* If nothing in buffer, then recv some */
1106  return EOF; /* Failed to recv data */
1107  }
1108  amount = PqRecvLength - PqRecvPointer;
1109  if (amount > len)
1110  amount = len;
1111  memcpy(s, PqRecvBuffer + PqRecvPointer, amount);
1112  PqRecvPointer += amount;
1113  s += amount;
1114  len -= amount;
1115  }
1116  return 0;
1117 }
static char PqRecvBuffer[PQ_RECV_BUFFER_SIZE]
Definition: pqcomm.c:142
static int PqRecvLength
Definition: pqcomm.c:144
static int pq_recvbuf(void)
Definition: pqcomm.c:940
#define Assert(condition)
Definition: c.h:732
static int PqRecvPointer
Definition: pqcomm.c:143
static bool PqCommReadingMsg
Definition: pqcomm.c:150

◆ pq_getkeepalivescount()

int pq_getkeepalivescount ( Port port)

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

1861 {
1862 #ifdef TCP_KEEPCNT
1863  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1864  return 0;
1865 
1866  if (port->keepalives_count != 0)
1867  return port->keepalives_count;
1868 
1869  if (port->default_keepalives_count == 0)
1870  {
1871  ACCEPT_TYPE_ARG3 size = sizeof(port->default_keepalives_count);
1872 
1873  if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPCNT,
1874  (char *) &port->default_keepalives_count,
1875  &size) < 0)
1876  {
1877  elog(LOG, "getsockopt(%s) failed: %m", "TCP_KEEPCNT");
1878  port->default_keepalives_count = -1; /* don't know */
1879  }
1880  }
1881 
1882  return port->default_keepalives_count;
1883 #else
1884  return 0;
1885 #endif
1886 }
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:226

◆ pq_getkeepalivesidle()

int pq_getkeepalivesidle ( Port port)

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

1698 {
1699 #if defined(PG_TCP_KEEPALIVE_IDLE) || defined(SIO_KEEPALIVE_VALS)
1700  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1701  return 0;
1702 
1703  if (port->keepalives_idle != 0)
1704  return port->keepalives_idle;
1705 
1706  if (port->default_keepalives_idle == 0)
1707  {
1708 #ifndef WIN32
1709  ACCEPT_TYPE_ARG3 size = sizeof(port->default_keepalives_idle);
1710 
1711  if (getsockopt(port->sock, IPPROTO_TCP, PG_TCP_KEEPALIVE_IDLE,
1712  (char *) &port->default_keepalives_idle,
1713  &size) < 0)
1714  {
1715  elog(LOG, "getsockopt(%s) failed: %m", PG_TCP_KEEPALIVE_IDLE_STR);
1716  port->default_keepalives_idle = -1; /* don't know */
1717  }
1718 #else /* WIN32 */
1719  /* We can't get the defaults on Windows, so return "don't know" */
1720  port->default_keepalives_idle = -1;
1721 #endif /* WIN32 */
1722  }
1723 
1724  return port->default_keepalives_idle;
1725 #else
1726  return 0;
1727 #endif
1728 }
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:226

◆ pq_getkeepalivesinterval()

int pq_getkeepalivesinterval ( Port port)

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

1780 {
1781 #if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
1782  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1783  return 0;
1784 
1785  if (port->keepalives_interval != 0)
1786  return port->keepalives_interval;
1787 
1788  if (port->default_keepalives_interval == 0)
1789  {
1790 #ifndef WIN32
1791  ACCEPT_TYPE_ARG3 size = sizeof(port->default_keepalives_interval);
1792 
1793  if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPINTVL,
1794  (char *) &port->default_keepalives_interval,
1795  &size) < 0)
1796  {
1797  elog(LOG, "getsockopt(%s) failed: %m", "TCP_KEEPINTVL");
1798  port->default_keepalives_interval = -1; /* don't know */
1799  }
1800 #else
1801  /* We can't get the defaults on Windows, so return "don't know" */
1802  port->default_keepalives_interval = -1;
1803 #endif /* WIN32 */
1804  }
1805 
1806  return port->default_keepalives_interval;
1807 #else
1808  return 0;
1809 #endif
1810 }
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:226
int keepalives_interval
Definition: libpq-be.h:169

◆ pq_getmessage()

int pq_getmessage ( StringInfo  s,
int  maxlen 
)

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

1274 {
1275  int32 len;
1276 
1278 
1279  resetStringInfo(s);
1280 
1281  /* Read message length word */
1282  if (pq_getbytes((char *) &len, 4) == EOF)
1283  {
1285  (errcode(ERRCODE_PROTOCOL_VIOLATION),
1286  errmsg("unexpected EOF within message length word")));
1287  return EOF;
1288  }
1289 
1290  len = pg_ntoh32(len);
1291 
1292  if (len < 4 ||
1293  (maxlen > 0 && len > maxlen))
1294  {
1296  (errcode(ERRCODE_PROTOCOL_VIOLATION),
1297  errmsg("invalid message length")));
1298  return EOF;
1299  }
1300 
1301  len -= 4; /* discount length itself */
1302 
1303  if (len > 0)
1304  {
1305  /*
1306  * Allocate space for message. If we run out of room (ridiculously
1307  * large message), we will elog(ERROR), but we want to discard the
1308  * message body so as not to lose communication sync.
1309  */
1310  PG_TRY();
1311  {
1312  enlargeStringInfo(s, len);
1313  }
1314  PG_CATCH();
1315  {
1316  if (pq_discardbytes(len) == EOF)
1318  (errcode(ERRCODE_PROTOCOL_VIOLATION),
1319  errmsg("incomplete message from client")));
1320 
1321  /* we discarded the rest of the message so we're back in sync. */
1322  PqCommReadingMsg = false;
1323  PG_RE_THROW();
1324  }
1325  PG_END_TRY();
1326 
1327  /* And grab the message */
1328  if (pq_getbytes(s->data, len) == EOF)
1329  {
1331  (errcode(ERRCODE_PROTOCOL_VIOLATION),
1332  errmsg("incomplete message from client")));
1333  return EOF;
1334  }
1335  s->len = len;
1336  /* Place a trailing null per StringInfo convention */
1337  s->data[len] = '\0';
1338  }
1339 
1340  /* finished reading the message. */
1341  PqCommReadingMsg = false;
1342 
1343  return 0;
1344 }
static int pq_discardbytes(size_t len)
Definition: pqcomm.c:1129
int errcode(int sqlerrcode)
Definition: elog.c:570
signed int int32
Definition: c.h:346
#define pg_ntoh32(x)
Definition: pg_bswap.h:125
int pq_getbytes(char *s, size_t len)
Definition: pqcomm.c:1095
#define COMMERROR
Definition: elog.h:30
void enlargeStringInfo(StringInfo str, int needed)
Definition: stringinfo.c:270
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:62
#define ereport(elevel, rest)
Definition: elog.h:141
#define PG_CATCH()
Definition: elog.h:310
#define Assert(condition)
Definition: c.h:732
#define PG_RE_THROW()
Definition: elog.h:331
int errmsg(const char *fmt,...)
Definition: elog.c:784
static bool PqCommReadingMsg
Definition: pqcomm.c:150
#define PG_TRY()
Definition: elog.h:301
#define PG_END_TRY()
Definition: elog.h:317

◆ pq_getstring()

int pq_getstring ( StringInfo  s)

Definition at line 1167 of file pqcomm.c.

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

Referenced by GetOldFunctionMessage(), and SocketBackend().

1168 {
1169  int i;
1170 
1172 
1173  resetStringInfo(s);
1174 
1175  /* Read until we get the terminating '\0' */
1176  for (;;)
1177  {
1178  while (PqRecvPointer >= PqRecvLength)
1179  {
1180  if (pq_recvbuf()) /* If nothing in buffer, then recv some */
1181  return EOF; /* Failed to recv data */
1182  }
1183 
1184  for (i = PqRecvPointer; i < PqRecvLength; i++)
1185  {
1186  if (PqRecvBuffer[i] == '\0')
1187  {
1188  /* include the '\0' in the copy */
1190  i - PqRecvPointer + 1);
1191  PqRecvPointer = i + 1; /* advance past \0 */
1192  return 0;
1193  }
1194  }
1195 
1196  /* If we're here we haven't got the \0 in the buffer yet. */
1198  PqRecvLength - PqRecvPointer);
1200  }
1201 }
static char PqRecvBuffer[PQ_RECV_BUFFER_SIZE]
Definition: pqcomm.c:142
static int PqRecvLength
Definition: pqcomm.c:144
static int pq_recvbuf(void)
Definition: pqcomm.c:940
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:62
#define Assert(condition)
Definition: c.h:732
int i
static int PqRecvPointer
Definition: pqcomm.c:143
static bool PqCommReadingMsg
Definition: pqcomm.c:150
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:214

◆ pq_gettcpusertimeout()

int pq_gettcpusertimeout ( Port port)

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

1933 {
1934 #ifdef TCP_USER_TIMEOUT
1935  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1936  return 0;
1937 
1938  if (port->tcp_user_timeout != 0)
1939  return port->tcp_user_timeout;
1940 
1941  if (port->default_tcp_user_timeout == 0)
1942  {
1943  ACCEPT_TYPE_ARG3 size = sizeof(port->default_tcp_user_timeout);
1944 
1945  if (getsockopt(port->sock, IPPROTO_TCP, TCP_USER_TIMEOUT,
1946  (char *) &port->default_tcp_user_timeout,
1947  &size) < 0)
1948  {
1949  elog(LOG, "getsockopt(%s) failed: %m", "TCP_USER_TIMEOUT");
1950  port->default_tcp_user_timeout = -1; /* don't know */
1951  }
1952  }
1953 
1954  return port->default_tcp_user_timeout;
1955 #else
1956  return 0;
1957 #endif
1958 }
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:226
int default_tcp_user_timeout
Definition: libpq-be.h:167

◆ pq_init()

void pq_init ( void  )

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

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

◆ pq_is_reading_msg()

bool pq_is_reading_msg ( void  )

Definition at line 1251 of file pqcomm.c.

References PqCommReadingMsg.

Referenced by PostgresMain().

1252 {
1253  return PqCommReadingMsg;
1254 }
static bool PqCommReadingMsg
Definition: pqcomm.c:150

◆ pq_peekbyte()

int pq_peekbyte ( void  )

Definition at line 1020 of file pqcomm.c.

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

Referenced by recv_password_packet().

1021 {
1023 
1024  while (PqRecvPointer >= PqRecvLength)
1025  {
1026  if (pq_recvbuf()) /* If nothing in buffer, then recv some */
1027  return EOF; /* Failed to recv data */
1028  }
1029  return (unsigned char) PqRecvBuffer[PqRecvPointer];
1030 }
static char PqRecvBuffer[PQ_RECV_BUFFER_SIZE]
Definition: pqcomm.c:142
static int PqRecvLength
Definition: pqcomm.c:144
static int pq_recvbuf(void)
Definition: pqcomm.c:940
#define Assert(condition)
Definition: c.h:732
static int PqRecvPointer
Definition: pqcomm.c:143
static bool PqCommReadingMsg
Definition: pqcomm.c:150

◆ pq_putbytes()

int pq_putbytes ( const char *  s,
size_t  len 
)

Definition at line 1354 of file pqcomm.c.

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

Referenced by CopySendEndOfRow(), and socket_endcopyout().

1355 {
1356  int res;
1357 
1358  /* Should only be called by old-style COPY OUT */
1360  /* No-op if reentrant call */
1361  if (PqCommBusy)
1362  return 0;
1363  PqCommBusy = true;
1364  res = internal_putbytes(s, len);
1365  PqCommBusy = false;
1366  return res;
1367 }
static bool DoingCopyOut
Definition: pqcomm.c:151
static int internal_putbytes(const char *s, size_t len)
Definition: pqcomm.c:1370
static bool PqCommBusy
Definition: pqcomm.c:149
#define Assert(condition)
Definition: c.h:732

◆ pq_recvbuf()

static int pq_recvbuf ( void  )
static

Definition at line 940 of file pqcomm.c.

References COMMERROR, EINTR, ereport, errcode_for_socket_access(), errmsg(), memmove, 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().

941 {
942  if (PqRecvPointer > 0)
943  {
945  {
946  /* still some unread data, left-justify it in the buffer */
950  PqRecvPointer = 0;
951  }
952  else
954  }
955 
956  /* Ensure that we're in blocking mode */
957  socket_set_nonblocking(false);
958 
959  /* Can fill buffer from PqRecvLength and upwards */
960  for (;;)
961  {
962  int r;
963 
966 
967  if (r < 0)
968  {
969  if (errno == EINTR)
970  continue; /* Ok if interrupted */
971 
972  /*
973  * Careful: an ereport() that tries to write to the client would
974  * cause recursion to here, leading to stack overflow and core
975  * dump! This message must go *only* to the postmaster log.
976  */
979  errmsg("could not receive data from client: %m")));
980  return EOF;
981  }
982  if (r == 0)
983  {
984  /*
985  * EOF detected. We used to write a log message here, but it's
986  * better to expect the ultimate caller to do that.
987  */
988  return EOF;
989  }
990  /* r contains number of bytes read, so just incr length */
991  PqRecvLength += r;
992  return 0;
993  }
994 }
static char PqRecvBuffer[PQ_RECV_BUFFER_SIZE]
Definition: pqcomm.c:142
struct Port * MyProcPort
Definition: globals.c:43
ssize_t secure_read(Port *port, void *ptr, size_t len)
Definition: be-secure.c:146
static void socket_set_nonblocking(bool nonblocking)
Definition: pqcomm.c:923
static int PqRecvLength
Definition: pqcomm.c:144
#define memmove(d, s, c)
Definition: c.h:1238
#define COMMERROR
Definition: elog.h:30
#define ereport(elevel, rest)
Definition: elog.h:141
int errcode_for_socket_access(void)
Definition: elog.c:664
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define PQ_RECV_BUFFER_SIZE
Definition: pqcomm.c:135
static int PqRecvPointer
Definition: pqcomm.c:143
#define EINTR
Definition: win32_port.h:332

◆ pq_setkeepalivescount()

int pq_setkeepalivescount ( int  count,
Port port 
)

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

1890 {
1891  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1892  return STATUS_OK;
1893 
1894 #ifdef TCP_KEEPCNT
1895  if (count == port->keepalives_count)
1896  return STATUS_OK;
1897 
1898  if (port->default_keepalives_count <= 0)
1899  {
1900  if (pq_getkeepalivescount(port) < 0)
1901  {
1902  if (count == 0)
1903  return STATUS_OK; /* default is set but unknown */
1904  else
1905  return STATUS_ERROR;
1906  }
1907  }
1908 
1909  if (count == 0)
1910  count = port->default_keepalives_count;
1911 
1912  if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPCNT,
1913  (char *) &count, sizeof(count)) < 0)
1914  {
1915  elog(LOG, "setsockopt(%s) failed: %m", "TCP_KEEPCNT");
1916  return STATUS_ERROR;
1917  }
1918 
1919  port->keepalives_count = count;
1920 #else
1921  if (count != 0)
1922  {
1923  elog(LOG, "setsockopt(%s) not supported", "TCP_KEEPCNT");
1924  return STATUS_ERROR;
1925  }
1926 #endif
1927 
1928  return STATUS_OK;
1929 }
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define STATUS_ERROR
Definition: c.h:1090
#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:1089
SockAddr laddr
Definition: libpq-be.h:125
int pq_getkeepalivescount(Port *port)
Definition: pqcomm.c:1860
#define elog(elevel,...)
Definition: elog.h:226

◆ pq_setkeepalivesidle()

int pq_setkeepalivesidle ( int  idle,
Port port 
)

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

1732 {
1733  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1734  return STATUS_OK;
1735 
1736 /* check SIO_KEEPALIVE_VALS here, not just WIN32, as some toolchains lack it */
1737 #if defined(PG_TCP_KEEPALIVE_IDLE) || defined(SIO_KEEPALIVE_VALS)
1738  if (idle == port->keepalives_idle)
1739  return STATUS_OK;
1740 
1741 #ifndef WIN32
1742  if (port->default_keepalives_idle <= 0)
1743  {
1744  if (pq_getkeepalivesidle(port) < 0)
1745  {
1746  if (idle == 0)
1747  return STATUS_OK; /* default is set but unknown */
1748  else
1749  return STATUS_ERROR;
1750  }
1751  }
1752 
1753  if (idle == 0)
1754  idle = port->default_keepalives_idle;
1755 
1756  if (setsockopt(port->sock, IPPROTO_TCP, PG_TCP_KEEPALIVE_IDLE,
1757  (char *) &idle, sizeof(idle)) < 0)
1758  {
1759  elog(LOG, "setsockopt(%s) failed: %m", PG_TCP_KEEPALIVE_IDLE_STR);
1760  return STATUS_ERROR;
1761  }
1762 
1763  port->keepalives_idle = idle;
1764 #else /* WIN32 */
1765  return pq_setkeepaliveswin32(port, idle, port->keepalives_interval);
1766 #endif
1767 #else
1768  if (idle != 0)
1769  {
1770  elog(LOG, "setting the keepalive idle time is not supported");
1771  return STATUS_ERROR;
1772  }
1773 #endif
1774 
1775  return STATUS_OK;
1776 }
int keepalives_idle
Definition: libpq-be.h:168
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define STATUS_ERROR
Definition: c.h:1090
#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:1089
SockAddr laddr
Definition: libpq-be.h:125
int default_keepalives_idle
Definition: libpq-be.h:164
int pq_getkeepalivesidle(Port *port)
Definition: pqcomm.c:1697
#define elog(elevel,...)
Definition: elog.h:226
int keepalives_interval
Definition: libpq-be.h:169

◆ pq_setkeepalivesinterval()

int pq_setkeepalivesinterval ( int  interval,
Port port 
)

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

1814 {
1815  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1816  return STATUS_OK;
1817 
1818 #if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
1819  if (interval == port->keepalives_interval)
1820  return STATUS_OK;
1821 
1822 #ifndef WIN32
1823  if (port->default_keepalives_interval <= 0)
1824  {
1825  if (pq_getkeepalivesinterval(port) < 0)
1826  {
1827  if (interval == 0)
1828  return STATUS_OK; /* default is set but unknown */
1829  else
1830  return STATUS_ERROR;
1831  }
1832  }
1833 
1834  if (interval == 0)
1836 
1837  if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPINTVL,
1838  (char *) &interval, sizeof(interval)) < 0)
1839  {
1840  elog(LOG, "setsockopt(%s) failed: %m", "TCP_KEEPINTVL");
1841  return STATUS_ERROR;
1842  }
1843 
1844  port->keepalives_interval = interval;
1845 #else /* WIN32 */
1846  return pq_setkeepaliveswin32(port, port->keepalives_idle, interval);
1847 #endif
1848 #else
1849  if (interval != 0)
1850  {
1851  elog(LOG, "setsockopt(%s) not supported", "TCP_KEEPINTVL");
1852  return STATUS_ERROR;
1853  }
1854 #endif
1855 
1856  return STATUS_OK;
1857 }
int keepalives_idle
Definition: libpq-be.h:168
int pq_getkeepalivesinterval(Port *port)
Definition: pqcomm.c:1779
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define STATUS_ERROR
Definition: c.h:1090
#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:1089
SockAddr laddr
Definition: libpq-be.h:125
#define elog(elevel,...)
Definition: elog.h:226
int keepalives_interval
Definition: libpq-be.h:169

◆ pq_settcpusertimeout()

int pq_settcpusertimeout ( int  timeout,
Port port 
)

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

1962 {
1963  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1964  return STATUS_OK;
1965 
1966 #ifdef TCP_USER_TIMEOUT
1967  if (timeout == port->tcp_user_timeout)
1968  return STATUS_OK;
1969 
1970  if (port->default_tcp_user_timeout <= 0)
1971  {
1972  if (pq_gettcpusertimeout(port) < 0)
1973  {
1974  if (timeout == 0)
1975  return STATUS_OK; /* default is set but unknown */
1976  else
1977  return STATUS_ERROR;
1978  }
1979  }
1980 
1981  if (timeout == 0)
1982  timeout = port->default_tcp_user_timeout;
1983 
1984  if (setsockopt(port->sock, IPPROTO_TCP, TCP_USER_TIMEOUT,
1985  (char *) &timeout, sizeof(timeout)) < 0)
1986  {
1987  elog(LOG, "setsockopt(%s) failed: %m", "TCP_USER_TIMEOUT");
1988  return STATUS_ERROR;
1989  }
1990 
1991  port->tcp_user_timeout = timeout;
1992 #else
1993  if (timeout != 0)
1994  {
1995  elog(LOG, "setsockopt(%s) not supported", "TCP_USER_TIMEOUT");
1996  return STATUS_ERROR;
1997  }
1998 #endif
1999 
2000  return STATUS_OK;
2001 }
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define STATUS_ERROR
Definition: c.h:1090
#define LOG
Definition: elog.h:26
pgsocket sock
Definition: libpq-be.h:122
int pq_gettcpusertimeout(Port *port)
Definition: pqcomm.c:1932
#define IS_AF_UNIX(fam)
Definition: ip.h:24
int tcp_user_timeout
Definition: libpq-be.h:171
#define STATUS_OK
Definition: c.h:1089
SockAddr laddr
Definition: libpq-be.h:125
#define elog(elevel,...)
Definition: elog.h:226
int default_tcp_user_timeout
Definition: libpq-be.h:167

◆ pq_startmsgread()

void pq_startmsgread ( void  )

Definition at line 1211 of file pqcomm.c.

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

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

1212 {
1213  /*
1214  * There shouldn't be a read active already, but let's check just to be
1215  * sure.
1216  */
1217  if (PqCommReadingMsg)
1218  ereport(FATAL,
1219  (errcode(ERRCODE_PROTOCOL_VIOLATION),
1220  errmsg("terminating connection because protocol synchronization was lost")));
1221 
1222  PqCommReadingMsg = true;
1223 }
int errcode(int sqlerrcode)
Definition: elog.c:570
#define FATAL
Definition: elog.h:52
#define ereport(elevel, rest)
Definition: elog.h:141
int errmsg(const char *fmt,...)
Definition: elog.c:784
static bool PqCommReadingMsg
Definition: pqcomm.c:150

◆ RemoveSocketFiles()

void RemoveSocketFiles ( void  )

Definition at line 890 of file pqcomm.c.

References lfirst, and NIL.

Referenced by CloseServerPorts().

891 {
892  ListCell *l;
893 
894  /* Loop through all created sockets... */
895  foreach(l, sock_paths)
896  {
897  char *sock_path = (char *) lfirst(l);
898 
899  /* Ignore any error. */
900  (void) unlink(sock_path);
901  }
902  /* Since we're about to exit, no need to reclaim storage */
903  sock_paths = NIL;
904 }
#define NIL
Definition: pg_list.h:65
static List * sock_paths
Definition: pqcomm.c:125
#define lfirst(lc)
Definition: pg_list.h:190

◆ socket_close()

static void socket_close ( int  code,
Datum  arg 
)
static

Definition at line 256 of file pqcomm.c.

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

Referenced by pq_init().

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

◆ socket_comm_reset()

static void socket_comm_reset ( void  )
static

Definition at line 239 of file pqcomm.c.

References pq_endcopyout, and PqCommBusy.

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

◆ socket_endcopyout()

static void socket_endcopyout ( bool  errorAbort)
static

Definition at line 1637 of file pqcomm.c.

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

1638 {
1639  if (!DoingCopyOut)
1640  return;
1641  if (errorAbort)
1642  pq_putbytes("\n\n\\.\n", 5);
1643  /* in non-error case, copy.c will have emitted the terminator line */
1644  DoingCopyOut = false;
1645 }
static bool DoingCopyOut
Definition: pqcomm.c:151
int pq_putbytes(const char *s, size_t len)
Definition: pqcomm.c:1354

◆ socket_flush()

static int socket_flush ( void  )
static

Definition at line 1401 of file pqcomm.c.

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

1402 {
1403  int res;
1404 
1405  /* No-op if reentrant call */
1406  if (PqCommBusy)
1407  return 0;
1408  PqCommBusy = true;
1409  socket_set_nonblocking(false);
1410  res = internal_flush();
1411  PqCommBusy = false;
1412  return res;
1413 }
static int internal_flush(void)
Definition: pqcomm.c:1423
static void socket_set_nonblocking(bool nonblocking)
Definition: pqcomm.c:923
static bool PqCommBusy
Definition: pqcomm.c:149

◆ socket_flush_if_writable()

static int socket_flush_if_writable ( void  )
static

Definition at line 1496 of file pqcomm.c.

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

1497 {
1498  int res;
1499 
1500  /* Quick exit if nothing to do */
1501  if (PqSendPointer == PqSendStart)
1502  return 0;
1503 
1504  /* No-op if reentrant call */
1505  if (PqCommBusy)
1506  return 0;
1507 
1508  /* Temporarily put the socket into non-blocking mode */
1509  socket_set_nonblocking(true);
1510 
1511  PqCommBusy = true;
1512  res = internal_flush();
1513  PqCommBusy = false;
1514  return res;
1515 }
static int PqSendStart
Definition: pqcomm.c:140
static int internal_flush(void)
Definition: pqcomm.c:1423
static void socket_set_nonblocking(bool nonblocking)
Definition: pqcomm.c:923
static int PqSendPointer
Definition: pqcomm.c:139
static bool PqCommBusy
Definition: pqcomm.c:149

◆ socket_is_send_pending()

static bool socket_is_send_pending ( void  )
static

Definition at line 1522 of file pqcomm.c.

References PqSendPointer, and PqSendStart.

1523 {
1524  return (PqSendStart < PqSendPointer);
1525 }
static int PqSendStart
Definition: pqcomm.c:140
static int PqSendPointer
Definition: pqcomm.c:139

◆ socket_putmessage()

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

Definition at line 1561 of file pqcomm.c.

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

1562 {
1563  if (DoingCopyOut || PqCommBusy)
1564  return 0;
1565  PqCommBusy = true;
1566  if (msgtype)
1567  if (internal_putbytes(&msgtype, 1))
1568  goto fail;
1570  {
1571  uint32 n32;
1572 
1573  n32 = pg_hton32((uint32) (len + 4));
1574  if (internal_putbytes((char *) &n32, 4))
1575  goto fail;
1576  }
1577  if (internal_putbytes(s, len))
1578  goto fail;
1579  PqCommBusy = false;
1580  return 0;
1581 
1582 fail:
1583  PqCommBusy = false;
1584  return EOF;
1585 }
static bool DoingCopyOut
Definition: pqcomm.c:151
#define PG_PROTOCOL_MAJOR(v)
Definition: pqcomm.h:104
#define pg_hton32(x)
Definition: pg_bswap.h:121
unsigned int uint32
Definition: c.h:358
static int internal_putbytes(const char *s, size_t len)
Definition: pqcomm.c:1370
static bool PqCommBusy
Definition: pqcomm.c:149
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 1594 of file pqcomm.c.

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

1595 {
1596  int res PG_USED_FOR_ASSERTS_ONLY;
1597  int required;
1598 
1599  /*
1600  * Ensure we have enough space in the output buffer for the message header
1601  * as well as the message itself.
1602  */
1603  required = PqSendPointer + 1 + 4 + len;
1604  if (required > PqSendBufferSize)
1605  {
1606  PqSendBuffer = repalloc(PqSendBuffer, required);
1608  }
1609  res = pq_putmessage(msgtype, s, len);
1610  Assert(res == 0); /* should not fail when the message fits in
1611  * buffer */
1612 }
static char * PqSendBuffer
Definition: pqcomm.c:137
static int PqSendPointer
Definition: pqcomm.c:139
static int PqSendBufferSize
Definition: pqcomm.c:138
#define Assert(condition)
Definition: c.h:732
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1044
#define pq_putmessage(msgtype, s, len)
Definition: libpq.h:42
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:123

◆ socket_set_nonblocking()

static void socket_set_nonblocking ( bool  nonblocking)
static

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

924 {
925  if (MyProcPort == NULL)
926  ereport(ERROR,
927  (errcode(ERRCODE_CONNECTION_DOES_NOT_EXIST),
928  errmsg("there is no client connection")));
929 
930  MyProcPort->noblock = nonblocking;
931 }
struct Port * MyProcPort
Definition: globals.c:43
int errcode(int sqlerrcode)
Definition: elog.c:570
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
bool noblock
Definition: libpq-be.h:123
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ socket_startcopyout()

static void socket_startcopyout ( void  )
static

Definition at line 1621 of file pqcomm.c.

References DoingCopyOut.

1622 {
1623  DoingCopyOut = true;
1624 }
static bool DoingCopyOut
Definition: pqcomm.c:151

◆ StreamClose()

void StreamClose ( pgsocket  sock)

Definition at line 845 of file pqcomm.c.

References closesocket.

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

846 {
847  closesocket(sock);
848 }
#define closesocket
Definition: port.h:312

◆ StreamConnection()

int StreamConnection ( pgsocket  server_fd,
Port port 
)

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

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  elog(LOG, "getsockname() failed: %m");
746  return STATUS_ERROR;
747  }
748 
749  /* select NODELAY and KEEPALIVE options if it's a TCP connection */
750  if (!IS_AF_UNIX(port->laddr.addr.ss_family))
751  {
752  int on;
753 #ifdef WIN32
754  int oldopt;
755  int optlen;
756  int newopt;
757 #endif
758 
759 #ifdef TCP_NODELAY
760  on = 1;
761  if (setsockopt(port->sock, IPPROTO_TCP, TCP_NODELAY,
762  (char *) &on, sizeof(on)) < 0)
763  {
764  elog(LOG, "setsockopt(%s) failed: %m", "TCP_NODELAY");
765  return STATUS_ERROR;
766  }
767 #endif
768  on = 1;
769  if (setsockopt(port->sock, SOL_SOCKET, SO_KEEPALIVE,
770  (char *) &on, sizeof(on)) < 0)
771  {
772  elog(LOG, "setsockopt(%s) failed: %m", "SO_KEEPALIVE");
773  return STATUS_ERROR;
774  }
775 
776 #ifdef WIN32
777 
778  /*
779  * This is a Win32 socket optimization. The OS send buffer should be
780  * large enough to send the whole Postgres send buffer in one go, or
781  * performance suffers. The Postgres send buffer can be enlarged if a
782  * very large message needs to be sent, but we won't attempt to
783  * enlarge the OS buffer if that happens, so somewhat arbitrarily
784  * ensure that the OS buffer is at least PQ_SEND_BUFFER_SIZE * 4.
785  * (That's 32kB with the current default).
786  *
787  * The default OS buffer size used to be 8kB in earlier Windows
788  * versions, but was raised to 64kB in Windows 2012. So it shouldn't
789  * be necessary to change it in later versions anymore. Changing it
790  * unnecessarily can even reduce performance, because setting
791  * SO_SNDBUF in the application disables the "dynamic send buffering"
792  * feature that was introduced in Windows 7. So before fiddling with
793  * SO_SNDBUF, check if the current buffer size is already large enough
794  * and only increase it if necessary.
795  *
796  * See https://support.microsoft.com/kb/823764/EN-US/ and
797  * https://msdn.microsoft.com/en-us/library/bb736549%28v=vs.85%29.aspx
798  */
799  optlen = sizeof(oldopt);
800  if (getsockopt(port->sock, SOL_SOCKET, SO_SNDBUF, (char *) &oldopt,
801  &optlen) < 0)
802  {
803  elog(LOG, "getsockopt(%s) failed: %m", "SO_SNDBUF");
804  return STATUS_ERROR;
805  }
806  newopt = PQ_SEND_BUFFER_SIZE * 4;
807  if (oldopt < newopt)
808  {
809  if (setsockopt(port->sock, SOL_SOCKET, SO_SNDBUF, (char *) &newopt,
810  sizeof(newopt)) < 0)
811  {
812  elog(LOG, "setsockopt(%s) failed: %m", "SO_SNDBUF");
813  return STATUS_ERROR;
814  }
815  }
816 #endif
817 
818  /*
819  * Also apply the current keepalive parameters. If we fail to set a
820  * parameter, don't error out, because these aren't universally
821  * supported. (Note: you might think we need to reset the GUC
822  * variables to 0 in such a case, but it's not necessary because the
823  * show hooks for these variables report the truth anyway.)
824  */
829  }
830 
831  return STATUS_OK;
832 }
#define accept(s, addr, addrlen)
Definition: win32_port.h:443
int pq_setkeepalivesinterval(int interval, Port *port)
Definition: pqcomm.c:1813
struct sockaddr_storage addr
Definition: pqcomm.h:64
int tcp_user_timeout
Definition: guc.c:534
#define STATUS_ERROR
Definition: c.h:1090
#define LOG
Definition: elog.h:26
pgsocket sock
Definition: libpq-be.h:122
int tcp_keepalives_idle
Definition: guc.c:531
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:1731
#define IS_AF_UNIX(fam)
Definition: ip.h:24
ACCEPT_TYPE_ARG3 salen
Definition: pqcomm.h:65
#define ereport(elevel, rest)
Definition: elog.h:141
#define STATUS_OK
Definition: c.h:1089
int tcp_keepalives_interval
Definition: guc.c:532
int errcode_for_socket_access(void)
Definition: elog.c:664
SockAddr laddr
Definition: libpq-be.h:125
#define PGINVALID_SOCKET
Definition: port.h:33
int pq_setkeepalivescount(int count, Port *port)
Definition: pqcomm.c:1889
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define elog(elevel,...)
Definition: elog.h:226
int pq_settcpusertimeout(int timeout, Port *port)
Definition: pqcomm.c:1961
int tcp_keepalives_count
Definition: guc.c:533
#define PQ_SEND_BUFFER_SIZE
Definition: pqcomm.c:134

◆ StreamServerPort()

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

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

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

◆ TouchSocketFiles()

void TouchSocketFiles ( void  )

Definition at line 860 of file pqcomm.c.

References lfirst.

Referenced by ServerLoop().

861 {
862  ListCell *l;
863 
864  /* Loop through all created sockets... */
865  foreach(l, sock_paths)
866  {
867  char *sock_path = (char *) lfirst(l);
868 
869  /*
870  * utime() is POSIX standard, utimes() is a common alternative. If we
871  * have neither, there's no way to affect the mod or access time of
872  * the socket :-(
873  *
874  * In either path, we ignore errors; there's no point in complaining.
875  */
876 #ifdef HAVE_UTIME
877  utime(sock_path, NULL);
878 #else /* !HAVE_UTIME */
879 #ifdef HAVE_UTIMES
880  utimes(sock_path, NULL);
881 #endif /* HAVE_UTIMES */
882 #endif /* HAVE_UTIME */
883  }
884 }
static List * sock_paths
Definition: pqcomm.c:125
#define lfirst(lc)
Definition: pg_list.h:190

Variable Documentation

◆ DoingCopyOut

bool DoingCopyOut
static

◆ FeBeWaitSet

WaitEventSet* FeBeWaitSet

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

Referenced by pq_redirect_to_shm_mq(), and pq_set_parallel_master().

◆ PqCommReadingMsg

◆ PqCommSocketMethods

const PQcommMethods PqCommSocketMethods
static
Initial value:
= {
}
static int socket_flush_if_writable(void)
Definition: pqcomm.c:1496
static bool socket_is_send_pending(void)
Definition: pqcomm.c:1522
static void socket_comm_reset(void)
Definition: pqcomm.c:239
static void socket_startcopyout(void)
Definition: pqcomm.c:1621
static void socket_endcopyout(bool errorAbort)
Definition: pqcomm.c:1637
static int socket_putmessage(char msgtype, const char *s, size_t len)
Definition: pqcomm.c:1561
static int socket_flush(void)
Definition: pqcomm.c:1401
static void socket_putmessage_noblock(char msgtype, const char *s, size_t len)
Definition: pqcomm.c:1594

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

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

◆ PqSendBufferSize

int PqSendBufferSize
static

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

◆ Unix_socket_group

char* Unix_socket_group

Definition at line 122 of file pqcomm.c.

Referenced by StreamServerPort().

◆ Unix_socket_permissions

int Unix_socket_permissions

Definition at line 121 of file pqcomm.c.

Referenced by show_unix_socket_permissions(), and StreamServerPort().