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