X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fshared%2Futil.h;h=4997225d832872e4a2146d1f65ecc5b62e3be9b0;hb=f32d2db140150b9d38684a699c9875b6e24ca27c;hp=ea87c9695604b611a070c2d17cc50f012185b6e4;hpb=1dedb74a2e1d840b531b76b01a76979f3b57456b;p=elogind.git diff --git a/src/shared/util.h b/src/shared/util.h index ea87c9695..4997225d8 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; @@ -432,6 +433,7 @@ int sigprocmask_many(int how, ...); bool hostname_is_set(void); +char* lookup_uid(uid_t uid); char* gethostname_malloc(void); char* getlogname_malloc(void); char* getusername_malloc(void); @@ -582,8 +584,6 @@ int block_get_whole_disk(dev_t d, dev_t *ret); int file_is_priv_sticky(const char *p); -int strdup_or_null(const char *a, char **b); - #define NULSTR_FOREACH(i, l) \ for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1) @@ -794,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; @@ -830,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; } @@ -853,6 +877,22 @@ int unlink_noerrno(const char *path); (void *) memset(_new_, 0, _len_); \ }) +#define alloca_align(size, align) \ + ({ \ + void *_ptr_; \ + size_t _mask_ = (align) - 1; \ + _ptr_ = alloca((size) + _mask_); \ + (void*)(((uintptr_t)_ptr_ + _mask_) & ~_mask_); \ + }) + +#define alloca0_align(size, align) \ + ({ \ + void *_new_; \ + size_t _size_ = (size); \ + _new_ = alloca_align(_size_, (align)); \ + (void*)memset(_new_, 0, _size_); \ + }) + #define strappenda(a, ...) \ ({ \ int _len = strlen(a); \ @@ -976,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_first_word(const char **p, char **ret, bool relax); int unquote_many_words(const char **p, ...) _sentinel_; int free_and_strdup(char **p, const char *s); + +int sethostname_idempotent(const char *s);