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 "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 119 of file pqcomm.c.

◆ PQ_SEND_BUFFER_SIZE

#define PQ_SEND_BUFFER_SIZE   8192

Definition at line 118 of file pqcomm.c.

Function Documentation

◆ AcceptConnection()

int AcceptConnection ( pgsocket  server_fd,
ClientSocket client_sock 
)

Definition at line 793 of file pqcomm.c.

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

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 2007 of file pqcomm.c.

2008 {
2009  /* See comments in assign_tcp_keepalives_idle */
2011 }
struct Port * MyProcPort
Definition: globals.c:49
#define newval
int pq_setkeepalivescount(int count, Port *port)
Definition: pqcomm.c:1828

References MyProcPort, newval, and pq_setkeepalivescount().

◆ assign_tcp_keepalives_idle()

void assign_tcp_keepalives_idle ( int  newval,
void *  extra 
)

Definition at line 1951 of file pqcomm.c.

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

References MyProcPort, newval, and pq_setkeepalivesidle().

◆ assign_tcp_keepalives_interval()

void assign_tcp_keepalives_interval ( int  newval,
void *  extra 
)

Definition at line 1984 of file pqcomm.c.

1985 {
1986  /* See comments in assign_tcp_keepalives_idle */
1988 }
int pq_setkeepalivesinterval(int interval, Port *port)
Definition: pqcomm.c:1749

References MyProcPort, newval, and pq_setkeepalivesinterval().

◆ assign_tcp_user_timeout()

void assign_tcp_user_timeout ( int  newval,
void *  extra 
)

Definition at line 2030 of file pqcomm.c.

2031 {
2032  /* See comments in assign_tcp_keepalives_idle */
2034 }
int pq_settcpusertimeout(int timeout, Port *port)
Definition: pqcomm.c:1903

References MyProcPort, newval, and pq_settcpusertimeout().

◆ internal_flush()

static int internal_flush ( void  )
inlinestatic

Definition at line 1346 of file pqcomm.c.

1347 {
1349 }
static size_t PqSendPointer
Definition: pqcomm.c:123
static char * PqSendBuffer
Definition: pqcomm.c:121
static pg_noinline int internal_flush_buffer(const char *buf, size_t *start, size_t *end)
Definition: pqcomm.c:1359
static size_t PqSendStart
Definition: pqcomm.c:124

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 1359 of file pqcomm.c.

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

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

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 417 of file pqcomm.c.

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

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 684 of file pqcomm.c.

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

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 1126 of file pqcomm.c.

1127 {
1129  return (PqRecvLength - PqRecvPointer);
1130 }
#define Assert(condition)
Definition: c.h:858
static int PqRecvLength
Definition: pqcomm.c:128
static int PqRecvPointer
Definition: pqcomm.c:127

References Assert, PqRecvLength, and PqRecvPointer.

Referenced by ProcessStartupPacket(), and secure_open_server().

◆ pq_check_connection()

bool pq_check_connection ( void  )

Definition at line 2053 of file pqcomm.c.

2054 {
2055  WaitEvent events[FeBeWaitSetNEvents];
2056  int rc;
2057 
2058  /*
2059  * It's OK to modify the socket event filter without restoring, because
2060  * all FeBeWaitSet socket wait sites do the same.
2061  */
2063 
2064 retry:
2065  rc = WaitEventSetWait(FeBeWaitSet, 0, events, lengthof(events), 0);
2066  for (int i = 0; i < rc; ++i)
2067  {
2068  if (events[i].events & WL_SOCKET_CLOSED)
2069  return false;
2070  if (events[i].events & WL_LATCH_SET)
2071  {
2072  /*
2073  * A latch event might be preventing other events from being
2074  * reported. Reset it and poll again. No need to restore it
2075  * because no code should expect latches to survive across
2076  * CHECK_FOR_INTERRUPTS().
2077  */
2079  goto retry;
2080  }
2081  }
2082 
2083  return true;
2084 }
#define lengthof(array)
Definition: c.h:788
struct Latch * MyLatch
Definition: globals.c:60
int i
Definition: isn.c:73
void ModifyWaitEvent(WaitEventSet *set, int pos, uint32 events, Latch *latch)
Definition: latch.c:1049
int WaitEventSetWait(WaitEventSet *set, long timeout, WaitEvent *occurred_events, int nevents, uint32 wait_event_info)
Definition: latch.c:1424
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:165

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 1096 of file pqcomm.c.

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

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

Referenced by pq_getmessage().

◆ pq_endmsgread()

void pq_endmsgread ( void  )

Definition at line 1164 of file pqcomm.c.

1165 {
1167 
1168  PqCommReadingMsg = false;
1169 }

References Assert, and PqCommReadingMsg.

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

◆ pq_getbyte()

int pq_getbyte ( void  )

Definition at line 963 of file pqcomm.c.

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

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 1003 of file pqcomm.c.

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

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

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 1798 of file pqcomm.c.

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

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

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 1714 of file pqcomm.c.

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

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 1202 of file pqcomm.c.

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

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 1873 of file pqcomm.c.

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

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 173 of file pqcomm.c.

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

