This source file includes following definitions.
- pcmk_pacemakerd_api_daemon_state_text2enum
- pcmk_pacemakerd_api_daemon_state_enum2text
- pcmk__pcmkd_state_enum2friendly
- pcmk__pcmkd_api_reply2str
- new_data
- free_data
- post_connect
- post_disconnect
- reply_expected
- dispatch
- pcmk__pacemakerd_api_methods
- do_pacemakerd_api_call
- pcmk_pacemakerd_api_ping
- pcmk_pacemakerd_api_shutdown
1
2
3
4
5
6
7
8
9
10 #include <crm_internal.h>
11
12 #include <stdlib.h>
13 #include <time.h>
14
15 #include <crm/crm.h>
16 #include <crm/common/xml.h>
17 #include <crm/common/ipc.h>
18 #include <crm/common/ipc_internal.h>
19 #include <crm/common/ipc_pacemakerd.h>
20 #include "crmcommon_private.h"
21
22 typedef struct pacemakerd_api_private_s {
23 enum pcmk_pacemakerd_state state;
24 char *client_uuid;
25 } pacemakerd_api_private_t;
26
27 static const char *pacemakerd_state_str[] = {
28 PCMK__VALUE_INIT,
29 PCMK__VALUE_STARTING_DAEMONS,
30 PCMK__VALUE_WAIT_FOR_PING,
31 PCMK__VALUE_RUNNING,
32 PCMK__VALUE_SHUTTING_DOWN,
33 PCMK__VALUE_SHUTDOWN_COMPLETE,
34 PCMK_VALUE_REMOTE,
35 };
36
37 enum pcmk_pacemakerd_state
38 pcmk_pacemakerd_api_daemon_state_text2enum(const char *state)
39 {
40 int i;
41
42 if (state == NULL) {
43 return pcmk_pacemakerd_state_invalid;
44 }
45 for (i=pcmk_pacemakerd_state_init; i <= pcmk_pacemakerd_state_max;
46 i++) {
47 if (pcmk__str_eq(state, pacemakerd_state_str[i], pcmk__str_none)) {
48 return i;
49 }
50 }
51 return pcmk_pacemakerd_state_invalid;
52 }
53
54 const char *
55 pcmk_pacemakerd_api_daemon_state_enum2text(
56 enum pcmk_pacemakerd_state state)
57 {
58 if ((state >= pcmk_pacemakerd_state_init) &&
59 (state <= pcmk_pacemakerd_state_max)) {
60 return pacemakerd_state_str[state];
61 }
62 return "invalid";
63 }
64
65
66
67
68
69
70
71
72
73
74 const char *
75 pcmk__pcmkd_state_enum2friendly(enum pcmk_pacemakerd_state state)
76 {
77 switch (state) {
78 case pcmk_pacemakerd_state_init:
79 return "Initializing pacemaker";
80 case pcmk_pacemakerd_state_starting_daemons:
81 return "Pacemaker daemons are starting";
82 case pcmk_pacemakerd_state_wait_for_ping:
83 return "Waiting for startup trigger from SBD";
84 case pcmk_pacemakerd_state_running:
85 return "Pacemaker is running";
86 case pcmk_pacemakerd_state_shutting_down:
87 return "Pacemaker daemons are shutting down";
88 case pcmk_pacemakerd_state_shutdown_complete:
89
90
91
92 return "Pacemaker daemons are shut down (reporting to SBD)";
93 case pcmk_pacemakerd_state_remote:
94 return "pacemaker-remoted is running (on a Pacemaker Remote node)";
95 default:
96 return "Invalid pacemakerd state";
97 }
98 }
99
100
101
102
103
104
105
106
107
108 const char *
109 pcmk__pcmkd_api_reply2str(enum pcmk_pacemakerd_api_reply reply)
110 {
111 switch (reply) {
112 case pcmk_pacemakerd_reply_ping:
113 return "ping";
114 case pcmk_pacemakerd_reply_shutdown:
115 return "shutdown";
116 default:
117 return "unknown";
118 }
119 }
120
121
122 static int
123 new_data(pcmk_ipc_api_t *api)
124 {
125 struct pacemakerd_api_private_s *private = NULL;
126
127 api->api_data = calloc(1, sizeof(struct pacemakerd_api_private_s));
128
129 if (api->api_data == NULL) {
130 return errno;
131 }
132
133 private = api->api_data;
134 private->state = pcmk_pacemakerd_state_invalid;
135
136
137
138 private->client_uuid = pcmk__getpid_s();
139
140 return pcmk_rc_ok;
141 }
142
143 static void
144 free_data(void *data)
145 {
146 free(((struct pacemakerd_api_private_s *) data)->client_uuid);
147 free(data);
148 }
149
150
151 static int
152 post_connect(pcmk_ipc_api_t *api)
153 {
154 struct pacemakerd_api_private_s *private = NULL;
155
156 if (api->api_data == NULL) {
157 return EINVAL;
158 }
159 private = api->api_data;
160 private->state = pcmk_pacemakerd_state_invalid;
161
162 return pcmk_rc_ok;
163 }
164
165 static void
166 post_disconnect(pcmk_ipc_api_t *api)
167 {
168 struct pacemakerd_api_private_s *private = NULL;
169
170 if (api->api_data == NULL) {
171 return;
172 }
173 private = api->api_data;
174 private->state = pcmk_pacemakerd_state_invalid;
175
176 return;
177 }
178
179 static bool
180 reply_expected(pcmk_ipc_api_t *api, const xmlNode *request)
181 {
182 const char *command = crm_element_value(request, PCMK__XA_CRM_TASK);
183
184 if (command == NULL) {
185 return false;
186 }
187
188
189 return pcmk__str_any_of(command, CRM_OP_PING, CRM_OP_QUIT, NULL);
190 }
191
192 static bool
193 dispatch(pcmk_ipc_api_t *api, xmlNode *reply)
194 {
195 crm_exit_t status = CRM_EX_OK;
196 xmlNode *wrapper = NULL;
197 xmlNode *msg_data = NULL;
198 pcmk_pacemakerd_api_reply_t reply_data = {
199 pcmk_pacemakerd_reply_unknown
200 };
201 const char *value = NULL;
202 long long value_ll = 0;
203
204 if (pcmk__xe_is(reply, PCMK__XE_ACK)) {
205 long long int ack_status = 0;
206 pcmk__scan_ll(crm_element_value(reply, PCMK_XA_STATUS), &ack_status,
207 CRM_EX_OK);
208 return ack_status == CRM_EX_INDETERMINATE;
209 }
210
211 value = crm_element_value(reply, PCMK__XA_SUBT);
212 if (!pcmk__str_eq(value, PCMK__VALUE_RESPONSE, pcmk__str_none)) {
213 crm_info("Unrecognizable message from %s: "
214 "message type '%s' not '" PCMK__VALUE_RESPONSE "'",
215 pcmk_ipc_name(api, true), pcmk__s(value, ""));
216 status = CRM_EX_PROTOCOL;
217 goto done;
218 }
219
220 if (pcmk__str_empty(crm_element_value(reply, PCMK_XA_REFERENCE))) {
221 crm_info("Unrecognizable message from %s: no reference",
222 pcmk_ipc_name(api, true));
223 status = CRM_EX_PROTOCOL;
224 goto done;
225 }
226
227 value = crm_element_value(reply, PCMK__XA_CRM_TASK);
228
229
230 wrapper = pcmk__xe_first_child(reply, PCMK__XE_CRM_XML, NULL, NULL);
231 msg_data = pcmk__xe_first_child(wrapper, NULL, NULL, NULL);
232
233 crm_element_value_ll(msg_data, PCMK_XA_CRM_TIMESTAMP, &value_ll);
234
235 if (pcmk__str_eq(value, CRM_OP_PING, pcmk__str_none)) {
236 reply_data.reply_type = pcmk_pacemakerd_reply_ping;
237 reply_data.data.ping.state =
238 pcmk_pacemakerd_api_daemon_state_text2enum(
239 crm_element_value(msg_data, PCMK__XA_PACEMAKERD_STATE));
240 reply_data.data.ping.status =
241 pcmk__str_eq(crm_element_value(msg_data, PCMK_XA_RESULT), "ok",
242 pcmk__str_casei)?pcmk_rc_ok:pcmk_rc_error;
243 reply_data.data.ping.last_good = (value_ll < 0)? 0 : (time_t) value_ll;
244 reply_data.data.ping.sys_from =
245 crm_element_value(msg_data, PCMK__XA_CRM_SUBSYSTEM);
246 } else if (pcmk__str_eq(value, CRM_OP_QUIT, pcmk__str_none)) {
247 const char *op_status = crm_element_value(msg_data, PCMK__XA_OP_STATUS);
248
249 reply_data.reply_type = pcmk_pacemakerd_reply_shutdown;
250 reply_data.data.shutdown.status = atoi(op_status);
251 } else {
252 crm_info("Unrecognizable message from %s: unknown command '%s'",
253 pcmk_ipc_name(api, true), pcmk__s(value, ""));
254 status = CRM_EX_PROTOCOL;
255 goto done;
256 }
257
258 done:
259 pcmk__call_ipc_callback(api, pcmk_ipc_event_reply, status, &reply_data);
260 return false;
261 }
262
263 pcmk__ipc_methods_t *
264 pcmk__pacemakerd_api_methods(void)
265 {
266 pcmk__ipc_methods_t *cmds = calloc(1, sizeof(pcmk__ipc_methods_t));
267
268 if (cmds != NULL) {
269 cmds->new_data = new_data;
270 cmds->free_data = free_data;
271 cmds->post_connect = post_connect;
272 cmds->reply_expected = reply_expected;
273 cmds->dispatch = dispatch;
274 cmds->post_disconnect = post_disconnect;
275 }
276 return cmds;
277 }
278
279 static int
280 do_pacemakerd_api_call(pcmk_ipc_api_t *api, const char *ipc_name, const char *task)
281 {
282 pacemakerd_api_private_t *private;
283 xmlNode *cmd;
284 int rc;
285
286 if (api == NULL) {
287 return EINVAL;
288 }
289
290 private = api->api_data;
291 CRM_ASSERT(private != NULL);
292
293 cmd = create_request(task, NULL, NULL, CRM_SYSTEM_MCP,
294 pcmk__ipc_sys_name(ipc_name, "client"),
295 private->client_uuid);
296
297 if (cmd) {
298 rc = pcmk__send_ipc_request(api, cmd);
299 if (rc != pcmk_rc_ok) {
300 crm_debug("Couldn't send request to %s: %s rc=%d",
301 pcmk_ipc_name(api, true), pcmk_rc_str(rc), rc);
302 }
303 free_xml(cmd);
304 } else {
305 rc = ENOMSG;
306 }
307
308 return rc;
309 }
310
311 int
312 pcmk_pacemakerd_api_ping(pcmk_ipc_api_t *api, const char *ipc_name)
313 {
314 return do_pacemakerd_api_call(api, ipc_name, CRM_OP_PING);
315 }
316
317 int
318 pcmk_pacemakerd_api_shutdown(pcmk_ipc_api_t *api, const char *ipc_name)
319 {
320 return do_pacemakerd_api_call(api, ipc_name, CRM_OP_QUIT);
321 }