pacemaker  2.0.4-2deceaa
Scalable High-Availability cluster resource manager
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
internal.h
Go to the documentation of this file.
1 /*
2  * Copyright 2015-2020 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 <string.h> // strcmp()
16 #include <sys/types.h> // uid_t, gid_t, pid_t
17 
18 #include <glib.h> // guint, GList, GHashTable
19 #include <libxml/tree.h> // xmlNode
20 
21 #include <crm/common/util.h> // crm_strdup_printf()
22 
23 // Internal ACL-related utilities (from acl.c)
24 
25 char *pcmk__uid2username(uid_t uid);
26 const char *pcmk__update_acl_user(xmlNode *request, const char *field,
27  const char *peer_user);
28 
29 #if ENABLE_ACL
30 # include <string.h>
31 static inline bool
32 pcmk__is_privileged(const char *user)
33 {
34  return user && (!strcmp(user, CRM_DAEMON_USER) || !strcmp(user, "root"));
35 }
36 #endif
37 
38 
39 #if SUPPORT_CIBSECRETS
40 // Internal CIB utilities (from cib_secrets.c) */
41 
42 int pcmk__substitute_secrets(const char *rsc_id, GHashTable *params);
43 #endif
44 
45 
46 /* internal digest-related utilities (from digest.c) */
47 
48 bool pcmk__verify_digest(xmlNode *input, const char *expected);
49 
50 
51 /* internal I/O utilities (from io.c) */
52 
53 int pcmk__real_path(const char *path, char **resolved_path);
54 
55 char *pcmk__series_filename(const char *directory, const char *series,
56  int sequence, bool bzip);
57 int pcmk__read_series_sequence(const char *directory, const char *series,
58  unsigned int *seq);
59 void pcmk__write_series_sequence(const char *directory, const char *series,
60  unsigned int sequence, int max);
61 int pcmk__chown_series_sequence(const char *directory, const char *series,
62  uid_t uid, gid_t gid);
63 
64 int pcmk__build_path(const char *path_c, mode_t mode);
65 bool pcmk__daemon_can_write(const char *dir, const char *file);
66 void pcmk__sync_directory(const char *name);
67 
68 int pcmk__file_contents(const char *filename, char **contents);
69 int pcmk__write_sync(int fd, const char *contents);
70 int pcmk__set_nonblocking(int fd);
71 const char *pcmk__get_tmpdir(void);
72 
73 void pcmk__close_fds_in_child(bool);
74 
84 static inline void
85 pcmk__open_devnull(int flags)
86 {
87  // Static analysis clutter
88  // cppcheck-suppress leakReturnValNotUsed
89  (void) open("/dev/null", flags);
90 }
91 
92 
93 /* internal logging utilities */
94 
95 # define pcmk__config_err(fmt...) do { \
96  crm_config_error = TRUE; \
97  crm_err(fmt); \
98  } while (0)
99 
100 # define pcmk__config_warn(fmt...) do { \
101  crm_config_warning = TRUE; \
102  crm_warn(fmt); \
103  } while (0)
104 
105 
106 /* internal procfs utilities (from procfs.c) */
107 
108 pid_t pcmk__procfs_pid_of(const char *name);
109 unsigned int pcmk__procfs_num_cores(void);
110 
111 
112 /* internal XML schema functions (from xml.c) */
113 
114 void crm_schema_init(void);
115 void crm_schema_cleanup(void);
116 
117 
118 /* internal functions related to process IDs (from pid.c) */
119 
136 int pcmk__pid_active(pid_t pid, const char *daemon);
137 
138 int pcmk__read_pidfile(const char *filename, pid_t *pid);
139 int pcmk__pidfile_matches(const char *filename, pid_t expected_pid,
140  const char *expected_name, pid_t *pid);
141 int pcmk__lock_pidfile(const char *filename, const char *name);
142 
143 
144 /* interal functions related to resource operations (from operations.c) */
145 
146 // printf-style format to create operation ID from resource, action, interval
147 #define PCMK__OP_FMT "%s_%s_%u"
148 
149 char *pcmk__op_key(const char *rsc_id, const char *op_type, guint interval_ms);
150 char *pcmk__notify_key(const char *rsc_id, const char *notify_type,
151  const char *op_type);
152 char *pcmk__transition_key(int transition_id, int action_id, int target_rc,
153  const char *node);
154 void pcmk__filter_op_for_digest(xmlNode *param_set);
155 
156 
157 // miscellaneous utilities (from utils.c)
158 
159 const char *pcmk_message_name(const char *name);
160 
161 extern int pcmk__score_red;
162 extern int pcmk__score_green;
163 extern int pcmk__score_yellow;
164 
165 
166 /* internal generic string functions (from strings.c) */
167 
168 int pcmk__guint_from_hash(GHashTable *table, const char *key, guint default_val,
169  guint *result);
170 bool pcmk__starts_with(const char *str, const char *prefix);
171 bool pcmk__ends_with(const char *s, const char *match);
172 bool pcmk__ends_with_ext(const char *s, const char *match);
173 char *pcmk__add_word(char *list, const char *word);
174 int pcmk__compress(const char *data, unsigned int length, unsigned int max,
175  char **result, unsigned int *result_len);
176 
177 /* Correctly displaying singular or plural is complicated; consider "1 node has"
178  * vs. "2 nodes have". A flexible solution is to pluralize entire strings, e.g.
179  *
180  * if (a == 1) {
181  * crm_info("singular message"):
182  * } else {
183  * crm_info("plural message");
184  * }
185  *
186  * though even that's not sufficient for all languages besides English (if we
187  * ever desire to do translations of output and log messages). But the following
188  * convenience macros are "good enough" and more concise for many cases.
189  */
190 
191 /* Example:
192  * crm_info("Found %d %s", nentries,
193  * pcmk__plural_alt(nentries, "entry", "entries"));
194  */
195 #define pcmk__plural_alt(i, s1, s2) (((i) == 1)? (s1) : (s2))
196 
197 // Example: crm_info("Found %d node%s", nnodes, pcmk__plural_s(nnodes));
198 #define pcmk__plural_s(i) pcmk__plural_alt(i, "", "s")
199 
200 static inline int
201 pcmk__str_empty(const char *s)
202 {
203  return (s == NULL) || (s[0] == '\0');
204 }
205 
206 static inline char *
207 pcmk__getpid_s(void)
208 {
209  return crm_strdup_printf("%lu", (unsigned long) getpid());
210 }
211 
212 // More efficient than g_list_length(list) == 1
213 static inline bool
214 pcmk__list_of_1(GList *list)
215 {
216  return list && (list->next == NULL);
217 }
218 
219 // More efficient than g_list_length(list) > 1
220 static inline bool
221 pcmk__list_of_multiple(GList *list)
222 {
223  return list && (list->next != NULL);
224 }
225 
226 /* convenience functions for failure-related node attributes */
227 
228 #define PCMK__FAIL_COUNT_PREFIX "fail-count"
229 #define PCMK__LAST_FAILURE_PREFIX "last-failure"
230 
248 static inline char *
249 pcmk__fail_attr_name(const char *prefix, const char *rsc_id, const char *op,
250  guint interval_ms)
251 {
252  CRM_CHECK(prefix && rsc_id && op, return NULL);
253  return crm_strdup_printf("%s-%s#%s_%u", prefix, rsc_id, op, interval_ms);
254 }
255 
256 static inline char *
257 pcmk__failcount_name(const char *rsc_id, const char *op, guint interval_ms)
258 {
259  return pcmk__fail_attr_name(PCMK__FAIL_COUNT_PREFIX, rsc_id, op,
260  interval_ms);
261 }
262 
263 static inline char *
264 pcmk__lastfailure_name(const char *rsc_id, const char *op, guint interval_ms)
265 {
266  return pcmk__fail_attr_name(PCMK__LAST_FAILURE_PREFIX, rsc_id, op,
267  interval_ms);
268 }
269 
270 #endif /* CRM_COMMON_INTERNAL__H */
#define CRM_CHECK(expr, failure_action)
Definition: logging.h:233
void crm_schema_init(void)
Definition: schemas.c:379
bool pcmk__ends_with_ext(const char *s, const char *match)
Definition: strings.c:437
int pcmk__read_pidfile(const char *filename, pid_t *pid)
Definition: pid.c:118
int pcmk__read_series_sequence(const char *directory, const char *series, unsigned int *seq)
Definition: io.c:159
int pcmk__guint_from_hash(GHashTable *table, const char *key, guint default_val, guint *result)
Definition: strings.c:162
int pcmk__write_sync(int fd, const char *contents)
Definition: io.c:506
int pcmk__substitute_secrets(const char *rsc_id, GHashTable *params)
Definition: cib_secrets.c:96
bool pcmk__verify_digest(xmlNode *input, const char *expected)
Definition: digest.c:217
void pcmk__close_fds_in_child(bool)
Definition: io.c:577
void crm_schema_cleanup(void)
Definition: schemas.c:554
void pcmk__filter_op_for_digest(xmlNode *param_set)
Definition: operations.c:272
uint32_t pid
Definition: internal.h:81
char * pcmk__series_filename(const char *directory, const char *series, int sequence, bool bzip)
Definition: io.c:140
#define PCMK__LAST_FAILURE_PREFIX
Definition: internal.h:229
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:129
const char * pcmk_message_name(const char *name)
Get name to be used as identifier for cluster messages.
Definition: utils.c:517
Utility functions.
int pcmk__real_path(const char *path, char **resolved_path)
Definition: io.c:104
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)
int pcmk__compress(const char *data, unsigned int length, unsigned int max, char **result, unsigned int *result_len)
Definition: strings.c:541
bool pcmk__daemon_can_write(const char *dir, const char *file)
Definition: io.c:346
#define PCMK__FAIL_COUNT_PREFIX
Definition: internal.h:228
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:450
#define CRM_DAEMON_USER
Definition: config.h:32
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:40
void pcmk__write_series_sequence(const char *directory, const char *series, unsigned int sequence, int max)
Definition: io.c:206
pid_t pcmk__procfs_pid_of(const char *name)
Definition: procfs.c:111
int pcmk__score_green
Definition: utils.c:55
int pcmk__lock_pidfile(const char *filename, const char *name)
Definition: pid.c:214
int pcmk__score_red
Definition: utils.c:54
bool pcmk__ends_with(const char *s, const char *match)
Definition: strings.c:410
const char * pcmk__get_tmpdir(void)
Definition: io.c:559
int pcmk__score_yellow
Definition: utils.c:56
char * pcmk__add_word(char *list, const char *word)
Definition: strings.c:517
char data[0]
Definition: internal.h:90
int pcmk__pidfile_matches(const char *filename, pid_t expected_pid, const char *expected_name, pid_t *pid)
Definition: pid.c:172
char * pcmk__transition_key(int transition_id, int action_id, int target_rc, const char *node)
Definition: operations.c:196
int pcmk__chown_series_sequence(const char *directory, const char *series, uid_t uid, gid_t gid)
Definition: io.c:256
void pcmk__sync_directory(const char *name)
Definition: io.c:414
bool pcmk__starts_with(const char *str, const char *prefix)
Check whether a string starts with a certain sequence.
Definition: strings.c:358
char * pcmk__uid2username(uid_t uid)
char * name
Definition: pcmk_fence.c:30
char * crm_strdup_printf(char const *format,...) __attribute__((__format__(__printf__
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:536