]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
shared: add common helper for unregistering all binfmt entries
authorLennart Poettering <lennart@poettering.net>
Thu, 23 Apr 2020 14:36:06 +0000 (16:36 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 23 Apr 2020 15:13:50 +0000 (17:13 +0200)
src/shared/binfmt-util.c [new file with mode: 0644]
src/shared/binfmt-util.h [new file with mode: 0644]
src/shared/meson.build

diff --git a/src/shared/binfmt-util.c b/src/shared/binfmt-util.c
new file mode 100644 (file)
index 0000000..0229726
--- /dev/null
@@ -0,0 +1,33 @@
+#include <sys/stat.h>
+#include <sys/statvfs.h>
+#include <sys/vfs.h>
+
+#include "binfmt-util.h"
+#include "fileio.h"
+#include "missing_magic.h"
+#include "stat-util.h"
+
+int disable_binfmt(void) {
+        int r;
+
+        /* Flush out all rules. This is important during shutdown to cover for rules using "F", since those
+         * might pin a file and thus block us from unmounting stuff cleanly.
+         *
+         * 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) {
+                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)
+                return log_warning_errno(r, "Failed to unregister binfmt_misc entries: %m");
+
+        log_debug("Unregistered all remaining binfmt_misc entries.");
+        return 0;
+}
diff --git a/src/shared/binfmt-util.h b/src/shared/binfmt-util.h
new file mode 100644 (file)
index 0000000..70feaad
--- /dev/null
@@ -0,0 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
+#pragma once
+
+int disable_binfmt(void);
index 483148492c876fbc477c26e12a1147e1477d7128..e608ea8a1da59a17485da72d58b20231a49b1682 100644 (file)
@@ -12,6 +12,8 @@ shared_sources = files('''
         barrier.h
         base-filesystem.c
         base-filesystem.h
+        binfmt-util.c
+        binfmt-util.h
         bitmap.c
         bitmap.h
         blkid-util.h