]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/binfmt.c
binfmt: add binfmt tool to set up binfmt_misc at boot
[thirdparty/systemd.git] / src / 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 General Public License as published by
10 the Free Software Foundation; either version 2 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 General Public License for more details.
17
18 You should have received a copy of the GNU 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
29 #include "log.h"
30 #include "util.h"
31
32 static int delete_rule(const char *rule) {
33 char *x, *fn, *e;
34 int r;
35
36 assert(rule[0]);
37
38 if (!(x = strdup(rule)))
39 return -ENOMEM;
40
41 e = strchrnul(x+1, x[0]);
42 *e = 0;
43
44 asprintf(&fn, "/proc/sys/fs/binfmt_misc/%s", x+1);
45 free(x);
46
47 if (!fn)
48 return -ENOMEM;
49
50 r = write_one_line_file(fn, "-1");
51 free(fn);
52
53 return r;
54 }
55
56 static int apply_rule(const char *rule) {
57 int r;
58
59 delete_rule(rule);
60
61 if ((r = write_one_line_file("/proc/sys/fs/binfmt_misc/register", rule)) < 0) {
62 log_error("Failed to add binary format: %s", strerror(-r));
63 return r;
64 }
65
66 return 0;
67 }
68
69 static int apply_file(const char *path, bool ignore_enoent) {
70 FILE *f;
71 int r = 0;
72
73 assert(path);
74
75 if (!(f = fopen(path, "re"))) {
76 if (ignore_enoent && errno == ENOENT)
77 return 0;
78
79 log_error("Failed to open file '%s', ignoring: %m", path);
80 return -errno;
81 }
82
83 while (!feof(f)) {
84 char l[LINE_MAX], *p;
85 int k;
86
87 if (!fgets(l, sizeof(l), f)) {
88 if (feof(f))
89 break;
90
91 log_error("Failed to read file '%s', ignoring: %m", path);
92 r = -errno;
93 goto finish;
94 }
95
96 p = strstrip(l);
97
98 if (!*p)
99 continue;
100
101 if (strchr(COMMENTS, *p))
102 continue;
103
104 if ((k = apply_rule(p)) < 0 && r == 0)
105 r = k;
106 }
107
108 finish:
109 fclose(f);
110
111 return r;
112 }
113
114 static int scandir_filter(const struct dirent *d) {
115 assert(d);
116
117 if (ignore_file(d->d_name))
118 return 0;
119
120 if (d->d_type != DT_REG &&
121 d->d_type != DT_LNK &&
122 d->d_type != DT_UNKNOWN)
123 return 0;
124
125 return endswith(d->d_name, ".conf");
126 }
127
128 static int apply_tree(const char *path) {
129 struct dirent **de = NULL;
130 int n, i, r = 0;
131
132 if ((n = scandir(path, &de, scandir_filter, alphasort)) < 0) {
133
134 if (errno == ENOENT)
135 return 0;
136
137 log_error("Failed to enumerate %s files: %m", path);
138 return -errno;
139 }
140
141 for (i = 0; i < n; i++) {
142 char *fn;
143 int k;
144
145 k = asprintf(&fn, "%s/%s", path, de[i]->d_name);
146 free(de[i]);
147
148 if (k < 0) {
149 log_error("Failed to allocate file name.");
150
151 if (r == 0)
152 r = -ENOMEM;
153 continue;
154 }
155
156 if ((k = apply_file(fn, true)) < 0 && r == 0)
157 r = k;
158 }
159
160 free(de);
161
162 return r;
163 }
164
165 int main(int argc, char *argv[]) {
166 int r = 0;
167
168 if (argc > 2) {
169 log_error("This program expects one or no arguments.");
170 return EXIT_FAILURE;
171 }
172
173 log_set_target(LOG_TARGET_AUTO);
174 log_parse_environment();
175 log_open();
176
177 if (argc > 1)
178 r = apply_file(argv[1], false);
179 else {
180 /* Flush out all rules */
181 write_one_line_file("/proc/sys/fs/binfmt_misc/status", "-1");
182
183 r = apply_tree("/etc/binfmt.d");
184 }
185
186 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
187 }