pacemaker  1.1.24-3850484742
Scalable High-Availability cluster resource manager
portability.h
Go to the documentation of this file.
1 /*
2  * Copyright 2001-2018 Alan Robertson <alanr@unix.sh>
3  *
4  * This source code is licensed under the GNU Lesser General Public License
5  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
6  */
7 #ifndef PORTABILITY_H
8 # define PORTABILITY_H
9 
10 # define EOS '\0'
11 # define DIMOF(a) ((int) (sizeof(a)/sizeof(a[0])) )
12 
13 /* Needs to be defined before any other includes, otherwise some system
14  * headers do not behave as expected! Major black magic... */
15 # undef _GNU_SOURCE /* in case it was defined on the command line */
16 # define _GNU_SOURCE
17 
18 /* Please leave this as the first #include - Solaris needs it there */
19 # ifdef HAVE_CONFIG_H
20 # include <config.h>
21 # endif
22 
23 /* Prototypes for libreplace functions */
24 
25 # ifndef HAVE_DAEMON
26  /* We supply a replacement function, but need a prototype */
27 int daemon(int nochdir, int noclose);
28 # endif /* HAVE_DAEMON */
29 
30 # ifndef HAVE_SETENV
31  /* We supply a replacement function, but need a prototype */
32 int setenv(const char *name, const char *value, int why);
33 # endif /* HAVE_SETENV */
34 
35 # ifndef HAVE_STRERROR
36  /* We supply a replacement function, but need a prototype */
37 char *strerror(int errnum);
38 # endif /* HAVE_STRERROR */
39 
40 # ifndef HAVE_STRCHRNUL
41  /* We supply a replacement function, but need a prototype */
42 char *strchrnul(const char *s, int c_in);
43 # endif /* HAVE_STRCHRNUL */
44 
45 # ifndef HAVE_ALPHASORT
46 # include <dirent.h>
47 int
48  alphasort(const void *dirent1, const void *dirent2);
49 # endif /* HAVE_ALPHASORT */
50 
51 # ifndef HAVE_STRNLEN
52 size_t strnlen(const char *s, size_t maxlen);
53 # else
54 # define USE_GNU
55 # endif
56 
57 # ifndef HAVE_STRNDUP
58 char *strndup(const char *str, size_t len);
59 # else
60 # define USE_GNU
61 # endif
62 
63 // This test could be better, but it covers platforms of interest
64 # if defined(ON_BSD) || defined(ON_SOLARIS)
65 # define SUPPORT_PROCFS 0
66 # else
67 # define SUPPORT_PROCFS 1
68 # endif
69 
70 # include <glib.h>
71 # if !GLIB_CHECK_VERSION(2,14,0)
72 
73 typedef struct fake_ghi {
74  GHashTable *hash;
75  int nth; /* current index over the iteration */
76  int lpc; /* internal loop counter inside g_hash_table_find */
77  gpointer key;
78  gpointer value;
80 
81 static inline void
82 g_hash_prepend_value(gpointer key, gpointer value, gpointer user_data)
83 {
84  GList **values = (GList **) user_data;
85 
86  *values = g_list_prepend(*values, value);
87 }
88 
89 /* Since: 2.14 */
90 static inline GList *
91 g_hash_table_get_values(GHashTable * hash_table)
92 {
93  GList *values = NULL;
94 
95  g_hash_table_foreach(hash_table, g_hash_prepend_value, &values);
96  return values;
97 }
98 # endif
99 
100 # if !GLIB_CHECK_VERSION(2,16,0)
101 
102 static inline gboolean
103 g_hash_table_nth_data(gpointer key, gpointer value, gpointer user_data)
104 {
105  GHashTableIter *iter = (GHashTableIter *) user_data;
106 
107  if (iter->lpc++ == iter->nth) {
108  iter->key = key;
109  iter->value = value;
110  return TRUE;
111  }
112  return FALSE;
113 }
114 
115 static inline void
116 g_hash_table_iter_init(GHashTableIter * iter, GHashTable * hash_table)
117 {
118  iter->hash = hash_table;
119  iter->nth = 0;
120  iter->lpc = 0;
121  iter->key = NULL;
122  iter->value = NULL;
123 }
124 
125 static inline gboolean
126 g_hash_table_iter_next(GHashTableIter * iter, gpointer * key, gpointer * value)
127 {
128  gboolean found = FALSE;
129 
130  iter->lpc = 0;
131  iter->key = NULL;
132  iter->value = NULL;
133  if (iter->nth < g_hash_table_size(iter->hash)) {
134  found = ! !g_hash_table_find(iter->hash, g_hash_table_nth_data, iter);
135  iter->nth++;
136  }
137  if (key)
138  *key = iter->key;
139  if (value)
140  *value = iter->value;
141  return found;
142 }
143 
144 static inline void
145 g_hash_table_iter_remove(GHashTableIter * iter)
146 {
147  g_hash_table_remove(iter->hash, iter->key);
148  iter->nth--; /* Or zero to be safe? */
149 }
150 
151 static inline int
152 g_strcmp0(const char *str1, const char *str2)
153 {
154  if (!str1)
155  return -(str1 != str2);
156  if (!str2)
157  return str1 != str2;
158  return strcmp(str1, str2);
159 }
160 # endif /* !HAVE_LIBGLIB_2_0 */
161 
162 # if !GLIB_CHECK_VERSION(2,28,0)
163 # include <string.h>
164 /* Since: 2.28 */
165 static inline void
166 g_list_free_full(GList * list, GDestroyNotify free_func)
167 {
168  g_list_foreach(list, (GFunc) free_func, NULL);
169  g_list_free(list);
170 }
171 # endif
172 
173 # if SUPPORT_DBUS
174 # ifndef HAVE_DBUSBASICVALUE
175 # include <stdint.h>
176 # include <dbus/dbus.h>
181 typedef struct
182 {
183  uint32_t first32;
184  uint32_t second32;
185 } DBus8ByteStruct;
186 
197 typedef union
198 {
199  unsigned char bytes[8];
200  int16_t i16;
201  uint16_t u16;
202  int32_t i32;
203  uint32_t u32;
204  uint32_t bool_val;
205 # ifdef DBUS_HAVE_INT64
206  int64_t i64;
207  uint64_t u64;
208 # endif
209  DBus8ByteStruct eight;
210  double dbl;
211  unsigned char byt;
212  char *str;
213  int fd;
214 } DBusBasicValue;
215 # endif
216 # endif
217 
218 /* Replacement error codes for non-linux */
219 # include <errno.h>
220 
221 # ifndef ENOTUNIQ
222 # define ENOTUNIQ 190
223 # endif
224 
225 # ifndef ECOMM
226 # define ECOMM 191
227 # endif
228 
229 # ifndef ELIBACC
230 # define ELIBACC 192
231 # endif
232 
233 # ifndef EREMOTEIO
234 # define EREMOTEIO 193
235 # endif
236 
237 # ifndef EUNATCH
238 # define EUNATCH 194
239 # endif
240 
241 # ifndef ENOKEY
242 # define ENOKEY 195
243 # endif
244 
245 # ifndef ENODATA
246 # define ENODATA 196
247 # endif
248 
249 # ifndef ETIME
250 # define ETIME 197
251 # endif
252 
253 # ifndef ENOSR
254 # define ENOSR 198
255 # endif
256 
257 # ifndef ENOSTR
258 # define ENOSTR 199
259 # endif
260 
261 # ifndef EKEYREJECTED
262 # define EKEYREJECTED 200
263 # endif
264 
265 /*
266  * Some compilers (eg. Sun studio) do not define __FUNCTION__
267  */
268 # ifdef __SUNPRO_C
269 # define __FUNCTION__ __func__
270 # endif
271 
272 # ifdef __MY_UNKNOWN_C
273 # define __FUNCTION__ "__FUNCTION__"
274 # endif
275 
276 #endif /* PORTABILITY_H */
int alphasort(const void *dirent1, const void *dirent2)
#define int16_t
Definition: stdint.in.h:150
char * strerror(int errnum)
char * strndup(const char *str, size_t len)
gpointer key
Definition: portability.h:77
int daemon(int nochdir, int noclose)
GHashTable * hash
Definition: portability.h:74
int setenv(const char *name, const char *value, int why)
size_t strnlen(const char *s, size_t maxlen)
struct fake_ghi GHashTableIter
#define uint32_t
Definition: stdint.in.h:158
char * strchrnul(const char *s, int c_in)
#define uint16_t
Definition: stdint.in.h:151
gpointer value
Definition: portability.h:78
#define int32_t
Definition: stdint.in.h:157