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 *);
81 isitme(const char *name)
83 char buf[MAXHOSTNAMELEN];
89 * First check if the given name matches our full hostname.
91 if (gethostname(buf, sizeof(buf)) < 0) {
92 pjdlog_errno(LOG_ERR, "gethostname() failed");
95 if (strcmp(buf, name) == 0)
99 * Check if it matches first part of the host name.
101 pos = strchr(buf, '.');
102 if (pos != NULL && (size_t)(pos - buf) == strlen(name) &&
103 strncmp(buf, name, pos - buf) == 0) {
108 * Check if it matches host UUID.
110 bufsize = sizeof(buf);
111 if (sysctlbyname("kern.hostuuid", buf, &bufsize, NULL, 0) < 0) {
112 pjdlog_errno(LOG_ERR, "sysctlbyname(kern.hostuuid) failed");
115 if (strcasecmp(buf, name) == 0)
119 * Check if it matches hostid.
121 bufsize = sizeof(hostid);
122 if (sysctlbyname("kern.hostid", &hostid, &bufsize, NULL, 0) < 0) {
123 pjdlog_errno(LOG_ERR, "sysctlbyname(kern.hostid) failed");
126 (void)snprintf(buf, sizeof(buf), "hostid%lu", hostid);
127 if (strcmp(buf, name) == 0)
131 * Looks like this isn't about us.
137 family_supported(int family)
141 sock = socket(family, SOCK_STREAM, 0);
142 if (sock == -1 && errno == EPROTONOSUPPORT)
150 node_names(char **namesp)
152 static char names[MAXHOSTNAMELEN * 3];
153 char buf[MAXHOSTNAMELEN];
154 unsigned long hostid;
158 if (gethostname(buf, sizeof(buf)) < 0) {
159 pjdlog_errno(LOG_ERR, "gethostname() failed");
163 /* First component of the host name. */
164 pos = strchr(buf, '.');
165 if (pos != NULL && pos != buf) {
166 (void)strlcpy(names, buf, MIN((size_t)(pos - buf + 1),
168 (void)strlcat(names, ", ", sizeof(names));
171 /* Full host name. */
172 (void)strlcat(names, buf, sizeof(names));
173 (void)strlcat(names, ", ", sizeof(names));
176 bufsize = sizeof(buf);
177 if (sysctlbyname("kern.hostuuid", buf, &bufsize, NULL, 0) < 0) {
178 pjdlog_errno(LOG_ERR, "sysctlbyname(kern.hostuuid) failed");
181 (void)strlcat(names, buf, sizeof(names));
182 (void)strlcat(names, ", ", sizeof(names));
185 bufsize = sizeof(hostid);
186 if (sysctlbyname("kern.hostid", &hostid, &bufsize, NULL, 0) < 0) {
187 pjdlog_errno(LOG_ERR, "sysctlbyname(kern.hostid) failed");
190 (void)snprintf(buf, sizeof(buf), "hostid%lu", hostid);
191 (void)strlcat(names, buf, sizeof(names));
199 yyerror(const char *str)
202 pjdlog_error("Unable to parse configuration file at line %d near '%s': %s",
203 lineno, yytext, str);
206 struct hastd_config *
207 yy_config_parse(const char *config, bool exitonerror)
216 depth0_timeout = HAST_TIMEOUT;
217 depth0_replication = HAST_REPLICATION_MEMSYNC;
218 depth0_checksum = HAST_CHECKSUM_NONE;
219 depth0_compression = HAST_COMPRESSION_HOLE;
220 strlcpy(depth0_control, HAST_CONTROL, sizeof(depth0_control));
221 strlcpy(depth0_pidfile, HASTD_PIDFILE, sizeof(depth0_pidfile));
222 TAILQ_INIT(&depth0_listen);
223 strlcpy(depth0_listen_tcp4, HASTD_LISTEN_TCP4,
224 sizeof(depth0_listen_tcp4));
225 strlcpy(depth0_listen_tcp6, HASTD_LISTEN_TCP6,
226 sizeof(depth0_listen_tcp6));
227 depth0_exec[0] = '\0';
228 depth0_metaflush = 1;
230 lconfig = calloc(1, sizeof(*lconfig));
231 if (lconfig == NULL) {
232 pjdlog_error("Unable to allocate memory for configuration.");
238 TAILQ_INIT(&lconfig->hc_listen);
239 TAILQ_INIT(&lconfig->hc_resources);
241 yyin = fopen(config, "r");
243 pjdlog_errno(LOG_ERR, "Unable to open configuration file %s",
245 yy_config_free(lconfig);
254 yy_config_free(lconfig);
261 * Let's see if everything is set up.
263 if (lconfig->hc_controladdr[0] == '\0') {
264 strlcpy(lconfig->hc_controladdr, depth0_control,
265 sizeof(lconfig->hc_controladdr));
267 if (lconfig->hc_pidfile[0] == '\0') {
268 strlcpy(lconfig->hc_pidfile, depth0_pidfile,
269 sizeof(lconfig->hc_pidfile));
271 if (!TAILQ_EMPTY(&depth0_listen))
272 TAILQ_CONCAT(&lconfig->hc_listen, &depth0_listen, hl_next);
273 if (TAILQ_EMPTY(&lconfig->hc_listen)) {
274 struct hastd_listen *lst;
276 if (family_supported(AF_INET)) {
277 lst = calloc(1, sizeof(*lst));
279 pjdlog_error("Unable to allocate memory for listen address.");
280 yy_config_free(lconfig);
285 (void)strlcpy(lst->hl_addr, depth0_listen_tcp4,
286 sizeof(lst->hl_addr));
287 TAILQ_INSERT_TAIL(&lconfig->hc_listen, lst, hl_next);
290 "No IPv4 support in the kernel, not listening on IPv4 address.");
292 if (family_supported(AF_INET6)) {
293 lst = calloc(1, sizeof(*lst));
295 pjdlog_error("Unable to allocate memory for listen address.");
296 yy_config_free(lconfig);
301 (void)strlcpy(lst->hl_addr, depth0_listen_tcp6,
302 sizeof(lst->hl_addr));
303 TAILQ_INSERT_TAIL(&lconfig->hc_listen, lst, hl_next);
306 "No IPv6 support in the kernel, not listening on IPv6 address.");
308 if (TAILQ_EMPTY(&lconfig->hc_listen)) {
309 pjdlog_error("No address to listen on.");
310 yy_config_free(lconfig);
316 TAILQ_FOREACH(curres, &lconfig->hc_resources, hr_next) {
317 PJDLOG_ASSERT(curres->hr_provname[0] != '\0');
318 PJDLOG_ASSERT(curres->hr_localpath[0] != '\0');
319 PJDLOG_ASSERT(curres->hr_remoteaddr[0] != '\0');
321 if (curres->hr_replication == -1) {
323 * Replication is not set at resource-level.
324 * Use global or default setting.
326 curres->hr_replication = depth0_replication;
327 curres->hr_original_replication = depth0_replication;
329 if (curres->hr_checksum == -1) {
331 * Checksum is not set at resource-level.
332 * Use global or default setting.
334 curres->hr_checksum = depth0_checksum;
336 if (curres->hr_compression == -1) {
338 * Compression is not set at resource-level.
339 * Use global or default setting.
341 curres->hr_compression = depth0_compression;
343 if (curres->hr_timeout == -1) {
345 * Timeout is not set at resource-level.
346 * Use global or default setting.
348 curres->hr_timeout = depth0_timeout;
350 if (curres->hr_exec[0] == '\0') {
352 * Exec is not set at resource-level.
353 * Use global or default setting.
355 strlcpy(curres->hr_exec, depth0_exec,
356 sizeof(curres->hr_exec));
358 if (curres->hr_metaflush == -1) {
360 * Metaflush is not set at resource-level.
361 * Use global or default setting.
363 curres->hr_metaflush = depth0_metaflush;
371 yy_config_free(struct hastd_config *config)
373 struct hastd_listen *lst;
374 struct hast_resource *res;
376 while ((lst = TAILQ_FIRST(&depth0_listen)) != NULL) {
377 TAILQ_REMOVE(&depth0_listen, lst, hl_next);
380 while ((lst = TAILQ_FIRST(&config->hc_listen)) != NULL) {
381 TAILQ_REMOVE(&config->hc_listen, lst, hl_next);
384 while ((res = TAILQ_FIRST(&config->hc_resources)) != NULL) {
385 TAILQ_REMOVE(&config->hc_resources, res, hr_next);
392 %token CONTROL PIDFILE LISTEN REPLICATION CHECKSUM COMPRESSION METAFLUSH
393 %token TIMEOUT EXEC RESOURCE NAME LOCAL REMOTE SOURCE ON OFF
394 %token FULLSYNC MEMSYNC ASYNC NONE CRC32 SHA256 HOLE LZF
397 %type <str> remote_str
398 %type <num> replication_type
399 %type <num> checksum_type
400 %type <num> compression_type
426 replication_statement
430 compression_statement
443 control_statement: CONTROL STR
447 if (strlcpy(depth0_control, $2,
448 sizeof(depth0_control)) >=
449 sizeof(depth0_control)) {
450 pjdlog_error("control argument is too long.");
458 if (strlcpy(lconfig->hc_controladdr, $2,
459 sizeof(lconfig->hc_controladdr)) >=
460 sizeof(lconfig->hc_controladdr)) {
461 pjdlog_error("control argument is too long.");
467 PJDLOG_ABORT("control at wrong depth level");
473 pidfile_statement: PIDFILE STR
477 if (strlcpy(depth0_pidfile, $2,
478 sizeof(depth0_pidfile)) >=
479 sizeof(depth0_pidfile)) {
480 pjdlog_error("pidfile argument is too long.");
488 if (strlcpy(lconfig->hc_pidfile, $2,
489 sizeof(lconfig->hc_pidfile)) >=
490 sizeof(lconfig->hc_pidfile)) {
491 pjdlog_error("pidfile argument is too long.");
497 PJDLOG_ABORT("pidfile at wrong depth level");
503 listen_statement: LISTEN STR
505 struct hastd_listen *lst;
507 lst = calloc(1, sizeof(*lst));
509 pjdlog_error("Unable to allocate memory for listen address.");
513 if (strlcpy(lst->hl_addr, $2, sizeof(lst->hl_addr)) >=
514 sizeof(lst->hl_addr)) {
515 pjdlog_error("listen argument is too long.");
522 TAILQ_INSERT_TAIL(&depth0_listen, lst, hl_next);
526 TAILQ_INSERT_TAIL(&depth0_listen, lst, hl_next);
531 PJDLOG_ABORT("listen at wrong depth level");
537 replication_statement: REPLICATION replication_type
541 depth0_replication = $2;
544 PJDLOG_ASSERT(curres != NULL);
545 curres->hr_replication = $2;
546 curres->hr_original_replication = $2;
549 PJDLOG_ABORT("replication at wrong depth level");
555 FULLSYNC { $$ = HAST_REPLICATION_FULLSYNC; }
557 MEMSYNC { $$ = HAST_REPLICATION_MEMSYNC; }
559 ASYNC { $$ = HAST_REPLICATION_ASYNC; }
562 checksum_statement: CHECKSUM checksum_type
566 depth0_checksum = $2;
569 PJDLOG_ASSERT(curres != NULL);
570 curres->hr_checksum = $2;
573 PJDLOG_ABORT("checksum at wrong depth level");
579 NONE { $$ = HAST_CHECKSUM_NONE; }
581 CRC32 { $$ = HAST_CHECKSUM_CRC32; }
583 SHA256 { $$ = HAST_CHECKSUM_SHA256; }
586 compression_statement: COMPRESSION compression_type
590 depth0_compression = $2;
593 PJDLOG_ASSERT(curres != NULL);
594 curres->hr_compression = $2;
597 PJDLOG_ABORT("compression at wrong depth level");
603 NONE { $$ = HAST_COMPRESSION_NONE; }
605 HOLE { $$ = HAST_COMPRESSION_HOLE; }
607 LZF { $$ = HAST_COMPRESSION_LZF; }
610 timeout_statement: TIMEOUT NUM
613 pjdlog_error("Negative or zero timeout.");
621 PJDLOG_ASSERT(curres != NULL);
622 curres->hr_timeout = $2;
625 PJDLOG_ABORT("timeout at wrong depth level");
630 exec_statement: EXEC STR
634 if (strlcpy(depth0_exec, $2, sizeof(depth0_exec)) >=
635 sizeof(depth0_exec)) {
636 pjdlog_error("Exec path is too long.");
642 PJDLOG_ASSERT(curres != NULL);
643 if (strlcpy(curres->hr_exec, $2,
644 sizeof(curres->hr_exec)) >=
645 sizeof(curres->hr_exec)) {
646 pjdlog_error("Exec path is too long.");
652 PJDLOG_ABORT("exec at wrong depth level");
658 metaflush_statement: METAFLUSH boolean
662 depth0_metaflush = $2;
665 PJDLOG_ASSERT(curres != NULL);
666 depth1_metaflush = $2;
671 PJDLOG_ASSERT(curres != NULL);
672 curres->hr_metaflush = $2;
675 PJDLOG_ABORT("metaflush at wrong depth level");
686 node_statement: ON node_start OB node_entries CB
694 switch (isitme($1)) {
704 PJDLOG_ABORT("invalid isitme() return value");
712 node_entries node_entry
723 resource_statement: RESOURCE resource_start OB resource_entries CB
725 if (curres != NULL) {
727 * There must be section for this node, at least with
728 * remote address configuration.
733 if (node_names(&names) != 0)
735 pjdlog_error("No resource %s configuration for this node (acceptable node names: %s).",
736 curres->hr_name, names);
741 * Let's see if there are some resource-level settings
742 * that we can use for node-level settings.
744 if (curres->hr_provname[0] == '\0' &&
745 depth1_provname[0] != '\0') {
747 * Provider name is not set at node-level,
748 * but is set at resource-level, use it.
750 strlcpy(curres->hr_provname, depth1_provname,
751 sizeof(curres->hr_provname));
753 if (curres->hr_localpath[0] == '\0' &&
754 depth1_localpath[0] != '\0') {
756 * Path to local provider is not set at
757 * node-level, but is set at resource-level,
760 strlcpy(curres->hr_localpath, depth1_localpath,
761 sizeof(curres->hr_localpath));
763 if (curres->hr_metaflush == -1 && depth1_metaflush != -1) {
765 * Metaflush is not set at node-level,
766 * but is set at resource-level, use it.
768 curres->hr_metaflush = depth1_metaflush;
772 * If provider name is not given, use resource name
775 if (curres->hr_provname[0] == '\0') {
776 strlcpy(curres->hr_provname, curres->hr_name,
777 sizeof(curres->hr_provname));
781 * Remote address has to be configured at this point.
783 if (curres->hr_remoteaddr[0] == '\0') {
784 pjdlog_error("Remote address not configured for resource %s.",
789 * Path to local provider has to be configured at this
792 if (curres->hr_localpath[0] == '\0') {
793 pjdlog_error("Path to local component not configured for resource %s.",
798 /* Put it onto resource list. */
799 TAILQ_INSERT_TAIL(&lconfig->hc_resources, curres, hr_next);
807 /* Check if there is no duplicate entry. */
808 TAILQ_FOREACH(curres, &lconfig->hc_resources, hr_next) {
809 if (strcmp(curres->hr_name, $1) == 0) {
810 pjdlog_error("Resource %s configured more than once.",
818 * Clear those, so we can tell if they were set at
819 * resource-level or not.
821 depth1_provname[0] = '\0';
822 depth1_localpath[0] = '\0';
823 depth1_metaflush = -1;
826 curres = calloc(1, sizeof(*curres));
827 if (curres == NULL) {
828 pjdlog_error("Unable to allocate memory for resource.");
832 if (strlcpy(curres->hr_name, $1,
833 sizeof(curres->hr_name)) >=
834 sizeof(curres->hr_name)) {
835 pjdlog_error("Resource name is too long.");
841 curres->hr_role = HAST_ROLE_INIT;
842 curres->hr_previous_role = HAST_ROLE_INIT;
843 curres->hr_replication = -1;
844 curres->hr_original_replication = -1;
845 curres->hr_checksum = -1;
846 curres->hr_compression = -1;
847 curres->hr_version = 1;
848 curres->hr_timeout = -1;
849 curres->hr_exec[0] = '\0';
850 curres->hr_provname[0] = '\0';
851 curres->hr_localpath[0] = '\0';
852 curres->hr_localfd = -1;
853 curres->hr_localflush = true;
854 curres->hr_metaflush = -1;
855 curres->hr_remoteaddr[0] = '\0';
856 curres->hr_sourceaddr[0] = '\0';
857 curres->hr_ggateunit = -1;
863 resource_entries resource_entry
867 replication_statement
871 compression_statement
883 resource_node_statement
886 name_statement: NAME STR
890 if (strlcpy(depth1_provname, $2,
891 sizeof(depth1_provname)) >=
892 sizeof(depth1_provname)) {
893 pjdlog_error("name argument is too long.");
901 PJDLOG_ASSERT(curres != NULL);
902 if (strlcpy(curres->hr_provname, $2,
903 sizeof(curres->hr_provname)) >=
904 sizeof(curres->hr_provname)) {
905 pjdlog_error("name argument is too long.");
911 PJDLOG_ABORT("name at wrong depth level");
917 local_statement: LOCAL STR
921 if (strlcpy(depth1_localpath, $2,
922 sizeof(depth1_localpath)) >=
923 sizeof(depth1_localpath)) {
924 pjdlog_error("local argument is too long.");
932 PJDLOG_ASSERT(curres != NULL);
933 if (strlcpy(curres->hr_localpath, $2,
934 sizeof(curres->hr_localpath)) >=
935 sizeof(curres->hr_localpath)) {
936 pjdlog_error("local argument is too long.");
942 PJDLOG_ABORT("local at wrong depth level");
948 resource_node_statement:ON resource_node_start OB resource_node_entries CB
954 resource_node_start: STR
956 if (curres != NULL) {
957 switch (isitme($1)) {
964 mynode = hadmynode = true;
967 PJDLOG_ABORT("invalid isitme() return value");
974 resource_node_entries:
976 resource_node_entries resource_node_entry
991 remote_statement: REMOTE remote_str
993 PJDLOG_ASSERT(depth == 2);
995 PJDLOG_ASSERT(curres != NULL);
996 if (strlcpy(curres->hr_remoteaddr, $2,
997 sizeof(curres->hr_remoteaddr)) >=
998 sizeof(curres->hr_remoteaddr)) {
999 pjdlog_error("remote argument is too long.");
1009 NONE { $$ = strdup("none"); }
1014 source_statement: SOURCE STR
1016 PJDLOG_ASSERT(depth == 2);
1018 PJDLOG_ASSERT(curres != NULL);
1019 if (strlcpy(curres->hr_sourceaddr, $2,
1020 sizeof(curres->hr_sourceaddr)) >=
1021 sizeof(curres->hr_sourceaddr)) {
1022 pjdlog_error("source argument is too long.");