1 /*
2 * Copyright 2012-2021 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 #ifndef LRMD__H
11 # define LRMD__H
12
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16
17 /**
18 * \file
19 * \brief Resource agent executor
20 * \ingroup lrmd
21 */
22 #include <stdbool.h> // bool
23 #include <glib.h> // guint, GList
24 #include <crm_config.h>
25 #include <crm/services.h>
26
27 typedef struct lrmd_s lrmd_t;
28 typedef struct lrmd_key_value_s {
29 char *key;
30 char *value;
31 struct lrmd_key_value_s *next;
32 } lrmd_key_value_t;
33
34 /* This should be bumped every time there is an incompatible change that
35 * prevents older clients from connecting to this version of the server.
36 */
37 #define LRMD_PROTOCOL_VERSION "1.1"
38
39 /* This is the version that the client version will actually be compared
40 * against. This should be identical to LRMD_PROTOCOL_VERSION. However, we
41 * accidentally bumped LRMD_PROTOCOL_VERSION in 6424a647 (1.1.15) when we didn't
42 * need to, so for now it's different. If we ever have a truly incompatible
43 * bump, we can drop this and compare against LRMD_PROTOCOL_VERSION.
44 */
45 #define LRMD_MIN_PROTOCOL_VERSION "1.0"
46
47 /* *INDENT-OFF* */
48 #define DEFAULT_REMOTE_KEY_LOCATION PACEMAKER_CONFIG_DIR "/authkey"
49 #define ALT_REMOTE_KEY_LOCATION "/etc/corosync/authkey"
50 #define DEFAULT_REMOTE_PORT 3121
51 #define DEFAULT_REMOTE_USERNAME "lrmd"
52
53 #define F_LRMD_OPERATION "lrmd_op"
54 #define F_LRMD_CLIENTNAME "lrmd_clientname"
55 #define F_LRMD_IS_IPC_PROVIDER "lrmd_is_ipc_provider"
56 #define F_LRMD_CLIENTID "lrmd_clientid"
57 #define F_LRMD_PROTOCOL_VERSION "lrmd_protocol_version"
58 #define F_LRMD_REMOTE_MSG_TYPE "lrmd_remote_msg_type"
59 #define F_LRMD_REMOTE_MSG_ID "lrmd_remote_msg_id"
60 #define F_LRMD_CALLBACK_TOKEN "lrmd_async_id"
61 #define F_LRMD_CALLID "lrmd_callid"
62 #define F_LRMD_CALLOPTS "lrmd_callopt"
63 #define F_LRMD_CALLDATA "lrmd_calldata"
64 #define F_LRMD_RC "lrmd_rc"
65 #define F_LRMD_EXEC_RC "lrmd_exec_rc"
66 #define F_LRMD_OP_STATUS "lrmd_exec_op_status"
67 #define F_LRMD_TIMEOUT "lrmd_timeout"
68 #define F_LRMD_WATCHDOG "lrmd_watchdog"
69 #define F_LRMD_CLASS "lrmd_class"
70 #define F_LRMD_PROVIDER "lrmd_provider"
71 #define F_LRMD_TYPE "lrmd_type"
72 #define F_LRMD_ORIGIN "lrmd_origin"
73
74 #define F_LRMD_RSC_RUN_TIME "lrmd_run_time"
75 #define F_LRMD_RSC_RCCHANGE_TIME "lrmd_rcchange_time"
76 #define F_LRMD_RSC_EXEC_TIME "lrmd_exec_time"
77 #define F_LRMD_RSC_QUEUE_TIME "lrmd_queue_time"
78
79 #define F_LRMD_RSC_ID "lrmd_rsc_id"
80 #define F_LRMD_RSC_ACTION "lrmd_rsc_action"
81 #define F_LRMD_RSC_USERDATA_STR "lrmd_rsc_userdata_str"
82 #define F_LRMD_RSC_OUTPUT "lrmd_rsc_output"
83 #define F_LRMD_RSC_EXIT_REASON "lrmd_rsc_exit_reason"
84 #define F_LRMD_RSC_START_DELAY "lrmd_rsc_start_delay"
85 #define F_LRMD_RSC_INTERVAL "lrmd_rsc_interval"
86 #define F_LRMD_RSC_DELETED "lrmd_rsc_deleted"
87 #define F_LRMD_RSC "lrmd_rsc"
88
89 #define F_LRMD_ALERT_ID "lrmd_alert_id"
90 #define F_LRMD_ALERT_PATH "lrmd_alert_path"
91 #define F_LRMD_ALERT "lrmd_alert"
92
93 #define LRMD_OP_RSC_REG "lrmd_rsc_register"
94 #define LRMD_OP_RSC_EXEC "lrmd_rsc_exec"
95 #define LRMD_OP_RSC_CANCEL "lrmd_rsc_cancel"
96 #define LRMD_OP_RSC_UNREG "lrmd_rsc_unregister"
97 #define LRMD_OP_RSC_INFO "lrmd_rsc_info"
98 #define LRMD_OP_RSC_METADATA "lrmd_rsc_metadata"
99 #define LRMD_OP_POKE "lrmd_rsc_poke"
100 #define LRMD_OP_NEW_CLIENT "lrmd_rsc_new_client"
101 #define LRMD_OP_CHECK "lrmd_check"
102 #define LRMD_OP_ALERT_EXEC "lrmd_alert_exec"
103 #define LRMD_OP_GET_RECURRING "lrmd_get_recurring"
104
105 #define LRMD_IPC_OP_NEW "new"
106 #define LRMD_IPC_OP_DESTROY "destroy"
107 #define LRMD_IPC_OP_EVENT "event"
108 #define LRMD_IPC_OP_REQUEST "request"
109 #define LRMD_IPC_OP_RESPONSE "response"
110 #define LRMD_IPC_OP_SHUTDOWN_REQ "shutdown_req"
111 #define LRMD_IPC_OP_SHUTDOWN_ACK "shutdown_ack"
112 #define LRMD_IPC_OP_SHUTDOWN_NACK "shutdown_nack"
113
114 #define F_LRMD_IPC_OP "lrmd_ipc_op"
115 #define F_LRMD_IPC_IPC_SERVER "lrmd_ipc_server"
116 #define F_LRMD_IPC_SESSION "lrmd_ipc_session"
117 #define F_LRMD_IPC_CLIENT "lrmd_ipc_client"
118 #define F_LRMD_IPC_USER "lrmd_ipc_user"
119 #define F_LRMD_IPC_MSG "lrmd_ipc_msg"
120 #define F_LRMD_IPC_MSG_ID "lrmd_ipc_msg_id"
121 #define F_LRMD_IPC_MSG_FLAGS "lrmd_ipc_msg_flags"
122
123 #define T_LRMD "lrmd"
124 #define T_LRMD_REPLY "lrmd_reply"
125 #define T_LRMD_NOTIFY "lrmd_notify"
126 #define T_LRMD_IPC_PROXY "lrmd_ipc_proxy"
127 #define T_LRMD_RSC_OP "lrmd_rsc_op"
128 /* *INDENT-ON* */
129
130 /*!
131 * \brief Create a new connection to the local executor
132 */
133 lrmd_t *lrmd_api_new(void);
134
135 /*!
136 * \brief Create a new TLS connection to a remote executor
137 *
138 * \param nodename name of remote node identified with this connection
139 * \param server name of server to connect to
140 * \param port port number to connect to
141 *
142 * \note nodename and server may be the same value.
143 */
144 lrmd_t *lrmd_remote_api_new(const char *nodename, const char *server, int port);
145
146 /*!
147 * \brief Use after lrmd_poll returns 1 to read and dispatch a message
148 *
149 * \param[in,out] lrmd Executor connection object
150 *
151 * \return TRUE if connection is still up, FALSE if disconnected
152 */
153 bool lrmd_dispatch(lrmd_t * lrmd);
154
155 /*!
156 * \brief Poll for a specified timeout period to determine if a message
157 * is ready for dispatch
158 *
159 * \retval 1 Message is ready
160 * \retval 0 Timeout occurred
161 * \retval negative errno Error occurred
162 */
163 int lrmd_poll(lrmd_t * lrmd, int timeout);
164
165 /*!
166 * \brief Destroy executor connection object
167 */
168 void lrmd_api_delete(lrmd_t * lrmd);
169 lrmd_key_value_t *lrmd_key_value_add(lrmd_key_value_t * kvp, const char *key, const char *value);
170
171 /* *INDENT-OFF* */
172 /* Reserved for future use */
173 enum lrmd_call_options {
174 lrmd_opt_none = 0x00000000,
175 /* lrmd_opt_sync_call = 0x00000001, //Not implemented, patches welcome. */
176 /*! Only notify the client originating a exec() the results */
177 lrmd_opt_notify_orig_only = 0x00000002,
178 /*! Drop recurring operations initiated by a client when client disconnects.
179 * This call_option is only valid when registering a resource. When used
180 * remotely with the pacemaker_remote daemon, this option means that recurring
181 * operations will be dropped once all the remote connections disconnect. */
182 lrmd_opt_drop_recurring = 0x00000003,
183 /*! Send notifications for recurring operations only when the result changes */
184 lrmd_opt_notify_changes_only = 0x00000004,
185 };
186
187 enum lrmd_callback_event {
188 lrmd_event_register,
189 lrmd_event_unregister,
190 lrmd_event_exec_complete,
191 lrmd_event_disconnect,
192 lrmd_event_connect,
193 lrmd_event_poke,
194 lrmd_event_new_client,
195 };
196
197 /* *INDENT-ON* */
198
199 typedef struct lrmd_event_data_s {
200 /*! Type of event, register, unregister, call_completed... */
201 enum lrmd_callback_event type;
202
203 /*! The resource this event occurred on. */
204 const char *rsc_id;
205 /*! The action performed, start, stop, monitor... */
206 const char *op_type;
207 /*! The user data passed by caller of exec() API function */
208 const char *user_data;
209
210 /*! The client api call id associated with this event */
211 int call_id;
212 /*! The operation's timeout period in ms. */
213 int timeout;
214 /*! The operation's recurring interval in ms. */
215 guint interval_ms;
216 /*! The operation's start delay value in ms. */
217 int start_delay;
218 /*! This operation that just completed is on a deleted rsc. */
219 int rsc_deleted;
220
221 /*! The executed ra return code mapped to OCF */
222 enum ocf_exitcode rc;
223 /*! The executor status returned for exec_complete events */
224 int op_status;
225 /*! stdout from resource agent operation */
226 const char *output;
227 /*! Timestamp of when op ran */
228 unsigned int t_run;
229 /*! Timestamp of last rc change */
230 unsigned int t_rcchange;
231 /*! Time in length op took to execute */
232 unsigned int exec_time;
233 /*! Time in length spent in queue */
234 unsigned int queue_time;
235
236 /*! int connection result. Used for connection and poke events */
237 int connection_rc;
238
239 /* This is a GHashTable containing the
240 * parameters given to the operation */
241 void *params;
242
243 /*! client node name associated with this connection
244 * (used to match actions to the proper client when there are multiple)
245 */
246 const char *remote_nodename;
247
248 /*! exit failure reason string from resource agent operation */
249 const char *exit_reason;
250 } lrmd_event_data_t;
251
252 lrmd_event_data_t *lrmd_new_event(const char *rsc_id, const char *task,
253 guint interval_ms);
254 lrmd_event_data_t *lrmd_copy_event(lrmd_event_data_t * event);
255 void lrmd_free_event(lrmd_event_data_t * event);
256
257 typedef struct lrmd_rsc_info_s {
258 char *id;
259 char *type;
260 char *standard;
261 char *provider;
262 } lrmd_rsc_info_t;
263
264 typedef struct lrmd_op_info_s {
265 char *rsc_id;
266 char *action;
267 char *interval_ms_s;
268 char *timeout_ms_s;
269 } lrmd_op_info_t;
270
271 lrmd_rsc_info_t *lrmd_new_rsc_info(const char *rsc_id, const char *standard,
272 const char *provider, const char *type);
273 lrmd_rsc_info_t *lrmd_copy_rsc_info(lrmd_rsc_info_t * rsc_info);
274 void lrmd_free_rsc_info(lrmd_rsc_info_t * rsc_info);
275 void lrmd_free_op_info(lrmd_op_info_t *op_info);
276
277 typedef void (*lrmd_event_callback) (lrmd_event_data_t * event);
278
279 typedef struct lrmd_list_s {
280 const char *val;
281 struct lrmd_list_s *next;
282 } lrmd_list_t;
283
284 void lrmd_list_freeall(lrmd_list_t * head);
285 void lrmd_key_value_freeall(lrmd_key_value_t * head);
286
287 typedef struct lrmd_api_operations_s {
288 /*!
289 * \brief Connect to an executor
290 *
291 * \return Legacy Pacemaker return code
292 */
293 int (*connect) (lrmd_t * lrmd, const char *client_name, int *fd);
294
295 /*!
296 * \brief Initiate an executor connection without blocking
297 *
298 * \return Legacy Pacemaker return code (if pcmk_ok, the event callback will
299 * be called later with the result)
300 * \note This function requires a mainloop.
301 */
302 int (*connect_async) (lrmd_t * lrmd, const char *client_name, int timeout /*ms */ );
303
304 /*!
305 * \brief Check whether connection to executor daemon is (still) active
306 *
307 * \return 1 if the executor connection is active, 0 otherwise
308 */
309 int (*is_connected) (lrmd_t * lrmd);
310
311 /*!
312 * \brief Poke executor connection to verify it is still capable of serving requests
313 * \note The response comes in the form of a poke event to the callback.
314 *
315 * \return Legacy Pacemaker return code (if pcmk_ok, the event callback will
316 * be called later with the result)
317 */
318 int (*poke_connection) (lrmd_t * lrmd);
319
320 /*!
321 * \brief Disconnect from the executor.
322 *
323 * \return Legacy Pacemaker return code
324 */
325 int (*disconnect) (lrmd_t * lrmd);
326
327 /*!
328 * \brief Register a resource with the executor.
329 *
330 * \note Synchronous, guaranteed to occur in daemon before function returns.
331 *
332 * \return Legacy Pacemaker return code
333 */
334 int (*register_rsc) (lrmd_t * lrmd,
335 const char *rsc_id,
336 const char *standard,
337 const char *provider, const char *agent, enum lrmd_call_options options);
338
339 /*!
340 * \brief Retrieve a resource's registration information
341 *
342 * \return Resource information on success, otherwise NULL
343 */
344 lrmd_rsc_info_t *(*get_rsc_info) (lrmd_t * lrmd,
345 const char *rsc_id, enum lrmd_call_options options);
346
347 /*!
348 * \brief Retrieve registered recurring operations
349 *
350 * \return Legacy Pacemaker return code
351 */
352 int (*get_recurring_ops) (lrmd_t *lrmd, const char *rsc_id, int timeout_ms,
353 enum lrmd_call_options options, GList **output);
354
355 /*!
356 * \brief Unregister a resource from the executor.
357 *
358 * \note All pending and recurring operations will be cancelled
359 * automatically.
360 *
361 * \note Synchronous, guaranteed to occur in daemon before function returns.
362 *
363 * \return Legacy Pacemaker return code (of particular interest, EINPROGRESS
364 * means that operations are in progress for the resource, and the
365 * unregistration will be done when they complete)
366 */
367 int (*unregister_rsc) (lrmd_t * lrmd, const char *rsc_id, enum lrmd_call_options options);
368
369 /*!
370 * \brief Set a callback for executor events
371 */
372 void (*set_callback) (lrmd_t * lrmd, lrmd_event_callback callback);
373
374 /*!
375 * \brief Issue a command on a resource
376 *
377 * \return A call ID for the action on success (in which case the action is
378 * queued in the executor, and the event callback will be called
379 * later with the result), otherwise a negative legacy Pacemaker
380 * return code
381 *
382 * \note exec() and cancel() operations on an individual resource are
383 * guaranteed to occur in the order the client API is called. However,
384 * operations on different resources are not guaranteed to occur in
385 * any specific order.
386 */
387 int (*exec) (lrmd_t * lrmd, const char *rsc_id, const char *action, const char *userdata, /* userdata string given back in event notification */
388 guint interval_ms,
389 int timeout, /* ms */
390 int start_delay, /* ms */
391 enum lrmd_call_options options, lrmd_key_value_t * params); /* ownership of params is given up to api here */
392
393 /*!
394 * \brief Cancel a recurring command.
395 *
396 * \return Legacy Pacemaker return code (if pcmk_ok, command is queued in
397 * daemon on function return, and the event callback will be called
398 * later with an exec_complete event with an lrmd_op_status
399 * signifying that the operation is cancelled)
400 *
401 * \note exec() and cancel() operations on an individual resource are
402 * guaranteed to occur in the order the client API is called. However,
403 * operations on different resources are not guaranteed to occur in
404 * any specific order.
405 */
406 int (*cancel) (lrmd_t *lrmd, const char *rsc_id, const char *action,
407 guint interval_ms);
408
409 /*!
410 * \brief Get resource metadata for a specified resource agent
411 *
412 * \param[in] lrmd Executor connection (unused)
413 * \param[in] standard Resource agent class
414 * \param[in] provider Resource agent provider
415 * \param[in] agent Resource agent type
416 * \param[out] output Metadata will be stored here (must not be NULL)
417 * \param[in] options Options to use with any executor API calls (unused)
418 *
419 * \return Legacy Pacemaker return code
420 *
421 * \note Caller is responsible for freeing output. This call is currently
422 * always synchronous (blocking), and always done directly by the
423 * library (not via the executor connection). This means that it is based
424 * on the local host environment, even if the executor connection is to a
425 * remote node, so (for most resource agent classes) this will fail if
426 * the agent is not installed locally. This also means that, if an
427 * external agent must be executed, it will be executed by the
428 * caller's user, not the executor's.
429 * \todo Add a metadata call to the executor API and let the server handle this.
430 */
431 int (*get_metadata) (lrmd_t * lrmd,
432 const char *standard,
433 const char *provider,
434 const char *agent, char **output, enum lrmd_call_options options);
435
436 /*!
437 * \brief Retrieve a list of installed resource agents.
438 *
439 * \return Number of items in list on success, negative legacy Pacemaker
440 * return code otherwise
441 *
442 * \note if standard is not provided, all known agents will be returned
443 * \note list must be freed using lrmd_list_freeall()
444 */
445 int (*list_agents) (lrmd_t * lrmd, lrmd_list_t ** agents,
446 const char *standard, const char *provider);
447
448 /*!
449 * \brief Retrieve a list of resource agent providers
450 *
451 * \return Number of items in list on success, negative legacy Pacemaker
452 * return code otherwise
453 *
454 * \note When the agent is provided, only the agent's provider will be returned
455 * \note When no agent is supplied, all providers will be returned.
456 * \note List must be freed using lrmd_list_freeall()
457 */
458 int (*list_ocf_providers) (lrmd_t * lrmd, const char *agent, lrmd_list_t ** providers);
459
460 /*!
461 * \brief Retrieve a list of standards supported by this machine/installation
462 *
463 * \return Number of items in list on success, negative legacy Pacemaker
464 * return code otherwise
465 *
466 * \note List must be freed using lrmd_list_freeall()
467 */
468 int (*list_standards) (lrmd_t * lrmd, lrmd_list_t ** standards);
469
470 /*!
471 * \brief Execute an alert agent
472 *
473 * \return Legacy Pacemaker return code (if pcmk_ok, the alert is queued in
474 * the executor, and the event callback will be called later with
475 * the result)
476 *
477 * \note Operations on individual alerts (by ID) are guaranteed to occur in
478 * the order the client API is called. Operations on different alerts
479 * are not guaranteed to occur in any specific order.
480 */
481 int (*exec_alert) (lrmd_t *lrmd, const char *alert_id,
482 const char *alert_path, int timeout, /* ms */
483 lrmd_key_value_t *params); /* ownership of params is given up to api here */
484
485 /*!
486 * \brief Get resource metadata for a resource agent, passing parameters
487 *
488 * \param[in] lrmd Executor connection (unused)
489 * \param[in] standard Resource agent class
490 * \param[in] provider Resource agent provider
491 * \param[in] agent Resource agent type
492 * \param[out] output Metadata will be stored here (must not be NULL)
493 * \param[in] options Options to use with any executor API calls (unused)
494 * \param[in] params Parameters to pass to agent via environment
495 *
496 * \return Legacy Pacemaker return code
497 *
498 * \note This is identical to the get_metadata() API call, except parameters
499 * will be passed to the resource agent via environment variables.
500 * \note The API will handle freeing params.
501 */
502 int (*get_metadata_params) (lrmd_t *lrmd, const char *standard,
503 const char *provider, const char *agent,
504 char **output, enum lrmd_call_options options,
505 lrmd_key_value_t *params);
506
507 } lrmd_api_operations_t;
508
509 struct lrmd_s {
510 lrmd_api_operations_t *cmds;
511 void *lrmd_private;
512 };
513
514 static inline const char *
515 lrmd_event_type2str(enum lrmd_callback_event type)
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
516 {
517 switch (type) {
518 case lrmd_event_register:
519 return "register";
520 case lrmd_event_unregister:
521 return "unregister";
522 case lrmd_event_exec_complete:
523 return "exec_complete";
524 case lrmd_event_disconnect:
525 return "disconnect";
526 case lrmd_event_connect:
527 return "connect";
528 case lrmd_event_poke:
529 return "poke";
530 case lrmd_event_new_client:
531 return "new_client";
532 }
533 return "unknown";
534 }
535
536 #ifdef __cplusplus
537 }
538 #endif
539
540 #endif