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