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_MEMSYNC;
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_checksum == -1) {
233 * Checksum is not set at resource-level.
234 * Use global or default setting.
236 curres->hr_checksum = depth0_checksum;
238 if (curres->hr_compression == -1) {
240 * Compression is not set at resource-level.
241 * Use global or default setting.
243 curres->hr_compression = depth0_compression;
245 if (curres->hr_timeout == -1) {
247 * Timeout is not set at resource-level.
248 * Use global or default setting.
250 curres->hr_timeout = depth0_timeout;
252 if (curres->hr_exec[0] == '\0') {
254 * Exec is not set at resource-level.
255 * Use global or default setting.
257 strlcpy(curres->hr_exec, depth0_exec,
258 sizeof(curres->hr_exec));
266 yy_config_free(struct hastd_config *config)
268 struct hast_resource *res;
270 while ((res = TAILQ_FIRST(&config->hc_resources)) != NULL) {
271 TAILQ_REMOVE(&config->hc_resources, res, hr_next);
278 %token CONTROL LISTEN PORT REPLICATION CHECKSUM COMPRESSION
279 %token TIMEOUT EXEC EXTENTSIZE RESOURCE NAME LOCAL REMOTE SOURCE ON
280 %token FULLSYNC MEMSYNC ASYNC NONE CRC32 SHA256 HOLE LZF
283 %type <num> replication_type
284 %type <num> checksum_type
285 %type <num> compression_type
308 replication_statement
312 compression_statement
323 control_statement: CONTROL STR
327 if (strlcpy(depth0_control, $2,
328 sizeof(depth0_control)) >=
329 sizeof(depth0_control)) {
330 pjdlog_error("control argument is too long.");
338 if (strlcpy(lconfig->hc_controladdr, $2,
339 sizeof(lconfig->hc_controladdr)) >=
340 sizeof(lconfig->hc_controladdr)) {
341 pjdlog_error("control argument is too long.");
347 assert(!"control at wrong depth level");
353 listen_statement: LISTEN STR
357 if (strlcpy(depth0_listen, $2,
358 sizeof(depth0_listen)) >=
359 sizeof(depth0_listen)) {
360 pjdlog_error("listen argument is too long.");
368 if (strlcpy(lconfig->hc_listenaddr, $2,
369 sizeof(lconfig->hc_listenaddr)) >=
370 sizeof(lconfig->hc_listenaddr)) {
371 pjdlog_error("listen argument is too long.");
377 assert(!"listen at wrong depth level");
383 replication_statement: REPLICATION replication_type
387 depth0_replication = $2;
391 curres->hr_replication = $2;
394 assert(!"replication at wrong depth level");
400 FULLSYNC { $$ = HAST_REPLICATION_FULLSYNC; }
402 MEMSYNC { $$ = HAST_REPLICATION_MEMSYNC; }
404 ASYNC { $$ = HAST_REPLICATION_ASYNC; }
407 checksum_statement: CHECKSUM checksum_type
411 depth0_checksum = $2;
415 curres->hr_checksum = $2;
418 assert(!"checksum at wrong depth level");
424 NONE { $$ = HAST_CHECKSUM_NONE; }
426 CRC32 { $$ = HAST_CHECKSUM_CRC32; }
428 SHA256 { $$ = HAST_CHECKSUM_SHA256; }
431 compression_statement: COMPRESSION compression_type
435 depth0_compression = $2;
439 curres->hr_compression = $2;
442 assert(!"compression at wrong depth level");
448 NONE { $$ = HAST_COMPRESSION_NONE; }
450 HOLE { $$ = HAST_COMPRESSION_HOLE; }
452 LZF { $$ = HAST_COMPRESSION_LZF; }
455 timeout_statement: TIMEOUT NUM
463 curres->hr_timeout = $2;
466 assert(!"timeout at wrong depth level");
471 exec_statement: EXEC STR
475 if (strlcpy(depth0_exec, $2, sizeof(depth0_exec)) >=
476 sizeof(depth0_exec)) {
477 pjdlog_error("Exec path is too long.");
485 if (strlcpy(curres->hr_exec, $2,
486 sizeof(curres->hr_exec)) >=
487 sizeof(curres->hr_exec)) {
488 pjdlog_error("Exec path is too long.");
494 assert(!"exec at wrong depth level");
500 node_statement: ON node_start OB node_entries CB
508 switch (isitme($1)) {
518 assert(!"invalid isitme() return value");
526 node_entries node_entry
535 resource_statement: RESOURCE resource_start OB resource_entries CB
537 if (curres != NULL) {
539 * There must be section for this node, at least with
540 * remote address configuration.
545 if (node_names(&names) != 0)
547 pjdlog_error("No resource %s configuration for this node (acceptable node names: %s).",
548 curres->hr_name, names);
553 * Let's see there are some resource-level settings
554 * that we can use for node-level settings.
556 if (curres->hr_provname[0] == '\0' &&
557 depth1_provname[0] != '\0') {
559 * Provider name is not set at node-level,
560 * but is set at resource-level, use it.
562 strlcpy(curres->hr_provname, depth1_provname,
563 sizeof(curres->hr_provname));
565 if (curres->hr_localpath[0] == '\0' &&
566 depth1_localpath[0] != '\0') {
568 * Path to local provider is not set at
569 * node-level, but is set at resource-level,
572 strlcpy(curres->hr_localpath, depth1_localpath,
573 sizeof(curres->hr_localpath));
577 * If provider name is not given, use resource name
580 if (curres->hr_provname[0] == '\0') {
581 strlcpy(curres->hr_provname, curres->hr_name,
582 sizeof(curres->hr_provname));
586 * Remote address has to be configured at this point.
588 if (curres->hr_remoteaddr[0] == '\0') {
589 pjdlog_error("Remote address not configured for resource %s.",
594 * Path to local provider has to be configured at this
597 if (curres->hr_localpath[0] == '\0') {
598 pjdlog_error("Path to local component not configured for resource %s.",
603 /* Put it onto resource list. */
604 TAILQ_INSERT_TAIL(&lconfig->hc_resources, curres, hr_next);
612 /* Check if there is no duplicate entry. */
613 TAILQ_FOREACH(curres, &lconfig->hc_resources, hr_next) {
614 if (strcmp(curres->hr_name, $1) == 0) {
615 pjdlog_error("Resource %s configured more than once.",
623 * Clear those, so we can tell if they were set at
624 * resource-level or not.
626 depth1_provname[0] = '\0';
627 depth1_localpath[0] = '\0';
630 curres = calloc(1, sizeof(*curres));
631 if (curres == NULL) {
632 pjdlog_error("Unable to allocate memory for resource.");
636 if (strlcpy(curres->hr_name, $1,
637 sizeof(curres->hr_name)) >=
638 sizeof(curres->hr_name)) {
639 pjdlog_error("Resource name is too long.");
644 curres->hr_role = HAST_ROLE_INIT;
645 curres->hr_previous_role = HAST_ROLE_INIT;
646 curres->hr_replication = -1;
647 curres->hr_checksum = -1;
648 curres->hr_compression = -1;
649 curres->hr_timeout = -1;
650 curres->hr_exec[0] = '\0';
651 curres->hr_provname[0] = '\0';
652 curres->hr_localpath[0] = '\0';
653 curres->hr_localfd = -1;
654 curres->hr_remoteaddr[0] = '\0';
655 curres->hr_sourceaddr[0] = '\0';
656 curres->hr_ggateunit = -1;
662 resource_entries resource_entry
666 replication_statement
670 compression_statement
680 resource_node_statement
683 name_statement: NAME STR
687 if (strlcpy(depth1_provname, $2,
688 sizeof(depth1_provname)) >=
689 sizeof(depth1_provname)) {
690 pjdlog_error("name argument is too long.");
698 assert(curres != NULL);
699 if (strlcpy(curres->hr_provname, $2,
700 sizeof(curres->hr_provname)) >=
701 sizeof(curres->hr_provname)) {
702 pjdlog_error("name argument is too long.");
708 assert(!"name at wrong depth level");
714 local_statement: LOCAL STR
718 if (strlcpy(depth1_localpath, $2,
719 sizeof(depth1_localpath)) >=
720 sizeof(depth1_localpath)) {
721 pjdlog_error("local argument is too long.");
729 assert(curres != NULL);
730 if (strlcpy(curres->hr_localpath, $2,
731 sizeof(curres->hr_localpath)) >=
732 sizeof(curres->hr_localpath)) {
733 pjdlog_error("local argument is too long.");
739 assert(!"local at wrong depth level");
745 resource_node_statement:ON resource_node_start OB resource_node_entries CB
751 resource_node_start: STR
753 if (curres != NULL) {
754 switch (isitme($1)) {
761 mynode = hadmynode = true;
764 assert(!"invalid isitme() return value");
771 resource_node_entries:
773 resource_node_entries resource_node_entry
786 remote_statement: REMOTE STR
790 assert(curres != NULL);
791 if (strlcpy(curres->hr_remoteaddr, $2,
792 sizeof(curres->hr_remoteaddr)) >=
793 sizeof(curres->hr_remoteaddr)) {
794 pjdlog_error("remote argument is too long.");
803 source_statement: SOURCE STR
807 assert(curres != NULL);
808 if (strlcpy(curres->hr_sourceaddr, $2,
809 sizeof(curres->hr_sourceaddr)) >=
810 sizeof(curres->hr_sourceaddr)) {
811 pjdlog_error("source argument is too long.");