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-2012, 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 * TouchSocketFile - Protect socket file 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 WIN32_ONLY_COMPILER /* mstcpip.h is missing on mingw */
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;
106 /* Where the Unix socket file is */
107 static char sock_path[MAXPGPATH];
111 * Buffers for low-level I/O.
113 * The receive buffer is fixed size. Send buffer is usually 8k, but can be
114 * enlarged by pq_putmessage_noblock() if the message doesn't fit otherwise.
117 #define PQ_SEND_BUFFER_SIZE 8192
118 #define PQ_RECV_BUFFER_SIZE 8192
120 static char *PqSendBuffer;
121 static int PqSendBufferSize; /* Size send buffer */
122 static int PqSendPointer; /* Next index to store a byte in PqSendBuffer */
123 static int PqSendStart; /* Next index to send a byte in PqSendBuffer */
125 static char PqRecvBuffer[PQ_RECV_BUFFER_SIZE];
126 static int PqRecvPointer; /* Next index to read a byte from PqRecvBuffer */
127 static int PqRecvLength; /* End of data available in PqRecvBuffer */
132 static bool PqCommBusy;
133 static bool DoingCopyOut;
136 /* Internal functions */
137 static void pq_close(int code, Datum arg);
138 static int internal_putbytes(const char *s, size_t len);
139 static int internal_flush(void);
140 static void pq_set_nonblocking(bool nonblocking);
142 #ifdef HAVE_UNIX_SOCKETS
143 static int Lock_AF_UNIX(unsigned short portNumber, char *unixSocketName);
144 static int Setup_AF_UNIX(void);
145 #endif /* HAVE_UNIX_SOCKETS */
148 /* --------------------------------
149 * pq_init - initialize libpq at backend startup
150 * --------------------------------
155 PqSendBufferSize = PQ_SEND_BUFFER_SIZE;
156 PqSendBuffer = MemoryContextAlloc(TopMemoryContext, PqSendBufferSize);
157 PqSendPointer = PqSendStart = PqRecvPointer = PqRecvLength = 0;
159 DoingCopyOut = false;
160 on_proc_exit(pq_close, 0);
163 /* --------------------------------
164 * pq_comm_reset - reset libpq during error recovery
166 * This is called from error recovery at the outer idle loop. It's
167 * just to get us out of trouble if we somehow manage to elog() from
168 * inside a pqcomm.c routine (which ideally will never happen, but...)
169 * --------------------------------
174 /* Do not throw away pending data, but do reset the busy flag */
176 /* We can abort any old-style COPY OUT, too */
180 /* --------------------------------
181 * pq_close - shutdown libpq at backend exit
183 * Note: in a standalone backend MyProcPort will be null,
184 * don't crash during exit...
185 * --------------------------------
188 pq_close(int code, Datum arg)
190 if (MyProcPort != NULL)
192 #if defined(ENABLE_GSS) || defined(ENABLE_SSPI)
196 /* Shutdown GSSAPI layer */
197 if (MyProcPort->gss->ctx != GSS_C_NO_CONTEXT)
198 gss_delete_sec_context(&min_s, &MyProcPort->gss->ctx, NULL);
200 if (MyProcPort->gss->cred != GSS_C_NO_CREDENTIAL)
201 gss_release_cred(&min_s, &MyProcPort->gss->cred);
202 #endif /* ENABLE_GSS */
203 /* GSS and SSPI share the port->gss struct */
205 free(MyProcPort->gss);
206 #endif /* ENABLE_GSS || ENABLE_SSPI */
208 /* Cleanly shut down SSL layer */
209 secure_close(MyProcPort);
212 * Formerly we did an explicit close() here, but it seems better to
213 * leave the socket open until the process dies. This allows clients
214 * to perform a "synchronous close" if they care --- wait till the
215 * transport layer reports connection closure, and you can be sure the
216 * backend has exited.
218 * We do set sock to PGINVALID_SOCKET to prevent any further I/O,
221 MyProcPort->sock = PGINVALID_SOCKET;
228 * Streams -- wrapper around Unix socket system calls
231 * Stream functions are used for vanilla TCP connection protocol.
236 * Shutdown routine for backend connection
237 * If a Unix socket is used for communication, explicitly close it.
239 #ifdef HAVE_UNIX_SOCKETS
241 StreamDoUnlink(int code, Datum arg)
243 Assert(sock_path[0]);
246 #endif /* HAVE_UNIX_SOCKETS */
249 * StreamServerPort -- open a "listening" port to accept connections.
251 * Successfully opened sockets are added to the ListenSocket[] array,
252 * at the first position that isn't PGINVALID_SOCKET.
254 * RETURNS: STATUS_OK or STATUS_ERROR
258 StreamServerPort(int family, char *hostName, unsigned short portNumber,
259 char *unixSocketName,
260 pgsocket ListenSocket[], int MaxListen)
266 char portNumberStr[32];
267 const char *familyDesc;
268 char familyDescBuf[64];
270 struct addrinfo *addrs = NULL,
272 struct addrinfo hint;
273 int listen_index = 0;
276 #if !defined(WIN32) || defined(IPV6_V6ONLY)
280 /* Initialize hint structure */
281 MemSet(&hint, 0, sizeof(hint));
282 hint.ai_family = family;
283 hint.ai_flags = AI_PASSIVE;
284 hint.ai_socktype = SOCK_STREAM;
286 #ifdef HAVE_UNIX_SOCKETS
287 if (family == AF_UNIX)
289 /* Lock_AF_UNIX will also fill in sock_path. */
290 if (Lock_AF_UNIX(portNumber, unixSocketName) != STATUS_OK)
295 #endif /* HAVE_UNIX_SOCKETS */
297 snprintf(portNumberStr, sizeof(portNumberStr), "%d", portNumber);
298 service = portNumberStr;
301 ret = pg_getaddrinfo_all(hostName, service, &hint, &addrs);
306 (errmsg("could not translate host name \"%s\", service \"%s\" to address: %s",
307 hostName, service, gai_strerror(ret))));
310 (errmsg("could not translate service \"%s\" to address: %s",
311 service, gai_strerror(ret))));
313 pg_freeaddrinfo_all(hint.ai_family, addrs);
317 for (addr = addrs; addr; addr = addr->ai_next)
319 if (!IS_AF_UNIX(family) && IS_AF_UNIX(addr->ai_family))
322 * Only set up a unix domain socket when they really asked for it.
323 * The service/port is different in that case.
328 /* See if there is still room to add 1 more socket. */
329 for (; listen_index < MaxListen; listen_index++)
331 if (ListenSocket[listen_index] == PGINVALID_SOCKET)
334 if (listen_index >= MaxListen)
337 (errmsg("could not bind to all requested addresses: MAXLISTEN (%d) exceeded",
342 /* set up family name for possible error messages */
343 switch (addr->ai_family)
346 familyDesc = _("IPv4");
350 familyDesc = _("IPv6");
353 #ifdef HAVE_UNIX_SOCKETS
355 familyDesc = _("Unix");
359 snprintf(familyDescBuf, sizeof(familyDescBuf),
360 _("unrecognized address family %d"),
362 familyDesc = familyDescBuf;
366 if ((fd = socket(addr->ai_family, SOCK_STREAM, 0)) < 0)
369 (errcode_for_socket_access(),
370 /* translator: %s is IPv4, IPv6, or Unix */
371 errmsg("could not create %s socket: %m",
379 * Without the SO_REUSEADDR flag, a new postmaster can't be started
380 * right away after a stop or crash, giving "address already in use"
381 * error on TCP ports.
383 * On win32, however, this behavior only happens if the
384 * SO_EXLUSIVEADDRUSE is set. With SO_REUSEADDR, win32 allows multiple
385 * servers to listen on the same address, resulting in unpredictable
386 * behavior. With no flags at all, win32 behaves as Unix with
389 if (!IS_AF_UNIX(addr->ai_family))
391 if ((setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
392 (char *) &one, sizeof(one))) == -1)
395 (errcode_for_socket_access(),
396 errmsg("setsockopt(SO_REUSEADDR) failed: %m")));
404 if (addr->ai_family == AF_INET6)
406 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY,
407 (char *) &one, sizeof(one)) == -1)
410 (errcode_for_socket_access(),
411 errmsg("setsockopt(IPV6_V6ONLY) failed: %m")));
419 * Note: This might fail on some OS's, like Linux older than
420 * 2.4.21-pre3, that don't have the IPV6_V6ONLY socket option, and map
421 * ipv4 addresses to ipv6. It will show ::ffff:ipv4 for all ipv4
424 err = bind(fd, addr->ai_addr, addr->ai_addrlen);
428 (errcode_for_socket_access(),
429 /* translator: %s is IPv4, IPv6, or Unix */
430 errmsg("could not bind %s socket: %m",
432 (IS_AF_UNIX(addr->ai_family)) ?
433 errhint("Is another postmaster already running on port %d?"
434 " If not, remove socket file \"%s\" and retry.",
435 (int) portNumber, sock_path) :
436 errhint("Is another postmaster already running on port %d?"
437 " If not, wait a few seconds and retry.",
443 #ifdef HAVE_UNIX_SOCKETS
444 if (addr->ai_family == AF_UNIX)
446 if (Setup_AF_UNIX() != STATUS_OK)
455 * Select appropriate accept-queue length limit. PG_SOMAXCONN is only
456 * intended to provide a clamp on the request on platforms where an
457 * overly large request provokes a kernel error (are there any?).
459 maxconn = MaxBackends * 2;
460 if (maxconn > PG_SOMAXCONN)
461 maxconn = PG_SOMAXCONN;
463 err = listen(fd, maxconn);
467 (errcode_for_socket_access(),
468 /* translator: %s is IPv4, IPv6, or Unix */
469 errmsg("could not listen on %s socket: %m",
474 ListenSocket[listen_index] = fd;
478 pg_freeaddrinfo_all(hint.ai_family, addrs);
487 #ifdef HAVE_UNIX_SOCKETS
490 * Lock_AF_UNIX -- configure unix socket file path
493 Lock_AF_UNIX(unsigned short portNumber, char *unixSocketName)
495 UNIXSOCK_PATH(sock_path, portNumber, unixSocketName);
498 * Grab an interlock file associated with the socket file.
500 * Note: there are two reasons for using a socket lock file, rather than
501 * trying to interlock directly on the socket itself. First, it's a lot
502 * more portable, and second, it lets us remove any pre-existing socket
503 * file without race conditions.
505 CreateSocketLockFile(sock_path, true);
508 * Once we have the interlock, we can safely delete any pre-existing
509 * socket file to avoid failure at bind() time.
518 * Setup_AF_UNIX -- configure unix socket permissions
523 /* Arrange to unlink the socket file at exit */
524 on_proc_exit(StreamDoUnlink, 0);
527 * Fix socket ownership/permission if requested. Note we must do this
528 * before we listen() to avoid a window where unwanted connections could
531 Assert(Unix_socket_group);
532 if (Unix_socket_group[0] != '\0')
535 elog(WARNING, "configuration item unix_socket_group is not supported on this platform");
541 val = strtoul(Unix_socket_group, &endptr, 10);
543 { /* numeric group id */
547 { /* convert group name to id */
550 gr = getgrnam(Unix_socket_group);
554 (errmsg("group \"%s\" does not exist",
555 Unix_socket_group)));
560 if (chown(sock_path, -1, gid) == -1)
563 (errcode_for_file_access(),
564 errmsg("could not set group of file \"%s\": %m",
571 if (chmod(sock_path, Unix_socket_permissions) == -1)
574 (errcode_for_file_access(),
575 errmsg("could not set permissions of file \"%s\": %m",
581 #endif /* HAVE_UNIX_SOCKETS */
585 * StreamConnection -- create a new connection with client using
586 * server port. Set port->sock to the FD of the new connection.
588 * ASSUME: that this doesn't need to be non-blocking because
589 * the Postmaster uses select() to tell when the server master
590 * socket is ready for accept().
592 * RETURNS: STATUS_OK or STATUS_ERROR
595 StreamConnection(pgsocket server_fd, Port *port)
597 /* accept connection and fill in the client (remote) address */
598 port->raddr.salen = sizeof(port->raddr.addr);
599 if ((port->sock = accept(server_fd,
600 (struct sockaddr *) & port->raddr.addr,
601 &port->raddr.salen)) < 0)
604 (errcode_for_socket_access(),
605 errmsg("could not accept new connection: %m")));
608 * If accept() fails then postmaster.c will still see the server
609 * socket as read-ready, and will immediately try again. To avoid
610 * uselessly sucking lots of CPU, delay a bit before trying again.
611 * (The most likely reason for failure is being out of kernel file
612 * table slots; we can do little except hope some will get freed up.)
614 pg_usleep(100000L); /* wait 0.1 sec */
618 #ifdef SCO_ACCEPT_BUG
621 * UnixWare 7+ and OpenServer 5.0.4 are known to have this bug, but it
622 * shouldn't hurt to catch it for all versions of those platforms.
624 if (port->raddr.addr.ss_family == 0)
625 port->raddr.addr.ss_family = AF_UNIX;
628 /* fill in the server (local) address */
629 port->laddr.salen = sizeof(port->laddr.addr);
630 if (getsockname(port->sock,
631 (struct sockaddr *) & port->laddr.addr,
632 &port->laddr.salen) < 0)
634 elog(LOG, "getsockname() failed: %m");
638 /* select NODELAY and KEEPALIVE options if it's a TCP connection */
639 if (!IS_AF_UNIX(port->laddr.addr.ss_family))
645 if (setsockopt(port->sock, IPPROTO_TCP, TCP_NODELAY,
646 (char *) &on, sizeof(on)) < 0)
648 elog(LOG, "setsockopt(TCP_NODELAY) failed: %m");
653 if (setsockopt(port->sock, SOL_SOCKET, SO_KEEPALIVE,
654 (char *) &on, sizeof(on)) < 0)
656 elog(LOG, "setsockopt(SO_KEEPALIVE) failed: %m");
663 * This is a Win32 socket optimization. The ideal size is 32k.
664 * http://support.microsoft.com/kb/823764/EN-US/
666 on = PQ_SEND_BUFFER_SIZE * 4;
667 if (setsockopt(port->sock, SOL_SOCKET, SO_SNDBUF, (char *) &on,
670 elog(LOG, "setsockopt(SO_SNDBUF) failed: %m");
676 * Also apply the current keepalive parameters. If we fail to set a
677 * parameter, don't error out, because these aren't universally
678 * supported. (Note: you might think we need to reset the GUC
679 * variables to 0 in such a case, but it's not necessary because the
680 * show hooks for these variables report the truth anyway.)
682 (void) pq_setkeepalivesidle(tcp_keepalives_idle, port);
683 (void) pq_setkeepalivesinterval(tcp_keepalives_interval, port);
684 (void) pq_setkeepalivescount(tcp_keepalives_count, port);
691 * StreamClose -- close a client/backend connection
693 * NOTE: this is NOT used to terminate a session; it is just used to release
694 * the file descriptor in a process that should no longer have the socket
695 * open. (For example, the postmaster calls this after passing ownership
696 * of the connection to a child process.) It is expected that someone else
697 * still has the socket open. So, we only want to close the descriptor,
698 * we do NOT want to send anything to the far end.
701 StreamClose(pgsocket sock)
707 * TouchSocketFile -- mark socket file as recently accessed
709 * This routine should be called every so often to ensure that the socket
710 * file has a recent mod date (ordinary operations on sockets usually won't
711 * change the mod date). That saves it from being removed by
712 * overenthusiastic /tmp-directory-cleaner daemons. (Another reason we should
713 * never have put the socket file in /tmp...)
716 TouchSocketFile(void)
718 /* Do nothing if we did not create a socket... */
719 if (sock_path[0] != '\0')
722 * utime() is POSIX standard, utimes() is a common alternative. If we
723 * have neither, there's no way to affect the mod or access time of
726 * In either path, we ignore errors; there's no point in complaining.
729 utime(sock_path, NULL);
730 #else /* !HAVE_UTIME */
732 utimes(sock_path, NULL);
733 #endif /* HAVE_UTIMES */
734 #endif /* HAVE_UTIME */
739 /* --------------------------------
740 * Low-level I/O routines begin here.
742 * These routines communicate with a frontend client across a connection
743 * already established by the preceding routines.
744 * --------------------------------
747 /* --------------------------------
748 * pq_set_nonblocking - set socket blocking/non-blocking
750 * Sets the socket non-blocking if nonblocking is TRUE, or sets it
751 * blocking otherwise.
752 * --------------------------------
755 pq_set_nonblocking(bool nonblocking)
757 if (MyProcPort->noblock == nonblocking)
761 pgwin32_noblock = nonblocking ? 1 : 0;
765 * Use COMMERROR on failure, because ERROR would try to send the error to
766 * the client, which might require changing the mode again, leading to
767 * infinite recursion.
771 if (!pg_set_noblock(MyProcPort->sock))
773 (errmsg("could not set socket to non-blocking mode: %m")));
777 if (!pg_set_block(MyProcPort->sock))
779 (errmsg("could not set socket to blocking mode: %m")));
782 MyProcPort->noblock = nonblocking;
785 /* --------------------------------
786 * pq_recvbuf - load some bytes into the input buffer
788 * returns 0 if OK, EOF if trouble
789 * --------------------------------
794 if (PqRecvPointer > 0)
796 if (PqRecvLength > PqRecvPointer)
798 /* still some unread data, left-justify it in the buffer */
799 memmove(PqRecvBuffer, PqRecvBuffer + PqRecvPointer,
800 PqRecvLength - PqRecvPointer);
801 PqRecvLength -= PqRecvPointer;
805 PqRecvLength = PqRecvPointer = 0;
808 /* Ensure that we're in blocking mode */
809 pq_set_nonblocking(false);
811 /* Can fill buffer from PqRecvLength and upwards */
816 r = secure_read(MyProcPort, PqRecvBuffer + PqRecvLength,
817 PQ_RECV_BUFFER_SIZE - PqRecvLength);
822 continue; /* Ok if interrupted */
825 * Careful: an ereport() that tries to write to the client would
826 * cause recursion to here, leading to stack overflow and core
827 * dump! This message must go *only* to the postmaster log.
830 (errcode_for_socket_access(),
831 errmsg("could not receive data from client: %m")));
837 * EOF detected. We used to write a log message here, but it's
838 * better to expect the ultimate caller to do that.
842 /* r contains number of bytes read, so just incr length */
848 /* --------------------------------
849 * pq_getbyte - get a single byte from connection, or return EOF
850 * --------------------------------
855 while (PqRecvPointer >= PqRecvLength)
857 if (pq_recvbuf()) /* If nothing in buffer, then recv some */
858 return EOF; /* Failed to recv data */
860 return (unsigned char) PqRecvBuffer[PqRecvPointer++];
863 /* --------------------------------
864 * pq_peekbyte - peek at next byte from connection
866 * Same as pq_getbyte() except we don't advance the pointer.
867 * --------------------------------
872 while (PqRecvPointer >= PqRecvLength)
874 if (pq_recvbuf()) /* If nothing in buffer, then recv some */
875 return EOF; /* Failed to recv data */
877 return (unsigned char) PqRecvBuffer[PqRecvPointer];
880 /* --------------------------------
881 * pq_getbyte_if_available - get a single byte from connection,
884 * The received byte is stored in *c. Returns 1 if a byte was read,
885 * 0 if no data was available, or EOF if trouble.
886 * --------------------------------
889 pq_getbyte_if_available(unsigned char *c)
893 if (PqRecvPointer < PqRecvLength)
895 *c = PqRecvBuffer[PqRecvPointer++];
899 /* Put the socket into non-blocking mode */
900 pq_set_nonblocking(true);
902 r = secure_read(MyProcPort, c, 1);
906 * Ok if no data available without blocking or interrupted (though
907 * EINTR really shouldn't happen with a non-blocking socket). Report
910 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
915 * Careful: an ereport() that tries to write to the client would
916 * cause recursion to here, leading to stack overflow and core
917 * dump! This message must go *only* to the postmaster log.
920 (errcode_for_socket_access(),
921 errmsg("could not receive data from client: %m")));
934 /* --------------------------------
935 * pq_getbytes - get a known number of bytes from connection
937 * returns 0 if OK, EOF if trouble
938 * --------------------------------
941 pq_getbytes(char *s, size_t len)
947 while (PqRecvPointer >= PqRecvLength)
949 if (pq_recvbuf()) /* If nothing in buffer, then recv some */
950 return EOF; /* Failed to recv data */
952 amount = PqRecvLength - PqRecvPointer;
955 memcpy(s, PqRecvBuffer + PqRecvPointer, amount);
956 PqRecvPointer += amount;
963 /* --------------------------------
964 * pq_discardbytes - throw away a known number of bytes
966 * same as pq_getbytes except we do not copy the data to anyplace.
967 * this is used for resynchronizing after read errors.
969 * returns 0 if OK, EOF if trouble
970 * --------------------------------
973 pq_discardbytes(size_t len)
979 while (PqRecvPointer >= PqRecvLength)
981 if (pq_recvbuf()) /* If nothing in buffer, then recv some */
982 return EOF; /* Failed to recv data */
984 amount = PqRecvLength - PqRecvPointer;
987 PqRecvPointer += amount;
993 /* --------------------------------
994 * pq_getstring - get a null terminated string from connection
996 * The return value is placed in an expansible StringInfo, which has
997 * already been initialized by the caller.
999 * This is used only for dealing with old-protocol clients. The idea
1000 * is to produce a StringInfo that looks the same as we would get from
1001 * pq_getmessage() with a newer client; we will then process it with
1002 * pq_getmsgstring. Therefore, no character set conversion is done here,
1003 * even though this is presumably useful only for text.
1005 * returns 0 if OK, EOF if trouble
1006 * --------------------------------
1009 pq_getstring(StringInfo s)
1015 /* Read until we get the terminating '\0' */
1018 while (PqRecvPointer >= PqRecvLength)
1020 if (pq_recvbuf()) /* If nothing in buffer, then recv some */
1021 return EOF; /* Failed to recv data */
1024 for (i = PqRecvPointer; i < PqRecvLength; i++)
1026 if (PqRecvBuffer[i] == '\0')
1028 /* include the '\0' in the copy */
1029 appendBinaryStringInfo(s, PqRecvBuffer + PqRecvPointer,
1030 i - PqRecvPointer + 1);
1031 PqRecvPointer = i + 1; /* advance past \0 */
1036 /* If we're here we haven't got the \0 in the buffer yet. */
1037 appendBinaryStringInfo(s, PqRecvBuffer + PqRecvPointer,
1038 PqRecvLength - PqRecvPointer);
1039 PqRecvPointer = PqRecvLength;
1044 /* --------------------------------
1045 * pq_getmessage - get a message with length word from connection
1047 * The return value is placed in an expansible StringInfo, which has
1048 * already been initialized by the caller.
1049 * Only the message body is placed in the StringInfo; the length word
1050 * is removed. Also, s->cursor is initialized to zero for convenience
1051 * in scanning the message contents.
1053 * If maxlen is not zero, it is an upper limit on the length of the
1054 * message we are willing to accept. We abort the connection (by
1055 * returning EOF) if client tries to send more than that.
1057 * returns 0 if OK, EOF if trouble
1058 * --------------------------------
1061 pq_getmessage(StringInfo s, int maxlen)
1067 /* Read message length word */
1068 if (pq_getbytes((char *) &len, 4) == EOF)
1071 (errcode(ERRCODE_PROTOCOL_VIOLATION),
1072 errmsg("unexpected EOF within message length word")));
1079 (maxlen > 0 && len > maxlen))
1082 (errcode(ERRCODE_PROTOCOL_VIOLATION),
1083 errmsg("invalid message length")));
1087 len -= 4; /* discount length itself */
1092 * Allocate space for message. If we run out of room (ridiculously
1093 * large message), we will elog(ERROR), but we want to discard the
1094 * message body so as not to lose communication sync.
1098 enlargeStringInfo(s, len);
1102 if (pq_discardbytes(len) == EOF)
1104 (errcode(ERRCODE_PROTOCOL_VIOLATION),
1105 errmsg("incomplete message from client")));
1110 /* And grab the message */
1111 if (pq_getbytes(s->data, len) == EOF)
1114 (errcode(ERRCODE_PROTOCOL_VIOLATION),
1115 errmsg("incomplete message from client")));
1119 /* Place a trailing null per StringInfo convention */
1120 s->data[len] = '\0';
1127 /* --------------------------------
1128 * pq_putbytes - send bytes to connection (not flushed until pq_flush)
1130 * returns 0 if OK, EOF if trouble
1131 * --------------------------------
1134 pq_putbytes(const char *s, size_t len)
1138 /* Should only be called by old-style COPY OUT */
1139 Assert(DoingCopyOut);
1140 /* No-op if reentrant call */
1144 res = internal_putbytes(s, len);
1150 internal_putbytes(const char *s, size_t len)
1156 /* If buffer is full, then flush it out */
1157 if (PqSendPointer >= PqSendBufferSize)
1159 pq_set_nonblocking(false);
1160 if (internal_flush())
1163 amount = PqSendBufferSize - PqSendPointer;
1166 memcpy(PqSendBuffer + PqSendPointer, s, amount);
1167 PqSendPointer += amount;
1174 /* --------------------------------
1175 * pq_flush - flush pending output
1177 * returns 0 if OK, EOF if trouble
1178 * --------------------------------
1185 /* No-op if reentrant call */
1189 pq_set_nonblocking(false);
1190 res = internal_flush();
1195 /* --------------------------------
1196 * internal_flush - flush pending output
1198 * Returns 0 if OK (meaning everything was sent, or operation would block
1199 * and the socket is in non-blocking mode), or EOF if trouble.
1200 * --------------------------------
1203 internal_flush(void)
1205 static int last_reported_send_errno = 0;
1207 char *bufptr = PqSendBuffer + PqSendStart;
1208 char *bufend = PqSendBuffer + PqSendPointer;
1210 while (bufptr < bufend)
1214 r = secure_write(MyProcPort, bufptr, bufend - bufptr);
1219 continue; /* Ok if we were interrupted */
1222 * Ok if no data writable without blocking, and the socket is in
1223 * non-blocking mode.
1225 if (errno == EAGAIN ||
1226 errno == EWOULDBLOCK)
1232 * Careful: an ereport() that tries to write to the client would
1233 * cause recursion to here, leading to stack overflow and core
1234 * dump! This message must go *only* to the postmaster log.
1236 * If a client disconnects while we're in the midst of output, we
1237 * might write quite a bit of data before we get to a safe query
1238 * abort point. So, suppress duplicate log messages.
1240 if (errno != last_reported_send_errno)
1242 last_reported_send_errno = errno;
1244 (errcode_for_socket_access(),
1245 errmsg("could not send data to client: %m")));
1249 * We drop the buffered data anyway so that processing can
1250 * continue, even though we'll probably quit soon. We also set a
1251 * flag that'll cause the next CHECK_FOR_INTERRUPTS to terminate
1254 PqSendStart = PqSendPointer = 0;
1255 ClientConnectionLost = 1;
1256 InterruptPending = 1;
1260 last_reported_send_errno = 0; /* reset after any successful send */
1265 PqSendStart = PqSendPointer = 0;
1269 /* --------------------------------
1270 * pq_flush_if_writable - flush pending output if writable without blocking
1272 * Returns 0 if OK, or EOF if trouble.
1273 * --------------------------------
1276 pq_flush_if_writable(void)
1280 /* Quick exit if nothing to do */
1281 if (PqSendPointer == PqSendStart)
1284 /* No-op if reentrant call */
1288 /* Temporarily put the socket into non-blocking mode */
1289 pq_set_nonblocking(true);
1292 res = internal_flush();
1297 /* --------------------------------
1298 * pq_is_send_pending - is there any pending data in the output buffer?
1299 * --------------------------------
1302 pq_is_send_pending(void)
1304 return (PqSendStart < PqSendPointer);
1307 /* --------------------------------
1308 * Message-level I/O routines begin here.
1310 * These routines understand about the old-style COPY OUT protocol.
1311 * --------------------------------
1315 /* --------------------------------
1316 * pq_putmessage - send a normal message (suppressed in COPY OUT mode)
1318 * If msgtype is not '\0', it is a message type code to place before
1319 * the message body. If msgtype is '\0', then the message has no type
1320 * code (this is only valid in pre-3.0 protocols).
1322 * len is the length of the message body data at *s. In protocol 3.0
1323 * and later, a message length word (equal to len+4 because it counts
1324 * itself too) is inserted by this routine.
1326 * All normal messages are suppressed while old-style COPY OUT is in
1327 * progress. (In practice only a few notice messages might get emitted
1328 * then; dropping them is annoying, but at least they will still appear
1329 * in the postmaster log.)
1331 * We also suppress messages generated while pqcomm.c is busy. This
1332 * avoids any possibility of messages being inserted within other
1333 * messages. The only known trouble case arises if SIGQUIT occurs
1334 * during a pqcomm.c routine --- quickdie() will try to send a warning
1335 * message, and the most reasonable approach seems to be to drop it.
1337 * returns 0 if OK, EOF if trouble
1338 * --------------------------------
1341 pq_putmessage(char msgtype, const char *s, size_t len)
1343 if (DoingCopyOut || PqCommBusy)
1347 if (internal_putbytes(&msgtype, 1))
1349 if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3)
1353 n32 = htonl((uint32) (len + 4));
1354 if (internal_putbytes((char *) &n32, 4))
1357 if (internal_putbytes(s, len))
1367 /* --------------------------------
1368 * pq_putmessage_noblock - like pq_putmessage, but never blocks
1370 * If the output buffer is too small to hold the message, the buffer
1374 pq_putmessage_noblock(char msgtype, const char *s, size_t len)
1376 int res PG_USED_FOR_ASSERTS_ONLY;
1380 * Ensure we have enough space in the output buffer for the message header
1381 * as well as the message itself.
1383 required = PqSendPointer + 1 + 4 + len;
1384 if (required > PqSendBufferSize)
1386 PqSendBuffer = repalloc(PqSendBuffer, required);
1387 PqSendBufferSize = required;
1389 res = pq_putmessage(msgtype, s, len);
1390 Assert(res == 0); /* should not fail when the message fits in
1395 /* --------------------------------
1396 * pq_startcopyout - inform libpq that an old-style COPY OUT transfer
1398 * --------------------------------
1401 pq_startcopyout(void)
1403 DoingCopyOut = true;
1406 /* --------------------------------
1407 * pq_endcopyout - end an old-style COPY OUT transfer
1409 * If errorAbort is indicated, we are aborting a COPY OUT due to an error,
1410 * and must send a terminator line. Since a partial data line might have
1411 * been emitted, send a couple of newlines first (the first one could
1412 * get absorbed by a backslash...) Note that old-style COPY OUT does
1413 * not allow binary transfers, so a textual terminator is always correct.
1414 * --------------------------------
1417 pq_endcopyout(bool errorAbort)
1422 pq_putbytes("\n\n\\.\n", 5);
1423 /* in non-error case, copy.c will have emitted the terminator line */
1424 DoingCopyOut = false;
1429 * Support for TCP Keepalive parameters
1433 * On Windows, we need to set both idle and interval at the same time.
1434 * We also cannot reset them to the default (setting to zero will
1435 * actually set them to zero, not default), therefor we fallback to
1436 * the out-of-the-box default instead.
1438 #if defined(WIN32) && defined(SIO_KEEPALIVE_VALS)
1440 pq_setkeepaliveswin32(Port *port, int idle, int interval)
1442 struct tcp_keepalive ka;
1446 idle = 2 * 60 * 60; /* default = 2 hours */
1448 interval = 1; /* default = 1 second */
1451 ka.keepalivetime = idle * 1000;
1452 ka.keepaliveinterval = interval * 1000;
1454 if (WSAIoctl(port->sock,
1465 elog(LOG, "WSAIoctl(SIO_KEEPALIVE_VALS) failed: %ui",
1467 return STATUS_ERROR;
1469 if (port->keepalives_idle != idle)
1470 port->keepalives_idle = idle;
1471 if (port->keepalives_interval != interval)
1472 port->keepalives_interval = interval;
1478 pq_getkeepalivesidle(Port *port)
1480 #if defined(TCP_KEEPIDLE) || defined(TCP_KEEPALIVE) || defined(WIN32)
1481 if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1484 if (port->keepalives_idle != 0)
1485 return port->keepalives_idle;
1487 if (port->default_keepalives_idle == 0)
1490 ACCEPT_TYPE_ARG3 size = sizeof(port->default_keepalives_idle);
1493 if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPIDLE,
1494 (char *) &port->default_keepalives_idle,
1497 elog(LOG, "getsockopt(TCP_KEEPIDLE) failed: %m");
1498 port->default_keepalives_idle = -1; /* don't know */
1501 if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPALIVE,
1502 (char *) &port->default_keepalives_idle,
1505 elog(LOG, "getsockopt(TCP_KEEPALIVE) failed: %m");
1506 port->default_keepalives_idle = -1; /* don't know */
1508 #endif /* TCP_KEEPIDLE */
1510 /* We can't get the defaults on Windows, so return "don't know" */
1511 port->default_keepalives_idle = -1;
1515 return port->default_keepalives_idle;
1522 pq_setkeepalivesidle(int idle, Port *port)
1524 if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1527 #if defined(TCP_KEEPIDLE) || defined(TCP_KEEPALIVE) || defined(SIO_KEEPALIVE_VALS)
1528 if (idle == port->keepalives_idle)
1532 if (port->default_keepalives_idle <= 0)
1534 if (pq_getkeepalivesidle(port) < 0)
1537 return STATUS_OK; /* default is set but unknown */
1539 return STATUS_ERROR;
1544 idle = port->default_keepalives_idle;
1547 if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPIDLE,
1548 (char *) &idle, sizeof(idle)) < 0)
1550 elog(LOG, "setsockopt(TCP_KEEPIDLE) failed: %m");
1551 return STATUS_ERROR;
1554 if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPALIVE,
1555 (char *) &idle, sizeof(idle)) < 0)
1557 elog(LOG, "setsockopt(TCP_KEEPALIVE) failed: %m");
1558 return STATUS_ERROR;
1562 port->keepalives_idle = idle;
1564 return pq_setkeepaliveswin32(port, idle, port->keepalives_interval);
1566 #else /* TCP_KEEPIDLE || SIO_KEEPALIVE_VALS */
1569 elog(LOG, "setting the keepalive idle time is not supported");
1570 return STATUS_ERROR;
1577 pq_getkeepalivesinterval(Port *port)
1579 #if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
1580 if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1583 if (port->keepalives_interval != 0)
1584 return port->keepalives_interval;
1586 if (port->default_keepalives_interval == 0)
1589 ACCEPT_TYPE_ARG3 size = sizeof(port->default_keepalives_interval);
1591 if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPINTVL,
1592 (char *) &port->default_keepalives_interval,
1595 elog(LOG, "getsockopt(TCP_KEEPINTVL) failed: %m");
1596 port->default_keepalives_interval = -1; /* don't know */
1599 /* We can't get the defaults on Windows, so return "don't know" */
1600 port->default_keepalives_interval = -1;
1604 return port->default_keepalives_interval;
1611 pq_setkeepalivesinterval(int interval, Port *port)
1613 if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1616 #if defined(TCP_KEEPINTVL) || defined (SIO_KEEPALIVE_VALS)
1617 if (interval == port->keepalives_interval)
1621 if (port->default_keepalives_interval <= 0)
1623 if (pq_getkeepalivesinterval(port) < 0)
1626 return STATUS_OK; /* default is set but unknown */
1628 return STATUS_ERROR;
1633 interval = port->default_keepalives_interval;
1635 if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPINTVL,
1636 (char *) &interval, sizeof(interval)) < 0)
1638 elog(LOG, "setsockopt(TCP_KEEPINTVL) failed: %m");
1639 return STATUS_ERROR;
1642 port->keepalives_interval = interval;
1644 return pq_setkeepaliveswin32(port, port->keepalives_idle, interval);
1649 elog(LOG, "setsockopt(TCP_KEEPINTVL) not supported");
1650 return STATUS_ERROR;
1658 pq_getkeepalivescount(Port *port)
1661 if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1664 if (port->keepalives_count != 0)
1665 return port->keepalives_count;
1667 if (port->default_keepalives_count == 0)
1669 ACCEPT_TYPE_ARG3 size = sizeof(port->default_keepalives_count);
1671 if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPCNT,
1672 (char *) &port->default_keepalives_count,
1675 elog(LOG, "getsockopt(TCP_KEEPCNT) failed: %m");
1676 port->default_keepalives_count = -1; /* don't know */
1680 return port->default_keepalives_count;
1687 pq_setkeepalivescount(int count, Port *port)
1689 if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1693 if (count == port->keepalives_count)
1696 if (port->default_keepalives_count <= 0)
1698 if (pq_getkeepalivescount(port) < 0)
1701 return STATUS_OK; /* default is set but unknown */
1703 return STATUS_ERROR;
1708 count = port->default_keepalives_count;
1710 if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPCNT,
1711 (char *) &count, sizeof(count)) < 0)
1713 elog(LOG, "setsockopt(TCP_KEEPCNT) failed: %m");
1714 return STATUS_ERROR;
1717 port->keepalives_count = count;
1721 elog(LOG, "setsockopt(TCP_KEEPCNT) not supported");
1722 return STATUS_ERROR;