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
|
2011-12-13 03:24:35 +07:00
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2011-11-30 03:07:43 +07:00
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
|
|
|
|
struct kmod_alias {
|
|
|
|
char *name;
|
2011-12-05 02:32:44 +07:00
|
|
|
char modname[];
|
2011-11-30 03:07:43 +07:00
|
|
|
};
|
|
|
|
|
2011-12-07 12:18:57 +07:00
|
|
|
struct kmod_options {
|
|
|
|
char *options;
|
|
|
|
char modname[];
|
|
|
|
};
|
|
|
|
|
2011-12-07 20:31:28 +07:00
|
|
|
struct kmod_command {
|
|
|
|
char *command;
|
|
|
|
char modname[];
|
|
|
|
};
|
|
|
|
|
2011-12-17 06:18:10 +07:00
|
|
|
struct kmod_softdep {
|
|
|
|
char *name;
|
|
|
|
const char **pre;
|
|
|
|
const char **post;
|
|
|
|
unsigned int n_pre;
|
|
|
|
unsigned int n_post;
|
|
|
|
};
|
|
|
|
|
2011-12-24 19:50:47 +07:00
|
|
|
const char *kmod_blacklist_get_modname(const struct kmod_list *l)
|
|
|
|
{
|
|
|
|
return l->data;
|
|
|
|
}
|
|
|
|
|
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-11 05:47:01 +07:00
|
|
|
const char *kmod_option_get_options(const struct kmod_list *l) {
|
|
|
|
const struct kmod_options *alias = l->data;
|
|
|
|
return alias->options;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *kmod_option_get_modname(const struct kmod_list *l) {
|
|
|
|
const struct kmod_options *alias = l->data;
|
|
|
|
return alias->modname;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *kmod_command_get_command(const struct kmod_list *l) {
|
|
|
|
const struct kmod_command *alias = l->data;
|
|
|
|
return alias->command;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *kmod_command_get_modname(const struct kmod_list *l) {
|
|
|
|
const struct kmod_command *alias = l->data;
|
|
|
|
return alias->modname;
|
|
|
|
}
|
|
|
|
|
2011-12-17 06:18:10 +07:00
|
|
|
const char *kmod_softdep_get_name(const struct kmod_list *l) {
|
|
|
|
const struct kmod_softdep *dep = l->data;
|
|
|
|
return dep->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * const *kmod_softdep_get_pre(const struct kmod_list *l, unsigned int *count) {
|
|
|
|
const struct kmod_softdep *dep = l->data;
|
|
|
|
*count = dep->n_pre;
|
|
|
|
return dep->pre;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * const *kmod_softdep_get_post(const struct kmod_list *l, unsigned int *count) {
|
|
|
|
const struct kmod_softdep *dep = l->data;
|
|
|
|
*count = dep->n_post;
|
|
|
|
return dep->post;
|
|
|
|
}
|
|
|
|
|
2011-12-07 20:31:28 +07:00
|
|
|
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;
|
|
|
|
struct kmod_list *l;
|
|
|
|
size_t modnamelen = strlen(modname) + 1;
|
|
|
|
size_t commandlen = strlen(command) + 1;
|
|
|
|
|
2011-12-19 23:36:23 +07:00
|
|
|
DBG(config->ctx, "modname='%s' cmd='%s %s'\n", modname, command_name,
|
2011-12-07 20:31:28 +07:00
|
|
|
command);
|
|
|
|
|
|
|
|
cmd = malloc(sizeof(*cmd) + modnamelen + commandlen);
|
|
|
|
if (cmd == NULL)
|
|
|
|
goto oom_error_init;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
*list = l;
|
|
|
|
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,
|
|
|
|
struct kmod_list *l,
|
|
|
|
struct kmod_list **list)
|
|
|
|
{
|
|
|
|
struct kmod_command *cmd = l->data;
|
|
|
|
|
|
|
|
free(cmd);
|
|
|
|
*list = kmod_list_remove(l);
|
|
|
|
}
|
|
|
|
|
2011-12-07 12:18:57 +07:00
|
|
|
static int kmod_config_add_options(struct kmod_config *config,
|
|
|
|
const char *modname, const char *options)
|
|
|
|
{
|
|
|
|
struct kmod_options *opt;
|
|
|
|
struct kmod_list *list;
|
|
|
|
size_t modnamelen = strlen(modname) + 1;
|
|
|
|
size_t optionslen = strlen(options) + 1;
|
|
|
|
|
2011-12-15 02:21:46 +07:00
|
|
|
DBG(config->ctx, "modname='%s' options='%s'\n", modname, options);
|
2011-12-07 12:18:57 +07:00
|
|
|
|
|
|
|
opt = malloc(sizeof(*opt) + modnamelen + optionslen);
|
|
|
|
if (opt == NULL)
|
|
|
|
goto oom_error_init;
|
|
|
|
|
|
|
|
opt->options = sizeof(*opt) + modnamelen + (char *)opt;
|
|
|
|
|
|
|
|
memcpy(opt->modname, modname, modnamelen);
|
|
|
|
memcpy(opt->options, options, optionslen);
|
|
|
|
strchr_replace(opt->options, '\t', ' ');
|
|
|
|
|
|
|
|
list = kmod_list_append(config->options, opt);
|
|
|
|
if (list == NULL)
|
|
|
|
goto oom_error;
|
|
|
|
|
|
|
|
config->options = list;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
oom_error:
|
|
|
|
free(opt);
|
|
|
|
oom_error_init:
|
|
|
|
ERR(config->ctx, "out-of-memory\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2011-12-12 19:48:02 +07:00
|
|
|
static void kmod_config_free_options(struct kmod_config *config,
|
|
|
|
struct kmod_list *l)
|
2011-12-07 12:18:57 +07:00
|
|
|
{
|
|
|
|
struct kmod_options *opt = l->data;
|
|
|
|
|
|
|
|
free(opt);
|
|
|
|
|
|
|
|
config->options = kmod_list_remove(l);
|
|
|
|
}
|
|
|
|
|
2011-12-03 06:40:22 +07:00
|
|
|
static int kmod_config_add_alias(struct kmod_config *config,
|
2011-12-12 19:48:02 +07:00
|
|
|
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-12-05 02:32:44 +07:00
|
|
|
size_t namelen = strlen(name) + 1, modnamelen = strlen(modname) + 1;
|
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
|
|
|
|
2011-12-05 02:32:44 +07:00
|
|
|
alias = malloc(sizeof(*alias) + namelen + modnamelen);
|
2011-12-03 06:40:22 +07:00
|
|
|
if (!alias)
|
|
|
|
goto oom_error_init;
|
2011-12-12 20:52:59 +07:00
|
|
|
|
2011-12-05 02:32:44 +07:00
|
|
|
alias->name = sizeof(*alias) + modnamelen + (char *)alias;
|
|
|
|
|
|
|
|
memcpy(alias->modname, modname, modnamelen);
|
|
|
|
memcpy(alias->name, name, namelen);
|
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;
|
2011-12-12 20:52:59 +07:00
|
|
|
|
2011-12-03 06:40:22 +07:00
|
|
|
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;
|
2011-11-30 03:07:43 +07:00
|
|
|
}
|
|
|
|
|
2011-12-12 19:48:02 +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);
|
|
|
|
|
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-12-12 19:48:02 +07:00
|
|
|
const char *modname)
|
2011-11-30 03:48:02 +07:00
|
|
|
{
|
|
|
|
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-17 06:18:10 +07:00
|
|
|
static int kmod_config_add_softdep(struct kmod_config *config,
|
|
|
|
const char *modname,
|
|
|
|
const char *line)
|
|
|
|
{
|
|
|
|
struct kmod_list *list;
|
|
|
|
struct kmod_softdep *dep;
|
|
|
|
const char *s, *p;
|
|
|
|
char *itr;
|
|
|
|
unsigned int n_pre = 0, n_post = 0;
|
|
|
|
size_t modnamelen = strlen(modname) + 1;
|
|
|
|
size_t buflen = 0;
|
|
|
|
bool was_space = false;
|
|
|
|
enum { S_NONE, S_PRE, S_POST } mode = S_NONE;
|
|
|
|
|
|
|
|
DBG(config->ctx, "modname=%s\n", modname);
|
|
|
|
|
|
|
|
/* analyze and count */
|
|
|
|
for (p = s = line; ; s++) {
|
|
|
|
size_t plen;
|
|
|
|
|
|
|
|
if (*s != '\0') {
|
|
|
|
if (!isspace(*s)) {
|
|
|
|
was_space = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (was_space) {
|
|
|
|
p = s + 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
was_space = true;
|
|
|
|
|
|
|
|
if (p >= s)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
plen = s - p;
|
|
|
|
|
|
|
|
if (plen == sizeof("pre:") - 1 &&
|
|
|
|
memcmp(p, "pre:", sizeof("pre:") - 1) == 0)
|
|
|
|
mode = S_PRE;
|
|
|
|
else if (plen == sizeof("post:") - 1 &&
|
|
|
|
memcmp(p, "post:", sizeof("post:") - 1) == 0)
|
|
|
|
mode = S_POST;
|
|
|
|
else if (*s != '\0' || (*s == '\0' && !was_space)) {
|
|
|
|
if (mode == S_PRE) {
|
|
|
|
buflen += plen + 1;
|
|
|
|
n_pre++;
|
|
|
|
} else if (mode == S_POST) {
|
|
|
|
buflen += plen + 1;
|
|
|
|
n_post++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p = s + 1;
|
|
|
|
if (*s == '\0')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
DBG(config->ctx, "%u pre, %u post\n", n_pre, n_post);
|
|
|
|
|
|
|
|
dep = malloc(sizeof(struct kmod_softdep) + modnamelen +
|
|
|
|
n_pre * sizeof(const char *) +
|
|
|
|
n_post * sizeof(const char *) +
|
|
|
|
buflen);
|
|
|
|
if (dep == NULL) {
|
|
|
|
ERR(config->ctx, "out-of-memory modname=%s\n", modname);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
dep->n_pre = n_pre;
|
|
|
|
dep->n_post = n_post;
|
|
|
|
dep->pre = (const char **)((char *)dep + sizeof(struct kmod_softdep));
|
|
|
|
dep->post = dep->pre + n_pre;
|
|
|
|
dep->name = (char *)(dep->post + n_post);
|
|
|
|
|
|
|
|
memcpy(dep->name, modname, modnamelen);
|
|
|
|
|
|
|
|
/* copy strings */
|
|
|
|
itr = dep->name + modnamelen;
|
|
|
|
n_pre = 0;
|
|
|
|
n_post = 0;
|
|
|
|
mode = S_NONE;
|
|
|
|
for (p = s = line; ; s++) {
|
|
|
|
size_t plen;
|
|
|
|
|
|
|
|
if (*s != '\0') {
|
|
|
|
if (!isspace(*s)) {
|
|
|
|
was_space = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (was_space) {
|
|
|
|
p = s + 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
was_space = true;
|
|
|
|
|
|
|
|
if (p >= s)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
plen = s - p;
|
|
|
|
|
|
|
|
if (plen == sizeof("pre:") - 1 &&
|
|
|
|
memcmp(p, "pre:", sizeof("pre:") - 1) == 0)
|
|
|
|
mode = S_PRE;
|
|
|
|
else if (plen == sizeof("post:") - 1 &&
|
|
|
|
memcmp(p, "post:", sizeof("post:") - 1) == 0)
|
|
|
|
mode = S_POST;
|
|
|
|
else if (*s != '\0' || (*s == '\0' && !was_space)) {
|
|
|
|
if (mode == S_PRE) {
|
|
|
|
dep->pre[n_pre] = itr;
|
|
|
|
memcpy(itr, p, plen);
|
|
|
|
itr[plen] = '\0';
|
|
|
|
itr += plen + 1;
|
|
|
|
n_pre++;
|
|
|
|
} else if (mode == S_POST) {
|
|
|
|
dep->post[n_post] = itr;
|
|
|
|
memcpy(itr, p, plen);
|
|
|
|
itr[plen] = '\0';
|
|
|
|
itr += plen + 1;
|
|
|
|
n_post++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p = s + 1;
|
|
|
|
if (*s == '\0')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
list = kmod_list_append(config->softdeps, dep);
|
|
|
|
if (list == NULL) {
|
|
|
|
free(dep);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
config->softdeps = list;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kmod_config_free_softdep(struct kmod_config *config,
|
|
|
|
struct kmod_list *l)
|
|
|
|
{
|
|
|
|
free(l->data);
|
|
|
|
config->softdeps = kmod_list_remove(l);
|
|
|
|
}
|
|
|
|
|
2011-12-15 02:19:19 +07:00
|
|
|
static void kcmdline_parse_result(struct kmod_config *config, char *modname,
|
|
|
|
char *param, char *value)
|
|
|
|
{
|
|
|
|
if (modname == NULL || param == NULL || value == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
DBG(config->ctx, "%s %s\n", modname, param);
|
|
|
|
|
|
|
|
if (streq(modname, "modprobe") && !strncmp(param, "blacklist=", 10)) {
|
|
|
|
for (;;) {
|
|
|
|
char *t = strsep(&value, ",");
|
|
|
|
if (t == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
kmod_config_add_blacklist(config, t);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
kmod_config_add_options(config,
|
|
|
|
underscores(config->ctx, modname), param);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int kmod_config_parse_kcmdline(struct kmod_config *config)
|
|
|
|
{
|
|
|
|
char buf[KCMD_LINE_SIZE];
|
|
|
|
int fd, err;
|
|
|
|
char *p, *modname, *param = NULL, *value = NULL;
|
|
|
|
|
2011-12-16 12:49:54 +07:00
|
|
|
fd = open("/proc/cmdline", O_RDONLY|O_CLOEXEC);
|
2011-12-15 02:19:19 +07:00
|
|
|
err = read_str_safe(fd, buf, sizeof(buf));
|
|
|
|
close(fd);
|
|
|
|
if (err < 0) {
|
|
|
|
ERR(config->ctx, "could not read from '/proc/cmdline': %s\n",
|
|
|
|
strerror(-err));
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (p = buf, modname = buf; *p != '\0' && *p != '\n'; p++) {
|
|
|
|
switch (*p) {
|
|
|
|
case ' ':
|
|
|
|
*p = '\0';
|
|
|
|
kcmdline_parse_result(config, modname, param, value);
|
|
|
|
param = value = NULL;
|
|
|
|
modname = p + 1;
|
|
|
|
break;
|
|
|
|
case '.':
|
|
|
|
*p = '\0';
|
|
|
|
param = p + 1;
|
|
|
|
break;
|
|
|
|
case '=':
|
2011-12-20 22:25:24 +07:00
|
|
|
if (param != NULL)
|
|
|
|
value = p + 1;
|
2011-12-15 02:19:19 +07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*p = '\0';
|
|
|
|
kcmdline_parse_result(config, modname, param, value);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-06 11:26:22 +07:00
|
|
|
/*
|
|
|
|
* Take an fd and own it. It will be closed on return. filename is used only
|
|
|
|
* for debug messages
|
|
|
|
*/
|
|
|
|
static int kmod_config_parse(struct kmod_config *config, int fd,
|
|
|
|
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;
|
2011-12-22 10:30:40 +07:00
|
|
|
unsigned int linenum = 0;
|
2011-12-06 11:26:22 +07:00
|
|
|
int err;
|
2011-11-30 03:07:43 +07:00
|
|
|
|
2011-12-06 11:26:22 +07:00
|
|
|
fp = fdopen(fd, "r");
|
|
|
|
if (fp == NULL) {
|
|
|
|
err = -errno;
|
|
|
|
ERR(config->ctx, "fd %d: %m", fd);
|
|
|
|
close(fd);
|
|
|
|
return err;
|
|
|
|
}
|
2011-11-30 03:07:43 +07:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2011-12-07 11:26:31 +07:00
|
|
|
if (streq(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-12-07 11:26:31 +07:00
|
|
|
} else if (streq(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-12-07 12:18:57 +07:00
|
|
|
} else if (streq(cmd, "options")) {
|
|
|
|
char *modname = strtok_r(NULL, "\t ", &saveptr);
|
|
|
|
|
|
|
|
if (modname == NULL)
|
|
|
|
goto syntax_error;
|
|
|
|
|
|
|
|
kmod_config_add_options(config,
|
|
|
|
underscores(ctx, modname),
|
|
|
|
strtok_r(NULL, "\0", &saveptr));
|
2011-12-29 00:01:16 +07:00
|
|
|
} else if (streq(cmd, "install")) {
|
2011-12-07 20:31:28 +07:00
|
|
|
char *modname = strtok_r(NULL, "\t ", &saveptr);
|
|
|
|
|
|
|
|
if (modname == NULL)
|
|
|
|
goto syntax_error;
|
|
|
|
|
|
|
|
kmod_config_add_command(config,
|
|
|
|
underscores(ctx, modname),
|
|
|
|
strtok_r(NULL, "\0", &saveptr),
|
|
|
|
cmd, &config->install_commands);
|
2011-12-29 00:01:16 +07:00
|
|
|
} else if (streq(cmd, "remove")) {
|
2011-12-07 20:31:28 +07:00
|
|
|
char *modname = strtok_r(NULL, "\t ", &saveptr);
|
|
|
|
|
|
|
|
if (modname == NULL)
|
|
|
|
goto syntax_error;
|
|
|
|
|
|
|
|
kmod_config_add_command(config,
|
|
|
|
underscores(ctx, modname),
|
|
|
|
strtok_r(NULL, "\0", &saveptr),
|
|
|
|
cmd, &config->remove_commands);
|
2011-12-29 00:01:16 +07:00
|
|
|
} else if (streq(cmd, "softdep")) {
|
2011-12-17 06:18:10 +07:00
|
|
|
char *modname = strtok_r(NULL, "\t ", &saveptr);
|
|
|
|
|
|
|
|
if (modname == NULL)
|
|
|
|
goto syntax_error;
|
|
|
|
|
|
|
|
kmod_config_add_softdep(config,
|
|
|
|
underscores(ctx, modname),
|
|
|
|
strtok_r(NULL, "\0", &saveptr));
|
2011-12-07 12:18:57 +07:00
|
|
|
} else if (streq(cmd, "include")
|
2011-12-07 11:26:31 +07:00
|
|
|
|| streq(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);
|
|
|
|
|
2011-12-07 12:18:57 +07:00
|
|
|
while (config->options)
|
|
|
|
kmod_config_free_options(config, config->options);
|
|
|
|
|
2011-12-07 20:31:28 +07:00
|
|
|
while (config->install_commands) {
|
|
|
|
kmod_config_free_command(config, config->install_commands,
|
|
|
|
&config->install_commands);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (config->remove_commands) {
|
|
|
|
kmod_config_free_command(config, config->remove_commands,
|
|
|
|
&config->remove_commands);
|
|
|
|
}
|
|
|
|
|
2011-12-17 06:18:10 +07:00
|
|
|
while (config->softdeps)
|
|
|
|
kmod_config_free_softdep(config, config->softdeps);
|
|
|
|
|
2012-01-01 03:48:05 +07:00
|
|
|
for (; config->paths != NULL;
|
|
|
|
config->paths = kmod_list_remove(config->paths))
|
|
|
|
free(config->paths->data);
|
|
|
|
|
2011-12-03 06:40:22 +07:00
|
|
|
free(config);
|
2011-11-30 03:07:43 +07:00
|
|
|
}
|
|
|
|
|
2011-12-13 00:23:03 +07:00
|
|
|
static bool conf_files_filter_out(struct kmod_ctx *ctx, DIR *d,
|
|
|
|
const char *path, const char *fn)
|
2011-11-30 03:07:43 +07:00
|
|
|
{
|
|
|
|
size_t len = strlen(fn);
|
2011-12-13 00:23:03 +07:00
|
|
|
struct stat st;
|
2011-11-30 03:07:43 +07:00
|
|
|
|
|
|
|
if (fn[0] == '.')
|
2011-12-13 00:27:57 +07:00
|
|
|
return true;
|
2011-11-30 03:07:43 +07:00
|
|
|
|
2011-12-07 11:26:31 +07:00
|
|
|
if (len < 6 || (!streq(&fn[len - 5], ".conf")
|
2011-12-31 23:19:55 +07:00
|
|
|
&& !streq(&fn[len - 6], ".alias")))
|
2011-12-13 00:27:57 +07:00
|
|
|
return true;
|
2011-11-30 03:07:43 +07:00
|
|
|
|
2011-12-13 00:23:03 +07:00
|
|
|
fstatat(dirfd(d), fn, &st, 0);
|
|
|
|
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
|
|
|
ERR(ctx, "Directories inside directories are not supported: "
|
|
|
|
"%s/%s\n", path, fn);
|
2011-12-13 00:27:57 +07:00
|
|
|
return true;
|
2011-12-13 00:23:03 +07:00
|
|
|
}
|
|
|
|
|
2011-12-13 00:27:57 +07:00
|
|
|
return false;
|
2011-11-30 03:07:43 +07:00
|
|
|
}
|
|
|
|
|
2011-12-21 00:56:31 +07:00
|
|
|
struct conf_file {
|
|
|
|
const char *path;
|
|
|
|
bool is_single;
|
|
|
|
char name[];
|
|
|
|
};
|
|
|
|
|
|
|
|
static int conf_files_insert_sorted(struct kmod_ctx *ctx,
|
|
|
|
struct kmod_list **list,
|
|
|
|
const char *path, const char *name)
|
|
|
|
{
|
|
|
|
struct kmod_list *lpos, *tmp;
|
|
|
|
struct conf_file *cf;
|
|
|
|
size_t namelen;
|
|
|
|
int cmp = -1;
|
|
|
|
bool is_single = false;
|
|
|
|
|
|
|
|
if (name == NULL) {
|
|
|
|
name = basename(path);
|
|
|
|
is_single = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
kmod_list_foreach(lpos, *list) {
|
|
|
|
cf = lpos->data;
|
|
|
|
|
|
|
|
if ((cmp = strcmp(name, cf->name)) <= 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmp == 0) {
|
|
|
|
DBG(ctx, "Ignoring duplicate config file: %s/%s\n", path,
|
|
|
|
name);
|
|
|
|
return -EEXIST;
|
|
|
|
}
|
|
|
|
|
|
|
|
namelen = strlen(name);
|
|
|
|
cf = malloc(sizeof(*cf) + namelen + 1);
|
|
|
|
if (cf == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
memcpy(cf->name, name, namelen + 1);
|
|
|
|
cf->path = path;
|
|
|
|
cf->is_single = is_single;
|
|
|
|
|
|
|
|
if (lpos == NULL)
|
|
|
|
tmp = kmod_list_append(*list, cf);
|
|
|
|
else if (lpos == *list)
|
|
|
|
tmp = kmod_list_prepend(*list, cf);
|
|
|
|
else
|
|
|
|
tmp = kmod_list_insert_before(lpos, cf);
|
|
|
|
|
|
|
|
if (tmp == NULL) {
|
|
|
|
free(cf);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lpos == NULL || lpos == *list)
|
|
|
|
*list = tmp;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-13 00:00:51 +07:00
|
|
|
/*
|
2011-12-21 00:56:31 +07:00
|
|
|
* Insert configuration files in @list, ignoring duplicates
|
2011-12-13 00:00:51 +07:00
|
|
|
*/
|
2011-12-21 00:56:31 +07:00
|
|
|
static int conf_files_list(struct kmod_ctx *ctx, struct kmod_list **list,
|
2012-01-01 03:48:05 +07:00
|
|
|
const char *path,
|
|
|
|
unsigned long long *path_stamp)
|
2011-11-30 03:07:43 +07:00
|
|
|
{
|
|
|
|
DIR *d;
|
|
|
|
int err;
|
2011-12-21 00:56:31 +07:00
|
|
|
struct stat st;
|
2011-11-30 03:07:43 +07:00
|
|
|
|
2011-12-21 00:56:31 +07:00
|
|
|
if (stat(path, &st) != 0) {
|
|
|
|
err = -errno;
|
|
|
|
DBG(ctx, "could not stat '%s': %m\n", path);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2012-01-01 03:48:05 +07:00
|
|
|
*path_stamp = ts_usec(&st.st_mtim);
|
|
|
|
|
2011-12-21 00:56:31 +07:00
|
|
|
if (S_ISREG(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;
|
|
|
|
}
|
2011-12-13 03:36:27 +07:00
|
|
|
|
2011-11-30 03:07:43 +07:00
|
|
|
d = opendir(path);
|
|
|
|
if (d == NULL) {
|
|
|
|
ERR(ctx, "%m\n");
|
2011-12-21 00:56:31 +07:00
|
|
|
return -EINVAL;
|
2011-11-30 03:07:43 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
struct dirent ent, *entp;
|
|
|
|
|
|
|
|
err = readdir_r(d, &ent, &entp);
|
|
|
|
if (err != 0) {
|
2011-12-06 11:26:22 +07:00
|
|
|
ERR(ctx, "reading entry %s\n", strerror(-err));
|
|
|
|
goto fail_read;
|
2011-11-30 03:07:43 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (entp == NULL)
|
|
|
|
break;
|
|
|
|
|
2011-12-13 00:27:57 +07:00
|
|
|
if (conf_files_filter_out(ctx, d, path, entp->d_name))
|
2011-11-30 03:07:43 +07:00
|
|
|
continue;
|
|
|
|
|
2011-12-21 00:56:31 +07:00
|
|
|
conf_files_insert_sorted(ctx, list, path, entp->d_name);
|
2011-12-06 11:26:22 +07:00
|
|
|
}
|
2011-11-30 03:07:43 +07:00
|
|
|
|
2011-12-21 00:56:31 +07:00
|
|
|
closedir(d);
|
|
|
|
return 0;
|
2011-11-30 03:07:43 +07:00
|
|
|
|
2011-12-06 11:26:22 +07:00
|
|
|
fail_read:
|
|
|
|
closedir(d);
|
2011-12-21 00:56:31 +07:00
|
|
|
return err;
|
2011-11-30 03:07:43 +07:00
|
|
|
}
|
|
|
|
|
2011-12-12 19:48:02 +07:00
|
|
|
int kmod_config_new(struct kmod_ctx *ctx, struct kmod_config **p_config,
|
|
|
|
const char * const *config_paths)
|
2011-11-30 03:07:43 +07:00
|
|
|
{
|
2011-12-03 06:40:22 +07:00
|
|
|
struct kmod_config *config;
|
2011-12-21 00:56:31 +07:00
|
|
|
struct kmod_list *list = NULL;
|
2012-01-01 03:48:05 +07:00
|
|
|
struct kmod_list *path_list = NULL;
|
2011-12-06 11:26:22 +07:00
|
|
|
size_t i;
|
2011-11-30 03:07:43 +07:00
|
|
|
|
2011-12-12 05:37:01 +07:00
|
|
|
for (i = 0; config_paths[i] != NULL; i++) {
|
|
|
|
const char *path = config_paths[i];
|
2012-01-01 03:48:05 +07:00
|
|
|
unsigned long long path_stamp = 0;
|
|
|
|
size_t pathlen;
|
|
|
|
struct kmod_list *tmp;
|
|
|
|
struct kmod_config_path *cf;
|
|
|
|
|
|
|
|
if (conf_files_list(ctx, &list, path, &path_stamp) < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
pathlen = strlen(path) + 1;
|
|
|
|
cf = malloc(sizeof(*cf) + pathlen);
|
|
|
|
if (cf == NULL)
|
|
|
|
goto oom;
|
2011-11-30 03:07:43 +07:00
|
|
|
|
2012-01-01 03:48:05 +07:00
|
|
|
cf->stamp = path_stamp;
|
|
|
|
memcpy(cf->path, path, pathlen);
|
|
|
|
|
|
|
|
tmp = kmod_list_append(path_list, cf);
|
|
|
|
if (tmp == NULL)
|
|
|
|
goto oom;
|
|
|
|
path_list = tmp;
|
2011-12-21 00:56:31 +07:00
|
|
|
}
|
2011-12-12 05:37:01 +07:00
|
|
|
|
2012-01-01 03:48:05 +07:00
|
|
|
*p_config = config = calloc(1, sizeof(struct kmod_config));
|
|
|
|
if (config == NULL)
|
|
|
|
goto oom;
|
|
|
|
|
|
|
|
config->paths = path_list;
|
2012-01-04 17:14:54 +07:00
|
|
|
config->ctx = ctx;
|
2012-01-01 03:48:05 +07:00
|
|
|
|
2011-12-21 00:56:31 +07:00
|
|
|
for (; list != NULL; list = kmod_list_remove(list)) {
|
|
|
|
char fn[PATH_MAX];
|
|
|
|
struct conf_file *cf = list->data;
|
|
|
|
int fd;
|
2011-12-12 05:37:01 +07:00
|
|
|
|
2011-12-21 00:56:31 +07:00
|
|
|
if (cf->is_single)
|
|
|
|
strcpy(fn, cf->path);
|
|
|
|
else
|
|
|
|
snprintf(fn, sizeof(fn),"%s/%s", cf->path,
|
|
|
|
cf->name);
|
2011-11-30 03:07:43 +07:00
|
|
|
|
2011-12-21 00:56:31 +07:00
|
|
|
fd = open(fn, O_RDONLY|O_CLOEXEC);
|
|
|
|
DBG(ctx, "parsing file '%s' fd=%d\n", fn, fd);
|
2011-11-30 03:07:43 +07:00
|
|
|
|
2011-12-21 00:56:31 +07:00
|
|
|
if (fd >= 0)
|
|
|
|
kmod_config_parse(config, fd, fn);
|
2011-11-30 03:07:43 +07:00
|
|
|
|
2011-12-21 00:56:31 +07:00
|
|
|
free(cf);
|
2011-11-30 03:07:43 +07:00
|
|
|
}
|
|
|
|
|
2011-12-15 02:19:19 +07:00
|
|
|
kmod_config_parse_kcmdline(config);
|
|
|
|
|
2011-12-06 11:26:22 +07:00
|
|
|
return 0;
|
2012-01-01 03:48:05 +07:00
|
|
|
|
|
|
|
oom:
|
|
|
|
for (; list != NULL; list = kmod_list_remove(list))
|
|
|
|
free(list->data);
|
|
|
|
|
|
|
|
for (; path_list != NULL; path_list = kmod_list_remove(path_list))
|
|
|
|
free(path_list->data);
|
|
|
|
|
|
|
|
return -ENOMEM;
|
2011-11-30 03:07:43 +07:00
|
|
|
}
|