1 /*- 2 * Copyright (c) 2009-2010 The FreeBSD Foundation 3 * All rights reserved. 4 * 5 * This software was developed by Pawel Jakub Dawidek under sponsorship from 6 * the FreeBSD Foundation. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 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. 16 * 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 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <sys/types.h> 34 #include <sys/wait.h> 35 36 #include <assert.h> 37 #include <errno.h> 38 #include <pthread.h> 39 #include <signal.h> 40 #include <stdio.h> 41 #include <string.h> 42 #include <unistd.h> 43 44 #include "hast.h" 45 #include "hastd.h" 46 #include "hast_proto.h" 47 #include "hooks.h" 48 #include "nv.h" 49 #include "pjdlog.h" 50 #include "proto.h" 51 #include "subr.h" 52 53 #include "control.h" 54 55 void 56 child_cleanup(struct hast_resource *res) 57 { 58 59 proto_close(res->hr_ctrl); 60 res->hr_ctrl = NULL; 61 if (res->hr_event != NULL) { 62 proto_close(res->hr_event); 63 res->hr_event = NULL; 64 } 65 if (res->hr_conn != NULL) { 66 proto_close(res->hr_conn); 67 res->hr_conn = NULL; 68 } 69 res->hr_workerpid = 0; 70 } 71 72 static void 73 control_set_role_common(struct hastd_config *cfg, struct nv *nvout, 74 uint8_t role, struct hast_resource *res, const char *name, unsigned int no) 75 { 76 int oldrole; 77 78 /* Name is always needed. */ 79 if (name != NULL) 80 nv_add_string(nvout, name, "resource%u", no); 81 82 if (res == NULL) { 83 assert(cfg != NULL); 84 assert(name != NULL); 85 86 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) { 87 if (strcmp(res->hr_name, name) == 0) 88 break; 89 } 90 if (res == NULL) { 91 nv_add_int16(nvout, EHAST_NOENTRY, "error%u", no); 92 return; 93 } 94 } 95 assert(res != NULL); 96 97 /* Send previous role back. */ 98 nv_add_string(nvout, role2str(res->hr_role), "role%u", no); 99 100 /* Nothing changed, return here. */ 101 if (role == res->hr_role) 102 return; 103 104 pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role)); 105 pjdlog_info("Role changed to %s.", role2str(role)); 106 107 /* Change role to the new one. */ 108 oldrole = res->hr_role; 109 res->hr_role = role; 110 pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role)); 111 112 /* 113 * If previous role was primary or secondary we have to kill process 114 * doing that work. 115 */ 116 if (res->hr_workerpid != 0) { 117 if (kill(res->hr_workerpid, SIGTERM) < 0) { 118 pjdlog_errno(LOG_WARNING, 119 "Unable to kill worker process %u", 120 (unsigned int)res->hr_workerpid); 121 } else if (waitpid(res->hr_workerpid, NULL, 0) != 122 res->hr_workerpid) { 123 pjdlog_errno(LOG_WARNING, 124 "Error while waiting for worker process %u", 125 (unsigned int)res->hr_workerpid); 126 } else { 127 pjdlog_debug(1, "Worker process %u stopped.", 128 (unsigned int)res->hr_workerpid); 129 } 130 child_cleanup(res); 131 } 132 133 /* Start worker process if we are changing to primary. */ 134 if (role == HAST_ROLE_PRIMARY) 135 hastd_primary(res); 136 pjdlog_prefix_set("%s", ""); 137 hook_exec(res->hr_exec, "role", res->hr_name, role2str(oldrole), 138 role2str(res->hr_role), NULL); 139 } 140 141 void 142 control_set_role(struct hast_resource *res, uint8_t role) 143 { 144 145 control_set_role_common(NULL, NULL, role, res, NULL, 0); 146 } 147 148 static void 149 control_status_worker(struct hast_resource *res, struct nv *nvout, 150 unsigned int no) 151 { 152 struct nv *cnvin, *cnvout; 153 const char *str; 154 int error; 155 156 cnvin = cnvout = NULL; 157 error = 0; 158 159 /* 160 * Prepare and send command to worker process. 161 */ 162 cnvout = nv_alloc(); 163 nv_add_uint8(cnvout, HASTCTL_STATUS, "cmd"); 164 error = nv_error(cnvout); 165 if (error != 0) { 166 pjdlog_common(LOG_ERR, 0, error, 167 "Unable to prepare control header"); 168 goto end; 169 } 170 if (hast_proto_send(res, res->hr_ctrl, cnvout, NULL, 0) < 0) { 171 error = errno; 172 pjdlog_errno(LOG_ERR, "Unable to send control header"); 173 goto end; 174 } 175 176 /* 177 * Receive response. 178 */ 179 if (hast_proto_recv_hdr(res->hr_ctrl, &cnvin) < 0) { 180 error = errno; 181 pjdlog_errno(LOG_ERR, "Unable to receive control header"); 182 goto end; 183 } 184 185 error = nv_get_int16(cnvin, "error"); 186 if (error != 0) 187 goto end; 188 189 if ((str = nv_get_string(cnvin, "status")) == NULL) { 190 error = ENOENT; 191 pjdlog_errno(LOG_ERR, "Field 'status' is missing."); 192 goto end; 193 } 194 nv_add_string(nvout, str, "status%u", no); 195 nv_add_uint64(nvout, nv_get_uint64(cnvin, "dirty"), "dirty%u", no); 196 nv_add_uint32(nvout, nv_get_uint32(cnvin, "extentsize"), 197 "extentsize%u", no); 198 nv_add_uint32(nvout, nv_get_uint32(cnvin, "keepdirty"), 199 "keepdirty%u", no); 200 end: 201 if (cnvin != NULL) 202 nv_free(cnvin); 203 if (cnvout != NULL) 204 nv_free(cnvout); 205 if (error != 0) 206 nv_add_int16(nvout, error, "error"); 207 } 208 209 static void 210 control_status(struct hastd_config *cfg, struct nv *nvout, 211 struct hast_resource *res, const char *name, unsigned int no) 212 { 213 214 assert(cfg != NULL); 215 assert(nvout != NULL); 216 assert(name != NULL); 217 218 /* Name is always needed. */ 219 nv_add_string(nvout, name, "resource%u", no); 220 221 if (res == NULL) { 222 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) { 223 if (strcmp(res->hr_name, name) == 0) 224 break; 225 } 226 if (res == NULL) { 227 nv_add_int16(nvout, EHAST_NOENTRY, "error%u", no); 228 return; 229 } 230 } 231 assert(res != NULL); 232 nv_add_string(nvout, res->hr_provname, "provname%u", no); 233 nv_add_string(nvout, res->hr_localpath, "localpath%u", no); 234 nv_add_string(nvout, res->hr_remoteaddr, "remoteaddr%u", no); 235 switch (res->hr_replication) { 236 case HAST_REPLICATION_FULLSYNC: 237 nv_add_string(nvout, "fullsync", "replication%u", no); 238 break; 239 case HAST_REPLICATION_MEMSYNC: 240 nv_add_string(nvout, "memsync", "replication%u", no); 241 break; 242 case HAST_REPLICATION_ASYNC: 243 nv_add_string(nvout, "async", "replication%u", no); 244 break; 245 default: 246 nv_add_string(nvout, "unknown", "replication%u", no); 247 break; 248 } 249 nv_add_string(nvout, role2str(res->hr_role), "role%u", no); 250 251 switch (res->hr_role) { 252 case HAST_ROLE_PRIMARY: 253 assert(res->hr_workerpid != 0); 254 /* FALLTHROUGH */ 255 case HAST_ROLE_SECONDARY: 256 if (res->hr_workerpid != 0) 257 break; 258 /* FALLTHROUGH */ 259 default: 260 return; 261 } 262 263 /* 264 * If we are here, it means that we have a worker process, which we 265 * want to ask some questions. 266 */ 267 control_status_worker(res, nvout, no); 268 } 269 270 void 271 control_handle(struct hastd_config *cfg) 272 { 273 struct proto_conn *conn; 274 struct nv *nvin, *nvout; 275 unsigned int ii; 276 const char *str; 277 uint8_t cmd, role; 278 int error; 279 280 if (proto_accept(cfg->hc_controlconn, &conn) < 0) { 281 pjdlog_errno(LOG_ERR, "Unable to accept control connection"); 282 return; 283 } 284 285 cfg->hc_controlin = conn; 286 nvin = nvout = NULL; 287 role = HAST_ROLE_UNDEF; 288 289 if (hast_proto_recv_hdr(conn, &nvin) < 0) { 290 pjdlog_errno(LOG_ERR, "Unable to receive control header"); 291 nvin = NULL; 292 goto close; 293 } 294 295 /* Obtain command code. 0 means that nv_get_uint8() failed. */ 296 cmd = nv_get_uint8(nvin, "cmd"); 297 if (cmd == 0) { 298 pjdlog_error("Control header is missing 'cmd' field."); 299 error = EHAST_INVALID; 300 goto close; 301 } 302 303 /* Allocate outgoing nv structure. */ 304 nvout = nv_alloc(); 305 if (nvout == NULL) { 306 pjdlog_error("Unable to allocate header for control response."); 307 error = EHAST_NOMEMORY; 308 goto close; 309 } 310 311 error = 0; 312 313 str = nv_get_string(nvin, "resource0"); 314 if (str == NULL) { 315 pjdlog_error("Control header is missing 'resource0' field."); 316 error = EHAST_INVALID; 317 goto fail; 318 } 319 if (cmd == HASTCTL_SET_ROLE) { 320 role = nv_get_uint8(nvin, "role"); 321 switch (role) { 322 case HAST_ROLE_INIT: /* Is that valid to set, hmm? */ 323 case HAST_ROLE_PRIMARY: 324 case HAST_ROLE_SECONDARY: 325 break; 326 default: 327 pjdlog_error("Invalid role received (%hhu).", role); 328 error = EHAST_INVALID; 329 goto fail; 330 } 331 } 332 if (strcmp(str, "all") == 0) { 333 struct hast_resource *res; 334 335 /* All configured resources. */ 336 337 ii = 0; 338 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) { 339 switch (cmd) { 340 case HASTCTL_SET_ROLE: 341 control_set_role_common(cfg, nvout, role, res, 342 res->hr_name, ii++); 343 break; 344 case HASTCTL_STATUS: 345 control_status(cfg, nvout, res, res->hr_name, 346 ii++); 347 break; 348 default: 349 pjdlog_error("Invalid command received (%hhu).", 350 cmd); 351 error = EHAST_UNIMPLEMENTED; 352 goto fail; 353 } 354 } 355 } else { 356 /* Only selected resources. */ 357 358 for (ii = 0; ; ii++) { 359 str = nv_get_string(nvin, "resource%u", ii); 360 if (str == NULL) 361 break; 362 switch (cmd) { 363 case HASTCTL_SET_ROLE: 364 control_set_role_common(cfg, nvout, role, NULL, 365 str, ii); 366 break; 367 case HASTCTL_STATUS: 368 control_status(cfg, nvout, NULL, str, ii); 369 break; 370 default: 371 pjdlog_error("Invalid command received (%hhu).", 372 cmd); 373 error = EHAST_UNIMPLEMENTED; 374 goto fail; 375 } 376 } 377 } 378 if (nv_error(nvout) != 0) 379 goto close; 380 fail: 381 if (error != 0) 382 nv_add_int16(nvout, error, "error"); 383 384 if (hast_proto_send(NULL, conn, nvout, NULL, 0) < 0) 385 pjdlog_errno(LOG_ERR, "Unable to send control response"); 386 close: 387 if (nvin != NULL) 388 nv_free(nvin); 389 if (nvout != NULL) 390 nv_free(nvout); 391 proto_close(conn); 392 cfg->hc_controlin = NULL; 393 } 394 395 /* 396 * Thread handles control requests from the parent. 397 */ 398 void * 399 ctrl_thread(void *arg) 400 { 401 struct hast_resource *res = arg; 402 struct nv *nvin, *nvout; 403 uint8_t cmd; 404 405 for (;;) { 406 if (hast_proto_recv_hdr(res->hr_ctrl, &nvin) < 0) { 407 if (sigexit_received) 408 pthread_exit(NULL); 409 pjdlog_errno(LOG_ERR, 410 "Unable to receive control message"); 411 kill(getpid(), SIGTERM); 412 pthread_exit(NULL); 413 } 414 cmd = nv_get_uint8(nvin, "cmd"); 415 if (cmd == 0) { 416 pjdlog_error("Control message is missing 'cmd' field."); 417 nv_free(nvin); 418 continue; 419 } 420 nvout = nv_alloc(); 421 switch (cmd) { 422 case HASTCTL_STATUS: 423 if (res->hr_remotein != NULL && 424 res->hr_remoteout != NULL) { 425 nv_add_string(nvout, "complete", "status"); 426 } else { 427 nv_add_string(nvout, "degraded", "status"); 428 } 429 nv_add_uint32(nvout, (uint32_t)res->hr_extentsize, 430 "extentsize"); 431 if (res->hr_role == HAST_ROLE_PRIMARY) { 432 nv_add_uint32(nvout, 433 (uint32_t)res->hr_keepdirty, "keepdirty"); 434 nv_add_uint64(nvout, 435 (uint64_t)(activemap_ndirty(res->hr_amp) * 436 res->hr_extentsize), "dirty"); 437 } else { 438 nv_add_uint32(nvout, (uint32_t)0, "keepdirty"); 439 nv_add_uint64(nvout, (uint64_t)0, "dirty"); 440 } 441 nv_add_int16(nvout, 0, "error"); 442 break; 443 case HASTCTL_RELOAD: 444 /* 445 * When parent receives SIGHUP and discovers that 446 * something related to us has changes, it sends reload 447 * message to us. 448 */ 449 assert(res->hr_role == HAST_ROLE_PRIMARY); 450 primary_config_reload(res, nvin); 451 nv_add_int16(nvout, 0, "error"); 452 break; 453 default: 454 nv_add_int16(nvout, EINVAL, "error"); 455 break; 456 } 457 nv_free(nvin); 458 if (nv_error(nvout) != 0) { 459 pjdlog_error("Unable to create answer on control message."); 460 nv_free(nvout); 461 continue; 462 } 463 if (hast_proto_send(NULL, res->hr_ctrl, nvout, NULL, 0) < 0) { 464 pjdlog_errno(LOG_ERR, 465 "Unable to send reply to control message"); 466 } 467 nv_free(nvout); 468 } 469 /* NOTREACHED */ 470 return (NULL); 471 } 472