/*
* libkmod - interface to kernel module operations
*
- * Copyright (C) 2011-2012 ProFUSION embedded systems
+ * Copyright (C) 2011-2013 ProFUSION embedded systems
+ * Copyright (C) 2013 Intel Corporation. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
+#include <ctype.h>
+#include <dirent.h>
+#include <errno.h>
+#include <stdarg.h>
+#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
-#include <stddef.h>
-#include <stdarg.h>
-#include <unistd.h>
-#include <errno.h>
#include <string.h>
-#include <ctype.h>
+#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
-#include <dirent.h>
+
+#include <shared/util.h>
#include "libkmod.h"
-#include "libkmod-private.h"
+#include "libkmod-internal.h"
struct kmod_alias {
char *name;
return dep->post;
}
-/*
- * Replace dashes with underscores.
- * Dashes inside character range patterns (e.g. [0-9]) are left unchanged.
- */
-static char *underscores(struct kmod_ctx *ctx, char *s)
-{
- unsigned int i;
-
- if (!s)
- return NULL;
-
- for (i = 0; s[i]; i++) {
- switch (s[i]) {
- case '-':
- s[i] = '_';
- break;
-
- case ']':
- INFO(ctx, "Unmatched bracket in %s\n", s);
- break;
-
- case '[':
- i += strcspn(&s[i], "]");
- if (!s[i])
- INFO(ctx, "Unmatched bracket in %s\n", s);
- break;
- }
- }
- return s;
-}
-
static int kmod_config_add_command(struct kmod_config *config,
const char *modname,
const char *command,
const char *command_name,
struct kmod_list **list)
{
- struct kmod_command *cmd;
+ _cleanup_free_ struct kmod_command *cmd;
struct kmod_list *l;
size_t modnamelen = strlen(modname) + 1;
size_t commandlen = strlen(command) + 1;
command);
cmd = malloc(sizeof(*cmd) + modnamelen + commandlen);
- if (cmd == NULL)
- goto oom_error_init;
+ if (!cmd)
+ return -ENOMEM;
cmd->command = sizeof(*cmd) + modnamelen + (char *)cmd;
memcpy(cmd->modname, modname, modnamelen);
memcpy(cmd->command, command, commandlen);
l = kmod_list_append(*list, cmd);
- if (l == NULL)
- goto oom_error;
+ if (!l)
+ return -ENOMEM;
*list = l;
+ cmd = NULL;
return 0;
-
-oom_error:
- free(cmd);
-oom_error_init:
- ERR(config->ctx, "out-of-memory\n");
- return -ENOMEM;
}
static void kmod_config_free_command(struct kmod_config *config,
static int kmod_config_add_options(struct kmod_config *config,
const char *modname, const char *options)
{
- struct kmod_options *opt;
+ _cleanup_free_ struct kmod_options *opt;
struct kmod_list *list;
size_t modnamelen = strlen(modname) + 1;
size_t optionslen = strlen(options) + 1;
DBG(config->ctx, "modname='%s' options='%s'\n", modname, options);
opt = malloc(sizeof(*opt) + modnamelen + optionslen);
- if (opt == NULL)
- goto oom_error_init;
+ if (!opt)
+ return -ENOMEM;
opt->options = sizeof(*opt) + modnamelen + (char *)opt;
strchr_replace(opt->options, '\t', ' ');
list = kmod_list_append(config->options, opt);
- if (list == NULL)
- goto oom_error;
+ if (!list)
+ return -ENOMEM;
+ opt = NULL;
config->options = list;
return 0;
-
-oom_error:
- free(opt);
-oom_error_init:
- ERR(config->ctx, "out-of-memory\n");
- return -ENOMEM;
}
static void kmod_config_free_options(struct kmod_config *config,
static int kmod_config_add_alias(struct kmod_config *config,
const char *name, const char *modname)
{
- struct kmod_alias *alias;
+ _cleanup_free_ struct kmod_alias *alias;
struct kmod_list *list;
size_t namelen = strlen(name) + 1, modnamelen = strlen(modname) + 1;
alias = malloc(sizeof(*alias) + namelen + modnamelen);
if (!alias)
- goto oom_error_init;
+ return -ENOMEM;
alias->name = sizeof(*alias) + modnamelen + (char *)alias;
list = kmod_list_append(config->aliases, alias);
if (!list)
- goto oom_error;
+ return -ENOMEM;
+ alias = NULL;
config->aliases = list;
return 0;
-
-oom_error:
- free(alias);
-oom_error_init:
- ERR(config->ctx, "out-of-memory name=%s modname=%s\n", name, modname);
- return -ENOMEM;
}
static void kmod_config_free_alias(struct kmod_config *config,
static int kmod_config_add_blacklist(struct kmod_config *config,
const char *modname)
{
- char *p;
+ _cleanup_free_ char *p;
struct kmod_list *list;
DBG(config->ctx, "modname=%s\n", modname);
p = strdup(modname);
if (!p)
- goto oom_error_init;
+ return -ENOMEM;
list = kmod_list_append(config->blacklists, p);
if (!list)
- goto oom_error;
+ return -ENOMEM;
+
+ p = NULL;
config->blacklists = list;
return 0;
-
-oom_error:
- free(p);
-oom_error_init:
- ERR(config->ctx, "out-of-memory modname=%s\n", modname);
- return -ENOMEM;
}
static void kmod_config_free_blacklist(struct kmod_config *config,
static void kcmdline_parse_result(struct kmod_config *config, char *modname,
char *param, char *value)
{
- if (modname == NULL || param == NULL || value == NULL)
+ if (modname == NULL || param == NULL)
return;
DBG(config->ctx, "%s %s\n", modname, param);
kmod_config_add_blacklist(config, t);
}
} else {
- kmod_config_add_options(config,
- underscores(config->ctx, modname), param);
+ if (underscores(modname) < 0) {
+ ERR(config->ctx, "Ignoring bad option on kernel command line while parsing module name: '%s'\n",
+ modname);
+ }
+ kmod_config_add_options(config, modname, param);
}
}
{
char buf[KCMD_LINE_SIZE];
int fd, err;
- char *p, *modname, *param = NULL, *value = NULL;
+ char *p, *modname, *param = NULL, *value = NULL, is_module = 1;
fd = open("/proc/cmdline", O_RDONLY|O_CLOEXEC);
if (fd < 0) {
switch (*p) {
case ' ':
*p = '\0';
- kcmdline_parse_result(config, modname, param, value);
+ if (is_module)
+ kcmdline_parse_result(config, modname, param, value);
param = value = NULL;
modname = p + 1;
+ is_module = 1;
break;
case '.':
- *p = '\0';
- param = p + 1;
+ if (param == NULL) {
+ *p = '\0';
+ param = p + 1;
+ }
break;
case '=':
if (param != NULL)
value = p + 1;
+ else
+ is_module = 0;
break;
}
}
*p = '\0';
- kcmdline_parse_result(config, modname, param, value);
+ if (is_module)
+ kcmdline_parse_result(config, modname, param, value);
return 0;
}
return err;
}
- while ((line = getline_wrapped(fp, &linenum)) != NULL) {
+ while ((line = freadline_wrapped(fp, &linenum)) != NULL) {
char *cmd, *saveptr;
if (line[0] == '\0' || line[0] == '#')
char *alias = strtok_r(NULL, "\t ", &saveptr);
char *modname = strtok_r(NULL, "\t ", &saveptr);
- if (alias == NULL || modname == NULL)
+ if (underscores(alias) < 0 || underscores(modname) < 0)
goto syntax_error;
- kmod_config_add_alias(config,
- underscores(ctx, alias),
- underscores(ctx, modname));
+ kmod_config_add_alias(config, alias, modname);
} else if (streq(cmd, "blacklist")) {
char *modname = strtok_r(NULL, "\t ", &saveptr);
- if (modname == NULL)
+ if (underscores(modname) < 0)
goto syntax_error;
- kmod_config_add_blacklist(config,
- underscores(ctx, modname));
+ kmod_config_add_blacklist(config, modname);
} else if (streq(cmd, "options")) {
char *modname = strtok_r(NULL, "\t ", &saveptr);
char *options = strtok_r(NULL, "\0", &saveptr);
- if (modname == NULL || options == NULL)
+ if (underscores(modname) < 0 || options == NULL)
goto syntax_error;
- kmod_config_add_options(config,
- underscores(ctx, modname),
- options);
+ kmod_config_add_options(config, modname, options);
} else if (streq(cmd, "install")) {
char *modname = strtok_r(NULL, "\t ", &saveptr);
char *installcmd = strtok_r(NULL, "\0", &saveptr);
- if (modname == NULL || installcmd == NULL)
+ if (underscores(modname) < 0 || installcmd == NULL)
goto syntax_error;
- kmod_config_add_command(config,
- underscores(ctx, modname),
- installcmd,
+ kmod_config_add_command(config, modname, installcmd,
cmd, &config->install_commands);
} else if (streq(cmd, "remove")) {
char *modname = strtok_r(NULL, "\t ", &saveptr);
char *removecmd = strtok_r(NULL, "\0", &saveptr);
- if (modname == NULL || removecmd == NULL)
+ if (underscores(modname) < 0 || removecmd == NULL)
goto syntax_error;
- kmod_config_add_command(config,
- underscores(ctx, modname),
- removecmd,
+ kmod_config_add_command(config, modname, removecmd,
cmd, &config->remove_commands);
} else if (streq(cmd, "softdep")) {
char *modname = strtok_r(NULL, "\t ", &saveptr);
char *softdeps = strtok_r(NULL, "\0", &saveptr);
- if (modname == NULL || softdeps == NULL)
+ if (underscores(modname) < 0 || softdeps == NULL)
goto syntax_error;
- kmod_config_add_softdep(config,
- underscores(ctx, modname),
- softdeps);
+ kmod_config_add_softdep(config, modname, softdeps);
} else if (streq(cmd, "include")
|| streq(cmd, "config")) {
ERR(ctx, "%s: command %s is deprecated and not parsed anymore\n",
DIR *d;
int err;
struct stat st;
+ struct dirent *dent;
if (stat(path, &st) != 0) {
err = -errno;
*path_stamp = stat_mstamp(&st);
- if (S_ISREG(st.st_mode)) {
+ if (!S_ISDIR(st.st_mode)) {
conf_files_insert_sorted(ctx, list, path, NULL);
return 0;
- } if (!S_ISDIR(st.st_mode)) {
- ERR(ctx, "unsupported file mode %s: %#x\n",
- path, st.st_mode);
- return -EINVAL;
}
d = opendir(path);
return -EINVAL;
}
- for (;;) {
- struct dirent ent, *entp;
-
- err = readdir_r(d, &ent, &entp);
- if (err != 0) {
- ERR(ctx, "reading entry %s\n", strerror(-err));
- goto fail_read;
- }
-
- if (entp == NULL)
- break;
-
- if (conf_files_filter_out(ctx, d, path, entp->d_name))
+ for (dent = readdir(d); dent != NULL; dent = readdir(d)) {
+ if (conf_files_filter_out(ctx, d, path, dent->d_name))
continue;
- conf_files_insert_sorted(ctx, list, path, entp->d_name);
+ conf_files_insert_sorted(ctx, list, path, dent->d_name);
}
closedir(d);
return 0;
-
-fail_read:
- closedir(d);
- return err;
}
int kmod_config_new(struct kmod_ctx *ctx, struct kmod_config **p_config,
struct kmod_list *path_list = NULL;
size_t i;
+ conf_files_insert_sorted(ctx, &list, kmod_get_dirname(ctx), "modules.softdep");
+
for (i = 0; config_paths[i] != NULL; i++) {
const char *path = config_paths[i];
unsigned long long path_stamp = 0;
enum config_type type)
{
struct kmod_config_iter *iter = calloc(1, sizeof(*iter));
+ const struct kmod_config *config = kmod_get_config(ctx);
if (iter == NULL)
return NULL;
switch (type) {
case CONFIG_TYPE_BLACKLIST:
- iter->list = kmod_get_blacklists(ctx);
+ iter->list = config->blacklists;
iter->get_key = kmod_blacklist_get_modname;
break;
case CONFIG_TYPE_INSTALL:
- iter->list = kmod_get_install_commands(ctx);
+ iter->list = config->install_commands;
iter->get_key = kmod_command_get_modname;
iter->get_value = kmod_command_get_command;
break;
case CONFIG_TYPE_REMOVE:
- iter->list = kmod_get_remove_commands(ctx);
+ iter->list = config->remove_commands;
iter->get_key = kmod_command_get_modname;
iter->get_value = kmod_command_get_command;
break;
case CONFIG_TYPE_ALIAS:
- iter->list = kmod_get_aliases(ctx);
+ iter->list = config->aliases;
iter->get_key = kmod_alias_get_name;
iter->get_value = kmod_alias_get_modname;
break;
case CONFIG_TYPE_OPTION:
- iter->list = kmod_get_options(ctx);
+ iter->list = config->options;
iter->get_key = kmod_option_get_modname;
iter->get_value = kmod_option_get_options;
break;
case CONFIG_TYPE_SOFTDEP:
- iter->list = kmod_get_softdeps(ctx);
+ iter->list = config->softdeps;
iter->get_key = kmod_softdep_get_name;
iter->get_value = softdep_get_plain_softdep;
iter->intermediate = true;