Coverage Report

Created: 2026-04-04 06:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/pacemaker/include/crm/common/resources_internal.h
Line
Count
Source
1
/*
2
 * Copyright 2024-2026 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__INCLUDED_CRM_COMMON_INTERNAL_H
11
#error "Include <crm/common/internal.h> instead of <resources_internal.h> directly"
12
#endif
13
14
#ifndef PCMK__CRM_COMMON_RESOURCES_INTERNAL__H
15
#define PCMK__CRM_COMMON_RESOURCES_INTERNAL__H
16
17
#include <inttypes.h>                   // UINT64_C
18
#include <stdbool.h>                    // bool
19
#include <stdint.h>                     // uint32_t
20
#include <glib.h>                       // gboolean, gpointer, guint, etc.
21
#include <libxml/tree.h>                // xmlNode
22
23
#include <crm/common/resources.h>       // pcmk_resource_t
24
#include <crm/common/roles.h>           // enum rsc_role_e
25
#include <crm/common/scheduler_types.h> // pcmk_node_t, etc.
26
27
#ifdef __cplusplus
28
extern "C" {
29
#endif
30
31
/*!
32
 * \internal
33
 * \brief Set resource flags
34
 *
35
 * \param[in,out] resource      Resource to set flags for
36
 * \param[in]     flags_to_set  Group of enum pcmk_rsc_flags to set
37
 */
38
#define pcmk__set_rsc_flags(resource, flags_to_set) do {                    \
39
        (resource)->flags = pcmk__set_flags_as(__func__, __LINE__,          \
40
            LOG_TRACE, "Resource", (resource)->id, (resource)->flags,       \
41
            (flags_to_set), #flags_to_set);                                 \
42
    } while (0)
43
44
/*!
45
 * \internal
46
 * \brief Clear resource flags
47
 *
48
 * \param[in,out] resource        Resource to clear flags for
49
 * \param[in]     flags_to_clear  Group of enum pcmk_rsc_flags to clear
50
 */
51
#define pcmk__clear_rsc_flags(resource, flags_to_clear) do {                \
52
        (resource)->flags = pcmk__clear_flags_as(__func__, __LINE__,        \
53
            LOG_TRACE, "Resource", (resource)->id, (resource)->flags,       \
54
            (flags_to_clear), #flags_to_clear);                             \
55
    } while (0)
