1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include <sys/types.h>
12 #include "alloc-util.h"
13 #include "conf-files.h"
18 #include "main-func.h"
20 #include "path-util.h"
21 #include "pretty-print.h"
22 #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
;
35 e
= strchrnul(rule
+ 1, rule
[0]);
36 x
= strndup(rule
+ 1, e
- rule
- 1);
40 if (!filename_is_valid(x
) ||
41 STR_IN_SET(x
, "register", "status"))
42 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
43 "Rule file name '%s' is not valid, refusing.", x
);
45 fn
= path_join("/proc/sys/fs/binfmt_misc", x
);
49 return write_string_file(fn
, "-1", WRITE_STRING_FILE_DISABLE_BUFFER
);
52 static int apply_rule(const char *rule
) {
55 (void) delete_rule(rule
);
57 r
= write_string_file("/proc/sys/fs/binfmt_misc/register", rule
, WRITE_STRING_FILE_DISABLE_BUFFER
);
59 return log_error_errno(r
, "Failed to add binary format: %m");
64 static int apply_file(const char *path
, bool ignore_enoent
) {
65 _cleanup_fclose_
FILE *f
= NULL
;
70 r
= search_and_fopen(path
, "re", NULL
, (const char**) CONF_PATHS_STRV("binfmt.d"), &f
);
72 if (ignore_enoent
&& r
== -ENOENT
)
75 return log_error_errno(r
, "Failed to open file '%s': %m", path
);
78 log_debug("apply: %s", path
);
80 _cleanup_free_
char *line
= NULL
;
84 k
= read_line(f
, LONG_LINE_MAX
, &line
);
86 return log_error_errno(k
, "Failed to read file '%s': %m", path
);
93 if (strchr(COMMENTS
, p
[0]))
104 static int help(void) {
105 _cleanup_free_
char *link
= NULL
;
108 r
= terminal_urlify_man("systemd-binfmt.service", "8", &link
);
112 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
113 "Registers binary formats with the kernel.\n\n"
114 " -h --help Show this help\n"
115 " --version Show package version\n"
116 " --cat-config Show configuration files\n"
117 " --no-pager Do not pipe output into a pager\n"
118 "\nSee the %s for details.\n"
119 , program_invocation_short_name
126 static int parse_argv(int argc
, char *argv
[]) {
133 static const struct option options
[] = {
134 { "help", no_argument
, NULL
, 'h' },
135 { "version", no_argument
, NULL
, ARG_VERSION
},
136 { "cat-config", no_argument
, NULL
, ARG_CAT_CONFIG
},
137 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
146 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
157 arg_cat_config
= true;
161 arg_pager_flags
|= PAGER_DISABLE
;
168 assert_not_reached("Unhandled option");
171 if (arg_cat_config
&& argc
> optind
)
172 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
173 "Positional arguments are not allowed with --cat-config");
178 static int run(int argc
, char *argv
[]) {
181 r
= parse_argv(argc
, argv
);
183 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;
194 for (i
= optind
; i
< argc
; i
++) {
195 k
= apply_file(argv
[i
], false);
200 _cleanup_strv_free_
char **files
= NULL
;
203 r
= conf_files_list_strv(&files
, ".conf", NULL
, 0, (const char**) CONF_PATHS_STRV("binfmt.d"));
205 return log_error_errno(r
, "Failed to enumerate binfmt.d files: %m");
207 if (arg_cat_config
) {
208 (void) pager_open(arg_pager_flags
);
210 return cat_files(NULL
, files
, 0);
213 /* Flush out all rules */
214 (void) write_string_file("/proc/sys/fs/binfmt_misc/status", "-1", WRITE_STRING_FILE_DISABLE_BUFFER
);
216 STRV_FOREACH(f
, files
) {
217 k
= apply_file(*f
, true);
226 DEFINE_MAIN_FUNCTION(run
);