pacemaker  1.1.24-3850484742
Scalable High-Availability cluster resource manager
internal.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015
3  * Andrew Beekhof <andrew@beekhof.net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 #ifndef CRM_COMMON_INTERNAL__H
21 #define CRM_COMMON_INTERNAL__H
22 
23 #include <glib.h> /* for gboolean */
24 #include <dirent.h> /* for struct dirent */
25 #include <unistd.h> /* for getpid() */
26 #include <stdbool.h> /* for bool */
27 #include <sys/types.h> /* for uid_t and gid_t */
28 
29 #include <crm/common/logging.h>
30 
31 /* internal I/O utilities (from io.c) */
32 
33 char *generate_series_filename(const char *directory, const char *series, int sequence,
34  gboolean bzip);
35 int get_last_sequence(const char *directory, const char *series);
36 void write_last_sequence(const char *directory, const char *series, int sequence, int max);
37 int crm_chown_last_sequence(const char *directory, const char *series, uid_t uid, gid_t gid);
38 
39 bool pcmk__daemon_can_write(const char *dir, const char *file);
40 void crm_sync_directory(const char *name);
41 
42 char *crm_read_contents(const char *filename);
43 int crm_write_sync(int fd, const char *contents);
44 int crm_set_nonblocking(int fd);
45 
46 void pcmk__close_fds_in_child(bool);
47 
48 
49 /* internal procfs utilities (from procfs.c) */
50 
51 int crm_procfs_process_info(struct dirent *entry, char *name, int *pid);
52 int crm_procfs_pid_of(const char *name);
53 unsigned int crm_procfs_num_cores(void);
54 
55 
56 /* internal XML schema functions (from xml.c) */
57 
58 void crm_schema_init(void);
59 void crm_schema_cleanup(void);
60 
61 
62 /* internal generic string functions (from strings.c) */
63 
64 char *crm_concat(const char *prefix, const char *suffix, char join);
65 void g_hash_destroy_str(gpointer data);
66 long long crm_int_helper(const char *text, char **end_text);
67 bool crm_starts_with(const char *str, const char *prefix);
68 gboolean crm_ends_with(const char *s, const char *match);
69 gboolean crm_ends_with_ext(const char *s, const char *match);
70 char *add_list_element(char *list, const char *value);
71 bool crm_compress_string(const char *data, int length, int max, char **result,
72  unsigned int *result_len);
73 gint crm_alpha_sort(gconstpointer a, gconstpointer b);
74 
75 /* Correctly displaying singular or plural is complicated; consider "1 node has"
76  * vs. "2 nodes have". A flexible solution is to pluralize entire strings, e.g.
77  *
78  * if (a == 1) {
79  * crm_info("singular message"):
80  * } else {
81  * crm_info("plural message");
82  * }
83  *
84  * though even that's not sufficient for all languages besides English (if we
85  * ever desire to do translations of output and log messages). But the following
86  * convenience macros are "good enough" and more concise for many cases.
87  */
88 
89 /* Example:
90  * crm_info("Found %d %s", nentries,
91  * pcmk__plural_alt(nentries, "entry", "entries"));
92  */
93 #define pcmk__plural_alt(i, s1, s2) (((i) == 1)? (s1) : (s2))
94 
95 // Example: crm_info("Found %d node%s", nnodes, pcmk__plural_s(nnodes));
96 #define pcmk__plural_s(i) pcmk__plural_alt(i, "", "s")
97 
98 static inline int
99 crm_strlen_zero(const char *s)
100 {
101  return !s || *s == '\0';
102 }
103 
104 static inline char *
105 crm_getpid_s()
106 {
107  return crm_strdup_printf("%lu", (unsigned long) getpid());
108 }
109 
110 // More efficient than g_list_length(list) == 1
111 static inline bool
112 pcmk__list_of_1(GList *list)
113 {
114  return list && (list->next == NULL);
115 }
116 
117 // More efficient than g_list_length(list) > 1
118 static inline bool
119 pcmk__list_of_multiple(GList *list)
120 {
121  return list && (list->next != NULL);
122 }
123 
124 /* convenience functions for failure-related node attributes */
125 
126 #define CRM_FAIL_COUNT_PREFIX "fail-count"
127 #define CRM_LAST_FAILURE_PREFIX "last-failure"
128 
146 static inline char *
147 crm_fail_attr_name(const char *prefix, const char *rsc_id, const char *op,
148  int interval)
149 {
150  CRM_CHECK(prefix && rsc_id && op, return NULL);
151  return crm_strdup_printf("%s-%s#%s_%d", prefix, rsc_id, op, interval);
152 }
153 
154 static inline char *
155 crm_failcount_name(const char *rsc_id, const char *op, int interval)
156 {
157  return crm_fail_attr_name(CRM_FAIL_COUNT_PREFIX, rsc_id, op, interval);
158 }
159 
160 static inline char *
161 crm_lastfailure_name(const char *rsc_id, const char *op, int interval)
162 {
163  return crm_fail_attr_name(CRM_LAST_FAILURE_PREFIX, rsc_id, op, interval);
164 }
165 
166 #endif /* CRM_COMMON_INTERNAL__H */
#define CRM_CHECK(expr, failure_action)
Definition: logging.h:190
void crm_schema_init(void)
Definition: schemas.c:273
int get_last_sequence(const char *directory, const char *series)
Definition: io.c:112
#define CRM_LAST_FAILURE_PREFIX
Definition: internal.h:127
long long crm_int_helper(const char *text, char **end_text)
Definition: strings.c:81
void pcmk__close_fds_in_child(bool)
Definition: io.c:534
void crm_schema_cleanup(void)
Definition: schemas.c:512
uint32_t pid
Definition: internal.h:77
char * generate_series_filename(const char *directory, const char *series, int sequence, gboolean bzip)
Definition: io.c:76
bool crm_starts_with(const char *str, const char *prefix)
Check whether a string starts with a certain sequence.
Definition: strings.c:284
Wrappers for and extensions to libqb logging.
char * crm_read_contents(const char *filename)
Definition: io.c:434
#define CRM_FAIL_COUNT_PREFIX
Definition: internal.h:126
char * add_list_element(char *list, const char *value)
Definition: strings.c:426
gint crm_alpha_sort(gconstpointer a, gconstpointer b)
Compare two strings alphabetically (case-insensitive)
Definition: strings.c:504
int crm_set_nonblocking(int fd)
Definition: io.c:510
bool pcmk__daemon_can_write(const char *dir, const char *file)
Definition: io.c:329
gboolean crm_ends_with_ext(const char *s, const char *match)
Definition: strings.c:363
gboolean crm_ends_with(const char *s, const char *match)
Definition: strings.c:334
int crm_procfs_process_info(struct dirent *entry, char *name, int *pid)
Definition: procfs.c:47
void write_last_sequence(const char *directory, const char *series, int sequence, int max)
Definition: io.c:184
bool crm_compress_string(const char *data, int length, int max, char **result, unsigned int *result_len)
Definition: strings.c:445
unsigned int crm_procfs_num_cores(void)
Definition: procfs.c:152
int crm_write_sync(int fd, const char *contents)
Definition: io.c:480
char data[0]
Definition: internal.h:86
int crm_chown_last_sequence(const char *directory, const char *series, uid_t uid, gid_t gid)
Definition: io.c:241
void crm_sync_directory(const char *name)
Definition: io.c:397
char * crm_concat(const char *prefix, const char *suffix, char join)
Definition: strings.c:33
int crm_procfs_pid_of(const char *name)
Definition: procfs.c:118
char * crm_strdup_printf(char const *format,...) __attribute__((__format__(__printf__
void g_hash_destroy_str(gpointer data)
Definition: strings.c:75