blob: 9a14406bc5c276bdf15124f5f4ed2c9cc5600781 [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
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700154Result<void> ParseUeventSocketRcvbufSizeLine(std::vector<std::string>&& args,
155 size_t* uevent_socket_rcvbuf_size) {
Tom Cherrye2910102018-12-06 13:29:30 -0800156 if (args.size() != 2) {
157 return Error() << "uevent_socket_rcvbuf_size lines take exactly one parameter";
158 }
159
160 size_t parsed_size;
161 if (!ParseByteCount(args[1], &parsed_size)) {
162 return Error() << "could not parse size '" << args[1] << "' for uevent_socket_rcvbuf_line";
163 }
164
165 *uevent_socket_rcvbuf_size = parsed_size;
166
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700167 return {};
Tom Cherrye2910102018-12-06 13:29:30 -0800168}
169
Tom Cherry7421fa12018-07-13 15:32:02 -0700170class SubsystemParser : public SectionParser {
171 public:
172 SubsystemParser(std::vector<Subsystem>* subsystems) : subsystems_(subsystems) {}
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700173 Result<void> ParseSection(std::vector<std::string>&& args, const std::string& filename,
174 int line) override;
175 Result<void> ParseLineSection(std::vector<std::string>&& args, int line) override;
176 Result<void> EndSection() override;
Tom Cherry7421fa12018-07-13 15:32:02 -0700177
178 private:
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700179 Result<void> ParseDevName(std::vector<std::string>&& args);
180 Result<void> ParseDirName(std::vector<std::string>&& args);
Tom Cherry7421fa12018-07-13 15:32:02 -0700181
182 Subsystem subsystem_;
183 std::vector<Subsystem>* subsystems_;
184};
185
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700186Result<void> SubsystemParser::ParseSection(std::vector<std::string>&& args,
187 const std::string& filename, int line) {
Tom Cherryed506f72017-05-25 15:58:59 -0700188 if (args.size() != 2) {
Tom Cherry89bcc852017-08-02 17:01:36 -0700189 return Error() << "subsystems must have exactly one name";
Tom Cherryed506f72017-05-25 15:58:59 -0700190 }
191
192 if (std::find(subsystems_->begin(), subsystems_->end(), args[1]) != subsystems_->end()) {
Tom Cherry89bcc852017-08-02 17:01:36 -0700193 return Error() << "ignoring duplicate subsystem entry";
Tom Cherryed506f72017-05-25 15:58:59 -0700194 }
195
Tom Cherry9c8d6dd2017-08-17 09:38:01 -0700196 subsystem_ = Subsystem(std::move(args[1]));
Tom Cherryed506f72017-05-25 15:58:59 -0700197
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700198 return {};
Tom Cherryed506f72017-05-25 15:58:59 -0700199}
200
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700201Result<void> SubsystemParser::ParseDevName(std::vector<std::string>&& args) {
Tom Cherryed506f72017-05-25 15:58:59 -0700202 if (args[1] == "uevent_devname") {
Tom Cherry5f0198b2018-07-17 15:28:16 -0700203 subsystem_.devname_source_ = Subsystem::DEVNAME_UEVENT_DEVNAME;
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700204 return {};
Tom Cherryed506f72017-05-25 15:58:59 -0700205 }
206 if (args[1] == "uevent_devpath") {
Tom Cherry5f0198b2018-07-17 15:28:16 -0700207 subsystem_.devname_source_ = Subsystem::DEVNAME_UEVENT_DEVPATH;
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700208 return {};
Tom Cherryed506f72017-05-25 15:58:59 -0700209 }
210
Tom Cherry89bcc852017-08-02 17:01:36 -0700211 return Error() << "invalid devname '" << args[1] << "'";
Tom Cherryed506f72017-05-25 15:58:59 -0700212}
213
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700214Result<void> SubsystemParser::ParseDirName(std::vector<std::string>&& args) {
Tom Cherryed506f72017-05-25 15:58:59 -0700215 if (args[1].front() != '/') {
Tom Cherry89bcc852017-08-02 17:01:36 -0700216 return Error() << "dirname '" << args[1] << " ' does not start with '/'";
Tom Cherryed506f72017-05-25 15:58:59 -0700217 }
218
219 subsystem_.dir_name_ = args[1];
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700220 return {};
Tom Cherryed506f72017-05-25 15:58:59 -0700221}
222
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700223Result<void> SubsystemParser::ParseLineSection(std::vector<std::string>&& args, int line) {
224 using OptionParser = Result<void> (SubsystemParser::*)(std::vector<std::string> && args);
Tom Cherryd52a5b32019-07-22 16:05:36 -0700225 // clang-format off
226 static const KeywordMap<OptionParser> parser_map = {
227 {"devname", {1, 1, &SubsystemParser::ParseDevName}},
228 {"dirname", {1, 1, &SubsystemParser::ParseDirName}},
229 };
230 // clang-format on
Tom Cherry89bcc852017-08-02 17:01:36 -0700231
Tom Cherryd52a5b32019-07-22 16:05:36 -0700232 auto parser = parser_map.Find(args);
Tom Cherryed506f72017-05-25 15:58:59 -0700233
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900234 if (!parser.ok()) return Error() << parser.error();
Tom Cherryed506f72017-05-25 15:58:59 -0700235
Tom Cherry89bcc852017-08-02 17:01:36 -0700236 return std::invoke(*parser, this, std::move(args));
Tom Cherryed506f72017-05-25 15:58:59 -0700237}
238
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700239Result<void> SubsystemParser::EndSection() {
Tom Cherryed506f72017-05-25 15:58:59 -0700240 subsystems_->emplace_back(std::move(subsystem_));
Steven Moreland7d0a5c32017-11-10 14:20:47 -0800241
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700242 return {};
Tom Cherryed506f72017-05-25 15:58:59 -0700243}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700244
Tom Cherry71dd7062020-12-11 09:26:55 -0800245UeventdConfiguration ParseConfig(const std::vector<std::string>& configs) {
Tom Cherry7421fa12018-07-13 15:32:02 -0700246 Parser parser;
247 UeventdConfiguration ueventd_configuration;
248
Tom Cherry5b271792020-12-08 13:18:14 -0800249 parser.AddSectionParser("import", std::make_unique<ImportParser>(&parser));
Tom Cherry7421fa12018-07-13 15:32:02 -0700250 parser.AddSectionParser("subsystem",
251 std::make_unique<SubsystemParser>(&ueventd_configuration.subsystems));
252
253 using namespace std::placeholders;
254 parser.AddSingleLineParser(
255 "/sys/",
256 std::bind(ParsePermissionsLine, _1, &ueventd_configuration.sysfs_permissions, nullptr));
257 parser.AddSingleLineParser("/dev/", std::bind(ParsePermissionsLine, _1, nullptr,
258 &ueventd_configuration.dev_permissions));
259 parser.AddSingleLineParser("firmware_directories",
260 std::bind(ParseFirmwareDirectoriesLine, _1,
261 &ueventd_configuration.firmware_directories));
Tom Cherrydcb3d152019-08-07 16:02:28 -0700262 parser.AddSingleLineParser("external_firmware_handler",
263 std::bind(ParseExternalFirmwareHandlerLine, _1,
264 &ueventd_configuration.external_firmware_handlers));
Tom Cherry457e28f2018-08-01 13:12:20 -0700265 parser.AddSingleLineParser("modalias_handling",
Tom Cherry4233ec72019-09-06 10:52:31 -0700266 std::bind(ParseEnabledDisabledLine, _1,
Tom Cherry457e28f2018-08-01 13:12:20 -0700267 &ueventd_configuration.enable_modalias_handling));
Tom Cherrye2910102018-12-06 13:29:30 -0800268 parser.AddSingleLineParser("uevent_socket_rcvbuf_size",
269 std::bind(ParseUeventSocketRcvbufSizeLine, _1,
270 &ueventd_configuration.uevent_socket_rcvbuf_size));
Tom Cherry4233ec72019-09-06 10:52:31 -0700271 parser.AddSingleLineParser("parallel_restorecon",
272 std::bind(ParseEnabledDisabledLine, _1,
273 &ueventd_configuration.enable_parallel_restorecon));
Tom Cherry7421fa12018-07-13 15:32:02 -0700274
Tom Cherry71dd7062020-12-11 09:26:55 -0800275 for (const auto& config : configs) {
276 parser.ParseConfig(config);
277 }
Tom Cherry7421fa12018-07-13 15:32:02 -0700278
279 return ueventd_configuration;
280}
281
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700282} // namespace init
283} // namespace android