modules-load.c revision 4cfa2c999dea269ddc646bfeba6c7f1021a73843
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/***
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte This file is part of systemd.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Copyright 2010 Lennart Poettering
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte systemd is free software; you can redistribute it and/or modify it
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte under the terms of the GNU General Public License as published by
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the Free Software Foundation; either version 2 of the License, or
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (at your option) any later version.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte systemd is distributed in the hope that it will be useful, but
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte WITHOUT ANY WARRANTY; without even the implied warranty of
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte General Public License for more details.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte You should have received a copy of the GNU General Public License
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte along with systemd; If not, see <http://www.gnu.org/licenses/>.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte***/
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <unistd.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <fcntl.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <errno.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <string.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sys/stat.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <limits.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <dirent.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include "log.h"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include "util.h"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include "strv.h"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint main(int argc, char *argv[]) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int r = EXIT_FAILURE;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char **arguments = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte unsigned n_arguments = 0, n_allocated = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char **files, **fn;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (argc > 1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte log_error("This program takes no argument.");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return EXIT_FAILURE;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte log_set_target(LOG_TARGET_AUTO);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte log_parse_environment();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte log_open();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte umask(0022);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!(arguments = strv_new("/sbin/modprobe", "-sab", "--", NULL))) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte log_error("Failed to allocate string array");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto finish;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n_arguments = n_allocated = 3;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (conf_files_list(&files, ".conf",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "/run/modules-load.d",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "/etc/modules-load.d",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "/usr/local/lib/modules-load.d",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "/usr/lib/modules-load.d",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "/lib/modules-load.d",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte NULL) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte log_error("Failed to enumerate modules-load.d files: %s", strerror(-r));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto finish;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte r = EXIT_SUCCESS;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte STRV_FOREACH(fn, files) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte FILE *f;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte f = fopen(*fn, "re");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!f) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (errno == ENOENT)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte log_error("Failed to open %s: %m", *fn);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte r = EXIT_FAILURE;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte log_debug("apply: %s\n", *fn);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (;;) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char line[LINE_MAX], *l, *t;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!(fgets(line, sizeof(line), f)))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte l = strstrip(line);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (*l == '#' || *l == 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!(t = strdup(l))) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte log_error("Failed to allocate module name.");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (n_arguments >= n_allocated) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char **a;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte unsigned m;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte m = MAX(16U, n_arguments*2);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!(a = realloc(arguments, sizeof(char*) * (m+1)))) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte log_error("Failed to increase module array size.");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(t);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte r = EXIT_FAILURE;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte arguments = a;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n_allocated = m;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte arguments[n_arguments++] = t;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ferror(f)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte r = EXIT_FAILURE;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte log_error("Failed to read from file: %m");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fclose(f);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte strv_free(files);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortefinish:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (n_arguments > 3) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte arguments[n_arguments] = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte strv_uniq(arguments);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte execv("/sbin/modprobe", arguments);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte log_error("Failed to execute /sbin/modprobe: %m");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte r = EXIT_FAILURE;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte strv_free(arguments);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return r;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte