PostgreSQL Source Code  git master
pqcomm.c File Reference
#include "postgres.h"
#include <poll.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 <netinet/tcp.h>
#include <utime.h>
#include "common/ip.h"
#include "libpq/libpq.h"
#include "miscadmin.h"
#include "port/pg_bswap.h"
#include "postmaster/postmaster.h"
#include "storage/ipc.h"
#include "utils/guc_hooks.h"
#include "utils/memutils.h"
Include dependency graph for pqcomm.c:

Go to the source code of this file.

Macros

#define PQ_SEND_BUFFER_SIZE   8192
 
#define PQ_RECV_BUFFER_SIZE   8192
 

Functions

static void socket_comm_reset (void)
 
static void socket_close (int code, Datum arg)
 
static void socket_set_nonblocking (bool nonblocking)
 
static int socket_flush (void)
 
static int socket_flush_if_writable (void)
 
static bool socket_is_send_pending (void)
 
static int socket_putmessage (char msgtype, const char *s, size_t len)
 
static void socket_putmessage_noblock (char msgtype, const char *s, size_t len)
 
static int internal_putbytes (const char *s, size_t len)
 
static int internal_flush (void)
 
static pg_noinline int internal_flush_buffer (const char *buf, size_t *start, size_t *end)
 
static int Lock_AF_UNIX (const char *unixSocketDir, const char *unixSocketPath)
 
static int Setup_AF_UNIX (const char *sock_path)
 
Portpq_init (ClientSocket *client_sock)
 
int ListenServerPort (int family, const char *hostName, unsigned short portNumber, const char *unixSocketDir, pgsocket ListenSockets[], int *NumListenSockets, int MaxListen)
 
int AcceptConnection (pgsocket server_fd, ClientSocket *client_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)
 
ssize_t pq_buffer_remaining_data (void)
 
void pq_startmsgread (void)
 
void pq_endmsgread (void)
 
bool pq_is_reading_msg (void)
 
int pq_getmessage (StringInfo s, int maxlen)
 
int pq_putmessage_v2 (char msgtype, const char *s, size_t len)
 
int pq_getkeepalivesidle (Port *port)
 
int pq_setkeepalivesidle (int idle, Port *port)
 
int pq_getkeepalivesinterval (Port *port)
 
int pq_setkeepalivesinterval (int interval, Port *port)
 
int pq_getkeepalivescount (Port *port)
 
int pq_setkeepalivescount (int count, Port *port)
 
int pq_gettcpusertimeout (Port *port)
 
int pq_settcpusertimeout (int timeout, Port *port)
 
void assign_tcp_keepalives_idle (int newval, void *extra)
 
const char * show_tcp_keepalives_idle (void)
 
void assign_tcp_keepalives_interval (int newval, void *extra)
 
const char * show_tcp_keepalives_interval (void)
 
void assign_tcp_keepalives_count (int newval, void *extra)
 
const char * show_tcp_keepalives_count (void)
 
void assign_tcp_user_timeout (int newval, void *extra)
 
const char * show_tcp_user_timeout (void)
 
bool pq_check_connection (void)
 

Variables

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

Macro Definition Documentation

◆ PQ_RECV_BUFFER_SIZE

#define PQ_RECV_BUFFER_SIZE   8192

Definition at line 120 of file pqcomm.c.

◆ PQ_SEND_BUFFER_SIZE

#define PQ_SEND_BUFFER_SIZE   8192

Definition at line 119 of file pqcomm.c.

Function Documentation

◆ AcceptConnection()

int AcceptConnection ( pgsocket  server_fd,
ClientSocket client_sock 
)

Definition at line 794 of file pqcomm.c.

795 {
796  /* accept connection and fill in the client (remote) address */
797  client_sock->raddr.salen = sizeof(client_sock->raddr.addr);
798  if ((client_sock->sock = accept(server_fd,
799  (struct sockaddr *) &client_sock->raddr.addr,
800  &client_sock->raddr.salen)) == PGINVALID_SOCKET)
801  {
802  ereport(LOG,
804  errmsg("could not accept new connection: %m")));
805 
806  /*
807  * If accept() fails then postmaster.c will still see the server
808  * socket as read-ready, and will immediately try again. To avoid
809  * uselessly sucking lots of CPU, delay a bit before trying again.
810  * (The most likely reason for failure is being out of kernel file
811  * table slots; we can do little except hope some will get freed up.)
812  */
813  pg_usleep(100000L); /* wait 0.1 sec */
814  return STATUS_ERROR;
815  }
816 
817  return STATUS_OK;
818 }
#define STATUS_OK
Definition: c.h:1123
#define STATUS_ERROR
Definition: c.h:1124
int errcode_for_socket_access(void)
Definition: elog.c:953
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define LOG
Definition: elog.h:31
#define ereport(elevel,...)
Definition: elog.h:149
#define PGINVALID_SOCKET
Definition: port.h:31
void pg_usleep(long microsec)
Definition: signal.c:53
SockAddr raddr
Definition: libpq-be.h:245
pgsocket sock
Definition: libpq-be.h:244
struct sockaddr_storage addr
Definition: pqcomm.h:32
socklen_t salen
Definition: pqcomm.h:33
#define accept(s, addr, addrlen)
Definition: win32_port.h:511

References accept, SockAddr::addr, ereport, errcode_for_socket_access(), errmsg(), LOG, pg_usleep(), PGINVALID_SOCKET, ClientSocket::raddr, SockAddr::salen, ClientSocket::sock, STATUS_ERROR, and STATUS_OK.

Referenced by ServerLoop().

◆ assign_tcp_keepalives_count()

void assign_tcp_keepalives_count ( int  newval,
void *  extra 
)

Definition at line 2008 of file pqcomm.c.

2009 {
2010  /* See comments in assign_tcp_keepalives_idle */
2012 }
struct Port * MyProcPort
Definition: globals.c:50
#define newval
int pq_setkeepalivescount(int count, Port *port)
Definition: pqcomm.c:1829

References MyProcPort, newval, and pq_setkeepalivescount().

◆ assign_tcp_keepalives_idle()

void assign_tcp_keepalives_idle ( int  newval,
void *  extra 
)

Definition at line 1952 of file pqcomm.c.

1953 {
1954  /*
1955  * The kernel API provides no way to test a value without setting it; and
1956  * once we set it we might fail to unset it. So there seems little point
1957  * in fully implementing the check-then-assign GUC API for these
1958  * variables. Instead we just do the assignment on demand.
1959  * pq_setkeepalivesidle reports any problems via ereport(LOG).
1960  *
1961  * This approach means that the GUC value might have little to do with the
1962  * actual kernel value, so we use a show_hook that retrieves the kernel
1963  * value rather than trusting GUC's copy.
1964  */
1966 }
int pq_setkeepalivesidle(int idle, Port *port)
Definition: pqcomm.c:1665

References MyProcPort, newval, and pq_setkeepalivesidle().

◆ assign_tcp_keepalives_interval()

void assign_tcp_keepalives_interval ( int  newval,
void *  extra 
)

Definition at line 1985 of file pqcomm.c.

1986 {
1987  /* See comments in assign_tcp_keepalives_idle */
1989 }
int pq_setkeepalivesinterval(int interval, Port *port)
Definition: pqcomm.c:1750

References MyProcPort, newval, and pq_setkeepalivesinterval().

◆ assign_tcp_user_timeout()

void assign_tcp_user_timeout ( int  newval,
void *  extra 
)

Definition at line 2031 of file pqcomm.c.

2032 {
2033  /* See comments in assign_tcp_keepalives_idle */
2035 }
int pq_settcpusertimeout(int timeout, Port *port)
Definition: pqcomm.c:1904

References MyProcPort, newval, and pq_settcpusertimeout().

◆ internal_flush()

static int internal_flush ( void  )
inlinestatic

Definition at line 1347 of file pqcomm.c.

1348 {
1350 }
static size_t PqSendPointer
Definition: pqcomm.c:124
static char * PqSendBuffer
Definition: pqcomm.c:122
static pg_noinline int internal_flush_buffer(const char *buf, size_t *start, size_t *end)
Definition: pqcomm.c:1360
static size_t PqSendStart
Definition: pqcomm.c:125

References internal_flush_buffer(), PqSendBuffer, PqSendPointer, and PqSendStart.

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

◆ internal_flush_buffer()

static pg_noinline int internal_flush_buffer ( const char *  buf,
size_t *  start,
size_t *  end 
)
static

Definition at line 1360 of file pqcomm.c.

