2010-04-20 23:32:50 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-04-07 01:30:22 +02:00
|
|
|
#include "ueventd_parser.h"
|
|
|
|
|
2013-11-19 00:24:40 +01:00
|
|
|
#include <ctype.h>
|
2010-04-20 23:32:50 +02:00
|
|
|
#include <stdarg.h>
|
2017-04-07 01:30:22 +02:00
|
|
|
#include <stdio.h>
|
2013-11-19 00:24:40 +01:00
|
|
|
#include <stdlib.h>
|
2010-04-20 23:32:50 +02:00
|
|
|
#include <string.h>
|
2017-04-07 01:30:22 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <android-base/logging.h>
|
2010-04-20 23:32:50 +02:00
|
|
|
|
|
|
|
#include "parser.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2013-11-19 00:24:40 +01:00
|
|
|
static list_declare(subsystem_list);
|
|
|
|
|
2010-04-20 23:32:50 +02:00
|
|
|
static void parse_line_device(struct parse_state *state, int nargs, char **args);
|
|
|
|
|
2013-11-19 00:24:40 +01:00
|
|
|
#define SECTION 0x01
|
|
|
|
#define OPTION 0x02
|
|
|
|
|
|
|
|
#include "ueventd_keywords.h"
|
|
|
|
|
|
|
|
#define KEYWORD(symbol, flags, nargs) \
|
2016-05-19 00:59:37 +02:00
|
|
|
[ K_##symbol ] = { #symbol, (nargs) + 1, flags, },
|
2013-11-19 00:24:40 +01:00
|
|
|
|
|
|
|
static struct {
|
|
|
|
const char *name;
|
|
|
|
unsigned char nargs;
|
|
|
|
unsigned char flags;
|
|
|
|
} keyword_info[KEYWORD_COUNT] = {
|
|
|
|
[ K_UNKNOWN ] = { "unknown", 0, 0 },
|
|
|
|
#include "ueventd_keywords.h"
|
|
|
|
};
|
|
|
|
#undef KEYWORD
|
|
|
|
|
|
|
|
#define kw_is(kw, type) (keyword_info[kw].flags & (type))
|
|
|
|
#define kw_nargs(kw) (keyword_info[kw].nargs)
|
|
|
|
|
|
|
|
static int lookup_keyword(const char *s)
|
|
|
|
{
|
|
|
|
switch (*s++) {
|
|
|
|
case 'd':
|
|
|
|
if (!strcmp(s, "evname")) return K_devname;
|
|
|
|
if (!strcmp(s, "irname")) return K_dirname;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
if (!strcmp(s, "ubsystem")) return K_subsystem;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return K_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2015-02-06 21:19:48 +01:00
|
|
|
static void parse_line_no_op(struct parse_state*, int, char**) {
|
2013-11-19 00:24:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int valid_name(const char *name)
|
|
|
|
{
|
|
|
|
while (*name) {
|
|
|
|
if (!isalnum(*name) && (*name != '_') && (*name != '-')) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
name++;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ueventd_subsystem *ueventd_subsystem_find_by_name(const char *name)
|
|
|
|
{
|
|
|
|
struct listnode *node;
|
|
|
|
struct ueventd_subsystem *s;
|
|
|
|
|
|
|
|
list_for_each(node, &subsystem_list) {
|
|
|
|
s = node_to_item(node, struct ueventd_subsystem, slist);
|
|
|
|
if (!strcmp(s->name, name)) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-02-06 21:19:48 +01:00
|
|
|
static void *parse_subsystem(parse_state* state, int /*nargs*/, char** args) {
|
2013-11-19 00:24:40 +01:00
|
|
|
if (!valid_name(args[1])) {
|
|
|
|
parse_error(state, "invalid subsystem name '%s'\n", args[1]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-02-04 02:12:07 +01:00
|
|
|
ueventd_subsystem* s = ueventd_subsystem_find_by_name(args[1]);
|
2013-11-19 00:24:40 +01:00
|
|
|
if (s) {
|
|
|
|
parse_error(state, "ignored duplicate definition of subsystem '%s'\n",
|
|
|
|
args[1]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-02-04 02:12:07 +01:00
|
|
|
s = (ueventd_subsystem*) calloc(1, sizeof(*s));
|
2013-11-19 00:24:40 +01:00
|
|
|
if (!s) {
|
|
|
|
parse_error(state, "out of memory\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
s->name = args[1];
|
|
|
|
s->dirname = "/dev";
|
|
|
|
list_add_tail(&subsystem_list, &s->slist);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_line_subsystem(struct parse_state *state, int nargs,
|
|
|
|
char **args)
|
|
|
|
{
|
2015-02-04 02:12:07 +01:00
|
|
|
struct ueventd_subsystem *s = (ueventd_subsystem*) state->context;
|
2013-11-19 00:24:40 +01:00
|
|
|
int kw;
|
|
|
|
|
|
|
|
if (nargs == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
kw = lookup_keyword(args[0]);
|
|
|
|
switch (kw) {
|
|
|
|
case K_devname:
|
|
|
|
if (!strcmp(args[1], "uevent_devname"))
|
|
|
|
s->devname_src = DEVNAME_UEVENT_DEVNAME;
|
|
|
|
else if (!strcmp(args[1], "uevent_devpath"))
|
|
|
|
s->devname_src = DEVNAME_UEVENT_DEVPATH;
|
|
|
|
else
|
|
|
|
parse_error(state, "invalid devname '%s'\n", args[1]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case K_dirname:
|
|
|
|
if (args[1][0] == '/')
|
|
|
|
s->dirname = args[1];
|
|
|
|
else
|
|
|
|
parse_error(state, "dirname '%s' does not start with '/'\n",
|
|
|
|
args[1]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
parse_error(state, "invalid option '%s'\n", args[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_new_section(struct parse_state *state, int kw,
|
|
|
|
int nargs, char **args)
|
|
|
|
{
|
|
|
|
printf("[ %s %s ]\n", args[0],
|
|
|
|
nargs > 1 ? args[1] : "");
|
|
|
|
|
|
|
|
switch(kw) {
|
|
|
|
case K_subsystem:
|
|
|
|
state->context = parse_subsystem(state, nargs, args);
|
|
|
|
if (state->context) {
|
|
|
|
state->parse_line = parse_line_subsystem;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
state->parse_line = parse_line_no_op;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_line(struct parse_state *state, char **args, int nargs)
|
|
|
|
{
|
|
|
|
int kw = lookup_keyword(args[0]);
|
|
|
|
int kw_nargs = kw_nargs(kw);
|
|
|
|
|
|
|
|
if (nargs < kw_nargs) {
|
|
|
|
parse_error(state, "%s requires %d %s\n", args[0], kw_nargs - 1,
|
|
|
|
kw_nargs > 2 ? "arguments" : "argument");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kw_is(kw, SECTION)) {
|
|
|
|
parse_new_section(state, kw, nargs, args);
|
|
|
|
} else if (kw_is(kw, OPTION)) {
|
|
|
|
state->parse_line(state, nargs, args);
|
|
|
|
} else {
|
|
|
|
parse_line_device(state, nargs, args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-06 21:19:48 +01:00
|
|
|
static void parse_config(const char *fn, const std::string& data)
|
2010-04-20 23:32:50 +02:00
|
|
|
{
|
|
|
|
char *args[UEVENTD_PARSER_MAXARGS];
|
2015-05-12 22:54:41 +02:00
|
|
|
|
|
|
|
int nargs = 0;
|
|
|
|
parse_state state;
|
2010-04-20 23:32:50 +02:00
|
|
|
state.filename = fn;
|
|
|
|
state.line = 1;
|
2015-02-06 21:19:48 +01:00
|
|
|
state.ptr = strdup(data.c_str()); // TODO: fix this code!
|
2010-04-20 23:32:50 +02:00
|
|
|
state.nexttoken = 0;
|
2013-11-19 00:24:40 +01:00
|
|
|
state.parse_line = parse_line_no_op;
|
2010-04-20 23:32:50 +02:00
|
|
|
for (;;) {
|
|
|
|
int token = next_token(&state);
|
|
|
|
switch (token) {
|
|
|
|
case T_EOF:
|
2013-11-19 00:24:40 +01:00
|
|
|
parse_line(&state, args, nargs);
|
2010-04-20 23:32:50 +02:00
|
|
|
return;
|
|
|
|
case T_NEWLINE:
|
|
|
|
if (nargs) {
|
2013-11-19 00:24:40 +01:00
|
|
|
parse_line(&state, args, nargs);
|
2010-04-20 23:32:50 +02:00
|
|
|
nargs = 0;
|
|
|
|
}
|
2013-11-19 00:24:40 +01:00
|
|
|
state.line++;
|
2010-04-20 23:32:50 +02:00
|
|
|
break;
|
|
|
|
case T_TEXT:
|
|
|
|
if (nargs < UEVENTD_PARSER_MAXARGS) {
|
|
|
|
args[nargs++] = state.text;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int ueventd_parse_config_file(const char *fn)
|
|
|
|
{
|
2015-02-06 21:19:48 +01:00
|
|
|
std::string data;
|
|
|
|
if (!read_file(fn, &data)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2010-04-20 23:32:50 +02:00
|
|
|
|
2015-05-12 22:54:41 +02:00
|
|
|
data.push_back('\n'); // TODO: fix parse_config.
|
2010-04-20 23:32:50 +02:00
|
|
|
parse_config(fn, data);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-29 13:33:00 +02:00
|
|
|
static void parse_line_device(parse_state* state, int nargs, char** args) {
|
|
|
|
set_device_permission(state->filename, state->line, nargs, args);
|
2010-04-20 23:32:50 +02:00
|
|
|
}
|