56
57
//! Resource variants supported by Pacemaker
58
enum pcmk__rsc_variant {
59
    // Order matters: some code compares greater or lesser than
60
    pcmk__rsc_variant_unknown    = -1,  //!< Unknown resource variant
61
    pcmk__rsc_variant_primitive  = 0,   //!< Primitive resource
62
    pcmk__rsc_variant_group      = 1,   //!< Group resource
63
    pcmk__rsc_variant_clone      = 2,   //!< Clone resource
64
    pcmk__rsc_variant_bundle     = 3,   //!< Bundle resource
65
};
66
67
//! How to recover a resource that is incorrectly active on multiple nodes
68
enum pcmk__multiply_active {
69
    pcmk__multiply_active_restart,      //!< Stop on all, start on desired
70
    pcmk__multiply_active_stop,         //!< Stop on all and leave stopped
71
    pcmk__multiply_active_block,        //!< Do nothing to resource
72
    pcmk__multiply_active_unexpected,   //!< Stop unexpected instances
73
};
74
75
//! Resource scheduling flags
76
enum pcmk__rsc_flags {
77
    // No resource flags set (compare with equality rather than bit set)
78
    pcmk__no_rsc_flags               = UINT64_C(0),
79
80
    // Whether resource has been removed from the configuration
81
    pcmk__rsc_removed                = (UINT64_C(1) << 0),
82
83
    /* NOTE: sbd (at least as of 1.5.2) uses pe_rsc_managed which equates to
84
     * this value, so the value should not be changed
85
     */
86
    // Whether resource is managed
87
    pcmk__rsc_managed                = (UINT64_C(1) << 1),
88
89
    // Whether resource is blocked from further action
90
    pcmk__rsc_blocked                = (UINT64_C(1) << 2),
91
92
    // Whether resource has been removed but was launched
93
    pcmk__rsc_removed_launched       = (UINT64_C(1) << 3),
94
95
    // Whether resource has clone notifications enabled
96
    pcmk__rsc_notify                 = (UINT64_C(1) << 4),
97
98
    // Whether resource is not an anonymous clone instance
99
    pcmk__rsc_unique                 = (UINT64_C(1) << 5),
100
101
    // Whether resource's class is "stonith"
102
    pcmk__rsc_fence_device           = (UINT64_C(1) << 6),
103
104
    // Whether resource can be promoted and demoted
105
    pcmk__rsc_promotable             = (UINT64_C(1) << 7),
106
107
    // Whether resource has not yet been assigned to a node
108
    pcmk__rsc_unassigned             = (UINT64_C(1) << 8),
109
110
    // Whether resource is in the process of being assigned to a node
111
    pcmk__rsc_assigning              = (UINT64_C(1) << 9),
112
113
    // Whether resource is in the process of modifying allowed node scores
114
    pcmk__rsc_updating_nodes         = (UINT64_C(1) << 10),
115
116
    // Whether resource is in the process of scheduling actions to restart
117
    pcmk__rsc_restarting             = (UINT64_C(1) << 11),
118
119
    // Whether resource must be stopped (instead of demoted) if it is failed
120
    pcmk__rsc_stop_if_failed         = (UINT64_C(1) << 12),
121
122
    // Whether a reload action has been scheduled for resource
123
    pcmk__rsc_reload                 = (UINT64_C(1) << 13),
124
125
    // Whether resource is a remote connection allowed to run on a remote node
126
    pcmk__rsc_remote_nesting_allowed = (UINT64_C(1) << 14),
127
128
    // Whether resource has \c PCMK_META_CRITICAL meta-attribute enabled
129
    pcmk__rsc_critical               = (UINT64_C(1) << 15),
130
131
    // Whether resource is considered failed
132
    pcmk__rsc_failed                 = (UINT64_C(1) << 16),
133
134
    // Flag for non-scheduler code to use to detect recursion loops
135
    pcmk__rsc_detect_loop            = (UINT64_C(1) << 17),
136
137
    // Whether resource is a Pacemaker Remote connection
138
    pcmk__rsc_is_remote_connection   = (UINT64_C(1) << 18),
139
140
    // Whether resource has pending start action in history
141
    pcmk__rsc_start_pending          = (UINT64_C(1) << 19),
142
143
    // Whether resource is probed only on nodes marked exclusive
144
    pcmk__rsc_exclusive_probes       = (UINT64_C(1) << 20),
145
146
    /*
147
     * Whether resource is multiply active with recovery set to
148
     * \c PCMK_VALUE_STOP_UNEXPECTED
149
     */
150
    pcmk__rsc_stop_unexpected        = (UINT64_C(1) << 22),
151
152
    // Whether resource is allowed to live-migrate
153
    pcmk__rsc_migratable             = (UINT64_C(1) << 23),
154
155
    // Whether resource has an ignorable failure
156
    pcmk__rsc_ignore_failure         = (UINT64_C(1) << 24),
157
158
    // Whether resource is an implicit container resource for a bundle replica
159
    pcmk__rsc_replica_container      = (UINT64_C(1) << 25),
160
161
    // Whether resource, its node, or entire cluster is in maintenance mode
162
    pcmk__rsc_maintenance            = (UINT64_C(1) << 26),
163
164
    // Whether resource can be started or promoted only on quorate nodes
165
    pcmk__rsc_needs_quorum           = (UINT64_C(1) << 28),
166
167
    // Whether resource requires fencing before recovery if on unclean node
168
    pcmk__rsc_needs_fencing          = (UINT64_C(1) << 29),
169
170
    // Whether resource can be started or promoted only on unfenced nodes
171
    pcmk__rsc_needs_unfencing        = (UINT64_C(1) << 30),
172
};
173
174
/*!
175
 * \internal
176
 * \brief Where to look for a resource
177
 */