1181 {
1182  return PqCommReadingMsg;
1183 }

References PqCommReadingMsg.

Referenced by PostgresMain().

◆ pq_peekbyte()

int pq_peekbyte ( void  )

Definition at line 982 of file pqcomm.c.

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

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 1558 of file pqcomm.c.

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

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

Referenced by send_message_to_frontend().

◆ pq_recvbuf()

static int pq_recvbuf ( void  )
static

Definition at line 897 of file pqcomm.c.

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

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 1828 of file pqcomm.c.

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

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 1664 of file pqcomm.c.

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

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 1749 of file pqcomm.c.

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

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 1903 of file pqcomm.c.

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

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 1140 of file pqcomm.c.

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

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 847 of file pqcomm.c.

848 {
849  ListCell *l;
850 
851  /* Loop through all created sockets... */
852  foreach(l, sock_paths)
853  {
854  char *sock_path = (char *) lfirst(l);
855 
856  /* Ignore any error. */
857  (void) unlink(sock_path);
858  }
859  /* Since we're about to exit, no need to reclaim storage */
860  sock_paths = NIL;
861 }
#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 719 of file pqcomm.c.

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

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

References MyProcPort, pq_getkeepalivescount(), and snprintf.

◆ show_tcp_keepalives_idle()

const char* show_tcp_keepalives_idle ( void  )

Definition at line 1971 of file pqcomm.c.

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

References MyProcPort, pq_getkeepalivesidle(), and snprintf.

◆ show_tcp_keepalives_interval()

const char* show_tcp_keepalives_interval ( void  )

Definition at line 1994 of file pqcomm.c.

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

References MyProcPort, pq_getkeepalivesinterval(), and snprintf.

◆ show_tcp_user_timeout()

const char* show_tcp_user_timeout ( void  )

Definition at line 2040 of file pqcomm.c.

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

References MyProcPort, pq_gettcpusertimeout(), and snprintf.

◆ socket_close()

static void socket_close ( int  code,
Datum  arg 
)
static

Definition at line 348 of file pqcomm.c.

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

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

References PqCommBusy.

◆ socket_flush()

static int socket_flush ( void  )
static

Definition at line 1324 of file pqcomm.c.

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

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

◆ socket_flush_if_writable()

static int socket_flush_if_writable ( void  )
static

Definition at line 1432 of file pqcomm.c.

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

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 1458 of file pqcomm.c.

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

References PqSendPointer, and PqSendStart.

◆ socket_putmessage()

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

Definition at line 1488 of file pqcomm.c.

1489 {
1490  uint32 n32;
1491 
1492  Assert(msgtype != 0);
1493 
1494  if (PqCommBusy)
1495  return 0;
1496  PqCommBusy = true;
1497  if (internal_putbytes(&msgtype, 1))
1498  goto fail;
1499 
1500  n32 = pg_hton32((uint32) (len + 4));
1501  if (internal_putbytes((char *) &n32, 4))
1502  goto fail;
1503 
1504  if (internal_putbytes(s, len))
1505  goto fail;
1506  PqCommBusy = false;
1507  return 0;
1508 
1509 fail:
1510  PqCommBusy = false;
1511  return EOF;
1512 }
unsigned int uint32
Definition: c.h:506
#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 1521 of file pqcomm.c.

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

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 880 of file pqcomm.c.

881 {
882  if (MyProcPort == NULL)
883  ereport(ERROR,
884  (errcode(ERRCODE_CONNECTION_DOES_NOT_EXIST),
885  errmsg("there is no client connection")));
886 
887  MyProcPort->noblock = nonblocking;
888 }
#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 829 of file pqcomm.c.

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

References lfirst, and sock_paths.

Referenced by ServerLoop().

Variable Documentation

◆ FeBeWaitSet

◆ PqCommBusy

bool PqCommBusy
static

◆ PqCommMethods

const PQcommMethods* PqCommMethods = &PqCommSocketMethods

Definition at line 163 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:1432
static int socket_flush(void)
Definition: pqcomm.c:1324
static void socket_comm_reset(void)
Definition: pqcomm.c:333
static bool socket_is_send_pending(void)
Definition: pqcomm.c:1458
static int socket_putmessage(char msgtype, const char *s, size_t len)
Definition: pqcomm.c:1488
static void socket_putmessage_noblock(char msgtype, const char *s, size_t len)
Definition: pqcomm.c:1521

Definition at line 154 of file pqcomm.c.

◆ PqRecvBuffer

char PqRecvBuffer[PQ_RECV_BUFFER_SIZE]
static

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

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

◆ PqSendBufferSize

int PqSendBufferSize
static

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

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

◆ Unix_socket_group

char* Unix_socket_group

Definition at line 106 of file pqcomm.c.

Referenced by Setup_AF_UNIX().

◆ Unix_socket_permissions

int Unix_socket_permissions

Definition at line 105 of file pqcomm.c.

Referenced by Setup_AF_UNIX(), and show_unix_socket_permissions().