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