1361 {
1362  static int last_reported_send_errno = 0;
1363 
1364  const char *bufptr = buf + *start;
1365  const char *bufend = buf + *end;
1366 
1367  while (bufptr < bufend)
1368  {
1369  int r;
1370 
1371  r = secure_write(MyProcPort, (char *) bufptr, bufend - bufptr);
1372 
1373  if (r <= 0)
1374  {
1375  if (errno == EINTR)
1376  continue; /* Ok if we were interrupted */
1377 
1378  /*
1379  * Ok if no data writable without blocking, and the socket is in
1380  * non-blocking mode.
1381  */
1382  if (errno == EAGAIN ||
1383  errno == EWOULDBLOCK)
1384  {
1385  return 0;
1386  }
1387 
1388  /*
1389  * Careful: an ereport() that tries to write to the client would
1390  * cause recursion to here, leading to stack overflow and core
1391  * dump! This message must go *only* to the postmaster log.
1392  *
1393  * If a client disconnects while we're in the midst of output, we
1394  * might write quite a bit of data before we get to a safe query
1395  * abort point. So, suppress duplicate log messages.
1396  */
1397  if (errno != last_reported_send_errno)
1398  {
1399  last_reported_send_errno = errno;
1402  errmsg("could not send data to client: %m")));
1403  }
1404 
1405  /*
1406  * We drop the buffered data anyway so that processing can
1407  * continue, even though we'll probably quit soon. We also set a
1408  * flag that'll cause the next CHECK_FOR_INTERRUPTS to terminate
1409  * the connection.
1410  */
1411  *start = *end = 0;
1413  InterruptPending = 1;
1414  return EOF;
1415  }
1416 
1417  last_reported_send_errno = 0; /* reset after any successful send */
1418  bufptr += r;
1419  *start += r;
1420  }
1421 
1422  *start = *end = 0;
1423  return 0;
1424 }
ssize_t secure_write(Port *port, void *ptr, size_t len)
Definition: be-secure.c:305
#define COMMERROR
Definition: elog.h:33
volatile sig_atomic_t InterruptPending
Definition: globals.c:31
volatile sig_atomic_t ClientConnectionLost
Definition: globals.c:35
return str start
static char * buf
Definition: pg_test_fsync.c:72
#define EINTR
Definition: win32_port.h:374
#define EWOULDBLOCK
Definition: win32_port.h:380
#define EAGAIN
Definition: win32_port.h:372

References buf, ClientConnectionLost, COMMERROR, EAGAIN, EINTR, ereport, errcode_for_socket_access(), errmsg(), EWOULDBLOCK, InterruptPending, MyProcPort, secure_write(), and start.

Referenced by internal_flush(), and internal_putbytes().

◆ internal_putbytes()

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

Definition at line 1277 of file pqcomm.c.

1278 {
1279  while (len > 0)
1280  {
1281  /* If buffer is full, then flush it out */
1283  {
1284  socket_set_nonblocking(false);
1285  if (internal_flush())
1286  return EOF;
1287  }
1288 
1289  /*
1290  * If the buffer is empty and data length is larger than the buffer
1291  * size, send it without buffering. Otherwise, copy as much data as
1292  * possible into the buffer.
1293  */
1295  {
1296  size_t start = 0;
1297 
1298  socket_set_nonblocking(false);
1299  if (internal_flush_buffer(s, &start, &len))
1300  return EOF;
1301  }
1302  else
1303  {
1304  size_t amount = PqSendBufferSize - PqSendPointer;
1305 
1306  if (amount > len)
1307  amount = len;
1308  memcpy(PqSendBuffer + PqSendPointer, s, amount);
1309  PqSendPointer += amount;
1310  s += amount;
1311  len -= amount;
1312  }
1313  }
1314 
1315  return 0;
1316 }
const void size_t len
static int internal_flush(void)
Definition: pqcomm.c:1347
static void socket_set_nonblocking(bool nonblocking)
Definition: pqcomm.c:881
static int PqSendBufferSize
Definition: pqcomm.c:123

References internal_flush(), internal_flush_buffer(), len, PqSendBuffer, PqSendBufferSize, PqSendPointer, PqSendStart, socket_set_nonblocking(), and start.

Referenced by pq_putmessage_v2(), and socket_putmessage().

◆ ListenServerPort()

int ListenServerPort ( int  family,
const char *  hostName,
unsigned short  portNumber,
const char *  unixSocketDir,
pgsocket  ListenSockets[],
int *  NumListenSockets,
int  MaxListen 
)

Definition at line 418 of file pqcomm.c.

421 {
422  pgsocket fd;
423  int err;
424  int maxconn;
425  int ret;
426  char portNumberStr[32];
427  const char *familyDesc;
428  char familyDescBuf[64];
429  const char *addrDesc;
430  char addrBuf[NI_MAXHOST];
431  char *service;
432  struct addrinfo *addrs = NULL,
433  *addr;
434  struct addrinfo hint;
435  int added = 0;
436  char unixSocketPath[MAXPGPATH];
437 #if !defined(WIN32) || defined(IPV6_V6ONLY)
438  int one = 1;
439 #endif
440 
441  /* Initialize hint structure */
442  MemSet(&hint, 0, sizeof(hint));
443  hint.ai_family = family;
444  hint.ai_flags = AI_PASSIVE;
445  hint.ai_socktype = SOCK_STREAM;
446 
447  if (family == AF_UNIX)
448  {
449  /*
450  * Create unixSocketPath from portNumber and unixSocketDir and lock
451  * that file path
452  */
453  UNIXSOCK_PATH(unixSocketPath, portNumber, unixSocketDir);
454  if (strlen(unixSocketPath) >= UNIXSOCK_PATH_BUFLEN)
455  {
456  ereport(LOG,
457  (errmsg("Unix-domain socket path \"%s\" is too long (maximum %d bytes)",
458  unixSocketPath,
459  (int) (UNIXSOCK_PATH_BUFLEN - 1))));
460  return STATUS_ERROR;
461  }
462  if (Lock_AF_UNIX(unixSocketDir, unixSocketPath) != STATUS_OK)
463  return STATUS_ERROR;
464  service = unixSocketPath;
465  }
466  else
467  {
468  snprintf(portNumberStr, sizeof(portNumberStr), "%d", portNumber);
469  service = portNumberStr;
470  }
471 
472  ret = pg_getaddrinfo_all(hostName, service, &hint, &addrs);
473  if (ret || !addrs)
474  {
475  if (hostName)
476  ereport(LOG,
477  (errmsg("could not translate host name \"%s\", service \"%s\" to address: %s",
478  hostName, service, gai_strerror(ret))));
479  else
480  ereport(LOG,
481  (errmsg("could not translate service \"%s\" to address: %s",
482  service, gai_strerror(ret))));
483  if (addrs)
484  pg_freeaddrinfo_all(hint.ai_family, addrs);
485  return STATUS_ERROR;
486  }
487 
488  for (addr = addrs; addr; addr = addr->ai_next)
489  {
490  if (family != AF_UNIX && addr->ai_family == AF_UNIX)
491  {
492  /*
493  * Only set up a unix domain socket when they really asked for it.
494  * The service/port is different in that case.
495  */
496  continue;
497  }
498 
499  /* See if there is still room to add 1 more socket. */
500  if (*NumListenSockets == MaxListen)
501  {
502  ereport(LOG,
503  (errmsg("could not bind to all requested addresses: MAXLISTEN (%d) exceeded",
504  MaxListen)));
505  break;
506  }
507 
508  /* set up address family name for log messages */
509  switch (addr->ai_family)
510  {
511  case AF_INET:
512  familyDesc = _("IPv4");
513  break;
514  case AF_INET6:
515  familyDesc = _("IPv6");
516  break;
517  case AF_UNIX:
518  familyDesc = _("Unix");
519  break;
520  default:
521  snprintf(familyDescBuf, sizeof(familyDescBuf),
522  _("unrecognized address family %d"),
523  addr->ai_family);
524  familyDesc = familyDescBuf;
525  break;
526  }
527 
528  /* set up text form of address for log messages */
529  if (addr->ai_family == AF_UNIX)
530  addrDesc = unixSocketPath;
531  else
532  {
533  pg_getnameinfo_all((const struct sockaddr_storage *) addr->ai_addr,
534  addr->ai_addrlen,
535  addrBuf, sizeof(addrBuf),
536  NULL, 0,
537  NI_NUMERICHOST);
538  addrDesc = addrBuf;
539  }
540 
541  if ((fd = socket(addr->ai_family, SOCK_STREAM, 0)) == PGINVALID_SOCKET)
542  {
543  ereport(LOG,
545  /* translator: first %s is IPv4, IPv6, or Unix */
546  errmsg("could not create %s socket for address \"%s\": %m",
547  familyDesc, addrDesc)));
548  continue;
549  }
550 
551 #ifndef WIN32
552  /* Don't give the listen socket to any subprograms we execute. */
553  if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0)
554  elog(FATAL, "fcntl(F_SETFD) failed on socket: %m");
555 
556  /*
557  * Without the SO_REUSEADDR flag, a new postmaster can't be started
558  * right away after a stop or crash, giving "address already in use"
559  * error on TCP ports.
560  *
561  * On win32, however, this behavior only happens if the
562  * SO_EXCLUSIVEADDRUSE is set. With SO_REUSEADDR, win32 allows
563  * multiple servers to listen on the same address, resulting in
564  * unpredictable behavior. With no flags at all, win32 behaves as Unix
565  * with SO_REUSEADDR.
566  */
567  if (addr->ai_family != AF_UNIX)
568  {
569  if ((setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
570  (char *) &one, sizeof(one))) == -1)
571  {
572  ereport(LOG,
574  /* translator: third %s is IPv4, IPv6, or Unix */
575  errmsg("%s(%s) failed for %s address \"%s\": %m",
576  "setsockopt", "SO_REUSEADDR",
577  familyDesc, addrDesc)));
578  closesocket(fd);
579  continue;
580  }
581  }
582 #endif
583 
584 #ifdef IPV6_V6ONLY
585  if (addr->ai_family == AF_INET6)
586  {
587  if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY,
588  (char *) &one, sizeof(one)) == -1)
589  {
590  ereport(LOG,
592  /* translator: third %s is IPv4, IPv6, or Unix */
593  errmsg("%s(%s) failed for %s address \"%s\": %m",
594  "setsockopt", "IPV6_V6ONLY",
595  familyDesc, addrDesc)));
596  closesocket(fd);
597  continue;
598  }
599  }
600 #endif
601 
602  /*
603  * Note: This might fail on some OS's, like Linux older than
604  * 2.4.21-pre3, that don't have the IPV6_V6ONLY socket option, and map
605  * ipv4 addresses to ipv6. It will show ::ffff:ipv4 for all ipv4
606  * connections.
607  */
608  err = bind(fd, addr->ai_addr, addr->ai_addrlen);
609  if (err < 0)
610  {
611  int saved_errno = errno;
612 
613  ereport(LOG,
615  /* translator: first %s is IPv4, IPv6, or Unix */
616  errmsg("could not bind %s address \"%s\": %m",
617  familyDesc, addrDesc),
618  saved_errno == EADDRINUSE ?
619  (addr->ai_family == AF_UNIX ?
620  errhint("Is another postmaster already running on port %d?",
621  (int) portNumber) :
622  errhint("Is another postmaster already running on port %d?"
623  " If not, wait a few seconds and retry.",
624  (int) portNumber)) : 0));
625  closesocket(fd);
626  continue;
627  }
628 
629  if (addr->ai_family == AF_UNIX)
630  {
631  if (Setup_AF_UNIX(service) != STATUS_OK)
632  {
633  closesocket(fd);
634  break;
635  }
636  }
637 
638  /*
639  * Select appropriate accept-queue length limit. It seems reasonable
640  * to use a value similar to the maximum number of child processes
641  * that the postmaster will permit.
642  */
643  maxconn = MaxConnections * 2;
644 
645  err = listen(fd, maxconn);
646  if (err < 0)
647  {
648  ereport(LOG,
650  /* translator: first %s is IPv4, IPv6, or Unix */
651  errmsg("could not listen on %s address \"%s\": %m",
652  familyDesc, addrDesc)));
653  closesocket(fd);
654  continue;
655  }
656 
657  if (addr->ai_family == AF_UNIX)
658  ereport(LOG,
659  (errmsg("listening on Unix socket \"%s\"",
660  addrDesc)));
661  else
662  ereport(LOG,
663  /* translator: first %s is IPv4 or IPv6 */
664  (errmsg("listening on %s address \"%s\", port %d",
665  familyDesc, addrDesc, (int) portNumber)));
666 
668  (*NumListenSockets)++;
669  added++;
670  }
671 
672  pg_freeaddrinfo_all(hint.ai_family, addrs);
673 
674  if (!added)
675  return STATUS_ERROR;
676 
677  return STATUS_OK;
678 }
#define MemSet(start, val, len)
Definition: c.h:974
int errhint(const char *fmt,...)
Definition: elog.c:1317
#define _(x)
Definition: elog.c:90
#define FATAL
Definition: elog.h:41
#define elog(elevel,...)
Definition: elog.h:225
void err(int eval, const char *fmt,...)
Definition: err.c:43
int MaxConnections
Definition: globals.c:142
void pg_freeaddrinfo_all(int hint_ai_family, struct addrinfo *ai)
Definition: ip.c:82
int pg_getnameinfo_all(const struct sockaddr_storage *addr, int salen, char *node, int nodelen, char *service, int servicelen, int flags)
Definition: ip.c:114
int pg_getaddrinfo_all(const char *hostname, const char *servname, const struct addrinfo *hintp, struct addrinfo **result)
Definition: ip.c:53
#define MAXPGPATH
int pgsocket
Definition: port.h:29
#define snprintf
Definition: port.h:238
#define closesocket
Definition: port.h:349
static pgsocket * ListenSockets
Definition: postmaster.c:227
static int NumListenSockets
Definition: postmaster.c:226
static int Lock_AF_UNIX(const char *unixSocketDir, const char *unixSocketPath)
Definition: pqcomm.c:685
static int Setup_AF_UNIX(const char *sock_path)
Definition: pqcomm.c:720
#define UNIXSOCK_PATH(path, port, sockdir)
Definition: pqcomm.h:44
#define UNIXSOCK_PATH_BUFLEN
Definition: pqcomm.h:60
static int fd(const char *x, int i)
Definition: preproc-init.c:105
const char * gai_strerror(int ecode)
#define bind(s, addr, addrlen)
Definition: win32_port.h:509
#define EADDRINUSE
Definition: win32_port.h:400
#define socket(af, type, protocol)
Definition: win32_port.h:508
#define listen(s, backlog)
Definition: win32_port.h:510

