diff options
author | Lennart Poettering <lennart@poettering.net> | 2013-04-02 16:31:55 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2013-04-02 17:47:59 +0200 |
commit | 5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0 (patch) | |
tree | 470f285684f6b3e79f9ff8c561deaa07a166cb07 /src/shared | |
parent | 2875e22b65452896d5569eecb3a08265dd658173 (diff) |
util: add a bit of syntactic sugar for saving/restoring errno
Diffstat (limited to 'src/shared')
-rw-r--r-- | src/shared/log.c | 31 | ||||
-rw-r--r-- | src/shared/util.c | 4 | ||||
-rw-r--r-- | src/shared/util.h | 6 |
3 files changed, 15 insertions, 26 deletions
diff --git a/src/shared/log.c b/src/shared/log.c index 293c261f9e..0dd04bc51e 100644 --- a/src/shared/log.c +++ b/src/shared/log.c @@ -598,18 +598,14 @@ int log_dump_internal( const char *func, char *buffer) { - int saved_errno, r; + PROTECT_ERRNO; /* This modifies the buffer... */ if (_likely_(LOG_PRI(level) > log_max_level)) return 0; - saved_errno = errno; - r = log_dispatch(level, file, line, func, NULL, NULL, buffer); - errno = saved_errno; - - return r; + return log_dispatch(level, file, line, func, NULL, NULL, buffer); } int log_metav( @@ -620,20 +616,16 @@ int log_metav( const char *format, va_list ap) { + PROTECT_ERRNO; char buffer[LINE_MAX]; - int saved_errno, r; if (_likely_(LOG_PRI(level) > log_max_level)) return 0; - saved_errno = errno; vsnprintf(buffer, sizeof(buffer), format, ap); char_array_0(buffer); - r = log_dispatch(level, file, line, func, NULL, NULL, buffer); - errno = saved_errno; - - return r; + return log_dispatch(level, file, line, func, NULL, NULL, buffer); } int log_meta( @@ -663,21 +655,17 @@ int log_metav_object( const char *format, va_list ap) { + PROTECT_ERRNO; char buffer[LINE_MAX]; - int saved_errno, r; if (_likely_(LOG_PRI(level) > log_max_level)) return 0; - saved_errno = errno; vsnprintf(buffer, sizeof(buffer), format, ap); char_array_0(buffer); - r = log_dispatch(level, file, line, func, - object_name, object, buffer); - errno = saved_errno; - - return r; + return log_dispatch(level, file, line, func, + object_name, object, buffer); } int log_meta_object( @@ -735,7 +723,7 @@ int log_struct_internal( const char *func, const char *format, ...) { - int saved_errno; + PROTECT_ERRNO; va_list ap; int r; @@ -748,8 +736,6 @@ int log_struct_internal( if ((level & LOG_FACMASK) == 0) level = log_facility | LOG_PRI(level); - saved_errno = errno; - if ((log_target == LOG_TARGET_AUTO || log_target == LOG_TARGET_JOURNAL_OR_KMSG || log_target == LOG_TARGET_JOURNAL) && @@ -843,7 +829,6 @@ int log_struct_internal( r = -EINVAL; } - errno = saved_errno; return r; } diff --git a/src/shared/util.c b/src/shared/util.c index 46c20bec9c..b69e7e882a 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -207,14 +207,12 @@ int close_nointr(int fd) { } void close_nointr_nofail(int fd) { - int saved_errno = errno; + PROTECT_ERRNO; /* like close_nointr() but cannot fail, and guarantees errno * is unchanged */ assert_se(close_nointr(fd) == 0); - - errno = saved_errno; } void close_many(const int fds[], unsigned n_fd) { diff --git a/src/shared/util.h b/src/shared/util.h index d1cdd901a0..7f050758a0 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -615,3 +615,9 @@ char *strextend(char **x, ...); char *strrep(const char *s, unsigned n); void* greedy_realloc(void **p, size_t *allocated, size_t need); + +static inline void reset_errno(int *saved_errno) { + errno = *saved_errno; +} + +#define PROTECT_ERRNO __attribute__((cleanup(reset_errno))) int _saved_errno_ = errno |