1 /* 2 * Copyright 2010-2022 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 PCMK__CRM_SERVICES__H 11 # define PCMK__CRM_SERVICES__H 12 13 14 # include <glib.h> 15 # include <stdio.h> 16 # include <stdint.h> 17 # include <string.h> 18 # include <stdbool.h> 19 # include <sys/types.h> 20 21 # include <crm_config.h> // OCF_ROOT_DIR 22 # include <crm/common/agents.h> 23 # include <crm/common/results.h> 24 25 #ifdef __cplusplus 26 extern "C" { 27 #endif 28 29 /*! 30 * \file 31 * \brief Services API 32 * \ingroup core 33 */ 34 35 /* TODO: Autodetect these two ?*/ 36 # ifndef SYSTEMCTL 37 # define SYSTEMCTL "/bin/systemctl" 38 # endif 39 40 /* This is the string passed in the OCF_EXIT_REASON_PREFIX environment variable. 41 * The stderr output that occurs after this prefix is encountered is considered 42 * the exit reason for a completed operation. 43 */ 44 #define PCMK_OCF_REASON_PREFIX "ocf-exit-reason:" 45 46 // Agent version to use if agent doesn't specify one 47 #define PCMK_DEFAULT_AGENT_VERSION "0.1" 48 49 enum lsb_exitcode { 50 PCMK_LSB_OK = 0, 51 PCMK_LSB_UNKNOWN_ERROR = 1, 52 PCMK_LSB_INVALID_PARAM = 2, 53 PCMK_LSB_UNIMPLEMENT_FEATURE = 3, 54 PCMK_LSB_INSUFFICIENT_PRIV = 4, 55 PCMK_LSB_NOT_INSTALLED = 5, 56 PCMK_LSB_NOT_CONFIGURED = 6, 57 PCMK_LSB_NOT_RUNNING = 7, 58 }; 59 60 // LSB uses different return codes for status actions 61 enum lsb_status_exitcode { 62 PCMK_LSB_STATUS_OK = 0, 63 PCMK_LSB_STATUS_VAR_PID = 1, 64 PCMK_LSB_STATUS_VAR_LOCK = 2, 65 PCMK_LSB_STATUS_NOT_RUNNING = 3, 66 PCMK_LSB_STATUS_UNKNOWN = 4, 67 68 /* custom codes should be in the 150-199 range reserved for application use */ 69 PCMK_LSB_STATUS_NOT_INSTALLED = 150, 70 PCMK_LSB_STATUS_INSUFFICIENT_PRIV = 151, 71 }; 72 73 enum nagios_exitcode { 74 NAGIOS_STATE_OK = 0, 75 NAGIOS_STATE_WARNING = 1, 76 NAGIOS_STATE_CRITICAL = 2, 77 NAGIOS_STATE_UNKNOWN = 3, 78 79 /* This is a custom Pacemaker value (not a nagios convention), used as an 80 * intermediate value between the services library and the executor, so the 81 * executor can map it to the corresponding OCF code. 82 */ 83 NAGIOS_INSUFFICIENT_PRIV = 100, 84 85 #if !defined(PCMK_ALLOW_DEPRECATED) || (PCMK_ALLOW_DEPRECATED == 1) 86 NAGIOS_STATE_DEPENDENT = 4, //! \deprecated Unused 87 NAGIOS_NOT_INSTALLED = 101, //! \deprecated Unused 88 #endif 89 }; 90 91 enum svc_action_flags { 92 /* On timeout, only kill pid, do not kill entire pid group */ 93 SVC_ACTION_LEAVE_GROUP = 0x01, 94 SVC_ACTION_NON_BLOCKED = 0x02, 95 }; 96 97 typedef struct svc_action_private_s svc_action_private_t; 98 99 /*! 100 * \brief Object for executing external actions 101 * 102 * \note This object should never be instantiated directly, but instead created 103 * using one of the constructor functions (resources_action_create() for 104 * resource agents, services_alert_create() for alert agents, or 105 * services_action_create_generic() for generic executables). Similarly, 106 * do not use sizeof() on this struct. 107 */ 108 /* 109 * NOTE: Internally, services__create_resource_action() is preferable to 110 * resources_action_create(). 111 */ 112 typedef struct svc_action_s { 113 /*! Operation key (<resource>_<action>_<interval>) for resource actions, 114 * XML ID for alert actions, or NULL for generic actions 115 */ 116 char *id; 117 118 //! XML ID of resource being executed for resource actions, otherwise NULL 119 char *rsc; 120 121 //! Name of action being executed for resource actions, otherwise NULL 122 char *action; 123 124 //! Action interval for recurring resource actions, otherwise 0 125 guint interval_ms; 126 127 //! Resource standard for resource actions, otherwise NULL 128 char *standard; 129 130 //! Resource provider for resource actions that require it, otherwise NULL 131 char *provider; 132 133 //! Resource agent name for resource actions, otherwise NULL 134 char *agent; 135 136 int timeout; //!< Action timeout (in milliseconds) 137 138 /*! A hash table of name/value pairs to use as parameters for resource and 139 * alert actions, otherwise NULL. These will be used to set environment 140 * variables for non-fencing resource agents and alert agents, and to send 141 * stdin to fence agents. 142 */ 143 GHashTable *params; 144 145 int rc; //!< Exit status of action (set by library upon completion) 146 147 //!@{ 148 //! This field should be treated as internal to Pacemaker 149 int pid; // Process ID of child 150 int cancel; // Whether this is a cancellation of a recurring action 151 //!@} 152 153 int status; //!< Execution status (enum pcmk_exec_status set by library) 154 155 /*! Action counter (set by library for resource actions, or by caller 156 * otherwise) 157 */ 158 int sequence; 159 160 //!@{ 161 //! This field should be treated as internal to Pacemaker 162 int expected_rc; // Unused 163 int synchronous; // Whether execution should be synchronous (blocking) 164 //!@} 165 166 enum svc_action_flags flags; //!< Flag group of enum svc_action_flags 167 char *stderr_data; //!< Action stderr (set by library) 168 char *stdout_data; //!< Action stdout (set by library) 169 void *cb_data; //!< For caller's use (not used by library) 170 171 //! This field should be treated as internal to Pacemaker 172 svc_action_private_t *opaque; 173 } svc_action_t; 174 175 /*! 176 * \brief Get a list of files or directories in a given path 177 * 178 * \param[in] root Full path to a directory to read 179 * \param[in] files Return list of files if TRUE or directories if FALSE 180 * \param[in] executable If TRUE and files is TRUE, only return executable files 181 * 182 * \return List of what was found as char * items. 183 * \note The caller is responsibile for freeing the result with 184 * g_list_free_full(list, free). 185 */ 186 GList *get_directory_list(const char *root, gboolean files, 187 gboolean executable); 188 189 /*! 190 * \brief Get a list of providers 191 * 192 * \param[in] standard List providers of this resource agent standard 193 * 194 * \return List of providers as char * list items (or NULL if standard does not 195 * support providers) 196 * \note The caller is responsible for freeing the result using 197 * g_list_free_full(list, free). 198 */ 199 GList *resources_list_providers(const char *standard); 200 201 /*! 202 * \brief Get a list of resource agents 203 * 204 * \param[in] standard List agents of this standard (or NULL for all) 205 * \param[in] provider List agents of this provider (or NULL for all) 206 * 207 * \return List of resource agents as char * items. 208 * \note The caller is responsible for freeing the result using 209 * g_list_free_full(list, free). 210 */ 211 GList *resources_list_agents(const char *standard, const char *provider); 212 213 /*! 214 * Get list of available standards 215 * 216 * \return List of resource standards as char * items. 217 * \note The caller is responsible for freeing the result using 218 * g_list_free_full(list, free). 219 */ 220 GList *resources_list_standards(void); 221 222 /*! 223 * \brief Check whether a resource agent exists on the local host 224 * 225 * \param[in] standard Resource agent standard of agent to check 226 * \param[in] provider Provider of agent to check (or NULL) 227 * \param[in] agent Name of agent to check 228 * 229 * \return TRUE if agent exists locally, otherwise FALSE 230 */ 231 gboolean resources_agent_exists(const char *standard, const char *provider, 232 const char *agent); 233 234 /*! 235 * \brief Create a new resource action 236 * 237 * \param[in] name Name of resource that action is for 238 * \param[in] standard Resource agent standard 239 * \param[in] provider Resource agent provider 240 * \param[in] agent Resource agent name 241 * \param[in] action Name of action to create 242 * \param[in] interval_ms How often to repeat action (if 0, execute once) 243 * \param[in] timeout Error if not complete within this time (ms) 244 * \param[in,out] params Action parameters 245 * \param[in] flags Group of enum svc_action_flags 246 * 247 * \return Newly allocated action 248 * \note This function assumes ownership of (and may free) \p params. 249 * \note The caller is responsible for freeing the return value using 250 * services_action_free(). 251 */ 252 svc_action_t *resources_action_create(const char *name, const char *standard, 253 const char *provider, const char *agent, 254 const char *action, guint interval_ms, 255 int timeout, GHashTable *params, 256 enum svc_action_flags flags); 257 258 /*! 259 * \brief Reschedule a recurring action for immediate execution 260 * 261 * \param[in] name Name of resource that action is for 262 * \param[in] action Action's name 263 * \param[in] interval_ms Action's interval (in milliseconds) 264 * 265 * \return TRUE on success, otherwise FALSE 266 */ 267 gboolean services_action_kick(const char *name, const char *action, 268 guint interval_ms); 269 270 const char *resources_find_service_class(const char *agent); 271 272 /*! 273 * \brief Request execution of an arbitrary command 274 * 275 * This API has useful infrastructure in place to be able to run a command 276 * in the background and get notified via a callback when the command finishes. 277 * 278 * \param[in] exec Full path to command executable 279 * \param[in] args NULL-terminated list of arguments to pass to command 280 * 281 * \return Newly allocated action object 282 */ 283 svc_action_t *services_action_create_generic(const char *exec, 284 const char *args[]); 285 286 void services_action_cleanup(svc_action_t *op); 287 void services_action_free(svc_action_t *op); 288 int services_action_user(svc_action_t *op, const char *user); 289 gboolean services_action_sync(svc_action_t *op); 290 291 /*! 292 * \brief Run an action asynchronously, with callback after process is forked 293 * 294 * \param[in,out] op Action to run 295 * \param[in] action_callback Function to call when action completes 296 * (if NULL, any previously set callback will 297 * continue to be used) 298 * \param[in] action_fork_callback Function to call after child process is 299 * forked for action (if NULL, any 300 * previously set callback will continue to 301 * be used) 302 * 303 * \retval TRUE if the caller should not free or otherwise use \p op again, 304 * because one of these conditions is true: 305 * 306 * * \p op is NULL. 307 * * The action was successfully initiated, in which case 308 * \p action_fork_callback has been called, but \p action_callback has 309 * not (it will be called when the action completes). 310 * * The action's ID matched an existing recurring action. The existing 311 * action has taken over the callback and callback data from \p op 312 * and has been re-initiated asynchronously, and \p op has been freed. 313 * * Another action for the same resource is in flight, and \p op will 314 * be blocked until it completes. 315 * * The action could not be initiated, and is either non-recurring or 316 * being cancelled. \p action_fork_callback has not been called, but 317 * \p action_callback has, and \p op has been freed. 318 * 319 * \retval FALSE if \op is still valid, because the action cannot be initiated, 320 * and is a recurring action that is not being cancelled. 321 * \p action_fork_callback has not been called, but \p action_callback 322 * has, and a timer has been set for the next invocation of \p op. 323 */ 324 gboolean services_action_async_fork_notify(svc_action_t *op, 325 void (*action_callback) (svc_action_t *), 326 void (*action_fork_callback) (svc_action_t *)); 327 328 /*! 329 * \brief Request asynchronous execution of an action 330 * 331 * \param[in,out] op Action to execute 332 * \param[in] action_callback Function to call when the action completes 333 * (if NULL, any previously set callback will 334 * continue to be used) 335 * 336 * \retval TRUE if the caller should not free or otherwise use \p op again, 337 * because one of these conditions is true: 338 * 339 * * \p op is NULL. 340 * * The action was successfully initiated, in which case 341 * \p action_callback has not been called (it will be called when the 342 * action completes). 343 * * The action's ID matched an existing recurring action. The existing 344 * action has taken over the callback and callback data from \p op 345 * and has been re-initiated asynchronously, and \p op has been freed. 346 * * Another action for the same resource is in flight, and \p op will 347 * be blocked until it completes. 348 * * The action could not be initiated, and is either non-recurring or 349 * being cancelled. \p action_callback has been called, and \p op has 350 * been freed. 351 * 352 * \retval FALSE if \op is still valid, because the action cannot be initiated, 353 * and is a recurring action that is not being cancelled. 354 * \p action_callback has been called, and a timer has been set for the 355 * next invocation of \p op. 356 */ 357 gboolean services_action_async(svc_action_t *op, 358 void (*action_callback) (svc_action_t *)); 359 360 gboolean services_action_cancel(const char *name, const char *action, 361 guint interval_ms); 362 363 /* functions for alert agents */ 364 svc_action_t *services_alert_create(const char *id, const char *exec, 365 int timeout, GHashTable *params, 366 int sequence, void *cb_data); 367 gboolean services_alert_async(svc_action_t *action, 368 void (*cb)(svc_action_t *op)); 369 370 enum ocf_exitcode services_result2ocf(const char *standard, const char *action, 371 int exit_status); 372 373 static inline const char *services_ocf_exitcode_str(enum ocf_exitcode code) { /* */ 374 switch (code) { 375 case PCMK_OCF_OK: 376 return "ok"; 377 case PCMK_OCF_UNKNOWN_ERROR: 378 return "error"; 379 case PCMK_OCF_INVALID_PARAM: 380 return "invalid parameter"; 381 case PCMK_OCF_UNIMPLEMENT_FEATURE: 382 return "unimplemented feature"; 383 case PCMK_OCF_INSUFFICIENT_PRIV: 384 return "insufficient privileges"; 385 case PCMK_OCF_NOT_INSTALLED: 386 return "not installed"; 387 case PCMK_OCF_NOT_CONFIGURED: 388 return "not configured"; 389 case PCMK_OCF_NOT_RUNNING: 390 return "not running"; 391 case PCMK_OCF_RUNNING_PROMOTED: 392 return "promoted"; 393 case PCMK_OCF_FAILED_PROMOTED: 394 return "promoted (failed)"; 395 case PCMK_OCF_DEGRADED: 396 return "OCF_DEGRADED"; 397 case PCMK_OCF_DEGRADED_PROMOTED: 398 return "promoted (degraded)"; 399 400 #if !defined(PCMK_ALLOW_DEPRECATED) || (PCMK_ALLOW_DEPRECATED == 1) 401 case PCMK_OCF_NOT_SUPPORTED: 402 return "not supported (DEPRECATED STATUS)"; 403 case PCMK_OCF_CANCELLED: 404 return "cancelled (DEPRECATED STATUS)"; 405 case PCMK_OCF_OTHER_ERROR: 406 return "other error (DEPRECATED STATUS)"; 407 case PCMK_OCF_SIGNAL: 408 return "interrupted by signal (DEPRECATED STATUS)"; 409 case PCMK_OCF_PENDING: 410 return "pending (DEPRECATED STATUS)"; 411 case PCMK_OCF_TIMEOUT: 412 return "timeout (DEPRECATED STATUS)"; 413 #endif 414 default: 415 return "unknown"; 416 } 417 } 418 419 #if !defined(PCMK_ALLOW_DEPRECATED) || (PCMK_ALLOW_DEPRECATED == 1) 420 #include <crm/services_compat.h> 421 #endif 422 423 # ifdef __cplusplus 424 } 425 # endif 426 427 #endif /* __PCMK_SERVICES__ */