1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
10 #include <sys/types.h>
12 #include "alloc-util.h"
13 #include "binfmt-util.h"
15 #include "conf-files.h"
16 #include "constants.h"
20 #include "main-func.h"
22 #include "path-util.h"
23 #include "pretty-print.h"
24 #include "string-util.h"
27 static bool arg_cat_config
= false;
28 static PagerFlags arg_pager_flags
= 0;
29 static bool arg_unregister
= false;
31 static int delete_rule(const char *rulename
) {
32 const char *fn
= strjoina("/proc/sys/fs/binfmt_misc/", rulename
);
33 return write_string_file(fn
, "-1", WRITE_STRING_FILE_DISABLE_BUFFER
);
36 static int apply_rule(const char *filename
, unsigned line
, const char *rule
) {
42 _cleanup_free_
char *rulename
= NULL
;
46 e
= strchrnul(rule
+ 1, rule
[0]);
47 rulename
= strndup(rule
+ 1, e
- rule
- 1);
51 if (!filename_is_valid(rulename
) ||
52 STR_IN_SET(rulename
, "register", "status"))
53 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
54 "%s:%u: Rule name '%s' is not valid, refusing.",
55 filename
, line
, rulename
);
56 r
= delete_rule(rulename
);
57 if (r
< 0 && r
!= -ENOENT
)
58 log_warning_errno(r
, "%s:%u: Failed to delete rule '%s', ignoring: %m",
59 filename
, line
, rulename
);
61 log_debug("%s:%u: Rule '%s' deleted.", filename
, line
, rulename
);
63 r
= write_string_file("/proc/sys/fs/binfmt_misc/register", rule
, WRITE_STRING_FILE_DISABLE_BUFFER
);
65 return log_error_errno(r
, "%s:%u: Failed to add binary format '%s': %m",
66 filename
, line
, rulename
);
68 log_debug("%s:%u: Binary format '%s' registered.", filename
, line
, rulename
);
72 static int apply_file(const char *filename
, bool ignore_enoent
) {
73 _cleanup_fclose_
FILE *f
= NULL
;
74 _cleanup_free_
char *pp
= NULL
;
79 r
= search_and_fopen(filename
, "re", NULL
, (const char**) CONF_PATHS_STRV("binfmt.d"), &f
, &pp
);
81 if (ignore_enoent
&& r
== -ENOENT
)
84 return log_error_errno(r
, "Failed to open file '%s': %m", filename
);
87 log_debug("Applying %s%s", pp
, special_glyph(SPECIAL_GLYPH_ELLIPSIS
));
88 for (unsigned line
= 1;; line
++) {
89 _cleanup_free_
char *text
= NULL
;
93 k
= read_line(f
, LONG_LINE_MAX
, &text
);
95 return log_error_errno(k
, "Failed to read file '%s': %m", pp
);
102 if (strchr(COMMENTS
, p
[0]))
105 k
= apply_rule(filename
, line
, p
);
113 static int help(void) {
114 _cleanup_free_
char *link
= NULL
;
117 r
= terminal_urlify_man("systemd-binfmt.service", "8", &link
);
121 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
122 "Registers binary formats with the kernel.\n\n"
123 " -h --help Show this help\n"
124 " --version Show package version\n"
125 " --cat-config Show configuration files\n"
126 " --no-pager Do not pipe output into a pager\n"
127 " --unregister Unregister all existing entries\n"
128 "\nSee the %s for details.\n",
129 program_invocation_short_name
,
135 static int parse_argv(int argc
, char *argv
[]) {
143 static const struct option options
[] = {
144 { "help", no_argument
, NULL
, 'h' },
145 { "version", no_argument
, NULL
, ARG_VERSION
},
146 { "cat-config", no_argument
, NULL
, ARG_CAT_CONFIG
},
147 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
148 { "unregister", no_argument
, NULL
, ARG_UNREGISTER
},
157 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
168 arg_cat_config
= true;
172 arg_pager_flags
|= PAGER_DISABLE
;
176 arg_unregister
= true;
183 assert_not_reached();
186 if ((arg_unregister
|| arg_cat_config
) && argc
> optind
)
187 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
188 "Positional arguments are not allowed with --cat-config or --unregister");
193 static int run(int argc
, char *argv
[]) {
196 r
= parse_argv(argc
, argv
);
207 return disable_binfmt();
210 for (int i
= optind
; i
< argc
; i
++) {
211 k
= apply_file(argv
[i
], false);
216 _cleanup_strv_free_
char **files
= NULL
;
218 r
= conf_files_list_strv(&files
, ".conf", NULL
, 0, (const char**) CONF_PATHS_STRV("binfmt.d"));
220 return log_error_errno(r
, "Failed to enumerate binfmt.d files: %m");
222 if (arg_cat_config
) {
223 pager_open(arg_pager_flags
);
225 return cat_files(NULL
, files
, 0);
228 /* Flush out all rules */
229 r
= write_string_file("/proc/sys/fs/binfmt_misc/status", "-1", WRITE_STRING_FILE_DISABLE_BUFFER
);
231 log_warning_errno(r
, "Failed to flush binfmt_misc rules, ignoring: %m");
233 log_debug("Flushed all binfmt_misc rules.");
235 STRV_FOREACH(f
, files
) {
236 k
= apply_file(*f
, true);
245 DEFINE_MAIN_FUNCTION(run
);