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/socket.h>
37 #include <sys/sysctl.h>
39 #include <arpa/inet.h>
58 static struct hastd_config *lconfig;
59 static struct hast_resource *curres;
60 static bool mynode, hadmynode;
62 static char depth0_control[HAST_ADDRSIZE];
63 static char depth0_pidfile[PATH_MAX];
64 static char depth0_listen_tcp4[HAST_ADDRSIZE];
65 static char depth0_listen_tcp6[HAST_ADDRSIZE];
66 static TAILQ_HEAD(, hastd_listen) depth0_listen;
67 static int depth0_replication;
68 static int depth0_checksum;
69 static int depth0_compression;
70 static int depth0_timeout;
71 static char depth0_exec[PATH_MAX];
72 static int depth0_metaflush;
74 static char depth1_provname[PATH_MAX];
75 static char depth1_localpath[PATH_MAX];
76 static int depth1_metaflush;
78 extern void yyrestart(FILE *);
80 static int isitme(const char *name);
81 static bool family_supported(int family);
82 static int node_names(char **namesp);
85 %token CONTROL PIDFILE LISTEN REPLICATION CHECKSUM COMPRESSION METAFLUSH
86 %token TIMEOUT EXEC RESOURCE NAME LOCAL REMOTE SOURCE ON OFF
87 %token FULLSYNC MEMSYNC ASYNC NONE CRC32 SHA256 HOLE LZF
90 %type <str> remote_str
91 %type <num> replication_type
92 %type <num> checksum_type
93 %type <num> compression_type
119 replication_statement
123 compression_statement
136 control_statement: CONTROL STR
140 if (strlcpy(depth0_control, $2,
141 sizeof(depth0_control)) >=
142 sizeof(depth0_control)) {
143 pjdlog_error("control argument is too long.");
151 if (strlcpy(lconfig->hc_controladdr, $2,
152 sizeof(lconfig->hc_controladdr)) >=
153 sizeof(lconfig->hc_controladdr)) {
154 pjdlog_error("control argument is too long.");
160 PJDLOG_ABORT("control at wrong depth level");
166 pidfile_statement: PIDFILE STR
170 if (strlcpy(depth0_pidfile, $2,
171 sizeof(depth0_pidfile)) >=
172 sizeof(depth0_pidfile)) {
173 pjdlog_error("pidfile argument is too long.");
181 if (strlcpy(lconfig->hc_pidfile, $2,
182 sizeof(lconfig->hc_pidfile)) >=
183 sizeof(lconfig->hc_pidfile)) {
184 pjdlog_error("pidfile argument is too long.");
190 PJDLOG_ABORT("pidfile at wrong depth level");
196 listen_statement: LISTEN STR
198 struct hastd_listen *lst;
200 lst = calloc(1, sizeof(*lst));
202 pjdlog_error("Unable to allocate memory for listen address.");
206 if (strlcpy(lst->hl_addr, $2, sizeof(lst->hl_addr)) >=
207 sizeof(lst->hl_addr)) {
208 pjdlog_error("listen argument is too long.");
215 TAILQ_INSERT_TAIL(&depth0_listen, lst, hl_next);
219 TAILQ_INSERT_TAIL(&depth0_listen, lst, hl_next);
224 PJDLOG_ABORT("listen at wrong depth level");
230 replication_statement: REPLICATION replication_type
234 depth0_replication = $2;
237 PJDLOG_ASSERT(curres != NULL);
238 curres->hr_replication = $2;
241 PJDLOG_ABORT("replication at wrong depth level");
247 FULLSYNC { $$ = HAST_REPLICATION_FULLSYNC; }
249 MEMSYNC { $$ = HAST_REPLICATION_MEMSYNC; }
251 ASYNC { $$ = HAST_REPLICATION_ASYNC; }
254 checksum_statement: CHECKSUM checksum_type
258 depth0_checksum = $2;
261 PJDLOG_ASSERT(curres != NULL);
262 curres->hr_checksum = $2;
265 PJDLOG_ABORT("checksum at wrong depth level");
271 NONE { $$ = HAST_CHECKSUM_NONE; }
273 CRC32 { $$ = HAST_CHECKSUM_CRC32; }
275 SHA256 { $$ = HAST_CHECKSUM_SHA256; }
278 compression_statement: COMPRESSION compression_type
282 depth0_compression = $2;
285 PJDLOG_ASSERT(curres != NULL);
286 curres->hr_compression = $2;
289 PJDLOG_ABORT("compression at wrong depth level");
295 NONE { $$ = HAST_COMPRESSION_NONE; }
297 HOLE { $$ = HAST_COMPRESSION_HOLE; }
299 LZF { $$ = HAST_COMPRESSION_LZF; }
302 timeout_statement: TIMEOUT NUM
305 pjdlog_error("Negative or zero timeout.");
313 PJDLOG_ASSERT(curres != NULL);
314 curres->hr_timeout = $2;
317 PJDLOG_ABORT("timeout at wrong depth level");
322 exec_statement: EXEC STR
326 if (strlcpy(depth0_exec, $2, sizeof(depth0_exec)) >=
327 sizeof(depth0_exec)) {
328 pjdlog_error("Exec path is too long.");
334 PJDLOG_ASSERT(curres != NULL);
335 if (strlcpy(curres->hr_exec, $2,
336 sizeof(curres->hr_exec)) >=
337 sizeof(curres->hr_exec)) {
338 pjdlog_error("Exec path is too long.");
344 PJDLOG_ABORT("exec at wrong depth level");
350 metaflush_statement: METAFLUSH boolean
354 depth0_metaflush = $2;
357 PJDLOG_ASSERT(curres != NULL);
358 depth1_metaflush = $2;
363 PJDLOG_ASSERT(curres != NULL);
364 curres->hr_metaflush = $2;
367 PJDLOG_ABORT("metaflush at wrong depth level");
378 node_statement: ON node_start OB node_entries CB
386 switch (isitme($1)) {
396 PJDLOG_ABORT("invalid isitme() return value");
404 node_entries node_entry
415 resource_statement: RESOURCE resource_start OB resource_entries CB
417 if (curres != NULL) {
419 * There must be section for this node, at least with
420 * remote address configuration.
425 if (node_names(&names) != 0)
427 pjdlog_error("No resource %s configuration for this node (acceptable node names: %s).",
428 curres->hr_name, names);
433 * Let's see if there are some resource-level settings
434 * that we can use for node-level settings.
436 if (curres->hr_provname[0] == '\0' &&
437 depth1_provname[0] != '\0') {
439 * Provider name is not set at node-level,
440 * but is set at resource-level, use it.
442 strlcpy(curres->hr_provname, depth1_provname,
443 sizeof(curres->hr_provname));
445 if (curres->hr_localpath[0] == '\0' &&
446 depth1_localpath[0] != '\0') {
448 * Path to local provider is not set at
449 * node-level, but is set at resource-level,
452 strlcpy(curres->hr_localpath, depth1_localpath,
453 sizeof(curres->hr_localpath));
455 if (curres->hr_metaflush == -1 && depth1_metaflush != -1) {
457 * Metaflush is not set at node-level,
458 * but is set at resource-level, use it.
460 curres->hr_metaflush = depth1_metaflush;
464 * If provider name is not given, use resource name
467 if (curres->hr_provname[0] == '\0') {
468 strlcpy(curres->hr_provname, curres->hr_name,
469 sizeof(curres->hr_provname));
473 * Remote address has to be configured at this point.
475 if (curres->hr_remoteaddr[0] == '\0') {
476 pjdlog_error("Remote address not configured for resource %s.",
481 * Path to local provider has to be configured at this
484 if (curres->hr_localpath[0] == '\0') {
485 pjdlog_error("Path to local component not configured for resource %s.",
490 /* Put it onto resource list. */
491 TAILQ_INSERT_TAIL(&lconfig->hc_resources, curres, hr_next);
499 /* Check if there is no duplicate entry. */
500 TAILQ_FOREACH(curres, &lconfig->hc_resources, hr_next) {
501 if (strcmp(curres->hr_name, $1) == 0) {
502 pjdlog_error("Resource %s configured more than once.",
510 * Clear those, so we can tell if they were set at
511 * resource-level or not.
513 depth1_provname[0] = '\0';
514 depth1_localpath[0] = '\0';
515 depth1_metaflush = -1;
518 curres = calloc(1, sizeof(*curres));
519 if (curres == NULL) {
520 pjdlog_error("Unable to allocate memory for resource.");
524 if (strlcpy(curres->hr_name, $1,
525 sizeof(curres->hr_name)) >=
526 sizeof(curres->hr_name)) {
527 pjdlog_error("Resource name is too long.");
533 curres->hr_role = HAST_ROLE_INIT;
534 curres->hr_previous_role = HAST_ROLE_INIT;
535 curres->hr_replication = -1;
536 curres->hr_checksum = -1;
537 curres->hr_compression = -1;
538 curres->hr_timeout = -1;
539 curres->hr_exec[0] = '\0';
540 curres->hr_provname[0] = '\0';
541 curres->hr_localpath[0] = '\0';
542 curres->hr_localfd = -1;
543 curres->hr_localflush = true;
544 curres->hr_metaflush = -1;
545 curres->hr_remoteaddr[0] = '\0';
546 curres->hr_sourceaddr[0] = '\0';
547 curres->hr_ggateunit = -1;
553 resource_entries resource_entry
557 replication_statement
561 compression_statement
573 resource_node_statement
576 name_statement: NAME STR
580 if (strlcpy(depth1_provname, $2,
581 sizeof(depth1_provname)) >=
582 sizeof(depth1_provname)) {
583 pjdlog_error("name argument is too long.");
591 PJDLOG_ASSERT(curres != NULL);
592 if (strlcpy(curres->hr_provname, $2,
593 sizeof(curres->hr_provname)) >=
594 sizeof(curres->hr_provname)) {
595 pjdlog_error("name argument is too long.");
601 PJDLOG_ABORT("name at wrong depth level");
607 local_statement: LOCAL STR
611 if (strlcpy(depth1_localpath, $2,
612 sizeof(depth1_localpath)) >=
613 sizeof(depth1_localpath)) {
614 pjdlog_error("local argument is too long.");
622 PJDLOG_ASSERT(curres != NULL);
623 if (strlcpy(curres->hr_localpath, $2,
624 sizeof(curres->hr_localpath)) >=
625 sizeof(curres->hr_localpath)) {
626 pjdlog_error("local argument is too long.");
632 PJDLOG_ABORT("local at wrong depth level");
638 resource_node_statement:ON resource_node_start OB resource_node_entries CB
644 resource_node_start: STR
646 if (curres != NULL) {
647 switch (isitme($1)) {
654 mynode = hadmynode = true;
657 PJDLOG_ABORT("invalid isitme() return value");
664 resource_node_entries:
666 resource_node_entries resource_node_entry
681 remote_statement: REMOTE remote_str
683 PJDLOG_ASSERT(depth == 2);
685 PJDLOG_ASSERT(curres != NULL);
686 if (strlcpy(curres->hr_remoteaddr, $2,
687 sizeof(curres->hr_remoteaddr)) >=
688 sizeof(curres->hr_remoteaddr)) {
689 pjdlog_error("remote argument is too long.");
699 NONE { $$ = strdup("none"); }
704 source_statement: SOURCE STR
706 PJDLOG_ASSERT(depth == 2);
708 PJDLOG_ASSERT(curres != NULL);
709 if (strlcpy(curres->hr_sourceaddr, $2,
710 sizeof(curres->hr_sourceaddr)) >=
711 sizeof(curres->hr_sourceaddr)) {
712 pjdlog_error("source argument is too long.");
724 isitme(const char *name)
726 char buf[MAXHOSTNAMELEN];
731 * First check if the given name matches our full hostname.
733 if (gethostname(buf, sizeof(buf)) < 0) {
734 pjdlog_errno(LOG_ERR, "gethostname() failed");
737 if (strcmp(buf, name) == 0)
741 * Now check if it matches first part of the host name.
743 pos = strchr(buf, '.');
744 if (pos != NULL && (size_t)(pos - buf) == strlen(name) &&
745 strncmp(buf, name, pos - buf) == 0) {
750 * At the end check if name is equal to our host's UUID.
752 bufsize = sizeof(buf);
753 if (sysctlbyname("kern.hostuuid", buf, &bufsize, NULL, 0) < 0) {
754 pjdlog_errno(LOG_ERR, "sysctlbyname(kern.hostuuid) failed");
757 if (strcasecmp(buf, name) == 0)
761 * Looks like this isn't about us.
767 family_supported(int family)
771 sock = socket(family, SOCK_STREAM, 0);
772 if (sock == -1 && errno == EAFNOSUPPORT)
780 node_names(char **namesp)
782 static char names[MAXHOSTNAMELEN * 3];
783 char buf[MAXHOSTNAMELEN];
787 if (gethostname(buf, sizeof(buf)) < 0) {
788 pjdlog_errno(LOG_ERR, "gethostname() failed");
792 /* First component of the host name. */
793 pos = strchr(buf, '.');
794 if (pos != NULL && pos != buf) {
795 (void)strlcpy(names, buf, MIN((size_t)(pos - buf + 1),
797 (void)strlcat(names, ", ", sizeof(names));
800 /* Full host name. */
801 (void)strlcat(names, buf, sizeof(names));
802 (void)strlcat(names, ", ", sizeof(names));
805 bufsize = sizeof(buf);
806 if (sysctlbyname("kern.hostuuid", buf, &bufsize, NULL, 0) < 0) {
807 pjdlog_errno(LOG_ERR, "sysctlbyname(kern.hostuuid) failed");
810 (void)strlcat(names, buf, sizeof(names));
818 yyerror(const char *str)
821 pjdlog_error("Unable to parse configuration file at line %d near '%s': %s",
822 lineno, yytext, str);
825 struct hastd_config *
826 yy_config_parse(const char *config, bool exitonerror)
835 depth0_timeout = HAST_TIMEOUT;
836 depth0_replication = HAST_REPLICATION_FULLSYNC;
837 depth0_checksum = HAST_CHECKSUM_NONE;
838 depth0_compression = HAST_COMPRESSION_HOLE;
839 strlcpy(depth0_control, HAST_CONTROL, sizeof(depth0_control));
840 strlcpy(depth0_pidfile, HASTD_PIDFILE, sizeof(depth0_pidfile));
841 TAILQ_INIT(&depth0_listen);
842 strlcpy(depth0_listen_tcp4, HASTD_LISTEN_TCP4,
843 sizeof(depth0_listen_tcp4));
844 strlcpy(depth0_listen_tcp6, HASTD_LISTEN_TCP6,
845 sizeof(depth0_listen_tcp6));
846 depth0_exec[0] = '\0';
847 depth0_metaflush = 1;
849 lconfig = calloc(1, sizeof(*lconfig));
850 if (lconfig == NULL) {
851 pjdlog_error("Unable to allocate memory for configuration.");
857 TAILQ_INIT(&lconfig->hc_listen);
858 TAILQ_INIT(&lconfig->hc_resources);
860 yyin = fopen(config, "r");
862 pjdlog_errno(LOG_ERR, "Unable to open configuration file %s",
864 yy_config_free(lconfig);
873 yy_config_free(lconfig);
880 * Let's see if everything is set up.
882 if (lconfig->hc_controladdr[0] == '\0') {
883 strlcpy(lconfig->hc_controladdr, depth0_control,
884 sizeof(lconfig->hc_controladdr));
886 if (lconfig->hc_pidfile[0] == '\0') {
887 strlcpy(lconfig->hc_pidfile, depth0_pidfile,
888 sizeof(lconfig->hc_pidfile));
890 if (!TAILQ_EMPTY(&depth0_listen))
891 TAILQ_CONCAT(&lconfig->hc_listen, &depth0_listen, hl_next);
892 if (TAILQ_EMPTY(&lconfig->hc_listen)) {
893 struct hastd_listen *lst;
895 if (family_supported(AF_INET)) {
896 lst = calloc(1, sizeof(*lst));
898 pjdlog_error("Unable to allocate memory for listen address.");
899 yy_config_free(lconfig);
904 (void)strlcpy(lst->hl_addr, depth0_listen_tcp4,
905 sizeof(lst->hl_addr));
906 TAILQ_INSERT_TAIL(&lconfig->hc_listen, lst, hl_next);
909 "No IPv4 support in the kernel, not listening on IPv4 address.");
911 if (family_supported(AF_INET6)) {
912 lst = calloc(1, sizeof(*lst));
914 pjdlog_error("Unable to allocate memory for listen address.");
915 yy_config_free(lconfig);
920 (void)strlcpy(lst->hl_addr, depth0_listen_tcp6,
921 sizeof(lst->hl_addr));
922 TAILQ_INSERT_TAIL(&lconfig->hc_listen, lst, hl_next);
925 "No IPv6 support in the kernel, not listening on IPv6 address.");
927 if (TAILQ_EMPTY(&lconfig->hc_listen)) {
928 pjdlog_error("No address to listen on.");
929 yy_config_free(lconfig);
935 TAILQ_FOREACH(curres, &lconfig->hc_resources, hr_next) {
936 PJDLOG_ASSERT(curres->hr_provname[0] != '\0');
937 PJDLOG_ASSERT(curres->hr_localpath[0] != '\0');
938 PJDLOG_ASSERT(curres->hr_remoteaddr[0] != '\0');
940 if (curres->hr_replication == -1) {
942 * Replication is not set at resource-level.
943 * Use global or default setting.
945 curres->hr_replication = depth0_replication;
947 if (curres->hr_replication == HAST_REPLICATION_MEMSYNC) {
948 pjdlog_warning("Replication mode \"%s\" is not implemented, falling back to \"%s\".",
949 "memsync", "fullsync");
950 curres->hr_replication = HAST_REPLICATION_FULLSYNC;
952 if (curres->hr_checksum == -1) {
954 * Checksum is not set at resource-level.
955 * Use global or default setting.
957 curres->hr_checksum = depth0_checksum;
959 if (curres->hr_compression == -1) {
961 * Compression is not set at resource-level.
962 * Use global or default setting.
964 curres->hr_compression = depth0_compression;
966 if (curres->hr_timeout == -1) {
968 * Timeout is not set at resource-level.
969 * Use global or default setting.
971 curres->hr_timeout = depth0_timeout;
973 if (curres->hr_exec[0] == '\0') {
975 * Exec is not set at resource-level.
976 * Use global or default setting.
978 strlcpy(curres->hr_exec, depth0_exec,
979 sizeof(curres->hr_exec));
981 if (curres->hr_metaflush == -1) {
983 * Metaflush is not set at resource-level.
984 * Use global or default setting.
986 curres->hr_metaflush = depth0_metaflush;
994 yy_config_free(struct hastd_config *config)
996 struct hastd_listen *lst;
997 struct hast_resource *res;
999 while ((lst = TAILQ_FIRST(&depth0_listen)) != NULL) {
1000 TAILQ_REMOVE(&depth0_listen, lst, hl_next);
1003 while ((lst = TAILQ_FIRST(&config->hc_listen)) != NULL) {
1004 TAILQ_REMOVE(&config->hc_listen, lst, hl_next);
1007 while ((res = TAILQ_FIRST(&config->hc_resources)) != NULL) {
1008 TAILQ_REMOVE(&config->hc_resources, res, hr_next);