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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
|
%{
/*-
* Copyright (c) 2009-2010 The FreeBSD Foundation
* Copyright (c) 2011 Pawel Jakub Dawidek <pawel@dawidek.net>
* All rights reserved.
*
* This software was developed by Pawel Jakub Dawidek under sponsorship from
* the FreeBSD Foundation.
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS 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 AUTHORS 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.
*
* $FreeBSD$
*/
#include <sys/param.h> /* MAXHOSTNAMELEN */
#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <arpa/inet.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
#include <pjdlog.h>
#include "hast.h"
extern int depth;
extern int lineno;
extern FILE *yyin;
extern char *yytext;
static struct hastd_config *lconfig;
static struct hast_resource *curres;
static bool mynode, hadmynode;
static char depth0_control[HAST_ADDRSIZE];
static char depth0_pidfile[PATH_MAX];
static char depth0_listen_tcp4[HAST_ADDRSIZE];
static char depth0_listen_tcp6[HAST_ADDRSIZE];
static TAILQ_HEAD(, hastd_listen) depth0_listen;
static int depth0_replication;
static int depth0_checksum;
static int depth0_compression;
static int depth0_timeout;
static char depth0_exec[PATH_MAX];
static int depth0_metaflush;
static char depth1_provname[PATH_MAX];
static char depth1_localpath[PATH_MAX];
static int depth1_metaflush;
extern void yyerror(const char *);
extern int yylex(void);
extern void yyrestart(FILE *);
static int isitme(const char *name);
static bool family_supported(int family);
static int node_names(char **namesp);
%}
%token CONTROL PIDFILE LISTEN REPLICATION CHECKSUM COMPRESSION METAFLUSH
%token TIMEOUT EXEC RESOURCE NAME LOCAL REMOTE SOURCE ON OFF
%token FULLSYNC MEMSYNC ASYNC NONE CRC32 SHA256 HOLE LZF
%token NUM STR OB CB
%type <str> remote_str
%type <num> replication_type
%type <num> checksum_type
%type <num> compression_type
%type <num> boolean
%union
{
int num;
char *str;
}
%token <num> NUM
%token <str> STR
%%
statements:
|
statements statement
;
statement:
control_statement
|
pidfile_statement
|
listen_statement
|
replication_statement
|
checksum_statement
|
compression_statement
|
timeout_statement
|
exec_statement
|
metaflush_statement
|
node_statement
|
resource_statement
;
control_statement: CONTROL STR
{
switch (depth) {
case 0:
if (strlcpy(depth0_control, $2,
sizeof(depth0_control)) >=
sizeof(depth0_control)) {
pjdlog_error("control argument is too long.");
free($2);
return (1);
}
break;
case 1:
if (!mynode)
break;
if (strlcpy(lconfig->hc_controladdr, $2,
sizeof(lconfig->hc_controladdr)) >=
sizeof(lconfig->hc_controladdr)) {
pjdlog_error("control argument is too long.");
free($2);
return (1);
}
break;
default:
PJDLOG_ABORT("control at wrong depth level");
}
free($2);
}
;
pidfile_statement: PIDFILE STR
{
switch (depth) {
case 0:
if (strlcpy(depth0_pidfile, $2,
sizeof(depth0_pidfile)) >=
sizeof(depth0_pidfile)) {
pjdlog_error("pidfile argument is too long.");
free($2);
return (1);
}
break;
case 1:
if (!mynode)
break;
if (strlcpy(lconfig->hc_pidfile, $2,
sizeof(lconfig->hc_pidfile)) >=
sizeof(lconfig->hc_pidfile)) {
pjdlog_error("pidfile argument is too long.");
free($2);
return (1);
}
break;
default:
PJDLOG_ABORT("pidfile at wrong depth level");
}
free($2);
}
;
listen_statement: LISTEN STR
{
struct hastd_listen *lst;
lst = calloc(1, sizeof(*lst));
if (lst == NULL) {
pjdlog_error("Unable to allocate memory for listen address.");
free($2);
return (1);
}
if (strlcpy(lst->hl_addr, $2, sizeof(lst->hl_addr)) >=
sizeof(lst->hl_addr)) {
pjdlog_error("listen argument is too long.");
free($2);
free(lst);
return (1);
}
switch (depth) {
case 0:
TAILQ_INSERT_TAIL(&depth0_listen, lst, hl_next);
break;
case 1:
if (mynode)
TAILQ_INSERT_TAIL(&depth0_listen, lst, hl_next);
else
free(lst);
break;
default:
PJDLOG_ABORT("listen at wrong depth level");
}
free($2);
}
;
replication_statement: REPLICATION replication_type
{
switch (depth) {
case 0:
depth0_replication = $2;
break;
case 1:
PJDLOG_ASSERT(curres != NULL);
curres->hr_replication = $2;
curres->hr_original_replication = $2;
break;
default:
PJDLOG_ABORT("replication at wrong depth level");
}
}
;
replication_type:
FULLSYNC { $$ = HAST_REPLICATION_FULLSYNC; }
|
MEMSYNC { $$ = HAST_REPLICATION_MEMSYNC; }
|
ASYNC { $$ = HAST_REPLICATION_ASYNC; }
;
checksum_statement: CHECKSUM checksum_type
{
switch (depth) {
case 0:
depth0_checksum = $2;
break;
case 1:
PJDLOG_ASSERT(curres != NULL);
curres->hr_checksum = $2;
break;
default:
PJDLOG_ABORT("checksum at wrong depth level");
}
}
;
checksum_type:
NONE { $$ = HAST_CHECKSUM_NONE; }
|
CRC32 { $$ = HAST_CHECKSUM_CRC32; }
|
SHA256 { $$ = HAST_CHECKSUM_SHA256; }
;
compression_statement: COMPRESSION compression_type
{
switch (depth) {
case 0:
depth0_compression = $2;
break;
case 1:
PJDLOG_ASSERT(curres != NULL);
curres->hr_compression = $2;
break;
default:
PJDLOG_ABORT("compression at wrong depth level");
}
}
;
compression_type:
NONE { $$ = HAST_COMPRESSION_NONE; }
|
HOLE { $$ = HAST_COMPRESSION_HOLE; }
|
LZF { $$ = HAST_COMPRESSION_LZF; }
;
timeout_statement: TIMEOUT NUM
{
if ($2 <= 0) {
pjdlog_error("Negative or zero timeout.");
return (1);
}
switch (depth) {
case 0:
depth0_timeout = $2;
break;
case 1:
PJDLOG_ASSERT(curres != NULL);
curres->hr_timeout = $2;
break;
default:
PJDLOG_ABORT("timeout at wrong depth level");
}
}
;
exec_statement: EXEC STR
{
switch (depth) {
case 0:
if (strlcpy(depth0_exec, $2, sizeof(depth0_exec)) >=
sizeof(depth0_exec)) {
pjdlog_error("Exec path is too long.");
free($2);
return (1);
}
break;
case 1:
PJDLOG_ASSERT(curres != NULL);
if (strlcpy(curres->hr_exec, $2,
sizeof(curres->hr_exec)) >=
sizeof(curres->hr_exec)) {
pjdlog_error("Exec path is too long.");
free($2);
return (1);
}
break;
default:
PJDLOG_ABORT("exec at wrong depth level");
}
free($2);
}
;
metaflush_statement: METAFLUSH boolean
{
switch (depth) {
case 0:
depth0_metaflush = $2;
break;
case 1:
PJDLOG_ASSERT(curres != NULL);
depth1_metaflush = $2;
break;
case 2:
if (!mynode)
break;
PJDLOG_ASSERT(curres != NULL);
curres->hr_metaflush = $2;
break;
default:
PJDLOG_ABORT("metaflush at wrong depth level");
}
}
;
boolean:
ON { $$ = 1; }
|
OFF { $$ = 0; }
;
node_statement: ON node_start OB node_entries CB
{
mynode = false;
}
;
node_start: STR
{
switch (isitme($1)) {
case -1:
free($1);
return (1);
case 0:
break;
case 1:
mynode = true;
break;
default:
PJDLOG_ABORT("invalid isitme() return value");
}
free($1);
}
;
node_entries:
|
node_entries node_entry
;
node_entry:
control_statement
|
pidfile_statement
|
listen_statement
;
resource_statement: RESOURCE resource_start OB resource_entries CB
{
if (curres != NULL) {
/*
* There must be section for this node, at least with
* remote address configuration.
*/
if (!hadmynode) {
char *names;
if (node_names(&names) != 0)
return (1);
pjdlog_error("No resource %s configuration for this node (acceptable node names: %s).",
curres->hr_name, names);
return (1);
}
/*
* Let's see if there are some resource-level settings
* that we can use for node-level settings.
*/
if (curres->hr_provname[0] == '\0' &&
depth1_provname[0] != '\0') {
/*
* Provider name is not set at node-level,
* but is set at resource-level, use it.
*/
strlcpy(curres->hr_provname, depth1_provname,
sizeof(curres->hr_provname));
}
if (curres->hr_localpath[0] == '\0' &&
depth1_localpath[0] != '\0') {
/*
* Path to local provider is not set at
* node-level, but is set at resource-level,
* use it.
*/
strlcpy(curres->hr_localpath, depth1_localpath,
sizeof(curres->hr_localpath));
}
if (curres->hr_metaflush == -1 && depth1_metaflush != -1) {
/*
* Metaflush is not set at node-level,
* but is set at resource-level, use it.
*/
curres->hr_metaflush = depth1_metaflush;
}
/*
* If provider name is not given, use resource name
* as provider name.
*/
if (curres->hr_provname[0] == '\0') {
strlcpy(curres->hr_provname, curres->hr_name,
sizeof(curres->hr_provname));
}
/*
* Remote address has to be configured at this point.
*/
if (curres->hr_remoteaddr[0] == '\0') {
pjdlog_error("Remote address not configured for resource %s.",
curres->hr_name);
return (1);
}
/*
* Path to local provider has to be configured at this
* point.
*/
if (curres->hr_localpath[0] == '\0') {
pjdlog_error("Path to local component not configured for resource %s.",
curres->hr_name);
return (1);
}
/* Put it onto resource list. */
TAILQ_INSERT_TAIL(&lconfig->hc_resources, curres, hr_next);
curres = NULL;
}
}
;
resource_start: STR
{
/* Check if there is no duplicate entry. */
TAILQ_FOREACH(curres, &lconfig->hc_resources, hr_next) {
if (strcmp(curres->hr_name, $1) == 0) {
pjdlog_error("Resource %s configured more than once.",
curres->hr_name);
free($1);
return (1);
}
}
/*
* Clear those, so we can tell if they were set at
* resource-level or not.
*/
depth1_provname[0] = '\0';
depth1_localpath[0] = '\0';
depth1_metaflush = -1;
hadmynode = false;
curres = calloc(1, sizeof(*curres));
if (curres == NULL) {
pjdlog_error("Unable to allocate memory for resource.");
free($1);
return (1);
}
if (strlcpy(curres->hr_name, $1,
sizeof(curres->hr_name)) >=
sizeof(curres->hr_name)) {
pjdlog_error("Resource name is too long.");
free(curres);
free($1);
return (1);
}
free($1);
curres->hr_role = HAST_ROLE_INIT;
curres->hr_previous_role = HAST_ROLE_INIT;
curres->hr_replication = -1;
curres->hr_original_replication = -1;
curres->hr_checksum = -1;
curres->hr_compression = -1;
curres->hr_version = 1;
curres->hr_timeout = -1;
curres->hr_exec[0] = '\0';
curres->hr_provname[0] = '\0';
curres->hr_localpath[0] = '\0';
curres->hr_localfd = -1;
curres->hr_localflush = true;
curres->hr_metaflush = -1;
curres->hr_remoteaddr[0] = '\0';
curres->hr_sourceaddr[0] = '\0';
curres->hr_ggateunit = -1;
}
;
resource_entries:
|
resource_entries resource_entry
;
resource_entry:
replication_statement
|
checksum_statement
|
compression_statement
|
timeout_statement
|
exec_statement
|
metaflush_statement
|
name_statement
|
local_statement
|
resource_node_statement
;
name_statement: NAME STR
{
switch (depth) {
case 1:
if (strlcpy(depth1_provname, $2,
sizeof(depth1_provname)) >=
sizeof(depth1_provname)) {
pjdlog_error("name argument is too long.");
free($2);
return (1);
}
break;
case 2:
if (!mynode)
break;
PJDLOG_ASSERT(curres != NULL);
if (strlcpy(curres->hr_provname, $2,
sizeof(curres->hr_provname)) >=
sizeof(curres->hr_provname)) {
pjdlog_error("name argument is too long.");
free($2);
return (1);
}
break;
default:
PJDLOG_ABORT("name at wrong depth level");
}
free($2);
}
;
local_statement: LOCAL STR
{
switch (depth) {
case 1:
if (strlcpy(depth1_localpath, $2,
sizeof(depth1_localpath)) >=
sizeof(depth1_localpath)) {
pjdlog_error("local argument is too long.");
free($2);
return (1);
}
break;
case 2:
if (!mynode)
break;
PJDLOG_ASSERT(curres != NULL);
if (strlcpy(curres->hr_localpath, $2,
sizeof(curres->hr_localpath)) >=
sizeof(curres->hr_localpath)) {
pjdlog_error("local argument is too long.");
free($2);
return (1);
}
break;
default:
PJDLOG_ABORT("local at wrong depth level");
}
free($2);
}
;
resource_node_statement:ON resource_node_start OB resource_node_entries CB
{
mynode = false;
}
;
resource_node_start: STR
{
if (curres != NULL) {
switch (isitme($1)) {
case -1:
free($1);
return (1);
case 0:
break;
case 1:
mynode = hadmynode = true;
break;
default:
PJDLOG_ABORT("invalid isitme() return value");
}
}
free($1);
}
;
resource_node_entries:
|
resource_node_entries resource_node_entry
;
resource_node_entry:
name_statement
|
local_statement
|
remote_statement
|
source_statement
|
metaflush_statement
;
remote_statement: REMOTE remote_str
{
PJDLOG_ASSERT(depth == 2);
if (mynode) {
PJDLOG_ASSERT(curres != NULL);
if (strlcpy(curres->hr_remoteaddr, $2,
sizeof(curres->hr_remoteaddr)) >=
sizeof(curres->hr_remoteaddr)) {
pjdlog_error("remote argument is too long.");
free($2);
return (1);
}
}
free($2);
}
;
remote_str:
NONE { $$ = strdup("none"); }
|
STR { }
;
source_statement: SOURCE STR
{
PJDLOG_ASSERT(depth == 2);
if (mynode) {
PJDLOG_ASSERT(curres != NULL);
if (strlcpy(curres->hr_sourceaddr, $2,
sizeof(curres->hr_sourceaddr)) >=
sizeof(curres->hr_sourceaddr)) {
pjdlog_error("source argument is too long.");
free($2);
return (1);
}
}
free($2);
}
;
%%
static int
isitme(const char *name)
{
char buf[MAXHOSTNAMELEN];
unsigned long hostid;
char *pos;
size_t bufsize;
/*
* First check if the given name matches our full hostname.
*/
if (gethostname(buf, sizeof(buf)) < 0) {
pjdlog_errno(LOG_ERR, "gethostname() failed");
return (-1);
}
if (strcmp(buf, name) == 0)
return (1);
/*
* Check if it matches first part of the host name.
*/
pos = strchr(buf, '.');
if (pos != NULL && (size_t)(pos - buf) == strlen(name) &&
strncmp(buf, name, pos - buf) == 0) {
return (1);
}
/*
* Check if it matches host UUID.
*/
bufsize = sizeof(buf);
if (sysctlbyname("kern.hostuuid", buf, &bufsize, NULL, 0) < 0) {
pjdlog_errno(LOG_ERR, "sysctlbyname(kern.hostuuid) failed");
return (-1);
}
if (strcasecmp(buf, name) == 0)
return (1);
/*
* Check if it matches hostid.
*/
bufsize = sizeof(hostid);
if (sysctlbyname("kern.hostid", &hostid, &bufsize, NULL, 0) < 0) {
pjdlog_errno(LOG_ERR, "sysctlbyname(kern.hostid) failed");
return (-1);
}
(void)snprintf(buf, sizeof(buf), "hostid%lu", hostid);
if (strcmp(buf, name) == 0)
return (1);
/*
* Looks like this isn't about us.
*/
return (0);
}
static bool
family_supported(int family)
{
int sock;
sock = socket(family, SOCK_STREAM, 0);
if (sock == -1 && errno == EPROTONOSUPPORT)
return (false);
if (sock >= 0)
(void)close(sock);
return (true);
}
static int
node_names(char **namesp)
{
static char names[MAXHOSTNAMELEN * 3];
char buf[MAXHOSTNAMELEN];
unsigned long hostid;
char *pos;
size_t bufsize;
if (gethostname(buf, sizeof(buf)) < 0) {
pjdlog_errno(LOG_ERR, "gethostname() failed");
return (-1);
}
/* First component of the host name. */
pos = strchr(buf, '.');
if (pos != NULL && pos != buf) {
(void)strlcpy(names, buf, MIN((size_t)(pos - buf + 1),
sizeof(names)));
(void)strlcat(names, ", ", sizeof(names));
}
/* Full host name. */
(void)strlcat(names, buf, sizeof(names));
(void)strlcat(names, ", ", sizeof(names));
/* Host UUID. */
bufsize = sizeof(buf);
if (sysctlbyname("kern.hostuuid", buf, &bufsize, NULL, 0) < 0) {
pjdlog_errno(LOG_ERR, "sysctlbyname(kern.hostuuid) failed");
return (-1);
}
(void)strlcat(names, buf, sizeof(names));
(void)strlcat(names, ", ", sizeof(names));
/* Host ID. */
bufsize = sizeof(hostid);
if (sysctlbyname("kern.hostid", &hostid, &bufsize, NULL, 0) < 0) {
pjdlog_errno(LOG_ERR, "sysctlbyname(kern.hostid) failed");
return (-1);
}
(void)snprintf(buf, sizeof(buf), "hostid%lu", hostid);
(void)strlcat(names, buf, sizeof(names));
*namesp = names;
return (0);
}
void
yyerror(const char *str)
{
pjdlog_error("Unable to parse configuration file at line %d near '%s': %s",
lineno, yytext, str);
}
struct hastd_config *
yy_config_parse(const char *config, bool exitonerror)
{
int ret;
curres = NULL;
mynode = false;
depth = 0;
lineno = 0;
depth0_timeout = HAST_TIMEOUT;
depth0_replication = HAST_REPLICATION_MEMSYNC;
depth0_checksum = HAST_CHECKSUM_NONE;
depth0_compression = HAST_COMPRESSION_HOLE;
strlcpy(depth0_control, HAST_CONTROL, sizeof(depth0_control));
strlcpy(depth0_pidfile, HASTD_PIDFILE, sizeof(depth0_pidfile));
TAILQ_INIT(&depth0_listen);
strlcpy(depth0_listen_tcp4, HASTD_LISTEN_TCP4,
sizeof(depth0_listen_tcp4));
strlcpy(depth0_listen_tcp6, HASTD_LISTEN_TCP6,
sizeof(depth0_listen_tcp6));
depth0_exec[0] = '\0';
depth0_metaflush = 1;
lconfig = calloc(1, sizeof(*lconfig));
if (lconfig == NULL) {
pjdlog_error("Unable to allocate memory for configuration.");
if (exitonerror)
exit(EX_TEMPFAIL);
return (NULL);
}
TAILQ_INIT(&lconfig->hc_listen);
TAILQ_INIT(&lconfig->hc_resources);
yyin = fopen(config, "r");
if (yyin == NULL) {
pjdlog_errno(LOG_ERR, "Unable to open configuration file %s",
config);
yy_config_free(lconfig);
if (exitonerror)
exit(EX_OSFILE);
return (NULL);
}
yyrestart(yyin);
ret = yyparse();
fclose(yyin);
if (ret != 0) {
yy_config_free(lconfig);
if (exitonerror)
exit(EX_CONFIG);
return (NULL);
}
/*
* Let's see if everything is set up.
*/
if (lconfig->hc_controladdr[0] == '\0') {
strlcpy(lconfig->hc_controladdr, depth0_control,
sizeof(lconfig->hc_controladdr));
}
if (lconfig->hc_pidfile[0] == '\0') {
strlcpy(lconfig->hc_pidfile, depth0_pidfile,
sizeof(lconfig->hc_pidfile));
}
if (!TAILQ_EMPTY(&depth0_listen))
TAILQ_CONCAT(&lconfig->hc_listen, &depth0_listen, hl_next);
if (TAILQ_EMPTY(&lconfig->hc_listen)) {
struct hastd_listen *lst;
if (family_supported(AF_INET)) {
lst = calloc(1, sizeof(*lst));
if (lst == NULL) {
pjdlog_error("Unable to allocate memory for listen address.");
yy_config_free(lconfig);
if (exitonerror)
exit(EX_TEMPFAIL);
return (NULL);
}
(void)strlcpy(lst->hl_addr, depth0_listen_tcp4,
sizeof(lst->hl_addr));
TAILQ_INSERT_TAIL(&lconfig->hc_listen, lst, hl_next);
} else {
pjdlog_debug(1,
"No IPv4 support in the kernel, not listening on IPv4 address.");
}
if (family_supported(AF_INET6)) {
lst = calloc(1, sizeof(*lst));
if (lst == NULL) {
pjdlog_error("Unable to allocate memory for listen address.");
yy_config_free(lconfig);
if (exitonerror)
exit(EX_TEMPFAIL);
return (NULL);
}
(void)strlcpy(lst->hl_addr, depth0_listen_tcp6,
sizeof(lst->hl_addr));
TAILQ_INSERT_TAIL(&lconfig->hc_listen, lst, hl_next);
} else {
pjdlog_debug(1,
"No IPv6 support in the kernel, not listening on IPv6 address.");
}
if (TAILQ_EMPTY(&lconfig->hc_listen)) {
pjdlog_error("No address to listen on.");
yy_config_free(lconfig);
if (exitonerror)
exit(EX_TEMPFAIL);
return (NULL);
}
}
TAILQ_FOREACH(curres, &lconfig->hc_resources, hr_next) {
PJDLOG_ASSERT(curres->hr_provname[0] != '\0');
PJDLOG_ASSERT(curres->hr_localpath[0] != '\0');
PJDLOG_ASSERT(curres->hr_remoteaddr[0] != '\0');
if (curres->hr_replication == -1) {
/*
* Replication is not set at resource-level.
* Use global or default setting.
*/
curres->hr_replication = depth0_replication;
curres->hr_original_replication = depth0_replication;
}
if (curres->hr_checksum == -1) {
/*
* Checksum is not set at resource-level.
* Use global or default setting.
*/
curres->hr_checksum = depth0_checksum;
}
if (curres->hr_compression == -1) {
/*
* Compression is not set at resource-level.
* Use global or default setting.
*/
curres->hr_compression = depth0_compression;
}
if (curres->hr_timeout == -1) {
/*
* Timeout is not set at resource-level.
* Use global or default setting.
*/
curres->hr_timeout = depth0_timeout;
}
if (curres->hr_exec[0] == '\0') {
/*
* Exec is not set at resource-level.
* Use global or default setting.
*/
strlcpy(curres->hr_exec, depth0_exec,
sizeof(curres->hr_exec));
}
if (curres->hr_metaflush == -1) {
/*
* Metaflush is not set at resource-level.
* Use global or default setting.
*/
curres->hr_metaflush = depth0_metaflush;
}
}
return (lconfig);
}
void
yy_config_free(struct hastd_config *config)
{
struct hastd_listen *lst;
struct hast_resource *res;
while ((lst = TAILQ_FIRST(&depth0_listen)) != NULL) {
TAILQ_REMOVE(&depth0_listen, lst, hl_next);
free(lst);
}
while ((lst = TAILQ_FIRST(&config->hc_listen)) != NULL) {
TAILQ_REMOVE(&config->hc_listen, lst, hl_next);
free(lst);
}
while ((res = TAILQ_FIRST(&config->hc_resources)) != NULL) {
TAILQ_REMOVE(&config->hc_resources, res, hr_next);
free(res);
}
free(config);
}
|