]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
stat-util: make sure inode_type_to_string() handles anonymous inodes in a reasonable way 31731/head
authorLennart Poettering <lennart@poettering.net>
Tue, 12 Mar 2024 09:45:24 +0000 (10:45 +0100)
committerLennart Poettering <lennart@poettering.net>
Tue, 12 Mar 2024 09:45:24 +0000 (10:45 +0100)
src/basic/stat-util.c
src/test/test-stat-util.c

index 6d09f8676678323ce0a7cfa8f8dd47a9f5e63312..ab45eda0edcc8747cda98df27c321aa4b2817d4b 100644 (file)
@@ -535,6 +535,8 @@ const char* inode_type_to_string(mode_t m) {
                 return "sock";
         }
 
+        /* Note anonmyous inodes in the kernel will have a zero type. Hence fstat() of an eventfd() will
+         * return an .st_mode where we'll return NULL here! */
         return NULL;
 }
 
index 272cd4c0d346f82735af5e7d65207c5f22849e5a..df37dcb528d50e6c16c19aefc169b75c65db4473 100644 (file)
@@ -3,6 +3,7 @@
 #include <fcntl.h>
 #include <linux/magic.h>
 #include <sched.h>
+#include <sys/eventfd.h>
 #include <unistd.h>
 
 #include "alloc-util.h"
@@ -195,6 +196,21 @@ TEST(inode_type_from_string) {
                 assert_se(inode_type_from_string(inode_type_to_string(*m)) == *m);
 }
 
+TEST(anonymous_inode) {
+        _cleanup_close_ int fd = -EBADF;
+
+        fd = eventfd(0, EFD_CLOEXEC);
+        assert_se(fd >= 0);
+
+        /* Verify that we handle anonymous inodes correctly, i.e. those which have no file type */
+
+        struct stat st;
+        assert_se(fstat(fd, &st) >= 0);
+        assert_se((st.st_mode & S_IFMT) == 0);
+
+        assert_se(!inode_type_to_string(st.st_mode));
+}
+
 TEST(fd_verify_linked) {
         _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
         _cleanup_close_ int tfd = -EBADF, fd = -EBADF;