pacemaker  2.1.8-3980678f03
Scalable High-Availability cluster resource manager
ipc_pacemakerd.c
Go to the documentation of this file.
1 /*
2  * Copyright 2020-2024 the Pacemaker project contributors
3  *
4  * The version control history for this file may have further details.
5  *
6  * This source code is licensed under the GNU Lesser General Public License
7  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
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>
20 #include "crmcommon_private.h"
21 
22 typedef struct pacemakerd_api_private_s {
23  enum pcmk_pacemakerd_state state;
24  char *client_uuid;
26 
27 static const char *pacemakerd_state_str[] = {
35 };
36 
39 {
40  int i;
41 
42  if (state == NULL) {
44  }
46  i++) {
47  if (pcmk__str_eq(state, pacemakerd_state_str[i], pcmk__str_none)) {
48  return i;
49  }
50  }
52 }
53 
54 const char *
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 
74 const char *
76 {
77  switch (state) {
79  return "Initializing pacemaker";
81  return "Pacemaker daemons are starting";
83  return "Waiting for startup trigger from SBD";
85  return "Pacemaker is running";
87  return "Pacemaker daemons are shutting down";
89  /* Assuming pacemakerd won't process messages while in
90  * shutdown_complete state unless reporting to SBD
91  */
92  return "Pacemaker daemons are shut down (reporting to SBD)";
94  return "pacemaker-remoted is running (on a Pacemaker Remote node)";
95  default:
96  return "Invalid pacemakerd state";
97  }
98 }
99 
108 const char *
110 {
111  switch (reply) {
113  return "ping";
115  return "shutdown";
116  default:
117  return "unknown";
118  }
119 }
120 
121 // \return Standard Pacemaker return code
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  /* other as with cib, controld, ... we are addressing pacemakerd just
136  from the local node -> pid is unique and thus sufficient as an ID
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 // \return Standard Pacemaker return code
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  // We only need to handle commands that functions in this file can send
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 = {
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  // Parse useful info from reply
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)) {
237  reply_data.data.ping.state =
240  reply_data.data.ping.status =
241  pcmk__str_eq(crm_element_value(msg_data, PCMK_XA_RESULT), "ok",
243  reply_data.data.ping.last_good = (value_ll < 0)? 0 : (time_t) value_ll;
244  reply_data.data.ping.sys_from =
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 
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 
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 }
A dumping ground.
#define PCMK__VALUE_RESPONSE
int pcmk_pacemakerd_api_ping(pcmk_ipc_api_t *api, const char *ipc_name)
char data[0]
Definition: cpg.c:58
Could not determine status.
Definition: results.h:298
#define PCMK_XA_REFERENCE
Definition: xml_names.h:367
#define PCMK__XA_PACEMAKERD_STATE
#define CRM_OP_QUIT
Definition: crm.h:126
enum crm_exit_e crm_exit_t
#define PCMK__VALUE_SHUTDOWN_COMPLETE
#define PCMK__XA_SUBT
struct pcmk_pacemakerd_api_reply_t::@5::@6 ping
pcmk_pacemakerd_api_reply
Possible types of pacemakerd replies.
const char * pcmk_rc_str(int rc)
Get a user-friendly description of a return code.
Definition: results.c:501
int crm_element_value_ll(const xmlNode *data, const char *name, long long *dest)
Retrieve the long long integer value of an XML attribute.
Definition: nvpair.c:514
G_GNUC_INTERNAL void pcmk__call_ipc_callback(pcmk_ipc_api_t *api, enum pcmk_ipc_event event_type, crm_exit_t status, void *event_data)
Definition: ipc_client.c:150
#define CRM_SYSTEM_MCP
Definition: crm.h:95
struct pacemakerd_api_private_s pacemakerd_api_private_t
union pcmk_pacemakerd_api_reply_t::@5 data
#define PCMK__XA_CRM_TASK
#define PCMK_XA_CRM_TIMESTAMP
Definition: xml_names.h:250
int pcmk__scan_ll(const char *text, long long *result, long long default_value)
Definition: strings.c:97
enum pcmk_pacemakerd_api_reply reply_type
op_status
Protocol violated.
Definition: results.h:281
#define crm_debug(fmt, args...)
Definition: logging.h:402
const char * crm_element_value(const xmlNode *data, const char *name)
Retrieve the value of an XML attribute.
Definition: nvpair.c:446
xmlNode * pcmk__xe_first_child(const xmlNode *parent, const char *node_name, const char *attr_n, const char *attr_v)
Definition: xml.c:440
G_GNUC_INTERNAL int pcmk__send_ipc_request(pcmk_ipc_api_t *api, const xmlNode *request)
Definition: ipc_client.c:668
#define PCMK__VALUE_WAIT_FOR_PING
Wrappers for and extensions to libxml2.
#define PCMK__VALUE_RUNNING
#define PCMK__VALUE_STARTING_DAEMONS
Success.
Definition: results.h:255
IPC commands for Pacemakerd.
#define PCMK__VALUE_INIT
void free_xml(xmlNode *child)
Definition: xml.c:867
int(* post_connect)(pcmk_ipc_api_t *api)
#define PCMK__XA_CRM_SUBSYSTEM
bool pcmk__str_any_of(const char *s,...) G_GNUC_NULL_TERMINATED
Definition: strings.c:1050
#define PCMK_XA_STATUS
Definition: xml_names.h:405
#define PCMK__VALUE_SHUTTING_DOWN
pcmk_pacemakerd_state
int(* new_data)(pcmk_ipc_api_t *api)
int pcmk_pacemakerd_api_shutdown(pcmk_ipc_api_t *api, const char *ipc_name)
enum pcmk_pacemakerd_state pcmk_pacemakerd_api_daemon_state_text2enum(const char *state)
const char * pcmk_pacemakerd_api_daemon_state_enum2text(enum pcmk_pacemakerd_state state)
#define PCMK__XE_CRM_XML
bool(* dispatch)(pcmk_ipc_api_t *api, xmlNode *msg)
bool(* reply_expected)(pcmk_ipc_api_t *api, const xmlNode *request)
#define CRM_ASSERT(expr)
Definition: results.h:42
#define PCMK__XE_ACK
#define PCMK__XA_OP_STATUS
void(* free_data)(void *api_data)
const char * pcmk_ipc_name(const pcmk_ipc_api_t *api, bool for_log)
Get the IPC name used with an IPC API connection.
Definition: ipc_client.c:247
struct pcmk_pacemakerd_api_reply_t::@5::@7 shutdown
IPC interface to Pacemaker daemons.
const char * pcmk__pcmkd_state_enum2friendly(enum pcmk_pacemakerd_state state)
const char * pcmk__pcmkd_api_reply2str(enum pcmk_pacemakerd_api_reply reply)
#define CRM_OP_PING
Definition: crm.h:119
void(* post_disconnect)(pcmk_ipc_api_t *api)
Daemon&#39;s reply to client IPC request.
Definition: ipc.h:96
#define PCMK_XA_RESULT
Definition: xml_names.h:381
#define create_request(task, xml_data, host_to, sys_to, sys_from, uuid_from)
Definition: ipc.h:49
#define PCMK_VALUE_REMOTE
Definition: options.h:198
#define crm_info(fmt, args...)
Definition: logging.h:399
pcmk__ipc_methods_t * pcmk__pacemakerd_api_methods(void)