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