control.c revision 219354
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: head/sbin/hastd/control.c 219354 2011-03-06 23:09:33Z pjd $");
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
57void
58child_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
74static void
75control_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
143void
144control_set_role(struct hast_resource *res, uint8_t role)
145{
146
147	control_set_role_common(NULL, NULL, role, res, NULL, 0);
148}
149
150static void
151control_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);
202end:
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
211static void
212control_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	switch (res->hr_replication) {
238	case HAST_REPLICATION_FULLSYNC:
239		nv_add_string(nvout, "fullsync", "replication%u", no);
240		break;
241	case HAST_REPLICATION_MEMSYNC:
242		nv_add_string(nvout, "memsync", "replication%u", no);
243		break;
244	case HAST_REPLICATION_ASYNC:
245		nv_add_string(nvout, "async", "replication%u", no);
246		break;
247	default:
248		nv_add_string(nvout, "unknown", "replication%u", no);
249		break;
250	}
251	nv_add_string(nvout, checksum_name(res->hr_checksum),
252	    "checksum%u", no);
253	nv_add_string(nvout, compression_name(res->hr_compression),
254	    "compression%u", no);
255	nv_add_string(nvout, role2str(res->hr_role), "role%u", no);
256
257	switch (res->hr_role) {
258	case HAST_ROLE_PRIMARY:
259		assert(res->hr_workerpid != 0);
260		/* FALLTHROUGH */
261	case HAST_ROLE_SECONDARY:
262		if (res->hr_workerpid != 0)
263			break;
264		/* FALLTHROUGH */
265	default:
266		return;
267	}
268
269	/*
270	 * If we are here, it means that we have a worker process, which we
271	 * want to ask some questions.
272	 */
273	control_status_worker(res, nvout, no);
274}
275
276void
277control_handle(struct hastd_config *cfg)
278{
279	struct proto_conn *conn;
280	struct nv *nvin, *nvout;
281	unsigned int ii;
282	const char *str;
283	uint8_t cmd, role;
284	int error;
285
286	if (proto_accept(cfg->hc_controlconn, &conn) < 0) {
287		pjdlog_errno(LOG_ERR, "Unable to accept control connection");
288		return;
289	}
290
291	cfg->hc_controlin = conn;
292	nvin = nvout = NULL;
293	role = HAST_ROLE_UNDEF;
294
295	if (hast_proto_recv_hdr(conn, &nvin) < 0) {
296		pjdlog_errno(LOG_ERR, "Unable to receive control header");
297		nvin = NULL;
298		goto close;
299	}
300
301	/* Obtain command code. 0 means that nv_get_uint8() failed. */
302	cmd = nv_get_uint8(nvin, "cmd");
303	if (cmd == 0) {
304		pjdlog_error("Control header is missing 'cmd' field.");
305		error = EHAST_INVALID;
306		goto close;
307	}
308
309	/* Allocate outgoing nv structure. */
310	nvout = nv_alloc();
311	if (nvout == NULL) {
312		pjdlog_error("Unable to allocate header for control response.");
313		error = EHAST_NOMEMORY;
314		goto close;
315	}
316
317	error = 0;
318
319	str = nv_get_string(nvin, "resource0");
320	if (str == NULL) {
321		pjdlog_error("Control header is missing 'resource0' field.");
322		error = EHAST_INVALID;
323		goto fail;
324	}
325	if (cmd == HASTCTL_SET_ROLE) {
326		role = nv_get_uint8(nvin, "role");
327		switch (role) {
328		case HAST_ROLE_INIT:	/* Is that valid to set, hmm? */
329		case HAST_ROLE_PRIMARY:
330		case HAST_ROLE_SECONDARY:
331			break;
332		default:
333			pjdlog_error("Invalid role received (%hhu).", role);
334			error = EHAST_INVALID;
335			goto fail;
336		}
337	}
338	if (strcmp(str, "all") == 0) {
339		struct hast_resource *res;
340
341		/* All configured resources. */
342
343		ii = 0;
344		TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
345			switch (cmd) {
346			case HASTCTL_SET_ROLE:
347				control_set_role_common(cfg, nvout, role, res,
348				    res->hr_name, ii++);
349				break;
350			case HASTCTL_STATUS:
351				control_status(cfg, nvout, res, res->hr_name,
352				    ii++);
353				break;
354			default:
355				pjdlog_error("Invalid command received (%hhu).",
356				    cmd);
357				error = EHAST_UNIMPLEMENTED;
358				goto fail;
359			}
360		}
361	} else {
362		/* Only selected resources. */
363
364		for (ii = 0; ; ii++) {
365			str = nv_get_string(nvin, "resource%u", ii);
366			if (str == NULL)
367				break;
368			switch (cmd) {
369			case HASTCTL_SET_ROLE:
370				control_set_role_common(cfg, nvout, role, NULL,
371				    str, ii);
372				break;
373			case HASTCTL_STATUS:
374				control_status(cfg, nvout, NULL, str, ii);
375				break;
376			default:
377				pjdlog_error("Invalid command received (%hhu).",
378				    cmd);
379				error = EHAST_UNIMPLEMENTED;
380				goto fail;
381			}
382		}
383	}
384	if (nv_error(nvout) != 0)
385		goto close;
386fail:
387	if (error != 0)
388		nv_add_int16(nvout, error, "error");
389
390	if (hast_proto_send(NULL, conn, nvout, NULL, 0) < 0)
391		pjdlog_errno(LOG_ERR, "Unable to send control response");
392close:
393	if (nvin != NULL)
394		nv_free(nvin);
395	if (nvout != NULL)
396		nv_free(nvout);
397	proto_close(conn);
398	cfg->hc_controlin = NULL;
399}
400
401/*
402 * Thread handles control requests from the parent.
403 */
404void *
405ctrl_thread(void *arg)
406{
407	struct hast_resource *res = arg;
408	struct nv *nvin, *nvout;
409	uint8_t cmd;
410
411	for (;;) {
412		if (hast_proto_recv_hdr(res->hr_ctrl, &nvin) < 0) {
413			if (sigexit_received)
414				pthread_exit(NULL);
415			pjdlog_errno(LOG_ERR,
416			    "Unable to receive control message");
417			kill(getpid(), SIGTERM);
418			pthread_exit(NULL);
419		}
420		cmd = nv_get_uint8(nvin, "cmd");
421		if (cmd == 0) {
422			pjdlog_error("Control message is missing 'cmd' field.");
423			nv_free(nvin);
424			continue;
425		}
426		nvout = nv_alloc();
427		switch (cmd) {
428		case HASTCTL_STATUS:
429			if (res->hr_remotein != NULL &&
430			    res->hr_remoteout != NULL) {
431				nv_add_string(nvout, "complete", "status");
432			} else {
433				nv_add_string(nvout, "degraded", "status");
434			}
435			nv_add_uint32(nvout, (uint32_t)res->hr_extentsize,
436			    "extentsize");
437			if (res->hr_role == HAST_ROLE_PRIMARY) {
438				nv_add_uint32(nvout,
439				    (uint32_t)res->hr_keepdirty, "keepdirty");
440				nv_add_uint64(nvout,
441				    (uint64_t)(activemap_ndirty(res->hr_amp) *
442				    res->hr_extentsize), "dirty");
443			} else {
444				nv_add_uint32(nvout, (uint32_t)0, "keepdirty");
445				nv_add_uint64(nvout, (uint64_t)0, "dirty");
446			}
447			nv_add_int16(nvout, 0, "error");
448			break;
449		case HASTCTL_RELOAD:
450			/*
451			 * When parent receives SIGHUP and discovers that
452			 * something related to us has changes, it sends reload
453			 * message to us.
454			 */
455			assert(res->hr_role == HAST_ROLE_PRIMARY);
456			primary_config_reload(res, nvin);
457			nv_add_int16(nvout, 0, "error");
458			break;
459		default:
460			nv_add_int16(nvout, EINVAL, "error");
461			break;
462		}
463		nv_free(nvin);
464		if (nv_error(nvout) != 0) {
465			pjdlog_error("Unable to create answer on control message.");
466			nv_free(nvout);
467			continue;
468		}
469		if (hast_proto_send(NULL, res->hr_ctrl, nvout, NULL, 0) < 0) {
470			pjdlog_errno(LOG_ERR,
471			    "Unable to send reply to control message");
472		}
473		nv_free(nvout);
474	}
475	/* NOTREACHED */
476	return (NULL);
477}
478