2 * Copyright (c) 2009-2010 The FreeBSD Foundation
5 * This software was developed by Pawel Jakub Dawidek under sponsorship from
6 * the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/types.h>
46 #include "hast_checksum.h"
47 #include "hast_compression.h"
48 #include "hast_proto.h"
58 child_cleanup(struct hast_resource *res)
61 proto_close(res->hr_ctrl);
63 if (res->hr_event != NULL) {
64 proto_close(res->hr_event);
67 if (res->hr_conn != NULL) {
68 proto_close(res->hr_conn);
71 res->hr_workerpid = 0;
75 control_set_role_common(struct hastd_config *cfg, struct nv *nvout,
76 uint8_t role, struct hast_resource *res, const char *name, unsigned int no)
80 /* Name is always needed. */
82 nv_add_string(nvout, name, "resource%u", no);
88 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
89 if (strcmp(res->hr_name, name) == 0)
93 nv_add_int16(nvout, EHAST_NOENTRY, "error%u", no);
99 /* Send previous role back. */
100 nv_add_string(nvout, role2str(res->hr_role), "role%u", no);
102 /* Nothing changed, return here. */
103 if (role == res->hr_role)
106 pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
107 pjdlog_info("Role changed to %s.", role2str(role));
109 /* Change role to the new one. */
110 oldrole = res->hr_role;
112 pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
115 * If previous role was primary or secondary we have to kill process
118 if (res->hr_workerpid != 0) {
119 if (kill(res->hr_workerpid, SIGTERM) < 0) {
120 pjdlog_errno(LOG_WARNING,
121 "Unable to kill worker process %u",
122 (unsigned int)res->hr_workerpid);
123 } else if (waitpid(res->hr_workerpid, NULL, 0) !=
125 pjdlog_errno(LOG_WARNING,
126 "Error while waiting for worker process %u",
127 (unsigned int)res->hr_workerpid);
129 pjdlog_debug(1, "Worker process %u stopped.",
130 (unsigned int)res->hr_workerpid);
135 /* Start worker process if we are changing to primary. */
136 if (role == HAST_ROLE_PRIMARY)
138 pjdlog_prefix_set("%s", "");
139 hook_exec(res->hr_exec, "role", res->hr_name, role2str(oldrole),
140 role2str(res->hr_role), NULL);
144 control_set_role(struct hast_resource *res, uint8_t role)
147 control_set_role_common(NULL, NULL, role, res, NULL, 0);
151 control_status_worker(struct hast_resource *res, struct nv *nvout,
154 struct nv *cnvin, *cnvout;
158 cnvin = cnvout = NULL;
162 * Prepare and send command to worker process.
165 nv_add_uint8(cnvout, CONTROL_STATUS, "cmd");
166 error = nv_error(cnvout);
168 pjdlog_common(LOG_ERR, 0, error,
169 "Unable to prepare control header");
172 if (hast_proto_send(res, res->hr_ctrl, cnvout, NULL, 0) < 0) {
174 pjdlog_errno(LOG_ERR, "Unable to send control header");
181 if (hast_proto_recv_hdr(res->hr_ctrl, &cnvin) < 0) {
183 pjdlog_errno(LOG_ERR, "Unable to receive control header");
187 error = nv_get_int16(cnvin, "error");
191 if ((str = nv_get_string(cnvin, "status")) == NULL) {
193 pjdlog_errno(LOG_ERR, "Field 'status' is missing.");
196 nv_add_string(nvout, str, "status%u", no);
197 nv_add_uint64(nvout, nv_get_uint64(cnvin, "dirty"), "dirty%u", no);
198 nv_add_uint32(nvout, nv_get_uint32(cnvin, "extentsize"),
200 nv_add_uint32(nvout, nv_get_uint32(cnvin, "keepdirty"),
208 nv_add_int16(nvout, error, "error");
212 control_status(struct hastd_config *cfg, struct nv *nvout,
213 struct hast_resource *res, const char *name, unsigned int no)
217 assert(nvout != NULL);
218 assert(name != NULL);
220 /* Name is always needed. */
221 nv_add_string(nvout, name, "resource%u", no);
224 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
225 if (strcmp(res->hr_name, name) == 0)
229 nv_add_int16(nvout, EHAST_NOENTRY, "error%u", no);
234 nv_add_string(nvout, res->hr_provname, "provname%u", no);
235 nv_add_string(nvout, res->hr_localpath, "localpath%u", no);
236 nv_add_string(nvout, res->hr_remoteaddr, "remoteaddr%u", no);
237 if (res->hr_sourceaddr[0] != '\0')
238 nv_add_string(nvout, res->hr_sourceaddr, "sourceaddr%u", no);
239 switch (res->hr_replication) {
240 case HAST_REPLICATION_FULLSYNC:
241 nv_add_string(nvout, "fullsync", "replication%u", no);
243 case HAST_REPLICATION_MEMSYNC:
244 nv_add_string(nvout, "memsync", "replication%u", no);
246 case HAST_REPLICATION_ASYNC:
247 nv_add_string(nvout, "async", "replication%u", no);
250 nv_add_string(nvout, "unknown", "replication%u", no);
253 nv_add_string(nvout, checksum_name(res->hr_checksum),
255 nv_add_string(nvout, compression_name(res->hr_compression),
256 "compression%u", no);
257 nv_add_string(nvout, role2str(res->hr_role), "role%u", no);
259 switch (res->hr_role) {
260 case HAST_ROLE_PRIMARY:
261 assert(res->hr_workerpid != 0);
263 case HAST_ROLE_SECONDARY:
264 if (res->hr_workerpid != 0)
272 * If we are here, it means that we have a worker process, which we
273 * want to ask some questions.
275 control_status_worker(res, nvout, no);
279 control_handle(struct hastd_config *cfg)
281 struct proto_conn *conn;
282 struct nv *nvin, *nvout;
288 if (proto_accept(cfg->hc_controlconn, &conn) < 0) {
289 pjdlog_errno(LOG_ERR, "Unable to accept control connection");
293 cfg->hc_controlin = conn;
295 role = HAST_ROLE_UNDEF;
297 if (hast_proto_recv_hdr(conn, &nvin) < 0) {
298 pjdlog_errno(LOG_ERR, "Unable to receive control header");
303 /* Obtain command code. 0 means that nv_get_uint8() failed. */
304 cmd = nv_get_uint8(nvin, "cmd");
306 pjdlog_error("Control header is missing 'cmd' field.");
307 error = EHAST_INVALID;
311 /* Allocate outgoing nv structure. */
314 pjdlog_error("Unable to allocate header for control response.");
315 error = EHAST_NOMEMORY;
321 str = nv_get_string(nvin, "resource0");
323 pjdlog_error("Control header is missing 'resource0' field.");
324 error = EHAST_INVALID;
327 if (cmd == HASTCTL_CMD_SETROLE) {
328 role = nv_get_uint8(nvin, "role");
331 case HAST_ROLE_PRIMARY:
332 case HAST_ROLE_SECONDARY:
335 pjdlog_error("Invalid role received (%hhu).", role);
336 error = EHAST_INVALID;
340 if (strcmp(str, "all") == 0) {
341 struct hast_resource *res;
343 /* All configured resources. */
346 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
348 case HASTCTL_CMD_SETROLE:
349 control_set_role_common(cfg, nvout, role, res,
352 case HASTCTL_CMD_STATUS:
353 control_status(cfg, nvout, res, res->hr_name,
357 pjdlog_error("Invalid command received (%hhu).",
359 error = EHAST_UNIMPLEMENTED;
364 /* Only selected resources. */
366 for (ii = 0; ; ii++) {
367 str = nv_get_string(nvin, "resource%u", ii);
371 case HASTCTL_CMD_SETROLE:
372 control_set_role_common(cfg, nvout, role, NULL,
375 case HASTCTL_CMD_STATUS:
376 control_status(cfg, nvout, NULL, str, ii);
379 pjdlog_error("Invalid command received (%hhu).",
381 error = EHAST_UNIMPLEMENTED;
386 if (nv_error(nvout) != 0)
390 nv_add_int16(nvout, error, "error");
392 if (hast_proto_send(NULL, conn, nvout, NULL, 0) < 0)
393 pjdlog_errno(LOG_ERR, "Unable to send control response");
400 cfg->hc_controlin = NULL;
404 * Thread handles control requests from the parent.
407 ctrl_thread(void *arg)
409 struct hast_resource *res = arg;
410 struct nv *nvin, *nvout;
414 if (hast_proto_recv_hdr(res->hr_ctrl, &nvin) < 0) {
415 if (sigexit_received)
417 pjdlog_errno(LOG_ERR,
418 "Unable to receive control message");
419 kill(getpid(), SIGTERM);
422 cmd = nv_get_uint8(nvin, "cmd");
424 pjdlog_error("Control message is missing 'cmd' field.");
431 if (res->hr_remotein != NULL &&
432 res->hr_remoteout != NULL) {
433 nv_add_string(nvout, "complete", "status");
435 nv_add_string(nvout, "degraded", "status");
437 nv_add_uint32(nvout, (uint32_t)res->hr_extentsize,
439 if (res->hr_role == HAST_ROLE_PRIMARY) {
441 (uint32_t)res->hr_keepdirty, "keepdirty");
443 (uint64_t)(activemap_ndirty(res->hr_amp) *
444 res->hr_extentsize), "dirty");
446 nv_add_uint32(nvout, (uint32_t)0, "keepdirty");
447 nv_add_uint64(nvout, (uint64_t)0, "dirty");
449 nv_add_int16(nvout, 0, "error");
453 * When parent receives SIGHUP and discovers that
454 * something related to us has changes, it sends reload
457 assert(res->hr_role == HAST_ROLE_PRIMARY);
458 primary_config_reload(res, nvin);
459 nv_add_int16(nvout, 0, "error");
462 nv_add_int16(nvout, EINVAL, "error");
466 if (nv_error(nvout) != 0) {
467 pjdlog_error("Unable to create answer on control message.");
471 if (hast_proto_send(NULL, res->hr_ctrl, nvout, NULL, 0) < 0) {
472 pjdlog_errno(LOG_ERR,
473 "Unable to send reply to control message");