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