]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
151b190e | 2 | |
151b190e | 3 | #include <errno.h> |
fabe5c0e | 4 | #include <getopt.h> |
3f6fd1ba LP |
5 | #include <limits.h> |
6 | #include <stdbool.h> | |
7 | #include <stdio.h> | |
8 | #include <stdlib.h> | |
ca78ad1d ZJS |
9 | #include <sys/stat.h> |
10 | #include <sys/types.h> | |
151b190e | 11 | |
b5efdb8a | 12 | #include "alloc-util.h" |
846acb67 | 13 | #include "binfmt-util.h" |
3f6fd1ba | 14 | #include "conf-files.h" |
a0f29c76 | 15 | #include "def.h" |
3ffd4af2 | 16 | #include "fd-util.h" |
3f6fd1ba | 17 | #include "fileio.h" |
151b190e | 18 | #include "log.h" |
3be1cabe | 19 | #include "main-func.h" |
dcd5c891 | 20 | #include "pager.h" |
7452c3ff | 21 | #include "path-util.h" |
294bf0c3 | 22 | #include "pretty-print.h" |
07630cea | 23 | #include "string-util.h" |
db1413d7 | 24 | #include "strv.h" |
151b190e | 25 | |
6aaab70f | 26 | static bool arg_cat_config = false; |
0221d68a | 27 | static PagerFlags arg_pager_flags = 0; |
846acb67 | 28 | static bool arg_unregister = false; |
fabe5c0e | 29 | |
151b190e | 30 | static int delete_rule(const char *rule) { |
fabe5c0e LP |
31 | _cleanup_free_ char *x = NULL, *fn = NULL; |
32 | char *e; | |
151b190e | 33 | |
7452c3ff | 34 | assert(rule); |
151b190e LP |
35 | assert(rule[0]); |
36 | ||
f3670df1 LP |
37 | e = strchrnul(rule + 1, rule[0]); |
38 | x = strndup(rule + 1, e - rule - 1); | |
fabe5c0e | 39 | if (!x) |
14212119 | 40 | return log_oom(); |
151b190e | 41 | |
f3670df1 LP |
42 | if (!filename_is_valid(x) || |
43 | STR_IN_SET(x, "register", "status")) | |
baaa35ad | 44 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
f3670df1 | 45 | "Rule file name '%s' is not valid, refusing.", x); |
7452c3ff | 46 | |
f3670df1 | 47 | fn = path_join("/proc/sys/fs/binfmt_misc", x); |
151b190e | 48 | if (!fn) |
14212119 | 49 | return log_oom(); |
151b190e | 50 | |
57512c89 | 51 | return write_string_file(fn, "-1", WRITE_STRING_FILE_DISABLE_BUFFER); |
151b190e LP |
52 | } |
53 | ||
54 | static int apply_rule(const char *rule) { | |
55 | int r; | |
56 | ||
7452c3ff | 57 | (void) delete_rule(rule); |
151b190e | 58 | |
57512c89 | 59 | r = write_string_file("/proc/sys/fs/binfmt_misc/register", rule, WRITE_STRING_FILE_DISABLE_BUFFER); |
23bbb0de MS |
60 | if (r < 0) |
61 | return log_error_errno(r, "Failed to add binary format: %m"); | |
151b190e LP |
62 | |
63 | return 0; | |
64 | } | |
65 | ||
66 | static int apply_file(const char *path, bool ignore_enoent) { | |
fabe5c0e | 67 | _cleanup_fclose_ FILE *f = NULL; |
2708160c | 68 | _cleanup_free_ char *pp = NULL; |
fabe5c0e | 69 | int r; |
151b190e LP |
70 | |
71 | assert(path); | |
72 | ||
2708160c | 73 | r = search_and_fopen(path, "re", NULL, (const char**) CONF_PATHS_STRV("binfmt.d"), &f, &pp); |
fabe5c0e LP |
74 | if (r < 0) { |
75 | if (ignore_enoent && r == -ENOENT) | |
151b190e LP |
76 | return 0; |
77 | ||
741d2cb5 | 78 | return log_error_errno(r, "Failed to open file '%s': %m", path); |
151b190e LP |
79 | } |
80 | ||
2708160c | 81 | log_debug("apply: %s", pp); |
fabe5c0e | 82 | for (;;) { |
741d2cb5 LP |
83 | _cleanup_free_ char *line = NULL; |
84 | char *p; | |
151b190e LP |
85 | int k; |
86 | ||
741d2cb5 LP |
87 | k = read_line(f, LONG_LINE_MAX, &line); |
88 | if (k < 0) | |
2708160c | 89 | return log_error_errno(k, "Failed to read file '%s': %m", pp); |
741d2cb5 LP |
90 | if (k == 0) |
91 | break; | |
151b190e | 92 | |
741d2cb5 LP |
93 | p = strstrip(line); |
94 | if (isempty(p)) | |
151b190e | 95 | continue; |
741d2cb5 | 96 | if (strchr(COMMENTS, p[0])) |
151b190e LP |
97 | continue; |
98 | ||
fabe5c0e LP |
99 | k = apply_rule(p); |
100 | if (k < 0 && r == 0) | |
151b190e LP |
101 | r = k; |
102 | } | |
103 | ||
151b190e LP |
104 | return r; |
105 | } | |
106 | ||
37ec0fdd LP |
107 | static int help(void) { |
108 | _cleanup_free_ char *link = NULL; | |
109 | int r; | |
110 | ||
111 | r = terminal_urlify_man("systemd-binfmt.service", "8", &link); | |
112 | if (r < 0) | |
113 | return log_oom(); | |
114 | ||
fabe5c0e | 115 | printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n" |
37ec0fdd | 116 | "Registers binary formats with the kernel.\n\n" |
eb9da376 | 117 | " -h --help Show this help\n" |
601185b4 | 118 | " --version Show package version\n" |
6aaab70f | 119 | " --cat-config Show configuration files\n" |
dcd5c891 | 120 | " --no-pager Do not pipe output into a pager\n" |
846acb67 | 121 | " --unregister Unregister all existing entries\n" |
bc556335 DDM |
122 | "\nSee the %s for details.\n", |
123 | program_invocation_short_name, | |
124 | link); | |
37ec0fdd LP |
125 | |
126 | return 0; | |
fabe5c0e LP |
127 | } |
128 | ||
129 | static int parse_argv(int argc, char *argv[]) { | |
eb9da376 LP |
130 | enum { |
131 | ARG_VERSION = 0x100, | |
6aaab70f | 132 | ARG_CAT_CONFIG, |
dcd5c891 | 133 | ARG_NO_PAGER, |
846acb67 | 134 | ARG_UNREGISTER, |
eb9da376 LP |
135 | }; |
136 | ||
fabe5c0e | 137 | static const struct option options[] = { |
dcd5c891 LP |
138 | { "help", no_argument, NULL, 'h' }, |
139 | { "version", no_argument, NULL, ARG_VERSION }, | |
140 | { "cat-config", no_argument, NULL, ARG_CAT_CONFIG }, | |
141 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, | |
846acb67 | 142 | { "unregister", no_argument, NULL, ARG_UNREGISTER }, |
eb9da376 | 143 | {} |
fabe5c0e LP |
144 | }; |
145 | ||
146 | int c; | |
147 | ||
148 | assert(argc >= 0); | |
149 | assert(argv); | |
150 | ||
601185b4 | 151 | while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) |
fabe5c0e LP |
152 | |
153 | switch (c) { | |
154 | ||
155 | case 'h': | |
37ec0fdd | 156 | return help(); |
eb9da376 LP |
157 | |
158 | case ARG_VERSION: | |
3f6fd1ba | 159 | return version(); |
fabe5c0e | 160 | |
6aaab70f ZJS |
161 | case ARG_CAT_CONFIG: |
162 | arg_cat_config = true; | |
163 | break; | |
164 | ||
dcd5c891 | 165 | case ARG_NO_PAGER: |
0221d68a | 166 | arg_pager_flags |= PAGER_DISABLE; |
dcd5c891 LP |
167 | break; |
168 | ||
846acb67 LP |
169 | case ARG_UNREGISTER: |
170 | arg_unregister = true; | |
171 | break; | |
172 | ||
fabe5c0e LP |
173 | case '?': |
174 | return -EINVAL; | |
175 | ||
176 | default: | |
04499a70 | 177 | assert_not_reached(); |
fabe5c0e | 178 | } |
fabe5c0e | 179 | |
846acb67 | 180 | if ((arg_unregister || arg_cat_config) && argc > optind) |
baaa35ad | 181 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
846acb67 | 182 | "Positional arguments are not allowed with --cat-config or --unregister"); |
6aaab70f | 183 | |
fabe5c0e LP |
184 | return 1; |
185 | } | |
186 | ||
3be1cabe | 187 | static int run(int argc, char *argv[]) { |
fabe5c0e LP |
188 | int r, k; |
189 | ||
190 | r = parse_argv(argc, argv); | |
191 | if (r <= 0) | |
192 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; | |
151b190e | 193 | |
d2acb93d | 194 | log_setup(); |
151b190e | 195 | |
4c12626c LP |
196 | umask(0022); |
197 | ||
fabe5c0e | 198 | r = 0; |
13317670 | 199 | |
846acb67 LP |
200 | if (arg_unregister) |
201 | return disable_binfmt(); | |
202 | ||
fabe5c0e LP |
203 | if (argc > optind) { |
204 | int i; | |
13317670 | 205 | |
fabe5c0e | 206 | for (i = optind; i < argc; i++) { |
170dcb7b | 207 | k = apply_file(argv[i], false); |
13317670 LP |
208 | if (k < 0 && r == 0) |
209 | r = k; | |
210 | } | |
db1413d7 | 211 | } else { |
fabe5c0e LP |
212 | _cleanup_strv_free_ char **files = NULL; |
213 | char **f; | |
db1413d7 | 214 | |
a826d4f7 | 215 | r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char**) CONF_PATHS_STRV("binfmt.d")); |
3be1cabe ZJS |
216 | if (r < 0) |
217 | return log_error_errno(r, "Failed to enumerate binfmt.d files: %m"); | |
151b190e | 218 | |
6aaab70f | 219 | if (arg_cat_config) { |
0221d68a | 220 | (void) pager_open(arg_pager_flags); |
dcd5c891 | 221 | |
3be1cabe | 222 | return cat_files(NULL, files, 0); |
6aaab70f ZJS |
223 | } |
224 | ||
13317670 | 225 | /* Flush out all rules */ |
22e596d6 | 226 | (void) write_string_file("/proc/sys/fs/binfmt_misc/status", "-1", WRITE_STRING_FILE_DISABLE_BUFFER); |
13317670 | 227 | |
db1413d7 | 228 | STRV_FOREACH(f, files) { |
db1413d7 KS |
229 | k = apply_file(*f, true); |
230 | if (k < 0 && r == 0) | |
231 | r = k; | |
232 | } | |
db1413d7 | 233 | } |
fabe5c0e | 234 | |
3be1cabe | 235 | return r; |
151b190e | 236 | } |
3be1cabe ZJS |
237 | |
238 | DEFINE_MAIN_FUNCTION(run); |