• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "init_parser.h"
18 
19 #include <dirent.h>
20 
21 #include <android-base/chrono_utils.h>
22 #include <android-base/logging.h>
23 #include <android-base/stringprintf.h>
24 #include <android-base/strings.h>
25 
26 #include "parser.h"
27 #include "util.h"
28 
29 namespace android {
30 namespace init {
31 
Parser()32 Parser::Parser() {
33 }
34 
GetInstance()35 Parser& Parser::GetInstance() {
36     static Parser instance;
37     return instance;
38 }
39 
AddSectionParser(const std::string & name,std::unique_ptr<SectionParser> parser)40 void Parser::AddSectionParser(const std::string& name,
41                               std::unique_ptr<SectionParser> parser) {
42     section_parsers_[name] = std::move(parser);
43 }
44 
AddSingleLineParser(const std::string & prefix,LineCallback callback)45 void Parser::AddSingleLineParser(const std::string& prefix, LineCallback callback) {
46     line_callbacks_.emplace_back(prefix, callback);
47 }
48 
ParseData(const std::string & filename,const std::string & data)49 void Parser::ParseData(const std::string& filename, const std::string& data) {
50     //TODO: Use a parser with const input and remove this copy
51     std::vector<char> data_copy(data.begin(), data.end());
52     data_copy.push_back('\0');
53 
54     parse_state state;
55     state.line = 0;
56     state.ptr = &data_copy[0];
57     state.nexttoken = 0;
58 
59     SectionParser* section_parser = nullptr;
60     std::vector<std::string> args;
61 
62     for (;;) {
63         switch (next_token(&state)) {
64         case T_EOF:
65             if (section_parser) {
66                 section_parser->EndSection();
67             }
68             return;
69         case T_NEWLINE:
70             state.line++;
71             if (args.empty()) {
72                 break;
73             }
74             // If we have a line matching a prefix we recognize, call its callback and unset any
75             // current section parsers.  This is meant for /sys/ and /dev/ line entries for uevent.
76             for (const auto& [prefix, callback] : line_callbacks_) {
77                 if (android::base::StartsWith(args[0], prefix.c_str())) {
78                     if (section_parser) section_parser->EndSection();
79 
80                     std::string ret_err;
81                     if (!callback(std::move(args), &ret_err)) {
82                         LOG(ERROR) << filename << ": " << state.line << ": " << ret_err;
83                     }
84                     section_parser = nullptr;
85                     break;
86                 }
87             }
88             if (section_parsers_.count(args[0])) {
89                 if (section_parser) {
90                     section_parser->EndSection();
91                 }
92                 section_parser = section_parsers_[args[0]].get();
93                 std::string ret_err;
94                 if (!section_parser->ParseSection(std::move(args), filename, state.line, &ret_err)) {
95                     LOG(ERROR) << filename << ": " << state.line << ": " << ret_err;
96                     section_parser = nullptr;
97                 }
98             } else if (section_parser) {
99                 std::string ret_err;
100                 if (!section_parser->ParseLineSection(std::move(args), state.line, &ret_err)) {
101                     LOG(ERROR) << filename << ": " << state.line << ": " << ret_err;
102                 }
103             }
104             args.clear();
105             break;
106         case T_TEXT:
107             args.emplace_back(state.text);
108             break;
109         }
110     }
111 }
112 
ParseConfigFile(const std::string & path)113 bool Parser::ParseConfigFile(const std::string& path) {
114     LOG(INFO) << "Parsing file " << path << "...";
115     android::base::Timer t;
116     std::string data;
117     std::string err;
118     if (!ReadFile(path, &data, &err)) {
119         LOG(ERROR) << err;
120         return false;
121     }
122 
123     data.push_back('\n'); // TODO: fix parse_config.
124     ParseData(path, data);
125     for (const auto& [section_name, section_parser] : section_parsers_) {
126         section_parser->EndFile();
127     }
128 
129     LOG(VERBOSE) << "(Parsing " << path << " took " << t << ".)";
130     return true;
131 }
132 
ParseConfigDir(const std::string & path)133 bool Parser::ParseConfigDir(const std::string& path) {
134     LOG(INFO) << "Parsing directory " << path << "...";
135     std::unique_ptr<DIR, int(*)(DIR*)> config_dir(opendir(path.c_str()), closedir);
136     if (!config_dir) {
137         PLOG(ERROR) << "Could not import directory '" << path << "'";
138         return false;
139     }
140     dirent* current_file;
141     std::vector<std::string> files;
142     while ((current_file = readdir(config_dir.get()))) {
143         // Ignore directories and only process regular files.
144         if (current_file->d_type == DT_REG) {
145             std::string current_path =
146                 android::base::StringPrintf("%s/%s", path.c_str(), current_file->d_name);
147             files.emplace_back(current_path);
148         }
149     }
150     // Sort first so we load files in a consistent order (bug 31996208)
151     std::sort(files.begin(), files.end());
152     for (const auto& file : files) {
153         if (!ParseConfigFile(file)) {
154             LOG(ERROR) << "could not import file '" << file << "'";
155         }
156     }
157     return true;
158 }
159 
ParseConfig(const std::string & path)160 bool Parser::ParseConfig(const std::string& path) {
161     if (is_dir(path.c_str())) {
162         return ParseConfigDir(path);
163     }
164     return ParseConfigFile(path);
165 }
166 
167 }  // namespace init
168 }  // namespace android
169