References _, bind, closesocket, EADDRINUSE, elog, ereport, err(), errcode_for_socket_access(), errhint(), errmsg(), FATAL, fd(), gai_strerror(), listen, ListenSockets, Lock_AF_UNIX(), LOG, MaxConnections, MAXPGPATH, MemSet, NumListenSockets, pg_freeaddrinfo_all(), pg_getaddrinfo_all(), pg_getnameinfo_all(), PGINVALID_SOCKET, Setup_AF_UNIX(), snprintf, socket, STATUS_ERROR, STATUS_OK, UNIXSOCK_PATH, and UNIXSOCK_PATH_BUFLEN.

Referenced by PostmasterMain().

◆ Lock_AF_UNIX()

static int Lock_AF_UNIX ( const char *  unixSocketDir,
const char *  unixSocketPath 
)
static

Definition at line 685 of file pqcomm.c.

686 {
687  /* no lock file for abstract sockets */
688  if (unixSocketPath[0] == '@')
689  return STATUS_OK;
690 
691  /*
692  * Grab an interlock file associated with the socket file.
693  *
694  * Note: there are two reasons for using a socket lock file, rather than
695  * trying to interlock directly on the socket itself. First, it's a lot
696  * more portable, and second, it lets us remove any pre-existing socket
697  * file without race conditions.
698  */
699  CreateSocketLockFile(unixSocketPath, true, unixSocketDir);
700 
701  /*
702  * Once we have the interlock, we can safely delete any pre-existing
703  * socket file to avoid failure at bind() time.
704  */
705  (void) unlink(unixSocketPath);
706 
707  /*
708  * Remember socket file pathnames for later maintenance.
709  */
710  sock_paths = lappend(sock_paths, pstrdup(unixSocketPath));
711 
712  return STATUS_OK;
713 }
List * lappend(List *list, void *datum)
Definition: list.c:339
char * pstrdup(const char *in)
Definition: mcxt.c:1696
void CreateSocketLockFile(const char *socketfile, bool amPostmaster, const char *socketDir)
Definition: miscinit.c:1525
static List * sock_paths
Definition: pqcomm.c:110

References CreateSocketLockFile(), lappend(), pstrdup(), sock_paths, and STATUS_OK.

Referenced by ListenServerPort().

◆ pq_buffer_remaining_data()

ssize_t pq_buffer_remaining_data ( void  )

Definition at line 1127 of file pqcomm.c.

1128 {
1130  return (PqRecvLength - PqRecvPointer);
1131 }
#define Assert(condition)
Definition: c.h:812
static int PqRecvLength
Definition: pqcomm.c:129
static int PqRecvPointer
Definition: pqcomm.c:128

References Assert, PqRecvLength, and PqRecvPointer.

Referenced by ProcessStartupPacket(), and secure_open_server().

◆ pq_check_connection()

bool pq_check_connection ( void  )

Definition at line 2054 of file pqcomm.c.

2055 {
2056  WaitEvent events[FeBeWaitSetNEvents];
2057  int rc;
2058 
2059  /*
2060  * It's OK to modify the socket event filter without restoring, because
2061  * all FeBeWaitSet socket wait sites do the same.
2062  */
2064 
2065 retry:
2066  rc = WaitEventSetWait(FeBeWaitSet, 0, events, lengthof(events), 0);
2067  for (int i = 0; i < rc; ++i)
2068  {
2069  if (events[i].events & WL_SOCKET_CLOSED)
2070  return false;
2071  if (events[i].events & WL_LATCH_SET)
2072  {
2073  /*
2074  * A latch event might be preventing other events from being
2075  * reported. Reset it and poll again. No need to restore it
2076  * because no code should expect latches to survive across
2077  * CHECK_FOR_INTERRUPTS().
2078  */
2080  goto retry;
2081  }
2082  }
2083 
2084  return true;
2085 }
#define lengthof(array)
Definition: c.h:742
struct Latch * MyLatch
Definition: globals.c:62
int i
Definition: isn.c:72
void ModifyWaitEvent(WaitEventSet *set, int pos, uint32 events, Latch *latch)
Definition: latch.c:1043
int WaitEventSetWait(WaitEventSet *set, long timeout, WaitEvent *occurred_events, int nevents, uint32 wait_event_info)
Definition: latch.c:1418
void ResetLatch(Latch *latch)
Definition: latch.c:724
#define WL_SOCKET_CLOSED
Definition: latch.h:139
#define WL_LATCH_SET
Definition: latch.h:127
#define FeBeWaitSetNEvents
Definition: libpq.h:65
#define FeBeWaitSetSocketPos
Definition: libpq.h:63
WaitEventSet * FeBeWaitSet
Definition: pqcomm.c:166

