]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/binfmt/binfmt.c
license: LGPL-2.1+ -> LGPL-2.1-or-later
[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"
37ec0fdd
LP
121 "\nSee the %s for details.\n"
122 , program_invocation_short_name
123 , link
124 );
125
126 return 0;
fabe5c0e
LP
127}
128
129static int parse_argv(int argc, char *argv[]) {
eb9da376
LP
130 enum {
131 ARG_VERSION = 0x100,
6aaab70f 132 ARG_CAT_CONFIG,
dcd5c891 133 ARG_NO_PAGER,
846acb67 134 ARG_UNREGISTER,
eb9da376
LP
135 };
136
fabe5c0e 137 static const struct option options[] = {
dcd5c891
LP
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 },
846acb67 142 { "unregister", no_argument, NULL, ARG_UNREGISTER },
eb9da376 143 {}
fabe5c0e
LP
144 };
145
146 int c;
147
148 assert(argc >= 0);
149 assert(argv);
150
601185b4 151 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
fabe5c0e
LP
152
153 switch (c) {
154
155 case 'h':
37ec0fdd 156 return help();
eb9da376
LP
157
158 case ARG_VERSION:
3f6fd1ba 159 return version();
fabe5c0e 160
6aaab70f
ZJS
161 case ARG_CAT_CONFIG:
162 arg_cat_config = true;
163 break;
164
dcd5c891 165 case ARG_NO_PAGER:
0221d68a 166 arg_pager_flags |= PAGER_DISABLE;
dcd5c891
LP
167 break;
168
846acb67
LP
169 case ARG_UNREGISTER:
170 arg_unregister = true;
171 break;
172
fabe5c0e
LP
173 case '?':
174 return -EINVAL;
175
176 default:
eb9da376 177 assert_not_reached("Unhandled option");
fabe5c0e 178 }
fabe5c0e 179
846acb67 180 if ((arg_unregister || arg_cat_config) && argc > optind)
baaa35ad 181 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
846acb67 182 "Positional arguments are not allowed with --cat-config or --unregister");
6aaab70f 183
fabe5c0e
LP
184 return 1;
185}
186
3be1cabe 187static int run(int argc, char *argv[]) {
fabe5c0e
LP
188 int r, k;
189
190 r = parse_argv(argc, argv);
191 if (r <= 0)
192 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
151b190e 193
6bf3c61c 194 log_setup_service();
151b190e 195
4c12626c
LP
196 umask(0022);
197
fabe5c0e 198 r = 0;
13317670 199
846acb67
LP
200 if (arg_unregister)
201 return disable_binfmt();
202
fabe5c0e
LP
203 if (argc > optind) {
204 int i;
13317670 205
fabe5c0e 206 for (i = optind; i < argc; i++) {
170dcb7b 207 k = apply_file(argv[i], false);
13317670
LP
208 if (k < 0 && r == 0)
209 r = k;
210 }
db1413d7 211 } else {
fabe5c0e
LP
212 _cleanup_strv_free_ char **files = NULL;
213 char **f;
db1413d7 214
a826d4f7 215 r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char**) CONF_PATHS_STRV("binfmt.d"));
3be1cabe
ZJS
216 if (r < 0)
217 return log_error_errno(r, "Failed to enumerate binfmt.d files: %m");
151b190e 218
6aaab70f 219 if (arg_cat_config) {
0221d68a 220 (void) pager_open(arg_pager_flags);
dcd5c891 221
3be1cabe 222 return cat_files(NULL, files, 0);
6aaab70f
ZJS
223 }
224
13317670 225 /* Flush out all rules */
22e596d6 226 (void) write_string_file("/proc/sys/fs/binfmt_misc/status", "-1", WRITE_STRING_FILE_DISABLE_BUFFER);
13317670 227
db1413d7 228 STRV_FOREACH(f, files) {
db1413d7
KS
229 k = apply_file(*f, true);
230 if (k < 0 && r == 0)
231 r = k;
232 }
db1413d7 233 }
fabe5c0e 234
3be1cabe 235 return r;
151b190e 236}
3be1cabe
ZJS
237
238DEFINE_MAIN_FUNCTION(run);