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$");
34 #include "hast_proto.h"
44 event_send(const struct hast_resource *res, int event)
46 struct nv *nvin, *nvout;
50 assert(event >= EVENT_MIN && event <= EVENT_MAX);
55 * Prepare and send event to parent process.
58 nv_add_uint8(nvout, (uint8_t)event, "event");
59 error = nv_error(nvout);
61 pjdlog_common(LOG_ERR, 0, error,
62 "Unable to prepare event header");
65 if (hast_proto_send(res, res->hr_event, nvout, NULL, 0) < 0) {
66 pjdlog_errno(LOG_ERR, "Unable to send event header");
69 if (hast_proto_recv_hdr(res->hr_event, &nvin) < 0) {
70 pjdlog_errno(LOG_ERR, "Unable to receive event header");
74 * Do nothing with the answer. We only wait for it to be sure not
75 * to exit too quickly after sending an event and exiting immediately.
85 event_recv(const struct hast_resource *res)
87 struct nv *nvin, *nvout;
96 if (hast_proto_recv_hdr(res->hr_event, &nvin) < 0) {
98 * First error log as debug. This is because worker process
101 pjdlog_common(LOG_DEBUG, 1, errno,
102 "Unable to receive event header");
106 event = nv_get_uint8(nvin, "event");
107 if (event == EVENT_NONE) {
108 pjdlog_error("Event header is missing 'event' field.");
116 case EVENT_DISCONNECT:
117 evstr = "disconnect";
119 case EVENT_SYNCSTART:
128 case EVENT_SPLITBRAIN:
129 evstr = "split-brain";
132 pjdlog_error("Event header contain invalid event number (%hhu).",
137 pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
138 hook_exec(res->hr_exec, evstr, res->hr_name, NULL);
139 pjdlog_prefix_set("%s", "");
142 nv_add_int16(nvout, 0, "error");
143 error = nv_error(nvout);
145 pjdlog_common(LOG_ERR, 0, error,
146 "Unable to prepare event header");
149 if (hast_proto_send(res, res->hr_event, nvout, NULL, 0) < 0) {
150 pjdlog_errno(LOG_ERR, "Unable to send event header");