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>
54 static struct hastd_config lconfig;
55 static struct hast_resource *curres;
58 static char depth0_control[HAST_ADDRSIZE];
59 static char depth0_listen[HAST_ADDRSIZE];
60 static int depth0_replication;
61 static int depth0_timeout;
63 static char depth1_provname[PATH_MAX];
64 static char depth1_localpath[PATH_MAX];
67 isitme(const char *name)
69 char buf[MAXHOSTNAMELEN];
74 * First check if the give name matches our full hostname.
76 if (gethostname(buf, sizeof(buf)) < 0)
77 err(EX_OSERR, "gethostname() failed");
78 if (strcmp(buf, name) == 0)
82 * Now check if it matches first part of the host name.
84 pos = strchr(buf, '.');
85 if (pos != NULL && pos != buf && strncmp(buf, name, pos - buf) == 0)
89 * At the end check if name is equal to our host's UUID.
91 bufsize = sizeof(buf);
92 if (sysctlbyname("kern.hostuuid", buf, &bufsize, NULL, 0) < 0)
93 err(EX_OSERR, "sysctlbyname(kern.hostuuid) failed");
94 if (strcasecmp(buf, name) == 0)
98 * Looks like this isn't about us.
104 yyerror(const char *str)
107 fprintf(stderr, "error at line %d near '%s': %s\n",
108 lineno, yytext, str);
111 struct hastd_config *
112 yy_config_parse(const char *config)
119 depth0_timeout = HAST_TIMEOUT;
120 depth0_replication = HAST_REPLICATION_MEMSYNC;
121 strlcpy(depth0_control, HAST_CONTROL, sizeof(depth0_control));
122 strlcpy(depth0_listen, HASTD_LISTEN, sizeof(depth0_listen));
124 TAILQ_INIT(&lconfig.hc_resources);
126 yyin = fopen(config, "r");
128 err(EX_OSFILE, "cannot open configuration file %s", config);
132 yy_config_free(&lconfig);
137 * Let's see if everything is set up.
139 if (lconfig.hc_controladdr[0] == '\0') {
140 strlcpy(lconfig.hc_controladdr, depth0_control,
141 sizeof(lconfig.hc_controladdr));
143 if (lconfig.hc_listenaddr[0] == '\0') {
144 strlcpy(lconfig.hc_listenaddr, depth0_listen,
145 sizeof(lconfig.hc_listenaddr));
147 TAILQ_FOREACH(curres, &lconfig.hc_resources, hr_next) {
148 assert(curres->hr_provname[0] != '\0');
149 assert(curres->hr_localpath[0] != '\0');
150 assert(curres->hr_remoteaddr[0] != '\0');
152 if (curres->hr_replication == -1) {
154 * Replication is not set at resource-level.
155 * Use global or default setting.
157 curres->hr_replication = depth0_replication;
159 if (curres->hr_timeout == -1) {
161 * Timeout is not set at resource-level.
162 * Use global or default setting.
164 curres->hr_timeout = depth0_timeout;
172 yy_config_free(struct hastd_config *config)
174 struct hast_resource *res;
176 while ((res = TAILQ_FIRST(&config->hc_resources)) != NULL) {
177 TAILQ_REMOVE(&config->hc_resources, res, hr_next);
183 %token CONTROL LISTEN PORT REPLICATION TIMEOUT EXTENTSIZE RESOURCE NAME LOCAL REMOTE ON
184 %token FULLSYNC MEMSYNC ASYNC
187 %type <num> replication_type
210 replication_statement
219 control_statement: CONTROL STR
223 if (strlcpy(depth0_control, $2,
224 sizeof(depth0_control)) >=
225 sizeof(depth0_control)) {
226 errx(EX_CONFIG, "control argument too long");
231 if (strlcpy(lconfig.hc_controladdr, $2,
232 sizeof(lconfig.hc_controladdr)) >=
233 sizeof(lconfig.hc_controladdr)) {
235 "control argument too long");
240 assert(!"control at wrong depth level");
245 listen_statement: LISTEN STR
249 if (strlcpy(depth0_listen, $2,
250 sizeof(depth0_listen)) >=
251 sizeof(depth0_listen)) {
252 errx(EX_CONFIG, "listen argument too long");
257 if (strlcpy(lconfig.hc_listenaddr, $2,
258 sizeof(lconfig.hc_listenaddr)) >=
259 sizeof(lconfig.hc_listenaddr)) {
261 "listen argument too long");
266 assert(!"listen at wrong depth level");
271 replication_statement: REPLICATION replication_type
275 depth0_replication = $2;
279 curres->hr_replication = $2;
282 assert(!"replication at wrong depth level");
288 FULLSYNC { $$ = HAST_REPLICATION_FULLSYNC; }
290 MEMSYNC { $$ = HAST_REPLICATION_MEMSYNC; }
292 ASYNC { $$ = HAST_REPLICATION_ASYNC; }
295 timeout_statement: TIMEOUT NUM
303 curres->hr_timeout = $2;
306 assert(!"timeout at wrong depth level");
311 node_statement: ON node_start OB node_entries CB
326 node_entries node_entry
335 resource_statement: RESOURCE resource_start OB resource_entries CB
337 if (curres != NULL) {
339 * Let's see there are some resource-level settings
340 * that we can use for node-level settings.
342 if (curres->hr_provname[0] == '\0' &&
343 depth1_provname[0] != '\0') {
345 * Provider name is not set at node-level,
346 * but is set at resource-level, use it.
348 strlcpy(curres->hr_provname, depth1_provname,
349 sizeof(curres->hr_provname));
351 if (curres->hr_localpath[0] == '\0' &&
352 depth1_localpath[0] != '\0') {
354 * Path to local provider is not set at
355 * node-level, but is set at resource-level,
358 strlcpy(curres->hr_localpath, depth1_localpath,
359 sizeof(curres->hr_localpath));
363 * If provider name is not given, use resource name
366 if (curres->hr_provname[0] == '\0') {
367 strlcpy(curres->hr_provname, curres->hr_name,
368 sizeof(curres->hr_provname));
372 * Remote address has to be configured at this point.
374 if (curres->hr_remoteaddr[0] == '\0') {
376 "remote address not configured for resource %s",
380 * Path to local provider has to be configured at this
383 if (curres->hr_localpath[0] == '\0') {
385 "path local component not configured for resource %s",
389 /* Put it onto resource list. */
390 TAILQ_INSERT_TAIL(&lconfig.hc_resources, curres, hr_next);
399 * Clear those, so we can tell if they were set at
400 * resource-level or not.
402 depth1_provname[0] = '\0';
403 depth1_localpath[0] = '\0';
405 curres = calloc(1, sizeof(*curres));
406 if (curres == NULL) {
408 "cannot allocate memory for resource");
410 if (strlcpy(curres->hr_name, $1,
411 sizeof(curres->hr_name)) >=
412 sizeof(curres->hr_name)) {
414 "resource name (%s) too long", $1);
416 curres->hr_role = HAST_ROLE_INIT;
417 curres->hr_previous_role = HAST_ROLE_INIT;
418 curres->hr_replication = -1;
419 curres->hr_timeout = -1;
420 curres->hr_provname[0] = '\0';
421 curres->hr_localpath[0] = '\0';
422 curres->hr_localfd = -1;
423 curres->hr_remoteaddr[0] = '\0';
424 curres->hr_ggateunit = -1;
430 resource_entries resource_entry
434 replication_statement
442 resource_node_statement
445 name_statement: NAME STR
449 if (strlcpy(depth1_provname, $2,
450 sizeof(depth1_provname)) >=
451 sizeof(depth1_provname)) {
452 errx(EX_CONFIG, "name argument too long");
457 assert(curres != NULL);
458 if (strlcpy(curres->hr_provname, $2,
459 sizeof(curres->hr_provname)) >=
460 sizeof(curres->hr_provname)) {
462 "name argument too long");
467 assert(!"name at wrong depth level");
472 local_statement: LOCAL STR
476 if (strlcpy(depth1_localpath, $2,
477 sizeof(depth1_localpath)) >=
478 sizeof(depth1_localpath)) {
479 errx(EX_CONFIG, "local argument too long");
484 assert(curres != NULL);
485 if (strlcpy(curres->hr_localpath, $2,
486 sizeof(curres->hr_localpath)) >=
487 sizeof(curres->hr_localpath)) {
489 "local argument too long");
494 assert(!"local at wrong depth level");
499 resource_node_statement:ON resource_node_start OB resource_node_entries CB
505 resource_node_start: STR
507 if (curres != NULL && isitme($1))
512 resource_node_entries:
514 resource_node_entries resource_node_entry
525 remote_statement: REMOTE STR
529 assert(curres != NULL);
530 if (strlcpy(curres->hr_remoteaddr, $2,
531 sizeof(curres->hr_remoteaddr)) >=
532 sizeof(curres->hr_remoteaddr)) {
533 errx(EX_CONFIG, "remote argument too long");