88#if defined(TCP_KEEPIDLE)
90#define PG_TCP_KEEPALIVE_IDLE TCP_KEEPIDLE
91#define PG_TCP_KEEPALIVE_IDLE_STR "TCP_KEEPIDLE"
92#elif defined(TCP_KEEPALIVE_THRESHOLD)
94#define PG_TCP_KEEPALIVE_IDLE TCP_KEEPALIVE_THRESHOLD
95#define PG_TCP_KEEPALIVE_IDLE_STR "TCP_KEEPALIVE_THRESHOLD"
96#elif defined(TCP_KEEPALIVE) && defined(__darwin__)
99#define PG_TCP_KEEPALIVE_IDLE TCP_KEEPALIVE
100#define PG_TCP_KEEPALIVE_IDLE_STR "TCP_KEEPALIVE"
119#define PQ_SEND_BUFFER_SIZE 8192
120#define PQ_RECV_BUFFER_SIZE 8192
152static int Lock_AF_UNIX(
const char *unixSocketDir,
const char *unixSocketPath);
187 port->laddr.salen =
sizeof(
port->laddr.addr);
188 if (getsockname(
port->sock,
189 (
struct sockaddr *) &
port->laddr.addr,
190 &
port->laddr.salen) < 0)
193 (
errmsg(
"%s() failed: %m",
"getsockname")));
197 if (
port->laddr.addr.ss_family != AF_UNIX)
208 if (setsockopt(
port->sock, IPPROTO_TCP, TCP_NODELAY,
209 (
char *) &on,
sizeof(on)) < 0)
212 (
errmsg(
"%s(%s) failed: %m",
"setsockopt",
"TCP_NODELAY")));
216 if (setsockopt(
port->sock, SOL_SOCKET, SO_KEEPALIVE,
217 (
char *) &on,
sizeof(on)) < 0)
220 (
errmsg(
"%s(%s) failed: %m",
"setsockopt",
"SO_KEEPALIVE")));
246 optlen =
sizeof(oldopt);
247 if (getsockopt(
port->sock, SOL_SOCKET, SO_SNDBUF, (
char *) &oldopt,
251 (
errmsg(
"%s(%s) failed: %m",
"getsockopt",
"SO_SNDBUF")));
256 if (setsockopt(
port->sock, SOL_SOCKET, SO_SNDBUF, (
char *) &newopt,
260 (
errmsg(
"%s(%s) failed: %m",
"setsockopt",
"SO_SNDBUF")));
297 (
errmsg(
"could not set socket to nonblocking mode: %m")));
303 if (fcntl(
port->sock, F_SETFD, FD_CLOEXEC) < 0)
304 elog(
FATAL,
"fcntl(F_SETFD) failed on socket: %m");
309 port->sock, NULL, NULL);
419 const char *unixSocketDir,
426 char portNumberStr[32];
427 const char *familyDesc;
428 char familyDescBuf[64];
429 const char *addrDesc;
430 char addrBuf[NI_MAXHOST];
432 struct addrinfo *addrs = NULL,
434 struct addrinfo hint;
437#if !defined(WIN32) || defined(IPV6_V6ONLY)
442 MemSet(&hint, 0,
sizeof(hint));
443 hint.ai_family = family;
444 hint.ai_flags = AI_PASSIVE;
445 hint.ai_socktype = SOCK_STREAM;
447 if (family == AF_UNIX)
457 (
errmsg(
"Unix-domain socket path \"%s\" is too long (maximum %d bytes)",
464 service = unixSocketPath;
468 snprintf(portNumberStr,
sizeof(portNumberStr),
"%d", portNumber);
469 service = portNumberStr;
477 (
errmsg(
"could not translate host name \"%s\", service \"%s\" to address: %s",
481 (
errmsg(
"could not translate service \"%s\" to address: %s",
488 for (addr = addrs; addr; addr = addr->ai_next)
490 if (family != AF_UNIX && addr->ai_family == AF_UNIX)
503 (
errmsg(
"could not bind to all requested addresses: MAXLISTEN (%d) exceeded",
509 switch (addr->ai_family)
512 familyDesc =
_(
"IPv4");
515 familyDesc =
_(
"IPv6");
518 familyDesc =
_(
"Unix");
521 snprintf(familyDescBuf,
sizeof(familyDescBuf),
522 _(
"unrecognized address family %d"),
524 familyDesc = familyDescBuf;
529 if (addr->ai_family == AF_UNIX)
530 addrDesc = unixSocketPath;
535 addrBuf,
sizeof(addrBuf),
546 errmsg(
"could not create %s socket for address \"%s\": %m",
547 familyDesc, addrDesc)));
553 if (fcntl(
fd, F_SETFD, FD_CLOEXEC) < 0)
554 elog(
FATAL,
"fcntl(F_SETFD) failed on socket: %m");
567 if (addr->ai_family != AF_UNIX)
569 if ((setsockopt(
fd, SOL_SOCKET, SO_REUSEADDR,
570 (
char *) &one,
sizeof(one))) == -1)
575 errmsg(
"%s(%s) failed for %s address \"%s\": %m",
576 "setsockopt",
"SO_REUSEADDR",
577 familyDesc, addrDesc)));
585 if (addr->ai_family == AF_INET6)
587 if (setsockopt(
fd, IPPROTO_IPV6, IPV6_V6ONLY,
588 (
char *) &one,
sizeof(one)) == -1)
593 errmsg(
"%s(%s) failed for %s address \"%s\": %m",
594 "setsockopt",
"IPV6_V6ONLY",
595 familyDesc, addrDesc)));
608 err =
bind(
fd, addr->ai_addr, addr->ai_addrlen);
611 int saved_errno = errno;
616 errmsg(
"could not bind %s address \"%s\": %m",
617 familyDesc, addrDesc),
619 (addr->ai_family == AF_UNIX ?
620 errhint(
"Is another postmaster already running on port %d?",
622 errhint(
"Is another postmaster already running on port %d?"
623 " If not, wait a few seconds and retry.",
624 (
int) portNumber)) : 0));
629 if (addr->ai_family == AF_UNIX)
651 errmsg(
"could not listen on %s address \"%s\": %m",
652 familyDesc, addrDesc)));
657 if (addr->ai_family == AF_UNIX)
659 (
errmsg(
"listening on Unix socket \"%s\"",
664 (
errmsg(
"listening on %s address \"%s\", port %d",
665 familyDesc, addrDesc, (
int) portNumber)));
668 (*NumListenSockets)++;
688 if (unixSocketPath[0] ==
'@')
705 (void) unlink(unixSocketPath);
723 if (sock_path[0] ==
'@')
735 elog(
WARNING,
"configuration item \"unix_socket_group\" is not supported on this platform");
754 (
errmsg(
"group \"%s\" does not exist",
760 if (chown(sock_path, -1, gid) == -1)
764 errmsg(
"could not set group of file \"%s\": %m",
775 errmsg(
"could not set permissions of file \"%s\": %m",
799 (
struct sockaddr *) &client_sock->
raddr.
addr,
804 errmsg(
"could not accept new connection: %m")));
837 char *sock_path = (
char *)
lfirst(l);
840 (void) utime(sock_path, NULL);
855 char *sock_path = (
char *)
lfirst(l);
858 (void) unlink(sock_path);
885 (
errcode(ERRCODE_CONNECTION_DOES_NOT_EXIST),
886 errmsg(
"there is no client connection")));
942 errmsg(
"could not receive data from client: %m")));
1043 errmsg(
"could not receive data from client: %m")));
1149 (
errcode(ERRCODE_PROTOCOL_VIOLATION),
1150 errmsg(
"terminating connection because protocol synchronization was lost")));
1215 (
errcode(ERRCODE_PROTOCOL_VIOLATION),
1216 errmsg(
"unexpected EOF within message length word")));
1222 if (len < 4 || len > maxlen)
1225 (
errcode(ERRCODE_PROTOCOL_VIOLATION),
1226 errmsg(
"invalid message length")));
1247 (
errcode(ERRCODE_PROTOCOL_VIOLATION),
1248 errmsg(
"incomplete message from client")));
1260 (
errcode(ERRCODE_PROTOCOL_VIOLATION),
1261 errmsg(
"incomplete message from client")));
1362 static int last_reported_send_errno = 0;
1365 const char *bufend =
buf + *end;
1367 while (bufptr < bufend)
1397 if (errno != last_reported_send_errno)
1399 last_reported_send_errno = errno;
1402 errmsg(
"could not send data to client: %m")));
1417 last_reported_send_errno = 0;
1589#if defined(WIN32) && defined(SIO_KEEPALIVE_VALS)
1593 struct tcp_keepalive ka;
1602 ka.keepalivetime = idle * 1000;
1603 ka.keepaliveinterval =
interval * 1000;
1605 if (WSAIoctl(
port->sock,
1617 (
errmsg(
"%s(%s) failed: error code %d",
1618 "WSAIoctl",
"SIO_KEEPALIVE_VALS", WSAGetLastError())));
1621 if (
port->keepalives_idle != idle)
1622 port->keepalives_idle = idle;
1632#if defined(PG_TCP_KEEPALIVE_IDLE) || defined(SIO_KEEPALIVE_VALS)
1633 if (
port == NULL ||
port->laddr.addr.ss_family == AF_UNIX)
1636 if (
port->keepalives_idle != 0)
1637 return port->keepalives_idle;
1639 if (
port->default_keepalives_idle == 0)
1644 if (getsockopt(
port->sock, IPPROTO_TCP, PG_TCP_KEEPALIVE_IDLE,
1645 (
char *) &
port->default_keepalives_idle,
1649 (
errmsg(
"%s(%s) failed: %m",
"getsockopt", PG_TCP_KEEPALIVE_IDLE_STR)));
1650 port->default_keepalives_idle = -1;
1654 port->default_keepalives_idle = -1;
1658 return port->default_keepalives_idle;
1667 if (
port == NULL ||
port->laddr.addr.ss_family == AF_UNIX)
1671#if defined(PG_TCP_KEEPALIVE_IDLE) || defined(SIO_KEEPALIVE_VALS)
1672 if (idle ==
port->keepalives_idle)
1676 if (
port->default_keepalives_idle <= 0)
1688 idle =
port->default_keepalives_idle;
1690 if (setsockopt(
port->sock, IPPROTO_TCP, PG_TCP_KEEPALIVE_IDLE,
1691 (
char *) &idle,
sizeof(idle)) < 0)
1694 (
errmsg(
"%s(%s) failed: %m",
"setsockopt", PG_TCP_KEEPALIVE_IDLE_STR)));
1698 port->keepalives_idle = idle;
1700 return pq_setkeepaliveswin32(
port, idle,
port->keepalives_interval);
1706 (
errmsg(
"setting the keepalive idle time is not supported")));
1717#if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
1718 if (
port == NULL ||
port->laddr.addr.ss_family == AF_UNIX)
1721 if (
port->keepalives_interval != 0)
1722 return port->keepalives_interval;
1724 if (
port->default_keepalives_interval == 0)
1729 if (getsockopt(
port->sock, IPPROTO_TCP, TCP_KEEPINTVL,
1730 (
char *) &
port->default_keepalives_interval,
1734 (
errmsg(
"%s(%s) failed: %m",
"getsockopt",
"TCP_KEEPINTVL")));
1735 port->default_keepalives_interval = -1;
1739 port->default_keepalives_interval = -1;
1743 return port->default_keepalives_interval;
1752 if (
port == NULL ||
port->laddr.addr.ss_family == AF_UNIX)
1755#if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
1760 if (
port->default_keepalives_interval <= 0)
1774 if (setsockopt(
port->sock, IPPROTO_TCP, TCP_KEEPINTVL,
1778 (
errmsg(
"%s(%s) failed: %m",
"setsockopt",
"TCP_KEEPINTVL")));
1790 (
errmsg(
"%s(%s) not supported",
"setsockopt",
"TCP_KEEPINTVL")));
1802 if (
port == NULL ||
port->laddr.addr.ss_family == AF_UNIX)
1805 if (
port->keepalives_count != 0)
1806 return port->keepalives_count;
1808 if (
port->default_keepalives_count == 0)
1812 if (getsockopt(
port->sock, IPPROTO_TCP, TCP_KEEPCNT,
1813 (
char *) &
port->default_keepalives_count,
1817 (
errmsg(
"%s(%s) failed: %m",
"getsockopt",
"TCP_KEEPCNT")));
1818 port->default_keepalives_count = -1;
1822 return port->default_keepalives_count;
1831 if (
port == NULL ||
port->laddr.addr.ss_family == AF_UNIX)
1835 if (count ==
port->keepalives_count)
1838 if (
port->default_keepalives_count <= 0)
1850 count =
port->default_keepalives_count;
1852 if (setsockopt(
port->sock, IPPROTO_TCP, TCP_KEEPCNT,
1853 (
char *) &count,
sizeof(count)) < 0)
1856 (
errmsg(
"%s(%s) failed: %m",
"setsockopt",
"TCP_KEEPCNT")));
1860 port->keepalives_count = count;
1865 (
errmsg(
"%s(%s) not supported",
"setsockopt",
"TCP_KEEPCNT")));
1876#ifdef TCP_USER_TIMEOUT
1877 if (
port == NULL ||
port->laddr.addr.ss_family == AF_UNIX)
1880 if (
port->tcp_user_timeout != 0)
1881 return port->tcp_user_timeout;
1883 if (
port->default_tcp_user_timeout == 0)
1887 if (getsockopt(
port->sock, IPPROTO_TCP, TCP_USER_TIMEOUT,
1888 (
char *) &
port->default_tcp_user_timeout,
1892 (
errmsg(
"%s(%s) failed: %m",
"getsockopt",
"TCP_USER_TIMEOUT")));
1893 port->default_tcp_user_timeout = -1;
1897 return port->default_tcp_user_timeout;
1906 if (
port == NULL ||
port->laddr.addr.ss_family == AF_UNIX)
1909#ifdef TCP_USER_TIMEOUT
1910 if (timeout ==
port->tcp_user_timeout)
1913 if (
port->default_tcp_user_timeout <= 0)
1925 timeout =
port->default_tcp_user_timeout;
1927 if (setsockopt(
port->sock, IPPROTO_TCP, TCP_USER_TIMEOUT,
1928 (
char *) &timeout,
sizeof(timeout)) < 0)
1931 (
errmsg(
"%s(%s) failed: %m",
"setsockopt",
"TCP_USER_TIMEOUT")));
1935 port->tcp_user_timeout = timeout;
1940 (
errmsg(
"%s(%s) not supported",
"setsockopt",
"TCP_USER_TIMEOUT")));
1975 static char nbuf[16];
1998 static char nbuf[16];
2021 static char nbuf[16];
2044 static char nbuf[16];
2067 for (
int i = 0;
i < rc; ++
i)
ssize_t secure_write(Port *port, void *ptr, size_t len)
void secure_close(Port *port)
ssize_t secure_read(Port *port, void *ptr, size_t len)
#define PG_USED_FOR_ASSERTS_ONLY
#define Assert(condition)
#define MemSet(start, val, len)
static void PGresult * res
int errcode_for_socket_access(void)
int errcode_for_file_access(void)
int errhint(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
void err(int eval, const char *fmt,...)
volatile sig_atomic_t InterruptPending
volatile sig_atomic_t ClientConnectionLost
int tcp_keepalives_interval
void pg_freeaddrinfo_all(int hint_ai_family, struct addrinfo *ai)
int pg_getnameinfo_all(const struct sockaddr_storage *addr, int salen, char *node, int nodelen, char *service, int servicelen, int flags)
int pg_getaddrinfo_all(const char *hostname, const char *servname, const struct addrinfo *hintp, struct addrinfo **result)
void on_proc_exit(pg_on_exit_callback function, Datum arg)
void ModifyWaitEvent(WaitEventSet *set, int pos, uint32 events, Latch *latch)
int AddWaitEventToSet(WaitEventSet *set, uint32 events, pgsocket fd, Latch *latch, void *user_data)
int WaitEventSetWait(WaitEventSet *set, long timeout, WaitEvent *occurred_events, int nevents, uint32 wait_event_info)
WaitEventSet * CreateWaitEventSet(ResourceOwner resowner, int nevents)
void ResetLatch(Latch *latch)
#define WL_POSTMASTER_DEATH
#define WL_SOCKET_WRITEABLE
#define pq_putmessage(msgtype, s, len)
#define FeBeWaitSetLatchPos
#define FeBeWaitSetNEvents
#define FeBeWaitSetSocketPos
List * lappend(List *list, void *datum)
void * MemoryContextAlloc(MemoryContext context, Size size)
char * pstrdup(const char *in)
void * repalloc(void *pointer, Size size)
void * palloc0(Size size)
MemoryContext TopMemoryContext
void CreateSocketLockFile(const char *socketfile, bool amPostmaster, const char *socketDir)
bool pg_set_noblock(pgsocket sock)
static pgsocket * ListenSockets
static int NumListenSockets
int pq_setkeepalivesinterval(int interval, Port *port)
Port * pq_init(ClientSocket *client_sock)
void assign_tcp_keepalives_count(int newval, void *extra)
const PQcommMethods * PqCommMethods
static int pq_recvbuf(void)
const char * show_tcp_keepalives_interval(void)
int Unix_socket_permissions
static int internal_flush(void)
static void socket_set_nonblocking(bool nonblocking)
static size_t PqSendPointer
const char * show_tcp_keepalives_count(void)
int pq_getbyte_if_available(unsigned char *c)
static int socket_flush_if_writable(void)
int pq_getkeepalivescount(Port *port)
#define PQ_RECV_BUFFER_SIZE
int pq_getkeepalivesinterval(Port *port)
static int pq_discardbytes(size_t len)
int pq_settcpusertimeout(int timeout, Port *port)
int pq_getmessage(StringInfo s, int maxlen)
static const PQcommMethods PqCommSocketMethods
int pq_getbytes(char *s, size_t len)
static bool PqCommReadingMsg
static int socket_flush(void)
ssize_t pq_buffer_remaining_data(void)
#define PQ_SEND_BUFFER_SIZE
const char * show_tcp_keepalives_idle(void)
int pq_setkeepalivesidle(int idle, Port *port)
int ListenServerPort(int family, const char *hostName, unsigned short portNumber, const char *unixSocketDir, pgsocket ListenSockets[], int *NumListenSockets, int MaxListen)
static void socket_comm_reset(void)
WaitEventSet * FeBeWaitSet
static char * PqSendBuffer
static int Lock_AF_UNIX(const char *unixSocketDir, const char *unixSocketPath)
int pq_getkeepalivesidle(Port *port)
int AcceptConnection(pgsocket server_fd, ClientSocket *client_sock)
void TouchSocketFiles(void)
static bool socket_is_send_pending(void)
void assign_tcp_keepalives_idle(int newval, void *extra)
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 char PqRecvBuffer[PQ_RECV_BUFFER_SIZE]
const char * show_tcp_user_timeout(void)
static void socket_close(int code, Datum arg)
void assign_tcp_user_timeout(int newval, void *extra)
int pq_putmessage_v2(char msgtype, const char *s, size_t len)
static int Setup_AF_UNIX(const char *sock_path)
bool pq_is_reading_msg(void)
void RemoveSocketFiles(void)
int pq_gettcpusertimeout(Port *port)
bool pq_check_connection(void)
static int PqSendBufferSize
void assign_tcp_keepalives_interval(int newval, void *extra)
void pq_startmsgread(void)
int pq_setkeepalivescount(int count, Port *port)
static pg_noinline int internal_flush_buffer(const char *buf, size_t *start, size_t *end)
static size_t PqSendStart
static int internal_putbytes(const char *s, size_t len)
#define UNIXSOCK_PATH(path, port, sockdir)
#define UNIXSOCK_PATH_BUFLEN
static int fd(const char *x, int i)
void pg_usleep(long microsec)
static pg_noinline void Size size
const char * gai_strerror(int ecode)
void resetStringInfo(StringInfo str)
void enlargeStringInfo(StringInfo str, int needed)
struct sockaddr_storage addr
#define bind(s, addr, addrlen)
#define socket(af, type, protocol)
#define accept(s, addr, addrlen)
#define listen(s, backlog)