1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
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.
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.
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/>.
35 #include "conf-files.h"
37 static const char conf_file_dirs[] =
40 "/usr/local/lib/binfmt.d\0"
47 static int delete_rule(const char *rule) {
48 _cleanup_free_ char *x = NULL, *fn = NULL;
57 e = strchrnul(x+1, x[0]);
60 fn = strappend("/proc/sys/fs/binfmt_misc/", x+1);
64 return write_one_line_file(fn, "-1");
67 static int apply_rule(const char *rule) {
72 r = write_one_line_file("/proc/sys/fs/binfmt_misc/register", rule);
74 log_error("Failed to add binary format: %s", strerror(-r));
81 static int apply_file(const char *path, bool ignore_enoent) {
82 _cleanup_fclose_ FILE *f = NULL;
87 r = search_and_fopen_nulstr(path, "re", conf_file_dirs, &f);
89 if (ignore_enoent && r == -ENOENT)
92 log_error("Failed to open file '%s', ignoring: %s", path, strerror(-r));
96 log_debug("apply: %s\n", path);
101 if (!fgets(l, sizeof(l), f)) {
105 log_error("Failed to read file '%s', ignoring: %m", path);
112 if (strchr(COMMENTS, *p))
123 static int help(void) {
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);
133 static int parse_argv(int argc, char *argv[]) {
135 static const struct option options[] = {
136 { "help", no_argument, NULL, 'h' },
145 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
157 log_error("Unknown option code %c", c);
165 int main(int argc, char *argv[]) {
168 r = parse_argv(argc, argv);
170 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
172 log_set_target(LOG_TARGET_AUTO);
173 log_parse_environment();
183 for (i = optind; i < argc; i++) {
184 k = apply_file(argv[i], false);
189 _cleanup_strv_free_ char **files = NULL;
192 r = conf_files_list_nulstr(&files, ".conf", NULL, conf_file_dirs);
194 log_error("Failed to enumerate binfmt.d files: %s", strerror(-r));
198 /* Flush out all rules */
199 write_one_line_file("/proc/sys/fs/binfmt_misc/status", "-1");
201 STRV_FOREACH(f, files) {
202 k = apply_file(*f, true);
209 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;