1 /*
2 * Copyright 2015-2023 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
23 #include <crm/common/actions_internal.h>
24 #include <crm/common/digests_internal.h>
25 #include <crm/common/health_internal.h>
26 #include <crm/common/io_internal.h>
27 #include <crm/common/iso8601_internal.h>
28 #include <crm/common/results_internal.h>
29 #include <crm/common/messages_internal.h>
30 #include <crm/common/strings_internal.h>
31 #include <crm/common/acl_internal.h>
32
33 /* This says whether the current application is a Pacemaker daemon or not,
34 * and is used to change default logging settings such as whether to log to
35 * stderr, etc., as well as a few other details such as whether blackbox signal
36 * handling is enabled.
37 *
38 * It is set when logging is initialized, and does not need to be set directly.
39 */
40 extern bool pcmk__is_daemon;
41
42 //! Node name of the local node
43 extern char *pcmk__our_nodename;
44
45 // Number of elements in a statically defined array
46 #define PCMK__NELEM(a) ((int) (sizeof(a)/sizeof(a[0])) )
47
48 #if SUPPORT_CIBSECRETS
49 /* internal CIB utilities (from cib_secrets.c) */
50
51 int pcmk__substitute_secrets(const char *rsc_id, GHashTable *params);
52 #endif
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);
60 guint pcmk__mainloop_timer_get_period(const mainloop_timer_t *timer);
61
62
63 /* internal node-related XML utilities (from nodes.c) */
64
65 /*!
66 * \internal
67 * \brief Add local node name and ID to an XML node
68 *
69 * \param[in,out] request XML node to modify
70 * \param[in] node The local node's name
71 * \param[in] nodeid The local node's ID (can be 0)
72 */
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
82 /*!
83 * \internal
84 * \brief Add a boolean attribute to an XML node.
85 *
86 * \param[in,out] node XML node to add attributes to
87 * \param[in] name XML attribute to create
88 * \param[in] value Value to give to the attribute
89 */
90 void
91 pcmk__xe_set_bool_attr(xmlNodePtr node, const char *name, bool value);
92
93 /*!
94 * \internal
95 * \brief Extract a boolean attribute's value from an XML element
96 *
97 * \param[in] node XML node to get attribute from
98 * \param[in] name XML attribute to get
99 *
100 * \return True if the given \p name is an attribute on \p node and has
101 * the value "true", False in all other cases
102 */
103 bool
104 pcmk__xe_attr_is_true(const xmlNode *node, const char *name);
105
106 /*!
107 * \internal
108 * \brief Extract a boolean attribute's value from an XML element, with
109 * error checking
110 *
111 * \param[in] node XML node to get attribute from
112 * \param[in] name XML attribute to get
113 * \param[out] value Destination for the value of the attribute
114 *
115 * \return EINVAL if \p name or \p value are NULL, ENODATA if \p node is
116 * NULL or the attribute does not exist, pcmk_rc_unknown_format
117 * if the attribute is not a boolean, and pcmk_rc_ok otherwise.
118 *
119 * \note \p value only has any meaning if the return value is pcmk_rc_ok.
120 */
121 int
122 pcmk__xe_get_bool_attr(const xmlNode *node, const char *name, bool *value);
123
124
125 /* internal procfs utilities (from procfs.c) */
126
127 pid_t pcmk__procfs_pid_of(const char *name);
128 unsigned int pcmk__procfs_num_cores(void);
129 int pcmk__procfs_pid2path(pid_t pid, char path[], size_t path_size);
130 bool pcmk__procfs_has_pids(void);
131
132 /* internal XML schema functions (from xml.c) */
133
134 void crm_schema_init(void);
135 void crm_schema_cleanup(void);
136
137
138 /* internal functions related to process IDs (from pid.c) */
139
140 /*!
141 * \internal
142 * \brief Check whether process exists (by PID and optionally executable path)
143 *
144 * \param[in] pid PID of process to check
145 * \param[in] daemon If not NULL, path component to match with procfs entry
146 *
147 * \return Standard Pacemaker return code
148 * \note Particular return codes of interest include pcmk_rc_ok for alive,
149 * ESRCH for process is not alive (verified by kill and/or executable path
150 * match), EACCES for caller unable or not allowed to check. A result of
151 * "alive" is less reliable when \p daemon is not provided or procfs is
152 * not available, since there is no guarantee that the PID has not been
153 * recycled for another process.
154 * \note This function cannot be used to verify \e authenticity of the process.
155 */
156 int pcmk__pid_active(pid_t pid, const char *daemon);
157
158 int pcmk__read_pidfile(const char *filename, pid_t *pid);
159 int pcmk__pidfile_matches(const char *filename, pid_t expected_pid,
160 const char *expected_name, pid_t *pid);
161 int pcmk__lock_pidfile(const char *filename, const char *name);
162
163
164 // bitwise arithmetic utilities
165
166 /*!
167 * \internal
168 * \brief Set specified flags in a flag group
169 *
170 * \param[in] function Function name of caller
171 * \param[in] line Line number of caller
172 * \param[in] log_level Log a message at this level
173 * \param[in] flag_type Label describing this flag group (for logging)
174 * \param[in] target Name of object whose flags these are (for logging)
175 * \param[in] flag_group Flag group being manipulated
176 * \param[in] flags Which flags in the group should be set
177 * \param[in] flags_str Readable equivalent of \p flags (for logging)
178 *
179 * \return Possibly modified flag group
180 */
181 static inline uint64_t
182 pcmk__set_flags_as(const char *function, int line, uint8_t log_level,
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
183 const char *flag_type, const char *target,
184 uint64_t flag_group, uint64_t flags, const char *flags_str)
185 {
186 uint64_t result = flag_group | flags;
187
188 if (result != flag_group) {
189 do_crm_log_unlikely(log_level,
190 "%s flags %#.8llx (%s) for %s set by %s:%d",
191 ((flag_type == NULL)? "Group of" : flag_type),
192 (unsigned long long) flags,
193 ((flags_str == NULL)? "flags" : flags_str),
194 ((target == NULL)? "target" : target),
195 function, line);
196 }
197 return result;
198 }
199
200 /*!
201 * \internal
202 * \brief Clear specified flags in a flag group
203 *
204 * \param[in] function Function name of caller
205 * \param[in] line Line number of caller
206 * \param[in] log_level Log a message at this level
207 * \param[in] flag_type Label describing this flag group (for logging)
208 * \param[in] target Name of object whose flags these are (for logging)
209 * \param[in] flag_group Flag group being manipulated
210 * \param[in] flags Which flags in the group should be cleared
211 * \param[in] flags_str Readable equivalent of \p flags (for logging)
212 *
213 * \return Possibly modified flag group
214 */
215 static inline uint64_t
216 pcmk__clear_flags_as(const char *function, int line, uint8_t log_level,
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
217 const char *flag_type, const char *target,
218 uint64_t flag_group, uint64_t flags, const char *flags_str)
219 {
220 uint64_t result = flag_group & ~flags;
221
222 if (result != flag_group) {
223 do_crm_log_unlikely(log_level,
224 "%s flags %#.8llx (%s) for %s cleared by %s:%d",
225 ((flag_type == NULL)? "Group of" : flag_type),
226 (unsigned long long) flags,
227 ((flags_str == NULL)? "flags" : flags_str),
228 ((target == NULL)? "target" : target),
229 function, line);
230 }
231 return result;
232 }
233
234 // miscellaneous utilities (from utils.c)
235
236 void pcmk__daemonize(const char *name, const char *pidfile);
237 void pcmk__panic(const char *origin);
238 pid_t pcmk__locate_sbd(void);
239 void pcmk__sleep_ms(unsigned int ms);
240
241 extern int pcmk__score_red;
242 extern int pcmk__score_green;
243 extern int pcmk__score_yellow;
244
245 /*!
246 * \internal
247 * \brief Resize a dynamically allocated memory block
248 *
249 * \param[in] ptr Memory block to resize (or NULL to allocate new memory)
250 * \param[in] size New size of memory block in bytes (must be > 0)
251 *
252 * \return Pointer to resized memory block
253 *
254 * \note This asserts on error, so the result is guaranteed to be non-NULL
255 * (which is the main advantage of this over directly using realloc()).
256 */
257 static inline void *
258 pcmk__realloc(void *ptr, size_t size)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
259 {
260 void *new_ptr;
261
262 // realloc(p, 0) can replace free(p) but this wrapper can't
263 CRM_ASSERT(size > 0);
264
265 new_ptr = realloc(ptr, size);
266 if (new_ptr == NULL) {
267 free(ptr);
268 abort();
269 }
270 return new_ptr;
271 }
272
273
274 static inline char *
275 pcmk__getpid_s(void)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
276 {
277 return crm_strdup_printf("%lu", (unsigned long) getpid());
278 }
279
280 // More efficient than g_list_length(list) == 1
281 static inline bool
282 pcmk__list_of_1(GList *list)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
283 {
284 return list && (list->next == NULL);
285 }
286
287 // More efficient than g_list_length(list) > 1
288 static inline bool
289 pcmk__list_of_multiple(GList *list)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
290 {
291 return list && (list->next != NULL);
292 }
293
294 /* convenience functions for failure-related node attributes */
295
296 #define PCMK__FAIL_COUNT_PREFIX "fail-count"
297 #define PCMK__LAST_FAILURE_PREFIX "last-failure"
298
299 /*!
300 * \internal
301 * \brief Generate a failure-related node attribute name for a resource
302 *
303 * \param[in] prefix Start of attribute name
304 * \param[in] rsc_id Resource name
305 * \param[in] op Operation name
306 * \param[in] interval_ms Operation interval
307 *
308 * \return Newly allocated string with attribute name
309 *
310 * \note Failure attributes are named like PREFIX-RSC#OP_INTERVAL (for example,
311 * "fail-count-myrsc#monitor_30000"). The '#' is used because it is not
312 * a valid character in a resource ID, to reliably distinguish where the
313 * operation name begins. The '_' is used simply to be more comparable to
314 * action labels like "myrsc_monitor_30000".
315 */
316 static inline char *
317 pcmk__fail_attr_name(const char *prefix, const char *rsc_id, const char *op,
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
318 guint interval_ms)
319 {
320 CRM_CHECK(prefix && rsc_id && op, return NULL);
321 return crm_strdup_printf("%s-%s#%s_%u", prefix, rsc_id, op, interval_ms);
322 }
323
324 static inline char *
325 pcmk__failcount_name(const char *rsc_id, const char *op, guint interval_ms)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
326 {
327 return pcmk__fail_attr_name(PCMK__FAIL_COUNT_PREFIX, rsc_id, op,
328 interval_ms);
329 }
330
331 static inline char *
332 pcmk__lastfailure_name(const char *rsc_id, const char *op, guint interval_ms)
/* ![[previous]](../icons/left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
333 {
334 return pcmk__fail_attr_name(PCMK__LAST_FAILURE_PREFIX, rsc_id, op,
335 interval_ms);
336 }
337
338 // internal resource agent functions (from agents.c)
339 int pcmk__effective_rc(int rc);
340
341 #endif /* CRM_COMMON_INTERNAL__H */