Log error on failed underscores(), moving it to shared/

Move underscores() to shared/. It's the same as alias_normalize(), but
it rather operates in place, with the same string being passed.

The difference now that it's in shared/ is that it's a non-logging
function.

This makes us a little bit more verbose: we don't accept partially
correct module and aliases names in kcmdline and in configuration files.
We log an error instead.
This commit is contained in:
Lucas De Marchi 2014-10-09 10:59:08 -03:00
parent 2c5bc218be
commit 52c9c99056
3 changed files with 49 additions and 56 deletions

View File

@ -111,37 +111,6 @@ const char * const *kmod_softdep_get_post(const struct kmod_list *l, unsigned in
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,
@ -516,8 +485,11 @@ static void kcmdline_parse_result(struct kmod_config *config, char *modname,
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);
}
}
@ -609,62 +581,51 @@ static int kmod_config_parse(struct kmod_config *config, int fd,
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",

View File

@ -114,6 +114,37 @@ finish:
return 0;
}
/*
* Replace dashes with underscores.
* Dashes inside character range patterns (e.g. [0-9]) are left unchanged.
*
* For convenience, it returns error if @s is NULL
*/
int underscores(char *s)
{
unsigned int i;
if (!s)
return -EINVAL;
for (i = 0; s[i]; i++) {
switch (s[i]) {
case '-':
s[i] = '_';
break;
case ']':
return -EINVAL;
case '[':
i += strcspn(&s[i], "]");
if (!s[i])
return -EINVAL;
break;
}
}
return 0;
}
char *modname_normalize(const char *modname, char buf[static PATH_MAX], size_t *len)
{
size_t s;

View File

@ -21,6 +21,7 @@ void *memdup(const void *p, size_t n) __attribute__((nonnull(1)));
#define KMOD_EXTENSION_UNCOMPRESSED ".ko"
int alias_normalize(const char *alias, char buf[static PATH_MAX], size_t *len) _must_check_ __attribute__((nonnull(1,2)));
int underscores(char *s) _must_check_;
char *modname_normalize(const char *modname, char buf[static PATH_MAX], size_t *len) __attribute__((nonnull(1, 2)));
char *path_to_modname(const char *path, char buf[static PATH_MAX], size_t *len) __attribute__((nonnull(2)));
bool path_ends_with_kmod_ext(const char *path, size_t len) __attribute__((nonnull(1)));