3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * Copyright (c) 2009-2010 The FreeBSD Foundation
6 * Copyright (c) 2011 Pawel Jakub Dawidek <pawel@dawidek.net>
9 * This software was developed by Pawel Jakub Dawidek under sponsorship from
10 * the FreeBSD Foundation.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/param.h> /* MAXHOSTNAMELEN */
37 #include <sys/queue.h>
38 #include <sys/socket.h>
39 #include <sys/sysctl.h>
41 #include <arpa/inet.h>
60 static struct hastd_config *lconfig;
61 static struct hast_resource *curres;
62 static bool mynode, hadmynode;
64 static char depth0_control[HAST_ADDRSIZE];
65 static char depth0_pidfile[PATH_MAX];
66 static char depth0_listen_tcp4[HAST_ADDRSIZE];
67 static char depth0_listen_tcp6[HAST_ADDRSIZE];
68 static TAILQ_HEAD(, hastd_listen) depth0_listen;
69 static int depth0_replication;
70 static int depth0_checksum;
71 static int depth0_compression;
72 static int depth0_timeout;
73 static char depth0_exec[PATH_MAX];
74 static int depth0_metaflush;
76 static char depth1_provname[PATH_MAX];
77 static char depth1_localpath[PATH_MAX];
78 static int depth1_metaflush;
80 extern void yyerror(const char *);
81 extern int yylex(void);
82 extern void yyrestart(FILE *);
84 static int isitme(const char *name);
85 static bool family_supported(int family);
86 static int node_names(char **namesp);
89 %token CONTROL PIDFILE LISTEN REPLICATION CHECKSUM COMPRESSION METAFLUSH
90 %token TIMEOUT EXEC RESOURCE NAME LOCAL REMOTE SOURCE ON OFF
91 %token FULLSYNC MEMSYNC ASYNC NONE CRC32 SHA256 HOLE LZF
94 %type <str> remote_str
95 %type <num> replication_type
96 %type <num> checksum_type
97 %type <num> compression_type
123 replication_statement
127 compression_statement
140 control_statement: CONTROL STR
144 if (strlcpy(depth0_control, $2,
145 sizeof(depth0_control)) >=
146 sizeof(depth0_control)) {
147 pjdlog_error("control argument is too long.");
155 if (strlcpy(lconfig->hc_controladdr, $2,
156 sizeof(lconfig->hc_controladdr)) >=
157 sizeof(lconfig->hc_controladdr)) {
158 pjdlog_error("control argument is too long.");
164 PJDLOG_ABORT("control at wrong depth level");
170 pidfile_statement: PIDFILE STR
174 if (strlcpy(depth0_pidfile, $2,
175 sizeof(depth0_pidfile)) >=
176 sizeof(depth0_pidfile)) {
177 pjdlog_error("pidfile argument is too long.");
185 if (strlcpy(lconfig->hc_pidfile, $2,
186 sizeof(lconfig->hc_pidfile)) >=
187 sizeof(lconfig->hc_pidfile)) {
188 pjdlog_error("pidfile argument is too long.");
194 PJDLOG_ABORT("pidfile at wrong depth level");
200 listen_statement: LISTEN STR
202 struct hastd_listen *lst;
204 lst = calloc(1, sizeof(*lst));
206 pjdlog_error("Unable to allocate memory for listen address.");
210 if (strlcpy(lst->hl_addr, $2, sizeof(lst->hl_addr)) >=
211 sizeof(lst->hl_addr)) {
212 pjdlog_error("listen argument is too long.");
219 TAILQ_INSERT_TAIL(&depth0_listen, lst, hl_next);
223 TAILQ_INSERT_TAIL(&depth0_listen, lst, hl_next);
228 PJDLOG_ABORT("listen at wrong depth level");
234 replication_statement: REPLICATION replication_type
238 depth0_replication = $2;
241 PJDLOG_ASSERT(curres != NULL);
242 curres->hr_replication = $2;
243 curres->hr_original_replication = $2;
246 PJDLOG_ABORT("replication at wrong depth level");
252 FULLSYNC { $$ = HAST_REPLICATION_FULLSYNC; }
254 MEMSYNC { $$ = HAST_REPLICATION_MEMSYNC; }
256 ASYNC { $$ = HAST_REPLICATION_ASYNC; }
259 checksum_statement: CHECKSUM checksum_type
263 depth0_checksum = $2;
266 PJDLOG_ASSERT(curres != NULL);
267 curres->hr_checksum = $2;
270 PJDLOG_ABORT("checksum at wrong depth level");
276 NONE { $$ = HAST_CHECKSUM_NONE; }
278 CRC32 { $$ = HAST_CHECKSUM_CRC32; }
280 SHA256 { $$ = HAST_CHECKSUM_SHA256; }
283 compression_statement: COMPRESSION compression_type
287 depth0_compression = $2;
290 PJDLOG_ASSERT(curres != NULL);
291 curres->hr_compression = $2;
294 PJDLOG_ABORT("compression at wrong depth level");
300 NONE { $$ = HAST_COMPRESSION_NONE; }
302 HOLE { $$ = HAST_COMPRESSION_HOLE; }
304 LZF { $$ = HAST_COMPRESSION_LZF; }
307 timeout_statement: TIMEOUT NUM
310 pjdlog_error("Negative or zero timeout.");
318 PJDLOG_ASSERT(curres != NULL);
319 curres->hr_timeout = $2;
322 PJDLOG_ABORT("timeout at wrong depth level");
327 exec_statement: EXEC STR
331 if (strlcpy(depth0_exec, $2, sizeof(depth0_exec)) >=
332 sizeof(depth0_exec)) {
333 pjdlog_error("Exec path is too long.");
339 PJDLOG_ASSERT(curres != NULL);
340 if (strlcpy(curres->hr_exec, $2,
341 sizeof(curres->hr_exec)) >=
342 sizeof(curres->hr_exec)) {
343 pjdlog_error("Exec path is too long.");
349 PJDLOG_ABORT("exec at wrong depth level");
355 metaflush_statement: METAFLUSH boolean
359 depth0_metaflush = $2;
362 PJDLOG_ASSERT(curres != NULL);
363 depth1_metaflush = $2;
368 PJDLOG_ASSERT(curres != NULL);
369 curres->hr_metaflush = $2;
372 PJDLOG_ABORT("metaflush at wrong depth level");
383 node_statement: ON node_start OB node_entries CB
391 switch (isitme($1)) {
401 PJDLOG_ABORT("invalid isitme() return value");
409 node_entries node_entry
420 resource_statement: RESOURCE resource_start OB resource_entries CB
422 if (curres != NULL) {
424 * There must be section for this node, at least with
425 * remote address configuration.
430 if (node_names(&names) != 0)
432 pjdlog_error("No resource %s configuration for this node (acceptable node names: %s).",
433 curres->hr_name, names);
438 * Let's see if there are some resource-level settings
439 * that we can use for node-level settings.
441 if (curres->hr_provname[0] == '\0' &&
442 depth1_provname[0] != '\0') {
444 * Provider name is not set at node-level,
445 * but is set at resource-level, use it.
447 strlcpy(curres->hr_provname, depth1_provname,
448 sizeof(curres->hr_provname));
450 if (curres->hr_localpath[0] == '\0' &&
451 depth1_localpath[0] != '\0') {
453 * Path to local provider is not set at
454 * node-level, but is set at resource-level,
457 strlcpy(curres->hr_localpath, depth1_localpath,
458 sizeof(curres->hr_localpath));
460 if (curres->hr_metaflush == -1 && depth1_metaflush != -1) {
462 * Metaflush is not set at node-level,
463 * but is set at resource-level, use it.
465 curres->hr_metaflush = depth1_metaflush;
469 * If provider name is not given, use resource name
472 if (curres->hr_provname[0] == '\0') {
473 strlcpy(curres->hr_provname, curres->hr_name,
474 sizeof(curres->hr_provname));
478 * Remote address has to be configured at this point.
480 if (curres->hr_remoteaddr[0] == '\0') {
481 pjdlog_error("Remote address not configured for resource %s.",
486 * Path to local provider has to be configured at this
489 if (curres->hr_localpath[0] == '\0') {
490 pjdlog_error("Path to local component not configured for resource %s.",
495 /* Put it onto resource list. */
496 TAILQ_INSERT_TAIL(&lconfig->hc_resources, curres, hr_next);
504 /* Check if there is no duplicate entry. */
505 TAILQ_FOREACH(curres, &lconfig->hc_resources, hr_next) {
506 if (strcmp(curres->hr_name, $1) == 0) {
507 pjdlog_error("Resource %s configured more than once.",
515 * Clear those, so we can tell if they were set at
516 * resource-level or not.
518 depth1_provname[0] = '\0';
519 depth1_localpath[0] = '\0';
520 depth1_metaflush = -1;
523 curres = calloc(1, sizeof(*curres));
524 if (curres == NULL) {
525 pjdlog_error("Unable to allocate memory for resource.");
529 if (strlcpy(curres->hr_name, $1,
530 sizeof(curres->hr_name)) >=
531 sizeof(curres->hr_name)) {
532 pjdlog_error("Resource name is too long.");
538 curres->hr_role = HAST_ROLE_INIT;
539 curres->hr_previous_role = HAST_ROLE_INIT;
540 curres->hr_replication = -1;
541 curres->hr_original_replication = -1;
542 curres->hr_checksum = -1;
543 curres->hr_compression = -1;
544 curres->hr_version = 1;
545 curres->hr_timeout = -1;
546 curres->hr_exec[0] = '\0';
547 curres->hr_provname[0] = '\0';
548 curres->hr_localpath[0] = '\0';
549 curres->hr_localfd = -1;
550 curres->hr_localflush = true;
551 curres->hr_metaflush = -1;
552 curres->hr_remoteaddr[0] = '\0';
553 curres->hr_sourceaddr[0] = '\0';
554 curres->hr_ggateunit = -1;
560 resource_entries resource_entry
564 replication_statement
568 compression_statement
580 resource_node_statement
583 name_statement: NAME STR
587 if (strlcpy(depth1_provname, $2,
588 sizeof(depth1_provname)) >=
589 sizeof(depth1_provname)) {
590 pjdlog_error("name argument is too long.");
598 PJDLOG_ASSERT(curres != NULL);
599 if (strlcpy(curres->hr_provname, $2,
600 sizeof(curres->hr_provname)) >=
601 sizeof(curres->hr_provname)) {
602 pjdlog_error("name argument is too long.");
608 PJDLOG_ABORT("name at wrong depth level");
614 local_statement: LOCAL STR
618 if (strlcpy(depth1_localpath, $2,
619 sizeof(depth1_localpath)) >=
620 sizeof(depth1_localpath)) {
621 pjdlog_error("local argument is too long.");
629 PJDLOG_ASSERT(curres != NULL);
630 if (strlcpy(curres->hr_localpath, $2,
631 sizeof(curres->hr_localpath)) >=
632 sizeof(curres->hr_localpath)) {
633 pjdlog_error("local argument is too long.");
639 PJDLOG_ABORT("local at wrong depth level");
645 resource_node_statement:ON resource_node_start OB resource_node_entries CB
651 resource_node_start: STR
653 if (curres != NULL) {
654 switch (isitme($1)) {
661 mynode = hadmynode = true;
664 PJDLOG_ABORT("invalid isitme() return value");
671 resource_node_entries:
673 resource_node_entries resource_node_entry
688 remote_statement: REMOTE remote_str
690 PJDLOG_ASSERT(depth == 2);
692 PJDLOG_ASSERT(curres != NULL);
693 if (strlcpy(curres->hr_remoteaddr, $2,
694 sizeof(curres->hr_remoteaddr)) >=
695 sizeof(curres->hr_remoteaddr)) {
696 pjdlog_error("remote argument is too long.");
706 NONE { $$ = strdup("none"); }
711 source_statement: SOURCE STR
713 PJDLOG_ASSERT(depth == 2);
715 PJDLOG_ASSERT(curres != NULL);
716 if (strlcpy(curres->hr_sourceaddr, $2,
717 sizeof(curres->hr_sourceaddr)) >=
718 sizeof(curres->hr_sourceaddr)) {
719 pjdlog_error("source argument is too long.");
731 isitme(const char *name)
733 char buf[MAXHOSTNAMELEN];
734 unsigned long hostid;
739 * First check if the given name matches our full hostname.
741 if (gethostname(buf, sizeof(buf)) < 0) {
742 pjdlog_errno(LOG_ERR, "gethostname() failed");
745 if (strcmp(buf, name) == 0)
749 * Check if it matches first part of the host name.
751 pos = strchr(buf, '.');
752 if (pos != NULL && (size_t)(pos - buf) == strlen(name) &&
753 strncmp(buf, name, pos - buf) == 0) {
758 * Check if it matches host UUID.
760 bufsize = sizeof(buf);
761 if (sysctlbyname("kern.hostuuid", buf, &bufsize, NULL, 0) < 0) {
762 pjdlog_errno(LOG_ERR, "sysctlbyname(kern.hostuuid) failed");
765 if (strcasecmp(buf, name) == 0)
769 * Check if it matches hostid.
771 bufsize = sizeof(hostid);
772 if (sysctlbyname("kern.hostid", &hostid, &bufsize, NULL, 0) < 0) {
773 pjdlog_errno(LOG_ERR, "sysctlbyname(kern.hostid) failed");
776 (void)snprintf(buf, sizeof(buf), "hostid%lu", hostid);
777 if (strcmp(buf, name) == 0)
781 * Looks like this isn't about us.
787 family_supported(int family)
791 sock = socket(family, SOCK_STREAM, 0);
792 if (sock == -1 && errno == EPROTONOSUPPORT)
800 node_names(char **namesp)
802 static char names[MAXHOSTNAMELEN * 3];
803 char buf[MAXHOSTNAMELEN];
804 unsigned long hostid;
808 if (gethostname(buf, sizeof(buf)) < 0) {
809 pjdlog_errno(LOG_ERR, "gethostname() failed");
813 /* First component of the host name. */
814 pos = strchr(buf, '.');
815 if (pos != NULL && pos != buf) {
816 (void)strlcpy(names, buf, MIN((size_t)(pos - buf + 1),
818 (void)strlcat(names, ", ", sizeof(names));
821 /* Full host name. */
822 (void)strlcat(names, buf, sizeof(names));
823 (void)strlcat(names, ", ", sizeof(names));
826 bufsize = sizeof(buf);
827 if (sysctlbyname("kern.hostuuid", buf, &bufsize, NULL, 0) < 0) {
828 pjdlog_errno(LOG_ERR, "sysctlbyname(kern.hostuuid) failed");
831 (void)strlcat(names, buf, sizeof(names));
832 (void)strlcat(names, ", ", sizeof(names));
835 bufsize = sizeof(hostid);
836 if (sysctlbyname("kern.hostid", &hostid, &bufsize, NULL, 0) < 0) {
837 pjdlog_errno(LOG_ERR, "sysctlbyname(kern.hostid) failed");
840 (void)snprintf(buf, sizeof(buf), "hostid%lu", hostid);
841 (void)strlcat(names, buf, sizeof(names));
849 yyerror(const char *str)
852 pjdlog_error("Unable to parse configuration file at line %d near '%s': %s",
853 lineno, yytext, str);
856 struct hastd_config *
857 yy_config_parse(const char *config, bool exitonerror)
866 depth0_timeout = HAST_TIMEOUT;
867 depth0_replication = HAST_REPLICATION_MEMSYNC;
868 depth0_checksum = HAST_CHECKSUM_NONE;
869 depth0_compression = HAST_COMPRESSION_HOLE;
870 strlcpy(depth0_control, HAST_CONTROL, sizeof(depth0_control));
871 strlcpy(depth0_pidfile, HASTD_PIDFILE, sizeof(depth0_pidfile));
872 TAILQ_INIT(&depth0_listen);
873 strlcpy(depth0_listen_tcp4, HASTD_LISTEN_TCP4,
874 sizeof(depth0_listen_tcp4));
875 strlcpy(depth0_listen_tcp6, HASTD_LISTEN_TCP6,
876 sizeof(depth0_listen_tcp6));
877 depth0_exec[0] = '\0';
878 depth0_metaflush = 1;
880 lconfig = calloc(1, sizeof(*lconfig));
881 if (lconfig == NULL) {
882 pjdlog_error("Unable to allocate memory for configuration.");
888 TAILQ_INIT(&lconfig->hc_listen);
889 TAILQ_INIT(&lconfig->hc_resources);
891 yyin = fopen(config, "r");
893 pjdlog_errno(LOG_ERR, "Unable to open configuration file %s",
895 yy_config_free(lconfig);
904 yy_config_free(lconfig);
911 * Let's see if everything is set up.
913 if (lconfig->hc_controladdr[0] == '\0') {
914 strlcpy(lconfig->hc_controladdr, depth0_control,
915 sizeof(lconfig->hc_controladdr));
917 if (lconfig->hc_pidfile[0] == '\0') {
918 strlcpy(lconfig->hc_pidfile, depth0_pidfile,
919 sizeof(lconfig->hc_pidfile));
921 if (!TAILQ_EMPTY(&depth0_listen))
922 TAILQ_CONCAT(&lconfig->hc_listen, &depth0_listen, hl_next);
923 if (TAILQ_EMPTY(&lconfig->hc_listen)) {
924 struct hastd_listen *lst;
926 if (family_supported(AF_INET)) {
927 lst = calloc(1, sizeof(*lst));
929 pjdlog_error("Unable to allocate memory for listen address.");
930 yy_config_free(lconfig);
935 (void)strlcpy(lst->hl_addr, depth0_listen_tcp4,
936 sizeof(lst->hl_addr));
937 TAILQ_INSERT_TAIL(&lconfig->hc_listen, lst, hl_next);
940 "No IPv4 support in the kernel, not listening on IPv4 address.");
942 if (family_supported(AF_INET6)) {
943 lst = calloc(1, sizeof(*lst));
945 pjdlog_error("Unable to allocate memory for listen address.");
946 yy_config_free(lconfig);
951 (void)strlcpy(lst->hl_addr, depth0_listen_tcp6,
952 sizeof(lst->hl_addr));
953 TAILQ_INSERT_TAIL(&lconfig->hc_listen, lst, hl_next);
956 "No IPv6 support in the kernel, not listening on IPv6 address.");
958 if (TAILQ_EMPTY(&lconfig->hc_listen)) {
959 pjdlog_error("No address to listen on.");
960 yy_config_free(lconfig);
966 TAILQ_FOREACH(curres, &lconfig->hc_resources, hr_next) {
967 PJDLOG_ASSERT(curres->hr_provname[0] != '\0');
968 PJDLOG_ASSERT(curres->hr_localpath[0] != '\0');
969 PJDLOG_ASSERT(curres->hr_remoteaddr[0] != '\0');
971 if (curres->hr_replication == -1) {
973 * Replication is not set at resource-level.
974 * Use global or default setting.
976 curres->hr_replication = depth0_replication;
977 curres->hr_original_replication = depth0_replication;
979 if (curres->hr_checksum == -1) {
981 * Checksum is not set at resource-level.
982 * Use global or default setting.
984 curres->hr_checksum = depth0_checksum;
986 if (curres->hr_compression == -1) {
988 * Compression is not set at resource-level.
989 * Use global or default setting.
991 curres->hr_compression = depth0_compression;
993 if (curres->hr_timeout == -1) {
995 * Timeout is not set at resource-level.
996 * Use global or default setting.
998 curres->hr_timeout = depth0_timeout;
1000 if (curres->hr_exec[0] == '\0') {
1002 * Exec is not set at resource-level.
1003 * Use global or default setting.
1005 strlcpy(curres->hr_exec, depth0_exec,
1006 sizeof(curres->hr_exec));
1008 if (curres->hr_metaflush == -1) {
1010 * Metaflush is not set at resource-level.
1011 * Use global or default setting.
1013 curres->hr_metaflush = depth0_metaflush;
1021 yy_config_free(struct hastd_config *config)
1023 struct hastd_listen *lst;
1024 struct hast_resource *res;
1026 while ((lst = TAILQ_FIRST(&depth0_listen)) != NULL) {
1027 TAILQ_REMOVE(&depth0_listen, lst, hl_next);
1030 while ((lst = TAILQ_FIRST(&config->hc_listen)) != NULL) {
1031 TAILQ_REMOVE(&config->hc_listen, lst, hl_next);
1034 while ((res = TAILQ_FIRST(&config->hc_resources)) != NULL) {
1035 TAILQ_REMOVE(&config->hc_resources, res, hr_next);