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