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