]> git.proxmox.com Git - systemd.git/blame - src/binfmt/binfmt.c
New upstream version 236
[systemd.git] / src / binfmt / binfmt.c
CommitLineData
52ad194e 1/* SPDX-License-Identifier: LGPL-2.1+ */
663996b3
MS
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
663996b3 21#include <errno.h>
663996b3 22#include <getopt.h>
6300502b
MP
23#include <limits.h>
24#include <stdbool.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
663996b3 28
db2df898 29#include "alloc-util.h"
6300502b 30#include "conf-files.h"
db2df898
MP
31#include "def.h"
32#include "fd-util.h"
6300502b 33#include "fileio.h"
663996b3 34#include "log.h"
db2df898 35#include "string-util.h"
663996b3
MS
36#include "strv.h"
37#include "util.h"
663996b3 38
db2df898 39static const char conf_file_dirs[] = CONF_PATHS_NULSTR("binfmt.d");
663996b3
MS
40
41static 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
7035cd9e 58 return write_string_file(fn, "-1", 0);
663996b3
MS
59}
60
61static int apply_rule(const char *rule) {
62 int r;
63
64 delete_rule(rule);
65
7035cd9e 66 r = write_string_file("/proc/sys/fs/binfmt_misc/register", rule, 0);
f47781d8
MP
67 if (r < 0)
68 return log_error_errno(r, "Failed to add binary format: %m");
663996b3
MS
69
70 return 0;
71}
72
73static int apply_file(const char *path, bool ignore_enoent) {
74 _cleanup_fclose_ FILE *f = NULL;
75 int r;
76
77 assert(path);
78
60f067b4 79 r = search_and_fopen_nulstr(path, "re", NULL, conf_file_dirs, &f);
663996b3
MS
80 if (r < 0) {
81 if (ignore_enoent && r == -ENOENT)
82 return 0;
83
f47781d8 84 return log_error_errno(r, "Failed to open file '%s', ignoring: %m", path);
663996b3
MS
85 }
86
60f067b4 87 log_debug("apply: %s", path);
663996b3
MS
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
db2df898 96 return log_error_errno(errno, "Failed to read file '%s', ignoring: %m", path);
663996b3
MS
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
5eef597e 113static void help(void) {
663996b3
MS
114 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
115 "Registers binary formats.\n\n"
60f067b4 116 " -h --help Show this help\n"
5eef597e
MP
117 " --version Show package version\n"
118 , program_invocation_short_name);
663996b3
MS
119}
120
121static int parse_argv(int argc, char *argv[]) {
122
60f067b4
JS
123 enum {
124 ARG_VERSION = 0x100,
125 };
126
663996b3
MS
127 static const struct option options[] = {
128 { "help", no_argument, NULL, 'h' },
60f067b4
JS
129 { "version", no_argument, NULL, ARG_VERSION },
130 {}
663996b3
MS
131 };
132
133 int c;
134
135 assert(argc >= 0);
136 assert(argv);
137
5eef597e 138 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
663996b3
MS
139
140 switch (c) {
141
142 case 'h':
5eef597e
MP
143 help();
144 return 0;
60f067b4
JS
145
146 case ARG_VERSION:
6300502b 147 return version();
663996b3
MS
148
149 case '?':
150 return -EINVAL;
151
152 default:
60f067b4 153 assert_not_reached("Unhandled option");
663996b3 154 }
663996b3
MS
155
156 return 1;
157}
158
159int 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
f5e65279 186 r = conf_files_list_nulstr(&files, ".conf", NULL, 0, conf_file_dirs);
663996b3 187 if (r < 0) {
f47781d8 188 log_error_errno(r, "Failed to enumerate binfmt.d files: %m");
663996b3
MS
189 goto finish;
190 }
191
192 /* Flush out all rules */
7035cd9e 193 write_string_file("/proc/sys/fs/binfmt_misc/status", "-1", 0);
663996b3
MS
194
195 STRV_FOREACH(f, files) {
196 k = apply_file(*f, true);
197 if (k < 0 && r == 0)
198 r = k;
199 }
200 }
201
202finish:
203 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
204}