]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/binfmt/binfmt.c
shared: conf-files - add root parameter
[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
30 #include "log.h"
31 #include "hashmap.h"
32 #include "strv.h"
33 #include "util.h"
34 #include "conf-files.h"
35
36 static int delete_rule(const char *rule) {
37 char *x, *fn = NULL, *e;
38 int r;
39
40 assert(rule[0]);
41
42 if (!(x = strdup(rule)))
43 return log_oom();
44
45 e = strchrnul(x+1, x[0]);
46 *e = 0;
47
48 asprintf(&fn, "/proc/sys/fs/binfmt_misc/%s", x+1);
49 free(x);
50
51 if (!fn)
52 return log_oom();
53
54 r = write_one_line_file(fn, "-1");
55 free(fn);
56
57 return r;
58 }
59
60 static int apply_rule(const char *rule) {
61 int r;
62
63 delete_rule(rule);
64
65 if ((r = write_one_line_file("/proc/sys/fs/binfmt_misc/register", rule)) < 0) {
66 log_error("Failed to add binary format: %s", strerror(-r));
67 return r;
68 }
69
70 return 0;
71 }
72
73 static int apply_file(const char *path, bool ignore_enoent) {
74 FILE *f;
75 int r = 0;
76
77 assert(path);
78
79 if (!(f = fopen(path, "re"))) {
80 if (ignore_enoent && errno == ENOENT)
81 return 0;
82
83 log_error("Failed to open file '%s', ignoring: %m", path);
84 return -errno;
85 }
86
87 log_debug("apply: %s\n", path);
88 while (!feof(f)) {
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 log_error("Failed to read file '%s', ignoring: %m", path);
97 r = -errno;
98 goto finish;
99 }
100
101 p = strstrip(l);
102
103 if (!*p)
104 continue;
105
106 if (strchr(COMMENTS, *p))
107 continue;
108
109 if ((k = apply_rule(p)) < 0 && r == 0)
110 r = k;
111 }
112
113 finish:
114 fclose(f);
115
116 return r;
117 }
118
119 int main(int argc, char *argv[]) {
120 int r = 0;
121
122 log_set_target(LOG_TARGET_AUTO);
123 log_parse_environment();
124 log_open();
125
126 umask(0022);
127
128 if (argc > 1) {
129 int i;
130
131 for (i = 1; i < argc; i++) {
132 int k;
133
134 k = apply_file(argv[i], false);
135 if (k < 0 && r == 0)
136 r = k;
137 }
138 } else {
139 char **files, **f;
140
141 r = conf_files_list(&files, ".conf", NULL,
142 "/etc/binfmt.d",
143 "/run/binfmt.d",
144 "/usr/local/lib/binfmt.d",
145 "/usr/lib/binfmt.d",
146 #ifdef HAVE_SPLIT_USR
147 "/lib/binfmt.d",
148 #endif
149 NULL);
150 if (r < 0) {
151 log_error("Failed to enumerate binfmt.d files: %s", strerror(-r));
152 goto finish;
153 }
154
155 /* Flush out all rules */
156 write_one_line_file("/proc/sys/fs/binfmt_misc/status", "-1");
157
158 STRV_FOREACH(f, files) {
159 int k;
160
161 k = apply_file(*f, true);
162 if (k < 0 && r == 0)
163 r = k;
164 }
165
166 strv_free(files);
167 }
168 finish:
169 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
170 }