References FeBeWaitSet, FeBeWaitSetNEvents, FeBeWaitSetSocketPos, i, lengthof, ModifyWaitEvent(), MyLatch, ResetLatch(), WaitEventSetWait(), WL_LATCH_SET, and WL_SOCKET_CLOSED.

Referenced by ProcessInterrupts().

◆ pq_discardbytes()

static int pq_discardbytes ( size_t  len)
static

Definition at line 1097 of file pqcomm.c.

1098 {
1099  size_t amount;
1100 
1102 
1103  while (len > 0)
1104  {
1105  while (PqRecvPointer >= PqRecvLength)
1106  {
1107  if (pq_recvbuf()) /* If nothing in buffer, then recv some */
1108  return EOF; /* Failed to recv data */
1109  }
1110  amount = PqRecvLength - PqRecvPointer;
1111  if (amount > len)
1112  amount = len;
1113  PqRecvPointer += amount;
1114  len -= amount;
1115  }
1116  return 0;
1117 }
static int pq_recvbuf(void)
Definition: pqcomm.c:898
static bool PqCommReadingMsg
Definition: pqcomm.c:135

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

Referenced by pq_getmessage().

◆ pq_endmsgread()

void pq_endmsgread ( void  )

Definition at line 1165 of file pqcomm.c.

1166 {
1168 
1169  PqCommReadingMsg = false;
1170 }

References Assert, and PqCommReadingMsg.

Referenced by ProcessRepliesIfAny(), ProcessSSLStartup(), ProcessStartupPacket(), and secure_open_server().

◆ pq_getbyte()

int pq_getbyte ( void  )

Definition at line 964 of file pqcomm.c.

965 {
967 
968  while (PqRecvPointer >= PqRecvLength)
969  {
970  if (pq_recvbuf()) /* If nothing in buffer, then recv some */
971  return EOF; /* Failed to recv data */
972  }
973  return (unsigned char) PqRecvBuffer[PqRecvPointer++];
974 }
static char PqRecvBuffer[PQ_RECV_BUFFER_SIZE]
Definition: pqcomm.c:127

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

Referenced by CheckSASLAuth(), CopyGetData(), HandleUploadManifestPacket(), recv_password_packet(), and SocketBackend().

◆ pq_getbyte_if_available()

int pq_getbyte_if_available ( unsigned char *  c)

Definition at line 1004 of file pqcomm.c.

1005 {
1006  int r;
1007 
1009 
1011  {
1013  return 1;
1014  }
1015 
1016  /* Put the socket into non-blocking mode */
1017  socket_set_nonblocking(true);
1018 
1019  errno = 0;
1020 
1021  r = secure_read(MyProcPort, c, 1);
1022  if (r < 0)
1023  {
1024  /*
1025  * Ok if no data available without blocking or interrupted (though
1026  * EINTR really shouldn't happen with a non-blocking socket). Report
1027  * other errors.
1028  */
1029  if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
1030  r = 0;
1031  else
1032  {
1033  /*
1034  * Careful: an ereport() that tries to write to the client would
1035  * cause recursion to here, leading to stack overflow and core
1036  * dump! This message must go *only* to the postmaster log.
1037  *
1038  * If errno is zero, assume it's EOF and let the caller complain.
1039  */
1040  if (errno != 0)
1043  errmsg("could not receive data from client: %m")));
1044  r = EOF;
1045  }
1046  }
1047  else if (r == 0)
1048  {
1049  /* EOF detected */
1050  r = EOF;
1051  }
1052 
1053  return r;
1054 }
ssize_t secure_read(Port *port, void *ptr, size_t len)
Definition: be-secure.c:179
char * 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().

◆ pq_getbytes()

int pq_getbytes ( char *  s,
size_t  len 
)

Definition at line 1063 of file pqcomm.c.

1064 {
1065  size_t amount;
1066 
1068 
1069  while (len > 0)
1070  {
1071  while (PqRecvPointer >= PqRecvLength)
1072  {
1073  if (pq_recvbuf()) /* If nothing in buffer, then recv some */
1074  return EOF; /* Failed to recv data */
1075  }
1076  amount = PqRecvLength - PqRecvPointer;
1077  if (amount > len)
1078  amount = len;
1079  memcpy(s, PqRecvBuffer + PqRecvPointer, amount);
1080  PqRecvPointer += amount;
1081  s += amount;
1082  len -= amount;
1083  }
1084  return 0;
1085 }

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

Referenced by pq_getmessage(), ProcessStartupPacket(), and secure_open_server().

◆ pq_getkeepalivescount()

int pq_getkeepalivescount ( Port port)

Definition at line 1799 of file pqcomm.c.

1800 {
1801 #ifdef TCP_KEEPCNT
1802  if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
1803  return 0;
1804 
1805  if (port->keepalives_count != 0)
1806  return port->keepalives_count;
1807 
1808  if (port->default_keepalives_count == 0)
1809  {
1810  socklen_t size = sizeof(port->default_keepalives_count);
1811 
1812  if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPCNT,
1813  (char *) &port->default_keepalives_count,
1814  &size) < 0)
1815  {
1816  ereport(LOG,
1817  (errmsg("%s(%s) failed: %m", "getsockopt", "TCP_KEEPCNT")));
1818  port->default_keepalives_count = -1; /* don't know */
1819  }
1820  }
1821 
1822  return port->default_keepalives_count;
1823 #else
1824  return 0;
1825 #endif
1826 }
static int port
Definition: pg_regress.c:115
unsigned int socklen_t
Definition: port.h:40
static pg_noinline void Size size
Definition: slab.c:607

References ereport, errmsg(), LOG, port, and size.

Referenced by pq_setkeepalivescount(), and show_tcp_keepalives_count().

◆ pq_getkeepalivesidle()

int pq_getkeepalivesidle ( Port port)

Definition at line 1630 of file pqcomm.c.

1631 {
1632 #if defined(PG_TCP_KEEPALIVE_IDLE) || defined(SIO_KEEPALIVE_VALS)
1633  if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
1634  return 0;
1635 
1636  if (port->keepalives_idle != 0)
1637  return port->keepalives_idle;
1638 
1639  if (port->default_keepalives_idle == 0)
1640  {
1641 #ifndef WIN32
1642  socklen_t size = sizeof(port->default_keepalives_idle);
1643 
1644  if (getsockopt(port->sock, IPPROTO_TCP, PG_TCP_KEEPALIVE_IDLE,
1645  (char *) &port->default_keepalives_idle,
1646  &size) < 0)
1647  {
1648  ereport(LOG,
1649  (errmsg("%s(%s) failed: %m", "getsockopt", PG_TCP_KEEPALIVE_IDLE_STR)));
1650  port->default_keepalives_idle = -1; /* don't know */
1651  }
1652 #else /* WIN32 */
1653  /* We can't get the defaults on Windows, so return "don't know" */
1654  port->default_keepalives_idle = -1;
1655 #endif /* WIN32 */
1656  }
1657 
1658  return port->default_keepalives_idle;
1659 #else
1660  return 0;
1661 #endif
1662 }

References ereport, errmsg(), LOG, port, and size.

Referenced by pq_setkeepalivesidle(), and show_tcp_keepalives_idle().

◆ pq_getkeepalivesinterval()

int pq_getkeepalivesinterval ( Port port)

Definition at line 1715 of file pqcomm.c.

1716 {
1717 #if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
1718  if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
1719  return 0;
1720 
1721  if (port->keepalives_interval != 0)
1722  return port->keepalives_interval;
1723 
1724  if (port->default_keepalives_interval == 0)
1725  {
1726 #ifndef WIN32
1727  socklen_t size = sizeof(port->default_keepalives_interval);
1728 
1729  if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPINTVL,
1730  (char *) &port->default_keepalives_interval,
1731  &size) < 0)
1732  {
1733  ereport(LOG,
1734  (errmsg("%s(%s) failed: %m", "getsockopt", "TCP_KEEPINTVL")));
1735  port->default_keepalives_interval = -1; /* don't know */
1736  }
1737 #else
1738  /* We can't get the defaults on Windows, so return "don't know" */
1739  port->default_keepalives_interval = -1;
1740 #endif /* WIN32 */
1741  }
1742 
1743  return port->default_keepalives_interval;
1744 #else
1745  return 0;
1746 #endif
1747 }

References ereport, errmsg(), LOG, port, and size.

Referenced by pq_setkeepalivesinterval(), and show_tcp_keepalives_interval().

◆ pq_getmessage()

int pq_getmessage ( StringInfo  s,
int  maxlen 
)

Definition at line 1203 of file pqcomm.c.

