1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
16 #include "alloc-util.h"
17 #include "conf-files.h"
22 #include "string-util.h"
26 static const char conf_file_dirs
[] = CONF_PATHS_NULSTR("binfmt.d");
28 static int delete_rule(const char *rule
) {
29 _cleanup_free_
char *x
= NULL
, *fn
= NULL
;
38 e
= strchrnul(x
+1, x
[0]);
41 fn
= strappend("/proc/sys/fs/binfmt_misc/", x
+1);
45 return write_string_file(fn
, "-1", 0);
48 static int apply_rule(const char *rule
) {
53 r
= write_string_file("/proc/sys/fs/binfmt_misc/register", rule
, 0);
55 return log_error_errno(r
, "Failed to add binary format: %m");
60 static int apply_file(const char *path
, bool ignore_enoent
) {
61 _cleanup_fclose_
FILE *f
= NULL
;
66 r
= search_and_fopen_nulstr(path
, "re", NULL
, conf_file_dirs
, &f
);
68 if (ignore_enoent
&& r
== -ENOENT
)
71 return log_error_errno(r
, "Failed to open file '%s', ignoring: %m", path
);
74 log_debug("apply: %s", path
);
79 if (!fgets(l
, sizeof(l
), f
)) {
83 return log_error_errno(errno
, "Failed to read file '%s', ignoring: %m", path
);
89 if (strchr(COMMENTS
"\n", *p
))
100 static void help(void) {
101 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
102 "Registers binary formats.\n\n"
103 " -h --help Show this help\n"
104 " --version Show package version\n"
105 , program_invocation_short_name
);
108 static int parse_argv(int argc
, char *argv
[]) {
114 static const struct option options
[] = {
115 { "help", no_argument
, NULL
, 'h' },
116 { "version", no_argument
, NULL
, ARG_VERSION
},
125 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
140 assert_not_reached("Unhandled option");
146 int main(int argc
, char *argv
[]) {
149 r
= parse_argv(argc
, argv
);
151 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;
153 log_set_target(LOG_TARGET_AUTO
);
154 log_parse_environment();
164 for (i
= optind
; i
< argc
; i
++) {
165 k
= apply_file(argv
[i
], false);
170 _cleanup_strv_free_
char **files
= NULL
;
173 r
= conf_files_list_nulstr(&files
, ".conf", NULL
, 0, conf_file_dirs
);
175 log_error_errno(r
, "Failed to enumerate binfmt.d files: %m");
179 /* Flush out all rules */
180 write_string_file("/proc/sys/fs/binfmt_misc/status", "-1", 0);
182 STRV_FOREACH(f
, files
) {
183 k
= apply_file(*f
, true);
190 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;