]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/binfmt/binfmt.c
udevd: use set_oom_score_adjust() to set OOM score
[thirdparty/systemd.git] / src / binfmt / binfmt.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
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>
9#include <string.h>
151b190e 10
b5efdb8a 11#include "alloc-util.h"
3f6fd1ba 12#include "conf-files.h"
a0f29c76 13#include "def.h"
3ffd4af2 14#include "fd-util.h"
3f6fd1ba 15#include "fileio.h"
151b190e 16#include "log.h"
dcd5c891 17#include "pager.h"
7452c3ff 18#include "path-util.h"
07630cea 19#include "string-util.h"
db1413d7 20#include "strv.h"
6aaab70f 21#include "terminal-util.h"
151b190e
LP
22#include "util.h"
23
6aaab70f 24static bool arg_cat_config = false;
dcd5c891 25static bool arg_no_pager = false;
fabe5c0e 26
151b190e 27static int delete_rule(const char *rule) {
fabe5c0e
LP
28 _cleanup_free_ char *x = NULL, *fn = NULL;
29 char *e;
151b190e 30
7452c3ff 31 assert(rule);
151b190e
LP
32 assert(rule[0]);
33
fabe5c0e
LP
34 x = strdup(rule);
35 if (!x)
14212119 36 return log_oom();
151b190e
LP
37
38 e = strchrnul(x+1, x[0]);
39 *e = 0;
40
7452c3ff
LP
41 if (!filename_is_valid(x + 1)) {
42 log_error("Rule file name '%s' is not valid, refusing.", x+1);
43 return -EINVAL;
44 }
45
fabe5c0e 46 fn = strappend("/proc/sys/fs/binfmt_misc/", x+1);
151b190e 47 if (!fn)
14212119 48 return log_oom();
151b190e 49
4c1fc3e4 50 return write_string_file(fn, "-1", 0);
151b190e
LP
51}
52
53static int apply_rule(const char *rule) {
54 int r;
55
7452c3ff 56 (void) delete_rule(rule);
151b190e 57
4c1fc3e4 58 r = write_string_file("/proc/sys/fs/binfmt_misc/register", rule, 0);
23bbb0de
MS
59 if (r < 0)
60 return log_error_errno(r, "Failed to add binary format: %m");
151b190e
LP
61
62 return 0;
63}
64
65static int apply_file(const char *path, bool ignore_enoent) {
fabe5c0e
LP
66 _cleanup_fclose_ FILE *f = NULL;
67 int r;
151b190e
LP
68
69 assert(path);
70
a826d4f7 71 r = search_and_fopen(path, "re", NULL, (const char**) CONF_PATHS_STRV("binfmt.d"), &f);
fabe5c0e
LP
72 if (r < 0) {
73 if (ignore_enoent && r == -ENOENT)
151b190e
LP
74 return 0;
75
741d2cb5 76 return log_error_errno(r, "Failed to open file '%s': %m", path);
151b190e
LP
77 }
78
9f6445e3 79 log_debug("apply: %s", path);
fabe5c0e 80 for (;;) {
741d2cb5
LP
81 _cleanup_free_ char *line = NULL;
82 char *p;
151b190e
LP
83 int k;
84
741d2cb5
LP
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;
151b190e 90
741d2cb5
LP
91 p = strstrip(line);
92 if (isempty(p))
151b190e 93 continue;
741d2cb5 94 if (strchr(COMMENTS, p[0]))
151b190e
LP
95 continue;
96
fabe5c0e
LP
97 k = apply_rule(p);
98 if (k < 0 && r == 0)
151b190e
LP
99 r = k;
100 }
101
151b190e
LP
102 return r;
103}
104
37ec0fdd
LP
105static 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
fabe5c0e 113 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
37ec0fdd 114 "Registers binary formats with the kernel.\n\n"
eb9da376 115 " -h --help Show this help\n"
601185b4 116 " --version Show package version\n"
6aaab70f 117 " --cat-config Show configuration files\n"
dcd5c891 118 " --no-pager Do not pipe output into a pager\n"
37ec0fdd
LP
119 "\nSee the %s for details.\n"
120 , program_invocation_short_name
121 , link
122 );
123
124 return 0;
fabe5c0e
LP
125}
126
127static int parse_argv(int argc, char *argv[]) {
128
eb9da376
LP
129 enum {
130 ARG_VERSION = 0x100,
6aaab70f 131 ARG_CAT_CONFIG,
dcd5c891 132 ARG_NO_PAGER,
eb9da376
LP
133 };
134
fabe5c0e 135 static const struct option options[] = {
dcd5c891
LP
136 { "help", no_argument, NULL, 'h' },
137 { "version", no_argument, NULL, ARG_VERSION },
138 { "cat-config", no_argument, NULL, ARG_CAT_CONFIG },
139 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
eb9da376 140 {}
fabe5c0e
LP
141 };
142
143 int c;
144
145 assert(argc >= 0);
146 assert(argv);
147
601185b4 148 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
fabe5c0e
LP
149
150 switch (c) {
151
152 case 'h':
37ec0fdd 153 return help();
eb9da376
LP
154
155 case ARG_VERSION:
3f6fd1ba 156 return version();
fabe5c0e 157
6aaab70f
ZJS
158 case ARG_CAT_CONFIG:
159 arg_cat_config = true;
160 break;
161
dcd5c891
LP
162 case ARG_NO_PAGER:
163 arg_no_pager = true;
164 break;
165
fabe5c0e
LP
166 case '?':
167 return -EINVAL;
168
169 default:
eb9da376 170 assert_not_reached("Unhandled option");
fabe5c0e 171 }
fabe5c0e 172
6aaab70f
ZJS
173 if (arg_cat_config && argc > optind) {
174 log_error("Positional arguments are not allowed with --cat-config");
175 return -EINVAL;
176 }
177
fabe5c0e
LP
178 return 1;
179}
180
151b190e 181int main(int argc, char *argv[]) {
fabe5c0e
LP
182 int r, k;
183
184 r = parse_argv(argc, argv);
185 if (r <= 0)
186 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
151b190e 187
151b190e
LP
188 log_set_target(LOG_TARGET_AUTO);
189 log_parse_environment();
190 log_open();
191
4c12626c
LP
192 umask(0022);
193
fabe5c0e 194 r = 0;
13317670 195
fabe5c0e
LP
196 if (argc > optind) {
197 int i;
13317670 198
fabe5c0e 199 for (i = optind; i < argc; i++) {
170dcb7b 200 k = apply_file(argv[i], false);
13317670
LP
201 if (k < 0 && r == 0)
202 r = k;
203 }
db1413d7 204 } else {
fabe5c0e
LP
205 _cleanup_strv_free_ char **files = NULL;
206 char **f;
db1413d7 207
a826d4f7 208 r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char**) CONF_PATHS_STRV("binfmt.d"));
44143309 209 if (r < 0) {
da927ba9 210 log_error_errno(r, "Failed to enumerate binfmt.d files: %m");
44143309
KS
211 goto finish;
212 }
151b190e 213
6aaab70f 214 if (arg_cat_config) {
dcd5c891
LP
215 (void) pager_open(arg_no_pager, false);
216
6aaab70f
ZJS
217 r = cat_files(NULL, files, 0);
218 goto finish;
219 }
220
13317670 221 /* Flush out all rules */
ad118bda 222 write_string_file("/proc/sys/fs/binfmt_misc/status", "-1", 0);
13317670 223
db1413d7 224 STRV_FOREACH(f, files) {
db1413d7
KS
225 k = apply_file(*f, true);
226 if (k < 0 && r == 0)
227 r = k;
228 }
db1413d7 229 }
fabe5c0e 230
44143309 231finish:
dcd5c891
LP
232 pager_close();
233
151b190e
LP
234 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
235}