1204 {
1205  int32 len;
1206 
1208 
1209  resetStringInfo(s);
1210 
1211  /* Read message length word */
1212  if (pq_getbytes((char *) &len, 4) == EOF)
1213  {
1215  (errcode(ERRCODE_PROTOCOL_VIOLATION),
1216  errmsg("unexpected EOF within message length word")));
1217  return EOF;
1218  }
1219 
1220  len = pg_ntoh32(len);
1221 
1222  if (len < 4 || len > maxlen)
1223  {
1225  (errcode(ERRCODE_PROTOCOL_VIOLATION),
1226  errmsg("invalid message length")));
1227  return EOF;
1228  }
1229 
1230  len -= 4; /* discount length itself */
1231 
1232  if (len > 0)
1233  {
1234  /*
1235  * Allocate space for message. If we run out of room (ridiculously
1236  * large message), we will elog(ERROR), but we want to discard the
1237  * message body so as not to lose communication sync.
1238  */
1239  PG_TRY();
1240  {
1241  enlargeStringInfo(s, len);
1242  }
1243  PG_CATCH();
1244  {
1245  if (pq_discardbytes(len) == EOF)
1247  (errcode(ERRCODE_PROTOCOL_VIOLATION),
1248  errmsg("incomplete message from client")));
1249 
1250  /* we discarded the rest of the message so we're back in sync. */
1251  PqCommReadingMsg = false;
1252  PG_RE_THROW();
1253  }
1254  PG_END_TRY();
1255 
1256  /* And grab the message */
1257  if (pq_getbytes(s->data, len) == EOF)
1258  {
1260  (errcode(ERRCODE_PROTOCOL_VIOLATION),
1261  errmsg("incomplete message from client")));
1262  return EOF;
1263  }
1264  s->len = len;
1265  /* Place a trailing null per StringInfo convention */
1266  s->data[len] = '\0';
1267  }
1268 
1269  /* finished reading the message. */
1270  PqCommReadingMsg = false;
1271 
1272  return 0;
1273 }
int32_t int32
Definition: c.h:481
int errcode(int sqlerrcode)
Definition: elog.c:853
#define PG_RE_THROW()
Definition: elog.h:412
#define PG_TRY(...)
Definition: elog.h:371
#define PG_END_TRY(...)
Definition: elog.h:396
#define PG_CATCH(...)
Definition: elog.h:381
#define pg_ntoh32(x)
Definition: pg_bswap.h:125
static int pq_discardbytes(size_t len)
Definition: pqcomm.c:1097
int pq_getbytes(char *s, size_t len)
Definition: pqcomm.c:1063
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:75
void enlargeStringInfo(StringInfo str, int needed)
Definition: stringinfo.c:286

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

Referenced by CheckSASLAuth(), CopyGetData(), HandleUploadManifestPacket(), ProcessRepliesIfAny(), recv_password_packet(), and SocketBackend().

◆ pq_gettcpusertimeout()

int pq_gettcpusertimeout ( Port port)

Definition at line 1874 of file pqcomm.c.

1875 {
1876 #ifdef TCP_USER_TIMEOUT
1877  if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
1878  return 0;
1879 
1880  if (port->tcp_user_timeout != 0)
1881  return port->tcp_user_timeout;
1882 
1883  if (port->default_tcp_user_timeout == 0)
1884  {
1885  socklen_t size = sizeof(port->default_tcp_user_timeout);
1886 
1887  if (getsockopt(port->sock, IPPROTO_TCP, TCP_USER_TIMEOUT,
1888  (char *) &port->default_tcp_user_timeout,
1889  &size) < 0)
1890  {
1891  ereport(LOG,
1892  (errmsg("%s(%s) failed: %m", "getsockopt", "TCP_USER_TIMEOUT")));
1893  port->default_tcp_user_timeout = -1; /* don't know */
1894  }
1895  }
1896 
1897  return port->default_tcp_user_timeout;
1898 #else
1899  return 0;
1900 #endif
1901 }

References ereport, errmsg(), LOG, port, and size.

Referenced by pq_settcpusertimeout(), and show_tcp_user_timeout().

◆ pq_init()

Port* pq_init ( ClientSocket client_sock)

Definition at line 174 of file pqcomm.c.

175 {
176  Port *port;
177  int socket_pos PG_USED_FOR_ASSERTS_ONLY;
178  int latch_pos PG_USED_FOR_ASSERTS_ONLY;
179 
180  /* allocate the Port struct and copy the ClientSocket contents to it */
181  port = palloc0(sizeof(Port));
182  port->sock = client_sock->sock;
183  memcpy(&port->raddr.addr, &client_sock->raddr.addr, client_sock->raddr.salen);
184  port->raddr.salen = client_sock->raddr.salen;
185 
186  /* fill in the server (local) address */
187  port->laddr.salen = sizeof(port->laddr.addr);
188  if (getsockname(port->sock,
189  (struct sockaddr *) &port->laddr.addr,
190  &port->laddr.salen) < 0)
191  {
192  ereport(FATAL,
193  (errmsg("%s() failed: %m", "getsockname")));
194  }
195 
196  /* select NODELAY and KEEPALIVE options if it's a TCP connection */
197  if (port->laddr.addr.ss_family != AF_UNIX)
198  {
199  int on;
200 #ifdef WIN32
201  int oldopt;
202  int optlen;
203  int newopt;
204 #endif
205 
206 #ifdef TCP_NODELAY
207  on = 1;
208  if (setsockopt(port->sock, IPPROTO_TCP, TCP_NODELAY,
209  (char *) &on, sizeof(on)) < 0)
210  {
211  ereport(FATAL,
212  (errmsg("%s(%s) failed: %m", "setsockopt", "TCP_NODELAY")));
213  }
214 #endif
215  on = 1;
216  if (setsockopt(port->sock, SOL_SOCKET, SO_KEEPALIVE,
217  (char *) &on, sizeof(on)) < 0)
218  {
219  ereport(FATAL,
220  (errmsg("%s(%s) failed: %m", "setsockopt", "SO_KEEPALIVE")));
221  }
222 
223 #ifdef WIN32
224 
225  /*
226  * This is a Win32 socket optimization. The OS send buffer should be
227  * large enough to send the whole Postgres send buffer in one go, or
228  * performance suffers. The Postgres send buffer can be enlarged if a
229  * very large message needs to be sent, but we won't attempt to
230  * enlarge the OS buffer if that happens, so somewhat arbitrarily
231  * ensure that the OS buffer is at least PQ_SEND_BUFFER_SIZE * 4.
232  * (That's 32kB with the current default).
233  *
234  * The default OS buffer size used to be 8kB in earlier Windows
235  * versions, but was raised to 64kB in Windows 2012. So it shouldn't
236  * be necessary to change it in later versions anymore. Changing it
237  * unnecessarily can even reduce performance, because setting
238  * SO_SNDBUF in the application disables the "dynamic send buffering"
239  * feature that was introduced in Windows 7. So before fiddling with
240  * SO_SNDBUF, check if the current buffer size is already large enough
241  * and only increase it if necessary.
242  *
243  * See https://support.microsoft.com/kb/823764/EN-US/ and
244  * https://msdn.microsoft.com/en-us/library/bb736549%28v=vs.85%29.aspx
245  */
246  optlen = sizeof(oldopt);
247  if (getsockopt(port->sock, SOL_SOCKET, SO_SNDBUF, (char *) &oldopt,
248  &optlen) < 0)
249  {
250  ereport(FATAL,
251  (errmsg("%s(%s) failed: %m", "getsockopt", "SO_SNDBUF")));
252  }
253  newopt = PQ_SEND_BUFFER_SIZE * 4;
254  if (oldopt < newopt)
255  {
256  if (setsockopt(port->sock, SOL_SOCKET, SO_SNDBUF, (char *) &newopt,
257  sizeof(newopt)) < 0)
258  {
259  ereport(FATAL,
260  (errmsg("%s(%s) failed: %m", "setsockopt", "SO_SNDBUF")));
261  }
262  }
263 #endif
264 
265  /*
266  * Also apply the current keepalive parameters. If we fail to set a
267  * parameter, don't error out, because these aren't universally
268  * supported. (Note: you might think we need to reset the GUC
269  * variables to 0 in such a case, but it's not necessary because the
270  * show hooks for these variables report the truth anyway.)
271  */
276  }
277 
278  /* initialize state variables */
282  PqCommBusy = false;
283  PqCommReadingMsg = false;
284 
285  /* set up process-exit hook to close the socket */
287 
288  /*
289  * In backends (as soon as forked) we operate the underlying socket in
290  * nonblocking mode and use latches to implement blocking semantics if
291  * needed. That allows us to provide safely interruptible reads and
292  * writes.
293  */
294 #ifndef WIN32
295  if (!pg_set_noblock(port->sock))
296  ereport(FATAL,
297  (errmsg("could not set socket to nonblocking mode: %m")));
298 #endif
299 
300 #ifndef WIN32
301 
302  /* Don't give the socket to any subprograms we execute. */
303  if (fcntl(port->sock, F_SETFD, FD_CLOEXEC) < 0)
304  elog(FATAL, "fcntl(F_SETFD) failed on socket: %m");
305 #endif
306 
309  port->sock, NULL, NULL);
311  MyLatch, NULL);
313  NULL, NULL);
314 
315  /*
316  * The event positions match the order we added them, but let's sanity
317  * check them to be sure.
318  */
319  Assert(socket_pos == FeBeWaitSetSocketPos);
320  Assert(latch_pos == FeBeWaitSetLatchPos);
321 
322  return port;
323 }
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:201
int tcp_keepalives_idle
Definition: guc_tables.c:545
int tcp_keepalives_interval
Definition: guc_tables.c:546
int tcp_keepalives_count
Definition: guc_tables.c:547
int tcp_user_timeout
Definition: guc_tables.c:548
void on_proc_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:309
WaitEventSet * CreateWaitEventSet(ResourceOwner resowner, int nevents)
Definition: latch.c:751
int AddWaitEventToSet(WaitEventSet *set, uint32 events, pgsocket fd, Latch *latch, void *user_data)
Definition: latch.c:957
#define WL_POSTMASTER_DEATH
Definition: latch.h:131
#define WL_SOCKET_WRITEABLE
Definition: latch.h:129
#define FeBeWaitSetLatchPos
Definition: libpq.h:64
MemoryContext TopMemoryContext
Definition: mcxt.c:149
void * palloc0(Size size)
Definition: mcxt.c:1347
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1181
bool pg_set_noblock(pgsocket sock)
Definition: noblock.c:25
#define PQ_SEND_BUFFER_SIZE
Definition: pqcomm.c:119
static bool PqCommBusy
Definition: pqcomm.c:134
static void socket_close(int code, Datum arg)
Definition: pqcomm.c:349
Definition: libpq-be.h:133

