X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fshared%2Futil.h;h=90464c940be37b0eeb64e3300729b050cb68da4e;hb=981e4cd325410384cdadd837f34c002699d2d750;hp=e99f8d1123641da0ae4f0e8f4a85d55aad463ffd;hpb=4cf7ea556aa1e74f9b34d4467f36d46a1bb25da3;p=elogind.git diff --git a/src/shared/util.h b/src/shared/util.h index e99f8d112..90464c940 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -149,7 +149,9 @@ char *endswith(const char *s, const char *postfix) _pure_; bool first_word(const char *s, const char *word) _pure_; int close_nointr(int fd); -void close_nointr_nofail(int fd); +int safe_close(int fd); +void safe_close_pair(int p[]); + void close_many(const int fds[], unsigned n_fd); int parse_size(const char *t, off_t base, off_t *size); @@ -377,7 +379,6 @@ int ignore_signals(int sig, ...); int default_signals(int sig, ...); int sigaction_many(const struct sigaction *sa, ...); -int close_pipe(int p[]); int fopen_temporary(const char *path, FILE **_f, char **_temp_path); ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll); @@ -620,16 +621,15 @@ static inline void freep(void *p) { struct __useless_struct_to_allow_trailing_semicolon__ static inline void closep(int *fd) { - if (*fd >= 0) - close_nointr_nofail(*fd); + safe_close(*fd); } static inline void umaskp(mode_t *u) { umask(*u); } -static inline void close_pipep(int (*p)[2]) { - close_pipe(*p); +static inline void close_pairp(int (*p)[2]) { + safe_close_pair(*p); } DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, fclose); @@ -645,7 +645,7 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, endmntent); #define _cleanup_pclose_ _cleanup_(pclosep) #define _cleanup_closedir_ _cleanup_(closedirp) #define _cleanup_endmntent_ _cleanup_(endmntentp) -#define _cleanup_close_pipe_ _cleanup_(close_pipep) +#define _cleanup_close_pair_ _cleanup_(close_pairp) _malloc_ _alloc_(1, 2) static inline void *malloc_multiply(size_t a, size_t b) { if (_unlikely_(b == 0 || a > ((size_t) -1) / b)) @@ -782,6 +782,12 @@ static inline unsigned u32ctz(uint32_t n) { #endif } +static inline int log2i(int x) { + assert(x > 0); + + return __SIZEOF_INT__ * 8 - __builtin_clz(x) - 1; +} + static inline bool logind_running(void) { return access("/run/systemd/seats/", F_OK) >= 0; } @@ -914,3 +920,5 @@ const char *personality_to_string(unsigned long); uint64_t physical_memory(void); char* mount_test_option(const char *haystack, const char *needle); + +void hexdump(FILE *f, const void *p, size_t s);