1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include <sys/stat.h>
4 #include <sys/statvfs.h>
5 #include <sys/vfs.h>
6 
7 #include "binfmt-util.h"
8 #include "fileio.h"
9 #include "missing_magic.h"
10 #include "stat-util.h"
11 
disable_binfmt(void)12 int disable_binfmt(void) {
13         int r;
14 
15         /* Flush out all rules. This is important during shutdown to cover for rules using "F", since those
16          * might pin a file and thus block us from unmounting stuff cleanly.
17          *
18          * We are a bit careful here, since binfmt_misc might still be an autofs which we don't want to
19          * trigger. */
20 
21         r = path_is_fs_type("/proc/sys/fs/binfmt_misc", BINFMTFS_MAGIC);
22         if (r == 0 || r == -ENOENT) {
23                 log_debug("binfmt_misc is not mounted, not detaching entries.");
24                 return 0;
25         }
26         if (r < 0)
27                 return log_warning_errno(r, "Failed to determine whether binfmt_misc is mounted: %m");
28 
29         r = write_string_file("/proc/sys/fs/binfmt_misc/status", "-1", WRITE_STRING_FILE_DISABLE_BUFFER);
30         if (r < 0)
31                 return log_warning_errno(r, "Failed to unregister binfmt_misc entries: %m");
32 
33         log_debug("Unregistered all remaining binfmt_misc entries.");
34         return 0;
35 }
36