forked from bloomberg/comdb2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.c
1536 lines (1396 loc) · 54.2 KB
/
config.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 2017, 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 <bb_getopt_long.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include "sqliteInt.h"
#include "comdb2.h"
#include "intern_strings.h"
#include "bb_oscompat.h"
#include "switches.h"
#include "util.h"
#include "sqllog.h"
#include "ssl_bend.h"
#include "translistener.h"
#include "rtcpu.h"
#include "config.h"
#include "phys_rep.h"
extern int gbl_create_mode;
extern int gbl_fullrecovery;
extern int gbl_exit;
extern int gbl_recovery_timestamp;
extern int gbl_recovery_lsn_file;
extern int gbl_recovery_lsn_offset;
extern int gbl_upgrade_blocksql_2_socksql;
extern int gbl_rep_node_pri;
extern int gbl_bad_lrl_fatal;
extern int gbl_disable_new_snapshot;
extern char *gbl_recovery_options;
extern const char *gbl_repoplrl_fname;
extern char gbl_dbname[MAX_DBNAME_LENGTH];
extern char **qdbs;
extern char **sfuncs;
extern char **afuncs;
static int gbl_nogbllrl; /* don't load /bb/bin/comdb2*.lrl */
static int pre_read_option(char *, int);
static int read_lrl_option(struct dbenv *, char *, struct read_lrl_option_type *, int);
static struct option long_options[] = {
{"lrl", required_argument, NULL, 0},
{"repopnewlrl", required_argument, NULL, 0},
{"recovertotime", required_argument, NULL, 0},
{"recovertolsn", required_argument, NULL, 0},
{"recovery_lsn", required_argument, NULL, 0},
{"pidfile", required_argument, NULL, 0},
{"help", no_argument, NULL, 'h'},
{"create", no_argument, &gbl_create_mode, 1},
{"fullrecovery", no_argument, &gbl_fullrecovery, 1},
{"no-global-lrl", no_argument, &gbl_nogbllrl, 1},
{"dir", required_argument, NULL, 0},
{"tunable", required_argument, NULL, 0},
{NULL, 0, NULL, 0}};
static const char *help_text = {
"Usage: comdb2 [--lrl LRLFILE] [--recovertotime EPOCH]\n"
" [--recovertolsn FILE:OFFSET]\n"
" [--tunable STRING]\n"
" [--fullrecovery] NAME\n"
"\n"
" comdb2 --create [--lrl LRLFILE] [--dir PATH] NAME\n"
"\n"
" --lrl specify alternate lrl file\n"
" --fullrecovery runs full recovery after a hot copy\n"
" --recovertolsn recovers database to file:offset\n"
" --recovertotime recovers database to epochtime\n"
" --create creates a new database\n"
" --dir specify path to database directory\n"
" --tunable override tunable\n"
"\n"
" NAME database name\n"
" LRLFILE lrl configuration file\n"
" FILE ID of a database file\n"
" OFFSET offset within FILE\n"
" EPOCH time in seconds since 1970\n"
" PATH path to database directory\n"};
struct read_lrl_option_type {
int lineno;
const char *lrlname;
const char *dbname;
};
void print_usage_and_exit()
{
logmsg(LOGMSG_WARN, "%s\n", help_text);
exit(1);
}
static int write_pidfile(const char *pidfile)
{
FILE *f;
f = fopen(pidfile, "w");
if (f == NULL) {
logmsg(LOGMSG_ERROR, "%s %s\n", pidfile, strerror(errno));
return -1;
}
fprintf(f, "%d\n", (int)getpid());
fclose(f);
return 0;
}
static void set_dbdir(char *dir)
{
if (dir == NULL)
return;
if (*dir == '/') {
gbl_dbdir = strdup(dir);
return;
}
char *wd = getcwd(NULL, 0);
int n = snprintf(NULL, 0, "%s/%s", wd, dir);
gbl_dbdir = malloc(++n);
snprintf(gbl_dbdir, n, "%s/%s", wd, dir);
free(wd);
}
#include <berkdb/dbinc/queue.h>
struct CmdLineTunable;
struct CmdLineTunable {
char *arg;
STAILQ_ENTRY(CmdLineTunable) entry;
};
STAILQ_HEAD(CmdLineTunables, CmdLineTunable) *cmd_line_tunables;
static void add_cmd_line_tunable(char *arg)
{
if (cmd_line_tunables == NULL) {
cmd_line_tunables = malloc(sizeof(*cmd_line_tunables));
STAILQ_INIT(cmd_line_tunables);
}
struct CmdLineTunable *t = malloc(sizeof(*t));
t->arg = arg;
STAILQ_INSERT_TAIL(cmd_line_tunables, t, entry);
}
void add_cmd_line_tunables_to_file(FILE *f)
{
if (cmd_line_tunables == NULL)
return;
struct CmdLineTunable *t, *tmp;
STAILQ_FOREACH_SAFE(t, cmd_line_tunables, entry, tmp) {
fprintf(f, "%s\n", t->arg);
free(t);
}
free(cmd_line_tunables);
cmd_line_tunables = NULL;
}
static void read_cmd_line_tunables(struct dbenv *dbenv)
{
if (cmd_line_tunables == NULL)
return;
struct read_lrl_option_type options = {
.lineno = 0, .lrlname = "cmd_line_args", .dbname = dbenv->envname};
struct CmdLineTunable *t, *tmp;
STAILQ_FOREACH_SAFE(t, cmd_line_tunables, entry, tmp) {
read_lrl_option(dbenv, t->arg, &options, strlen(t->arg));
free(t);
}
free(cmd_line_tunables);
cmd_line_tunables = NULL;
}
int handle_cmdline_options(int argc, char **argv, char **lrlname)
{
char *p;
int c;
int options_idx;
while ((c = bb_getopt_long(argc, argv, "h", long_options, &options_idx)) !=
-1) {
if (c == 'h') print_usage_and_exit();
if (c == '?') return 1;
switch (options_idx) {
case 0: /* lrl */ *lrlname = optarg; break;
case 1: /* repopnewlrl */
logmsg(LOGMSG_INFO, "repopulate external .lrl mode.\n");
gbl_repoplrl_fname = optarg;
gbl_exit = 1;
break;
case 2: /* recovertotime */
logmsg(LOGMSG_FATAL, "force full recovery to timestamp %u\n",
gbl_recovery_timestamp);
gbl_recovery_timestamp = strtoul(optarg, NULL, 10);
gbl_fullrecovery = 1;
break;
case 3: /* recovertolsn */
if ((p = strchr(optarg, ':')) == NULL) {
logmsg(LOGMSG_FATAL, "recovertolsn: invalid lsn format.\n");
exit(1);
}
p++;
gbl_recovery_lsn_file = atoi(optarg);
gbl_recovery_lsn_offset = atoi(p);
logmsg(LOGMSG_FATAL, "force full recovery to lsn %d:%d\n",
gbl_recovery_lsn_file, gbl_recovery_lsn_offset);
gbl_fullrecovery = 1;
break;
case 4: /* recovery_lsn */ gbl_recovery_options = optarg; break;
case 5: /* pidfile */ write_pidfile(optarg); break;
case 10: /* dir */ set_dbdir(optarg); break;
case 11: /* tunable */ add_cmd_line_tunable(optarg); break;
}
}
return 0;
}
struct deferred_option {
char *option;
int line;
int len;
LINKC_T(struct deferred_option) lnk;
};
LISTC_T(struct deferred_option) deferred_options;
static int defer_option(char *option, int len, int line)
{
struct deferred_option *opt;
if (len == -1) len = strlen(option);
opt = malloc(sizeof(struct deferred_option));
if (opt == NULL) {
logmsg(LOGMSG_ERROR, "%s:%d out of memory\n", __FILE__, __LINE__);
return 1;
}
opt->option = calloc(1, len + 1);
if (opt->option == NULL) {
logmsg(LOGMSG_ERROR, "%s:%d out of memory\n", __FILE__, __LINE__);
free(opt);
return 1;
}
memcpy(opt->option, option, len);
opt->line = line;
opt->len = strlen(opt->option);
listc_abl(&deferred_options, opt);
return 0;
}
int deferred_do_commands(struct dbenv *env, char *option,
struct read_lrl_option_type *p, int len)
{
char *tok;
int st = 0, tlen = 0;
int rc = 0;
tok = segtok(option, len, &st, &tlen);
if (tokcmp(tok, tlen, "sqllogger") == 0)
sqllogger_process_message(option + st, len - st);
else if (tokcmp(tok, tlen, "do") == 0)
rc = process_command(env, option + st, len - st, 0);
return rc;
}
void process_deferred_options(struct dbenv *dbenv, lrl_reader *callback)
{
struct deferred_option *opt;
LISTC_FOR_EACH(&deferred_options, opt, lnk) {
callback(dbenv, opt->option, NULL, opt->len);
}
}
void clear_deferred_options(void)
{
struct deferred_option *opt;
opt = listc_rtl(&deferred_options);
while (opt) {
free(opt->option);
free(opt);
opt = listc_rtl(&deferred_options);
}
}
static char *legacy_options[] = {"disallow write from beta if prod",
"noblobstripe",
"nullsort high",
"dont_sort_nulls_with_header",
"nochecksums",
"off fix_cstr",
"no_null_blob_fix",
"no_static_tag_blob_fix",
"dont_forbid_ulonglong",
"dont_init_with_ondisk_header",
"dont_init_with_instant_schema_change",
"dont_init_with_inplace_updates",
"dont_prefix_foreign_keys",
"dont_superset_foreign_keys",
"disable_inplace_blobs",
"disable_inplace_blob_optimization",
"disable_osql_blob_optimization",
"nocrc32c",
"enable_tagged_api",
"nokeycompr",
"norcache",
"usenames",
"setattr DIRECTIO 0",
"berkattr elect_highest_committed_gen 0",
"unnatural_types 1",
"enable_sql_stmt_caching none",
"on accept_on_child_nets",
"env_messages",
"off return_long_column_names",
"ddl_cascade_drop 0",
"setattr NET_SEND_GBLCONTEXT 1",
"setattr ENABLE_SEQNUM_GENERATIONS 0",
"setattr MASTER_LEASE 0",
"setattr SC_DONE_SAME_TRAN 0",
"logmsg notimestamp",
"queuedb_genid_filename off",
"decoupled_logputs off",
"init_with_time_based_genids",
"logmsg level info",
"logput window 1",
"osql_send_startgen off",
"create_default_user",
"allow_negative_column_size",
"osql_check_replicant_numops off",
"reorder_socksql_no_deadlock off",
"disable_tpsc_tblvers",
"on disable_etc_services_lookup",
"off osql_odh_blob",
"legacy_schema on",
"online_recovery off"};
int gbl_legacy_defaults = 0;
int pre_read_legacy_defaults(void *_, void *__)
{
if (gbl_legacy_defaults != 0) return 0;
gbl_legacy_defaults = 1;
for (int i = 0; i < sizeof(legacy_options) / sizeof(legacy_options[0]); i++) {
pre_read_option(legacy_options[i], strlen(legacy_options[i]));
}
return 0;
}
static void read_legacy_defaults(struct dbenv *dbenv,
struct read_lrl_option_type *options)
{
if (gbl_legacy_defaults != 1) return;
gbl_legacy_defaults = 2;
for (int i = 0; i < sizeof(legacy_options) / sizeof(legacy_options[0]); i++) {
read_lrl_option(dbenv, legacy_options[i], options, strlen(legacy_options[i]));
}
}
/* handles "if"'s, returns 1 if this isn't an "if" statement or if the statement
* is true, 0 if it is false (ie if this line should be skipped)
* this replaces a couple duplicate sections of code */
static int lrl_if(char **tok_inout, char *line, int line_len, int *st,
int *ltok)
{
char *tok = *tok_inout;
if (tokcmp(tok, *ltok, "if") == 0) {
enum mach_class my_class = get_my_mach_class();
tok = segtok(line, line_len, st, ltok);
if (my_class == CLASS_TEST && tokcmp(tok, *ltok, "test") &&
tokcmp(tok, *ltok, "dev"))
return 0;
if (my_class == CLASS_ALPHA && tokcmp(tok, *ltok, "alpha")) return 0;
if (my_class == CLASS_UAT && tokcmp(tok, *ltok, "uat")) return 0;
if (my_class == CLASS_BETA && tokcmp(tok, *ltok, "beta")) return 0;
if (my_class == CLASS_PROD && tokcmp(tok, *ltok, "prod")) return 0;
if (my_class == CLASS_UNKNOWN) return 0;
tok = segtok(line, line_len, st, ltok);
*tok_inout = tok;
}
return 1; /* there was no "if" statement or it was true */
}
void getmyaddr()
{
if (comdb2_gethostbyname(&gbl_mynode, &gbl_myaddr) != 0) {
gbl_myaddr.s_addr = INADDR_LOOPBACK; /* default to localhost */
return;
}
}
static int pre_read_option(char *line, int llen)
{
char *tok;
int st = 0;
int ltok;
comdb2_tunable_err rc;
tok = segtok(line, llen, &st, <ok);
if (ltok == 0 || tok[0] == '#') return 0;
/* if this is an "if" statement that evaluates to false, skip */
if (!lrl_if(&tok, line, llen, &st, <ok)) {
return 0;
}
/* Handle global tunables which are supposed to be read early. */
rc = handle_lrl_tunable(tok, ltok, line + st, llen - st, READEARLY);
/* Follow through, if the tunable is not found. */
if (rc != TUNABLE_ERR_INVALID_TUNABLE) {
return rc;
}
return 0;
}
static void pre_read_lrl_file(struct dbenv *dbenv, const char *lrlname)
{
FILE *ff;
char line[512];
ff = fopen(lrlname, "r");
if (ff == 0) {
return;
}
while (fgets(line, sizeof(line), ff)) {
pre_read_option(line, strlen(line));
}
fclose(ff); /* lets get one fd back */
}
struct dbenv *read_lrl_file_int(struct dbenv *dbenv, const char *lrlname,
int required)
{
FILE *ff;
char line[512] = {0}; // valgrind doesn't like sse42 instructions
struct lrlfile *lrlfile;
struct read_lrl_option_type options = {
.lineno = 0, .lrlname = lrlname, .dbname = dbenv->envname,
};
dbenv->nsiblings = 1;
lrlfile = malloc(sizeof(struct lrlfile));
lrlfile->file = strdup(lrlname);
listc_atl(&dbenv->lrl_files, lrlfile);
ff = fopen(lrlname, "r");
if (ff == 0) {
if (required) {
logmsg(LOGMSG_FATAL, "%s : %s\n", lrlname, strerror(errno));
return 0;
} else if (errno != ENOENT) {
logmsgperror(lrlname);
}
return dbenv;
}
logmsg(LOGMSG_INFO, "processing %s...\n", lrlname);
while (fgets(line, sizeof(line), ff)) {
char *s = strchr(line, '\n');
if (s) *s = 0;
options.lineno++;
read_lrl_option(dbenv, line, &options, strlen(line));
}
/* process legacy options (we deferred them) */
if (gbl_disable_new_snapshot) {
gbl_new_snapisol = 0;
gbl_new_snapisol_asof = 0;
gbl_new_snapisol_logging = 0;
}
if (gbl_rowlocks) {
/* We can't ever choose a writer as a deadlock victim in rowlocks mode
(at least without some kludgery, or snapshots) */
bdb_attr_set(dbenv->bdb_attr, BDB_ATTR_PAGE_ORDER_TABLESCAN, 0);
}
fclose(ff);
return dbenv;
}
static struct dbenv *read_lrl_file(struct dbenv *dbenv, const char *lrlname,
int required)
{
struct lrlfile *lrlfile;
struct dbenv *out;
out = read_lrl_file_int(dbenv, (char *)lrlname, required);
lrlfile = listc_rtl(&dbenv->lrl_files);
free(lrlfile->file);
free(lrlfile);
return out;
}
static char *get_qdb_name(const char *file)
{
FILE *f = fopen(file, "r");
if (f == NULL) {
logmsg(LOGMSG_ERROR, "%s:fopen(\"%s\"):%s\n", __func__, file,
strerror(errno));
return NULL;
}
size_t n;
ssize_t s;
// Name of queue
char *name = NULL;
s = getline(&name, &n, f);
fclose(f);
if (s == -1) return NULL;
name[s - 1] = 0;
return name;
}
/* defines lrl directives that get ignored by comdb2 */
static int lrltokignore(char *tok, int ltok)
{
/* used by comdb2backup script */
if (tokcmp(tok, ltok, "backup") == 0) return 0;
/* reserved for use by cmdb2filechk script */
if (tokcmp(tok, ltok, "filechkopts") == 0) return 0;
/*not a reserved token */
return 1;
}
#define parse_lua_funcs(pfx) \
do { \
tok = segtok(line, sizeof(line), &st, <ok); \
int num = toknum(tok, ltok); \
pfx##funcs = malloc(sizeof(char *) * (num + 1)); \
int i; \
for (i = 0; i < num; ++i) { \
tok = segtok(line, sizeof(line), &st, <ok); \
pfx##funcs[i] = tokdup(tok, ltok); \
} \
pfx##funcs[i] = NULL; \
} while (0)
static int read_lrl_option(struct dbenv *dbenv, char *line,
struct read_lrl_option_type *options, int len)
{
char *tok;
int st = 0;
int ltok;
int ii, kk;
int rc;
tok = segtok(line, len, &st, <ok);
if (ltok == 0 || tok[0] == '#') return 0;
if (tokcmp(tok, ltok, "on") == 0) {
change_switch(1, line, len, st);
} else if (tokcmp(tok, ltok, "off") == 0) {
change_switch(0, line, len, st);
} else if (tokcmp(tok, ltok, "setattr") == 0) {
char name[48] = {0}; // oh valgrind
int value;
tok = segtok(line, len, &st, <ok);
if (ltok == 0) {
logmsg(LOGMSG_ERROR, "%s:%d: expected attribute name\n",
options->lrlname, options->lineno);
return -1;
}
tokcpy0(tok, ltok, name, sizeof(name));
tok = segtok(line, len, &st, <ok);
if (ltok == 0) {
logmsg(LOGMSG_ERROR, "%s:%d: expected attribute value\n",
options->lrlname, options->lineno);
return -1;
}
value = toknum(tok, ltok);
if (bdb_attr_set_by_name(NULL, dbenv->bdb_attr, name, value) != 0) {
logmsg(LOGMSG_ERROR, "%s:%d: bad attribute name %s\n",
options->lrlname, options->lineno, name);
}
} else if (!lrl_if(&tok, line, len, &st, <ok)) {
/* If this is an "if" statement that evaluates to false, skip */
return 1;
} else if (tokcmp(tok, ltok, "sqlsortermaxmmapsize") == 0) {
tok = segtok(line, len, &st, <ok);
long long maxmmapsz = toknumll(tok, ltok);
logmsg(LOGMSG_INFO, "setting sqlsortermaxmmapsize to %lld bytes\n",
maxmmapsz);
sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, SQLITE_DEFAULT_MMAP_SIZE,
maxmmapsz);
} else if (tokcmp(tok, ltok, "cache") == 0) { /* cache <nn> <kb|mb|gb> */
tok = segtok(line, len, &st, <ok);
int nn = toknum(tok, ltok);
tok = segtok(line, len, &st, <ok);
if (tokcmp(tok, ltok, "kb") == 0)
dbenv->cacheszkb = nn;
else if (tokcmp(tok, ltok, "mb") == 0)
dbenv->cacheszkb = nn * 1024;
else if (tokcmp(tok, ltok, "gb") == 0)
dbenv->cacheszkb = nn * 1024 * 1024;
else
logmsg(LOGMSG_ERROR, "bad unit for cache sz - needs kb|mb|gb\n");
logmsg(LOGMSG_INFO, "cache size is %dKB\n", dbenv->cacheszkb);
} else if (tokcmp(tok, ltok, "dedicated_network_suffixes") == 0) {
while (1) {
char suffix[50];
tok = segtok(line, len, &st, <ok);
if (ltok == 0) break;
tokcpy(tok, ltok, suffix);
if (net_add_to_subnets(suffix, options->lrlname)) {
return -1;
}
}
} else if (tokcmp(tok, ltok, "nullsort") == 0) {
tok = segtok(line, len, &st, <ok);
if (ltok == 0) {
logmsg(LOGMSG_ERROR, "Expected argument for nullsort\n");
return -1;
} else if (tokcmp(tok, ltok, "high") == 0) {
/* default */
comdb2_types_set_null_bit(null_bit_high);
logmsg(LOGMSG_INFO, "nulls will sort high\n");
} else if (tokcmp(tok, ltok, "low") == 0) {
comdb2_types_set_null_bit(null_bit_low);
logmsg(LOGMSG_INFO, "nulls will sort low\n");
} else {
logmsg(LOGMSG_ERROR, "Invalid argument for nullsort\n");
}
} else if (tokcmp(tok, ltok, "port") == 0) {
char hostname[255];
int port;
tok = segtok(line, len, &st, <ok);
if (ltok == 0) {
logmsg(LOGMSG_ERROR,
"Expected hostname port for \"port\" directive\n");
return -1;
}
if (ltok >= sizeof(hostname)) {
logmsg(LOGMSG_ERROR,
"Unexpectedly long hostname %.*s len %d max %zu\n", ltok,
hostname, ltok, sizeof(hostname));
return -1;
}
tokcpy(tok, ltok, hostname);
tok = segtok(line, len, &st, <ok);
if (ltok == 0) {
logmsg(LOGMSG_ERROR,
"Expected hostname port for \"port\" directive\n");
return -1;
}
port = toknum(tok, ltok);
if (port <= 0 || port >= 65536) {
logmsg(LOGMSG_ERROR, "Port out of range for \"port\" directive\n");
return -1;
}
if (dbenv->nsiblings > 1) {
for (ii = 0; ii < dbenv->nsiblings; ii++) {
if (strcmp(dbenv->sibling_hostname[ii], hostname) == 0) {
dbenv->sibling_port[ii][NET_REPLICATION] = port;
dbenv->sibling_port[ii][NET_SQL] = port;
break;
}
}
if (ii >= dbenv->nsiblings) {
logmsg(LOGMSG_ERROR,
"Don't recognize %s as part of my cluster. Please make "
"sure \"port\" directives follow \"cluster nodes\"\n",
hostname);
return -1;
}
} else if (strcmp(hostname, "localhost") == 0) {
/* nsiblings == 1 means there's no other nodes in the cluster */
dbenv->sibling_port[0][NET_REPLICATION] = port;
dbenv->sibling_port[0][NET_SQL] = port;
}
} else if (tokcmp(tok, ltok, "cluster") == 0) {
/*parse line...*/
tok = segtok(line, len, &st, <ok);
if (tokcmp(tok, ltok, "nodes") == 0) {
/*create replication group. only me by default*/
while (1) {
char nodename[512];
tok = segtok(line, len, &st, <ok);
if (ltok == 0) break;
if (ltok > sizeof(nodename)) {
logmsg(LOGMSG_ERROR,
"host %.*s name too long (expected < %lu)\n", ltok,
tok, sizeof(nodename));
return -1;
}
tokcpy(tok, ltok, nodename);
errno = 0;
if (dbenv->nsiblings >= MAXSIBLINGS) {
logmsg(LOGMSG_ERROR,
"too many sibling nodes (max=%d) in lrl %s\n",
MAXSIBLINGS, options->lrlname);
return -1;
}
/* Check to see if this name is another name for me. */
struct in_addr addr;
char *name = nodename;
if (comdb2_gethostbyname(&name, &addr) == 0 &&
addr.s_addr == gbl_myaddr.s_addr) {
/* Assume I am better known by this name. */
gbl_mynode = intern(name);
gbl_mynodeid = machine_num(gbl_mynode);
}
if (strcmp(gbl_mynode, name) == 0 &&
gbl_rep_node_pri == 0) {
/* assign the priority of current node according to its
* sequence in nodes list. */
gbl_rep_node_pri = MAXSIBLINGS - dbenv->nsiblings;
continue;
}
/* lets ignore duplicate for now and make a list out of what is
* given in lrl */
for (kk = 1; kk < dbenv->nsiblings &&
strcmp(dbenv->sibling_hostname[kk], name);
kk++)
; /*look for dupes*/
if (kk == dbenv->nsiblings) {
/*not a dupe.*/
dbenv->sibling_hostname[dbenv->nsiblings] =
intern(name);
for (int netnum = 0; netnum < MAXNETS; netnum++)
dbenv->sibling_port[dbenv->nsiblings][netnum] = 0;
dbenv->nsiblings++;
}
}
dbenv->sibling_hostname[0] = gbl_mynode;
}
} else if (tokcmp(tok, ltok, "pagesize") == 0) {
tok = segtok(line, len, &st, <ok);
if (ltok == 0) {
logmsg(LOGMSG_ERROR, "Need to specify options for pagesize.\n");
return -1;
}
if (tokcmp(tok, ltok, "all") == 0) {
logmsg(LOGMSG_ERROR, "Need to specify size for all\n");
return -1;
#if 0
NOT SURE WHY THE SHORTEN PATH, but commenting out to
remove compiler warning
ii=toknum(tok,ltok);
bdb_attr_set(dbenv->bdb_attr, BDB_ATTR_PAGESIZEDTA, ii);
bdb_attr_set(dbenv->bdb_attr, BDB_ATTR_PAGESIZEIX, ii);
#endif
} else if (tokcmp(tok, ltok, "dta") == 0) {
tok = segtok(line, len, &st, <ok);
if (ltok == 0) {
logmsg(LOGMSG_ERROR, "Need to specify size for dta\n");
return -1;
}
ii = toknum(tok, ltok);
bdb_attr_set(dbenv->bdb_attr, BDB_ATTR_PAGESIZEDTA, ii);
} else if (tokcmp(tok, ltok, "ix") == 0) {
tok = segtok(line, len, &st, <ok);
if (ltok == 0) {
logmsg(LOGMSG_ERROR, "Need to specify size for ix\n");
return -1;
}
ii = toknum(tok, ltok);
bdb_attr_set(dbenv->bdb_attr, BDB_ATTR_PAGESIZEIX, ii);
} else {
logmsg(LOGMSG_ERROR, "Need to specify options for pagesize.\n");
return -1;
}
}
/* we cant actually BECOME a different version of comdb2 at runtime,
just because the lrl file told us to. what we can do is check for
some confused situations where the lrl file is for a different
version of comdb2 than we are */
else if (tokcmp(tok, ltok, "version") == 0) {
tok = segtok(line, len, &st, <ok);
for (ii = 0; ii < 10; ii++)
if (!isprint(tok[ii])) tok[ii] = '\0';
logmsg(LOGMSG_ERROR, "lrl file for comdb2 version %s found\n", tok);
if (strcmp(tok, COMDB2_VERSION)) {
logmsg(LOGMSG_ERROR, "but we are version %s\n", COMDB2_VERSION);
exit(1);
}
}
else if (tokcmp(tok, ltok, "checkctags") == 0) {
tok = segtok(line, len, &st, <ok);
if (ltok == 0) {
logmsg(LOGMSG_ERROR, "Need to specify options for checkctags.\n");
return -1;
}
if (tokcmp(tok, ltok, "off") == 0) {
logmsg(LOGMSG_INFO, "check tag logic is now off\n");
gbl_check_client_tags = 0;
} else if (tokcmp(tok, ltok, "soft") == 0) {
logmsg(LOGMSG_INFO, "check tag logic will now produce warning\n");
gbl_check_client_tags = 2;
} else if (tokcmp(tok, ltok, "full") == 0) {
logmsg(LOGMSG_INFO,
"check tag logic will now error out to client\n");
gbl_check_client_tags = 1;
} else {
logmsg(LOGMSG_INFO, "Need to specify options for checktags.\n");
return -1;
}
} else if (tokcmp(tok, ltok, "sync") == 0) {
rc = process_sync_command(dbenv, line, len, st);
if (rc != 0) {
return -1;
}
} else if (tokcmp(tok, ltok, "sfuncs") == 0) {
parse_lua_funcs(s);
} else if (tokcmp(tok, ltok, "afuncs") == 0) {
parse_lua_funcs(a);
} else if (tokcmp(tok, ltok, "queuedb") == 0) {
char **slot = &qdbs[0];
while (*slot)
++slot;
tok = segtok(line, len, &st, <ok);
*slot = tokdup(tok, ltok);
struct dbtable **qdb = &dbenv->qdbs[0];
while (*qdb)
++qdb;
char *name = get_qdb_name(*slot);
if (name == NULL) {
logmsg(LOGMSG_ERROR, "Failed to obtain queuedb name from:%s\n",
*slot);
return -1;
}
*qdb = newqdb(dbenv, name, 65536, 65536, 1);
if (*qdb == NULL) {
logmsg(LOGMSG_ERROR, "newqdb failed for:%s\n", name);
return -1;
}
free(name);
} else if (tokcmp(tok, ltok, "table") == 0) {
/*
* variants:
* table foo foo.lrl # load a table given secondary lrl files
* table foo foo.csc # load a table from a csc file
* table foo foo.csc dbnum # load a table from a csc file and have
* # it also accept requests as db# dbnum
*
* relative paths are looked for relative to the parent lrl
*
* table sqlite_stat1 bin/comdb2_stats.csc2
*/
char *fname;
char *tblname;
char tmpname[MAXTABLELEN];
dbenv->dbs = realloc(dbenv->dbs,
(dbenv->num_dbs + 1) * sizeof(struct dbtable *));
tok = segtok(line, len, &st, <ok); /* tbl name */
if (ltok == 0) {
logmsg(LOGMSG_ERROR, "Malformed \"table\" directive\n");
return -1;
}
tblname = tokdup(tok, ltok);
tok = segtok(line, len, &st, <ok);
fname = getdbrelpath(tokdup(tok, ltok));
/* if it's a schema file, allocate a struct dbtable, populate with crap
* data, then load schema. if it's an lrl file, we don't support it
* anymore */
if (strstr(fname, ".lrl") != 0) {
logmsg(LOGMSG_ERROR, "this version of comdb2 does not support "
"loading another lrl from this lrl\n");
return -1;
} else if (strstr(fname, ".csc2") != 0) {
int dbnum;
struct dbtable *db;
bdb_attr_set(dbenv->bdb_attr, BDB_ATTR_GENIDS, 1);
/* optional dbnum */
tok = segtok(line, len, &st, <ok);
tokcpy(tok, ltok, tmpname);
if (ltok == 0)
dbnum = 0;
else {
dbnum = toknum(tok, ltok);
if (dbnum <= 0) {
logmsg(LOGMSG_ERROR,
"Invalid dbnum entry in \"table\" directive\n");
return -1;
}
/* if db number matches parent database db number then
* table name must match parent database name. otherwise
* we get mysterious failures to receive qtraps (setting
* taskname to something not our task name causes initque
* to fail, and the ldgblzr papers over this) */
if (dbnum == dbenv->dbnum &&
strcasecmp(gbl_dbname, tblname) != 0) {
logmsg(LOGMSG_ERROR,
"Table %s has same db number as parent database "
"but different name\n",
tblname);
return -1;
}
}
rc = dyns_load_schema(fname, (char *)gbl_dbname, tblname);
if (rc != 0) {
logmsg(LOGMSG_ERROR, "Error loading %s schema.\n", tok);
return -1;
}
/* create one */
db = newdb_from_schema(dbenv, tblname, fname, dbnum, dbenv->num_dbs,
0);
if (db == NULL) {
return -1;
}
db->dbs_idx = dbenv->num_dbs;
dbenv->dbs[dbenv->num_dbs++] = db;
/* Add table to the hash. */
hash_add(dbenv->db_hash, db);
/* just got a bunch of data. remember it so key forming
routines and SQL can get at it */
if (add_cmacc_stmt(db, 0)) {
logmsg(LOGMSG_ERROR,
"Failed to load schema: can't process schema file %s\n",
tok);
return -1;
}
} else {
logmsg(LOGMSG_ERROR, "Invalid table option\n");
return -1;
}
} else if (tokcmp(tok, ltok, "allow") == 0 ||
tokcmp(tok, ltok, "disallow") == 0 ||
tokcmp(tok, ltok, "clrpol") == 0 ||
tokcmp(tok, ltok, "setclass") == 0) {
if (dbenv->num_allow_lines >= dbenv->max_allow_lines) {
dbenv->max_allow_lines += 1;
dbenv->allow_lines = realloc(
dbenv->allow_lines, sizeof(char *) * dbenv->max_allow_lines);
if (!dbenv->allow_lines) {
logmsg(LOGMSG_ERROR, "out of memory\n");
return -1;
}
}
dbenv->allow_lines[dbenv->num_allow_lines] = strdup(line);
if (!dbenv->allow_lines[dbenv->num_allow_lines]) {
logmsg(LOGMSG_ERROR, "out of memory\n");
return -1;
}
dbenv->num_allow_lines++;
} else if (tokcmp(tok, ltok, "debug") == 0) {
tok = segtok(line, len, &st, <ok);
while (ltok > 0) {
if (tokcmp(tok, ltok, "rtcpu") == 0) {
logmsg(LOGMSG_INFO, "enable rtcpu debugging\n");
gbl_rtcpu_debug = 1;
}
tok = segtok(line, len, &st, <ok);
}
} else if (tokcmp(tok, ltok, "resource") == 0) {
/* I used to allow a one argument version of resource -
* resource <filepath>
* This wasn't implemented too well and caused problems. Explicit
* two argument version is better:
* resource <name> <filepath>
*/
char *name = NULL;
char *file = NULL;
tok = segtok(line, len, &st, <ok);
if (ltok == 0) {
logmsg(LOGMSG_ERROR, "Expected resource name\n");