tree-wide: use new RET_NERRNO() helper at various places

This commit is contained in:
Lennart Poettering
2021-11-14 22:40:49 +01:00
committed by Zbigniew Jędrzejewski-Szmek
parent ef470ffa23
commit 7c248223eb
51 changed files with 151 additions and 469 deletions

View File

@@ -629,10 +629,7 @@ int cg_set_xattr(const char *controller, const char *path, const char *name, con
if (r < 0)
return r;
if (setxattr(fs, name, value, size, flags) < 0)
return -errno;
return 0;
return RET_NERRNO(setxattr(fs, name, value, size, flags));
}
int cg_get_xattr(const char *controller, const char *path, const char *name, void *value, size_t size) {
@@ -697,10 +694,7 @@ int cg_remove_xattr(const char *controller, const char *path, const char *name)
if (r < 0)
return r;
if (removexattr(fs, name) < 0)
return -errno;
return 0;
return RET_NERRNO(removexattr(fs, name));
}
int cg_pid_get_path(const char *controller, pid_t pid, char **ret_path) {

View File

@@ -121,10 +121,7 @@ int read_attr_fd(int fd, unsigned *ret) {
if (!S_ISDIR(st.st_mode) && !S_ISREG(st.st_mode))
return -ENOTTY;
if (ioctl(fd, FS_IOC_GETFLAGS, ret) < 0)
return -errno;
return 0;
return RET_NERRNO(ioctl(fd, FS_IOC_GETFLAGS, ret));
}
int read_attr_path(const char *p, unsigned *ret) {

View File

@@ -8,6 +8,7 @@
#include "alloc-util.h"
#include "env-util.h"
#include "errno-util.h"
#include "escape.h"
#include "extract-word.h"
#include "macro.h"
@@ -786,15 +787,12 @@ int getenv_bool_secure(const char *p) {
}
int set_unset_env(const char *name, const char *value, bool overwrite) {
int r;
assert(name);
if (value)
r = setenv(name, value, overwrite);
else
r = unsetenv(name);
if (r < 0)
return -errno;
return 0;
return RET_NERRNO(setenv(name, value, overwrite));
return RET_NERRNO(unsetenv(name));
}
int putenv_dup(const char *assignment, bool override) {
@@ -807,9 +805,7 @@ int putenv_dup(const char *assignment, bool override) {
n = strndupa_safe(assignment, e - assignment);
/* This is like putenv(), but uses setenv() so that our memory doesn't become part of environ[]. */
if (setenv(n, e + 1, override) < 0)
return -errno;
return 0;
return RET_NERRNO(setenv(n, e + 1, override));
}
int setenv_systemd_exec_pid(bool update_only) {

View File

@@ -152,10 +152,7 @@ int fd_nonblock(int fd, bool nonblock) {
if (nflags == flags)
return 0;
if (fcntl(fd, F_SETFL, nflags) < 0)
return -errno;
return 0;
return RET_NERRNO(fcntl(fd, F_SETFL, nflags));
}
int fd_cloexec(int fd, bool cloexec) {
@@ -171,10 +168,7 @@ int fd_cloexec(int fd, bool cloexec) {
if (nflags == flags)
return 0;
if (fcntl(fd, F_SETFD, nflags) < 0)
return -errno;
return 0;
return RET_NERRNO(fcntl(fd, F_SETFD, nflags));
}
_pure_ static bool fd_in_set(int fd, const int fdset[], size_t n_fdset) {
@@ -802,8 +796,5 @@ int btrfs_defrag_fd(int fd) {
if (r < 0)
return r;
if (ioctl(fd, BTRFS_IOC_DEFRAG, NULL) < 0)
return -errno;
return 0;
return RET_NERRNO(ioctl(fd, BTRFS_IOC_DEFRAG, NULL));
}

View File

@@ -30,18 +30,13 @@
#include "strv.h"
#include "time-util.h"
#include "tmpfile-util.h"
#include "umask-util.h"
#include "user-util.h"
#include "util.h"
int unlink_noerrno(const char *path) {
PROTECT_ERRNO;
int r;
r = unlink(path);
if (r < 0)
return -errno;
return 0;
return RET_NERRNO(unlink(path));
}
int rmdir_parents(const char *path, const char *stop) {
@@ -97,8 +92,8 @@ int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char
* want — though not atomic (i.e. for a short period both the new and the old filename will exist). */
if (linkat(olddirfd, oldpath, newdirfd, newpath, 0) >= 0) {
if (unlinkat(olddirfd, oldpath, 0) < 0) {
r = -errno; /* Backup errno before the following unlinkat() alters it */
r = RET_NERRNO(unlinkat(olddirfd, oldpath, 0));
if (r < 0) {
(void) unlinkat(newdirfd, newpath, 0);
return r;
}
@@ -117,10 +112,7 @@ int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char
if (errno != ENOENT)
return -errno;
if (renameat(olddirfd, oldpath, newdirfd, newpath) < 0)
return -errno;
return 0;
return RET_NERRNO(renameat(olddirfd, oldpath, newdirfd, newpath));
}
int readlinkat_malloc(int fd, const char *p, char **ret) {
@@ -286,14 +278,9 @@ int fchmod_and_chown_with_fallback(int fd, const char *path, mode_t mode, uid_t
}
int fchmod_umask(int fd, mode_t m) {
mode_t u;
int r;
_cleanup_umask_ mode_t u = umask(0777);
u = umask(0777);
r = fchmod(fd, m & (~u)) < 0 ? -errno : 0;
umask(u);
return r;
return RET_NERRNO(fchmod(fd, m & (~u)));
}
int fchmod_opath(int fd, mode_t m) {
@@ -822,7 +809,7 @@ int unlinkat_deallocate(int fd, const char *name, UnlinkDeallocateFlags flags) {
int open_parent(const char *path, int flags, mode_t mode) {
_cleanup_free_ char *parent = NULL;
int fd, r;
int r;
r = path_extract_directory(path, &parent);
if (r < 0)
@@ -836,11 +823,7 @@ int open_parent(const char *path, int flags, mode_t mode) {
else if (!FLAGS_SET(flags, O_TMPFILE))
flags |= O_DIRECTORY|O_RDONLY;
fd = open(parent, flags, mode);
if (fd < 0)
return -errno;
return fd;
return RET_NERRNO(open(parent, flags, mode));
}
int conservative_renameat(

View File

@@ -47,7 +47,7 @@ int fd_warn_permissions(const char *path, int fd);
int stat_warn_permissions(const char *path, const struct stat *st);
#define laccess(path, mode) \
(faccessat(AT_FDCWD, (path), (mode), AT_SYMLINK_NOFOLLOW) < 0 ? -errno : 0)
RET_NERRNO(faccessat(AT_FDCWD, (path), (mode), AT_SYMLINK_NOFOLLOW))
int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode);
int touch(const char *path);

View File

@@ -10,6 +10,7 @@
#include <sys/prctl.h>
#include "alloc-util.h"
#include "errno-util.h"
#include "fd-util.h"
#include "macro.h"
#include "memfd-util.h"
@@ -21,7 +22,6 @@
int memfd_new(const char *name) {
_cleanup_free_ char *g = NULL;
int fd;
if (!name) {
char pr[17] = {};
@@ -49,11 +49,7 @@ int memfd_new(const char *name) {
}
}
fd = memfd_create(name, MFD_ALLOW_SEALING | MFD_CLOEXEC);
if (fd < 0)
return -errno;
return fd;
return RET_NERRNO(memfd_create(name, MFD_ALLOW_SEALING | MFD_CLOEXEC));
}
int memfd_map(int fd, uint64_t offset, size_t size, void **p) {
@@ -72,7 +68,6 @@ int memfd_map(int fd, uint64_t offset, size_t size, void **p) {
q = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, offset);
else
q = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset);
if (q == MAP_FAILED)
return -errno;
@@ -81,15 +76,9 @@ int memfd_map(int fd, uint64_t offset, size_t size, void **p) {
}
int memfd_set_sealed(int fd) {
int r;
assert(fd >= 0);
r = fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE | F_SEAL_SEAL);
if (r < 0)
return -errno;
return 0;
return RET_NERRNO(fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE | F_SEAL_SEAL));
}
int memfd_get_sealed(int fd) {
@@ -106,13 +95,11 @@ int memfd_get_sealed(int fd) {
int memfd_get_size(int fd, uint64_t *sz) {
struct stat stat;
int r;
assert(fd >= 0);
assert(sz);
r = fstat(fd, &stat);
if (r < 0)
if (fstat(fd, &stat) < 0)
return -errno;
*sz = stat.st_size;
@@ -120,15 +107,9 @@ int memfd_get_size(int fd, uint64_t *sz) {
}
int memfd_set_size(int fd, uint64_t sz) {
int r;
assert(fd >= 0);
r = ftruncate(fd, sz);
if (r < 0)
return -errno;
return 0;
return RET_NERRNO(ftruncate(fd, sz));
}
int memfd_new_and_map(const char *name, size_t sz, void **p) {

View File

@@ -80,15 +80,11 @@ int mkdir_safe_internal(
}
int mkdir_errno_wrapper(const char *pathname, mode_t mode) {
if (mkdir(pathname, mode) < 0)
return -errno;
return 0;
return RET_NERRNO(mkdir(pathname, mode));
}
int mkdirat_errno_wrapper(int dirfd, const char *pathname, mode_t mode) {
if (mkdirat(dirfd, pathname, mode) < 0)
return -errno;
return 0;
return RET_NERRNO(mkdirat(dirfd, pathname, mode));
}
int mkdir_safe(const char *path, mode_t mode, uid_t uid, gid_t gid, MkdirFlags flags) {

View File

@@ -4,6 +4,7 @@
#include <sys/ioctl.h>
#include <sys/mount.h>
#include "errno-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "missing_fs.h"
@@ -177,10 +178,7 @@ int detach_mount_namespace(void) {
if (unshare(CLONE_NEWNS) < 0)
return -errno;
if (mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL) < 0)
return -errno;
return 0;
return RET_NERRNO(mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL));
}
int userns_acquire(const char *uid_map, const char *gid_map) {

View File

@@ -813,7 +813,7 @@ int wait_for_terminate_with_timeout(pid_t pid, usec_t timeout) {
if (n >= until)
break;
r = sigtimedwait(&mask, NULL, timespec_store(&ts, until - n)) < 0 ? -errno : 0;
r = RET_NERRNO(sigtimedwait(&mask, NULL, timespec_store(&ts, until - n)));
/* Assuming we woke due to the child exiting. */
if (waitid(P_PID, pid, &status, WEXITED|WNOHANG) == 0) {
if (status.si_pid == pid) {
@@ -871,7 +871,7 @@ void sigterm_wait(pid_t pid) {
int kill_and_sigcont(pid_t pid, int sig) {
int r;
r = kill(pid, sig) < 0 ? -errno : 0;
r = RET_NERRNO(kill(pid, sig));
/* If this worked, also send SIGCONT, unless we already just sent a SIGCONT, or SIGKILL was sent which isn't
* affected by a process being suspended anyway. */

View File

@@ -3,6 +3,7 @@
#include <errno.h>
#include "alloc-util.h"
#include "errno-util.h"
#include "extract-word.h"
#include "fd-util.h"
#include "format-util.h"
@@ -45,10 +46,7 @@ int setrlimit_closest(int resource, const struct rlimit *rlim) {
log_debug("Failed at setting rlimit " RLIM_FMT " for resource RLIMIT_%s. Will attempt setting value " RLIM_FMT " instead.", rlim->rlim_max, rlimit_to_string(resource), fixed.rlim_max);
if (setrlimit(resource, &fixed) < 0)
return -errno;
return 0;
return RET_NERRNO(setrlimit(resource, &fixed));
}
int setrlimit_closest_all(const struct rlimit *const *rlim, int *which_failed) {

View File

@@ -3,6 +3,7 @@
#include <errno.h>
#include <stdarg.h>
#include "errno-util.h"
#include "macro.h"
#include "parse-util.h"
#include "signal-util.h"
@@ -39,10 +40,7 @@ int reset_signal_mask(void) {
if (sigemptyset(&ss) < 0)
return -errno;
if (sigprocmask(SIG_SETMASK, &ss, NULL) < 0)
return -errno;
return 0;
return RET_NERRNO(sigprocmask(SIG_SETMASK, &ss, NULL));
}
int sigaction_many_internal(const struct sigaction *sa, ...) {
@@ -247,11 +245,7 @@ int signal_is_blocked(int sig) {
if (r != 0)
return -r;
r = sigismember(&ss, sig);
if (r < 0)
return -errno;
return r;
return RET_NERRNO(sigismember(&ss, sig));
}
int pop_pending_signal_internal(int sig, ...) {

View File

@@ -1226,10 +1226,7 @@ int socket_bind_to_ifname(int fd, const char *ifname) {
/* Call with NULL to drop binding */
if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen_ptr(ifname)) < 0)
return -errno;
return 0;
return RET_NERRNO(setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen_ptr(ifname)));
}
int socket_bind_to_ifindex(int fd, int ifindex) {
@@ -1238,13 +1235,9 @@ int socket_bind_to_ifindex(int fd, int ifindex) {
assert(fd >= 0);
if (ifindex <= 0) {
if (ifindex <= 0)
/* Drop binding */
if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, NULL, 0) < 0)
return -errno;
return 0;
}
return RET_NERRNO(setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, NULL, 0));
r = setsockopt_int(fd, SOL_SOCKET, SO_BINDTOIFINDEX, ifindex);
if (r != -ENOPROTOOPT)
@@ -1332,16 +1325,10 @@ int socket_set_unicast_if(int fd, int af, int ifi) {
switch (af) {
case AF_INET:
if (setsockopt(fd, IPPROTO_IP, IP_UNICAST_IF, &ifindex_be, sizeof(ifindex_be)) < 0)
return -errno;
return 0;
return RET_NERRNO(setsockopt(fd, IPPROTO_IP, IP_UNICAST_IF, &ifindex_be, sizeof(ifindex_be)));
case AF_INET6:
if (setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_IF, &ifindex_be, sizeof(ifindex_be)) < 0)
return -errno;
return 0;
return RET_NERRNO(setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_IF, &ifindex_be, sizeof(ifindex_be)));
default:
return -EAFNOSUPPORT;

View File

@@ -66,10 +66,7 @@ int fsync_directory_of_file(int fd) {
return dfd;
}
if (fsync(dfd) < 0)
return -errno;
return 0;
return RET_NERRNO(fsync(dfd));
}
int fsync_full(int fd) {
@@ -77,7 +74,7 @@ int fsync_full(int fd) {
/* Sync both the file and the directory */
r = fsync(fd) < 0 ? -errno : 0;
r = RET_NERRNO(fsync(fd));
q = fsync_directory_of_file(fd);
if (r < 0) /* Return earlier error */
@@ -109,10 +106,7 @@ int fsync_path_at(int at_fd, const char *path) {
fd = opened_fd;
}
if (fsync(fd) < 0)
return -errno;
return 0;
return RET_NERRNO(fsync(fd));
}
int fsync_parent_at(int at_fd, const char *path) {
@@ -126,10 +120,7 @@ int fsync_parent_at(int at_fd, const char *path) {
if (opened_fd < 0)
return -errno;
if (fsync(opened_fd) < 0)
return -errno;
return 0;
return RET_NERRNO(fsync(opened_fd));
}
opened_fd = openat(at_fd, path, O_PATH|O_CLOEXEC|O_NOFOLLOW);
@@ -160,7 +151,7 @@ int syncfs_path(int at_fd, const char *path) {
if (isempty(path)) {
if (at_fd != AT_FDCWD)
return syncfs(at_fd) < 0 ? -errno : 0;
return RET_NERRNO(syncfs(at_fd));
fd = open(".", O_RDONLY|O_DIRECTORY|O_CLOEXEC);
} else
@@ -168,8 +159,5 @@ int syncfs_path(int at_fd, const char *path) {
if (fd < 0)
return -errno;
if (syncfs(fd) < 0)
return -errno;
return 0;
return RET_NERRNO(syncfs(fd));
}

View File

@@ -74,10 +74,7 @@ int chvt(int vt) {
vt = tiocl[0] <= 0 ? 1 : tiocl[0];
}
if (ioctl(fd, VT_ACTIVATE, vt) < 0)
return -errno;
return 0;
return RET_NERRNO(ioctl(fd, VT_ACTIVATE, vt));
}
int read_one_char(FILE *f, char *ret, usec_t t, bool *need_nl) {
@@ -409,8 +406,7 @@ int acquire_terminal(
assert_se(sigaction(SIGHUP, &sa_new, &sa_old) == 0);
/* First, try to get the tty */
r = ioctl(fd, TIOCSCTTY,
(flags & ~ACQUIRE_TERMINAL_PERMISSIVE) == ACQUIRE_TERMINAL_FORCE) < 0 ? -errno : 0;
r = RET_NERRNO(ioctl(fd, TIOCSCTTY, (flags & ~ACQUIRE_TERMINAL_PERMISSIVE) == ACQUIRE_TERMINAL_FORCE));
/* Reset signal handler to old value */
assert_se(sigaction(SIGHUP, &sa_old, NULL) == 0);
@@ -500,7 +496,7 @@ int release_terminal(void) {
* by our own TIOCNOTTY */
assert_se(sigaction(SIGHUP, &sa_new, &sa_old) == 0);
r = ioctl(fd, TIOCNOTTY) < 0 ? -errno : 0;
r = RET_NERRNO(ioctl(fd, TIOCNOTTY));
assert_se(sigaction(SIGHUP, &sa_old, NULL) == 0);
@@ -509,11 +505,7 @@ int release_terminal(void) {
int terminal_vhangup_fd(int fd) {
assert(fd >= 0);
if (ioctl(fd, TIOCVHANGUP) < 0)
return -errno;
return 0;
return RET_NERRNO(ioctl(fd, TIOCVHANGUP));
}
int terminal_vhangup(const char *name) {
@@ -1361,10 +1353,7 @@ int vt_reset_keyboard(int fd) {
/* If we can't read the default, then default to unicode. It's 2017 after all. */
kb = vt_default_utf8() != 0 ? K_UNICODE : K_XLATE;
if (ioctl(fd, KDSKBMODE, kb) < 0)
return -errno;
return 0;
return RET_NERRNO(ioctl(fd, KDSKBMODE, kb));
}
int vt_restore(int fd) {

View File

@@ -65,16 +65,9 @@ int fopen_temporary(const char *path, FILE **ret_f, char **ret_temp_path) {
/* This is much like mkostemp() but is subject to umask(). */
int mkostemp_safe(char *pattern) {
int fd = -1; /* avoid false maybe-uninitialized warning */
assert(pattern);
RUN_WITH_UMASK(0077)
fd = mkostemp(pattern, O_CLOEXEC);
if (fd < 0)
return -errno;
return fd;
BLOCK_WITH_UMASK(0077);
return RET_NERRNO(mkostemp(pattern, O_CLOEXEC));
}
int fmkostemp_safe(char *pattern, const char *mode, FILE **ret_f) {
@@ -283,8 +276,6 @@ int open_tmpfile_linkable(const char *target, int flags, char **ret_path) {
}
int link_tmpfile(int fd, const char *path, const char *target) {
int r;
assert(fd >= 0);
assert(target);
@@ -295,16 +286,10 @@ int link_tmpfile(int fd, const char *path, const char *target) {
* Note that in both cases we will not replace existing files. This is because linkat() does not support this
* operation currently (renameat2() does), and there is no nice way to emulate this. */
if (path) {
r = rename_noreplace(AT_FDCWD, path, AT_FDCWD, target);
if (r < 0)
return r;
} else {
if (linkat(AT_FDCWD, FORMAT_PROC_FD_PATH(fd), AT_FDCWD, target, AT_SYMLINK_FOLLOW) < 0)
return -errno;
}
if (path)
return rename_noreplace(AT_FDCWD, path, AT_FDCWD, target);
return 0;
return RET_NERRNO(linkat(AT_FDCWD, FORMAT_PROC_FD_PATH(fd), AT_FDCWD, target, AT_SYMLINK_FOLLOW));
}
int mkdtemp_malloc(const char *template, char **ret) {

View File

@@ -680,10 +680,7 @@ int reset_uid_gid(void) {
if (setresgid(0, 0, 0) < 0)
return -errno;
if (setresuid(0, 0, 0) < 0)
return -errno;
return 0;
return RET_NERRNO(setresuid(0, 0, 0));
}
int take_etc_passwd_lock(const char *root) {
@@ -943,10 +940,7 @@ int maybe_setgroups(size_t size, const gid_t *list) {
}
}
if (setgroups(size, list) < 0)
return -errno;
return 0;
return RET_NERRNO(setgroups(size, list));
}
bool synthesize_nobody(void) {

View File

@@ -8,6 +8,7 @@
#include <sys/xattr.h>
#include "alloc-util.h"
#include "errno-util.h"
#include "fd-util.h"
#include "macro.h"
#include "missing_syscall.h"
@@ -202,10 +203,7 @@ int fd_setcrtime(int fd, usec_t usec) {
usec = now(CLOCK_REALTIME);
le = htole64((uint64_t) usec);
if (fsetxattr(fd, "user.crtime_usec", &le, sizeof(le), 0) < 0)
return -errno;
return 0;
return RET_NERRNO(fsetxattr(fd, "user.crtime_usec", &le, sizeof(le), 0));
}
int listxattr_at_malloc(

View File

@@ -335,10 +335,7 @@ static int boot_entry_file_check(const char *root, const char *p) {
if (!path)
return log_oom();
if (access(path, F_OK) < 0)
return -errno;
return 0;
return RET_NERRNO(access(path, F_OK));
}
static void boot_entry_file_list(const char *field, const char *root, const char *p, int *ret_status) {

View File

@@ -425,10 +425,7 @@ static int autofs_set_timeout(int dev_autofs_fd, int ioctl_fd, usec_t usec) {
/* Convert to seconds, rounding up. */
param.timeout.timeout = DIV_ROUND_UP(usec, USEC_PER_SEC);
if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_TIMEOUT, &param) < 0)
return -errno;
return 0;
return RET_NERRNO(ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_TIMEOUT, &param));
}
static int autofs_send_ready(int dev_autofs_fd, int ioctl_fd, uint32_t token, int status) {
@@ -446,10 +443,7 @@ static int autofs_send_ready(int dev_autofs_fd, int ioctl_fd, uint32_t token, in
} else
param.ready.token = token;
if (ioctl(dev_autofs_fd, status ? AUTOFS_DEV_IOCTL_FAIL : AUTOFS_DEV_IOCTL_READY, &param) < 0)
return -errno;
return 0;
return RET_NERRNO(ioctl(dev_autofs_fd, status ? AUTOFS_DEV_IOCTL_FAIL : AUTOFS_DEV_IOCTL_READY, &param));
}
static int automount_send_ready(Automount *a, Set *tokens, int status) {

Some files were not shown because too many files have changed in this diff Show More