1
0
mirror of https://github.com/systemd/systemd.git synced 2025-03-08 08:58:27 +03:00

condition: rewrite condition_test_kernel_command_line() based on unquote_first_word()

This commit is contained in:
Lennart Poettering 2014-11-05 19:43:55 +01:00
parent d5ce1cfce4
commit 07318c2908
2 changed files with 32 additions and 26 deletions

View File

@ -74,12 +74,10 @@ void condition_free_list(Condition *first) {
}
bool condition_test_kernel_command_line(Condition *c) {
char *line, *word = NULL;
const char *w, *state;
_cleanup_free_ char *line = NULL;
const char *p;
bool equal;
int r;
size_t l, pl;
bool found = false;
assert(c);
assert(c->parameter);
@ -92,35 +90,30 @@ bool condition_test_kernel_command_line(Condition *c) {
return c->negate;
equal = !!strchr(c->parameter, '=');
pl = strlen(c->parameter);
p = line;
FOREACH_WORD_QUOTED(w, l, line, state) {
for (;;) {
_cleanup_free_ char *word = NULL;
bool found;
free(word);
word = strndup(w, l);
if (!word)
break;
r = unquote_first_word(&p, &word);
if (r <= 0)
return c->negate;
if (equal) {
if (streq(word, c->parameter)) {
found = true;
break;
}
} else {
if (startswith(word, c->parameter) && (word[pl] == '=' || word[pl] == 0)) {
found = true;
break;
}
if (equal)
found = streq(word, c->parameter);
else {
const char *f;
f = startswith(word, c->parameter);
found = f && (*f == '=' || *f == 0);
}
if (found)
return !c->negate;
}
if (!isempty(state))
log_warning("Trailing garbage and the end of kernel commandline, ignoring.");
free(word);
free(line);
return found == !c->negate;
return c->negate;
}
bool condition_test_virtualization(Condition *c) {

View File

@ -95,6 +95,18 @@ static void test_condition_test_architecture(void) {
condition_free(condition);
}
static void test_condition_test_kernel_command_line(void) {
Condition *condition;
condition = condition_new(CONDITION_KERNEL_COMMAND_LINE, "thisreallyshouldntbeonthekernelcommandline", false, false);
assert_se(!condition_test_kernel_command_line(condition));
condition_free(condition);
condition = condition_new(CONDITION_KERNEL_COMMAND_LINE, "andthis=neither", false, false);
assert_se(!condition_test_kernel_command_line(condition));
condition_free(condition);
}
int main(int argc, char *argv[]) {
log_parse_environment();
log_open();
@ -102,6 +114,7 @@ int main(int argc, char *argv[]) {
test_condition_test_ac_power();
test_condition_test_host();
test_condition_test_architecture();
test_condition_test_kernel_command_line();
return 0;
}