]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/binfmt/binfmt.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / binfmt / binfmt.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <errno.h>
22 #include <getopt.h>
23 #include <limits.h>
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "alloc-util.h"
30 #include "conf-files.h"
31 #include "def.h"
32 #include "fd-util.h"
33 #include "fileio.h"
34 #include "log.h"
35 #include "string-util.h"
36 #include "strv.h"
37 #include "util.h"
38
39 static const char conf_file_dirs[] = CONF_PATHS_NULSTR("binfmt.d");
40
41 static int delete_rule(const char *rule) {
42 _cleanup_free_ char *x = NULL, *fn = NULL;
43 char *e;
44
45 assert(rule[0]);
46
47 x = strdup(rule);
48 if (!x)
49 return log_oom();
50
51 e = strchrnul(x+1, x[0]);
52 *e = 0;
53
54 fn = strappend("/proc/sys/fs/binfmt_misc/", x+1);
55 if (!fn)
56 return log_oom();
57
58 return write_string_file(fn, "-1", 0);
59 }
60
61 static int apply_rule(const char *rule) {
62 int r;
63
64 delete_rule(rule);
65
66 r = write_string_file("/proc/sys/fs/binfmt_misc/register", rule, 0);
67 if (r < 0)
68 return log_error_errno(r, "Failed to add binary format: %m");
69
70 return 0;
71 }
72
73 static int apply_file(const char *path, bool ignore_enoent) {
74 _cleanup_fclose_ FILE *f = NULL;
75 int r;
76
77 assert(path);
78
79 r = search_and_fopen_nulstr(path, "re", NULL, conf_file_dirs, &f);
80 if (r < 0) {
81 if (ignore_enoent && r == -ENOENT)
82 return 0;
83
84 return log_error_errno(r, "Failed to open file '%s', ignoring: %m", path);
85 }
86
87 log_debug("apply: %s", path);
88 for (;;) {
89 char l[LINE_MAX], *p;
90 int k;
91
92 if (!fgets(l, sizeof(l), f)) {
93 if (feof(f))
94 break;
95
96 return log_error_errno(errno, "Failed to read file '%s', ignoring: %m", path);
97 }
98
99 p = strstrip(l);
100 if (!*p)
101 continue;
102 if (strchr(COMMENTS "\n", *p))
103 continue;
104
105 k = apply_rule(p);
106 if (k < 0 && r == 0)
107 r = k;
108 }
109
110 return r;
111 }
112
113 static void help(void) {
114 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
115 "Registers binary formats.\n\n"
116 " -h --help Show this help\n"
117 " --version Show package version\n"
118 , program_invocation_short_name);
119 }
120
121 static int parse_argv(int argc, char *argv[]) {
122
123 enum {
124 ARG_VERSION = 0x100,
125 };
126
127 static const struct option options[] = {
128 { "help", no_argument, NULL, 'h' },
129 { "version", no_argument, NULL, ARG_VERSION },
130 {}
131 };
132
133 int c;
134
135 assert(argc >= 0);
136 assert(argv);
137
138 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
139
140 switch (c) {
141
142 case 'h':
143 help();
144 return 0;
145
146 case ARG_VERSION:
147 return version();
148
149 case '?':
150 return -EINVAL;
151
152 default:
153 assert_not_reached("Unhandled option");
154 }
155
156 return 1;
157 }
158
159 int main(int argc, char *argv[]) {
160 int r, k;
161
162 r = parse_argv(argc, argv);
163 if (r <= 0)
164 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
165
166 log_set_target(LOG_TARGET_AUTO);
167 log_parse_environment();
168 log_open();
169
170 umask(0022);
171
172 r = 0;
173
174 if (argc > optind) {
175 int i;
176
177 for (i = optind; i < argc; i++) {
178 k = apply_file(argv[i], false);
179 if (k < 0 && r == 0)
180 r = k;
181 }
182 } else {
183 _cleanup_strv_free_ char **files = NULL;
184 char **f;
185
186 r = conf_files_list_nulstr(&files, ".conf", NULL, 0, conf_file_dirs);
187 if (r < 0) {
188 log_error_errno(r, "Failed to enumerate binfmt.d files: %m");
189 goto finish;
190 }
191
192 /* Flush out all rules */
193 write_string_file("/proc/sys/fs/binfmt_misc/status", "-1", 0);
194
195 STRV_FOREACH(f, files) {
196 k = apply_file(*f, true);
197 if (k < 0 && r == 0)
198 r = k;
199 }
200 }
201
202 finish:
203 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
204 }