blob: 54b0d16aa88332ecacbcafd529944e26da7ed839 [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
87class SubsystemParser : public SectionParser {
88 public:
89 SubsystemParser(std::vector<Subsystem>* subsystems) : subsystems_(subsystems) {}
90 Result<Success> ParseSection(std::vector<std::string>&& args, const std::string& filename,
91 int line) override;
92 Result<Success> ParseLineSection(std::vector<std::string>&& args, int line) override;
93 Result<Success> EndSection() override;
94
95 private:
96 Result<Success> ParseDevName(std::vector<std::string>&& args);
97 Result<Success> ParseDirName(std::vector<std::string>&& args);
98
99 Subsystem subsystem_;
100 std::vector<Subsystem>* subsystems_;
101};
102
Tom Cherry89bcc852017-08-02 17:01:36 -0700103Result<Success> SubsystemParser::ParseSection(std::vector<std::string>&& args,
104 const std::string& filename, int line) {
Tom Cherryed506f72017-05-25 15:58:59 -0700105 if (args.size() != 2) {
Tom Cherry89bcc852017-08-02 17:01:36 -0700106 return Error() << "subsystems must have exactly one name";
Tom Cherryed506f72017-05-25 15:58:59 -0700107 }
108
109 if (std::find(subsystems_->begin(), subsystems_->end(), args[1]) != subsystems_->end()) {
Tom Cherry89bcc852017-08-02 17:01:36 -0700110 return Error() << "ignoring duplicate subsystem entry";
Tom Cherryed506f72017-05-25 15:58:59 -0700111 }
112
Tom Cherry9c8d6dd2017-08-17 09:38:01 -0700113 subsystem_ = Subsystem(std::move(args[1]));
Tom Cherryed506f72017-05-25 15:58:59 -0700114
Tom Cherry89bcc852017-08-02 17:01:36 -0700115 return Success();
Tom Cherryed506f72017-05-25 15:58:59 -0700116}
117
Tom Cherry89bcc852017-08-02 17:01:36 -0700118Result<Success> SubsystemParser::ParseDevName(std::vector<std::string>&& args) {
Tom Cherryed506f72017-05-25 15:58:59 -0700119 if (args[1] == "uevent_devname") {
Tom Cherry5f0198b2018-07-17 15:28:16 -0700120 subsystem_.devname_source_ = Subsystem::DEVNAME_UEVENT_DEVNAME;
Tom Cherry89bcc852017-08-02 17:01:36 -0700121 return Success();
Tom Cherryed506f72017-05-25 15:58:59 -0700122 }
123 if (args[1] == "uevent_devpath") {
Tom Cherry5f0198b2018-07-17 15:28:16 -0700124 subsystem_.devname_source_ = Subsystem::DEVNAME_UEVENT_DEVPATH;
Tom Cherry89bcc852017-08-02 17:01:36 -0700125 return Success();
Tom Cherryed506f72017-05-25 15:58:59 -0700126 }
127
Tom Cherry89bcc852017-08-02 17:01:36 -0700128 return Error() << "invalid devname '" << args[1] << "'";
Tom Cherryed506f72017-05-25 15:58:59 -0700129}
130
Tom Cherry89bcc852017-08-02 17:01:36 -0700131Result<Success> SubsystemParser::ParseDirName(std::vector<std::string>&& args) {
Tom Cherryed506f72017-05-25 15:58:59 -0700132 if (args[1].front() != '/') {
Tom Cherry89bcc852017-08-02 17:01:36 -0700133 return Error() << "dirname '" << args[1] << " ' does not start with '/'";
Tom Cherryed506f72017-05-25 15:58:59 -0700134 }
135
136 subsystem_.dir_name_ = args[1];
Tom Cherry89bcc852017-08-02 17:01:36 -0700137 return Success();
Tom Cherryed506f72017-05-25 15:58:59 -0700138}
139
Tom Cherry89bcc852017-08-02 17:01:36 -0700140Result<Success> SubsystemParser::ParseLineSection(std::vector<std::string>&& args, int line) {
141 using OptionParser = Result<Success> (SubsystemParser::*)(std::vector<std::string> && args);
142
Tom Cherryed506f72017-05-25 15:58:59 -0700143 static class OptionParserMap : public KeywordMap<OptionParser> {
144 private:
145 const Map& map() const override {
146 // clang-format off
147 static const Map option_parsers = {
148 {"devname", {1, 1, &SubsystemParser::ParseDevName}},
149 {"dirname", {1, 1, &SubsystemParser::ParseDirName}},
150 };
151 // clang-format on
152 return option_parsers;
153 }
154 } parser_map;
155
Tom Cherry89bcc852017-08-02 17:01:36 -0700156 auto parser = parser_map.FindFunction(args);
Tom Cherryed506f72017-05-25 15:58:59 -0700157
Tom Cherry89bcc852017-08-02 17:01:36 -0700158 if (!parser) return Error() << parser.error();
Tom Cherryed506f72017-05-25 15:58:59 -0700159
Tom Cherry89bcc852017-08-02 17:01:36 -0700160 return std::invoke(*parser, this, std::move(args));
Tom Cherryed506f72017-05-25 15:58:59 -0700161}
162
Steven Moreland7d0a5c32017-11-10 14:20:47 -0800163Result<Success> SubsystemParser::EndSection() {
Tom Cherryed506f72017-05-25 15:58:59 -0700164 subsystems_->emplace_back(std::move(subsystem_));
Steven Moreland7d0a5c32017-11-10 14:20:47 -0800165
166 return Success();
Tom Cherryed506f72017-05-25 15:58:59 -0700167}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700168
Tom Cherry7421fa12018-07-13 15:32:02 -0700169UeventdConfiguration ParseConfig(const std::vector<std::string>& configs) {
170 Parser parser;
171 UeventdConfiguration ueventd_configuration;
172
173 parser.AddSectionParser("subsystem",
174 std::make_unique<SubsystemParser>(&ueventd_configuration.subsystems));
175
176 using namespace std::placeholders;
177 parser.AddSingleLineParser(
178 "/sys/",
179 std::bind(ParsePermissionsLine, _1, &ueventd_configuration.sysfs_permissions, nullptr));
180 parser.AddSingleLineParser("/dev/", std::bind(ParsePermissionsLine, _1, nullptr,
181 &ueventd_configuration.dev_permissions));
182 parser.AddSingleLineParser("firmware_directories",
183 std::bind(ParseFirmwareDirectoriesLine, _1,
184 &ueventd_configuration.firmware_directories));
185
186 for (const auto& config : configs) {
187 parser.ParseConfig(config);
188 }
189
190 return ueventd_configuration;
191}
192
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700193} // namespace init
194} // namespace android