xref: /freebsd/sbin/hastd/event.c (revision 4c8945a06b01a5c8122cdeb402af36bb46a06acc)
1 /*-
2  * Copyright (c) 2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <assert.h>
31 #include <errno.h>
32 
33 #include "hast.h"
34 #include "hast_proto.h"
35 #include "hooks.h"
36 #include "nv.h"
37 #include "pjdlog.h"
38 #include "proto.h"
39 #include "subr.h"
40 
41 #include "event.h"
42 
43 void
44 event_send(const struct hast_resource *res, int event)
45 {
46 	struct nv *nvin, *nvout;
47 	int error;
48 
49 	assert(res != NULL);
50 	assert(event >= EVENT_MIN && event <= EVENT_MAX);
51 
52 	nvin = nvout = NULL;
53 
54 	/*
55 	 * Prepare and send event to parent process.
56 	 */
57 	nvout = nv_alloc();
58 	nv_add_uint8(nvout, (uint8_t)event, "event");
59 	error = nv_error(nvout);
60 	if (error != 0) {
61 		pjdlog_common(LOG_ERR, 0, error,
62 		    "Unable to prepare event header");
63 		goto done;
64 	}
65 	if (hast_proto_send(res, res->hr_event, nvout, NULL, 0) < 0) {
66 		pjdlog_errno(LOG_ERR, "Unable to send event header");
67 		goto done;
68 	}
69 	if (hast_proto_recv_hdr(res->hr_event, &nvin) < 0) {
70 		pjdlog_errno(LOG_ERR, "Unable to receive event header");
71 		goto done;
72 	}
73 	/*
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.
76 	 */
77 done:
78 	if (nvin != NULL)
79 		nv_free(nvin);
80 	if (nvout != NULL)
81 		nv_free(nvout);
82 }
83 
84 int
85 event_recv(const struct hast_resource *res)
86 {
87 	struct nv *nvin, *nvout;
88 	const char *evstr;
89 	uint8_t event;
90 	int error;
91 
92 	assert(res != NULL);
93 
94 	nvin = nvout = NULL;
95 
96 	if (hast_proto_recv_hdr(res->hr_event, &nvin) < 0) {
97 		/*
98 		 * First error log as debug. This is because worker process
99 		 * most likely exited.
100 		 */
101 		pjdlog_common(LOG_DEBUG, 1, errno,
102 		    "Unable to receive event header");
103 		goto fail;
104 	}
105 
106 	event = nv_get_uint8(nvin, "event");
107 	if (event == EVENT_NONE) {
108 		pjdlog_error("Event header is missing 'event' field.");
109 		goto fail;
110 	}
111 
112 	switch (event) {
113 	case EVENT_CONNECT:
114 		evstr = "connect";
115 		break;
116 	case EVENT_DISCONNECT:
117 		evstr = "disconnect";
118 		break;
119 	case EVENT_SYNCSTART:
120 		evstr = "syncstart";
121 		break;
122 	case EVENT_SYNCDONE:
123 		evstr = "syncdone";
124 		break;
125 	case EVENT_SYNCINTR:
126 		evstr = "syncintr";
127 		break;
128 	case EVENT_SPLITBRAIN:
129 		evstr = "split-brain";
130 		break;
131 	default:
132 		pjdlog_error("Event header contain invalid event number (%hhu).",
133 		    event);
134 		goto fail;
135 	}
136 
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", "");
140 
141 	nvout = nv_alloc();
142 	nv_add_int16(nvout, 0, "error");
143 	error = nv_error(nvout);
144 	if (error != 0) {
145 		pjdlog_common(LOG_ERR, 0, error,
146 		    "Unable to prepare event header");
147 		goto fail;
148 	}
149 	if (hast_proto_send(res, res->hr_event, nvout, NULL, 0) < 0) {
150 		pjdlog_errno(LOG_ERR, "Unable to send event header");
151 		goto fail;
152 	}
153 	nv_free(nvin);
154 	nv_free(nvout);
155 	return (0);
156 fail:
157 	if (nvin != NULL)
158 		nv_free(nvin);
159 	if (nvout != NULL)
160 		nv_free(nvout);
161 	return (-1);
162 }
163