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