pacemaker 3.0.1-16e74fc4da
Scalable High-Availability cluster resource manager
Loading...
Searching...
No Matches
internal.h
Go to the documentation of this file.
1/*
2 * Copyright 2015-2025 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_COMMON_INTERNAL__H
11#define PCMK__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#include <inttypes.h> // PRIu64
17
18#include <glib.h> // guint, GList, GHashTable
19#include <libxml/tree.h> // xmlNode
20
21#include <crm/common/logging.h> // do_crm_log_unlikely(), etc.
22#include <crm/common/mainloop.h> // mainloop_io_t, struct ipc_client_callbacks
23#include <crm/common/strings.h> // crm_strdup_printf()
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
40/* This says whether the current application is a Pacemaker daemon or not,
41 * and is used to change default logging settings such as whether to log to
42 * stderr, etc., as well as a few other details such as whether blackbox signal
43 * handling is enabled.
44 *
45 * It is set when logging is initialized, and does not need to be set directly.
46 */
47extern bool pcmk__is_daemon;
48
49// Number of elements in a statically defined array
50#define PCMK__NELEM(a) ((int) (sizeof(a)/sizeof(a[0])) )
51
52#if PCMK__ENABLE_CIBSECRETS
53/* internal CIB utilities (from cib_secrets.c) */
54
55int pcmk__substitute_secrets(const char *rsc_id, GHashTable *params);
56#endif
57
58
59/* internal main loop utilities (from mainloop.c) */
60
61int pcmk__add_mainloop_ipc(crm_ipc_t *ipc, int priority, void *userdata,
62 const struct ipc_client_callbacks *callbacks,
63 mainloop_io_t **source);
65
66
67/* internal name/value utilities (from nvpair.c) */
68
69int pcmk__scan_nvpair(const gchar *input, gchar **name, gchar **value);
70char *pcmk__format_nvpair(const char *name, const char *value,
71 const char *units);
72
73/* internal procfs utilities (from procfs.c) */
74
75pid_t pcmk__procfs_pid_of(const char *name);
76unsigned int pcmk__procfs_num_cores(void);
77int pcmk__procfs_pid2path(pid_t pid, char path[], size_t path_size);
78bool pcmk__procfs_has_pids(void);
79DIR *pcmk__procfs_fd_dir(void);
80void pcmk__sysrq_trigger(char t);
81bool pcmk__throttle_cib_load(const char *server, float *load);
82bool pcmk__throttle_load_avg(float *load);
83
84/* internal functions related to process IDs (from pid.c) */
85
102int pcmk__pid_active(pid_t pid, const char *daemon);
103
104int pcmk__read_pidfile(const char *filename, pid_t *pid);
105int pcmk__pidfile_matches(const char *filename, pid_t expected_pid,
106 const char *expected_name, pid_t *pid);
107int pcmk__lock_pidfile(const char *filename, const char *name);
108
109
110// bitwise arithmetic utilities
111
127static inline uint64_t
128pcmk__set_flags_as(const char *function, int line, uint8_t log_level,
129 const char *flag_type, const char *target,
130 uint64_t flag_group, uint64_t flags, const char *flags_str)
131{
132 uint64_t result = flag_group | flags;
133
134 if (result != flag_group) {
135 do_crm_log_unlikely(log_level,
136 "%s flags %#.8" PRIx64 " (%s) for %s set by %s:%d",
137 pcmk__s(flag_type, "Group of"), flags,
138 pcmk__s(flags_str, "flags"),
139 pcmk__s(target, "target"), function, line);
140 }
141 return result;
142}
143
159static inline uint64_t
160pcmk__clear_flags_as(const char *function, int line, uint8_t log_level,
161 const char *flag_type, const char *target,
162 uint64_t flag_group, uint64_t flags, const char *flags_str)
163{
164 uint64_t result = flag_group & ~flags;
165
166 if (result != flag_group) {
167 do_crm_log_unlikely(log_level,
168 "%s flags %#.8" PRIx64
169 " (%s) for %s cleared by %s:%d",
170 pcmk__s(flag_type, "Group of"), flags,
171 pcmk__s(flags_str, "flags"),
172 pcmk__s(target, "target"), function, line);
173 }
174 return result;
175}
176
186static inline const char *
187pcmk__flag_text(uint64_t flag_group, uint64_t flags)
188{
189 return pcmk__btoa(pcmk_all_flags_set(flag_group, flags));
190}
191
192
193// miscellaneous utilities (from utils.c)
194
195void pcmk__daemonize(const char *name, const char *pidfile);
196void pcmk__panic(const char *reason);
197pid_t pcmk__locate_sbd(void);
198void pcmk__sleep_ms(unsigned int ms);
199guint pcmk__create_timer(guint interval_ms, GSourceFunc fn, gpointer data);
200guint pcmk__timeout_ms2s(guint timeout_ms);
201
202extern int pcmk__score_red;
203extern int pcmk__score_green;
204extern int pcmk__score_yellow;
205
221static inline void *
222pcmk__assert_alloc_as(const char *file, const char *function, uint32_t line,
223 size_t nmemb, size_t size)
224{
225 void *ptr = calloc(nmemb, size);
226
227 if (ptr == NULL) {
228 crm_abort(file, function, line, "Out of memory", FALSE, TRUE);
230 }
231 return ptr;
232}
233
246#define pcmk__assert_alloc(nmemb, size) \
247 pcmk__assert_alloc_as(__FILE__, __func__, __LINE__, nmemb, size)
248
261static inline void *
262pcmk__realloc(void *ptr, size_t size)
263{
264 void *new_ptr;
265
266 // realloc(p, 0) can replace free(p) but this wrapper can't
267 pcmk__assert(size > 0);
268
269 new_ptr = realloc(ptr, size);
270 if (new_ptr == NULL) {
271 free(ptr);
272 abort();
273 }
274 return new_ptr;
275}
276
277static inline char *
278pcmk__getpid_s(void)
279{
280 return crm_strdup_printf("%lu", (unsigned long) getpid());
281}
282
283// More efficient than g_list_length(list) == 1
284static inline bool
285pcmk__list_of_1(GList *list)
286{
287 return list && (list->next == NULL);
288}
289
290// More efficient than g_list_length(list) > 1
291static inline bool
292pcmk__list_of_multiple(GList *list)
293{
294 return list && (list->next != NULL);
295}
296
297/* convenience functions for failure-related node attributes */
298
299#define PCMK__FAIL_COUNT_PREFIX "fail-count"
300#define PCMK__LAST_FAILURE_PREFIX "last-failure"
301
319static inline char *
320pcmk__fail_attr_name(const char *prefix, const char *rsc_id, const char *op,
321 guint interval_ms)
322{
323 CRM_CHECK(prefix && rsc_id && op, return NULL);
324 return crm_strdup_printf("%s-%s#%s_%u", prefix, rsc_id, op, interval_ms);
325}
326
327static inline char *
328pcmk__failcount_name(const char *rsc_id, const char *op, guint interval_ms)
329{
330 return pcmk__fail_attr_name(PCMK__FAIL_COUNT_PREFIX, rsc_id, op,
331 interval_ms);
332}
333
334static inline char *
335pcmk__lastfailure_name(const char *rsc_id, const char *op, guint interval_ms)
336{
337 return pcmk__fail_attr_name(PCMK__LAST_FAILURE_PREFIX, rsc_id, op,
338 interval_ms);
339}
340
341// internal resource agent functions (from agents.c)
342int pcmk__effective_rc(int rc);
343
344#ifdef __cplusplus
345}
346#endif
347
348#endif // PCMK__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)
void pcmk__panic(const char *reason)
Definition watchdog.c:146
guint pcmk__mainloop_timer_get_period(const mainloop_timer_t *timer)
Get period for mainloop timer.
Definition mainloop.c:913
bool pcmk__throttle_cib_load(const char *server, float *load)
Definition procfs.c:298
int pcmk__score_yellow
Definition scores.c:18
unsigned int pcmk__procfs_num_cores(void)
Definition procfs.c:165
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:867
pid_t pcmk__locate_sbd(void)
Definition watchdog.c:164
guint pcmk__timeout_ms2s(guint timeout_ms)
Definition utils.c:429
guint pcmk__create_timer(guint interval_ms, GSourceFunc fn, gpointer data)
Definition utils.c:405
int pcmk__effective_rc(int rc)
Definition agents.c:63
#define PCMK__LAST_FAILURE_PREFIX
Definition internal.h:300
#define PCMK__FAIL_COUNT_PREFIX
Definition internal.h:299
char * pcmk__format_nvpair(const char *name, const char *value, const char *units)
Definition nvpair.c:176
bool pcmk__procfs_has_pids(void)
Definition procfs.c:235
int pcmk__read_pidfile(const char *filename, pid_t *pid)
Definition pid.c:107
void pcmk__daemonize(const char *name, const char *pidfile)
Definition utils.c:288
void pcmk__sysrq_trigger(char t)
Definition procfs.c:282
int pcmk__scan_nvpair(const gchar *input, gchar **name, gchar **value)
Definition nvpair.c:126
int pcmk__score_green
Definition scores.c:17
DIR * pcmk__procfs_fd_dir(void)
Definition procfs.c:259
int pcmk__pid_active(pid_t pid, const char *daemon)
Definition pid.c:19
bool pcmk__is_daemon
Definition logging.c:47
void pcmk__sleep_ms(unsigned int ms)
Definition utils.c:359
int pcmk__pidfile_matches(const char *filename, pid_t expected_pid, const char *expected_name, pid_t *pid)
Definition pid.c:168
bool pcmk__throttle_load_avg(float *load)
Definition procfs.c:431
pid_t pcmk__procfs_pid_of(const char *name)
Definition procfs.c:127
int pcmk__lock_pidfile(const char *filename, const char *name)
Definition pid.c:210
int pcmk__score_red
Definition scores.c:16
int pcmk__procfs_pid2path(pid_t pid, char path[], size_t path_size)
Definition procfs.c:203
uint64_t flags
Definition remote.c:3
char data[0]
Definition cpg.c:10
uint32_t size
Definition cpg.c:4
uint32_t pid
Definition cpg.c:1
struct crm_ipc_s crm_ipc_t
Definition ipc.h:159
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:174
#define CRM_CHECK(expr, failure_action)
Definition logging.h:213
Wrappers for and extensions to glib mainloop.
struct mainloop_timer_s mainloop_timer_t
Definition mainloop.h:45
struct mainloop_io_s mainloop_io_t
Definition mainloop.h:41
xmlNode * input
pcmk__action_result_t result
Definition pcmk_fence.c:37
const char * target
Definition pcmk_fence.c:31
@ CRM_EX_OSERR
External (OS/environmental) problem.
Definition results.h:254
_Noreturn crm_exit_t crm_exit(crm_exit_t rc)
Definition results.c:1058
void crm_abort(const char *file, const char *function, int line, const char *condition, gboolean do_core, gboolean do_fork)
Definition results.c:215
#define pcmk__assert(expr)
API for strings.
char * crm_strdup_printf(char const *format,...) G_GNUC_PRINTF(1