]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/binfmt/binfmt.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[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 <string.h>
10
11 #include "alloc-util.h"
12 #include "conf-files.h"
13 #include "def.h"
14 #include "fd-util.h"
15 #include "fileio.h"
16 #include "log.h"
17 #include "main-func.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 #include "util.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 x = strdup(rule);
36 if (!x)
37 return log_oom();
38
39 e = strchrnul(x+1, x[0]);
40 *e = 0;
41
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);
45
46 fn = strappend("/proc/sys/fs/binfmt_misc/", x+1);
47 if (!fn)
48 return log_oom();
49
50 return write_string_file(fn, "-1", WRITE_STRING_FILE_DISABLE_BUFFER);
51 }
52
53 static int apply_rule(const char *rule) {
54 int r;
55
56 (void) delete_rule(rule);
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, "Failed to add binary format: %m");
61
62 return 0;
63 }
64
65 static int apply_file(const char *path, bool ignore_enoent) {
66 _cleanup_fclose_ FILE *f = NULL;
67 int r;
68
69 assert(path);
70
71 r = search_and_fopen(path, "re", NULL, (const char**) CONF_PATHS_STRV("binfmt.d"), &f);
72 if (r < 0) {
73 if (ignore_enoent && r == -ENOENT)
74 return 0;
75
76 return log_error_errno(r, "Failed to open file '%s': %m", path);
77 }
78
79 log_debug("apply: %s", path);
80 for (;;) {
81 _cleanup_free_ char *line = NULL;
82 char *p;
83 int k;
84
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;
90
91 p = strstrip(line);
92 if (isempty(p))
93 continue;
94 if (strchr(COMMENTS, p[0]))
95 continue;
96
97 k = apply_rule(p);
98 if (k < 0 && r == 0)
99 r = k;
100 }
101
102 return r;
103 }
104
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
113 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
114 "Registers binary formats with the kernel.\n\n"
115 " -h --help Show this help\n"
116 " --version Show package version\n"
117 " --cat-config Show configuration files\n"
118 " --no-pager Do not pipe output into a pager\n"
119 "\nSee the %s for details.\n"
120 , program_invocation_short_name
121 , link
122 );
123
124 return 0;
125 }
126
127 static int parse_argv(int argc, char *argv[]) {
128 enum {
129 ARG_VERSION = 0x100,
130 ARG_CAT_CONFIG,
131 ARG_NO_PAGER,
132 };
133
134 static const struct option options[] = {
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 },
139 {}
140 };
141
142 int c;
143
144 assert(argc >= 0);
145 assert(argv);
146
147 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
148
149 switch (c) {
150
151 case 'h':
152 return help();
153
154 case ARG_VERSION:
155 return version();
156
157 case ARG_CAT_CONFIG:
158 arg_cat_config = true;
159 break;
160
161 case ARG_NO_PAGER:
162 arg_pager_flags |= PAGER_DISABLE;
163 break;
164
165 case '?':
166 return -EINVAL;
167
168 default:
169 assert_not_reached("Unhandled option");
170 }
171
172 if (arg_cat_config && argc > optind)
173 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
174 "Positional arguments are not allowed with --cat-config");
175
176 return 1;
177 }
178
179 static int run(int argc, char *argv[]) {
180 int r, k;
181
182 r = parse_argv(argc, argv);
183 if (r <= 0)
184 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
185
186 log_setup_service();
187
188 umask(0022);
189
190 r = 0;
191
192 if (argc > optind) {
193 int i;
194
195 for (i = optind; i < argc; i++) {
196 k = apply_file(argv[i], false);
197 if (k < 0 && r == 0)
198 r = k;
199 }
200 } else {
201 _cleanup_strv_free_ char **files = NULL;
202 char **f;
203
204 r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char**) CONF_PATHS_STRV("binfmt.d"));
205 if (r < 0)
206 return log_error_errno(r, "Failed to enumerate binfmt.d files: %m");
207
208 if (arg_cat_config) {
209 (void) pager_open(arg_pager_flags);
210
211 return cat_files(NULL, files, 0);
212 }
213
214 /* Flush out all rules */
215 write_string_file("/proc/sys/fs/binfmt_misc/status", "-1", WRITE_STRING_FILE_DISABLE_BUFFER);
216
217 STRV_FOREACH(f, files) {
218 k = apply_file(*f, true);
219 if (k < 0 && r == 0)
220 r = k;
221 }
222 }
223
224 return r;
225 }
226
227 DEFINE_MAIN_FUNCTION(run);