3 * Copyright (c) 2009-2010 The FreeBSD Foundation
6 * This software was developed by Pawel Jakub Dawidek under sponsorship from
7 * the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/param.h> /* MAXHOSTNAMELEN */
34 #include <sys/queue.h>
35 #include <sys/sysctl.h>
37 #include <arpa/inet.h>
56 static struct hastd_config *lconfig;
57 static struct hast_resource *curres;
58 static bool mynode, hadmynode;
60 static char depth0_control[HAST_ADDRSIZE];
61 static char depth0_listen[HAST_ADDRSIZE];
62 static int depth0_replication;
63 static int depth0_timeout;
64 static char depth0_exec[PATH_MAX];
66 static char depth1_provname[PATH_MAX];
67 static char depth1_localpath[PATH_MAX];
69 extern void yyrestart(FILE *);
72 isitme(const char *name)
74 char buf[MAXHOSTNAMELEN];
79 * First check if the give name matches our full hostname.
81 if (gethostname(buf, sizeof(buf)) < 0) {
82 pjdlog_errno(LOG_ERR, "gethostname() failed");
85 if (strcmp(buf, name) == 0)
89 * Now check if it matches first part of the host name.
91 pos = strchr(buf, '.');
92 if (pos != NULL && pos != buf && strncmp(buf, name, pos - buf) == 0)
96 * At the end check if name is equal to our host's UUID.
98 bufsize = sizeof(buf);
99 if (sysctlbyname("kern.hostuuid", buf, &bufsize, NULL, 0) < 0) {
100 pjdlog_errno(LOG_ERR, "sysctlbyname(kern.hostuuid) failed");
103 if (strcasecmp(buf, name) == 0)
107 * Looks like this isn't about us.
113 node_names(char **namesp)
115 static char names[MAXHOSTNAMELEN * 3];
116 char buf[MAXHOSTNAMELEN];
120 if (gethostname(buf, sizeof(buf)) < 0) {
121 pjdlog_errno(LOG_ERR, "gethostname() failed");
125 /* First component of the host name. */
126 pos = strchr(buf, '.');
127 if (pos != NULL && pos != buf) {
128 (void)strlcpy(names, buf, MIN((size_t)(pos - buf + 1),
130 (void)strlcat(names, ", ", sizeof(names));
133 /* Full host name. */
134 (void)strlcat(names, buf, sizeof(names));
135 (void)strlcat(names, ", ", sizeof(names));
138 bufsize = sizeof(buf);
139 if (sysctlbyname("kern.hostuuid", buf, &bufsize, NULL, 0) < 0) {
140 pjdlog_errno(LOG_ERR, "sysctlbyname(kern.hostuuid) failed");
143 (void)strlcat(names, buf, sizeof(names));
151 yyerror(const char *str)
154 pjdlog_error("Unable to parse configuration file at line %d near '%s': %s",
155 lineno, yytext, str);
158 struct hastd_config *
159 yy_config_parse(const char *config, bool exitonerror)
168 depth0_timeout = HAST_TIMEOUT;
169 depth0_replication = HAST_REPLICATION_MEMSYNC;
170 strlcpy(depth0_control, HAST_CONTROL, sizeof(depth0_control));
171 strlcpy(depth0_listen, HASTD_LISTEN, sizeof(depth0_listen));
172 depth0_exec[0] = '\0';
174 lconfig = calloc(1, sizeof(*lconfig));
175 if (lconfig == NULL) {
176 pjdlog_error("Unable to allocate memory for configuration.");
182 TAILQ_INIT(&lconfig->hc_resources);
184 yyin = fopen(config, "r");
186 pjdlog_errno(LOG_ERR, "Unable to open configuration file %s",
188 yy_config_free(lconfig);
197 yy_config_free(lconfig);
204 * Let's see if everything is set up.
206 if (lconfig->hc_controladdr[0] == '\0') {
207 strlcpy(lconfig->hc_controladdr, depth0_control,
208 sizeof(lconfig->hc_controladdr));
210 if (lconfig->hc_listenaddr[0] == '\0') {
211 strlcpy(lconfig->hc_listenaddr, depth0_listen,
212 sizeof(lconfig->hc_listenaddr));
214 TAILQ_FOREACH(curres, &lconfig->hc_resources, hr_next) {
215 assert(curres->hr_provname[0] != '\0');
216 assert(curres->hr_localpath[0] != '\0');
217 assert(curres->hr_remoteaddr[0] != '\0');
219 if (curres->hr_replication == -1) {
221 * Replication is not set at resource-level.
222 * Use global or default setting.
224 curres->hr_replication = depth0_replication;
226 if (curres->hr_timeout == -1) {
228 * Timeout is not set at resource-level.
229 * Use global or default setting.
231 curres->hr_timeout = depth0_timeout;
233 if (curres->hr_exec[0] == '\0') {
235 * Exec is not set at resource-level.
236 * Use global or default setting.
238 strlcpy(curres->hr_exec, depth0_exec,
239 sizeof(curres->hr_exec));
247 yy_config_free(struct hastd_config *config)
249 struct hast_resource *res;
251 while ((res = TAILQ_FIRST(&config->hc_resources)) != NULL) {
252 TAILQ_REMOVE(&config->hc_resources, res, hr_next);
259 %token CONTROL LISTEN PORT REPLICATION TIMEOUT EXEC EXTENTSIZE RESOURCE NAME LOCAL REMOTE ON
260 %token FULLSYNC MEMSYNC ASYNC
263 %type <num> replication_type
286 replication_statement
297 control_statement: CONTROL STR
301 if (strlcpy(depth0_control, $2,
302 sizeof(depth0_control)) >=
303 sizeof(depth0_control)) {
304 pjdlog_error("control argument is too long.");
312 if (strlcpy(lconfig->hc_controladdr, $2,
313 sizeof(lconfig->hc_controladdr)) >=
314 sizeof(lconfig->hc_controladdr)) {
315 pjdlog_error("control argument is too long.");
321 assert(!"control at wrong depth level");
327 listen_statement: LISTEN STR
331 if (strlcpy(depth0_listen, $2,
332 sizeof(depth0_listen)) >=
333 sizeof(depth0_listen)) {
334 pjdlog_error("listen argument is too long.");
342 if (strlcpy(lconfig->hc_listenaddr, $2,
343 sizeof(lconfig->hc_listenaddr)) >=
344 sizeof(lconfig->hc_listenaddr)) {
345 pjdlog_error("listen argument is too long.");
351 assert(!"listen at wrong depth level");
357 replication_statement: REPLICATION replication_type
361 depth0_replication = $2;
365 curres->hr_replication = $2;
368 assert(!"replication at wrong depth level");
374 FULLSYNC { $$ = HAST_REPLICATION_FULLSYNC; }
376 MEMSYNC { $$ = HAST_REPLICATION_MEMSYNC; }
378 ASYNC { $$ = HAST_REPLICATION_ASYNC; }
381 timeout_statement: TIMEOUT NUM
389 curres->hr_timeout = $2;
392 assert(!"timeout at wrong depth level");
397 exec_statement: EXEC STR
401 if (strlcpy(depth0_exec, $2, sizeof(depth0_exec)) >=
402 sizeof(depth0_exec)) {
403 pjdlog_error("Exec path is too long.");
411 if (strlcpy(curres->hr_exec, $2,
412 sizeof(curres->hr_exec)) >=
413 sizeof(curres->hr_exec)) {
414 pjdlog_error("Exec path is too long.");
420 assert(!"exec at wrong depth level");
426 node_statement: ON node_start OB node_entries CB
434 switch (isitme($1)) {
444 assert(!"invalid isitme() return value");
452 node_entries node_entry
461 resource_statement: RESOURCE resource_start OB resource_entries CB
463 if (curres != NULL) {
465 * There must be section for this node, at least with
466 * remote address configuration.
471 if (node_names(&names) != 0)
473 pjdlog_error("No resource %s configuration for this node (acceptable node names: %s).",
474 curres->hr_name, names);
479 * Let's see there are some resource-level settings
480 * that we can use for node-level settings.
482 if (curres->hr_provname[0] == '\0' &&
483 depth1_provname[0] != '\0') {
485 * Provider name is not set at node-level,
486 * but is set at resource-level, use it.
488 strlcpy(curres->hr_provname, depth1_provname,
489 sizeof(curres->hr_provname));
491 if (curres->hr_localpath[0] == '\0' &&
492 depth1_localpath[0] != '\0') {
494 * Path to local provider is not set at
495 * node-level, but is set at resource-level,
498 strlcpy(curres->hr_localpath, depth1_localpath,
499 sizeof(curres->hr_localpath));
503 * If provider name is not given, use resource name
506 if (curres->hr_provname[0] == '\0') {
507 strlcpy(curres->hr_provname, curres->hr_name,
508 sizeof(curres->hr_provname));
512 * Remote address has to be configured at this point.
514 if (curres->hr_remoteaddr[0] == '\0') {
515 pjdlog_error("Remote address not configured for resource %s.",
520 * Path to local provider has to be configured at this
523 if (curres->hr_localpath[0] == '\0') {
524 pjdlog_error("Path to local component not configured for resource %s.",
529 /* Put it onto resource list. */
530 TAILQ_INSERT_TAIL(&lconfig->hc_resources, curres, hr_next);
538 /* Check if there is no duplicate entry. */
539 TAILQ_FOREACH(curres, &lconfig->hc_resources, hr_next) {
540 if (strcmp(curres->hr_name, $1) == 0) {
541 pjdlog_error("Resource %s configured more than once.",
549 * Clear those, so we can tell if they were set at
550 * resource-level or not.
552 depth1_provname[0] = '\0';
553 depth1_localpath[0] = '\0';
556 curres = calloc(1, sizeof(*curres));
557 if (curres == NULL) {
558 pjdlog_error("Unable to allocate memory for resource.");
562 if (strlcpy(curres->hr_name, $1,
563 sizeof(curres->hr_name)) >=
564 sizeof(curres->hr_name)) {
565 pjdlog_error("Resource name is too long.");
570 curres->hr_role = HAST_ROLE_INIT;
571 curres->hr_previous_role = HAST_ROLE_INIT;
572 curres->hr_replication = -1;
573 curres->hr_timeout = -1;
574 curres->hr_exec[0] = '\0';
575 curres->hr_provname[0] = '\0';
576 curres->hr_localpath[0] = '\0';
577 curres->hr_localfd = -1;
578 curres->hr_remoteaddr[0] = '\0';
579 curres->hr_ggateunit = -1;
585 resource_entries resource_entry
589 replication_statement
599 resource_node_statement
602 name_statement: NAME STR
606 if (strlcpy(depth1_provname, $2,
607 sizeof(depth1_provname)) >=
608 sizeof(depth1_provname)) {
609 pjdlog_error("name argument is too long.");
617 assert(curres != NULL);
618 if (strlcpy(curres->hr_provname, $2,
619 sizeof(curres->hr_provname)) >=
620 sizeof(curres->hr_provname)) {
621 pjdlog_error("name argument is too long.");
627 assert(!"name at wrong depth level");
633 local_statement: LOCAL STR
637 if (strlcpy(depth1_localpath, $2,
638 sizeof(depth1_localpath)) >=
639 sizeof(depth1_localpath)) {
640 pjdlog_error("local argument is too long.");
648 assert(curres != NULL);
649 if (strlcpy(curres->hr_localpath, $2,
650 sizeof(curres->hr_localpath)) >=
651 sizeof(curres->hr_localpath)) {
652 pjdlog_error("local argument is too long.");
658 assert(!"local at wrong depth level");
664 resource_node_statement:ON resource_node_start OB resource_node_entries CB
670 resource_node_start: STR
672 if (curres != NULL) {
673 switch (isitme($1)) {
680 mynode = hadmynode = true;
683 assert(!"invalid isitme() return value");
690 resource_node_entries:
692 resource_node_entries resource_node_entry
703 remote_statement: REMOTE STR
707 assert(curres != NULL);
708 if (strlcpy(curres->hr_remoteaddr, $2,
709 sizeof(curres->hr_remoteaddr)) >=
710 sizeof(curres->hr_remoteaddr)) {
711 pjdlog_error("remote argument is too long.");