diff --git a/src/basic/cgroup-util.c b/src/basic/cgroup-util.c index 9b145aa70f..446f63023b 100644 --- a/src/basic/cgroup-util.c +++ b/src/basic/cgroup-util.c @@ -81,7 +81,7 @@ int cg_read_pid(FILE *f, pid_t *_pid) { if (feof(f)) return 0; - return errno > 0 ? -errno : -EIO; + return errno_or_else(EIO); } if (ul <= 0) @@ -770,10 +770,8 @@ int cg_trim(const char *controller, const char *path, bool delete_root) { if (nftw(fs, trim_cb, 64, FTW_DEPTH|FTW_MOUNT|FTW_PHYS) != 0) { if (errno == ENOENT) r = 0; - else if (errno > 0) - r = -errno; else - r = -EIO; + r = errno_or_else(EIO); } if (delete_root) { @@ -2502,8 +2500,8 @@ int cg_kernel_controllers(Set **ret) { if (feof(f)) break; - if (ferror(f) && errno > 0) - return -errno; + if (ferror(f)) + return errno_or_else(EIO); return -EBADMSG; } diff --git a/src/basic/fileio.c b/src/basic/fileio.c index 40a20c2b3b..bd4c964bec 100644 --- a/src/basic/fileio.c +++ b/src/basic/fileio.c @@ -298,7 +298,7 @@ int verify_file(const char *fn, const char *blob, bool accept_extra_nl) { errno = 0; k = fread(buf, 1, l + accept_extra_nl + 1, f); if (ferror(f)) - return errno > 0 ? -errno : -EIO; + return errno_or_else(EIO); if (k != l && k != l + accept_extra_nl) return 0; @@ -382,7 +382,7 @@ int read_full_stream_full( l += k; if (ferror(f)) { - r = errno > 0 ? -errno : -EIO; + r = errno_or_else(EIO); goto finalize; } @@ -661,7 +661,7 @@ int fflush_and_check(FILE *f) { fflush(f); if (ferror(f)) - return errno > 0 ? -errno : -EIO; + return errno_or_else(EIO); return 0; } @@ -888,7 +888,7 @@ int safe_fgetc(FILE *f, char *ret) { k = fgetc(f); if (k == EOF) { if (ferror(f)) - return errno > 0 ? -errno : -EIO; + return errno_or_else(EIO); if (ret) *ret = 0; diff --git a/src/basic/glob-util.c b/src/basic/glob-util.c index 32c53f8c73..b335af8d97 100644 --- a/src/basic/glob-util.c +++ b/src/basic/glob-util.c @@ -8,6 +8,7 @@ #include #include "dirent-util.h" +#include "errno-util.h" #include "glob-util.h" #include "macro.h" #include "path-util.h" @@ -36,13 +37,12 @@ int safe_glob(const char *path, int flags, glob_t *pglob) { errno = 0; k = glob(path, flags | GLOB_ALTDIRFUNC, NULL, pglob); - if (k == GLOB_NOMATCH) return -ENOENT; if (k == GLOB_NOSPACE) return -ENOMEM; if (k != 0) - return errno > 0 ? -errno : -EIO; + return errno_or_else(EIO); if (strv_isempty(pglob->gl_pathv)) return -ENOENT; diff --git a/src/basic/in-addr-util.c b/src/basic/in-addr-util.c index c1fab51b5e..7a5384bab3 100644 --- a/src/basic/in-addr-util.c +++ b/src/basic/in-addr-util.c @@ -9,6 +9,7 @@ #include #include "alloc-util.h" +#include "errno-util.h" #include "in-addr-util.h" #include "macro.h" #include "parse-util.h" @@ -315,7 +316,7 @@ int in_addr_to_string(int family, const union in_addr_union *u, char **ret) { errno = 0; if (!inet_ntop(family, u, x, l)) - return errno > 0 ? -errno : -EINVAL; + return errno_or_else(EINVAL); *ret = TAKE_PTR(x); return 0; @@ -345,7 +346,7 @@ int in_addr_prefix_to_string(int family, const union in_addr_union *u, unsigned errno = 0; if (!inet_ntop(family, u, x, l)) - return errno > 0 ? -errno : -EINVAL; + return errno_or_else(EINVAL); p = x + strlen(x); l -= strlen(x); @@ -384,7 +385,7 @@ int in_addr_ifindex_to_string(int family, const union in_addr_union *u, int ifin errno = 0; if (!inet_ntop(family, u, x, l)) - return errno > 0 ? -errno : -EINVAL; + return errno_or_else(EINVAL); sprintf(strchr(x, 0), "%%%i", ifindex); @@ -404,7 +405,7 @@ int in_addr_from_string(int family, const char *s, union in_addr_union *ret) { errno = 0; if (inet_pton(family, s, ret ?: &buffer) <= 0) - return errno > 0 ? -errno : -EINVAL; + return errno_or_else(EINVAL); return 0; } diff --git a/src/basic/socket-util.c b/src/basic/socket-util.c index e1636bca6e..82bda64801 100644 --- a/src/basic/socket-util.c +++ b/src/basic/socket-util.c @@ -78,7 +78,7 @@ int socket_address_parse(SocketAddress *a, const char *s) { errno = 0; if (inet_pton(AF_INET6, n, &a->sockaddr.in6.sin6_addr) <= 0) - return errno > 0 ? -errno : -EINVAL; + return errno_or_else(EINVAL); e++; if (*e != ':') diff --git a/src/basic/user-util.c b/src/basic/user-util.c index 1dd8e11e9e..b8eb894f40 100644 --- a/src/basic/user-util.c +++ b/src/basic/user-util.c @@ -15,6 +15,7 @@ #include #include "alloc-util.h" +#include "errno-util.h" #include "fd-util.h" #include "fileio.h" #include "format-util.h" @@ -213,7 +214,7 @@ int get_user_creds( p = getpwnam(*username); } if (!p) { - r = errno > 0 ? -errno : -ESRCH; + r = errno_or_else(ESRCH); /* If the user requested that we only synthesize as fallback, do so now */ if (FLAGS_SET(flags, USER_CREDS_PREFER_NSS)) { @@ -307,7 +308,7 @@ int get_group_creds(const char **groupname, gid_t *gid, UserCredsFlags flags) { } if (!g) - return errno > 0 ? -errno : -ESRCH; + return errno_or_else(ESRCH); if (gid) { if (!gid_is_valid(g->gr_gid)) @@ -492,7 +493,7 @@ int get_home_dir(char **_h) { errno = 0; p = getpwuid(u); if (!p) - return errno > 0 ? -errno : -ESRCH; + return errno_or_else(ESRCH); if (!path_is_valid(p->pw_dir) || !path_is_absolute(p->pw_dir)) @@ -549,7 +550,7 @@ int get_shell(char **_s) { errno = 0; p = getpwuid(u); if (!p) - return errno > 0 ? -errno : -ESRCH; + return errno_or_else(ESRCH); if (!path_is_valid(p->pw_shell) || !path_is_absolute(p->pw_shell)) @@ -770,7 +771,7 @@ int putpwent_sane(const struct passwd *pw, FILE *stream) { errno = 0; if (putpwent(pw, stream) != 0) - return errno > 0 ? -errno : -EIO; + return errno_or_else(EIO); return 0; } @@ -781,7 +782,7 @@ int putspent_sane(const struct spwd *sp, FILE *stream) { errno = 0; if (putspent(sp, stream) != 0) - return errno > 0 ? -errno : -EIO; + return errno_or_else(EIO); return 0; } @@ -792,7 +793,7 @@ int putgrent_sane(const struct group *gr, FILE *stream) { errno = 0; if (putgrent(gr, stream) != 0) - return errno > 0 ? -errno : -EIO; + return errno_or_else(EIO); return 0; } @@ -804,7 +805,7 @@ int putsgent_sane(const struct sgrp *sg, FILE *stream) { errno = 0; if (putsgent(sg, stream) != 0) - return errno > 0 ? -errno : -EIO; + return errno_or_else(EIO); return 0; } @@ -819,7 +820,7 @@ int fgetpwent_sane(FILE *stream, struct passwd **pw) { errno = 0; p = fgetpwent(stream); if (!p && errno != ENOENT) - return errno > 0 ? -errno : -EIO; + return errno_or_else(EIO); *pw = p; return !!p; @@ -834,7 +835,7 @@ int fgetspent_sane(FILE *stream, struct spwd **sp) { errno = 0; s = fgetspent(stream); if (!s && errno != ENOENT) - return errno > 0 ? -errno : -EIO; + return errno_or_else(EIO); *sp = s; return !!s; @@ -849,7 +850,7 @@ int fgetgrent_sane(FILE *stream, struct group **gr) { errno = 0; g = fgetgrent(stream); if (!g && errno != ENOENT) - return errno > 0 ? -errno : -EIO; + return errno_or_else(EIO); *gr = g; return !!g; @@ -865,7 +866,7 @@ int fgetsgent_sane(FILE *stream, struct sgrp **sg) { errno = 0; s = fgetsgent(stream); if (!s && errno != ENOENT) - return errno > 0 ? -errno : -EIO; + return errno_or_else(EIO); *sg = s; return !!s; diff --git a/src/core/dbus-cgroup.c b/src/core/dbus-cgroup.c index f70e6c87ee..2f2313c599 100644 --- a/src/core/dbus-cgroup.c +++ b/src/core/dbus-cgroup.c @@ -10,6 +10,7 @@ #include "cgroup.h" #include "dbus-cgroup.h" #include "dbus-util.h" +#include "errno-util.h" #include "fd-util.h" #include "fileio.h" #include "limits-util.h" @@ -1492,7 +1493,7 @@ int bus_cgroup_set_property( errno = 0; if (!inet_ntop(item->family, &item->address, buffer, sizeof(buffer))) - return errno > 0 ? -errno : -EINVAL; + return errno_or_else(EINVAL); fprintf(f, "%s=%s/%u\n", name, buffer, item->prefixlen); } diff --git a/src/core/dynamic-user.c b/src/core/dynamic-user.c index c380bee9e5..fb0a794348 100644 --- a/src/core/dynamic-user.c +++ b/src/core/dynamic-user.c @@ -494,7 +494,7 @@ static int dynamic_user_realize( errno = 0; p = getpwuid(num); if (!p) - return errno > 0 ? -errno : -ESRCH; + return errno_or_else(ESRCH); gid = p->pw_gid; } diff --git a/src/core/execute.c b/src/core/execute.c index 36a6641421..d8137090ed 100644 --- a/src/core/execute.c +++ b/src/core/execute.c @@ -999,12 +999,8 @@ static int get_supplementary_groups(const ExecContext *c, const char *user, */ errno = 0; ngroups_max = (int) sysconf(_SC_NGROUPS_MAX); - if (ngroups_max <= 0) { - if (errno > 0) - return -errno; - else - return -EOPNOTSUPP; /* For all other values */ - } + if (ngroups_max <= 0) + return errno_or_else(EOPNOTSUPP); l_gids = new(gid_t, ngroups_max); if (!l_gids) diff --git a/src/getty-generator/getty-generator.c b/src/getty-generator/getty-generator.c index 8610ab6705..4563daf3ad 100644 --- a/src/getty-generator/getty-generator.c +++ b/src/getty-generator/getty-generator.c @@ -6,6 +6,7 @@ #include #include "alloc-util.h" +#include "errno-util.h" #include "fd-util.h" #include "fileio.h" #include "generator.h" @@ -94,7 +95,7 @@ static int verify_tty(const char *name) { errno = 0; if (isatty(fd) <= 0) - return errno > 0 ? -errno : -EIO; + return errno_or_else(EIO); return 0; } diff --git a/src/login/logind-core.c b/src/login/logind-core.c index dacd3b3d9c..f631ba7219 100644 --- a/src/login/logind-core.c +++ b/src/login/logind-core.c @@ -17,6 +17,7 @@ #include "cgroup-util.h" #include "conf-parser.h" #include "device-util.h" +#include "errno-util.h" #include "fd-util.h" #include "limits-util.h" #include "logind.h" @@ -191,7 +192,7 @@ int manager_add_user_by_uid(Manager *m, uid_t uid, User **_user) { errno = 0; p = getpwuid(uid); if (!p) - return errno > 0 ? -errno : -ENOENT; + return errno_or_else(ENOENT); return manager_add_user(m, uid, p->pw_gid, p->pw_name, p->pw_dir, _user); } diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c index 0e8925ab94..1f5bdfecfc 100644 --- a/src/login/logind-dbus.c +++ b/src/login/logind-dbus.c @@ -1242,7 +1242,7 @@ static int method_set_user_linger(sd_bus_message *message, void *userdata, sd_bu errno = 0; pw = getpwuid(uid); if (!pw) - return errno > 0 ? -errno : -ENOENT; + return errno_or_else(ENOENT); r = bus_verify_polkit_async( message, diff --git a/src/machine/machine.c b/src/machine/machine.c index ef8ccd98fb..b203a1dde8 100644 --- a/src/machine/machine.c +++ b/src/machine/machine.c @@ -11,6 +11,7 @@ #include "bus-error.h" #include "bus-util.h" #include "env-file.h" +#include "errno-util.h" #include "escape.h" #include "extract-word.h" #include "fd-util.h" @@ -620,7 +621,7 @@ int machine_get_uid_shift(Machine *m, uid_t *ret) { k = fscanf(f, UID_FMT " " UID_FMT " " UID_FMT "\n", &uid_base, &uid_shift, &uid_range); if (k != 3) { if (ferror(f)) - return -errno; + return errno_or_else(EIO); return -EBADMSG; } @@ -651,7 +652,7 @@ int machine_get_uid_shift(Machine *m, uid_t *ret) { k = fscanf(f, GID_FMT " " GID_FMT " " GID_FMT "\n", &gid_base, &gid_shift, &gid_range); if (k != 3) { if (ferror(f)) - return -errno; + return errno_or_else(EIO); return -EBADMSG; } diff --git a/src/machine/machined-dbus.c b/src/machine/machined-dbus.c index fea9cc2633..8031dafe15 100644 --- a/src/machine/machined-dbus.c +++ b/src/machine/machined-dbus.c @@ -11,6 +11,7 @@ #include "bus-common-errors.h" #include "bus-util.h" #include "cgroup-util.h" +#include "errno-util.h" #include "fd-util.h" #include "fileio.h" #include "format-util.h" @@ -625,7 +626,7 @@ static int clean_pool_done(Operation *operation, int ret, sd_bus_error *error) { errno = 0; n = fread(&success, 1, sizeof(success), f); if (n != sizeof(success)) - return ret < 0 ? ret : (errno != 0 ? -errno : -EIO); + return ret < 0 ? ret : errno_or_else(EIO); if (ret < 0) { _cleanup_free_ char *name = NULL; @@ -669,7 +670,7 @@ static int clean_pool_done(Operation *operation, int ret, sd_bus_error *error) { errno = 0; n = fread(&size, 1, sizeof(size), f); if (n != sizeof(size)) - return errno != 0 ? -errno : -EIO; + return errno_or_else(EIO); r = sd_bus_message_append(reply, "(st)", name, size); if (r < 0) @@ -914,8 +915,8 @@ static int method_map_from_machine_user(sd_bus_message *message, void *userdata, if (k < 0 && feof(f)) break; if (k != 3) { - if (ferror(f) && errno > 0) - return -errno; + if (ferror(f)) + return errno_or_else(EIO); return -EIO; } @@ -972,8 +973,8 @@ static int method_map_to_machine_user(sd_bus_message *message, void *userdata, s if (k < 0 && feof(f)) break; if (k != 3) { - if (ferror(f) && errno > 0) - return -errno; + if (ferror(f)) + return errno_or_else(EIO); return -EIO; } @@ -1036,8 +1037,8 @@ static int method_map_from_machine_group(sd_bus_message *message, void *groupdat if (k < 0 && feof(f)) break; if (k != 3) { - if (ferror(f) && errno > 0) - return -errno; + if (ferror(f)) + return errno_or_else(EIO); return -EIO; } @@ -1094,8 +1095,8 @@ static int method_map_to_machine_group(sd_bus_message *message, void *groupdata, if (k < 0 && feof(f)) break; if (k != 3) { - if (ferror(f) && errno > 0) - return -errno; + if (ferror(f)) + return errno_or_else(EIO); return -EIO; } diff --git a/src/shared/dissect-image.c b/src/shared/dissect-image.c index 030e12e4ee..a54714169e 100644 --- a/src/shared/dissect-image.c +++ b/src/shared/dissect-image.c @@ -60,7 +60,7 @@ int probe_filesystem(const char *node, char **ret_fstype) { errno = 0; b = blkid_new_probe_from_filename(node); if (!b) - return -errno ?: -ENOMEM; + return errno_or_else(ENOMEM); blkid_probe_enable_superblocks(b, 1); blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE); @@ -76,7 +76,7 @@ int probe_filesystem(const char *node, char **ret_fstype) { return -EUCLEAN; } if (r != 0) - return -errno ?: -EIO; + return errno_or_else(EIO); (void) blkid_probe_lookup_value(b, "TYPE", &fstype, NULL); @@ -333,7 +333,7 @@ int dissect_image( errno = 0; r = blkid_probe_set_device(b, fd, 0, 0); if (r != 0) - return -errno ?: -ENOMEM; + return errno_or_else(ENOMEM); if ((flags & DISSECT_IMAGE_GPT_ONLY) == 0) { /* Look for file system superblocks, unless we only shall look for GPT partition tables */ @@ -349,7 +349,7 @@ int dissect_image( if (IN_SET(r, -2, 1)) return log_debug_errno(SYNTHETIC_ERRNO(ENOPKG), "Failed to identify any partition table."); if (r != 0) - return -errno ?: -EIO; + return errno_or_else(EIO); m = new0(DissectedImage, 1); if (!m) @@ -415,7 +415,7 @@ int dissect_image( errno = 0; pl = blkid_probe_get_partitions(b); if (!pl) - return -errno ?: -ENOMEM; + return errno_or_else(ENOMEM); r = loop_wait_for_partitions_to_appear(fd, d, blkid_partlist_numof_partitions(pl), flags, &e); if (r < 0) diff --git a/src/udev/udev-builtin-blkid.c b/src/udev/udev-builtin-blkid.c index efbb0732f1..7ef75e6f91 100644 --- a/src/udev/udev-builtin-blkid.c +++ b/src/udev/udev-builtin-blkid.c @@ -20,6 +20,7 @@ #include "blkid-util.h" #include "device-util.h" #include "efivars.h" +#include "errno-util.h" #include "fd-util.h" #include "gpt.h" #include "parse-util.h" @@ -113,7 +114,7 @@ static int find_gpt_root(sd_device *dev, blkid_probe pr, bool test) { errno = 0; pl = blkid_probe_get_partitions(pr); if (!pl) - return -errno ?: -ENOMEM; + return errno_or_else(ENOMEM); nvals = blkid_partlist_numof_partitions(pl); for (i = 0; i < nvals; i++) {