Merge pull request #13024 from poettering/errno-or-else

Errno or else
This commit is contained in:
Yu Watanabe 2019-07-12 09:33:21 +09:00 committed by GitHub
commit 8bf7e6709b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 71 additions and 58 deletions

View File

@ -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;
}

View File

@ -36,6 +36,17 @@ static inline char *strerror_safe(int error) {
return strerror(abs(error));
}
static inline int errno_or_else(int fallback) {
/* To be used when invoking library calls where errno handling is not defined clearly: we return
* errno if it is set, and the specified error otherwise. The idea is that the caller initializes
* errno to zero before doing an API call, and then uses this helper to retrieve a somewhat useful
* error code */
if (errno > 0)
return -errno;
return -abs(fallback);
}
/* Hint #1: ENETUNREACH happens if we try to connect to "non-existing" special IP addresses, such as ::5.
*
* Hint #2: The kernel sends e.g., EHOSTUNREACH or ENONET to userspace in some ICMP error cases. See the

View File

@ -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;

View File

@ -8,6 +8,7 @@
#include <unistd.h>
#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;

View File

@ -9,6 +9,7 @@
#include <stdlib.h>
#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;
}

View File

@ -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 != ':')

View File

@ -15,6 +15,7 @@
#include <utmp.h>
#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;

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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)

View File

@ -6,6 +6,7 @@
#include <unistd.h>
#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;
}

View File

@ -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);
}

View File

@ -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,

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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)

View File

@ -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++) {