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
187 port->laddr.salen =
sizeof(
port->laddr.addr);
190 &
port->laddr.salen) < 0)
193 (
errmsg(
"%s() failed: %m",
"getsockname")));
209 (
char *) &
on,
sizeof(
on)) < 0)
212 (
errmsg(
"%s(%s) failed: %m",
"setsockopt",
"TCP_NODELAY")));
217 (
char *) &
on,
sizeof(
on)) < 0)
220 (
errmsg(
"%s(%s) failed: %m",
"setsockopt",
"SO_KEEPALIVE")));
251 (
errmsg(
"%s(%s) failed: %m",
"getsockopt",
"SO_SNDBUF")));
260 (
errmsg(
"%s(%s) failed: %m",
"setsockopt",
"SO_SNDBUF")));
297 (
errmsg(
"could not set socket to nonblocking mode: %m")));
304 elog(
FATAL,
"fcntl(F_SETFD) failed on socket: %m");
437#if !defined(WIN32) || defined(IPV6_V6ONLY)
442 MemSet(&hint, 0,
sizeof(hint));
443 hint.ai_family = family;
457 (
errmsg(
"Unix-domain socket path \"%s\" is too long (maximum %zu bytes)",
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)
503 (
errmsg(
"could not bind to all requested addresses: MAXLISTEN (%d) exceeded",
509 switch (addr->ai_family)
522 _(
"unrecognized address family %d"),
529 if (addr->ai_family ==
AF_UNIX)
546 errmsg(
"could not create %s socket for address \"%s\": %m",
554 elog(
FATAL,
"fcntl(F_SETFD) failed on socket: %m");
567 if (addr->ai_family !=
AF_UNIX)
570 (
char *) &
one,
sizeof(
one))) == -1)
575 errmsg(
"%s(%s) failed for %s address \"%s\": %m",
576 "setsockopt",
"SO_REUSEADDR",
588 (
char *) &
one,
sizeof(
one)) == -1)
593 errmsg(
"%s(%s) failed for %s address \"%s\": %m",
594 "setsockopt",
"IPV6_V6ONLY",
608 err =
bind(
fd, addr->ai_addr, addr->ai_addrlen);
611 int saved_errno =
errno;
616 errmsg(
"could not bind %s address \"%s\": %m",
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.",
629 if (addr->ai_family ==
AF_UNIX)
651 errmsg(
"could not listen on %s address \"%s\": %m",
657 if (addr->ai_family ==
AF_UNIX)
659 (
errmsg(
"listening on Unix socket \"%s\"",
664 (
errmsg(
"listening on %s address \"%s\", port %d",
668 (*NumListenSockets)++;
735 elog(
WARNING,
"configuration item \"unix_socket_group\" is not supported on this platform");
754 (
errmsg(
"group \"%s\" does not exist",
764 errmsg(
"could not set group of file \"%s\": %m",
775 errmsg(
"could not set permissions of file \"%s\": %m",
804 errmsg(
"could not accept new connection: %m")));
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")));
1150 errmsg(
"terminating connection because protocol synchronization was lost")));
1216 errmsg(
"unexpected EOF within message length word")));
1226 errmsg(
"invalid message length")));
1248 errmsg(
"incomplete message from client")));
1261 errmsg(
"incomplete message from client")));
1367 const char *bufend =
buf + *end;
1369 while (bufptr < bufend)
1404 errmsg(
"could not send data to client: %m")));
1591#if defined(WIN32) && defined(SIO_KEEPALIVE_VALS)
1604 ka.keepalivetime =
idle * 1000;
1619 (
errmsg(
"%s(%s) failed: error code %d",
1634#if defined(PG_TCP_KEEPALIVE_IDLE) || defined(SIO_KEEPALIVE_VALS)
1638 if (
port->keepalives_idle != 0)
1639 return port->keepalives_idle;
1641 if (
port->default_keepalives_idle == 0)
1647 (
char *) &
port->default_keepalives_idle,
1652 port->default_keepalives_idle = -1;
1656 port->default_keepalives_idle = -1;
1660 return port->default_keepalives_idle;
1673#if defined(PG_TCP_KEEPALIVE_IDLE) || defined(SIO_KEEPALIVE_VALS)
1678 if (
port->default_keepalives_idle <= 0)
1690 idle =
port->default_keepalives_idle;
1708 (
errmsg(
"setting the keepalive idle time is not supported")));
1719#if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
1723 if (
port->keepalives_interval != 0)
1724 return port->keepalives_interval;
1726 if (
port->default_keepalives_interval == 0)
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;
1757#if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
1762 if (
port->default_keepalives_interval <= 0)
1780 (
errmsg(
"%s(%s) failed: %m",
"setsockopt",
"TCP_KEEPINTVL")));
1792 (
errmsg(
"%s(%s) not supported",
"setsockopt",
"TCP_KEEPINTVL")));
1807 if (
port->keepalives_count != 0)
1808 return port->keepalives_count;
1810 if (
port->default_keepalives_count == 0)
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;
1837 if (count ==
port->keepalives_count)
1840 if (
port->default_keepalives_count <= 0)
1852 count =
port->default_keepalives_count;
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
1882 if (
port->tcp_user_timeout != 0)
1883 return port->tcp_user_timeout;
1885 if (
port->default_tcp_user_timeout == 0)
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;
1911#ifdef TCP_USER_TIMEOUT
1915 if (
port->default_tcp_user_timeout <= 0)
1933 (
errmsg(
"%s(%s) failed: %m",
"setsockopt",
"TCP_USER_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 Assert(condition)
#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,...)
#define palloc0_object(type)
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 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)
MemoryContext TopMemoryContext
void CreateSocketLockFile(const char *socketfile, bool amPostmaster, const char *socketDir)
static char buf[DEFAULT_XLOG_SEG_SIZE]
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 errcode)
void resetStringInfo(StringInfo str)
void enlargeStringInfo(StringInfo str, int needed)
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)