]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/binfmt/binfmt.c
Merge pull request #19391 from poettering/dissect-grow
[thirdparty/systemd.git] / src / binfmt / binfmt.c
CommitLineData
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 26static bool arg_cat_config = false;
0221d68a 27static PagerFlags arg_pager_flags = 0;
846acb67 28static bool arg_unregister = false;
fabe5c0e 29
151b190e 30static 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
54static 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
66static int apply_file(const char *path, bool ignore_enoent) {
fabe5c0e
LP
67 _cleanup_fclose_ FILE *f = NULL;
68 int r;
151b190e
LP
69
70 assert(path);
71
a826d4f7 72 r = search_and_fopen(path, "re", NULL, (const char**) CONF_PATHS_STRV("binfmt.d"), &f);
fabe5c0e
LP
73 if (r < 0) {
74 if (ignore_enoent && r == -ENOENT)
151b190e
LP
75 return 0;
76
741d2cb5 77 return log_error_errno(r, "Failed to open file '%s': %m", path);
151b190e
LP
78 }
79
9f6445e3 80 log_debug("apply: %s", path);
fabe5c0e 81 for (;;) {
741d2cb5
LP
82 _cleanup_free_ char *line = NULL;
83 char *p;
151b190e
LP
84 int k;
85
741d2cb5
LP
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;
151b190e 91
741d2cb5
LP
92 p = strstrip(line);
93 if (isempty(p))
151b190e 94 continue;
741d2cb5 95 if (strchr(COMMENTS, p[0]))
151b190e
LP
96 continue;
97
fabe5c0e
LP
98 k = apply_rule(p);
99 if (k < 0 && r == 0)
151b190e
LP
100 r = k;
101 }
102
151b190e
LP
103 return r;
104}
105
37ec0fdd
LP
106static 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
fabe5c0e 114 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
37ec0fdd 115 "Registers binary formats with the kernel.\n\n"
eb9da376 116 " -h --help Show this help\n"
601185b4 117 " --version Show package version\n"
6aaab70f 118 " --cat-config Show configuration files\n"
dcd5c891 119 " --no-pager Do not pipe output into a pager\n"
846acb67 120 " --unregister Unregister all existing entries\n"
bc556335
DDM
121 "\nSee the %s for details.\n",
122 program_invocation_short_name,
123 link);
37ec0fdd
LP
124
125 return 0;
fabe5c0e
LP
126}
127
128static int parse_argv(int argc, char *argv[]) {
eb9da376
LP
129 enum {
130 ARG_VERSION = 0x100,
6aaab70f 131 ARG_CAT_CONFIG,
dcd5c891 132 ARG_NO_PAGER,
846acb67 133 ARG_UNREGISTER,
eb9da376
LP
134 };
135
fabe5c0e 136 static const struct option options[] = {
dcd5c891
LP
137 { "help", no_argument, NULL, 'h' },
138 { "version", no_argument, NULL, ARG_VERSION },
139 { "cat-config", no_argument, NULL, ARG_CAT_CONFIG },
140 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
846acb67 141 { "unregister", no_argument, NULL, ARG_UNREGISTER },
eb9da376 142 {}
fabe5c0e
LP
143 };
144
145 int c;
146
147 assert(argc >= 0);
148 assert(argv);
149
601185b4 150 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
fabe5c0e
LP
151
152 switch (c) {
153
154 case 'h':
37ec0fdd 155 return help();
eb9da376
LP
156
157 case ARG_VERSION:
3f6fd1ba 158 return version();
fabe5c0e 159
6aaab70f
ZJS
160 case ARG_CAT_CONFIG:
161 arg_cat_config = true;
162 break;
163
dcd5c891 164 case ARG_NO_PAGER:
0221d68a 165 arg_pager_flags |= PAGER_DISABLE;
dcd5c891
LP
166 break;
167
846acb67
LP
168 case ARG_UNREGISTER:
169 arg_unregister = true;
170 break;
171
fabe5c0e
LP
172 case '?':
173 return -EINVAL;
174
175 default:
eb9da376 176 assert_not_reached("Unhandled option");
fabe5c0e 177 }
fabe5c0e 178
846acb67 179 if ((arg_unregister || arg_cat_config) && argc > optind)
baaa35ad 180 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
846acb67 181 "Positional arguments are not allowed with --cat-config or --unregister");
6aaab70f 182
fabe5c0e
LP
183 return 1;
184}
185
3be1cabe 186static int run(int argc, char *argv[]) {
fabe5c0e
LP
187 int r, k;
188
189 r = parse_argv(argc, argv);
190 if (r <= 0)
191 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
151b190e 192
d2acb93d 193 log_setup();
151b190e 194
4c12626c
LP
195 umask(0022);
196
fabe5c0e 197 r = 0;
13317670 198
846acb67
LP
199 if (arg_unregister)
200 return disable_binfmt();
201
fabe5c0e
LP
202 if (argc > optind) {
203 int i;
13317670 204
fabe5c0e 205 for (i = optind; i < argc; i++) {
170dcb7b 206 k = apply_file(argv[i], false);
13317670
LP
207 if (k < 0 && r == 0)
208 r = k;
209 }
db1413d7 210 } else {
fabe5c0e
LP
211 _cleanup_strv_free_ char **files = NULL;
212 char **f;
db1413d7 213
a826d4f7 214 r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char**) CONF_PATHS_STRV("binfmt.d"));
3be1cabe
ZJS
215 if (r < 0)
216 return log_error_errno(r, "Failed to enumerate binfmt.d files: %m");
151b190e 217
6aaab70f 218 if (arg_cat_config) {
0221d68a 219 (void) pager_open(arg_pager_flags);
dcd5c891 220
3be1cabe 221 return cat_files(NULL, files, 0);
6aaab70f
ZJS
222 }
223
13317670 224 /* Flush out all rules */
22e596d6 225 (void) write_string_file("/proc/sys/fs/binfmt_misc/status", "-1", WRITE_STRING_FILE_DISABLE_BUFFER);
13317670 226
db1413d7 227 STRV_FOREACH(f, files) {
db1413d7
KS
228 k = apply_file(*f, true);
229 if (k < 0 && r == 0)
230 r = k;
231 }
db1413d7 232 }
fabe5c0e 233
3be1cabe 234 return r;
151b190e 235}
3be1cabe
ZJS
236
237DEFINE_MAIN_FUNCTION(run);