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);
884 (
errcode(ERRCODE_CONNECTION_DOES_NOT_EXIST),
885 errmsg(
"there is no client connection")));
941 errmsg(
"could not receive data from client: %m")));
1042 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")));
1364 static int last_reported_send_errno = 0;
1367 const char *bufend =
buf + *end;
1369 while (bufptr < bufend)
1399 if (errno != last_reported_send_errno)
1401 last_reported_send_errno = errno;
1404 errmsg(
"could not send data to client: %m")));
1419 last_reported_send_errno = 0;
1591#if defined(WIN32) && defined(SIO_KEEPALIVE_VALS)
1595 struct tcp_keepalive ka;
1604 ka.keepalivetime = idle * 1000;
1605 ka.keepaliveinterval =
interval * 1000;
1607 if (WSAIoctl(
port->sock,
1619 (
errmsg(
"%s(%s) failed: error code %d",
1620 "WSAIoctl",
"SIO_KEEPALIVE_VALS", WSAGetLastError())));
1623 if (
port->keepalives_idle != idle)
1624 port->keepalives_idle = idle;
1634#if defined(PG_TCP_KEEPALIVE_IDLE) || defined(SIO_KEEPALIVE_VALS)
1635 if (
port == NULL ||
port->laddr.addr.ss_family == AF_UNIX)
1638 if (
port->keepalives_idle != 0)
1639 return port->keepalives_idle;
1641 if (
port->default_keepalives_idle == 0)
1646 if (getsockopt(
port->sock, IPPROTO_TCP, PG_TCP_KEEPALIVE_IDLE,
1647 (
char *) &
port->default_keepalives_idle,
1651 (
errmsg(
"%s(%s) failed: %m",
"getsockopt", PG_TCP_KEEPALIVE_IDLE_STR)));
1652 port->default_keepalives_idle = -1;
1656 port->default_keepalives_idle = -1;
1660 return port->default_keepalives_idle;
1669 if (
port == NULL ||
port->laddr.addr.ss_family == AF_UNIX)
1673#if defined(PG_TCP_KEEPALIVE_IDLE) || defined(SIO_KEEPALIVE_VALS)
1674 if (idle ==
port->keepalives_idle)
1678 if (
port->default_keepalives_idle <= 0)
1690 idle =
port->default_keepalives_idle;
1692 if (setsockopt(
port->sock, IPPROTO_TCP, PG_TCP_KEEPALIVE_IDLE,
1693 (
char *) &idle,
sizeof(idle)) < 0)
1696 (
errmsg(
"%s(%s) failed: %m",
"setsockopt", PG_TCP_KEEPALIVE_IDLE_STR)));
1700 port->keepalives_idle = idle;
1702 return pq_setkeepaliveswin32(
port, idle,
port->keepalives_interval);
1708 (
errmsg(
"setting the keepalive idle time is not supported")));
1719#if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
1720 if (
port == NULL ||
port->laddr.addr.ss_family == AF_UNIX)
1723 if (
port->keepalives_interval != 0)
1724 return port->keepalives_interval;
1726 if (
port->default_keepalives_interval == 0)
1731 if (getsockopt(
port->sock, IPPROTO_TCP, TCP_KEEPINTVL,
1732 (
char *) &
port->default_keepalives_interval,
1736 (
errmsg(
"%s(%s) failed: %m",
"getsockopt",
"TCP_KEEPINTVL")));
1737 port->default_keepalives_interval = -1;
1741 port->default_keepalives_interval = -1;
1745 return port->default_keepalives_interval;
1754 if (
port == NULL ||
port->laddr.addr.ss_family == AF_UNIX)
1757#if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
1762 if (
port->default_keepalives_interval <= 0)
1776 if (setsockopt(
port->sock, IPPROTO_TCP, TCP_KEEPINTVL,
1780 (
errmsg(
"%s(%s) failed: %m",
"setsockopt",
"TCP_KEEPINTVL")));
1792 (
errmsg(
"%s(%s) not supported",
"setsockopt",
"TCP_KEEPINTVL")));
1804 if (
port == NULL ||
port->laddr.addr.ss_family == AF_UNIX)
1807 if (
port->keepalives_count != 0)
1808 return port->keepalives_count;
1810 if (
port->default_keepalives_count == 0)
1814 if (getsockopt(
port->sock, IPPROTO_TCP, TCP_KEEPCNT,
1815 (
char *) &
port->default_keepalives_count,
1819 (
errmsg(
"%s(%s) failed: %m",
"getsockopt",
"TCP_KEEPCNT")));
1820 port->default_keepalives_count = -1;
1824 return port->default_keepalives_count;
1833 if (
port == NULL ||
port->laddr.addr.ss_family == AF_UNIX)
1837 if (count ==
port->keepalives_count)
1840 if (
port->default_keepalives_count <= 0)
1852 count =
port->default_keepalives_count;
1854 if (setsockopt(
port->sock, IPPROTO_TCP, TCP_KEEPCNT,
1855 (
char *) &count,
sizeof(count)) < 0)
1858 (
errmsg(
"%s(%s) failed: %m",
"setsockopt",
"TCP_KEEPCNT")));
1862 port->keepalives_count = count;
1867 (
errmsg(
"%s(%s) not supported",
"setsockopt",
"TCP_KEEPCNT")));
1878#ifdef TCP_USER_TIMEOUT
1879 if (
port == NULL ||
port->laddr.addr.ss_family == AF_UNIX)
1882 if (
port->tcp_user_timeout != 0)
1883 return port->tcp_user_timeout;
1885 if (
port->default_tcp_user_timeout == 0)
1889 if (getsockopt(
port->sock, IPPROTO_TCP, TCP_USER_TIMEOUT,
1890 (
char *) &
port->default_tcp_user_timeout,
1894 (
errmsg(
"%s(%s) failed: %m",
"getsockopt",
"TCP_USER_TIMEOUT")));
1895 port->default_tcp_user_timeout = -1;
1899 return port->default_tcp_user_timeout;
1908 if (
port == NULL ||
port->laddr.addr.ss_family == AF_UNIX)
1911#ifdef TCP_USER_TIMEOUT
1912 if (timeout ==
port->tcp_user_timeout)
1915 if (
port->default_tcp_user_timeout <= 0)
1927 timeout =
port->default_tcp_user_timeout;
1929 if (setsockopt(
port->sock, IPPROTO_TCP, TCP_USER_TIMEOUT,
1930 (
char *) &timeout,
sizeof(timeout)) < 0)
1933 (
errmsg(
"%s(%s) failed: %m",
"setsockopt",
"TCP_USER_TIMEOUT")));
1937 port->tcp_user_timeout = timeout;
1942 (
errmsg(
"%s(%s) not supported",
"setsockopt",
"TCP_USER_TIMEOUT")));
1977 static char nbuf[16];
2000 static char nbuf[16];
2023 static char nbuf[16];
2046 static char nbuf[16];
2069 for (
int i = 0;
i < rc; ++
i)
ssize_t secure_write(Port *port, const 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 MemSet(start, val, len)
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
Assert(PointerIsAligned(start, uint64))
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 ResetLatch(Latch *latch)
#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
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)
static int internal_putbytes(const void *b, size_t len)
int pq_getbytes(void *b, size_t len)
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
#define UNIXSOCK_PATH(path, port, sockdir)
#define UNIXSOCK_PATH_BUFLEN
static int fd(const char *x, int i)
void pg_usleep(long microsec)
const char * gai_strerror(int ecode)
void resetStringInfo(StringInfo str)
void enlargeStringInfo(StringInfo str, int needed)
struct sockaddr_storage addr
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)
#define WL_POSTMASTER_DEATH
#define WL_SOCKET_WRITEABLE
#define bind(s, addr, addrlen)
#define socket(af, type, protocol)
#define accept(s, addr, addrlen)
#define listen(s, backlog)