• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 <android-base/parseint.h>
23 
24 #include "keyword_map.h"
25 #include "parser.h"
26 
27 using android::base::ParseByteCount;
28 
29 namespace android {
30 namespace init {
31 
ParsePermissionsLine(std::vector<std::string> && args,std::vector<SysfsPermissions> * out_sysfs_permissions,std::vector<Permissions> * out_dev_permissions)32 Result<Success> ParsePermissionsLine(std::vector<std::string>&& args,
33                                      std::vector<SysfsPermissions>* out_sysfs_permissions,
34                                      std::vector<Permissions>* out_dev_permissions) {
35     bool is_sysfs = out_sysfs_permissions != nullptr;
36     if (is_sysfs && args.size() != 5) {
37         return Error() << "/sys/ lines must have 5 entries";
38     }
39 
40     if (!is_sysfs && args.size() != 4) {
41         return Error() << "/dev/ lines must have 4 entries";
42     }
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') {
55         return Error() << "invalid mode '" << perm_string << "'";
56     }
57 
58     std::string& uid_string = *it++;
59     passwd* pwd = getpwnam(uid_string.c_str());
60     if (!pwd) {
61         return Error() << "invalid uid '" << uid_string << "'";
62     }
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) {
68         return Error() << "invalid gid '" << gid_string << "'";
69     }
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     }
77     return Success();
78 }
79 
ParseFirmwareDirectoriesLine(std::vector<std::string> && args,std::vector<std::string> * firmware_directories)80 Result<Success> ParseFirmwareDirectoriesLine(std::vector<std::string>&& args,
81                                              std::vector<std::string>* firmware_directories) {
82     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 
88     return Success();
89 }
90 
ParseModaliasHandlingLine(std::vector<std::string> && args,bool * enable_modalias_handling)91 Result<Success> ParseModaliasHandlingLine(std::vector<std::string>&& args,
92                                           bool* enable_modalias_handling) {
93     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 
105     return Success();
106 }
107 
ParseUeventSocketRcvbufSizeLine(std::vector<std::string> && args,size_t * uevent_socket_rcvbuf_size)108 Result<Success> ParseUeventSocketRcvbufSizeLine(std::vector<std::string>&& args,
109                                                 size_t* uevent_socket_rcvbuf_size) {
110     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 
121     return Success();
122 }
123 
124 class SubsystemParser : public SectionParser {
125   public:
SubsystemParser(std::vector<Subsystem> * subsystems)126     SubsystemParser(std::vector<Subsystem>* subsystems) : subsystems_(subsystems) {}
127     Result<Success> ParseSection(std::vector<std::string>&& args, const std::string& filename,
128                                  int line) override;
129     Result<Success> ParseLineSection(std::vector<std::string>&& args, int line) override;
130     Result<Success> EndSection() override;
131 
132   private:
133     Result<Success> ParseDevName(std::vector<std::string>&& args);
134     Result<Success> ParseDirName(std::vector<std::string>&& args);
135 
136     Subsystem subsystem_;
137     std::vector<Subsystem>* subsystems_;
138 };
139 
ParseSection(std::vector<std::string> && args,const std::string & filename,int line)140 Result<Success> SubsystemParser::ParseSection(std::vector<std::string>&& args,
141                                               const std::string& filename, int line) {
142     if (args.size() != 2) {
143         return Error() << "subsystems must have exactly one name";
144     }
145 
146     if (std::find(subsystems_->begin(), subsystems_->end(), args[1]) != subsystems_->end()) {
147         return Error() << "ignoring duplicate subsystem entry";
148     }
149 
150     subsystem_ = Subsystem(std::move(args[1]));
151 
152     return Success();
153 }
154 
ParseDevName(std::vector<std::string> && args)155 Result<Success> SubsystemParser::ParseDevName(std::vector<std::string>&& args) {
156     if (args[1] == "uevent_devname") {
157         subsystem_.devname_source_ = Subsystem::DEVNAME_UEVENT_DEVNAME;
158         return Success();
159     }
160     if (args[1] == "uevent_devpath") {
161         subsystem_.devname_source_ = Subsystem::DEVNAME_UEVENT_DEVPATH;
162         return Success();
163     }
164 
165     return Error() << "invalid devname '" << args[1] << "'";
166 }
167 
ParseDirName(std::vector<std::string> && args)168 Result<Success> SubsystemParser::ParseDirName(std::vector<std::string>&& args) {
169     if (args[1].front() != '/') {
170         return Error() << "dirname '" << args[1] << " ' does not start with '/'";
171     }
172 
173     subsystem_.dir_name_ = args[1];
174     return Success();
175 }
176 
ParseLineSection(std::vector<std::string> && args,int line)177 Result<Success> SubsystemParser::ParseLineSection(std::vector<std::string>&& args, int line) {
178     using OptionParser = Result<Success> (SubsystemParser::*)(std::vector<std::string> && args);
179 
180     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 
193     auto parser = parser_map.FindFunction(args);
194 
195     if (!parser) return Error() << parser.error();
196 
197     return std::invoke(*parser, this, std::move(args));
198 }
199 
EndSection()200 Result<Success> SubsystemParser::EndSection() {
201     subsystems_->emplace_back(std::move(subsystem_));
202 
203     return Success();
204 }
205 
ParseConfig(const std::vector<std::string> & configs)206 UeventdConfiguration 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));
222     parser.AddSingleLineParser("modalias_handling",
223                                std::bind(ParseModaliasHandlingLine, _1,
224                                          &ueventd_configuration.enable_modalias_handling));
225     parser.AddSingleLineParser("uevent_socket_rcvbuf_size",
226                                std::bind(ParseUeventSocketRcvbufSizeLine, _1,
227                                          &ueventd_configuration.uevent_socket_rcvbuf_size));
228 
229     for (const auto& config : configs) {
230         parser.ParseConfig(config);
231     }
232 
233     return ueventd_configuration;
234 }
235 
236 }  // namespace init
237 }  // namespace android
238