| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdio.h> |
| 4 |
#include <sys/stat.h> |
| 5 |
|
| 6 |
#include "alloc-util.h" |
| 7 |
#include "binfmt-util.h" |
| 8 |
#include "build.h" |
| 9 |
#include "conf-files.h" |
| 10 |
#include "constants.h" |
| 11 |
#include "errno-util.h" |
| 12 |
#include "fd-util.h" |
| 13 |
#include "fileio.h" |
| 14 |
#include "format-table.h" |
| 15 |
#include "log.h" |
| 16 |
#include "main-func.h" |
| 17 |
#include "options.h" |
| 18 |
#include "pager.h" |
| 19 |
#include "path-util.h" |
| 20 |
#include "pretty-print.h" |
| 21 |
#include "string-util.h" |
| 22 |
#include "strv.h" |
| 23 |
|
| 24 |
static CatFlags arg_cat_flags = CAT_CONFIG_OFF; |
| 25 |
static PagerFlags arg_pager_flags = 0; |
| 26 |
static bool arg_unregister = false; |
| 27 |
|
| 28 |
static int delete_rule(const char *rulename) { |
| 29 |
const char *fn = strjoina("/proc/sys/fs/binfmt_misc/", rulename); |
| 30 |
return write_string_file(fn, "-1", WRITE_STRING_FILE_DISABLE_BUFFER); |
| 31 |
} |
| 32 |
|
| 33 |
static int apply_rule(const char *filename, unsigned line, const char *rule) { |
| 34 |
assert(filename); |
| 35 |
assert(line > 0); |
| 36 |
assert(rule); |
| 37 |
assert(rule[0]); |
| 38 |
|
| 39 |
_cleanup_free_ char *rulename = NULL; |
| 40 |
int r; |
| 41 |
|
| 42 |
rulename = strdupcspn(rule + 1, CHAR_TO_STR(rule[0])); |
| 43 |
if (!rulename) |
| 44 |
return log_oom(); |
| 45 |
|
| 46 |
if (!filename_is_valid(rulename) || |
| 47 |
STR_IN_SET(rulename, "register", "status")) |
| 48 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 49 |
"%s:%u: Rule name '%s' is not valid, refusing.", |
| 50 |
filename, line, rulename); |
| 51 |
r = delete_rule(rulename); |
| 52 |
if (r < 0 && r != -ENOENT) |
| 53 |
log_warning_errno(r, "%s:%u: Failed to delete rule '%s', ignoring: %m", |
| 54 |
filename, line, rulename); |
| 55 |
if (r >= 0) |
| 56 |
log_debug("%s:%u: Rule '%s' deleted.", filename, line, rulename); |
| 57 |
|
| 58 |
r = write_string_file("/proc/sys/fs/binfmt_misc/register", rule, WRITE_STRING_FILE_DISABLE_BUFFER); |
| 59 |
if (r < 0) |
| 60 |
return log_error_errno(r, "%s:%u: Failed to add binary format '%s': %m", |
| 61 |
filename, line, rulename); |
| 62 |
|
| 63 |
log_debug("%s:%u: Binary format '%s' registered.", filename, line, rulename); |
| 64 |
return 0; |
| 65 |
} |
| 66 |
|
| 67 |
static int apply_file(const char *filename, bool ignore_enoent) { |
| 68 |
_cleanup_fclose_ FILE *f = NULL; |
| 69 |
_cleanup_free_ char *pp = NULL; |
| 70 |
int r; |
| 71 |
|
| 72 |
assert(filename); |
| 73 |
|
| 74 |
r = search_and_fopen(filename, "re", NULL, (const char**) CONF_PATHS_STRV("binfmt.d"), &f, &pp); |
| 75 |
if (r < 0) { |
| 76 |
if (ignore_enoent && r == -ENOENT) |
| 77 |
return 0; |
| 78 |
|
| 79 |
return log_error_errno(r, "Failed to open file '%s': %m", filename); |
| 80 |
} |
| 81 |
|
| 82 |
log_debug("Applying %s%s", pp, glyph(GLYPH_ELLIPSIS)); |
| 83 |
for (unsigned line = 1;; line++) { |
| 84 |
_cleanup_free_ char *text = NULL; |
| 85 |
int k; |
| 86 |
|
| 87 |
k = read_stripped_line(f, LONG_LINE_MAX, &text); |
| 88 |
if (k < 0) |
| 89 |
return log_error_errno(k, "Failed to read file '%s': %m", pp); |
| 90 |
if (k == 0) |
| 91 |
break; |
| 92 |
|
| 93 |
if (isempty(text)) |
| 94 |
continue; |
| 95 |
if (strchr(COMMENTS, text[0])) |
| 96 |
continue; |
| 97 |
|
| 98 |
RET_GATHER(r, apply_rule(filename, line, text)); |
| 99 |
} |
| 100 |
|
| 101 |
return r; |
| 102 |
} |
| 103 |
|
| 104 |
static int cat_config(char **files) { |
| 105 |
pager_open(arg_pager_flags); |
| 106 |
|
| 107 |
return cat_files(NULL, files, arg_cat_flags); |
| 108 |
} |
| 109 |
|
| 110 |
static int help(void) { |
| 111 |
_cleanup_free_ char *link = NULL; |
| 112 |
_cleanup_(table_unrefp) Table *options = NULL; |
| 113 |
int r; |
| 114 |
|
| 115 |
r = terminal_urlify_man("systemd-binfmt.service", "8", &link); |
| 116 |
if (r < 0) |
| 117 |
return log_oom(); |
| 118 |
|
| 119 |
r = option_parser_get_help_table(&options); |
| 120 |
if (r < 0) |
| 121 |
return r; |
| 122 |
|
| 123 |
printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n" |
| 124 |
"\n%sRegisters binary formats with the kernel.%s\n" |
| 125 |
"\nOptions:\n", |
| 126 |
program_invocation_short_name, |
| 127 |
ansi_highlight(), |
| 128 |
ansi_normal()); |
| 129 |
r = table_print_or_warn(options); |
| 130 |
if (r < 0) |
| 131 |
return r; |
| 132 |
|
| 133 |
printf("\nSee the %s for details.\n", link); |
| 134 |
return 0; |
| 135 |
} |
| 136 |
|
| 137 |
static int parse_argv(int argc, char *argv[], char ***ret_args) { |
| 138 |
|
| 139 |
assert(argc >= 0); |
| 140 |
assert(argv); |
| 141 |
|
| 142 |
OptionParser opts = { argc, argv }; |
| 143 |
|
| 144 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 145 |
switch (c) { |
| 146 |
OPTION_COMMON_HELP: |
| 147 |
return help(); |
| 148 |
|
| 149 |
OPTION_COMMON_VERSION: |
| 150 |
return version(); |
| 151 |
|
| 152 |
OPTION_COMMON_CAT_CONFIG: |
| 153 |
arg_cat_flags = CAT_CONFIG_ON; |
| 154 |
break; |
| 155 |
|
| 156 |
OPTION_COMMON_TLDR: |
| 157 |
arg_cat_flags = CAT_TLDR; |
| 158 |
break; |
| 159 |
|
| 160 |
OPTION_COMMON_NO_PAGER: |
| 161 |
arg_pager_flags |= PAGER_DISABLE; |
| 162 |
break; |
| 163 |
|
| 164 |
OPTION_LONG("unregister", NULL, "Unregister all existing entries"): |
| 165 |
arg_unregister = true; |
| 166 |
break; |
| 167 |
} |
| 168 |
|
| 169 |
char **args = option_parser_get_args(&opts); |
| 170 |
|
| 171 |
if ((arg_unregister || arg_cat_flags != CAT_CONFIG_OFF) && !strv_isempty(args)) |
| 172 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 173 |
"Positional arguments are not allowed with --cat-config/--tldr or --unregister."); |
| 174 |
|
| 175 |
*ret_args = args; |
| 176 |
return 1; |
| 177 |
} |
| 178 |
|
| 179 |
static int binfmt_mounted_and_writable_warn(void) { |
| 180 |
int r; |
| 181 |
|
| 182 |
r = binfmt_mounted_and_writable(); |
| 183 |
if (r < 0) |
| 184 |
return log_error_errno(r, "Failed to check if /proc/sys/fs/binfmt_misc is mounted: %m"); |
| 185 |
if (r == 0) |
| 186 |
log_debug("/proc/sys/fs/binfmt_misc is not mounted in read-write mode, skipping."); |
| 187 |
|
| 188 |
return r; |
| 189 |
} |
| 190 |
|
| 191 |
static int run(int argc, char *argv[]) { |
| 192 |
int r; |
| 193 |
|
| 194 |
char **args = NULL; |
| 195 |
r = parse_argv(argc, argv, &args); |
| 196 |
if (r <= 0) |
| 197 |
return r; |
| 198 |
|
| 199 |
log_setup(); |
| 200 |
|
| 201 |
umask(0022); |
| 202 |
|
| 203 |
r = 0; |
| 204 |
|
| 205 |
if (arg_unregister) |
| 206 |
return disable_binfmt(); |
| 207 |
|
| 208 |
if (!strv_isempty(args)) { |
| 209 |
r = binfmt_mounted_and_writable_warn(); |
| 210 |
if (r <= 0) |
| 211 |
return r; |
| 212 |
|
| 213 |
STRV_FOREACH(f, args) |
| 214 |
RET_GATHER(r, apply_file(*f, false)); |
| 215 |
|
| 216 |
} else { |
| 217 |
_cleanup_strv_free_ char **files = NULL; |
| 218 |
|
| 219 |
r = conf_files_list_strv(&files, ".conf", /* root= */ NULL, CONF_FILES_WARN, (const char**) CONF_PATHS_STRV("binfmt.d")); |
| 220 |
if (r < 0) |
| 221 |
return log_error_errno(r, "Failed to enumerate binfmt.d files: %m"); |
| 222 |
|
| 223 |
if (arg_cat_flags != CAT_CONFIG_OFF) |
| 224 |
return cat_config(files); |
| 225 |
|
| 226 |
r = binfmt_mounted_and_writable_warn(); |
| 227 |
if (r <= 0) |
| 228 |
return r; |
| 229 |
|
| 230 |
/* Flush out all rules */ |
| 231 |
r = write_string_file("/proc/sys/fs/binfmt_misc/status", "-1", WRITE_STRING_FILE_DISABLE_BUFFER); |
| 232 |
if (r < 0) |
| 233 |
log_warning_errno(r, "Failed to flush binfmt_misc rules, ignoring: %m"); |
| 234 |
else |
| 235 |
log_debug("Flushed all binfmt_misc rules."); |
| 236 |
|
| 237 |
STRV_FOREACH(f, files) |
| 238 |
RET_GATHER(r, apply_file(*f, true)); |
| 239 |
} |
| 240 |
|
| 241 |
return r; |
| 242 |
} |
| 243 |
|
| 244 |
DEFINE_MAIN_FUNCTION(run); |