pacemaker  2.1.5-b7adf64e51
Scalable High-Availability cluster resource manager
internal.h
Go to the documentation of this file.
1 /*
2  * Copyright 2015-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 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
24 #include <crm/common/io_internal.h>
30 
31 /* This says whether the current application is a Pacemaker daemon or not,
32  * and is used to change default logging settings such as whether to log to
33  * stderr, etc., as well as a few other details such as whether blackbox signal
34  * handling is enabled.
35  *
36  * It is set when logging is initialized, and does not need to be set directly.
37  */
38 extern bool pcmk__is_daemon;
39 
40 // Number of elements in a statically defined array
41 #define PCMK__NELEM(a) ((int) (sizeof(a)/sizeof(a[0])) )
42 
43 #if SUPPORT_CIBSECRETS
44 /* internal CIB utilities (from cib_secrets.c) */
45 
46 int pcmk__substitute_secrets(const char *rsc_id, GHashTable *params);
47 #endif
48 
49 
50 /* internal digest-related utilities (from digest.c) */
51 
52 bool pcmk__verify_digest(xmlNode *input, const char *expected);
53 
54 
55 /* internal main loop utilities (from mainloop.c) */
56 
57 int pcmk__add_mainloop_ipc(crm_ipc_t *ipc, int priority, void *userdata,
58  const struct ipc_client_callbacks *callbacks,
59  mainloop_io_t **source);
61 
62 
63 /* internal node-related XML utilities (from nodes.c) */
64 
73 void pcmk__xe_add_node(xmlNode *xml, const char *node, int nodeid);
74 
75 
76 /* internal name/value utilities (from nvpair.c) */
77 
78 int pcmk__scan_nvpair(const char *input, char **name, char **value);
79 char *pcmk__format_nvpair(const char *name, const char *value,
80  const char *units);
81 char *pcmk__format_named_time(const char *name, time_t epoch_time);
82 
91 void
92 pcmk__xe_set_bool_attr(xmlNodePtr node, const char *name, bool value);
93 
104 bool
105 pcmk__xe_attr_is_true(const xmlNode *node, const char *name);
106 
122 int
123 pcmk__xe_get_bool_attr(const xmlNode *node, const char *name, bool *value);
124 
125 
126 /* internal procfs utilities (from procfs.c) */
127 
128 pid_t pcmk__procfs_pid_of(const char *name);
129 unsigned int pcmk__procfs_num_cores(void);
130 int pcmk__procfs_pid2path(pid_t pid, char path[], size_t path_size);
131 bool pcmk__procfs_has_pids(void);
132 
133 /* internal XML schema functions (from xml.c) */
134 
135 void crm_schema_init(void);
136 void crm_schema_cleanup(void);
137 
138 
139 /* internal functions related to process IDs (from pid.c) */
140 
157 int pcmk__pid_active(pid_t pid, const char *daemon);
158 
159 int pcmk__read_pidfile(const char *filename, pid_t *pid);
160 int pcmk__pidfile_matches(const char *filename, pid_t expected_pid,
161  const char *expected_name, pid_t *pid);
162 int pcmk__lock_pidfile(const char *filename, const char *name);
163 
164 
165 /* internal functions related to resource operations (from operations.c) */
166 
167 // printf-style format to create operation ID from resource, action, interval
168 #define PCMK__OP_FMT "%s_%s_%u"
169 
170 char *pcmk__op_key(const char *rsc_id, const char *op_type, guint interval_ms);
171 char *pcmk__notify_key(const char *rsc_id, const char *notify_type,
172  const char *op_type);
173 char *pcmk__transition_key(int transition_id, int action_id, int target_rc,
174  const char *node);
175 void pcmk__filter_op_for_digest(xmlNode *param_set);
176 bool pcmk__is_fencing_action(const char *action);
177 
178 
179 // bitwise arithmetic utilities
180 
196 static inline uint64_t
197 pcmk__set_flags_as(const char *function, int line, uint8_t log_level,
198  const char *flag_type, const char *target,
199  uint64_t flag_group, uint64_t flags, const char *flags_str)
200 {
201  uint64_t result = flag_group | flags;
202 
203  if (result != flag_group) {
204  do_crm_log_unlikely(log_level,
205  "%s flags %#.8llx (%s) for %s set by %s:%d",
206  ((flag_type == NULL)? "Group of" : flag_type),
207  (unsigned long long) flags,
208  ((flags_str == NULL)? "flags" : flags_str),
209  ((target == NULL)? "target" : target),
210  function, line);
211  }
212  return result;
213 }
214 
230 static inline uint64_t
231 pcmk__clear_flags_as(const char *function, int line, uint8_t log_level,
232  const char *flag_type, const char *target,
233  uint64_t flag_group, uint64_t flags, const char *flags_str)
234 {
235  uint64_t result = flag_group & ~flags;
236 
237  if (result != flag_group) {
238  do_crm_log_unlikely(log_level,
239  "%s flags %#.8llx (%s) for %s cleared by %s:%d",
240  ((flag_type == NULL)? "Group of" : flag_type),
241  (unsigned long long) flags,
242  ((flags_str == NULL)? "flags" : flags_str),
243  ((target == NULL)? "target" : target),
244  function, line);
245  }
246  return result;
247 }
248 
249 // miscellaneous utilities (from utils.c)
250 
251 void pcmk__daemonize(const char *name, const char *pidfile);
252 void pcmk__panic(const char *origin);
253 pid_t pcmk__locate_sbd(void);
254 void pcmk__sleep_ms(unsigned int ms);
255 
256 extern int pcmk__score_red;
257 extern int pcmk__score_green;
258 extern int pcmk__score_yellow;
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  CRM_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 
289 static inline char *
290 pcmk__getpid_s(void)
291 {
292  return crm_strdup_printf("%lu", (unsigned long) getpid());
293 }
294 
295 // More efficient than g_list_length(list) == 1
296 static inline bool
297 pcmk__list_of_1(GList *list)
298 {
299  return list && (list->next == NULL);
300 }
301 
302 // More efficient than g_list_length(list) > 1
303 static inline bool
304 pcmk__list_of_multiple(GList *list)
305 {
306  return list && (list->next != NULL);
307 }
308 
309 /* convenience functions for failure-related node attributes */
310 
311 #define PCMK__FAIL_COUNT_PREFIX "fail-count"
312 #define PCMK__LAST_FAILURE_PREFIX "last-failure"
313 
331 static inline char *
332 pcmk__fail_attr_name(const char *prefix, const char *rsc_id, const char *op,
333  guint interval_ms)
334 {
335  CRM_CHECK(prefix && rsc_id && op, return NULL);
336  return crm_strdup_printf("%s-%s#%s_%u", prefix, rsc_id, op, interval_ms);
337 }
338 
339 static inline char *
340 pcmk__failcount_name(const char *rsc_id, const char *op, guint interval_ms)
341 {
342  return pcmk__fail_attr_name(PCMK__FAIL_COUNT_PREFIX, rsc_id, op,
343  interval_ms);
344 }
345 
346 static inline char *
347 pcmk__lastfailure_name(const char *rsc_id, const char *op, guint interval_ms)
348 {
349  return pcmk__fail_attr_name(PCMK__LAST_FAILURE_PREFIX, rsc_id, op,
350  interval_ms);
351 }
352 
353 // internal resource agent functions (from agents.c)
354 int pcmk__effective_rc(int rc);
355 
356 #endif /* CRM_COMMON_INTERNAL__H */
#define CRM_CHECK(expr, failure_action)
Definition: logging.h:227
uint32_t size
Definition: cpg.c:49
void crm_schema_init(void)
Definition: schemas.c:377
int pcmk__read_pidfile(const char *filename, pid_t *pid)
Definition: pid.c:111
const char * name
Definition: cib.c:24
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:35
bool pcmk__verify_digest(xmlNode *input, const char *expected)
Definition: digest.c:205
struct mainloop_io_s mainloop_io_t
Definition: mainloop.h:33
void crm_schema_cleanup(void)
Definition: schemas.c:554
void pcmk__xe_set_bool_attr(xmlNodePtr node, const char *name, bool value)
Definition: nvpair.c:942
void pcmk__filter_op_for_digest(xmlNode *param_set)
Definition: operations.c:390
bool pcmk__xe_attr_is_true(const xmlNode *node, const char *name)
Definition: nvpair.c:975
char * pcmk__format_named_time(const char *name, time_t epoch_time)
Definition: nvpair.c:303
Wrappers for and extensions to glib mainloop.
#define PCMK__LAST_FAILURE_PREFIX
Definition: internal.h:312
Wrappers for and extensions to libqb logging.
const char * action
Definition: pcmk_fence.c:30
bool pcmk__procfs_has_pids(void)
Definition: procfs.c:211
int pcmk__effective_rc(int rc)
Definition: agents.c:71
char * pcmk__format_nvpair(const char *name, const char *value, const char *units)
Definition: nvpair.c:284
#define do_crm_log_unlikely(level, fmt, args...)
Log a message that is likely to be filtered out.
Definition: logging.h:192
int daemon(int nochdir, int noclose)
char * pcmk__notify_key(const char *rsc_id, const char *notify_type, const char *op_type)
Definition: operations.c:229
void pcmk__sleep_ms(unsigned int ms)
Definition: utils.c:561
uint32_t pid
Definition: cpg.c:46
struct crm_ipc_s crm_ipc_t
Definition: ipc.h:165
Utility functions.
unsigned int pcmk__procfs_num_cores(void)
Definition: procfs.c:145
char * crm_strdup_printf(char const *format,...) G_GNUC_PRINTF(1
int pcmk__scan_nvpair(const char *input, char **name, char **value)
Definition: nvpair.c:221
int pcmk__xe_get_bool_attr(const xmlNode *node, const char *name, bool *value)
Definition: nvpair.c:948
bool pcmk__is_daemon
Definition: logging.c:47
bool pcmk__is_fencing_action(const char *action)
Definition: operations.c:535
#define PCMK__FAIL_COUNT_PREFIX
Definition: internal.h:311
int pcmk__pid_active(pid_t pid, const char *daemon)
Definition: pid.c:23
pid_t pcmk__locate_sbd(void)
Definition: watchdog.c:199
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:874
char * pcmk__op_key(const char *rsc_id, const char *op_type, guint interval_ms)
Generate an operation key (RESOURCE_ACTION_INTERVAL)
Definition: operations.c:45
pid_t pcmk__procfs_pid_of(const char *name)
Definition: procfs.c:111
const char * target
Definition: pcmk_fence.c:29
int pcmk__score_green
Definition: scores.c:21
void pcmk__daemonize(const char *name, const char *pidfile)
Definition: utils.c:421
int pcmk__lock_pidfile(const char *filename, const char *name)
Definition: pid.c:207
pcmk__action_result_t result
Definition: pcmk_fence.c:35
int pcmk__score_red
Definition: scores.c:20
const char * path
Definition: cib.c:26
#define CRM_ASSERT(expr)
Definition: results.h:42
xmlNode * input
int pcmk__procfs_pid2path(pid_t pid, char path[], size_t path_size)
Definition: procfs.c:179
int pcmk__score_yellow
Definition: scores.c:22
int pcmk__pidfile_matches(const char *filename, pid_t expected_pid, const char *expected_name, pid_t *pid)
Definition: pid.c:165
char * pcmk__transition_key(int transition_id, int action_id, int target_rc, const char *node)
Definition: operations.c:296
void pcmk__xe_add_node(xmlNode *xml, const char *node, int nodeid)
Definition: nodes.c:15
void pcmk__panic(const char *origin)
Definition: watchdog.c:162
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:215