References SockAddr::addr, AddWaitEventToSet(), Assert, CreateWaitEventSet(), elog, ereport, errmsg(), FATAL, FeBeWaitSet, FeBeWaitSetLatchPos, FeBeWaitSetNEvents, FeBeWaitSetSocketPos, MemoryContextAlloc(), MyLatch, on_proc_exit(), palloc0(), pg_set_noblock(), PG_USED_FOR_ASSERTS_ONLY, PGINVALID_SOCKET, port, PQ_SEND_BUFFER_SIZE, pq_setkeepalivescount(), pq_setkeepalivesidle(), pq_setkeepalivesinterval(), pq_settcpusertimeout(), PqCommBusy, PqCommReadingMsg, PqRecvLength, PqRecvPointer, PqSendBuffer, PqSendBufferSize, PqSendPointer, PqSendStart, ClientSocket::raddr, SockAddr::salen, ClientSocket::sock, socket_close(), tcp_keepalives_count, tcp_keepalives_idle, tcp_keepalives_interval, tcp_user_timeout, TopMemoryContext, WL_LATCH_SET, WL_POSTMASTER_DEATH, and WL_SOCKET_WRITEABLE.

Referenced by BackendInitialize().

◆ pq_is_reading_msg()

bool pq_is_reading_msg ( void  )

Definition at line 1181 of file pqcomm.c.

1182 {
1183  return PqCommReadingMsg;
1184 }

References PqCommReadingMsg.

Referenced by PostgresMain().

◆ pq_peekbyte()

int pq_peekbyte ( void  )

Definition at line 983 of file pqcomm.c.

984 {
986 
987  while (PqRecvPointer >= PqRecvLength)
988  {
989  if (pq_recvbuf()) /* If nothing in buffer, then recv some */
990  return EOF; /* Failed to recv data */
991  }
992  return (unsigned char) PqRecvBuffer[PqRecvPointer];
993 }

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

Referenced by ProcessSSLStartup().

◆ pq_putmessage_v2()

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

Definition at line 1559 of file pqcomm.c.

1560 {
1561  Assert(msgtype != 0);
1562 
1563  if (PqCommBusy)
1564  return 0;
1565  PqCommBusy = true;
1566  if (internal_putbytes(&msgtype, 1))
1567  goto fail;
1568 
1569  if (internal_putbytes(s, len))
1570  goto fail;
1571  PqCommBusy = false;
1572  return 0;
1573 
1574 fail:
1575  PqCommBusy = false;
1576  return EOF;
1577 }
static int internal_putbytes(const char *s, size_t len)
Definition: pqcomm.c:1277

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

Referenced by send_message_to_frontend().

◆ pq_recvbuf()

static int pq_recvbuf ( void  )
static

Definition at line 898 of file pqcomm.c.

899 {
900  if (PqRecvPointer > 0)
901  {
903  {
904  /* still some unread data, left-justify it in the buffer */
908  PqRecvPointer = 0;
909  }
910  else
912  }
913 
914  /* Ensure that we're in blocking mode */
915  socket_set_nonblocking(false);
916 
917  /* Can fill buffer from PqRecvLength and upwards */
918  for (;;)
919  {
920  int r;
921 
922  errno = 0;
923 
926 
927  if (r < 0)
928  {
929  if (errno == EINTR)
930  continue; /* Ok if interrupted */
931 
932  /*
933  * Careful: an ereport() that tries to write to the client would
934  * cause recursion to here, leading to stack overflow and core
935  * dump! This message must go *only* to the postmaster log.
936  *
937  * If errno is zero, assume it's EOF and let the caller complain.
938  */
939  if (errno != 0)
942  errmsg("could not receive data from client: %m")));
943  return EOF;
944  }
945  if (r == 0)
946  {
947  /*
948  * EOF detected. We used to write a log message here, but it's
949  * better to expect the ultimate caller to do that.
950  */
951  return EOF;
952  }
953  /* r contains number of bytes read, so just incr length */
954  PqRecvLength += r;
955  return 0;
956  }
957 }
#define PQ_RECV_BUFFER_SIZE
Definition: pqcomm.c:120

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

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

◆ pq_setkeepalivescount()

int pq_setkeepalivescount ( int  count,
Port port 
)

Definition at line 1829 of file pqcomm.c.

1830 {
1831  if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
1832  return STATUS_OK;
1833 
1834 #ifdef TCP_KEEPCNT
1835  if (count == port->keepalives_count)
1836  return STATUS_OK;
1837 
1838  if (port->default_keepalives_count <= 0)
1839  {
1840  if (pq_getkeepalivescount(port) < 0)
1841  {
1842  if (count == 0)
1843  return STATUS_OK; /* default is set but unknown */
1844  else
1845  return STATUS_ERROR;
1846  }
1847  }
1848 
1849  if (count == 0)
1850  count = port->default_keepalives_count;
1851 
1852  if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPCNT,
1853  (char *) &count, sizeof(count)) < 0)
1854  {
1855  ereport(LOG,
1856  (errmsg("%s(%s) failed: %m", "setsockopt", "TCP_KEEPCNT")));
1857  return STATUS_ERROR;
1858  }
1859 
1860  port->keepalives_count = count;
1861 #else
1862  if (count != 0)
1863  {
1864  ereport(LOG,
1865  (errmsg("%s(%s) not supported", "setsockopt", "TCP_KEEPCNT")));
1866  return STATUS_ERROR;
1867  }
1868 #endif
1869 
1870  return STATUS_OK;
1871 }
int pq_getkeepalivescount(Port *port)
Definition: pqcomm.c:1799

References ereport, errmsg(), LOG, port, pq_getkeepalivescount(), STATUS_ERROR, and STATUS_OK.

Referenced by assign_tcp_keepalives_count(), and pq_init().

◆ pq_setkeepalivesidle()

int pq_setkeepalivesidle ( int  idle,
Port port 
)

Definition at line 1665 of file pqcomm.c.

1666 {
1667  if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
1668  return STATUS_OK;
1669 
1670 /* check SIO_KEEPALIVE_VALS here, not just WIN32, as some toolchains lack it */
1671 #if defined(PG_TCP_KEEPALIVE_IDLE) || defined(SIO_KEEPALIVE_VALS)
1672  if (idle == port->keepalives_idle)
1673  return STATUS_OK;
1674 
1675 #ifndef WIN32
1676  if (port->default_keepalives_idle <= 0)
1677  {
1678  if (pq_getkeepalivesidle(port) < 0)
1679  {
1680  if (idle == 0)
1681  return STATUS_OK; /* default is set but unknown */
1682  else
1683  return STATUS_ERROR;
1684  }
1685  }
1686 
1687  if (idle == 0)
1688  idle = port->default_keepalives_idle;
1689 
1690  if (setsockopt(port->sock, IPPROTO_TCP, PG_TCP_KEEPALIVE_IDLE,
1691  (char *) &idle, sizeof(idle)) < 0)
1692  {
1693  ereport(LOG,
1694  (errmsg("%s(%s) failed: %m", "setsockopt", PG_TCP_KEEPALIVE_IDLE_STR)));
1695  return STATUS_ERROR;
1696  }
1697 
1698  port->keepalives_idle = idle;
1699 #else /* WIN32 */
1700  return pq_setkeepaliveswin32(port, idle, port->keepalives_interval);
1701 #endif
1702 #else
1703  if (idle != 0)
1704  {
1705  ereport(LOG,
1706  (errmsg("setting the keepalive idle time is not supported")));
1707  return STATUS_ERROR;
1708  }
1709 #endif
1710 
1711  return STATUS_OK;
1712 }
int pq_getkeepalivesidle(Port *port)
Definition: pqcomm.c:1630