178
enum pcmk__rsc_node {
179
    //! Nowhere
180
    pcmk__rsc_node_none     = 0,
181
182
    //! Where resource is assigned
183
    pcmk__rsc_node_assigned = (UINT32_C(1) << 0),
184
185
    //! Where resource is running
186
    pcmk__rsc_node_current  = (UINT32_C(1) << 1),
187
188
    //! Where resource is pending
189
    pcmk__rsc_node_pending  = (UINT32_C(1) << 2),
190
};
191
192
//! Resource assignment methods (implementation defined by libpacemaker)
193
typedef struct pcmk__assignment_methods pcmk__assignment_methods_t;
194
195
//! Resource object methods
196
typedef struct {
197
    /*!
198
     * \internal
199
     * \brief Parse variant-specific resource XML from CIB into struct members
200
     *
201
     * \param[in,out] rsc  Partially unpacked resource
202
     *
203
     * \return \c true if resource was unpacked successfully, otherwise \c false
204
     */
205
    bool (*unpack)(pcmk_resource_t *rsc);
206
207
    /*!
208
     * \internal
209
     * \brief Search for a resource ID in a resource and its children
210
     *
211
     * \param[in] rsc      Search this resource and its children
212
     * \param[in] id       Search for this resource ID
213
     * \param[in] on_node  If not NULL, limit search to resources on this node
214
     * \param[in] flags    Group of enum pe_find flags
215
     *
216
     * \return Resource that matches search criteria if any, otherwise NULL
217
     */
218
    pcmk_resource_t *(*find_rsc)(pcmk_resource_t *rsc, const char *search,
219
                                 const pcmk_node_t *node, uint32_t flags);
220
221
    /*!
222
     * \internal
223
     * \brief Check whether a resource is active
224
     *
225
     * \param[in] rsc  Resource to check
226
     * \param[in] all  If \p rsc is collective, all instances must be active
227
     *
228
     * \return TRUE if \p rsc is active, otherwise FALSE
229
     */
230
    bool (*active)(const pcmk_resource_t *rsc, bool all);
231
232
    /*!
233
     * \internal
234
     * \brief Get resource's current or assigned role
235
     *
236
     * \param[in] rsc      Resource to check
237
     * \param[in] current  If \c true, check current role; otherwise, check
238
     *                     assigned role
239
     *
240
     * \return Current or assigned role of \p rsc
241
     */
242
    enum rsc_role_e (*state)(const pcmk_resource_t *rsc, bool current);
243
244
    /*!
245
     * \internal
246
     * \brief List nodes where a resource (or any of its children) is
247
     *
248
     * \param[in]  rsc      Resource to check
249
     * \param[out] list     List to add result to
250
     * \param[in]  target   Which resource conditions to target (group of
251
     *                      enum pcmk__rsc_node flags)
252
     *
253
     * \return If list contains only one node, that node, otherwise NULL
254
     */
255
    pcmk_node_t *(*location)(const pcmk_resource_t *rsc, GList **list,
256
                             uint32_t target);
257
258
    /*!
259
     * \internal
260
     * \brief Free all memory used by a resource
261
     *
262
     * \param[in,out] rsc  Resource to free
263
     */
264
    void (*free)(pcmk_resource_t *rsc);
265
266
    /*!
267
     * \internal
268
     * \brief Increment cluster's instance counts for a resource
269
     *
270
     * Given a resource, increment its cluster's ninstances, disabled_resources,
271
     * and blocked_resources counts for the resource and its descendants.
272
     *
273
     * \param[in,out] rsc  Resource to count
274
     */
275
    void (*count)(pcmk_resource_t *rsc);
276
277
    /*!
278
     * \internal
279
     * \brief Check whether a given resource is in a list of resources
280
     *
281
     * \param[in] rsc           Resource ID to check for
282
     * \param[in] only_rsc      List of resource IDs to check
283
     * \param[in] check_parent  If \c true, check top ancestor as well
284
     *
285
     * \return \c true if \p rsc, its top parent if requested, or \c "*" is in
286
     *         \p only_rsc, or \c false otherwise
287
     */
288
    bool (*is_filtered)(const pcmk_resource_t *rsc, const GList *only_rsc,
289
                        bool check_parent);
290
291
    /*!
292
     * \internal
293
     * \brief Find a node (and optionally count all) where resource is active
294
     *
295
     * \param[in]  rsc          Resource to check
296
     * \param[out] count_all    If not NULL, set this to count of active nodes
297
     * \param[out] count_clean  If not NULL, set this to count of clean nodes
298
     *
299
     * \return A node where the resource is active, preferring the source node
300
     *         if the resource is involved in a partial migration, or a clean,
301
     *         online node if the resource's \c PCMK_META_REQUIRES is
302
     *         \c PCMK_VALUE_QUORUM or \c PCMK_VALUE_NOTHING, otherwise \c NULL.
303
     */
304
    pcmk_node_t *(*active_node)(const pcmk_resource_t *rsc,
305
                                unsigned int *count_all,
306
                                unsigned int *count_clean);
307
308
    /*!
309
     * \internal
310
     * \brief Get maximum resource instances per node
311
     *
312
     * \param[in] rsc  Resource to check
313
     *
314
     * \return Maximum number of \p rsc instances that can be active on one node
315
     */
316
    unsigned int (*max_per_node)(const pcmk_resource_t *rsc);
317
} pcmk__rsc_methods_t;
318
319
// Implementation of pcmk__resource_private_t
320
struct pcmk__resource_private {
321
    enum pcmk__rsc_variant variant; // Resource variant
322
    void *variant_opaque;           // Variant-specific data
323
    char *history_id;               // Resource instance ID in history
324
    GHashTable *meta;               // Resource meta-attributes
325
    GHashTable *utilization;        // Resource utilization attributes
326
    int priority;                   // Priority relative other resources
327
    int promotion_priority;         // Promotion priority on assigned node
328
    enum rsc_role_e orig_role;      // Resource's role at start of transition
329
    enum rsc_role_e next_role;      // Resource's role at end of transition
330
    int stickiness;                 // Extra preference for current node
331
    guint failure_expiration_ms;    // Failures expire after this much time
332
    int ban_after_failures;         // Ban from node after this many failures
333
    guint remote_reconnect_ms;      // Retry interval for remote connections
334
    char *pending_action;           // Pending action in history, if any
335
    const pcmk_node_t *pending_node;// Node on which pending_action is happening
336
    time_t lock_time;               // When shutdown lock started
337
    const pcmk_node_t *lock_node;   // Node that resource is shutdown-locked to
338
    GList *actions;                 // Actions scheduled for resource
339
    GList *children;                // Resource's child resources, if any
340
    pcmk_resource_t *parent;        // Resource's parent resource, if any
341
    pcmk_scheduler_t *scheduler;    // Scheduler data containing resource
342
343
    // Resource configuration (possibly expanded from template)
344
    xmlNode *xml;
345
346
    // Original resource configuration, if using template
347
    xmlNode *orig_xml;
348
349
    // Configuration of resource operations (possibly expanded from template)
350
    xmlNode *ops_xml;
351
352
    /*
353
     * Resource parameters may have node-attribute-based rules, which means the
354
     * values can vary by node. This table has node names as keys and parameter
355
     * name/value tables as values. Use pe_rsc_params() to get the table for a
356
     * given node rather than use this directly.
357
     */
358
    GHashTable *parameter_cache;
359
360
    /* A "launcher" is defined in one of these ways:
361
     *
362
     * - A Pacemaker Remote connection for a guest node or bundle node has its
363
     *   launcher set to the resource that starts the guest or the bundle
364
     *   replica's container.
365
     *
366
     * - If the user configures the PCMK__META_CONTAINER meta-attribute for this
367
     *   resource, the launcher is set to that.
368
     *
369
     *   If the launcher is a Pacemaker Remote connection resource, this
370
     *   resource may run only on the node created by that connection.
371
     *
372
     *   Otherwise, this resource will be colocated with and ordered after the
373
     *   launcher, and failures of this resource will cause the launcher to be
374
     *   recovered instead of this one. This is appropriate for monitoring-only
375
     *   resources that represent a service launched by the other resource.
376
     */
377
    pcmk_resource_t *launcher;
378
379
    // Resources launched by this one, if any (pcmk_resource_t *)
380
    GList *launched;
381
382
    // What to do if the resource is incorrectly active on multiple nodes
383
    enum pcmk__multiply_active multiply_active_policy;
384
385
    /* The assigned node (if not NULL) is the one where the resource *should*
386
     * be active by the end of the current scheduler transition. Only primitive
387
     * resources have an assigned node. This is a node copy (created by
388
     * pe__copy_node()) and so must be freed using pcmk__free_node_copy().
389
     *
390
     * @TODO This should probably be part of the primitive variant data.
391
     */
392
    pcmk_node_t *assigned_node;
393
394
    /* The active nodes are ones where the resource is (or might be, if
395
     * insufficient information is available to be sure) already active at the
396
     * start of the current scheduler transition.
397
     *
398
     * For primitive resources, there should be at most one, but could be more
399
     * if it is (incorrectly) multiply active. For collective resources, this
400
     * combines active nodes of all descendants.
401
     */
402
    GList *active_nodes;
403
404
    /* The next two tables store node copies (created by pe__copy_node()), which
405
     * share some members with the original node objects and must be freed with
406
     * pcmk__free_node_copy().
407
     */
408
409
    // Nodes where resource has been probed (key is node ID, not name)
410
    GHashTable *probed_nodes;
411
412
    // Nodes where resource is allowed to run (key is node ID, not name)
413
    GHashTable *allowed_nodes;
414
415
    // The source node, if migrate_to completed but migrate_from has not
416
    pcmk_node_t *partial_migration_source;
417
418
    // The destination node, if migrate_to completed but migrate_from has not
419
    pcmk_node_t *partial_migration_target;
420
421
    // Source nodes where stop is needed after migrate_from and migrate_to
422
    GList *dangling_migration_sources;
423
424
    /* Pay special attention to whether you want to use with_this_colocations
425
     * and this_with_colocations directly, which include only colocations
426
     * explicitly involving this resource, or call libpacemaker's
427
     * pcmk__with_this_colocations() and pcmk__this_with_colocations()
428
     * functions, which may return relevant colocations involving the resource's
429
     * ancestors as well.
430
     */
431
432
    // Colocations of other resources with this one
433
    GList *with_this_colocations;
434
435
    // Colocations of this resource with others
436
    GList *this_with_colocations;
437
438
    GList *location_constraints;        // Location constraints for resource
439
    GList *ticket_constraints;          // Ticket constraints for resource
440
441
    const pcmk__rsc_methods_t *fns;         // Resource object methods
442
    const pcmk__assignment_methods_t *cmds; // Resource assignment methods
443
};
444
445
void pcmk__free_resource(gpointer user_data);
446
const char *pcmk__multiply_active_text(const pcmk_resource_t *rsc);
447
448
/*!
449
 * \internal
450
 * \brief Get node where resource is currently active (if any)
451
 *
452
 * \param[in] rsc  Resource to check
453
 *
454
 * \return Node that \p rsc is active on, if any, otherwise NULL
455
 */
