/
child.c
1872 lines (1622 loc) · 41.5 KB
/
child.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
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*-pgsql-c-*- */
/*
* $Header: /cvsroot/pgpool/pgpool-II/child.c,v 1.37 2009/12/02 14:19:56 t-ishii Exp $
*
* pgpool: a language independent connection pool server for PostgreSQL
* written by Tatsuo Ishii
*
* Copyright (c) 2003-2009 PgPool Global Development Group
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose and without fee is hereby
* granted, provided that the above copyright notice appear in all
* copies and that both that copyright notice and this permission
* notice appear in supporting documentation, and that the name of the
* author not be used in advertising or publicity pertaining to
* distribution of the software without specific, written prior
* permission. The author makes no representations about the
* suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
* child.c: child process main
*
*/
#include "config.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <arpa/inet.h>
#include <netdb.h>
#ifdef HAVE_NETINET_TCP_H
#include <netinet/tcp.h>
#endif
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#include <signal.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/time.h>
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_CRYPT_H
#include <crypt.h>
#endif
#include "pool.h"
#include "pool_ip.h"
#include "md5.h"
static POOL_CONNECTION *do_accept(int unix_fd, int inet_fd, struct timeval *timeout);
static StartupPacket *read_startup_packet(POOL_CONNECTION *cp);
static POOL_CONNECTION_POOL *connect_backend(StartupPacket *sp, POOL_CONNECTION *frontend);
static RETSIGTYPE die(int sig);
static RETSIGTYPE close_idle_connection(int sig);
static RETSIGTYPE wakeup_handler(int sig);
static RETSIGTYPE reload_config_handler(int sig);
static RETSIGTYPE authentication_timeout(int sig);
static int send_params(POOL_CONNECTION *frontend, POOL_CONNECTION_POOL *backend);
static void send_frontend_exits(void);
static int s_do_auth(POOL_CONNECTION_POOL_SLOT *cp, char *password);
static void connection_count_up(void);
static void connection_count_down(void);
/*
* non 0 means SIGTERM(smart shutdown) or SIGINT(fast shutdown) has arrived
*/
volatile sig_atomic_t exit_request = 0;
static int idle; /* non 0 means this child is in idle state */
static int accepted = 0;
extern int myargc;
extern char **myargv;
char remote_ps_data[NI_MAXHOST]; /* used for set_ps_display */
volatile sig_atomic_t got_sighup = 0;
int LocalSessionId; /* Local session id */
/*
* child main loop
*/
void do_child(int unix_fd, int inet_fd)
{
POOL_CONNECTION *frontend;
POOL_CONNECTION_POOL *backend;
struct timeval now;
struct timezone tz;
int child_idle_sec;
struct timeval timeout;
static int connected;
int connections_count = 0; /* used if child_max_connections > 0 */
int first_ready_for_query_received; /* for master/slave mode */
int found;
char psbuf[NI_MAXHOST + 128];
pool_debug("I am %d", getpid());
/* Identify myself via ps */
init_ps_display("", "", "", "");
/* set up signal handlers */
signal(SIGALRM, SIG_DFL);
signal(SIGTERM, die);
signal(SIGINT, die);
signal(SIGHUP, reload_config_handler);
signal(SIGQUIT, die);
signal(SIGCHLD, SIG_DFL);
signal(SIGUSR1, close_idle_connection);
signal(SIGUSR2, wakeup_handler);
signal(SIGPIPE, SIG_IGN);
#ifdef NONE_BLOCK
/* set listen fds to none block */
pool_set_nonblock(unix_fd);
if (inet_fd)
{
pool_set_nonblock(inet_fd);
}
#endif
/* initialize random seed */
gettimeofday(&now, &tz);
srandom((unsigned int) now.tv_usec);
/* initialize systemdb connection */
if (pool_config->parallel_mode || pool_config->enable_query_cache)
{
system_db_connect();
if (PQstatus(system_db_info->pgconn) != CONNECTION_OK)
{
pool_error("Could not make persistent libpq system DB connection");
}
system_db_info->connection = make_persistent_db_connection(pool_config->system_db_hostname,
pool_config->system_db_port,
pool_config->system_db_dbname,
pool_config->system_db_user,
pool_config->system_db_password);
if (system_db_info->connection == NULL)
{
pool_error("Could not make persistent system DB connection");
}
}
/* initialize connection pool */
if (pool_init_cp())
{
child_exit(1);
}
child_idle_sec = 0;
timeout.tv_sec = pool_config->child_life_time;
timeout.tv_usec = 0;
init_prepared_list();
for (;;)
{
int connection_reuse = 1;
int ssl_request = 0;
StartupPacket *sp;
/* pgpool stop request already sent? */
check_stop_request();
idle = 1;
accepted = 0;
/* perform accept() */
frontend = do_accept(unix_fd, inet_fd, &timeout);
if (frontend == NULL) /* connection request from frontend timed out */
{
/* check select() timeout */
if (connected && pool_config->child_life_time > 0 &&
timeout.tv_sec == 0 && timeout.tv_usec == 0)
{
pool_debug("child life %d seconds expired", pool_config->child_life_time);
/*
* Doesn't need to call this. child_exit() calls it.
* send_frontend_exits();
*/
child_exit(2);
}
continue;
}
/* set frontend fd to blocking */
pool_unset_nonblock(frontend->fd);
/* set busy flag and clear child idle timer */
idle = 0;
child_idle_sec = 0;
/* check backend timer is expired */
if (backend_timer_expired)
{
pool_backend_timer();
backend_timer_expired = 0;
}
/* read the startup packet */
retry_startup:
sp = read_startup_packet(frontend);
if (sp == NULL)
{
/* failed to read the startup packet. return to the accept() loop */
pool_close(frontend);
connection_count_down();
continue;
}
/* cancel request? */
if (sp->major == 1234 && sp->minor == 5678)
{
cancel_request((CancelPacket *)sp->startup_packet);
pool_close(frontend);
pool_free_startup_packet(sp);
connection_count_down();
continue;
}
/* SSL? */
if (sp->major == 1234 && sp->minor == 5679)
{
/* SSL not supported */
pool_debug("SSLRequest: sent N; retry startup");
if (ssl_request)
{
pool_close(frontend);
pool_free_startup_packet(sp);
connection_count_down();
continue;
}
/*
* say to the frontend "we do not suppport SSL"
* note that this is not a NOTICE response despite it's an 'N'!
*/
pool_write_and_flush(frontend, "N", 1);
ssl_request = 1;
pool_free_startup_packet(sp);
goto retry_startup;
}
if (pool_config->enable_pool_hba)
{
/*
* do client authentication.
* Note that ClientAuthentication does not return if frontend
* was rejected; it simply terminates this process.
*/
frontend->protoVersion = sp->major;
frontend->database = strdup(sp->database);
if (frontend->database == NULL)
{
pool_error("do_child: strdup failed: %s\n", strerror(errno));
child_exit(1);
}
frontend->username = strdup(sp->user);
if (frontend->username == NULL)
{
pool_error("do_child: strdup failed: %s\n", strerror(errno));
child_exit(1);
}
ClientAuthentication(frontend);
}
/*
* Ok, negotiaton with frontend has been done. Let's go to the next step.
*/
/*
* if there's no connection associated with user and database,
* we need to connect to the backend and send the startup packet.
*/
first_ready_for_query_received = 0; /* for master/slave mode */
/* look for existing connection */
found = 0;
backend = pool_get_cp(sp->user, sp->database, sp->major, 1);
if (backend != NULL)
{
found = 1;
/* existing connection associated with same user/database/major found.
* however we should make sure that the startup packet contents are identical.
* OPTION data and others might be different.
*/
if (sp->len != MASTER_CONNECTION(backend)->sp->len)
{
pool_debug("pool_process_query: connection exists but startup packet length is not identical");
found = 0;
}
else if(memcmp(sp->startup_packet, MASTER_CONNECTION(backend)->sp->startup_packet, sp->len) != 0)
{
pool_debug("pool_process_query: connection exists but startup packet contents is not identical");
found = 0;
}
if (found == 0)
{
/* we need to discard existing connection since startup packet is different */
pool_discard_cp(sp->user, sp->database, sp->major);
backend = NULL;
}
}
if (backend == NULL)
{
/* create a new connection to backend */
connection_reuse = 0;
if ((backend = connect_backend(sp, frontend)) == NULL)
{
connection_count_down();
continue;
}
/* in master/slave mode, the first "ready for query"
* packet should be treated as if we were not in the
* mode
*/
if (MASTER_SLAVE)
first_ready_for_query_received = 1;
}
else
{
int i, freed = 0;
/*
* save startup packet info
*/
for (i = 0; i < NUM_BACKENDS; i++)
{
if (VALID_BACKEND(i))
{
if (!freed)
{
pool_free_startup_packet(backend->slots[i]->sp);
freed = 1;
}
backend->slots[i]->sp = sp;
}
}
/* reuse existing connection to backend */
if (pool_do_reauth(frontend, backend))
{
pool_close(frontend);
connection_count_down();
continue;
}
if (MAJOR(backend) == 3)
{
if (send_params(frontend, backend))
{
pool_close(frontend);
connection_count_down();
continue;
}
}
/* send ReadyForQuery to frontend */
pool_write(frontend, "Z", 1);
if (MAJOR(backend) == 3)
{
int len;
char tstate;
len = htonl(5);
pool_write(frontend, &len, sizeof(len));
tstate = TSTATE(backend);
pool_write(frontend, &tstate, 1);
}
if (pool_flush(frontend) < 0)
{
pool_close(frontend);
connection_count_down();
continue;
}
}
connected = 1;
LocalSessionId++;
/* show ps status */
sp = MASTER_CONNECTION(backend)->sp;
snprintf(psbuf, sizeof(psbuf), "%s %s %s idle",
sp->user, sp->database, remote_ps_data);
set_ps_display(psbuf, false);
if (MAJOR(backend) == PROTO_MAJOR_V2)
TSTATE(backend) = 'I';
if (pool_config->load_balance_mode)
{
/* select load balancing node */
backend->info->load_balancing_node = select_load_balancing_node();
}
/* query process loop */
for (;;)
{
POOL_STATUS status;
status = pool_process_query(frontend, backend, 0, first_ready_for_query_received);
sp = MASTER_CONNECTION(backend)->sp;
switch (status)
{
/* client exits */
case POOL_END:
/*
* do not cache connection if:
* pool_config->connection_cahe == 0 or
* database name is template0, template1, postgres or regression
*/
if (pool_config->connection_cache == 0 ||
!strcmp(sp->database, "template0") ||
!strcmp(sp->database, "template1") ||
!strcmp(sp->database, "postgres") ||
!strcmp(sp->database, "regression"))
{
reset_variables();
pool_close(frontend);
pool_send_frontend_exits(backend);
pool_discard_cp(sp->user, sp->database, sp->major);
}
else
{
POOL_STATUS status1;
/* send reset request to backend */
status1 = pool_process_query(frontend, backend, 1, 0);
pool_close(frontend);
/* if we detect errors on resetting connection, we need to discard
* this connection since it might be in unknown status
*/
if (status1 != POOL_CONTINUE)
{
pool_debug("error in resetting connections. discarding connection pools...");
pool_send_frontend_exits(backend);
pool_discard_cp(sp->user, sp->database, sp->major);
}
else
pool_connection_pool_timer(backend);
}
break;
/* error occured. discard backend connection pool
and disconnect connection to the frontend */
case POOL_ERROR:
#ifdef NOT_USED
pool_discard_cp(sp->user, sp->database);
pool_close(frontend);
notice_backend_error();
#endif
pool_log("do_child: exits with status 1 due to error");
child_exit(1);
break;
/* fatal error occured. just exit myself... */
case POOL_FATAL:
notice_backend_error(1);
child_exit(1);
break;
/* not implemented yet */
case POOL_IDLE:
do_accept(unix_fd, inet_fd, &timeout);
pool_debug("accept while idle");
break;
default:
break;
}
if (status != POOL_CONTINUE)
break;
}
accepted = 0;
connection_count_down();
timeout.tv_sec = pool_config->child_life_time;
timeout.tv_usec = 0;
/* increment queries counter if necessary */
if ( pool_config->child_max_connections > 0 )
connections_count++;
/* check if maximum connections count for this child reached */
if ( ( pool_config->child_max_connections > 0 ) &&
( connections_count >= pool_config->child_max_connections ) )
{
pool_log("child exiting, %d connections reached", pool_config->child_max_connections);
send_frontend_exits();
child_exit(2);
}
}
child_exit(0);
}
/* -------------------------------------------------------------------
* private functions
* -------------------------------------------------------------------
*/
/*
* set non-block flag
*/
void pool_set_nonblock(int fd)
{
int var;
/* set fd to none blocking */
var = fcntl(fd, F_GETFL, 0);
if (var == -1)
{
pool_error("fcntl failed. %s", strerror(errno));
child_exit(1);
}
if (fcntl(fd, F_SETFL, var | O_NONBLOCK) == -1)
{
pool_error("fcntl failed. %s", strerror(errno));
child_exit(1);
}
}
/*
* unset non-block flag
*/
void pool_unset_nonblock(int fd)
{
int var;
/* set fd to none blocking */
var = fcntl(fd, F_GETFL, 0);
if (var == -1)
{
pool_error("fcntl failed. %s", strerror(errno));
child_exit(1);
}
if (fcntl(fd, F_SETFL, var & ~O_NONBLOCK) == -1)
{
pool_error("fcntl failed. %s", strerror(errno));
child_exit(1);
}
}
/*
* perform accept() and return new fd
*/
static POOL_CONNECTION *do_accept(int unix_fd, int inet_fd, struct timeval *timeout)
{
fd_set readmask;
int fds;
int save_errno;
SockAddr saddr;
int fd = 0;
int afd;
int inet = 0;
POOL_CONNECTION *cp;
#ifdef ACCEPT_PERFORMANCE
struct timeval now1, now2;
static long atime;
static int cnt;
#endif
struct timeval *timeoutval;
struct timeval tv1, tv2, tmback = {0, 0};
char remote_host[NI_MAXHOST];
char remote_port[NI_MAXSERV];
set_ps_display("wait for connection request", false);
FD_ZERO(&readmask);
FD_SET(unix_fd, &readmask);
if (inet_fd)
FD_SET(inet_fd, &readmask);
if (timeout->tv_sec == 0 && timeout->tv_usec == 0)
timeoutval = NULL;
else
{
timeoutval = timeout;
tmback.tv_sec = timeout->tv_sec;
tmback.tv_usec = timeout->tv_usec;
gettimeofday(&tv1, NULL);
#ifdef DEBUG
pool_log("before select = {%d, %d}", timeoutval->tv_sec, timeoutval->tv_usec);
pool_log("g:before select = {%d, %d}", tv1.tv_sec, tv1.tv_usec);
#endif
}
fds = select(Max(unix_fd, inet_fd)+1, &readmask, NULL, NULL, timeoutval);
save_errno = errno;
/* check backend timer is expired */
if (backend_timer_expired)
{
pool_backend_timer();
backend_timer_expired = 0;
}
/*
* following code fragment computes remaining timeout val in a
* portable way. Linux does this automazically but other platforms do not.
*/
if (timeoutval)
{
gettimeofday(&tv2, NULL);
tmback.tv_usec -= tv2.tv_usec - tv1.tv_usec;
tmback.tv_sec -= tv2.tv_sec - tv1.tv_sec;
if (tmback.tv_usec < 0)
{
tmback.tv_sec--;
if (tmback.tv_sec < 0)
{
timeout->tv_sec = 0;
timeout->tv_usec = 0;
}
else
{
tmback.tv_usec += 1000000;
timeout->tv_sec = tmback.tv_sec;
timeout->tv_usec = tmback.tv_usec;
}
}
#ifdef DEBUG
pool_log("g:after select = {%d, %d}", tv2.tv_sec, tv2.tv_usec);
pool_log("after select = {%d, %d}", timeout->tv_sec, timeout->tv_usec);
#endif
}
errno = save_errno;
if (fds == -1)
{
if (errno == EAGAIN || errno == EINTR)
return NULL;
pool_error("select() failed. reason %s", strerror(errno));
return NULL;
}
/* timeout */
if (fds == 0)
{
return NULL;
}
if (FD_ISSET(unix_fd, &readmask))
{
fd = unix_fd;
}
if (FD_ISSET(inet_fd, &readmask))
{
fd = inet_fd;
inet++;
}
/*
* Note that some SysV systems do not work here. For those
* systems, we need some locking mechanism for the fd.
*/
memset(&saddr, 0, sizeof(saddr));
saddr.salen = sizeof(saddr.addr);
#ifdef ACCEPT_PERFORMANCE
gettimeofday(&now1,0);
#endif
retry_accept:
/* wait if recovery is started */
while (*InRecovery == 1)
{
pause();
}
afd = accept(fd, (struct sockaddr *)&saddr.addr, &saddr.salen);
save_errno = errno;
/* check backend timer is expired */
if (backend_timer_expired)
{
pool_backend_timer();
backend_timer_expired = 0;
}
errno = save_errno;
if (afd < 0)
{
if (errno == EINTR && *InRecovery)
goto retry_accept;
/*
* "Resource temporarily unavailable" (EAGAIN or EWOULDBLOCK)
* can be silently ignored. And EINTR can be ignored.
*/
if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
pool_error("accept() failed. reason: %s", strerror(errno));
return NULL;
}
#ifdef ACCEPT_PERFORMANCE
gettimeofday(&now2,0);
atime += (now2.tv_sec - now1.tv_sec)*1000000 + (now2.tv_usec - now1.tv_usec);
cnt++;
if (cnt % 100 == 0)
{
pool_log("cnt: %d atime: %ld", cnt, atime);
}
#endif
/* reload config file */
if (got_sighup)
{
pool_get_config(get_config_file_name(), RELOAD_CONFIG);
if (pool_config->enable_pool_hba)
load_hba(get_hba_file_name());
if (pool_config->parallel_mode)
pool_memset_system_db_info(system_db_info->info);
got_sighup = 0;
}
connection_count_up();
accepted = 1;
if (pool_config->parallel_mode)
{
/*
* do not accept new connection if any of DB node or SystemDB is down when operating in
* parallel mode
*/
int i;
for (i=0;i<NUM_BACKENDS;i++)
{
if (BACKEND_INFO(i).backend_status == CON_DOWN || SYSDB_STATUS == CON_DOWN)
{
StartupPacket *sp;
char *msg = "pgpool is not available in parallel query mode";
if (SYSDB_STATUS == CON_DOWN)
pool_log("Cannot accept() new connection. SystemDB is down");
else
pool_log("Cannot accept() new connection. %d th backend is down", i);
if ((cp = pool_open(afd)) == NULL)
{
close(afd);
child_exit(1);
}
sp = read_startup_packet(cp);
if (sp == NULL)
{
/* failed to read the startup packet. return to the accept() loop */
pool_close(cp);
child_exit(1);
}
pool_debug("do_accept: send error message to frontend");
if (sp->major == PROTO_MAJOR_V3)
{
char buf[256];
if (SYSDB_STATUS == CON_DOWN)
snprintf(buf, sizeof(buf), "SystemDB is down");
else
snprintf(buf, sizeof(buf), "%d th backend is down", i);
pool_send_error_message(cp, sp->major, "08S01",
msg,
buf,
((SYSDB_STATUS == CON_DOWN) ? "repair the SystemDB and restart pgpool"
: "repair the backend and restart pgpool"),
__FILE__,
__LINE__);
}
else
{
pool_send_error_message(cp, sp->major,
0,
msg,
"",
"",
"",
0);
}
pool_close(cp);
child_exit(1);
}
}
}
else
{
/*
* do not accept new connection if all DB nodes are down when operating in
* non parallel mode
*/
int i;
int found = 0;
for (i=0;i<NUM_BACKENDS;i++)
{
if (VALID_BACKEND(i))
{
found = 1;
}
}
if (found == 0)
{
pool_log("Cannot accept() new connection. all backends are down");
child_exit(1);
}
}
pool_debug("I am %d accept fd %d", getpid(), afd);
pool_getnameinfo_all(&saddr, remote_host, remote_port);
snprintf(remote_ps_data, sizeof(remote_ps_data),
remote_port[0] == '\0' ? "%s" : "%s(%s)",
remote_host, remote_port);
set_ps_display("accept connection", false);
/* log who is connecting */
if (pool_config->log_connections)
{
pool_log("connection received: host=%s%s%s",
remote_host, remote_port[0] ? " port=" : "", remote_port);
}
/* set NODELAY and KEEPALIVE options if INET connection */
if (inet)
{
int on = 1;
if (setsockopt(afd, IPPROTO_TCP, TCP_NODELAY,
(char *) &on,
sizeof(on)) < 0)
{
pool_error("do_accept: setsockopt() failed: %s", strerror(errno));
close(afd);
return NULL;
}
if (setsockopt(afd, SOL_SOCKET, SO_KEEPALIVE,
(char *) &on,
sizeof(on)) < 0)
{
pool_error("do_accept: setsockopt() failed: %s", strerror(errno));
close(afd);
return NULL;
}
}
if ((cp = pool_open(afd)) == NULL)
{
close(afd);
return NULL;
}
/* save ip addres for hba */
memcpy(&cp->raddr, &saddr, sizeof(SockAddr));
if (cp->raddr.addr.ss_family == 0)
cp->raddr.addr.ss_family = AF_UNIX;
return cp;
}
/*
* read startup packet
*/
static StartupPacket *read_startup_packet(POOL_CONNECTION *cp)
{
StartupPacket *sp;
StartupPacket_v2 *sp2;
int protov;
int len;
char *p;
sp = (StartupPacket *)calloc(sizeof(*sp), 1);
if (!sp)
{
pool_error("read_startup_packet: out of memory");
return NULL;
}
if (pool_config->authentication_timeout > 0)
{
pool_signal(SIGALRM, authentication_timeout);
alarm(pool_config->authentication_timeout);
}
/* read startup packet length */
if (pool_read(cp, &len, sizeof(len)))
{
return NULL;
}
len = ntohl(len);
len -= sizeof(len);
if (len <= 0)
{
pool_error("read_startup_packet: incorrect packet length (%d)", len);
}
else if (len >= MAX_STARTUP_PACKET_LENGTH)
{
pool_error("read_startup_packet: invalid startup packet");
pool_free_startup_packet(sp);
return NULL;
}
sp->startup_packet = calloc(len, 1);
if (!sp->startup_packet)
{
pool_error("read_startup_packet: out of memory");
pool_free_startup_packet(sp);
alarm(0);
pool_signal(SIGALRM, SIG_IGN);
return NULL;
}
/* read startup packet */
if (pool_read(cp, sp->startup_packet, len))
{
pool_free_startup_packet(sp);
alarm(0);
pool_signal(SIGALRM, SIG_IGN);
return NULL;
}
sp->len = len;
memcpy(&protov, sp->startup_packet, sizeof(protov));
sp->major = ntohl(protov)>>16;
sp->minor = ntohl(protov) & 0x0000ffff;
p = sp->startup_packet;
switch(sp->major)
{
case PROTO_MAJOR_V2: /* V2 */
sp2 = (StartupPacket_v2 *)(sp->startup_packet);
sp->database = calloc(SM_DATABASE+1, 1);
if (!sp->database)
{
pool_error("read_startup_packet: out of memory");
pool_free_startup_packet(sp);
alarm(0);
pool_signal(SIGALRM, SIG_IGN);
return NULL;
}
strncpy(sp->database, sp2->database, SM_DATABASE);
sp->user = calloc(SM_USER+1, 1);
if (!sp->user)
{
pool_error("read_startup_packet: out of memory");
pool_free_startup_packet(sp);
alarm(0);
pool_signal(SIGALRM, SIG_IGN);
return NULL;
}
strncpy(sp->user, sp2->user, SM_USER);
break;
case PROTO_MAJOR_V3: /* V3 */
p += sizeof(int); /* skip protocol version info */