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;
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 yyerror(const char *str)
116 pjdlog_error("Unable to parse configuration file at line %d near '%s': %s",
117 lineno, yytext, str);
120 struct hastd_config *
121 yy_config_parse(const char *config, bool exitonerror)
130 depth0_timeout = HAST_TIMEOUT;
131 depth0_replication = HAST_REPLICATION_MEMSYNC;
132 strlcpy(depth0_control, HAST_CONTROL, sizeof(depth0_control));
133 strlcpy(depth0_listen, HASTD_LISTEN, sizeof(depth0_listen));
134 depth0_exec[0] = '\0';
136 lconfig = calloc(1, sizeof(*lconfig));
137 if (lconfig == NULL) {
138 pjdlog_error("Unable to allocate memory for configuration.");
144 TAILQ_INIT(&lconfig->hc_resources);
146 yyin = fopen(config, "r");
148 pjdlog_errno(LOG_ERR, "Unable to open configuration file %s",
150 yy_config_free(lconfig);
159 yy_config_free(lconfig);
166 * Let's see if everything is set up.
168 if (lconfig->hc_controladdr[0] == '\0') {
169 strlcpy(lconfig->hc_controladdr, depth0_control,
170 sizeof(lconfig->hc_controladdr));
172 if (lconfig->hc_listenaddr[0] == '\0') {
173 strlcpy(lconfig->hc_listenaddr, depth0_listen,
174 sizeof(lconfig->hc_listenaddr));
176 TAILQ_FOREACH(curres, &lconfig->hc_resources, hr_next) {
177 assert(curres->hr_provname[0] != '\0');
178 assert(curres->hr_localpath[0] != '\0');
179 assert(curres->hr_remoteaddr[0] != '\0');
181 if (curres->hr_replication == -1) {
183 * Replication is not set at resource-level.
184 * Use global or default setting.
186 curres->hr_replication = depth0_replication;
188 if (curres->hr_timeout == -1) {
190 * Timeout is not set at resource-level.
191 * Use global or default setting.
193 curres->hr_timeout = depth0_timeout;
195 if (curres->hr_exec[0] == '\0') {
197 * Exec is not set at resource-level.
198 * Use global or default setting.
200 strlcpy(curres->hr_exec, depth0_exec,
201 sizeof(curres->hr_exec));
209 yy_config_free(struct hastd_config *config)
211 struct hast_resource *res;
213 while ((res = TAILQ_FIRST(&config->hc_resources)) != NULL) {
214 TAILQ_REMOVE(&config->hc_resources, res, hr_next);
221 %token CONTROL LISTEN PORT REPLICATION TIMEOUT EXEC EXTENTSIZE RESOURCE NAME LOCAL REMOTE ON
222 %token FULLSYNC MEMSYNC ASYNC
225 %type <num> replication_type
248 replication_statement
259 control_statement: CONTROL STR
263 if (strlcpy(depth0_control, $2,
264 sizeof(depth0_control)) >=
265 sizeof(depth0_control)) {
266 pjdlog_error("control argument is too long.");
273 if (strlcpy(lconfig->hc_controladdr, $2,
274 sizeof(lconfig->hc_controladdr)) >=
275 sizeof(lconfig->hc_controladdr)) {
276 pjdlog_error("control argument is too long.");
281 assert(!"control at wrong depth level");
286 listen_statement: LISTEN STR
290 if (strlcpy(depth0_listen, $2,
291 sizeof(depth0_listen)) >=
292 sizeof(depth0_listen)) {
293 pjdlog_error("listen argument is too long.");
300 if (strlcpy(lconfig->hc_listenaddr, $2,
301 sizeof(lconfig->hc_listenaddr)) >=
302 sizeof(lconfig->hc_listenaddr)) {
303 pjdlog_error("listen argument is too long.");
308 assert(!"listen at wrong depth level");
313 replication_statement: REPLICATION replication_type
317 depth0_replication = $2;
321 curres->hr_replication = $2;
324 assert(!"replication at wrong depth level");
330 FULLSYNC { $$ = HAST_REPLICATION_FULLSYNC; }
332 MEMSYNC { $$ = HAST_REPLICATION_MEMSYNC; }
334 ASYNC { $$ = HAST_REPLICATION_ASYNC; }
337 timeout_statement: TIMEOUT NUM
345 curres->hr_timeout = $2;
348 assert(!"timeout at wrong depth level");
353 exec_statement: EXEC STR
357 if (strlcpy(depth0_exec, $2, sizeof(depth0_exec)) >=
358 sizeof(depth0_exec)) {
359 pjdlog_error("Exec path is too long.");
366 if (strlcpy(curres->hr_exec, $2,
367 sizeof(curres->hr_exec)) >=
368 sizeof(curres->hr_exec)) {
369 pjdlog_error("Exec path is too long.");
374 assert(!"exec at wrong depth level");
379 node_statement: ON node_start OB node_entries CB
387 switch (isitme($1)) {
396 assert(!"invalid isitme() return value");
403 node_entries node_entry
412 resource_statement: RESOURCE resource_start OB resource_entries CB
414 if (curres != NULL) {
416 * Let's see there are some resource-level settings
417 * that we can use for node-level settings.
419 if (curres->hr_provname[0] == '\0' &&
420 depth1_provname[0] != '\0') {
422 * Provider name is not set at node-level,
423 * but is set at resource-level, use it.
425 strlcpy(curres->hr_provname, depth1_provname,
426 sizeof(curres->hr_provname));
428 if (curres->hr_localpath[0] == '\0' &&
429 depth1_localpath[0] != '\0') {
431 * Path to local provider is not set at
432 * node-level, but is set at resource-level,
435 strlcpy(curres->hr_localpath, depth1_localpath,
436 sizeof(curres->hr_localpath));
440 * If provider name is not given, use resource name
443 if (curres->hr_provname[0] == '\0') {
444 strlcpy(curres->hr_provname, curres->hr_name,
445 sizeof(curres->hr_provname));
449 * Remote address has to be configured at this point.
451 if (curres->hr_remoteaddr[0] == '\0') {
452 pjdlog_error("Remote address not configured for resource %s.",
457 * Path to local provider has to be configured at this
460 if (curres->hr_localpath[0] == '\0') {
461 pjdlog_error("Path to local component not configured for resource %s.",
466 /* Put it onto resource list. */
467 TAILQ_INSERT_TAIL(&lconfig->hc_resources, curres, hr_next);
476 * Clear those, so we can tell if they were set at
477 * resource-level or not.
479 depth1_provname[0] = '\0';
480 depth1_localpath[0] = '\0';
482 curres = calloc(1, sizeof(*curres));
483 if (curres == NULL) {
484 pjdlog_error("Unable to allocate memory for resource.");
487 if (strlcpy(curres->hr_name, $1,
488 sizeof(curres->hr_name)) >=
489 sizeof(curres->hr_name)) {
490 pjdlog_error("Resource name is too long.");
493 curres->hr_role = HAST_ROLE_INIT;
494 curres->hr_previous_role = HAST_ROLE_INIT;
495 curres->hr_replication = -1;
496 curres->hr_timeout = -1;
497 curres->hr_exec[0] = '\0';
498 curres->hr_provname[0] = '\0';
499 curres->hr_localpath[0] = '\0';
500 curres->hr_localfd = -1;
501 curres->hr_remoteaddr[0] = '\0';
502 curres->hr_ggateunit = -1;
508 resource_entries resource_entry
512 replication_statement
522 resource_node_statement
525 name_statement: NAME STR
529 if (strlcpy(depth1_provname, $2,
530 sizeof(depth1_provname)) >=
531 sizeof(depth1_provname)) {
532 pjdlog_error("name argument is too long.");
539 assert(curres != NULL);
540 if (strlcpy(curres->hr_provname, $2,
541 sizeof(curres->hr_provname)) >=
542 sizeof(curres->hr_provname)) {
543 pjdlog_error("name argument is too long.");
548 assert(!"name at wrong depth level");
553 local_statement: LOCAL STR
557 if (strlcpy(depth1_localpath, $2,
558 sizeof(depth1_localpath)) >=
559 sizeof(depth1_localpath)) {
560 pjdlog_error("local argument is too long.");
567 assert(curres != NULL);
568 if (strlcpy(curres->hr_localpath, $2,
569 sizeof(curres->hr_localpath)) >=
570 sizeof(curres->hr_localpath)) {
571 pjdlog_error("local argument is too long.");
576 assert(!"local at wrong depth level");
581 resource_node_statement:ON resource_node_start OB resource_node_entries CB
587 resource_node_start: STR
589 if (curres != NULL) {
590 switch (isitme($1)) {
599 assert(!"invalid isitme() return value");
605 resource_node_entries:
607 resource_node_entries resource_node_entry
618 remote_statement: REMOTE STR
622 assert(curres != NULL);
623 if (strlcpy(curres->hr_remoteaddr, $2,
624 sizeof(curres->hr_remoteaddr)) >=
625 sizeof(curres->hr_remoteaddr)) {
626 pjdlog_error("remote argument is too long.");