456
static inline pcmk_node_t *
457
pcmk__current_node(const pcmk_resource_t *rsc)
458
0
{
459
0
    if (rsc == NULL) {
460
0
        return NULL;
461
0
    }
462
0
    return rsc->priv->fns->active_node(rsc, NULL, NULL);
463
0
}
Unexecuted instantiation: scores_fuzzer.c:pcmk__current_node
Unexecuted instantiation: results.c:pcmk__current_node
Unexecuted instantiation: scores.c:pcmk__current_node
Unexecuted instantiation: strings.c:pcmk__current_node
Unexecuted instantiation: utils.c:pcmk__current_node
Unexecuted instantiation: iso8601.c:pcmk__current_node
Unexecuted instantiation: logging.c:pcmk__current_node
Unexecuted instantiation: mainloop.c:pcmk__current_node
Unexecuted instantiation: options.c:pcmk__current_node
Unexecuted instantiation: output.c:pcmk__current_node
Unexecuted instantiation: output_log.c:pcmk__current_node
Unexecuted instantiation: output_text.c:pcmk__current_node
Unexecuted instantiation: output_xml.c:pcmk__current_node
Unexecuted instantiation: patchset_display.c:pcmk__current_node
Unexecuted instantiation: schemas.c:pcmk__current_node
Unexecuted instantiation: xml.c:pcmk__current_node
Unexecuted instantiation: xml_attr.c:pcmk__current_node
Unexecuted instantiation: xml_comment.c:pcmk__current_node
Unexecuted instantiation: xml_display.c:pcmk__current_node
Unexecuted instantiation: xml_element.c:pcmk__current_node
Unexecuted instantiation: xml_idref.c:pcmk__current_node
Unexecuted instantiation: xml_io.c:pcmk__current_node
Unexecuted instantiation: xpath.c:pcmk__current_node
Unexecuted instantiation: acl.c:pcmk__current_node
Unexecuted instantiation: actions.c:pcmk__current_node
Unexecuted instantiation: agents.c:pcmk__current_node
Unexecuted instantiation: cmdline.c:pcmk__current_node
Unexecuted instantiation: digest.c:pcmk__current_node
Unexecuted instantiation: health.c:pcmk__current_node
Unexecuted instantiation: io.c:pcmk__current_node
Unexecuted instantiation: ipc_client.c:pcmk__current_node
Unexecuted instantiation: ipc_common.c:pcmk__current_node
Unexecuted instantiation: ipc_controld.c:pcmk__current_node
Unexecuted instantiation: ipc_pacemakerd.c:pcmk__current_node
Unexecuted instantiation: ipc_schedulerd.c:pcmk__current_node
Unexecuted instantiation: ipc_server.c:pcmk__current_node
Unexecuted instantiation: messages.c:pcmk__current_node
Unexecuted instantiation: nodes.c:pcmk__current_node
Unexecuted instantiation: nvpair.c:pcmk__current_node
Unexecuted instantiation: options_display.c:pcmk__current_node
Unexecuted instantiation: patchset.c:pcmk__current_node
Unexecuted instantiation: procfs.c:pcmk__current_node
Unexecuted instantiation: rules.c:pcmk__current_node
Unexecuted instantiation: servers.c:pcmk__current_node
Unexecuted instantiation: cib.c:pcmk__current_node
Unexecuted instantiation: ipc_attrd.c:pcmk__current_node
Unexecuted instantiation: pid.c:pcmk__current_node
Unexecuted instantiation: attrs.c:pcmk__current_node
Unexecuted instantiation: strings_fuzzer.c:pcmk__current_node
Unexecuted instantiation: cib_file_fuzzer.c:pcmk__current_node
Unexecuted instantiation: cib_client.c:pcmk__current_node
Unexecuted instantiation: cib_file.c:pcmk__current_node
Unexecuted instantiation: cib_native.c:pcmk__current_node
Unexecuted instantiation: cib_ops.c:pcmk__current_node
Unexecuted instantiation: cib_remote.c:pcmk__current_node
Unexecuted instantiation: cib_utils.c:pcmk__current_node
Unexecuted instantiation: remote.c:pcmk__current_node
Unexecuted instantiation: tls.c:pcmk__current_node
Unexecuted instantiation: watchdog.c:pcmk__current_node
Unexecuted instantiation: iso8601_fuzzer.c:pcmk__current_node
464
465
#ifdef __cplusplus
466
}
467
#endif
468
469
#endif // PCMK__CRM_COMMON_RESOURCES_INTERNAL__H