blob: 677938e10706a368e62404c881cf420a089ff276 [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
22#include "keyword_map.h"
Tom Cherry7421fa12018-07-13 15:32:02 -070023#include "parser.h"
Tom Cherryed506f72017-05-25 15:58:59 -070024
Tom Cherry81f5d3e2017-06-22 12:53:17 -070025namespace android {
26namespace init {
27
Tom Cherry89bcc852017-08-02 17:01:36 -070028Result<Success> ParsePermissionsLine(std::vector<std::string>&& args,
29 std::vector<SysfsPermissions>* out_sysfs_permissions,
30 std::vector<Permissions>* out_dev_permissions) {
Tom Cherryed506f72017-05-25 15:58:59 -070031 bool is_sysfs = out_sysfs_permissions != nullptr;
32 if (is_sysfs && args.size() != 5) {
Tom Cherry89bcc852017-08-02 17:01:36 -070033 return Error() << "/sys/ lines must have 5 entries";
Tom Cherryed506f72017-05-25 15:58:59 -070034 }
35
36 if (!is_sysfs && args.size() != 4) {
Tom Cherry89bcc852017-08-02 17:01:36 -070037 return Error() << "/dev/ lines must have 4 entries";
Tom Cherryed506f72017-05-25 15:58:59 -070038 }
39
40 auto it = args.begin();
41 const std::string& name = *it++;
42
43 std::string sysfs_attribute;
44 if (is_sysfs) sysfs_attribute = *it++;
45
46 // args is now common to both sys and dev entries and contains: <perm> <uid> <gid>
47 std::string& perm_string = *it++;
48 char* end_pointer = 0;
49 mode_t perm = strtol(perm_string.c_str(), &end_pointer, 8);
50 if (end_pointer == nullptr || *end_pointer != '\0') {
Tom Cherry89bcc852017-08-02 17:01:36 -070051 return Error() << "invalid mode '" << perm_string << "'";
Tom Cherryed506f72017-05-25 15:58:59 -070052 }
53
54 std::string& uid_string = *it++;
55 passwd* pwd = getpwnam(uid_string.c_str());
56 if (!pwd) {
Tom Cherry89bcc852017-08-02 17:01:36 -070057 return Error() << "invalid uid '" << uid_string << "'";
Tom Cherryed506f72017-05-25 15:58:59 -070058 }
59 uid_t uid = pwd->pw_uid;
60
61 std::string& gid_string = *it++;
62 struct group* grp = getgrnam(gid_string.c_str());
63 if (!grp) {
Tom Cherry89bcc852017-08-02 17:01:36 -070064 return Error() << "invalid gid '" << gid_string << "'";
Tom Cherryed506f72017-05-25 15:58:59 -070065 }
66 gid_t gid = grp->gr_gid;
67
68 if (is_sysfs) {
69 out_sysfs_permissions->emplace_back(name, sysfs_attribute, perm, uid, gid);
70 } else {
71 out_dev_permissions->emplace_back(name, perm, uid, gid);
72 }
Tom Cherry89bcc852017-08-02 17:01:36 -070073 return Success();
Tom Cherryed506f72017-05-25 15:58:59 -070074}
75
Tom Cherry7421fa12018-07-13 15:32:02 -070076Result<Success> ParseFirmwareDirectoriesLine(std::vector<std::string>&& args,
77 std::vector<std::string>* firmware_directories) {
78 if (args.size() < 2) {
79 return Error() << "firmware_directories must have at least 1 entry";
80 }
81
82 std::move(std::next(args.begin()), args.end(), std::back_inserter(*firmware_directories));
83
84 return Success();
85}
86
Tom Cherry457e28f2018-08-01 13:12:20 -070087Result<Success> ParseModaliasHandlingLine(std::vector<std::string>&& args,
88 bool* enable_modalias_handling) {
89 if (args.size() != 2) {
90 return Error() << "modalias_handling lines take exactly one parameter";
91 }
92
93 if (args[1] == "enabled") {
94 *enable_modalias_handling = true;
95 } else if (args[1] == "disabled") {
96 *enable_modalias_handling = false;
97 } else {
98 return Error() << "modalias_handling takes either 'enabled' or 'disabled' as a parameter";
99 }
100
101 return Success();
102}
103
Tom Cherry7421fa12018-07-13 15:32:02 -0700104class SubsystemParser : public SectionParser {
105 public:
106 SubsystemParser(std::vector<Subsystem>* subsystems) : subsystems_(subsystems) {}
107 Result<Success> ParseSection(std::vector<std::string>&& args, const std::string& filename,
108 int line) override;
109 Result<Success> ParseLineSection(std::vector<std::string>&& args, int line) override;
110 Result<Success> EndSection() override;
111
112 private:
113 Result<Success> ParseDevName(std::vector<std::string>&& args);
114 Result<Success> ParseDirName(std::vector<std::string>&& args);
115
116 Subsystem subsystem_;
117 std::vector<Subsystem>* subsystems_;
118};
119
Tom Cherry89bcc852017-08-02 17:01:36 -0700120Result<Success> SubsystemParser::ParseSection(std::vector<std::string>&& args,
121 const std::string& filename, int line) {
Tom Cherryed506f72017-05-25 15:58:59 -0700122 if (args.size() != 2) {
Tom Cherry89bcc852017-08-02 17:01:36 -0700123 return Error() << "subsystems must have exactly one name";
Tom Cherryed506f72017-05-25 15:58:59 -0700124 }
125
126 if (std::find(subsystems_->begin(), subsystems_->end(), args[1]) != subsystems_->end()) {
Tom Cherry89bcc852017-08-02 17:01:36 -0700127 return Error() << "ignoring duplicate subsystem entry";
Tom Cherryed506f72017-05-25 15:58:59 -0700128 }
129
Tom Cherry9c8d6dd2017-08-17 09:38:01 -0700130 subsystem_ = Subsystem(std::move(args[1]));
Tom Cherryed506f72017-05-25 15:58:59 -0700131
Tom Cherry89bcc852017-08-02 17:01:36 -0700132 return Success();
Tom Cherryed506f72017-05-25 15:58:59 -0700133}
134
Tom Cherry89bcc852017-08-02 17:01:36 -0700135Result<Success> SubsystemParser::ParseDevName(std::vector<std::string>&& args) {
Tom Cherryed506f72017-05-25 15:58:59 -0700136 if (args[1] == "uevent_devname") {
Tom Cherry5f0198b2018-07-17 15:28:16 -0700137 subsystem_.devname_source_ = Subsystem::DEVNAME_UEVENT_DEVNAME;
Tom Cherry89bcc852017-08-02 17:01:36 -0700138 return Success();
Tom Cherryed506f72017-05-25 15:58:59 -0700139 }
140 if (args[1] == "uevent_devpath") {
Tom Cherry5f0198b2018-07-17 15:28:16 -0700141 subsystem_.devname_source_ = Subsystem::DEVNAME_UEVENT_DEVPATH;
Tom Cherry89bcc852017-08-02 17:01:36 -0700142 return Success();
Tom Cherryed506f72017-05-25 15:58:59 -0700143 }
144
Tom Cherry89bcc852017-08-02 17:01:36 -0700145 return Error() << "invalid devname '" << args[1] << "'";
Tom Cherryed506f72017-05-25 15:58:59 -0700146}
147
Tom Cherry89bcc852017-08-02 17:01:36 -0700148Result<Success> SubsystemParser::ParseDirName(std::vector<std::string>&& args) {
Tom Cherryed506f72017-05-25 15:58:59 -0700149 if (args[1].front() != '/') {
Tom Cherry89bcc852017-08-02 17:01:36 -0700150 return Error() << "dirname '" << args[1] << " ' does not start with '/'";
Tom Cherryed506f72017-05-25 15:58:59 -0700151 }
152
153 subsystem_.dir_name_ = args[1];
Tom Cherry89bcc852017-08-02 17:01:36 -0700154 return Success();
Tom Cherryed506f72017-05-25 15:58:59 -0700155}
156
Tom Cherry89bcc852017-08-02 17:01:36 -0700157Result<Success> SubsystemParser::ParseLineSection(std::vector<std::string>&& args, int line) {
158 using OptionParser = Result<Success> (SubsystemParser::*)(std::vector<std::string> && args);
159
Tom Cherryed506f72017-05-25 15:58:59 -0700160 static class OptionParserMap : public KeywordMap<OptionParser> {
161 private:
162 const Map& map() const override {
163 // clang-format off
164 static const Map option_parsers = {
165 {"devname", {1, 1, &SubsystemParser::ParseDevName}},
166 {"dirname", {1, 1, &SubsystemParser::ParseDirName}},
167 };
168 // clang-format on
169 return option_parsers;
170 }
171 } parser_map;
172
Tom Cherry89bcc852017-08-02 17:01:36 -0700173 auto parser = parser_map.FindFunction(args);
Tom Cherryed506f72017-05-25 15:58:59 -0700174
Tom Cherry89bcc852017-08-02 17:01:36 -0700175 if (!parser) return Error() << parser.error();
Tom Cherryed506f72017-05-25 15:58:59 -0700176
Tom Cherry89bcc852017-08-02 17:01:36 -0700177 return std::invoke(*parser, this, std::move(args));
Tom Cherryed506f72017-05-25 15:58:59 -0700178}
179
Steven Moreland7d0a5c32017-11-10 14:20:47 -0800180Result<Success> SubsystemParser::EndSection() {
Tom Cherryed506f72017-05-25 15:58:59 -0700181 subsystems_->emplace_back(std::move(subsystem_));
Steven Moreland7d0a5c32017-11-10 14:20:47 -0800182
183 return Success();
Tom Cherryed506f72017-05-25 15:58:59 -0700184}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700185
Tom Cherry7421fa12018-07-13 15:32:02 -0700186UeventdConfiguration ParseConfig(const std::vector<std::string>& configs) {
187 Parser parser;
188 UeventdConfiguration ueventd_configuration;
189
190 parser.AddSectionParser("subsystem",
191 std::make_unique<SubsystemParser>(&ueventd_configuration.subsystems));
192
193 using namespace std::placeholders;
194 parser.AddSingleLineParser(
195 "/sys/",
196 std::bind(ParsePermissionsLine, _1, &ueventd_configuration.sysfs_permissions, nullptr));
197 parser.AddSingleLineParser("/dev/", std::bind(ParsePermissionsLine, _1, nullptr,
198 &ueventd_configuration.dev_permissions));
199 parser.AddSingleLineParser("firmware_directories",
200 std::bind(ParseFirmwareDirectoriesLine, _1,
201 &ueventd_configuration.firmware_directories));
Tom Cherry457e28f2018-08-01 13:12:20 -0700202 parser.AddSingleLineParser("modalias_handling",
203 std::bind(ParseModaliasHandlingLine, _1,
204 &ueventd_configuration.enable_modalias_handling));
Tom Cherry7421fa12018-07-13 15:32:02 -0700205
206 for (const auto& config : configs) {
207 parser.ParseConfig(config);
208 }
209
210 return ueventd_configuration;
211}
212
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700213} // namespace init
214} // namespace android