blob: 25bab93389ea707f2c0b642c03c047b8615192b9 [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 Cherryed506f72017-05-25 15:58:59 -070024#include "keyword_map.h"
Tom Cherry7421fa12018-07-13 15:32:02 -070025#include "parser.h"
Tom Cherryed506f72017-05-25 15:58:59 -070026
Tom Cherrye2910102018-12-06 13:29:30 -080027using android::base::ParseByteCount;
28
Tom Cherry81f5d3e2017-06-22 12:53:17 -070029namespace android {
30namespace init {
31
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070032Result<void> ParsePermissionsLine(std::vector<std::string>&& args,
33 std::vector<SysfsPermissions>* out_sysfs_permissions,
34 std::vector<Permissions>* out_dev_permissions) {
Tom Cherryed506f72017-05-25 15:58:59 -070035 bool is_sysfs = out_sysfs_permissions != nullptr;
36 if (is_sysfs && args.size() != 5) {
Tom Cherry89bcc852017-08-02 17:01:36 -070037 return Error() << "/sys/ lines must have 5 entries";
Tom Cherryed506f72017-05-25 15:58:59 -070038 }
39
40 if (!is_sysfs && args.size() != 4) {
Tom Cherry89bcc852017-08-02 17:01:36 -070041 return Error() << "/dev/ lines must have 4 entries";
Tom Cherryed506f72017-05-25 15:58:59 -070042 }
43
44 auto it = args.begin();
45 const std::string& name = *it++;
46
47 std::string sysfs_attribute;
48 if (is_sysfs) sysfs_attribute = *it++;
49
50 // args is now common to both sys and dev entries and contains: <perm> <uid> <gid>
51 std::string& perm_string = *it++;
52 char* end_pointer = 0;
53 mode_t perm = strtol(perm_string.c_str(), &end_pointer, 8);
54 if (end_pointer == nullptr || *end_pointer != '\0') {
Tom Cherry89bcc852017-08-02 17:01:36 -070055 return Error() << "invalid mode '" << perm_string << "'";
Tom Cherryed506f72017-05-25 15:58:59 -070056 }
57
58 std::string& uid_string = *it++;
59 passwd* pwd = getpwnam(uid_string.c_str());
60 if (!pwd) {
Tom Cherry89bcc852017-08-02 17:01:36 -070061 return Error() << "invalid uid '" << uid_string << "'";
Tom Cherryed506f72017-05-25 15:58:59 -070062 }
63 uid_t uid = pwd->pw_uid;
64
65 std::string& gid_string = *it++;
66 struct group* grp = getgrnam(gid_string.c_str());
67 if (!grp) {
Tom Cherry89bcc852017-08-02 17:01:36 -070068 return Error() << "invalid gid '" << gid_string << "'";
Tom Cherryed506f72017-05-25 15:58:59 -070069 }
70 gid_t gid = grp->gr_gid;
71
72 if (is_sysfs) {
73 out_sysfs_permissions->emplace_back(name, sysfs_attribute, perm, uid, gid);
74 } else {
75 out_dev_permissions->emplace_back(name, perm, uid, gid);
76 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070077 return {};
Tom Cherryed506f72017-05-25 15:58:59 -070078}
79
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070080Result<void> ParseFirmwareDirectoriesLine(std::vector<std::string>&& args,
81 std::vector<std::string>* firmware_directories) {
Tom Cherry7421fa12018-07-13 15:32:02 -070082 if (args.size() < 2) {
83 return Error() << "firmware_directories must have at least 1 entry";
84 }
85
86 std::move(std::next(args.begin()), args.end(), std::back_inserter(*firmware_directories));
87
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070088 return {};
Tom Cherry7421fa12018-07-13 15:32:02 -070089}
90
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070091Result<void> ParseModaliasHandlingLine(std::vector<std::string>&& args,
92 bool* enable_modalias_handling) {
Tom Cherry457e28f2018-08-01 13:12:20 -070093 if (args.size() != 2) {
94 return Error() << "modalias_handling lines take exactly one parameter";
95 }
96
97 if (args[1] == "enabled") {
98 *enable_modalias_handling = true;
99 } else if (args[1] == "disabled") {
100 *enable_modalias_handling = false;
101 } else {
102 return Error() << "modalias_handling takes either 'enabled' or 'disabled' as a parameter";
103 }
104
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700105 return {};
Tom Cherry457e28f2018-08-01 13:12:20 -0700106}
107
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700108Result<void> ParseUeventSocketRcvbufSizeLine(std::vector<std::string>&& args,
109 size_t* uevent_socket_rcvbuf_size) {
Tom Cherrye2910102018-12-06 13:29:30 -0800110 if (args.size() != 2) {
111 return Error() << "uevent_socket_rcvbuf_size lines take exactly one parameter";
112 }
113
114 size_t parsed_size;
115 if (!ParseByteCount(args[1], &parsed_size)) {
116 return Error() << "could not parse size '" << args[1] << "' for uevent_socket_rcvbuf_line";
117 }
118
119 *uevent_socket_rcvbuf_size = parsed_size;
120
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700121 return {};
Tom Cherrye2910102018-12-06 13:29:30 -0800122}
123
Tom Cherry7421fa12018-07-13 15:32:02 -0700124class SubsystemParser : public SectionParser {
125 public:
126 SubsystemParser(std::vector<Subsystem>* subsystems) : subsystems_(subsystems) {}
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700127 Result<void> ParseSection(std::vector<std::string>&& args, const std::string& filename,
128 int line) override;
129 Result<void> ParseLineSection(std::vector<std::string>&& args, int line) override;
130 Result<void> EndSection() override;
Tom Cherry7421fa12018-07-13 15:32:02 -0700131
132 private:
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700133 Result<void> ParseDevName(std::vector<std::string>&& args);
134 Result<void> ParseDirName(std::vector<std::string>&& args);
Tom Cherry7421fa12018-07-13 15:32:02 -0700135
136 Subsystem subsystem_;
137 std::vector<Subsystem>* subsystems_;
138};
139
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700140Result<void> SubsystemParser::ParseSection(std::vector<std::string>&& args,
141 const std::string& filename, int line) {
Tom Cherryed506f72017-05-25 15:58:59 -0700142 if (args.size() != 2) {
Tom Cherry89bcc852017-08-02 17:01:36 -0700143 return Error() << "subsystems must have exactly one name";
Tom Cherryed506f72017-05-25 15:58:59 -0700144 }
145
146 if (std::find(subsystems_->begin(), subsystems_->end(), args[1]) != subsystems_->end()) {
Tom Cherry89bcc852017-08-02 17:01:36 -0700147 return Error() << "ignoring duplicate subsystem entry";
Tom Cherryed506f72017-05-25 15:58:59 -0700148 }
149
Tom Cherry9c8d6dd2017-08-17 09:38:01 -0700150 subsystem_ = Subsystem(std::move(args[1]));
Tom Cherryed506f72017-05-25 15:58:59 -0700151
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700152 return {};
Tom Cherryed506f72017-05-25 15:58:59 -0700153}
154
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700155Result<void> SubsystemParser::ParseDevName(std::vector<std::string>&& args) {
Tom Cherryed506f72017-05-25 15:58:59 -0700156 if (args[1] == "uevent_devname") {
Tom Cherry5f0198b2018-07-17 15:28:16 -0700157 subsystem_.devname_source_ = Subsystem::DEVNAME_UEVENT_DEVNAME;
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700158 return {};
Tom Cherryed506f72017-05-25 15:58:59 -0700159 }
160 if (args[1] == "uevent_devpath") {
Tom Cherry5f0198b2018-07-17 15:28:16 -0700161 subsystem_.devname_source_ = Subsystem::DEVNAME_UEVENT_DEVPATH;
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700162 return {};
Tom Cherryed506f72017-05-25 15:58:59 -0700163 }
164
Tom Cherry89bcc852017-08-02 17:01:36 -0700165 return Error() << "invalid devname '" << args[1] << "'";
Tom Cherryed506f72017-05-25 15:58:59 -0700166}
167
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700168Result<void> SubsystemParser::ParseDirName(std::vector<std::string>&& args) {
Tom Cherryed506f72017-05-25 15:58:59 -0700169 if (args[1].front() != '/') {
Tom Cherry89bcc852017-08-02 17:01:36 -0700170 return Error() << "dirname '" << args[1] << " ' does not start with '/'";
Tom Cherryed506f72017-05-25 15:58:59 -0700171 }
172
173 subsystem_.dir_name_ = args[1];
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700174 return {};
Tom Cherryed506f72017-05-25 15:58:59 -0700175}
176
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700177Result<void> SubsystemParser::ParseLineSection(std::vector<std::string>&& args, int line) {
178 using OptionParser = Result<void> (SubsystemParser::*)(std::vector<std::string> && args);
Tom Cherry89bcc852017-08-02 17:01:36 -0700179
Tom Cherryed506f72017-05-25 15:58:59 -0700180 static class OptionParserMap : public KeywordMap<OptionParser> {
181 private:
182 const Map& map() const override {
183 // clang-format off
184 static const Map option_parsers = {
185 {"devname", {1, 1, &SubsystemParser::ParseDevName}},
186 {"dirname", {1, 1, &SubsystemParser::ParseDirName}},
187 };
188 // clang-format on
189 return option_parsers;
190 }
191 } parser_map;
192
Tom Cherry89bcc852017-08-02 17:01:36 -0700193 auto parser = parser_map.FindFunction(args);
Tom Cherryed506f72017-05-25 15:58:59 -0700194
Tom Cherry89bcc852017-08-02 17:01:36 -0700195 if (!parser) return Error() << parser.error();
Tom Cherryed506f72017-05-25 15:58:59 -0700196
Tom Cherry89bcc852017-08-02 17:01:36 -0700197 return std::invoke(*parser, this, std::move(args));
Tom Cherryed506f72017-05-25 15:58:59 -0700198}
199
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700200Result<void> SubsystemParser::EndSection() {
Tom Cherryed506f72017-05-25 15:58:59 -0700201 subsystems_->emplace_back(std::move(subsystem_));
Steven Moreland7d0a5c32017-11-10 14:20:47 -0800202
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700203 return {};
Tom Cherryed506f72017-05-25 15:58:59 -0700204}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700205
Tom Cherry7421fa12018-07-13 15:32:02 -0700206UeventdConfiguration ParseConfig(const std::vector<std::string>& configs) {
207 Parser parser;
208 UeventdConfiguration ueventd_configuration;
209
210 parser.AddSectionParser("subsystem",
211 std::make_unique<SubsystemParser>(&ueventd_configuration.subsystems));
212
213 using namespace std::placeholders;
214 parser.AddSingleLineParser(
215 "/sys/",
216 std::bind(ParsePermissionsLine, _1, &ueventd_configuration.sysfs_permissions, nullptr));
217 parser.AddSingleLineParser("/dev/", std::bind(ParsePermissionsLine, _1, nullptr,
218 &ueventd_configuration.dev_permissions));
219 parser.AddSingleLineParser("firmware_directories",
220 std::bind(ParseFirmwareDirectoriesLine, _1,
221 &ueventd_configuration.firmware_directories));
Tom Cherry457e28f2018-08-01 13:12:20 -0700222 parser.AddSingleLineParser("modalias_handling",
223 std::bind(ParseModaliasHandlingLine, _1,
224 &ueventd_configuration.enable_modalias_handling));
Tom Cherrye2910102018-12-06 13:29:30 -0800225 parser.AddSingleLineParser("uevent_socket_rcvbuf_size",
226 std::bind(ParseUeventSocketRcvbufSizeLine, _1,
227 &ueventd_configuration.uevent_socket_rcvbuf_size));
Tom Cherry7421fa12018-07-13 15:32:02 -0700228
229 for (const auto& config : configs) {
230 parser.ParseConfig(config);
231 }
232
233 return ueventd_configuration;
234}
235
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700236} // namespace init
237} // namespace android