2 * Copyright (c) 2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
33 #include "hast_proto.h"
43 event_send(const struct hast_resource *res, int event)
45 struct nv *nvin, *nvout;
48 PJDLOG_ASSERT(res != NULL);
49 PJDLOG_ASSERT(event >= EVENT_MIN && event <= EVENT_MAX);
54 * Prepare and send event to parent process.
57 nv_add_uint8(nvout, (uint8_t)event, "event");
58 error = nv_error(nvout);
60 pjdlog_common(LOG_ERR, 0, error,
61 "Unable to prepare event header");
64 if (hast_proto_send(res, res->hr_event, nvout, NULL, 0) == -1) {
65 pjdlog_errno(LOG_ERR, "Unable to send event header");
68 if (hast_proto_recv_hdr(res->hr_event, &nvin) == -1) {
69 pjdlog_errno(LOG_ERR, "Unable to receive event header");
73 * Do nothing with the answer. We only wait for it to be sure not
74 * to exit too quickly after sending an event and exiting immediately.
84 event_recv(const struct hast_resource *res)
86 struct nv *nvin, *nvout;
91 PJDLOG_ASSERT(res != NULL);
95 if (hast_proto_recv_hdr(res->hr_event, &nvin) == -1) {
97 * First error log as debug. This is because worker process
100 pjdlog_common(LOG_DEBUG, 1, errno,
101 "Unable to receive event header");
105 event = nv_get_uint8(nvin, "event");
106 if (event == EVENT_NONE) {
107 pjdlog_error("Event header is missing 'event' field.");
115 case EVENT_DISCONNECT:
116 evstr = "disconnect";
118 case EVENT_SYNCSTART:
127 case EVENT_SPLITBRAIN:
128 evstr = "split-brain";
131 pjdlog_error("Event header contain invalid event number (%hhu).",
136 pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
137 hook_exec(res->hr_exec, evstr, res->hr_name, NULL);
138 pjdlog_prefix_set("%s", "");
141 nv_add_int16(nvout, 0, "error");
142 error = nv_error(nvout);
144 pjdlog_common(LOG_ERR, 0, error,
145 "Unable to prepare event header");
148 if (hast_proto_send(res, res->hr_event, nvout, NULL, 0) == -1) {
149 pjdlog_errno(LOG_ERR, "Unable to send event header");