X Tutup
Skip to content

Commit b2a331f

Browse files
committed
virt: simplify userns_has_mapping() by using fscanf() instead of scanf()
And while we are at it, also fix propagation of an uninitialized errno error.
1 parent 49be038 commit b2a331f

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

src/basic/virt.c

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "cgroup-util.h"
1313
#include "dirent-util.h"
1414
#include "env-util.h"
15+
#include "errno-util.h"
1516
#include "fd-util.h"
1617
#include "fileio.h"
1718
#include "macro.h"
@@ -791,10 +792,7 @@ int detect_virtualization(void) {
791792

792793
static int userns_has_mapping(const char *name) {
793794
_cleanup_fclose_ FILE *f = NULL;
794-
_cleanup_free_ char *buf = NULL;
795-
size_t n_allocated = 0;
796-
ssize_t n;
797-
uint32_t a, b, c;
795+
uid_t a, b, c;
798796
int r;
799797

800798
f = fopen(name, "re");
@@ -803,19 +801,17 @@ static int userns_has_mapping(const char *name) {
803801
return errno == ENOENT ? false : -errno;
804802
}
805803

806-
n = getline(&buf, &n_allocated, f);
807-
if (n < 0) {
808-
if (feof(f)) {
809-
log_debug("%s is empty, we're in an uninitialized user namespace", name);
810-
return true;
811-
}
804+
errno = 0;
805+
r = fscanf(f, UID_FMT " " UID_FMT " " UID_FMT "\n", &a, &b, &c);
806+
if (r == EOF) {
807+
if (ferror(f))
808+
return log_debug_errno(errno_or_else(EIO), "Failed to read %s: %m", name);
812809

813-
return log_debug_errno(errno, "Failed to read %s: %m", name);
810+
log_debug("%s is empty, we're in an uninitialized user namespace", name);
811+
return true;
814812
}
815-
816-
r = sscanf(buf, "%"PRIu32" %"PRIu32" %"PRIu32, &a, &b, &c);
817-
if (r < 3)
818-
return log_debug_errno(errno, "Failed to parse %s: %m", name);
813+
if (r != 3)
814+
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), "Failed to parse %s: %m", name);
819815

820816
if (a == 0 && b == 0 && c == UINT32_MAX) {
821817
/* The kernel calls mappings_overlap() and does not allow overlaps */

0 commit comments

Comments
 (0)
X Tutup