1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
16 #include "alloc-util.h"
17 #include "conf-files.h"
23 #include "string-util.h"
25 #include "terminal-util.h"
28 static bool arg_cat_config
= false;
29 static bool arg_no_pager
= false;
31 static int delete_rule(const char *rule
) {
32 _cleanup_free_
char *x
= NULL
, *fn
= NULL
;
41 e
= strchrnul(x
+1, x
[0]);
44 fn
= strappend("/proc/sys/fs/binfmt_misc/", x
+1);
48 return write_string_file(fn
, "-1", 0);
51 static int apply_rule(const char *rule
) {
56 r
= write_string_file("/proc/sys/fs/binfmt_misc/register", rule
, 0);
58 return log_error_errno(r
, "Failed to add binary format: %m");
63 static int apply_file(const char *path
, bool ignore_enoent
) {
64 _cleanup_fclose_
FILE *f
= NULL
;
69 r
= search_and_fopen(path
, "re", NULL
, (const char**) CONF_PATHS_STRV("binfmt.d"), &f
);
71 if (ignore_enoent
&& r
== -ENOENT
)
74 return log_error_errno(r
, "Failed to open file '%s', ignoring: %m", path
);
77 log_debug("apply: %s", path
);
82 if (!fgets(l
, sizeof(l
), f
)) {
86 return log_error_errno(errno
, "Failed to read file '%s', ignoring: %m", path
);
92 if (strchr(COMMENTS
"\n", *p
))
103 static void help(void) {
104 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
105 "Registers binary formats.\n\n"
106 " -h --help Show this help\n"
107 " --version Show package version\n"
108 " --cat-config Show configuration files\n"
109 " --no-pager Do not pipe output into a pager\n"
110 , program_invocation_short_name
);
113 static int parse_argv(int argc
, char *argv
[]) {
121 static const struct option options
[] = {
122 { "help", no_argument
, NULL
, 'h' },
123 { "version", no_argument
, NULL
, ARG_VERSION
},
124 { "cat-config", no_argument
, NULL
, ARG_CAT_CONFIG
},
125 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
134 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
146 arg_cat_config
= true;
157 assert_not_reached("Unhandled option");
160 if (arg_cat_config
&& argc
> optind
) {
161 log_error("Positional arguments are not allowed with --cat-config");
168 int main(int argc
, char *argv
[]) {
171 r
= parse_argv(argc
, argv
);
173 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;
175 log_set_target(LOG_TARGET_AUTO
);
176 log_parse_environment();
186 for (i
= optind
; i
< argc
; i
++) {
187 k
= apply_file(argv
[i
], false);
192 _cleanup_strv_free_
char **files
= NULL
;
195 r
= conf_files_list_strv(&files
, ".conf", NULL
, 0, (const char**) CONF_PATHS_STRV("binfmt.d"));
197 log_error_errno(r
, "Failed to enumerate binfmt.d files: %m");
201 if (arg_cat_config
) {
202 (void) pager_open(arg_no_pager
, false);
204 r
= cat_files(NULL
, files
, 0);
208 /* Flush out all rules */
209 write_string_file("/proc/sys/fs/binfmt_misc/status", "-1", 0);
211 STRV_FOREACH(f
, files
) {
212 k
= apply_file(*f
, true);
221 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;