1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
10 #include <sys/types.h>
12 #include "alloc-util.h"
13 #include "binfmt-util.h"
14 #include "conf-files.h"
19 #include "main-func.h"
21 #include "path-util.h"
22 #include "pretty-print.h"
23 #include "string-util.h"
26 static bool arg_cat_config
= false;
27 static PagerFlags arg_pager_flags
= 0;
28 static bool arg_unregister
= false;
30 static int delete_rule(const char *rule
) {
31 _cleanup_free_
char *x
= NULL
, *fn
= NULL
;
37 e
= strchrnul(rule
+ 1, rule
[0]);
38 x
= strndup(rule
+ 1, e
- rule
- 1);
42 if (!filename_is_valid(x
) ||
43 STR_IN_SET(x
, "register", "status"))
44 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
45 "Rule file name '%s' is not valid, refusing.", x
);
47 fn
= path_join("/proc/sys/fs/binfmt_misc", x
);
51 return write_string_file(fn
, "-1", WRITE_STRING_FILE_DISABLE_BUFFER
);
54 static int apply_rule(const char *rule
) {
57 (void) delete_rule(rule
);
59 r
= write_string_file("/proc/sys/fs/binfmt_misc/register", rule
, WRITE_STRING_FILE_DISABLE_BUFFER
);
61 return log_error_errno(r
, "Failed to add binary format: %m");
66 static int apply_file(const char *path
, bool ignore_enoent
) {
67 _cleanup_fclose_
FILE *f
= NULL
;
68 _cleanup_free_
char *pp
= NULL
;
73 r
= search_and_fopen(path
, "re", NULL
, (const char**) CONF_PATHS_STRV("binfmt.d"), &f
, &pp
);
75 if (ignore_enoent
&& r
== -ENOENT
)
78 return log_error_errno(r
, "Failed to open file '%s': %m", path
);
81 log_debug("apply: %s", pp
);
83 _cleanup_free_
char *line
= NULL
;
87 k
= read_line(f
, LONG_LINE_MAX
, &line
);
89 return log_error_errno(k
, "Failed to read file '%s': %m", pp
);
96 if (strchr(COMMENTS
, p
[0]))
107 static int help(void) {
108 _cleanup_free_
char *link
= NULL
;
111 r
= terminal_urlify_man("systemd-binfmt.service", "8", &link
);
115 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
116 "Registers binary formats with the kernel.\n\n"
117 " -h --help Show this help\n"
118 " --version Show package version\n"
119 " --cat-config Show configuration files\n"
120 " --no-pager Do not pipe output into a pager\n"
121 " --unregister Unregister all existing entries\n"
122 "\nSee the %s for details.\n",
123 program_invocation_short_name
,
129 static int parse_argv(int argc
, char *argv
[]) {
137 static const struct option options
[] = {
138 { "help", no_argument
, NULL
, 'h' },
139 { "version", no_argument
, NULL
, ARG_VERSION
},
140 { "cat-config", no_argument
, NULL
, ARG_CAT_CONFIG
},
141 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
142 { "unregister", no_argument
, NULL
, ARG_UNREGISTER
},
151 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
162 arg_cat_config
= true;
166 arg_pager_flags
|= PAGER_DISABLE
;
170 arg_unregister
= true;
177 assert_not_reached();
180 if ((arg_unregister
|| arg_cat_config
) && argc
> optind
)
181 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
182 "Positional arguments are not allowed with --cat-config or --unregister");
187 static int run(int argc
, char *argv
[]) {
190 r
= parse_argv(argc
, argv
);
201 return disable_binfmt();
206 for (i
= optind
; i
< argc
; i
++) {
207 k
= apply_file(argv
[i
], false);
212 _cleanup_strv_free_
char **files
= NULL
;
215 r
= conf_files_list_strv(&files
, ".conf", NULL
, 0, (const char**) CONF_PATHS_STRV("binfmt.d"));
217 return log_error_errno(r
, "Failed to enumerate binfmt.d files: %m");
219 if (arg_cat_config
) {
220 pager_open(arg_pager_flags
);
222 return cat_files(NULL
, files
, 0);
225 /* Flush out all rules */
226 (void) write_string_file("/proc/sys/fs/binfmt_misc/status", "-1", WRITE_STRING_FILE_DISABLE_BUFFER
);
228 STRV_FOREACH(f
, files
) {
229 k
= apply_file(*f
, true);
238 DEFINE_MAIN_FUNCTION(run
);