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) {
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) {
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) {
#include "alloc-util.h"
#include "env-util.h"
+#include "errno-util.h"
#include "escape.h"
#include "extract-word.h"
#include "macro.h"
}
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) {
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) {
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) {
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) {
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));
}
#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) {
* 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;
}
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) {
}
int fchmod_umask(int fd, mode_t m) {
- mode_t u;
- int r;
-
- u = umask(0777);
- r = fchmod(fd, m & (~u)) < 0 ? -errno : 0;
- umask(u);
+ _cleanup_umask_ mode_t u = umask(0777);
- return r;
+ return RET_NERRNO(fchmod(fd, m & (~u)));
}
int fchmod_opath(int fd, mode_t m) {
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)
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(
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);
#include <sys/prctl.h>
#include "alloc-util.h"
+#include "errno-util.h"
#include "fd-util.h"
#include "macro.h"
#include "memfd-util.h"
int memfd_new(const char *name) {
_cleanup_free_ char *g = NULL;
- int fd;
if (!name) {
char pr[17] = {};
}
}
- 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) {
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;
}
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) {
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;
}
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) {
}
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) {
#include <sys/ioctl.h>
#include <sys/mount.h>
+#include "errno-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "missing_fs.h"
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) {
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) {
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. */
#include <errno.h>
#include "alloc-util.h"
+#include "errno-util.h"
#include "extract-word.h"
#include "fd-util.h"
#include "format-util.h"
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) {
#include <errno.h>
#include <stdarg.h>
+#include "errno-util.h"
#include "macro.h"
#include "parse-util.h"
#include "signal-util.h"
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, ...) {
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, ...) {
/* 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) {
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)
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;
return dfd;
}
- if (fsync(dfd) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(fsync(dfd));
}
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 */
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) {
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);
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
if (fd < 0)
return -errno;
- if (syncfs(fd) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(syncfs(fd));
}
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) {
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);
* 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);
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) {
/* 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) {
/* 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) {
}
int link_tmpfile(int fd, const char *path, const char *target) {
- int r;
-
assert(fd >= 0);
assert(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) {
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) {
}
}
- if (setgroups(size, list) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(setgroups(size, list));
}
bool synthesize_nobody(void) {
#include <sys/xattr.h>
#include "alloc-util.h"
+#include "errno-util.h"
#include "fd-util.h"
#include "macro.h"
#include "missing_syscall.h"
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(
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) {
/* Convert to seconds, rounding up. */
param.timeout.timeout = DIV_ROUND_UP(usec, USEC_PER_SEC);
- if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_TIMEOUT, ¶m) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_TIMEOUT, ¶m));
}
static int autofs_send_ready(int dev_autofs_fd, int ioctl_fd, uint32_t token, int status) {
} else
param.ready.token = token;
- if (ioctl(dev_autofs_fd, status ? AUTOFS_DEV_IOCTL_FAIL : AUTOFS_DEV_IOCTL_READY, ¶m) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(ioctl(dev_autofs_fd, status ? AUTOFS_DEV_IOCTL_FAIL : AUTOFS_DEV_IOCTL_READY, ¶m));
}
static int automount_send_ready(Automount *a, Set *tokens, int status) {
}
}
- r = connect(fd, &sa.sa, sa_len) < 0 ? -errno : 0;
+ r = RET_NERRNO(connect(fd, &sa.sa, sa_len));
/* If we fail to restore the uid or gid, things will likely
fail later on. This should only happen if an LSM interferes. */
case EXEC_INPUT_SOCKET:
assert(socket_fd >= 0);
- return dup2(socket_fd, STDIN_FILENO) < 0 ? -errno : STDIN_FILENO;
+ return RET_NERRNO(dup2(socket_fd, STDIN_FILENO));
case EXEC_INPUT_NAMED_FD:
assert(named_iofds[STDIN_FILENO] >= 0);
(void) fd_nonblock(named_iofds[STDIN_FILENO], false);
- return dup2(named_iofds[STDIN_FILENO], STDIN_FILENO) < 0 ? -errno : STDIN_FILENO;
+ return RET_NERRNO(dup2(named_iofds[STDIN_FILENO], STDIN_FILENO));
case EXEC_INPUT_DATA: {
int fd;
/* Duplicate from stdout if possible */
if (can_inherit_stderr_from_stdout(context, o, e))
- return dup2(STDOUT_FILENO, fileno) < 0 ? -errno : fileno;
+ return RET_NERRNO(dup2(STDOUT_FILENO, fileno));
o = e;
/* If the input is connected to anything that's not a /dev/null or a data fd, inherit that... */
if (!IN_SET(i, EXEC_INPUT_NULL, EXEC_INPUT_DATA))
- return dup2(STDIN_FILENO, fileno) < 0 ? -errno : fileno;
+ return RET_NERRNO(dup2(STDIN_FILENO, fileno));
/* If we are not started from PID 1 we just inherit STDOUT from our parent process. */
if (getppid() != 1)
case EXEC_OUTPUT_TTY:
if (is_terminal_input(i))
- return dup2(STDIN_FILENO, fileno) < 0 ? -errno : fileno;
+ return RET_NERRNO(dup2(STDIN_FILENO, fileno));
/* We don't reset the terminal if this is just about output */
return open_terminal_as(exec_context_tty_path(context), O_WRONLY, fileno);
case EXEC_OUTPUT_SOCKET:
assert(socket_fd >= 0);
- return dup2(socket_fd, fileno) < 0 ? -errno : fileno;
+ return RET_NERRNO(dup2(socket_fd, fileno));
case EXEC_OUTPUT_NAMED_FD:
assert(named_iofds[fileno] >= 0);
(void) fd_nonblock(named_iofds[fileno], false);
- return dup2(named_iofds[fileno], fileno) < 0 ? -errno : fileno;
+ return RET_NERRNO(dup2(named_iofds[fileno], fileno));
case EXEC_OUTPUT_FILE:
case EXEC_OUTPUT_FILE_APPEND:
streq_ptr(context->stdio_file[fileno], context->stdio_file[STDIN_FILENO]);
if (rw)
- return dup2(STDIN_FILENO, fileno) < 0 ? -errno : fileno;
+ return RET_NERRNO(dup2(STDIN_FILENO, fileno));
flags = O_WRONLY;
if (o == EXEC_OUTPUT_FILE_APPEND)
cmd = quote_command_line(c->argv, SHELL_ESCAPE_EMPTY);
fprintf(f,
"%sCommand Line: %s\n",
- prefix, cmd ? cmd : strerror_safe(ENOMEM));
+ prefix, cmd ?: strerror_safe(ENOMEM));
exec_status_dump(&c->exec_status, f, prefix2);
}
int set_unit_path(const char *p) {
/* This is mostly for debug purposes */
- if (setenv("SYSTEMD_UNIT_PATH", p, 1) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(setenv("SYSTEMD_UNIT_PATH", p, 1));
}
char *unit_dbus_path(Unit *u) {
if (!S_ISBLK(st.st_mode))
return -ENOTBLK;
- if (ioctl(fd, BLKGETSIZE64, ret) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(ioctl(fd, BLKGETSIZE64, ret));
}
static int block_get_size_by_path(const char *path, uint64_t *ret) {
if (i->flags & IMPORT_BTRFS_SUBVOL)
r = btrfs_subvol_make_fallback(d, 0755);
else
- r = mkdir(d, 0755) < 0 ? -errno : 0;
+ r = RET_NERRNO(mkdir(d, 0755));
if (r == -EEXIST && (i->flags & IMPORT_DIRECT)) /* EEXIST is OK if in direct mode, but not otherwise,
* because in that case our temporary path collided */
r = 0;
if (i->flags & PULL_BTRFS_SUBVOL)
r = btrfs_subvol_make_fallback(where, 0755);
else
- r = mkdir(where, 0755) < 0 ? -errno : 0;
+ r = RET_NERRNO(mkdir(where, 0755));
if (r == -EEXIST && (i->flags & PULL_DIRECT)) /* EEXIST is OK if in direct mode, but not otherwise,
* because in that case our temporary path collided */
r = 0;
if (its.it_value.tv_sec == 0 && its.it_value.tv_nsec == 0)
its.it_value.tv_nsec = 1;
- if (timerfd_settime(e->watchdog_fd, TFD_TIMER_ABSTIME, &its, NULL) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(timerfd_settime(e->watchdog_fd, TFD_TIMER_ABSTIME, &its, NULL));
}
static int process_watchdog(sd_event *e) {
int maxevents,
usec_t timeout) {
- int r, msec;
+ int msec;
#if 0
static bool epoll_pwait2_absent = false;
msec = (int) k;
}
- r = epoll_wait(fd,
- events,
- maxevents,
- msec);
- if (r < 0)
- return -errno;
-
- return r;
+ return RET_NERRNO(epoll_wait(fd, events, maxevents, msec));
}
static int process_epoll(sd_event *e, usec_t timeout, int64_t threshold, int64_t *ret_min_priority) {
if (r < 0)
return r;
- r = fcntl(fd, F_DUPFD_CLOEXEC, 3);
- if (r < 0)
- return -errno;
-
- return r;
+ return RET_NERRNO(fcntl(fd, F_DUPFD_CLOEXEC, 3));
}
static int print_inhibitors(sd_bus *bus) {
#include "alloc-util.h"
#include "env-file.h"
#include "errno-list.h"
+#include "errno-util.h"
#include "escape.h"
#include "fd-util.h"
#include "fileio.h"
}
/* Open writing side */
- r = open(i->fifo_path, O_WRONLY|O_CLOEXEC|O_NONBLOCK);
- if (r < 0)
- return -errno;
-
- return r;
+ return RET_NERRNO(open(i->fifo_path, O_WRONLY|O_CLOEXEC|O_NONBLOCK));
}
static void inhibitor_remove_fifo(Inhibitor *i) {
static int sd_drmsetmaster(int fd) {
assert(fd >= 0);
-
- if (ioctl(fd, DRM_IOCTL_SET_MASTER, 0) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(ioctl(fd, DRM_IOCTL_SET_MASTER, 0));
}
static int sd_drmdropmaster(int fd) {
assert(fd >= 0);
-
- if (ioctl(fd, DRM_IOCTL_DROP_MASTER, 0) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(ioctl(fd, DRM_IOCTL_DROP_MASTER, 0));
}
static int session_device_open(SessionDevice *sd, bool active) {
}
/* Open writing side */
- r = open(s->fifo_path, O_WRONLY|O_CLOEXEC|O_NONBLOCK);
- if (r < 0)
- return -errno;
-
- return r;
+ return RET_NERRNO(open(s->fifo_path, O_WRONLY|O_CLOEXEC|O_NONBLOCK));
}
static void session_remove_fifo(Session *s) {
if (!m->unit)
return -ESRCH;
- if (who == KILL_LEADER) {
- /* If we shall simply kill the leader, do so directly */
-
- if (kill(m->leader, signo) < 0)
- return -errno;
-
- return 0;
- }
+ if (who == KILL_LEADER) /* If we shall simply kill the leader, do so directly */
+ return RET_NERRNO(kill(m->leader, signo));
/* Otherwise, make PID 1 do it for us, for the entire cgroup */
return manager_kill_unit(m->manager, m->unit, signo, NULL);
return -EOVERFLOW;
}
- if (lchown(p, uid, gid) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(lchown(p, uid, gid));
}
int userns_mkdir(const char *root, const char *path, mode_t mode, uid_t uid, gid_t gid) {
#include "efi-loader.h"
#include "env-file.h"
#include "env-util.h"
+#include "errno-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "parse-util.h"
if (!parent)
return log_oom();
- r = stat(parent, &st2) < 0 ? -errno : 0;
+ r = RET_NERRNO(stat(parent, &st2));
}
if (r < 0)
#include "alloc-util.h"
#include "bpf-program.h"
+#include "errno-util.h"
#include "escape.h"
#include "fd-util.h"
#include "memory-util.h"
attr.info.info_len = info_len;
attr.info.info = PTR_TO_UINT64(info);
- if (bpf(BPF_OBJ_GET_INFO_BY_FD, &attr, sizeof(attr)) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(bpf(BPF_OBJ_GET_INFO_BY_FD, &attr, sizeof(attr)));
}
int bpf_program_new(uint32_t prog_type, BPFProgram **ret) {
int bpf_map_new(enum bpf_map_type type, size_t key_size, size_t value_size, size_t max_entries, uint32_t flags) {
union bpf_attr attr;
- int fd;
zero(attr);
attr.map_type = type;
attr.max_entries = max_entries;
attr.map_flags = flags;
- fd = bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
- if (fd < 0)
- return -errno;
-
- return fd;
+ return RET_NERRNO(bpf(BPF_MAP_CREATE, &attr, sizeof(attr)));
}
int bpf_map_update_element(int fd, const void *key, void *value) {
attr.key = PTR_TO_UINT64(key);
attr.value = PTR_TO_UINT64(value);
- if (bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr)) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr)));
}
int bpf_map_lookup_element(int fd, const void *key, void *value) {
attr.key = PTR_TO_UINT64(key);
attr.value = PTR_TO_UINT64(value);
- if (bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr)) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr)));
}
int bpf_program_pin(int prog_fd, const char *bpffs_path) {
attr.pathname = PTR_TO_UINT64((void *) bpffs_path);
attr.bpf_fd = prog_fd;
- if (bpf(BPF_OBJ_PIN, &attr, sizeof(attr)) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(bpf(BPF_OBJ_PIN, &attr, sizeof(attr)));
}
int bpf_program_get_id_by_fd(int prog_fd, uint32_t *ret_id) {
strncpy(args.name, subvolume, sizeof(args.name)-1);
- if (ioctl(fd, BTRFS_IOC_SUBVOL_CREATE, &args) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(ioctl(fd, BTRFS_IOC_SUBVOL_CREATE, &args));
}
int btrfs_subvol_make(const char *path) {
if (flags == nflags)
return 0;
- if (ioctl(fd, BTRFS_IOC_SUBVOL_SETFLAGS, &nflags) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(ioctl(fd, BTRFS_IOC_SUBVOL_SETFLAGS, &nflags));
}
int btrfs_subvol_set_read_only(const char *path, bool b) {
if (r < 0)
return r;
- if (ioctl(outfd, BTRFS_IOC_CLONE, infd) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(ioctl(outfd, BTRFS_IOC_CLONE, infd));
}
int btrfs_clone_range(int infd, uint64_t in_offset, int outfd, uint64_t out_offset, uint64_t sz) {
if (r < 0)
return r;
- if (ioctl(outfd, BTRFS_IOC_CLONE_RANGE, &args) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(ioctl(outfd, BTRFS_IOC_CLONE_RANGE, &args));
}
int btrfs_get_block_device_fd(int fd, dev_t *dev) {
if (!r)
return -ENOTTY;
- if (ioctl(fd, BTRFS_IOC_QUOTA_CTL, &args) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(ioctl(fd, BTRFS_IOC_QUOTA_CTL, &args));
}
int btrfs_quota_enable(const char *path, bool b) {
assert(fd >= 0);
- if (ioctl(fd, BTRFS_IOC_QUOTA_RESCAN, &args) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(ioctl(fd, BTRFS_IOC_QUOTA_RESCAN, &args));
}
int btrfs_quota_scan_wait(int fd) {
assert(fd >= 0);
- if (ioctl(fd, BTRFS_IOC_QUOTA_RESCAN_WAIT) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(ioctl(fd, BTRFS_IOC_QUOTA_RESCAN_WAIT));
}
int btrfs_quota_scan_ongoing(int fd) {
if (fd < 0)
return -errno;
- if (ioctl(fd, RTC_SET_TIME, tm) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(ioctl(fd, RTC_SET_TIME, tm));
}
int clock_is_localtime(const char* adjtime_path) {
/* The very first call to settimeofday() does time warp magic. Do a dummy call here, so the time
* warping is sealed and all later calls behave as expected. */
- if (settimeofday(NULL, &tz) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(settimeofday(NULL, &tz));
}
#define EPOCH_FILE "/usr/lib/clock-epoch"
if (fstat(fdf, &st) < 0)
return -errno;
- if (fchmod(fdt, st.st_mode & 07777) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(fchmod(fdt, st.st_mode & 07777));
}
int copy_rights_with_fallback(int fdf, int fdt, const char *patht) {
* It sucks a bit that depending on the situation we return very different objects here, but that's Linux I
* figure. */
- if (size == 0 && ((flags & ACQUIRE_NO_DEV_NULL) == 0)) {
+ if (size == 0 && ((flags & ACQUIRE_NO_DEV_NULL) == 0))
/* As a special case, return /dev/null if we have been called for an empty data block */
- r = open("/dev/null", O_RDONLY|O_CLOEXEC|O_NOCTTY);
- if (r < 0)
- return -errno;
-
- return r;
- }
+ return RET_NERRNO(open("/dev/null", O_RDONLY|O_CLOEXEC|O_NOCTTY));
if ((flags & ACQUIRE_NO_MEMFD) == 0) {
fd = memfd_new("data-fd");
return 0;
}
- r = 0;
ecmd.cmd = ETHTOOL_SWOL;
- if (ioctl(*ethtool_fd, SIOCETHTOOL, &ifr) < 0)
- r = -errno;
+ r = RET_NERRNO(ioctl(*ethtool_fd, SIOCETHTOOL, &ifr));
explicit_bzero_safe(&ecmd, sizeof(ecmd));
return r;
return 0;
ecmd.cmd = ETHTOOL_SRINGPARAM;
- if (ioctl(*ethtool_fd, SIOCETHTOOL, &ifr) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(ioctl(*ethtool_fd, SIOCETHTOOL, &ifr));
}
static int get_stringset(int ethtool_fd, const char *ifname, enum ethtool_stringset stringset_id, struct ethtool_gstrings **ret) {
ifr->ifr_data = (void *) &ecmd;
- if (ioctl(fd, SIOCETHTOOL, ifr) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(ioctl(fd, SIOCETHTOOL, ifr));
}
static int set_sset(int fd, struct ifreq *ifr, const struct ethtool_link_usettings *u) {
ifr->ifr_data = (void *) &ecmd;
- if (ioctl(fd, SIOCETHTOOL, ifr) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(ioctl(fd, SIOCETHTOOL, ifr));
}
int ethtool_set_glinksettings(
return 0;
ecmd.cmd = ETHTOOL_SCHANNELS;
- if (ioctl(*fd, SIOCETHTOOL, &ifr) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(ioctl(*fd, SIOCETHTOOL, &ifr));
}
int ethtool_set_flow_control(int *fd, const char *ifname, int rx, int tx, int autoneg) {
return 0;
ecmd.cmd = ETHTOOL_SPAUSEPARAM;
- if (ioctl(*fd, SIOCETHTOOL, &ifr) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(ioctl(*fd, SIOCETHTOOL, &ifr));
}
int ethtool_set_nic_coalesce_settings(int *ethtool_fd, const char *ifname, const netdev_coalesce_param *coalesce) {
return 0;
ecmd.cmd = ETHTOOL_SCOALESCE;
- if (ioctl(*ethtool_fd, SIOCETHTOOL, &ifr) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(ioctl(*ethtool_fd, SIOCETHTOOL, &ifr));
}
int config_parse_advertise(
if (r < 0)
return r;
- if (symlink(old_path, new_path) < 0)
- r = -errno;
-
+ r = RET_NERRNO(symlink(old_path, new_path));
mac_selinux_create_file_clear();
if (r < 0)
if (r < 0)
return r;
- if (symlink_atomic(from, to) < 0)
- r = -errno;
-
+ r = symlink_atomic(from, to);
mac_selinux_create_file_clear();
if (r < 0)
if (r < 0)
return r;
- if (mknod(pathname, mode, dev) < 0)
- r = -errno;
-
+ r = RET_NERRNO(mknod(pathname, mode, dev));
mac_selinux_create_file_clear();
if (r < 0)
.datalen = sizeof(bp),
};
- if (ioctl(whole_fd, BLKPG, &ba) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(ioctl(whole_fd, BLKPG, &ba));
}
int loop_device_refresh_size(LoopDevice *d, uint64_t offset, uint64_t size) {
if (offset != UINT64_MAX)
info.lo_offset = offset;
- if (ioctl(d->fd, LOOP_SET_STATUS64, &info) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(ioctl(d->fd, LOOP_SET_STATUS64, &info));
}
int loop_device_flock(LoopDevice *d, int operation) {
if (d->fd < 0)
return -EBADF;
- if (flock(d->fd, operation) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(flock(d->fd, operation));
}
int loop_device_sync(LoopDevice *d) {
if (d->fd < 0)
return -EBADF;
- if (fsync(d->fd) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(fsync(d->fd));
}
strna(p->options));
if (FLAGS_SET(p->mode, MNT_FOLLOW_SYMLINK))
- r = mount(p->what, p->where, p->type, p->flags, p->options) < 0 ? -errno : 0;
+ r = RET_NERRNO(mount(p->what, p->where, p->type, p->flags, p->options));
else
r = mount_nofollow(p->what, p->where, p->type, p->flags, p->options);
if (r < 0) {
if (chroot(".") < 0)
return -errno;
- if (chdir("/") < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(chdir("/"));
}
int repeat_unmount(const char *path, int flags) {
strna(what), strna(type), where, strnull(fl), strempty(o));
if (follow_symlink)
- r = mount(what, where, type, f, o) < 0 ? -errno : 0;
+ r = RET_NERRNO(mount(what, where, type, f, o));
else
r = mount_nofollow(what, where, type, f, o);
if (r < 0)
if (sclass == 0)
return -ENOSYS;
- r = security_compute_create_raw(mycon, fcon, sclass, label);
- if (r < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(security_compute_create_raw(mycon, fcon, sclass, label));
#else
return -EOPNOTSUPP;
#endif
int mac_selinux_get_our_label(char **label) {
#if HAVE_SELINUX
- int r;
-
assert(label);
if (!mac_selinux_use())
return -EOPNOTSUPP;
- r = getcon_raw(label);
- if (r < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(getcon_raw(label));
#else
return -EOPNOTSUPP;
#endif
if (sclass == 0)
return -ENOSYS;
- r = security_compute_create_raw(mycon, fcon, sclass, label);
- if (r < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(security_compute_create_raw(mycon, fcon, sclass, label));
#else
return -EOPNOTSUPP;
#endif
context_changed = true;
}
- r = bind(fd, addr, addrlen) < 0 ? -errno : 0;
+ r = RET_NERRNO(bind(fd, addr, addrlen));
if (context_changed)
(void) setfscreatecon_raw(NULL);
skipped:
#endif
- if (bind(fd, addr, addrlen) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(bind(fd, addr, addrlen));
}
return r;
}
- fd = socket(socket_address_family(a), a->type | flags, a->protocol);
- r = fd < 0 ? -errno : 0;
+ fd = RET_NERRNO(socket(socket_address_family(a), a->type | flags, a->protocol));
if (label)
mac_selinux_create_socket_clear();
- if (r < 0)
- return r;
+ if (fd < 0)
+ return fd;
if (socket_address_family(a) == AF_INET6 && only != SOCKET_ADDRESS_DEFAULT) {
r = setsockopt_int(fd, IPPROTO_IPV6, IPV6_V6ONLY, only == SOCKET_ADDRESS_IPV6_ONLY);
if (r < 0)
log_debug_errno(r, "Failed to sync DM block device %s, ignoring: %m", m->path);
- if (ioctl(fd, DM_DEV_REMOVE, &(struct dm_ioctl) {
+ return RET_NERRNO(ioctl(fd, DM_DEV_REMOVE, &(struct dm_ioctl) {
.version = {
DM_VERSION_MAJOR,
DM_VERSION_MINOR,
},
.data_size = sizeof(struct dm_ioctl),
.dev = m->devnum,
- }) < 0)
- return -errno;
-
- return 0;
+ }));
}
static int delete_md(MountPoint *m) {
if (fsync(fd) < 0)
log_debug_errno(errno, "Failed to sync MD block device %s, ignoring: %m", m->path);
- if (ioctl(fd, STOP_ARRAY, NULL) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(ioctl(fd, STOP_ARRAY, NULL));
}
static bool nonunmountable_path(const char *path) {
const char *fix;
fix = prefix_roota(arg_root, p);
- if (stat(fix, st) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(stat(fix, st));
}
static int read_id_from_file(Item *i, uid_t *_uid, gid_t *_gid) {
/* rm_if_wrong_type_safe already logs errors. */
return r;
- next_fd = openat(parent_fd, t, O_NOCTTY | O_CLOEXEC | O_DIRECTORY);
+ next_fd = RET_NERRNO(openat(parent_fd, t, O_NOCTTY | O_CLOEXEC | O_DIRECTORY));
if (next_fd < 0) {
_cleanup_free_ char *parent_name = NULL;
- r = -errno;
(void) fd_get_path(parent_fd, &parent_name);
- return log_error_errno(r, "Failed to open \"%s\" at \"%s\": %m", t, strnull(parent_name));
+ return log_error_errno(next_fd, "Failed to open \"%s\" at \"%s\": %m", t, strnull(parent_name));
}
- if (fstat(next_fd, &parent_st) < 0) {
+ r = RET_NERRNO(fstat(next_fd, &parent_st));
+ if (r < 0) {
_cleanup_free_ char *parent_name = NULL;
- r = -errno;
(void) fd_get_path(parent_fd, &parent_name);
return log_error_errno(r, "Failed to stat \"%s\" at \"%s\": %m", t, strnull(parent_name));
}
return log_error_errno(r, "rm -fr %s failed: %m", i->path);
mac_selinux_create_file_prepare(i->path, S_IFLNK);
- r = symlink(i->argument, i->path) < 0 ? -errno : 0;
+ r = RET_NERRNO(symlink(i->argument, i->path));
mac_selinux_create_file_clear();
}
if (r < 0)
return r;
mac_selinux_create_file_prepare(slink, S_IFLNK);
- if (symlink(target, slink) < 0)
- r = -errno;
- else
- r = 0;
+ r = RET_NERRNO(symlink(target, slink));
mac_selinux_create_file_clear();
if (r != -ENOENT)
return r;
}
(void) rename_process("systemd-userwork: waiting...");
-
- fd = accept4(listen_fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
- if (fd < 0)
- fd = -errno;
-
+ fd = RET_NERRNO(accept4(listen_fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC));
(void) rename_process("systemd-userwork: processing...");
if (fd == -EAGAIN)
#include "alloc-util.h"
#include "env-file.h"
+#include "errno-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "io-util.h"
TIOCL_GETFGCONSOLE,
};
- int r;
-
- r = ioctl(fd, TIOCLINUX, data);
- if (r < 0)
- return -errno;
-
- return r;
+ return RET_NERRNO(ioctl(fd, TIOCLINUX, data));
}
static int verify_vc_allocation(unsigned idx) {
xsprintf(vcname, "/dev/vcs%u", idx);
- if (access(vcname, F_OK) < 0)
- return -errno;
-
- return 0;
+ return RET_NERRNO(access(vcname, F_OK));
}
static int verify_vc_allocation_byfd(int fd) {