-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathmain.c
2718 lines (2285 loc) · 77.2 KB
/
main.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
/*
* Copyright (C) 2025 The pgagroal community
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may
* be used to endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* pgagroal */
#include <pgagroal.h>
#include <configuration.h>
#include <connection.h>
#include <json.h>
#include <logging.h>
#include <management.h>
#include <memory.h>
#include <network.h>
#include <pipeline.h>
#include <pool.h>
#include <prometheus.h>
#include <remote.h>
#include <security.h>
#include <server.h>
#include <shmem.h>
#include <status.h>
#include <utils.h>
#include <worker.h>
/* system */
#include <arpa/inet.h>
#include <err.h>
#include <errno.h>
#include <ev.h>
#include <fcntl.h>
#include <getopt.h>
#include <netinet/in.h>
#include <openssl/crypto.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#ifdef HAVE_LINUX
#include <systemd/sd-daemon.h>
#endif
#define MAX_FDS 64
static void accept_main_cb(struct ev_loop* loop, struct ev_io* watcher, int revents);
static void accept_mgt_cb(struct ev_loop* loop, struct ev_io* watcher, int revents);
static void accept_transfer_cb(struct ev_loop* loop, struct ev_io* watcher, int revents);
static void accept_metrics_cb(struct ev_loop* loop, struct ev_io* watcher, int revents);
static void accept_management_cb(struct ev_loop* loop, struct ev_io* watcher, int revents);
static void shutdown_cb(struct ev_loop* loop, ev_signal* w, int revents);
static void reload_cb(struct ev_loop* loop, ev_signal* w, int revents);
static void graceful_cb(struct ev_loop* loop, ev_signal* w, int revents);
static void coredump_cb(struct ev_loop* loop, ev_signal* w, int revents);
static void idle_timeout_cb(struct ev_loop* loop, ev_periodic* w, int revents);
static void max_connection_age_cb(struct ev_loop* loop, ev_periodic* w, int revents);
static void rotate_frontend_password_cb(struct ev_loop* loop, ev_periodic* w, int revents);
static void validation_cb(struct ev_loop* loop, ev_periodic* w, int revents);
static void disconnect_client_cb(struct ev_loop* loop, ev_periodic* w, int revents);
static void frontend_user_password_startup(struct main_configuration* config);
static bool accept_fatal(int error);
static void add_client(pid_t pid);
static void remove_client(pid_t pid);
static bool reload_configuration(void);
static void create_pidfile_or_exit(void);
static void remove_pidfile(void);
static void shutdown_ports(void);
static volatile int keep_running = 1;
static char** argv_ptr;
static struct ev_loop* main_loop = NULL;
static struct accept_io io_main[MAX_FDS];
static struct accept_io io_mgt;
static struct accept_io io_uds;
static int* main_fds = NULL;
static int main_fds_length = -1;
static int unix_management_socket = -1;
static int unix_transfer_socket = -1;
static int unix_pgsql_socket = -1;
static struct accept_io io_metrics[MAX_FDS];
static int* metrics_fds = NULL;
static int metrics_fds_length = -1;
static struct accept_io io_management[MAX_FDS];
static int* management_fds = NULL;
static int management_fds_length = -1;
static struct pipeline main_pipeline;
static int known_fds[MAX_NUMBER_OF_CONNECTIONS];
static struct client* clients = NULL;
static struct accept_io io_transfer;
static void
start_mgt(void)
{
memset(&io_mgt, 0, sizeof(struct accept_io));
ev_io_init((struct ev_io*)&io_mgt, accept_mgt_cb, unix_management_socket, EV_READ);
io_mgt.socket = unix_management_socket;
io_mgt.argv = argv_ptr;
ev_io_start(main_loop, (struct ev_io*)&io_mgt);
}
static void
shutdown_mgt(void)
{
struct main_configuration* config;
config = (struct main_configuration*)shmem;
ev_io_stop(main_loop, (struct ev_io*)&io_mgt);
pgagroal_disconnect(unix_management_socket);
errno = 0;
pgagroal_remove_unix_socket(config->unix_socket_dir, MAIN_UDS);
errno = 0;
}
static void
start_transfer(void)
{
memset(&io_transfer, 0, sizeof(struct accept_io));
ev_io_init((struct ev_io*)&io_transfer, accept_transfer_cb, unix_transfer_socket, EV_READ);
io_transfer.socket = unix_transfer_socket;
io_transfer.argv = argv_ptr;
ev_io_start(main_loop, (struct ev_io*)&io_transfer);
}
static void
shutdown_transfer(void)
{
struct main_configuration* config;
config = (struct main_configuration*)shmem;
ev_io_stop(main_loop, (struct ev_io*)&io_transfer);
pgagroal_disconnect(unix_transfer_socket);
errno = 0;
pgagroal_remove_unix_socket(config->unix_socket_dir, TRANSFER_UDS);
errno = 0;
}
static void
start_uds(void)
{
memset(&io_uds, 0, sizeof(struct accept_io));
ev_io_init((struct ev_io*)&io_uds, accept_main_cb, unix_pgsql_socket, EV_READ);
io_uds.socket = unix_pgsql_socket;
io_uds.argv = argv_ptr;
ev_io_start(main_loop, (struct ev_io*)&io_uds);
}
static void
shutdown_uds(void)
{
char pgsql[MISC_LENGTH];
struct main_configuration* config;
config = (struct main_configuration*)shmem;
memset(&pgsql, 0, sizeof(pgsql));
snprintf(&pgsql[0], sizeof(pgsql), ".s.PGSQL.%d", config->common.port);
ev_io_stop(main_loop, (struct ev_io*)&io_uds);
pgagroal_disconnect(unix_pgsql_socket);
errno = 0;
pgagroal_remove_unix_socket(config->unix_socket_dir, &pgsql[0]);
errno = 0;
}
static void
start_io(void)
{
for (int i = 0; i < main_fds_length; i++)
{
int sockfd = *(main_fds + i);
memset(&io_main[i], 0, sizeof(struct accept_io));
ev_io_init((struct ev_io*)&io_main[i], accept_main_cb, sockfd, EV_READ);
io_main[i].socket = sockfd;
io_main[i].argv = argv_ptr;
ev_io_start(main_loop, (struct ev_io*)&io_main[i]);
}
}
static void
shutdown_io(void)
{
for (int i = 0; i < main_fds_length; i++)
{
ev_io_stop(main_loop, (struct ev_io*)&io_main[i]);
pgagroal_disconnect(io_main[i].socket);
errno = 0;
}
}
static void
start_metrics(void)
{
for (int i = 0; i < metrics_fds_length; i++)
{
int sockfd = *(metrics_fds + i);
memset(&io_metrics[i], 0, sizeof(struct accept_io));
ev_io_init((struct ev_io*)&io_metrics[i], accept_metrics_cb, sockfd, EV_READ);
io_metrics[i].socket = sockfd;
io_metrics[i].argv = argv_ptr;
ev_io_start(main_loop, (struct ev_io*)&io_metrics[i]);
}
}
static void
shutdown_metrics(void)
{
for (int i = 0; i < metrics_fds_length; i++)
{
ev_io_stop(main_loop, (struct ev_io*)&io_metrics[i]);
pgagroal_disconnect(io_metrics[i].socket);
errno = 0;
}
}
static void
start_management(void)
{
for (int i = 0; i < management_fds_length; i++)
{
int sockfd = *(management_fds + i);
memset(&io_management[i], 0, sizeof(struct accept_io));
ev_io_init((struct ev_io*)&io_management[i], accept_management_cb, sockfd, EV_READ);
io_management[i].socket = sockfd;
io_management[i].argv = argv_ptr;
ev_io_start(main_loop, (struct ev_io*)&io_management[i]);
}
}
static void
shutdown_management(void)
{
for (int i = 0; i < management_fds_length; i++)
{
ev_io_stop(main_loop, (struct ev_io*)&io_management[i]);
pgagroal_disconnect(io_management[i].socket);
errno = 0;
}
}
static void
version(void)
{
printf("pgagroal %s\n", PGAGROAL_VERSION);
exit(1);
}
static void
usage(void)
{
printf("pgagroal %s\n", PGAGROAL_VERSION);
printf(" High-performance connection pool for PostgreSQL\n");
printf("\n");
printf("Usage:\n");
printf(" pgagroal [ -c CONFIG_FILE ] [ -a HBA_FILE ] [ -d ]\n");
printf("\n");
printf("Options:\n");
printf(" -c, --config CONFIG_FILE Set the path to the pgagroal.conf file\n");
printf(" Default: %s\n", PGAGROAL_DEFAULT_CONF_FILE);
printf(" -a, --hba HBA_FILE Set the path to the pgagroal_hba.conf file\n");
printf(" Default: %s\n", PGAGROAL_DEFAULT_HBA_FILE);
printf(" -l, --limit LIMIT_FILE Set the path to the pgagroal_databases.conf file\n");
printf(" Default: %s\n", PGAGROAL_DEFAULT_LIMIT_FILE);
printf(" -u, --users USERS_FILE Set the path to the pgagroal_users.conf file\n");
printf(" Default: %s\n", PGAGROAL_DEFAULT_USERS_FILE);
printf(" -F, --frontend FRONTEND_USERS_FILE Set the path to the pgagroal_frontend_users.conf file\n");
printf(" Default: %s\n", PGAGROAL_DEFAULT_FRONTEND_USERS_FILE);
printf(" -A, --admins ADMINS_FILE Set the path to the pgagroal_admins.conf file\n");
printf(" Default: %s\n", PGAGROAL_DEFAULT_ADMINS_FILE);
printf(" -S, --superuser SUPERUSER_FILE Set the path to the pgagroal_superuser.conf file\n");
printf(" Default: %s\n", PGAGROAL_DEFAULT_SUPERUSER_FILE);
printf(" -d, --daemon Run as a daemon\n");
printf(" -V, --version Display version information\n");
printf(" -?, --help Display help\n");
printf("\n");
printf("pgagroal: %s\n", PGAGROAL_HOMEPAGE);
printf("Report bugs: %s\n", PGAGROAL_ISSUES);
}
int
main(int argc, char** argv)
{
char* configuration_path = NULL;
char* hba_path = NULL;
char* limit_path = NULL;
char* users_path = NULL;
char* frontend_users_path = NULL;
char* admins_path = NULL;
char* superuser_path = NULL;
bool daemon = false;
pid_t pid, sid;
#ifdef HAVE_LINUX
int sds;
#endif
bool has_unix_socket = false;
bool has_main_sockets = false;
void* tmp_shmem = NULL;
struct signal_info signal_watcher[6];
struct ev_periodic idle_timeout;
struct ev_periodic max_connection_age;
struct ev_periodic validation;
struct ev_periodic disconnect_client;
struct ev_periodic rotate_frontend_password;
struct rlimit flimit;
size_t shmem_size;
size_t pipeline_shmem_size = 0;
size_t prometheus_shmem_size = 0;
size_t prometheus_cache_shmem_size = 0;
size_t tmp_size;
struct main_configuration* config = NULL;
int ret;
int c;
bool conf_file_mandatory;
char message[MISC_LENGTH]; // a generic message used for errors
argv_ptr = argv;
while (1)
{
static struct option long_options[] =
{
{"config", required_argument, 0, 'c'},
{"hba", required_argument, 0, 'a'},
{"limit", required_argument, 0, 'l'},
{"users", required_argument, 0, 'u'},
{"frontend", required_argument, 0, 'F'},
{"admins", required_argument, 0, 'A'},
{"superuser", required_argument, 0, 'S'},
{"daemon", no_argument, 0, 'd'},
{"version", no_argument, 0, 'V'},
{"help", no_argument, 0, '?'}
};
int option_index = 0;
c = getopt_long (argc, argv, "dV?a:c:l:u:F:A:S:",
long_options, &option_index);
if (c == -1)
{
break;
}
switch (c)
{
case 'a':
hba_path = optarg;
break;
case 'c':
configuration_path = optarg;
break;
case 'l':
limit_path = optarg;
break;
case 'u':
users_path = optarg;
break;
case 'F':
frontend_users_path = optarg;
break;
case 'A':
admins_path = optarg;
break;
case 'S':
superuser_path = optarg;
break;
case 'd':
daemon = true;
break;
case 'V':
version();
break;
case '?':
usage();
exit(1);
break;
default:
break;
}
}
if (getuid() == 0)
{
#ifdef HAVE_LINUX
sd_notify(0, "STATUS=Using the root account is not allowed");
#endif
errx(1, "Using the root account is not allowed");
}
shmem_size = sizeof(struct main_configuration);
if (pgagroal_create_shared_memory(shmem_size, HUGEPAGE_OFF, &shmem))
{
#ifdef HAVE_LINUX
sd_notifyf(0, "STATUS=Error in creating shared memory");
#endif
errx(1, "Error in creating shared memory");
}
pgagroal_init_configuration(shmem);
config = (struct main_configuration*)shmem;
memset(&known_fds, 0, sizeof(known_fds));
memset(message, 0, MISC_LENGTH);
// the main configuration file is mandatory!
configuration_path = configuration_path != NULL ? configuration_path : PGAGROAL_DEFAULT_CONF_FILE;
if ((ret = pgagroal_read_configuration(shmem, configuration_path, true)) != PGAGROAL_CONFIGURATION_STATUS_OK)
{
// the configuration has some problem, build up a descriptive message
if (ret == PGAGROAL_CONFIGURATION_STATUS_FILE_NOT_FOUND)
{
snprintf(message, MISC_LENGTH, "Configuration file not found");
}
else if (ret == PGAGROAL_CONFIGURATION_STATUS_FILE_TOO_BIG)
{
snprintf(message, MISC_LENGTH, "Too many sections");
}
else if (ret == PGAGROAL_CONFIGURATION_STATUS_KO)
{
snprintf(message, MISC_LENGTH, "Invalid configuration file");
}
else if (ret > 0)
{
snprintf(message, MISC_LENGTH, "%d problematic or duplicated section%c",
ret,
ret > 1 ? 's' : ' ');
}
#ifdef HAVE_LINUX
sd_notifyf(0, "STATUS=%s: %s", message, configuration_path);
#endif
errx(1, "%s (file <%s>)", message, configuration_path);
}
memcpy(&config->common.configuration_path[0], configuration_path, MIN(strlen(configuration_path), MAX_PATH - 1));
// the HBA file is mandatory!
hba_path = hba_path != NULL ? hba_path : PGAGROAL_DEFAULT_HBA_FILE;
memset(message, 0, MISC_LENGTH);
ret = pgagroal_read_hba_configuration(shmem, hba_path);
if (ret == PGAGROAL_CONFIGURATION_STATUS_FILE_NOT_FOUND)
{
snprintf(message, MISC_LENGTH, "HBA configuration file not found");
#ifdef HAVE_LINUX
sd_notifyf(0, "STATUS=%s: %s", message, hba_path);
#endif
errx(1, "%s (file <%s>)", message, hba_path);
}
else if (ret == PGAGROAL_CONFIGURATION_STATUS_FILE_TOO_BIG)
{
snprintf(message, MISC_LENGTH, "HBA too many entries (max %d)", NUMBER_OF_HBAS);
#ifdef HAVE_LINUX
sd_notifyf(0, "STATUS=%s: %s", message, hba_path);
#endif
errx(1, "%s (file <%s>)", message, hba_path);
}
memcpy(&config->hba_path[0], hba_path, MIN(strlen(hba_path), MAX_PATH - 1));
conf_file_mandatory = true;
read_limit_path:
if (limit_path != NULL)
{
memset(message, 0, MISC_LENGTH);
ret = pgagroal_read_limit_configuration(shmem, limit_path);
if (ret == PGAGROAL_CONFIGURATION_STATUS_OK)
{
memcpy(&config->limit_path[0], limit_path, MIN(strlen(limit_path), MAX_PATH - 1));
}
else if (conf_file_mandatory && ret == PGAGROAL_CONFIGURATION_STATUS_FILE_NOT_FOUND)
{
snprintf(message, MISC_LENGTH, "LIMIT configuration file not found");
printf("pgagroal: %s (file <%s>)\n", message, limit_path);
#ifdef HAVE_LINUX
sd_notifyf(0, "STATUS=%s: %s", message, limit_path);
#endif
exit(1);
}
else if (ret == PGAGROAL_CONFIGURATION_STATUS_FILE_TOO_BIG)
{
snprintf(message, MISC_LENGTH, "Too many limit entries");
#ifdef HAVE_LINUX
sd_notifyf(0, "STATUS=%s: %s", message, limit_path);
#endif
errx(1, "%s (file <%s>)", message, limit_path);
}
}
else
{
// the user did not specify a file on the command line
// so try the default one and allow it to be missing
limit_path = PGAGROAL_DEFAULT_LIMIT_FILE;
conf_file_mandatory = false;
goto read_limit_path;
}
conf_file_mandatory = true;
read_users_path:
if (users_path != NULL)
{
memset(message, 0, MISC_LENGTH);
ret = pgagroal_read_users_configuration(shmem, users_path);
if (ret == PGAGROAL_CONFIGURATION_STATUS_OK)
{
memcpy(&config->users_path[0], users_path, MIN(strlen(users_path), MAX_PATH - 1));
}
else if (ret == PGAGROAL_CONFIGURATION_STATUS_FILE_NOT_FOUND && conf_file_mandatory)
{
snprintf(message, MISC_LENGTH, "USERS configuration file not found");
#ifdef HAVE_LINUX
sd_notifyf(0, "STATUS=%s : %s", message, users_path);
#endif
errx(1, "%s (file <%s>)", message, users_path);
}
else if (ret == PGAGROAL_CONFIGURATION_STATUS_KO
|| ret == PGAGROAL_CONFIGURATION_STATUS_CANNOT_DECRYPT)
{
snprintf(message, MISC_LENGTH, "Invalid master key file");
#ifdef HAVE_LINUX
sd_notifyf(0, "STATUS=%s: <%s>", message, users_path);
#endif
errx(1, "%s (file <%s>)", message, users_path);
}
else if (ret == PGAGROAL_CONFIGURATION_STATUS_FILE_TOO_BIG)
{
snprintf(message, MISC_LENGTH, "USERS: too many users defined (%d, max %d)", config->number_of_users, NUMBER_OF_USERS);
#ifdef HAVE_LINUX
sd_notifyf(0, "STATUS=%s: %s", message, users_path);
#endif
errx(1, "%s (file <%s>)", message, users_path);
}
}
else
{
// the user did not specify a file on the command line
// so try the default one and allow it to be missing
users_path = PGAGROAL_DEFAULT_USERS_FILE;
conf_file_mandatory = false;
goto read_users_path;
}
conf_file_mandatory = true;
read_frontend_users_path:
if (frontend_users_path != NULL)
{
ret = pgagroal_read_frontend_users_configuration(shmem, frontend_users_path);
if (ret == PGAGROAL_CONFIGURATION_STATUS_FILE_NOT_FOUND && conf_file_mandatory)
{
memset(message, 0, MISC_LENGTH);
snprintf(message, MISC_LENGTH, "FRONTEND USERS configuration file not found");
#ifdef HAVE_LINUX
sd_notifyf(0, "STATUS=%s: %s", message, frontend_users_path);
#endif
errx(1, "%s (file <%s>)", message, frontend_users_path);
}
else if (ret == PGAGROAL_CONFIGURATION_STATUS_CANNOT_DECRYPT
|| ret == PGAGROAL_CONFIGURATION_STATUS_KO)
{
#ifdef HAVE_LINUX
sd_notifyf(0, "STATUS=Invalid master key file: <%s>", frontend_users_path);
#endif
errx(1, "%s (file <%s>)", message, frontend_users_path);
}
else if (ret == PGAGROAL_CONFIGURATION_STATUS_FILE_TOO_BIG)
{
memset(message, 0, MISC_LENGTH);
snprintf(message, MISC_LENGTH, "FRONTEND USERS: Too many users defined %d (max %d)",
config->number_of_frontend_users, NUMBER_OF_USERS);
#ifdef HAVE_LINUX
sd_notifyf(0, "STATUS=%s: %s", message, frontend_users_path);
#endif
errx(1, "%s (file <%s>)", message, frontend_users_path);
}
else if (ret == PGAGROAL_CONFIGURATION_STATUS_OK)
{
memcpy(&config->frontend_users_path[0], frontend_users_path, MIN(strlen(frontend_users_path), MAX_PATH - 1));
}
}
else
{
// the user did not specify a file on the command line
// so try the default one and allow it to be missing
frontend_users_path = PGAGROAL_DEFAULT_FRONTEND_USERS_FILE;
conf_file_mandatory = false;
goto read_frontend_users_path;
}
conf_file_mandatory = true;
read_admins_path:
if (admins_path != NULL)
{
memset(message, 0, MISC_LENGTH);
ret = pgagroal_read_admins_configuration(shmem, admins_path);
if (ret == PGAGROAL_CONFIGURATION_STATUS_FILE_NOT_FOUND && conf_file_mandatory)
{
snprintf(message, MISC_LENGTH, "ADMINS configuration file not found");
#ifdef HAVE_LINUX
sd_notifyf(0, "STATUS=%s: %s", message, admins_path);
#endif
errx(1, "%s (file <%s>)", message, admins_path);
}
else if (ret == PGAGROAL_CONFIGURATION_STATUS_CANNOT_DECRYPT
|| ret == PGAGROAL_CONFIGURATION_STATUS_KO)
{
#ifdef HAVE_LINUX
sd_notifyf(0, "STATUS=Invalid master key file: <%s>", admins_path);
#endif
errx(1, "Invalid master key file (file <%s>)", admins_path);
}
else if (ret == PGAGROAL_CONFIGURATION_STATUS_FILE_TOO_BIG)
{
snprintf(message, MISC_LENGTH, "Too many admins defined %d (max %d)", config->number_of_admins, NUMBER_OF_ADMINS);
#ifdef HAVE_LINUX
sd_notifyf(0, "STATUS=%s %s", message, admins_path);
#endif
errx(1, "%s (file <%s>)", message, admins_path);
}
else if (ret == PGAGROAL_CONFIGURATION_STATUS_OK)
{
memcpy(&config->admins_path[0], admins_path, MIN(strlen(admins_path), MAX_PATH - 1));
}
}
else
{
// the user did not specify a file on the command line
// so try the default one and allow it to be missing
admins_path = PGAGROAL_DEFAULT_ADMINS_FILE;
conf_file_mandatory = false;
goto read_admins_path;
}
conf_file_mandatory = true;
read_superuser_path:
if (superuser_path != NULL)
{
ret = pgagroal_read_superuser_configuration(shmem, superuser_path);
memset(message, 0, MISC_LENGTH);
if (ret == PGAGROAL_CONFIGURATION_STATUS_FILE_NOT_FOUND && conf_file_mandatory)
{
snprintf(message, MISC_LENGTH, "SUPERUSER configuration file not found");
#ifdef HAVE_LINUX
sd_notifyf(0, "STATUS=%s: %s", message, superuser_path);
#endif
errx(1, "%s (file <%s>)", message, superuser_path);
}
else if (ret == PGAGROAL_CONFIGURATION_STATUS_CANNOT_DECRYPT || ret == PGAGROAL_CONFIGURATION_STATUS_KO)
{
#ifdef HAVE_LINUX
sd_notifyf(0, "STATUS=Invalid master key file: <%s>", superuser_path);
#endif
errx(1, "Invalid master key file (file <%s>)", superuser_path);
}
else if (ret == PGAGROAL_CONFIGURATION_STATUS_FILE_TOO_BIG)
{
snprintf(message, MISC_LENGTH, "SUPERUSER: Too many superusers defined (max 1)");
#ifdef HAVE_LINUX
sd_notifyf(0, "STATUS=%s: %s", message, superuser_path);
#endif
errx(1, "%s (file <%s>)", message, superuser_path);
}
else if (ret == PGAGROAL_CONFIGURATION_STATUS_OK)
{
memcpy(&config->superuser_path[0], superuser_path, MIN(strlen(superuser_path), MAX_PATH - 1));
}
}
else
{
// the user did not specify a file on the command line
// so try the default one and allow it to be missing
superuser_path = PGAGROAL_DEFAULT_SUPERUSER_FILE;
conf_file_mandatory = false;
goto read_superuser_path;
}
/* systemd sockets */
#ifdef HAVE_LINUX
sds = sd_listen_fds(0);
if (sds > 0)
{
int m = 0;
main_fds_length = 0;
for (int i = 0; i < sds; i++)
{
int fd = SD_LISTEN_FDS_START + i;
if (sd_is_socket(fd, AF_INET, 0, -1) || sd_is_socket(fd, AF_INET6, 0, -1))
{
main_fds_length++;
}
}
if (main_fds_length > 0)
{
main_fds = malloc(main_fds_length * sizeof(int));
}
for (int i = 0; i < sds; i++)
{
int fd = SD_LISTEN_FDS_START + i;
if (sd_is_socket(fd, AF_UNIX, 0, -1))
{
unix_pgsql_socket = fd;
has_unix_socket = true;
}
else if (sd_is_socket(fd, AF_INET, 0, -1) || sd_is_socket(fd, AF_INET6, 0, -1))
{
*(main_fds + (m * sizeof(int))) = fd;
has_main_sockets = true;
m++;
}
}
}
#endif
if (pgagroal_init_logging())
{
#ifdef HAVE_LINUX
sd_notify(0, "STATUS=Failed to init logging");
#endif
exit(1);
}
if (pgagroal_start_logging())
{
#ifdef HAVE_LINUX
sd_notify(0, "STATUS=Failed to start logging");
#endif
errx(1, "Failed to start logging");
}
if (config->common.metrics > 0)
{
if (pgagroal_init_prometheus(&prometheus_shmem_size, &prometheus_shmem))
{
#ifdef HAVE_LINUX
sd_notifyf(0, "STATUS=Error in creating and initializing prometheus shared memory");
#endif
errx(1, "Error in creating and initializing prometheus shared memory");
}
if (pgagroal_init_prometheus_cache(&prometheus_cache_shmem_size, &prometheus_cache_shmem))
{
#ifdef HAVE_LINUX
sd_notifyf(0, "STATUS=Error in creating and initializing prometheus cache shared memory");
#endif
errx(1, "Error in creating and initializing prometheus cache shared memory");
}
}
if (pgagroal_validate_configuration(shmem, has_unix_socket, has_main_sockets))
{
#ifdef HAVE_LINUX
sd_notify(0, "STATUS=Invalid configuration");
#endif
errx(1, "Invalid configuration");
}
frontend_user_password_startup(config);
if (pgagroal_validate_hba_configuration(shmem))
{
#ifdef HAVE_LINUX
sd_notify(0, "STATUS=Invalid HBA configuration");
#endif
errx(1, "Invalid HBA configuration");
}
if (pgagroal_validate_limit_configuration(shmem))
{
#ifdef HAVE_LINUX
sd_notify(0, "STATUS=Invalid LIMIT configuration");
#endif
errx(1, "Invalid LIMIT configuration");
}
if (pgagroal_validate_users_configuration(shmem))
{
#ifdef HAVE_LINUX
sd_notify(0, "STATUS=Invalid USERS configuration");
#endif
errx(1, "Invalid USERS configuration");
}
if (pgagroal_validate_frontend_users_configuration(shmem))
{
#ifdef HAVE_LINUX
sd_notify(0, "STATUS=Invalid FRONTEND USERS configuration");
#endif
errx(1, "Invalid FRONTEND USERS configuration");
}
if (pgagroal_validate_admins_configuration(shmem))
{
#ifdef HAVE_LINUX
sd_notify(0, "STATUS=Invalid ADMINS configuration");
#endif
errx(1, "Invalid ADMINS configuration");
}
if (pgagroal_resize_shared_memory(shmem_size, shmem, &tmp_size, &tmp_shmem))
{
#ifdef HAVE_LINUX
sd_notifyf(0, "STATUS=Error in creating shared memory");
#endif
errx(1, "Error in creating shared memory");
}
if (pgagroal_destroy_shared_memory(shmem, shmem_size) == -1)
{
#ifdef HAVE_LINUX
sd_notifyf(0, "STATUS=Error in destroying shared memory");
#endif
errx(1, "Error in destroying shared memory");
}
shmem_size = tmp_size;
shmem = tmp_shmem;
config = (struct main_configuration*)shmem;
pgagroal_memory_init();
if (getrlimit(RLIMIT_NOFILE, &flimit) == -1)
{
#ifdef HAVE_LINUX
sd_notifyf(0, "STATUS=Unable to find limit due to %s", strerror(errno));
#endif
err(1, "Unable to find limit");
}
/* We are "reserving" 30 file descriptors for pgagroal main */
if (config->max_connections > (flimit.rlim_cur - 30))
{
#ifdef HAVE_LINUX
sd_notifyf(0,
"STATUS=max_connections is larger than the file descriptor limit (%ld available)",
(long)(flimit.rlim_cur - 30));
#endif
errx(1, "max_connections is larger than the file descriptor limit (%ld available)", (long)(flimit.rlim_cur - 30));
}
if (daemon)
{
if (config->common.log_type == PGAGROAL_LOGGING_TYPE_CONSOLE)
{
#ifdef HAVE_LINUX
sd_notify(0, "STATUS=Daemon mode can't be used with console logging");
#endif
errx(1, "Daemon mode can't be used with console logging");
}
pid = fork();
if (pid < 0)
{
#ifdef HAVE_LINUX
sd_notify(0, "STATUS=Daemon mode failed");
#endif
errx(1, "Daemon mode failed");
}
if (pid > 0)
{
exit(0);
}
/* We are a daemon now */
umask(0);
sid = setsid();
if (sid < 0)
{
exit(1);
}
}
create_pidfile_or_exit();
pgagroal_pool_init();
pgagroal_initialize_random();
pgagroal_set_proc_title(argc, argv, "main", NULL);
/* Bind Unix Domain Socket: Main */
if (pgagroal_bind_unix_socket(config->unix_socket_dir, MAIN_UDS, &unix_management_socket))
{
pgagroal_log_fatal("pgagroal: Could not bind to %s/%s", config->unix_socket_dir, MAIN_UDS);
#ifdef HAVE_LINUX
sd_notifyf(0, "STATUS=Could not bind to %s/%s", config->unix_socket_dir, MAIN_UDS);
#endif
goto error;
}
/* Bind Unix Domain Socket: Transfer */
if (pgagroal_bind_unix_socket(config->unix_socket_dir, TRANSFER_UDS, &unix_transfer_socket))
{
pgagroal_log_fatal("pgagroal: Could not bind to %s/%s", config->unix_socket_dir, TRANSFER_UDS);
#ifdef HAVE_LINUX
sd_notifyf(0, "STATUS=Could not bind to %s/%s", config->unix_socket_dir, TRANSFER_UDS);
#endif
goto error;
}
if (!has_unix_socket)
{
char pgsql[MISC_LENGTH];
memset(&pgsql, 0, sizeof(pgsql));
snprintf(&pgsql[0], sizeof(pgsql), ".s.PGSQL.%d", config->common.port);
if (pgagroal_bind_unix_socket(config->unix_socket_dir, &pgsql[0], &unix_pgsql_socket))
{
pgagroal_log_fatal("pgagroal: Could not bind to %s/%s", config->unix_socket_dir, &pgsql[0]);
#ifdef HAVE_LINUX
sd_notifyf(0, "STATUS=Could not bind to %s/%s", config->unix_socket_dir, &pgsql[0]);
#endif
goto error;
}
}
/* Bind main socket */
if (!has_main_sockets)
{
if (pgagroal_bind(config->common.host, config->common.port, &main_fds, &main_fds_length, config->non_blocking, config->nodelay, config->backlog))
{
pgagroal_log_fatal("pgagroal: Could not bind to %s:%d", config->common.host, config->common.port);
#ifdef HAVE_LINUX
sd_notifyf(0, "STATUS=Could not bind to %s:%d", config->common.host, config->common.port);
#endif
goto error;
}
}
if (main_fds_length > MAX_FDS)
{
pgagroal_log_fatal("pgagroal: Too many descriptors %d", main_fds_length);
#ifdef HAVE_LINUX
sd_notifyf(0, "STATUS=Too many descriptors %d", main_fds_length);
#endif
goto error;
}
/* libev */
main_loop = ev_default_loop(pgagroal_libev(config->libev));
if (!main_loop)
{
pgagroal_log_fatal("pgagroal: No loop implementation (%x) (%x)",
pgagroal_libev(config->libev), ev_supported_backends());
#ifdef HAVE_LINUX
sd_notifyf(0, "STATUS=No loop implementation (%x) (%x)", pgagroal_libev(config->libev), ev_supported_backends());
#endif
goto error;
}
ev_signal_init((struct ev_signal*)&signal_watcher[0], shutdown_cb, SIGTERM);
ev_signal_init((struct ev_signal*)&signal_watcher[1], reload_cb, SIGHUP);
ev_signal_init((struct ev_signal*)&signal_watcher[2], shutdown_cb, SIGINT);
ev_signal_init((struct ev_signal*)&signal_watcher[3], graceful_cb, SIGTRAP);
ev_signal_init((struct ev_signal*)&signal_watcher[4], coredump_cb, SIGABRT);
ev_signal_init((struct ev_signal*)&signal_watcher[5], shutdown_cb, SIGALRM);
for (int i = 0; i < 6; i++)
{