pacemaker  2.1.4-dc6eb4362
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> // getpid()
14 #include <stdbool.h> // bool
15 #include <stdint.h> // uint8_t, uint64_t
16 #include <string.h> // strcmp()
17 #include <fcntl.h> // open()
18 #include <sys/types.h> // uid_t, gid_t, pid_t
19 
20 #include <glib.h> // guint, GList, GHashTable
21 #include <libxml/tree.h> // xmlNode
22 
23 #include <crm/common/util.h> // crm_strdup_printf()
24 #include <crm/common/logging.h> // do_crm_log_unlikely(), etc.
25 #include <crm/common/mainloop.h> // mainloop_io_t, struct ipc_client_callbacks
31 
32 /* This says whether the current application is a Pacemaker daemon or not,
33  * and is used to change default logging settings such as whether to log to
34  * stderr, etc., as well as a few other details such as whether blackbox signal
35  * handling is enabled.
36  *
37  * It is set when logging is initialized, and does not need to be set directly.
38  */
39 extern bool pcmk__is_daemon;
40 
41 // Number of elements in a statically defined array
42 #define PCMK__NELEM(a) ((int) (sizeof(a)/sizeof(a[0])) )
43 
44 /* internal ACL-related utilities */
45 
46 char *pcmk__uid2username(uid_t uid);
47 const char *pcmk__update_acl_user(xmlNode *request, const char *field,
48  const char *peer_user);
49 
50 static inline bool
51 pcmk__is_privileged(const char *user)
52 {
53  return user && (!strcmp(user, CRM_DAEMON_USER) || !strcmp(user, "root"));
54 }
55 
56 void pcmk__enable_acl(xmlNode *acl_source, xmlNode *target, const char *user);
57 
58 bool pcmk__check_acl(xmlNode *xml, const char *name,
59  enum xml_private_flags mode);
60 
61 #if SUPPORT_CIBSECRETS
62 /* internal CIB utilities (from cib_secrets.c) */
63 
64 int pcmk__substitute_secrets(const char *rsc_id, GHashTable *params);
65 #endif
66 
67 
68 /* internal digest-related utilities (from digest.c) */
69 
70 bool pcmk__verify_digest(xmlNode *input, const char *expected);
71 
72 
73 /* internal I/O utilities (from io.c) */
74 
75 int pcmk__real_path(const char *path, char **resolved_path);
76 
77 char *pcmk__series_filename(const char *directory, const char *series,
78  int sequence, bool bzip);
79 int pcmk__read_series_sequence(const char *directory, const char *series,
80  unsigned int *seq);
81 void pcmk__write_series_sequence(const char *directory, const char *series,
82  unsigned int sequence, int max);
83 int pcmk__chown_series_sequence(const char *directory, const char *series,
84  uid_t uid, gid_t gid);
85 
86 int pcmk__build_path(const char *path_c, mode_t mode);
87 char *pcmk__full_path(const char *filename, const char *dirname);
88 bool pcmk__daemon_can_write(const char *dir, const char *file);
89 void pcmk__sync_directory(const char *name);
90 
91 int pcmk__file_contents(const char *filename, char **contents);
92 int pcmk__write_sync(int fd, const char *contents);
93 int pcmk__set_nonblocking(int fd);
94 const char *pcmk__get_tmpdir(void);
95 
96 void pcmk__close_fds_in_child(bool);
97 
107 static inline void
108 pcmk__open_devnull(int flags)
109 {
110  // Static analysis clutter
111  // cppcheck-suppress leakReturnValNotUsed
112  (void) open("/dev/null", flags);
113 }
114 
115 
116 /* internal main loop utilities (from mainloop.c) */
117 
118 int pcmk__add_mainloop_ipc(crm_ipc_t *ipc, int priority, void *userdata,
119  struct ipc_client_callbacks *callbacks,
120  mainloop_io_t **source);
122 
123 
124 /* internal name/value utilities (from nvpair.c) */
125 
126 int pcmk__scan_nvpair(const char *input, char **name, char **value);
127 char *pcmk__format_nvpair(const char *name, const char *value,
128  const char *units);
129 char *pcmk__format_named_time(const char *name, time_t epoch_time);
130 
139 void
140 pcmk__xe_set_bool_attr(xmlNodePtr node, const char *name, bool value);
141 
152 bool
153 pcmk__xe_attr_is_true(xmlNodePtr node, const char *name);
154 
170 int
171 pcmk__xe_get_bool_attr(xmlNodePtr node, const char *name, bool *value);
172 
173 
174 /* internal procfs utilities (from procfs.c) */
175 
176 pid_t pcmk__procfs_pid_of(const char *name);
177 unsigned int pcmk__procfs_num_cores(void);
178 
179 
180 /* internal XML schema functions (from xml.c) */
181 
182 void crm_schema_init(void);
183 void crm_schema_cleanup(void);
184 
185 
186 /* internal functions related to process IDs (from pid.c) */
187 
204 int pcmk__pid_active(pid_t pid, const char *daemon);
205 
206 int pcmk__read_pidfile(const char *filename, pid_t *pid);
207 int pcmk__pidfile_matches(const char *filename, pid_t expected_pid,
208  const char *expected_name, pid_t *pid);
209 int pcmk__lock_pidfile(const char *filename, const char *name);
210 
211 
212 /* internal functions related to resource operations (from operations.c) */
213 
214 // printf-style format to create operation ID from resource, action, interval
215 #define PCMK__OP_FMT "%s_%s_%u"
216 
217 char *pcmk__op_key(const char *rsc_id, const char *op_type, guint interval_ms);
218 char *pcmk__notify_key(const char *rsc_id, const char *notify_type,
219  const char *op_type);
220 char *pcmk__transition_key(int transition_id, int action_id, int target_rc,
221  const char *node);
222 void pcmk__filter_op_for_digest(xmlNode *param_set);
223 bool pcmk__is_fencing_action(const char *action);
224 
225 
226 // bitwise arithmetic utilities
227 
243 static inline uint64_t
244 pcmk__set_flags_as(const char *function, int line, uint8_t log_level,
245  const char *flag_type, const char *target,
246  uint64_t flag_group, uint64_t flags, const char *flags_str)
247 {
248  uint64_t result = flag_group | flags;
249 
250  if (result != flag_group) {
251  do_crm_log_unlikely(log_level,
252  "%s flags %#.8llx (%s) for %s set by %s:%d",
253  ((flag_type == NULL)? "Group of" : flag_type),
254  (unsigned long long) flags,
255  ((flags_str == NULL)? "flags" : flags_str),
256  ((target == NULL)? "target" : target),
257  function, line);
258  }
259  return result;
260 }
261 
277 static inline uint64_t
278 pcmk__clear_flags_as(const char *function, int line, uint8_t log_level,
279  const char *flag_type, const char *target,
280  uint64_t flag_group, uint64_t flags, const char *flags_str)
281 {
282  uint64_t result = flag_group & ~flags;
283 
284  if (result != flag_group) {
285  do_crm_log_unlikely(log_level,
286  "%s flags %#.8llx (%s) for %s cleared by %s:%d",
287  ((flag_type == NULL)? "Group of" : flag_type),
288  (unsigned long long) flags,
289  ((flags_str == NULL)? "flags" : flags_str),
290  ((target == NULL)? "target" : target),
291  function, line);
292  }
293  return result;
294 }
295 
296 // miscellaneous utilities (from utils.c)
297 
298 void pcmk__daemonize(const char *name, const char *pidfile);
299 void pcmk__panic(const char *origin);
300 pid_t pcmk__locate_sbd(void);
301 void pcmk__sleep_ms(unsigned int ms);
302 
303 extern int pcmk__score_red;
304 extern int pcmk__score_green;
305 extern int pcmk__score_yellow;
306 
319 static inline void *
320 pcmk__realloc(void *ptr, size_t size)
321 {
322  void *new_ptr;
323 
324  // realloc(p, 0) can replace free(p) but this wrapper can't
325  CRM_ASSERT(size > 0);
326 
327  new_ptr = realloc(ptr, size);
328  if (new_ptr == NULL) {
329  free(ptr);
330  abort();
331  }
332  return new_ptr;
333 }
334 
335 
336 static inline char *
337 pcmk__getpid_s(void)
338 {
339  return crm_strdup_printf("%lu", (unsigned long) getpid());
340 }
341 
342 // More efficient than g_list_length(list) == 1
343 static inline bool
344 pcmk__list_of_1(GList *list)
345 {
346  return list && (list->next == NULL);
347 }
348 
349 // More efficient than g_list_length(list) > 1
350 static inline bool
351 pcmk__list_of_multiple(GList *list)
352 {
353  return list && (list->next != NULL);
354 }
355 
356 /* convenience functions for failure-related node attributes */
357 
358 #define PCMK__FAIL_COUNT_PREFIX "fail-count"
359 #define PCMK__LAST_FAILURE_PREFIX "last-failure"
360 
378 static inline char *
379 pcmk__fail_attr_name(const char *prefix, const char *rsc_id, const char *op,
380  guint interval_ms)
381 {
382  CRM_CHECK(prefix && rsc_id && op, return NULL);
383  return crm_strdup_printf("%s-%s#%s_%u", prefix, rsc_id, op, interval_ms);
384 }
385 
386 static inline char *
387 pcmk__failcount_name(const char *rsc_id, const char *op, guint interval_ms)
388 {
389  return pcmk__fail_attr_name(PCMK__FAIL_COUNT_PREFIX, rsc_id, op,
390  interval_ms);
391 }
392 
393 static inline char *
394 pcmk__lastfailure_name(const char *rsc_id, const char *op, guint interval_ms)
395 {
396  return pcmk__fail_attr_name(PCMK__LAST_FAILURE_PREFIX, rsc_id, op,
397  interval_ms);
398 }
399 
400 // internal resource agent functions (from agents.c)
401 int pcmk__effective_rc(int rc);
402 
403 #endif /* CRM_COMMON_INTERNAL__H */
#define CRM_CHECK(expr, failure_action)
Definition: logging.h:226
guint pcmk__mainloop_timer_get_period(mainloop_timer_t *timer)
Get period for mainloop timer.
Definition: mainloop.c:927
uint32_t size
Definition: cpg.c:49
void crm_schema_init(void)
Definition: schemas.c:376
int pcmk__read_pidfile(const char *filename, pid_t *pid)
Definition: pid.c:132
const char * name
Definition: cib.c:24
int pcmk__read_series_sequence(const char *directory, const char *series, unsigned int *seq)
Definition: io.c:140
int pcmk__write_sync(int fd, const char *contents)
Definition: io.c:487
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:220
struct mainloop_io_s mainloop_io_t
Definition: mainloop.h:33
void pcmk__close_fds_in_child(bool)
Definition: io.c:558
void crm_schema_cleanup(void)
Definition: schemas.c:552
void pcmk__xe_set_bool_attr(xmlNodePtr node, const char *name, bool value)
Definition: nvpair.c:954
void pcmk__filter_op_for_digest(xmlNode *param_set)
Definition: operations.c:390
char * pcmk__format_named_time(const char *name, time_t epoch_time)
Definition: nvpair.c:303
char * pcmk__series_filename(const char *directory, const char *series, int sequence, bool bzip)
Definition: io.c:121
Wrappers for and extensions to glib mainloop.
#define PCMK__LAST_FAILURE_PREFIX
Definition: internal.h:359
Wrappers for and extensions to libqb logging.
const char * action
Definition: pcmk_fence.c:29
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:191
int daemon(int nochdir, int noclose)
int pcmk__xe_get_bool_attr(xmlNodePtr node, const char *name, bool *value)
Definition: nvpair.c:960
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:531
uint32_t pid
Definition: cpg.c:46
struct crm_ipc_s crm_ipc_t
Definition: ipc.h:163
Utility functions.
int pcmk__real_path(const char *path, char **resolved_path)
Definition: io.c:85
unsigned int pcmk__procfs_num_cores(void)
Definition: procfs.c:145
const char * pcmk__update_acl_user(xmlNode *request, const char *field, const char *peer_user)
Definition: acl.c:728
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
bool pcmk__daemon_can_write(const char *dir, const char *file)
Definition: io.c:327
bool pcmk__is_daemon
Definition: logging.c:47
bool pcmk__is_fencing_action(const char *action)
Definition: operations.c:536
#define PCMK__FAIL_COUNT_PREFIX
Definition: internal.h:358
int pcmk__pid_active(pid_t pid, const char *daemon)
Definition: pid.c:23
int pcmk__file_contents(const char *filename, char **contents)
Definition: io.c:431
#define CRM_DAEMON_USER
Definition: config.h:30
pid_t pcmk__locate_sbd(void)
Definition: watchdog.c:199
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
void pcmk__write_series_sequence(const char *directory, const char *series, unsigned int sequence, int max)
Definition: io.c:187
pid_t pcmk__procfs_pid_of(const char *name)
Definition: procfs.c:111
xml_private_flags
Definition: xml_internal.h:315
const char * target
Definition: pcmk_fence.c:28
int pcmk__score_green
Definition: scores.c:21
void pcmk__daemonize(const char *name, const char *pidfile)
Definition: utils.c:392
int pcmk__lock_pidfile(const char *filename, const char *name)
Definition: pid.c:228
pcmk__action_result_t result
Definition: pcmk_fence.c:34
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
const char * pcmk__get_tmpdir(void)
Definition: io.c:540
void pcmk__enable_acl(xmlNode *acl_source, xmlNode *target, const char *user)
Definition: acl.c:338
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:186
char * pcmk__transition_key(int transition_id, int action_id, int target_rc, const char *node)
Definition: operations.c:296
int pcmk__chown_series_sequence(const char *directory, const char *series, uid_t uid, gid_t gid)
Definition: io.c:237
void pcmk__sync_directory(const char *name)
Definition: io.c:395
bool pcmk__check_acl(xmlNode *xml, const char *name, enum xml_private_flags mode)
Definition: acl.c:611
bool pcmk__xe_attr_is_true(xmlNodePtr node, const char *name)
Definition: nvpair.c:986
void pcmk__panic(const char *origin)
Definition: watchdog.c:162
char * pcmk__uid2username(uid_t uid)
Definition: acl.c:698
int pcmk__add_mainloop_ipc(crm_ipc_t *ipc, int priority, void *userdata, struct ipc_client_callbacks *callbacks, mainloop_io_t **source)
Connect to IPC and add it as a main loop source.
Definition: mainloop.c:894
char * pcmk__full_path(const char *filename, const char *dirname)
Duplicate a file path, inserting a prefix if not absolute.
Definition: io.c:627
uint64_t flags
Definition: remote.c:149
int pcmk__build_path(const char *path_c, mode_t mode)
Definition: io.c:45
int pcmk__set_nonblocking(int fd)
Definition: io.c:517