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