/usr/include/libisns/util.h is in libisns-dev 0.97-2build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | /*
* Utility functions
*
* Copyright (C) 2006, 2007 Olaf Kirch <olaf.kirch@oracle.com>
*/
#ifndef UTIL_H
#define UTIL_H
#include <sys/types.h>
#include <stdint.h>
#include <stdio.h>
#include <stddef.h>
#include <string.h> // for strdup
#include <signal.h>
#include <libisns/types.h>
#define array_num_elements(a) (sizeof(a) / sizeof((a)[0]))
const char * isns_dirname(const char *);
int isns_mkdir_recursive(const char *);
extern const char *parser_separators;
char * parser_get_next_line(FILE *);
char * parser_get_next_word(char **);
char * parser_get_rest_of_line(char **);
int parser_split_line(char *, unsigned int, char **);
unsigned long parse_size(const char *);
unsigned int parse_count(const char *);
int parse_int(const char *);
long long parse_longlong(const char *);
double parse_double(const char *);
unsigned int parse_timeout(const char *);
char * print_size(unsigned long);
/*
* for signal management
*/
static inline void signals_hold(void)
{
sighold(SIGTERM);
sighold(SIGINT);
}
static inline void signals_release(void)
{
sigrelse(SIGTERM);
sigrelse(SIGINT);
}
/*
* Very simple and stupid string array.
*/
struct string_array {
unsigned int count;
char ** list;
};
void isns_string_array_append(struct string_array *, const char *);
void isns_string_array_destroy(struct string_array *);
void isns_assign_string(char **, const char *);
void isns_write_pidfile(const char *);
void isns_update_pidfile(const char *);
void isns_remove_pidfile(const char *);
extern void isns_log_background(void);
extern void isns_assert_failed(const char *,
const char *, unsigned int);
extern void isns_fatal(const char *, ...);
extern void isns_warning(const char *, ...);
extern void isns_error(const char *, ...);
extern void isns_notice(const char *, ...);
extern void isns_debug_general(const char *, ...);
extern void isns_debug_socket(const char *, ...);
extern void isns_debug_protocol(const char *, ...);
extern void isns_debug_message(const char *, ...);
extern void isns_debug_state(const char *, ...);
extern void isns_debug_auth(const char *, ...);
extern void isns_debug_scn(const char *, ...);
extern void isns_debug_esi(const char *, ...);
extern void isns_enable_debugging(const char *);
extern int isns_debug_enabled(int);
enum {
DBG_GENERAL = 0,
DBG_SOCKET,
DBG_PROTOCOL,
DBG_MESSAGE,
DBG_STATE,
DBG_AUTH,
DBG_SCN,
DBG_ESI,
};
/*
* There's no htonll yet
*/
#ifndef htonll
# ifdef __GLIBC__
# include <endian.h>
# include <byteswap.h>
# if __BYTE_ORDER == __BIG_ENDIAN
# define htonll(x) (x)
# define ntohll(x) (x)
# elif __BYTE_ORDER == __LITTLE_ENDIAN
# define htonll(x) __bswap_64(x)
# define ntohll(x) __bswap_64(x)
# endif
# else
# include <sys/endian.h>
# define htonll(x) htobe64(x)
# define ntohll(x) be64toh(x)
# endif
#endif
/*
* FreeBSD's libc doesn't define this for userland code
*/
#ifndef s6_addr32
#define s6_addr32 __u6_addr.__u6_addr32
#endif
/*
* One of the those eternal staples of C coding:
*/
#ifndef MIN
# define MIN(a, b) ((a) < (b)? (a) : (b))
# define MAX(a, b) ((a) > (b)? (a) : (b))
#endif
#define DECLARE_BITMAP(name, NBITS) \
uint32_t name[(NBITS+31) >> 5] = { 0 }
#define __BIT_INDEX(nr) (nr >> 5)
#define __BIT_MASK(nr) (1 << (nr & 31))
static inline void
set_bit(uint32_t *map, unsigned int nr)
{
map[__BIT_INDEX(nr)] |= __BIT_MASK(nr);
}
static inline void
clear_bit(uint32_t *map, unsigned int nr)
{
map[__BIT_INDEX(nr)] &= ~__BIT_MASK(nr);
}
static inline int
test_bit(const uint32_t *map, unsigned int nr)
{
return !!(map[__BIT_INDEX(nr)] & __BIT_MASK(nr));
}
/*
* Dynamically sized bit vector
*/
extern isns_bitvector_t *isns_bitvector_alloc(void);
extern void isns_bitvector_init(isns_bitvector_t *);
extern void isns_bitvector_destroy(isns_bitvector_t *);
extern void isns_bitvector_free(isns_bitvector_t *);
extern int isns_bitvector_test_bit(const isns_bitvector_t *, unsigned int);
extern int isns_bitvector_set_bit(isns_bitvector_t *, unsigned int);
extern int isns_bitvector_clear_bit(isns_bitvector_t *, unsigned int);
extern int isns_bitvector_is_empty(const isns_bitvector_t *);
extern int isns_bitvector_intersect(const isns_bitvector_t *a,
const isns_bitvector_t *b,
isns_bitvector_t *result);
extern void isns_bitvector_print(const isns_bitvector_t *,
isns_print_fn_t *);
extern void isns_bitvector_foreach(const isns_bitvector_t *bv,
int (*cb)(uint32_t, void *),
void *user_data);
/*
* List manipulation primites
*/
typedef struct isns_list isns_list_t;
struct isns_list {
isns_list_t * next;
isns_list_t * prev;
};
#define ISNS_LIST_DECLARE(list) \
isns_list_t list = { &list, &list }
static inline void
isns_list_init(isns_list_t *head)
{
head->next = head->prev = head;
}
static inline void
__isns_list_insert(isns_list_t *prev, isns_list_t *item, isns_list_t *next)
{
item->next = next;
item->prev = prev;
next->prev = item;
prev->next = item;
}
static inline void
isns_list_append(isns_list_t *head, isns_list_t *item)
{
__isns_list_insert(head->prev, item, head);
}
static inline void
isns_list_insert(isns_list_t *head, isns_list_t *item)
{
__isns_list_insert(head, item, head->next);
}
static inline void
isns_item_insert_before(isns_list_t *where, isns_list_t *item)
{
__isns_list_insert(where->prev, item, where);
}
static inline void
isns_item_insert_after(isns_list_t *where, isns_list_t *item)
{
__isns_list_insert(where, item, where->next);
}
static inline void
isns_list_del(isns_list_t *item)
{
isns_list_t *prev = item->prev;
isns_list_t *next = item->next;
prev->next = next;
next->prev = prev;
item->next = item->prev = item;
}
static inline int
isns_list_empty(const isns_list_t *head)
{
return head == head->next;
}
static inline void
isns_list_move(isns_list_t *dst, isns_list_t *src)
{
isns_list_t *prev, *next;
isns_list_t *head, *tail;
if (isns_list_empty(src))
return;
prev = dst->prev;
next = dst;
head = src->next;
tail = src->prev;
next->prev = tail;
prev->next = head;
head->prev = prev;
tail->next = next;
src->next = src->prev = src;
}
#define isns_list_item(type, member, ptr) \
container_of(type, member, ptr)
#define isns_list_foreach(list, __pos, __next) \
for (__pos = (list)->next; \
(__pos != list) && (__next = __pos->next, 1); \
__pos = __next)
#if 0
/* This is defined in stddef */
#define offsetof(type, member) ((unsigned long) &(((type *) 0)->member))
#endif
#define container_of(type, member, ptr) \
((type *) (((unsigned char *) ptr) - offsetof(type, member)))
/*
* Use isns_assert instead of libc's assert, so that the
* message can be captured and sent to syslog.
*/
#define isns_assert(condition) do { \
if (!(condition)) \
isns_assert_failed(#condition, \
__FILE__, __LINE__); \
} while (0)
#ifndef MDEBUG
# define isns_malloc(size) malloc(size)
# define isns_calloc(n, size) calloc(n, size)
# define isns_realloc(p, size) realloc(p, size)
# define isns_strdup(s) strdup(s)
# define isns_free(p) free(p)
#else
# define isns_malloc(size) isns_malloc_fn(size, __FILE__, __LINE__)
# define isns_calloc(n, size) isns_calloc_fn(n, size, __FILE__, __LINE__)
# define isns_realloc(p, size) isns_realloc_fn(p, size, __FILE__, __LINE__)
# define isns_strdup(s) isns_strdup_fn(s, __FILE__, __LINE__)
# define isns_free(p) isns_free_fn(p, __FILE__, __LINE__)
extern void * (*isns_malloc_fn)(size_t, const char *, unsigned int);
extern void * (*isns_calloc_fn)(unsigned int, size_t,
const char *, unsigned int);
extern void * (*isns_realloc_fn)(void *, size_t,
const char *, unsigned int);
extern char * (*isns_strdup_fn)(const char *, const char *, unsigned int);
extern void (*isns_free_fn)(void *, const char *, unsigned int);
#endif
#endif /* UTIL_H */
|