tree-wide: make use of errno_or_else() everywhere

This commit is contained in:
Lennart Poettering 2019-07-11 15:42:14 +02:00
parent fed813778f
commit 66855de739
16 changed files with 60 additions and 58 deletions

View file

@ -81,7 +81,7 @@ int cg_read_pid(FILE *f, pid_t *_pid) {
if (feof(f)) if (feof(f))
return 0; return 0;
return errno > 0 ? -errno : -EIO; return errno_or_else(EIO);
} }
if (ul <= 0) 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 (nftw(fs, trim_cb, 64, FTW_DEPTH|FTW_MOUNT|FTW_PHYS) != 0) {
if (errno == ENOENT) if (errno == ENOENT)
r = 0; r = 0;
else if (errno > 0)
r = -errno;
else else
r = -EIO; r = errno_or_else(EIO);
} }
if (delete_root) { if (delete_root) {
@ -2502,8 +2500,8 @@ int cg_kernel_controllers(Set **ret) {
if (feof(f)) if (feof(f))
break; break;
if (ferror(f) && errno > 0) if (ferror(f))
return -errno; return errno_or_else(EIO);
return -EBADMSG; return -EBADMSG;
} }

View file

@ -298,7 +298,7 @@ int verify_file(const char *fn, const char *blob, bool accept_extra_nl) {
errno = 0; errno = 0;
k = fread(buf, 1, l + accept_extra_nl + 1, f); k = fread(buf, 1, l + accept_extra_nl + 1, f);
if (ferror(f)) if (ferror(f))
return errno > 0 ? -errno : -EIO; return errno_or_else(EIO);
if (k != l && k != l + accept_extra_nl) if (k != l && k != l + accept_extra_nl)
return 0; return 0;
@ -382,7 +382,7 @@ int read_full_stream_full(
l += k; l += k;
if (ferror(f)) { if (ferror(f)) {
r = errno > 0 ? -errno : -EIO; r = errno_or_else(EIO);
goto finalize; goto finalize;
} }
@ -661,7 +661,7 @@ int fflush_and_check(FILE *f) {
fflush(f); fflush(f);
if (ferror(f)) if (ferror(f))
return errno > 0 ? -errno : -EIO; return errno_or_else(EIO);
return 0; return 0;
} }
@ -888,7 +888,7 @@ int safe_fgetc(FILE *f, char *ret) {
k = fgetc(f); k = fgetc(f);
if (k == EOF) { if (k == EOF) {
if (ferror(f)) if (ferror(f))
return errno > 0 ? -errno : -EIO; return errno_or_else(EIO);
if (ret) if (ret)
*ret = 0; *ret = 0;

View file

@ -8,6 +8,7 @@
#include <unistd.h> #include <unistd.h>
#include "dirent-util.h" #include "dirent-util.h"
#include "errno-util.h"
#include "glob-util.h" #include "glob-util.h"
#include "macro.h" #include "macro.h"
#include "path-util.h" #include "path-util.h"
@ -36,13 +37,12 @@ int safe_glob(const char *path, int flags, glob_t *pglob) {
errno = 0; errno = 0;
k = glob(path, flags | GLOB_ALTDIRFUNC, NULL, pglob); k = glob(path, flags | GLOB_ALTDIRFUNC, NULL, pglob);
if (k == GLOB_NOMATCH) if (k == GLOB_NOMATCH)
return -ENOENT; return -ENOENT;
if (k == GLOB_NOSPACE) if (k == GLOB_NOSPACE)
return -ENOMEM; return -ENOMEM;
if (k != 0) if (k != 0)
return errno > 0 ? -errno : -EIO; return errno_or_else(EIO);
if (strv_isempty(pglob->gl_pathv)) if (strv_isempty(pglob->gl_pathv))
return -ENOENT; return -ENOENT;

View file

@ -9,6 +9,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "alloc-util.h" #include "alloc-util.h"
#include "errno-util.h"
#include "in-addr-util.h" #include "in-addr-util.h"
#include "macro.h" #include "macro.h"
#include "parse-util.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; errno = 0;
if (!inet_ntop(family, u, x, l)) if (!inet_ntop(family, u, x, l))
return errno > 0 ? -errno : -EINVAL; return errno_or_else(EINVAL);
*ret = TAKE_PTR(x); *ret = TAKE_PTR(x);
return 0; return 0;
@ -345,7 +346,7 @@ int in_addr_prefix_to_string(int family, const union in_addr_union *u, unsigned
errno = 0; errno = 0;
if (!inet_ntop(family, u, x, l)) if (!inet_ntop(family, u, x, l))
return errno > 0 ? -errno : -EINVAL; return errno_or_else(EINVAL);
p = x + strlen(x); p = x + strlen(x);
l -= 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; errno = 0;
if (!inet_ntop(family, u, x, l)) if (!inet_ntop(family, u, x, l))
return errno > 0 ? -errno : -EINVAL; return errno_or_else(EINVAL);
sprintf(strchr(x, 0), "%%%i", ifindex); 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; errno = 0;
if (inet_pton(family, s, ret ?: &buffer) <= 0) if (inet_pton(family, s, ret ?: &buffer) <= 0)
return errno > 0 ? -errno : -EINVAL; return errno_or_else(EINVAL);
return 0; return 0;
} }

View file

@ -78,7 +78,7 @@ int socket_address_parse(SocketAddress *a, const char *s) {
errno = 0; errno = 0;
if (inet_pton(AF_INET6, n, &a->sockaddr.in6.sin6_addr) <= 0) if (inet_pton(AF_INET6, n, &a->sockaddr.in6.sin6_addr) <= 0)
return errno > 0 ? -errno : -EINVAL; return errno_or_else(EINVAL);
e++; e++;
if (*e != ':') if (*e != ':')

View file

@ -15,6 +15,7 @@
#include <utmp.h> #include <utmp.h>
#include "alloc-util.h" #include "alloc-util.h"
#include "errno-util.h"
#include "fd-util.h" #include "fd-util.h"
#include "fileio.h" #include "fileio.h"
#include "format-util.h" #include "format-util.h"
@ -213,7 +214,7 @@ int get_user_creds(
p = getpwnam(*username); p = getpwnam(*username);
} }
if (!p) { 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 the user requested that we only synthesize as fallback, do so now */
if (FLAGS_SET(flags, USER_CREDS_PREFER_NSS)) { 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) if (!g)
return errno > 0 ? -errno : -ESRCH; return errno_or_else(ESRCH);
if (gid) { if (gid) {
if (!gid_is_valid(g->gr_gid)) if (!gid_is_valid(g->gr_gid))
@ -492,7 +493,7 @@ int get_home_dir(char **_h) {
errno = 0; errno = 0;
p = getpwuid(u); p = getpwuid(u);
if (!p) if (!p)
return errno > 0 ? -errno : -ESRCH; return errno_or_else(ESRCH);
if (!path_is_valid(p->pw_dir) || if (!path_is_valid(p->pw_dir) ||
!path_is_absolute(p->pw_dir)) !path_is_absolute(p->pw_dir))
@ -549,7 +550,7 @@ int get_shell(char **_s) {
errno = 0; errno = 0;
p = getpwuid(u); p = getpwuid(u);
if (!p) if (!p)
return errno > 0 ? -errno : -ESRCH; return errno_or_else(ESRCH);
if (!path_is_valid(p->pw_shell) || if (!path_is_valid(p->pw_shell) ||
!path_is_absolute(p->pw_shell)) !path_is_absolute(p->pw_shell))
@ -770,7 +771,7 @@ int putpwent_sane(const struct passwd *pw, FILE *stream) {
errno = 0; errno = 0;
if (putpwent(pw, stream) != 0) if (putpwent(pw, stream) != 0)
return errno > 0 ? -errno : -EIO; return errno_or_else(EIO);
return 0; return 0;
} }
@ -781,7 +782,7 @@ int putspent_sane(const struct spwd *sp, FILE *stream) {
errno = 0; errno = 0;
if (putspent(sp, stream) != 0) if (putspent(sp, stream) != 0)
return errno > 0 ? -errno : -EIO; return errno_or_else(EIO);
return 0; return 0;
} }
@ -792,7 +793,7 @@ int putgrent_sane(const struct group *gr, FILE *stream) {
errno = 0; errno = 0;
if (putgrent(gr, stream) != 0) if (putgrent(gr, stream) != 0)
return errno > 0 ? -errno : -EIO; return errno_or_else(EIO);
return 0; return 0;
} }
@ -804,7 +805,7 @@ int putsgent_sane(const struct sgrp *sg, FILE *stream) {
errno = 0; errno = 0;
if (putsgent(sg, stream) != 0) if (putsgent(sg, stream) != 0)
return errno > 0 ? -errno : -EIO; return errno_or_else(EIO);
return 0; return 0;
} }
@ -819,7 +820,7 @@ int fgetpwent_sane(FILE *stream, struct passwd **pw) {
errno = 0; errno = 0;
p = fgetpwent(stream); p = fgetpwent(stream);
if (!p && errno != ENOENT) if (!p && errno != ENOENT)
return errno > 0 ? -errno : -EIO; return errno_or_else(EIO);
*pw = p; *pw = p;
return !!p; return !!p;
@ -834,7 +835,7 @@ int fgetspent_sane(FILE *stream, struct spwd **sp) {
errno = 0; errno = 0;
s = fgetspent(stream); s = fgetspent(stream);
if (!s && errno != ENOENT) if (!s && errno != ENOENT)
return errno > 0 ? -errno : -EIO; return errno_or_else(EIO);
*sp = s; *sp = s;
return !!s; return !!s;
@ -849,7 +850,7 @@ int fgetgrent_sane(FILE *stream, struct group **gr) {
errno = 0; errno = 0;
g = fgetgrent(stream); g = fgetgrent(stream);
if (!g && errno != ENOENT) if (!g && errno != ENOENT)
return errno > 0 ? -errno : -EIO; return errno_or_else(EIO);
*gr = g; *gr = g;
return !!g; return !!g;
@ -865,7 +866,7 @@ int fgetsgent_sane(FILE *stream, struct sgrp **sg) {
errno = 0; errno = 0;
s = fgetsgent(stream); s = fgetsgent(stream);
if (!s && errno != ENOENT) if (!s && errno != ENOENT)
return errno > 0 ? -errno : -EIO; return errno_or_else(EIO);
*sg = s; *sg = s;
return !!s; return !!s;

View file

@ -10,6 +10,7 @@
#include "cgroup.h" #include "cgroup.h"
#include "dbus-cgroup.h" #include "dbus-cgroup.h"
#include "dbus-util.h" #include "dbus-util.h"
#include "errno-util.h"
#include "fd-util.h" #include "fd-util.h"
#include "fileio.h" #include "fileio.h"
#include "limits-util.h" #include "limits-util.h"
@ -1492,7 +1493,7 @@ int bus_cgroup_set_property(
errno = 0; errno = 0;
if (!inet_ntop(item->family, &item->address, buffer, sizeof(buffer))) 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); fprintf(f, "%s=%s/%u\n", name, buffer, item->prefixlen);
} }

View file

@ -494,7 +494,7 @@ static int dynamic_user_realize(
errno = 0; errno = 0;
p = getpwuid(num); p = getpwuid(num);
if (!p) if (!p)
return errno > 0 ? -errno : -ESRCH; return errno_or_else(ESRCH);
gid = p->pw_gid; gid = p->pw_gid;
} }

View file

@ -999,12 +999,8 @@ static int get_supplementary_groups(const ExecContext *c, const char *user,
*/ */
errno = 0; errno = 0;
ngroups_max = (int) sysconf(_SC_NGROUPS_MAX); ngroups_max = (int) sysconf(_SC_NGROUPS_MAX);
if (ngroups_max <= 0) { if (ngroups_max <= 0)
if (errno > 0) return errno_or_else(EOPNOTSUPP);
return -errno;
else
return -EOPNOTSUPP; /* For all other values */
}
l_gids = new(gid_t, ngroups_max); l_gids = new(gid_t, ngroups_max);
if (!l_gids) if (!l_gids)

View file

@ -6,6 +6,7 @@
#include <unistd.h> #include <unistd.h>
#include "alloc-util.h" #include "alloc-util.h"
#include "errno-util.h"
#include "fd-util.h" #include "fd-util.h"
#include "fileio.h" #include "fileio.h"
#include "generator.h" #include "generator.h"
@ -94,7 +95,7 @@ static int verify_tty(const char *name) {
errno = 0; errno = 0;
if (isatty(fd) <= 0) if (isatty(fd) <= 0)
return errno > 0 ? -errno : -EIO; return errno_or_else(EIO);
return 0; return 0;
} }

View file

@ -17,6 +17,7 @@
#include "cgroup-util.h" #include "cgroup-util.h"
#include "conf-parser.h" #include "conf-parser.h"
#include "device-util.h" #include "device-util.h"
#include "errno-util.h"
#include "fd-util.h" #include "fd-util.h"
#include "limits-util.h" #include "limits-util.h"
#include "logind.h" #include "logind.h"
@ -191,7 +192,7 @@ int manager_add_user_by_uid(Manager *m, uid_t uid, User **_user) {
errno = 0; errno = 0;
p = getpwuid(uid); p = getpwuid(uid);
if (!p) 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); return manager_add_user(m, uid, p->pw_gid, p->pw_name, p->pw_dir, _user);
} }

View file

@ -1242,7 +1242,7 @@ static int method_set_user_linger(sd_bus_message *message, void *userdata, sd_bu
errno = 0; errno = 0;
pw = getpwuid(uid); pw = getpwuid(uid);
if (!pw) if (!pw)
return errno > 0 ? -errno : -ENOENT; return errno_or_else(ENOENT);
r = bus_verify_polkit_async( r = bus_verify_polkit_async(
message, message,

View file

@ -11,6 +11,7 @@
#include "bus-error.h" #include "bus-error.h"
#include "bus-util.h" #include "bus-util.h"
#include "env-file.h" #include "env-file.h"
#include "errno-util.h"
#include "escape.h" #include "escape.h"
#include "extract-word.h" #include "extract-word.h"
#include "fd-util.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); k = fscanf(f, UID_FMT " " UID_FMT " " UID_FMT "\n", &uid_base, &uid_shift, &uid_range);
if (k != 3) { if (k != 3) {
if (ferror(f)) if (ferror(f))
return -errno; return errno_or_else(EIO);
return -EBADMSG; 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); k = fscanf(f, GID_FMT " " GID_FMT " " GID_FMT "\n", &gid_base, &gid_shift, &gid_range);
if (k != 3) { if (k != 3) {
if (ferror(f)) if (ferror(f))
return -errno; return errno_or_else(EIO);
return -EBADMSG; return -EBADMSG;
} }

View file

@ -11,6 +11,7 @@
#include "bus-common-errors.h" #include "bus-common-errors.h"
#include "bus-util.h" #include "bus-util.h"
#include "cgroup-util.h" #include "cgroup-util.h"
#include "errno-util.h"
#include "fd-util.h" #include "fd-util.h"
#include "fileio.h" #include "fileio.h"
#include "format-util.h" #include "format-util.h"
@ -625,7 +626,7 @@ static int clean_pool_done(Operation *operation, int ret, sd_bus_error *error) {
errno = 0; errno = 0;
n = fread(&success, 1, sizeof(success), f); n = fread(&success, 1, sizeof(success), f);
if (n != sizeof(success)) if (n != sizeof(success))
return ret < 0 ? ret : (errno != 0 ? -errno : -EIO); return ret < 0 ? ret : errno_or_else(EIO);
if (ret < 0) { if (ret < 0) {
_cleanup_free_ char *name = NULL; _cleanup_free_ char *name = NULL;
@ -669,7 +670,7 @@ static int clean_pool_done(Operation *operation, int ret, sd_bus_error *error) {
errno = 0; errno = 0;
n = fread(&size, 1, sizeof(size), f); n = fread(&size, 1, sizeof(size), f);
if (n != sizeof(size)) if (n != sizeof(size))
return errno != 0 ? -errno : -EIO; return errno_or_else(EIO);
r = sd_bus_message_append(reply, "(st)", name, size); r = sd_bus_message_append(reply, "(st)", name, size);
if (r < 0) 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)) if (k < 0 && feof(f))
break; break;
if (k != 3) { if (k != 3) {
if (ferror(f) && errno > 0) if (ferror(f))
return -errno; return errno_or_else(EIO);
return -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)) if (k < 0 && feof(f))
break; break;
if (k != 3) { if (k != 3) {
if (ferror(f) && errno > 0) if (ferror(f))
return -errno; return errno_or_else(EIO);
return -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)) if (k < 0 && feof(f))
break; break;
if (k != 3) { if (k != 3) {
if (ferror(f) && errno > 0) if (ferror(f))
return -errno; return errno_or_else(EIO);
return -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)) if (k < 0 && feof(f))
break; break;
if (k != 3) { if (k != 3) {
if (ferror(f) && errno > 0) if (ferror(f))
return -errno; return errno_or_else(EIO);
return -EIO; return -EIO;
} }

View file

@ -60,7 +60,7 @@ int probe_filesystem(const char *node, char **ret_fstype) {
errno = 0; errno = 0;
b = blkid_new_probe_from_filename(node); b = blkid_new_probe_from_filename(node);
if (!b) if (!b)
return -errno ?: -ENOMEM; return errno_or_else(ENOMEM);
blkid_probe_enable_superblocks(b, 1); blkid_probe_enable_superblocks(b, 1);
blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE); blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE);
@ -76,7 +76,7 @@ int probe_filesystem(const char *node, char **ret_fstype) {
return -EUCLEAN; return -EUCLEAN;
} }
if (r != 0) if (r != 0)
return -errno ?: -EIO; return errno_or_else(EIO);
(void) blkid_probe_lookup_value(b, "TYPE", &fstype, NULL); (void) blkid_probe_lookup_value(b, "TYPE", &fstype, NULL);
@ -333,7 +333,7 @@ int dissect_image(
errno = 0; errno = 0;
r = blkid_probe_set_device(b, fd, 0, 0); r = blkid_probe_set_device(b, fd, 0, 0);
if (r != 0) if (r != 0)
return -errno ?: -ENOMEM; return errno_or_else(ENOMEM);
if ((flags & DISSECT_IMAGE_GPT_ONLY) == 0) { if ((flags & DISSECT_IMAGE_GPT_ONLY) == 0) {
/* Look for file system superblocks, unless we only shall look for GPT partition tables */ /* 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)) if (IN_SET(r, -2, 1))
return log_debug_errno(SYNTHETIC_ERRNO(ENOPKG), "Failed to identify any partition table."); return log_debug_errno(SYNTHETIC_ERRNO(ENOPKG), "Failed to identify any partition table.");
if (r != 0) if (r != 0)
return -errno ?: -EIO; return errno_or_else(EIO);
m = new0(DissectedImage, 1); m = new0(DissectedImage, 1);
if (!m) if (!m)
@ -415,7 +415,7 @@ int dissect_image(
errno = 0; errno = 0;
pl = blkid_probe_get_partitions(b); pl = blkid_probe_get_partitions(b);
if (!pl) 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); r = loop_wait_for_partitions_to_appear(fd, d, blkid_partlist_numof_partitions(pl), flags, &e);
if (r < 0) if (r < 0)

View file

@ -20,6 +20,7 @@
#include "blkid-util.h" #include "blkid-util.h"
#include "device-util.h" #include "device-util.h"
#include "efivars.h" #include "efivars.h"
#include "errno-util.h"
#include "fd-util.h" #include "fd-util.h"
#include "gpt.h" #include "gpt.h"
#include "parse-util.h" #include "parse-util.h"
@ -113,7 +114,7 @@ static int find_gpt_root(sd_device *dev, blkid_probe pr, bool test) {
errno = 0; errno = 0;
pl = blkid_probe_get_partitions(pr); pl = blkid_probe_get_partitions(pr);
if (!pl) if (!pl)
return -errno ?: -ENOMEM; return errno_or_else(ENOMEM);
nvals = blkid_partlist_numof_partitions(pl); nvals = blkid_partlist_numof_partitions(pl);
for (i = 0; i < nvals; i++) { for (i = 0; i < nvals; i++) {