blob: d34672e245c1659432d33ad2570af0c88ef5b9ee [file] [log] [blame]
Tom Cherryed506f72017-05-25 15:58:59 -07001/*
2 * Copyright (C) 2017 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 "ueventd_parser.h"
18
19#include <grp.h>
20#include <pwd.h>
21
Tom Cherrye2910102018-12-06 13:29:30 -080022#include <android-base/parseint.h>
23
Tom Cherry5b271792020-12-08 13:18:14 -080024#include "import_parser.h"
Tom Cherryed506f72017-05-25 15:58:59 -070025#include "keyword_map.h"
Tom Cherry7421fa12018-07-13 15:32:02 -070026#include "parser.h"
Tom Cherryed506f72017-05-25 15:58:59 -070027
Tom Cherrye2910102018-12-06 13:29:30 -080028using android::base::ParseByteCount;
29
Tom Cherry81f5d3e2017-06-22 12:53:17 -070030namespace android {
31namespace init {
32
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070033Result<void> ParsePermissionsLine(std::vector<std::string>&& args,
34 std::vector<SysfsPermissions>* out_sysfs_permissions,
35 std::vector<Permissions>* out_dev_permissions) {
Tom Cherryed506f72017-05-25 15:58:59 -070036 bool is_sysfs = out_sysfs_permissions != nullptr;
Tom Cherry47031c82020-12-07 13:33:46 -080037 if (is_sysfs && !(args.size() == 5 || args.size() == 6)) {
38 return Error() << "/sys/ lines must have 5 or 6 entries";
Tom Cherryed506f72017-05-25 15:58:59 -070039 }
40
Tom Cherry47031c82020-12-07 13:33:46 -080041 if (!is_sysfs && !(args.size() == 4 || args.size() == 5)) {
42 return Error() << "/dev/ lines must have 4 or 5 entries";
Tom Cherryed506f72017-05-25 15:58:59 -070043 }
44
45 auto it = args.begin();
46 const std::string& name = *it++;
47
48 std::string sysfs_attribute;
49 if (is_sysfs) sysfs_attribute = *it++;
50
51 // args is now common to both sys and dev entries and contains: <perm> <uid> <gid>
52 std::string& perm_string = *it++;
53 char* end_pointer = 0;
54 mode_t perm = strtol(perm_string.c_str(), &end_pointer, 8);
55 if (end_pointer == nullptr || *end_pointer != '\0') {
Tom Cherry89bcc852017-08-02 17:01:36 -070056 return Error() << "invalid mode '" << perm_string << "'";
Tom Cherryed506f72017-05-25 15:58:59 -070057 }
58
59 std::string& uid_string = *it++;
60 passwd* pwd = getpwnam(uid_string.c_str());
61 if (!pwd) {
Tom Cherry89bcc852017-08-02 17:01:36 -070062 return Error() << "invalid uid '" << uid_string << "'";
Tom Cherryed506f72017-05-25 15:58:59 -070063 }
64 uid_t uid = pwd->pw_uid;
65
66 std::string& gid_string = *it++;
67 struct group* grp = getgrnam(gid_string.c_str());
68 if (!grp) {
Tom Cherry89bcc852017-08-02 17:01:36 -070069 return Error() << "invalid gid '" << gid_string << "'";
Tom Cherryed506f72017-05-25 15:58:59 -070070 }
71 gid_t gid = grp->gr_gid;
72
Tom Cherry47031c82020-12-07 13:33:46 -080073 bool no_fnm_pathname = false;
74 if (it != args.end()) {
75 std::string& flags = *it++;
76 if (flags != "no_fnm_pathname") {
77 return Error() << "invalid option '" << flags << "', only no_fnm_pathname is supported";
78 }
79 no_fnm_pathname = true;
80 }
81
Tom Cherryed506f72017-05-25 15:58:59 -070082 if (is_sysfs) {
Tom Cherry47031c82020-12-07 13:33:46 -080083 out_sysfs_permissions->emplace_back(name, sysfs_attribute, perm, uid, gid, no_fnm_pathname);
Tom Cherryed506f72017-05-25 15:58:59 -070084 } else {
Tom Cherry47031c82020-12-07 13:33:46 -080085 out_dev_permissions->emplace_back(name, perm, uid, gid, no_fnm_pathname);
Tom Cherryed506f72017-05-25 15:58:59 -070086 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070087 return {};
Tom Cherryed506f72017-05-25 15:58:59 -070088}
89
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070090Result<void> ParseFirmwareDirectoriesLine(std::vector<std::string>&& args,
91 std::vector<std::string>* firmware_directories) {
Tom Cherry7421fa12018-07-13 15:32:02 -070092 if (args.size() < 2) {
93 return Error() << "firmware_directories must have at least 1 entry";
94 }
95
96 std::move(std::next(args.begin()), args.end(), std::back_inserter(*firmware_directories));
97
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070098 return {};
Tom Cherry7421fa12018-07-13 15:32:02 -070099}
100
Tom Cherrydcb3d152019-08-07 16:02:28 -0700101Result<void> ParseExternalFirmwareHandlerLine(
102 std::vector<std::string>&& args,
103 std::vector<ExternalFirmwareHandler>* external_firmware_handlers) {
Suchang Woo10c63742021-05-13 18:56:31 +0900104 if (args.size() != 4 && args.size() != 5) {
105 return Error() << "external_firmware_handler lines must have 3 or 4 parameters";
Tom Cherrydcb3d152019-08-07 16:02:28 -0700106 }
107
108 if (std::find_if(external_firmware_handlers->begin(), external_firmware_handlers->end(),
Suchang Woo8681f7e2021-04-06 10:55:34 +0900109 [&args](const auto& other) { return other.devpath == args[1]; }) !=
Tom Cherrydcb3d152019-08-07 16:02:28 -0700110 external_firmware_handlers->end()) {
111 return Error() << "found a previous external_firmware_handler with the same devpath, '"
Suchang Woo8681f7e2021-04-06 10:55:34 +0900112 << args[1] << "'";
Tom Cherrydcb3d152019-08-07 16:02:28 -0700113 }
114
115 passwd* pwd = getpwnam(args[2].c_str());
116 if (!pwd) {
117 return ErrnoError() << "invalid handler uid'" << args[2] << "'";
118 }
119
Suchang Woo10c63742021-05-13 18:56:31 +0900120 gid_t gid = 0;
121 int handler_index = 3;
122 if (args.size() == 5) {
123 struct group* grp = getgrnam(args[3].c_str());
124 if (!grp) {
125 return ErrnoError() << "invalid handler gid '" << args[3] << "'";
126 }
127 gid = grp->gr_gid;
128 handler_index = 4;
129 }
130
131 ExternalFirmwareHandler handler(std::move(args[1]), pwd->pw_uid, gid,
132 std::move(args[handler_index]));
Tom Cherrydcb3d152019-08-07 16:02:28 -0700133 external_firmware_handlers->emplace_back(std::move(handler));
134
135 return {};
136}
137
Tom Cherry4233ec72019-09-06 10:52:31 -0700138Result<void> ParseEnabledDisabledLine(std::vector<std::string>&& args, bool* feature) {
Tom Cherry457e28f2018-08-01 13:12:20 -0700139 if (args.size() != 2) {
Tom Cherry4233ec72019-09-06 10:52:31 -0700140 return Error() << args[0] << " lines take exactly one parameter";
Tom Cherry457e28f2018-08-01 13:12:20 -0700141 }
142
143 if (args[1] == "enabled") {
Tom Cherry4233ec72019-09-06 10:52:31 -0700144 *feature = true;
Tom Cherry457e28f2018-08-01 13:12:20 -0700145 } else if (args[1] == "disabled") {
Tom Cherry4233ec72019-09-06 10:52:31 -0700146 *feature = false;
Tom Cherry457e28f2018-08-01 13:12:20 -0700147 } else {
Tom Cherry4233ec72019-09-06 10:52:31 -0700148 return Error() << args[0] << " takes either 'enabled' or 'disabled' as a parameter";
Tom Cherry457e28f2018-08-01 13:12:20 -0700149 }
150
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700151 return {};
Tom Cherry457e28f2018-08-01 13:12:20 -0700152}
153
yuehu middffa0e2021-05-01 14:36:58 +0800154Result<void> ParseParallelRestoreconDirsLine(std::vector<std::string>&& args,
155 std::vector<std::string>* parallel_restorecon_dirs) {
156 if (args.size() != 2) {
157 return Error() << "parallel_restorecon_dir lines must have exactly 2 parameters";
158 }
159
160 std::move(std::next(args.begin()), args.end(), std::back_inserter(*parallel_restorecon_dirs));
161
162 return {};
163}
164
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700165Result<void> ParseUeventSocketRcvbufSizeLine(std::vector<std::string>&& args,
166 size_t* uevent_socket_rcvbuf_size) {
Tom Cherrye2910102018-12-06 13:29:30 -0800167 if (args.size() != 2) {
168 return Error() << "uevent_socket_rcvbuf_size lines take exactly one parameter";
169 }
170
171 size_t parsed_size;
172 if (!ParseByteCount(args[1], &parsed_size)) {
173 return Error() << "could not parse size '" << args[1] << "' for uevent_socket_rcvbuf_line";
174 }
175
176 *uevent_socket_rcvbuf_size = parsed_size;
177
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700178 return {};
Tom Cherrye2910102018-12-06 13:29:30 -0800179}
180
Tom Cherry7421fa12018-07-13 15:32:02 -0700181class SubsystemParser : public SectionParser {
182 public:
183 SubsystemParser(std::vector<Subsystem>* subsystems) : subsystems_(subsystems) {}
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700184 Result<void> ParseSection(std::vector<std::string>&& args, const std::string& filename,
185 int line) override;
186 Result<void> ParseLineSection(std::vector<std::string>&& args, int line) override;
187 Result<void> EndSection() override;
Tom Cherry7421fa12018-07-13 15:32:02 -0700188
189 private:
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700190 Result<void> ParseDevName(std::vector<std::string>&& args);
191 Result<void> ParseDirName(std::vector<std::string>&& args);
Tom Cherry7421fa12018-07-13 15:32:02 -0700192
193 Subsystem subsystem_;
194 std::vector<Subsystem>* subsystems_;
195};
196
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700197Result<void> SubsystemParser::ParseSection(std::vector<std::string>&& args,
198 const std::string& filename, int line) {
Tom Cherryed506f72017-05-25 15:58:59 -0700199 if (args.size() != 2) {
Tom Cherry89bcc852017-08-02 17:01:36 -0700200 return Error() << "subsystems must have exactly one name";
Tom Cherryed506f72017-05-25 15:58:59 -0700201 }
202
203 if (std::find(subsystems_->begin(), subsystems_->end(), args[1]) != subsystems_->end()) {
Tom Cherry89bcc852017-08-02 17:01:36 -0700204 return Error() << "ignoring duplicate subsystem entry";
Tom Cherryed506f72017-05-25 15:58:59 -0700205 }
206
Tom Cherry9c8d6dd2017-08-17 09:38:01 -0700207 subsystem_ = Subsystem(std::move(args[1]));
Tom Cherryed506f72017-05-25 15:58:59 -0700208
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700209 return {};
Tom Cherryed506f72017-05-25 15:58:59 -0700210}
211
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700212Result<void> SubsystemParser::ParseDevName(std::vector<std::string>&& args) {
Tom Cherryed506f72017-05-25 15:58:59 -0700213 if (args[1] == "uevent_devname") {
Tom Cherry5f0198b2018-07-17 15:28:16 -0700214 subsystem_.devname_source_ = Subsystem::DEVNAME_UEVENT_DEVNAME;
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700215 return {};
Tom Cherryed506f72017-05-25 15:58:59 -0700216 }
217 if (args[1] == "uevent_devpath") {
Tom Cherry5f0198b2018-07-17 15:28:16 -0700218 subsystem_.devname_source_ = Subsystem::DEVNAME_UEVENT_DEVPATH;
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700219 return {};
Tom Cherryed506f72017-05-25 15:58:59 -0700220 }
221
Tom Cherry89bcc852017-08-02 17:01:36 -0700222 return Error() << "invalid devname '" << args[1] << "'";
Tom Cherryed506f72017-05-25 15:58:59 -0700223}
224
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700225Result<void> SubsystemParser::ParseDirName(std::vector<std::string>&& args) {
Tom Cherryed506f72017-05-25 15:58:59 -0700226 if (args[1].front() != '/') {
Tom Cherry89bcc852017-08-02 17:01:36 -0700227 return Error() << "dirname '" << args[1] << " ' does not start with '/'";
Tom Cherryed506f72017-05-25 15:58:59 -0700228 }
229
230 subsystem_.dir_name_ = args[1];
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700231 return {};
Tom Cherryed506f72017-05-25 15:58:59 -0700232}
233
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700234Result<void> SubsystemParser::ParseLineSection(std::vector<std::string>&& args, int line) {
235 using OptionParser = Result<void> (SubsystemParser::*)(std::vector<std::string> && args);
Tom Cherryd52a5b32019-07-22 16:05:36 -0700236 // clang-format off
237 static const KeywordMap<OptionParser> parser_map = {
238 {"devname", {1, 1, &SubsystemParser::ParseDevName}},
239 {"dirname", {1, 1, &SubsystemParser::ParseDirName}},
240 };
241 // clang-format on
Tom Cherry89bcc852017-08-02 17:01:36 -0700242
Tom Cherryd52a5b32019-07-22 16:05:36 -0700243 auto parser = parser_map.Find(args);
Tom Cherryed506f72017-05-25 15:58:59 -0700244
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900245 if (!parser.ok()) return Error() << parser.error();
Tom Cherryed506f72017-05-25 15:58:59 -0700246
Tom Cherry89bcc852017-08-02 17:01:36 -0700247 return std::invoke(*parser, this, std::move(args));
Tom Cherryed506f72017-05-25 15:58:59 -0700248}
249
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700250Result<void> SubsystemParser::EndSection() {
Tom Cherryed506f72017-05-25 15:58:59 -0700251 subsystems_->emplace_back(std::move(subsystem_));
Steven Moreland7d0a5c32017-11-10 14:20:47 -0800252
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700253 return {};
Tom Cherryed506f72017-05-25 15:58:59 -0700254}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700255
Tom Cherry71dd7062020-12-11 09:26:55 -0800256UeventdConfiguration ParseConfig(const std::vector<std::string>& configs) {
Tom Cherry7421fa12018-07-13 15:32:02 -0700257 Parser parser;
258 UeventdConfiguration ueventd_configuration;
259
Tom Cherry5b271792020-12-08 13:18:14 -0800260 parser.AddSectionParser("import", std::make_unique<ImportParser>(&parser));
Tom Cherry7421fa12018-07-13 15:32:02 -0700261 parser.AddSectionParser("subsystem",
262 std::make_unique<SubsystemParser>(&ueventd_configuration.subsystems));
263
264 using namespace std::placeholders;
265 parser.AddSingleLineParser(
266 "/sys/",
267 std::bind(ParsePermissionsLine, _1, &ueventd_configuration.sysfs_permissions, nullptr));
268 parser.AddSingleLineParser("/dev/", std::bind(ParsePermissionsLine, _1, nullptr,
269 &ueventd_configuration.dev_permissions));
270 parser.AddSingleLineParser("firmware_directories",
271 std::bind(ParseFirmwareDirectoriesLine, _1,
272 &ueventd_configuration.firmware_directories));
Tom Cherrydcb3d152019-08-07 16:02:28 -0700273 parser.AddSingleLineParser("external_firmware_handler",
274 std::bind(ParseExternalFirmwareHandlerLine, _1,
275 &ueventd_configuration.external_firmware_handlers));
Tom Cherry457e28f2018-08-01 13:12:20 -0700276 parser.AddSingleLineParser("modalias_handling",
Tom Cherry4233ec72019-09-06 10:52:31 -0700277 std::bind(ParseEnabledDisabledLine, _1,
Tom Cherry457e28f2018-08-01 13:12:20 -0700278 &ueventd_configuration.enable_modalias_handling));
Tom Cherrye2910102018-12-06 13:29:30 -0800279 parser.AddSingleLineParser("uevent_socket_rcvbuf_size",
280 std::bind(ParseUeventSocketRcvbufSizeLine, _1,
281 &ueventd_configuration.uevent_socket_rcvbuf_size));
yuehu middffa0e2021-05-01 14:36:58 +0800282 parser.AddSingleLineParser("parallel_restorecon_dir",
283 std::bind(ParseParallelRestoreconDirsLine, _1,
284 &ueventd_configuration.parallel_restorecon_dirs));
Tom Cherry4233ec72019-09-06 10:52:31 -0700285 parser.AddSingleLineParser("parallel_restorecon",
286 std::bind(ParseEnabledDisabledLine, _1,
287 &ueventd_configuration.enable_parallel_restorecon));
Tom Cherry7421fa12018-07-13 15:32:02 -0700288
Tom Cherry71dd7062020-12-11 09:26:55 -0800289 for (const auto& config : configs) {
290 parser.ParseConfig(config);
291 }
Tom Cherry7421fa12018-07-13 15:32:02 -0700292
293 return ueventd_configuration;
294}
295
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700296} // namespace init
297} // namespace android