pacemaker  3.0.0-d8340737c4
Scalable High-Availability cluster resource manager
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 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 
17 #include <glib.h> // guint, GList, GHashTable
18 #include <libxml/tree.h> // xmlNode
19 
20 #include <crm/common/logging.h> // do_crm_log_unlikely(), etc.
21 #include <crm/common/mainloop.h> // mainloop_io_t, struct ipc_client_callbacks
22 #include <crm/common/strings.h> // crm_strdup_printf()
26 #include <crm/common/io_internal.h>
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 /* This says whether the current application is a Pacemaker daemon or not,
40  * and is used to change default logging settings such as whether to log to
41  * stderr, etc., as well as a few other details such as whether blackbox signal
42  * handling is enabled.
43  *
44  * It is set when logging is initialized, and does not need to be set directly.
45  */
46 extern bool pcmk__is_daemon;
47 
48 // Number of elements in a statically defined array
49 #define PCMK__NELEM(a) ((int) (sizeof(a)/sizeof(a[0])) )
50 
51 #if PCMK__ENABLE_CIBSECRETS
52 /* internal CIB utilities (from cib_secrets.c) */
53 
54 int pcmk__substitute_secrets(const char *rsc_id, GHashTable *params);
55 #endif
56 
57 
58 /* internal main loop utilities (from mainloop.c) */
59 
60 int pcmk__add_mainloop_ipc(crm_ipc_t *ipc, int priority, void *userdata,
61  const struct ipc_client_callbacks *callbacks,
62  mainloop_io_t **source);
64 
65 
66 /* internal node-related XML utilities (from nodes.c) */
67 
76 void pcmk__xe_add_node(xmlNode *xml, const char *node, int nodeid);
77 
78 
79 /* internal name/value utilities (from nvpair.c) */
80 
81 int pcmk__scan_nvpair(const char *input, char **name, char **value);
82 char *pcmk__format_nvpair(const char *name, const char *value,
83  const char *units);
84 
85 /* internal procfs utilities (from procfs.c) */
86 
87 pid_t pcmk__procfs_pid_of(const char *name);
88 unsigned int pcmk__procfs_num_cores(void);
89 int pcmk__procfs_pid2path(pid_t pid, char path[], size_t path_size);
90 bool pcmk__procfs_has_pids(void);
91 
92 /* internal functions related to process IDs (from pid.c) */
93 
110 int pcmk__pid_active(pid_t pid, const char *daemon);
111 
112 int pcmk__read_pidfile(const char *filename, pid_t *pid);
113 int pcmk__pidfile_matches(const char *filename, pid_t expected_pid,
114  const char *expected_name, pid_t *pid);
115 int pcmk__lock_pidfile(const char *filename, const char *name);
116 
117 
118 // bitwise arithmetic utilities
119 
135 static inline uint64_t
136 pcmk__set_flags_as(const char *function, int line, uint8_t log_level,
137  const char *flag_type, const char *target,
138  uint64_t flag_group, uint64_t flags, const char *flags_str)
139 {
140  uint64_t result = flag_group | flags;
141 
142  if (result != flag_group) {
143  do_crm_log_unlikely(log_level,
144  "%s flags %#.8llx (%s) for %s set by %s:%d",
145  ((flag_type == NULL)? "Group of" : flag_type),
146  (unsigned long long) flags,
147  ((flags_str == NULL)? "flags" : flags_str),
148  ((target == NULL)? "target" : target),
149  function, line);
150  }
151  return result;
152 }
153 
169 static inline uint64_t
170 pcmk__clear_flags_as(const char *function, int line, uint8_t log_level,
171  const char *flag_type, const char *target,
172  uint64_t flag_group, uint64_t flags, const char *flags_str)
173 {
174  uint64_t result = flag_group & ~flags;
175 
176  if (result != flag_group) {
177  do_crm_log_unlikely(log_level,
178  "%s flags %#.8llx (%s) for %s cleared by %s:%d",
179  ((flag_type == NULL)? "Group of" : flag_type),
180  (unsigned long long) flags,
181  ((flags_str == NULL)? "flags" : flags_str),
182  ((target == NULL)? "target" : target),
183  function, line);
184  }
185  return result;
186 }
187 
197 static inline const char *
198 pcmk__flag_text(uint64_t flag_group, uint64_t flags)
199 {
200  return pcmk__btoa(pcmk_all_flags_set(flag_group, flags));
201 }
202 
203 
204 // miscellaneous utilities (from utils.c)
205 
206 void pcmk__daemonize(const char *name, const char *pidfile);
207 void pcmk__panic(const char *reason);
208 pid_t pcmk__locate_sbd(void);
209 void pcmk__sleep_ms(unsigned int ms);
210 guint pcmk__create_timer(guint interval_ms, GSourceFunc fn, gpointer data);
211 guint pcmk__timeout_ms2s(guint timeout_ms);
212 
213 extern int pcmk__score_red;
214 extern int pcmk__score_green;
215 extern int pcmk__score_yellow;
216 
232 static inline void *
233 pcmk__assert_alloc_as(const char *file, const char *function, uint32_t line,
234  size_t nmemb, size_t size)
235 {
236  void *ptr = calloc(nmemb, size);
237 
238  if (ptr == NULL) {
239  crm_abort(file, function, line, "Out of memory", FALSE, TRUE);
241  }
242  return ptr;
243 }
244 
257 #define pcmk__assert_alloc(nmemb, size) \
258  pcmk__assert_alloc_as(__FILE__, __func__, __LINE__, nmemb, size)
259 
272 static inline void *
273 pcmk__realloc(void *ptr, size_t size)
274 {
275  void *new_ptr;
276 
277  // realloc(p, 0) can replace free(p) but this wrapper can't
278  pcmk__assert(size > 0);
279 
280  new_ptr = realloc(ptr, size);
281  if (new_ptr == NULL) {
282  free(ptr);
283  abort();
284  }
285  return new_ptr;
286 }
287 
288 static inline char *
289 pcmk__getpid_s(void)
290 {
291  return crm_strdup_printf("%lu", (unsigned long) getpid());
292 }
293 
294 // More efficient than g_list_length(list) == 1
295 static inline bool
296 pcmk__list_of_1(GList *list)
297 {
298  return list && (list->next == NULL);
299 }
300 
301 // More efficient than g_list_length(list) > 1
302 static inline bool
303 pcmk__list_of_multiple(GList *list)
304 {
305  return list && (list->next != NULL);
306 }
307 
308 /* convenience functions for failure-related node attributes */
309 
310 #define PCMK__FAIL_COUNT_PREFIX "fail-count"
311 #define PCMK__LAST_FAILURE_PREFIX "last-failure"
312 
330 static inline char *
331 pcmk__fail_attr_name(const char *prefix, const char *rsc_id, const char *op,
332  guint interval_ms)
333 {
334  CRM_CHECK(prefix && rsc_id && op, return NULL);
335  return crm_strdup_printf("%s-%s#%s_%u", prefix, rsc_id, op, interval_ms);
336 }
337 
338 static inline char *
339 pcmk__failcount_name(const char *rsc_id, const char *op, guint interval_ms)
340 {
341  return pcmk__fail_attr_name(PCMK__FAIL_COUNT_PREFIX, rsc_id, op,
342  interval_ms);
343 }
344 
345 static inline char *
346 pcmk__lastfailure_name(const char *rsc_id, const char *op, guint interval_ms)
347 {
348  return pcmk__fail_attr_name(PCMK__LAST_FAILURE_PREFIX, rsc_id, op,
349  interval_ms);
350 }
351 
352 // internal resource agent functions (from agents.c)
353 int pcmk__effective_rc(int rc);
354 
355 #ifdef __cplusplus
356 }
357 #endif
358 
359 #endif // PCMK__CRM_COMMON_INTERNAL__H
#define CRM_CHECK(expr, failure_action)
Definition: logging.h:213
uint32_t size
Definition: cpg.c:52
char data[0]
Definition: cpg.c:58
_Noreturn crm_exit_t crm_exit(crm_exit_t rc)
Definition: results.c:1044
API for strings.
int pcmk__read_pidfile(const char *filename, pid_t *pid)
Definition: pid.c:107
const char * name
Definition: cib.c:26
void crm_abort(const char *file, const char *function, int line, const char *condition, gboolean do_core, gboolean do_fork)
Definition: results.c:215
int pcmk__substitute_secrets(const char *rsc_id, GHashTable *params)
Definition: cib_secrets.c:96
struct mainloop_timer_s mainloop_timer_t
Definition: mainloop.h:45
struct mainloop_io_s mainloop_io_t
Definition: mainloop.h:41
Wrappers for and extensions to glib mainloop.
#define PCMK__LAST_FAILURE_PREFIX
Definition: internal.h:311
Wrappers for and extensions to libqb logging.
bool pcmk__procfs_has_pids(void)
Definition: procfs.c:207
int pcmk__effective_rc(int rc)
Definition: agents.c:63
char * pcmk__format_nvpair(const char *name, const char *value, const char *units)
Definition: nvpair.c:187
#define do_crm_log_unlikely(level, fmt, args...)
Log a message that is likely to be filtered out.
Definition: logging.h:174
void pcmk__sleep_ms(unsigned int ms)
Definition: utils.c:355
uint32_t pid
Definition: cpg.c:49
struct crm_ipc_s crm_ipc_t
Definition: ipc.h:151
External (OS/environmental) problem.
Definition: results.h:252
unsigned int pcmk__procfs_num_cores(void)
Definition: procfs.c:141
guint pcmk__create_timer(guint interval_ms, GSourceFunc fn, gpointer data)
Definition: utils.c:401
int pcmk__scan_nvpair(const char *input, char **name, char **value)
Definition: nvpair.c:124
bool pcmk__is_daemon
Definition: logging.c:47
#define PCMK__FAIL_COUNT_PREFIX
Definition: internal.h:310
int pcmk__pid_active(pid_t pid, const char *daemon)
Definition: pid.c:19
pid_t pcmk__locate_sbd(void)
Definition: watchdog.c:190
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:861
pid_t pcmk__procfs_pid_of(const char *name)
Definition: procfs.c:107
#define pcmk__assert(expr)
const char * target
Definition: pcmk_fence.c:31
int pcmk__score_green
Definition: scores.c:17
void pcmk__panic(const char *reason)
Definition: watchdog.c:172
void pcmk__daemonize(const char *name, const char *pidfile)
Definition: utils.c:284
int pcmk__lock_pidfile(const char *filename, const char *name)
Definition: pid.c:210
pcmk__action_result_t result
Definition: pcmk_fence.c:37
int pcmk__score_red
Definition: scores.c:16
const char * path
Definition: cib.c:28
xmlNode * input
int pcmk__procfs_pid2path(pid_t pid, char path[], size_t path_size)
Definition: procfs.c:175
guint pcmk__timeout_ms2s(guint timeout_ms)
Definition: utils.c:425
int pcmk__score_yellow
Definition: scores.c:18
int pcmk__pidfile_matches(const char *filename, pid_t expected_pid, const char *expected_name, pid_t *pid)
Definition: pid.c:168
void pcmk__xe_add_node(xmlNode *xml, const char *node, int nodeid)
Definition: nodes.c:147
guint pcmk__mainloop_timer_get_period(const mainloop_timer_t *timer)
Get period for mainloop timer.
Definition: mainloop.c:907
uint64_t flags
Definition: remote.c:211
char * crm_strdup_printf(char const *format,...) G_GNUC_PRINTF(1