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>
45 #include "hast_proto.h"
54 control_set_role(struct hastd_config *cfg, struct nv *nvout, uint8_t role,
55 struct hast_resource *res, const char *name, unsigned int no)
59 assert(nvout != NULL);
62 /* Name is always needed. */
63 nv_add_string(nvout, name, "resource%u", no);
66 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
67 if (strcmp(res->hr_name, name) == 0)
71 nv_add_int16(nvout, EHAST_NOENTRY, "error%u", no);
77 /* Send previous role back. */
78 nv_add_string(nvout, role2str(res->hr_role), "role%u", no);
80 /* Nothing changed, return here. */
81 if (role == res->hr_role)
84 pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
85 pjdlog_info("Role changed to %s.", role2str(role));
87 /* Change role to the new one. */
89 pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
92 * If previous role was primary or secondary we have to kill process
95 if (res->hr_workerpid != 0) {
96 if (kill(res->hr_workerpid, SIGTERM) < 0) {
97 pjdlog_errno(LOG_WARNING,
98 "Unable to kill worker process %u",
99 (unsigned int)res->hr_workerpid);
100 } else if (waitpid(res->hr_workerpid, NULL, 0) !=
102 pjdlog_errno(LOG_WARNING,
103 "Error while waiting for worker process %u",
104 (unsigned int)res->hr_workerpid);
106 pjdlog_debug(1, "Worker process %u stopped.",
107 (unsigned int)res->hr_workerpid);
109 res->hr_workerpid = 0;
112 /* Start worker process if we are changing to primary. */
113 if (role == HAST_ROLE_PRIMARY)
115 pjdlog_prefix_set("%s", "");
119 control_status_worker(struct hast_resource *res, struct nv *nvout,
122 struct nv *cnvin, *cnvout;
126 cnvin = cnvout = NULL;
130 * Prepare and send command to worker process.
133 nv_add_uint8(cnvout, HASTCTL_STATUS, "cmd");
134 error = nv_error(cnvout);
139 if (hast_proto_send(res, res->hr_ctrl, cnvout, NULL, 0) < 0) {
148 if (hast_proto_recv_hdr(res->hr_ctrl, &cnvin) < 0) {
154 error = nv_get_int64(cnvin, "error");
158 if ((str = nv_get_string(cnvin, "status")) == NULL) {
163 nv_add_string(nvout, str, "status%u", no);
164 nv_add_uint64(nvout, nv_get_uint64(cnvin, "dirty"), "dirty%u", no);
165 nv_add_uint32(nvout, nv_get_uint32(cnvin, "extentsize"),
167 nv_add_uint32(nvout, nv_get_uint32(cnvin, "keepdirty"),
175 nv_add_int16(nvout, error, "error");
179 control_status(struct hastd_config *cfg, struct nv *nvout,
180 struct hast_resource *res, const char *name, unsigned int no)
184 assert(nvout != NULL);
185 assert(name != NULL);
187 /* Name is always needed. */
188 nv_add_string(nvout, name, "resource%u", no);
191 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
192 if (strcmp(res->hr_name, name) == 0)
196 nv_add_int16(nvout, EHAST_NOENTRY, "error%u", no);
201 nv_add_string(nvout, res->hr_provname, "provname%u", no);
202 nv_add_string(nvout, res->hr_localpath, "localpath%u", no);
203 nv_add_string(nvout, res->hr_remoteaddr, "remoteaddr%u", no);
204 switch (res->hr_replication) {
205 case HAST_REPLICATION_FULLSYNC:
206 nv_add_string(nvout, "fullsync", "replication%u", no);
208 case HAST_REPLICATION_MEMSYNC:
209 nv_add_string(nvout, "memsync", "replication%u", no);
211 case HAST_REPLICATION_ASYNC:
212 nv_add_string(nvout, "async", "replication%u", no);
215 nv_add_string(nvout, "unknown", "replication%u", no);
218 nv_add_string(nvout, role2str(res->hr_role), "role%u", no);
220 switch (res->hr_role) {
221 case HAST_ROLE_PRIMARY:
222 assert(res->hr_workerpid != 0);
224 case HAST_ROLE_SECONDARY:
225 if (res->hr_workerpid != 0)
233 * If we are here, it means that we have a worker process, which we
234 * want to ask some questions.
236 control_status_worker(res, nvout, no);
240 control_handle(struct hastd_config *cfg)
242 struct proto_conn *conn;
243 struct nv *nvin, *nvout;
249 if (proto_accept(cfg->hc_controlconn, &conn) < 0) {
250 pjdlog_errno(LOG_ERR, "Unable to accept control connection");
255 role = HAST_ROLE_UNDEF;
257 if (hast_proto_recv_hdr(conn, &nvin) < 0) {
258 pjdlog_errno(LOG_ERR, "Unable to receive control header");
263 /* Obtain command code. 0 means that nv_get_uint8() failed. */
264 cmd = nv_get_uint8(nvin, "cmd");
266 pjdlog_error("Control header is missing 'cmd' field.");
267 error = EHAST_INVALID;
271 /* Allocate outgoing nv structure. */
274 pjdlog_error("Unable to allocate header for control response.");
275 error = EHAST_NOMEMORY;
281 str = nv_get_string(nvin, "resource0");
283 pjdlog_error("Control header is missing 'resource0' field.");
284 error = EHAST_INVALID;
287 if (cmd == HASTCTL_SET_ROLE) {
288 role = nv_get_uint8(nvin, "role");
290 case HAST_ROLE_INIT: /* Is that valid to set, hmm? */
291 case HAST_ROLE_PRIMARY:
292 case HAST_ROLE_SECONDARY:
295 pjdlog_error("Invalid role received (%hhu).", role);
296 error = EHAST_INVALID;
300 if (strcmp(str, "all") == 0) {
301 struct hast_resource *res;
303 /* All configured resources. */
306 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
308 case HASTCTL_SET_ROLE:
309 control_set_role(cfg, nvout, role, res,
313 control_status(cfg, nvout, res, res->hr_name,
317 pjdlog_error("Invalid command received (%hhu).",
319 error = EHAST_UNIMPLEMENTED;
324 /* Only selected resources. */
326 for (ii = 0; ; ii++) {
327 str = nv_get_string(nvin, "resource%u", ii);
331 case HASTCTL_SET_ROLE:
332 control_set_role(cfg, nvout, role, NULL, str,
336 control_status(cfg, nvout, NULL, str, ii);
339 pjdlog_error("Invalid command received (%hhu).",
341 error = EHAST_UNIMPLEMENTED;
346 if (nv_error(nvout) != 0)
350 nv_add_int16(nvout, error, "error");
352 if (hast_proto_send(NULL, conn, nvout, NULL, 0) < 0)
353 pjdlog_errno(LOG_ERR, "Unable to send control response");
363 * Thread handles control requests from the parent.
366 ctrl_thread(void *arg)
368 struct hast_resource *res = arg;
369 struct nv *nvin, *nvout;
373 if (hast_proto_recv_hdr(res->hr_ctrl, &nvin) < 0) {
374 if (sigexit_received)
376 pjdlog_errno(LOG_ERR,
377 "Unable to receive control message");
380 cmd = nv_get_uint8(nvin, "cmd");
382 pjdlog_error("Control message is missing 'cmd' field.");
390 if (res->hr_remotein != NULL &&
391 res->hr_remoteout != NULL) {
392 nv_add_string(nvout, "complete", "status");
394 nv_add_string(nvout, "degraded", "status");
396 nv_add_uint32(nvout, (uint32_t)res->hr_extentsize,
398 if (res->hr_role == HAST_ROLE_PRIMARY) {
400 (uint32_t)res->hr_keepdirty, "keepdirty");
402 (uint64_t)(activemap_ndirty(res->hr_amp) *
403 res->hr_extentsize), "dirty");
405 nv_add_uint32(nvout, (uint32_t)0, "keepdirty");
406 nv_add_uint64(nvout, (uint64_t)0, "dirty");
410 nv_add_int16(nvout, EINVAL, "error");
413 if (nv_error(nvout) != 0) {
414 pjdlog_error("Unable to create answer on control message.");
418 if (hast_proto_send(NULL, res->hr_ctrl, nvout, NULL, 0) < 0) {
419 pjdlog_errno(LOG_ERR,
420 "Unable to send reply to control message");