3 * Copyright (c) 2009-2010 The FreeBSD Foundation
4 * Copyright (c) 2011 Pawel Jakub Dawidek <pawel@dawidek.net>
7 * This software was developed by Pawel Jakub Dawidek under sponsorship from
8 * the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/param.h> /* MAXHOSTNAMELEN */
35 #include <sys/queue.h>
36 #include <sys/sysctl.h>
38 #include <arpa/inet.h>
57 static struct hastd_config *lconfig;
58 static struct hast_resource *curres;
59 static bool mynode, hadmynode;
61 static char depth0_control[HAST_ADDRSIZE];
62 static char depth0_listen[HAST_ADDRSIZE];
63 static int depth0_replication;
64 static int depth0_checksum;
65 static int depth0_compression;
66 static int depth0_timeout;
67 static char depth0_exec[PATH_MAX];
69 static char depth1_provname[PATH_MAX];
70 static char depth1_localpath[PATH_MAX];
72 extern void yyrestart(FILE *);
75 isitme(const char *name)
77 char buf[MAXHOSTNAMELEN];
82 * First check if the give name matches our full hostname.
84 if (gethostname(buf, sizeof(buf)) < 0) {
85 pjdlog_errno(LOG_ERR, "gethostname() failed");
88 if (strcmp(buf, name) == 0)
92 * Now check if it matches first part of the host name.
94 pos = strchr(buf, '.');
95 if (pos != NULL && pos != buf && strncmp(buf, name, pos - buf) == 0)
99 * At the end check if name is equal to our host's UUID.
101 bufsize = sizeof(buf);
102 if (sysctlbyname("kern.hostuuid", buf, &bufsize, NULL, 0) < 0) {
103 pjdlog_errno(LOG_ERR, "sysctlbyname(kern.hostuuid) failed");
106 if (strcasecmp(buf, name) == 0)
110 * Looks like this isn't about us.
116 node_names(char **namesp)
118 static char names[MAXHOSTNAMELEN * 3];
119 char buf[MAXHOSTNAMELEN];
123 if (gethostname(buf, sizeof(buf)) < 0) {
124 pjdlog_errno(LOG_ERR, "gethostname() failed");
128 /* First component of the host name. */
129 pos = strchr(buf, '.');
130 if (pos != NULL && pos != buf) {
131 (void)strlcpy(names, buf, MIN((size_t)(pos - buf + 1),
133 (void)strlcat(names, ", ", sizeof(names));
136 /* Full host name. */
137 (void)strlcat(names, buf, sizeof(names));
138 (void)strlcat(names, ", ", sizeof(names));
141 bufsize = sizeof(buf);
142 if (sysctlbyname("kern.hostuuid", buf, &bufsize, NULL, 0) < 0) {
143 pjdlog_errno(LOG_ERR, "sysctlbyname(kern.hostuuid) failed");
146 (void)strlcat(names, buf, sizeof(names));
154 yyerror(const char *str)
157 pjdlog_error("Unable to parse configuration file at line %d near '%s': %s",
158 lineno, yytext, str);
161 struct hastd_config *
162 yy_config_parse(const char *config, bool exitonerror)
171 depth0_timeout = HAST_TIMEOUT;
172 depth0_replication = HAST_REPLICATION_FULLSYNC;
173 depth0_checksum = HAST_CHECKSUM_NONE;
174 depth0_compression = HAST_COMPRESSION_HOLE;
175 strlcpy(depth0_control, HAST_CONTROL, sizeof(depth0_control));
176 strlcpy(depth0_listen, HASTD_LISTEN, sizeof(depth0_listen));
177 depth0_exec[0] = '\0';
179 lconfig = calloc(1, sizeof(*lconfig));
180 if (lconfig == NULL) {
181 pjdlog_error("Unable to allocate memory for configuration.");
187 TAILQ_INIT(&lconfig->hc_resources);
189 yyin = fopen(config, "r");
191 pjdlog_errno(LOG_ERR, "Unable to open configuration file %s",
193 yy_config_free(lconfig);
202 yy_config_free(lconfig);
209 * Let's see if everything is set up.
211 if (lconfig->hc_controladdr[0] == '\0') {
212 strlcpy(lconfig->hc_controladdr, depth0_control,
213 sizeof(lconfig->hc_controladdr));
215 if (lconfig->hc_listenaddr[0] == '\0') {
216 strlcpy(lconfig->hc_listenaddr, depth0_listen,
217 sizeof(lconfig->hc_listenaddr));
219 TAILQ_FOREACH(curres, &lconfig->hc_resources, hr_next) {
220 assert(curres->hr_provname[0] != '\0');
221 assert(curres->hr_localpath[0] != '\0');
222 assert(curres->hr_remoteaddr[0] != '\0');
224 if (curres->hr_replication == -1) {
226 * Replication is not set at resource-level.
227 * Use global or default setting.
229 curres->hr_replication = depth0_replication;
231 if (curres->hr_replication == HAST_REPLICATION_MEMSYNC ||
232 curres->hr_replication == HAST_REPLICATION_ASYNC) {
233 pjdlog_warning("Replication mode \"%s\" is not implemented, falling back to \"%s\".",
234 curres->hr_replication == HAST_REPLICATION_MEMSYNC ?
235 "memsync" : "async", "fullsync");
236 curres->hr_replication = HAST_REPLICATION_FULLSYNC;
238 if (curres->hr_checksum == -1) {
240 * Checksum is not set at resource-level.
241 * Use global or default setting.
243 curres->hr_checksum = depth0_checksum;
245 if (curres->hr_compression == -1) {
247 * Compression is not set at resource-level.
248 * Use global or default setting.
250 curres->hr_compression = depth0_compression;
252 if (curres->hr_timeout == -1) {
254 * Timeout is not set at resource-level.
255 * Use global or default setting.
257 curres->hr_timeout = depth0_timeout;
259 if (curres->hr_exec[0] == '\0') {
261 * Exec is not set at resource-level.
262 * Use global or default setting.
264 strlcpy(curres->hr_exec, depth0_exec,
265 sizeof(curres->hr_exec));
273 yy_config_free(struct hastd_config *config)
275 struct hast_resource *res;
277 while ((res = TAILQ_FIRST(&config->hc_resources)) != NULL) {
278 TAILQ_REMOVE(&config->hc_resources, res, hr_next);
285 %token CONTROL LISTEN PORT REPLICATION CHECKSUM COMPRESSION
286 %token TIMEOUT EXEC EXTENTSIZE RESOURCE NAME LOCAL REMOTE SOURCE ON
287 %token FULLSYNC MEMSYNC ASYNC NONE CRC32 SHA256 HOLE LZF
290 %type <num> replication_type
291 %type <num> checksum_type
292 %type <num> compression_type
315 replication_statement
319 compression_statement
330 control_statement: CONTROL STR
334 if (strlcpy(depth0_control, $2,
335 sizeof(depth0_control)) >=
336 sizeof(depth0_control)) {
337 pjdlog_error("control argument is too long.");
345 if (strlcpy(lconfig->hc_controladdr, $2,
346 sizeof(lconfig->hc_controladdr)) >=
347 sizeof(lconfig->hc_controladdr)) {
348 pjdlog_error("control argument is too long.");
354 assert(!"control at wrong depth level");
360 listen_statement: LISTEN STR
364 if (strlcpy(depth0_listen, $2,
365 sizeof(depth0_listen)) >=
366 sizeof(depth0_listen)) {
367 pjdlog_error("listen argument is too long.");
375 if (strlcpy(lconfig->hc_listenaddr, $2,
376 sizeof(lconfig->hc_listenaddr)) >=
377 sizeof(lconfig->hc_listenaddr)) {
378 pjdlog_error("listen argument is too long.");
384 assert(!"listen at wrong depth level");
390 replication_statement: REPLICATION replication_type
394 depth0_replication = $2;
398 curres->hr_replication = $2;
401 assert(!"replication at wrong depth level");
407 FULLSYNC { $$ = HAST_REPLICATION_FULLSYNC; }
409 MEMSYNC { $$ = HAST_REPLICATION_MEMSYNC; }
411 ASYNC { $$ = HAST_REPLICATION_ASYNC; }
414 checksum_statement: CHECKSUM checksum_type
418 depth0_checksum = $2;
422 curres->hr_checksum = $2;
425 assert(!"checksum at wrong depth level");
431 NONE { $$ = HAST_CHECKSUM_NONE; }
433 CRC32 { $$ = HAST_CHECKSUM_CRC32; }
435 SHA256 { $$ = HAST_CHECKSUM_SHA256; }
438 compression_statement: COMPRESSION compression_type
442 depth0_compression = $2;
446 curres->hr_compression = $2;
449 assert(!"compression at wrong depth level");
455 NONE { $$ = HAST_COMPRESSION_NONE; }
457 HOLE { $$ = HAST_COMPRESSION_HOLE; }
459 LZF { $$ = HAST_COMPRESSION_LZF; }
462 timeout_statement: TIMEOUT NUM
465 pjdlog_error("Negative or zero timeout.");
474 curres->hr_timeout = $2;
477 assert(!"timeout at wrong depth level");
482 exec_statement: EXEC STR
486 if (strlcpy(depth0_exec, $2, sizeof(depth0_exec)) >=
487 sizeof(depth0_exec)) {
488 pjdlog_error("Exec path is too long.");
496 if (strlcpy(curres->hr_exec, $2,
497 sizeof(curres->hr_exec)) >=
498 sizeof(curres->hr_exec)) {
499 pjdlog_error("Exec path is too long.");
505 assert(!"exec at wrong depth level");
511 node_statement: ON node_start OB node_entries CB
519 switch (isitme($1)) {
529 assert(!"invalid isitme() return value");
537 node_entries node_entry
546 resource_statement: RESOURCE resource_start OB resource_entries CB
548 if (curres != NULL) {
550 * There must be section for this node, at least with
551 * remote address configuration.
556 if (node_names(&names) != 0)
558 pjdlog_error("No resource %s configuration for this node (acceptable node names: %s).",
559 curres->hr_name, names);
564 * Let's see there are some resource-level settings
565 * that we can use for node-level settings.
567 if (curres->hr_provname[0] == '\0' &&
568 depth1_provname[0] != '\0') {
570 * Provider name is not set at node-level,
571 * but is set at resource-level, use it.
573 strlcpy(curres->hr_provname, depth1_provname,
574 sizeof(curres->hr_provname));
576 if (curres->hr_localpath[0] == '\0' &&
577 depth1_localpath[0] != '\0') {
579 * Path to local provider is not set at
580 * node-level, but is set at resource-level,
583 strlcpy(curres->hr_localpath, depth1_localpath,
584 sizeof(curres->hr_localpath));
588 * If provider name is not given, use resource name
591 if (curres->hr_provname[0] == '\0') {
592 strlcpy(curres->hr_provname, curres->hr_name,
593 sizeof(curres->hr_provname));
597 * Remote address has to be configured at this point.
599 if (curres->hr_remoteaddr[0] == '\0') {
600 pjdlog_error("Remote address not configured for resource %s.",
605 * Path to local provider has to be configured at this
608 if (curres->hr_localpath[0] == '\0') {
609 pjdlog_error("Path to local component not configured for resource %s.",
614 /* Put it onto resource list. */
615 TAILQ_INSERT_TAIL(&lconfig->hc_resources, curres, hr_next);
623 /* Check if there is no duplicate entry. */
624 TAILQ_FOREACH(curres, &lconfig->hc_resources, hr_next) {
625 if (strcmp(curres->hr_name, $1) == 0) {
626 pjdlog_error("Resource %s configured more than once.",
634 * Clear those, so we can tell if they were set at
635 * resource-level or not.
637 depth1_provname[0] = '\0';
638 depth1_localpath[0] = '\0';
641 curres = calloc(1, sizeof(*curres));
642 if (curres == NULL) {
643 pjdlog_error("Unable to allocate memory for resource.");
647 if (strlcpy(curres->hr_name, $1,
648 sizeof(curres->hr_name)) >=
649 sizeof(curres->hr_name)) {
650 pjdlog_error("Resource name is too long.");
655 curres->hr_role = HAST_ROLE_INIT;
656 curres->hr_previous_role = HAST_ROLE_INIT;
657 curres->hr_replication = -1;
658 curres->hr_checksum = -1;
659 curres->hr_compression = -1;
660 curres->hr_timeout = -1;
661 curres->hr_exec[0] = '\0';
662 curres->hr_provname[0] = '\0';
663 curres->hr_localpath[0] = '\0';
664 curres->hr_localfd = -1;
665 curres->hr_remoteaddr[0] = '\0';
666 curres->hr_sourceaddr[0] = '\0';
667 curres->hr_ggateunit = -1;
673 resource_entries resource_entry
677 replication_statement
681 compression_statement
691 resource_node_statement
694 name_statement: NAME STR
698 if (strlcpy(depth1_provname, $2,
699 sizeof(depth1_provname)) >=
700 sizeof(depth1_provname)) {
701 pjdlog_error("name argument is too long.");
709 assert(curres != NULL);
710 if (strlcpy(curres->hr_provname, $2,
711 sizeof(curres->hr_provname)) >=
712 sizeof(curres->hr_provname)) {
713 pjdlog_error("name argument is too long.");
719 assert(!"name at wrong depth level");
725 local_statement: LOCAL STR
729 if (strlcpy(depth1_localpath, $2,
730 sizeof(depth1_localpath)) >=
731 sizeof(depth1_localpath)) {
732 pjdlog_error("local argument is too long.");
740 assert(curres != NULL);
741 if (strlcpy(curres->hr_localpath, $2,
742 sizeof(curres->hr_localpath)) >=
743 sizeof(curres->hr_localpath)) {
744 pjdlog_error("local argument is too long.");
750 assert(!"local at wrong depth level");
756 resource_node_statement:ON resource_node_start OB resource_node_entries CB
762 resource_node_start: STR
764 if (curres != NULL) {
765 switch (isitme($1)) {
772 mynode = hadmynode = true;
775 assert(!"invalid isitme() return value");
782 resource_node_entries:
784 resource_node_entries resource_node_entry
797 remote_statement: REMOTE STR
801 assert(curres != NULL);
802 if (strlcpy(curres->hr_remoteaddr, $2,
803 sizeof(curres->hr_remoteaddr)) >=
804 sizeof(curres->hr_remoteaddr)) {
805 pjdlog_error("remote argument is too long.");
814 source_statement: SOURCE STR
818 assert(curres != NULL);
819 if (strlcpy(curres->hr_sourceaddr, $2,
820 sizeof(curres->hr_sourceaddr)) >=
821 sizeof(curres->hr_sourceaddr)) {
822 pjdlog_error("source argument is too long.");