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 yyerror(const char *);
79 extern int yylex(void);
80 extern void yyrestart(FILE *);
82 static int isitme(const char *name);
83 static bool family_supported(int family);
84 static int node_names(char **namesp);
87 %token CONTROL PIDFILE LISTEN REPLICATION CHECKSUM COMPRESSION METAFLUSH
88 %token TIMEOUT EXEC RESOURCE NAME LOCAL REMOTE SOURCE ON OFF
89 %token FULLSYNC MEMSYNC ASYNC NONE CRC32 SHA256 HOLE LZF
92 %type <str> remote_str
93 %type <num> replication_type
94 %type <num> checksum_type
95 %type <num> compression_type
121 replication_statement
125 compression_statement
138 control_statement: CONTROL STR
142 if (strlcpy(depth0_control, $2,
143 sizeof(depth0_control)) >=
144 sizeof(depth0_control)) {
145 pjdlog_error("control argument is too long.");
153 if (strlcpy(lconfig->hc_controladdr, $2,
154 sizeof(lconfig->hc_controladdr)) >=
155 sizeof(lconfig->hc_controladdr)) {
156 pjdlog_error("control argument is too long.");
162 PJDLOG_ABORT("control at wrong depth level");
168 pidfile_statement: PIDFILE STR
172 if (strlcpy(depth0_pidfile, $2,
173 sizeof(depth0_pidfile)) >=
174 sizeof(depth0_pidfile)) {
175 pjdlog_error("pidfile argument is too long.");
183 if (strlcpy(lconfig->hc_pidfile, $2,
184 sizeof(lconfig->hc_pidfile)) >=
185 sizeof(lconfig->hc_pidfile)) {
186 pjdlog_error("pidfile argument is too long.");
192 PJDLOG_ABORT("pidfile at wrong depth level");
198 listen_statement: LISTEN STR
200 struct hastd_listen *lst;
202 lst = calloc(1, sizeof(*lst));
204 pjdlog_error("Unable to allocate memory for listen address.");
208 if (strlcpy(lst->hl_addr, $2, sizeof(lst->hl_addr)) >=
209 sizeof(lst->hl_addr)) {
210 pjdlog_error("listen argument is too long.");
217 TAILQ_INSERT_TAIL(&depth0_listen, lst, hl_next);
221 TAILQ_INSERT_TAIL(&depth0_listen, lst, hl_next);
226 PJDLOG_ABORT("listen at wrong depth level");
232 replication_statement: REPLICATION replication_type
236 depth0_replication = $2;
239 PJDLOG_ASSERT(curres != NULL);
240 curres->hr_replication = $2;
241 curres->hr_original_replication = $2;
244 PJDLOG_ABORT("replication at wrong depth level");
250 FULLSYNC { $$ = HAST_REPLICATION_FULLSYNC; }
252 MEMSYNC { $$ = HAST_REPLICATION_MEMSYNC; }
254 ASYNC { $$ = HAST_REPLICATION_ASYNC; }
257 checksum_statement: CHECKSUM checksum_type
261 depth0_checksum = $2;
264 PJDLOG_ASSERT(curres != NULL);
265 curres->hr_checksum = $2;
268 PJDLOG_ABORT("checksum at wrong depth level");
274 NONE { $$ = HAST_CHECKSUM_NONE; }
276 CRC32 { $$ = HAST_CHECKSUM_CRC32; }
278 SHA256 { $$ = HAST_CHECKSUM_SHA256; }
281 compression_statement: COMPRESSION compression_type
285 depth0_compression = $2;
288 PJDLOG_ASSERT(curres != NULL);
289 curres->hr_compression = $2;
292 PJDLOG_ABORT("compression at wrong depth level");
298 NONE { $$ = HAST_COMPRESSION_NONE; }
300 HOLE { $$ = HAST_COMPRESSION_HOLE; }
302 LZF { $$ = HAST_COMPRESSION_LZF; }
305 timeout_statement: TIMEOUT NUM
308 pjdlog_error("Negative or zero timeout.");
316 PJDLOG_ASSERT(curres != NULL);
317 curres->hr_timeout = $2;
320 PJDLOG_ABORT("timeout at wrong depth level");
325 exec_statement: EXEC STR
329 if (strlcpy(depth0_exec, $2, sizeof(depth0_exec)) >=
330 sizeof(depth0_exec)) {
331 pjdlog_error("Exec path is too long.");
337 PJDLOG_ASSERT(curres != NULL);
338 if (strlcpy(curres->hr_exec, $2,
339 sizeof(curres->hr_exec)) >=
340 sizeof(curres->hr_exec)) {
341 pjdlog_error("Exec path is too long.");
347 PJDLOG_ABORT("exec at wrong depth level");
353 metaflush_statement: METAFLUSH boolean
357 depth0_metaflush = $2;
360 PJDLOG_ASSERT(curres != NULL);
361 depth1_metaflush = $2;
366 PJDLOG_ASSERT(curres != NULL);
367 curres->hr_metaflush = $2;
370 PJDLOG_ABORT("metaflush at wrong depth level");
381 node_statement: ON node_start OB node_entries CB
389 switch (isitme($1)) {
399 PJDLOG_ABORT("invalid isitme() return value");
407 node_entries node_entry
418 resource_statement: RESOURCE resource_start OB resource_entries CB
420 if (curres != NULL) {
422 * There must be section for this node, at least with
423 * remote address configuration.
428 if (node_names(&names) != 0)
430 pjdlog_error("No resource %s configuration for this node (acceptable node names: %s).",
431 curres->hr_name, names);
436 * Let's see if there are some resource-level settings
437 * that we can use for node-level settings.
439 if (curres->hr_provname[0] == '\0' &&
440 depth1_provname[0] != '\0') {
442 * Provider name is not set at node-level,
443 * but is set at resource-level, use it.
445 strlcpy(curres->hr_provname, depth1_provname,
446 sizeof(curres->hr_provname));
448 if (curres->hr_localpath[0] == '\0' &&
449 depth1_localpath[0] != '\0') {
451 * Path to local provider is not set at
452 * node-level, but is set at resource-level,
455 strlcpy(curres->hr_localpath, depth1_localpath,
456 sizeof(curres->hr_localpath));
458 if (curres->hr_metaflush == -1 && depth1_metaflush != -1) {
460 * Metaflush is not set at node-level,
461 * but is set at resource-level, use it.
463 curres->hr_metaflush = depth1_metaflush;
467 * If provider name is not given, use resource name
470 if (curres->hr_provname[0] == '\0') {
471 strlcpy(curres->hr_provname, curres->hr_name,
472 sizeof(curres->hr_provname));
476 * Remote address has to be configured at this point.
478 if (curres->hr_remoteaddr[0] == '\0') {
479 pjdlog_error("Remote address not configured for resource %s.",
484 * Path to local provider has to be configured at this
487 if (curres->hr_localpath[0] == '\0') {
488 pjdlog_error("Path to local component not configured for resource %s.",
493 /* Put it onto resource list. */
494 TAILQ_INSERT_TAIL(&lconfig->hc_resources, curres, hr_next);
502 /* Check if there is no duplicate entry. */
503 TAILQ_FOREACH(curres, &lconfig->hc_resources, hr_next) {
504 if (strcmp(curres->hr_name, $1) == 0) {
505 pjdlog_error("Resource %s configured more than once.",
513 * Clear those, so we can tell if they were set at
514 * resource-level or not.
516 depth1_provname[0] = '\0';
517 depth1_localpath[0] = '\0';
518 depth1_metaflush = -1;
521 curres = calloc(1, sizeof(*curres));
522 if (curres == NULL) {
523 pjdlog_error("Unable to allocate memory for resource.");
527 if (strlcpy(curres->hr_name, $1,
528 sizeof(curres->hr_name)) >=
529 sizeof(curres->hr_name)) {
530 pjdlog_error("Resource name is too long.");
536 curres->hr_role = HAST_ROLE_INIT;
537 curres->hr_previous_role = HAST_ROLE_INIT;
538 curres->hr_replication = -1;
539 curres->hr_original_replication = -1;
540 curres->hr_checksum = -1;
541 curres->hr_compression = -1;
542 curres->hr_version = 1;
543 curres->hr_timeout = -1;
544 curres->hr_exec[0] = '\0';
545 curres->hr_provname[0] = '\0';
546 curres->hr_localpath[0] = '\0';
547 curres->hr_localfd = -1;
548 curres->hr_localflush = true;
549 curres->hr_metaflush = -1;
550 curres->hr_remoteaddr[0] = '\0';
551 curres->hr_sourceaddr[0] = '\0';
552 curres->hr_ggateunit = -1;
558 resource_entries resource_entry
562 replication_statement
566 compression_statement
578 resource_node_statement
581 name_statement: NAME STR
585 if (strlcpy(depth1_provname, $2,
586 sizeof(depth1_provname)) >=
587 sizeof(depth1_provname)) {
588 pjdlog_error("name argument is too long.");
596 PJDLOG_ASSERT(curres != NULL);
597 if (strlcpy(curres->hr_provname, $2,
598 sizeof(curres->hr_provname)) >=
599 sizeof(curres->hr_provname)) {
600 pjdlog_error("name argument is too long.");
606 PJDLOG_ABORT("name at wrong depth level");
612 local_statement: LOCAL STR
616 if (strlcpy(depth1_localpath, $2,
617 sizeof(depth1_localpath)) >=
618 sizeof(depth1_localpath)) {
619 pjdlog_error("local argument is too long.");
627 PJDLOG_ASSERT(curres != NULL);
628 if (strlcpy(curres->hr_localpath, $2,
629 sizeof(curres->hr_localpath)) >=
630 sizeof(curres->hr_localpath)) {
631 pjdlog_error("local argument is too long.");
637 PJDLOG_ABORT("local at wrong depth level");
643 resource_node_statement:ON resource_node_start OB resource_node_entries CB
649 resource_node_start: STR
651 if (curres != NULL) {
652 switch (isitme($1)) {
659 mynode = hadmynode = true;
662 PJDLOG_ABORT("invalid isitme() return value");
669 resource_node_entries:
671 resource_node_entries resource_node_entry
686 remote_statement: REMOTE remote_str
688 PJDLOG_ASSERT(depth == 2);
690 PJDLOG_ASSERT(curres != NULL);
691 if (strlcpy(curres->hr_remoteaddr, $2,
692 sizeof(curres->hr_remoteaddr)) >=
693 sizeof(curres->hr_remoteaddr)) {
694 pjdlog_error("remote argument is too long.");
704 NONE { $$ = strdup("none"); }
709 source_statement: SOURCE STR
711 PJDLOG_ASSERT(depth == 2);
713 PJDLOG_ASSERT(curres != NULL);
714 if (strlcpy(curres->hr_sourceaddr, $2,
715 sizeof(curres->hr_sourceaddr)) >=
716 sizeof(curres->hr_sourceaddr)) {
717 pjdlog_error("source argument is too long.");
729 isitme(const char *name)
731 char buf[MAXHOSTNAMELEN];
732 unsigned long hostid;
737 * First check if the given name matches our full hostname.
739 if (gethostname(buf, sizeof(buf)) < 0) {
740 pjdlog_errno(LOG_ERR, "gethostname() failed");
743 if (strcmp(buf, name) == 0)
747 * Check if it matches first part of the host name.
749 pos = strchr(buf, '.');
750 if (pos != NULL && (size_t)(pos - buf) == strlen(name) &&
751 strncmp(buf, name, pos - buf) == 0) {
756 * Check if it matches host UUID.
758 bufsize = sizeof(buf);
759 if (sysctlbyname("kern.hostuuid", buf, &bufsize, NULL, 0) < 0) {
760 pjdlog_errno(LOG_ERR, "sysctlbyname(kern.hostuuid) failed");
763 if (strcasecmp(buf, name) == 0)
767 * Check if it matches hostid.
769 bufsize = sizeof(hostid);
770 if (sysctlbyname("kern.hostid", &hostid, &bufsize, NULL, 0) < 0) {
771 pjdlog_errno(LOG_ERR, "sysctlbyname(kern.hostid) failed");
774 (void)snprintf(buf, sizeof(buf), "hostid%lu", hostid);
775 if (strcmp(buf, name) == 0)
779 * Looks like this isn't about us.
785 family_supported(int family)
789 sock = socket(family, SOCK_STREAM, 0);
790 if (sock == -1 && errno == EPROTONOSUPPORT)
798 node_names(char **namesp)
800 static char names[MAXHOSTNAMELEN * 3];
801 char buf[MAXHOSTNAMELEN];
802 unsigned long hostid;
806 if (gethostname(buf, sizeof(buf)) < 0) {
807 pjdlog_errno(LOG_ERR, "gethostname() failed");
811 /* First component of the host name. */
812 pos = strchr(buf, '.');
813 if (pos != NULL && pos != buf) {
814 (void)strlcpy(names, buf, MIN((size_t)(pos - buf + 1),
816 (void)strlcat(names, ", ", sizeof(names));
819 /* Full host name. */
820 (void)strlcat(names, buf, sizeof(names));
821 (void)strlcat(names, ", ", sizeof(names));
824 bufsize = sizeof(buf);
825 if (sysctlbyname("kern.hostuuid", buf, &bufsize, NULL, 0) < 0) {
826 pjdlog_errno(LOG_ERR, "sysctlbyname(kern.hostuuid) failed");
829 (void)strlcat(names, buf, sizeof(names));
830 (void)strlcat(names, ", ", sizeof(names));
833 bufsize = sizeof(hostid);
834 if (sysctlbyname("kern.hostid", &hostid, &bufsize, NULL, 0) < 0) {
835 pjdlog_errno(LOG_ERR, "sysctlbyname(kern.hostid) failed");
838 (void)snprintf(buf, sizeof(buf), "hostid%lu", hostid);
839 (void)strlcat(names, buf, sizeof(names));
847 yyerror(const char *str)
850 pjdlog_error("Unable to parse configuration file at line %d near '%s': %s",
851 lineno, yytext, str);
854 struct hastd_config *
855 yy_config_parse(const char *config, bool exitonerror)
864 depth0_timeout = HAST_TIMEOUT;
865 depth0_replication = HAST_REPLICATION_MEMSYNC;
866 depth0_checksum = HAST_CHECKSUM_NONE;
867 depth0_compression = HAST_COMPRESSION_HOLE;
868 strlcpy(depth0_control, HAST_CONTROL, sizeof(depth0_control));
869 strlcpy(depth0_pidfile, HASTD_PIDFILE, sizeof(depth0_pidfile));
870 TAILQ_INIT(&depth0_listen);
871 strlcpy(depth0_listen_tcp4, HASTD_LISTEN_TCP4,
872 sizeof(depth0_listen_tcp4));
873 strlcpy(depth0_listen_tcp6, HASTD_LISTEN_TCP6,
874 sizeof(depth0_listen_tcp6));
875 depth0_exec[0] = '\0';
876 depth0_metaflush = 1;
878 lconfig = calloc(1, sizeof(*lconfig));
879 if (lconfig == NULL) {
880 pjdlog_error("Unable to allocate memory for configuration.");
886 TAILQ_INIT(&lconfig->hc_listen);
887 TAILQ_INIT(&lconfig->hc_resources);
889 yyin = fopen(config, "r");
891 pjdlog_errno(LOG_ERR, "Unable to open configuration file %s",
893 yy_config_free(lconfig);
902 yy_config_free(lconfig);
909 * Let's see if everything is set up.
911 if (lconfig->hc_controladdr[0] == '\0') {
912 strlcpy(lconfig->hc_controladdr, depth0_control,
913 sizeof(lconfig->hc_controladdr));
915 if (lconfig->hc_pidfile[0] == '\0') {
916 strlcpy(lconfig->hc_pidfile, depth0_pidfile,
917 sizeof(lconfig->hc_pidfile));
919 if (!TAILQ_EMPTY(&depth0_listen))
920 TAILQ_CONCAT(&lconfig->hc_listen, &depth0_listen, hl_next);
921 if (TAILQ_EMPTY(&lconfig->hc_listen)) {
922 struct hastd_listen *lst;
924 if (family_supported(AF_INET)) {
925 lst = calloc(1, sizeof(*lst));
927 pjdlog_error("Unable to allocate memory for listen address.");
928 yy_config_free(lconfig);
933 (void)strlcpy(lst->hl_addr, depth0_listen_tcp4,
934 sizeof(lst->hl_addr));
935 TAILQ_INSERT_TAIL(&lconfig->hc_listen, lst, hl_next);
938 "No IPv4 support in the kernel, not listening on IPv4 address.");
940 if (family_supported(AF_INET6)) {
941 lst = calloc(1, sizeof(*lst));
943 pjdlog_error("Unable to allocate memory for listen address.");
944 yy_config_free(lconfig);
949 (void)strlcpy(lst->hl_addr, depth0_listen_tcp6,
950 sizeof(lst->hl_addr));
951 TAILQ_INSERT_TAIL(&lconfig->hc_listen, lst, hl_next);
954 "No IPv6 support in the kernel, not listening on IPv6 address.");
956 if (TAILQ_EMPTY(&lconfig->hc_listen)) {
957 pjdlog_error("No address to listen on.");
958 yy_config_free(lconfig);
964 TAILQ_FOREACH(curres, &lconfig->hc_resources, hr_next) {
965 PJDLOG_ASSERT(curres->hr_provname[0] != '\0');
966 PJDLOG_ASSERT(curres->hr_localpath[0] != '\0');
967 PJDLOG_ASSERT(curres->hr_remoteaddr[0] != '\0');
969 if (curres->hr_replication == -1) {
971 * Replication is not set at resource-level.
972 * Use global or default setting.
974 curres->hr_replication = depth0_replication;
975 curres->hr_original_replication = depth0_replication;
977 if (curres->hr_checksum == -1) {
979 * Checksum is not set at resource-level.
980 * Use global or default setting.
982 curres->hr_checksum = depth0_checksum;
984 if (curres->hr_compression == -1) {
986 * Compression is not set at resource-level.
987 * Use global or default setting.
989 curres->hr_compression = depth0_compression;
991 if (curres->hr_timeout == -1) {
993 * Timeout is not set at resource-level.
994 * Use global or default setting.
996 curres->hr_timeout = depth0_timeout;
998 if (curres->hr_exec[0] == '\0') {
1000 * Exec is not set at resource-level.
1001 * Use global or default setting.
1003 strlcpy(curres->hr_exec, depth0_exec,
1004 sizeof(curres->hr_exec));
1006 if (curres->hr_metaflush == -1) {
1008 * Metaflush is not set at resource-level.
1009 * Use global or default setting.
1011 curres->hr_metaflush = depth0_metaflush;
1019 yy_config_free(struct hastd_config *config)
1021 struct hastd_listen *lst;
1022 struct hast_resource *res;
1024 while ((lst = TAILQ_FIRST(&depth0_listen)) != NULL) {
1025 TAILQ_REMOVE(&depth0_listen, lst, hl_next);
1028 while ((lst = TAILQ_FIRST(&config->hc_listen)) != NULL) {
1029 TAILQ_REMOVE(&config->hc_listen, lst, hl_next);
1032 while ((res = TAILQ_FIRST(&config->hc_resources)) != NULL) {
1033 TAILQ_REMOVE(&config->hc_resources, res, hr_next);