2017-05-26 00:58:59 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ueventd_parser.h"
|
|
|
|
|
|
|
|
#include <grp.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
|
|
|
|
#include "keyword_map.h"
|
|
|
|
|
2017-06-22 21:53:17 +02:00
|
|
|
namespace android {
|
|
|
|
namespace init {
|
|
|
|
|
2017-08-03 02:01:36 +02:00
|
|
|
Result<Success> ParsePermissionsLine(std::vector<std::string>&& args,
|
|
|
|
std::vector<SysfsPermissions>* out_sysfs_permissions,
|
|
|
|
std::vector<Permissions>* out_dev_permissions) {
|
2017-05-26 00:58:59 +02:00
|
|
|
bool is_sysfs = out_sysfs_permissions != nullptr;
|
|
|
|
if (is_sysfs && args.size() != 5) {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << "/sys/ lines must have 5 entries";
|
2017-05-26 00:58:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_sysfs && args.size() != 4) {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << "/dev/ lines must have 4 entries";
|
2017-05-26 00:58:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto it = args.begin();
|
|
|
|
const std::string& name = *it++;
|
|
|
|
|
|
|
|
std::string sysfs_attribute;
|
|
|
|
if (is_sysfs) sysfs_attribute = *it++;
|
|
|
|
|
|
|
|
// args is now common to both sys and dev entries and contains: <perm> <uid> <gid>
|
|
|
|
std::string& perm_string = *it++;
|
|
|
|
char* end_pointer = 0;
|
|
|
|
mode_t perm = strtol(perm_string.c_str(), &end_pointer, 8);
|
|
|
|
if (end_pointer == nullptr || *end_pointer != '\0') {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << "invalid mode '" << perm_string << "'";
|
2017-05-26 00:58:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string& uid_string = *it++;
|
|
|
|
passwd* pwd = getpwnam(uid_string.c_str());
|
|
|
|
if (!pwd) {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << "invalid uid '" << uid_string << "'";
|
2017-05-26 00:58:59 +02:00
|
|
|
}
|
|
|
|
uid_t uid = pwd->pw_uid;
|
|
|
|
|
|
|
|
std::string& gid_string = *it++;
|
|
|
|
struct group* grp = getgrnam(gid_string.c_str());
|
|
|
|
if (!grp) {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << "invalid gid '" << gid_string << "'";
|
2017-05-26 00:58:59 +02:00
|
|
|
}
|
|
|
|
gid_t gid = grp->gr_gid;
|
|
|
|
|
|
|
|
if (is_sysfs) {
|
|
|
|
out_sysfs_permissions->emplace_back(name, sysfs_attribute, perm, uid, gid);
|
|
|
|
} else {
|
|
|
|
out_dev_permissions->emplace_back(name, perm, uid, gid);
|
|
|
|
}
|
2017-08-03 02:01:36 +02:00
|
|
|
return Success();
|
2017-05-26 00:58:59 +02:00
|
|
|
}
|
|
|
|
|
2017-08-03 02:01:36 +02:00
|
|
|
Result<Success> SubsystemParser::ParseSection(std::vector<std::string>&& args,
|
|
|
|
const std::string& filename, int line) {
|
2017-05-26 00:58:59 +02:00
|
|
|
if (args.size() != 2) {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << "subsystems must have exactly one name";
|
2017-05-26 00:58:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (std::find(subsystems_->begin(), subsystems_->end(), args[1]) != subsystems_->end()) {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << "ignoring duplicate subsystem entry";
|
2017-05-26 00:58:59 +02:00
|
|
|
}
|
|
|
|
|
2017-08-17 18:38:01 +02:00
|
|
|
subsystem_ = Subsystem(std::move(args[1]));
|
2017-05-26 00:58:59 +02:00
|
|
|
|
2017-08-03 02:01:36 +02:00
|
|
|
return Success();
|
2017-05-26 00:58:59 +02:00
|
|
|
}
|
|
|
|
|
2017-08-03 02:01:36 +02:00
|
|
|
Result<Success> SubsystemParser::ParseDevName(std::vector<std::string>&& args) {
|
2017-05-26 00:58:59 +02:00
|
|
|
if (args[1] == "uevent_devname") {
|
|
|
|
subsystem_.devname_source_ = Subsystem::DevnameSource::DEVNAME_UEVENT_DEVNAME;
|
2017-08-03 02:01:36 +02:00
|
|
|
return Success();
|
2017-05-26 00:58:59 +02:00
|
|
|
}
|
|
|
|
if (args[1] == "uevent_devpath") {
|
|
|
|
subsystem_.devname_source_ = Subsystem::DevnameSource::DEVNAME_UEVENT_DEVPATH;
|
2017-08-03 02:01:36 +02:00
|
|
|
return Success();
|
2017-05-26 00:58:59 +02:00
|
|
|
}
|
|
|
|
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << "invalid devname '" << args[1] << "'";
|
2017-05-26 00:58:59 +02:00
|
|
|
}
|
|
|
|
|
2017-08-03 02:01:36 +02:00
|
|
|
Result<Success> SubsystemParser::ParseDirName(std::vector<std::string>&& args) {
|
2017-05-26 00:58:59 +02:00
|
|
|
if (args[1].front() != '/') {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << "dirname '" << args[1] << " ' does not start with '/'";
|
2017-05-26 00:58:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
subsystem_.dir_name_ = args[1];
|
2017-08-03 02:01:36 +02:00
|
|
|
return Success();
|
2017-05-26 00:58:59 +02:00
|
|
|
}
|
|
|
|
|
2017-08-03 02:01:36 +02:00
|
|
|
Result<Success> SubsystemParser::ParseLineSection(std::vector<std::string>&& args, int line) {
|
|
|
|
using OptionParser = Result<Success> (SubsystemParser::*)(std::vector<std::string> && args);
|
|
|
|
|
2017-05-26 00:58:59 +02:00
|
|
|
static class OptionParserMap : public KeywordMap<OptionParser> {
|
|
|
|
private:
|
|
|
|
const Map& map() const override {
|
|
|
|
// clang-format off
|
|
|
|
static const Map option_parsers = {
|
|
|
|
{"devname", {1, 1, &SubsystemParser::ParseDevName}},
|
|
|
|
{"dirname", {1, 1, &SubsystemParser::ParseDirName}},
|
|
|
|
};
|
|
|
|
// clang-format on
|
|
|
|
return option_parsers;
|
|
|
|
}
|
|
|
|
} parser_map;
|
|
|
|
|
2017-08-03 02:01:36 +02:00
|
|
|
auto parser = parser_map.FindFunction(args);
|
2017-05-26 00:58:59 +02:00
|
|
|
|
2017-08-03 02:01:36 +02:00
|
|
|
if (!parser) return Error() << parser.error();
|
2017-05-26 00:58:59 +02:00
|
|
|
|
2017-08-03 02:01:36 +02:00
|
|
|
return std::invoke(*parser, this, std::move(args));
|
2017-05-26 00:58:59 +02:00
|
|
|
}
|
|
|
|
|
2017-11-10 23:20:47 +01:00
|
|
|
Result<Success> SubsystemParser::EndSection() {
|
2017-05-26 00:58:59 +02:00
|
|
|
subsystems_->emplace_back(std::move(subsystem_));
|
2017-11-10 23:20:47 +01:00
|
|
|
|
|
|
|
return Success();
|
2017-05-26 00:58:59 +02:00
|
|
|
}
|
2017-06-22 21:53:17 +02:00
|
|
|
|
|
|
|
} // namespace init
|
|
|
|
} // namespace android
|