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