X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fshared%2Futil.h;h=af589b6708c79d04f7b917538a1c408935be3711;hb=b5884878a2874447b2a9f07f324a7cd909d96d48;hp=a1d5657237368803ef9c6ab8e781d3670ddff132;hpb=95d78c7e7c81a6b788f28c33ef2cafd87471a0d7;p=elogind.git diff --git a/src/shared/util.h b/src/shared/util.h index a1d565723..af589b670 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -205,7 +205,7 @@ int safe_atod(const char *s, double *ret_d); int safe_atou8(const char *s, uint8_t *ret); -#if __WORDSIZE == 32 +#if LONG_MAX == INT_MAX static inline int safe_atolu(const char *s, unsigned long *ret_u) { assert_cc(sizeof(unsigned long) == sizeof(unsigned)); return safe_atou(s, (unsigned*) ret_u); @@ -321,6 +321,7 @@ int make_console_stdio(void); int dev_urandom(void *p, size_t n); void random_bytes(void *p, size_t n); +void initialize_srand(void); static inline uint64_t random_u64(void) { uint64_t u; @@ -793,6 +794,15 @@ static inline void _reset_errno_(int *saved_errno) { #define PROTECT_ERRNO _cleanup_(_reset_errno_) __attribute__((unused)) int _saved_errno_ = errno +static inline int negative_errno(void) { + /* This helper should be used to shut up gcc if you know 'errno' is + * negative. Instead of "return -errno;", use "return negative_errno();" + * It will suppress bogus gcc warnings in case it assumes 'errno' might + * be 0 and thus the caller's error-handling might not be triggered. */ + assert_return(errno > 0, -EINVAL); + return -errno; +} + struct _umask_struct_ { mode_t mask; bool quit; @@ -829,6 +839,21 @@ static inline int log2i(int x) { return __SIZEOF_INT__ * 8 - __builtin_clz(x) - 1; } +static inline unsigned log2u(unsigned x) { + assert(x > 0); + + return sizeof(unsigned) * 8 - __builtin_clz(x) - 1; +} + +static inline unsigned log2u_round_up(unsigned x) { + assert(x > 0); + + if (x == 1) + return 0; + + return log2u(x - 1) + 1; +} + static inline bool logind_running(void) { return access("/run/systemd/seats/", F_OK) >= 0; } @@ -991,8 +1016,11 @@ bool is_localhost(const char *hostname); int take_password_lock(const char *root); int is_symlink(const char *path); +int is_dir(const char *path, bool follow); int unquote_first_word(const char **p, char **ret); int unquote_many_words(const char **p, ...) _sentinel_; int free_and_strdup(char **p, const char *s); + +int sethostname_idempotent(const char *s);