Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * pqcomm.c
4 : : * Communication functions between the Frontend and the Backend
5 : : *
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.
9 : : *
10 : : * To emit an outgoing message, use the routines in pqformat.c to construct
11 : : * the message in a buffer and then emit it in one call to pq_putmessage.
12 : : * There are no functions to send raw bytes or partial messages; this
13 : : * ensures that the channel will not be clogged by an incomplete message if
14 : : * execution is aborted by ereport(ERROR) partway through the message.
15 : : *
16 : : * At one time, libpq was shared between frontend and backend, but now
17 : : * the backend's "backend/libpq" is quite separate from "interfaces/libpq".
18 : : * All that remains is similarities of names to trap the unwary...
19 : : *
20 : : * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
21 : : * Portions Copyright (c) 1994, Regents of the University of California
22 : : *
23 : : * src/backend/libpq/pqcomm.c
24 : : *
25 : : *-------------------------------------------------------------------------
26 : : */
27 : :
28 : : /*------------------------
29 : : * INTERFACE ROUTINES
30 : : *
31 : : * setup/teardown:
32 : : * ListenServerPort - Open postmaster's server port
33 : : * AcceptConnection - Accept new connection with client
34 : : * TouchSocketFiles - Protect socket files against /tmp cleaners
35 : : * pq_init - initialize libpq at backend startup
36 : : * socket_comm_reset - reset libpq during error recovery
37 : : * socket_close - shutdown libpq at backend exit
38 : : *
39 : : * low-level I/O:
40 : : * pq_getbytes - get a known number of bytes from connection
41 : : * pq_getmessage - get a message with length word from connection
42 : : * pq_getbyte - get next byte from connection
43 : : * pq_peekbyte - peek at next byte from connection
44 : : * pq_flush - flush pending output
45 : : * pq_flush_if_writable - flush pending output if writable without blocking
46 : : * pq_getbyte_if_available - get a byte if available without blocking
47 : : *
48 : : * message-level I/O
49 : : * pq_putmessage - send a normal message (suppressed in COPY OUT mode)
50 : : * pq_putmessage_noblock - buffer a normal message (suppressed in COPY OUT)
51 : : *
52 : : *------------------------
53 : : */
54 : : #include "postgres.h"
55 : :
56 : : #ifdef HAVE_POLL_H
57 : : #include <poll.h>
58 : : #endif
59 : : #include <signal.h>
60 : : #include <fcntl.h>
61 : : #include <grp.h>
62 : : #include <unistd.h>
63 : : #include <sys/file.h>
64 : : #include <sys/socket.h>
65 : : #include <sys/stat.h>
66 : : #include <sys/time.h>
67 : : #include <netdb.h>
68 : : #include <netinet/in.h>
69 : : #include <netinet/tcp.h>
70 : : #include <utime.h>
71 : : #ifdef WIN32
72 : : #include <mstcpip.h>
73 : : #endif
74 : :
75 : : #include "common/ip.h"
76 : : #include "libpq/libpq.h"
77 : : #include "miscadmin.h"
78 : : #include "port/pg_bswap.h"
79 : : #include "postmaster/postmaster.h"
80 : : #include "storage/ipc.h"
81 : : #include "utils/guc_hooks.h"
82 : : #include "utils/memutils.h"
83 : :
84 : : /*
85 : : * Cope with the various platform-specific ways to spell TCP keepalive socket
86 : : * options. This doesn't cover Windows, which as usual does its own thing.
87 : : */
88 : : #if defined(TCP_KEEPIDLE)
89 : : /* TCP_KEEPIDLE is the name of this option on Linux and *BSD */
90 : : #define PG_TCP_KEEPALIVE_IDLE TCP_KEEPIDLE
91 : : #define PG_TCP_KEEPALIVE_IDLE_STR "TCP_KEEPIDLE"
92 : : #elif defined(TCP_KEEPALIVE_THRESHOLD)
93 : : /* TCP_KEEPALIVE_THRESHOLD is the name of this option on Solaris >= 11 */
94 : : #define PG_TCP_KEEPALIVE_IDLE TCP_KEEPALIVE_THRESHOLD
95 : : #define PG_TCP_KEEPALIVE_IDLE_STR "TCP_KEEPALIVE_THRESHOLD"
96 : : #elif defined(TCP_KEEPALIVE) && defined(__darwin__)
97 : : /* TCP_KEEPALIVE is the name of this option on macOS */
98 : : /* Caution: Solaris has this symbol but it means something different */
99 : : #define PG_TCP_KEEPALIVE_IDLE TCP_KEEPALIVE
100 : : #define PG_TCP_KEEPALIVE_IDLE_STR "TCP_KEEPALIVE"
101 : : #endif
102 : :
103 : : /*
104 : : * Configuration options
105 : : */
106 : : int Unix_socket_permissions;
107 : : char *Unix_socket_group;
108 : :
109 : : /* Where the Unix socket files are (list of palloc'd strings) */
110 : : static List *sock_paths = NIL;
111 : :
112 : : /*
113 : : * Buffers for low-level I/O.
114 : : *
115 : : * The receive buffer is fixed size. Send buffer is usually 8k, but can be
116 : : * enlarged by pq_putmessage_noblock() if the message doesn't fit otherwise.
117 : : */
118 : :
119 : : #define PQ_SEND_BUFFER_SIZE 8192
120 : : #define PQ_RECV_BUFFER_SIZE 8192
121 : :
122 : : static char *PqSendBuffer;
123 : : static int PqSendBufferSize; /* Size send buffer */
124 : : static size_t PqSendPointer; /* Next index to store a byte in PqSendBuffer */
125 : : static size_t PqSendStart; /* Next index to send a byte in PqSendBuffer */
126 : :
127 : : static char PqRecvBuffer[PQ_RECV_BUFFER_SIZE];
128 : : static int PqRecvPointer; /* Next index to read a byte from PqRecvBuffer */
129 : : static int PqRecvLength; /* End of data available in PqRecvBuffer */
130 : :
131 : : /*
132 : : * Message status
133 : : */
134 : : static bool PqCommBusy; /* busy sending data to the client */
135 : : static bool PqCommReadingMsg; /* in the middle of reading a message */
136 : :
137 : :
138 : : /* Internal functions */
139 : : static void socket_comm_reset(void);
140 : : static void socket_close(int code, Datum arg);
141 : : static void socket_set_nonblocking(bool nonblocking);
142 : : static int socket_flush(void);
143 : : static int socket_flush_if_writable(void);
144 : : static bool socket_is_send_pending(void);
145 : : static int socket_putmessage(char msgtype, const char *s, size_t len);
146 : : static void socket_putmessage_noblock(char msgtype, const char *s, size_t len);
147 : : static inline int internal_putbytes(const void *b, size_t len);
148 : : static inline int internal_flush(void);
149 : : static pg_noinline int internal_flush_buffer(const char *buf, size_t *start,
150 : : size_t *end);
151 : :
152 : : static int Lock_AF_UNIX(const char *unixSocketDir, const char *unixSocketPath);
153 : : static int Setup_AF_UNIX(const char *sock_path);
154 : :
155 : : static const PQcommMethods PqCommSocketMethods = {
156 : : .comm_reset = socket_comm_reset,
157 : : .flush = socket_flush,
158 : : .flush_if_writable = socket_flush_if_writable,
159 : : .is_send_pending = socket_is_send_pending,
160 : : .putmessage = socket_putmessage,
161 : : .putmessage_noblock = socket_putmessage_noblock
162 : : };
163 : :
164 : : const PQcommMethods *PqCommMethods = &PqCommSocketMethods;
165 : :
166 : : WaitEventSet *FeBeWaitSet;
167 : :
168 : :
169 : : /* --------------------------------
170 : : * pq_init - initialize libpq at backend startup
171 : : * --------------------------------
172 : : */
173 : : Port *
543 heikki.linnakangas@i 174 :CBC 12298 : pq_init(ClientSocket *client_sock)
175 : : {
176 : : Port *port;
177 : : int socket_pos PG_USED_FOR_ASSERTS_ONLY;
178 : : int latch_pos PG_USED_FOR_ASSERTS_ONLY;
179 : :
180 : : /* allocate the Port struct and copy the ClientSocket contents to it */
181 : 12298 : port = palloc0(sizeof(Port));
182 : 12298 : port->sock = client_sock->sock;
183 : 12298 : memcpy(&port->raddr.addr, &client_sock->raddr.addr, client_sock->raddr.salen);
184 : 12298 : port->raddr.salen = client_sock->raddr.salen;
185 : :
186 : : /* fill in the server (local) address */
187 : 12298 : port->laddr.salen = sizeof(port->laddr.addr);
188 [ - + ]: 12298 : if (getsockname(port->sock,
189 : 12298 : (struct sockaddr *) &port->laddr.addr,
190 : : &port->laddr.salen) < 0)
191 : : {
543 heikki.linnakangas@i 192 [ # # ]:UBC 0 : ereport(FATAL,
193 : : (errmsg("%s() failed: %m", "getsockname")));
194 : : }
195 : :
196 : : /* select NODELAY and KEEPALIVE options if it's a TCP connection */
543 heikki.linnakangas@i 197 [ + + ]:CBC 12298 : if (port->laddr.addr.ss_family != AF_UNIX)
198 : : {
199 : : int on;
200 : : #ifdef WIN32
201 : : int oldopt;
202 : : int optlen;
203 : : int newopt;
204 : : #endif
205 : :
206 : : #ifdef TCP_NODELAY
207 : 340 : on = 1;
208 [ - + ]: 340 : if (setsockopt(port->sock, IPPROTO_TCP, TCP_NODELAY,
209 : : (char *) &on, sizeof(on)) < 0)
210 : : {
543 heikki.linnakangas@i 211 [ # # ]:UBC 0 : ereport(FATAL,
212 : : (errmsg("%s(%s) failed: %m", "setsockopt", "TCP_NODELAY")));
213 : : }
214 : : #endif
543 heikki.linnakangas@i 215 :CBC 340 : on = 1;
216 [ - + ]: 340 : if (setsockopt(port->sock, SOL_SOCKET, SO_KEEPALIVE,
217 : : (char *) &on, sizeof(on)) < 0)
218 : : {
543 heikki.linnakangas@i 219 [ # # ]:UBC 0 : ereport(FATAL,
220 : : (errmsg("%s(%s) failed: %m", "setsockopt", "SO_KEEPALIVE")));
221 : : }
222 : :
223 : : #ifdef WIN32
224 : :
225 : : /*
226 : : * This is a Win32 socket optimization. The OS send buffer should be
227 : : * large enough to send the whole Postgres send buffer in one go, or
228 : : * performance suffers. The Postgres send buffer can be enlarged if a
229 : : * very large message needs to be sent, but we won't attempt to
230 : : * enlarge the OS buffer if that happens, so somewhat arbitrarily
231 : : * ensure that the OS buffer is at least PQ_SEND_BUFFER_SIZE * 4.
232 : : * (That's 32kB with the current default).
233 : : *
234 : : * The default OS buffer size used to be 8kB in earlier Windows
235 : : * versions, but was raised to 64kB in Windows 2012. So it shouldn't
236 : : * be necessary to change it in later versions anymore. Changing it
237 : : * unnecessarily can even reduce performance, because setting
238 : : * SO_SNDBUF in the application disables the "dynamic send buffering"
239 : : * feature that was introduced in Windows 7. So before fiddling with
240 : : * SO_SNDBUF, check if the current buffer size is already large enough
241 : : * and only increase it if necessary.
242 : : *
243 : : * See https://support.microsoft.com/kb/823764/EN-US/ and
244 : : * https://msdn.microsoft.com/en-us/library/bb736549%28v=vs.85%29.aspx
245 : : */
246 : : optlen = sizeof(oldopt);
247 : : if (getsockopt(port->sock, SOL_SOCKET, SO_SNDBUF, (char *) &oldopt,
248 : : &optlen) < 0)
249 : : {
250 : : ereport(FATAL,
251 : : (errmsg("%s(%s) failed: %m", "getsockopt", "SO_SNDBUF")));
252 : : }
253 : : newopt = PQ_SEND_BUFFER_SIZE * 4;
254 : : if (oldopt < newopt)
255 : : {
256 : : if (setsockopt(port->sock, SOL_SOCKET, SO_SNDBUF, (char *) &newopt,
257 : : sizeof(newopt)) < 0)
258 : : {
259 : : ereport(FATAL,
260 : : (errmsg("%s(%s) failed: %m", "setsockopt", "SO_SNDBUF")));
261 : : }
262 : : }
263 : : #endif
264 : :
265 : : /*
266 : : * Also apply the current keepalive parameters. If we fail to set a
267 : : * parameter, don't error out, because these aren't universally
268 : : * supported. (Note: you might think we need to reset the GUC
269 : : * variables to 0 in such a case, but it's not necessary because the
270 : : * show hooks for these variables report the truth anyway.)
271 : : */
543 heikki.linnakangas@i 272 :CBC 340 : (void) pq_setkeepalivesidle(tcp_keepalives_idle, port);
273 : 340 : (void) pq_setkeepalivesinterval(tcp_keepalives_interval, port);
274 : 340 : (void) pq_setkeepalivescount(tcp_keepalives_count, port);
275 : 340 : (void) pq_settcpusertimeout(tcp_user_timeout, port);
276 : : }
277 : :
278 : : /* initialize state variables */
5274 279 : 12298 : PqSendBufferSize = PQ_SEND_BUFFER_SIZE;
280 : 12298 : PqSendBuffer = MemoryContextAlloc(TopMemoryContext, PqSendBufferSize);
281 : 12298 : PqSendPointer = PqSendStart = PqRecvPointer = PqRecvLength = 0;
7650 tgl@sss.pgh.pa.us 282 : 12298 : PqCommBusy = false;
3869 heikki.linnakangas@i 283 : 12298 : PqCommReadingMsg = false;
284 : :
285 : : /* set up process-exit hook to close the socket */
3963 rhaas@postgresql.org 286 : 12298 : on_proc_exit(socket_close, 0);
287 : :
288 : : /*
289 : : * In backends (as soon as forked) we operate the underlying socket in
290 : : * nonblocking mode and use latches to implement blocking semantics if
291 : : * needed. That allows us to provide safely interruptible reads and
292 : : * writes.
293 : : */
294 : : #ifndef WIN32
543 heikki.linnakangas@i 295 [ - + ]: 12298 : if (!pg_set_noblock(port->sock))
543 heikki.linnakangas@i 296 [ # # ]:UBC 0 : ereport(FATAL,
297 : : (errmsg("could not set socket to nonblocking mode: %m")));
298 : : #endif
299 : :
300 : : #ifndef WIN32
301 : :
302 : : /* Don't give the socket to any subprograms we execute. */
543 heikki.linnakangas@i 303 [ - + ]:CBC 12298 : if (fcntl(port->sock, F_SETFD, FD_CLOEXEC) < 0)
918 tmunro@postgresql.or 304 [ # # ]:UBC 0 : elog(FATAL, "fcntl(F_SETFD) failed on socket: %m");
305 : : #endif
306 : :
653 heikki.linnakangas@i 307 :CBC 12298 : FeBeWaitSet = CreateWaitEventSet(NULL, FeBeWaitSetNEvents);
1650 tmunro@postgresql.or 308 : 12298 : socket_pos = AddWaitEventToSet(FeBeWaitSet, WL_SOCKET_WRITEABLE,
309 : : port->sock, NULL, NULL);
310 : 12298 : latch_pos = AddWaitEventToSet(FeBeWaitSet, WL_LATCH_SET, PGINVALID_SOCKET,
311 : : MyLatch, NULL);
312 : 12298 : AddWaitEventToSet(FeBeWaitSet, WL_POSTMASTER_DEATH, PGINVALID_SOCKET,
313 : : NULL, NULL);
314 : :
315 : : /*
316 : : * The event positions match the order we added them, but let's sanity
317 : : * check them to be sure.
318 : : */
319 [ - + ]: 12298 : Assert(socket_pos == FeBeWaitSetSocketPos);
320 [ - + ]: 12298 : Assert(latch_pos == FeBeWaitSetLatchPos);
321 : :
543 heikki.linnakangas@i 322 : 12298 : return port;
323 : : }
324 : :
325 : : /* --------------------------------
326 : : * socket_comm_reset - reset libpq during error recovery
327 : : *
328 : : * This is called from error recovery at the outer idle loop. It's
329 : : * just to get us out of trouble if we somehow manage to elog() from
330 : : * inside a pqcomm.c routine (which ideally will never happen, but...)
331 : : * --------------------------------
332 : : */
333 : : static void
3963 rhaas@postgresql.org 334 : 21737 : socket_comm_reset(void)
335 : : {
336 : : /* Do not throw away pending data, but do reset the busy flag */
7650 tgl@sss.pgh.pa.us 337 : 21737 : PqCommBusy = false;
338 : 21737 : }
339 : :
340 : : /* --------------------------------
341 : : * socket_close - shutdown libpq at backend exit
342 : : *
343 : : * This is the one pg_on_exit_callback in place during BackendInitialize().
344 : : * That function's unusual signal handling constrains that this callback be
345 : : * safe to run at any instant.
346 : : * --------------------------------
347 : : */
348 : : static void
3963 rhaas@postgresql.org 349 : 12298 : socket_close(int code, Datum arg)
350 : : {
351 : : /* Nothing to do in a standalone backend, where MyProcPort is NULL. */
9542 tgl@sss.pgh.pa.us 352 [ + - ]: 12298 : if (MyProcPort != NULL)
353 : : {
354 : : #ifdef ENABLE_GSS
355 : : /*
356 : : * Shutdown GSSAPI layer. This section does nothing when interrupting
357 : : * BackendInitialize(), because pg_GSS_recvauth() makes first use of
358 : : * "ctx" and "cred".
359 : : *
360 : : * Note that we don't bother to free MyProcPort->gss, since we're
361 : : * about to exit anyway.
362 : : */
1713 363 [ + + ]: 12298 : if (MyProcPort->gss)
364 : : {
365 : : OM_uint32 min_s;
366 : :
367 [ + + ]: 134 : if (MyProcPort->gss->ctx != GSS_C_NO_CONTEXT)
368 : 132 : gss_delete_sec_context(&min_s, &MyProcPort->gss->ctx, NULL);
369 : :
370 [ - + ]: 134 : if (MyProcPort->gss->cred != GSS_C_NO_CREDENTIAL)
1713 tgl@sss.pgh.pa.us 371 :UBC 0 : gss_release_cred(&min_s, &MyProcPort->gss->cred);
372 : : }
373 : : #endif /* ENABLE_GSS */
374 : :
375 : : /*
376 : : * Cleanly shut down SSL layer. Nowhere else does a postmaster child
377 : : * call this, so this is safe when interrupting BackendInitialize().
378 : : */
8485 bruce@momjian.us 379 :CBC 12298 : secure_close(MyProcPort);
380 : :
381 : : /*
382 : : * Formerly we did an explicit close() here, but it seems better to
383 : : * leave the socket open until the process dies. This allows clients
384 : : * to perform a "synchronous close" if they care --- wait till the
385 : : * transport layer reports connection closure, and you can be sure the
386 : : * backend has exited.
387 : : *
388 : : * We do set sock to PGINVALID_SOCKET to prevent any further I/O,
389 : : * though.
390 : : */
5718 magnus@hagander.net 391 : 12298 : MyProcPort->sock = PGINVALID_SOCKET;
392 : : }
10651 scrappy@hub.org 393 : 12298 : }
394 : :
395 : :
396 : :
397 : : /* --------------------------------
398 : : * Postmaster functions to handle sockets.
399 : : * --------------------------------
400 : : */
401 : :
402 : : /*
403 : : * ListenServerPort -- open a "listening" port to accept connections.
404 : : *
405 : : * family should be AF_UNIX or AF_UNSPEC; portNumber is the port number.
406 : : * For AF_UNIX ports, hostName should be NULL and unixSocketDir must be
407 : : * specified. For TCP ports, hostName is either NULL for all interfaces or
408 : : * the interface to listen on, and unixSocketDir is ignored (can be NULL).
409 : : *
410 : : * Successfully opened sockets are appended to the ListenSockets[] array. On
411 : : * entry, *NumListenSockets holds the number of elements currently in the
412 : : * array, and it is updated to reflect the opened sockets. MaxListen is the
413 : : * allocated size of the array.
414 : : *
415 : : * RETURNS: STATUS_OK or STATUS_ERROR
416 : : */
417 : : int
543 heikki.linnakangas@i 418 : 839 : ListenServerPort(int family, const char *hostName, unsigned short portNumber,
419 : : const char *unixSocketDir,
420 : : pgsocket ListenSockets[], int *NumListenSockets, int MaxListen)
421 : : {
422 : : pgsocket fd;
423 : : int err;
424 : : int maxconn;
425 : : int ret;
426 : : char portNumberStr[32];
427 : : const char *familyDesc;
428 : : char familyDescBuf[64];
429 : : const char *addrDesc;
430 : : char addrBuf[NI_MAXHOST];
431 : : char *service;
8069 bruce@momjian.us 432 : 839 : struct addrinfo *addrs = NULL,
433 : : *addr;
434 : : struct addrinfo hint;
8122 435 : 839 : int added = 0;
436 : : char unixSocketPath[MAXPGPATH];
437 : : #if !defined(WIN32) || defined(IPV6_V6ONLY)
6619 magnus@hagander.net 438 : 839 : int one = 1;
439 : : #endif
440 : :
441 : : /* Initialize hint structure */
8279 bruce@momjian.us 442 [ + - + - : 5873 : MemSet(&hint, 0, sizeof(hint));
+ - + - +
+ ]
443 : 839 : hint.ai_family = family;
8081 tgl@sss.pgh.pa.us 444 : 839 : hint.ai_flags = AI_PASSIVE;
8279 bruce@momjian.us 445 : 839 : hint.ai_socktype = SOCK_STREAM;
446 : :
8310 447 [ + + ]: 839 : if (family == AF_UNIX)
448 : : {
449 : : /*
450 : : * Create unixSocketPath from portNumber and unixSocketDir and lock
451 : : * that file path
452 : : */
4775 tgl@sss.pgh.pa.us 453 [ - + - + ]: 806 : UNIXSOCK_PATH(unixSocketPath, portNumber, unixSocketDir);
4664 454 [ - + ]: 806 : if (strlen(unixSocketPath) >= UNIXSOCK_PATH_BUFLEN)
455 : : {
4664 tgl@sss.pgh.pa.us 456 [ # # ]:UBC 0 : ereport(LOG,
457 : : (errmsg("Unix-domain socket path \"%s\" is too long (maximum %d bytes)",
458 : : unixSocketPath,
459 : : (int) (UNIXSOCK_PATH_BUFLEN - 1))));
460 : 0 : return STATUS_ERROR;
461 : : }
4775 tgl@sss.pgh.pa.us 462 [ - + ]:CBC 806 : if (Lock_AF_UNIX(unixSocketDir, unixSocketPath) != STATUS_OK)
8279 bruce@momjian.us 463 :UBC 0 : return STATUS_ERROR;
4775 tgl@sss.pgh.pa.us 464 :CBC 806 : service = unixSocketPath;
465 : : }
466 : : else
467 : : {
8279 bruce@momjian.us 468 : 33 : snprintf(portNumberStr, sizeof(portNumberStr), "%d", portNumber);
469 : 33 : service = portNumberStr;
470 : : }
471 : :
7264 tgl@sss.pgh.pa.us 472 : 839 : ret = pg_getaddrinfo_all(hostName, service, &hint, &addrs);
8081 473 [ + - - + ]: 839 : if (ret || !addrs)
474 : : {
8081 tgl@sss.pgh.pa.us 475 [ # # ]:UBC 0 : if (hostName)
476 [ # # ]: 0 : ereport(LOG,
477 : : (errmsg("could not translate host name \"%s\", service \"%s\" to address: %s",
478 : : hostName, service, gai_strerror(ret))));
479 : : else
480 [ # # ]: 0 : ereport(LOG,
481 : : (errmsg("could not translate service \"%s\" to address: %s",
482 : : service, gai_strerror(ret))));
7773 bruce@momjian.us 483 [ # # ]: 0 : if (addrs)
7264 tgl@sss.pgh.pa.us 484 : 0 : pg_freeaddrinfo_all(hint.ai_family, addrs);
8279 bruce@momjian.us 485 : 0 : return STATUS_ERROR;
486 : : }
487 : :
8122 bruce@momjian.us 488 [ + + ]:CBC 1678 : for (addr = addrs; addr; addr = addr->ai_next)
489 : : {
1299 peter@eisentraut.org 490 [ + + - + ]: 839 : if (family != AF_UNIX && addr->ai_family == AF_UNIX)
491 : : {
492 : : /*
493 : : * Only set up a unix domain socket when they really asked for it.
494 : : * The service/port is different in that case.
495 : : */
8122 bruce@momjian.us 496 :UBC 0 : continue;
497 : : }
498 : :
499 : : /* See if there is still room to add 1 more socket. */
702 heikki.linnakangas@i 500 [ - + ]:CBC 839 : if (*NumListenSockets == MaxListen)
501 : : {
7542 tgl@sss.pgh.pa.us 502 [ # # ]:UBC 0 : ereport(LOG,
503 : : (errmsg("could not bind to all requested addresses: MAXLISTEN (%d) exceeded",
504 : : MaxListen)));
8122 bruce@momjian.us 505 : 0 : break;
506 : : }
507 : :
508 : : /* set up address family name for log messages */
8061 tgl@sss.pgh.pa.us 509 [ + - + - ]:CBC 839 : switch (addr->ai_family)
510 : : {
511 : 33 : case AF_INET:
7501 bruce@momjian.us 512 : 33 : familyDesc = _("IPv4");
8061 tgl@sss.pgh.pa.us 513 : 33 : break;
8061 tgl@sss.pgh.pa.us 514 :UBC 0 : case AF_INET6:
7501 bruce@momjian.us 515 : 0 : familyDesc = _("IPv6");
8061 tgl@sss.pgh.pa.us 516 : 0 : break;
8061 tgl@sss.pgh.pa.us 517 :CBC 806 : case AF_UNIX:
7501 bruce@momjian.us 518 : 806 : familyDesc = _("Unix");
8061 tgl@sss.pgh.pa.us 519 : 806 : break;
8061 tgl@sss.pgh.pa.us 520 :UBC 0 : default:
521 : 0 : snprintf(familyDescBuf, sizeof(familyDescBuf),
7501 bruce@momjian.us 522 : 0 : _("unrecognized address family %d"),
523 : : addr->ai_family);
8061 tgl@sss.pgh.pa.us 524 : 0 : familyDesc = familyDescBuf;
525 : 0 : break;
526 : : }
527 : :
528 : : /* set up text form of address for log messages */
3102 tgl@sss.pgh.pa.us 529 [ + + ]:CBC 839 : if (addr->ai_family == AF_UNIX)
530 : 806 : addrDesc = unixSocketPath;
531 : : else
532 : : {
533 : 33 : pg_getnameinfo_all((const struct sockaddr_storage *) addr->ai_addr,
534 : 33 : addr->ai_addrlen,
535 : : addrBuf, sizeof(addrBuf),
536 : : NULL, 0,
537 : : NI_NUMERICHOST);
538 : 33 : addrDesc = addrBuf;
539 : : }
540 : :
4161 bruce@momjian.us 541 [ - + ]: 839 : if ((fd = socket(addr->ai_family, SOCK_STREAM, 0)) == PGINVALID_SOCKET)
542 : : {
8082 tgl@sss.pgh.pa.us 543 [ # # ]:UBC 0 : ereport(LOG,
544 : : (errcode_for_socket_access(),
545 : : /* translator: first %s is IPv4, IPv6, or Unix */
546 : : errmsg("could not create %s socket for address \"%s\": %m",
547 : : familyDesc, addrDesc)));
8122 bruce@momjian.us 548 : 0 : continue;
549 : : }
550 : :
551 : : #ifndef WIN32
552 : : /* Don't give the listen socket to any subprograms we execute. */
744 heikki.linnakangas@i 553 [ - + ]:CBC 839 : if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0)
744 heikki.linnakangas@i 554 [ # # ]:UBC 0 : elog(FATAL, "fcntl(F_SETFD) failed on socket: %m");
555 : :
556 : : /*
557 : : * Without the SO_REUSEADDR flag, a new postmaster can't be started
558 : : * right away after a stop or crash, giving "address already in use"
559 : : * error on TCP ports.
560 : : *
561 : : * On win32, however, this behavior only happens if the
562 : : * SO_EXCLUSIVEADDRUSE is set. With SO_REUSEADDR, win32 allows
563 : : * multiple servers to listen on the same address, resulting in
564 : : * unpredictable behavior. With no flags at all, win32 behaves as Unix
565 : : * with SO_REUSEADDR.
566 : : */
1299 peter@eisentraut.org 567 [ + + ]:CBC 839 : if (addr->ai_family != AF_UNIX)
568 : : {
8122 bruce@momjian.us 569 [ - + ]: 33 : if ((setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
570 : : (char *) &one, sizeof(one))) == -1)
571 : : {
8082 tgl@sss.pgh.pa.us 572 [ # # ]:UBC 0 : ereport(LOG,
573 : : (errcode_for_socket_access(),
574 : : /* translator: third %s is IPv4 or IPv6 */
575 : : errmsg("%s(%s) failed for %s address \"%s\": %m",
576 : : "setsockopt", "SO_REUSEADDR",
577 : : familyDesc, addrDesc)));
8122 bruce@momjian.us 578 : 0 : closesocket(fd);
579 : 0 : continue;
580 : : }
581 : : }
582 : : #endif
583 : :
584 : : #ifdef IPV6_V6ONLY
8122 bruce@momjian.us 585 [ - + ]:CBC 839 : if (addr->ai_family == AF_INET6)
586 : : {
8122 bruce@momjian.us 587 [ # # ]:UBC 0 : if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY,
588 : : (char *) &one, sizeof(one)) == -1)
589 : : {
8082 tgl@sss.pgh.pa.us 590 [ # # ]: 0 : ereport(LOG,
591 : : (errcode_for_socket_access(),
592 : : /* translator: third %s is IPv6 */
593 : : errmsg("%s(%s) failed for %s address \"%s\": %m",
594 : : "setsockopt", "IPV6_V6ONLY",
595 : : familyDesc, addrDesc)));
8122 bruce@momjian.us 596 : 0 : closesocket(fd);
597 : 0 : continue;
598 : : }
599 : : }
600 : : #endif
601 : :
602 : : /*
603 : : * Note: This might fail on some OS's, like Linux older than
604 : : * 2.4.21-pre3, that don't have the IPV6_V6ONLY socket option, and map
605 : : * ipv4 addresses to ipv6. It will show ::ffff:ipv4 for all ipv4
606 : : * connections.
607 : : */
8122 bruce@momjian.us 608 :CBC 839 : err = bind(fd, addr->ai_addr, addr->ai_addrlen);
609 [ - + ]: 839 : if (err < 0)
8122 bruce@momjian.us 610 :UBC 0 : {
1746 peter@eisentraut.org 611 : 0 : int saved_errno = errno;
612 : :
8082 tgl@sss.pgh.pa.us 613 [ # # # # : 0 : ereport(LOG,
# # ]
614 : : (errcode_for_socket_access(),
615 : : /* translator: first %s is IPv4, IPv6, or Unix */
616 : : errmsg("could not bind %s address \"%s\": %m",
617 : : familyDesc, addrDesc),
618 : : saved_errno == EADDRINUSE ?
619 : : (addr->ai_family == AF_UNIX ?
620 : : errhint("Is another postmaster already running on port %d?",
621 : : (int) portNumber) :
622 : : errhint("Is another postmaster already running on port %d?"
623 : : " If not, wait a few seconds and retry.",
624 : : (int) portNumber)) : 0));
8122 bruce@momjian.us 625 : 0 : closesocket(fd);
626 : 0 : continue;
627 : : }
628 : :
8122 bruce@momjian.us 629 [ + + ]:CBC 839 : if (addr->ai_family == AF_UNIX)
630 : : {
4775 tgl@sss.pgh.pa.us 631 [ - + ]: 806 : if (Setup_AF_UNIX(service) != STATUS_OK)
632 : : {
8122 bruce@momjian.us 633 :UBC 0 : closesocket(fd);
634 : 0 : break;
635 : : }
636 : : }
637 : :
638 : : /*
639 : : * Select appropriate accept-queue length limit. It seems reasonable
640 : : * to use a value similar to the maximum number of child processes
641 : : * that the postmaster will permit.
642 : : */
1110 tgl@sss.pgh.pa.us 643 :CBC 839 : maxconn = MaxConnections * 2;
644 : :
8122 bruce@momjian.us 645 : 839 : err = listen(fd, maxconn);
646 [ - + ]: 839 : if (err < 0)
647 : : {
8082 tgl@sss.pgh.pa.us 648 [ # # ]:UBC 0 : ereport(LOG,
649 : : (errcode_for_socket_access(),
650 : : /* translator: first %s is IPv4, IPv6, or Unix */
651 : : errmsg("could not listen on %s address \"%s\": %m",
652 : : familyDesc, addrDesc)));
8122 bruce@momjian.us 653 : 0 : closesocket(fd);
654 : 0 : continue;
655 : : }
656 : :
3098 tgl@sss.pgh.pa.us 657 [ + + ]:CBC 839 : if (addr->ai_family == AF_UNIX)
658 [ + - ]: 806 : ereport(LOG,
659 : : (errmsg("listening on Unix socket \"%s\"",
660 : : addrDesc)));
661 : : else
662 [ + - ]: 33 : ereport(LOG,
663 : : /* translator: first %s is IPv4 or IPv6 */
664 : : (errmsg("listening on %s address \"%s\", port %d",
665 : : familyDesc, addrDesc, (int) portNumber)));
666 : :
702 heikki.linnakangas@i 667 : 839 : ListenSockets[*NumListenSockets] = fd;
668 : 839 : (*NumListenSockets)++;
8122 bruce@momjian.us 669 : 839 : added++;
670 : : }
671 : :
7264 tgl@sss.pgh.pa.us 672 : 839 : pg_freeaddrinfo_all(hint.ai_family, addrs);
673 : :
8122 bruce@momjian.us 674 [ - + ]: 839 : if (!added)
8310 bruce@momjian.us 675 :UBC 0 : return STATUS_ERROR;
676 : :
8279 bruce@momjian.us 677 :CBC 839 : return STATUS_OK;
678 : : }
679 : :
680 : :
681 : : /*
682 : : * Lock_AF_UNIX -- configure unix socket file path
683 : : */
684 : : static int
2045 peter@eisentraut.org 685 : 806 : Lock_AF_UNIX(const char *unixSocketDir, const char *unixSocketPath)
686 : : {
687 : : /* no lock file for abstract sockets */
1746 688 [ - + ]: 806 : if (unixSocketPath[0] == '@')
1746 peter@eisentraut.org 689 :UBC 0 : return STATUS_OK;
690 : :
691 : : /*
692 : : * Grab an interlock file associated with the socket file.
693 : : *
694 : : * Note: there are two reasons for using a socket lock file, rather than
695 : : * trying to interlock directly on the socket itself. First, it's a lot
696 : : * more portable, and second, it lets us remove any pre-existing socket
697 : : * file without race conditions.
698 : : */
4775 tgl@sss.pgh.pa.us 699 :CBC 806 : CreateSocketLockFile(unixSocketPath, true, unixSocketDir);
700 : :
701 : : /*
702 : : * Once we have the interlock, we can safely delete any pre-existing
703 : : * socket file to avoid failure at bind() time.
704 : : */
3688 705 : 806 : (void) unlink(unixSocketPath);
706 : :
707 : : /*
708 : : * Remember socket file pathnames for later maintenance.
709 : : */
4775 710 : 806 : sock_paths = lappend(sock_paths, pstrdup(unixSocketPath));
711 : :
8279 bruce@momjian.us 712 : 806 : return STATUS_OK;
713 : : }
714 : :
715 : :
716 : : /*
717 : : * Setup_AF_UNIX -- configure unix socket permissions
718 : : */
719 : : static int
2045 peter@eisentraut.org 720 : 806 : Setup_AF_UNIX(const char *sock_path)
721 : : {
722 : : /* no file system permissions for abstract sockets */
1746 723 [ - + ]: 806 : if (sock_path[0] == '@')
1746 peter@eisentraut.org 724 :UBC 0 : return STATUS_OK;
725 : :
726 : : /*
727 : : * Fix socket ownership/permission if requested. Note we must do this
728 : : * before we listen() to avoid a window where unwanted connections could
729 : : * get accepted.
730 : : */
8279 bruce@momjian.us 731 [ - + ]:CBC 806 : Assert(Unix_socket_group);
732 [ - + ]: 806 : if (Unix_socket_group[0] != '\0')
733 : : {
734 : : #ifdef WIN32
735 : : elog(WARNING, "configuration item \"unix_socket_group\" is not supported on this platform");
736 : : #else
737 : : char *endptr;
738 : : unsigned long val;
739 : : gid_t gid;
740 : :
8279 bruce@momjian.us 741 :UBC 0 : val = strtoul(Unix_socket_group, &endptr, 10);
742 [ # # ]: 0 : if (*endptr == '\0')
743 : : { /* numeric group id */
744 : 0 : gid = val;
745 : : }
746 : : else
747 : : { /* convert group name to id */
748 : : struct group *gr;
749 : :
750 : 0 : gr = getgrnam(Unix_socket_group);
751 [ # # ]: 0 : if (!gr)
752 : : {
8082 tgl@sss.pgh.pa.us 753 [ # # ]: 0 : ereport(LOG,
754 : : (errmsg("group \"%s\" does not exist",
755 : : Unix_socket_group)));
8279 bruce@momjian.us 756 : 0 : return STATUS_ERROR;
757 : : }
758 : 0 : gid = gr->gr_gid;
759 : : }
760 [ # # ]: 0 : if (chown(sock_path, -1, gid) == -1)
761 : : {
8082 tgl@sss.pgh.pa.us 762 [ # # ]: 0 : ereport(LOG,
763 : : (errcode_for_file_access(),
764 : : errmsg("could not set group of file \"%s\": %m",
765 : : sock_path)));
8279 bruce@momjian.us 766 : 0 : return STATUS_ERROR;
767 : : }
768 : : #endif
769 : : }
770 : :
8279 bruce@momjian.us 771 [ - + ]:CBC 806 : if (chmod(sock_path, Unix_socket_permissions) == -1)
772 : : {
8082 tgl@sss.pgh.pa.us 773 [ # # ]:UBC 0 : ereport(LOG,
774 : : (errcode_for_file_access(),
775 : : errmsg("could not set permissions of file \"%s\": %m",
776 : : sock_path)));
8279 bruce@momjian.us 777 : 0 : return STATUS_ERROR;
778 : : }
8310 bruce@momjian.us 779 :CBC 806 : return STATUS_OK;
780 : : }
781 : :
782 : :
783 : : /*
784 : : * AcceptConnection -- accept a new connection with client using
785 : : * server port. Fills *client_sock with the FD and endpoint info
786 : : * of the new connection.
787 : : *
788 : : * ASSUME: that this doesn't need to be non-blocking because
789 : : * the Postmaster waits for the socket to be ready to accept().
790 : : *
791 : : * RETURNS: STATUS_OK or STATUS_ERROR
792 : : */
793 : : int
543 heikki.linnakangas@i 794 : 12614 : AcceptConnection(pgsocket server_fd, ClientSocket *client_sock)
795 : : {
796 : : /* accept connection and fill in the client (remote) address */
797 : 12614 : client_sock->raddr.salen = sizeof(client_sock->raddr.addr);
798 [ - + ]: 12614 : if ((client_sock->sock = accept(server_fd,
799 : 12614 : (struct sockaddr *) &client_sock->raddr.addr,
800 : : &client_sock->raddr.salen)) == PGINVALID_SOCKET)
801 : : {
8082 tgl@sss.pgh.pa.us 802 [ # # ]:UBC 0 : ereport(LOG,
803 : : (errcode_for_socket_access(),
804 : : errmsg("could not accept new connection: %m")));
805 : :
806 : : /*
807 : : * If accept() fails then postmaster.c will still see the server
808 : : * socket as read-ready, and will immediately try again. To avoid
809 : : * uselessly sucking lots of CPU, delay a bit before trying again.
810 : : * (The most likely reason for failure is being out of kernel file
811 : : * table slots; we can do little except hope some will get freed up.)
812 : : */
6780 813 : 0 : pg_usleep(100000L); /* wait 0.1 sec */
9867 bruce@momjian.us 814 : 0 : return STATUS_ERROR;
815 : : }
816 : :
9867 bruce@momjian.us 817 :CBC 12614 : return STATUS_OK;
818 : : }
819 : :
820 : : /*
821 : : * TouchSocketFiles -- mark socket files as recently accessed
822 : : *
823 : : * This routine should be called every so often to ensure that the socket
824 : : * files have a recent mod date (ordinary operations on sockets usually won't
825 : : * change the mod date). That saves them from being removed by
826 : : * overenthusiastic /tmp-directory-cleaner daemons. (Another reason we should
827 : : * never have put the socket file in /tmp...)
828 : : */
829 : : void
4775 tgl@sss.pgh.pa.us 830 :UBC 0 : TouchSocketFiles(void)
831 : : {
832 : : ListCell *l;
833 : :
834 : : /* Loop through all created sockets... */
835 [ # # # # : 0 : foreach(l, sock_paths)
# # ]
836 : : {
837 : 0 : char *sock_path = (char *) lfirst(l);
838 : :
839 : : /* Ignore errors; there's no point in complaining */
2024 840 : 0 : (void) utime(sock_path, NULL);
841 : : }
8260 842 : 0 : }
843 : :
844 : : /*
845 : : * RemoveSocketFiles -- unlink socket files at postmaster shutdown
846 : : */
847 : : void
3688 tgl@sss.pgh.pa.us 848 :CBC 806 : RemoveSocketFiles(void)
849 : : {
850 : : ListCell *l;
851 : :
852 : : /* Loop through all created sockets... */
853 [ + - + + : 1612 : foreach(l, sock_paths)
+ + ]
854 : : {
855 : 806 : char *sock_path = (char *) lfirst(l);
856 : :
857 : : /* Ignore any error. */
858 : 806 : (void) unlink(sock_path);
859 : : }
860 : : /* Since we're about to exit, no need to reclaim storage */
861 : 806 : }
862 : :
863 : :
864 : : /* --------------------------------
865 : : * Low-level I/O routines begin here.
866 : : *
867 : : * These routines communicate with a frontend client across a connection
868 : : * already established by the preceding routines.
869 : : * --------------------------------
870 : : */
871 : :
872 : : /* --------------------------------
873 : : * socket_set_nonblocking - set socket blocking/non-blocking
874 : : *
875 : : * Sets the socket non-blocking if nonblocking is true, or sets it
876 : : * blocking otherwise.
877 : : * --------------------------------
878 : : */
879 : : static void
3963 rhaas@postgresql.org 880 : 2206390 : socket_set_nonblocking(bool nonblocking)
881 : : {
882 [ - + ]: 2206390 : if (MyProcPort == NULL)
3963 rhaas@postgresql.org 883 [ # # ]:UBC 0 : ereport(ERROR,
884 : : (errcode(ERRCODE_CONNECTION_DOES_NOT_EXIST),
885 : : errmsg("there is no client connection")));
886 : :
5274 heikki.linnakangas@i 887 :CBC 2206390 : MyProcPort->noblock = nonblocking;
888 : 2206390 : }
889 : :
890 : : /* --------------------------------
891 : : * pq_recvbuf - load some bytes into the input buffer
892 : : *
893 : : * returns 0 if OK, EOF if trouble
894 : : * --------------------------------
895 : : */
896 : : static int
9631 tgl@sss.pgh.pa.us 897 : 405678 : pq_recvbuf(void)
898 : : {
899 [ + + ]: 405678 : if (PqRecvPointer > 0)
900 : : {
901 [ - + ]: 393380 : if (PqRecvLength > PqRecvPointer)
902 : : {
903 : : /* still some unread data, left-justify it in the buffer */
9601 bruce@momjian.us 904 :UBC 0 : memmove(PqRecvBuffer, PqRecvBuffer + PqRecvPointer,
905 : 0 : PqRecvLength - PqRecvPointer);
9631 tgl@sss.pgh.pa.us 906 : 0 : PqRecvLength -= PqRecvPointer;
907 : 0 : PqRecvPointer = 0;
908 : : }
909 : : else
9631 tgl@sss.pgh.pa.us 910 :CBC 393380 : PqRecvLength = PqRecvPointer = 0;
911 : : }
912 : :
913 : : /* Ensure that we're in blocking mode */
3963 rhaas@postgresql.org 914 : 405678 : socket_set_nonblocking(false);
915 : :
916 : : /* Can fill buffer from PqRecvLength and upwards */
917 : : for (;;)
9631 tgl@sss.pgh.pa.us 918 :UBC 0 : {
919 : : int r;
920 : :
635 tgl@sss.pgh.pa.us 921 :CBC 405678 : errno = 0;
922 : :
8485 bruce@momjian.us 923 : 811334 : r = secure_read(MyProcPort, PqRecvBuffer + PqRecvLength,
5274 heikki.linnakangas@i 924 : 405678 : PQ_RECV_BUFFER_SIZE - PqRecvLength);
925 : :
9631 tgl@sss.pgh.pa.us 926 [ + + ]: 405656 : if (r < 0)
927 : : {
928 [ - + ]: 1 : if (errno == EINTR)
9631 tgl@sss.pgh.pa.us 929 :UBC 0 : continue; /* Ok if interrupted */
930 : :
931 : : /*
932 : : * Careful: an ereport() that tries to write to the client would
933 : : * cause recursion to here, leading to stack overflow and core
934 : : * dump! This message must go *only* to the postmaster log.
935 : : *
936 : : * If errno is zero, assume it's EOF and let the caller complain.
937 : : */
635 tgl@sss.pgh.pa.us 938 [ + - ]:CBC 1 : if (errno != 0)
939 [ + - ]: 1 : ereport(COMMERROR,
940 : : (errcode_for_socket_access(),
941 : : errmsg("could not receive data from client: %m")));
9631 942 : 1 : return EOF;
943 : : }
944 [ + + ]: 405655 : if (r == 0)
945 : : {
946 : : /*
947 : : * EOF detected. We used to write a log message here, but it's
948 : : * better to expect the ultimate caller to do that.
949 : : */
950 : 87 : return EOF;
951 : : }
952 : : /* r contains number of bytes read, so just incr length */
953 : 405568 : PqRecvLength += r;
954 : 405568 : return 0;
955 : : }
956 : : }
957 : :
958 : : /* --------------------------------
959 : : * pq_getbyte - get a single byte from connection, or return EOF
960 : : * --------------------------------
961 : : */
962 : : int
963 : 550150 : pq_getbyte(void)
964 : : {
3869 heikki.linnakangas@i 965 [ - + ]: 550150 : Assert(PqCommReadingMsg);
966 : :
9631 tgl@sss.pgh.pa.us 967 [ + + ]: 865213 : while (PqRecvPointer >= PqRecvLength)
968 : : {
969 [ + + ]: 315151 : if (pq_recvbuf()) /* If nothing in buffer, then recv some */
970 : 66 : return EOF; /* Failed to recv data */
971 : : }
7287 972 : 550062 : return (unsigned char) PqRecvBuffer[PqRecvPointer++];
973 : : }
974 : :
975 : : /* --------------------------------
976 : : * pq_peekbyte - peek at next byte from connection
977 : : *
978 : : * Same as pq_getbyte() except we don't advance the pointer.
979 : : * --------------------------------
980 : : */
981 : : int
9631 982 : 12298 : pq_peekbyte(void)
983 : : {
3865 noah@leadboat.com 984 [ - + ]: 12298 : Assert(PqCommReadingMsg);
985 : :
9631 tgl@sss.pgh.pa.us 986 [ + + ]: 24594 : while (PqRecvPointer >= PqRecvLength)
987 : : {
988 [ + + ]: 12298 : if (pq_recvbuf()) /* If nothing in buffer, then recv some */
989 : 2 : return EOF; /* Failed to recv data */
990 : : }
7287 991 : 12296 : return (unsigned char) PqRecvBuffer[PqRecvPointer];
992 : : }
993 : :
994 : : /* --------------------------------
995 : : * pq_getbyte_if_available - get a single byte from connection,
996 : : * if available
997 : : *
998 : : * The received byte is stored in *c. Returns 1 if a byte was read,
999 : : * 0 if no data was available, or EOF if trouble.
1000 : : * --------------------------------
1001 : : */
1002 : : int
5713 heikki.linnakangas@i 1003 : 879851 : pq_getbyte_if_available(unsigned char *c)
1004 : : {
1005 : : int r;
1006 : :
3869 1007 [ - + ]: 879851 : Assert(PqCommReadingMsg);
1008 : :
5713 1009 [ + + ]: 879851 : if (PqRecvPointer < PqRecvLength)
1010 : : {
1011 : 70768 : *c = PqRecvBuffer[PqRecvPointer++];
1012 : 70768 : return 1;
1013 : : }
1014 : :
1015 : : /* Put the socket into non-blocking mode */
3963 rhaas@postgresql.org 1016 : 809083 : socket_set_nonblocking(true);
1017 : :
635 tgl@sss.pgh.pa.us 1018 : 809083 : errno = 0;
1019 : :
5274 heikki.linnakangas@i 1020 : 809083 : r = secure_read(MyProcPort, c, 1);
1021 [ + + ]: 809083 : if (r < 0)
1022 : : {
1023 : : /*
1024 : : * Ok if no data available without blocking or interrupted (though
1025 : : * EINTR really shouldn't happen with a non-blocking socket). Report
1026 : : * other errors.
1027 : : */
1028 [ + + + - : 732423 : if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
- + ]
1029 : 732420 : r = 0;
1030 : : else
1031 : : {
1032 : : /*
1033 : : * Careful: an ereport() that tries to write to the client would
1034 : : * cause recursion to here, leading to stack overflow and core
1035 : : * dump! This message must go *only* to the postmaster log.
1036 : : *
1037 : : * If errno is zero, assume it's EOF and let the caller complain.
1038 : : */
635 tgl@sss.pgh.pa.us 1039 [ + - ]: 3 : if (errno != 0)
1040 [ + - ]: 3 : ereport(COMMERROR,
1041 : : (errcode_for_socket_access(),
1042 : : errmsg("could not receive data from client: %m")));
5679 heikki.linnakangas@i 1043 : 3 : r = EOF;
1044 : : }
1045 : : }
5274 1046 [ + + ]: 76660 : else if (r == 0)
1047 : : {
1048 : : /* EOF detected */
1049 : 14 : r = EOF;
1050 : : }
1051 : :
5713 1052 : 809083 : return r;
1053 : : }
1054 : :
1055 : : /* --------------------------------
1056 : : * pq_getbytes - get a known number of bytes from connection
1057 : : *
1058 : : * returns 0 if OK, EOF if trouble
1059 : : * --------------------------------
1060 : : */
1061 : : int
195 peter@eisentraut.org 1062 : 1410557 : pq_getbytes(void *b, size_t len)
1063 : : {
1064 : 1410557 : char *s = b;
1065 : : size_t amount;
1066 : :
3869 heikki.linnakangas@i 1067 [ - + ]: 1410557 : Assert(PqCommReadingMsg);
1068 : :
9631 tgl@sss.pgh.pa.us 1069 [ + + ]: 2822391 : while (len > 0)
1070 : : {
1071 [ + + ]: 1490063 : while (PqRecvPointer >= PqRecvLength)
1072 : : {
1073 [ + + ]: 78229 : if (pq_recvbuf()) /* If nothing in buffer, then recv some */
1074 : 20 : return EOF; /* Failed to recv data */
1075 : : }
1076 : 1411834 : amount = PqRecvLength - PqRecvPointer;
1077 [ + + ]: 1411834 : if (amount > len)
1078 : 1006279 : amount = len;
1079 : 1411834 : memcpy(s, PqRecvBuffer + PqRecvPointer, amount);
1080 : 1411834 : PqRecvPointer += amount;
1081 : 1411834 : s += amount;
1082 : 1411834 : len -= amount;
1083 : : }
1084 : 1410537 : return 0;
1085 : : }
1086 : :
1087 : : /* --------------------------------
1088 : : * pq_discardbytes - throw away a known number of bytes
1089 : : *
1090 : : * same as pq_getbytes except we do not copy the data to anyplace.
1091 : : * this is used for resynchronizing after read errors.
1092 : : *
1093 : : * returns 0 if OK, EOF if trouble
1094 : : * --------------------------------
1095 : : */
1096 : : static int
7628 tgl@sss.pgh.pa.us 1097 :UBC 0 : pq_discardbytes(size_t len)
1098 : : {
1099 : : size_t amount;
1100 : :
3869 heikki.linnakangas@i 1101 [ # # ]: 0 : Assert(PqCommReadingMsg);
1102 : :
7628 tgl@sss.pgh.pa.us 1103 [ # # ]: 0 : while (len > 0)
1104 : : {
1105 [ # # ]: 0 : while (PqRecvPointer >= PqRecvLength)
1106 : : {
1107 [ # # ]: 0 : if (pq_recvbuf()) /* If nothing in buffer, then recv some */
1108 : 0 : return EOF; /* Failed to recv data */
1109 : : }
1110 : 0 : amount = PqRecvLength - PqRecvPointer;
1111 [ # # ]: 0 : if (amount > len)
1112 : 0 : amount = len;
1113 : 0 : PqRecvPointer += amount;
1114 : 0 : len -= amount;
1115 : : }
1116 : 0 : return 0;
1117 : : }
1118 : :
1119 : : /* --------------------------------
1120 : : * pq_buffer_remaining_data - return number of bytes in receive buffer
1121 : : *
1122 : : * This will *not* attempt to read more data. And reading up to that number of
1123 : : * bytes should not cause reading any more data either.
1124 : : * --------------------------------
1125 : : */
1126 : : ssize_t
516 heikki.linnakangas@i 1127 :CBC 569 : pq_buffer_remaining_data(void)
1128 : : {
1129 [ - + ]: 569 : Assert(PqRecvLength >= PqRecvPointer);
1130 : 569 : return (PqRecvLength - PqRecvPointer);
1131 : : }
1132 : :
1133 : :
1134 : : /* --------------------------------
1135 : : * pq_startmsgread - begin reading a message from the client.
1136 : : *
1137 : : * This must be called before any of the pq_get* functions.
1138 : : * --------------------------------
1139 : : */
1140 : : void
3869 1141 : 1454868 : pq_startmsgread(void)
1142 : : {
1143 : : /*
1144 : : * There shouldn't be a read active already, but let's check just to be
1145 : : * sure.
1146 : : */
1147 [ - + ]: 1454868 : if (PqCommReadingMsg)
3869 heikki.linnakangas@i 1148 [ # # ]:UBC 0 : ereport(FATAL,
1149 : : (errcode(ERRCODE_PROTOCOL_VIOLATION),
1150 : : errmsg("terminating connection because protocol synchronization was lost")));
1151 : :
3869 heikki.linnakangas@i 1152 :CBC 1454868 : PqCommReadingMsg = true;
1153 : 1454868 : }
1154 : :
1155 : :
1156 : : /* --------------------------------
1157 : : * pq_endmsgread - finish reading message.
1158 : : *
1159 : : * This must be called after reading a message with pq_getbytes()
1160 : : * and friends, to indicate that we have read the whole message.
1161 : : * pq_getmessage() does this implicitly.
1162 : : * --------------------------------
1163 : : */
1164 : : void
1165 : 757267 : pq_endmsgread(void)
1166 : : {
1167 [ - + ]: 757267 : Assert(PqCommReadingMsg);
1168 : :
1169 : 757267 : PqCommReadingMsg = false;
1170 : 757267 : }
1171 : :
1172 : : /* --------------------------------
1173 : : * pq_is_reading_msg - are we currently reading a message?
1174 : : *
1175 : : * This is used in error recovery at the outer idle loop to detect if we have
1176 : : * lost protocol sync, and need to terminate the connection. pq_startmsgread()
1177 : : * will check for that too, but it's nicer to detect it earlier.
1178 : : * --------------------------------
1179 : : */
1180 : : bool
1181 : 21737 : pq_is_reading_msg(void)
1182 : : {
1183 : 21737 : return PqCommReadingMsg;
1184 : : }
1185 : :
1186 : : /* --------------------------------
1187 : : * pq_getmessage - get a message with length word from connection
1188 : : *
1189 : : * The return value is placed in an expansible StringInfo, which has
1190 : : * already been initialized by the caller.
1191 : : * Only the message body is placed in the StringInfo; the length word
1192 : : * is removed. Also, s->cursor is initialized to zero for convenience
1193 : : * in scanning the message contents.
1194 : : *
1195 : : * maxlen is the upper limit on the length of the
1196 : : * message we are willing to accept. We abort the connection (by
1197 : : * returning EOF) if client tries to send more than that.
1198 : : *
1199 : : * returns 0 if OK, EOF if trouble
1200 : : * --------------------------------
1201 : : */
1202 : : int
8176 tgl@sss.pgh.pa.us 1203 : 697474 : pq_getmessage(StringInfo s, int maxlen)
1204 : : {
1205 : : int32 len;
1206 : :
3869 heikki.linnakangas@i 1207 [ - + ]: 697474 : Assert(PqCommReadingMsg);
1208 : :
6762 neilc@samurai.com 1209 : 697474 : resetStringInfo(s);
1210 : :
1211 : : /* Read message length word */
195 peter@eisentraut.org 1212 [ - + ]: 697474 : if (pq_getbytes(&len, 4) == EOF)
1213 : : {
8082 tgl@sss.pgh.pa.us 1214 [ # # ]:UBC 0 : ereport(COMMERROR,
1215 : : (errcode(ERRCODE_PROTOCOL_VIOLATION),
1216 : : errmsg("unexpected EOF within message length word")));
8176 1217 : 0 : return EOF;
1218 : : }
1219 : :
2897 andres@anarazel.de 1220 :CBC 697474 : len = pg_ntoh32(len);
1221 : :
1592 tgl@sss.pgh.pa.us 1222 [ + - - + ]: 697474 : if (len < 4 || len > maxlen)
1223 : : {
8082 tgl@sss.pgh.pa.us 1224 [ # # ]:UBC 0 : ereport(COMMERROR,
1225 : : (errcode(ERRCODE_PROTOCOL_VIOLATION),
1226 : : errmsg("invalid message length")));
8176 1227 : 0 : return EOF;
1228 : : }
1229 : :
7628 tgl@sss.pgh.pa.us 1230 :CBC 697474 : len -= 4; /* discount length itself */
1231 : :
8176 1232 [ + + ]: 697474 : if (len > 0)
1233 : : {
1234 : : /*
1235 : : * Allocate space for message. If we run out of room (ridiculously
1236 : : * large message), we will elog(ERROR), but we want to discard the
1237 : : * message body so as not to lose communication sync.
1238 : : */
7628 1239 [ + - ]: 675436 : PG_TRY();
1240 : : {
1241 : 675436 : enlargeStringInfo(s, len);
1242 : : }
7628 tgl@sss.pgh.pa.us 1243 :UBC 0 : PG_CATCH();
1244 : : {
1245 [ # # ]: 0 : if (pq_discardbytes(len) == EOF)
1246 [ # # ]: 0 : ereport(COMMERROR,
1247 : : (errcode(ERRCODE_PROTOCOL_VIOLATION),
1248 : : errmsg("incomplete message from client")));
1249 : :
1250 : : /* we discarded the rest of the message so we're back in sync. */
3869 heikki.linnakangas@i 1251 : 0 : PqCommReadingMsg = false;
7628 tgl@sss.pgh.pa.us 1252 : 0 : PG_RE_THROW();
1253 : : }
7628 tgl@sss.pgh.pa.us 1254 [ - + ]:CBC 675436 : PG_END_TRY();
1255 : :
1256 : : /* And grab the message */
8176 1257 [ - + ]: 675436 : if (pq_getbytes(s->data, len) == EOF)
1258 : : {
8082 tgl@sss.pgh.pa.us 1259 [ # # ]:UBC 0 : ereport(COMMERROR,
1260 : : (errcode(ERRCODE_PROTOCOL_VIOLATION),
1261 : : errmsg("incomplete message from client")));
8403 1262 : 0 : return EOF;
1263 : : }
8176 tgl@sss.pgh.pa.us 1264 :CBC 675436 : s->len = len;
1265 : : /* Place a trailing null per StringInfo convention */
1266 : 675436 : s->data[len] = '\0';
1267 : : }
1268 : :
1269 : : /* finished reading the message. */
3869 heikki.linnakangas@i 1270 : 697474 : PqCommReadingMsg = false;
1271 : :
8176 tgl@sss.pgh.pa.us 1272 : 697474 : return 0;
1273 : : }
1274 : :
1275 : :
1276 : : static inline int
195 peter@eisentraut.org 1277 : 21558045 : internal_putbytes(const void *b, size_t len)
1278 : : {
1279 : 21558045 : const char *s = b;
1280 : :
9631 tgl@sss.pgh.pa.us 1281 [ + + ]: 43293036 : while (len > 0)
1282 : : {
1283 : : /* If buffer is full, then flush it out */
5274 heikki.linnakangas@i 1284 [ + + ]: 21734996 : if (PqSendPointer >= PqSendBufferSize)
1285 : : {
3963 rhaas@postgresql.org 1286 : 203484 : socket_set_nonblocking(false);
7650 tgl@sss.pgh.pa.us 1287 [ + + ]: 203484 : if (internal_flush())
9631 1288 : 4 : return EOF;
1289 : : }
1290 : :
1291 : : /*
1292 : : * If the buffer is empty and data length is larger than the buffer
1293 : : * size, send it without buffering. Otherwise, copy as much data as
1294 : : * possible into the buffer.
1295 : : */
517 drowley@postgresql.o 1296 [ + + + + ]: 21734992 : if (len >= PqSendBufferSize && PqSendStart == PqSendPointer)
1297 : 114281 : {
1298 : 114282 : size_t start = 0;
1299 : :
1300 : 114282 : socket_set_nonblocking(false);
1301 [ + + ]: 114282 : if (internal_flush_buffer(s, &start, &len))
517 drowley@postgresql.o 1302 :GBC 1 : return EOF;
1303 : : }
1304 : : else
1305 : : {
517 drowley@postgresql.o 1306 :CBC 21620710 : size_t amount = PqSendBufferSize - PqSendPointer;
1307 : :
1308 [ + + ]: 21620710 : if (amount > len)
1309 : 21339662 : amount = len;
1310 : 21620710 : memcpy(PqSendBuffer + PqSendPointer, s, amount);
1311 : 21620710 : PqSendPointer += amount;
1312 : 21620710 : s += amount;
1313 : 21620710 : len -= amount;
1314 : : }
1315 : : }
1316 : :
9631 tgl@sss.pgh.pa.us 1317 : 21558040 : return 0;
1318 : : }
1319 : :
1320 : : /* --------------------------------
1321 : : * socket_flush - flush pending output
1322 : : *
1323 : : * returns 0 if OK, EOF if trouble
1324 : : * --------------------------------
1325 : : */
1326 : : static int
3963 rhaas@postgresql.org 1327 : 349805 : socket_flush(void)
1328 : : {
1329 : : int res;
1330 : :
1331 : : /* No-op if reentrant call */
7650 tgl@sss.pgh.pa.us 1332 [ - + ]: 349805 : if (PqCommBusy)
7650 tgl@sss.pgh.pa.us 1333 :UBC 0 : return 0;
7650 tgl@sss.pgh.pa.us 1334 :CBC 349805 : PqCommBusy = true;
3963 rhaas@postgresql.org 1335 : 349805 : socket_set_nonblocking(false);
7650 tgl@sss.pgh.pa.us 1336 : 349805 : res = internal_flush();
1337 : 349805 : PqCommBusy = false;
1338 : 349805 : return res;
1339 : : }
1340 : :
1341 : : /* --------------------------------
1342 : : * internal_flush - flush pending output
1343 : : *
1344 : : * Returns 0 if OK (meaning everything was sent, or operation would block
1345 : : * and the socket is in non-blocking mode), or EOF if trouble.
1346 : : * --------------------------------
1347 : : */
1348 : : static inline int
1349 : 877347 : internal_flush(void)
1350 : : {
517 drowley@postgresql.o 1351 : 877347 : return internal_flush_buffer(PqSendBuffer, &PqSendStart, &PqSendPointer);
1352 : : }
1353 : :
1354 : : /* --------------------------------
1355 : : * internal_flush_buffer - flush the given buffer content
1356 : : *
1357 : : * Returns 0 if OK (meaning everything was sent, or operation would block
1358 : : * and the socket is in non-blocking mode), or EOF if trouble.
1359 : : * --------------------------------
1360 : : */
1361 : : static pg_noinline int
1362 : 991629 : internal_flush_buffer(const char *buf, size_t *start, size_t *end)
1363 : : {
1364 : : static int last_reported_send_errno = 0;
1365 : :
1366 : 991629 : const char *bufptr = buf + *start;
1367 : 991629 : const char *bufend = buf + *end;
1368 : :
9631 tgl@sss.pgh.pa.us 1369 [ + + ]: 1974274 : while (bufptr < bufend)
1370 : : {
1371 : : int r;
1372 : :
209 peter@eisentraut.org 1373 : 1019599 : r = secure_write(MyProcPort, bufptr, bufend - bufptr);
1374 : :
9631 tgl@sss.pgh.pa.us 1375 [ + + ]: 1019599 : if (r <= 0)
1376 : : {
1377 [ - + ]: 36954 : if (errno == EINTR)
9631 tgl@sss.pgh.pa.us 1378 :UBC 0 : continue; /* Ok if we were interrupted */
1379 : :
1380 : : /*
1381 : : * Ok if no data writable without blocking, and the socket is in
1382 : : * non-blocking mode.
1383 : : */
5274 heikki.linnakangas@i 1384 [ + + ]:CBC 36954 : if (errno == EAGAIN ||
1385 [ - + ]: 22 : errno == EWOULDBLOCK)
1386 : : {
1387 : 36932 : return 0;
1388 : : }
1389 : :
1390 : : /*
1391 : : * Careful: an ereport() that tries to write to the client would
1392 : : * cause recursion to here, leading to stack overflow and core
1393 : : * dump! This message must go *only* to the postmaster log.
1394 : : *
1395 : : * If a client disconnects while we're in the midst of output, we
1396 : : * might write quite a bit of data before we get to a safe query
1397 : : * abort point. So, suppress duplicate log messages.
1398 : : */
8699 tgl@sss.pgh.pa.us 1399 [ + + ]: 22 : if (errno != last_reported_send_errno)
1400 : : {
1401 : 20 : last_reported_send_errno = errno;
8082 1402 [ + - ]: 20 : ereport(COMMERROR,
1403 : : (errcode_for_socket_access(),
1404 : : errmsg("could not send data to client: %m")));
1405 : : }
1406 : :
1407 : : /*
1408 : : * We drop the buffered data anyway so that processing can
1409 : : * continue, even though we'll probably quit soon. We also set a
1410 : : * flag that'll cause the next CHECK_FOR_INTERRUPTS to terminate
1411 : : * the connection.
1412 : : */
517 drowley@postgresql.o 1413 : 22 : *start = *end = 0;
5020 heikki.linnakangas@i 1414 : 22 : ClientConnectionLost = 1;
1415 : 22 : InterruptPending = 1;
9631 tgl@sss.pgh.pa.us 1416 : 22 : return EOF;
1417 : : }
1418 : :
8403 bruce@momjian.us 1419 : 982645 : last_reported_send_errno = 0; /* reset after any successful send */
9631 tgl@sss.pgh.pa.us 1420 : 982645 : bufptr += r;
517 drowley@postgresql.o 1421 : 982645 : *start += r;
1422 : : }
1423 : :
1424 : 954675 : *start = *end = 0;
9631 tgl@sss.pgh.pa.us 1425 : 954675 : return 0;
1426 : : }
1427 : :
1428 : : /* --------------------------------
1429 : : * pq_flush_if_writable - flush pending output if writable without blocking
1430 : : *
1431 : : * Returns 0 if OK, or EOF if trouble.
1432 : : * --------------------------------
1433 : : */
1434 : : static int
3963 rhaas@postgresql.org 1435 : 912328 : socket_flush_if_writable(void)
1436 : : {
1437 : : int res;
1438 : :
1439 : : /* Quick exit if nothing to do */
5274 heikki.linnakangas@i 1440 [ + + ]: 912328 : if (PqSendPointer == PqSendStart)
1441 : 588270 : return 0;
1442 : :
1443 : : /* No-op if reentrant call */
1444 [ - + ]: 324058 : if (PqCommBusy)
5274 heikki.linnakangas@i 1445 :UBC 0 : return 0;
1446 : :
1447 : : /* Temporarily put the socket into non-blocking mode */
3963 rhaas@postgresql.org 1448 :CBC 324058 : socket_set_nonblocking(true);
1449 : :
5274 heikki.linnakangas@i 1450 : 324058 : PqCommBusy = true;
1451 : 324058 : res = internal_flush();
1452 : 324058 : PqCommBusy = false;
1453 : 324058 : return res;
1454 : : }
1455 : :
1456 : : /* --------------------------------
1457 : : * socket_is_send_pending - is there any pending data in the output buffer?
1458 : : * --------------------------------
1459 : : */
1460 : : static bool
3963 rhaas@postgresql.org 1461 : 1763982 : socket_is_send_pending(void)
1462 : : {
5274 heikki.linnakangas@i 1463 : 1763982 : return (PqSendStart < PqSendPointer);
1464 : : }
1465 : :
1466 : : /* --------------------------------
1467 : : * Message-level I/O routines begin here.
1468 : : * --------------------------------
1469 : : */
1470 : :
1471 : :
1472 : : /* --------------------------------
1473 : : * socket_putmessage - send a normal message (suppressed in COPY OUT mode)
1474 : : *
1475 : : * msgtype is a message type code to place before the message body.
1476 : : *
1477 : : * len is the length of the message body data at *s. A message length
1478 : : * word (equal to len+4 because it counts itself too) is inserted by this
1479 : : * routine.
1480 : : *
1481 : : * We suppress messages generated while pqcomm.c is busy. This
1482 : : * avoids any possibility of messages being inserted within other
1483 : : * messages. The only known trouble case arises if SIGQUIT occurs
1484 : : * during a pqcomm.c routine --- quickdie() will try to send a warning
1485 : : * message, and the most reasonable approach seems to be to drop it.
1486 : : *
1487 : : * returns 0 if OK, EOF if trouble
1488 : : * --------------------------------
1489 : : */
1490 : : static int
3963 rhaas@postgresql.org 1491 : 7186015 : socket_putmessage(char msgtype, const char *s, size_t len)
1492 : : {
1493 : : uint32 n32;
1494 : :
1647 heikki.linnakangas@i 1495 [ - + ]: 7186015 : Assert(msgtype != 0);
1496 : :
1497 [ - + ]: 7186015 : if (PqCommBusy)
9631 tgl@sss.pgh.pa.us 1498 :UBC 0 : return 0;
7650 tgl@sss.pgh.pa.us 1499 :CBC 7186015 : PqCommBusy = true;
1647 heikki.linnakangas@i 1500 [ - + ]: 7186015 : if (internal_putbytes(&msgtype, 1))
1647 heikki.linnakangas@i 1501 :UBC 0 : goto fail;
1502 : :
1647 heikki.linnakangas@i 1503 :CBC 7186015 : n32 = pg_hton32((uint32) (len + 4));
195 peter@eisentraut.org 1504 [ - + ]: 7186015 : if (internal_putbytes(&n32, 4))
1647 heikki.linnakangas@i 1505 :UBC 0 : goto fail;
1506 : :
7650 tgl@sss.pgh.pa.us 1507 [ + + ]:CBC 7186015 : if (internal_putbytes(s, len))
1508 : 5 : goto fail;
1509 : 7186010 : PqCommBusy = false;
1510 : 7186010 : return 0;
1511 : :
1512 : 5 : fail:
1513 : 5 : PqCommBusy = false;
1514 : 5 : return EOF;
1515 : : }
1516 : :
1517 : : /* --------------------------------
1518 : : * pq_putmessage_noblock - like pq_putmessage, but never blocks
1519 : : *
1520 : : * If the output buffer is too small to hold the message, the buffer
1521 : : * is enlarged.
1522 : : */
1523 : : static void
3963 rhaas@postgresql.org 1524 : 287059 : socket_putmessage_noblock(char msgtype, const char *s, size_t len)
1525 : : {
1526 : : int res PG_USED_FOR_ASSERTS_ONLY;
1527 : : int required;
1528 : :
1529 : : /*
1530 : : * Ensure we have enough space in the output buffer for the message header
1531 : : * as well as the message itself.
1532 : : */
5274 heikki.linnakangas@i 1533 : 287059 : required = PqSendPointer + 1 + 4 + len;
1534 [ + + ]: 287059 : if (required > PqSendBufferSize)
1535 : : {
1536 : 474 : PqSendBuffer = repalloc(PqSendBuffer, required);
1537 : 474 : PqSendBufferSize = required;
1538 : : }
1539 : 287059 : res = pq_putmessage(msgtype, s, len);
5263 bruce@momjian.us 1540 [ - + ]: 287059 : Assert(res == 0); /* should not fail when the message fits in
1541 : : * buffer */
5274 heikki.linnakangas@i 1542 : 287059 : }
1543 : :
1544 : : /* --------------------------------
1545 : : * pq_putmessage_v2 - send a message in protocol version 2
1546 : : *
1547 : : * msgtype is a message type code to place before the message body.
1548 : : *
1549 : : * We no longer support protocol version 2, but we have kept this
1550 : : * function so that if a client tries to connect with protocol version 2,
1551 : : * as a courtesy we can still send the "unsupported protocol version"
1552 : : * error to the client in the old format.
1553 : : *
1554 : : * Like in pq_putmessage(), we suppress messages generated while
1555 : : * pqcomm.c is busy.
1556 : : *
1557 : : * returns 0 if OK, EOF if trouble
1558 : : * --------------------------------
1559 : : */
1560 : : int
1647 heikki.linnakangas@i 1561 :UBC 0 : pq_putmessage_v2(char msgtype, const char *s, size_t len)
1562 : : {
1563 [ # # ]: 0 : Assert(msgtype != 0);
1564 : :
1565 [ # # ]: 0 : if (PqCommBusy)
1566 : 0 : return 0;
1567 : 0 : PqCommBusy = true;
1568 [ # # ]: 0 : if (internal_putbytes(&msgtype, 1))
1569 : 0 : goto fail;
1570 : :
1571 [ # # ]: 0 : if (internal_putbytes(s, len))
1572 : 0 : goto fail;
1573 : 0 : PqCommBusy = false;
1574 : 0 : return 0;
1575 : :
1576 : 0 : fail:
1577 : 0 : PqCommBusy = false;
1578 : 0 : return EOF;
1579 : : }
1580 : :
1581 : : /*
1582 : : * Support for TCP Keepalive parameters
1583 : : */
1584 : :
1585 : : /*
1586 : : * On Windows, we need to set both idle and interval at the same time.
1587 : : * We also cannot reset them to the default (setting to zero will
1588 : : * actually set them to zero, not default), therefore we fallback to
1589 : : * the out-of-the-box default instead.
1590 : : */
1591 : : #if defined(WIN32) && defined(SIO_KEEPALIVE_VALS)
1592 : : static int
1593 : : pq_setkeepaliveswin32(Port *port, int idle, int interval)
1594 : : {
1595 : : struct tcp_keepalive ka;
1596 : : DWORD retsize;
1597 : :
1598 : : if (idle <= 0)
1599 : : idle = 2 * 60 * 60; /* default = 2 hours */
1600 : : if (interval <= 0)
1601 : : interval = 1; /* default = 1 second */
1602 : :
1603 : : ka.onoff = 1;
1604 : : ka.keepalivetime = idle * 1000;
1605 : : ka.keepaliveinterval = interval * 1000;
1606 : :
1607 : : if (WSAIoctl(port->sock,
1608 : : SIO_KEEPALIVE_VALS,
1609 : : (LPVOID) &ka,
1610 : : sizeof(ka),
1611 : : NULL,
1612 : : 0,
1613 : : &retsize,
1614 : : NULL,
1615 : : NULL)
1616 : : != 0)
1617 : : {
1618 : : ereport(LOG,
1619 : : (errmsg("%s(%s) failed: error code %d",
1620 : : "WSAIoctl", "SIO_KEEPALIVE_VALS", WSAGetLastError())));
1621 : : return STATUS_ERROR;
1622 : : }
1623 : : if (port->keepalives_idle != idle)
1624 : : port->keepalives_idle = idle;
1625 : : if (port->keepalives_interval != interval)
1626 : : port->keepalives_interval = interval;
1627 : : return STATUS_OK;
1628 : : }
1629 : : #endif
1630 : :
1631 : : int
7343 bruce@momjian.us 1632 :CBC 1694 : pq_getkeepalivesidle(Port *port)
1633 : : {
1634 : : #if defined(PG_TCP_KEEPALIVE_IDLE) || defined(SIO_KEEPALIVE_VALS)
1299 peter@eisentraut.org 1635 [ + - + - ]: 1694 : if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
7343 bruce@momjian.us 1636 : 1694 : return 0;
1637 : :
7343 bruce@momjian.us 1638 [ # # ]:UBC 0 : if (port->keepalives_idle != 0)
1639 : 0 : return port->keepalives_idle;
1640 : :
1641 [ # # ]: 0 : if (port->default_keepalives_idle == 0)
1642 : : {
1643 : : #ifndef WIN32
1397 peter@eisentraut.org 1644 : 0 : socklen_t size = sizeof(port->default_keepalives_idle);
1645 : :
2992 tgl@sss.pgh.pa.us 1646 [ # # ]: 0 : if (getsockopt(port->sock, IPPROTO_TCP, PG_TCP_KEEPALIVE_IDLE,
2993 1647 : 0 : (char *) &port->default_keepalives_idle,
1648 : : &size) < 0)
1649 : : {
1737 peter@eisentraut.org 1650 [ # # ]: 0 : ereport(LOG,
1651 : : (errmsg("%s(%s) failed: %m", "getsockopt", PG_TCP_KEEPALIVE_IDLE_STR)));
2993 tgl@sss.pgh.pa.us 1652 : 0 : port->default_keepalives_idle = -1; /* don't know */
1653 : : }
1654 : : #else /* WIN32 */
1655 : : /* We can't get the defaults on Windows, so return "don't know" */
1656 : : port->default_keepalives_idle = -1;
1657 : : #endif /* WIN32 */
1658 : : }
1659 : :
7343 bruce@momjian.us 1660 : 0 : return port->default_keepalives_idle;
1661 : : #else
1662 : : return 0;
1663 : : #endif
1664 : : }
1665 : :
1666 : : int
7343 bruce@momjian.us 1667 :CBC 1407 : pq_setkeepalivesidle(int idle, Port *port)
1668 : : {
1299 peter@eisentraut.org 1669 [ + + - + ]: 1407 : if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
7343 bruce@momjian.us 1670 : 1067 : return STATUS_OK;
1671 : :
1672 : : /* check SIO_KEEPALIVE_VALS here, not just WIN32, as some toolchains lack it */
1673 : : #if defined(PG_TCP_KEEPALIVE_IDLE) || defined(SIO_KEEPALIVE_VALS)
1674 [ + - ]: 340 : if (idle == port->keepalives_idle)
1675 : 340 : return STATUS_OK;
1676 : :
1677 : : #ifndef WIN32
7299 tgl@sss.pgh.pa.us 1678 [ # # ]:UBC 0 : if (port->default_keepalives_idle <= 0)
1679 : : {
7343 bruce@momjian.us 1680 [ # # ]: 0 : if (pq_getkeepalivesidle(port) < 0)
1681 : : {
7299 tgl@sss.pgh.pa.us 1682 [ # # ]: 0 : if (idle == 0)
2999 1683 : 0 : return STATUS_OK; /* default is set but unknown */
1684 : : else
7299 1685 : 0 : return STATUS_ERROR;
1686 : : }
1687 : : }
1688 : :
7343 bruce@momjian.us 1689 [ # # ]: 0 : if (idle == 0)
1690 : 0 : idle = port->default_keepalives_idle;
1691 : :
2992 tgl@sss.pgh.pa.us 1692 [ # # ]: 0 : if (setsockopt(port->sock, IPPROTO_TCP, PG_TCP_KEEPALIVE_IDLE,
1693 : : (char *) &idle, sizeof(idle)) < 0)
1694 : : {
1737 peter@eisentraut.org 1695 [ # # ]: 0 : ereport(LOG,
1696 : : (errmsg("%s(%s) failed: %m", "setsockopt", PG_TCP_KEEPALIVE_IDLE_STR)));
2993 tgl@sss.pgh.pa.us 1697 : 0 : return STATUS_ERROR;
1698 : : }
1699 : :
7343 bruce@momjian.us 1700 : 0 : port->keepalives_idle = idle;
1701 : : #else /* WIN32 */
1702 : : return pq_setkeepaliveswin32(port, idle, port->keepalives_interval);
1703 : : #endif
1704 : : #else
1705 : : if (idle != 0)
1706 : : {
1707 : : ereport(LOG,
1708 : : (errmsg("setting the keepalive idle time is not supported")));
1709 : : return STATUS_ERROR;
1710 : : }
1711 : : #endif
1712 : :
1713 : 0 : return STATUS_OK;
1714 : : }
1715 : :
1716 : : int
7343 bruce@momjian.us 1717 :CBC 1694 : pq_getkeepalivesinterval(Port *port)
1718 : : {
1719 : : #if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
1299 peter@eisentraut.org 1720 [ + - + - ]: 1694 : if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
7343 bruce@momjian.us 1721 : 1694 : return 0;
1722 : :
7343 bruce@momjian.us 1723 [ # # ]:UBC 0 : if (port->keepalives_interval != 0)
1724 : 0 : return port->keepalives_interval;
1725 : :
1726 [ # # ]: 0 : if (port->default_keepalives_interval == 0)
1727 : : {
1728 : : #ifndef WIN32
1397 peter@eisentraut.org 1729 : 0 : socklen_t size = sizeof(port->default_keepalives_interval);
1730 : :
7343 tgl@sss.pgh.pa.us 1731 [ # # ]: 0 : if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPINTVL,
7299 1732 : 0 : (char *) &port->default_keepalives_interval,
1733 : : &size) < 0)
1734 : : {
1737 peter@eisentraut.org 1735 [ # # ]: 0 : ereport(LOG,
1736 : : (errmsg("%s(%s) failed: %m", "getsockopt", "TCP_KEEPINTVL")));
2999 tgl@sss.pgh.pa.us 1737 : 0 : port->default_keepalives_interval = -1; /* don't know */
1738 : : }
1739 : : #else
1740 : : /* We can't get the defaults on Windows, so return "don't know" */
1741 : : port->default_keepalives_interval = -1;
1742 : : #endif /* WIN32 */
1743 : : }
1744 : :
7343 bruce@momjian.us 1745 : 0 : return port->default_keepalives_interval;
1746 : : #else
1747 : : return 0;
1748 : : #endif
1749 : : }
1750 : :
1751 : : int
7343 bruce@momjian.us 1752 :CBC 1407 : pq_setkeepalivesinterval(int interval, Port *port)
1753 : : {
1299 peter@eisentraut.org 1754 [ + + - + ]: 1407 : if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
7343 bruce@momjian.us 1755 : 1067 : return STATUS_OK;
1756 : :
1757 : : #if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
1758 [ + - ]: 340 : if (interval == port->keepalives_interval)
1759 : 340 : return STATUS_OK;
1760 : :
1761 : : #ifndef WIN32
7299 tgl@sss.pgh.pa.us 1762 [ # # ]:UBC 0 : if (port->default_keepalives_interval <= 0)
1763 : : {
7343 bruce@momjian.us 1764 [ # # ]: 0 : if (pq_getkeepalivesinterval(port) < 0)
1765 : : {
7299 tgl@sss.pgh.pa.us 1766 [ # # ]: 0 : if (interval == 0)
2999 1767 : 0 : return STATUS_OK; /* default is set but unknown */
1768 : : else
7299 1769 : 0 : return STATUS_ERROR;
1770 : : }
1771 : : }
1772 : :
7343 bruce@momjian.us 1773 [ # # ]: 0 : if (interval == 0)
1774 : 0 : interval = port->default_keepalives_interval;
1775 : :
tgl@sss.pgh.pa.us 1776 [ # # ]: 0 : if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPINTVL,
1777 : : (char *) &interval, sizeof(interval)) < 0)
1778 : : {
1737 peter@eisentraut.org 1779 [ # # ]: 0 : ereport(LOG,
1780 : : (errmsg("%s(%s) failed: %m", "setsockopt", "TCP_KEEPINTVL")));
7343 bruce@momjian.us 1781 : 0 : return STATUS_ERROR;
1782 : : }
1783 : :
1784 : 0 : port->keepalives_interval = interval;
1785 : : #else /* WIN32 */
1786 : : return pq_setkeepaliveswin32(port, port->keepalives_idle, interval);
1787 : : #endif
1788 : : #else
1789 : : if (interval != 0)
1790 : : {
1791 : : ereport(LOG,
1792 : : (errmsg("%s(%s) not supported", "setsockopt", "TCP_KEEPINTVL")));
1793 : : return STATUS_ERROR;
1794 : : }
1795 : : #endif
1796 : :
1797 : 0 : return STATUS_OK;
1798 : : }
1799 : :
1800 : : int
7343 bruce@momjian.us 1801 :CBC 1694 : pq_getkeepalivescount(Port *port)
1802 : : {
1803 : : #ifdef TCP_KEEPCNT
1299 peter@eisentraut.org 1804 [ + - + - ]: 1694 : if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
7343 bruce@momjian.us 1805 : 1694 : return 0;
1806 : :
7343 bruce@momjian.us 1807 [ # # ]:UBC 0 : if (port->keepalives_count != 0)
1808 : 0 : return port->keepalives_count;
1809 : :
1810 [ # # ]: 0 : if (port->default_keepalives_count == 0)
1811 : : {
1397 peter@eisentraut.org 1812 : 0 : socklen_t size = sizeof(port->default_keepalives_count);
1813 : :
7343 tgl@sss.pgh.pa.us 1814 [ # # ]: 0 : if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPCNT,
7299 1815 : 0 : (char *) &port->default_keepalives_count,
1816 : : &size) < 0)
1817 : : {
1737 peter@eisentraut.org 1818 [ # # ]: 0 : ereport(LOG,
1819 : : (errmsg("%s(%s) failed: %m", "getsockopt", "TCP_KEEPCNT")));
2999 tgl@sss.pgh.pa.us 1820 : 0 : port->default_keepalives_count = -1; /* don't know */
1821 : : }
1822 : : }
1823 : :
7343 bruce@momjian.us 1824 : 0 : return port->default_keepalives_count;
1825 : : #else
1826 : : return 0;
1827 : : #endif
1828 : : }
1829 : :
1830 : : int
7343 bruce@momjian.us 1831 :CBC 1407 : pq_setkeepalivescount(int count, Port *port)
1832 : : {
1299 peter@eisentraut.org 1833 [ + + - + ]: 1407 : if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
7343 bruce@momjian.us 1834 : 1067 : return STATUS_OK;
1835 : :
1836 : : #ifdef TCP_KEEPCNT
1837 [ + - ]: 340 : if (count == port->keepalives_count)
1838 : 340 : return STATUS_OK;
1839 : :
7299 tgl@sss.pgh.pa.us 1840 [ # # ]:UBC 0 : if (port->default_keepalives_count <= 0)
1841 : : {
7343 bruce@momjian.us 1842 [ # # ]: 0 : if (pq_getkeepalivescount(port) < 0)
1843 : : {
7299 tgl@sss.pgh.pa.us 1844 [ # # ]: 0 : if (count == 0)
2999 1845 : 0 : return STATUS_OK; /* default is set but unknown */
1846 : : else
7299 1847 : 0 : return STATUS_ERROR;
1848 : : }
1849 : : }
1850 : :
7343 bruce@momjian.us 1851 [ # # ]: 0 : if (count == 0)
1852 : 0 : count = port->default_keepalives_count;
1853 : :
tgl@sss.pgh.pa.us 1854 [ # # ]: 0 : if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPCNT,
1855 : : (char *) &count, sizeof(count)) < 0)
1856 : : {
1737 peter@eisentraut.org 1857 [ # # ]: 0 : ereport(LOG,
1858 : : (errmsg("%s(%s) failed: %m", "setsockopt", "TCP_KEEPCNT")));
7343 bruce@momjian.us 1859 : 0 : return STATUS_ERROR;
1860 : : }
1861 : :
1862 : 0 : port->keepalives_count = count;
1863 : : #else
1864 : : if (count != 0)
1865 : : {
1866 : : ereport(LOG,
1867 : : (errmsg("%s(%s) not supported", "setsockopt", "TCP_KEEPCNT")));
1868 : : return STATUS_ERROR;
1869 : : }
1870 : : #endif
1871 : :
1872 : 0 : return STATUS_OK;
1873 : : }
1874 : :
1875 : : int
2345 michael@paquier.xyz 1876 :CBC 1694 : pq_gettcpusertimeout(Port *port)
1877 : : {
1878 : : #ifdef TCP_USER_TIMEOUT
1299 peter@eisentraut.org 1879 [ + - + - ]: 1694 : if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
2345 michael@paquier.xyz 1880 : 1694 : return 0;
1881 : :
2345 michael@paquier.xyz 1882 [ # # ]:UBC 0 : if (port->tcp_user_timeout != 0)
1883 : 0 : return port->tcp_user_timeout;
1884 : :
1885 [ # # ]: 0 : if (port->default_tcp_user_timeout == 0)
1886 : : {
1397 peter@eisentraut.org 1887 : 0 : socklen_t size = sizeof(port->default_tcp_user_timeout);
1888 : :
2345 michael@paquier.xyz 1889 [ # # ]: 0 : if (getsockopt(port->sock, IPPROTO_TCP, TCP_USER_TIMEOUT,
1890 : 0 : (char *) &port->default_tcp_user_timeout,
1891 : : &size) < 0)
1892 : : {
1737 peter@eisentraut.org 1893 [ # # ]: 0 : ereport(LOG,
1894 : : (errmsg("%s(%s) failed: %m", "getsockopt", "TCP_USER_TIMEOUT")));
2345 michael@paquier.xyz 1895 : 0 : port->default_tcp_user_timeout = -1; /* don't know */
1896 : : }
1897 : : }
1898 : :
1899 : 0 : return port->default_tcp_user_timeout;
1900 : : #else
1901 : : return 0;
1902 : : #endif
1903 : : }
1904 : :
1905 : : int
2345 michael@paquier.xyz 1906 :CBC 1407 : pq_settcpusertimeout(int timeout, Port *port)
1907 : : {
1299 peter@eisentraut.org 1908 [ + + - + ]: 1407 : if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
2345 michael@paquier.xyz 1909 : 1067 : return STATUS_OK;
1910 : :
1911 : : #ifdef TCP_USER_TIMEOUT
1912 [ + - ]: 340 : if (timeout == port->tcp_user_timeout)
1913 : 340 : return STATUS_OK;
1914 : :
2345 michael@paquier.xyz 1915 [ # # ]:UBC 0 : if (port->default_tcp_user_timeout <= 0)
1916 : : {
1917 [ # # ]: 0 : if (pq_gettcpusertimeout(port) < 0)
1918 : : {
1919 [ # # ]: 0 : if (timeout == 0)
1920 : 0 : return STATUS_OK; /* default is set but unknown */
1921 : : else
1922 : 0 : return STATUS_ERROR;
1923 : : }
1924 : : }
1925 : :
1926 [ # # ]: 0 : if (timeout == 0)
1927 : 0 : timeout = port->default_tcp_user_timeout;
1928 : :
1929 [ # # ]: 0 : if (setsockopt(port->sock, IPPROTO_TCP, TCP_USER_TIMEOUT,
1930 : : (char *) &timeout, sizeof(timeout)) < 0)
1931 : : {
1737 peter@eisentraut.org 1932 [ # # ]: 0 : ereport(LOG,
1933 : : (errmsg("%s(%s) failed: %m", "setsockopt", "TCP_USER_TIMEOUT")));
2345 michael@paquier.xyz 1934 : 0 : return STATUS_ERROR;
1935 : : }
1936 : :
1937 : 0 : port->tcp_user_timeout = timeout;
1938 : : #else
1939 : : if (timeout != 0)
1940 : : {
1941 : : ereport(LOG,
1942 : : (errmsg("%s(%s) not supported", "setsockopt", "TCP_USER_TIMEOUT")));
1943 : : return STATUS_ERROR;
1944 : : }
1945 : : #endif
1946 : :
1947 : 0 : return STATUS_OK;
1948 : : }
1949 : :
1950 : : /*
1951 : : * GUC assign_hook for tcp_keepalives_idle
1952 : : */
1953 : : void
1089 tgl@sss.pgh.pa.us 1954 :CBC 1067 : assign_tcp_keepalives_idle(int newval, void *extra)
1955 : : {
1956 : : /*
1957 : : * The kernel API provides no way to test a value without setting it; and
1958 : : * once we set it we might fail to unset it. So there seems little point
1959 : : * in fully implementing the check-then-assign GUC API for these
1960 : : * variables. Instead we just do the assignment on demand.
1961 : : * pq_setkeepalivesidle reports any problems via ereport(LOG).
1962 : : *
1963 : : * This approach means that the GUC value might have little to do with the
1964 : : * actual kernel value, so we use a show_hook that retrieves the kernel
1965 : : * value rather than trusting GUC's copy.
1966 : : */
1967 : 1067 : (void) pq_setkeepalivesidle(newval, MyProcPort);
1968 : 1067 : }
1969 : :
1970 : : /*
1971 : : * GUC show_hook for tcp_keepalives_idle
1972 : : */
1973 : : const char *
1974 : 1694 : show_tcp_keepalives_idle(void)
1975 : : {
1976 : : /* See comments in assign_tcp_keepalives_idle */
1977 : : static char nbuf[16];
1978 : :
1979 : 1694 : snprintf(nbuf, sizeof(nbuf), "%d", pq_getkeepalivesidle(MyProcPort));
1980 : 1694 : return nbuf;
1981 : : }
1982 : :
1983 : : /*
1984 : : * GUC assign_hook for tcp_keepalives_interval
1985 : : */
1986 : : void
1987 : 1067 : assign_tcp_keepalives_interval(int newval, void *extra)
1988 : : {
1989 : : /* See comments in assign_tcp_keepalives_idle */
1990 : 1067 : (void) pq_setkeepalivesinterval(newval, MyProcPort);
1991 : 1067 : }
1992 : :
1993 : : /*
1994 : : * GUC show_hook for tcp_keepalives_interval
1995 : : */
1996 : : const char *
1997 : 1694 : show_tcp_keepalives_interval(void)
1998 : : {
1999 : : /* See comments in assign_tcp_keepalives_idle */
2000 : : static char nbuf[16];
2001 : :
2002 : 1694 : snprintf(nbuf, sizeof(nbuf), "%d", pq_getkeepalivesinterval(MyProcPort));
2003 : 1694 : return nbuf;
2004 : : }
2005 : :
2006 : : /*
2007 : : * GUC assign_hook for tcp_keepalives_count
2008 : : */
2009 : : void
2010 : 1067 : assign_tcp_keepalives_count(int newval, void *extra)
2011 : : {
2012 : : /* See comments in assign_tcp_keepalives_idle */
2013 : 1067 : (void) pq_setkeepalivescount(newval, MyProcPort);
2014 : 1067 : }
2015 : :
2016 : : /*
2017 : : * GUC show_hook for tcp_keepalives_count
2018 : : */
2019 : : const char *
2020 : 1694 : show_tcp_keepalives_count(void)
2021 : : {
2022 : : /* See comments in assign_tcp_keepalives_idle */
2023 : : static char nbuf[16];
2024 : :
2025 : 1694 : snprintf(nbuf, sizeof(nbuf), "%d", pq_getkeepalivescount(MyProcPort));
2026 : 1694 : return nbuf;
2027 : : }
2028 : :
2029 : : /*
2030 : : * GUC assign_hook for tcp_user_timeout
2031 : : */
2032 : : void
2033 : 1067 : assign_tcp_user_timeout(int newval, void *extra)
2034 : : {
2035 : : /* See comments in assign_tcp_keepalives_idle */
2036 : 1067 : (void) pq_settcpusertimeout(newval, MyProcPort);
2037 : 1067 : }
2038 : :
2039 : : /*
2040 : : * GUC show_hook for tcp_user_timeout
2041 : : */
2042 : : const char *
2043 : 1694 : show_tcp_user_timeout(void)
2044 : : {
2045 : : /* See comments in assign_tcp_keepalives_idle */
2046 : : static char nbuf[16];
2047 : :
2048 : 1694 : snprintf(nbuf, sizeof(nbuf), "%d", pq_gettcpusertimeout(MyProcPort));
2049 : 1694 : return nbuf;
2050 : : }
2051 : :
2052 : : /*
2053 : : * Check if the client is still connected.
2054 : : */
2055 : : bool
1617 tmunro@postgresql.or 2056 :UBC 0 : pq_check_connection(void)
2057 : : {
2058 : : WaitEvent events[FeBeWaitSetNEvents];
2059 : : int rc;
2060 : :
2061 : : /*
2062 : : * It's OK to modify the socket event filter without restoring, because
2063 : : * all FeBeWaitSet socket wait sites do the same.
2064 : : */
1300 2065 : 0 : ModifyWaitEvent(FeBeWaitSet, FeBeWaitSetSocketPos, WL_SOCKET_CLOSED, NULL);
2066 : :
2067 : 0 : retry:
2068 : 0 : rc = WaitEventSetWait(FeBeWaitSet, 0, events, lengthof(events), 0);
2069 [ # # ]: 0 : for (int i = 0; i < rc; ++i)
2070 : : {
2071 [ # # ]: 0 : if (events[i].events & WL_SOCKET_CLOSED)
2072 : 0 : return false;
2073 [ # # ]: 0 : if (events[i].events & WL_LATCH_SET)
2074 : : {
2075 : : /*
2076 : : * A latch event might be preventing other events from being
2077 : : * reported. Reset it and poll again. No need to restore it
2078 : : * because no code should expect latches to survive across
2079 : : * CHECK_FOR_INTERRUPTS().
2080 : : */
1213 tgl@sss.pgh.pa.us 2081 : 0 : ResetLatch(MyLatch);
2082 : 0 : goto retry;
2083 : : }
2084 : : }
2085 : :
1617 tmunro@postgresql.or 2086 : 0 : return true;
2087 : : }
|