ia64/xen-unstable

view tools/xenstore/utils.h @ 7238:971e7c7411b3

Raise an exception if an error appears on the pipes to our children, and make
sure that the child's pipes are closed even under that exception. Move the
handling of POLLHUP to the end of the loop, so that we guarantee to read any
remaining data from the child if POLLHUP and POLLIN appear at the same time.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author emellor@ewan
date Thu Oct 06 10:13:11 2005 +0100 (2005-10-06)
parents 06d84bf87159
children 015f8ae81276
line source
1 #ifndef _UTILS_H
2 #define _UTILS_H
3 #include <stdbool.h>
4 #include <string.h>
5 #include <stdint.h>
7 /* Is A == B ? */
8 #define streq(a,b) (strcmp((a),(b)) == 0)
10 /* Does A start with B ? */
11 #define strstarts(a,b) (strncmp((a),(b),strlen(b)) == 0)
13 /* Does A end in B ? */
14 static inline bool strends(const char *a, const char *b)
15 {
16 if (strlen(a) < strlen(b))
17 return false;
19 return streq(a + strlen(a) - strlen(b), b);
20 }
22 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
24 #define ___stringify(x) #x
25 #define __stringify(x) ___stringify(x)
27 /* Convenient wrappers for malloc and realloc. Use them. */
28 #define new(type) ((type *)malloc_nofail(sizeof(type)))
29 #define new_array(type, num) realloc_array((type *)0, (num))
30 #define realloc_array(ptr, num) ((__typeof__(ptr))_realloc_array((ptr), sizeof((*ptr)), (num)))
32 void *malloc_nofail(size_t size);
33 void *realloc_nofail(void *ptr, size_t size);
34 void *_realloc_array(void *ptr, size_t size, size_t num);
36 void barf(const char *fmt, ...) __attribute__((noreturn));
37 void barf_perror(const char *fmt, ...) __attribute__((noreturn));
39 /* This version adds one byte (for nul term) */
40 void *grab_file(const char *filename, unsigned long *size);
41 void release_file(void *data, unsigned long size);
43 /* Signal handling: returns fd to listen on. */
44 int signal_to_fd(int signal);
45 void close_signal(int fd);
47 void xprintf(const char *fmt, ...);
49 #define eprintf(_fmt, _args...) xprintf("[ERR] %s" _fmt, __FUNCTION__, ##_args)
50 #define iprintf(_fmt, _args...) xprintf("[INF] %s" _fmt, __FUNCTION__, ##_args)
52 #ifdef DEBUG
53 #define dprintf(_fmt, _args...) xprintf("[DBG] %s" _fmt, __FUNCTION__, ##_args)
54 #else
55 #define dprintf(_fmt, _args...) ((void)0)
56 #endif
58 #endif /* _UTILS_H */