]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
binfmt-util: split out binfmt_mounted()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 10 Dec 2022 02:25:28 +0000 (11:25 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 14 Dec 2022 18:26:39 +0000 (03:26 +0900)
No functional changes, just refactoring and preparation for later
commits.

src/shared/binfmt-util.c

index 724d7f27d999ac2324f3af808aae84b568ce0900..e813c3ebcb44321ab67ff384b7be58e4997993d6 100644 (file)
@@ -5,10 +5,29 @@
 #include <sys/vfs.h>
 
 #include "binfmt-util.h"
+#include "errno-util.h"
+#include "fd-util.h"
 #include "fileio.h"
 #include "missing_magic.h"
 #include "stat-util.h"
 
+static int binfmt_mounted(void) {
+        _cleanup_close_ int fd = -EBADF;
+        int r;
+
+        fd = RET_NERRNO(open("/proc/sys/fs/binfmt_misc", O_CLOEXEC | O_DIRECTORY | O_PATH));
+        if (fd == -ENOENT)
+                return false;
+        if (fd < 0)
+                return fd;
+
+        r = fd_is_fs_type(fd, BINFMTFS_MAGIC);
+        if (r <= 0)
+                return r;
+
+        return true;
+}
+
 int disable_binfmt(void) {
         int r;
 
@@ -18,13 +37,13 @@ int disable_binfmt(void) {
          * We are a bit careful here, since binfmt_misc might still be an autofs which we don't want to
          * trigger. */
 
-        r = path_is_fs_type("/proc/sys/fs/binfmt_misc", BINFMTFS_MAGIC);
-        if (r == 0 || r == -ENOENT) {
+        r = binfmt_mounted();
+        if (r < 0)
+                return log_warning_errno(r, "Failed to determine whether binfmt_misc is mounted: %m");
+        if (r == 0) {
                 log_debug("binfmt_misc is not mounted, not detaching entries.");
                 return 0;
         }
-        if (r < 0)
-                return log_warning_errno(r, "Failed to determine whether binfmt_misc is mounted: %m");
 
         r = write_string_file("/proc/sys/fs/binfmt_misc/status", "-1", WRITE_STRING_FILE_DISABLE_BUFFER);
         if (r < 0)