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 *);
83 isitme(const char *name)
85 char buf[MAXHOSTNAMELEN];
91 * First check if the given name matches our full hostname.
93 if (gethostname(buf, sizeof(buf)) < 0) {
94 pjdlog_errno(LOG_ERR, "gethostname() failed");
97 if (strcmp(buf, name) == 0)
101 * Check if it matches first part of the host name.
103 pos = strchr(buf, '.');
104 if (pos != NULL && (size_t)(pos - buf) == strlen(name) &&
105 strncmp(buf, name, pos - buf) == 0) {
110 * Check if it matches host UUID.
112 bufsize = sizeof(buf);
113 if (sysctlbyname("kern.hostuuid", buf, &bufsize, NULL, 0) < 0) {
114 pjdlog_errno(LOG_ERR, "sysctlbyname(kern.hostuuid) failed");
117 if (strcasecmp(buf, name) == 0)
121 * Check if it matches hostid.
123 bufsize = sizeof(hostid);
124 if (sysctlbyname("kern.hostid", &hostid, &bufsize, NULL, 0) < 0) {
125 pjdlog_errno(LOG_ERR, "sysctlbyname(kern.hostid) failed");
128 (void)snprintf(buf, sizeof(buf), "hostid%lu", hostid);
129 if (strcmp(buf, name) == 0)
133 * Looks like this isn't about us.
139 family_supported(int family)
143 sock = socket(family, SOCK_STREAM, 0);
144 if (sock == -1 && errno == EPROTONOSUPPORT)
152 node_names(char **namesp)
154 static char names[MAXHOSTNAMELEN * 3];
155 char buf[MAXHOSTNAMELEN];
156 unsigned long hostid;
160 if (gethostname(buf, sizeof(buf)) < 0) {
161 pjdlog_errno(LOG_ERR, "gethostname() failed");
165 /* First component of the host name. */
166 pos = strchr(buf, '.');
167 if (pos != NULL && pos != buf) {
168 (void)strlcpy(names, buf, MIN((size_t)(pos - buf + 1),
170 (void)strlcat(names, ", ", sizeof(names));
173 /* Full host name. */
174 (void)strlcat(names, buf, sizeof(names));
175 (void)strlcat(names, ", ", sizeof(names));
178 bufsize = sizeof(buf);
179 if (sysctlbyname("kern.hostuuid", buf, &bufsize, NULL, 0) < 0) {
180 pjdlog_errno(LOG_ERR, "sysctlbyname(kern.hostuuid) failed");
183 (void)strlcat(names, buf, sizeof(names));
184 (void)strlcat(names, ", ", sizeof(names));
187 bufsize = sizeof(hostid);
188 if (sysctlbyname("kern.hostid", &hostid, &bufsize, NULL, 0) < 0) {
189 pjdlog_errno(LOG_ERR, "sysctlbyname(kern.hostid) failed");
192 (void)snprintf(buf, sizeof(buf), "hostid%lu", hostid);
193 (void)strlcat(names, buf, sizeof(names));
201 yyerror(const char *str)
204 pjdlog_error("Unable to parse configuration file at line %d near '%s': %s",
205 lineno, yytext, str);
208 struct hastd_config *
209 yy_config_parse(const char *config, bool exitonerror)
218 depth0_timeout = HAST_TIMEOUT;
219 depth0_replication = HAST_REPLICATION_MEMSYNC;
220 depth0_checksum = HAST_CHECKSUM_NONE;
221 depth0_compression = HAST_COMPRESSION_HOLE;
222 strlcpy(depth0_control, HAST_CONTROL, sizeof(depth0_control));
223 strlcpy(depth0_pidfile, HASTD_PIDFILE, sizeof(depth0_pidfile));
224 TAILQ_INIT(&depth0_listen);
225 strlcpy(depth0_listen_tcp4, HASTD_LISTEN_TCP4,
226 sizeof(depth0_listen_tcp4));
227 strlcpy(depth0_listen_tcp6, HASTD_LISTEN_TCP6,
228 sizeof(depth0_listen_tcp6));
229 depth0_exec[0] = '\0';
230 depth0_metaflush = 1;
232 lconfig = calloc(1, sizeof(*lconfig));
233 if (lconfig == NULL) {
234 pjdlog_error("Unable to allocate memory for configuration.");
240 TAILQ_INIT(&lconfig->hc_listen);
241 TAILQ_INIT(&lconfig->hc_resources);
243 yyin = fopen(config, "r");
245 pjdlog_errno(LOG_ERR, "Unable to open configuration file %s",
247 yy_config_free(lconfig);
256 yy_config_free(lconfig);
263 * Let's see if everything is set up.
265 if (lconfig->hc_controladdr[0] == '\0') {
266 strlcpy(lconfig->hc_controladdr, depth0_control,
267 sizeof(lconfig->hc_controladdr));
269 if (lconfig->hc_pidfile[0] == '\0') {
270 strlcpy(lconfig->hc_pidfile, depth0_pidfile,
271 sizeof(lconfig->hc_pidfile));
273 if (!TAILQ_EMPTY(&depth0_listen))
274 TAILQ_CONCAT(&lconfig->hc_listen, &depth0_listen, hl_next);
275 if (TAILQ_EMPTY(&lconfig->hc_listen)) {
276 struct hastd_listen *lst;
278 if (family_supported(AF_INET)) {
279 lst = calloc(1, sizeof(*lst));
281 pjdlog_error("Unable to allocate memory for listen address.");
282 yy_config_free(lconfig);
287 (void)strlcpy(lst->hl_addr, depth0_listen_tcp4,
288 sizeof(lst->hl_addr));
289 TAILQ_INSERT_TAIL(&lconfig->hc_listen, lst, hl_next);
292 "No IPv4 support in the kernel, not listening on IPv4 address.");
294 if (family_supported(AF_INET6)) {
295 lst = calloc(1, sizeof(*lst));
297 pjdlog_error("Unable to allocate memory for listen address.");
298 yy_config_free(lconfig);
303 (void)strlcpy(lst->hl_addr, depth0_listen_tcp6,
304 sizeof(lst->hl_addr));
305 TAILQ_INSERT_TAIL(&lconfig->hc_listen, lst, hl_next);
308 "No IPv6 support in the kernel, not listening on IPv6 address.");
310 if (TAILQ_EMPTY(&lconfig->hc_listen)) {
311 pjdlog_error("No address to listen on.");
312 yy_config_free(lconfig);
318 TAILQ_FOREACH(curres, &lconfig->hc_resources, hr_next) {
319 PJDLOG_ASSERT(curres->hr_provname[0] != '\0');
320 PJDLOG_ASSERT(curres->hr_localpath[0] != '\0');
321 PJDLOG_ASSERT(curres->hr_remoteaddr[0] != '\0');
323 if (curres->hr_replication == -1) {
325 * Replication is not set at resource-level.
326 * Use global or default setting.
328 curres->hr_replication = depth0_replication;
329 curres->hr_original_replication = depth0_replication;
331 if (curres->hr_checksum == -1) {
333 * Checksum is not set at resource-level.
334 * Use global or default setting.
336 curres->hr_checksum = depth0_checksum;
338 if (curres->hr_compression == -1) {
340 * Compression is not set at resource-level.
341 * Use global or default setting.
343 curres->hr_compression = depth0_compression;
345 if (curres->hr_timeout == -1) {
347 * Timeout is not set at resource-level.
348 * Use global or default setting.
350 curres->hr_timeout = depth0_timeout;
352 if (curres->hr_exec[0] == '\0') {
354 * Exec is not set at resource-level.
355 * Use global or default setting.
357 strlcpy(curres->hr_exec, depth0_exec,
358 sizeof(curres->hr_exec));
360 if (curres->hr_metaflush == -1) {
362 * Metaflush is not set at resource-level.
363 * Use global or default setting.
365 curres->hr_metaflush = depth0_metaflush;
373 yy_config_free(struct hastd_config *config)
375 struct hastd_listen *lst;
376 struct hast_resource *res;
378 while ((lst = TAILQ_FIRST(&depth0_listen)) != NULL) {
379 TAILQ_REMOVE(&depth0_listen, lst, hl_next);
382 while ((lst = TAILQ_FIRST(&config->hc_listen)) != NULL) {
383 TAILQ_REMOVE(&config->hc_listen, lst, hl_next);
386 while ((res = TAILQ_FIRST(&config->hc_resources)) != NULL) {
387 TAILQ_REMOVE(&config->hc_resources, res, hr_next);
394 %token CONTROL PIDFILE LISTEN REPLICATION CHECKSUM COMPRESSION METAFLUSH
395 %token TIMEOUT EXEC RESOURCE NAME LOCAL REMOTE SOURCE ON OFF
396 %token FULLSYNC MEMSYNC ASYNC NONE CRC32 SHA256 HOLE LZF
399 %type <str> remote_str
400 %type <num> replication_type
401 %type <num> checksum_type
402 %type <num> compression_type
428 replication_statement
432 compression_statement
445 control_statement: CONTROL STR
449 if (strlcpy(depth0_control, $2,
450 sizeof(depth0_control)) >=
451 sizeof(depth0_control)) {
452 pjdlog_error("control argument is too long.");
460 if (strlcpy(lconfig->hc_controladdr, $2,
461 sizeof(lconfig->hc_controladdr)) >=
462 sizeof(lconfig->hc_controladdr)) {
463 pjdlog_error("control argument is too long.");
469 PJDLOG_ABORT("control at wrong depth level");
475 pidfile_statement: PIDFILE STR
479 if (strlcpy(depth0_pidfile, $2,
480 sizeof(depth0_pidfile)) >=
481 sizeof(depth0_pidfile)) {
482 pjdlog_error("pidfile argument is too long.");
490 if (strlcpy(lconfig->hc_pidfile, $2,
491 sizeof(lconfig->hc_pidfile)) >=
492 sizeof(lconfig->hc_pidfile)) {
493 pjdlog_error("pidfile argument is too long.");
499 PJDLOG_ABORT("pidfile at wrong depth level");
505 listen_statement: LISTEN STR
507 struct hastd_listen *lst;
509 lst = calloc(1, sizeof(*lst));
511 pjdlog_error("Unable to allocate memory for listen address.");
515 if (strlcpy(lst->hl_addr, $2, sizeof(lst->hl_addr)) >=
516 sizeof(lst->hl_addr)) {
517 pjdlog_error("listen argument is too long.");
524 TAILQ_INSERT_TAIL(&depth0_listen, lst, hl_next);
528 TAILQ_INSERT_TAIL(&depth0_listen, lst, hl_next);
533 PJDLOG_ABORT("listen at wrong depth level");
539 replication_statement: REPLICATION replication_type
543 depth0_replication = $2;
546 PJDLOG_ASSERT(curres != NULL);
547 curres->hr_replication = $2;
548 curres->hr_original_replication = $2;
551 PJDLOG_ABORT("replication at wrong depth level");
557 FULLSYNC { $$ = HAST_REPLICATION_FULLSYNC; }
559 MEMSYNC { $$ = HAST_REPLICATION_MEMSYNC; }
561 ASYNC { $$ = HAST_REPLICATION_ASYNC; }
564 checksum_statement: CHECKSUM checksum_type
568 depth0_checksum = $2;
571 PJDLOG_ASSERT(curres != NULL);
572 curres->hr_checksum = $2;
575 PJDLOG_ABORT("checksum at wrong depth level");
581 NONE { $$ = HAST_CHECKSUM_NONE; }
583 CRC32 { $$ = HAST_CHECKSUM_CRC32; }
585 SHA256 { $$ = HAST_CHECKSUM_SHA256; }
588 compression_statement: COMPRESSION compression_type
592 depth0_compression = $2;
595 PJDLOG_ASSERT(curres != NULL);
596 curres->hr_compression = $2;
599 PJDLOG_ABORT("compression at wrong depth level");
605 NONE { $$ = HAST_COMPRESSION_NONE; }
607 HOLE { $$ = HAST_COMPRESSION_HOLE; }
609 LZF { $$ = HAST_COMPRESSION_LZF; }
612 timeout_statement: TIMEOUT NUM
615 pjdlog_error("Negative or zero timeout.");
623 PJDLOG_ASSERT(curres != NULL);
624 curres->hr_timeout = $2;
627 PJDLOG_ABORT("timeout at wrong depth level");
632 exec_statement: EXEC STR
636 if (strlcpy(depth0_exec, $2, sizeof(depth0_exec)) >=
637 sizeof(depth0_exec)) {
638 pjdlog_error("Exec path is too long.");
644 PJDLOG_ASSERT(curres != NULL);
645 if (strlcpy(curres->hr_exec, $2,
646 sizeof(curres->hr_exec)) >=
647 sizeof(curres->hr_exec)) {
648 pjdlog_error("Exec path is too long.");
654 PJDLOG_ABORT("exec at wrong depth level");
660 metaflush_statement: METAFLUSH boolean
664 depth0_metaflush = $2;
667 PJDLOG_ASSERT(curres != NULL);
668 depth1_metaflush = $2;
673 PJDLOG_ASSERT(curres != NULL);
674 curres->hr_metaflush = $2;
677 PJDLOG_ABORT("metaflush at wrong depth level");
688 node_statement: ON node_start OB node_entries CB
696 switch (isitme($1)) {
706 PJDLOG_ABORT("invalid isitme() return value");
714 node_entries node_entry
725 resource_statement: RESOURCE resource_start OB resource_entries CB
727 if (curres != NULL) {
729 * There must be section for this node, at least with
730 * remote address configuration.
735 if (node_names(&names) != 0)
737 pjdlog_error("No resource %s configuration for this node (acceptable node names: %s).",
738 curres->hr_name, names);
743 * Let's see if there are some resource-level settings
744 * that we can use for node-level settings.
746 if (curres->hr_provname[0] == '\0' &&
747 depth1_provname[0] != '\0') {
749 * Provider name is not set at node-level,
750 * but is set at resource-level, use it.
752 strlcpy(curres->hr_provname, depth1_provname,
753 sizeof(curres->hr_provname));
755 if (curres->hr_localpath[0] == '\0' &&
756 depth1_localpath[0] != '\0') {
758 * Path to local provider is not set at
759 * node-level, but is set at resource-level,
762 strlcpy(curres->hr_localpath, depth1_localpath,
763 sizeof(curres->hr_localpath));
765 if (curres->hr_metaflush == -1 && depth1_metaflush != -1) {
767 * Metaflush is not set at node-level,
768 * but is set at resource-level, use it.
770 curres->hr_metaflush = depth1_metaflush;
774 * If provider name is not given, use resource name
777 if (curres->hr_provname[0] == '\0') {
778 strlcpy(curres->hr_provname, curres->hr_name,
779 sizeof(curres->hr_provname));
783 * Remote address has to be configured at this point.
785 if (curres->hr_remoteaddr[0] == '\0') {
786 pjdlog_error("Remote address not configured for resource %s.",
791 * Path to local provider has to be configured at this
794 if (curres->hr_localpath[0] == '\0') {
795 pjdlog_error("Path to local component not configured for resource %s.",
800 /* Put it onto resource list. */
801 TAILQ_INSERT_TAIL(&lconfig->hc_resources, curres, hr_next);
809 /* Check if there is no duplicate entry. */
810 TAILQ_FOREACH(curres, &lconfig->hc_resources, hr_next) {
811 if (strcmp(curres->hr_name, $1) == 0) {
812 pjdlog_error("Resource %s configured more than once.",
820 * Clear those, so we can tell if they were set at
821 * resource-level or not.
823 depth1_provname[0] = '\0';
824 depth1_localpath[0] = '\0';
825 depth1_metaflush = -1;
828 curres = calloc(1, sizeof(*curres));
829 if (curres == NULL) {
830 pjdlog_error("Unable to allocate memory for resource.");
834 if (strlcpy(curres->hr_name, $1,
835 sizeof(curres->hr_name)) >=
836 sizeof(curres->hr_name)) {
837 pjdlog_error("Resource name is too long.");
843 curres->hr_role = HAST_ROLE_INIT;
844 curres->hr_previous_role = HAST_ROLE_INIT;
845 curres->hr_replication = -1;
846 curres->hr_original_replication = -1;
847 curres->hr_checksum = -1;
848 curres->hr_compression = -1;
849 curres->hr_version = 1;
850 curres->hr_timeout = -1;
851 curres->hr_exec[0] = '\0';
852 curres->hr_provname[0] = '\0';
853 curres->hr_localpath[0] = '\0';
854 curres->hr_localfd = -1;
855 curres->hr_localflush = true;
856 curres->hr_metaflush = -1;
857 curres->hr_remoteaddr[0] = '\0';
858 curres->hr_sourceaddr[0] = '\0';
859 curres->hr_ggateunit = -1;
865 resource_entries resource_entry
869 replication_statement
873 compression_statement
885 resource_node_statement
888 name_statement: NAME STR
892 if (strlcpy(depth1_provname, $2,
893 sizeof(depth1_provname)) >=
894 sizeof(depth1_provname)) {
895 pjdlog_error("name argument is too long.");
903 PJDLOG_ASSERT(curres != NULL);
904 if (strlcpy(curres->hr_provname, $2,
905 sizeof(curres->hr_provname)) >=
906 sizeof(curres->hr_provname)) {
907 pjdlog_error("name argument is too long.");
913 PJDLOG_ABORT("name at wrong depth level");
919 local_statement: LOCAL STR
923 if (strlcpy(depth1_localpath, $2,
924 sizeof(depth1_localpath)) >=
925 sizeof(depth1_localpath)) {
926 pjdlog_error("local argument is too long.");
934 PJDLOG_ASSERT(curres != NULL);
935 if (strlcpy(curres->hr_localpath, $2,
936 sizeof(curres->hr_localpath)) >=
937 sizeof(curres->hr_localpath)) {
938 pjdlog_error("local argument is too long.");
944 PJDLOG_ABORT("local at wrong depth level");
950 resource_node_statement:ON resource_node_start OB resource_node_entries CB
956 resource_node_start: STR
958 if (curres != NULL) {
959 switch (isitme($1)) {
966 mynode = hadmynode = true;
969 PJDLOG_ABORT("invalid isitme() return value");
976 resource_node_entries:
978 resource_node_entries resource_node_entry
993 remote_statement: REMOTE remote_str
995 PJDLOG_ASSERT(depth == 2);
997 PJDLOG_ASSERT(curres != NULL);
998 if (strlcpy(curres->hr_remoteaddr, $2,
999 sizeof(curres->hr_remoteaddr)) >=
1000 sizeof(curres->hr_remoteaddr)) {
1001 pjdlog_error("remote argument is too long.");
1011 NONE { $$ = strdup("none"); }
1016 source_statement: SOURCE STR
1018 PJDLOG_ASSERT(depth == 2);
1020 PJDLOG_ASSERT(curres != NULL);
1021 if (strlcpy(curres->hr_sourceaddr, $2,
1022 sizeof(curres->hr_sourceaddr)) >=
1023 sizeof(curres->hr_sourceaddr)) {
1024 pjdlog_error("source argument is too long.");