blob: 1ca1715a792653182345fa8c9adb1420f85bce03 [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 Cherry4233ec72019-09-06 10:52:31 -070091Result<void> ParseEnabledDisabledLine(std::vector<std::string>&& args, bool* feature) {
Tom Cherry457e28f2018-08-01 13:12:20 -070092 if (args.size() != 2) {
Tom Cherry4233ec72019-09-06 10:52:31 -070093 return Error() << args[0] << " lines take exactly one parameter";
Tom Cherry457e28f2018-08-01 13:12:20 -070094 }
95
96 if (args[1] == "enabled") {
Tom Cherry4233ec72019-09-06 10:52:31 -070097 *feature = true;
Tom Cherry457e28f2018-08-01 13:12:20 -070098 } else if (args[1] == "disabled") {
Tom Cherry4233ec72019-09-06 10:52:31 -070099 *feature = false;
Tom Cherry457e28f2018-08-01 13:12:20 -0700100 } else {
Tom Cherry4233ec72019-09-06 10:52:31 -0700101 return Error() << args[0] << " takes either 'enabled' or 'disabled' as a parameter";
Tom Cherry457e28f2018-08-01 13:12:20 -0700102 }
103
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700104 return {};
Tom Cherry457e28f2018-08-01 13:12:20 -0700105}
106
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700107Result<void> ParseUeventSocketRcvbufSizeLine(std::vector<std::string>&& args,
108 size_t* uevent_socket_rcvbuf_size) {
Tom Cherrye2910102018-12-06 13:29:30 -0800109 if (args.size() != 2) {
110 return Error() << "uevent_socket_rcvbuf_size lines take exactly one parameter";
111 }
112
113 size_t parsed_size;
114 if (!ParseByteCount(args[1], &parsed_size)) {
115 return Error() << "could not parse size '" << args[1] << "' for uevent_socket_rcvbuf_line";
116 }
117
118 *uevent_socket_rcvbuf_size = parsed_size;
119
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700120 return {};
Tom Cherrye2910102018-12-06 13:29:30 -0800121}
122
Tom Cherry7421fa12018-07-13 15:32:02 -0700123class SubsystemParser : public SectionParser {
124 public:
125 SubsystemParser(std::vector<Subsystem>* subsystems) : subsystems_(subsystems) {}
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700126 Result<void> ParseSection(std::vector<std::string>&& args, const std::string& filename,
127 int line) override;
128 Result<void> ParseLineSection(std::vector<std::string>&& args, int line) override;
129 Result<void> EndSection() override;
Tom Cherry7421fa12018-07-13 15:32:02 -0700130
131 private:
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700132 Result<void> ParseDevName(std::vector<std::string>&& args);
133 Result<void> ParseDirName(std::vector<std::string>&& args);
Tom Cherry7421fa12018-07-13 15:32:02 -0700134
135 Subsystem subsystem_;
136 std::vector<Subsystem>* subsystems_;
137};
138
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700139Result<void> SubsystemParser::ParseSection(std::vector<std::string>&& args,
140 const std::string& filename, int line) {
Tom Cherryed506f72017-05-25 15:58:59 -0700141 if (args.size() != 2) {
Tom Cherry89bcc852017-08-02 17:01:36 -0700142 return Error() << "subsystems must have exactly one name";
Tom Cherryed506f72017-05-25 15:58:59 -0700143 }
144
145 if (std::find(subsystems_->begin(), subsystems_->end(), args[1]) != subsystems_->end()) {
Tom Cherry89bcc852017-08-02 17:01:36 -0700146 return Error() << "ignoring duplicate subsystem entry";
Tom Cherryed506f72017-05-25 15:58:59 -0700147 }
148
Tom Cherry9c8d6dd2017-08-17 09:38:01 -0700149 subsystem_ = Subsystem(std::move(args[1]));
Tom Cherryed506f72017-05-25 15:58:59 -0700150
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700151 return {};
Tom Cherryed506f72017-05-25 15:58:59 -0700152}
153
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700154Result<void> SubsystemParser::ParseDevName(std::vector<std::string>&& args) {
Tom Cherryed506f72017-05-25 15:58:59 -0700155 if (args[1] == "uevent_devname") {
Tom Cherry5f0198b2018-07-17 15:28:16 -0700156 subsystem_.devname_source_ = Subsystem::DEVNAME_UEVENT_DEVNAME;
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700157 return {};
Tom Cherryed506f72017-05-25 15:58:59 -0700158 }
159 if (args[1] == "uevent_devpath") {
Tom Cherry5f0198b2018-07-17 15:28:16 -0700160 subsystem_.devname_source_ = Subsystem::DEVNAME_UEVENT_DEVPATH;
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700161 return {};
Tom Cherryed506f72017-05-25 15:58:59 -0700162 }
163
Tom Cherry89bcc852017-08-02 17:01:36 -0700164 return Error() << "invalid devname '" << args[1] << "'";
Tom Cherryed506f72017-05-25 15:58:59 -0700165}
166
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700167Result<void> SubsystemParser::ParseDirName(std::vector<std::string>&& args) {
Tom Cherryed506f72017-05-25 15:58:59 -0700168 if (args[1].front() != '/') {
Tom Cherry89bcc852017-08-02 17:01:36 -0700169 return Error() << "dirname '" << args[1] << " ' does not start with '/'";
Tom Cherryed506f72017-05-25 15:58:59 -0700170 }
171
172 subsystem_.dir_name_ = args[1];
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700173 return {};
Tom Cherryed506f72017-05-25 15:58:59 -0700174}
175
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700176Result<void> SubsystemParser::ParseLineSection(std::vector<std::string>&& args, int line) {
177 using OptionParser = Result<void> (SubsystemParser::*)(std::vector<std::string> && args);
Tom Cherryd52a5b32019-07-22 16:05:36 -0700178 // clang-format off
179 static const KeywordMap<OptionParser> parser_map = {
180 {"devname", {1, 1, &SubsystemParser::ParseDevName}},
181 {"dirname", {1, 1, &SubsystemParser::ParseDirName}},
182 };
183 // clang-format on
Tom Cherry89bcc852017-08-02 17:01:36 -0700184
Tom Cherryd52a5b32019-07-22 16:05:36 -0700185 auto parser = parser_map.Find(args);
Tom Cherryed506f72017-05-25 15:58:59 -0700186
Tom Cherry89bcc852017-08-02 17:01:36 -0700187 if (!parser) return Error() << parser.error();
Tom Cherryed506f72017-05-25 15:58:59 -0700188
Tom Cherry89bcc852017-08-02 17:01:36 -0700189 return std::invoke(*parser, this, std::move(args));
Tom Cherryed506f72017-05-25 15:58:59 -0700190}
191
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700192Result<void> SubsystemParser::EndSection() {
Tom Cherryed506f72017-05-25 15:58:59 -0700193 subsystems_->emplace_back(std::move(subsystem_));
Steven Moreland7d0a5c32017-11-10 14:20:47 -0800194
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700195 return {};
Tom Cherryed506f72017-05-25 15:58:59 -0700196}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700197
Tom Cherry7421fa12018-07-13 15:32:02 -0700198UeventdConfiguration ParseConfig(const std::vector<std::string>& configs) {
199 Parser parser;
200 UeventdConfiguration ueventd_configuration;
201
202 parser.AddSectionParser("subsystem",
203 std::make_unique<SubsystemParser>(&ueventd_configuration.subsystems));
204
205 using namespace std::placeholders;
206 parser.AddSingleLineParser(
207 "/sys/",
208 std::bind(ParsePermissionsLine, _1, &ueventd_configuration.sysfs_permissions, nullptr));
209 parser.AddSingleLineParser("/dev/", std::bind(ParsePermissionsLine, _1, nullptr,
210 &ueventd_configuration.dev_permissions));
211 parser.AddSingleLineParser("firmware_directories",
212 std::bind(ParseFirmwareDirectoriesLine, _1,
213 &ueventd_configuration.firmware_directories));
Tom Cherry457e28f2018-08-01 13:12:20 -0700214 parser.AddSingleLineParser("modalias_handling",
Tom Cherry4233ec72019-09-06 10:52:31 -0700215 std::bind(ParseEnabledDisabledLine, _1,
Tom Cherry457e28f2018-08-01 13:12:20 -0700216 &ueventd_configuration.enable_modalias_handling));
Tom Cherrye2910102018-12-06 13:29:30 -0800217 parser.AddSingleLineParser("uevent_socket_rcvbuf_size",
218 std::bind(ParseUeventSocketRcvbufSizeLine, _1,
219 &ueventd_configuration.uevent_socket_rcvbuf_size));
Tom Cherry4233ec72019-09-06 10:52:31 -0700220 parser.AddSingleLineParser("parallel_restorecon",
221 std::bind(ParseEnabledDisabledLine, _1,
222 &ueventd_configuration.enable_parallel_restorecon));
Tom Cherry7421fa12018-07-13 15:32:02 -0700223
224 for (const auto& config : configs) {
225 parser.ParseConfig(config);
226 }
227
228 return ueventd_configuration;
229}
230
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700231} // namespace init
232} // namespace android