2011-11-30 03:07:43 +07:00
|
|
|
/*
|
|
|
|
* libkmod - interface to kernel module operations
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011 ProFUSION embedded systems
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation version 2.1.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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 <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
|
|
|
|
#include "libkmod.h"
|
|
|
|
#include "libkmod-private.h"
|
|
|
|
|
|
|
|
static const char *config_files[] = {
|
|
|
|
"/run/modprobe.d",
|
|
|
|
"/etc/modprobe.d",
|
|
|
|
"/lib/modprobe.d",
|
|
|
|
};
|
|
|
|
|
|
|
|
struct kmod_alias {
|
|
|
|
char *name;
|
|
|
|
char *modname;
|
|
|
|
};
|
|
|
|
|
2011-12-01 03:18:13 +07:00
|
|
|
const char *kmod_alias_get_name(const struct kmod_list *l) {
|
2011-12-03 05:24:07 +07:00
|
|
|
const struct kmod_alias *alias = l->data;
|
2011-12-01 03:18:13 +07:00
|
|
|
return alias->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *kmod_alias_get_modname(const struct kmod_list *l) {
|
2011-12-03 05:24:07 +07:00
|
|
|
const struct kmod_alias *alias = l->data;
|
2011-12-01 03:18:13 +07:00
|
|
|
return alias->modname;
|
|
|
|
}
|
|
|
|
|
2011-12-03 06:40:22 +07:00
|
|
|
static int kmod_config_add_alias(struct kmod_config *config,
|
|
|
|
const char *name, const char *modname)
|
2011-11-30 03:07:43 +07:00
|
|
|
{
|
|
|
|
struct kmod_alias *alias;
|
2011-12-03 06:40:22 +07:00
|
|
|
struct kmod_list *list;
|
2011-11-30 03:07:43 +07:00
|
|
|
|
2011-12-03 06:40:22 +07:00
|
|
|
DBG(config->ctx, "name=%s modname=%s\n", name, modname);
|
2011-11-30 03:07:43 +07:00
|
|
|
|
|
|
|
alias = malloc(sizeof(*alias));
|
2011-12-03 06:40:22 +07:00
|
|
|
if (!alias)
|
|
|
|
goto oom_error_init;
|
2011-11-30 03:07:43 +07:00
|
|
|
alias->name = strdup(name);
|
|
|
|
alias->modname = strdup(modname);
|
2011-12-03 06:40:22 +07:00
|
|
|
if (!alias->name || !alias->modname)
|
|
|
|
goto oom_error;
|
2011-11-30 03:07:43 +07:00
|
|
|
|
2011-12-03 06:40:22 +07:00
|
|
|
list = kmod_list_append(config->aliases, alias);
|
|
|
|
if (!list)
|
|
|
|
goto oom_error;
|
|
|
|
config->aliases = list;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
oom_error:
|
|
|
|
free(alias->name);
|
|
|
|
free(alias->modname);
|
|
|
|
free(alias);
|
|
|
|
oom_error_init:
|
|
|
|
ERR(config->ctx, "out-of-memory name=%s modname=%s\n", name, modname);
|
|
|
|
return -ENOMEM;
|
2011-11-30 03:07:43 +07:00
|
|
|
}
|
|
|
|
|
2011-12-03 06:40:22 +07:00
|
|
|
static void kmod_config_free_alias(struct kmod_config *config, struct kmod_list *l)
|
2011-11-30 03:07:43 +07:00
|
|
|
{
|
|
|
|
struct kmod_alias *alias = l->data;
|
|
|
|
|
|
|
|
free(alias->modname);
|
|
|
|
free(alias->name);
|
|
|
|
free(alias);
|
|
|
|
|
2011-12-03 06:40:22 +07:00
|
|
|
config->aliases = kmod_list_remove(l);
|
2011-11-30 03:07:43 +07:00
|
|
|
}
|
|
|
|
|
2011-12-03 06:40:22 +07:00
|
|
|
static int kmod_config_add_blacklist(struct kmod_config *config,
|
2011-11-30 03:48:02 +07:00
|
|
|
const char *modname)
|
|
|
|
{
|
|
|
|
char *p;
|
2011-12-03 06:40:22 +07:00
|
|
|
struct kmod_list *list;
|
2011-11-30 03:48:02 +07:00
|
|
|
|
2011-12-03 06:40:22 +07:00
|
|
|
DBG(config->ctx, "modname=%s\n", modname);
|
2011-11-30 03:48:02 +07:00
|
|
|
|
|
|
|
p = strdup(modname);
|
2011-12-03 06:40:22 +07:00
|
|
|
if (!p)
|
|
|
|
goto oom_error_init;
|
|
|
|
|
|
|
|
list = kmod_list_append(config->blacklists, p);
|
|
|
|
if (!list)
|
|
|
|
goto oom_error;
|
|
|
|
config->blacklists = list;
|
|
|
|
return 0;
|
2011-11-30 03:48:02 +07:00
|
|
|
|
2011-12-03 06:40:22 +07:00
|
|
|
oom_error:
|
|
|
|
free(p);
|
|
|
|
oom_error_init:
|
|
|
|
ERR(config->ctx, "out-of-memory modname=%s\n", modname);
|
|
|
|
return -ENOMEM;
|
2011-11-30 03:48:02 +07:00
|
|
|
}
|
|
|
|
|
2011-12-03 06:40:22 +07:00
|
|
|
static void kmod_config_free_blacklist(struct kmod_config *config,
|
2011-11-30 03:48:02 +07:00
|
|
|
struct kmod_list *l)
|
|
|
|
{
|
|
|
|
free(l->data);
|
2011-12-03 06:40:22 +07:00
|
|
|
config->blacklists = kmod_list_remove(l);
|
2011-11-30 03:48:02 +07:00
|
|
|
}
|
|
|
|
|
2011-12-03 06:40:22 +07:00
|
|
|
static int kmod_config_parse(struct kmod_config *config, const char *filename)
|
2011-11-30 03:07:43 +07:00
|
|
|
{
|
2011-12-03 06:40:22 +07:00
|
|
|
struct kmod_ctx *ctx = config->ctx;
|
2011-11-30 03:07:43 +07:00
|
|
|
char *line;
|
|
|
|
FILE *fp;
|
|
|
|
unsigned int linenum;
|
|
|
|
|
|
|
|
DBG(ctx, "%s\n", filename);
|
|
|
|
|
|
|
|
fp = fopen(filename, "r");
|
|
|
|
if (fp == NULL)
|
|
|
|
return errno;
|
|
|
|
|
|
|
|
while ((line = getline_wrapped(fp, &linenum)) != NULL) {
|
2011-12-02 03:59:54 +07:00
|
|
|
char *cmd, *saveptr;
|
2011-11-30 03:07:43 +07:00
|
|
|
|
|
|
|
if (line[0] == '\0' || line[0] == '#')
|
|
|
|
goto done_next;
|
|
|
|
|
2011-12-02 03:59:54 +07:00
|
|
|
cmd = strtok_r(line, "\t ", &saveptr);
|
2011-11-30 03:07:43 +07:00
|
|
|
if (cmd == NULL)
|
|
|
|
goto done_next;
|
|
|
|
|
|
|
|
if (!strcmp(cmd, "alias")) {
|
2011-12-02 03:59:54 +07:00
|
|
|
char *alias = strtok_r(NULL, "\t ", &saveptr);
|
|
|
|
char *modname = strtok_r(NULL, "\t ", &saveptr);
|
2011-11-30 03:07:43 +07:00
|
|
|
|
|
|
|
if (alias == NULL || modname == NULL)
|
|
|
|
goto syntax_error;
|
|
|
|
|
2011-12-03 06:40:22 +07:00
|
|
|
kmod_config_add_alias(config,
|
2011-11-30 11:14:57 +07:00
|
|
|
underscores(ctx, alias),
|
|
|
|
underscores(ctx, modname));
|
2011-11-30 03:48:02 +07:00
|
|
|
} else if (!strcmp(cmd, "blacklist")) {
|
2011-12-02 03:59:54 +07:00
|
|
|
char *modname = strtok_r(NULL, "\t ", &saveptr);
|
2011-11-30 03:48:02 +07:00
|
|
|
|
|
|
|
if (modname == NULL)
|
|
|
|
goto syntax_error;
|
|
|
|
|
2011-12-03 06:40:22 +07:00
|
|
|
kmod_config_add_blacklist(config,
|
2011-12-03 13:05:22 +07:00
|
|
|
underscores(ctx, modname));
|
2011-11-30 03:07:43 +07:00
|
|
|
} else if (!strcmp(cmd, "include") || !strcmp(cmd, "options")
|
|
|
|
|| !strcmp(cmd, "install")
|
|
|
|
|| !strcmp(cmd, "remove")
|
|
|
|
|| !strcmp(cmd, "softdep")
|
|
|
|
|| !strcmp(cmd, "config")) {
|
2011-11-30 03:48:02 +07:00
|
|
|
INFO(ctx, "%s: command %s not implemented yet\n",
|
|
|
|
filename, cmd);
|
2011-11-30 03:07:43 +07:00
|
|
|
} else {
|
|
|
|
syntax_error:
|
|
|
|
ERR(ctx, "%s line %u: ignoring bad line starting with '%s'\n",
|
|
|
|
filename, linenum, cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
done_next:
|
|
|
|
free(line);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-03 06:40:22 +07:00
|
|
|
void kmod_config_free(struct kmod_config *config)
|
2011-11-30 03:07:43 +07:00
|
|
|
{
|
|
|
|
while (config->aliases)
|
2011-12-03 06:40:22 +07:00
|
|
|
kmod_config_free_alias(config, config->aliases);
|
2011-11-30 03:48:02 +07:00
|
|
|
|
|
|
|
while (config->blacklists)
|
2011-12-03 06:40:22 +07:00
|
|
|
kmod_config_free_blacklist(config, config->blacklists);
|
|
|
|
|
|
|
|
free(config);
|
2011-11-30 03:07:43 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool conf_files_filter(struct kmod_ctx *ctx, const char *path,
|
|
|
|
const char *fn)
|
|
|
|
{
|
|
|
|
size_t len = strlen(fn);
|
|
|
|
|
|
|
|
if (fn[0] == '.')
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (len < 6 || (strcmp(&fn[len - 5], ".conf") != 0
|
|
|
|
&& strcmp(&fn[len - 6], ".alias"))) {
|
|
|
|
INFO(ctx, "All config files need .conf: %s/%s, "
|
|
|
|
"it will be ignored in a future release\n",
|
|
|
|
path, fn);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int conf_files_list(struct kmod_ctx *ctx, struct kmod_list **list,
|
|
|
|
const char *path, size_t *n)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
DIR *d;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (stat(path, &st) < 0)
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
if (!S_ISDIR(st.st_mode)) {
|
|
|
|
*list = kmod_list_append(*list, (void *)path);
|
|
|
|
*n += 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
d = opendir(path);
|
|
|
|
if (d == NULL) {
|
|
|
|
err = errno;
|
|
|
|
ERR(ctx, "%m\n");
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
struct dirent ent, *entp;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
err = readdir_r(d, &ent, &entp);
|
|
|
|
if (err != 0) {
|
|
|
|
err = -err;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entp == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (conf_files_filter(ctx, path, entp->d_name) == 1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (asprintf(&p, "%s/%s", path, entp->d_name) < 0) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
DBG(ctx, "%s\n", p);
|
|
|
|
|
|
|
|
*list = kmod_list_append(*list, p);
|
|
|
|
*n += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
finish:
|
|
|
|
closedir(d);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int base_cmp(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const char *s1, *s2;
|
|
|
|
|
|
|
|
s1 = *(char * const *)a;
|
|
|
|
s2 = *(char * const *)b;
|
|
|
|
|
|
|
|
return strcmp(basename(s1), basename(s2));
|
|
|
|
}
|
|
|
|
|
2011-12-03 06:40:22 +07:00
|
|
|
int kmod_config_new(struct kmod_ctx *ctx, struct kmod_config **p_config)
|
2011-11-30 03:07:43 +07:00
|
|
|
{
|
2011-12-03 06:40:22 +07:00
|
|
|
struct kmod_config *config;
|
2011-11-30 03:07:43 +07:00
|
|
|
size_t i, n = 0;
|
2011-12-03 05:24:07 +07:00
|
|
|
const char **files;
|
2011-11-30 03:07:43 +07:00
|
|
|
int err = 0;
|
|
|
|
struct kmod_list *list = NULL, *l;
|
|
|
|
|
2011-12-03 06:40:22 +07:00
|
|
|
*p_config = config = calloc(1, sizeof(struct kmod_config));
|
2011-12-03 13:05:22 +07:00
|
|
|
if (config == NULL)
|
2011-12-03 06:40:22 +07:00
|
|
|
return -ENOMEM;
|
2011-12-03 13:05:22 +07:00
|
|
|
|
2011-12-03 06:40:22 +07:00
|
|
|
config->ctx = ctx;
|
|
|
|
|
2011-11-30 03:07:43 +07:00
|
|
|
for (i = 0; i < ARRAY_SIZE(config_files); i++)
|
|
|
|
conf_files_list(ctx, &list, config_files[i], &n);
|
|
|
|
|
|
|
|
files = malloc(sizeof(char *) * n);
|
|
|
|
if (files == NULL) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
kmod_list_foreach(l, list) {
|
|
|
|
files[i] = l->data;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
qsort(files, n, sizeof(char *), base_cmp);
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++)
|
2011-12-03 06:40:22 +07:00
|
|
|
kmod_config_parse(config, files[i]);
|
2011-11-30 03:07:43 +07:00
|
|
|
|
|
|
|
finish:
|
|
|
|
free(files);
|
|
|
|
|
|
|
|
while (list) {
|
|
|
|
free(list->data);
|
|
|
|
list = kmod_list_remove(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|