util: split out errno related stuff
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include "async.h"
|
||||
#include "errno-util.h"
|
||||
#include "fd-util.h"
|
||||
#include "log.h"
|
||||
#include "macro.h"
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1+ */
|
||||
#pragma once
|
||||
|
||||
#include "macro.h"
|
||||
|
||||
static inline void _reset_errno_(int *saved_errno) {
|
||||
if (*saved_errno < 0) /* Invalidated by UNPROTECT_ERRNO? */
|
||||
return;
|
||||
|
||||
errno = *saved_errno;
|
||||
}
|
||||
|
||||
#define PROTECT_ERRNO \
|
||||
_cleanup_(_reset_errno_) _unused_ int _saved_errno_ = errno
|
||||
|
||||
#define UNPROTECT_ERRNO \
|
||||
do { \
|
||||
errno = _saved_errno_; \
|
||||
_saved_errno_ = -1; \
|
||||
} while (false)
|
||||
|
||||
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;
|
||||
}
|
||||
+1
-1
@@ -10,8 +10,8 @@
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "errno-util.h"
|
||||
#include "time-util.h"
|
||||
#include "util.h"
|
||||
|
||||
int unlink_noerrno(const char *path);
|
||||
|
||||
|
||||
+1
-1
@@ -19,6 +19,7 @@
|
||||
#include "sd-messages.h"
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "errno-util.h"
|
||||
#include "fd-util.h"
|
||||
#include "format-util.h"
|
||||
#include "io-util.h"
|
||||
@@ -37,7 +38,6 @@
|
||||
#include "terminal-util.h"
|
||||
#include "time-util.h"
|
||||
#include "utf8.h"
|
||||
#include "util.h"
|
||||
|
||||
#define SNDBUF_SIZE (8*1024*1024)
|
||||
|
||||
|
||||
@@ -45,6 +45,7 @@ basic_sources = files('''
|
||||
env-util.h
|
||||
errno-list.c
|
||||
errno-list.h
|
||||
errno-util.h
|
||||
escape.c
|
||||
escape.h
|
||||
ether-addr-util.c
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@
|
||||
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "util.h"
|
||||
#include "errno-util.h"
|
||||
|
||||
typedef enum RemoveFlags {
|
||||
REMOVE_ONLY_DIRECTORIES = 1 << 0,
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#endif
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "errno-util.h"
|
||||
#include "fd-util.h"
|
||||
#include "log.h"
|
||||
#include "macro.h"
|
||||
@@ -23,7 +24,6 @@
|
||||
#include "selinux-util.h"
|
||||
#include "stdio-util.h"
|
||||
#include "time-util.h"
|
||||
#include "util.h"
|
||||
|
||||
#if HAVE_SELINUX
|
||||
DEFINE_TRIVIAL_CLEANUP_FUNC(char*, freecon);
|
||||
|
||||
@@ -63,31 +63,6 @@ void in_initrd_force(bool value);
|
||||
|
||||
int on_ac_power(void);
|
||||
|
||||
static inline void _reset_errno_(int *saved_errno) {
|
||||
if (*saved_errno < 0) /* Invalidated by UNPROTECT_ERRNO? */
|
||||
return;
|
||||
|
||||
errno = *saved_errno;
|
||||
}
|
||||
|
||||
#define PROTECT_ERRNO \
|
||||
_cleanup_(_reset_errno_) _unused_ int _saved_errno_ = errno
|
||||
|
||||
#define UNPROTECT_ERRNO \
|
||||
do { \
|
||||
errno = _saved_errno_; \
|
||||
_saved_errno_ = -1; \
|
||||
} while (false)
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
static inline unsigned u64log2(uint64_t n) {
|
||||
#if __SIZEOF_LONG_LONG__ == 8
|
||||
return (n > 1) ? (unsigned) __builtin_clzll(n) ^ 63U : 0;
|
||||
|
||||
Reference in New Issue
Block a user