2010-04-21 05:32:50 +08: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.
|
|
|
|
*/
|
|
|
|
|
2013-11-19 07:24:40 +08:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
2010-04-21 05:32:50 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdarg.h>
|
2013-11-19 07:24:40 +08:00
|
|
|
#include <stdlib.h>
|
2010-04-21 05:32:50 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
2013-11-19 07:24:40 +08:00
|
|
|
#include "ueventd.h"
|
2010-04-21 05:32:50 +08:00
|
|
|
#include "ueventd_parser.h"
|
|
|
|
#include "parser.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2013-11-19 07:24:40 +08:00
|
|
|
static list_declare(subsystem_list);
|
|
|
|
|
2010-04-21 05:32:50 +08:00
|
|
|
static void parse_line_device(struct parse_state *state, int nargs, char **args);
|
|
|
|
|
2013-11-19 07:24:40 +08:00
|
|
|
#define SECTION 0x01
|
|
|
|
#define OPTION 0x02
|
|
|
|
|
|
|
|
#include "ueventd_keywords.h"
|
|
|
|
|
|
|
|
#define KEYWORD(symbol, flags, nargs) \
|
|
|
|
[ K_##symbol ] = { #symbol, nargs + 1, flags, },
|
|
|
|
|
|
|
|
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-07 04:19:48 +08:00
|
|
|
static void parse_line_no_op(struct parse_state*, int, char**) {
|
2013-11-19 07:24:40 +08: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-07 04:19:48 +08:00
|
|
|
static void *parse_subsystem(parse_state* state, int /*nargs*/, char** args) {
|
2013-11-19 07:24:40 +08:00
|
|
|
if (!valid_name(args[1])) {
|
|
|
|
parse_error(state, "invalid subsystem name '%s'\n", args[1]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-02-04 09:12:07 +08:00
|
|
|
ueventd_subsystem* s = ueventd_subsystem_find_by_name(args[1]);
|
2013-11-19 07:24:40 +08:00
|
|
|
if (s) {
|
|
|
|
parse_error(state, "ignored duplicate definition of subsystem '%s'\n",
|
|
|
|
args[1]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-02-04 09:12:07 +08:00
|
|
|
s = (ueventd_subsystem*) calloc(1, sizeof(*s));
|
2013-11-19 07:24:40 +08: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 09:12:07 +08:00
|
|
|
struct ueventd_subsystem *s = (ueventd_subsystem*) state->context;
|
2013-11-19 07:24:40 +08: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-07 04:19:48 +08:00
|
|
|
static void parse_config(const char *fn, const std::string& data)
|
2010-04-21 05:32:50 +08:00
|
|
|
{
|
|
|
|
char *args[UEVENTD_PARSER_MAXARGS];
|
2015-05-13 04:54:41 +08:00
|
|
|
|
|
|
|
int nargs = 0;
|
|
|
|
parse_state state;
|
2010-04-21 05:32:50 +08:00
|
|
|
state.filename = fn;
|
|
|
|
state.line = 1;
|
2015-02-07 04:19:48 +08:00
|
|
|
state.ptr = strdup(data.c_str()); // TODO: fix this code!
|
2010-04-21 05:32:50 +08:00
|
|
|
state.nexttoken = 0;
|
2013-11-19 07:24:40 +08:00
|
|
|
state.parse_line = parse_line_no_op;
|
2010-04-21 05:32:50 +08:00
|
|
|
for (;;) {
|
|
|
|
int token = next_token(&state);
|
|
|
|
switch (token) {
|
|
|
|
case T_EOF:
|
2013-11-19 07:24:40 +08:00
|
|
|
parse_line(&state, args, nargs);
|
2010-04-21 05:32:50 +08:00
|
|
|
return;
|
|
|
|
case T_NEWLINE:
|
|
|
|
if (nargs) {
|
2013-11-19 07:24:40 +08:00
|
|
|
parse_line(&state, args, nargs);
|
2010-04-21 05:32:50 +08:00
|
|
|
nargs = 0;
|
|
|
|
}
|
2013-11-19 07:24:40 +08:00
|
|
|
state.line++;
|
2010-04-21 05:32:50 +08: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-07 04:19:48 +08:00
|
|
|
std::string data;
|
|
|
|
if (!read_file(fn, &data)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2010-04-21 05:32:50 +08:00
|
|
|
|
2015-05-13 04:54:41 +08:00
|
|
|
data.push_back('\n'); // TODO: fix parse_config.
|
2010-04-21 05:32:50 +08:00
|
|
|
parse_config(fn, data);
|
2015-02-05 06:46:36 +08:00
|
|
|
dump_parser_state();
|
2010-04-21 05:32:50 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-02-07 04:19:48 +08:00
|
|
|
static void parse_line_device(parse_state*, int nargs, char** args) {
|
2010-04-21 05:32:50 +08:00
|
|
|
set_device_permission(nargs, args);
|
|
|
|
}
|