1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include "alloc-util.h"
12 #include "conf-files.h"
17 #include "main-func.h"
19 #include "path-util.h"
20 #include "pretty-print.h"
21 #include "string-util.h"
25 static bool arg_cat_config
= false;
26 static PagerFlags arg_pager_flags
= 0;
28 static int delete_rule(const char *rule
) {
29 _cleanup_free_
char *x
= NULL
, *fn
= NULL
;
39 e
= strchrnul(x
+1, x
[0]);
42 if (!filename_is_valid(x
+ 1))
43 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
44 "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", WRITE_STRING_FILE_DISABLE_BUFFER
);
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
, WRITE_STRING_FILE_DISABLE_BUFFER
);
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
[]) {
134 static const struct option options
[] = {
135 { "help", no_argument
, NULL
, 'h' },
136 { "version", no_argument
, NULL
, ARG_VERSION
},
137 { "cat-config", no_argument
, NULL
, ARG_CAT_CONFIG
},
138 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
147 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
158 arg_cat_config
= true;
162 arg_pager_flags
|= PAGER_DISABLE
;
169 assert_not_reached("Unhandled option");
172 if (arg_cat_config
&& argc
> optind
)
173 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
174 "Positional arguments are not allowed with --cat-config");
179 static int run(int argc
, char *argv
[]) {
182 r
= parse_argv(argc
, argv
);
184 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;
195 for (i
= optind
; i
< argc
; i
++) {
196 k
= apply_file(argv
[i
], false);
201 _cleanup_strv_free_
char **files
= NULL
;
204 r
= conf_files_list_strv(&files
, ".conf", NULL
, 0, (const char**) CONF_PATHS_STRV("binfmt.d"));
206 return log_error_errno(r
, "Failed to enumerate binfmt.d files: %m");
208 if (arg_cat_config
) {
209 (void) pager_open(arg_pager_flags
);
211 return cat_files(NULL
, files
, 0);
214 /* Flush out all rules */
215 write_string_file("/proc/sys/fs/binfmt_misc/status", "-1", WRITE_STRING_FILE_DISABLE_BUFFER
);
217 STRV_FOREACH(f
, files
) {
218 k
= apply_file(*f
, true);
227 DEFINE_MAIN_FUNCTION(run
);