1 /*-------------------------------------------------------------------------
4 * Communication functions between the Frontend and the Backend
6 * These routines handle the low-level details of communication between
7 * frontend and backend. They just shove data across the communication
8 * channel, and are ignorant of the semantics of the data --- or would be,
9 * except for major brain damage in the design of the old COPY OUT protocol.
10 * Unfortunately, COPY OUT was designed to commandeer the communication
11 * channel (it just transfers data without wrapping it into messages).
12 * No other messages can be sent while COPY OUT is in progress; and if the
13 * copy is aborted by an ereport(ERROR), we need to close out the copy so that
14 * the frontend gets back into sync. Therefore, these routines have to be
15 * aware of COPY OUT state. (New COPY-OUT is message-based and does *not*
16 * set the DoingCopyOut flag.)
18 * NOTE: generally, it's a bad idea to emit outgoing messages directly with
19 * pq_putbytes(), especially if the message would require multiple calls
20 * to send. Instead, use the routines in pqformat.c to construct the message
21 * in a buffer and then emit it in one call to pq_putmessage. This ensures
22 * that the channel will not be clogged by an incomplete message if execution
23 * is aborted by ereport(ERROR) partway through the message. The only
24 * non-libpq code that should call pq_putbytes directly is old-style COPY OUT.
26 * At one time, libpq was shared between frontend and backend, but now
27 * the backend's "backend/libpq" is quite separate from "interfaces/libpq".
28 * All that remains is similarities of names to trap the unwary...
30 * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
31 * Portions Copyright (c) 1994, Regents of the University of California
33 * src/backend/libpq/pqcomm.c
35 *-------------------------------------------------------------------------
38 /*------------------------
42 * StreamServerPort - Open postmaster's server port
43 * StreamConnection - Create new connection with client
44 * StreamClose - Close a client/backend connection
45 * TouchSocketFiles - Protect socket files against /tmp cleaners
46 * pq_init - initialize libpq at backend startup
47 * pq_comm_reset - reset libpq during error recovery
48 * pq_close - shutdown libpq at backend exit
51 * pq_getbytes - get a known number of bytes from connection
52 * pq_getstring - get a null terminated string from connection
53 * pq_getmessage - get a message with length word from connection
54 * pq_getbyte - get next byte from connection
55 * pq_peekbyte - peek at next byte from connection
56 * pq_putbytes - send bytes to connection (not flushed until pq_flush)
57 * pq_flush - flush pending output
58 * pq_flush_if_writable - flush pending output if writable without blocking
59 * pq_getbyte_if_available - get a byte if available without blocking
61 * message-level I/O (and old-style-COPY-OUT cruft):
62 * pq_putmessage - send a normal message (suppressed in COPY OUT mode)
63 * pq_putmessage_noblock - buffer a normal message (suppressed in COPY OUT)
64 * pq_startcopyout - inform libpq that a COPY OUT transfer is beginning
65 * pq_endcopyout - end a COPY OUT transfer
67 *------------------------
76 #include <sys/socket.h>
80 #include <netinet/in.h>
81 #ifdef HAVE_NETINET_TCP_H
82 #include <netinet/tcp.h>
84 #include <arpa/inet.h>
88 #ifdef _MSC_VER /* mstcpip.h is missing on mingw */
92 #include "common/ip.h"
93 #include "libpq/libpq.h"
94 #include "miscadmin.h"
95 #include "storage/ipc.h"
96 #include "utils/guc.h"
97 #include "utils/memutils.h"
100 * Configuration options
102 int Unix_socket_permissions;
103 char *Unix_socket_group;
105 /* Where the Unix socket files are (list of palloc'd strings) */
106 static List *sock_paths = NIL;
109 * Buffers for low-level I/O.
111 * The receive buffer is fixed size. Send buffer is usually 8k, but can be
112 * enlarged by pq_putmessage_noblock() if the message doesn't fit otherwise.
115 #define PQ_SEND_BUFFER_SIZE 8192
116 #define PQ_RECV_BUFFER_SIZE 8192
118 static char *PqSendBuffer;
119 static int PqSendBufferSize; /* Size send buffer */
120 static int PqSendPointer; /* Next index to store a byte in PqSendBuffer */
121 static int PqSendStart; /* Next index to send a byte in PqSendBuffer */
123 static char PqRecvBuffer[PQ_RECV_BUFFER_SIZE];
124 static int PqRecvPointer; /* Next index to read a byte from PqRecvBuffer */
125 static int PqRecvLength; /* End of data available in PqRecvBuffer */
130 static bool PqCommBusy; /* busy sending data to the client */
131 static bool PqCommReadingMsg; /* in the middle of reading a message */
132 static bool DoingCopyOut; /* in old-protocol COPY OUT processing */
135 /* Internal functions */
136 static void socket_comm_reset(void);
137 static void socket_close(int code, Datum arg);
138 static void socket_set_nonblocking(bool nonblocking);
139 static int socket_flush(void);
140 static int socket_flush_if_writable(void);
141 static bool socket_is_send_pending(void);
142 static int socket_putmessage(char msgtype, const char *s, size_t len);
143 static void socket_putmessage_noblock(char msgtype, const char *s, size_t len);
144 static void socket_startcopyout(void);
145 static void socket_endcopyout(bool errorAbort);
146 static int internal_putbytes(const char *s, size_t len);
147 static int internal_flush(void);
149 #ifdef HAVE_UNIX_SOCKETS
150 static int Lock_AF_UNIX(char *unixSocketDir, char *unixSocketPath);
151 static int Setup_AF_UNIX(char *sock_path);
152 #endif /* HAVE_UNIX_SOCKETS */
154 static PQcommMethods PqCommSocketMethods = {
157 socket_flush_if_writable,
158 socket_is_send_pending,
160 socket_putmessage_noblock,
165 PQcommMethods *PqCommMethods = &PqCommSocketMethods;
167 WaitEventSet *FeBeWaitSet;
170 /* --------------------------------
171 * pq_init - initialize libpq at backend startup
172 * --------------------------------
177 /* initialize state variables */
178 PqSendBufferSize = PQ_SEND_BUFFER_SIZE;
179 PqSendBuffer = MemoryContextAlloc(TopMemoryContext, PqSendBufferSize);
180 PqSendPointer = PqSendStart = PqRecvPointer = PqRecvLength = 0;
182 PqCommReadingMsg = false;
183 DoingCopyOut = false;
185 /* set up process-exit hook to close the socket */
186 on_proc_exit(socket_close, 0);
189 * In backends (as soon as forked) we operate the underlying socket in
190 * nonblocking mode and use latches to implement blocking semantics if
191 * needed. That allows us to provide safely interruptible reads and
194 * Use COMMERROR on failure, because ERROR would try to send the error to
195 * the client, which might require changing the mode again, leading to
196 * infinite recursion.
199 if (!pg_set_noblock(MyProcPort->sock))
201 (errmsg("could not set socket to nonblocking mode: %m")));
204 FeBeWaitSet = CreateWaitEventSet(TopMemoryContext, 3);
205 AddWaitEventToSet(FeBeWaitSet, WL_SOCKET_WRITEABLE, MyProcPort->sock,
207 AddWaitEventToSet(FeBeWaitSet, WL_LATCH_SET, -1, MyLatch, NULL);
208 AddWaitEventToSet(FeBeWaitSet, WL_POSTMASTER_DEATH, -1, NULL, NULL);
211 /* --------------------------------
212 * socket_comm_reset - reset libpq during error recovery
214 * This is called from error recovery at the outer idle loop. It's
215 * just to get us out of trouble if we somehow manage to elog() from
216 * inside a pqcomm.c routine (which ideally will never happen, but...)
217 * --------------------------------
220 socket_comm_reset(void)
222 /* Do not throw away pending data, but do reset the busy flag */
224 /* We can abort any old-style COPY OUT, too */
228 /* --------------------------------
229 * socket_close - shutdown libpq at backend exit
231 * This is the one pg_on_exit_callback in place during BackendInitialize().
232 * That function's unusual signal handling constrains that this callback be
233 * safe to run at any instant.
234 * --------------------------------
237 socket_close(int code, Datum arg)
239 /* Nothing to do in a standalone backend, where MyProcPort is NULL. */
240 if (MyProcPort != NULL)
242 #if defined(ENABLE_GSS) || defined(ENABLE_SSPI)
247 * Shutdown GSSAPI layer. This section does nothing when interrupting
248 * BackendInitialize(), because pg_GSS_recvauth() makes first use of
251 if (MyProcPort->gss->ctx != GSS_C_NO_CONTEXT)
252 gss_delete_sec_context(&min_s, &MyProcPort->gss->ctx, NULL);
254 if (MyProcPort->gss->cred != GSS_C_NO_CREDENTIAL)
255 gss_release_cred(&min_s, &MyProcPort->gss->cred);
256 #endif /* ENABLE_GSS */
259 * GSS and SSPI share the port->gss struct. Since nowhere else does a
260 * postmaster child free this, doing so is safe when interrupting
261 * BackendInitialize().
263 free(MyProcPort->gss);
264 #endif /* ENABLE_GSS || ENABLE_SSPI */
267 * Cleanly shut down SSL layer. Nowhere else does a postmaster child
268 * call this, so this is safe when interrupting BackendInitialize().
270 secure_close(MyProcPort);
273 * Formerly we did an explicit close() here, but it seems better to
274 * leave the socket open until the process dies. This allows clients
275 * to perform a "synchronous close" if they care --- wait till the
276 * transport layer reports connection closure, and you can be sure the
277 * backend has exited.
279 * We do set sock to PGINVALID_SOCKET to prevent any further I/O,
282 MyProcPort->sock = PGINVALID_SOCKET;
289 * Streams -- wrapper around Unix socket system calls
292 * Stream functions are used for vanilla TCP connection protocol.
297 * StreamServerPort -- open a "listening" port to accept connections.
299 * family should be AF_UNIX or AF_UNSPEC; portNumber is the port number.
300 * For AF_UNIX ports, hostName should be NULL and unixSocketDir must be
301 * specified. For TCP ports, hostName is either NULL for all interfaces or
302 * the interface to listen on, and unixSocketDir is ignored (can be NULL).
304 * Successfully opened sockets are added to the ListenSocket[] array (of
305 * length MaxListen), at the first position that isn't PGINVALID_SOCKET.
307 * RETURNS: STATUS_OK or STATUS_ERROR
311 StreamServerPort(int family, char *hostName, unsigned short portNumber,
313 pgsocket ListenSocket[], int MaxListen)
319 char portNumberStr[32];
320 const char *familyDesc;
321 char familyDescBuf[64];
322 const char *addrDesc;
323 char addrBuf[NI_MAXHOST];
325 struct addrinfo *addrs = NULL,
327 struct addrinfo hint;
328 int listen_index = 0;
331 #ifdef HAVE_UNIX_SOCKETS
332 char unixSocketPath[MAXPGPATH];
334 #if !defined(WIN32) || defined(IPV6_V6ONLY)
338 /* Initialize hint structure */
339 MemSet(&hint, 0, sizeof(hint));
340 hint.ai_family = family;
341 hint.ai_flags = AI_PASSIVE;
342 hint.ai_socktype = SOCK_STREAM;
344 #ifdef HAVE_UNIX_SOCKETS
345 if (family == AF_UNIX)
348 * Create unixSocketPath from portNumber and unixSocketDir and lock
351 UNIXSOCK_PATH(unixSocketPath, portNumber, unixSocketDir);
352 if (strlen(unixSocketPath) >= UNIXSOCK_PATH_BUFLEN)
355 (errmsg("Unix-domain socket path \"%s\" is too long (maximum %d bytes)",
357 (int) (UNIXSOCK_PATH_BUFLEN - 1))));
360 if (Lock_AF_UNIX(unixSocketDir, unixSocketPath) != STATUS_OK)
362 service = unixSocketPath;
365 #endif /* HAVE_UNIX_SOCKETS */
367 snprintf(portNumberStr, sizeof(portNumberStr), "%d", portNumber);
368 service = portNumberStr;
371 ret = pg_getaddrinfo_all(hostName, service, &hint, &addrs);
376 (errmsg("could not translate host name \"%s\", service \"%s\" to address: %s",
377 hostName, service, gai_strerror(ret))));
380 (errmsg("could not translate service \"%s\" to address: %s",
381 service, gai_strerror(ret))));
383 pg_freeaddrinfo_all(hint.ai_family, addrs);
387 for (addr = addrs; addr; addr = addr->ai_next)
389 if (!IS_AF_UNIX(family) && IS_AF_UNIX(addr->ai_family))
392 * Only set up a unix domain socket when they really asked for it.
393 * The service/port is different in that case.
398 /* See if there is still room to add 1 more socket. */
399 for (; listen_index < MaxListen; listen_index++)
401 if (ListenSocket[listen_index] == PGINVALID_SOCKET)
404 if (listen_index >= MaxListen)
407 (errmsg("could not bind to all requested addresses: MAXLISTEN (%d) exceeded",
412 /* set up address family name for log messages */
413 switch (addr->ai_family)
416 familyDesc = _("IPv4");
420 familyDesc = _("IPv6");
423 #ifdef HAVE_UNIX_SOCKETS
425 familyDesc = _("Unix");
429 snprintf(familyDescBuf, sizeof(familyDescBuf),
430 _("unrecognized address family %d"),
432 familyDesc = familyDescBuf;
436 /* set up text form of address for log messages */
437 #ifdef HAVE_UNIX_SOCKETS
438 if (addr->ai_family == AF_UNIX)
439 addrDesc = unixSocketPath;
443 pg_getnameinfo_all((const struct sockaddr_storage *) addr->ai_addr,
445 addrBuf, sizeof(addrBuf),
451 if ((fd = socket(addr->ai_family, SOCK_STREAM, 0)) == PGINVALID_SOCKET)
454 (errcode_for_socket_access(),
455 /* translator: first %s is IPv4, IPv6, or Unix */
456 errmsg("could not create %s socket for address \"%s\": %m",
457 familyDesc, addrDesc)));
464 * Without the SO_REUSEADDR flag, a new postmaster can't be started
465 * right away after a stop or crash, giving "address already in use"
466 * error on TCP ports.
468 * On win32, however, this behavior only happens if the
469 * SO_EXLUSIVEADDRUSE is set. With SO_REUSEADDR, win32 allows multiple
470 * servers to listen on the same address, resulting in unpredictable
471 * behavior. With no flags at all, win32 behaves as Unix with
474 if (!IS_AF_UNIX(addr->ai_family))
476 if ((setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
477 (char *) &one, sizeof(one))) == -1)
480 (errcode_for_socket_access(),
481 /* translator: first %s is IPv4, IPv6, or Unix */
482 errmsg("setsockopt(SO_REUSEADDR) failed for %s address \"%s\": %m",
483 familyDesc, addrDesc)));
491 if (addr->ai_family == AF_INET6)
493 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY,
494 (char *) &one, sizeof(one)) == -1)
497 (errcode_for_socket_access(),
498 /* translator: first %s is IPv4, IPv6, or Unix */
499 errmsg("setsockopt(IPV6_V6ONLY) failed for %s address \"%s\": %m",
500 familyDesc, addrDesc)));
508 * Note: This might fail on some OS's, like Linux older than
509 * 2.4.21-pre3, that don't have the IPV6_V6ONLY socket option, and map
510 * ipv4 addresses to ipv6. It will show ::ffff:ipv4 for all ipv4
513 err = bind(fd, addr->ai_addr, addr->ai_addrlen);
517 (errcode_for_socket_access(),
518 /* translator: first %s is IPv4, IPv6, or Unix */
519 errmsg("could not bind %s address \"%s\": %m",
520 familyDesc, addrDesc),
521 (IS_AF_UNIX(addr->ai_family)) ?
522 errhint("Is another postmaster already running on port %d?"
523 " If not, remove socket file \"%s\" and retry.",
524 (int) portNumber, service) :
525 errhint("Is another postmaster already running on port %d?"
526 " If not, wait a few seconds and retry.",
532 #ifdef HAVE_UNIX_SOCKETS
533 if (addr->ai_family == AF_UNIX)
535 if (Setup_AF_UNIX(service) != STATUS_OK)
544 * Select appropriate accept-queue length limit. PG_SOMAXCONN is only
545 * intended to provide a clamp on the request on platforms where an
546 * overly large request provokes a kernel error (are there any?).
548 maxconn = MaxBackends * 2;
549 if (maxconn > PG_SOMAXCONN)
550 maxconn = PG_SOMAXCONN;
552 err = listen(fd, maxconn);
556 (errcode_for_socket_access(),
557 /* translator: first %s is IPv4, IPv6, or Unix */
558 errmsg("could not listen on %s address \"%s\": %m",
559 familyDesc, addrDesc)));
564 #ifdef HAVE_UNIX_SOCKETS
565 if (addr->ai_family == AF_UNIX)
567 (errmsg("listening on Unix socket \"%s\"",
572 /* translator: first %s is IPv4 or IPv6 */
573 (errmsg("listening on %s address \"%s\", port %d",
574 familyDesc, addrDesc, (int) portNumber)));
576 ListenSocket[listen_index] = fd;
580 pg_freeaddrinfo_all(hint.ai_family, addrs);
589 #ifdef HAVE_UNIX_SOCKETS
592 * Lock_AF_UNIX -- configure unix socket file path
595 Lock_AF_UNIX(char *unixSocketDir, char *unixSocketPath)
598 * Grab an interlock file associated with the socket file.
600 * Note: there are two reasons for using a socket lock file, rather than
601 * trying to interlock directly on the socket itself. First, it's a lot
602 * more portable, and second, it lets us remove any pre-existing socket
603 * file without race conditions.
605 CreateSocketLockFile(unixSocketPath, true, unixSocketDir);
608 * Once we have the interlock, we can safely delete any pre-existing
609 * socket file to avoid failure at bind() time.
611 (void) unlink(unixSocketPath);
614 * Remember socket file pathnames for later maintenance.
616 sock_paths = lappend(sock_paths, pstrdup(unixSocketPath));
623 * Setup_AF_UNIX -- configure unix socket permissions
626 Setup_AF_UNIX(char *sock_path)
629 * Fix socket ownership/permission if requested. Note we must do this
630 * before we listen() to avoid a window where unwanted connections could
633 Assert(Unix_socket_group);
634 if (Unix_socket_group[0] != '\0')
637 elog(WARNING, "configuration item unix_socket_group is not supported on this platform");
643 val = strtoul(Unix_socket_group, &endptr, 10);
645 { /* numeric group id */
649 { /* convert group name to id */
652 gr = getgrnam(Unix_socket_group);
656 (errmsg("group \"%s\" does not exist",
657 Unix_socket_group)));
662 if (chown(sock_path, -1, gid) == -1)
665 (errcode_for_file_access(),
666 errmsg("could not set group of file \"%s\": %m",
673 if (chmod(sock_path, Unix_socket_permissions) == -1)
676 (errcode_for_file_access(),
677 errmsg("could not set permissions of file \"%s\": %m",
683 #endif /* HAVE_UNIX_SOCKETS */
687 * StreamConnection -- create a new connection with client using
688 * server port. Set port->sock to the FD of the new connection.
690 * ASSUME: that this doesn't need to be non-blocking because
691 * the Postmaster uses select() to tell when the server master
692 * socket is ready for accept().
694 * RETURNS: STATUS_OK or STATUS_ERROR
697 StreamConnection(pgsocket server_fd, Port *port)
699 /* accept connection and fill in the client (remote) address */
700 port->raddr.salen = sizeof(port->raddr.addr);
701 if ((port->sock = accept(server_fd,
702 (struct sockaddr *) & port->raddr.addr,
703 &port->raddr.salen)) == PGINVALID_SOCKET)
706 (errcode_for_socket_access(),
707 errmsg("could not accept new connection: %m")));
710 * If accept() fails then postmaster.c will still see the server
711 * socket as read-ready, and will immediately try again. To avoid
712 * uselessly sucking lots of CPU, delay a bit before trying again.
713 * (The most likely reason for failure is being out of kernel file
714 * table slots; we can do little except hope some will get freed up.)
716 pg_usleep(100000L); /* wait 0.1 sec */
720 /* fill in the server (local) address */
721 port->laddr.salen = sizeof(port->laddr.addr);
722 if (getsockname(port->sock,
723 (struct sockaddr *) & port->laddr.addr,
724 &port->laddr.salen) < 0)
726 elog(LOG, "getsockname() failed: %m");
730 /* select NODELAY and KEEPALIVE options if it's a TCP connection */
731 if (!IS_AF_UNIX(port->laddr.addr.ss_family))
742 if (setsockopt(port->sock, IPPROTO_TCP, TCP_NODELAY,
743 (char *) &on, sizeof(on)) < 0)
745 elog(LOG, "setsockopt(TCP_NODELAY) failed: %m");
750 if (setsockopt(port->sock, SOL_SOCKET, SO_KEEPALIVE,
751 (char *) &on, sizeof(on)) < 0)
753 elog(LOG, "setsockopt(SO_KEEPALIVE) failed: %m");
760 * This is a Win32 socket optimization. The OS send buffer should be
761 * large enough to send the whole Postgres send buffer in one go, or
762 * performance suffers. The Postgres send buffer can be enlarged if a
763 * very large message needs to be sent, but we won't attempt to
764 * enlarge the OS buffer if that happens, so somewhat arbitrarily
765 * ensure that the OS buffer is at least PQ_SEND_BUFFER_SIZE * 4.
766 * (That's 32kB with the current default).
768 * The default OS buffer size used to be 8kB in earlier Windows
769 * versions, but was raised to 64kB in Windows 2012. So it shouldn't
770 * be necessary to change it in later versions anymore. Changing it
771 * unnecessarily can even reduce performance, because setting
772 * SO_SNDBUF in the application disables the "dynamic send buffering"
773 * feature that was introduced in Windows 7. So before fiddling with
774 * SO_SNDBUF, check if the current buffer size is already large enough
775 * and only increase it if necessary.
777 * See https://support.microsoft.com/kb/823764/EN-US/ and
778 * https://msdn.microsoft.com/en-us/library/bb736549%28v=vs.85%29.aspx
780 optlen = sizeof(oldopt);
781 if (getsockopt(port->sock, SOL_SOCKET, SO_SNDBUF, (char *) &oldopt,
784 elog(LOG, "getsockopt(SO_SNDBUF) failed: %m");
787 newopt = PQ_SEND_BUFFER_SIZE * 4;
790 if (setsockopt(port->sock, SOL_SOCKET, SO_SNDBUF, (char *) &newopt,
793 elog(LOG, "setsockopt(SO_SNDBUF) failed: %m");
800 * Also apply the current keepalive parameters. If we fail to set a
801 * parameter, don't error out, because these aren't universally
802 * supported. (Note: you might think we need to reset the GUC
803 * variables to 0 in such a case, but it's not necessary because the
804 * show hooks for these variables report the truth anyway.)
806 (void) pq_setkeepalivesidle(tcp_keepalives_idle, port);
807 (void) pq_setkeepalivesinterval(tcp_keepalives_interval, port);
808 (void) pq_setkeepalivescount(tcp_keepalives_count, port);
815 * StreamClose -- close a client/backend connection
817 * NOTE: this is NOT used to terminate a session; it is just used to release
818 * the file descriptor in a process that should no longer have the socket
819 * open. (For example, the postmaster calls this after passing ownership
820 * of the connection to a child process.) It is expected that someone else
821 * still has the socket open. So, we only want to close the descriptor,
822 * we do NOT want to send anything to the far end.
825 StreamClose(pgsocket sock)
831 * TouchSocketFiles -- mark socket files as recently accessed
833 * This routine should be called every so often to ensure that the socket
834 * files have a recent mod date (ordinary operations on sockets usually won't
835 * change the mod date). That saves them from being removed by
836 * overenthusiastic /tmp-directory-cleaner daemons. (Another reason we should
837 * never have put the socket file in /tmp...)
840 TouchSocketFiles(void)
844 /* Loop through all created sockets... */
845 foreach(l, sock_paths)
847 char *sock_path = (char *) lfirst(l);
850 * utime() is POSIX standard, utimes() is a common alternative. If we
851 * have neither, there's no way to affect the mod or access time of
854 * In either path, we ignore errors; there's no point in complaining.
857 utime(sock_path, NULL);
858 #else /* !HAVE_UTIME */
860 utimes(sock_path, NULL);
861 #endif /* HAVE_UTIMES */
862 #endif /* HAVE_UTIME */
867 * RemoveSocketFiles -- unlink socket files at postmaster shutdown
870 RemoveSocketFiles(void)
874 /* Loop through all created sockets... */
875 foreach(l, sock_paths)
877 char *sock_path = (char *) lfirst(l);
879 /* Ignore any error. */
880 (void) unlink(sock_path);
882 /* Since we're about to exit, no need to reclaim storage */
887 /* --------------------------------
888 * Low-level I/O routines begin here.
890 * These routines communicate with a frontend client across a connection
891 * already established by the preceding routines.
892 * --------------------------------
895 /* --------------------------------
896 * socket_set_nonblocking - set socket blocking/non-blocking
898 * Sets the socket non-blocking if nonblocking is TRUE, or sets it
899 * blocking otherwise.
900 * --------------------------------
903 socket_set_nonblocking(bool nonblocking)
905 if (MyProcPort == NULL)
907 (errcode(ERRCODE_CONNECTION_DOES_NOT_EXIST),
908 errmsg("there is no client connection")));
910 MyProcPort->noblock = nonblocking;
913 /* --------------------------------
914 * pq_recvbuf - load some bytes into the input buffer
916 * returns 0 if OK, EOF if trouble
917 * --------------------------------
922 if (PqRecvPointer > 0)
924 if (PqRecvLength > PqRecvPointer)
926 /* still some unread data, left-justify it in the buffer */
927 memmove(PqRecvBuffer, PqRecvBuffer + PqRecvPointer,
928 PqRecvLength - PqRecvPointer);
929 PqRecvLength -= PqRecvPointer;
933 PqRecvLength = PqRecvPointer = 0;
936 /* Ensure that we're in blocking mode */
937 socket_set_nonblocking(false);
939 /* Can fill buffer from PqRecvLength and upwards */
944 r = secure_read(MyProcPort, PqRecvBuffer + PqRecvLength,
945 PQ_RECV_BUFFER_SIZE - PqRecvLength);
950 continue; /* Ok if interrupted */
953 * Careful: an ereport() that tries to write to the client would
954 * cause recursion to here, leading to stack overflow and core
955 * dump! This message must go *only* to the postmaster log.
958 (errcode_for_socket_access(),
959 errmsg("could not receive data from client: %m")));
965 * EOF detected. We used to write a log message here, but it's
966 * better to expect the ultimate caller to do that.
970 /* r contains number of bytes read, so just incr length */
976 /* --------------------------------
977 * pq_getbyte - get a single byte from connection, or return EOF
978 * --------------------------------
983 Assert(PqCommReadingMsg);
985 while (PqRecvPointer >= PqRecvLength)
987 if (pq_recvbuf()) /* If nothing in buffer, then recv some */
988 return EOF; /* Failed to recv data */
990 return (unsigned char) PqRecvBuffer[PqRecvPointer++];
993 /* --------------------------------
994 * pq_peekbyte - peek at next byte from connection
996 * Same as pq_getbyte() except we don't advance the pointer.
997 * --------------------------------
1002 Assert(PqCommReadingMsg);
1004 while (PqRecvPointer >= PqRecvLength)
1006 if (pq_recvbuf()) /* If nothing in buffer, then recv some */
1007 return EOF; /* Failed to recv data */
1009 return (unsigned char) PqRecvBuffer[PqRecvPointer];
1012 /* --------------------------------
1013 * pq_getbyte_if_available - get a single byte from connection,
1016 * The received byte is stored in *c. Returns 1 if a byte was read,
1017 * 0 if no data was available, or EOF if trouble.
1018 * --------------------------------
1021 pq_getbyte_if_available(unsigned char *c)
1025 Assert(PqCommReadingMsg);
1027 if (PqRecvPointer < PqRecvLength)
1029 *c = PqRecvBuffer[PqRecvPointer++];
1033 /* Put the socket into non-blocking mode */
1034 socket_set_nonblocking(true);
1036 r = secure_read(MyProcPort, c, 1);
1040 * Ok if no data available without blocking or interrupted (though
1041 * EINTR really shouldn't happen with a non-blocking socket). Report
1044 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
1049 * Careful: an ereport() that tries to write to the client would
1050 * cause recursion to here, leading to stack overflow and core
1051 * dump! This message must go *only* to the postmaster log.
1054 (errcode_for_socket_access(),
1055 errmsg("could not receive data from client: %m")));
1068 /* --------------------------------
1069 * pq_getbytes - get a known number of bytes from connection
1071 * returns 0 if OK, EOF if trouble
1072 * --------------------------------
1075 pq_getbytes(char *s, size_t len)
1079 Assert(PqCommReadingMsg);
1083 while (PqRecvPointer >= PqRecvLength)
1085 if (pq_recvbuf()) /* If nothing in buffer, then recv some */
1086 return EOF; /* Failed to recv data */
1088 amount = PqRecvLength - PqRecvPointer;
1091 memcpy(s, PqRecvBuffer + PqRecvPointer, amount);
1092 PqRecvPointer += amount;
1099 /* --------------------------------
1100 * pq_discardbytes - throw away a known number of bytes
1102 * same as pq_getbytes except we do not copy the data to anyplace.
1103 * this is used for resynchronizing after read errors.
1105 * returns 0 if OK, EOF if trouble
1106 * --------------------------------
1109 pq_discardbytes(size_t len)
1113 Assert(PqCommReadingMsg);
1117 while (PqRecvPointer >= PqRecvLength)
1119 if (pq_recvbuf()) /* If nothing in buffer, then recv some */
1120 return EOF; /* Failed to recv data */
1122 amount = PqRecvLength - PqRecvPointer;
1125 PqRecvPointer += amount;
1131 /* --------------------------------
1132 * pq_getstring - get a null terminated string from connection
1134 * The return value is placed in an expansible StringInfo, which has
1135 * already been initialized by the caller.
1137 * This is used only for dealing with old-protocol clients. The idea
1138 * is to produce a StringInfo that looks the same as we would get from
1139 * pq_getmessage() with a newer client; we will then process it with
1140 * pq_getmsgstring. Therefore, no character set conversion is done here,
1141 * even though this is presumably useful only for text.
1143 * returns 0 if OK, EOF if trouble
1144 * --------------------------------
1147 pq_getstring(StringInfo s)
1151 Assert(PqCommReadingMsg);
1155 /* Read until we get the terminating '\0' */
1158 while (PqRecvPointer >= PqRecvLength)
1160 if (pq_recvbuf()) /* If nothing in buffer, then recv some */
1161 return EOF; /* Failed to recv data */
1164 for (i = PqRecvPointer; i < PqRecvLength; i++)
1166 if (PqRecvBuffer[i] == '\0')
1168 /* include the '\0' in the copy */
1169 appendBinaryStringInfo(s, PqRecvBuffer + PqRecvPointer,
1170 i - PqRecvPointer + 1);
1171 PqRecvPointer = i + 1; /* advance past \0 */
1176 /* If we're here we haven't got the \0 in the buffer yet. */
1177 appendBinaryStringInfo(s, PqRecvBuffer + PqRecvPointer,
1178 PqRecvLength - PqRecvPointer);
1179 PqRecvPointer = PqRecvLength;
1184 /* --------------------------------
1185 * pq_startmsgread - begin reading a message from the client.
1187 * This must be called before any of the pq_get* functions.
1188 * --------------------------------
1191 pq_startmsgread(void)
1194 * There shouldn't be a read active already, but let's check just to be
1197 if (PqCommReadingMsg)
1199 (errcode(ERRCODE_PROTOCOL_VIOLATION),
1200 errmsg("terminating connection because protocol synchronization was lost")));
1202 PqCommReadingMsg = true;
1206 /* --------------------------------
1207 * pq_endmsgread - finish reading message.
1209 * This must be called after reading a V2 protocol message with
1210 * pq_getstring() and friends, to indicate that we have read the whole
1211 * message. In V3 protocol, pq_getmessage() does this implicitly.
1212 * --------------------------------
1217 Assert(PqCommReadingMsg);
1219 PqCommReadingMsg = false;
1222 /* --------------------------------
1223 * pq_is_reading_msg - are we currently reading a message?
1225 * This is used in error recovery at the outer idle loop to detect if we have
1226 * lost protocol sync, and need to terminate the connection. pq_startmsgread()
1227 * will check for that too, but it's nicer to detect it earlier.
1228 * --------------------------------
1231 pq_is_reading_msg(void)
1233 return PqCommReadingMsg;
1236 /* --------------------------------
1237 * pq_getmessage - get a message with length word from connection
1239 * The return value is placed in an expansible StringInfo, which has
1240 * already been initialized by the caller.
1241 * Only the message body is placed in the StringInfo; the length word
1242 * is removed. Also, s->cursor is initialized to zero for convenience
1243 * in scanning the message contents.
1245 * If maxlen is not zero, it is an upper limit on the length of the
1246 * message we are willing to accept. We abort the connection (by
1247 * returning EOF) if client tries to send more than that.
1249 * returns 0 if OK, EOF if trouble
1250 * --------------------------------
1253 pq_getmessage(StringInfo s, int maxlen)
1257 Assert(PqCommReadingMsg);
1261 /* Read message length word */
1262 if (pq_getbytes((char *) &len, 4) == EOF)
1265 (errcode(ERRCODE_PROTOCOL_VIOLATION),
1266 errmsg("unexpected EOF within message length word")));
1273 (maxlen > 0 && len > maxlen))
1276 (errcode(ERRCODE_PROTOCOL_VIOLATION),
1277 errmsg("invalid message length")));
1281 len -= 4; /* discount length itself */
1286 * Allocate space for message. If we run out of room (ridiculously
1287 * large message), we will elog(ERROR), but we want to discard the
1288 * message body so as not to lose communication sync.
1292 enlargeStringInfo(s, len);
1296 if (pq_discardbytes(len) == EOF)
1298 (errcode(ERRCODE_PROTOCOL_VIOLATION),
1299 errmsg("incomplete message from client")));
1301 /* we discarded the rest of the message so we're back in sync. */
1302 PqCommReadingMsg = false;
1307 /* And grab the message */
1308 if (pq_getbytes(s->data, len) == EOF)
1311 (errcode(ERRCODE_PROTOCOL_VIOLATION),
1312 errmsg("incomplete message from client")));
1316 /* Place a trailing null per StringInfo convention */
1317 s->data[len] = '\0';
1320 /* finished reading the message. */
1321 PqCommReadingMsg = false;
1327 /* --------------------------------
1328 * pq_putbytes - send bytes to connection (not flushed until pq_flush)
1330 * returns 0 if OK, EOF if trouble
1331 * --------------------------------
1334 pq_putbytes(const char *s, size_t len)
1338 /* Should only be called by old-style COPY OUT */
1339 Assert(DoingCopyOut);
1340 /* No-op if reentrant call */
1344 res = internal_putbytes(s, len);
1350 internal_putbytes(const char *s, size_t len)
1356 /* If buffer is full, then flush it out */
1357 if (PqSendPointer >= PqSendBufferSize)
1359 socket_set_nonblocking(false);
1360 if (internal_flush())
1363 amount = PqSendBufferSize - PqSendPointer;
1366 memcpy(PqSendBuffer + PqSendPointer, s, amount);
1367 PqSendPointer += amount;
1374 /* --------------------------------
1375 * socket_flush - flush pending output
1377 * returns 0 if OK, EOF if trouble
1378 * --------------------------------
1385 /* No-op if reentrant call */
1389 socket_set_nonblocking(false);
1390 res = internal_flush();
1395 /* --------------------------------
1396 * internal_flush - flush pending output
1398 * Returns 0 if OK (meaning everything was sent, or operation would block
1399 * and the socket is in non-blocking mode), or EOF if trouble.
1400 * --------------------------------
1403 internal_flush(void)
1405 static int last_reported_send_errno = 0;
1407 char *bufptr = PqSendBuffer + PqSendStart;
1408 char *bufend = PqSendBuffer + PqSendPointer;
1410 while (bufptr < bufend)
1414 r = secure_write(MyProcPort, bufptr, bufend - bufptr);
1419 continue; /* Ok if we were interrupted */
1422 * Ok if no data writable without blocking, and the socket is in
1423 * non-blocking mode.
1425 if (errno == EAGAIN ||
1426 errno == EWOULDBLOCK)
1432 * Careful: an ereport() that tries to write to the client would
1433 * cause recursion to here, leading to stack overflow and core
1434 * dump! This message must go *only* to the postmaster log.
1436 * If a client disconnects while we're in the midst of output, we
1437 * might write quite a bit of data before we get to a safe query
1438 * abort point. So, suppress duplicate log messages.
1440 if (errno != last_reported_send_errno)
1442 last_reported_send_errno = errno;
1444 (errcode_for_socket_access(),
1445 errmsg("could not send data to client: %m")));
1449 * We drop the buffered data anyway so that processing can
1450 * continue, even though we'll probably quit soon. We also set a
1451 * flag that'll cause the next CHECK_FOR_INTERRUPTS to terminate
1454 PqSendStart = PqSendPointer = 0;
1455 ClientConnectionLost = 1;
1456 InterruptPending = 1;
1460 last_reported_send_errno = 0; /* reset after any successful send */
1465 PqSendStart = PqSendPointer = 0;
1469 /* --------------------------------
1470 * pq_flush_if_writable - flush pending output if writable without blocking
1472 * Returns 0 if OK, or EOF if trouble.
1473 * --------------------------------
1476 socket_flush_if_writable(void)
1480 /* Quick exit if nothing to do */
1481 if (PqSendPointer == PqSendStart)
1484 /* No-op if reentrant call */
1488 /* Temporarily put the socket into non-blocking mode */
1489 socket_set_nonblocking(true);
1492 res = internal_flush();
1497 /* --------------------------------
1498 * socket_is_send_pending - is there any pending data in the output buffer?
1499 * --------------------------------
1502 socket_is_send_pending(void)
1504 return (PqSendStart < PqSendPointer);
1507 /* --------------------------------
1508 * Message-level I/O routines begin here.
1510 * These routines understand about the old-style COPY OUT protocol.
1511 * --------------------------------
1515 /* --------------------------------
1516 * socket_putmessage - send a normal message (suppressed in COPY OUT mode)
1518 * If msgtype is not '\0', it is a message type code to place before
1519 * the message body. If msgtype is '\0', then the message has no type
1520 * code (this is only valid in pre-3.0 protocols).
1522 * len is the length of the message body data at *s. In protocol 3.0
1523 * and later, a message length word (equal to len+4 because it counts
1524 * itself too) is inserted by this routine.
1526 * All normal messages are suppressed while old-style COPY OUT is in
1527 * progress. (In practice only a few notice messages might get emitted
1528 * then; dropping them is annoying, but at least they will still appear
1529 * in the postmaster log.)
1531 * We also suppress messages generated while pqcomm.c is busy. This
1532 * avoids any possibility of messages being inserted within other
1533 * messages. The only known trouble case arises if SIGQUIT occurs
1534 * during a pqcomm.c routine --- quickdie() will try to send a warning
1535 * message, and the most reasonable approach seems to be to drop it.
1537 * returns 0 if OK, EOF if trouble
1538 * --------------------------------
1541 socket_putmessage(char msgtype, const char *s, size_t len)
1543 if (DoingCopyOut || PqCommBusy)
1547 if (internal_putbytes(&msgtype, 1))
1549 if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3)
1553 n32 = htonl((uint32) (len + 4));
1554 if (internal_putbytes((char *) &n32, 4))
1557 if (internal_putbytes(s, len))
1567 /* --------------------------------
1568 * pq_putmessage_noblock - like pq_putmessage, but never blocks
1570 * If the output buffer is too small to hold the message, the buffer
1574 socket_putmessage_noblock(char msgtype, const char *s, size_t len)
1576 int res PG_USED_FOR_ASSERTS_ONLY;
1580 * Ensure we have enough space in the output buffer for the message header
1581 * as well as the message itself.
1583 required = PqSendPointer + 1 + 4 + len;
1584 if (required > PqSendBufferSize)
1586 PqSendBuffer = repalloc(PqSendBuffer, required);
1587 PqSendBufferSize = required;
1589 res = pq_putmessage(msgtype, s, len);
1590 Assert(res == 0); /* should not fail when the message fits in
1595 /* --------------------------------
1596 * socket_startcopyout - inform libpq that an old-style COPY OUT transfer
1598 * --------------------------------
1601 socket_startcopyout(void)
1603 DoingCopyOut = true;
1606 /* --------------------------------
1607 * socket_endcopyout - end an old-style COPY OUT transfer
1609 * If errorAbort is indicated, we are aborting a COPY OUT due to an error,
1610 * and must send a terminator line. Since a partial data line might have
1611 * been emitted, send a couple of newlines first (the first one could
1612 * get absorbed by a backslash...) Note that old-style COPY OUT does
1613 * not allow binary transfers, so a textual terminator is always correct.
1614 * --------------------------------
1617 socket_endcopyout(bool errorAbort)
1622 pq_putbytes("\n\n\\.\n", 5);
1623 /* in non-error case, copy.c will have emitted the terminator line */
1624 DoingCopyOut = false;
1628 * Support for TCP Keepalive parameters
1632 * On Windows, we need to set both idle and interval at the same time.
1633 * We also cannot reset them to the default (setting to zero will
1634 * actually set them to zero, not default), therefore we fallback to
1635 * the out-of-the-box default instead.
1637 #if defined(WIN32) && defined(SIO_KEEPALIVE_VALS)
1639 pq_setkeepaliveswin32(Port *port, int idle, int interval)
1641 struct tcp_keepalive ka;
1645 idle = 2 * 60 * 60; /* default = 2 hours */
1647 interval = 1; /* default = 1 second */
1650 ka.keepalivetime = idle * 1000;
1651 ka.keepaliveinterval = interval * 1000;
1653 if (WSAIoctl(port->sock,
1664 elog(LOG, "WSAIoctl(SIO_KEEPALIVE_VALS) failed: %ui",
1666 return STATUS_ERROR;
1668 if (port->keepalives_idle != idle)
1669 port->keepalives_idle = idle;
1670 if (port->keepalives_interval != interval)
1671 port->keepalives_interval = interval;
1677 pq_getkeepalivesidle(Port *port)
1679 #if defined(TCP_KEEPIDLE) || defined(TCP_KEEPALIVE) || defined(WIN32)
1680 if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1683 if (port->keepalives_idle != 0)
1684 return port->keepalives_idle;
1686 if (port->default_keepalives_idle == 0)
1689 ACCEPT_TYPE_ARG3 size = sizeof(port->default_keepalives_idle);
1692 if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPIDLE,
1693 (char *) &port->default_keepalives_idle,
1696 elog(LOG, "getsockopt(TCP_KEEPIDLE) failed: %m");
1697 port->default_keepalives_idle = -1; /* don't know */
1700 if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPALIVE,
1701 (char *) &port->default_keepalives_idle,
1704 elog(LOG, "getsockopt(TCP_KEEPALIVE) failed: %m");
1705 port->default_keepalives_idle = -1; /* don't know */
1707 #endif /* TCP_KEEPIDLE */
1709 /* We can't get the defaults on Windows, so return "don't know" */
1710 port->default_keepalives_idle = -1;
1714 return port->default_keepalives_idle;
1721 pq_setkeepalivesidle(int idle, Port *port)
1723 if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1726 #if defined(TCP_KEEPIDLE) || defined(TCP_KEEPALIVE) || defined(SIO_KEEPALIVE_VALS)
1727 if (idle == port->keepalives_idle)
1731 if (port->default_keepalives_idle <= 0)
1733 if (pq_getkeepalivesidle(port) < 0)
1736 return STATUS_OK; /* default is set but unknown */
1738 return STATUS_ERROR;
1743 idle = port->default_keepalives_idle;
1746 if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPIDLE,
1747 (char *) &idle, sizeof(idle)) < 0)
1749 elog(LOG, "setsockopt(TCP_KEEPIDLE) failed: %m");
1750 return STATUS_ERROR;
1753 if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPALIVE,
1754 (char *) &idle, sizeof(idle)) < 0)
1756 elog(LOG, "setsockopt(TCP_KEEPALIVE) failed: %m");
1757 return STATUS_ERROR;
1761 port->keepalives_idle = idle;
1763 return pq_setkeepaliveswin32(port, idle, port->keepalives_interval);
1765 #else /* TCP_KEEPIDLE || SIO_KEEPALIVE_VALS */
1768 elog(LOG, "setting the keepalive idle time is not supported");
1769 return STATUS_ERROR;
1776 pq_getkeepalivesinterval(Port *port)
1778 #if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
1779 if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1782 if (port->keepalives_interval != 0)
1783 return port->keepalives_interval;
1785 if (port->default_keepalives_interval == 0)
1788 ACCEPT_TYPE_ARG3 size = sizeof(port->default_keepalives_interval);
1790 if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPINTVL,
1791 (char *) &port->default_keepalives_interval,
1794 elog(LOG, "getsockopt(TCP_KEEPINTVL) failed: %m");
1795 port->default_keepalives_interval = -1; /* don't know */
1798 /* We can't get the defaults on Windows, so return "don't know" */
1799 port->default_keepalives_interval = -1;
1803 return port->default_keepalives_interval;
1810 pq_setkeepalivesinterval(int interval, Port *port)
1812 if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1815 #if defined(TCP_KEEPINTVL) || defined (SIO_KEEPALIVE_VALS)
1816 if (interval == port->keepalives_interval)
1820 if (port->default_keepalives_interval <= 0)
1822 if (pq_getkeepalivesinterval(port) < 0)
1825 return STATUS_OK; /* default is set but unknown */
1827 return STATUS_ERROR;
1832 interval = port->default_keepalives_interval;
1834 if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPINTVL,
1835 (char *) &interval, sizeof(interval)) < 0)
1837 elog(LOG, "setsockopt(TCP_KEEPINTVL) failed: %m");
1838 return STATUS_ERROR;
1841 port->keepalives_interval = interval;
1843 return pq_setkeepaliveswin32(port, port->keepalives_idle, interval);
1848 elog(LOG, "setsockopt(TCP_KEEPINTVL) not supported");
1849 return STATUS_ERROR;
1857 pq_getkeepalivescount(Port *port)
1860 if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1863 if (port->keepalives_count != 0)
1864 return port->keepalives_count;
1866 if (port->default_keepalives_count == 0)
1868 ACCEPT_TYPE_ARG3 size = sizeof(port->default_keepalives_count);
1870 if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPCNT,
1871 (char *) &port->default_keepalives_count,
1874 elog(LOG, "getsockopt(TCP_KEEPCNT) failed: %m");
1875 port->default_keepalives_count = -1; /* don't know */
1879 return port->default_keepalives_count;
1886 pq_setkeepalivescount(int count, Port *port)
1888 if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1892 if (count == port->keepalives_count)
1895 if (port->default_keepalives_count <= 0)
1897 if (pq_getkeepalivescount(port) < 0)
1900 return STATUS_OK; /* default is set but unknown */
1902 return STATUS_ERROR;
1907 count = port->default_keepalives_count;
1909 if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPCNT,
1910 (char *) &count, sizeof(count)) < 0)
1912 elog(LOG, "setsockopt(TCP_KEEPCNT) failed: %m");
1913 return STATUS_ERROR;
1916 port->keepalives_count = count;
1920 elog(LOG, "setsockopt(TCP_KEEPCNT) not supported");
1921 return STATUS_ERROR;