87 #if defined(TCP_KEEPIDLE)
89 #define PG_TCP_KEEPALIVE_IDLE TCP_KEEPIDLE
90 #define PG_TCP_KEEPALIVE_IDLE_STR "TCP_KEEPIDLE"
91 #elif defined(TCP_KEEPALIVE_THRESHOLD)
93 #define PG_TCP_KEEPALIVE_IDLE TCP_KEEPALIVE_THRESHOLD
94 #define PG_TCP_KEEPALIVE_IDLE_STR "TCP_KEEPALIVE_THRESHOLD"
95 #elif defined(TCP_KEEPALIVE) && defined(__darwin__)
98 #define PG_TCP_KEEPALIVE_IDLE TCP_KEEPALIVE
99 #define PG_TCP_KEEPALIVE_IDLE_STR "TCP_KEEPALIVE"
118 #define PQ_SEND_BUFFER_SIZE 8192
119 #define PQ_RECV_BUFFER_SIZE 8192
151 static int Lock_AF_UNIX(
const char *unixSocketDir,
const char *unixSocketPath);
186 port->laddr.salen =
sizeof(
port->laddr.addr);
187 if (getsockname(
port->sock,
188 (
struct sockaddr *) &
port->laddr.addr,
189 &
port->laddr.salen) < 0)
192 (
errmsg(
"%s() failed: %m",
"getsockname")));
196 if (
port->laddr.addr.ss_family != AF_UNIX)
207 if (setsockopt(
port->sock, IPPROTO_TCP, TCP_NODELAY,
208 (
char *) &on,
sizeof(on)) < 0)
211 (
errmsg(
"%s(%s) failed: %m",
"setsockopt",
"TCP_NODELAY")));
215 if (setsockopt(
port->sock, SOL_SOCKET, SO_KEEPALIVE,
216 (
char *) &on,
sizeof(on)) < 0)
219 (
errmsg(
"%s(%s) failed: %m",
"setsockopt",
"SO_KEEPALIVE")));
245 optlen =
sizeof(oldopt);
246 if (getsockopt(
port->sock, SOL_SOCKET, SO_SNDBUF, (
char *) &oldopt,
250 (
errmsg(
"%s(%s) failed: %m",
"getsockopt",
"SO_SNDBUF")));
255 if (setsockopt(
port->sock, SOL_SOCKET, SO_SNDBUF, (
char *) &newopt,
259 (
errmsg(
"%s(%s) failed: %m",
"setsockopt",
"SO_SNDBUF")));
296 (
errmsg(
"could not set socket to nonblocking mode: %m")));
302 if (fcntl(
port->sock, F_SETFD, FD_CLOEXEC) < 0)
303 elog(
FATAL,
"fcntl(F_SETFD) failed on socket: %m");
308 port->sock, NULL, NULL);
418 const char *unixSocketDir,
425 char portNumberStr[32];
426 const char *familyDesc;
427 char familyDescBuf[64];
428 const char *addrDesc;
429 char addrBuf[NI_MAXHOST];
431 struct addrinfo *addrs = NULL,
433 struct addrinfo hint;
436 #if !defined(WIN32) || defined(IPV6_V6ONLY)
441 MemSet(&hint, 0,
sizeof(hint));
442 hint.ai_family = family;
443 hint.ai_flags = AI_PASSIVE;
444 hint.ai_socktype = SOCK_STREAM;
446 if (family == AF_UNIX)
456 (
errmsg(
"Unix-domain socket path \"%s\" is too long (maximum %d bytes)",
463 service = unixSocketPath;
467 snprintf(portNumberStr,
sizeof(portNumberStr),
"%d", portNumber);
468 service = portNumberStr;
476 (
errmsg(
"could not translate host name \"%s\", service \"%s\" to address: %s",
480 (
errmsg(
"could not translate service \"%s\" to address: %s",
487 for (addr = addrs; addr; addr = addr->ai_next)
489 if (family != AF_UNIX && addr->ai_family == AF_UNIX)
502 (
errmsg(
"could not bind to all requested addresses: MAXLISTEN (%d) exceeded",
508 switch (addr->ai_family)
511 familyDesc =
_(
"IPv4");
514 familyDesc =
_(
"IPv6");
517 familyDesc =
_(
"Unix");
520 snprintf(familyDescBuf,
sizeof(familyDescBuf),
521 _(
"unrecognized address family %d"),
523 familyDesc = familyDescBuf;
528 if (addr->ai_family == AF_UNIX)
529 addrDesc = unixSocketPath;
534 addrBuf,
sizeof(addrBuf),
545 errmsg(
"could not create %s socket for address \"%s\": %m",
546 familyDesc, addrDesc)));
552 if (fcntl(
fd, F_SETFD, FD_CLOEXEC) < 0)
553 elog(
FATAL,
"fcntl(F_SETFD) failed on socket: %m");
566 if (addr->ai_family != AF_UNIX)
568 if ((setsockopt(
fd, SOL_SOCKET, SO_REUSEADDR,
569 (
char *) &one,
sizeof(one))) == -1)
574 errmsg(
"%s(%s) failed for %s address \"%s\": %m",
575 "setsockopt",
"SO_REUSEADDR",
576 familyDesc, addrDesc)));
584 if (addr->ai_family == AF_INET6)
586 if (setsockopt(
fd, IPPROTO_IPV6, IPV6_V6ONLY,
587 (
char *) &one,
sizeof(one)) == -1)
592 errmsg(
"%s(%s) failed for %s address \"%s\": %m",
593 "setsockopt",
"IPV6_V6ONLY",
594 familyDesc, addrDesc)));
607 err =
bind(
fd, addr->ai_addr, addr->ai_addrlen);
610 int saved_errno = errno;
615 errmsg(
"could not bind %s address \"%s\": %m",
616 familyDesc, addrDesc),
618 (addr->ai_family == AF_UNIX ?
619 errhint(
"Is another postmaster already running on port %d?",
621 errhint(
"Is another postmaster already running on port %d?"
622 " If not, wait a few seconds and retry.",
623 (
int) portNumber)) : 0));
628 if (addr->ai_family == AF_UNIX)
650 errmsg(
"could not listen on %s address \"%s\": %m",
651 familyDesc, addrDesc)));
656 if (addr->ai_family == AF_UNIX)
658 (
errmsg(
"listening on Unix socket \"%s\"",
663 (
errmsg(
"listening on %s address \"%s\", port %d",
664 familyDesc, addrDesc, (
int) portNumber)));
667 (*NumListenSockets)++;
687 if (unixSocketPath[0] ==
'@')
704 (void) unlink(unixSocketPath);
722 if (sock_path[0] ==
'@')
734 elog(
WARNING,
"configuration item unix_socket_group is not supported on this platform");
753 (
errmsg(
"group \"%s\" does not exist",
759 if (chown(sock_path, -1, gid) == -1)
763 errmsg(
"could not set group of file \"%s\": %m",
774 errmsg(
"could not set permissions of file \"%s\": %m",
798 (
struct sockaddr *) &client_sock->
raddr.
addr,
803 errmsg(
"could not accept new connection: %m")));
836 char *sock_path = (
char *)
lfirst(l);
839 (void) utime(sock_path, NULL);
854 char *sock_path = (
char *)
lfirst(l);
857 (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")));
1148 (
errcode(ERRCODE_PROTOCOL_VIOLATION),
1149 errmsg(
"terminating connection because protocol synchronization was lost")));
1214 (
errcode(ERRCODE_PROTOCOL_VIOLATION),
1215 errmsg(
"unexpected EOF within message length word")));
1221 if (len < 4 || len > maxlen)
1224 (
errcode(ERRCODE_PROTOCOL_VIOLATION),
1225 errmsg(
"invalid message length")));
1246 (
errcode(ERRCODE_PROTOCOL_VIOLATION),
1247 errmsg(
"incomplete message from client")));
1259 (
errcode(ERRCODE_PROTOCOL_VIOLATION),
1260 errmsg(
"incomplete message from client")));
1361 static int last_reported_send_errno = 0;
1364 const char *bufend =
buf + *end;
1366 while (bufptr < bufend)
1396 if (errno != last_reported_send_errno)
1398 last_reported_send_errno = errno;
1401 errmsg(
"could not send data to client: %m")));
1416 last_reported_send_errno = 0;
1588 #if defined(WIN32) && defined(SIO_KEEPALIVE_VALS)
1592 struct tcp_keepalive ka;
1601 ka.keepalivetime = idle * 1000;
1602 ka.keepaliveinterval =
interval * 1000;
1604 if (WSAIoctl(
port->sock,
1616 (
errmsg(
"%s(%s) failed: error code %d",
1617 "WSAIoctl",
"SIO_KEEPALIVE_VALS", WSAGetLastError())));
1620 if (
port->keepalives_idle != idle)
1621 port->keepalives_idle = idle;
1631 #if defined(PG_TCP_KEEPALIVE_IDLE) || defined(SIO_KEEPALIVE_VALS)
1632 if (
port == NULL ||
port->laddr.addr.ss_family == AF_UNIX)
1635 if (
port->keepalives_idle != 0)
1636 return port->keepalives_idle;
1638 if (
port->default_keepalives_idle == 0)
1643 if (getsockopt(
port->sock, IPPROTO_TCP, PG_TCP_KEEPALIVE_IDLE,
1644 (
char *) &
port->default_keepalives_idle,
1648 (
errmsg(
"%s(%s) failed: %m",
"getsockopt", PG_TCP_KEEPALIVE_IDLE_STR)));
1649 port->default_keepalives_idle = -1;
1653 port->default_keepalives_idle = -1;
1657 return port->default_keepalives_idle;
1666 if (
port == NULL ||
port->laddr.addr.ss_family == AF_UNIX)
1670 #if defined(PG_TCP_KEEPALIVE_IDLE) || defined(SIO_KEEPALIVE_VALS)
1671 if (idle ==
port->keepalives_idle)
1675 if (
port->default_keepalives_idle <= 0)
1687 idle =
port->default_keepalives_idle;
1689 if (setsockopt(
port->sock, IPPROTO_TCP, PG_TCP_KEEPALIVE_IDLE,
1690 (
char *) &idle,
sizeof(idle)) < 0)
1693 (
errmsg(
"%s(%s) failed: %m",
"setsockopt", PG_TCP_KEEPALIVE_IDLE_STR)));
1697 port->keepalives_idle = idle;
1699 return pq_setkeepaliveswin32(
port, idle,
port->keepalives_interval);
1705 (
errmsg(
"setting the keepalive idle time is not supported")));
1716 #if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
1717 if (
port == NULL ||
port->laddr.addr.ss_family == AF_UNIX)
1720 if (
port->keepalives_interval != 0)
1721 return port->keepalives_interval;
1723 if (
port->default_keepalives_interval == 0)
1728 if (getsockopt(
port->sock, IPPROTO_TCP, TCP_KEEPINTVL,
1729 (
char *) &
port->default_keepalives_interval,
1733 (
errmsg(
"%s(%s) failed: %m",
"getsockopt",
"TCP_KEEPINTVL")));
1734 port->default_keepalives_interval = -1;
1738 port->default_keepalives_interval = -1;
1742 return port->default_keepalives_interval;
1751 if (
port == NULL ||
port->laddr.addr.ss_family == AF_UNIX)
1754 #if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
1759 if (
port->default_keepalives_interval <= 0)
1773 if (setsockopt(
port->sock, IPPROTO_TCP, TCP_KEEPINTVL,
1777 (
errmsg(
"%s(%s) failed: %m",
"setsockopt",
"TCP_KEEPINTVL")));
1789 (
errmsg(
"%s(%s) not supported",
"setsockopt",
"TCP_KEEPINTVL")));
1801 if (
port == NULL ||
port->laddr.addr.ss_family == AF_UNIX)
1804 if (
port->keepalives_count != 0)
1805 return port->keepalives_count;
1807 if (
port->default_keepalives_count == 0)
1811 if (getsockopt(
port->sock, IPPROTO_TCP, TCP_KEEPCNT,
1812 (
char *) &
port->default_keepalives_count,
1816 (
errmsg(
"%s(%s) failed: %m",
"getsockopt",
"TCP_KEEPCNT")));
1817 port->default_keepalives_count = -1;
1821 return port->default_keepalives_count;
1830 if (
port == NULL ||
port->laddr.addr.ss_family == AF_UNIX)
1834 if (count ==
port->keepalives_count)
1837 if (
port->default_keepalives_count <= 0)
1849 count =
port->default_keepalives_count;
1851 if (setsockopt(
port->sock, IPPROTO_TCP, TCP_KEEPCNT,
1852 (
char *) &count,
sizeof(count)) < 0)
1855 (
errmsg(
"%s(%s) failed: %m",
"setsockopt",
"TCP_KEEPCNT")));
1859 port->keepalives_count = count;
1864 (
errmsg(
"%s(%s) not supported",
"setsockopt",
"TCP_KEEPCNT")));
1875 #ifdef TCP_USER_TIMEOUT
1876 if (
port == NULL ||
port->laddr.addr.ss_family == AF_UNIX)
1879 if (
port->tcp_user_timeout != 0)
1880 return port->tcp_user_timeout;
1882 if (
port->default_tcp_user_timeout == 0)
1886 if (getsockopt(
port->sock, IPPROTO_TCP, TCP_USER_TIMEOUT,
1887 (
char *) &
port->default_tcp_user_timeout,
1891 (
errmsg(
"%s(%s) failed: %m",
"getsockopt",
"TCP_USER_TIMEOUT")));
1892 port->default_tcp_user_timeout = -1;
1896 return port->default_tcp_user_timeout;
1905 if (
port == NULL ||
port->laddr.addr.ss_family == AF_UNIX)
1908 #ifdef TCP_USER_TIMEOUT
1909 if (timeout ==
port->tcp_user_timeout)
1912 if (
port->default_tcp_user_timeout <= 0)
1924 timeout =
port->default_tcp_user_timeout;
1926 if (setsockopt(
port->sock, IPPROTO_TCP, TCP_USER_TIMEOUT,
1927 (
char *) &timeout,
sizeof(timeout)) < 0)
1930 (
errmsg(
"%s(%s) failed: %m",
"setsockopt",
"TCP_USER_TIMEOUT")));
1934 port->tcp_user_timeout = timeout;
1939 (
errmsg(
"%s(%s) not supported",
"setsockopt",
"TCP_USER_TIMEOUT")));
1974 static char nbuf[16];
1997 static char nbuf[16];
2020 static char nbuf[16];
2043 static char nbuf[16];
2066 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)
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
List * lappend(List *list, void *datum)
char * pstrdup(const char *in)
MemoryContext TopMemoryContext
void * palloc0(Size size)
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)
static size_t PqSendPointer
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
Port * pq_init(ClientSocket *client_sock)
static int socket_flush(void)
ssize_t pq_buffer_remaining_data(void)
const char * show_tcp_user_timeout(void)
#define PQ_SEND_BUFFER_SIZE
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]
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)
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)