diff --git a/include/kerndat.h b/include/kerndat.h index 2b0a653b2..322af0866 100644 --- a/include/kerndat.h +++ b/include/kerndat.h @@ -3,6 +3,8 @@ #include "asm/types.h" +struct stat; + /* * kerndat stands for "kernel data" and is a collection * of run-time information about current kernel @@ -21,8 +23,14 @@ extern int tcp_max_rshare; extern int kern_last_cap; extern u64 zero_page_pfn; -struct stat; -extern struct stat *kerndat_get_devpts_stat(void); +enum { + KERNDAT_FS_STAT_DEVPTS, + KERNDAT_FS_STAT_DEVTMPFS, + + KERNDAT_FS_STAT_MAX +}; + +extern struct stat *kerndat_get_fs_stat(unsigned int which); extern bool memfd_is_supported; diff --git a/kerndat.c b/kerndat.c index 5ab708070..5abfd9b9c 100644 --- a/kerndat.c +++ b/kerndat.c @@ -54,30 +54,50 @@ static int kerndat_get_shmemdev(void) return 0; } -struct stat *kerndat_get_devpts_stat() +struct stat *kerndat_get_fs_stat(unsigned int which) { - static struct stat st = {}; + static struct { + const char *name; + const char *path; + unsigned int magic; + struct stat st; + } kstat[KERNDAT_FS_STAT_MAX] = { + [KERNDAT_FS_STAT_DEVPTS] = { + .name = "devpts", + .path = "/dev/pts", + .magic = DEVPTS_SUPER_MAGIC, + }, + [KERNDAT_FS_STAT_DEVTMPFS] = { + .name = "devtmpfs", + .path = "/dev", + .magic = TMPFS_MAGIC, + }, + }; struct statfs fst; - if (st.st_dev != 0) - return &st; - - if (statfs("/dev/pts", &fst)) { - pr_perror("Unable to statefs /dev/pts"); - return NULL; - } - if (fst.f_type != DEVPTS_SUPER_MAGIC) { - pr_err("devpts isn't mount on the host\n"); + if (which >= KERNDAT_FS_STAT_MAX) { + pr_err("Wrong fs type %u passed\n", which); return NULL; } - /* The root /dev/pts is mounted w/o newinstance, isn't it? */ - if (stat("/dev/pts", &st)) { - pr_perror("Unable to stat /dev/pts"); + if (kstat[which].st.st_dev != 0) + return &kstat[which].st; + + if (statfs(kstat[which].path, &fst)) { + pr_perror("Unable to statefs %s", kstat[which].path); + return NULL; + } + if (fst.f_type != kstat[which].magic) { + pr_err("%s isn't mount on the host\n", kstat[which].name); return NULL; } - return &st; + if (stat(kstat[which].path, &kstat[which].st)) { + pr_perror("Unable to stat %s", kstat[which].path); + return NULL; + } + + return &kstat[which].st; } /* diff --git a/mount.c b/mount.c index e1f82ec09..244da296e 100644 --- a/mount.c +++ b/mount.c @@ -686,7 +686,7 @@ static int devpts_parse(struct mount_info *pm) { struct stat *host_st; - host_st = kerndat_get_devpts_stat(); + host_st = kerndat_get_fs_stat(KERNDAT_FS_STAT_DEVPTS); if (host_st == NULL) return -1;