pacemaker 2.1.8-2.1.8
Scalable High-Availability cluster resource manager
Loading...
Searching...
No Matches
internal.h
Go to the documentation of this file.
1/*
2 * Copyright 2015-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#ifndef CRM_COMMON_INTERNAL__H
11#define CRM_COMMON_INTERNAL__H
12
13#include <unistd.h> // pid_t, getpid()
14#include <stdbool.h> // bool
15#include <stdint.h> // uint8_t, uint64_t
16
17#include <glib.h> // guint, GList, GHashTable
18#include <libxml/tree.h> // xmlNode
19
20#include <crm/common/util.h> // crm_strdup_printf()
21#include <crm/common/logging.h> // do_crm_log_unlikely(), etc.
22#include <crm/common/mainloop.h> // mainloop_io_t, struct ipc_client_callbacks
34
35/* This says whether the current application is a Pacemaker daemon or not,
36 * and is used to change default logging settings such as whether to log to
37 * stderr, etc., as well as a few other details such as whether blackbox signal
38 * handling is enabled.
39 *
40 * It is set when logging is initialized, and does not need to be set directly.
41 */
42extern bool pcmk__is_daemon;
43
45extern char *pcmk__our_nodename;
46
47// Number of elements in a statically defined array
48#define PCMK__NELEM(a) ((int) (sizeof(a)/sizeof(a[0])) )
49
50#if SUPPORT_CIBSECRETS
51/* internal CIB utilities (from cib_secrets.c) */
52
53int pcmk__substitute_secrets(const char *rsc_id, GHashTable *params);
54#endif
55
56
57/* internal main loop utilities (from mainloop.c) */
58
59int pcmk__add_mainloop_ipc(crm_ipc_t *ipc, int priority, void *userdata,
60 const struct ipc_client_callbacks *callbacks,
61 mainloop_io_t **source);
63
64
65/* internal node-related XML utilities (from nodes.c) */
66
75void pcmk__xe_add_node(xmlNode *xml, const char *node, int nodeid);
76
77
78/* internal name/value utilities (from nvpair.c) */
79
80int pcmk__scan_nvpair(const char *input, char **name, char **value);
81char *pcmk__format_nvpair(const char *name, const char *value,
82 const char *units);
83
92void
93pcmk__xe_set_bool_attr(xmlNodePtr node, const char *name, bool value);
94
105bool
106pcmk__xe_attr_is_true(const xmlNode *node, const char *name);
107
123int
124pcmk__xe_get_bool_attr(const xmlNode *node, const char *name, bool *value);
125
126
127/* internal procfs utilities (from procfs.c) */
128
129pid_t pcmk__procfs_pid_of(const char *name);
130unsigned int pcmk__procfs_num_cores(void);
131int pcmk__procfs_pid2path(pid_t pid, char path[], size_t path_size);
132bool pcmk__procfs_has_pids(void);
133
134/* internal functions related to process IDs (from pid.c) */
135
152int pcmk__pid_active(pid_t pid, const char *daemon);
153
154int pcmk__read_pidfile(const char *filename, pid_t *pid);
155int pcmk__pidfile_matches(const char *filename, pid_t expected_pid,
156 const char *expected_name, pid_t *pid);
157int pcmk__lock_pidfile(const char *filename, const char *name);
158
159
160// bitwise arithmetic utilities
161
177static inline uint64_t
178pcmk__set_flags_as(const char *function, int line, uint8_t log_level,
179 const char *flag_type, const char *target,
180 uint64_t flag_group, uint64_t flags, const char *flags_str)
181{
182 uint64_t result = flag_group | flags;
183
184 if (result != flag_group) {
185 do_crm_log_unlikely(log_level,
186 "%s flags %#.8llx (%s) for %s set by %s:%d",
187 ((flag_type == NULL)? "Group of" : flag_type),
188 (unsigned long long) flags,
189 ((flags_str == NULL)? "flags" : flags_str),
190 ((target == NULL)? "target" : target),
191 function, line);
192 }
193 return result;
194}
195
211static inline uint64_t
212pcmk__clear_flags_as(const char *function, int line, uint8_t log_level,
213 const char *flag_type, const char *target,
214 uint64_t flag_group, uint64_t flags, const char *flags_str)
215{
216 uint64_t result = flag_group & ~flags;
217
218 if (result != flag_group) {
219 do_crm_log_unlikely(log_level,
220 "%s flags %#.8llx (%s) for %s cleared by %s:%d",
221 ((flag_type == NULL)? "Group of" : flag_type),
222 (unsigned long long) flags,
223 ((flags_str == NULL)? "flags" : flags_str),
224 ((target == NULL)? "target" : target),
225 function, line);
226 }
227 return result;
228}
229
239static inline const char *
240pcmk__flag_text(uint64_t flag_group, uint64_t flags)
241{
242 return pcmk__btoa(pcmk_all_flags_set(flag_group, flags));
243}
244
245
246// miscellaneous utilities (from utils.c)
247
248void pcmk__daemonize(const char *name, const char *pidfile);
249void pcmk__panic(const char *origin);
250pid_t pcmk__locate_sbd(void);
251void pcmk__sleep_ms(unsigned int ms);
252
253extern int pcmk__score_red;
254extern int pcmk__score_green;
255extern int pcmk__score_yellow;
256
272static inline void *
273pcmk__assert_alloc_as(const char *file, const char *function, uint32_t line,
274 size_t nmemb, size_t size)
275{
276 void *ptr = calloc(nmemb, size);
277
278 if (ptr == NULL) {
279 crm_abort(file, function, line, "Out of memory", FALSE, TRUE);
281 }
282 return ptr;
283}
284
297#define pcmk__assert_alloc(nmemb, size) \
298 pcmk__assert_alloc_as(__FILE__, __func__, __LINE__, nmemb, size)
299
312static inline void *
313pcmk__realloc(void *ptr, size_t size)
314{
315 void *new_ptr;
316
317 // realloc(p, 0) can replace free(p) but this wrapper can't
318 CRM_ASSERT(size > 0);
319
320 new_ptr = realloc(ptr, size);
321 if (new_ptr == NULL) {
322 free(ptr);
323 abort();
324 }
325 return new_ptr;
326}
327
328static inline char *
329pcmk__getpid_s(void)
330{
331 return crm_strdup_printf("%lu", (unsigned long) getpid());
332}
333
334// More efficient than g_list_length(list) == 1
335static inline bool
336pcmk__list_of_1(GList *list)
337{
338 return list && (list->next == NULL);
339}
340
341// More efficient than g_list_length(list) > 1
342static inline bool
343pcmk__list_of_multiple(GList *list)
344{
345 return list && (list->next != NULL);
346}
347
348/* convenience functions for failure-related node attributes */
349
350#define PCMK__FAIL_COUNT_PREFIX "fail-count"
351#define PCMK__LAST_FAILURE_PREFIX "last-failure"
352
370static inline char *
371pcmk__fail_attr_name(const char *prefix, const char *rsc_id, const char *op,
372 guint interval_ms)
373{
374 CRM_CHECK(prefix && rsc_id && op, return NULL);
375 return crm_strdup_printf("%s-%s#%s_%u", prefix, rsc_id, op, interval_ms);
376}
377
378static inline char *
379pcmk__failcount_name(const char *rsc_id, const char *op, guint interval_ms)
380{
381 return pcmk__fail_attr_name(PCMK__FAIL_COUNT_PREFIX, rsc_id, op,
382 interval_ms);
383}
384
385static inline char *
386pcmk__lastfailure_name(const char *rsc_id, const char *op, guint interval_ms)
387{
388 return pcmk__fail_attr_name(PCMK__LAST_FAILURE_PREFIX, rsc_id, op,
389 interval_ms);
390}
391
392// internal resource agent functions (from agents.c)
393int pcmk__effective_rc(int rc);
394
395#endif /* CRM_COMMON_INTERNAL__H */
const char * path
Definition cib.c:28
const char * name
Definition cib.c:26
int pcmk__substitute_secrets(const char *rsc_id, GHashTable *params)
Definition cib_secrets.c:96
int pcmk__xe_get_bool_attr(const xmlNode *node, const char *name, bool *value)
Definition nvpair.c:909
char * pcmk__our_nodename
Node name of the local node.
Definition logging.c:48
guint pcmk__mainloop_timer_get_period(const mainloop_timer_t *timer)
Get period for mainloop timer.
Definition mainloop.c:909
void pcmk__xe_add_node(xmlNode *xml, const char *node, int nodeid)
Definition nodes.c:127
int pcmk__score_yellow
Definition scores.c:22
unsigned int pcmk__procfs_num_cores(void)
Definition procfs.c:145
int pcmk__add_mainloop_ipc(crm_ipc_t *ipc, int priority, void *userdata, const struct ipc_client_callbacks *callbacks, mainloop_io_t **source)
Connect to IPC and add it as a main loop source.
Definition mainloop.c:863
bool pcmk__xe_attr_is_true(const xmlNode *node, const char *name)
Definition nvpair.c:936
pid_t pcmk__locate_sbd(void)
Definition watchdog.c:202
int pcmk__effective_rc(int rc)
Definition agents.c:72
#define PCMK__LAST_FAILURE_PREFIX
Definition internal.h:351
#define PCMK__FAIL_COUNT_PREFIX
Definition internal.h:350
int pcmk__scan_nvpair(const char *input, char **name, char **value)
Definition nvpair.c:220
char * pcmk__format_nvpair(const char *name, const char *value, const char *units)
Definition nvpair.c:283
bool pcmk__procfs_has_pids(void)
Definition procfs.c:211
int pcmk__read_pidfile(const char *filename, pid_t *pid)
Definition pid.c:111
void pcmk__daemonize(const char *name, const char *pidfile)
Definition utils.c:380
int pcmk__score_green
Definition scores.c:21
int pcmk__pid_active(pid_t pid, const char *daemon)
Definition pid.c:23
void pcmk__xe_set_bool_attr(xmlNodePtr node, const char *name, bool value)
Definition nvpair.c:903
bool pcmk__is_daemon
Definition logging.c:47
void pcmk__sleep_ms(unsigned int ms)
Definition utils.c:471
int pcmk__pidfile_matches(const char *filename, pid_t expected_pid, const char *expected_name, pid_t *pid)
Definition pid.c:172
void pcmk__panic(const char *origin)
Definition watchdog.c:169
pid_t pcmk__procfs_pid_of(const char *name)
Definition procfs.c:111
int pcmk__lock_pidfile(const char *filename, const char *name)
Definition pid.c:214
int pcmk__score_red
Definition scores.c:20
int pcmk__procfs_pid2path(pid_t pid, char path[], size_t path_size)
Definition procfs.c:179
uint64_t flags
Definition remote.c:3
Utility functions.
void crm_abort(const char *file, const char *function, int line, const char *condition, gboolean do_core, gboolean do_fork)
Definition utils.c:356
char * crm_strdup_printf(char const *format,...) G_GNUC_PRINTF(1
uint32_t size
Definition cpg.c:4
uint32_t pid
Definition cpg.c:1
struct crm_ipc_s crm_ipc_t
Definition ipc.h:184
Wrappers for and extensions to libqb logging.
#define do_crm_log_unlikely(level, fmt, args...)
Log a message that is likely to be filtered out.
Definition logging.h:206
#define CRM_CHECK(expr, failure_action)
Definition logging.h:245
Wrappers for and extensions to glib mainloop.
struct mainloop_timer_s mainloop_timer_t
Definition mainloop.h:39
struct mainloop_io_s mainloop_io_t
Definition mainloop.h:35
xmlNode * input
pcmk__action_result_t result
Definition pcmk_fence.c:35
const char * target
Definition pcmk_fence.c:29
#define CRM_ASSERT(expr)
Definition results.h:42
@ CRM_EX_OSERR
External (OS/environmental) problem.
Definition results.h:276
_Noreturn crm_exit_t crm_exit(crm_exit_t rc)
Definition results.c:936