-
Notifications
You must be signed in to change notification settings - Fork 19
/
dbgcomm.c
684 lines (601 loc) · 18.6 KB
/
dbgcomm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
/**********************************************************************
* dbgcomm.c
*
* This file contains some helper functions for the proxy - target
* communication.
* Copyright (c) 2004-2024 EnterpriseDB Corporation. All Rights Reserved.
*
* Licensed under the Artistic License v2.0, see
* https://opensource.org/licenses/artistic-license-2.0
* for full details
*
**********************************************************************/
#include "postgres.h"
#include <unistd.h>
#include <netdb.h>
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "miscadmin.h"
#if (PG_VERSION_NUM < 170000)
#include "storage/backendid.h"
#endif
#include "storage/lwlock.h"
#include "storage/pmsignal.h"
#include "storage/shmem.h"
#include "storage/sinvaladt.h"
#include "dbgcomm.h"
#include "pldebugger.h"
#if (PG_VERSION_NUM < 90200)
/* before 9.2, PostmasterIsAlive() had one parameter */
#define PostmasterIsAlive() PostmasterIsAlive(false)
#endif
/*
* Shared memory structure. This is used for authenticating debugger
* connections. Each backend has a dedicated slot.
*
* Whenever a target backend is waiting for a proxy to connect to it
* (pldbg_oid_debug()), or trying to connect to a proxy (when it hits a
* global breakpoint), it advertises the connection attempt in shared memory.
* Each backend has a slot of its own.
*
* When the proxy initiates the connection and target backend listens
* (pldbg_oid_debug()), the backend first sets its status to
* LISTENING_FOR_PROXY, and the port it's listening on in 'port'. When the
* proxy wants to connect to it, it changes the status to PROXY_CONNECTING
* and sets the port to the port it's connecting from. When the target backend
* accept()s the connection, it checks that the remote port of the connection
* matches the one in the slot. This makes the communication secure, because
* only a legitimate proxy backend can access shared memory.
*
* Target backend connecting to a proxy (when a global breakpoint is hit) works
* similarly, except that the LISTENING step is not needed. The backend sets
* the port it's connecting from in its slot's port field, and connects.
* The proxy accept()s the connection, and scans all the slots for a match
* on the port number the connection came from. If it finds the port number
* in one of the slots, the connection came from a legitimate target backend.
*/
#define DBGCOMM_IDLE 0
#define DBGCOMM_LISTENING_FOR_PROXY 1 /* target is listening for a proxy */
#define DBGCOMM_PROXY_CONNECTING 2 /* proxy is connecting to our port */
#define DBGCOMM_CONNECTING_TO_PROXY 3 /* target is connecting to a proxy */
typedef struct
{
BackendId backendid;
int status;
int pid;
int port;
} dbgcomm_target_slot_t;
static dbgcomm_target_slot_t *dbgcomm_slots = NULL;
/*
* Each in-progress connection attempt between proxy and target require
* a slot. 50 should be plenty.
*/
#define NumTargetSlots 50
/**********************************************************************
* Prototypes for static functions
**********************************************************************/
static void dbgcomm_init(void);
static uint32 resolveHostName(const char *hostName);
static int findFreeTargetSlot(void);
static int findTargetSlot(BackendId backendid);
/**********************************************************************
* Initialization routines
**********************************************************************/
/*
* Reserves the right amount of shared memory, when the library is
* preloaded by shared_preload_libraries.
*/
void
dbgcomm_reserve(void)
{
RequestAddinShmemSpace(sizeof(dbgcomm_target_slot_t) * NumTargetSlots);
}
/*
* Initialize slots in shared memory.
*/
static void
dbgcomm_init(void)
{
bool found;
if (dbgcomm_slots)
return;
LWLockAcquire(getPLDebuggerLock(), LW_EXCLUSIVE);
dbgcomm_slots = ShmemInitStruct("Debugger Connection slots", sizeof(dbgcomm_target_slot_t) * NumTargetSlots, &found);
if (dbgcomm_slots == NULL)
elog(ERROR, "out of shared memory");
if (!found)
{
int i;
for (i = 0; i < NumTargetSlots; i++)
{
dbgcomm_slots[i].backendid = InvalidBackendId;
dbgcomm_slots[i].status = DBGCOMM_IDLE;
}
}
LWLockRelease(getPLDebuggerLock());
}
/**********************************************************************
* Routines called by debugging target
*
* These routines use ereport(COMMERROR, ...) for errors, so that they
* are logged but not sent to the client, and don't abort the
* transaction.
*
**********************************************************************/
/*
* dbcomm_connect_to_proxy
*
* This does socket() + connect(), to connect to a listener. The connection
* is authenticated. Returns the file descriptor of the open socket.
*
* We assume that the proxyPort came from a breakpoint or some other reliable
* source, so that we don't allow connecting to any random port in the system.
*/
int
dbgcomm_connect_to_proxy(int proxyPort)
{
int sockfd;
struct sockaddr_in remoteaddr = {0};
struct sockaddr_in localaddr = {0};
socklen_t addrlen = sizeof( remoteaddr );
int reuse_addr_flag = 1;
int slot;
dbgcomm_init();
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
ereport(COMMERROR,
(errcode_for_socket_access(),
errmsg("could not create socket for connecting to proxy: %m")));
return -1;
}
/* Sockets seem to be non-blocking by default on Windows.. */
if (!pg_set_block(sockfd))
{
closesocket(sockfd);
ereport(COMMERROR,
(errmsg("could not set socket to blocking mode: %m")));
return -1;
}
/*
* We have to bind the socket before connecting, so that we know the
* local port number it will use. We have to store it in shared memory
* before connecting, so that the target knows the connection is legit.
*/
localaddr.sin_family = AF_INET;
localaddr.sin_port = htons( 0 );
localaddr.sin_addr.s_addr = resolveHostName( "127.0.0.1" );
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
(const char *) &reuse_addr_flag, sizeof(reuse_addr_flag));
if (bind(sockfd, (struct sockaddr *) &localaddr, sizeof(localaddr)) < 0)
{
ereport(COMMERROR,
(errcode_for_socket_access(),
errmsg("could not bind local port: %m")));
return -1;
}
/* Get the port number selected by the TCP/IP stack */
getsockname(sockfd, (struct sockaddr *) &localaddr, &addrlen);
LWLockAcquire(getPLDebuggerLock(), LW_EXCLUSIVE);
slot = findFreeTargetSlot();
if (slot < 0)
{
closesocket(sockfd);
LWLockRelease(getPLDebuggerLock());
ereport(COMMERROR,
(errcode_for_socket_access(),
errmsg("could not find a free target slot")));
return -1;
}
dbgcomm_slots[slot].port = ntohs(localaddr.sin_port);
dbgcomm_slots[slot].status = DBGCOMM_CONNECTING_TO_PROXY;
dbgcomm_slots[slot].backendid = MyBackendId;
dbgcomm_slots[slot].pid = MyProcPid;
LWLockRelease(getPLDebuggerLock());
remoteaddr.sin_family = AF_INET;
remoteaddr.sin_port = htons(proxyPort);
remoteaddr.sin_addr.s_addr = resolveHostName( "127.0.0.1" );
/* Now connect to the other end. */
if (connect(sockfd, (struct sockaddr *) &remoteaddr,
sizeof(remoteaddr)) < 0)
{
ereport(COMMERROR,
(errcode_for_socket_access(),
errmsg("could not connect to debugging proxy at port %d: %m", proxyPort)));
/*
* Reset our entry in the array. On success, this will be done by
* the proxy.
*/
LWLockAcquire(getPLDebuggerLock(), LW_EXCLUSIVE);
dbgcomm_slots[slot].status = DBGCOMM_IDLE;
dbgcomm_slots[slot].backendid = InvalidBackendId;
dbgcomm_slots[slot].port = 0;
LWLockRelease(getPLDebuggerLock());
return -1;
}
return sockfd;
}
/*
* dbcomm_connect_to_proxy
*
* This does listen() + accept(), to wait for a proxy to connect to us.
*/
int
dbgcomm_listen_for_proxy(void)
{
struct sockaddr_in remoteaddr = {0};
struct sockaddr_in localaddr = {0};
socklen_t addrlen = sizeof( remoteaddr );
int sockfd;
int serverSocket;
int localport;
bool done;
int slot;
dbgcomm_init();
sockfd = socket( AF_INET, SOCK_STREAM, 0 );
if (sockfd < 0)
{
ereport(COMMERROR,
(errcode_for_socket_access(),
errmsg("could not create socket for connecting to proxy: %m")));
return -1;
}
/* Sockets seem to be non-blocking by default on Windows.. */
if (!pg_set_block(sockfd))
{
closesocket(sockfd);
ereport(COMMERROR,
(errmsg("could not set socket to blocking mode: %m")));
return -1;
}
/* Bind the listener socket to any available port */
localaddr.sin_family = AF_INET;
localaddr.sin_port = htons( 0 );
localaddr.sin_addr.s_addr = resolveHostName( "127.0.0.1" );
if (bind( sockfd, (struct sockaddr *) &localaddr, sizeof(localaddr)) < 0)
{
ereport(COMMERROR,
(errcode_for_socket_access(),
errmsg("could not bind socket for listening for proxy: %m")));
return -1;
}
/* Get the port number selected by the TCP/IP stack */
getsockname(sockfd, (struct sockaddr *) &localaddr, &addrlen);
localport = ntohs(localaddr.sin_port);
/* Get ready to wait for a client. */
if (listen(sockfd, 2) < 0)
{
ereport(COMMERROR,
(errcode_for_socket_access(),
errmsg("could not listen() for proxy: %m")));
return -1;
}
LWLockAcquire(getPLDebuggerLock(), LW_EXCLUSIVE);
slot = findFreeTargetSlot();
if (slot < 0)
{
closesocket(sockfd);
LWLockRelease(getPLDebuggerLock());
ereport(COMMERROR,
(errcode_for_socket_access(),
errmsg("could not find a free target slot")));
return -1;
}
dbgcomm_slots[slot].port = localport;
dbgcomm_slots[slot].status = DBGCOMM_LISTENING_FOR_PROXY;
dbgcomm_slots[slot].backendid = MyBackendId;
dbgcomm_slots[slot].pid = MyProcPid;
LWLockRelease(getPLDebuggerLock());
/* Notify the client application that this backend is waiting for a proxy. */
elog(NOTICE, "PLDBGBREAK:%d", MyBackendId);
/* wait for the other end to connect to us */
done = false;
while (!done)
{
serverSocket = accept(sockfd, (struct sockaddr *) &remoteaddr, &addrlen);
if (serverSocket < 0)
ereport(ERROR,
(errmsg("could not accept connection from debugging proxy")));
/*
* Authenticate the connection. We do this by checking that the remote
* end's port number matches what's posted in the shared memory slot.
*/
LWLockAcquire(getPLDebuggerLock(), LW_EXCLUSIVE);
if (dbgcomm_slots[slot].status == DBGCOMM_PROXY_CONNECTING &&
dbgcomm_slots[slot].port == ntohs(remoteaddr.sin_port))
{
dbgcomm_slots[slot].backendid = InvalidBackendId;
dbgcomm_slots[slot].status = DBGCOMM_IDLE;
done = true;
}
else
closesocket(serverSocket);
LWLockRelease(getPLDebuggerLock());
}
closesocket(sockfd);
return serverSocket;
}
/**********************************************************************
* Routines called by debugging proxy
**********************************************************************/
/*
* dbgcomm_connect_to_target
*
* Connect to given target backend that's waiting for us. Returns a socket
* that is open for communication. Uses ereport(ERROR) on error.
*/
int
dbgcomm_connect_to_target(BackendId targetBackend)
{
int sockfd;
struct sockaddr_in remoteaddr = {0};
struct sockaddr_in localaddr = {0};
socklen_t addrlen = sizeof( remoteaddr );
int reuse_addr_flag = 1;
int localport;
int remoteport;
int slot;
dbgcomm_init();
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
ereport(ERROR,
(errcode_for_socket_access(),
errmsg("could not create socket for connecting to target: %m")));
/* Sockets seem to be non-blocking by default on Windows.. */
if (!pg_set_block(sockfd))
{
int save_errno = errno;
closesocket(sockfd);
errno = save_errno;
ereport(ERROR,
(errmsg("could not set socket to blocking mode: %m")));
}
/*
* We have to bind the socket before connecting, so that we know the
* local port number it will use. We have to store it in shared memory
* before connecting, so that the target knows the connection is legit.
*/
localaddr.sin_family = AF_INET;
localaddr.sin_port = htons( 0 );
localaddr.sin_addr.s_addr = resolveHostName( "127.0.0.1" );
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
(const char *) &reuse_addr_flag, sizeof(reuse_addr_flag));
if (bind(sockfd, (struct sockaddr *) &localaddr, sizeof(localaddr)) < 0)
elog(ERROR, "pl_debugger: could not bind local port: %m");
/* Get the port number selected by the TCP/IP stack */
getsockname(sockfd, (struct sockaddr *) &localaddr, &addrlen);
localport = ntohs(localaddr.sin_port);
/*
* Find the target backend's slot. Check which port it's listening on, and
* let it know we're connecting to it from this port.
*/
LWLockAcquire(getPLDebuggerLock(), LW_EXCLUSIVE);
slot = findTargetSlot(targetBackend);
if (slot < 0 || dbgcomm_slots[slot].status != DBGCOMM_LISTENING_FOR_PROXY)
{
closesocket(sockfd);
ereport(ERROR,
(errmsg("target backend is not listening for a connection")));
}
remoteport = dbgcomm_slots[slot].port;
dbgcomm_slots[slot].port = localport;
dbgcomm_slots[slot].status = DBGCOMM_PROXY_CONNECTING;
LWLockRelease(getPLDebuggerLock());
/* Now connect to the other end. */
remoteaddr.sin_family = AF_INET;
remoteaddr.sin_port = htons(remoteport);
remoteaddr.sin_addr.s_addr = resolveHostName( "127.0.0.1" );
if (connect(sockfd, (struct sockaddr *) &remoteaddr,
sizeof(remoteaddr)) < 0)
{
ereport(ERROR,
(errmsg("could not connect to target backend: %m")));
/* XXX: should we do something about the slot on error? */
}
return sockfd;
}
/*
* dbgcomm_accept_target
*
* Waits for one connection from a target backend to the given socket. Returns
* a socket that is open for communication. Uses ereport(ERROR) on error.
*/
int
dbgcomm_accept_target(int sockfd, int *targetPid)
{
int serverSocket;
int i;
struct sockaddr_in remoteaddr = {0};
socklen_t addrlen = sizeof(remoteaddr);
dbgcomm_init();
/* wait for the target to connect to us */
for (;;)
{
fd_set rmask;
int rc;
struct timeval timeout;
/* Check for query cancel or termination request */
CHECK_FOR_INTERRUPTS();
if (!PostmasterIsAlive())
{
/* Emergency bailout if postmaster has died. */
ereport(FATAL,
(errmsg("canceling debugging session because postmaster died")));
}
FD_ZERO(&rmask);
FD_SET(sockfd, &rmask);
/*
* Wake up every 1 second to check if we've been killed or
* postmaster has died.
*/
timeout.tv_sec = 1;
timeout.tv_usec = 0;
rc = select(sockfd + 1, &rmask, NULL, NULL, &timeout);
if (rc < 0)
{
if (errno == EINTR)
continue;
/* anything else is an error */
ereport(ERROR,
(errmsg("select() failed while waiting for target: %m")));
}
if (rc == 0)
{
/* Timeout expired. */
continue;
}
if (!FD_ISSET(sockfd, &rmask))
continue;
serverSocket = accept(sockfd, (struct sockaddr *) &remoteaddr, &addrlen);
if (serverSocket < 0)
ereport(ERROR,
(errmsg("could not accept connection from debugging target: %m")));
/*
* Authenticate the connection. We do this by checking that the remote
* end's port number is listed in a slot in shared memory.
*/
LWLockAcquire(getPLDebuggerLock(), LW_EXCLUSIVE);
for (i = 0; i < NumTargetSlots; i++)
{
if (dbgcomm_slots[i].status == DBGCOMM_CONNECTING_TO_PROXY &&
dbgcomm_slots[i].port == ntohs(remoteaddr.sin_port))
{
*targetPid = dbgcomm_slots[i].pid;
dbgcomm_slots[i].status = DBGCOMM_IDLE;
break;
}
}
LWLockRelease(getPLDebuggerLock());
if (i >= NumTargetSlots)
{
/*
* This connection did not come from a backend. Reject and continue
* listening.
*/
closesocket(serverSocket);
continue;
}
else
{
/* This looks like a legitimate connection. */
break;
}
}
return serverSocket;
}
int
dbgcomm_listen_for_target(int *port)
{
int sockfd;
struct sockaddr_in proxy_addr = {0};
socklen_t proxy_addr_len = sizeof( proxy_addr );
int reuse_addr_flag = 1;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
ereport(ERROR,
(errcode_for_socket_access(),
errmsg("could not create socket: %m")));
/* Sockets seem to be non-blocking by default on Windows.. */
if (!pg_set_block(sockfd))
{
int save_errno = errno;
closesocket(sockfd);
errno = save_errno;
ereport(ERROR,
(errmsg("could not set socket to blocking mode: %m")));
}
/* Ask the TCP/IP stack for an unused port */
proxy_addr.sin_family = AF_INET;
proxy_addr.sin_port = htons(0);
proxy_addr.sin_addr.s_addr = resolveHostName("127.0.0.1");
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
(const char *) &reuse_addr_flag, sizeof( reuse_addr_flag ));
/* Bind a listener socket to that port */
if (bind( sockfd, (struct sockaddr *) &proxy_addr, sizeof( proxy_addr )) < 0 )
{
int save_errno = errno;
closesocket(sockfd);
errno = save_errno;
ereport(ERROR,
(errmsg("could not create listener for debugger connection")));
}
/* Get the port number selected by the TCP/IP stack */
getsockname( sockfd, (struct sockaddr *)&proxy_addr, &proxy_addr_len );
*port = (int) ntohs( proxy_addr.sin_port );
/* Get ready to wait for a client */
listen(sockfd, 2);
elog(DEBUG1, "listening for debugging target at port %d", *port);
return sockfd;
}
/*
* Find first available target slot.
*
* Note: Caller must be holding the lock.
*/
static int
findFreeTargetSlot(void)
{
int i;
for (i = 0; i < NumTargetSlots; i++)
{
if (dbgcomm_slots[i].backendid == InvalidBackendId)
return i;
if (dbgcomm_slots[i].backendid == MyBackendId)
{
/*
* If we've failed to deallocate our slot earlier, reuse this slot.
* This shouldn't happen.
*/
elog(LOG, "found leftover debugging target slot for backend %d",
MyBackendId);
return i;
}
}
return -1;
}
/*
* Find target slot belonging to given backend.
*
* Note: Caller must be holding the lock.
*/
static int
findTargetSlot(BackendId backendid)
{
int i;
for (i = 0; i < NumTargetSlots; i++)
{
if (dbgcomm_slots[i].backendid == backendid)
return i;
}
return -1;
}
/*******************************************************************************
* resolveHostName()
*
* Given the name of a host (hostName), this function returns the IP address
* of that host (or 0 if the name does not resolve to an address).
*
* FIXME: this function should probably be a bit more flexibile.
*/
#ifndef INADDR_NONE
#define INADDR_NONE ((unsigned long int) -1) /* For Solaris */
#endif
static uint32 resolveHostName( const char * hostName )
{
struct hostent * hostDesc;
uint32 hostAddress;
if(( hostDesc = gethostbyname( hostName )))
hostAddress = ((struct in_addr *)hostDesc->h_addr )->s_addr;
else
hostAddress = inet_addr( hostName );
if(( hostAddress == -1 ) || ( hostAddress == INADDR_NONE ))
return( 0 );
else
return( hostAddress );
}