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 CatFlags arg_cat_flags
= CAT_CONFIG_OFF
;
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
;
45 rulename
= strdupcspn(rule
+ 1, CHAR_TO_STR(rule
[0]));
49 if (!filename_is_valid(rulename
) ||
50 STR_IN_SET(rulename
, "register", "status"))
51 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
52 "%s:%u: Rule name '%s' is not valid, refusing.",
53 filename
, line
, rulename
);
54 r
= delete_rule(rulename
);
55 if (r
< 0 && r
!= -ENOENT
)
56 log_warning_errno(r
, "%s:%u: Failed to delete rule '%s', ignoring: %m",
57 filename
, line
, rulename
);
59 log_debug("%s:%u: Rule '%s' deleted.", filename
, line
, rulename
);
61 r
= write_string_file("/proc/sys/fs/binfmt_misc/register", rule
, WRITE_STRING_FILE_DISABLE_BUFFER
);
63 return log_error_errno(r
, "%s:%u: Failed to add binary format '%s': %m",
64 filename
, line
, rulename
);
66 log_debug("%s:%u: Binary format '%s' registered.", filename
, line
, rulename
);
70 static int apply_file(const char *filename
, bool ignore_enoent
) {
71 _cleanup_fclose_
FILE *f
= NULL
;
72 _cleanup_free_
char *pp
= NULL
;
77 r
= search_and_fopen(filename
, "re", NULL
, (const char**) CONF_PATHS_STRV("binfmt.d"), &f
, &pp
);
79 if (ignore_enoent
&& r
== -ENOENT
)
82 return log_error_errno(r
, "Failed to open file '%s': %m", filename
);
85 log_debug("Applying %s%s", pp
, special_glyph(SPECIAL_GLYPH_ELLIPSIS
));
86 for (unsigned line
= 1;; line
++) {
87 _cleanup_free_
char *text
= NULL
;
90 k
= read_stripped_line(f
, LONG_LINE_MAX
, &text
);
92 return log_error_errno(k
, "Failed to read file '%s': %m", pp
);
98 if (strchr(COMMENTS
, text
[0]))
101 RET_GATHER(r
, apply_rule(filename
, line
, text
));
107 static int cat_config(char **files
) {
108 pager_open(arg_pager_flags
);
110 return cat_files(NULL
, files
, arg_cat_flags
);
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 " --tldr Show non-comment parts of configuration\n"
127 " --no-pager Do not pipe output into a pager\n"
128 " --unregister Unregister all existing entries\n"
129 "\nSee the %s for details.\n",
130 program_invocation_short_name
,
136 static int parse_argv(int argc
, char *argv
[]) {
145 static const struct option options
[] = {
146 { "help", no_argument
, NULL
, 'h' },
147 { "version", no_argument
, NULL
, ARG_VERSION
},
148 { "cat-config", no_argument
, NULL
, ARG_CAT_CONFIG
},
149 { "tldr", no_argument
, NULL
, ARG_TLDR
},
150 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
151 { "unregister", no_argument
, NULL
, ARG_UNREGISTER
},
160 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
171 arg_cat_flags
= CAT_CONFIG_ON
;
175 arg_cat_flags
= CAT_TLDR
;
179 arg_pager_flags
|= PAGER_DISABLE
;
183 arg_unregister
= true;
190 assert_not_reached();
193 if ((arg_unregister
|| arg_cat_flags
!= CAT_CONFIG_OFF
) && argc
> optind
)
194 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
195 "Positional arguments are not allowed with --cat-config/--tldr or --unregister.");
200 static int binfmt_mounted_warn(void) {
203 r
= binfmt_mounted();
205 return log_error_errno(r
, "Failed to check if /proc/sys/fs/binfmt_misc is mounted: %m");
207 log_debug("/proc/sys/fs/binfmt_misc is not mounted in read-write mode, skipping.");
212 static int run(int argc
, char *argv
[]) {
215 r
= parse_argv(argc
, argv
);
226 return disable_binfmt();
229 r
= binfmt_mounted_warn();
233 for (int i
= optind
; i
< argc
; i
++)
234 RET_GATHER(r
, apply_file(argv
[i
], false));
237 _cleanup_strv_free_
char **files
= NULL
;
239 r
= conf_files_list_strv(&files
, ".conf", NULL
, 0, (const char**) CONF_PATHS_STRV("binfmt.d"));
241 return log_error_errno(r
, "Failed to enumerate binfmt.d files: %m");
243 if (arg_cat_flags
!= CAT_CONFIG_OFF
)
244 return cat_config(files
);
246 r
= binfmt_mounted_warn();
250 /* Flush out all rules */
251 r
= write_string_file("/proc/sys/fs/binfmt_misc/status", "-1", WRITE_STRING_FILE_DISABLE_BUFFER
);
253 log_warning_errno(r
, "Failed to flush binfmt_misc rules, ignoring: %m");
255 log_debug("Flushed all binfmt_misc rules.");
257 STRV_FOREACH(f
, files
)
258 RET_GATHER(r
, apply_file(*f
, true));
264 DEFINE_MAIN_FUNCTION(run
);