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_proto.h"
56 child_cleanup(struct hast_resource *res)
59 proto_close(res->hr_ctrl);
61 if (res->hr_event != NULL) {
62 proto_close(res->hr_event);
65 res->hr_workerpid = 0;
69 control_set_role_common(struct hastd_config *cfg, struct nv *nvout,
70 uint8_t role, struct hast_resource *res, const char *name, unsigned int no)
74 /* Name is always needed. */
76 nv_add_string(nvout, name, "resource%u", no);
82 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
83 if (strcmp(res->hr_name, name) == 0)
87 nv_add_int16(nvout, EHAST_NOENTRY, "error%u", no);
93 /* Send previous role back. */
94 nv_add_string(nvout, role2str(res->hr_role), "role%u", no);
96 /* Nothing changed, return here. */
97 if (role == res->hr_role)
100 pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
101 pjdlog_info("Role changed to %s.", role2str(role));
103 /* Change role to the new one. */
104 oldrole = res->hr_role;
106 pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
109 * If previous role was primary or secondary we have to kill process
112 if (res->hr_workerpid != 0) {
113 if (kill(res->hr_workerpid, SIGTERM) < 0) {
114 pjdlog_errno(LOG_WARNING,
115 "Unable to kill worker process %u",
116 (unsigned int)res->hr_workerpid);
117 } else if (waitpid(res->hr_workerpid, NULL, 0) !=
119 pjdlog_errno(LOG_WARNING,
120 "Error while waiting for worker process %u",
121 (unsigned int)res->hr_workerpid);
123 pjdlog_debug(1, "Worker process %u stopped.",
124 (unsigned int)res->hr_workerpid);
129 /* Start worker process if we are changing to primary. */
130 if (role == HAST_ROLE_PRIMARY)
132 pjdlog_prefix_set("%s", "");
133 hook_exec(res->hr_exec, "role", res->hr_name, role2str(oldrole),
134 role2str(res->hr_role), NULL);
138 control_set_role(struct hast_resource *res, uint8_t role)
141 control_set_role_common(NULL, NULL, role, res, NULL, 0);
145 control_status_worker(struct hast_resource *res, struct nv *nvout,
148 struct nv *cnvin, *cnvout;
152 cnvin = cnvout = NULL;
156 * Prepare and send command to worker process.
159 nv_add_uint8(cnvout, HASTCTL_STATUS, "cmd");
160 error = nv_error(cnvout);
165 if (hast_proto_send(res, res->hr_ctrl, cnvout, NULL, 0) < 0) {
174 if (hast_proto_recv_hdr(res->hr_ctrl, &cnvin) < 0) {
180 error = nv_get_int64(cnvin, "error");
184 if ((str = nv_get_string(cnvin, "status")) == NULL) {
189 nv_add_string(nvout, str, "status%u", no);
190 nv_add_uint64(nvout, nv_get_uint64(cnvin, "dirty"), "dirty%u", no);
191 nv_add_uint32(nvout, nv_get_uint32(cnvin, "extentsize"),
193 nv_add_uint32(nvout, nv_get_uint32(cnvin, "keepdirty"),
201 nv_add_int16(nvout, error, "error");
205 control_status(struct hastd_config *cfg, struct nv *nvout,
206 struct hast_resource *res, const char *name, unsigned int no)
210 assert(nvout != NULL);
211 assert(name != NULL);
213 /* Name is always needed. */
214 nv_add_string(nvout, name, "resource%u", no);
217 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
218 if (strcmp(res->hr_name, name) == 0)
222 nv_add_int16(nvout, EHAST_NOENTRY, "error%u", no);
227 nv_add_string(nvout, res->hr_provname, "provname%u", no);
228 nv_add_string(nvout, res->hr_localpath, "localpath%u", no);
229 nv_add_string(nvout, res->hr_remoteaddr, "remoteaddr%u", no);
230 switch (res->hr_replication) {
231 case HAST_REPLICATION_FULLSYNC:
232 nv_add_string(nvout, "fullsync", "replication%u", no);
234 case HAST_REPLICATION_MEMSYNC:
235 nv_add_string(nvout, "memsync", "replication%u", no);
237 case HAST_REPLICATION_ASYNC:
238 nv_add_string(nvout, "async", "replication%u", no);
241 nv_add_string(nvout, "unknown", "replication%u", no);
244 nv_add_string(nvout, role2str(res->hr_role), "role%u", no);
246 switch (res->hr_role) {
247 case HAST_ROLE_PRIMARY:
248 assert(res->hr_workerpid != 0);
250 case HAST_ROLE_SECONDARY:
251 if (res->hr_workerpid != 0)
259 * If we are here, it means that we have a worker process, which we
260 * want to ask some questions.
262 control_status_worker(res, nvout, no);
266 control_handle(struct hastd_config *cfg)
268 struct proto_conn *conn;
269 struct nv *nvin, *nvout;
275 if (proto_accept(cfg->hc_controlconn, &conn) < 0) {
276 pjdlog_errno(LOG_ERR, "Unable to accept control connection");
281 role = HAST_ROLE_UNDEF;
283 if (hast_proto_recv_hdr(conn, &nvin) < 0) {
284 pjdlog_errno(LOG_ERR, "Unable to receive control header");
289 /* Obtain command code. 0 means that nv_get_uint8() failed. */
290 cmd = nv_get_uint8(nvin, "cmd");
292 pjdlog_error("Control header is missing 'cmd' field.");
293 error = EHAST_INVALID;
297 /* Allocate outgoing nv structure. */
300 pjdlog_error("Unable to allocate header for control response.");
301 error = EHAST_NOMEMORY;
307 str = nv_get_string(nvin, "resource0");
309 pjdlog_error("Control header is missing 'resource0' field.");
310 error = EHAST_INVALID;
313 if (cmd == HASTCTL_SET_ROLE) {
314 role = nv_get_uint8(nvin, "role");
316 case HAST_ROLE_INIT: /* Is that valid to set, hmm? */
317 case HAST_ROLE_PRIMARY:
318 case HAST_ROLE_SECONDARY:
321 pjdlog_error("Invalid role received (%hhu).", role);
322 error = EHAST_INVALID;
326 if (strcmp(str, "all") == 0) {
327 struct hast_resource *res;
329 /* All configured resources. */
332 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
334 case HASTCTL_SET_ROLE:
335 control_set_role_common(cfg, nvout, role, res,
339 control_status(cfg, nvout, res, res->hr_name,
343 pjdlog_error("Invalid command received (%hhu).",
345 error = EHAST_UNIMPLEMENTED;
350 /* Only selected resources. */
352 for (ii = 0; ; ii++) {
353 str = nv_get_string(nvin, "resource%u", ii);
357 case HASTCTL_SET_ROLE:
358 control_set_role_common(cfg, nvout, role, NULL,
362 control_status(cfg, nvout, NULL, str, ii);
365 pjdlog_error("Invalid command received (%hhu).",
367 error = EHAST_UNIMPLEMENTED;
372 if (nv_error(nvout) != 0)
376 nv_add_int16(nvout, error, "error");
378 if (hast_proto_send(NULL, conn, nvout, NULL, 0) < 0)
379 pjdlog_errno(LOG_ERR, "Unable to send control response");
389 * Thread handles control requests from the parent.
392 ctrl_thread(void *arg)
394 struct hast_resource *res = arg;
395 struct nv *nvin, *nvout;
399 if (hast_proto_recv_hdr(res->hr_ctrl, &nvin) < 0) {
400 if (sigexit_received)
402 pjdlog_errno(LOG_ERR,
403 "Unable to receive control message");
404 kill(getpid(), SIGTERM);
407 cmd = nv_get_uint8(nvin, "cmd");
409 pjdlog_error("Control message is missing 'cmd' field.");
417 if (res->hr_remotein != NULL &&
418 res->hr_remoteout != NULL) {
419 nv_add_string(nvout, "complete", "status");
421 nv_add_string(nvout, "degraded", "status");
423 nv_add_uint32(nvout, (uint32_t)res->hr_extentsize,
425 if (res->hr_role == HAST_ROLE_PRIMARY) {
427 (uint32_t)res->hr_keepdirty, "keepdirty");
429 (uint64_t)(activemap_ndirty(res->hr_amp) *
430 res->hr_extentsize), "dirty");
432 nv_add_uint32(nvout, (uint32_t)0, "keepdirty");
433 nv_add_uint64(nvout, (uint64_t)0, "dirty");
437 nv_add_int16(nvout, EINVAL, "error");
440 if (nv_error(nvout) != 0) {
441 pjdlog_error("Unable to create answer on control message.");
445 if (hast_proto_send(NULL, res->hr_ctrl, nvout, NULL, 0) < 0) {
446 pjdlog_errno(LOG_ERR,
447 "Unable to send reply to control message");