X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fshared%2Futil.h;h=68e87da7cd4213ed35f1e755d8a83dbdd7e37dfa;hp=7c3da08dd8856787a8937a882d79aba17e6eae6d;hb=b1e2b33c5258f1cefcee55116ac5d049478c804d;hpb=144e51eca20b72c8177314c225d8c15c1b0b9d6b diff --git a/src/shared/util.h b/src/shared/util.h index 7c3da08dd..68e87da7c 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -21,6 +21,7 @@ along with systemd; If not, see . ***/ +#include #include #include #include @@ -36,7 +37,8 @@ #include #include #include -#include +#include +#include #include "macro.h" #include "time-util.h" @@ -51,7 +53,7 @@ union dirent_storage { #define WHITESPACE " \t\n\r" #define NEWLINE "\n\r" #define QUOTES "\"\'" -#define COMMENTS "#;\n" +#define COMMENTS "#;" #define FORMAT_BYTES_MAX 8 @@ -359,8 +361,8 @@ int pipe_eof(int fd); cpu_set_t* cpu_set_malloc(unsigned *ncpus); -int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char *format, va_list ap); -int status_printf(const char *status, bool ellipse, bool ephemeral, const char *format, ...); +int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char *format, va_list ap) _printf_attr_(4,0); +int status_printf(const char *status, bool ellipse, bool ephemeral, const char *format, ...) _printf_attr_(4,5); int status_welcome(void); int fd_columns(int fd); @@ -498,7 +500,7 @@ char *format_bytes(char *buf, size_t l, off_t t); int fd_wait_for_event(int fd, int event, usec_t timeout); -void* memdup(const void *p, size_t l) _malloc_; +void* memdup(const void *p, size_t l) _alloc_(2); int is_kernel_thread(pid_t pid); @@ -526,26 +528,46 @@ static inline void freep(void *p) { free(*(void**) p); } -void fclosep(FILE **f); -void pclosep(FILE **f); -void closep(int *fd); -void closedirp(DIR **d); +static inline void fclosep(FILE **f) { + if (*f) + fclose(*f); +} + +static inline void pclosep(FILE **f) { + if (*f) + pclose(*f); +} + +static inline void closep(int *fd) { + if (*fd >= 0) + close_nointr_nofail(*fd); +} + +static inline void closedirp(DIR **d) { + if (*d) + closedir(*d); +} + static inline void umaskp(mode_t *u) { umask(*u); } -static inline void journal_closep(sd_journal **j) { - sd_journal_close(*j); -} +#define _cleanup_free_ _cleanup_(freep) +#define _cleanup_fclose_ _cleanup_(fclosep) +#define _cleanup_pclose_ _cleanup_(pclosep) +#define _cleanup_close_ _cleanup_(closep) +#define _cleanup_closedir_ _cleanup_(closedirp) +#define _cleanup_umask_ _cleanup_(umaskp) +#define _cleanup_globfree_ _cleanup_(globfree) -_malloc_ static inline void *malloc_multiply(size_t a, size_t b) { +_malloc_ _alloc_(1, 2) static inline void *malloc_multiply(size_t a, size_t b) { if (_unlikely_(b == 0 || a > ((size_t) -1) / b)) return NULL; return malloc(a * b); } -_malloc_ static inline void *memdup_multiply(const void *p, size_t a, size_t b) { +_alloc_(2, 3) static inline void *memdup_multiply(const void *p, size_t a, size_t b) { if (_unlikely_(b == 0 || a > ((size_t) -1) / b)) return NULL; @@ -605,7 +627,7 @@ int create_tmp_dir(char template[], char** dir_name); static inline void *mempset(void *s, int c, size_t n) { memset(s, c, n); - return (char*)s + n; + return (uint8_t*)s + n; } char *hexmem(const void *p, size_t l); @@ -615,27 +637,99 @@ char *strextend(char **x, ...); char *strrep(const char *s, unsigned n); void* greedy_realloc(void **p, size_t *allocated, size_t need); +#define GREEDY_REALLOC(array, allocated, need) \ + greedy_realloc((void**) &(array), &(allocated), sizeof((array)[0]) * (need)) static inline void _reset_errno_(int *saved_errno) { errno = *saved_errno; } -#define PROTECT_ERRNO __attribute__((cleanup(_reset_errno_))) int _saved_errno_ = errno +#define PROTECT_ERRNO _cleanup_(_reset_errno_) int _saved_errno_ = errno -struct umask_struct { +struct _umask_struct_ { mode_t mask; bool quit; }; -static inline void _reset_umask_(struct umask_struct *s) { +static inline void _reset_umask_(struct _umask_struct_ *s) { umask(s->mask); }; #define RUN_WITH_UMASK(mask) \ - for (__attribute__((cleanup(_reset_umask_))) struct umask_struct _saved_umask_ = { umask(mask), false }; \ + for (_cleanup_(_reset_umask_) struct _umask_struct_ _saved_umask_ = { umask(mask), false }; \ !_saved_umask_.quit ; \ _saved_umask_.quit = true) static inline unsigned u64log2(uint64_t n) { return (n > 1) ? __builtin_clzll(n) ^ 63U : 0; } + +static inline bool logind_running(void) { + return access("/run/systemd/seats/", F_OK) >= 0; +} + +#define DECIMAL_STR_WIDTH(x) \ + ({ \ + typeof(x) _x_ = (x); \ + unsigned ans = 1; \ + while (_x_ /= 10) \ + ans++; \ + ans; \ + }) + +int unlink_noerrno(const char *path); + +#define alloca0(n) \ + ({ \ + char *_new_; \ + size_t _len_ = n; \ + _new_ = alloca(_len_); \ + (void *) memset(_new_, 0, _len_); \ + }) + +#define strappenda(a, b) \ + ({ \ + const char *_a_ = (a), *_b_ = (b); \ + char *_c_; \ + size_t _x_, _y_; \ + _x_ = strlen(_a_); \ + _y_ = strlen(_b_); \ + _c_ = alloca(_x_ + _y_ + 1); \ + strcpy(stpcpy(_c_, _a_), _b_); \ + _c_; \ + }) + +#define procfs_file_alloca(pid, field) \ + ({ \ + pid_t _pid_ = (pid); \ + char *_r_; \ + _r_ = alloca(sizeof("/proc/") -1 + DECIMAL_STR_MAX(pid_t) + 1 + sizeof(field)); \ + sprintf(_r_, "/proc/%lu/" field, (unsigned long) _pid_); \ + _r_; \ + }) + +struct _locale_struct_ { + locale_t saved_locale; + locale_t new_locale; + bool quit; +}; + +static inline void _reset_locale_(struct _locale_struct_ *s) { + PROTECT_ERRNO; + if (s->saved_locale != (locale_t) 0) + uselocale(s->saved_locale); + if (s->new_locale != (locale_t) 0) + freelocale(s->new_locale); +} + +#define RUN_WITH_LOCALE(mask, loc) \ + for (_cleanup_(_reset_locale_) struct _locale_struct_ _saved_locale_ = { (locale_t) 0, (locale_t) 0, false }; \ + ({ \ + if (!_saved_locale_.quit) { \ + PROTECT_ERRNO; \ + _saved_locale_.new_locale = newlocale((mask), (loc), (locale_t) 0); \ + if (_saved_locale_.new_locale != (locale_t) 0) \ + _saved_locale_.saved_locale = uselocale(_saved_locale_.new_locale); \ + } \ + !_saved_locale_.quit; }) ; \ + _saved_locale_.quit = true)