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
150 static int Lock_AF_UNIX(
const char *unixSocketDir,
const char *unixSocketPath);
200 (
errmsg(
"could not set socket to nonblocking mode: %m")));
207 elog(
FATAL,
"fcntl(F_SETFD) failed on socket: %m");
323 const char *unixSocketDir,
330 char portNumberStr[32];
331 const char *familyDesc;
332 char familyDescBuf[64];
333 const char *addrDesc;
334 char addrBuf[NI_MAXHOST];
336 struct addrinfo *addrs = NULL,
338 struct addrinfo hint;
341 #if !defined(WIN32) || defined(IPV6_V6ONLY)
346 MemSet(&hint, 0,
sizeof(hint));
347 hint.ai_family = family;
348 hint.ai_flags = AI_PASSIVE;
349 hint.ai_socktype = SOCK_STREAM;
351 if (family == AF_UNIX)
361 (
errmsg(
"Unix-domain socket path \"%s\" is too long (maximum %d bytes)",
368 service = unixSocketPath;
372 snprintf(portNumberStr,
sizeof(portNumberStr),
"%d", portNumber);
373 service = portNumberStr;
381 (
errmsg(
"could not translate host name \"%s\", service \"%s\" to address: %s",
382 hostName, service, gai_strerror(ret))));
385 (
errmsg(
"could not translate service \"%s\" to address: %s",
386 service, gai_strerror(ret))));
392 for (addr = addrs; addr; addr = addr->ai_next)
394 if (family != AF_UNIX && addr->ai_family == AF_UNIX)
407 (
errmsg(
"could not bind to all requested addresses: MAXLISTEN (%d) exceeded",
413 switch (addr->ai_family)
416 familyDesc =
_(
"IPv4");
419 familyDesc =
_(
"IPv6");
422 familyDesc =
_(
"Unix");
425 snprintf(familyDescBuf,
sizeof(familyDescBuf),
426 _(
"unrecognized address family %d"),
428 familyDesc = familyDescBuf;
433 if (addr->ai_family == AF_UNIX)
434 addrDesc = unixSocketPath;
439 addrBuf,
sizeof(addrBuf),
450 errmsg(
"could not create %s socket for address \"%s\": %m",
451 familyDesc, addrDesc)));
457 if (fcntl(
fd, F_SETFD, FD_CLOEXEC) < 0)
458 elog(
FATAL,
"fcntl(F_SETFD) failed on socket: %m");
471 if (addr->ai_family != AF_UNIX)
473 if ((setsockopt(
fd, SOL_SOCKET, SO_REUSEADDR,
474 (
char *) &one,
sizeof(one))) == -1)
479 errmsg(
"%s(%s) failed for %s address \"%s\": %m",
480 "setsockopt",
"SO_REUSEADDR",
481 familyDesc, addrDesc)));
489 if (addr->ai_family == AF_INET6)
491 if (setsockopt(
fd, IPPROTO_IPV6, IPV6_V6ONLY,
492 (
char *) &one,
sizeof(one)) == -1)
497 errmsg(
"%s(%s) failed for %s address \"%s\": %m",
498 "setsockopt",
"IPV6_V6ONLY",
499 familyDesc, addrDesc)));
512 err =
bind(
fd, addr->ai_addr, addr->ai_addrlen);
515 int saved_errno = errno;
520 errmsg(
"could not bind %s address \"%s\": %m",
521 familyDesc, addrDesc),
523 (addr->ai_family == AF_UNIX ?
524 errhint(
"Is another postmaster already running on port %d?",
526 errhint(
"Is another postmaster already running on port %d?"
527 " If not, wait a few seconds and retry.",
528 (
int) portNumber)) : 0));
533 if (addr->ai_family == AF_UNIX)
555 errmsg(
"could not listen on %s address \"%s\": %m",
556 familyDesc, addrDesc)));
561 if (addr->ai_family == AF_UNIX)
563 (
errmsg(
"listening on Unix socket \"%s\"",
568 (
errmsg(
"listening on %s address \"%s\", port %d",
569 familyDesc, addrDesc, (
int) portNumber)));
572 (*NumListenSockets)++;
592 if (unixSocketPath[0] ==
'@')
609 (void) unlink(unixSocketPath);
627 if (sock_path[0] ==
'@')
639 elog(
WARNING,
"configuration item unix_socket_group is not supported on this platform");
658 (
errmsg(
"group \"%s\" does not exist",
664 if (chown(sock_path, -1, gid) == -1)
668 errmsg(
"could not set group of file \"%s\": %m",
679 errmsg(
"could not set permissions of file \"%s\": %m",
700 port->raddr.salen =
sizeof(
port->raddr.addr);
702 (
struct sockaddr *) &
port->raddr.addr,
707 errmsg(
"could not accept new connection: %m")));
721 port->laddr.salen =
sizeof(
port->laddr.addr);
722 if (getsockname(
port->sock,
723 (
struct sockaddr *) &
port->laddr.addr,
724 &
port->laddr.salen) < 0)
727 (
errmsg(
"%s() failed: %m",
"getsockname")));
732 if (
port->laddr.addr.ss_family != AF_UNIX)
743 if (setsockopt(
port->sock, IPPROTO_TCP, TCP_NODELAY,
744 (
char *) &on,
sizeof(on)) < 0)
747 (
errmsg(
"%s(%s) failed: %m",
"setsockopt",
"TCP_NODELAY")));
752 if (setsockopt(
port->sock, SOL_SOCKET, SO_KEEPALIVE,
753 (
char *) &on,
sizeof(on)) < 0)
756 (
errmsg(
"%s(%s) failed: %m",
"setsockopt",
"SO_KEEPALIVE")));
783 optlen =
sizeof(oldopt);
784 if (getsockopt(
port->sock, SOL_SOCKET, SO_SNDBUF, (
char *) &oldopt,
788 (
errmsg(
"%s(%s) failed: %m",
"getsockopt",
"SO_SNDBUF")));
794 if (setsockopt(
port->sock, SOL_SOCKET, SO_SNDBUF, (
char *) &newopt,
798 (
errmsg(
"%s(%s) failed: %m",
"setsockopt",
"SO_SNDBUF")));
834 elog(
LOG,
"could not close client or listen socket: %m");
854 char *sock_path = (
char *)
lfirst(l);
857 (void) utime(sock_path, NULL);
872 char *sock_path = (
char *)
lfirst(l);
875 (void) unlink(sock_path);
902 (
errcode(ERRCODE_CONNECTION_DOES_NOT_EXIST),
903 errmsg(
"there is no client connection")));
954 errmsg(
"could not receive data from client: %m")));
1050 errmsg(
"could not receive data from client: %m")));
1154 (
errcode(ERRCODE_PROTOCOL_VIOLATION),
1155 errmsg(
"terminating connection because protocol synchronization was lost")));
1220 (
errcode(ERRCODE_PROTOCOL_VIOLATION),
1221 errmsg(
"unexpected EOF within message length word")));
1227 if (len < 4 || len > maxlen)
1230 (
errcode(ERRCODE_PROTOCOL_VIOLATION),
1231 errmsg(
"invalid message length")));
1252 (
errcode(ERRCODE_PROTOCOL_VIOLATION),
1253 errmsg(
"incomplete message from client")));
1265 (
errcode(ERRCODE_PROTOCOL_VIOLATION),
1266 errmsg(
"incomplete message from client")));
1337 static int last_reported_send_errno = 0;
1342 while (bufptr < bufend)
1372 if (errno != last_reported_send_errno)
1374 last_reported_send_errno = errno;
1377 errmsg(
"could not send data to client: %m")));
1392 last_reported_send_errno = 0;
1564 #if defined(WIN32) && defined(SIO_KEEPALIVE_VALS)
1568 struct tcp_keepalive ka;
1577 ka.keepalivetime = idle * 1000;
1578 ka.keepaliveinterval =
interval * 1000;
1580 if (WSAIoctl(
port->sock,
1592 (
errmsg(
"%s(%s) failed: error code %d",
1593 "WSAIoctl",
"SIO_KEEPALIVE_VALS", WSAGetLastError())));
1596 if (
port->keepalives_idle != idle)
1597 port->keepalives_idle = idle;
1607 #if defined(PG_TCP_KEEPALIVE_IDLE) || defined(SIO_KEEPALIVE_VALS)
1608 if (
port == NULL ||
port->laddr.addr.ss_family == AF_UNIX)
1611 if (
port->keepalives_idle != 0)
1612 return port->keepalives_idle;
1614 if (
port->default_keepalives_idle == 0)
1619 if (getsockopt(
port->sock, IPPROTO_TCP, PG_TCP_KEEPALIVE_IDLE,
1620 (
char *) &
port->default_keepalives_idle,
1624 (
errmsg(
"%s(%s) failed: %m",
"getsockopt", PG_TCP_KEEPALIVE_IDLE_STR)));
1625 port->default_keepalives_idle = -1;
1629 port->default_keepalives_idle = -1;
1633 return port->default_keepalives_idle;
1642 if (
port == NULL ||
port->laddr.addr.ss_family == AF_UNIX)
1646 #if defined(PG_TCP_KEEPALIVE_IDLE) || defined(SIO_KEEPALIVE_VALS)
1647 if (idle ==
port->keepalives_idle)
1651 if (
port->default_keepalives_idle <= 0)
1663 idle =
port->default_keepalives_idle;
1665 if (setsockopt(
port->sock, IPPROTO_TCP, PG_TCP_KEEPALIVE_IDLE,
1666 (
char *) &idle,
sizeof(idle)) < 0)
1669 (
errmsg(
"%s(%s) failed: %m",
"setsockopt", PG_TCP_KEEPALIVE_IDLE_STR)));
1673 port->keepalives_idle = idle;
1675 return pq_setkeepaliveswin32(
port, idle,
port->keepalives_interval);
1681 (
errmsg(
"setting the keepalive idle time is not supported")));
1692 #if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
1693 if (
port == NULL ||
port->laddr.addr.ss_family == AF_UNIX)
1696 if (
port->keepalives_interval != 0)
1697 return port->keepalives_interval;
1699 if (
port->default_keepalives_interval == 0)
1704 if (getsockopt(
port->sock, IPPROTO_TCP, TCP_KEEPINTVL,
1705 (
char *) &
port->default_keepalives_interval,
1709 (
errmsg(
"%s(%s) failed: %m",
"getsockopt",
"TCP_KEEPINTVL")));
1710 port->default_keepalives_interval = -1;
1714 port->default_keepalives_interval = -1;
1718 return port->default_keepalives_interval;
1727 if (
port == NULL ||
port->laddr.addr.ss_family == AF_UNIX)
1730 #if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
1735 if (
port->default_keepalives_interval <= 0)
1749 if (setsockopt(
port->sock, IPPROTO_TCP, TCP_KEEPINTVL,
1753 (
errmsg(
"%s(%s) failed: %m",
"setsockopt",
"TCP_KEEPINTVL")));
1765 (
errmsg(
"%s(%s) not supported",
"setsockopt",
"TCP_KEEPINTVL")));
1777 if (
port == NULL ||
port->laddr.addr.ss_family == AF_UNIX)
1780 if (
port->keepalives_count != 0)
1781 return port->keepalives_count;
1783 if (
port->default_keepalives_count == 0)
1787 if (getsockopt(
port->sock, IPPROTO_TCP, TCP_KEEPCNT,
1788 (
char *) &
port->default_keepalives_count,
1792 (
errmsg(
"%s(%s) failed: %m",
"getsockopt",
"TCP_KEEPCNT")));
1793 port->default_keepalives_count = -1;
1797 return port->default_keepalives_count;
1806 if (
port == NULL ||
port->laddr.addr.ss_family == AF_UNIX)
1810 if (count ==
port->keepalives_count)
1813 if (
port->default_keepalives_count <= 0)
1825 count =
port->default_keepalives_count;
1827 if (setsockopt(
port->sock, IPPROTO_TCP, TCP_KEEPCNT,
1828 (
char *) &count,
sizeof(count)) < 0)
1831 (
errmsg(
"%s(%s) failed: %m",
"setsockopt",
"TCP_KEEPCNT")));
1835 port->keepalives_count = count;
1840 (
errmsg(
"%s(%s) not supported",
"setsockopt",
"TCP_KEEPCNT")));
1851 #ifdef TCP_USER_TIMEOUT
1852 if (
port == NULL ||
port->laddr.addr.ss_family == AF_UNIX)
1855 if (
port->tcp_user_timeout != 0)
1856 return port->tcp_user_timeout;
1858 if (
port->default_tcp_user_timeout == 0)
1862 if (getsockopt(
port->sock, IPPROTO_TCP, TCP_USER_TIMEOUT,
1863 (
char *) &
port->default_tcp_user_timeout,
1867 (
errmsg(
"%s(%s) failed: %m",
"getsockopt",
"TCP_USER_TIMEOUT")));
1868 port->default_tcp_user_timeout = -1;
1872 return port->default_tcp_user_timeout;
1881 if (
port == NULL ||
port->laddr.addr.ss_family == AF_UNIX)
1884 #ifdef TCP_USER_TIMEOUT
1885 if (timeout ==
port->tcp_user_timeout)
1888 if (
port->default_tcp_user_timeout <= 0)
1900 timeout =
port->default_tcp_user_timeout;
1902 if (setsockopt(
port->sock, IPPROTO_TCP, TCP_USER_TIMEOUT,
1903 (
char *) &timeout,
sizeof(timeout)) < 0)
1906 (
errmsg(
"%s(%s) failed: %m",
"setsockopt",
"TCP_USER_TIMEOUT")));
1910 port->tcp_user_timeout = timeout;
1915 (
errmsg(
"%s(%s) not supported",
"setsockopt",
"TCP_USER_TIMEOUT")));
1950 static char nbuf[16];
1973 static char nbuf[16];
1996 static char nbuf[16];
2019 static char nbuf[16];
2042 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 MemSet(start, val, len)
static void PGresult * res
elog(ERROR, "%s: %s", p2, msg)
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)
WaitEventSet * CreateWaitEventSet(ResourceOwner resowner, int nevents)
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)
void ResetLatch(Latch *latch)
#define WL_POSTMASTER_DEATH
#define WL_SOCKET_WRITEABLE
#define pq_putmessage(msgtype, s, len)
#define FeBeWaitSetLatchPos
#define FeBeWaitSetNEvents
#define FeBeWaitSetSocketPos
Assert(fmt[strlen(fmt) - 1] !='\n')
List * lappend(List *list, void *datum)
char * pstrdup(const char *in)
MemoryContext TopMemoryContext
void * repalloc(void *pointer, Size size)
void * MemoryContextAlloc(MemoryContext context, Size size)
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)
void assign_tcp_keepalives_count(int newval, void *extra)
const PQcommMethods * PqCommMethods
static int pq_recvbuf(void)
int Unix_socket_permissions
static int internal_flush(void)
static void socket_set_nonblocking(bool nonblocking)
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
const char * show_tcp_keepalives_interval(void)
int pq_getbytes(char *s, size_t len)
static bool PqCommReadingMsg
void StreamClose(pgsocket sock)
static int socket_flush(void)
int StreamServerPort(int family, const char *hostName, unsigned short portNumber, const char *unixSocketDir, pgsocket ListenSockets[], int *NumListenSockets, int MaxListen)
const char * show_tcp_user_timeout(void)
#define PQ_SEND_BUFFER_SIZE
int pq_setkeepalivesidle(int idle, Port *port)
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)
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)
bool pq_buffer_has_data(void)
static char PqRecvBuffer[PQ_RECV_BUFFER_SIZE]
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)
const char * show_tcp_keepalives_idle(void)
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
const char * show_tcp_keepalives_count(void)
void assign_tcp_keepalives_interval(int newval, void *extra)
void pq_startmsgread(void)
int pq_setkeepalivescount(int count, Port *port)
int StreamConnection(pgsocket server_fd, Port *port)
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)
void resetStringInfo(StringInfo str)
void enlargeStringInfo(StringInfo str, int needed)
#define bind(s, addr, addrlen)
#define socket(af, type, protocol)
#define accept(s, addr, addrlen)
#define listen(s, backlog)