References ereport, errmsg(), LOG, port, pq_getkeepalivesidle(), STATUS_ERROR, and STATUS_OK.

Referenced by assign_tcp_keepalives_idle(), and pq_init().

◆ pq_setkeepalivesinterval()

int pq_setkeepalivesinterval ( int  interval,
Port port 
)

Definition at line 1750 of file pqcomm.c.

1751 {
1752  if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
1753  return STATUS_OK;
1754 
1755 #if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
1756  if (interval == port->keepalives_interval)
1757  return STATUS_OK;
1758 
1759 #ifndef WIN32
1760  if (port->default_keepalives_interval <= 0)
1761  {
1762  if (pq_getkeepalivesinterval(port) < 0)
1763  {
1764  if (interval == 0)
1765  return STATUS_OK; /* default is set but unknown */
1766  else
1767  return STATUS_ERROR;
1768  }
1769  }
1770 
1771  if (interval == 0)
1772  interval = port->default_keepalives_interval;
1773 
1774  if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPINTVL,
1775  (char *) &interval, sizeof(interval)) < 0)
1776  {
1777  ereport(LOG,
1778  (errmsg("%s(%s) failed: %m", "setsockopt", "TCP_KEEPINTVL")));
1779  return STATUS_ERROR;
1780  }
1781 
1782  port->keepalives_interval = interval;
1783 #else /* WIN32 */
1784  return pq_setkeepaliveswin32(port, port->keepalives_idle, interval);
1785 #endif
1786 #else
1787  if (interval != 0)
1788  {
1789  ereport(LOG,
1790  (errmsg("%s(%s) not supported", "setsockopt", "TCP_KEEPINTVL")));
1791  return STATUS_ERROR;
1792  }
1793 #endif
1794 
1795  return STATUS_OK;
1796 }
int pq_getkeepalivesinterval(Port *port)
Definition: pqcomm.c:1715

References ereport, errmsg(), LOG, port, pq_getkeepalivesinterval(), STATUS_ERROR, and STATUS_OK.

Referenced by assign_tcp_keepalives_interval(), and pq_init().

◆ pq_settcpusertimeout()

int pq_settcpusertimeout ( int  timeout,
Port port 
)

Definition at line 1904 of file pqcomm.c.

1905 {
1906  if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
1907  return STATUS_OK;
1908 
1909 #ifdef TCP_USER_TIMEOUT
1910  if (timeout == port->tcp_user_timeout)
1911  return STATUS_OK;
1912 
1913  if (port->default_tcp_user_timeout <= 0)
1914  {
1915  if (pq_gettcpusertimeout(port) < 0)
1916  {
1917  if (timeout == 0)
1918  return STATUS_OK; /* default is set but unknown */
1919  else
1920  return STATUS_ERROR;
1921  }
1922  }
1923 
1924  if (timeout == 0)
1925  timeout = port->default_tcp_user_timeout;
1926 
1927  if (setsockopt(port->sock, IPPROTO_TCP, TCP_USER_TIMEOUT,
1928  (char *) &timeout, sizeof(timeout)) < 0)
1929  {
1930  ereport(LOG,
1931  (errmsg("%s(%s) failed: %m", "setsockopt", "TCP_USER_TIMEOUT")));
1932  return STATUS_ERROR;
1933  }
1934 
1935  port->tcp_user_timeout = timeout;
1936 #else
1937  if (timeout != 0)
1938  {
1939  ereport(LOG,
1940  (errmsg("%s(%s) not supported", "setsockopt", "TCP_USER_TIMEOUT")));
1941  return STATUS_ERROR;
1942  }
1943 #endif
1944 
1945  return STATUS_OK;
1946 }
int pq_gettcpusertimeout(Port *port)
Definition: pqcomm.c:1874

References ereport, errmsg(), LOG, port, pq_gettcpusertimeout(), STATUS_ERROR, and STATUS_OK.

Referenced by assign_tcp_user_timeout(), and pq_init().

◆ pq_startmsgread()

void pq_startmsgread ( void  )

Definition at line 1141 of file pqcomm.c.

1142 {
1143  /*
1144  * There shouldn't be a read active already, but let's check just to be
1145  * sure.
1146  */
1147  if (PqCommReadingMsg)
1148  ereport(FATAL,
1149  (errcode(ERRCODE_PROTOCOL_VIOLATION),
1150  errmsg("terminating connection because protocol synchronization was lost")));
1151 
1152  PqCommReadingMsg = true;
1153 }

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

Referenced by CheckSASLAuth(), CopyGetData(), HandleUploadManifestPacket(), ProcessRepliesIfAny(), ProcessSSLStartup(), ProcessStartupPacket(), recv_password_packet(), secure_open_server(), and SocketBackend().

◆ RemoveSocketFiles()

void RemoveSocketFiles ( void  )

Definition at line 848 of file pqcomm.c.

849 {
850  ListCell *l;
851 
852  /* Loop through all created sockets... */
853  foreach(l, sock_paths)
854  {
855  char *sock_path = (char *) lfirst(l);
856 
857  /* Ignore any error. */
858  (void) unlink(sock_path);
859  }
860  /* Since we're about to exit, no need to reclaim storage */
861  sock_paths = NIL;
862 }
#define lfirst(lc)
Definition: pg_list.h:172
#define NIL
Definition: pg_list.h:68

References lfirst, NIL, and sock_paths.

Referenced by CloseServerPorts().

◆ Setup_AF_UNIX()

static int Setup_AF_UNIX ( const char *  sock_path)
static

Definition at line 720 of file pqcomm.c.

