89#if defined(TCP_KEEPIDLE)
91#define PG_TCP_KEEPALIVE_IDLE TCP_KEEPIDLE
92#define PG_TCP_KEEPALIVE_IDLE_STR "TCP_KEEPIDLE"
93#elif defined(TCP_KEEPALIVE_THRESHOLD)
95#define PG_TCP_KEEPALIVE_IDLE TCP_KEEPALIVE_THRESHOLD
96#define PG_TCP_KEEPALIVE_IDLE_STR "TCP_KEEPALIVE_THRESHOLD"
97#elif defined(TCP_KEEPALIVE) && defined(__darwin__)
100#define PG_TCP_KEEPALIVE_IDLE TCP_KEEPALIVE
101#define PG_TCP_KEEPALIVE_IDLE_STR "TCP_KEEPALIVE"
120#define PQ_SEND_BUFFER_SIZE 8192
121#define PQ_RECV_BUFFER_SIZE 8192
188 port->laddr.salen =
sizeof(
port->laddr.addr);
191 &
port->laddr.salen) < 0)
194 (
errmsg(
"%s() failed: %m",
"getsockname")));
210 (
char *) &
on,
sizeof(
on)) < 0)
213 (
errmsg(
"%s(%s) failed: %m",
"setsockopt",
"TCP_NODELAY")));
218 (
char *) &
on,
sizeof(
on)) < 0)
221 (
errmsg(
"%s(%s) failed: %m",
"setsockopt",
"SO_KEEPALIVE")));
252 (
errmsg(
"%s(%s) failed: %m",
"getsockopt",
"SO_SNDBUF")));
261 (
errmsg(
"%s(%s) failed: %m",
"setsockopt",
"SO_SNDBUF")));
298 (
errmsg(
"could not set socket to nonblocking mode: %m")));
305 elog(
FATAL,
"fcntl(F_SETFD) failed on socket: %m");
438#if !defined(WIN32) || defined(IPV6_V6ONLY)
443 MemSet(&hint, 0,
sizeof(hint));
444 hint.ai_family = family;
458 (
errmsg(
"Unix-domain socket path \"%s\" is too long (maximum %zu bytes)",
478 (
errmsg(
"could not translate host name \"%s\", service \"%s\" to address: %s",
482 (
errmsg(
"could not translate service \"%s\" to address: %s",
489 for (addr = addrs; addr; addr = addr->ai_next)
504 (
errmsg(
"could not bind to all requested addresses: MAXLISTEN (%d) exceeded",
510 switch (addr->ai_family)
523 _(
"unrecognized address family %d"),
530 if (addr->ai_family ==
AF_UNIX)
547 errmsg(
"could not create %s socket for address \"%s\": %m",
555 elog(
FATAL,
"fcntl(F_SETFD) failed on socket: %m");
568 if (addr->ai_family !=
AF_UNIX)
571 (
char *) &
one,
sizeof(
one))) == -1)
576 errmsg(
"%s(%s) failed for %s address \"%s\": %m",
577 "setsockopt",
"SO_REUSEADDR",
589 (
char *) &
one,
sizeof(
one)) == -1)
594 errmsg(
"%s(%s) failed for %s address \"%s\": %m",
595 "setsockopt",
"IPV6_V6ONLY",
609 err =
bind(
fd, addr->ai_addr, addr->ai_addrlen);
612 int saved_errno =
errno;
617 errmsg(
"could not bind %s address \"%s\": %m",
621 errhint(
"Is another postmaster already running on port %d?",
623 errhint(
"Is another postmaster already running on port %d?"
624 " If not, wait a few seconds and retry.",
630 if (addr->ai_family ==
AF_UNIX)
652 errmsg(
"could not listen on %s address \"%s\": %m",
658 if (addr->ai_family ==
AF_UNIX)
660 (
errmsg(
"listening on Unix socket \"%s\"",
665 (
errmsg(
"listening on %s address \"%s\", port %d",
669 (*NumListenSockets)++;
736 elog(
WARNING,
"configuration item \"unix_socket_group\" is not supported on this platform");
755 (
errmsg(
"group \"%s\" does not exist",
765 errmsg(
"could not set group of file \"%s\": %m",
776 errmsg(
"could not set permissions of file \"%s\": %m",
805 errmsg(
"could not accept new connection: %m")));
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")));
1151 errmsg(
"terminating connection because protocol synchronization was lost")));
1217 errmsg(
"unexpected EOF within message length word")));
1227 errmsg(
"invalid message length")));
1249 errmsg(
"incomplete message from client")));
1262 errmsg(
"incomplete message from client")));
1368 const char *bufend =
buf + *end;
1370 while (bufptr < bufend)
1405 errmsg(
"could not send data to client: %m")));
1592#if defined(WIN32) && defined(SIO_KEEPALIVE_VALS)
1605 ka.keepalivetime =
idle * 1000;
1620 (
errmsg(
"%s(%s) failed: error code %d",
1635#if defined(PG_TCP_KEEPALIVE_IDLE) || defined(SIO_KEEPALIVE_VALS)
1639 if (
port->keepalives_idle != 0)
1640 return port->keepalives_idle;
1642 if (
port->default_keepalives_idle == 0)
1648 (
char *) &
port->default_keepalives_idle,
1653 port->default_keepalives_idle = -1;
1657 port->default_keepalives_idle = -1;
1661 return port->default_keepalives_idle;
1674#if defined(PG_TCP_KEEPALIVE_IDLE) || defined(SIO_KEEPALIVE_VALS)
1679 if (
port->default_keepalives_idle <= 0)
1691 idle =
port->default_keepalives_idle;
1709 (
errmsg(
"setting the keepalive idle time is not supported")));
1720#if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
1724 if (
port->keepalives_interval != 0)
1725 return port->keepalives_interval;
1727 if (
port->default_keepalives_interval == 0)
1733 (
char *) &
port->default_keepalives_interval,
1737 (
errmsg(
"%s(%s) failed: %m",
"getsockopt",
"TCP_KEEPINTVL")));
1738 port->default_keepalives_interval = -1;
1742 port->default_keepalives_interval = -1;
1746 return port->default_keepalives_interval;
1758#if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
1763 if (
port->default_keepalives_interval <= 0)
1781 (
errmsg(
"%s(%s) failed: %m",
"setsockopt",
"TCP_KEEPINTVL")));
1793 (
errmsg(
"%s(%s) not supported",
"setsockopt",
"TCP_KEEPINTVL")));
1808 if (
port->keepalives_count != 0)
1809 return port->keepalives_count;
1811 if (
port->default_keepalives_count == 0)
1816 (
char *) &
port->default_keepalives_count,
1820 (
errmsg(
"%s(%s) failed: %m",
"getsockopt",
"TCP_KEEPCNT")));
1821 port->default_keepalives_count = -1;
1825 return port->default_keepalives_count;
1838 if (count ==
port->keepalives_count)
1841 if (
port->default_keepalives_count <= 0)
1853 count =
port->default_keepalives_count;
1856 (
char *) &count,
sizeof(count)) < 0)
1859 (
errmsg(
"%s(%s) failed: %m",
"setsockopt",
"TCP_KEEPCNT")));
1863 port->keepalives_count = count;
1868 (
errmsg(
"%s(%s) not supported",
"setsockopt",
"TCP_KEEPCNT")));
1879#ifdef TCP_USER_TIMEOUT
1883 if (
port->tcp_user_timeout != 0)
1884 return port->tcp_user_timeout;
1886 if (
port->default_tcp_user_timeout == 0)
1891 (
char *) &
port->default_tcp_user_timeout,
1895 (
errmsg(
"%s(%s) failed: %m",
"getsockopt",
"TCP_USER_TIMEOUT")));
1896 port->default_tcp_user_timeout = -1;
1900 return port->default_tcp_user_timeout;
1912#ifdef TCP_USER_TIMEOUT
1916 if (
port->default_tcp_user_timeout <= 0)
1934 (
errmsg(
"%s(%s) failed: %m",
"setsockopt",
"TCP_USER_TIMEOUT")));
1943 (
errmsg(
"%s(%s) not supported",
"setsockopt",
"TCP_USER_TIMEOUT")));
1978 static char nbuf[16];
2001 static char nbuf[16];
2024 static char nbuf[16];
2047 static char nbuf[16];
2070 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 errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
int errhint(const char *fmt,...) pg_attribute_printf(1
#define ereport(elevel,...)
void err(int eval, const char *fmt,...)
#define ERRCODE_PROTOCOL_VIOLATION
#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)