1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include "alloc-util.h"
12 #include "conf-files.h"
18 #include "path-util.h"
19 #include "string-util.h"
21 #include "terminal-util.h"
24 static bool arg_cat_config
= false;
25 static bool arg_no_pager
= false;
27 static int delete_rule(const char *rule
) {
28 _cleanup_free_
char *x
= NULL
, *fn
= NULL
;
38 e
= strchrnul(x
+1, x
[0]);
41 if (!filename_is_valid(x
+ 1)) {
42 log_error("Rule file name '%s' is not valid, refusing.", x
+1);
46 fn
= strappend("/proc/sys/fs/binfmt_misc/", x
+1);
50 return write_string_file(fn
, "-1", 0);
53 static int apply_rule(const char *rule
) {
56 (void) delete_rule(rule
);
58 r
= write_string_file("/proc/sys/fs/binfmt_misc/register", rule
, 0);
60 return log_error_errno(r
, "Failed to add binary format: %m");
65 static int apply_file(const char *path
, bool ignore_enoent
) {
66 _cleanup_fclose_
FILE *f
= NULL
;
71 r
= search_and_fopen(path
, "re", NULL
, (const char**) CONF_PATHS_STRV("binfmt.d"), &f
);
73 if (ignore_enoent
&& r
== -ENOENT
)
76 return log_error_errno(r
, "Failed to open file '%s': %m", path
);
79 log_debug("apply: %s", path
);
81 _cleanup_free_
char *line
= NULL
;
85 k
= read_line(f
, LONG_LINE_MAX
, &line
);
87 return log_error_errno(k
, "Failed to read file '%s': %m", path
);
94 if (strchr(COMMENTS
, p
[0]))
105 static int help(void) {
106 _cleanup_free_
char *link
= NULL
;
109 r
= terminal_urlify_man("systemd-binfmt.service", "8", &link
);
113 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
114 "Registers binary formats with the kernel.\n\n"
115 " -h --help Show this help\n"
116 " --version Show package version\n"
117 " --cat-config Show configuration files\n"
118 " --no-pager Do not pipe output into a pager\n"
119 "\nSee the %s for details.\n"
120 , program_invocation_short_name
127 static int parse_argv(int argc
, char *argv
[]) {
135 static const struct option options
[] = {
136 { "help", no_argument
, NULL
, 'h' },
137 { "version", no_argument
, NULL
, ARG_VERSION
},
138 { "cat-config", no_argument
, NULL
, ARG_CAT_CONFIG
},
139 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
148 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
159 arg_cat_config
= true;
170 assert_not_reached("Unhandled option");
173 if (arg_cat_config
&& argc
> optind
) {
174 log_error("Positional arguments are not allowed with --cat-config");
181 int main(int argc
, char *argv
[]) {
184 r
= parse_argv(argc
, argv
);
186 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;
188 log_set_target(LOG_TARGET_AUTO
);
189 log_parse_environment();
199 for (i
= optind
; i
< argc
; i
++) {
200 k
= apply_file(argv
[i
], false);
205 _cleanup_strv_free_
char **files
= NULL
;
208 r
= conf_files_list_strv(&files
, ".conf", NULL
, 0, (const char**) CONF_PATHS_STRV("binfmt.d"));
210 log_error_errno(r
, "Failed to enumerate binfmt.d files: %m");
214 if (arg_cat_config
) {
215 (void) pager_open(arg_no_pager
, false);
217 r
= cat_files(NULL
, files
, 0);
221 /* Flush out all rules */
222 write_string_file("/proc/sys/fs/binfmt_misc/status", "-1", 0);
224 STRV_FOREACH(f
, files
) {
225 k
= apply_file(*f
, true);
234 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;