1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 #include "alloc-util.h"
30 #include "conf-files.h"
35 #include "string-util.h"
39 static const char conf_file_dirs
[] = CONF_PATHS_NULSTR("binfmt.d");
41 static int delete_rule(const char *rule
) {
42 _cleanup_free_
char *x
= NULL
, *fn
= NULL
;
51 e
= strchrnul(x
+1, x
[0]);
54 fn
= strappend("/proc/sys/fs/binfmt_misc/", x
+1);
58 return write_string_file(fn
, "-1", 0);
61 static int apply_rule(const char *rule
) {
66 r
= write_string_file("/proc/sys/fs/binfmt_misc/register", rule
, 0);
68 return log_error_errno(r
, "Failed to add binary format: %m");
73 static int apply_file(const char *path
, bool ignore_enoent
) {
74 _cleanup_fclose_
FILE *f
= NULL
;
79 r
= search_and_fopen_nulstr(path
, "re", NULL
, conf_file_dirs
, &f
);
81 if (ignore_enoent
&& r
== -ENOENT
)
84 return log_error_errno(r
, "Failed to open file '%s', ignoring: %m", path
);
87 log_debug("apply: %s", path
);
92 if (!fgets(l
, sizeof(l
), f
)) {
96 return log_error_errno(errno
, "Failed to read file '%s', ignoring: %m", path
);
102 if (strchr(COMMENTS
"\n", *p
))
113 static void help(void) {
114 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
115 "Registers binary formats.\n\n"
116 " -h --help Show this help\n"
117 " --version Show package version\n"
118 , program_invocation_short_name
);
121 static int parse_argv(int argc
, char *argv
[]) {
127 static const struct option options
[] = {
128 { "help", no_argument
, NULL
, 'h' },
129 { "version", no_argument
, NULL
, ARG_VERSION
},
138 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
153 assert_not_reached("Unhandled option");
159 int main(int argc
, char *argv
[]) {
162 r
= parse_argv(argc
, argv
);
164 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;
166 log_set_target(LOG_TARGET_AUTO
);
167 log_parse_environment();
177 for (i
= optind
; i
< argc
; i
++) {
178 k
= apply_file(argv
[i
], false);
183 _cleanup_strv_free_
char **files
= NULL
;
186 r
= conf_files_list_nulstr(&files
, ".conf", NULL
, 0, conf_file_dirs
);
188 log_error_errno(r
, "Failed to enumerate binfmt.d files: %m");
192 /* Flush out all rules */
193 write_string_file("/proc/sys/fs/binfmt_misc/status", "-1", 0);
195 STRV_FOREACH(f
, files
) {
196 k
= apply_file(*f
, true);
203 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;