-
Notifications
You must be signed in to change notification settings - Fork 224
/
Copy pathcdb2api.c
7342 lines (6511 loc) · 237 KB
/
cdb2api.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 2015, 2023, Bloomberg Finance L.P.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <inttypes.h>
#include <alloca.h>
#include <stdarg.h>
#include <sbuf2.h>
#include <limits.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>
#include <sys/time.h>
#include <sys/poll.h>
#include <sys/socket.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <math.h> // ceil
#include <limits.h> // int_max
#include "cdb2api.h"
#include "sqlquery.pb-c.h"
#include "sqlresponse.pb-c.h"
#include "str_util.h" /* QUOTE */
/*
*******************************************************************************
** WARNING: If you add any internal configuration state to this file, please
** update the reset_the_configuration() function as well to include
** it.
*******************************************************************************
*/
#define SOCKPOOL_SOCKET_NAME "/tmp/sockpool.socket"
static char *SOCKPOOL_OTHER_NAME = NULL;
#define COMDB2DB "comdb2db"
#define COMDB2DB_NUM 32432
#define MAX_BUFSIZE_ONSTACK 8192
#define CDB2HOSTNAME_LEN 128
static char COMDB2DB_OVERRIDE[32] = {0};
static int COMDB2DB_NUM_OVERRIDE = 0;
#define CDB2DBCONFIG_NOBBENV_DEFAULT "/opt/bb/etc/cdb2/config/comdb2db.cfg"
static char CDB2DBCONFIG_NOBBENV[512] = CDB2DBCONFIG_NOBBENV_DEFAULT;
/* The real path is COMDB2_ROOT + CDB2DBCONFIG_NOBBENV_PATH */
#define CDB2DBCONFIG_NOBBENV_PATH_DEFAULT "/etc/cdb2/config.d/"
static char CDB2DBCONFIG_NOBBENV_PATH[] = CDB2DBCONFIG_NOBBENV_PATH_DEFAULT; /* READ-ONLY */
#define CDB2DBCONFIG_TEMP_BB_BIN_DEFAULT "/bb/bin/comdb2db.cfg"
static char CDB2DBCONFIG_TEMP_BB_BIN[512] = "/bb/bin/comdb2db.cfg";
static char *CDB2DBCONFIG_BUF = NULL;
static char cdb2_default_cluster[64] = "";
static char cdb2_comdb2dbname[32] = "";
#define API_DRIVER_NAME open_cdb2api
static char api_driver_name[] = QUOTE(API_DRIVER_NAME);
#ifndef API_DRIVER_VERSION
#define API_DRIVER_VERSION latest
#endif
static char api_driver_version[] = QUOTE(API_DRIVER_VERSION);
#ifndef CDB2_DNS_SUFFIX
#define CDB2_DNS_SUFFIX
#endif
static char cdb2_dnssuffix[255] = QUOTE(CDB2_DNS_SUFFIX);
static char cdb2_machine_room[16] = "";
#define CDB2_PORTMUXPORT_DEFAULT 5105
static int CDB2_PORTMUXPORT = CDB2_PORTMUXPORT_DEFAULT;
#define MAX_RETRIES_DEFAULT 21
static int MAX_RETRIES = MAX_RETRIES_DEFAULT; /* We are looping each node twice. */
#define MIN_RETRIES_DEFAULT 16
static int MIN_RETRIES = MIN_RETRIES_DEFAULT;
#define CDB2_CONNECT_TIMEOUT_DEFAULT 100
static int CDB2_CONNECT_TIMEOUT = CDB2_CONNECT_TIMEOUT_DEFAULT;
#define CDB2_AUTO_CONSUME_TIMEOUT_MS_DEFAULT 0
static int CDB2_AUTO_CONSUME_TIMEOUT_MS = CDB2_AUTO_CONSUME_TIMEOUT_MS_DEFAULT;
#define COMDB2DB_TIMEOUT_DEFAULT 2000
static int COMDB2DB_TIMEOUT = COMDB2DB_TIMEOUT_DEFAULT;
#define CDB2_API_CALL_TIMEOUT_DEFAULT 120000 /* defaults to 2 minute */
static int CDB2_API_CALL_TIMEOUT = CDB2_API_CALL_TIMEOUT_DEFAULT;
#define CDB2_SOCKET_TIMEOUT_DEFAULT 5000
static int CDB2_SOCKET_TIMEOUT = CDB2_SOCKET_TIMEOUT_DEFAULT;
#define CDB2_POLL_TIMEOUT_DEFAULT 250
static int CDB2_POLL_TIMEOUT = CDB2_POLL_TIMEOUT_DEFAULT;
#define CDB2_PROTOBUF_SIZE_DEFAULT 4096
static int CDB2_PROTOBUF_SIZE = CDB2_PROTOBUF_SIZE_DEFAULT;
#define CDB2_TCPBUFSZ_DEFAULT 0
static int cdb2_tcpbufsz = CDB2_TCPBUFSZ_DEFAULT;
#define CDB2CFG_OVERRIDE_DEFAULT 0
static int cdb2cfg_override = CDB2CFG_OVERRIDE_DEFAULT;
#define CDB2_REQUEST_FP_DEFAULT 0
static int CDB2_REQUEST_FP = CDB2_REQUEST_FP_DEFAULT;
#define CDB2_GET_HOSTNAME_FROM_SOCKPOOL_FD_DEFAULT 1
static int CDB2_GET_HOSTNAME_FROM_SOCKPOOL_FD = CDB2_GET_HOSTNAME_FROM_SOCKPOOL_FD_DEFAULT;
#include <openssl/conf.h>
#include <openssl/crypto.h>
static ssl_mode cdb2_c_ssl_mode = SSL_ALLOW;
static char cdb2_sslcertpath[PATH_MAX];
static char cdb2_sslcert[PATH_MAX];
static char cdb2_sslkey[PATH_MAX];
static char cdb2_sslca[PATH_MAX];
#if HAVE_CRL
static char cdb2_sslcrl[PATH_MAX];
#endif
#ifdef NID_host /* available as of RFC 4524 */
#define CDB2_NID_DBNAME_DEFAULT NID_host
#else
#define CDB2_NID_DBNAME_DEFAULT NID_commonName
#endif
int cdb2_nid_dbname = CDB2_NID_DBNAME_DEFAULT;
#define CDB2_CACHE_SSL_SESS_DEFAULT 0
static int cdb2_cache_ssl_sess = CDB2_CACHE_SSL_SESS_DEFAULT;
#define CDB2_MIN_TLS_VER_DEFAULT 0
static double cdb2_min_tls_ver = CDB2_MIN_TLS_VER_DEFAULT;
static pthread_mutex_t cdb2_ssl_sess_lock = PTHREAD_MUTEX_INITIALIZER;
typedef struct cdb2_ssl_sess cdb2_ssl_sess;
static cdb2_ssl_sess *cdb2_get_ssl_sessions(cdb2_hndl_tp *hndl);
static int cdb2_set_ssl_sessions(cdb2_hndl_tp *hndl,
cdb2_ssl_sess *sessions);
static int cdb2_add_ssl_session(cdb2_hndl_tp *hndl);
static pthread_mutex_t cdb2_cfg_lock = PTHREAD_MUTEX_INITIALIZER;
#define CDB2_ALLOW_PMUX_ROUTE_DEFAULT 0
static int cdb2_allow_pmux_route = CDB2_ALLOW_PMUX_ROUTE_DEFAULT;
static int _PID; /* ONE-TIME */
static int _MACHINE_ID; /* ONE-TIME */
static char *_ARGV0; /* ONE-TIME */
static int iam_identity = 0;
#define DB_TZNAME_DEFAULT "America/New_York"
#define MAX_NODES 128
#define MAX_CONTEXTS 10 /* Maximum stack size for storing context messages */
#define MAX_CONTEXT_LEN 100 /* Maximum allowed length of a context message */
#define MAX_STACK 512 /* Size of call-stack which opened the handle */
pthread_mutex_t cdb2_sockpool_mutex = PTHREAD_MUTEX_INITIALIZER;
#define MAX_SOCKPOOL_FDS 8
#include <netdb.h>
static pthread_once_t init_once = PTHREAD_ONCE_INIT;
static int log_calls = 0; /* ONE-TIME */
static void reset_sockpool(void);
struct cdb2_event {
cdb2_event_type types;
cdb2_event_ctrl ctrls;
cdb2_event_callback cb;
int global;
void *user_arg;
cdb2_event *next;
int argc;
cdb2_event_arg argv[1];
};
static pthread_mutex_t cdb2_event_mutex = PTHREAD_MUTEX_INITIALIZER;
static cdb2_event cdb2_gbl_events;
static int cdb2_gbl_event_version;
static cdb2_event *cdb2_next_callback(cdb2_hndl_tp *, cdb2_event_type,
cdb2_event *);
static void *cdb2_invoke_callback(cdb2_hndl_tp *, cdb2_event *, int, ...);
static int refresh_gbl_events_on_hndl(cdb2_hndl_tp *);
#define PROCESS_EVENT_CTRL_BEFORE(h, e, rc, callbackrc, ovwrrc) \
do { \
if (e->ctrls & CDB2_OVERWRITE_RETURN_VALUE) { \
ovwrrc = 1; \
rc = (int)(intptr_t)callbackrc; \
} \
if (e->ctrls & CDB2_AS_HANDLE_SPECIFIC_ARG) \
h->user_arg = callbackrc; \
} while (0)
#define PROCESS_EVENT_CTRL_AFTER(h, e, rc, callbackrc) \
do { \
if (e->ctrls & CDB2_OVERWRITE_RETURN_VALUE) { \
rc = (int)(intptr_t)callbackrc; \
} \
if (e->ctrls & CDB2_AS_HANDLE_SPECIFIC_ARG) \
h->user_arg = callbackrc; \
} while (0)
typedef void (*cdb2_init_t)(void);
/* Undocumented compile-time library installation/uninstallation routine. */
#ifndef CDB2_INSTALL_LIBS
#define CDB2_INSTALL_LIBS NULL
#else
extern void CDB2_INSTALL_LIBS(void);
#endif
void (*cdb2_install)(void) = CDB2_INSTALL_LIBS;
#ifndef CDB2_UNINSTALL_LIBS
#define CDB2_UNINSTALL_LIBS NULL
#else
extern void CDB2_UNINSTALL_LIBS(void);
#endif
void (*cdb2_uninstall)(void) = CDB2_UNINSTALL_LIBS;
#ifndef CDB2_IDENTITY_CALLBACKS
struct cdb2_identity *identity_cb = NULL;
#else
extern struct cdb2_identity CDB2_IDENTITY_CALLBACKS;
struct cdb2_identity *identity_cb = &CDB2_IDENTITY_CALLBACKS;
#endif
#ifndef WITH_DL_LIBS
#define WITH_DL_LIBS 0
#endif
#if WITH_DL_LIBS
#include <dlfcn.h>
void cdb2_set_install_libs(void (*ptr)(void)) { cdb2_install = ptr; }
void cdb2_set_uninstall_libs(void (*ptr)(void)) { cdb2_uninstall = ptr; }
#endif
#define debugprint(fmt, args...) \
do { \
if (hndl && hndl->debug_trace) \
fprintf(stderr, "td 0x%p %s:%d " fmt, (void *)pthread_self(), \
__func__, __LINE__, ##args); \
} while (0);
/*
** NOTE: This function is designed to reset the internal state of this module,
** related to the configuration, back to initial defaults. It should
** allow for the subsequent reconfiguration using different parameters.
** Currently, it is surfaced via passing a NULL value to the public APIs
** cdb2_set_comdb2db_config() and cdb2_set_comdb2db_info().
*/
static void reset_the_configuration(void)
{
if (log_calls)
fprintf(stderr, "%p> %s()\n", (void *)pthread_self(), __func__);
memset(CDB2DBCONFIG_NOBBENV, 0, sizeof(CDB2DBCONFIG_NOBBENV));
strncpy(CDB2DBCONFIG_NOBBENV, CDB2DBCONFIG_NOBBENV_DEFAULT, 511);
memset(CDB2DBCONFIG_TEMP_BB_BIN, 0, sizeof(CDB2DBCONFIG_TEMP_BB_BIN));
strncpy(CDB2DBCONFIG_TEMP_BB_BIN, CDB2DBCONFIG_TEMP_BB_BIN_DEFAULT, 511);
memset(COMDB2DB_OVERRIDE, 0, sizeof(COMDB2DB_OVERRIDE));
COMDB2DB_NUM_OVERRIDE = 0;
if (CDB2DBCONFIG_BUF != NULL) {
free(CDB2DBCONFIG_BUF);
CDB2DBCONFIG_BUF = NULL;
}
memset(cdb2_default_cluster, 0, sizeof(cdb2_default_cluster));
memset(cdb2_comdb2dbname, 0, sizeof(cdb2_comdb2dbname));
memset(cdb2_dnssuffix, 0, sizeof(cdb2_dnssuffix));
memset(cdb2_machine_room, 0, sizeof(cdb2_machine_room));
CDB2_PORTMUXPORT = CDB2_PORTMUXPORT_DEFAULT;
MAX_RETRIES = MAX_RETRIES_DEFAULT;
MIN_RETRIES = MIN_RETRIES_DEFAULT;
CDB2_CONNECT_TIMEOUT = CDB2_CONNECT_TIMEOUT_DEFAULT;
CDB2_API_CALL_TIMEOUT = CDB2_API_CALL_TIMEOUT_DEFAULT;
CDB2_SOCKET_TIMEOUT = CDB2_SOCKET_TIMEOUT_DEFAULT;
CDB2_POLL_TIMEOUT = CDB2_POLL_TIMEOUT_DEFAULT;
CDB2_AUTO_CONSUME_TIMEOUT_MS = CDB2_AUTO_CONSUME_TIMEOUT_MS_DEFAULT;
COMDB2DB_TIMEOUT = COMDB2DB_TIMEOUT_DEFAULT;
CDB2_API_CALL_TIMEOUT = CDB2_API_CALL_TIMEOUT_DEFAULT;
CDB2_SOCKET_TIMEOUT = CDB2_SOCKET_TIMEOUT_DEFAULT;
CDB2_PROTOBUF_SIZE = CDB2_PROTOBUF_SIZE_DEFAULT;
cdb2_tcpbufsz = CDB2_TCPBUFSZ_DEFAULT;
cdb2_allow_pmux_route = CDB2_ALLOW_PMUX_ROUTE_DEFAULT;
cdb2cfg_override = CDB2CFG_OVERRIDE_DEFAULT;
CDB2_REQUEST_FP = CDB2_REQUEST_FP_DEFAULT;
CDB2_GET_HOSTNAME_FROM_SOCKPOOL_FD = CDB2_GET_HOSTNAME_FROM_SOCKPOOL_FD_DEFAULT;
cdb2_c_ssl_mode = SSL_ALLOW;
memset(cdb2_sslcertpath, 0, sizeof(cdb2_sslcertpath));
memset(cdb2_sslcert, 0, sizeof(cdb2_sslcert));
memset(cdb2_sslkey, 0, sizeof(cdb2_sslkey));
memset(cdb2_sslca, 0, sizeof(cdb2_sslca));
memset(cdb2_sslcrl, 0, sizeof(cdb2_sslcrl));
cdb2_nid_dbname = CDB2_NID_DBNAME_DEFAULT;
cdb2_cache_ssl_sess = CDB2_CACHE_SSL_SESS_DEFAULT;
cdb2_min_tls_ver = CDB2_MIN_TLS_VER_DEFAULT;
reset_sockpool();
}
static SBUF2 *sbuf2openread(const char *filename)
{
int fd;
SBUF2 *s;
if ((fd = open(filename, O_RDONLY, 0)) < 0 ||
(s = sbuf2open(fd, 0)) == NULL) {
if (fd >= 0)
close(fd);
return NULL;
}
return s;
}
#if defined(__APPLE__)
#include <libproc.h>
static char *apple_getargv0(void)
{
static char argv0[PATH_MAX];
int ret = proc_pidpath(_PID, argv0, sizeof(argv0));
if (ret <= 0) {
fprintf(stderr, "%s proc_pidpath returns %d\n", __func__, ret);
return NULL;
}
return argv0;
}
#endif
#if defined(_SUN_SOURCE) || defined(_LINUX_SOURCE)
static char *proc_cmdline_getargv0(void)
{
char procname[64];
static char argv0[PATH_MAX];
snprintf(procname, sizeof(procname), "/proc/self/cmdline");
SBUF2 *s = sbuf2openread(procname);
if (s == NULL) {
fprintf(stderr, "%s cannot open %s, %s\n", __func__, procname,
strerror(errno));
return NULL;
}
if ((sbuf2gets(argv0, PATH_MAX, s)) < 0) {
fprintf(stderr, "%s error reading from %s, %s\n", __func__, procname,
strerror(errno));
sbuf2close(s);
return NULL;
}
sbuf2close(s);
return argv0;
}
#endif
#define SQLCACHEHINT "/*+ RUNCOMDB2SQL "
#define SQLCACHEHINTLENGTH 17
static int value_on_off(const char *value) {
if (strcasecmp("on", value) == 0) {
return 1;
} else if (strcasecmp("off", value) == 0) {
return 0;
} else if (strcasecmp("no", value) == 0) {
return 0;
} else if (strcasecmp("yes", value) == 0) {
return 1;
}
return atoi(value);
}
static inline const char *cdb2_skipws(const char *str)
{
while (*str && isspace(*str))
str++;
return str;
}
char *cdb2_getargv0(void)
{
#if defined(__APPLE__)
return apple_getargv0();
#elif defined(_LINUX_SOURCE) || defined(_SUN_SOURCE)
return proc_cmdline_getargv0();
#else
fprintf(stderr, "%s unsupported architecture\n", __func__);
return NULL;
#endif
}
static void atfork_prepare(void) {
pthread_mutex_lock(&cdb2_sockpool_mutex);
if (identity_cb)
identity_cb->resetIdentity_start();
}
static void atfork_me(void) {
if (identity_cb)
identity_cb->resetIdentity_end(1);
pthread_mutex_unlock(&cdb2_sockpool_mutex);
}
static void sockpool_close_all(void);
static void atfork_child(void) {
sockpool_close_all();
_PID = getpid();
if (identity_cb)
identity_cb->resetIdentity_end(0);
pthread_mutex_unlock(&cdb2_sockpool_mutex);
}
static int init_once_has_run = 0;
static void do_init_once(void)
{
static pthread_mutex_t lk = PTHREAD_MUTEX_INITIALIZER;
if (init_once_has_run == 0) {
pthread_mutex_lock(&lk);
if (!init_once_has_run) {
srandom(time(0));
_PID = getpid();
_MACHINE_ID = gethostid();
_ARGV0 = cdb2_getargv0();
pthread_atfork(atfork_prepare, atfork_me, atfork_child);
init_once_has_run = 1;
}
pthread_mutex_unlock(&lk);
}
char *do_log = getenv("CDB2_LOG_CALLS");
if (do_log)
log_calls = 1;
char *config = getenv("CDB2_CONFIG_FILE");
if (config) {
/* can't call back cdb2_set_comdb2db_config from do_init_once */
strncpy(CDB2DBCONFIG_NOBBENV, config, 511);
}
char *cdb2db_override = getenv("CDB2_COMDB2DB_OVERRIDE");
if (cdb2db_override) {
strncpy(COMDB2DB_OVERRIDE, cdb2db_override, 31);
}
char *cdb2db_dbnum_override = getenv("CDB2_COMDB2DB_DBNUM_OVERRIDE");
if (cdb2db_dbnum_override) {
COMDB2DB_NUM_OVERRIDE = atoi(cdb2db_dbnum_override);
}
}
/* if sqlstr is a read stmt will return 1 otherwise return 0
* returns -1 if sqlstr is null
*/
static int is_sql_read(const char *sqlstr)
{
const char get[] = "GET";
const char sp_exec[] = "EXEC";
const char with[] = "WITH";
const char sel[] = "SELECT";
const char explain[] = "EXPLAIN";
if (sqlstr == NULL)
return -1;
sqlstr = cdb2_skipws(sqlstr);
int slen = strlen(sqlstr);
if (slen) {
if (slen < sizeof(get) - 1)
return 0;
if (!strncasecmp(sqlstr, get, sizeof(get) - 1))
return 1;
if (slen < sizeof(sp_exec) - 1)
return 0;
if (!strncasecmp(sqlstr, sp_exec, sizeof(sp_exec) - 1))
return 1;
if (!strncasecmp(sqlstr, with, sizeof(with) - 1))
return 1;
if (slen < sizeof(sel) - 1)
return 0;
if (!strncasecmp(sqlstr, sel, sizeof(sel) - 1))
return 1;
if (slen < sizeof(explain) - 1)
return 0;
if (!strncasecmp(sqlstr, explain, sizeof(explain) - 1))
return 1;
}
return 0;
}
/* PASSFD CODE */
#if defined(_LINUX_SOURCE)
#define HAVE_MSGHDR_MSG_CONTROL
#endif
enum {
PASSFD_SUCCESS = 0,
PASSFD_RECVMSG = -1, /* error with recvmsg() */
PASSFD_EOF = -2, /* eof before message completely read */
PASSFD_2FDS = -3, /* received more than one file descriptor */
PASSFD_BADCTRL = -4, /* received bad control message */
PASSFD_TIMEOUT = -5, /* timed out */
PASSFD_POLL = -6, /* error with poll() */
PASSFD_SENDMSG = -7 /* error with sendmsg() */
};
static int recv_fd_int(int sockfd, void *data, size_t nbytes, int *fd_recvd)
{
ssize_t rc;
size_t bytesleft;
char *cdata;
struct msghdr msg;
struct iovec iov[1];
int recvfd;
#ifdef HAVE_MSGHDR_MSG_CONTROL
union {
struct cmsghdr cm;
unsigned char control[CMSG_SPACE(sizeof(int))];
} control_un;
struct cmsghdr *cmsgptr;
#endif
*fd_recvd = -1;
cdata = data;
bytesleft = nbytes;
while (bytesleft > 0) {
#ifdef HAVE_MSGHDR_MSG_CONTROL
msg.msg_control = control_un.control;
msg.msg_controllen = sizeof(control_un.control);
msg.msg_flags = 0;
#else
msg.msg_accrights = (caddr_t)&recvfd;
msg.msg_accrightslen = sizeof(int);
#endif
msg.msg_name = NULL;
msg.msg_namelen = 0;
msg.msg_iov = iov;
msg.msg_iovlen = 1;
iov[0].iov_base = cdata;
iov[0].iov_len = bytesleft;
rc = recvmsg(sockfd, &msg, 0);
if (rc == -1) {
if (errno == EINTR || errno == EAGAIN)
continue;
return PASSFD_RECVMSG;
}
if (rc == 0) {
/* Premature eof */
return PASSFD_EOF;
}
cdata += rc;
bytesleft -= rc;
/* See if we got a descriptor with this message */
#ifdef HAVE_MSGHDR_MSG_CONTROL
cmsgptr = CMSG_FIRSTHDR(&msg);
if (cmsgptr) {
if (cmsgptr->cmsg_len != CMSG_LEN(sizeof(int)) ||
cmsgptr->cmsg_level != SOL_SOCKET ||
cmsgptr->cmsg_type != SCM_RIGHTS) {
return PASSFD_BADCTRL;
}
recvfd = *((int *)CMSG_DATA(cmsgptr));
if (*fd_recvd != -1) {
if (close(recvfd) == -1) {
fprintf(stderr, "%s: error closing second fd %d: %d %s\n",
__func__, recvfd, errno, strerror(errno));
}
return PASSFD_2FDS;
}
*fd_recvd = recvfd;
if (CMSG_NXTHDR(&msg, cmsgptr)) {
return PASSFD_BADCTRL;
}
}
#else
if (msg.msg_accrightslen == sizeof(int)) {
if (*fd_recvd != -1) {
if (close(recvfd) == -1) {
fprintf(stderr, "%s: error closing second fd %d: %d %s\n",
__func__, recvfd, errno, strerror(errno));
}
return PASSFD_2FDS;
}
*fd_recvd = recvfd;
}
#endif
}
return PASSFD_SUCCESS;
}
/* This wrapper ensures that on error we close any file descriptor that we
* may have received before the error occured. Alse we make sure that we
* preserve the value of errno which may be needed if the error was
* PASSFD_RECVMSG. */
static int recv_fd(int sockfd, void *data, size_t nbytes, int *fd_recvd)
{
int rc;
rc = recv_fd_int(sockfd, data, nbytes, fd_recvd);
if (rc != 0 && *fd_recvd != -1) {
int errno_save = errno;
if (close(*fd_recvd) == -1) {
fprintf(stderr, "%s: close(%d) error: %d %s\n", __func__, *fd_recvd,
errno, strerror(errno));
}
*fd_recvd = -1;
errno = errno_save;
}
return rc;
}
static int send_fd_to(int sockfd, const void *data, size_t nbytes,
int fd_to_send, int timeoutms)
{
ssize_t rc;
size_t bytesleft;
struct msghdr msg;
struct iovec iov[1];
const char *cdata;
#ifdef HAVE_MSGHDR_MSG_CONTROL
union {
struct cmsghdr cm;
unsigned char control[CMSG_SPACE(sizeof(int))];
} control_un;
struct cmsghdr *cmsgptr;
#endif
bytesleft = nbytes;
cdata = data;
while (bytesleft > 0) {
if (timeoutms > 0) {
struct pollfd pol;
int pollrc;
pol.fd = sockfd;
pol.events = POLLOUT;
pollrc = poll(&pol, 1, timeoutms);
if (pollrc == 0) {
return PASSFD_TIMEOUT;
} else if (pollrc == -1) {
/* error will be in errno */
return PASSFD_POLL;
}
}
if (fd_to_send != -1) {
#ifdef HAVE_MSGHDR_MSG_CONTROL
msg.msg_control = control_un.control;
msg.msg_controllen = sizeof(control_un.control);
msg.msg_flags = 0;
cmsgptr = CMSG_FIRSTHDR(&msg);
cmsgptr->cmsg_len = CMSG_LEN(sizeof(int));
cmsgptr->cmsg_level = SOL_SOCKET;
cmsgptr->cmsg_type = SCM_RIGHTS;
*((int *)CMSG_DATA(cmsgptr)) = fd_to_send;
#else
msg.msg_accrights = (caddr_t)&fd_to_send;
msg.msg_accrightslen = sizeof(int);
#endif
} else {
#ifdef HAVE_MSGHDR_MSG_CONTROL
msg.msg_control = NULL;
msg.msg_controllen = 0;
msg.msg_flags = 0;
#else
msg.msg_accrights = NULL;
msg.msg_accrightslen = 0;
#endif
}
msg.msg_name = NULL;
msg.msg_namelen = 0;
msg.msg_iov = iov;
msg.msg_iovlen = 1;
iov[0].iov_base = (caddr_t)cdata;
iov[0].iov_len = bytesleft;
rc = sendmsg(sockfd, &msg, 0);
if (rc == -1) {
if (errno == EINTR || errno == EAGAIN)
continue;
return PASSFD_SENDMSG;
}
if (rc == 0) {
return PASSFD_EOF;
}
/* We didn't get an error, so the fd must have been sent. */
fd_to_send = -1;
cdata += rc;
bytesleft -= rc;
}
return PASSFD_SUCCESS;
}
static int send_fd(int sockfd, const void *data, size_t nbytes, int fd_to_send)
{
return send_fd_to(sockfd, data, nbytes, fd_to_send, 0);
}
static int cdb2_tcpresolve(const char *host, struct in_addr *in, int *port)
{
/*RESOLVE AN ADDRESS*/
in_addr_t inaddr;
int len;
struct hostent *hp = NULL;
char tok[128], *cc;
cc = strchr(host, (int)':');
if (cc == 0) {
len = strlen(host);
if (len >= sizeof(tok))
return -2;
memcpy(tok, host, len);
tok[len] = 0;
} else {
*port = atoi(cc + 1);
len = (int)(cc - host);
if (len >= sizeof(tok))
return -2;
memcpy(tok, host, len);
tok[len] = 0;
}
if ((inaddr = inet_addr(tok)) != (in_addr_t)-1) {
/* it's dotted-decimal */
memcpy(&in->s_addr, &inaddr, sizeof(inaddr));
} else {
#ifdef __APPLE__
hp = gethostbyname(tok);
#elif _LINUX_SOURCE
int herr;
char tmp[8192];
int tmplen = 8192;
struct hostent hostbuf;
gethostbyname_r(tok, &hostbuf, tmp, tmplen, &hp, &herr);
#elif _SUN_SOURCE
int herr;
char tmp[8192];
int tmplen = 8192;
struct hostent hostbuf;
hp = gethostbyname_r(tok, &hostbuf, tmp, tmplen, &herr);
#else
hp = gethostbyname(tok);
#endif
if (hp == NULL) {
fprintf(stderr, "%s:gethostbyname(%s): errno=%d err=%s\n", __func__,
tok, errno, strerror(errno));
return -1;
}
memcpy(&in->s_addr, hp->h_addr, hp->h_length);
}
return 0;
}
static int lclconn(int s, const struct sockaddr *name, int namelen,
int timeoutms)
{
/* connect with timeout */
struct pollfd pfd;
int flags, rc;
int err;
socklen_t len;
if (timeoutms <= 0)
return connect(s, name, namelen); /*no timeout specified*/
flags = fcntl(s, F_GETFL, 0);
if (flags < 0)
return -1;
if (fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0) {
return -1;
}
rc = connect(s, name, namelen);
if (rc == -1 && errno == EINPROGRESS) {
/*wait for connect event */
pfd.fd = s;
pfd.events = POLLOUT;
rc = poll(&pfd, 1, timeoutms);
if (rc == 0) {
/*timeout*/
/*fprintf(stderr,"connect timed out\n");*/
return -2;
}
if (rc != 1) { /*poll failed?*/
return -1;
}
if ((pfd.revents & POLLOUT) == 0) { /*wrong event*/
/*fprintf(stderr,"poll event %d\n",pfd.revents);*/
return -1;
}
} else if (rc == -1) {
/*connect failed?*/
return -1;
}
if (fcntl(s, F_SETFL, flags) < 0) {
return -1;
}
len = sizeof(err);
if (getsockopt(s, SOL_SOCKET, SO_ERROR, &err, &len)) {
return -1;
}
errno = err;
if (errno != 0)
return -1;
return 0;
}
static int cdb2_do_tcpconnect(struct in_addr in, int port, int myport,
int timeoutms)
{
int sockfd, rc;
int sendbuff;
struct sockaddr_in tcp_srv_addr; /* server's Internet socket addr */
struct sockaddr_in my_addr; /* my Internet address */
bzero((char *)&tcp_srv_addr, sizeof tcp_srv_addr);
tcp_srv_addr.sin_family = AF_INET;
if (port <= 0) {
return -1;
}
tcp_srv_addr.sin_port = htons(port);
memcpy(&tcp_srv_addr.sin_addr, &in.s_addr, sizeof(in.s_addr));
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
fprintf(stderr, "tcpconnect_to: can't create TCP socket\n");
return -1;
}
#if 0
/* Allow connect port to be re-used */
sendbuff = 1; /* enable option */
if (setsockopt( sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&sendbuff,
sizeof sendbuff ) < 0)
{
fprintf(stderr,"tcpconnect_to: setsockopt failure\n" );
close( sockfd );
return -1;
}
#endif
sendbuff = 1; /* enable option */
if (setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char *)&sendbuff,
sizeof sendbuff) < 0) {
fprintf(stderr, "tcpconnect_to: setsockopt failure\n");
close(sockfd);
return -1;
}
struct linger ling;
ling.l_onoff = 1;
ling.l_linger = 0;
if (setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (char *)&ling, sizeof(ling)) <
0) {
fprintf(stderr, "tcpconnect_to: setsockopt failure:%s",
strerror(errno));
close(sockfd);
return -1;
}
if (cdb2_tcpbufsz) {
int tcpbufsz = cdb2_tcpbufsz;
if (setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, (char *)&tcpbufsz,
sizeof(tcpbufsz)) < 0) {
fprintf(stderr, "tcpconnect_to: setsockopt failure:%s",
strerror(errno));
close(sockfd);
return -1;
}
}
if (myport > 0) { /* want to use specific port on local host */
bzero((char *)&my_addr, sizeof my_addr);
my_addr.sin_family = AF_INET;
my_addr.sin_addr.s_addr = INADDR_ANY;
my_addr.sin_port = htons((u_short)myport);
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) {
fprintf(stderr, "tcpconnect_to: bind failed on local port %d: %s",
myport, strerror(errno));
close(sockfd);
return -1;
}
}
/* Connect to the server. */
rc = lclconn(sockfd, (struct sockaddr *)&tcp_srv_addr, sizeof(tcp_srv_addr),
timeoutms);
if (rc < 0) {
close(sockfd);
return rc;
}
return (sockfd); /* all OK */
}
struct context_messages {
char *message[MAX_CONTEXTS];
int count;
int has_changed;
};
/* Forward declarations. */
static void cdb2_init_context_msgs(cdb2_hndl_tp *hndl);
static int cdb2_free_context_msgs(cdb2_hndl_tp *hndl);
/* Make it equal to FSQL header. */
struct newsqlheader {
int type;
int compression;
int state; /* query state */
int length;
};
typedef struct cdb2_query_list_item {
void *buf;
int len;
int is_read;
char *sql;
struct cdb2_query_list_item *next;
} cdb2_query_list;
struct cdb2_ssl_sess {
struct cdb2_ssl_sess *next;
char dbname[64];
char cluster[64];
int ref;
SSL_SESSION *sessobj;
};
static cdb2_ssl_sess cdb2_ssl_sess_cache;
/* A cnonce is composed of
- 32 bits of machine ID
- 32 bits of process PID
- 32 or 64 bits of handle address
- 52 bits for the epoch time in microseconds
- 12 bits for the sequence number
The allocation allows a handle to run at a maximum transaction rate of
4096 txn/us (~4 billion transactions per second) till September 17, 2112.
See next_cnonce() for details. */
#define CNONCE_STR_FMT "%lx-%x-%llx-"
#define CNONCE_STR_SZ 60 /* 16 + 1 + 8 + 1 + 16 + 1 + 16 + 1 (NUL) */
#define CNT_BITS 12
#define TIME_MASK (-1ULL << CNT_BITS)
#define CNT_MASK (-1ULL ^ TIME_MASK)
typedef struct cnonce {
long hostid;
int pid;
struct cdb2_hndl *hndl;
uint64_t seq;
int ofs;
char str[CNONCE_STR_SZ];
} cnonce_t;
#define DBNAME_LEN 64
#define TYPE_LEN 64
#define POLICY_LEN 24
struct cdb2_hndl {