171c895ebSPawel Jakub Dawidek /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni *
471c895ebSPawel Jakub Dawidek * Copyright (c) 2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
571c895ebSPawel Jakub Dawidek * All rights reserved.
671c895ebSPawel Jakub Dawidek *
771c895ebSPawel Jakub Dawidek * Redistribution and use in source and binary forms, with or without
871c895ebSPawel Jakub Dawidek * modification, are permitted provided that the following conditions
971c895ebSPawel Jakub Dawidek * are met:
1071c895ebSPawel Jakub Dawidek * 1. Redistributions of source code must retain the above copyright
1171c895ebSPawel Jakub Dawidek * notice, this list of conditions and the following disclaimer.
1271c895ebSPawel Jakub Dawidek * 2. Redistributions in binary form must reproduce the above copyright
1371c895ebSPawel Jakub Dawidek * notice, this list of conditions and the following disclaimer in the
1471c895ebSPawel Jakub Dawidek * documentation and/or other materials provided with the distribution.
1571c895ebSPawel Jakub Dawidek *
1671c895ebSPawel Jakub Dawidek * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
1771c895ebSPawel Jakub Dawidek * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1871c895ebSPawel Jakub Dawidek * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1971c895ebSPawel Jakub Dawidek * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
2071c895ebSPawel Jakub Dawidek * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2171c895ebSPawel Jakub Dawidek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2271c895ebSPawel Jakub Dawidek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2371c895ebSPawel Jakub Dawidek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2471c895ebSPawel Jakub Dawidek * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2571c895ebSPawel Jakub Dawidek * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2671c895ebSPawel Jakub Dawidek * SUCH DAMAGE.
2771c895ebSPawel Jakub Dawidek */
2871c895ebSPawel Jakub Dawidek
2971c895ebSPawel Jakub Dawidek #include <sys/cdefs.h>
3071c895ebSPawel Jakub Dawidek #include <errno.h>
3171c895ebSPawel Jakub Dawidek
3271c895ebSPawel Jakub Dawidek #include "hast.h"
3371c895ebSPawel Jakub Dawidek #include "hast_proto.h"
3471c895ebSPawel Jakub Dawidek #include "hooks.h"
3571c895ebSPawel Jakub Dawidek #include "nv.h"
3671c895ebSPawel Jakub Dawidek #include "pjdlog.h"
3771c895ebSPawel Jakub Dawidek #include "proto.h"
3871c895ebSPawel Jakub Dawidek #include "subr.h"
3971c895ebSPawel Jakub Dawidek
4071c895ebSPawel Jakub Dawidek #include "event.h"
4171c895ebSPawel Jakub Dawidek
4271c895ebSPawel Jakub Dawidek void
event_send(const struct hast_resource * res,int event)4371c895ebSPawel Jakub Dawidek event_send(const struct hast_resource *res, int event)
4471c895ebSPawel Jakub Dawidek {
4571c895ebSPawel Jakub Dawidek struct nv *nvin, *nvout;
4671c895ebSPawel Jakub Dawidek int error;
4771c895ebSPawel Jakub Dawidek
48adf8002bSPawel Jakub Dawidek PJDLOG_ASSERT(res != NULL);
49adf8002bSPawel Jakub Dawidek PJDLOG_ASSERT(event >= EVENT_MIN && event <= EVENT_MAX);
5071c895ebSPawel Jakub Dawidek
5171c895ebSPawel Jakub Dawidek nvin = nvout = NULL;
5271c895ebSPawel Jakub Dawidek
5371c895ebSPawel Jakub Dawidek /*
5471c895ebSPawel Jakub Dawidek * Prepare and send event to parent process.
5571c895ebSPawel Jakub Dawidek */
5671c895ebSPawel Jakub Dawidek nvout = nv_alloc();
5771c895ebSPawel Jakub Dawidek nv_add_uint8(nvout, (uint8_t)event, "event");
5871c895ebSPawel Jakub Dawidek error = nv_error(nvout);
5971c895ebSPawel Jakub Dawidek if (error != 0) {
6071c895ebSPawel Jakub Dawidek pjdlog_common(LOG_ERR, 0, error,
6171c895ebSPawel Jakub Dawidek "Unable to prepare event header");
6271c895ebSPawel Jakub Dawidek goto done;
6371c895ebSPawel Jakub Dawidek }
642b1b224dSPawel Jakub Dawidek if (hast_proto_send(res, res->hr_event, nvout, NULL, 0) == -1) {
6571c895ebSPawel Jakub Dawidek pjdlog_errno(LOG_ERR, "Unable to send event header");
6671c895ebSPawel Jakub Dawidek goto done;
6771c895ebSPawel Jakub Dawidek }
682b1b224dSPawel Jakub Dawidek if (hast_proto_recv_hdr(res->hr_event, &nvin) == -1) {
6971c895ebSPawel Jakub Dawidek pjdlog_errno(LOG_ERR, "Unable to receive event header");
7071c895ebSPawel Jakub Dawidek goto done;
7171c895ebSPawel Jakub Dawidek }
7271c895ebSPawel Jakub Dawidek /*
7371c895ebSPawel Jakub Dawidek * Do nothing with the answer. We only wait for it to be sure not
7471c895ebSPawel Jakub Dawidek * to exit too quickly after sending an event and exiting immediately.
7571c895ebSPawel Jakub Dawidek */
7671c895ebSPawel Jakub Dawidek done:
7771c895ebSPawel Jakub Dawidek if (nvin != NULL)
7871c895ebSPawel Jakub Dawidek nv_free(nvin);
7971c895ebSPawel Jakub Dawidek if (nvout != NULL)
8071c895ebSPawel Jakub Dawidek nv_free(nvout);
8171c895ebSPawel Jakub Dawidek }
8271c895ebSPawel Jakub Dawidek
8371c895ebSPawel Jakub Dawidek int
event_recv(const struct hast_resource * res)8471c895ebSPawel Jakub Dawidek event_recv(const struct hast_resource *res)
8571c895ebSPawel Jakub Dawidek {
8671c895ebSPawel Jakub Dawidek struct nv *nvin, *nvout;
8771c895ebSPawel Jakub Dawidek const char *evstr;
8871c895ebSPawel Jakub Dawidek uint8_t event;
8971c895ebSPawel Jakub Dawidek int error;
9071c895ebSPawel Jakub Dawidek
91adf8002bSPawel Jakub Dawidek PJDLOG_ASSERT(res != NULL);
9271c895ebSPawel Jakub Dawidek
9371c895ebSPawel Jakub Dawidek nvin = nvout = NULL;
9471c895ebSPawel Jakub Dawidek
952b1b224dSPawel Jakub Dawidek if (hast_proto_recv_hdr(res->hr_event, &nvin) == -1) {
9671c895ebSPawel Jakub Dawidek /*
9771c895ebSPawel Jakub Dawidek * First error log as debug. This is because worker process
9871c895ebSPawel Jakub Dawidek * most likely exited.
9971c895ebSPawel Jakub Dawidek */
10071c895ebSPawel Jakub Dawidek pjdlog_common(LOG_DEBUG, 1, errno,
10171c895ebSPawel Jakub Dawidek "Unable to receive event header");
10271c895ebSPawel Jakub Dawidek goto fail;
10371c895ebSPawel Jakub Dawidek }
10471c895ebSPawel Jakub Dawidek
10571c895ebSPawel Jakub Dawidek event = nv_get_uint8(nvin, "event");
10671c895ebSPawel Jakub Dawidek if (event == EVENT_NONE) {
10771c895ebSPawel Jakub Dawidek pjdlog_error("Event header is missing 'event' field.");
10871c895ebSPawel Jakub Dawidek goto fail;
10971c895ebSPawel Jakub Dawidek }
11071c895ebSPawel Jakub Dawidek
11171c895ebSPawel Jakub Dawidek switch (event) {
11271c895ebSPawel Jakub Dawidek case EVENT_CONNECT:
11371c895ebSPawel Jakub Dawidek evstr = "connect";
11471c895ebSPawel Jakub Dawidek break;
11571c895ebSPawel Jakub Dawidek case EVENT_DISCONNECT:
11671c895ebSPawel Jakub Dawidek evstr = "disconnect";
11771c895ebSPawel Jakub Dawidek break;
11871c895ebSPawel Jakub Dawidek case EVENT_SYNCSTART:
11971c895ebSPawel Jakub Dawidek evstr = "syncstart";
12071c895ebSPawel Jakub Dawidek break;
12171c895ebSPawel Jakub Dawidek case EVENT_SYNCDONE:
12271c895ebSPawel Jakub Dawidek evstr = "syncdone";
12371c895ebSPawel Jakub Dawidek break;
12471c895ebSPawel Jakub Dawidek case EVENT_SYNCINTR:
12571c895ebSPawel Jakub Dawidek evstr = "syncintr";
12671c895ebSPawel Jakub Dawidek break;
12771c895ebSPawel Jakub Dawidek case EVENT_SPLITBRAIN:
12871c895ebSPawel Jakub Dawidek evstr = "split-brain";
12971c895ebSPawel Jakub Dawidek break;
13071c895ebSPawel Jakub Dawidek default:
13171c895ebSPawel Jakub Dawidek pjdlog_error("Event header contain invalid event number (%hhu).",
13271c895ebSPawel Jakub Dawidek event);
13371c895ebSPawel Jakub Dawidek goto fail;
13471c895ebSPawel Jakub Dawidek }
13571c895ebSPawel Jakub Dawidek
13671c895ebSPawel Jakub Dawidek pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
13771c895ebSPawel Jakub Dawidek hook_exec(res->hr_exec, evstr, res->hr_name, NULL);
13871c895ebSPawel Jakub Dawidek pjdlog_prefix_set("%s", "");
13971c895ebSPawel Jakub Dawidek
14071c895ebSPawel Jakub Dawidek nvout = nv_alloc();
14171c895ebSPawel Jakub Dawidek nv_add_int16(nvout, 0, "error");
14271c895ebSPawel Jakub Dawidek error = nv_error(nvout);
14371c895ebSPawel Jakub Dawidek if (error != 0) {
14471c895ebSPawel Jakub Dawidek pjdlog_common(LOG_ERR, 0, error,
14571c895ebSPawel Jakub Dawidek "Unable to prepare event header");
14671c895ebSPawel Jakub Dawidek goto fail;
14771c895ebSPawel Jakub Dawidek }
1482b1b224dSPawel Jakub Dawidek if (hast_proto_send(res, res->hr_event, nvout, NULL, 0) == -1) {
14971c895ebSPawel Jakub Dawidek pjdlog_errno(LOG_ERR, "Unable to send event header");
15071c895ebSPawel Jakub Dawidek goto fail;
15171c895ebSPawel Jakub Dawidek }
15271c895ebSPawel Jakub Dawidek nv_free(nvin);
15371c895ebSPawel Jakub Dawidek nv_free(nvout);
15471c895ebSPawel Jakub Dawidek return (0);
15571c895ebSPawel Jakub Dawidek fail:
15671c895ebSPawel Jakub Dawidek if (nvin != NULL)
15771c895ebSPawel Jakub Dawidek nv_free(nvin);
15871c895ebSPawel Jakub Dawidek if (nvout != NULL)
15971c895ebSPawel Jakub Dawidek nv_free(nvout);
16071c895ebSPawel Jakub Dawidek return (-1);
16171c895ebSPawel Jakub Dawidek }
162