721 {
722  /* no file system permissions for abstract sockets */
723  if (sock_path[0] == '@')
724  return STATUS_OK;
725 
726  /*
727  * Fix socket ownership/permission if requested. Note we must do this
728  * before we listen() to avoid a window where unwanted connections could
729  * get accepted.
730  */
732  if (Unix_socket_group[0] != '\0')
733  {
734 #ifdef WIN32
735  elog(WARNING, "configuration item \"unix_socket_group\" is not supported on this platform");
736 #else
737  char *endptr;
738  unsigned long val;
739  gid_t gid;
740 
741  val = strtoul(Unix_socket_group, &endptr, 10);
742  if (*endptr == '\0')
743  { /* numeric group id */
744  gid = val;
745  }
746  else
747  { /* convert group name to id */
748  struct group *gr;
749 
750  gr = getgrnam(Unix_socket_group);
751  if (!gr)
752  {
753  ereport(LOG,
754  (errmsg("group \"%s\" does not exist",
756  return STATUS_ERROR;
757  }
758  gid = gr->gr_gid;
759  }
760  if (chown(sock_path, -1, gid) == -1)
761  {
762  ereport(LOG,
764  errmsg("could not set group of file \"%s\": %m",
765  sock_path)));
766  return STATUS_ERROR;
767  }
768 #endif
769  }
770 
771  if (chmod(sock_path, Unix_socket_permissions) == -1)
772  {
773  ereport(LOG,
775  errmsg("could not set permissions of file \"%s\": %m",
776  sock_path)));
777  return STATUS_ERROR;
778  }
779  return STATUS_OK;
780 }
int errcode_for_file_access(void)
Definition: elog.c:876
#define WARNING
Definition: elog.h:36
long val
Definition: informix.c:689
int Unix_socket_permissions
Definition: pqcomm.c:106
char * Unix_socket_group
Definition: pqcomm.c:107
int gid_t
Definition: win32_port.h:245

References Assert, elog, ereport, errcode_for_file_access(), errmsg(), LOG, STATUS_ERROR, STATUS_OK, Unix_socket_group, Unix_socket_permissions, val, and WARNING.

Referenced by ListenServerPort().

◆ show_tcp_keepalives_count()

const char* show_tcp_keepalives_count ( void  )

Definition at line 2018 of file pqcomm.c.

2019 {
2020  /* See comments in assign_tcp_keepalives_idle */
2021  static char nbuf[16];
2022 
2023  snprintf(nbuf, sizeof(nbuf), "%d", pq_getkeepalivescount(MyProcPort));
2024  return nbuf;
2025 }

References MyProcPort, pq_getkeepalivescount(), and snprintf.

◆ show_tcp_keepalives_idle()

const char* show_tcp_keepalives_idle ( void  )

Definition at line 1972 of file pqcomm.c.

1973 {
1974  /* See comments in assign_tcp_keepalives_idle */
1975  static char nbuf[16];
1976 
1977  snprintf(nbuf, sizeof(nbuf), "%d", pq_getkeepalivesidle(MyProcPort));
1978  return nbuf;
1979 }

References MyProcPort, pq_getkeepalivesidle(), and snprintf.

◆ show_tcp_keepalives_interval()

const char* show_tcp_keepalives_interval ( void  )

Definition at line 1995 of file pqcomm.c.

1996 {
1997  /* See comments in assign_tcp_keepalives_idle */
1998  static char nbuf[16];
1999 
2000  snprintf(nbuf, sizeof(nbuf), "%d", pq_getkeepalivesinterval(MyProcPort));
2001  return nbuf;
2002 }

References MyProcPort, pq_getkeepalivesinterval(), and snprintf.

◆ show_tcp_user_timeout()

const char* show_tcp_user_timeout ( void  )

Definition at line 2041 of file pqcomm.c.

2042 {
2043  /* See comments in assign_tcp_keepalives_idle */
2044  static char nbuf[16];
2045 
2046  snprintf(nbuf, sizeof(nbuf), "%d", pq_gettcpusertimeout(MyProcPort));
2047  return nbuf;
2048 }

References MyProcPort, pq_gettcpusertimeout(), and snprintf.

◆ socket_close()

static void socket_close ( int  code,
Datum  arg 
)
static

Definition at line 349 of file pqcomm.c.

350 {
351  /* Nothing to do in a standalone backend, where MyProcPort is NULL. */
352  if (MyProcPort != NULL)
353  {
354 #ifdef ENABLE_GSS
355  /*
356  * Shutdown GSSAPI layer. This section does nothing when interrupting
357  * BackendInitialize(), because pg_GSS_recvauth() makes first use of
358  * "ctx" and "cred".
359  *
360  * Note that we don't bother to free MyProcPort->gss, since we're
361  * about to exit anyway.
362  */
363  if (MyProcPort->gss)
364  {
365  OM_uint32 min_s;
366 
367  if (MyProcPort->gss->ctx != GSS_C_NO_CONTEXT)
368  gss_delete_sec_context(&min_s, &MyProcPort->gss->ctx, NULL);
369 
370  if (MyProcPort->gss->cred != GSS_C_NO_CREDENTIAL)
371  gss_release_cred(&min_s, &MyProcPort->gss->cred);
372  }
373 #endif /* ENABLE_GSS */
374 
375  /*
376  * Cleanly shut down SSL layer. Nowhere else does a postmaster child
377  * call this, so this is safe when interrupting BackendInitialize().
378  */
380 
381  /*
382  * Formerly we did an explicit close() here, but it seems better to
383  * leave the socket open until the process dies. This allows clients
384  * to perform a "synchronous close" if they care --- wait till the
385  * transport layer reports connection closure, and you can be sure the
386  * backend has exited.
387  *
388  * We do set sock to PGINVALID_SOCKET to prevent any further I/O,
389  * though.
390  */
392  }
393 }
void secure_close(Port *port)
Definition: be-secure.c:167
void * gss
Definition: libpq-be.h:196
pgsocket sock
Definition: libpq-be.h:134

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

Referenced by pq_init().

◆ socket_comm_reset()

static void socket_comm_reset ( void  )
static

Definition at line 334 of file pqcomm.c.

335 {
336  /* Do not throw away pending data, but do reset the busy flag */
337  PqCommBusy = false;
338 }

References PqCommBusy.

◆ socket_flush()

static int socket_flush ( void  )
static

Definition at line 1325 of file pqcomm.c.

1326 {
1327  int res;
1328 
1329  /* No-op if reentrant call */
1330  if (PqCommBusy)
1331  return 0;
1332  PqCommBusy = true;
1333  socket_set_nonblocking(false);
1334  res = internal_flush();
1335  PqCommBusy = false;
1336  return res;
1337 }

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

◆ socket_flush_if_writable()

static int socket_flush_if_writable ( void  )
static

Definition at line 1433 of file pqcomm.c.

1434 {
1435  int res;
1436 
1437  /* Quick exit if nothing to do */
1438  if (PqSendPointer == PqSendStart)
1439  return 0;
1440 
1441  /* No-op if reentrant call */
1442  if (PqCommBusy)
1443  return 0;
1444 
1445  /* Temporarily put the socket into non-blocking mode */
1446  socket_set_nonblocking(true);
1447 
1448  PqCommBusy = true;
1449  res = internal_flush();
1450  PqCommBusy = false;
1451  return res;
1452 }

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

◆ socket_is_send_pending()

static bool socket_is_send_pending ( void  )
static

Definition at line 1459 of file pqcomm.c.

1460 {
1461  return (PqSendStart < PqSendPointer);
1462 }

References PqSendPointer, and PqSendStart.

◆ socket_putmessage()

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

Definition at line 1489 of file pqcomm.c.

1490 {
1491  uint32 n32;
1492 
1493  Assert(msgtype != 0);
1494 
1495  if (PqCommBusy)
1496  return 0;
1497  PqCommBusy = true;
1498  if (internal_putbytes(&msgtype, 1))
1499  goto fail;
1500 
1501  n32 = pg_hton32((uint32) (len + 4));
1502  if (internal_putbytes((char *) &n32, 4))
1503  goto fail;
1504 
1505  if (internal_putbytes(s, len))
1506  goto fail;
1507  PqCommBusy = false;
1508  return 0;
1509 
1510 fail:
1511  PqCommBusy = false;
1512  return EOF;
1513 }
uint32_t uint32
Definition: c.h:485
#define pg_hton32(x)
Definition: pg_bswap.h:121

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

◆ socket_putmessage_noblock()

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

Definition at line 1522 of file pqcomm.c.

1523 {
1525  int required;
1526 
1527  /*
1528  * Ensure we have enough space in the output buffer for the message header
1529  * as well as the message itself.
1530  */
1531  required = PqSendPointer + 1 + 4 + len;
1532  if (required > PqSendBufferSize)
1533  {
1536  }
1537  res = pq_putmessage(msgtype, s, len);
1538  Assert(res == 0); /* should not fail when the message fits in
1539  * buffer */
1540 }
#define pq_putmessage(msgtype, s, len)
Definition: libpq.h:49
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1541

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

◆ socket_set_nonblocking()

static void socket_set_nonblocking ( bool  nonblocking)
static

Definition at line 881 of file pqcomm.c.

882 {
883  if (MyProcPort == NULL)
884  ereport(ERROR,
885  (errcode(ERRCODE_CONNECTION_DOES_NOT_EXIST),
886  errmsg("there is no client connection")));
887 
888  MyProcPort->noblock = nonblocking;
889 }
#define ERROR
Definition: elog.h:39
bool noblock
Definition: libpq-be.h:135

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().

◆ TouchSocketFiles()

void TouchSocketFiles ( void  )

Definition at line 830 of file pqcomm.c.

831 {
832  ListCell *l;
833 
834  /* Loop through all created sockets... */
835  foreach(l, sock_paths)
836  {
837  char *sock_path = (char *) lfirst(l);
838 
839  /* Ignore errors; there's no point in complaining */
840  (void) utime(sock_path, NULL);
841  }
842 }

References lfirst, and sock_paths.

Referenced by ServerLoop().

Variable Documentation

◆ FeBeWaitSet

◆ PqCommBusy

bool PqCommBusy
static

◆ PqCommMethods

const PQcommMethods* PqCommMethods = &PqCommSocketMethods

Definition at line 164 of file pqcomm.c.

Referenced by pq_redirect_to_shm_mq(), and pq_set_parallel_leader().

◆ PqCommReadingMsg

◆ PqCommSocketMethods

const PQcommMethods PqCommSocketMethods
static
Initial value:
= {
.comm_reset = socket_comm_reset,
.flush = socket_flush,
.flush_if_writable = socket_flush_if_writable,
.is_send_pending = socket_is_send_pending,
.putmessage = socket_putmessage,
.putmessage_noblock = socket_putmessage_noblock
}
static int socket_flush_if_writable(void)
Definition: pqcomm.c:1433
static int socket_flush(void)
Definition: pqcomm.c:1325
static void socket_comm_reset(void)
Definition: pqcomm.c:334
static bool socket_is_send_pending(void)
Definition: pqcomm.c:1459
static int socket_putmessage(char msgtype, const char *s, size_t len)
Definition: pqcomm.c:1489
static void socket_putmessage_noblock(char msgtype, const char *s, size_t len)
Definition: pqcomm.c:1522

Definition at line 155 of file pqcomm.c.

◆ PqRecvBuffer

char PqRecvBuffer[PQ_RECV_BUFFER_SIZE]
static

Definition at line 127 of file pqcomm.c.

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

◆ PqRecvLength

◆ PqRecvPointer

◆ PqSendBuffer

char* PqSendBuffer
static

Definition at line 122 of file pqcomm.c.

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

◆ PqSendBufferSize

int PqSendBufferSize
static

Definition at line 123 of file pqcomm.c.

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

◆ PqSendPointer

size_t PqSendPointer
static

◆ PqSendStart

size_t PqSendStart
static

◆ sock_paths

List* sock_paths = NIL
static

Definition at line 110 of file pqcomm.c.

Referenced by Lock_AF_UNIX(), RemoveSocketFiles(), and TouchSocketFiles().

◆ Unix_socket_group

char* Unix_socket_group

Definition at line 107 of file pqcomm.c.

Referenced by Setup_AF_UNIX().

◆ Unix_socket_permissions

int Unix_socket_permissions

Definition at line 106 of file pqcomm.c.

Referenced by Setup_AF_UNIX(), and show_unix_socket_permissions().